em.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* $Header$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. #include "sizes.h"
  7. #include "Lpars.h"
  8. #include <em.h>
  9. #include "em.h"
  10. /* This file is used to shield code.c as much as possible from em dependant
  11. * details. It introduces some call overhead but not enough for a coffee
  12. * break. (Sorry)
  13. * Note that functions with a leading upper case letter normally decide between
  14. * word or double word arith.
  15. */
  16. int wz = 4, pz = 4, vz = 4;
  17. int Lflag;
  18. static Lab=0;
  19. char *malloc();
  20. void init()
  21. {
  22. C_init((arith) wz, (arith) pz);
  23. }
  24. void openfile(file) char *file;
  25. {
  26. if (C_open(file) < 0) {
  27. fatal("Could not open output file");
  28. }
  29. }
  30. void meswp()
  31. {
  32. C_ms_emx((arith) wz, (arith) pz);
  33. }
  34. void maxdes()
  35. {
  36. C_df_dnam("maxcdes");
  37. rom(wz, 0L); rom(wz, -1L); rom(wz, (long) (wz+pz));
  38. C_df_dnam("maxwdes");
  39. rom(wz, 0L); rom(wz, -1L); rom(wz, (long) vz);
  40. C_df_dnam("maxbdes");
  41. rom(wz, 0L); rom(wz, -1L); rom(wz, 1L);
  42. }
  43. int new_label(L) register *L;
  44. {
  45. if (*L==0) *L= ++Lab;
  46. return *L;
  47. }
  48. void Label(L) register L;
  49. {
  50. if (L!=0) C_df_ilb((label) L);
  51. }
  52. static Dot_label=0;
  53. int new_dot_label(L) int *L;
  54. {
  55. return *L= ++Dot_label;
  56. }
  57. void dot_label(L) int L;
  58. {
  59. C_df_dlb((label) L);
  60. }
  61. void branch(L) int *L;
  62. {
  63. C_bra((label) new_label(L));
  64. }
  65. char *proc_label(L, name) register L; register char *name;
  66. {
  67. static char *lab=nil;
  68. register char *n;
  69. if (lab!=nil) free(lab);
  70. lab=malloc(strlen(name)+(1+sizeof(int)*3+1));
  71. /* That is: P<L><name>\0 */
  72. sprint(lab, "P%d", L);
  73. n=lab+strlen(lab);
  74. while (*name!=0) {
  75. *n++ = *name=='.' ? '_' : *name;
  76. name++;
  77. }
  78. *n=0;
  79. return lab;
  80. }
  81. void magic() /* magic? should be called invisible */
  82. {
  83. C_magic();
  84. }
  85. void cwv()
  86. {
  87. if (vz>wz) {
  88. C_loc((arith) wz);
  89. C_loc((arith) vz);
  90. C_cii();
  91. }
  92. }
  93. void cvw()
  94. {
  95. if (vz>wz) {
  96. C_loc((arith) vz);
  97. C_loc((arith) wz);
  98. C_cii();
  99. }
  100. }
  101. void Loc(const) long const;
  102. {
  103. if (vz>wz) C_ldc((arith) const); else C_loc((arith) const);
  104. }
  105. void Lol(offset) int offset;
  106. {
  107. if (vz>wz) C_ldl((arith) offset); else C_lol((arith) offset);
  108. }
  109. void Lolp(offset) int offset;
  110. {
  111. if (pz>wz) C_ldl((arith) offset); else C_lol((arith) offset);
  112. }
  113. void Lil(offset) register offset;
  114. {
  115. if (vz>wz) {
  116. Lolp(offset);
  117. C_loi((arith) vz);
  118. } else
  119. C_lil((arith) offset);
  120. }
  121. void Lof(offset) int offset;
  122. {
  123. if (vz>wz) C_ldf((arith) offset); else C_lof((arith) offset);
  124. }
  125. void Lofp(offset) int offset;
  126. {
  127. if (pz>wz) C_ldf((arith) offset); else C_lof((arith) offset);
  128. }
  129. void Lif(offset) register offset;
  130. {
  131. Lofp(offset);
  132. C_loi((arith) vz);
  133. }
  134. void Stl(offset) int offset;
  135. {
  136. if (vz>wz) C_sdl((arith) offset); else C_stl((arith) offset);
  137. }
  138. void Inl(offset) register offset;
  139. {
  140. if (vz>wz) {
  141. C_ldl((arith) offset);
  142. C_ldc((arith) 1);
  143. C_adi((arith) vz);
  144. C_sdl((arith) offset);
  145. } else
  146. C_inl((arith) offset);
  147. }
  148. void Del(offset) register offset;
  149. {
  150. if (vz>wz) {
  151. C_ldl((arith) offset);
  152. C_ldc((arith) 1);
  153. C_sbi((arith) vz);
  154. C_sdl((arith) offset);
  155. } else
  156. C_del((arith) offset);
  157. }
  158. void Loe(name, offset) char *name; int offset;
  159. {
  160. if (vz>wz)
  161. C_lde_dnam(name, (arith) offset);
  162. else
  163. C_loe_dnam(name, (arith) offset);
  164. }
  165. typedef int (*pfi)();
  166. static int operators[]= { '<', '>', '=', GE, LE, NE };
  167. extern C_blt(), C_bgt(), C_beq(), C_bge(), C_ble(), C_bne();
  168. extern C_tlt(), C_tgt(), C_teq(), C_tge(), C_tle(), C_tne();
  169. extern C_zlt(), C_zgt(), C_zeq(), C_zge(), C_zle(), C_zne();
  170. static pfi C_bxx[]= { C_blt, C_bgt, C_beq, C_bge, C_ble, C_bne };
  171. static pfi C_txx[]= { C_tlt, C_tgt, C_teq, C_tge, C_tle, C_tne };
  172. static pfi C_zxx[]= { C_zlt, C_zgt, C_zeq, C_zge, C_zle, C_zne };
  173. void bxx(pos, op, L) register pos, op, L;
  174. {
  175. register i;
  176. if (op==AFTER) {
  177. C_sbi((arith) vz);
  178. if (vz>wz) {
  179. C_ldc((arith) 0);
  180. C_cmi((arith) vz);
  181. }
  182. if (pos) C_zle((label) L); else C_zgt((label) L);
  183. } else {
  184. for (i=0; operators[i]!=op; i++) ;
  185. if (pos && (i+=3)>=6) i-=6;
  186. if (vz>wz) {
  187. C_cmi((arith) vz);
  188. (C_zxx[i])((label) L);
  189. } else {
  190. (C_bxx[i])((label) L);
  191. }
  192. }
  193. }
  194. void Txx(op) register int op;
  195. {
  196. register i;
  197. for (i=0; operators[i]!=op; i++) ;
  198. (C_txx[i])();
  199. cwv();
  200. C_ngi((arith) vz);
  201. }
  202. extern C_adi(), C_sbi(), C_mli(), C_dvi(), C_rmi();
  203. void xxi(op) register op;
  204. {
  205. static int operators[]= { '+', '-', '*', '/', BS };
  206. static pfi C_xxi[]= { C_adi, C_sbi, C_mli, C_dvi, C_rmi };
  207. register i;
  208. for (i=0; operators[i]!=op; i++) ;
  209. (C_xxi[i])((arith) vz);
  210. }
  211. void aar() { C_aar((arith) wz); }
  212. void adp(offset) int offset; { C_adp((arith) offset); }
  213. void and() { C_and((arith) vz); }
  214. void asp(size) int size; { C_asp((arith) size); }
  215. void blm(size) int size; { C_blm((arith) size); }
  216. void blt(lab) int lab; { C_blt((label) lab); }
  217. void cal(lab) char *lab; { C_cal(lab); }
  218. void cmi() { C_cmi((arith) vz); }
  219. void com() { C_com((arith) vz); }
  220. void del(offset) int offset; { C_del((arith) offset); }
  221. void _end(size) int size; { C_end((arith) size); }
  222. void exp(lab) char *lab; { C_exp(lab); }
  223. void ior() { C_ior((arith) vz); }
  224. void lae(lab, offset) char *lab; int offset;
  225. { C_lae_dnam(lab, (arith) offset); }
  226. void laedot(lab) int lab; { C_lae_dlb((label) lab, (arith) 0); }
  227. void lal(offset) int offset; { C_lal((arith) offset); }
  228. void lar() { C_lar((arith) wz); }
  229. void ldc0() { C_ldc((arith) 0); }
  230. void ldl(offset) int offset; { C_ldl((arith) offset); }
  231. void lfr(size) int size; { C_lfr((arith) size); }
  232. void loc(cst) int cst; { C_loc((arith) cst); }
  233. void loi(size) int size; { C_loi((arith) size); }
  234. void lol(offset) int offset; { C_lol((arith) offset); }
  235. void lor0() { C_lor((arith) 0); }
  236. void lxa(offset) int offset; { C_lxa((arith) offset); }
  237. void lxl(offset) int offset; { C_lxl((arith) offset); }
  238. void meserr() { C_ms_err(); }
  239. void ngi() { C_ngi((arith) vz); }
  240. void pro(lab) char *lab; { C_pro_narg(lab); }
  241. void ret(size) int size; { C_ret((arith) size); }
  242. void init_rt() { C_cal("init"); }
  243. void sli() { C_sli((arith) vz); }
  244. void sri() { C_sri((arith) vz); }
  245. void ste(lab, offset) char *lab; int offset;
  246. { C_ste_dnam(lab, (arith) offset); }
  247. void sti(size) int size; { C_sti((arith) size); }
  248. void stl(offset) int offset; { C_stl((arith) offset); }
  249. void trp() { C_trp(); }
  250. void tst() { /* No flags in EM */ }
  251. void xor() { C_xor((arith) vz); }
  252. void zeq(lab) int lab; { C_zeq((label) lab); }
  253. void zgt(lab) int lab; { C_zgt((label) lab); }
  254. void zlt(lab) int lab; { C_zlt((label) lab); }
  255. void zne(lab) int lab; { C_zne((label) lab); }
  256. char *itoa(i) long i;
  257. {
  258. static char a[sizeof(long)*3];
  259. sprint(a, "%ld", i);
  260. return a;
  261. }
  262. void rom(size, c) int size; long c;
  263. {
  264. C_rom_icon(itoa(c), (arith) size);
  265. }
  266. void lin()
  267. {
  268. static oldline=0;
  269. extern yylineno;
  270. if (Lflag) return;
  271. if (yylineno!=oldline)
  272. C_lin((arith) (oldline=yylineno));
  273. }
  274. static struct ftree {
  275. char *file;
  276. int lab;
  277. struct ftree *left, *right;
  278. } std_f = { "stdin", 0, nil, nil }, *curr_f= &std_f, *main_f=nil;
  279. char *curr_file="stdin";
  280. static void do_fil(f) struct ftree *f;
  281. {
  282. if (Lflag) return;
  283. if (f->lab==0) {
  284. dot_label(new_dot_label(&f->lab));
  285. C_rom_scon(f->file, (arith) (strlen(f->file)+1));
  286. }
  287. C_fil_dlb((label) f->lab);
  288. }
  289. void fil()
  290. {
  291. do_fil(curr_f);
  292. }
  293. void main_fil()
  294. {
  295. do_fil(main_f==nil ? &std_f : main_f);
  296. }
  297. int set_file(f) char *f;
  298. {
  299. char *strcpy();
  300. static struct ftree *ftop=nil;
  301. register struct ftree *pf, **apf= &ftop;
  302. register cmp;
  303. while ((pf= *apf)!=nil && (cmp=strcmp(f, pf->file))!=0)
  304. apf= cmp<0 ? &pf->left : &pf->right;
  305. if (pf==nil) {
  306. *apf= pf= (struct ftree *) malloc(sizeof *pf);
  307. pf->file=strcpy(malloc(strlen(f)+1), f);
  308. pf->lab=0;
  309. pf->left=pf->right=nil;
  310. }
  311. curr_f=pf;
  312. curr_file=pf->file;
  313. if (main_f==nil) {
  314. main_f=curr_f;
  315. return 0;
  316. } else
  317. return curr_f!=main_f;
  318. }
  319. void par_begin()
  320. {
  321. C_lal((arith) curr_offset);
  322. C_cal("pc_begin");
  323. C_asp((arith) pz);
  324. }
  325. void par_fork(NONZERO) int *NONZERO;
  326. {
  327. C_cal("pc_fork");
  328. C_lfr((arith) wz);
  329. C_zne((label) new_label(NONZERO));
  330. }
  331. void resumenext()
  332. {
  333. C_cal("resumene");
  334. }
  335. void no_deadlock()
  336. {
  337. C_zre_dnam("deadlock", (arith) 0);
  338. }
  339. void par_end()
  340. {
  341. C_cal("parend");
  342. }
  343. void closefile()
  344. {
  345. C_close();
  346. }