B compiler development

More stuff working.
This commit is contained in:
rswier
2016-03-11 00:29:01 -05:00
parent 97f29f046c
commit c62890ab56
+57 -131
View File
@@ -27,13 +27,13 @@ int fin = 0;
int fout = 1; int fout = 1;
int symtab[500] = { /* , type = 1, value = 1, name = 4 */ int symtab[500] = { /* , type = 1, value = 1, name = 4 */
1, 5,0,0,'a','u','t','o',0, 1, 5,'a','u','t','o',0,
1, 6,0,0,'e','x','t','r','n',0, 1, 6,'e','x','t','r','n',0,
1,10,0,0,'g','o','t','o',0, 1,10,'g','o','t','o',0,
1,11,0,0,'r','e','t','u','r','n',0, 1,11,'r','e','t','u','r','n',0,
1,12,0,0,'i','f',0, 1,12,'i','f',0,
1,13,0,0,'w','h','i','l','e',0, 1,13,'w','h','i','l','e',0,
1,14,0,0,'e','l','s','e',0, 1,14,'e','l','s','e',0,
0 0
}; };
@@ -67,6 +67,7 @@ int *csym = 0;
int cval = 0; int cval = 0;
int isn = 1; int isn = 1;
int nerror = 0; int nerror = 0;
int nauto = 0;
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (argc > 1) { if (argc > 1) {
@@ -96,7 +97,7 @@ int *lookup() {
rp = symtab; rp = symtab;
while (*rp) { while (*rp) {
np = rp + 4; np = rp + 2;
sp = symbuf; sp = symbuf;
while (*np==*sp) { while (*np==*sp) {
if (!*np) if (!*np)
@@ -109,7 +110,7 @@ int *lookup() {
rp = np + 1; rp = np + 1;
} }
sp = symbuf; sp = symbuf;
np = rp + 4; np = rp + 2;
if (np >= symtab + 490) { if (np >= symtab + 490) {
error("sf"); error("sf");
exit(1); exit(1);
@@ -312,26 +313,25 @@ void expr(int lev) {
if (o==21) { /* constant */ if (o==21) { /* constant */
case21: case21:
gen('n',5); /* litrl */ gen('n',5); /* litrl */
xwrite(' ');
number(cval); number(cval);
xwrite('\n'); xwrite('\n');
goto loop; goto loop;
} }
if (o==20) { /* name */ if (o==20) { /* name */
if (*csym==0) { if (*csym==0) { /* not seen */
if((peeksym=symbol())==6) { /* ( */ // if((peeksym=symbol())==6) { /* ( */
*csym = 6; /* extern */ *csym = 6; /* extern */
} // }
else { // else {
if(csym[2]==0) /* unseen so far */ // if(csym[2]==0) /* unseen so far */
csym[2] = isn++; // csym[2] = isn++;
} // }
} }
if(*csym==6) /* extern */ if(*csym==6) /* extern */
gens('x',csym+4); gens('x',csym+2);
else else
gen('a',csym+4); gen('a',csym[1]);
goto loop; goto loop;
} }
@@ -446,25 +446,15 @@ loop:
peeksym = o; peeksym = o;
} }
#if 0 declare(kw) {
// extern csym[], symbol, paraml[], parame[];
decl(kw) { // extern error, cval, peeksym, exit;
extern csym[], symbol, paraml[], parame[]; // int t[], n, o;
extern error, cval, peeksym, exit; int o;
int t[], n, o;
while((o=symbol())==20) { /* name */ while((o=symbol())==20) { /* name */
if(kw>=5) { /* type or sort? */
if(*csym>0)
error('rd'); //error("%p redeclared", csym[4]);
*csym = kw; *csym = kw;
} else { #if 0
if ((csym[1]&017)!=0)
error('rd'); //error("%p redeclared", &csym[4]);
csym[1] =| csym[1]&0760 | kw;
if (*csym==0)
*csym = -2;
}
while((o=symbol())==4) { /* [ */ while((o=symbol())==4) { /* [ */
if((o=symbol())==21) { /* const */ if((o=symbol())==21) { /* const */
if(csym[1]>=020) if(csym[1]>=020)
@@ -474,43 +464,26 @@ decl(kw) {
} }
if (o!=5) /* ] */ if (o!=5) /* ] */
goto syntax; goto syntax;
csym[1] =+ 020; csym[1] += 020;
} }
if(kw==8) { /* parameter */ #endif
if(kw==5) { /* auto */
csym[1] = nauto;
nauto++;
*csym = -1; *csym = -1;
if (paraml==0)
paraml = csym;
else
*parame = csym;
parame = csym;
} }
if (o!=9) /* , */ if (o!=9) /* , */
break; goto done;
} }
if(o==1 & kw!=8 | o==7 & kw==8) done:
if(o==1 | o==7) /* ; or ')' */
return; return;
syntax: syntax:
error('[]'); //error("Declaration syntax"); error('[]'); //error("Declaration syntax");
} }
#endif
function() {
// extern declare, blkhed, blkend;
// extern printf, statement, peeksym, cval, symbol;
// extern paraml;
// auto o;
// printf(".text; 1:mov r5,-(sp); mov sp,r5\n");
// declare(8);
// declist();
gen('s',2); /* ??? */
statement(1);
gen('n',7); /* retrn */
}
extdef() { extdef() {
// extern eof, function, cval; // extern eof, cval;
// extern symbol, block, printf, pname, csym[]; // extern symbol, block, printf, pname, csym[];
// extern error; // extern error;
auto o, c, *cs; auto o, c, *cs;
@@ -524,7 +497,7 @@ extdef() {
goto syntax; goto syntax;
csym[0] = 6; csym[0] = 6;
cs = csym + 4; cs = csym + 2;
xwrite('.'); xwrite('.');
name(cs); name(cs);
xwrite(': '); xwrite(': ');
@@ -534,13 +507,18 @@ extdef() {
o=symbol(); o=symbol();
if (o==6) { /* ( */ if (o==6) { /* ( */
function(); nauto = 2;
declare(5);
statement(1);
gen('n',7); /* retrn */
return; return;
} }
if (o==2) { /* $( */ if (o==2) { /* $( */
peeksym = o; peeksym = o;
function(); nauto = 2;
statement(1);
gen('n',7); /* retrn */
return; return;
} }
@@ -617,8 +595,10 @@ stmt:
return; return;
if (o==2) { /* { */ if (o==2) { /* { */
if(d) if(d) {
blkhed(); declist();
gen('s',nauto); /* setop */
}
while (!eof) { while (!eof) {
o = symbol(); o = symbol();
if (o==3) /* } */ if (o==3) /* } */
@@ -632,14 +612,11 @@ stmt:
if (o==19) { /* keyword */ if (o==19) { /* keyword */
#if 0
if (cval==10) { /* goto */ if (cval==10) { /* goto */
o1 = block(1,102,0,0,tree()); expr(15);
// rcexpr(o1, regtab); gen('n',6); /* goto */
goto semi; goto semi;
} }
#endif
if (cval==11) { /* return */ if (cval==11) { /* return */
if((peeksym=symbol())==6) /* ( */ if((peeksym=symbol())==6) /* ( */
@@ -688,7 +665,6 @@ stmt:
} }
if (o==20) { /* name */ if (o==20) { /* name */
#if 0
if (peekc==':') { if (peekc==':') {
peekc = 0; peekc = 0;
if (csym[0]>0) { if (csym[0]>0) {
@@ -699,14 +675,14 @@ stmt:
csym[1] = 020; /* int[] */ csym[1] = 020; /* int[] */
// if (csym[2]==0) // if (csym[2]==0)
// csym[2] = isn++; // csym[2] = isn++;
slabel(); // slabel();
goto stmt; goto stmt;
} }
#endif
} }
peeksym = o; peeksym = o;
expr(15); expr(15);
gen('s',nauto); /* setop */
semi: semi:
o = symbol(); o = symbol();
@@ -730,66 +706,17 @@ pexpr()
error('()'); error('()');
} }
blkhed() declist()
{ {
#if 0 // extern peeksym, peekc, csym[], cval;
extern symbol, cval, declare, peeksym, paraml[], parame[]; auto o;
extern error, length, rlength, setstk, defvec, isn, defstat;
extern stack, hshtab[], hshsiz, pssiz;
int o, al, pl, cs[], hl;
declist(); while((o=symbol())==19 & cval<10)
stack = al = -2; declare(cval);
pl = 4; peeksym = o;
while(paraml) {
*parame = 0;
paraml = *(cs = paraml);
cs[2] = pl;
*cs = 10;
pl =+ rlength(cs[1]);
}
cs = hshtab;
hl = hshsiz;
while(hl--) {
if (cs[4])
switch(cs[0]) {
/* sort unmentioned */
case 0177776: /* -2 */
cs[0] = 5; /* auto */
/* auto */
case 5:
if (cs[3]) { /* vector */
al =- (cs[3]*length(cs[1]-020)+1) & 077776;
setstk(al);
defvec(al);
}
cs[2] = al;
al =- rlength(cs[1]);
goto loop;
/* parameter */
case 10:
cs[0] = 5;
goto loop;
/* static */
case 7:
cs[2] = isn++;
defstat(cs);
goto loop;
loop:;
}
cs = cs+pssiz;
}
setstk(al);
#endif
} }
gens(o,n) { gens(o,n) {
xwrite(' ');
xwrite(o); xwrite(o);
xwrite(' .'); xwrite(' .');
name(n); name(n);
@@ -797,7 +724,6 @@ gens(o,n) {
} }
gen(o,n) { gen(o,n) {
xwrite(' ');
xwrite(o); xwrite(o);
xwrite(' '); xwrite(' ');
number(n); number(n);
@@ -862,7 +788,7 @@ error(code)
xwrite(code); xwrite(code);
xwrite(' '); xwrite(' ');
if (code=='rd' | code=='un') { if (code=='rd' | code=='un') {
name(csym + 4); name(csym + 2);
xwrite(' '); xwrite(' ');
} }
printn(line); printn(line);