1 |
15 |
hellwig |
#include "c.h"
|
2 |
|
|
|
3 |
|
|
static char rcsid[] = "$Name: v4_2 $($Id: main.c,v 1.1 2002/08/28 23:12:44 drh Exp $)";
|
4 |
|
|
|
5 |
|
|
static void typestab(Symbol, void *);
|
6 |
|
|
|
7 |
|
|
static void stabline(Coordinate *);
|
8 |
|
|
static void stabend(Coordinate *, Symbol, Coordinate **, Symbol *, Symbol *);
|
9 |
|
|
Interface *IR = NULL;
|
10 |
|
|
|
11 |
|
|
int Aflag; /* >= 0 if -A specified */
|
12 |
|
|
int Pflag; /* != 0 if -P specified */
|
13 |
|
|
int glevel; /* == [0-9] if -g[0-9] specified */
|
14 |
|
|
int xref; /* != 0 for cross-reference data */
|
15 |
|
|
Symbol YYnull; /* _YYnull symbol if -n or -nvalidate specified */
|
16 |
|
|
Symbol YYcheck; /* _YYcheck symbol if -nvalidate,check specified */
|
17 |
|
|
|
18 |
|
|
static char *comment;
|
19 |
|
|
static Interface stabIR;
|
20 |
|
|
static char *currentfile; /* current file name */
|
21 |
|
|
static int currentline; /* current line number */
|
22 |
|
|
static FILE *srcfp; /* stream for current file, if non-NULL */
|
23 |
|
|
static int srcpos; /* position of srcfp, if srcfp is non-NULL */
|
24 |
|
|
int main(int argc, char *argv[]) {
|
25 |
|
|
int i, j;
|
26 |
|
|
for (i = argc - 1; i > 0; i--)
|
27 |
|
|
if (strncmp(argv[i], "-target=", 8) == 0)
|
28 |
|
|
break;
|
29 |
|
|
if (i > 0) {
|
30 |
|
|
char *s = strchr(argv[i], '\\');
|
31 |
|
|
if (s != NULL)
|
32 |
|
|
*s = '/';
|
33 |
|
|
for (j = 0; bindings[j].name && bindings[j].ir; j++)
|
34 |
|
|
if (strcmp(&argv[i][8], bindings[j].name) == 0) {
|
35 |
|
|
IR = bindings[j].ir;
|
36 |
|
|
break;
|
37 |
|
|
}
|
38 |
|
|
if (s != NULL)
|
39 |
|
|
*s = '\\';
|
40 |
|
|
}
|
41 |
|
|
if (!IR) {
|
42 |
|
|
fprint(stderr, "%s: unknown target", argv[0]);
|
43 |
|
|
if (i > 0)
|
44 |
|
|
fprint(stderr, " `%s'", &argv[i][8]);
|
45 |
|
|
fprint(stderr, "; must specify one of\n");
|
46 |
|
|
for (i = 0; bindings[i].name; i++)
|
47 |
|
|
fprint(stderr, "\t-target=%s\n", bindings[i].name);
|
48 |
|
|
exit(EXIT_FAILURE);
|
49 |
|
|
}
|
50 |
|
|
init(argc, argv);
|
51 |
|
|
t = gettok();
|
52 |
|
|
(*IR->progbeg)(argc, argv);
|
53 |
|
|
for (i = 1; i < argc; i++)
|
54 |
|
|
if (strcmp(argv[i], "-n") == 0) {
|
55 |
|
|
if (!YYnull) {
|
56 |
|
|
YYnull = install(string("_YYnull"), &globals, GLOBAL, PERM);
|
57 |
|
|
YYnull->type = func(voidptype, NULL, 1);
|
58 |
|
|
YYnull->sclass = EXTERN;
|
59 |
|
|
(*IR->defsymbol)(YYnull);
|
60 |
|
|
}
|
61 |
|
|
} else if (strncmp(argv[i], "-n", 2) == 0) { /* -nvalid[,check] */
|
62 |
|
|
char *p = strchr(argv[i], ',');
|
63 |
|
|
if (p) {
|
64 |
|
|
YYcheck = install(string(p+1), &globals, GLOBAL, PERM);
|
65 |
|
|
YYcheck->type = func(voidptype, NULL, 1);
|
66 |
|
|
YYcheck->sclass = EXTERN;
|
67 |
|
|
(*IR->defsymbol)(YYcheck);
|
68 |
|
|
p = stringn(argv[i]+2, p - (argv[i]+2));
|
69 |
|
|
} else
|
70 |
|
|
p = string(argv[i]+2);
|
71 |
|
|
YYnull = install(p, &globals, GLOBAL, PERM);
|
72 |
|
|
YYnull->type = func(voidptype, NULL, 1);
|
73 |
|
|
YYnull->sclass = EXTERN;
|
74 |
|
|
(*IR->defsymbol)(YYnull);
|
75 |
|
|
} else {
|
76 |
|
|
profInit(argv[i]);
|
77 |
|
|
traceInit(argv[i]);
|
78 |
|
|
}
|
79 |
|
|
if (glevel && IR->stabinit)
|
80 |
|
|
(*IR->stabinit)(firstfile, argc, argv);
|
81 |
|
|
program();
|
82 |
|
|
if (events.end)
|
83 |
|
|
apply(events.end, NULL, NULL);
|
84 |
|
|
memset(&events, 0, sizeof events);
|
85 |
|
|
if (glevel || xref) {
|
86 |
|
|
Symbol symroot = NULL;
|
87 |
|
|
Coordinate src;
|
88 |
|
|
foreach(types, GLOBAL, typestab, &symroot);
|
89 |
|
|
foreach(identifiers, GLOBAL, typestab, &symroot);
|
90 |
|
|
src.file = firstfile;
|
91 |
|
|
src.x = 0;
|
92 |
|
|
src.y = lineno;
|
93 |
|
|
if ((glevel > 2 || xref) && IR->stabend)
|
94 |
|
|
(*IR->stabend)(&src, symroot,
|
95 |
|
|
ltov(&loci, PERM),
|
96 |
|
|
ltov(&symbols, PERM), NULL);
|
97 |
|
|
else if (IR->stabend)
|
98 |
|
|
(*IR->stabend)(&src, NULL, NULL, NULL, NULL);
|
99 |
|
|
}
|
100 |
|
|
finalize();
|
101 |
|
|
(*IR->progend)();
|
102 |
|
|
deallocate(PERM);
|
103 |
|
|
return errcnt > 0;
|
104 |
|
|
}
|
105 |
|
|
/* main_init - process program arguments */
|
106 |
|
|
void main_init(int argc, char *argv[]) {
|
107 |
|
|
char *infile = NULL, *outfile = NULL;
|
108 |
|
|
int i;
|
109 |
|
|
static int inited;
|
110 |
|
|
|
111 |
|
|
if (inited)
|
112 |
|
|
return;
|
113 |
|
|
inited = 1;
|
114 |
|
|
type_init(argc, argv);
|
115 |
|
|
for (i = 1; i < argc; i++)
|
116 |
|
|
if (strcmp(argv[i], "-g") == 0 || strcmp(argv[i], "-g2") == 0)
|
117 |
|
|
glevel = 2;
|
118 |
|
|
else if (strncmp(argv[i], "-g", 2) == 0) { /* -gn[,x] */
|
119 |
|
|
char *p = strchr(argv[i], ',');
|
120 |
|
|
glevel = atoi(argv[i]+2);
|
121 |
|
|
if (p) {
|
122 |
|
|
comment = p + 1;
|
123 |
|
|
if (glevel == 0)
|
124 |
|
|
glevel = 1;
|
125 |
|
|
if (stabIR.stabline == NULL) {
|
126 |
|
|
stabIR.stabline = IR->stabline;
|
127 |
|
|
stabIR.stabend = IR->stabend;
|
128 |
|
|
IR->stabline = stabline;
|
129 |
|
|
IR->stabend = stabend;
|
130 |
|
|
}
|
131 |
|
|
}
|
132 |
|
|
} else if (strcmp(argv[i], "-x") == 0)
|
133 |
|
|
xref++;
|
134 |
|
|
else if (strcmp(argv[i], "-A") == 0) {
|
135 |
|
|
++Aflag;
|
136 |
|
|
} else if (strcmp(argv[i], "-P") == 0)
|
137 |
|
|
Pflag++;
|
138 |
|
|
else if (strcmp(argv[i], "-w") == 0)
|
139 |
|
|
wflag++;
|
140 |
|
|
else if (strcmp(argv[i], "-v") == 0)
|
141 |
|
|
fprint(stderr, "%s %s\n", argv[0], rcsid);
|
142 |
|
|
else if (strncmp(argv[i], "-s", 2) == 0)
|
143 |
|
|
density = strtod(&argv[i][2], NULL);
|
144 |
|
|
else if (strncmp(argv[i], "-errout=", 8) == 0) {
|
145 |
|
|
FILE *f = fopen(argv[i]+8, "w");
|
146 |
|
|
if (f == NULL) {
|
147 |
|
|
fprint(stderr, "%s: can't write errors to `%s'\n", argv[0], argv[i]+8);
|
148 |
|
|
exit(EXIT_FAILURE);
|
149 |
|
|
}
|
150 |
|
|
fclose(f);
|
151 |
|
|
f = freopen(argv[i]+8, "w", stderr);
|
152 |
|
|
assert(f);
|
153 |
|
|
} else if (strncmp(argv[i], "-e", 2) == 0) {
|
154 |
|
|
int x;
|
155 |
|
|
if ((x = strtol(&argv[i][2], NULL, 0)) > 0)
|
156 |
|
|
errlimit = x;
|
157 |
|
|
} else if (strncmp(argv[i], "-little_endian=", 15) == 0)
|
158 |
|
|
IR->little_endian = argv[i][15] - '0';
|
159 |
|
|
else if (strncmp(argv[i], "-mulops_calls=", 18) == 0)
|
160 |
|
|
IR->mulops_calls = argv[i][18] - '0';
|
161 |
|
|
else if (strncmp(argv[i], "-wants_callb=", 13) == 0)
|
162 |
|
|
IR->wants_callb = argv[i][13] - '0';
|
163 |
|
|
else if (strncmp(argv[i], "-wants_argb=", 12) == 0)
|
164 |
|
|
IR->wants_argb = argv[i][12] - '0';
|
165 |
|
|
else if (strncmp(argv[i], "-left_to_right=", 15) == 0)
|
166 |
|
|
IR->left_to_right = argv[i][15] - '0';
|
167 |
|
|
else if (strncmp(argv[i], "-wants_dag=", 11) == 0)
|
168 |
|
|
IR->wants_dag = argv[i][11] - '0';
|
169 |
|
|
else if (*argv[i] != '-' || strcmp(argv[i], "-") == 0) {
|
170 |
|
|
if (infile == NULL)
|
171 |
|
|
infile = argv[i];
|
172 |
|
|
else if (outfile == NULL)
|
173 |
|
|
outfile = argv[i];
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
if (infile != NULL && strcmp(infile, "-") != 0
|
177 |
|
|
&& freopen(infile, "r", stdin) == NULL) {
|
178 |
|
|
fprint(stderr, "%s: can't read `%s'\n", argv[0], infile);
|
179 |
|
|
exit(EXIT_FAILURE);
|
180 |
|
|
}
|
181 |
|
|
if (outfile != NULL && strcmp(outfile, "-") != 0
|
182 |
|
|
&& freopen(outfile, "w", stdout) == NULL) {
|
183 |
|
|
fprint(stderr, "%s: can't write `%s'\n", argv[0], outfile);
|
184 |
|
|
exit(EXIT_FAILURE);
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
/* typestab - emit stab entries for p */
|
188 |
|
|
static void typestab(Symbol p, void *cl) {
|
189 |
|
|
if (*(Symbol *)cl == 0 && p->sclass && p->sclass != TYPEDEF)
|
190 |
|
|
*(Symbol *)cl = p;
|
191 |
|
|
if ((p->sclass == TYPEDEF || p->sclass == 0) && IR->stabtype)
|
192 |
|
|
(*IR->stabtype)(p);
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
/* stabline - emit source code for source coordinate *cp */
|
196 |
|
|
static void stabline(Coordinate *cp) {
|
197 |
|
|
if (cp->file && cp->file != currentfile) {
|
198 |
|
|
if (srcfp)
|
199 |
|
|
fclose(srcfp);
|
200 |
|
|
currentfile = cp->file;
|
201 |
|
|
srcfp = fopen(currentfile, "r");
|
202 |
|
|
srcpos = 0;
|
203 |
|
|
currentline = 0;
|
204 |
|
|
}
|
205 |
|
|
if (currentline != cp->y && srcfp) {
|
206 |
|
|
char buf[512];
|
207 |
|
|
if (srcpos > cp->y) {
|
208 |
|
|
rewind(srcfp);
|
209 |
|
|
srcpos = 0;
|
210 |
|
|
}
|
211 |
|
|
for ( ; srcpos < cp->y; srcpos++)
|
212 |
|
|
if (fgets(buf, sizeof buf, srcfp) == NULL) {
|
213 |
|
|
fclose(srcfp);
|
214 |
|
|
srcfp = NULL;
|
215 |
|
|
break;
|
216 |
|
|
}
|
217 |
|
|
if (srcfp && srcpos == cp->y)
|
218 |
|
|
print("%s%s", comment, buf);
|
219 |
|
|
}
|
220 |
|
|
currentline = cp->y;
|
221 |
|
|
if (stabIR.stabline)
|
222 |
|
|
(*stabIR.stabline)(cp);
|
223 |
|
|
}
|
224 |
|
|
|
225 |
|
|
static void stabend(Coordinate *cp, Symbol p, Coordinate **cpp, Symbol *sp, Symbol *stab) {
|
226 |
|
|
if (stabIR.stabend)
|
227 |
|
|
(*stabIR.stabend)(cp, p, cpp, sp, stab);
|
228 |
|
|
if (srcfp)
|
229 |
|
|
fclose(srcfp);
|
230 |
|
|
}
|