em.c 6.2 KB

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