main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. T.abi1(fn);
  78. simpl(fn);
  79. fillcfg(fn);
  80. filluse(fn);
  81. T.isel(fn);
  82. fillcfg(fn);
  83. filllive(fn);
  84. fillloop(fn);
  85. fillcost(fn);
  86. spill(fn);
  87. rega(fn);
  88. fillcfg(fn);
  89. simpljmp(fn);
  90. fillcfg(fn);
  91. assert(fn->rpo[0] == fn->start);
  92. for (n=0;; n++)
  93. if (n == fn->nblk-1) {
  94. fn->rpo[n]->link = 0;
  95. break;
  96. } else
  97. fn->rpo[n]->link = fn->rpo[n+1];
  98. if (!dbg) {
  99. T.emitfn(fn, outf);
  100. fprintf(outf, "/* end function %s */\n\n", fn->name);
  101. } else
  102. fprintf(stderr, "\n");
  103. freeall();
  104. }
  105. static void
  106. dbgfile(char *fn)
  107. {
  108. emitdbgfile(fn, outf);
  109. }
  110. int
  111. main(int ac, char *av[])
  112. {
  113. Target **t;
  114. FILE *inf, *hf;
  115. char *f, *sep;
  116. int c;
  117. T = Deftgt;
  118. outf = stdout;
  119. while ((c = getopt(ac, av, "hd:o:t:")) != -1)
  120. switch (c) {
  121. case 'd':
  122. for (; *optarg; optarg++)
  123. if (isalpha(*optarg)) {
  124. debug[toupper(*optarg)] = 1;
  125. dbg = 1;
  126. }
  127. break;
  128. case 'o':
  129. if (strcmp(optarg, "-") != 0) {
  130. outf = fopen(optarg, "w");
  131. if (!outf) {
  132. fprintf(stderr, "cannot open '%s'\n", optarg);
  133. exit(1);
  134. }
  135. }
  136. break;
  137. case 't':
  138. if (strcmp(optarg, "?") == 0) {
  139. puts(T.name);
  140. exit(0);
  141. }
  142. for (t=tlist;; t++) {
  143. if (!*t) {
  144. fprintf(stderr, "unknown target '%s'\n", optarg);
  145. exit(1);
  146. }
  147. if (strcmp(optarg, (*t)->name) == 0) {
  148. T = **t;
  149. break;
  150. }
  151. }
  152. break;
  153. case 'h':
  154. default:
  155. hf = c != 'h' ? stderr : stdout;
  156. fprintf(hf, "%s [OPTIONS] {file.ssa, -}\n", av[0]);
  157. fprintf(hf, "\t%-11s prints this help\n", "-h");
  158. fprintf(hf, "\t%-11s output to file\n", "-o file");
  159. fprintf(hf, "\t%-11s generate for a target among:\n", "-t <target>");
  160. fprintf(hf, "\t%-11s ", "");
  161. for (t=tlist, sep=""; *t; t++, sep=", ") {
  162. fprintf(hf, "%s%s", sep, (*t)->name);
  163. if (*t == &Deftgt)
  164. fputs(" (default)", hf);
  165. }
  166. fprintf(hf, "\n");
  167. fprintf(hf, "\t%-11s dump debug information\n", "-d <flags>");
  168. exit(c != 'h');
  169. }
  170. do {
  171. f = av[optind];
  172. if (!f || strcmp(f, "-") == 0) {
  173. inf = stdin;
  174. f = "-";
  175. } else {
  176. inf = fopen(f, "r");
  177. if (!inf) {
  178. fprintf(stderr, "cannot open '%s'\n", f);
  179. exit(1);
  180. }
  181. }
  182. parse(inf, f, dbgfile, data, func);
  183. fclose(inf);
  184. } while (++optind < ac);
  185. if (!dbg)
  186. T.emitfin(outf);
  187. exit(0);
  188. }