3 static char rcsid
[] = "main.c - faked rcsid";
5 static void typestab(Symbol
, void *);
7 static void stabline(Coordinate
*);
8 static void stabend(Coordinate
*, Symbol
, Coordinate
**, Symbol
*, Symbol
*);
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 */
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
[]) {
26 for (i
= argc
- 1; i
> 0; i
--)
27 if (strncmp(argv
[i
], "-target=", 8) == 0)
30 char *s
= strchr(argv
[i
], '\\');
33 for (j
= 0; bindings
[j
].name
&& bindings
[j
].ir
; j
++)
34 if (strcmp(&argv
[i
][8], bindings
[j
].name
) == 0) {
42 fprint(stderr
, "%s: unknown target", argv
[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
);
52 (*IR
->progbeg
)(argc
, argv
);
53 if (glevel
&& IR
->stabinit
)
54 (*IR
->stabinit
)(firstfile
, argc
, argv
);
57 apply(events
.end
, NULL
, NULL
);
58 memset(&events
, 0, sizeof events
);
60 Symbol symroot
= NULL
;
62 foreach(types
, GLOBAL
, typestab
, &symroot
);
63 foreach(identifiers
, GLOBAL
, typestab
, &symroot
);
67 if ((glevel
> 2 || xref
) && IR
->stabend
)
68 (*IR
->stabend
)(&src
, symroot
,
70 ltov(&symbols
, PERM
), NULL
);
72 (*IR
->stabend
)(&src
, NULL
, NULL
, NULL
, NULL
);
79 /* main_init - process program arguments */
80 void main_init(int argc
, char *argv
[]) {
81 char *infile
= NULL
, *outfile
= NULL
;
88 type_init(argc
, argv
);
89 for (i
= 1; i
< argc
; i
++)
90 if (strcmp(argv
[i
], "-g") == 0 || strcmp(argv
[i
], "-g2") == 0)
92 else if (strncmp(argv
[i
], "-g", 2) == 0) { /* -gn[,x] */
93 char *p
= strchr(argv
[i
], ',');
94 glevel
= atoi(argv
[i
]+2);
99 if (stabIR
.stabline
== NULL
) {
100 stabIR
.stabline
= IR
->stabline
;
101 stabIR
.stabend
= IR
->stabend
;
102 IR
->stabline
= stabline
;
103 IR
->stabend
= stabend
;
106 } else if (strcmp(argv
[i
], "-x") == 0)
108 else if (strcmp(argv
[i
], "-A") == 0) {
110 } else if (strcmp(argv
[i
], "-P") == 0)
112 else if (strcmp(argv
[i
], "-w") == 0)
114 else if (strcmp(argv
[i
], "-n") == 0) {
116 YYnull
= install(string("_YYnull"), &globals
, GLOBAL
, PERM
);
117 YYnull
->type
= func(voidptype
, NULL
, 1);
118 YYnull
->sclass
= EXTERN
;
119 (*IR
->defsymbol
)(YYnull
);
121 } else if (strncmp(argv
[i
], "-n", 2) == 0) { /* -nvalid[,check] */
122 char *p
= strchr(argv
[i
], ',');
124 YYcheck
= install(string(p
+1), &globals
, GLOBAL
, PERM
);
125 YYcheck
->type
= func(voidptype
, NULL
, 1);
126 YYcheck
->sclass
= EXTERN
;
127 (*IR
->defsymbol
)(YYcheck
);
128 p
= stringn(argv
[i
]+2, p
- (argv
[i
]+2));
130 p
= string(argv
[i
]+2);
131 YYnull
= install(p
, &globals
, GLOBAL
, PERM
);
132 YYnull
->type
= func(voidptype
, NULL
, 1);
133 YYnull
->sclass
= EXTERN
;
134 (*IR
->defsymbol
)(YYnull
);
135 } else if (strcmp(argv
[i
], "-v") == 0)
136 fprint(stderr
, "%s %s\n", argv
[0], rcsid
);
137 else if (strncmp(argv
[i
], "-s", 2) == 0)
138 density
= strtod(&argv
[i
][2], NULL
);
139 else if (strncmp(argv
[i
], "-errout=", 8) == 0) {
140 FILE *f
= fopen(argv
[i
]+8, "w");
142 fprint(stderr
, "%s: can't write errors to `%s'\n", argv
[0], argv
[i
]+8);
146 f
= freopen(argv
[i
]+8, "w", stderr
);
148 } else if (strncmp(argv
[i
], "-e", 2) == 0) {
150 if ((x
= strtol(&argv
[i
][2], NULL
, 0)) > 0)
152 } else if (strncmp(argv
[i
], "-little_endian=", 15) == 0)
153 IR
->little_endian
= argv
[i
][15] - '0';
154 else if (strncmp(argv
[i
], "-mulops_calls=", 18) == 0)
155 IR
->mulops_calls
= argv
[i
][18] - '0';
156 else if (strncmp(argv
[i
], "-wants_callb=", 13) == 0)
157 IR
->wants_callb
= argv
[i
][13] - '0';
158 else if (strncmp(argv
[i
], "-wants_argb=", 12) == 0)
159 IR
->wants_argb
= argv
[i
][12] - '0';
160 else if (strncmp(argv
[i
], "-left_to_right=", 15) == 0)
161 IR
->left_to_right
= argv
[i
][15] - '0';
162 else if (strncmp(argv
[i
], "-wants_dag=", 11) == 0)
163 IR
->wants_dag
= argv
[i
][11] - '0';
164 else if (*argv
[i
] != '-' || strcmp(argv
[i
], "-") == 0) {
167 else if (outfile
== NULL
)
171 if (infile
!= NULL
&& strcmp(infile
, "-") != 0
172 && freopen(infile
, "r", stdin
) == NULL
) {
173 fprint(stderr
, "%s: can't read `%s'\n", argv
[0], infile
);
176 if (outfile
!= NULL
&& strcmp(outfile
, "-") != 0
177 && freopen(outfile
, "w", stdout
) == NULL
) {
178 fprint(stderr
, "%s: can't write `%s'\n", argv
[0], outfile
);
182 /* typestab - emit stab entries for p */
183 static void typestab(Symbol p
, void *cl
) {
184 if (*(Symbol
*)cl
== 0 && p
->sclass
&& p
->sclass
!= TYPEDEF
)
186 if ((p
->sclass
== TYPEDEF
|| p
->sclass
== 0) && IR
->stabtype
)
190 /* stabline - emit source code for source coordinate *cp */
191 static void stabline(Coordinate
*cp
) {
192 if (cp
->file
&& cp
->file
!= currentfile
) {
195 currentfile
= cp
->file
;
196 srcfp
= fopen(currentfile
, "r");
200 if (currentline
!= cp
->y
&& srcfp
) {
202 if (srcpos
> cp
->y
) {
206 for ( ; srcpos
< cp
->y
; srcpos
++)
207 if (fgets(buf
, sizeof buf
, srcfp
) == NULL
) {
212 if (srcfp
&& srcpos
== cp
->y
)
213 print("%s%s", comment
, buf
);
217 (*stabIR
.stabline
)(cp
);
220 static void stabend(Coordinate
*cp
, Symbol p
, Coordinate
**cpp
, Symbol
*sp
, Symbol
*stab
) {
222 (*stabIR
.stabend
)(cp
, p
, cpp
, sp
, stab
);