|
|
1.1 root 1: /*
2: * n1/cc1.c
3: * This is the mainline of the new, direct to bits code generator.
4: * It is mainly just a driving routine.
5: * The tree reader and the diagnostic routines are also in this file.
6: * Everything in here is machine independent.
7: */
8:
9: #ifdef vax
10: #include "INC$LIB:cc1.h"
11: #else
12: #include "cc1.h"
13: #endif
14:
15: #if !OVERLAID
16: FILE *ifp; /* Input file */
17: FILE *ofp; /* Output file */
18: char file[NFNAME]; /* File name */
19: char module[NMNAME]; /* Module name */
20: int line; /* Line number */
21: char id[NCSYMB]; /* Temp. id buffer */
22: VARIANT variant; /* Variant template */
23: #if !TINY
24: int mflag; /* Debug modify routines */
25: int sflag; /* Debug select routines */
26: int oflag; /* Debug output routines */
27: #endif
28: #endif
29:
30: int qccmp(); /* Quicksort compare */
31: SYM *hash1[NSHASH]; /* Hash table */
32: int nnsw; /* Switch nesting level */
33: int nsw[NNSW]; /* Switch label */
34: CASES *cases; /* Case buffer */
35:
36: #if !OVERLAID
37: /*
38: * Mainline.
39: * Interpret options and
40: * open files.
41: */
42: main(argc, argv)
43: char *argv[];
44: {
45: passname = "cc1";
46: if (argc < 4)
47: usage();
48: getvariant(argv[1]);
49: #if GEMDOS
50: {
51: extern long gemdos();
52: extern char *lmalloc();
53: free(lmalloc((gemdos(0x48,-1L)-4096) & ~1023L));
54: }
55: #endif
56: if ((ifp=fopen(argv[2], SRMODE)) == NULL) {
57: fprintf(stderr, "%s: cannot open.\n", argv[2]);
58: exit(BAD);
59: }
60: if ((ofp=fopen(argv[3], SWMODE)) == NULL) {
61: fprintf(stderr, "%s: cannot create.\n", argv[3]);
62: exit(BAD);
63: }
64: #if !TINY
65: if (argc > 4)
66: mflag = atoi(argv[4]);
67: if (argc > 5)
68: sflag = atoi(argv[5]);
69: if (argc > 6)
70: oflag = atoi(argv[6]);
71: #endif
72: labgen = 10000;
73: oldseg = -1;
74: work1();
75: if (nerr != 0)
76: exit(BAD);
77: exit(OK);
78: }
79:
80: /*
81: * Print usage message.
82: */
83: usage()
84: {
85: #if !TINY
86: fprintf(stderr,
87: "Usage: cc1 variant in out [mlevel [slevel [olevel]]]\n");
88: #else
89: fprintf(stderr, "Usage: cc1 variant in out\n");
90: #endif
91: exit(BAD);
92: }
93: #else
94: /*
95: * Mainline for the overlaid version
96: * of the coder. The files are already open.
97: * Set up for fatal errors. Process the file
98: * and return status.
99: */
100: cc1()
101: {
102: passname = "cc1";
103: if (setjmp(death) != 0) {
104: freetree();
105: freepool();
106: freegsym();
107: return (ABORT);
108: }
109: labgen = 10000;
110: oldseg = -1;
111: #if MONOLITHIC
112: nnsw = 0;
113: #endif
114: work1();
115: freetree();
116: freepool();
117: freegsym();
118: if (nerr != 0)
119: return (BAD);
120: return (OK);
121: }
122: #endif
123:
124: /*
125: * Read temp. file.
126: * Do simple things right here.
127: * Read in trees and pass them over
128: * to the tree compilation code.
129: */
130: work1()
131: {
132: register int i;
133: register int op;
134: register TREE *tp;
135: register int d;
136: register int n;
137: register int tt;
138: register int ln;
139:
140: coderinit();
141: for (;;) {
142: op = bget();
143: switch (op) {
144:
145: case LINE:
146: line = iget();
147: /* bput(op); */
148: /* iput(line); */
149: break;
150:
151: case DLABEL:
152: bput(DLABEL);
153: tcpy(bget());
154: break;
155:
156: case MNAME:
157: sget(module, NMNAME);
158: bput(op);
159: sput(module);
160: break;
161:
162: case FNAME:
163: sget(file, NFNAME);
164: /* bput(op); */
165: /* sput(file); */
166: break;
167:
168: case GLABEL:
169: case SLABEL:
170: sget(id, NCSYMB);
171: bput(op);
172: sput(id);
173: break;
174:
175: case COMM:
176: bput(op);
177: sget(id, NCSYMB);
178: sput(id);
179: zput(zget());
180: break;
181:
182: case LLABEL:
183: bput(op);
184: iput(iget());
185: break;
186:
187: case BLOCK:
188: bput(op);
189: zput(zget());
190: break;
191:
192: case JUMP:
193: genubr(iget());
194: break;
195:
196: case ENTER:
197: newseg(bget());
198: break;
199:
200: case AUTOS:
201: doautos();
202: break;
203:
204: case PROLOG:
205: bput(PROLOG);
206: doprolog();
207: break;
208:
209: case ALIGN:
210: bput(ALIGN);
211: bput(bget());
212: break;
213:
214: case EPILOG:
215: doepilog();
216: bput(EPILOG);
217: break;
218:
219: case EEXPR:
220: tp = modify(treeget(), MEFFECT);
221: code(tp, MEFFECT, 0, 0);
222: break;
223:
224: case IBLOCK:
225: iblock(bget());
226: break;
227:
228: case IEXPR:
229: tp = treeget();
230: tt = tp->t_type;
231: tp = modify(tp->t_lp, MINIT);
232: iexpr(tp, tt);
233: break;
234:
235: case FEXPR:
236: case TEXPR:
237: ln = iget();
238: tp = modify(treeget(), MFLOW);
239: code(tp, MFLOW, op==TEXPR, ln);
240: break;
241:
242: case REXPR:
243: tp = modify(treeget(), MRETURN);
244: code(tp, MRETURN, 0, 0);
245: break;
246:
247: case SEXPR:
248: if (nnsw >= NNSW)
249: cfatal("switch overflow");
250: nsw[nnsw++] = n = newlab();
251: tp = modify(treeget(), MSWITCH);
252: code(tp, MSWITCH, 0, 0);
253: genubr(n);
254: break;
255:
256: case SBODY:
257: if (nnsw <= 0)
258: cbotch("switch underflow");
259: genlab(nsw[--nnsw]);
260: d = iget();
261: n = iget();
262: if (n != 0) {
263: cases = (CASES *)malloc(n*sizeof(CASES));
264: if (cases == NULL)
265: cfatal("too many cases");
266: for (i=0; i<n; ++i) {
267: cases[i].c_val = iget();
268: cases[i].c_lab = iget();
269: }
270: shellsort((char *)cases, n, sizeof(cases[0]), &qccmp);
271: }
272: genswitch(d, n);
273: if (n != 0)
274: free((char *)cases);
275: break;
276:
277: case FINISH:
278: bput(op);
279: return;
280:
281: case EOF:
282: cfatal("unexpected EOF");
283:
284: default:
285: cbotch("bad temporary file opcode %d", op);
286: }
287: }
288: }
289:
290: /*
291: * This routine is used by the library
292: * quicksort routine to compare two elements
293: * in the case constant buffer.
294: */
295: qccmp(a, b)
296: struct cases *a, *b;
297: {
298: return (a->c_val - b->c_val);
299: }
300:
301: /*
302: * Generate a local label.
303: */
304: genlab(n)
305: {
306: bput(LLABEL);
307: iput((ival_t)n);
308: }
309:
310: /* end of n1/cc1.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.