evalop: convert if/else to switch statement
This commit is contained in:
parent
468a4232af
commit
a9352fdeb3
445
gen.c
445
gen.c
@ -925,241 +925,228 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
||||
}
|
||||
|
||||
// INT OP INT
|
||||
else if(addrmode==AM(EE_I32,EE_I32))
|
||||
else if(nops>1)
|
||||
{
|
||||
switch(curtok->subtype)
|
||||
switch(addrmode)
|
||||
{
|
||||
case AM(EE_I32,EE_I32):
|
||||
{
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case LADD:res=elem[1].val.i32+elem[0].val.i32;break;
|
||||
case LSMINUS:res=elem[1].val.i32-elem[0].val.i32;break;
|
||||
case LSMUL:res=elem[1].val.i32*elem[0].val.i32;break;
|
||||
case LSDIV:res=elem[1].val.i32/elem[0].val.i32;break;
|
||||
case LADD:res=elem[1].val.i32+elem[0].val.i32;break;
|
||||
case LSMINUS:res=elem[1].val.i32-elem[0].val.i32;break;
|
||||
case LSMUL:res=elem[1].val.i32*elem[0].val.i32;break;
|
||||
case LSDIV:res=elem[1].val.i32/elem[0].val.i32;break;
|
||||
|
||||
}
|
||||
tmpelem=(eval_elem){.type=EE_I32,.val.i32=res};
|
||||
vec_push(stack,&tmpelem);
|
||||
}
|
||||
break;
|
||||
|
||||
// INT OP ACC
|
||||
// ACC OP INT
|
||||
case AM(EE_I32,EE_ACC):
|
||||
case AM(EE_ACC,EE_I32):
|
||||
{
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
if(elem[0].type==EE_I32)
|
||||
{
|
||||
eval_elem etmp=elem[0];
|
||||
elem[0]=elem[1];
|
||||
elem[1]=etmp;
|
||||
|
||||
char btmp[64]={0};
|
||||
strcpy(btmp,operands[0]);
|
||||
strcpy(operands[0],operands[1]);
|
||||
strcpy(operands[1],btmp);
|
||||
}
|
||||
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case LASSIGN:
|
||||
err_log("%u: cannot assign to rvalue '%d'",vec_at(&pn->tokens,0,Tok*)->line,operands[1]);
|
||||
break;
|
||||
|
||||
case LADD:
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LSMUL:
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%%s\n",operands[1],ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %%%s\n",operator,ld.scratch1);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
// INT OP IDENT
|
||||
|
||||
case AM(EE_VAR,EE_I32):
|
||||
{
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case LASSIGN:
|
||||
err_log("%u: cannot assign to rvalue '%s'",vec_at(&pn->tokens,0,Tok*)->line,operands[1]);
|
||||
break;
|
||||
|
||||
case LADD:
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LSMUL:
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%%s\n",operands[1],ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %%%s\n",operator,ld.scratch1);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
// IDENT OP INT
|
||||
case AM(EE_I32,EE_VAR):
|
||||
{
|
||||
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case LASSIGN:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%s\n",operator,operands[0],operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LADD:
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LSMUL:
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%%s\n",operands[0],ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %%%s\n",operator,ld.scratch1);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
// IDENT OP IDENT
|
||||
case AM(EE_VAR,EE_VAR):
|
||||
{
|
||||
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case LASSIGN:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %%eax,%s\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LADD:
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LSMUL:
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
// IDENT OP ACC
|
||||
case AM(EE_VAR,EE_ACC):
|
||||
{
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case LASSIGN:
|
||||
case LADD:
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%s\n",operator,operands[0],operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LSMUL:
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
// ACC OP IDENT
|
||||
case AM(EE_ACC,EE_VAR):
|
||||
{
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case LASSIGN:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%s\n",operator,operands[0],operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LADD:
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%s\n",operator,operands[1],operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LSMUL:
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
tmpelem=(eval_elem){.type=EE_I32,.val.i32=res};
|
||||
vec_push(stack,&tmpelem);
|
||||
}
|
||||
|
||||
// INT OP ACC
|
||||
// ACC OP INT
|
||||
else if(addrmode==AM(EE_I32,EE_ACC)||addrmode==AM(EE_ACC,EE_I32))
|
||||
{
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
if(elem[0].type==EE_I32)
|
||||
{
|
||||
eval_elem etmp=elem[0];
|
||||
elem[0]=elem[1];
|
||||
elem[1]=etmp;
|
||||
|
||||
char btmp[64]={0};
|
||||
strcpy(btmp,operands[0]);
|
||||
strcpy(operands[0],operands[1]);
|
||||
strcpy(operands[1],btmp);
|
||||
}
|
||||
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case LASSIGN:
|
||||
err_log("%u: cannot assign to rvalue '%d'",vec_at(&pn->tokens,0,Tok*)->line,operands[1]);
|
||||
break;
|
||||
|
||||
case LADD:
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LSMUL:
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%%s\n",operands[1],ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %%%s\n",operator,ld.scratch1);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// INT OP IDENT
|
||||
else if(addrmode==AM(EE_VAR,EE_I32))
|
||||
{
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case LASSIGN:
|
||||
err_log("%u: cannot assign to rvalue '%s'",vec_at(&pn->tokens,0,Tok*)->line,operands[1]);
|
||||
break;
|
||||
|
||||
case LADD:
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LSMUL:
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%%s\n",operands[1],ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %%%s\n",operator,ld.scratch1);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// IDENT OP INT
|
||||
else if(addrmode==AM(EE_I32,EE_VAR))
|
||||
{
|
||||
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case LASSIGN:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%s\n",operator,operands[0],operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LADD:
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LSMUL:
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%%s\n",operands[0],ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %%%s\n",operator,ld.scratch1);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// IDENT OP IDENT
|
||||
else if(addrmode==AM(EE_VAR,EE_VAR))
|
||||
{
|
||||
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case LASSIGN:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %%eax,%s\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LADD:
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LSMUL:
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
}
|
||||
fwrite(output_buffer,1,opos,file);
|
||||
|
||||
}
|
||||
|
||||
// IDENT OP ACC
|
||||
else if(addrmode==AM(EE_VAR,EE_ACC))
|
||||
{
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case LASSIGN:
|
||||
case LADD:
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%s\n",operator,operands[0],operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LSMUL:
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// I32 OP ACC
|
||||
else if(addrmode==AM(EE_ACC,EE_I32))
|
||||
{
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case LASSIGN:
|
||||
err_log("%u: cannot assign to accumulator",vec_at(&pn->tokens,0,Tok*)->line,tmpelem.val.name);
|
||||
/* vec_push(stack,&tmpelem); */
|
||||
break;
|
||||
|
||||
case LADD:
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%s\n",operator,operands[0],operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LSMUL:
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ACC OP IDENT
|
||||
else if(addrmode==AM(EE_ACC,EE_VAR))
|
||||
{
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case LASSIGN:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%s\n",operator,operands[0],operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LADD:
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%s\n",operator,operands[1],operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case LSMUL:
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fwrite(output_buffer,1,opos,file);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user