ca.c 4.8 KB

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