argc does not leak its address argument
[qbe.git] / main.c
blob8959cd9c1136b6b0a6e7e41327b04104bebb80bb
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 memopt(fn);
66 filluse(fn);
67 ssa(fn);
68 filluse(fn);
69 ssacheck(fn);
70 fillalias(fn);
71 loadopt(fn);
72 filluse(fn);
73 ssacheck(fn);
74 copy(fn);
75 filluse(fn);
76 fold(fn);
77 T.abi1(fn);
78 fillpreds(fn);
79 filluse(fn);
80 T.isel(fn);
81 fillrpo(fn);
82 filllive(fn);
83 fillloop(fn);
84 fillcost(fn);
85 spill(fn);
86 rega(fn);
87 fillrpo(fn);
88 simpljmp(fn);
89 fillpreds(fn);
90 fillrpo(fn);
91 assert(fn->rpo[0] == fn->start);
92 for (n=0;; n++)
93 if (n == fn->nblk-1) {
94 fn->rpo[n]->link = 0;
95 break;
96 } else
97 fn->rpo[n]->link = fn->rpo[n+1];
98 if (!dbg) {
99 T.emitfn(fn, outf);
100 fprintf(outf, "/* end function %s */\n\n", fn->name);
101 } else
102 fprintf(stderr, "\n");
103 freeall();
107 main(int ac, char *av[])
109 Target **t;
110 FILE *inf, *hf;
111 char *f, *sep;
112 int c;
114 T = Deftgt;
115 outf = stdout;
116 while ((c = getopt(ac, av, "hd:o:t:")) != -1)
117 switch (c) {
118 case 'd':
119 for (; *optarg; optarg++)
120 if (isalpha(*optarg)) {
121 debug[toupper(*optarg)] = 1;
122 dbg = 1;
124 break;
125 case 'o':
126 if (strcmp(optarg, "-") != 0) {
127 outf = fopen(optarg, "w");
128 if (!outf) {
129 fprintf(stderr, "cannot open '%s'\n", optarg);
130 exit(1);
133 break;
134 case 't':
135 if (strcmp(optarg, "?") == 0) {
136 puts(T.name);
137 exit(0);
139 for (t=tlist;; t++) {
140 if (!*t) {
141 fprintf(stderr, "unknown target '%s'\n", optarg);
142 exit(1);
144 if (strcmp(optarg, (*t)->name) == 0) {
145 T = **t;
146 break;
149 break;
150 case 'h':
151 default:
152 hf = c != 'h' ? stderr : stdout;
153 fprintf(hf, "%s [OPTIONS] {file.ssa, -}\n", av[0]);
154 fprintf(hf, "\t%-11s prints this help\n", "-h");
155 fprintf(hf, "\t%-11s output to file\n", "-o file");
156 fprintf(hf, "\t%-11s generate for a target among:\n", "-t <target>");
157 fprintf(hf, "\t%-11s ", "");
158 for (t=tlist, sep=""; *t; t++, sep=", ") {
159 fprintf(hf, "%s%s", sep, (*t)->name);
160 if (*t == &Deftgt)
161 fputs(" (default)", hf);
163 fprintf(hf, "\n");
164 fprintf(hf, "\t%-11s dump debug information\n", "-d <flags>");
165 exit(c != 'h');
168 do {
169 f = av[optind];
170 if (!f || strcmp(f, "-") == 0) {
171 inf = stdin;
172 f = "-";
173 } else {
174 inf = fopen(f, "r");
175 if (!inf) {
176 fprintf(stderr, "cannot open '%s'\n", f);
177 exit(1);
180 parse(inf, f, data, func);
181 fclose(inf);
182 } while (++optind < ac);
184 if (!dbg)
185 T.emitfin(outf);
187 exit(0);