doc: minor fixes
[qbe.git] / main.c
blobee16a75d78093f2c6d2840b1740099beaf8b092d
1 #include "all.h"
2 #include "config.h"
3 #include <ctype.h>
4 #include <getopt.h>
6 Target T;
8 extern Target T_amd64_sysv;
9 extern Target T_arm64;
10 extern Target T_rv64;
12 static struct TMap {
13 char *name;
14 Target *T;
15 } tmap[] = {
16 { "amd64_sysv", &T_amd64_sysv },
17 { "arm64", &T_arm64 },
18 { "rv64", &T_rv64 },
19 { 0, 0 }
22 enum Asm {
23 Gasmacho,
24 Gaself,
27 char debug['Z'+1] = {
28 ['P'] = 0, /* parsing */
29 ['M'] = 0, /* memory optimization */
30 ['N'] = 0, /* ssa construction */
31 ['C'] = 0, /* copy elimination */
32 ['F'] = 0, /* constant folding */
33 ['A'] = 0, /* abi lowering */
34 ['I'] = 0, /* instruction selection */
35 ['L'] = 0, /* liveness */
36 ['S'] = 0, /* spilling */
37 ['R'] = 0, /* reg. allocation */
40 static FILE *outf;
41 static int dbg;
43 static void
44 data(Dat *d)
46 if (dbg)
47 return;
48 if (d->type == DEnd) {
49 fputs("/* end data */\n\n", outf);
50 freeall();
52 gasemitdat(d, outf);
55 static void
56 func(Fn *fn)
58 uint n;
60 if (dbg)
61 fprintf(stderr, "**** Function %s ****", fn->name);
62 if (debug['P']) {
63 fprintf(stderr, "\n> After parsing:\n");
64 printfn(fn, stderr);
66 fillrpo(fn);
67 fillpreds(fn);
68 filluse(fn);
69 memopt(fn);
70 filluse(fn);
71 ssa(fn);
72 filluse(fn);
73 ssacheck(fn);
74 fillalias(fn);
75 loadopt(fn);
76 filluse(fn);
77 ssacheck(fn);
78 copy(fn);
79 filluse(fn);
80 fold(fn);
81 T.abi(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 struct TMap *tm;
114 FILE *inf, *hf;
115 char *f, *sep;
116 int c, asmmode;
118 asmmode = Defasm;
119 T = Deftgt;
120 outf = stdout;
121 while ((c = getopt(ac, av, "hd:o:G:t:")) != -1)
122 switch (c) {
123 case 'd':
124 for (; *optarg; optarg++)
125 if (isalpha(*optarg)) {
126 debug[toupper(*optarg)] = 1;
127 dbg = 1;
129 break;
130 case 'o':
131 if (strcmp(optarg, "-") != 0) {
132 outf = fopen(optarg, "w");
133 if (!outf) {
134 fprintf(stderr, "cannot open '%s'\n", optarg);
135 exit(1);
138 break;
139 case 't':
140 for (tm=tmap;; tm++) {
141 if (!tm->name) {
142 fprintf(stderr, "unknown target '%s'\n", optarg);
143 exit(1);
145 if (strcmp(optarg, tm->name) == 0) {
146 T = *tm->T;
147 break;
150 break;
151 case 'G':
152 if (strcmp(optarg, "e") == 0)
153 asmmode = Gaself;
154 else if (strcmp(optarg, "m") == 0)
155 asmmode = Gasmacho;
156 else {
157 fprintf(stderr, "unknown gas flavor '%s'\n", optarg);
158 exit(1);
160 break;
161 case 'h':
162 default:
163 hf = c != 'h' ? stderr : stdout;
164 fprintf(hf, "%s [OPTIONS] {file.ssa, -}\n", av[0]);
165 fprintf(hf, "\t%-11s prints this help\n", "-h");
166 fprintf(hf, "\t%-11s output to file\n", "-o file");
167 fprintf(hf, "\t%-11s generate for a target among:\n", "-t <target>");
168 fprintf(hf, "\t%-11s ", "");
169 for (tm=tmap, sep=""; tm->name; tm++, sep=", ")
170 fprintf(hf, "%s%s", sep, tm->name);
171 fprintf(hf, "\n");
172 fprintf(hf, "\t%-11s generate gas (e) or osx (m) asm\n", "-G {e,m}");
173 fprintf(hf, "\t%-11s dump debug information\n", "-d <flags>");
174 exit(c != 'h');
177 switch (asmmode) {
178 case Gaself:
179 gasloc = ".L";
180 gassym = "";
181 break;
182 case Gasmacho:
183 gasloc = "L";
184 gassym = "_";
185 break;
188 do {
189 f = av[optind];
190 if (!f || strcmp(f, "-") == 0) {
191 inf = stdin;
192 f = "-";
193 } else {
194 inf = fopen(f, "r");
195 if (!inf) {
196 fprintf(stderr, "cannot open '%s'\n", f);
197 exit(1);
200 parse(inf, f, data, func);
201 } while (++optind < ac);
203 if (!dbg) {
204 gasemitfin(outf);
205 if (asmmode == Gaself)
206 fprintf(outf, ".section .note.GNU-stack,\"\",@progbits\n");
209 exit(0);