capitalize a label
[qbe.git] / main.c
blobe82b062cbe8757d590175d25c898d48ea9722bf1
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_rv64;
26 static Target *tlist[] = {
27 &T_amd64_sysv,
28 &T_amd64_apple,
29 &T_arm64,
30 &T_rv64,
33 static FILE *outf;
34 static int dbg;
36 static void
37 data(Dat *d)
39 if (dbg)
40 return;
41 if (d->type == DEnd) {
42 fputs("/* end data */\n\n", outf);
43 freeall();
45 emitdat(d, outf);
48 static void
49 func(Fn *fn)
51 uint n;
53 if (dbg)
54 fprintf(stderr, "**** Function %s ****", fn->name);
55 if (debug['P']) {
56 fprintf(stderr, "\n> After parsing:\n");
57 printfn(fn, stderr);
59 fillrpo(fn);
60 fillpreds(fn);
61 filluse(fn);
62 memopt(fn);
63 filluse(fn);
64 ssa(fn);
65 filluse(fn);
66 ssacheck(fn);
67 fillalias(fn);
68 loadopt(fn);
69 filluse(fn);
70 ssacheck(fn);
71 copy(fn);
72 filluse(fn);
73 fold(fn);
74 T.abi(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();
104 main(int ac, char *av[])
106 Target **t;
107 FILE *inf, *hf;
108 char *f, *sep;
109 int c;
111 T = Deftgt;
112 outf = stdout;
113 while ((c = getopt(ac, av, "hd:o:t:")) != -1)
114 switch (c) {
115 case 'd':
116 for (; *optarg; optarg++)
117 if (isalpha(*optarg)) {
118 debug[toupper(*optarg)] = 1;
119 dbg = 1;
121 break;
122 case 'o':
123 if (strcmp(optarg, "-") != 0) {
124 outf = fopen(optarg, "w");
125 if (!outf) {
126 fprintf(stderr, "cannot open '%s'\n", optarg);
127 exit(1);
130 break;
131 case 't':
132 if (strcmp(optarg, "?") == 0) {
133 puts(T.name);
134 exit(0);
136 for (t=tlist;; t++) {
137 if (!*t) {
138 fprintf(stderr, "unknown target '%s'\n", optarg);
139 exit(1);
141 if (strcmp(optarg, (*t)->name) == 0) {
142 T = **t;
143 break;
146 break;
147 case 'h':
148 default:
149 hf = c != 'h' ? stderr : stdout;
150 fprintf(hf, "%s [OPTIONS] {file.ssa, -}\n", av[0]);
151 fprintf(hf, "\t%-11s prints this help\n", "-h");
152 fprintf(hf, "\t%-11s output to file\n", "-o file");
153 fprintf(hf, "\t%-11s generate for a target among:\n", "-t <target>");
154 fprintf(hf, "\t%-11s ", "");
155 for (t=tlist, sep=""; *t; t++, sep=", ") {
156 fprintf(hf, "%s%s", sep, (*t)->name);
157 if (*t == &Deftgt)
158 fputs(" (default)", hf);
160 fprintf(hf, "\n");
161 fprintf(hf, "\t%-11s dump debug information\n", "-d <flags>");
162 exit(c != 'h');
165 do {
166 f = av[optind];
167 if (!f || strcmp(f, "-") == 0) {
168 inf = stdin;
169 f = "-";
170 } else {
171 inf = fopen(f, "r");
172 if (!inf) {
173 fprintf(stderr, "cannot open '%s'\n", f);
174 exit(1);
177 parse(inf, f, data, func);
178 fclose(inf);
179 } while (++optind < ac);
181 if (!dbg)
182 T.emitfin(outf);
184 exit(0);