Fix 1 C UB
[qbe.git] / main.c
blobabfe03eaa414bfecd97c9b46f8a803315fed8b6e
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 simpl(fn);
82 fillpreds(fn);
83 filluse(fn);
84 T.isel(fn);
85 fillrpo(fn);
86 filllive(fn);
87 fillloop(fn);
88 fillcost(fn);
89 spill(fn);
90 rega(fn);
91 fillrpo(fn);
92 simpljmp(fn);
93 fillpreds(fn);
94 fillrpo(fn);
95 assert(fn->rpo[0] == fn->start);
96 for (n=0;; n++)
97 if (n == fn->nblk-1) {
98 fn->rpo[n]->link = 0;
99 break;
100 } else
101 fn->rpo[n]->link = fn->rpo[n+1];
102 if (!dbg) {
103 T.emitfn(fn, outf);
104 fprintf(outf, "/* end function %s */\n\n", fn->name);
105 } else
106 fprintf(stderr, "\n");
107 freeall();
111 main(int ac, char *av[])
113 Target **t;
114 FILE *inf, *hf;
115 char *f, *sep;
116 int c;
118 T = Deftgt;
119 outf = stdout;
120 while ((c = getopt(ac, av, "hd:o:t:")) != -1)
121 switch (c) {
122 case 'd':
123 for (; *optarg; optarg++)
124 if (isalpha(*optarg)) {
125 debug[toupper(*optarg)] = 1;
126 dbg = 1;
128 break;
129 case 'o':
130 if (strcmp(optarg, "-") != 0) {
131 outf = fopen(optarg, "w");
132 if (!outf) {
133 fprintf(stderr, "cannot open '%s'\n", optarg);
134 exit(1);
137 break;
138 case 't':
139 if (strcmp(optarg, "?") == 0) {
140 puts(T.name);
141 exit(0);
143 for (t=tlist;; t++) {
144 if (!*t) {
145 fprintf(stderr, "unknown target '%s'\n", optarg);
146 exit(1);
148 if (strcmp(optarg, (*t)->name) == 0) {
149 T = **t;
150 break;
153 break;
154 case 'h':
155 default:
156 hf = c != 'h' ? stderr : stdout;
157 fprintf(hf, "%s [OPTIONS] {file.ssa, -}\n", av[0]);
158 fprintf(hf, "\t%-11s prints this help\n", "-h");
159 fprintf(hf, "\t%-11s output to file\n", "-o file");
160 fprintf(hf, "\t%-11s generate for a target among:\n", "-t <target>");
161 fprintf(hf, "\t%-11s ", "");
162 for (t=tlist, sep=""; *t; t++, sep=", ") {
163 fprintf(hf, "%s%s", sep, (*t)->name);
164 if (*t == &Deftgt)
165 fputs(" (default)", hf);
167 fprintf(hf, "\n");
168 fprintf(hf, "\t%-11s dump debug information\n", "-d <flags>");
169 exit(c != 'h');
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);
184 parse(inf, f, data, func);
185 fclose(inf);
186 } while (++optind < ac);
188 if (!dbg)
189 T.emitfin(outf);
191 exit(0);