em.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* $Id$ */
  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. /* EM CODE OUTPUT ROUTINES */
  7. /* I/O part of em_code module.
  8. Also contains C_open, C_close
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "alloc.h"
  14. #include "em_arith.h"
  15. #include "insert.h"
  16. #include "em_private.h"
  17. int C_ontmpfile = 0;
  18. int C_sequential = 1;
  19. Part *C_curr_part;
  20. int (*C_outpart)(), (*C_swtout)(), (*C_swttmp)();
  21. #ifdef INCORE
  22. char *C_BASE;
  23. #endif
  24. File *C_ofp;
  25. #ifndef INCORE
  26. File *C_tfr;
  27. char *C_tmpfile;
  28. char *strcpy(), *strcat();
  29. char *C_ibuf = 0;
  30. long C_current_out;
  31. #endif
  32. #if BUFSIZ <= 1024 && BIGMACHINE
  33. #define BUFFERSIZ 8*BUFSIZ
  34. #else
  35. #define BUFFERSIZ BUFSIZ
  36. #endif
  37. static char obuf[BUFFERSIZ];
  38. char *C_top = &obuf[BUFFERSIZ];
  39. #ifdef INCORE
  40. char *C_current_out = obuf;
  41. #else
  42. char *C_opp = obuf;
  43. #endif
  44. void
  45. C_flush() {
  46. #ifdef INCORE
  47. static unsigned int bufsiz;
  48. if (C_ontmpfile) {
  49. if (C_BASE == 0) {
  50. C_BASE = Malloc(BUFFERSIZ);
  51. bufsiz = BUFFERSIZ;
  52. C_current_out = C_BASE;
  53. }
  54. else {
  55. C_BASE = Srealloc(C_BASE, (bufsiz << 1));
  56. C_current_out = C_BASE + bufsiz;
  57. bufsiz <<= 1;
  58. }
  59. C_top = C_BASE + bufsiz;
  60. return;
  61. }
  62. #endif
  63. if (C_opp != obuf && sys_write(C_ofp, obuf, (int)(C_opp - obuf)) == 0) {
  64. C_ofp = 0;
  65. C_failed();
  66. }
  67. C_opp = obuf;
  68. }
  69. #ifndef INCORE
  70. #define Xputbyte(c) if (C_ontmpfile) C_current_out++; put(c)
  71. #else
  72. #define Xputbyte(c) put(c)
  73. #endif
  74. void
  75. C_putbyte(c)
  76. int c;
  77. {
  78. Xputbyte(c);
  79. }
  80. #if BIGMACHINE
  81. #define C_putbyte Xputbyte
  82. #endif
  83. /*ARGSUSED*/
  84. void
  85. C_init(w, p)
  86. arith w, p;
  87. {
  88. }
  89. int
  90. C_open(nm)
  91. char *nm;
  92. {
  93. /* Open file "nm" for output
  94. */
  95. if (nm == 0)
  96. C_ofp = STDOUT; /* standard output */
  97. else
  98. if (sys_open(nm, OP_WRITE, &C_ofp) == 0)
  99. return 0;
  100. return 1;
  101. }
  102. void
  103. C_close()
  104. {
  105. /* Finish the code-generation.
  106. */
  107. #ifndef INCORE
  108. C_flush();
  109. if (C_tmpfile) {
  110. (*C_swttmp)();
  111. sys_close(C_ofp);
  112. #else
  113. if (C_BASE) {
  114. #endif
  115. if (C_curr_part) {
  116. C_curr_part->p_parts->pp_end = C_current_out - C_BASE;
  117. }
  118. (*C_swtout)();
  119. if (! C_sequential) {
  120. (*C_outpart)(0);
  121. }
  122. #ifndef INCORE
  123. sys_close(C_tfr);
  124. sys_remove(C_tmpfile);
  125. if (C_ibuf) free(C_ibuf);
  126. #else
  127. free(C_BASE);
  128. #endif
  129. }
  130. C_flush();
  131. if (C_ofp != STDOUT)
  132. sys_close(C_ofp);
  133. C_ofp = 0;
  134. }
  135. int
  136. C_busy()
  137. {
  138. return C_ofp != 0; /* true if code is being generated */
  139. }
  140. #ifdef READABLE_EM
  141. /*
  142. The C_pt_*() functions serve as formatting functions of the
  143. various EM language constructs.
  144. See "Description of a Machine Architecture for use with
  145. Block Structured Languages" par. 11.2 for the meaning of these
  146. names.
  147. */
  148. void
  149. C_magic()
  150. {
  151. }
  152. /*** the readable code generating routines ***/
  153. static
  154. wrs(s)
  155. register char *s;
  156. {
  157. while (*s) {
  158. C_putbyte(*s++);
  159. }
  160. }
  161. void
  162. C_pt_dnam(s)
  163. char *s;
  164. {
  165. wrs(s);
  166. }
  167. void
  168. C_pt_ilb(l)
  169. label l;
  170. {
  171. char buf[16];
  172. sprint(buf, "*%ld", (long) l);
  173. wrs(buf);
  174. }
  175. extern char em_mnem[][4];
  176. extern char em_pseu[][4];
  177. void
  178. C_pt_op(x)
  179. {
  180. C_putbyte(' ');
  181. wrs(em_mnem[x - sp_fmnem]);
  182. C_putbyte(' ');
  183. }
  184. void
  185. C_pt_cst(l)
  186. arith l;
  187. {
  188. char buf[16];
  189. sprint(buf, "%ld", (long) l);
  190. wrs(buf);
  191. }
  192. void
  193. C_pt_scon(x, y)
  194. char *x;
  195. arith y;
  196. {
  197. char xbuf[1024];
  198. register char *p;
  199. char *bts2str();
  200. C_putbyte('\'');
  201. p = bts2str(x, (int) y, xbuf);
  202. while (*p) {
  203. if (*p == '\'') {
  204. C_putbyte('\\');
  205. }
  206. C_putbyte(*p++);
  207. }
  208. C_putbyte('\'');
  209. }
  210. void
  211. C_pt_ps(x)
  212. {
  213. C_putbyte(' ');
  214. wrs(em_pseu[x - sp_fpseu]);
  215. C_putbyte(' ');
  216. }
  217. void
  218. C_pt_dlb(l)
  219. label l;
  220. {
  221. char buf[16];
  222. sprint(buf, ".%ld", (long) l);
  223. wrs(buf);
  224. }
  225. void
  226. C_pt_doff(l, v)
  227. label l;
  228. arith v;
  229. {
  230. char buf[16];
  231. C_pt_dlb(l);
  232. if (v != 0) {
  233. sprint(buf,"+%ld", (long) v);
  234. wrs(buf);
  235. }
  236. }
  237. void
  238. C_pt_noff(s, v)
  239. char *s;
  240. arith v;
  241. {
  242. char buf[16];
  243. wrs(s);
  244. if (v != 0) {
  245. sprint(buf,"+%ld", (long) v);
  246. wrs(buf);
  247. }
  248. }
  249. void
  250. C_pt_pnam(s)
  251. char *s;
  252. {
  253. C_putbyte('$');
  254. wrs(s);
  255. }
  256. void
  257. C_pt_dfilb(l)
  258. label l;
  259. {
  260. char buf[16];
  261. sprint(buf, "%ld", (long) l);
  262. wrs(buf);
  263. }
  264. void
  265. C_pt_wcon(sp, v, sz) /* sp_icon, sp_ucon or sp_fcon with int repr */
  266. int sp;
  267. char *v;
  268. arith sz;
  269. {
  270. int ch = sp == sp_icon ? 'I' : sp == sp_ucon ? 'U' : 'F';
  271. wrs(v);
  272. C_putbyte(ch);
  273. C_pt_cst(sz);
  274. }
  275. void
  276. C_pt_nl() {
  277. C_putbyte('\n');
  278. }
  279. void
  280. C_pt_comma() {
  281. C_putbyte(',');
  282. }
  283. void
  284. C_pt_ccend() {
  285. C_putbyte('?');
  286. }
  287. #else /* READABLE_EM */
  288. #define put8(x) C_putbyte(x)
  289. #define put16(x) put8((int) x); put8((int) (x >> 8))
  290. #define put32(x) put16((int) x); put16((int) (x >> 16))
  291. /*
  292. The C_pt_*() functions serve as formatting functions of the
  293. various EM language constructs.
  294. See "Description of a Machine Architecture for use with
  295. Block Structured Languages" par. 11.2 for the meaning of these
  296. names.
  297. */
  298. void
  299. C_magic()
  300. {
  301. put16(sp_magic);
  302. }
  303. /*** the compact code generating routines ***/
  304. #define fit16i(x) ((x) >= (long)(-0x8000) && (x) <= (long)0x7FFF)
  305. #define fit8u(x) ((x) <= 0xFF) /* x is already unsigned */
  306. void
  307. C_pt_ilb(l)
  308. register label l;
  309. {
  310. if (fit8u(l)) {
  311. put8(sp_ilb1);
  312. put8((int)l);
  313. }
  314. else {
  315. put8(sp_ilb2);
  316. put16((int)l);
  317. }
  318. }
  319. void
  320. C_pt_dlb(l)
  321. register label l;
  322. {
  323. if (fit8u(l)) {
  324. put8(sp_dlb1);
  325. put8((int)l);
  326. }
  327. else {
  328. put8(sp_dlb2);
  329. put16((int)l);
  330. }
  331. }
  332. void
  333. C_pt_cst(l)
  334. register arith l;
  335. {
  336. if (l >= (arith) -sp_zcst0 && l < (arith) (sp_ncst0 - sp_zcst0)) {
  337. /* we can convert 'l' to an int because its value
  338. can be stored in a byte.
  339. */
  340. put8((int)l + (sp_zcst0 + sp_fcst0));
  341. }
  342. else
  343. if (fit16i(l)) { /* the cast from long to int causes no trouble here */
  344. put8(sp_cst2);
  345. put16((int) l);
  346. }
  347. else {
  348. put8(sp_cst4);
  349. put32(l);
  350. }
  351. }
  352. void
  353. C_pt_doff(l, v)
  354. label l;
  355. arith v;
  356. {
  357. if (v == 0) {
  358. C_pt_dlb(l);
  359. }
  360. else {
  361. put8(sp_doff);
  362. C_pt_dlb(l);
  363. C_pt_cst(v);
  364. }
  365. }
  366. void
  367. C_pt_str(s)
  368. register char *s;
  369. {
  370. register int len;
  371. C_pt_cst((arith) (len = strlen(s)));
  372. while (--len >= 0) {
  373. put8(*s++);
  374. }
  375. }
  376. void
  377. C_pt_dnam(s)
  378. char *s;
  379. {
  380. put8(sp_dnam);
  381. C_pt_str(s);
  382. }
  383. void
  384. C_pt_noff(s, v)
  385. char *s;
  386. arith v;
  387. {
  388. if (v == 0) {
  389. C_pt_dnam(s);
  390. }
  391. else {
  392. put8(sp_doff);
  393. C_pt_dnam(s);
  394. C_pt_cst(v);
  395. }
  396. }
  397. void
  398. C_pt_pnam(s)
  399. char *s;
  400. {
  401. put8(sp_pnam);
  402. C_pt_str(s);
  403. }
  404. void
  405. C_pt_wcon(sp, v, sz) /* sp_icon, sp_ucon or sp_fcon with int repr */
  406. int sp;
  407. char *v;
  408. arith sz;
  409. {
  410. /* how 'bout signextension int --> long ??? */
  411. put8(sp);
  412. C_pt_cst(sz);
  413. C_pt_str(v);
  414. }
  415. void
  416. C_pt_scon(b, n)
  417. register char *b;
  418. register arith n;
  419. {
  420. put8(sp_scon);
  421. C_pt_cst(n);
  422. while (--n >= 0) {
  423. put8(*b++);
  424. }
  425. }
  426. #endif /* READABLE_EM */