Skip to content

Commit afe2238

Browse files
committed
Semantic checks refactored extensively. Found and fixed bugs in semanticChecks.c file. Finally, call by reference information is stored in Function argument list, and will reflect in Local Symbol table when arguments are installed
1 parent 5a3bec1 commit afe2238

File tree

12 files changed

+483
-184
lines changed

12 files changed

+483
-184
lines changed

Main/SILsource1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
decl
2-
integer swap(integer &a,&b);
2+
integer swap(integer &a,&b),arr[5];
33
enddecl
44

55
integer swap(integer &a,&b){
@@ -22,14 +22,14 @@ integer main(){
2222
enddecl
2323

2424
begin
25-
read(x);
26-
read(y);
25+
read(arr[1]);
26+
read(arr[2]);
2727

28-
flg = swap(&x,&y);
28+
flg = swap(&arr[1],&arr[2]);
2929

3030
if(flg==1) then
31-
write(x);
32-
write(y);
31+
write(arr[1]);
32+
write(arr[2]);
3333
endif;
3434

3535
return 1;

Main/SIMCode

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
START
2+
HALT

SILCompiler/LexYaccFiles/SIL.y

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,11 @@
3535

3636
%%
3737
Prog : GDefblock FdefList Mainblock
38-
{ printf("DONE");
39-
compile($1,$2,$3);
38+
{ compile($1,$2,$3);
4039
}
4140

4241
| GDefblock Mainblock
43-
{ printf("DONE");
44-
compile($1,NULL,$2);
42+
{ compile($1,NULL,$2);
4543
}
4644

4745
;
@@ -190,7 +188,7 @@ ArgId : ID
190188
}
191189

192190
| '&' ID
193-
{ $$ = TreeCreate(0,IDALIASARG,$2,0,NULL,NULL,NULL,NULL,line);
191+
{ $$ = TreeCreate(0,IDADDRARG,$2,0,NULL,NULL,NULL,NULL,line);
194192
}
195193

196194
;

SILCompiler/codeGen.c

Lines changed: 97 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,29 @@ void funcCodeGen(Tnode *root)
2020

2121
case FUNCBLOCK : gnode = Glookup(root->NAME);
2222

23-
locneg = 0;
23+
locneg = -2;
2424
argInstallLocal(gnode->ARGLIST,&Lhead);
2525

2626
locpos = 0;
27+
idstatus = 0;
2728
localDecInstall(root->Ptr2,&Lhead);
2829

2930
fprintf("fp,Label%d:\n",gnode->BINDING);
3031
codeGenerate(root->Ptr3);
32+
33+
return;
3134

32-
case MAINBLOCK : locpos = -1;
33-
localDecInstall(root->Ptr1,&Lhead);
35+
case MAINBLOCK : fprintf(fp,"MOV SP -1");
36+
pushGlobalVar();
3437

35-
fprintf(fp,"MOV SP %d",maxbind);
3638
fprintf(fp,"MOV BP SP");
3739
fprintf(fp,"PUSH BP");
3840

41+
locpos = -1;
42+
localDecInstall(root->Ptr1,&Lhead);
43+
44+
pushLocalVar(&Lhead);
45+
3946
regcnt = 0;
4047
codeGenerate(root->Ptr2,&Lhead);
4148
}
@@ -56,11 +63,33 @@ void argInstallLocal(ArgStruct *HEAD,struct Lsymbol **Lhead)
5663
}
5764
}
5865

59-
int codeGenerate(Tnode *root)
66+
void pushAllRegs()
67+
{
68+
int i = 0;
69+
70+
while(i <= regcnt)
71+
{
72+
fprintf(fp,"PUSH R%d\n",i);
73+
i++;
74+
}
75+
}
76+
77+
void funcCodeGen(char *NAME)
78+
{
79+
struct Tnode *node;
80+
81+
82+
83+
fprintf("fp,Label%d:\n",gnode->BINDING);
84+
codeGenerate(node->);
85+
}
86+
87+
int codeGenerate(Tnode *root,struct Lsymbol **Lhead)
6088
{
6189
int loc,r,r1,r2;
6290
int lbl1,lbl2;
6391
struct Gsymbol *gnode;
92+
struct Lsymbol *lnode;
6493

6594
if(root==NULL)
6695
return;
@@ -72,18 +101,15 @@ int codeGenerate(Tnode *root)
72101

73102
return;
74103

75-
case FUNCCALL : gnode = Glookup(root->NAME);
76-
Arghead = gnode->ARGLIST;
104+
case FUNCCALL : pushAllRegs();
77105

78-
struct Lsymbol *Ltable;
79-
Ltable = NULL;
106+
pushCallParams(root->Ptr1,Lhead);
80107

81-
if(Arghead != NULL)
82-
funcParamInstall(root->Ptr1,Lhead,&Ltable);
108+
CALL LABEL 1;
83109

84-
tempnode = searchFunc(root->NAME,funcroot);
85-
return interpreter(tempnode,&Ltable);
110+
return;
86111

112+
//Call by Reference
87113
case IDADDR : lnode = Llookup(root->NAME,Lhead);
88114
gnode = Glookup(root->NAME);
89115

@@ -310,3 +336,61 @@ int codeGenerate(Tnode *root)
310336
return r;
311337
}
312338
}
339+
340+
void pushGlobalVar()
341+
{
342+
struct Gsymbol *ptr = Ghead;
343+
int i;
344+
345+
while(ptr)
346+
{
347+
if(ptr->SIZE >= 0)
348+
{
349+
i = 0;
350+
do{
351+
r = getReg();
352+
fprintf(fp,"PUSH R%d\n",r);
353+
freeReg();
354+
}while(i++ < ptr->SIZE);
355+
}
356+
357+
ptr = ptr->NEXT;
358+
}
359+
}
360+
361+
void pushLocalVar(&Lhead)
362+
{
363+
struct Lsymbol *ptr = Lhead;
364+
365+
while(ptr)
366+
{
367+
r = getReg();
368+
fprintf(fp,"PUSH R%d\n",r);
369+
freeReg();
370+
371+
ptr = ptr->NEXT;
372+
}
373+
}
374+
375+
/* Push function call parameters in reverse
376+
*/
377+
void pushCallParams(Tnode *root,struct Lsymbol **Lhead)
378+
{
379+
if(root == NULL)
380+
return;
381+
382+
switch(root->NODETYPE)
383+
{
384+
case CONTINUE : pushCallParams(root->Ptr2,Lhead);
385+
pushCallParams(root->Ptr1,Lhead);
386+
break;
387+
388+
case FUNCPARAM : r1 = codeGenerate(root->Ptr1,Lhead);
389+
r2 = getReg();
390+
fprintf(fp,"MOV R%d R%d\n",r2,r1);
391+
fprintf(fp,"PUSH R%d\n",r2);
392+
freeReg();
393+
freeReg();
394+
break;
395+
}
396+
}

