gen: use subtypes, operator buffer
This commit is contained in:
parent
eb4b29f964
commit
f479dcb5b5
50
TODO
50
TODO
|
@ -6,38 +6,28 @@ print('hello');
|
|||
into an intermediate form that can be used programmatically
|
||||
___________________________________________________________
|
||||
|
||||
Lexer
|
||||
|
||||
* Find patterns of characters [DONE]
|
||||
* Define syntactical types [DONE]
|
||||
* Preidentifiers --> (identifier|keyword) [DONE]
|
||||
* Fix string type [DONE]
|
||||
Lexer-----------------------
|
||||
1. Refactor using buffering
|
||||
|
||||
Parser
|
||||
|
||||
* Find patterns of lexemes [DONE]
|
||||
* Store parse data in grammar tree [DONE]
|
||||
* Define grammatical structures [DONE]
|
||||
Parser----------------------
|
||||
|
||||
Code Generator
|
||||
|
||||
* Code generator state
|
||||
- File output buffer
|
||||
> Set a block size and only output when full
|
||||
- Variable data
|
||||
> Types & type annotation (i32, i8, string, references,...)
|
||||
- Function data [DONE]
|
||||
- "Run" build architecture
|
||||
> Virtual machine for intermediate representation/language
|
||||
- "Intermediate build representation/architecture/language"
|
||||
> Smart pointers (references/borrow checking)?
|
||||
* x86_64-linux ABI
|
||||
- Abstract away build architectures
|
||||
> expression evaluation
|
||||
>> Consider completely redoing eval
|
||||
>> Function evaluation: handle as 0 operand instruction?
|
||||
> basic code generation [DONE]
|
||||
- Stack frames [DONE]
|
||||
- Expression Evaluation [DONE]
|
||||
- FUNCTION CALLS
|
||||
> Convert argument lists using ABI
|
||||
Code Generator--------------
|
||||
1. Code generator state
|
||||
File output buffer
|
||||
Set a block size and only output when full
|
||||
Variable data
|
||||
Types & type annotation (i32, i8, string, references,...)
|
||||
"Run" build architecture
|
||||
Virtual machine for intermediate representation/language
|
||||
"Intermediate build representation/architecture/language"
|
||||
Smart pointers (references/borrow checking)?
|
||||
2. x86_64-linux ABI
|
||||
Abstract away build architectures
|
||||
expression evaluation
|
||||
Consider completely redoing eval
|
||||
Function evaluation: handle as 0 operand instruction?
|
||||
FUNCTION CALLS
|
||||
Convert argument lists using ABI
|
||||
|
|
154
gen.c
154
gen.c
|
@ -874,9 +874,9 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
|||
// UNARY OPS
|
||||
if(nops==1)
|
||||
{
|
||||
switch(curtok->str.buffer[0])
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
case '!':
|
||||
case LSNOT:
|
||||
if(elem[0].type==EE_VAR)
|
||||
{
|
||||
opos+=sprintf(output_buffer+opos,"\tcmpl $0,%s\n",operands[0]);
|
||||
|
@ -912,13 +912,13 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
|||
// INT OP INT
|
||||
else if(elem[0].type==EE_I32&&elem[1].type==EE_I32)
|
||||
{
|
||||
switch(curtok->str.buffer[0])
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
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;
|
||||
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};
|
||||
|
@ -945,34 +945,34 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
|||
strcpy(operands[1],btmp);
|
||||
}
|
||||
|
||||
switch(curtok->str.buffer[0])
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case '=':
|
||||
case LASSIGN:
|
||||
err_log("%u: cannot assign to rvalue '%d'",vec_at(&pn->tokens,0,Tok*)->line,operands[1]);
|
||||
break;
|
||||
|
||||
case '+':
|
||||
opos+=sprintf(output_buffer+opos,"\taddl %s,%%eax\n",operands[1]);
|
||||
case LADD:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '-':
|
||||
opos+=sprintf(output_buffer+opos,"\tsubl %s,%%eax\n",operands[1]);
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '*':
|
||||
case LSMUL:
|
||||
opos+=sprintf(output_buffer+opos,"\txorl %%edx,%%edx\n");
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%%s\n",operands[1],ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\timull %%%s\n",ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %%%s\n",operator,ld.scratch1);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '/':
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\txorl %%edx,%%edx\n");
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%%s\n",operands[1],ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\tidiv %%%s\n",ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %%%s\n",operator,ld.scratch1);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
|
@ -984,38 +984,38 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
|||
{
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->str.buffer[0])
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case '=':
|
||||
case LASSIGN:
|
||||
err_log("%u: cannot assign to rvalue '%s'",vec_at(&pn->tokens,0,Tok*)->line,operands[1]);
|
||||
break;
|
||||
|
||||
case '+':
|
||||
case LADD:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\taddl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '-':
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\tsubl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '*':
|
||||
case LSMUL:
|
||||
opos+=sprintf(output_buffer+opos,"\txorl %%edx,%%edx\n");
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%%s\n",operands[1],ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\timull %%%s\n",ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %%%s\n",operator,ld.scratch1);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '/':
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\txorl %%edx,%%edx\n");
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%%s\n",operands[1],ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\tidivl %%%s\n",ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %%%s\n",operator,ld.scratch1);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
|
@ -1028,28 +1028,28 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
|||
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->str.buffer[0])
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case '=':
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%s\n",operands[0],operands[1]);
|
||||
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 '+':
|
||||
case LADD:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\taddl %s,%%eax\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '-':
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\tsubl %s,%%eax\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '*':
|
||||
case LSMUL:
|
||||
opos+=sprintf(output_buffer+opos,"\txorl %%edx,%%edx\n");
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%%s\n",operands[0],ld.scratch1);
|
||||
|
@ -1057,11 +1057,11 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
|||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '/':
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\txorl %%edx,%%edx\n");
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%%s\n",operands[0],ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\tidivl %%%s\n",ld.scratch1);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %%%s\n",operator,ld.scratch1);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
|
@ -1074,38 +1074,38 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
|||
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->str.buffer[0])
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case '=':
|
||||
case LASSIGN:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %%eax,%s\n",operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '+':
|
||||
case LADD:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\taddl %s,%%eax\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '-':
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\tsubl %s,%%eax\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '*':
|
||||
case LSMUL:
|
||||
opos+=sprintf(output_buffer+opos,"\txorl %%edx,%%edx\n");
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\timull %s\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '/':
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\txorl %%edx,%%edx\n");
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %s,%%eax\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\tidivl %s\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
|
@ -1118,33 +1118,33 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
|||
{
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->str.buffer[0])
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case '=':
|
||||
case LASSIGN:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %%eax,%s\n",operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '+':
|
||||
opos+=sprintf(output_buffer+opos,"\taddl %s,%%eax\n",operands[0]);
|
||||
case LADD:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '-':
|
||||
opos+=sprintf(output_buffer+opos,"\tsubl %s,%%eax\n",operands[0]);
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '*':
|
||||
case LSMUL:
|
||||
opos+=sprintf(output_buffer+opos,"\txorl %%edx,%%edx\n");
|
||||
opos+=sprintf(output_buffer+opos,"\timull %s\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '/':
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\txorl %%edx,%%edx\n");
|
||||
opos+=sprintf(output_buffer+opos,"\tidivl %s\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
|
@ -1157,33 +1157,33 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
|||
{
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->str.buffer[0])
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case '=':
|
||||
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 '+':
|
||||
opos+=sprintf(output_buffer+opos,"\taddl %s,%%eax\n",operands[0]);
|
||||
case LADD:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '-':
|
||||
opos+=sprintf(output_buffer+opos,"\tsubl %s,%%eax\n",operands[0]);
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '*':
|
||||
case LSMUL:
|
||||
opos+=sprintf(output_buffer+opos,"\txorl %%edx,%%edx\n");
|
||||
opos+=sprintf(output_buffer+opos,"\timull %s\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '/':
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\txorl %%edx,%%edx\n");
|
||||
opos+=sprintf(output_buffer+opos,"\tidivl %s\n",operands[0]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[0]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
|
@ -1196,33 +1196,33 @@ void gen_evalop(Gen*gen,PNode*pn,eval_elem*elem,size_t nops,Tok*curtok,FILE*file
|
|||
{
|
||||
tmpelem=(eval_elem){.type=EE_ACC,.val={0}};
|
||||
|
||||
switch(curtok->str.buffer[0])
|
||||
switch(curtok->subtype)
|
||||
{
|
||||
|
||||
case '=':
|
||||
case LASSIGN:
|
||||
opos+=sprintf(output_buffer+opos,"\tmovl %%eax,%s\n",operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '+':
|
||||
opos+=sprintf(output_buffer+opos,"\taddl %s,%%eax\n",operands[1]);
|
||||
case LADD:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '-':
|
||||
opos+=sprintf(output_buffer+opos,"\tsubl %s,%%eax\n",operands[1]);
|
||||
case LSMINUS:
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s,%%eax\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '*':
|
||||
case LSMUL:
|
||||
opos+=sprintf(output_buffer+opos,"\txorl %%edx,%%edx\n");
|
||||
opos+=sprintf(output_buffer+opos,"\timull %s\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
case '/':
|
||||
case LSDIV:
|
||||
opos+=sprintf(output_buffer+opos,"\txorl %%edx,%%edx\n");
|
||||
opos+=sprintf(output_buffer+opos,"\tidivl %s\n",operands[1]);
|
||||
opos+=sprintf(output_buffer+opos,"\t%s %s\n",operator,operands[1]);
|
||||
vec_push(stack,&tmpelem);
|
||||
break;
|
||||
|
||||
|
|
11
sprint.c
11
sprint.c
|
@ -135,15 +135,13 @@ static int sprinti16(char*s,int d)
|
|||
}
|
||||
b[i]=0;
|
||||
|
||||
int max=i;
|
||||
int max=i-1;
|
||||
|
||||
// Print reversed string
|
||||
for(;i>=0;--i)
|
||||
{
|
||||
if(b[i]>31)
|
||||
s[max-i]=b[i];
|
||||
else
|
||||
s[max-i]=1;
|
||||
}
|
||||
s[max+1]=0;
|
||||
|
||||
|
@ -169,19 +167,18 @@ static int sprinti(char*s,int d)
|
|||
{
|
||||
int m=d%10;
|
||||
d/=10;
|
||||
b[i++]=m+'0';
|
||||
if(m+'0'>31)
|
||||
b[i++]=m+'0';
|
||||
}
|
||||
b[i]=0;
|
||||
|
||||
int max=i;
|
||||
int max=i-1;
|
||||
|
||||
// Print reversed string
|
||||
for(;i>=0;--i)
|
||||
{
|
||||
if(b[i]>31)
|
||||
s[max-i]=b[i];
|
||||
else
|
||||
s[max-i]=1;
|
||||
}
|
||||
s[max+1]=0;
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user