cfg: remove unnecessary check for jump type
[qbe.git] / main.c
blob83f08ba8a9818d27bf36d5580c14b9542dda692b
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;
11 static struct TMap {
12 char *name;
13 Target *T;
14 } tmap[] = {
15 { "amd64_sysv", &T_amd64_sysv },
16 { "arm64", &T_arm64 },
17 { 0, 0 }
20 enum Asm {
21 Gasmacho,
22 Gaself,
25 char debug['Z'+1] = {
26 ['P'] = 0, /* parsing */
27 ['M'] = 0, /* memory optimization */
28 ['N'] = 0, /* ssa construction */
29 ['C'] = 0, /* copy elimination */
30 ['F'] = 0, /* constant folding */
31 ['A'] = 0, /* abi lowering */
32 ['I'] = 0, /* instruction selection */
33 ['L'] = 0, /* liveness */
34 ['S'] = 0, /* spilling */
35 ['R'] = 0, /* reg. allocation */
38 static FILE *outf;
39 static int dbg;
41 static void
42 data(Dat *d)
44 if (dbg)
45 return;
46 if (d->type == DEnd) {
47 fputs("/* end data */\n\n", outf);
48 freeall();
50 gasemitdat(d, outf);
53 static void
54 func(Fn *fn)
56 uint n;
58 if (dbg)
59 fprintf(stderr, "**** Function %s ****", fn->name);
60 if (debug['P']) {
61 fprintf(stderr, "\n> After parsing:\n");
62 printfn(fn, stderr);
64 fillrpo(fn);
65 fillpreds(fn);
66 filluse(fn);
67 memopt(fn);
68 filluse(fn);
69 ssa(fn);
70 filluse(fn);
71 ssacheck(fn);
72 fillalias(fn);
73 loadopt(fn);
74 filluse(fn);
75 ssacheck(fn);
76 copy(fn);
77 filluse(fn);
78 fold(fn);
79 T.abi(fn);
80 fillpreds(fn);
81 filluse(fn);
82 T.isel(fn);
83 fillrpo(fn);
84 filllive(fn);
85 fillloop(fn);
86 fillcost(fn);
87 spill(fn);
88 rega(fn);
89 fillrpo(fn);
90 simpljmp(fn);
91 fillpreds(fn);
92 fillrpo(fn);
93 assert(fn->rpo[0] == fn->start);
94 for (n=0;; n++)
95 if (n == fn->nblk-1) {
96 fn->rpo[n]->link = 0;
97 break;
98 } else
99 fn->rpo[n]->link = fn->rpo[n+1];
100 if (!dbg) {
101 T.emitfn(fn, outf);
102 fprintf(outf, "/* end function %s */\n\n", fn->name);
103 } else
104 fprintf(stderr, "\n");
105 freeall();
109 main(int ac, char *av[])
111 struct TMap *tm;
112 FILE *inf, *hf;
113 char *f, *sep;
114 int c, asmmode;
116 asmmode = Defasm;
117 T = Deftgt;
118 outf = stdout;
119 while ((c = getopt(ac, av, "hd:o:G: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 for (tm=tmap;; tm++) {
139 if (!tm->name) {
140 fprintf(stderr, "unknown target '%s'\n", optarg);
141 exit(1);
143 if (strcmp(optarg, tm->name) == 0) {
144 T = *tm->T;
145 break;
148 break;
149 case 'G':
150 if (strcmp(optarg, "e") == 0)
151 asmmode = Gaself;
152 else if (strcmp(optarg, "m") == 0)
153 asmmode = Gasmacho;
154 else {
155 fprintf(stderr, "unknown gas flavor '%s'\n", optarg);
156 exit(1);
158 break;
159 case 'h':
160 default:
161 hf = c != 'h' ? stderr : stdout;
162 fprintf(hf, "%s [OPTIONS] {file.ssa, -}\n", av[0]);
163 fprintf(hf, "\t%-11s prints this help\n", "-h");
164 fprintf(hf, "\t%-11s output to file\n", "-o file");
165 fprintf(hf, "\t%-11s generate for a target among:\n", "-t <target>");
166 fprintf(hf, "\t%-11s ", "");
167 for (tm=tmap, sep=""; tm->name; tm++, sep=", ")
168 fprintf(hf, "%s%s", sep, tm->name);
169 fprintf(hf, "\n");
170 fprintf(hf, "\t%-11s generate gas (e) or osx (m) asm\n", "-G {e,m}");
171 fprintf(hf, "\t%-11s dump debug information\n", "-d <flags>");
172 exit(c != 'h');
175 switch (asmmode) {
176 case Gaself:
177 gasloc = ".L";
178 gassym = "";
179 break;
180 case Gasmacho:
181 gasloc = "L";
182 gassym = "_";
183 break;
186 do {
187 f = av[optind];
188 if (!f || strcmp(f, "-") == 0) {
189 inf = stdin;
190 f = "-";
191 } else {
192 inf = fopen(f, "r");
193 if (!inf) {
194 fprintf(stderr, "cannot open '%s'\n", f);
195 exit(1);
198 parse(inf, f, data, func);
199 } while (++optind < ac);
201 if (!dbg) {
202 gasemitfin(outf);
203 if (asmmode == Gaself)
204 fprintf(outf, ".section .note.GNU-stack,\"\",@progbits\n");
207 exit(0);