main.c 3.7 KB

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