ca.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * C O M P A C T A S S E M B L Y L A N G U A G E G E N E R A T I O N
  3. *
  4. */
  5. #include <stdio.h>
  6. #include "../share/types.h"
  7. #include "ca.h"
  8. #include "../share/debug.h"
  9. #include "../share/global.h"
  10. #include "../share/lset.h"
  11. #include "../share/files.h"
  12. #include "../share/map.h"
  13. #include "../share/alloc.h"
  14. #include "../share/get.h"
  15. #include "../../../h/em_pseu.h"
  16. #include "../../../h/em_mes.h"
  17. #include "ca_put.h"
  18. /* This phase transforms the Intermediate Code of the global optimizer
  19. * to 'standard' compact assembly language, which will be processed
  20. * by the code generator.
  21. */
  22. short dlength;
  23. dblock_p *dmap;
  24. char **dnames, **pnames; /* Dynamically allocated arrays of strings.
  25. * pnames[i] contains a pointer to the name
  26. * of the procedure with proc_id i.
  27. */
  28. STATIC char **newnametab(tablen,namelen)
  29. short tablen,namelen;
  30. {
  31. register char **np, **tab;
  32. tab = (char **) newmap(tablen);
  33. for (np = &tab[1]; np <= &tab[tablen]; np++) {
  34. *np = (char *) newcore(namelen);
  35. }
  36. return tab;
  37. }
  38. STATIC line_p get_ca_lines(lf,p_out)
  39. FILE *lf;
  40. proc_p *p_out;
  41. {
  42. /* Read lines of EM text and link them.
  43. * Register messages are outputted immediately after the PRO.
  44. */
  45. line_p head, *pp, l;
  46. line_p headm, *mp;
  47. arg_p a;
  48. curinp = lf; /* EM input file */
  49. pp = &head;
  50. mp = &headm;
  51. headm = (line_p) 0;
  52. while (TRUE) {
  53. l = read_line(p_out);
  54. if (feof(curinp)) break;
  55. assert (l != (line_p) 0);
  56. if (INSTR(l) == ps_end && INSTR(head) != ps_pro) {
  57. /* Delete end pseudo after data-unit */
  58. oldline(l);
  59. break;
  60. }
  61. if (INSTR(l) == ps_mes && l->l_a.la_arg->a_a.a_offset == ms_reg) {
  62. /* l is a register message */
  63. if (l->l_a.la_arg->a_next == (arg_p) 0) {
  64. /* register message without arguments */
  65. oldline(l);
  66. } else {
  67. *mp = l;
  68. mp = &l->l_next;
  69. }
  70. } else {
  71. *pp = l;
  72. pp = &l->l_next;
  73. }
  74. if (INSTR(l) == ps_end) {
  75. break;
  76. }
  77. }
  78. *pp = (line_p) 0;
  79. if (head != (line_p) 0 && INSTR(head) == ps_pro) {
  80. /* append register message without arguments to list */
  81. l = newline(OPLIST);
  82. l->l_instr = ps_mes;
  83. a = ARG(l) = newarg(ARGOFF);
  84. a->a_a.a_offset = ms_reg;
  85. *mp = l;
  86. l->l_next = head->l_next;
  87. head->l_next = headm;
  88. } else {
  89. assert(headm == (line_p) 0);
  90. }
  91. return head;
  92. }
  93. STATIC int makedmap(dbl)
  94. dblock_p dbl;
  95. {
  96. /* construct the dmap table */
  97. dblock_p d;
  98. int cnt;
  99. /* determine the length of the table */
  100. cnt = 0;
  101. for (d = dbl; d != (dblock_p) 0; d = d->d_next) cnt++;
  102. dmap = (dblock_p *) newmap(cnt);
  103. for (d = dbl; d != (dblock_p) 0; d = d->d_next) {
  104. assert(d->d_id) <= cnt;
  105. dmap[d->d_id] = d;
  106. }
  107. return cnt;
  108. }
  109. STATIC getdnames(dumpd)
  110. FILE *dumpd;
  111. {
  112. /* Read the names of the datalabels from
  113. * the dump file.
  114. */
  115. char str[IDL+1];
  116. char *s;
  117. int id;
  118. register int i;
  119. dnames = (char **) newnametab(dlength,IDL);
  120. for (;;) {
  121. if (fscanf(dumpd,"%d %s",&id,str) == EOF) return;
  122. assert(id <= dlength);
  123. s = dnames[id];
  124. for (i = 0; i < IDL; i++) {
  125. *s++ = str[i];
  126. }
  127. }
  128. }
  129. STATIC getpnames(dumpp)
  130. FILE *dumpp;
  131. {
  132. /* Read the names of the procedures from
  133. * the dump file.
  134. */
  135. char str[IDL+1];
  136. char *s;
  137. int id;
  138. register int i;
  139. pnames = (char **) newnametab(plength,IDL);
  140. for (;;) {
  141. if (fscanf(dumpp,"%d %s",&id,str) == EOF) return;
  142. assert(id <= plength);
  143. s = pnames[id];
  144. for (i = 0; i < IDL; i++) {
  145. *s++ = str[i];
  146. }
  147. }
  148. }
  149. STATIC bool name_exists(name,endp,endd)
  150. char *name;
  151. proc_p endp;
  152. dblock_p endd;
  153. {
  154. /* Search the proctable (from fproc to endp)
  155. * and the data block table (from fdblock to endd)
  156. * to see if the name is already in use.
  157. */
  158. proc_p p;
  159. dblock_p d;
  160. for (p = fproc; p != endp; p = p->p_next) {
  161. if (strncmp(name,pnames[p->p_id],IDL) == 0) return TRUE;
  162. }
  163. for (d = fdblock; d != endd; d = d->d_next) {
  164. if (strncmp(name,dnames[d->d_id],IDL) == 0) return TRUE;
  165. }
  166. return FALSE;
  167. }
  168. static int nn = 0;
  169. STATIC new_name(s)
  170. char *s;
  171. {
  172. s[0] = '_';
  173. s[1] = 'I';
  174. s[2] = 'I';
  175. sprintf(&s[3],"%d",nn);
  176. nn++;
  177. }
  178. STATIC uniq_names()
  179. {
  180. /* The names of all internal procedures and data blocks
  181. * are made different. As the optimizer combines several
  182. * modules into one, there may be name conflicts between
  183. * procedures or data blocks that were internal in
  184. * different source modules.
  185. */
  186. proc_p p;
  187. dblock_p d;
  188. for (p = fproc; p != (proc_p) 0; p = p->p_next) {
  189. if (!(p->p_flags1 & PF_EXTERNAL) &&
  190. name_exists(pnames[p->p_id],p,fdblock)) {
  191. new_name(pnames[p->p_id]);
  192. }
  193. }
  194. for (d = fdblock; d != (dblock_p) 0; d = d->d_next) {
  195. if (!(d->d_flags1 & DF_EXTERNAL) &&
  196. name_exists(dnames[d->d_id],(proc_p) 0,d) ) {
  197. new_name(dnames[d->d_id]);
  198. }
  199. }
  200. }
  201. main(argc,argv)
  202. int argc;
  203. char *argv[];
  204. {
  205. /* CA does not output proctable etc. files. Instead, its
  206. * pname2 and dname2 arguments contain the names of the
  207. * dump files created by IC.
  208. */
  209. FILE *f, *f2; /* The EM input and output. */
  210. FILE *df, *pf; /* The dump files */
  211. line_p lnp;
  212. fproc = getptable(pname); /* proc table */
  213. fdblock = getdtable(dname); /* data block table */
  214. dlength = makedmap(fdblock); /* allocate dmap table */
  215. df = openfile(dname2,"r");
  216. getdnames(df);
  217. fclose(df);
  218. pf = openfile(pname2,"r");
  219. getpnames(pf);
  220. fclose(pf);
  221. uniq_names();
  222. f = openfile(lname,"r");
  223. f2 = stdout;
  224. cputmagic(f2); /* write magic number */
  225. while ((lnp = get_ca_lines(f,&curproc)) != (line_p) 0) {
  226. cputlines(lnp,f2);
  227. }
  228. fclose(f);
  229. fclose(f2);
  230. exit(0);
  231. }