ca.c 4.7 KB

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