minor tweaks to simpljmp pass
[qbe.git] / main.c
blob4d2e6bd7c91fe16d9dd467c1aa53a1c685645e74
1 #include "all.h"
2 #include "config.h"
3 #include <ctype.h>
4 #include <getopt.h>
6 enum Asm {
7 Gasmacho,
8 Gaself,
9 };
11 char debug['Z'+1] = {
12 ['P'] = 0, /* parsing */
13 ['A'] = 0, /* abi lowering */
14 ['I'] = 0, /* instruction selection */
15 ['L'] = 0, /* liveness */
16 ['M'] = 0, /* memory optimization */
17 ['N'] = 0, /* ssa construction */
18 ['C'] = 0, /* copy elimination */
19 ['F'] = 0, /* constant folding */
20 ['S'] = 0, /* spilling */
21 ['R'] = 0, /* reg. allocation */
24 static FILE *outf;
25 static int dbg;
27 static void
28 data(Dat *d)
30 if (dbg)
31 return;
32 if (d->type == DEnd) {
33 fputs("/* end data */\n\n", outf);
34 freeall();
36 emitdat(d, outf);
39 static void
40 func(Fn *fn)
42 uint n;
44 if (dbg)
45 fprintf(stderr, "**** Function %s ****", fn->name);
46 if (debug['P']) {
47 fprintf(stderr, "\n> After parsing:\n");
48 printfn(fn, stderr);
50 fillrpo(fn);
51 fillpreds(fn);
52 filluse(fn);
53 memopt(fn);
54 ssa(fn);
55 filluse(fn);
56 ssacheck(fn);
57 fillloop(fn);
58 fillalias(fn);
59 loadopt(fn);
60 filluse(fn);
61 ssacheck(fn);
62 copy(fn);
63 filluse(fn);
64 fold(fn);
65 abi(fn);
66 fillpreds(fn);
67 filluse(fn);
68 isel(fn);
69 fillrpo(fn);
70 filllive(fn);
71 fillcost(fn);
72 spill(fn);
73 rega(fn);
74 fillrpo(fn);
75 simpljmp(fn);
76 fillpreds(fn);
77 fillrpo(fn);
78 assert(fn->rpo[0] == fn->start);
79 for (n=0;; n++)
80 if (n == fn->nblk-1) {
81 fn->rpo[n]->link = 0;
82 break;
83 } else
84 fn->rpo[n]->link = fn->rpo[n+1];
85 if (!dbg) {
86 emitfn(fn, outf);
87 fprintf(outf, "/* end function %s */\n\n", fn->name);
88 } else
89 fprintf(stderr, "\n");
90 freeall();
93 int
94 main(int ac, char *av[])
96 FILE *inf;
97 char *f;
98 int c, asm;
100 asm = Defaultasm;
101 outf = stdout;
102 while ((c = getopt(ac, av, "hd:o:G:")) != -1)
103 switch (c) {
104 case 'd':
105 for (; *optarg; optarg++)
106 if (isalpha(*optarg)) {
107 debug[toupper(*optarg)] = 1;
108 dbg = 1;
110 break;
111 case 'o':
112 if (strcmp(optarg, "-") != 0)
113 outf = fopen(optarg, "w");
114 break;
115 case 'G':
116 if (strcmp(optarg, "e") == 0)
117 asm = Gaself;
118 else if (strcmp(optarg, "m") == 0)
119 asm = Gasmacho;
120 else {
121 fprintf(stderr, "unknown gas flavor '%s'\n", optarg);
122 exit(1);
124 break;
125 case 'h':
126 default:
127 fprintf(stderr, "%s [OPTIONS] {file.ssa, -}\n", av[0]);
128 fprintf(stderr, "\t%-10s prints this help\n", "-h");
129 fprintf(stderr, "\t%-10s output to file\n", "-o file");
130 fprintf(stderr, "\t%-10s generate gas (e) or osx (m) asm\n", "-G {e,m}");
131 fprintf(stderr, "\t%-10s dump debug information\n", "-d <flags>");
132 exit(c != 'h');
135 switch (asm) {
136 case Gaself:
137 locprefix = ".L";
138 symprefix = "";
139 break;
140 case Gasmacho:
141 locprefix = "L";
142 symprefix = "_";
143 break;
146 do {
147 f = av[optind];
148 if (!f || strcmp(f, "-") == 0) {
149 inf = stdin;
150 f = "-";
151 } else {
152 inf = fopen(f, "r");
153 if (!inf) {
154 fprintf(stderr, "cannot open '%s'\n", f);
155 exit(1);
158 parse(inf, f, data, func);
159 } while (++optind < ac);
161 if (!dbg)
162 emitfin(outf);
164 exit(0);