2
0

main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include "all.h"
  2. #include "config.h"
  3. #include <ctype.h>
  4. #include <getopt.h>
  5. Target T;
  6. char debug['Z'+1] = {
  7. ['P'] = 0, /* parsing */
  8. ['M'] = 0, /* memory optimization */
  9. ['N'] = 0, /* ssa construction */
  10. ['C'] = 0, /* copy elimination */
  11. ['F'] = 0, /* constant folding */
  12. ['A'] = 0, /* abi lowering */
  13. ['I'] = 0, /* instruction selection */
  14. ['L'] = 0, /* liveness */
  15. ['S'] = 0, /* spilling */
  16. ['R'] = 0, /* reg. allocation */
  17. };
  18. extern Target T_amd64_sysv;
  19. extern Target T_amd64_apple;
  20. extern Target T_arm64;
  21. extern Target T_arm64_apple;
  22. extern Target T_rv64;
  23. static Target *tlist[] = {
  24. &T_amd64_sysv,
  25. &T_amd64_apple,
  26. &T_arm64,
  27. &T_arm64_apple,
  28. &T_rv64,
  29. 0
  30. };
  31. static FILE *outf;
  32. static int dbg;
  33. static void
  34. data(Dat *d)
  35. {
  36. if (dbg)
  37. return;
  38. emitdat(d, outf);
  39. if (d->type == DEnd) {
  40. fputs("/* end data */\n\n", outf);
  41. freeall();
  42. }
  43. }
  44. static void
  45. func(Fn *fn)
  46. {
  47. uint n;
  48. if (dbg)
  49. fprintf(stderr, "**** Function %s ****", fn->name);
  50. if (debug['P']) {
  51. fprintf(stderr, "\n> After parsing:\n");
  52. printfn(fn, stderr);
  53. }
  54. T.abi0(fn);
  55. fillcfg(fn);
  56. filluse(fn);
  57. promote(fn);
  58. filluse(fn);
  59. ssa(fn);
  60. filluse(fn);
  61. ssacheck(fn);
  62. fillalias(fn);
  63. loadopt(fn);
  64. filluse(fn);
  65. fillalias(fn);
  66. coalesce(fn);
  67. filluse(fn);
  68. filldom(fn);
  69. ssacheck(fn);
  70. gvn(fn);
  71. fillcfg(fn);
  72. filluse(fn);
  73. filldom(fn);
  74. gcm(fn);
  75. filluse(fn);
  76. ssacheck(fn);
  77. loopopt(fn);
  78. filluse(fn);
  79. T.abi1(fn);
  80. simpl(fn);
  81. fillcfg(fn);
  82. filluse(fn);
  83. T.isel(fn);
  84. fillcfg(fn);
  85. filllive(fn);
  86. fillloop(fn);
  87. fillcost(fn);
  88. spill(fn);
  89. rega(fn);
  90. fillcfg(fn);
  91. simpljmp(fn);
  92. fillcfg(fn);
  93. assert(fn->rpo[0] == fn->start);
  94. for (n=0;; n++)
  95. if (n == fn->nblk-1) {
  96. fn->rpo[n]->link = 0;
  97. break;
  98. } else
  99. fn->rpo[n]->link = fn->rpo[n+1];
  100. if (!dbg) {
  101. T.emitfn(fn, outf);
  102. fprintf(outf, "/* end function %s */\n\n", fn->name);
  103. } else
  104. fprintf(stderr, "\n");
  105. freeall();
  106. }
  107. static void
  108. dbgfile(char *fn)
  109. {
  110. emitdbgfile(fn, outf);
  111. }
  112. int
  113. main(int ac, char *av[])
  114. {
  115. Target **t;
  116. FILE *inf, *hf;
  117. char *f, *sep;
  118. int c;
  119. T = Deftgt;
  120. outf = stdout;
  121. while ((c = getopt(ac, av, "hd:o:t:")) != -1)
  122. switch (c) {
  123. case 'd':
  124. for (; *optarg; optarg++)
  125. if (isalpha(*optarg)) {
  126. debug[toupper(*optarg)] = 1;
  127. dbg = 1;
  128. }
  129. break;
  130. case 'o':
  131. if (strcmp(optarg, "-") != 0) {
  132. outf = fopen(optarg, "w");
  133. if (!outf) {
  134. fprintf(stderr, "cannot open '%s'\n", optarg);
  135. exit(1);
  136. }
  137. }
  138. break;
  139. case 't':
  140. if (strcmp(optarg, "?") == 0) {
  141. puts(T.name);
  142. exit(0);
  143. }
  144. for (t=tlist;; t++) {
  145. if (!*t) {
  146. fprintf(stderr, "unknown target '%s'\n", optarg);
  147. exit(1);
  148. }
  149. if (strcmp(optarg, (*t)->name) == 0) {
  150. T = **t;
  151. break;
  152. }
  153. }
  154. break;
  155. case 'h':
  156. default:
  157. hf = c != 'h' ? stderr : stdout;
  158. fprintf(hf, "%s [OPTIONS] {file.ssa, -}\n", av[0]);
  159. fprintf(hf, "\t%-11s prints this help\n", "-h");
  160. fprintf(hf, "\t%-11s output to file\n", "-o file");
  161. fprintf(hf, "\t%-11s generate for a target among:\n", "-t <target>");
  162. fprintf(hf, "\t%-11s ", "");
  163. for (t=tlist, sep=""; *t; t++, sep=", ") {
  164. fprintf(hf, "%s%s", sep, (*t)->name);
  165. if (*t == &Deftgt)
  166. fputs(" (default)", hf);
  167. }
  168. fprintf(hf, "\n");
  169. fprintf(hf, "\t%-11s dump debug information\n", "-d <flags>");
  170. exit(c != 'h');
  171. }
  172. do {
  173. f = av[optind];
  174. if (!f || strcmp(f, "-") == 0) {
  175. inf = stdin;
  176. f = "-";
  177. } else {
  178. inf = fopen(f, "r");
  179. if (!inf) {
  180. fprintf(stderr, "cannot open '%s'\n", f);
  181. exit(1);
  182. }
  183. }
  184. parse(inf, f, dbgfile, data, func);
  185. fclose(inf);
  186. } while (++optind < ac);
  187. if (!dbg)
  188. T.emitfin(outf);
  189. exit(0);
  190. }