em.c 6.2 KB

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