Add tests, fixes for tests, reinstate and type-convert stuff marked "bitrot"
[ccom.git] / c13.c
1 /*
2  * C second pass -- tables
3  */
4
5 #if     !defined(lint) && defined(DOSCCS)
6 static  char    sccsid[] = "@(#)c13.c   2.1 (2.11BSD GTE) 10/4/94";
7 #endif
8
9 #include "c1.h"
10 /*
11  * Operator dope table-- see description in c0.
12  */
13 /* notes on type safety: */
14 /* LEAF=0 implies node is a tnode and tn_tr1 is valid */
15 /* CNVRT=1 implies node is a tnode and tn_tr1 is valid */
16 /* BINARY=1 implies node is a tnode and tn_tr1, tn_tr2 are valid */
17 /* ASSGOP=1 implies node is a tnode and tn_tr1, tn_tr2 are valid */
18 int opdope1[] = {
19         000000, /* EOFC (0) */
20         000000, /* ; */
21         000000, /* { */
22         000000, /* } */
23         036000, /* [ */
24         002000, /* ] */
25         036000, /* ( */
26         002000, /* ) */
27         014201, /* : */
28         007001, /* , */
29         000000, /* field selection (10) */
30         000000, /* reverse field selection */
31         /*000001*/000000,       /* temporary field selection */
32         000001, /* int->ptr */
33         000001, /* ptr->int */
34         000001, /* long->ptr */
35         000001, /* field assignment */
36         000001, /* >> unsigned */
37         000001, /* >>= unsigned */
38         000000, /* keyword */
39         000400, /* name (20) */
40         000400, /* short constant */
41         000400, /* string */
42         000400, /* float */
43         000400, /* double */
44         000400, /* long const */
45         000400, /* long const <= 16 bits */
46         000400, /* autoi, *r++ */
47         000400, /* autod, *--r */
48         000400, /* () empty arglist */
49         034213, /* ++pre (30) */
50         034213, /* --pre */
51         034213, /* ++post */
52         034213, /* --post */
53         034220, /* !un */
54         034202, /* &un */
55         034220, /* *un */
56         034200, /* -un */
57         034220, /* ~un */
58         036001, /* . (structure reference) */
59         030101, /* + (40) */
60         030001, /* - */
61         032101, /* * */
62         032001, /* / */
63         032001, /* % */
64         026061, /* >> */
65         026061, /* << */
66         020161, /* & */
67         016161, /* | */
68         016161, /* ^ */
69         036001, /* -> (50) */
70         001000, /* int -> double */
71         001000, /* double -> int */
72         000001, /* && */
73         000001, /* || */
74         030001, /* &~ */
75         001000, /* double -> long */
76         001000, /* long -> double */
77         001000, /* integer -> long */
78         000000, /* long -> integer */
79         022005, /* == (60) */
80         022005, /* != */
81         024005, /* <= */
82         024005, /* < */
83         024005, /* >= */
84         024005, /* > */
85         024005, /* <p */
86         024005, /* <=p */
87         024005, /* >p */
88         024005, /* >=p */
89         012213, /* += (70) */
90         012213, /* -= */
91         012213, /* *= */
92         012213, /* /= */
93         012213, /* %= */
94         012253, /* >>= */
95         012253, /* <<= */
96         012253, /* &= */
97         012253, /* |= */
98         012253, /* ^= */
99         012213, /* = (80) */
100         030001, /* & for tests */
101         032001, /*  * (long) */
102         032001, /*  / (long) */
103         032001, /* % (long) */
104         012253, /* &= ~ */
105         012213, /* *= (long) */
106         012213, /* /= (long) */
107         012213, /* %= (long) */
108         000000, /* (89) */
109         014201, /* question '?' (90) */
110         026061, /* long << */
111         012253, /* long <<= */
112         000101, /* max */
113         000101, /* maxp */
114         000101, /* min */
115         000101, /* minp */
116         000001, /* , */
117         000000, /* call1 */
118         000000, /* call2 */
119         036001, /* call (100) */
120         036000, /* mcall */
121         000000, /* goto */
122         000000, /* jump cond */
123         000000, /* branch cond */
124         000400, /* set nregs */
125         000000, /* load */
126         030001, /* ptoi1 */
127         000000, /* (108) */
128         000000, /* int->char */
129         000000, /* force r0 (110) */
130         000000, /* branch */
131         000000, /* label */
132         000000, /* nlabel */
133         000000, /* rlabel */
134         000000, /* structure assign */
135         000001, /* struct assignment setup */
136         032001, /* unsigned / */
137         032001, /* unsigned % */
138         012213, /* unsigned /= */
139         012213, /* unsigned %= (120) */
140         032001, /* unsigned long * */
141         032001, /* unsigned long / */
142         032001, /* unsigned long % */
143         012213, /* unsigned long *= */
144         012213, /* unsigned long /= */
145         012213, /* unsigned long %= */
146         01000,  /* unsigned long -> float(double) */
147         026061, /* unsigned long >> */
148         012253, /* unsigned long >>= (129) */
149 };
150
151 char    *opntab[] = {
152         0,                      /* 0 */
153         0,
154         0,
155         0,
156         0,
157         0,
158         0,
159         0,
160         ":",
161         ",",
162         "field select",         /* 10 */
163         0,
164         0,
165         "int->ptr",
166         "ptr->int",
167         "long->ptr",
168         "field assign",
169         ">>",
170         ">>=",
171         "keyword",
172         "name",                 /* 20 */
173         "short constant",
174         "string",
175         "float",
176         "double",
177         "long constant",
178         "long constant",
179         "*r++",
180         "*--r",
181         "()",
182         "++pre",                /* 30 */
183         "--pre",
184         "++post",
185         "--post",
186         "!un",
187         "&",
188         "*",
189         "-",
190         "~",
191         ".",
192         "+",                    /* 40 */
193         "-",
194         "*",
195         "/",
196         "%",
197         ">>",
198         "<<",
199         "&",
200         "|",
201         "^",
202         "->",                   /* 50 */
203         "int->double",
204         "double->int",
205         "&&",
206         "||",
207         "&~",
208         "double->long",
209         "long->double",
210         "integer->long",
211         "long->integer",
212         "==",                   /* 60 */
213         "!=",
214         "<=",
215         "<",
216         ">=",
217         ">",
218         "<p",
219         "<=p",
220         ">p",
221         ">=p",
222         "+=",                   /* 70 */
223         "-=",
224         "*=",
225         "/=",
226         "%=",
227         ">>=",
228         "<<=",
229         "&=",
230         "|=",
231         "^=",
232         "=",                    /* 80 */
233         "& for tests",
234         "*",
235         "/",
236         "%",
237         "&= ~",
238         "*=",
239         "/=",
240         "%=",
241         0,
242         "?",                    /* 90 */
243         "<<",
244         "<<=",
245         "\\/",
246         "\\/",
247         "/\\",
248         "/\\",
249         ",",
250         "call1",
251         "call2",
252         "call",                 /* 100 */
253         "mcall",
254         "goto",
255         "jump cond",
256         "branch cond",
257         "set nregs",
258         "load value",
259         "ptr->integer",
260         0,
261         "int->char",
262         "force register",       /* 110 */
263         "branch",
264         "label",
265         "nlabel",
266         "rlabel",
267         "=structure",
268         "= (struct setup)",
269         "/",
270         "%",
271         "/=",
272         "%=",                   /* 120 */
273         "*",                    /* unsigned long */
274         "/",                    /* unsigned long */
275         "%",                    /* unsigned long */
276         "*=",                   /* unsigned long */
277         "/=",                   /* unsigned long */
278         "%=",                   /* unsigned long */
279         "u_long->double",       /* unsigned long */
280         ">>",                   /* unsigned long */
281         ">>=",                  /* 129 unsigned long */
282 };
283
284 /*
285  * Strings for instruction tables.
286  */
287 char mov[] = "mov";
288 char clr[] = "clr";
289 char cmp[] = "cmp";
290 char tst[] = "tst";
291 char add[] = "add";
292 char sub[] = "sub";
293 char inc[] = "inc";
294 char dec[] = "dec";
295 char mul[] = "mul";
296 char _div[] = "div";
297 char asr[] = "asr";
298 char ash[] = "ash";
299 char asl[] = "asl";
300 char bic[] = "bic";
301 char bic1[] = "bic $1,";
302 char bit[] = "bit";
303 char bit1[] = "bit $1,";
304 char bis[] = "bis";
305 char bis1[] = "bis $1,";
306 char xor[] = "xor";
307 char neg[] = "neg";
308 char com[] = "com";
309 char stdol[] = "*$";
310 char ashc[] = "ashc";
311 char slmul[] = "lmul";
312 char sldiv[] = "ldiv";
313 char slrem[] = "lrem";
314 char uldiv[] = "uldiv";
315 char ulrem[] = "ulrem";
316 char ualdiv[] = "ualdiv";
317 char ualrem[] = "ualrem";
318 char ultof[] = "ultof";
319 char ulsh[] = "ulsh";
320 char ualsh[] = "ualsh";
321 char almul[] = "almul";
322 char aldiv[] = "aldiv";
323 char alrem[] = "alrem";
324 char udiv[] = "udiv";
325 char urem[] = "urem";
326 char jeq[] = "jeq";
327 char jne[] = "jne";
328 char jle[] = "jle";
329 char jgt[] = "jgt";
330 char jlt[] = "jlt";
331 char jge[] = "jge";
332 char jlos[] = "jlos";
333 char jhi[] = "jhi";
334 char jlo[] = "jlo";
335 char jhis[] = "jhis";
336 char nop[] = "/nop";
337 char jbr[] = "jbr";
338 char jpl[] = "jpl";
339 char jmi[] = "jmi";
340 char jmijne[] = "jmi\tL%d\njne";
341 char jmijeq[] = "jmi\tL%d\njeq";
342
343 /*
344  * Instruction tables, accessed by
345  * I (first operand) or I' (second) macros.
346  */
347
348 struct instab instab[] = {
349         {LOAD,          mov,    tst},
350         {ASSIGN,        mov,    clr},
351         {EQUAL,         cmp,    tst},
352         {NEQUAL,        cmp,    tst},
353         {LESSEQ,        cmp,    tst},
354         {LESS,          cmp,    tst},
355         {GREATEQ,       cmp,    tst},
356         {GREAT,         cmp,    tst},
357         {LESSEQP,       cmp,    tst},
358         {LESSP,         cmp,    tst},
359         {GREATQP,       cmp,    tst},
360         {GREATP,        cmp,    tst},
361         {PLUS,          add,    inc},
362         {ASPLUS,        add,    inc},
363         {MINUS,         sub,    dec},
364         {ASMINUS,       sub,    dec},
365         {INCBEF,        add,    inc},
366         {DECBEF,        sub,    dec},
367         {INCAFT,        add,    inc},
368         {DECAFT,        sub,    dec},
369         {TIMES,         mul,    mul},
370         {ASTIMES,       mul,    mul},
371         {DIVIDE,        _div,   _div},
372         {ASDIV,         _div,   _div},
373         {MOD,           _div,   _div},
374         {ASMOD,         _div,   _div},
375         {PTOI,          _div,   _div},
376         {RSHIFT,        ash,    asr},
377         {ASRSH,         ash,    asr},
378         {LSHIFT,        ash,    asl},
379         {ASLSH,         ash,    asl},
380         {AND,           bic,    bic1},
381         {ANDN,          bic,    bic1},
382         {ASANDN,        bic,    bic1},
383         {TAND,          bit,    bit1},
384         {OR,            bis,    bis1},
385         {ASOR,          bis,    bis1},
386         {EXOR,          xor,    xor},
387         {ASXOR,         xor,    xor},
388         {NEG,           neg,    neg},
389         {COMPL,         com,    com},
390         {CALL1,         stdol,  stdol},
391         {CALL2,         "",     ""},
392         {LLSHIFT,       ashc,   ashc},
393         {ASLSHL,        ashc,   ashc},
394         {LTIMES,        slmul,  slmul},
395         {LDIV,          sldiv,  sldiv},
396         {LMOD,          slrem,  slrem},
397         {LASTIMES,      almul,  almul},
398         {LASDIV,        aldiv,  aldiv},
399         {LASMOD,        alrem,  alrem},
400         {ULSH,          ashc,   ashc},
401         {ASULSH,        ashc,   ashc},
402         {UDIV,          udiv,   udiv},
403         {UMOD,          urem,   urem},
404         {ASUDIV,        udiv,   udiv},
405         {ASUMOD,        urem,   urem},
406         {ULTIMES,       slmul,  slmul},         /* symmetry */
407         {ULDIV,         uldiv,  uldiv},
408         {ULMOD,         ulrem,  ulrem},
409         {ULASTIMES,     almul,  almul},         /* symmetry */
410         {ULASDIV,       ualdiv, ualdiv},
411         {ULASMOD,       ualrem, ualrem},
412         {ULTOF,         ultof,  ultof},
413         {ULLSHIFT,      ulsh,   ulsh},
414         {UASLSHL,       ualsh,  ualsh},
415         {0,             0,      0}
416 };
417
418 /*
419  * Similar table for relationals.
420  * The first string is for the positive
421  * test, the second for the inverted one.
422  * The '200+' entries are 
423  * used in tests against 0 where a 'tst'
424  * instruction is used; it clears the c-bit
425  * the c-bit so ptr tests are funny.
426  */
427 struct instab branchtab[] = {
428         {EQUAL,         jeq,    jne},
429         {NEQUAL,        jne,    jeq},
430         {LESSEQ,        jle,    jgt},
431         {LESS,          jlt,    jge},
432         {GREATEQ,       jge,    jlt},
433         {GREAT,         jgt,    jle},
434         {LESSEQP,       jlos,   jhi},
435         {LESSP,         jlo,    jhis},
436         {GREATQP,       jhis,   jlo},
437         {GREATP,        jhi,    jlos},
438         {200+EQUAL,     jeq,    jne},
439         {200+NEQUAL,    jne,    jeq},
440         {200+LESSEQ,    jmijeq, jmijne},
441         {200+LESS,      jmi,    jpl},
442         {200+GREATEQ,   jpl,    jmi},
443         {200+GREAT,     jmijne, jmijeq},
444         {200+LESSEQP,   jeq,    jne},
445         {200+LESSP,     nop,    jbr},
446         {200+GREATQP,   jbr,    nop},
447         {200+GREATP,    jne,    jeq},
448         {0,             0,      0}
449 };
450
451 int     line;
452 int     nerror;                 /* number of hard errors */
453 struct  table   lsptab[1];
454 int     nstack;
455 int     nfloat;
456 char    *funcbase;
457 char    *curbase;
458 char    *coremax;
459 long    totspace;
460 int     regpanic;               /* set when SU register alg. fails */
461 int     panicposs;              /* set when there might be need for regpanic */
462 jmp_buf jmpbuf;
463
464 int     xlab1, xlab2, xop, xzero;