gen: showeval, improved operand analysis
This commit is contained in:
parent
2134ad42e6
commit
62b853e8b1
90
gen.c
90
gen.c
@ -811,7 +811,6 @@ void gen_eval(Gen*gen,const PNode*pn,FILE*file)
|
||||
// Evaluate one operation
|
||||
void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file,Vec*stack)
|
||||
{
|
||||
uint32_t integers[2]={elem[0].val.i32,elem[1].val.i32};
|
||||
uint32_t res=0;
|
||||
LangDef ld=(gen->buildarch==M_X86_64)?gen_x86_64_langdef():gen_i386_langdef();
|
||||
|
||||
@ -819,44 +818,47 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
||||
// removing special cases and pendantic-ness
|
||||
// Create strings of operands, operators
|
||||
|
||||
/* { */
|
||||
/* char operator[1024]; */
|
||||
/* char operands[2][1024]; */
|
||||
/* */
|
||||
/* // Create operand eval_elems */
|
||||
/* for(size_t i=0;i<nops;++i) */
|
||||
/* { */
|
||||
/* if(elem[i].type==EE_VAR) */
|
||||
/* sprint(operands[i],"v::%s",elem[i].val.name); */
|
||||
/* else if(elem[i].type==EE_FUNC) */
|
||||
/* sprint(operands[i],"f::%s",elem[i].val.name); */
|
||||
/* else if(elem[i].type==EE_I32) */
|
||||
/* sprint(operands[i],"%d",elem[i].val.i32); */
|
||||
/* else */
|
||||
/* sprint(operands[i],"%%eax"); */
|
||||
/* } */
|
||||
/* */
|
||||
/* if(nops>1&&elem[0].type==EE_I32&&elem[1].type==EE_I32) */
|
||||
/* printf("[[[%d+%d=%d]]]\n",elem[1].val.i32,elem[0].val.i32,elem[1].val.i32+elem[0].val.i32); */
|
||||
/* */
|
||||
/* // Convert operator name to instruction name */
|
||||
/* if(strcmp("+",curtok->str.buffer)==0) */
|
||||
/* sprint(operator,"addl"); */
|
||||
/* else if(strcmp("-",curtok->str.buffer)==0) */
|
||||
/* sprint(operator,"subl"); */
|
||||
/* else if(strcmp("*",curtok->str.buffer)==0) */
|
||||
/* sprint(operator,"imull"); */
|
||||
/* else if(strcmp("/",curtok->str.buffer)==0) */
|
||||
/* sprint(operator,"idivl"); */
|
||||
/* else if(strcmp("=",curtok->str.buffer)==0) */
|
||||
/* sprint(operator,"movl"); */
|
||||
/* */
|
||||
/* // Print output */
|
||||
/* if(nops>1) */
|
||||
/* printf("%s %s, %s\n",operator,operands[0],operands[1]); */
|
||||
/* else */
|
||||
/* printf("%s %s\n",operator,operands[0]); */
|
||||
/* } */
|
||||
// Analyze and print evaluation "steps"
|
||||
if(gen->showeval)
|
||||
{
|
||||
char operator[1024];
|
||||
char operands[2][1024];
|
||||
|
||||
// Create operand eval_elems
|
||||
for(size_t i=0;i<nops;++i)
|
||||
{
|
||||
switch(elem[i].type)
|
||||
{
|
||||
case EE_VAR: sprint(operands[i],"-%u(%s)",elem[i].vstackloc,ld.bp); break;
|
||||
case EE_FUNC: sprint(operands[i],"%s()",elem[i].val.name); break;
|
||||
case EE_IDENT: sprint(operands[i],"%s?",elem[i].val.name); break;
|
||||
case EE_I32: sprint(operands[i],"$%d",elem[i].val.i32); break;
|
||||
case EE_ACC: sprint(operands[i],"%%eax"); break;
|
||||
default: sprint(operands[i],"???"); break;
|
||||
}
|
||||
}
|
||||
|
||||
/* if(nops>1&&elem[0].type==EE_I32&&elem[1].type==EE_I32) */
|
||||
/* printf("[[[%d+%d=%d]]]\n",elem[1].val.i32,elem[0].val.i32,elem[1].val.i32+elem[0].val.i32); */
|
||||
|
||||
// Convert operator name to instruction name
|
||||
if(strcmp("+",curtok->str.buffer)==0)
|
||||
sprint(operator,"addl");
|
||||
else if(strcmp("-",curtok->str.buffer)==0)
|
||||
sprint(operator,"subl");
|
||||
else if(strcmp("*",curtok->str.buffer)==0)
|
||||
sprint(operator,"imull");
|
||||
else if(strcmp("/",curtok->str.buffer)==0)
|
||||
sprint(operator,"idivl");
|
||||
else if(strcmp("=",curtok->str.buffer)==0)
|
||||
sprint(operator,"movl");
|
||||
|
||||
// Print output
|
||||
if(nops>1)
|
||||
printf("%s %s, %s\n",operator,operands[0],operands[1]);
|
||||
else
|
||||
printf("%s %s\n",operator,operands[0]);
|
||||
}
|
||||
|
||||
// UNARY OPS
|
||||
if(nops==1)
|
||||
@ -876,7 +878,7 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
||||
}
|
||||
else if(elem[0].type==EE_I32)
|
||||
{
|
||||
fprintf(file,"\tmovl $%d,%%eax\n",integers[0]);
|
||||
fprintf(file,"\tmovl $%d,%%eax\n",elem[0].val.i32);
|
||||
fprintf(file,"\tcmpl $0,%%eax\n");
|
||||
fprintf(file,"\tsete %%al\n");
|
||||
fprintf(file,"\tmovzbl %%al,%%eax\n");
|
||||
@ -902,10 +904,10 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
||||
switch(curtok->str.buffer[0])
|
||||
{
|
||||
|
||||
case '+':res=integers[1]+integers[0];break;
|
||||
case '-':res=integers[1]-integers[0];break;
|
||||
case '*':res=integers[1]*integers[0];break;
|
||||
case '/':res=integers[1]/integers[0];break;
|
||||
case '+':res=elem[1].val.i32+elem[0].val.i32;break;
|
||||
case '-':res=elem[1].val.i32-elem[0].val.i32;break;
|
||||
case '*':res=elem[1].val.i32*elem[0].val.i32;break;
|
||||
case '/':res=elem[1].val.i32/elem[0].val.i32;break;
|
||||
|
||||
}
|
||||
eval_elem tmpelem={.type=EE_I32,.val.i32=res};
|
||||
|
1
gen.h
1
gen.h
@ -20,6 +20,7 @@ typedef struct Gen
|
||||
size_t labelno;
|
||||
uint32_t buildarch;
|
||||
bool showrpn;
|
||||
bool showeval;
|
||||
} Gen;
|
||||
|
||||
// Type of elements in evaluation of RPN Vec
|
||||
|
6
main.c
6
main.c
@ -18,6 +18,7 @@ int main(int argc,char**argv)
|
||||
bool showparsetreebrief=false;
|
||||
bool showtokens=false;
|
||||
bool showrpn=false;
|
||||
bool showeval=false;
|
||||
char*setoutfile_name=NULL;
|
||||
int skip_arg=0;
|
||||
uint32_t buildarch=M_X86_64;
|
||||
@ -122,6 +123,10 @@ int main(int argc,char**argv)
|
||||
showrpn=true;
|
||||
break;
|
||||
|
||||
case 'e':
|
||||
showeval=true;
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
puts(HELPMSG);
|
||||
cleanquit(0);
|
||||
@ -150,6 +155,7 @@ int main(int argc,char**argv)
|
||||
state.gen.rootnode=&state.parser.root;
|
||||
state.gen.buildarch=buildarch;
|
||||
state.gen.showrpn=showrpn;
|
||||
state.gen.showeval=showeval;
|
||||
|
||||
state.infile=fopen(*vec_at(&args,i,const char**),"r");
|
||||
if(!state.infile)
|
||||
|
Loading…
Reference in New Issue
Block a user