gen: move most instructions to LangDef

This commit is contained in:
corey 2024-02-02 07:40:05 -06:00
parent ba3518ed50
commit 245522d2f3
4 changed files with 26 additions and 5 deletions

10
gen.c
View File

@ -992,13 +992,13 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
// Convert operator name to instruction name
if(curtok->subtype==LADD)
sprintf(operator,"addl");
sprintf(operator,"%s",gen->ld.add_32);
else if(curtok->subtype==LSMINUS)
sprintf(operator,"subl");
sprintf(operator,"%s",gen->ld.sub_32);
else if(curtok->subtype==LSMUL)
sprintf(operator,"imull");
sprintf(operator,"%s",gen->ld.mul_32);
else if(curtok->subtype==LSDIV)
sprintf(operator,"idivl");
sprintf(operator,"%s",gen->ld.div_32);
else if(curtok->subtype==LASSIGN)
sprintf(operator,"%s",gen->ld.mov_32);
else if(curtok->subtype==LSREF)
@ -1306,7 +1306,7 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
case LSMINUS:
opos+=sprintf(output_buffer+opos,"\t%s %s,%%%s\n",gen->ld.mov_32,operands[0],gen->ld.scratch1_32);
opos+=sprintf(output_buffer+opos,"\t%s %s,%s\n",gen->ld.mov_32,operands[1],operands[0]);
opos+=sprintf(output_buffer+opos,"\tsubl %%%s,%s\n",gen->ld.scratch1_32,operands[0]);
opos+=sprintf(output_buffer+opos,"\t%s %%%s,%s\n",gen->ld.sub_32,gen->ld.scratch1_32,operands[0]);
break;
case LSMUL:

7
gen.h
View File

@ -18,10 +18,16 @@ typedef struct LangDef
{
const char*acc_32;
const char*acc_wordsize;
const char*add_32;
const char*add_wordsize;
const char*bp;
const char*div_32;
const char*div_wordsize;
const char*lea_wordsize;
const char*mov_32;
const char*mov_wordsize;
const char*mul_32;
const char*mul_wordsize;
const char*pop_wordsize;
const char*push_wordsize;
const char*registers_abi_32[6];
@ -29,6 +35,7 @@ typedef struct LangDef
const char*scratch1_32;
const char*scratch1_wordsize;
const char*sp;
const char*sub_32;
const char*sub_wordsize;
}LangDef;

7
i386.c
View File

@ -8,10 +8,16 @@ LangDef gen_i386_langdef(void)
LangDef ld={
.acc_32="eax",
.acc_wordsize="eax",
.add_32="addl",
.add_wordsize="addl",
.bp="ebp",
.div_32="idivl",
.div_wordsize="idivl",
.lea_wordsize="leal",
.mov_32="movl",
.mov_wordsize="movl",
.mul_32="imull",
.mul_wordsize="imull",
.pop_wordsize="popl",
.push_wordsize="pushl",
.registers_abi_32={"edi","esi","edx","ecx","ecx","ecx"},
@ -19,6 +25,7 @@ LangDef gen_i386_langdef(void)
.scratch1_32="ecx",
.scratch1_wordsize="ecx",
.sp="esp",
.sub_32="subl",
.sub_wordsize="subl",
};
return ld;

View File

@ -10,10 +10,16 @@ LangDef gen_x86_64_langdef(void)
LangDef ld={
.acc_32="eax",
.acc_wordsize="rax",
.add_32="addl",
.add_wordsize="addq",
.bp="rbp",
.div_32="idivl",
.div_wordsize="idivq",
.lea_wordsize="leaq",
.mov_32="movl",
.mov_wordsize="movq",
.mul_32="imull",
.mul_wordsize="imulq",
.pop_wordsize="popq",
.push_wordsize="pushq",
.registers_abi_32={"edi","esi","edx","ecx","r8d","r9d"},
@ -21,6 +27,7 @@ LangDef gen_x86_64_langdef(void)
.scratch1_32="r8d",
.scratch1_wordsize="r8",
.sp="rsp",
.sub_32="subl",
.sub_wordsize="subq",
};
return ld;