ca_put.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. #include <stdio.h>
  7. #include <em_spec.h>
  8. #include <em_pseu.h>
  9. #include <em_mnem.h>
  10. #include <em_flag.h>
  11. #include <em_mes.h>
  12. #include "../share/types.h"
  13. #include "ca.h"
  14. #include "../share/debug.h"
  15. #include "../share/def.h"
  16. #include "../share/map.h"
  17. #include "../share/alloc.h"
  18. #define outbyte(b) putc(b,outfile)
  19. FILE *outfile;
  20. STATIC proc_p thispro;
  21. STATIC outinst(m) {
  22. outbyte( (byte) m );
  23. }
  24. STATIC coutshort(i) short i; {
  25. outbyte( (byte) (i&BMASK) );
  26. outbyte( (byte) (i>>8) );
  27. }
  28. STATIC coutint(i) short i; {
  29. if (i>= -sp_zcst0 && i< sp_ncst0-sp_zcst0)
  30. outbyte( (byte) (i+sp_zcst0+sp_fcst0) );
  31. else {
  32. outbyte( (byte) sp_cst2) ;
  33. coutshort(i);
  34. }
  35. }
  36. STATIC coutoff(off) offset off; {
  37. if ((short) off == off)
  38. coutint((short) off);
  39. else {
  40. outbyte( (byte) sp_cst4) ;
  41. coutshort( (short) (off&0177777L) );
  42. coutshort( (short) (off>>16) );
  43. }
  44. }
  45. STATIC outsym(s,t)
  46. char *s;
  47. int t;
  48. {
  49. register byte *p;
  50. register unsigned num;
  51. if (s[0] == '.') {
  52. num = atoi(&s[1]);
  53. if (num < 256) {
  54. outbyte( (byte) sp_dlb1) ;
  55. outbyte( (byte) (num) );
  56. } else {
  57. outbyte( (byte) sp_dlb2) ;
  58. coutshort((short) num);
  59. }
  60. } else {
  61. p= s;
  62. while (*p && p < &s[IDL])
  63. p++;
  64. num = p - s;
  65. outbyte( (byte) t);
  66. coutint((short) num);
  67. p = s;
  68. while (num--)
  69. outbyte( (byte) *p++ );
  70. }
  71. }
  72. STATIC outdsym(dbl)
  73. dblock_p dbl;
  74. {
  75. if (dnames[dbl->d_id]) outsym(dnames[dbl->d_id],sp_dnam);
  76. }
  77. STATIC outpsym(p)
  78. proc_p p;
  79. {
  80. outsym(pnames[p->p_id],sp_pnam);
  81. }
  82. STATIC outddef(id) short id; {
  83. dblock_p dbl;
  84. dbl = dmap[id];
  85. dbl->d_flags2 |= DF_SYMOUT;
  86. if (dbl->d_flags1 & DF_EXTERNAL) {
  87. outinst(ps_exa);
  88. outdsym(dbl);
  89. }
  90. }
  91. STATIC outpdef(p) proc_p p; {
  92. p->p_flags2 |= PF_SYMOUT;
  93. if (p->p_flags1 & PF_EXTERNAL) {
  94. outinst(ps_exp);
  95. outpsym(p);
  96. }
  97. }
  98. STATIC outdocc(obj) obj_p obj; {
  99. dblock_p dbl;
  100. dbl = obj->o_dblock;
  101. if ((dbl->d_flags2 & DF_SYMOUT) == 0) {
  102. dbl->d_flags2 |= DF_SYMOUT;
  103. if (dnames[dbl->d_id] != 0 &&
  104. (dbl->d_flags1 & DF_EXTERNAL) == 0) {
  105. outinst(ps_ina);
  106. outdsym(dbl);
  107. }
  108. }
  109. }
  110. STATIC outpocc(p) proc_p p; {
  111. if ((p->p_flags2 & PF_SYMOUT) == 0) {
  112. p->p_flags2 |= PF_SYMOUT;
  113. if ((p->p_flags1 & PF_EXTERNAL) == 0) {
  114. outinst(ps_inp);
  115. outpsym(p);
  116. }
  117. }
  118. }
  119. STATIC coutobject(obj)
  120. obj_p obj;
  121. {
  122. /* In general, an object is defined by a global data
  123. * label and an offset. There are two special cases:
  124. * the label is omitted if the object is part of the current
  125. * hol block; the offset is omitted if it is 0 and the label
  126. * was not omitted.
  127. */
  128. if (dnames[obj->o_dblock->d_id] == 0) {
  129. coutoff(obj->o_off);
  130. } else {
  131. if (obj->o_off == 0) {
  132. outdsym(obj->o_dblock);
  133. } else {
  134. outbyte((byte) sp_doff);
  135. outdsym(obj->o_dblock);
  136. coutoff(obj->o_off);
  137. }
  138. }
  139. }
  140. STATIC cputstr(abp) register argb_p abp; {
  141. register argb_p tbp;
  142. register length;
  143. length = 0;
  144. tbp = abp;
  145. while (tbp!= (argb_p) 0) {
  146. length += tbp->ab_index;
  147. tbp = tbp->ab_next;
  148. }
  149. coutint(length);
  150. while (abp != (argb_p) 0) {
  151. for (length=0;length<abp->ab_index;length++)
  152. outbyte( (byte) abp->ab_contents[length] );
  153. abp = abp->ab_next;
  154. }
  155. }
  156. STATIC outnum(n)
  157. int n;
  158. {
  159. if (n < 256) {
  160. outbyte((byte) sp_ilb1);
  161. outbyte((byte) n);
  162. } else {
  163. outbyte((byte) sp_ilb2);
  164. coutshort((short) n);
  165. }
  166. }
  167. STATIC numlab(n)
  168. int n;
  169. {
  170. if (n < sp_nilb0) {
  171. outbyte((byte) (n + sp_filb0));
  172. } else {
  173. outnum(n);
  174. }
  175. }
  176. STATIC cputargs(lnp)
  177. line_p lnp;
  178. {
  179. register arg_p ap;
  180. int cnt = 0;
  181. ap = ARG(lnp);
  182. while (ap != (arg_p) 0) {
  183. switch(ap->a_type) {
  184. case ARGOFF:
  185. coutoff(ap->a_a.a_offset);
  186. break;
  187. case ARGOBJECT:
  188. coutobject(ap->a_a.a_obj);
  189. break;
  190. case ARGPROC:
  191. outpsym(ap->a_a.a_proc);
  192. break;
  193. case ARGINSTRLAB:
  194. outnum(ap->a_a.a_instrlab);
  195. break;
  196. case ARGSTRING:
  197. outbyte((byte) sp_scon);
  198. cputstr(&ap->a_a.a_string);
  199. break;
  200. case ARGICN:
  201. outbyte((byte) sp_icon);
  202. goto casecon;
  203. case ARGUCN:
  204. outbyte((byte) sp_ucon);
  205. goto casecon;
  206. case ARGFCN:
  207. outbyte((byte) sp_fcon);
  208. casecon:
  209. coutint(ap->a_a.a_con.ac_length);
  210. cputstr(&ap->a_a.a_con.ac_con);
  211. break;
  212. default:
  213. assert(FALSE);
  214. }
  215. ap = ap->a_next;
  216. /* Avoid generating extremely long CON or ROM statements */
  217. if (cnt++ > 10 && ap != (arg_p) 0 &&
  218. (INSTR(lnp) == ps_con || INSTR(lnp) == ps_rom)) {
  219. cnt = 0;
  220. outbyte((byte) sp_cend);
  221. outinst(INSTR(lnp));
  222. }
  223. }
  224. }
  225. STATIC outoperand(lnp)
  226. line_p lnp;
  227. {
  228. /* Output the operand of instruction lnp */
  229. switch(TYPE(lnp)) {
  230. case OPNO:
  231. if (INSTR(lnp) <= sp_lmnem &&
  232. (em_flag[INSTR(lnp)-sp_fmnem]&EM_PAR) != PAR_NO) {
  233. outbyte((byte) sp_cend);
  234. }
  235. break;
  236. case OPSHORT:
  237. if (INSTR(lnp) == ps_sym) {
  238. outsym(dnames[SHORT(lnp)],sp_dnam);
  239. } else {
  240. coutint(SHORT(lnp));
  241. }
  242. break;
  243. case OPOFFSET:
  244. coutoff(OFFSET(lnp));
  245. break;
  246. case OPINSTRLAB:
  247. if (INSTR(lnp) == op_lab) {
  248. numlab(INSTRLAB(lnp));
  249. } else {
  250. if (INSTR(lnp) < sp_fpseu) {
  251. coutint(INSTRLAB(lnp));
  252. } else {
  253. numlab(INSTRLAB(lnp));
  254. }
  255. }
  256. break;
  257. case OPOBJECT:
  258. coutobject(OBJ(lnp));
  259. break;
  260. case OPPROC:
  261. outpsym(PROC(lnp));
  262. break;
  263. case OPLIST:
  264. cputargs(lnp);
  265. switch(INSTR(lnp)) {
  266. case ps_con:
  267. case ps_rom:
  268. case ps_mes:
  269. outbyte((byte) sp_cend);
  270. /* list terminator */
  271. break;
  272. }
  273. break;
  274. default:
  275. assert(FALSE);
  276. }
  277. }
  278. STATIC outvisibility(lnp)
  279. line_p lnp;
  280. {
  281. /* In EM names of datalabels and procedures can be made
  282. * externally visible, so they can be used in other files.
  283. * There are special EM pseudo-instructions to state
  284. * explicitly that a certain identifier is externally
  285. * visible (ps_exa,ps_exp) or invisible (ps_ina,ps_inp).
  286. * If there is no such pseudo for a certain identifier,
  287. * the identifier is external only if its first use
  288. * in the current file is an applied occurrence.
  289. * Unfortunately the global optimizer may change the
  290. * order of defining and applied occurrences.
  291. * In the first optimizer pass (ic) we record for each identifier
  292. * whether it is external or not. If necessary we generate
  293. * pseudo instructions here.
  294. */
  295. arg_p ap;
  296. short instr;
  297. instr = INSTR(lnp);
  298. switch(TYPE(lnp)) {
  299. case OPOBJECT:
  300. outdocc(OBJ(lnp));
  301. /* applied occurrence of a data label */
  302. break;
  303. case OPSHORT:
  304. if (instr == ps_sym) {
  305. outddef(SHORT(lnp));
  306. /* defining occ. data label */
  307. }
  308. break;
  309. case OPPROC:
  310. if (instr == ps_pro) {
  311. outpdef(PROC(lnp));
  312. /* defining occ. procedure */
  313. } else {
  314. outpocc(PROC(lnp));
  315. }
  316. break;
  317. case OPLIST:
  318. for (ap = ARG(lnp); ap != (arg_p) 0; ap = ap->a_next) {
  319. switch(ap->a_type) {
  320. case ARGOBJECT:
  321. outdocc(ap->a_a.a_obj);
  322. break;
  323. case ARGPROC:
  324. outpocc(ap->a_a.a_proc);
  325. break;
  326. }
  327. }
  328. break;
  329. }
  330. }
  331. cputlines(l,lf)
  332. line_p l;
  333. FILE *lf;
  334. {
  335. /* Output the lines in Campact assembly language
  336. * format.
  337. */
  338. line_p next,lnp;
  339. outfile = lf;
  340. for (lnp = l; lnp != (line_p) 0; lnp = next) {
  341. next = lnp->l_next;
  342. outvisibility(lnp); /* take care of visibiltity rules */
  343. if (INSTR(lnp) != ps_sym && INSTR(lnp) != op_lab) {
  344. outinst(INSTR(lnp));
  345. }
  346. outoperand(lnp);
  347. switch(INSTR(lnp)) {
  348. case ps_pro:
  349. thispro = PROC(lnp);
  350. /* fall through ... */
  351. case ps_end:
  352. coutoff(thispro->p_localbytes);
  353. }
  354. oldline(lnp);
  355. }
  356. if (lmap != (line_p *) 0) {
  357. oldmap(lmap,llength);
  358. lmap = (line_p *) 0;
  359. }
  360. }
  361. cputmagic(lf)
  362. FILE *lf;
  363. {
  364. /* write the magic number */
  365. outfile = lf;
  366. coutshort(sp_magic);
  367. }