ca_put.c 7.5 KB

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