recognize some phis as copies
[qbe.git] / main.c
blob3fcfd7fbd4c3d11003b0f5539eae660970e23954
1 #include "all.h"
2 #include "config.h"
3 #include <ctype.h>
4 #include <getopt.h>
6 Target T;
8 char debug['Z'+1] = {
9 ['P'] = 0, /* parsing */
10 ['M'] = 0, /* memory optimization */
11 ['N'] = 0, /* ssa construction */
12 ['C'] = 0, /* copy elimination */
13 ['F'] = 0, /* constant folding */
14 ['A'] = 0, /* abi lowering */
15 ['I'] = 0, /* instruction selection */
16 ['L'] = 0, /* liveness */
17 ['S'] = 0, /* spilling */
18 ['R'] = 0, /* reg. allocation */
21 extern Target T_amd64_sysv;
22 extern Target T_amd64_apple;
23 extern Target T_arm64;
24 extern Target T_arm64_apple;
25 extern Target T_rv64;
27 static Target *tlist[] = {
28 &T_amd64_sysv,
29 &T_amd64_apple,
30 &T_arm64,
31 &T_arm64_apple,
32 &T_rv64,
35 static FILE *outf;
36 static int dbg;
38 static void
39 data(Dat *d)
41 if (dbg)
42 return;
43 emitdat(d, outf);
44 if (d->type == DEnd) {
45 fputs("/* end data */\n\n", outf);
46 freeall();
50 static void
51 func(Fn *fn)
53 uint n;
55 if (dbg)
56 fprintf(stderr, "**** Function %s ****", fn->name);
57 if (debug['P']) {
58 fprintf(stderr, "\n> After parsing:\n");
59 printfn(fn, stderr);
61 T.abi0(fn);
62 fillrpo(fn);
63 fillpreds(fn);
64 filluse(fn);
65 promote(fn);
66 filluse(fn);
67 ssa(fn);
68 filluse(fn);
69 ssacheck(fn);
70 fillalias(fn);
71 loadopt(fn);
72 filluse(fn);
73 fillalias(fn);
74 coalesce(fn);
75 filluse(fn);
76 ssacheck(fn);
77 copy(fn);
78 filluse(fn);
79 fold(fn);
80 T.abi1(fn);
81 fillpreds(fn);
82 filluse(fn);
83 T.isel(fn);
84 fillrpo(fn);
85 filllive(fn);
86 fillloop(fn);
87 fillcost(fn);
88 spill(fn);
89 rega(fn);
90 fillrpo(fn);
91 simpljmp(fn);
92 fillpreds(fn);
93 fillrpo(fn);
94 assert(fn->rpo[0] == fn->start);
95 for (n=0;; n++)
96 if (n == fn->nblk-1) {
97 fn->rpo[n]->link = 0;
98 break;
99 } else
100 fn->rpo[n]->link = fn->rpo[n+1];
101 if (!dbg) {
102 T.emitfn(fn, outf);
103 fprintf(outf, "/* end function %s */\n\n", fn->name);
104 } else
105 fprintf(stderr, "\n");
106 freeall();
110 main(int ac, char *av[])
112 Target **t;
113 FILE *inf, *hf;
114 char *f, *sep;
115 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;
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);
136 break;
137 case 't':
138 if (strcmp(optarg, "?") == 0) {
139 puts(T.name);
140 exit(0);
142 for (t=tlist;; t++) {
143 if (!*t) {
144 fprintf(stderr, "unknown target '%s'\n", optarg);
145 exit(1);
147 if (strcmp(optarg, (*t)->name) == 0) {
148 T = **t;
149 break;
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);
166 fprintf(hf, "\n");
167 fprintf(hf, "\t%-11s dump debug information\n", "-d <flags>");
168 exit(c != 'h');
171 do {
172 f = av[optind];
173 if (!f || strcmp(f, "-") == 0) {
174 inf = stdin;
175 f = "-";
176 } else {
177 inf = fopen(f, "r");
178 if (!inf) {
179 fprintf(stderr, "cannot open '%s'\n", f);
180 exit(1);
183 parse(inf, f, data, func);
184 fclose(inf);
185 } while (++optind < ac);
187 if (!dbg)
188 T.emitfin(outf);
190 exit(0);