em.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* $Header$ */
  2. /* EM CODE OUTPUT ROUTINES */
  3. #define CMODE 0644
  4. #define MAX_ARG_CNT 32
  5. #include "em.h"
  6. #include <system.h>
  7. #include "arith.h"
  8. #include "label.h"
  9. /*
  10. putbyte(), C_open() and C_close() are the basic routines for
  11. respectively write on, open and close the output file.
  12. The put_*() functions serve as formatting functions of the
  13. various EM language constructs.
  14. See "Description of a Machine Architecture for use with
  15. Block Structured Languages" par. 11.2 for the meaning of these
  16. names.
  17. */
  18. /* supply a kind of buffered output */
  19. #define flush(x) sys_write(ofp, &obuf[0], x)
  20. static char obuf[BUFSIZ];
  21. static char *opp = &obuf[0];
  22. File *ofp = 0;
  23. putbyte(b) /* shouldn't putbyte() be a macro ??? (EB) */
  24. int b;
  25. {
  26. if (opp >= &obuf[BUFSIZ]) { /* flush if buffer overflows */
  27. if (flush(BUFSIZ) == 0)
  28. sys_stop(S_ABORT);
  29. opp = &obuf[0];
  30. }
  31. *opp++ = (char) b;
  32. }
  33. C_init(wsize, psize)
  34. arith wsize, psize;
  35. {}
  36. C_open(nm) /* open file for compact code output */
  37. char *nm;
  38. {
  39. if (nm == 0)
  40. ofp = STDOUT; /* standard output */
  41. else
  42. if (sys_open(nm, OP_WRITE, &ofp) == 0)
  43. return 0;
  44. return 1;
  45. }
  46. C_close()
  47. {
  48. if (flush(opp - &obuf[0]) == 0)
  49. sys_stop(S_ABORT);
  50. opp = obuf; /* reset opp */
  51. if (ofp != STDOUT)
  52. sys_close(ofp);
  53. ofp = 0;
  54. }
  55. C_busy()
  56. {
  57. return ofp != 0; /* true if code is being generated */
  58. }
  59. /*** the compact code generating routines ***/
  60. #define fit16i(x) ((x) >= (long)0xFFFF8000 && (x) <= (long)0x00007FFF)
  61. #define fit8u(x) ((x) <= 0xFF) /* x is already unsigned */
  62. put_ilb(l)
  63. label l;
  64. {
  65. if (fit8u(l)) {
  66. put8(sp_ilb1);
  67. put8((int)l);
  68. }
  69. else {
  70. put8(sp_ilb2);
  71. put16(l);
  72. }
  73. }
  74. put_dlb(l)
  75. label l;
  76. {
  77. if (fit8u(l)) {
  78. put8(sp_dlb1);
  79. put8((int)l);
  80. }
  81. else {
  82. put8(sp_dlb2);
  83. put16(l);
  84. }
  85. }
  86. put_cst(l)
  87. arith l;
  88. {
  89. if (l >= (arith) -sp_zcst0 && l < (arith) (sp_ncst0 - sp_zcst0)) {
  90. /* we can convert 'l' to an int because its value
  91. can be stored in a byte.
  92. */
  93. put8((int) l + (sp_zcst0 + sp_fcst0));
  94. }
  95. else
  96. if (fit16i(l)) { /* the cast from long to int causes no trouble here */
  97. put8(sp_cst2);
  98. put16((int) l);
  99. }
  100. else {
  101. put8(sp_cst4);
  102. put32(l);
  103. }
  104. }
  105. put_doff(l, v)
  106. label l;
  107. arith v;
  108. {
  109. if (v == 0)
  110. put_dlb(l);
  111. else {
  112. put8(sp_doff);
  113. put_dlb(l);
  114. put_cst(v);
  115. }
  116. }
  117. put_noff(s, v)
  118. char *s;
  119. arith v;
  120. {
  121. if (v == 0)
  122. put_dnam(s);
  123. else {
  124. put8(sp_doff);
  125. put_dnam(s);
  126. put_cst(v);
  127. }
  128. }
  129. put_dnam(s)
  130. char *s;
  131. {
  132. put8(sp_dnam);
  133. put_str(s);
  134. }
  135. put_pnam(s)
  136. char *s;
  137. {
  138. put8(sp_pnam);
  139. put_str(s);
  140. }
  141. #ifdef ____
  142. put_fcon(s, sz)
  143. char *s;
  144. arith sz;
  145. {
  146. put8(sp_fcon);
  147. put_cst(sz);
  148. put_str(s);
  149. }
  150. #endif ____
  151. put_wcon(sp, v, sz) /* sp_icon, sp_ucon or sp_fcon with int repr */
  152. int sp;
  153. char *v;
  154. arith sz;
  155. {
  156. /* how 'bout signextension int --> long ??? */
  157. put8(sp);
  158. put_cst(sz);
  159. put_str(v);
  160. }
  161. put_str(s)
  162. char *s;
  163. {
  164. register int len;
  165. put_cst((arith) (len = strlen(s)));
  166. while (--len >= 0)
  167. put8(*s++);
  168. }
  169. put_cstr(s)
  170. char *s;
  171. {
  172. register int len = prepare_string(s);
  173. put8(sp_scon);
  174. put_cst((arith) len);
  175. while (--len >= 0)
  176. put8(*s++);
  177. }