SILCompiler/codeGen.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@
1010
void funcCodeGen(Tnode *root);
1111
int argInstallLocal(ArgStruct *Arghead,struct Lsymbol **Lhead);
1212
int codeGenerate(Tnode *root);
13+
void pushLocalVar(struct Lsymbol **Lhead);
14+
void pushGlobalVar();
1315

1416
#endif

SILCompiler/compilerLib.c

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void globalInstall(Tnode *root)
3636
{
3737
struct Gsymbol *gnode;
3838
ArgStruct *arg;
39+
int typ;
3940

4041
if(root == NULL)
4142
return;
@@ -58,7 +59,7 @@ void globalInstall(Tnode *root)
5859

5960
if(gnode != NULL)
6061
{
61-
if(root->TYPE != gnode->TYPE)
62+
if(decnode->TYPE != gnode->TYPE)
6263
printf("\nSIL:%d: Error: conflicting types for '%s'",root->LINE,root->NAME);
6364
else
6465
printf("\nSIL:%d: Error: redeclaration of '%s'",root->LINE,root->NAME);
@@ -69,7 +70,12 @@ void globalInstall(Tnode *root)
6970
globalInstall(root->ArgList);
7071

7172
if(gnode == NULL)
73+
{
74+
if((root->NODETYPE == ARRAYDECL) && (root->VALUE == 0))
75+
printf("\nSIL:%d: Warning: size of array '%s' is ZERO",root->LINE,root->NAME);
76+
7277
Ginstall(root->NAME,decnode->TYPE,root->VALUE,Arghead);
78+
}
7379

7480
break;
7581

@@ -78,13 +84,29 @@ void globalInstall(Tnode *root)
7884
globalInstall(root->Ptr2);
7985
break;
8086

81-
case IDALIASARG :
87+
case IDADDRARG : typ = returnAddrType(argnode->TYPE);
88+
89+
arg = argLookup(root->NAME,Arghead);
90+
91+
if(arg != NULL)
92+
{
93+
if(typ != arg->TYPE)
94+
printf("\nSIL:%d: Error: conflicting types for '%s'",root->LINE,root->NAME);
95+
else
96+
printf("\nSIL:%d: Error: redefinition of parameter '%s'",root->LINE,root->NAME);
97+
98+
error++;
99+
}
100+
101+
else argInstall(root->NAME,typ);
102+
103+
break;
82104

83105
case IDARG : arg = argLookup(root->NAME,Arghead);
84106

85107
if(arg != NULL)
86108
{
87-
if(root->TYPE != arg->TYPE)
109+
if(argnode->TYPE != arg->TYPE)
88110
printf("\nSIL:%d: Error: conflicting types for '%s'",root->LINE,root->NAME);
89111
else
90112
printf("\nSIL:%d: Error: redefinition of parameter '%s'",root->LINE,root->NAME);
@@ -167,7 +189,7 @@ struct Gsymbol *Glookup(char *NAME)
167189
/* Install an identifier in the global symbol table
168190
*/
169191
void Ginstall(char *NAME,int TYPE,int SIZE,ArgStruct *ARGLIST)
170-
{
192+
{
171193
int i;
172194
struct Gsymbol *Gnode = malloc(sizeof(struct Gsymbol));
173195

@@ -294,6 +316,74 @@ void Gallocate()
294316
}
295317
}
296318

319+
320+
/* Returns appropriate type for identifier addresses
321+
*/
322+
int returnAddrType(int TYPE)
323+
{
324+
if(TYPE == INTGR)
325+
return INTGRALIAS;
326+
327+
else if(TYPE == BOOL)
328+
idtype = BOOLALIAS;
329+
330+
else return;
331+
}
332+
333+
334+
/* Lookup an AST function node in the function nodes list (having head *Funchead)
335+
*/
336+
Tnode *funcLookup(char *NAME)
337+
{
338+
FuncStruct *ptr = Funchead;
339+
340+
while(ptr)
341+
{
342+
if(!strcmp(ptr->FUNCNODE->NAME,NAME))
343+
return ptr->FUNCNODE;
344+
345+
ptr = ptr->NEXT;
346+
}
347+
348+
return NULL;
349+
}
350+
351+
352+
/* Add an AST function node to the function nodes list (having head *Funclist)
353+
*/
354+
void addToFuncList(Tnode *root)
355+
{
356+
Tnode *func;
357+
358+
func = funcLookup(root->NAME);
359+
360+
if(func != NULL)
361+
{
362+
if(func->TYPE != root->Ptr1->TYPE)
363+
printf("\nSIL:%d: Error: conflicting types for function '%s'",root->LINE,root->NAME);
364+
else
365+
printf("\nSIL:%d: Error: redefinition of function '%s'",root->LINE,root->NAME);
366+
367+
error++;
368+
}
369+
370+
else funcInstall(root);
371+
}
372+
373+
374+
/* Install AST function node in function nodes list (having head *Funclist)
375+
*/
376+
void funcInstall(Tnode *func)
377+
{
378+
FuncStruct *Node = malloc(sizeof(FuncStruct));
379+
380+
Node->FUNCNODE = func;
381+
382+
Node->NEXT = Funchead;
383+
Funchead = Node;
384+
}
385+
386+
297387
int getPositiveLoc()
298388
{
299389
++locpos;

0 commit comments

Comments
 (0)