main.c 3.5 KB

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