immutable Vecs where possible
This commit is contained in:
parent
28e946a814
commit
8518d632a8
10
ir.c
10
ir.c
|
@ -10,7 +10,7 @@
|
|||
#include"ir.h"
|
||||
#include"state.h"
|
||||
|
||||
void gen_ir(Gen*gen,PNode*pn,FILE*file)
|
||||
void gen_ir(Gen*gen,const PNode*pn,FILE*file)
|
||||
{
|
||||
if(!pn)
|
||||
{
|
||||
|
@ -28,11 +28,11 @@ void gen_ir(Gen*gen,PNode*pn,FILE*file)
|
|||
{
|
||||
|
||||
case PCOMMENT:
|
||||
printf("; %s\n",vec_at(&pn->tokens,0,Tok*)->str.buffer);
|
||||
printf("; %s\n",vec_at(&pn->tokens,0,const Tok*)->str.buffer);
|
||||
break;
|
||||
|
||||
case PFUNDECL:
|
||||
printf("%s:\n",vec_at(&pn->tokens,0,Tok*)->str.buffer);
|
||||
printf("%s:\n",vec_at(&pn->tokens,0,const Tok*)->str.buffer);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -40,11 +40,11 @@ void gen_ir(Gen*gen,PNode*pn,FILE*file)
|
|||
}
|
||||
|
||||
for(size_t i=0;i<pn->pnodes.size;++i)
|
||||
gen_ir(gen,vec_at(&pn->pnodes,i,PNode*),file);
|
||||
gen_ir(gen,vec_at(&pn->pnodes,i,const PNode*),file);
|
||||
if(pn->tokens.size>0)
|
||||
{
|
||||
for(size_t i=0;i<pn->tokens.size;++i)
|
||||
printf("%s ",vec_at(&pn->tokens,i,Tok*)->str.buffer);
|
||||
printf("%s ",vec_at(&pn->tokens,i,const Tok*)->str.buffer);
|
||||
puts("");
|
||||
}
|
||||
}
|
||||
|
|
2
ir.h
2
ir.h
|
@ -8,4 +8,4 @@
|
|||
#include"state.h"
|
||||
#include"mem.h"
|
||||
|
||||
void gen_ir(Gen*gen,PNode*pn,FILE*file);
|
||||
void gen_ir(Gen*gen,const PNode*pn,FILE*file);
|
||||
|
|
72
run.c
72
run.c
|
@ -3,9 +3,9 @@
|
|||
#include"run.h"
|
||||
#include"state.h"
|
||||
|
||||
void gen_run_eval(Vec*tokens);
|
||||
void gen_run_eval(const Vec*tokens);
|
||||
|
||||
void gen_run(Gen*gen,PNode*pn,FILE*file)
|
||||
void gen_run(Gen*gen,const PNode*pn,FILE*file)
|
||||
{
|
||||
if(!pn)
|
||||
{
|
||||
|
@ -34,7 +34,7 @@ void gen_run(Gen*gen,PNode*pn,FILE*file)
|
|||
if(pn->tokens.size>0)
|
||||
{
|
||||
for(size_t i=0;i<pn->tokens.size;++i)
|
||||
printf("%s ",vec_at(&pn->tokens,i,Tok*)->str.buffer);
|
||||
printf("%s ",vec_at(&pn->tokens,i,const Tok*)->str.buffer);
|
||||
printf("\n");
|
||||
gen_run_eval(&pn->tokens);
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ void gen_run(Gen*gen,PNode*pn,FILE*file)
|
|||
if(pn->pnodes.size>0)
|
||||
{
|
||||
for(size_t i=0;i<pn->pnodes.size;++i)
|
||||
gen_run(gen,vec_at(&pn->pnodes,i,PNode*),file);
|
||||
gen_run(gen,vec_at(&pn->pnodes,i,const PNode*),file);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
@ -56,14 +56,14 @@ void gen_run(Gen*gen,PNode*pn,FILE*file)
|
|||
if(pn->tokens.size>0)
|
||||
{
|
||||
for(size_t i=0;i<pn->tokens.size;++i)
|
||||
printf("%s ",vec_at(&pn->tokens,i,Tok*)->str.buffer);
|
||||
printf("%s ",vec_at(&pn->tokens,i,const Tok*)->str.buffer);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
if(pn->pnodes.size>0)
|
||||
{
|
||||
for(size_t i=0;i<pn->pnodes.size;++i)
|
||||
gen_run(gen,vec_at(&pn->pnodes,i,PNode*),file);
|
||||
gen_run(gen,vec_at(&pn->pnodes,i,const PNode*),file);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ void gen_run(Gen*gen,PNode*pn,FILE*file)
|
|||
}
|
||||
}
|
||||
|
||||
static void vec_print_tokens(Vec*tokens)
|
||||
static void vec_print_tokens(const Vec*tokens)
|
||||
{
|
||||
if(!tokens)return;
|
||||
if(tokens->size<1)return;
|
||||
|
@ -80,7 +80,7 @@ static void vec_print_tokens(Vec*tokens)
|
|||
printf("%p (%lu/%lu): [",tokens,tokens->size,tokens->capacity);
|
||||
for(size_t i=0;i<tokens->size;++i)
|
||||
{
|
||||
printf("%s",vec_at(tokens,i,Tok*)->str.buffer);
|
||||
printf("%s",vec_at(tokens,i,const Tok*)->str.buffer);
|
||||
if(i<tokens->size-1)
|
||||
printf(", ");
|
||||
}
|
||||
|
@ -89,10 +89,10 @@ static void vec_print_tokens(Vec*tokens)
|
|||
|
||||
// Convert infix to RPN
|
||||
// Vec<Tok>
|
||||
Vec gen_run_i2r(Vec*tokens)
|
||||
Vec gen_run_i2r(const Vec*tokens)
|
||||
{
|
||||
Vec stack=vec_new(sizeof(Tok));
|
||||
Vec operators=vec_new(sizeof(Tok));
|
||||
Vec stack=vec_new(sizeof(const Tok));
|
||||
Vec operators=vec_new(sizeof(const Tok));
|
||||
int precedence[1024]={0};
|
||||
precedence['(']=5;
|
||||
precedence[')']=5;
|
||||
|
@ -107,28 +107,28 @@ Vec gen_run_i2r(Vec*tokens)
|
|||
{
|
||||
|
||||
// identifiers
|
||||
if(vec_at(tokens,i,Tok*)->type==LIDENTIFIER)
|
||||
vec_push(&stack,vec_at(tokens,i,Tok*));
|
||||
if(vec_at(tokens,i,const Tok*)->type==LIDENTIFIER)
|
||||
vec_push(&stack,vec_at(tokens,i,const Tok*));
|
||||
|
||||
// integers
|
||||
if(vec_at(tokens,i,Tok*)->type==LINTEGER)
|
||||
vec_push(&stack,vec_at(tokens,i,Tok*));
|
||||
if(vec_at(tokens,i,const Tok*)->type==LINTEGER)
|
||||
vec_push(&stack,vec_at(tokens,i,const Tok*));
|
||||
// operators
|
||||
else if(vec_at(tokens,i,Tok*)->type==LOPERATOR)
|
||||
else if(vec_at(tokens,i,const Tok*)->type==LOPERATOR)
|
||||
{
|
||||
|
||||
// '('
|
||||
if(vec_at(tokens,i,Tok*)->subtype==LLPAREN)
|
||||
if(vec_at(tokens,i,const Tok*)->subtype==LLPAREN)
|
||||
{
|
||||
vec_push(&operators,vec_at(tokens,i,Tok*));
|
||||
vec_push(&operators,vec_at(tokens,i,const Tok*));
|
||||
}
|
||||
|
||||
// ')'
|
||||
else if(vec_at(tokens,i,Tok*)->subtype==LRPAREN)
|
||||
else if(vec_at(tokens,i,const Tok*)->subtype==LRPAREN)
|
||||
{
|
||||
while(operators.size>0&&vec_at(&operators,operators.size-1,Tok*)->subtype!=LLPAREN)
|
||||
while(operators.size>0&&vec_at(&operators,operators.size-1,const Tok*)->subtype!=LLPAREN)
|
||||
{
|
||||
Tok*newtok=vec_at(&operators,operators.size-1,Tok*);
|
||||
const Tok*newtok=vec_at(&operators,operators.size-1,const Tok*);
|
||||
vec_push(&stack,newtok);
|
||||
vec_pop(&operators);
|
||||
}
|
||||
|
@ -142,13 +142,13 @@ Vec gen_run_i2r(Vec*tokens)
|
|||
// Pop operators from operators ==> stack
|
||||
// who have higher precedence than current
|
||||
// operator
|
||||
while(operators.size>0&&precedence[(uint32_t)vec_at(&operators,operators.size-1,Tok*)->str.buffer[0]]>=precedence[(uint32_t)vec_at(tokens,i,Tok*)->str.buffer[0]])
|
||||
while(operators.size>0&&precedence[(uint32_t)vec_at(&operators,operators.size-1,const Tok*)->str.buffer[0]]>=precedence[(uint32_t)vec_at(tokens,i,const Tok*)->str.buffer[0]])
|
||||
{
|
||||
Tok*newtok=vec_at(&operators,operators.size-1,Tok*);
|
||||
const Tok*newtok=vec_at(&operators,operators.size-1,const Tok*);
|
||||
vec_push(&stack,newtok);
|
||||
vec_pop(&operators);
|
||||
}
|
||||
vec_push(&operators,vec_at(tokens,i,Tok*));
|
||||
vec_push(&operators,vec_at(tokens,i,const Tok*));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -158,7 +158,7 @@ Vec gen_run_i2r(Vec*tokens)
|
|||
|
||||
while(operators.size>0)
|
||||
{
|
||||
Tok*newtok=vec_at(&operators,operators.size-1,Tok*);
|
||||
const Tok*newtok=vec_at(&operators,operators.size-1,const Tok*);
|
||||
vec_push(&stack,newtok);
|
||||
vec_pop(&operators);
|
||||
}
|
||||
|
@ -176,10 +176,10 @@ Vec gen_run_i2r(Vec*tokens)
|
|||
}
|
||||
|
||||
// Convert to infix, then evaluate
|
||||
void gen_run_eval(Vec*tokens)
|
||||
void gen_run_eval(const Vec*tokens)
|
||||
{
|
||||
Vec rpn_stack={0};
|
||||
Vec stack=vec_new(sizeof(int));
|
||||
Vec stack=vec_new(sizeof(const int));
|
||||
|
||||
rpn_stack=gen_run_i2r(tokens);
|
||||
|
||||
|
@ -194,25 +194,25 @@ void gen_run_eval(Vec*tokens)
|
|||
|
||||
/* vec_print(&stack,"%d"); */
|
||||
|
||||
switch(vec_at(&rpn_stack,i,Tok*)->type)
|
||||
switch(vec_at(&rpn_stack,i,const Tok*)->type)
|
||||
{
|
||||
|
||||
case LIDENTIFIER:
|
||||
{
|
||||
printf("ignoring '%s'\n",vec_at(&rpn_stack,i,Tok*)->str.buffer);
|
||||
printf("ignoring '%s'\n",vec_at(&rpn_stack,i,const Tok*)->str.buffer);
|
||||
}
|
||||
break;
|
||||
|
||||
case LINTEGER:
|
||||
{
|
||||
int32_t d=atoi(vec_at(&rpn_stack,i,Tok*)->str.buffer);
|
||||
int32_t d=atoi(vec_at(&rpn_stack,i,const Tok*)->str.buffer);
|
||||
vec_pushi(&stack,d);
|
||||
}
|
||||
break;
|
||||
|
||||
case LOPERATOR:
|
||||
|
||||
/* printf("op:'%s'\n",vec_at(&rpn_stack,i,Tok*)->str.buffer); */
|
||||
/* printf("op:'%s'\n",vec_at(&rpn_stack,i,const Tok*)->str.buffer); */
|
||||
|
||||
if(stack.size>1)
|
||||
{
|
||||
|
@ -221,18 +221,18 @@ void gen_run_eval(Vec*tokens)
|
|||
int32_t i2=0;
|
||||
int32_t res=0;
|
||||
|
||||
i1=*vec_at(&stack,stack.size-1,int*);
|
||||
i1=*vec_at(&stack,stack.size-1,const int*);
|
||||
vec_pop(&stack);
|
||||
i2=*vec_at(&stack,stack.size-1,int*);
|
||||
i2=*vec_at(&stack,stack.size-1,const int*);
|
||||
vec_pop(&stack);
|
||||
|
||||
printf("%d%s%d\n",
|
||||
i2,
|
||||
vec_at(&rpn_stack,i,Tok*)->str.buffer,
|
||||
vec_at(&rpn_stack,i,const Tok*)->str.buffer,
|
||||
i1
|
||||
);
|
||||
|
||||
switch(vec_at(&rpn_stack,i,Tok*)->str.buffer[0])
|
||||
switch(vec_at(&rpn_stack,i,const Tok*)->str.buffer[0])
|
||||
{
|
||||
|
||||
case '+':res=i2+i1;break;
|
||||
|
@ -255,7 +255,7 @@ void gen_run_eval(Vec*tokens)
|
|||
|
||||
if(stack.size>0)
|
||||
{
|
||||
int32_t res=*vec_at(&stack,stack.size-1,int32_t*);
|
||||
int32_t res=*vec_at(&stack,stack.size-1,const int32_t*);
|
||||
printf("%d\n",res);
|
||||
}
|
||||
/* printf("s:"); */
|
||||
|
|
2
run.h
2
run.h
|
@ -9,4 +9,4 @@
|
|||
#include"state.h"
|
||||
#include"mem.h"
|
||||
|
||||
void gen_run(Gen*gen,PNode*pn,FILE*file);
|
||||
void gen_run(Gen*gen,const PNode*pn,FILE*file);
|
||||
|
|
2
vec.c
2
vec.c
|
@ -77,7 +77,7 @@ void vec_grow(Vec*v)
|
|||
v->capacity+=VECDEFSIZE;
|
||||
}
|
||||
|
||||
void vec_push(Vec*v,void*d)
|
||||
void vec_push(Vec*v,const void*d)
|
||||
{
|
||||
if(!v){if(VECVERBOSE)fprintf(stderr,"vec_push: NULL Vec*\n");return;}
|
||||
if(!v->unitsize){if(VECVERBOSE)fprintf(stderr,"vec_push: unitsize is zero\n");return;}
|
||||
|
|
2
vec.h
2
vec.h
|
@ -32,4 +32,4 @@ void vec_free(Vec*v);
|
|||
void vec_grow(Vec*v);
|
||||
void vec_pop(Vec*v);
|
||||
void vec_print(Vec*v,const char*fmt);
|
||||
void vec_push(Vec*v,void*d);
|
||||
void vec_push(Vec*v,const void*d);
|
||||
|
|
121
x86_64.c
121
x86_64.c
|
@ -3,11 +3,10 @@
|
|||
#include"x86_64.h"
|
||||
#include"state.h"
|
||||
|
||||
void gen_x86_64_prolog(PNode*pn,FILE*file,size_t stacksize);
|
||||
void gen_x86_64_epilog(PNode*pn,FILE*file);
|
||||
void gen_x86_64_eval(PNode*pn,FILE*file);
|
||||
static void gen_x86_64_prolog(const PNode*pn,FILE*file,size_t stacksize);
|
||||
static void gen_x86_64_epilog(const PNode*pn,FILE*file);
|
||||
|
||||
void gen_x86_64(Gen*gen,PNode*pn,FILE*file)
|
||||
void gen_x86_64(Gen*gen,const PNode*pn,FILE*file)
|
||||
{
|
||||
|
||||
if(!pn)
|
||||
|
@ -28,21 +27,21 @@ void gen_x86_64(Gen*gen,PNode*pn,FILE*file)
|
|||
case PFUNDECL:
|
||||
if(pn->parentnode&&pn->parentnode->parentnode!=NULL)
|
||||
err_log("%u: nested function declaration",pn->firstline);
|
||||
fprintf(file,"global %s\n%s:\n",vec_at(&pn->tokens,0,Tok*)->str.buffer,vec_at(&pn->tokens,0,Tok*)->str.buffer);
|
||||
fprintf(file,"global %s\n%s:\n",vec_at(&pn->tokens,0,const Tok*)->str.buffer,vec_at(&pn->tokens,0,const Tok*)->str.buffer);
|
||||
gen_x86_64_prolog(pn,file,0);
|
||||
for(size_t i=0;i<pn->pnodes.size;++i)
|
||||
gen_x86_64(gen,vec_at(&pn->pnodes,i,PNode*),file);
|
||||
gen_x86_64(gen,vec_at(&pn->pnodes,i,const PNode*),file);
|
||||
//gen_x86_64_epilog(pn,file);
|
||||
break;
|
||||
|
||||
case PVARDECL:
|
||||
if(pn->tokens.size>1)
|
||||
{
|
||||
if(vec_at(&pn->tokens,1,Tok*)->type!=LOPERATOR||(strcmp(vec_at(&pn->tokens,1,Tok*)->str.buffer,"=")))
|
||||
err_log("%u: expected either ';' or '='",vec_at(&pn->tokens,1,Tok*)->line);
|
||||
if(vec_at(&pn->tokens,1,const Tok*)->type!=LOPERATOR||(strcmp(vec_at(&pn->tokens,1,const Tok*)->str.buffer,"=")))
|
||||
err_log("%u: expected either ';' or '='",vec_at(&pn->tokens,1,const Tok*)->line);
|
||||
Var var={
|
||||
.value.i32=0,
|
||||
.name=vec_at(&pn->tokens,0,Tok*)->str.buffer,
|
||||
.name=vec_at(&pn->tokens,0,const Tok*)->str.buffer,
|
||||
.type=I32,
|
||||
};
|
||||
vec_push(&gen->var_desc,&var);
|
||||
|
@ -53,16 +52,16 @@ void gen_x86_64(Gen*gen,PNode*pn,FILE*file)
|
|||
fprintf(file,"\tsub rsp,%lu\n",4lu);
|
||||
if(pn->tokens.size>2)
|
||||
{
|
||||
if(vec_at(&pn->tokens,1,Tok*)->type==LOPERATOR&&vec_at(&pn->tokens,1,Tok*)->str.buffer[0]=='=')
|
||||
if(vec_at(&pn->tokens,1,const Tok*)->type==LOPERATOR&&vec_at(&pn->tokens,1,const Tok*)->str.buffer[0]=='=')
|
||||
{
|
||||
gen_x86_64_eval(pn,file);
|
||||
fprintf(file,"\tmov dword[rsp],eax\n");
|
||||
/* fprintf(file,"\tmov dword[rsp],%s ;%s\n",vec_at(&pn->tokens,2,Tok*)->str.buffer,vec_at(&pn->tokens,0,Tok*)->str.buffer); */
|
||||
/* fprintf(file,"\tmov dword[rsp],%s ;%s\n",vec_at(&pn->tokens,2,const Tok*)->str.buffer,vec_at(&pn->tokens,0,const Tok*)->str.buffer); */
|
||||
}
|
||||
}
|
||||
else if(pn->tokens.size>0)
|
||||
{
|
||||
fprintf(file,"\txor eax,eax ;%s\n",vec_at(&pn->tokens,0,Tok*)->str.buffer);
|
||||
fprintf(file,"\txor eax,eax ;%s\n",vec_at(&pn->tokens,0,const Tok*)->str.buffer);
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -70,16 +69,16 @@ void gen_x86_64(Gen*gen,PNode*pn,FILE*file)
|
|||
gen_x86_64_epilog(pn,file);
|
||||
if(pn->tokens.size>0)
|
||||
{
|
||||
if(vec_at(&pn->tokens,0,Tok*)->type==LOPERATOR&&vec_at(&pn->tokens,0,Tok*)->subtype==LSMINUS)
|
||||
if(vec_at(&pn->tokens,0,const Tok*)->type==LOPERATOR&&vec_at(&pn->tokens,0,const Tok*)->subtype==LSMINUS)
|
||||
{
|
||||
if(pn->tokens.size<2)
|
||||
err_log("%u: expected integer",vec_at(&pn->tokens,0,Tok*)->line);
|
||||
err_log("%u: expected integer",vec_at(&pn->tokens,0,const Tok*)->line);
|
||||
else
|
||||
fprintf(file,"\tmov eax,-%s\n",vec_at(&pn->tokens,1,Tok*)->str.buffer);
|
||||
fprintf(file,"\tmov eax,-%s\n",vec_at(&pn->tokens,1,const Tok*)->str.buffer);
|
||||
}
|
||||
/* else if(vec_at(&pn->tokens,0,Tok*)->type!=LINTEGER) */
|
||||
/* else if(vec_at(&pn->tokens,0,const Tok*)->type!=LINTEGER) */
|
||||
/* { */
|
||||
/* err_log("%u: returning non-integer",vec_at(&pn->tokens,0,Tok*)->line); */
|
||||
/* err_log("%u: returning non-integer",vec_at(&pn->tokens,0,const Tok*)->line); */
|
||||
/* } */
|
||||
else
|
||||
gen_x86_64_eval(pn,file);
|
||||
|
@ -104,7 +103,7 @@ void gen_x86_64(Gen*gen,PNode*pn,FILE*file)
|
|||
|
||||
if(pn->tokens.size<1)
|
||||
{
|
||||
err_log("%u: expected function name after 'call'",vec_at(&pn->tokens,0,Tok*)->line);
|
||||
err_log("%u: expected function name after 'call'",vec_at(&pn->tokens,0,const Tok*)->line);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -114,22 +113,22 @@ void gen_x86_64(Gen*gen,PNode*pn,FILE*file)
|
|||
|
||||
if(i==0)
|
||||
{
|
||||
if(strcmp(vec_at(&pn->tokens,i,Tok*)->str.buffer,"(")!=0)
|
||||
if(strcmp(vec_at(&pn->tokens,i,const Tok*)->str.buffer,"(")!=0)
|
||||
{
|
||||
err_log("%u: expected '(' after function call",vec_at(&pn->tokens,0,Tok*)->line);
|
||||
err_log("%u: expected '(' after function call",vec_at(&pn->tokens,0,const Tok*)->line);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if(i%2!=0)
|
||||
{
|
||||
if(i<pn->tokens.size-1&&strcmp(vec_at(&pn->tokens,i,Tok*)->str.buffer,",")!=0)
|
||||
if(i<pn->tokens.size-1&&strcmp(vec_at(&pn->tokens,i,const Tok*)->str.buffer,",")!=0)
|
||||
{
|
||||
err_log("%u: expected ',' delimiting arguments (got '%s')",vec_at(&pn->tokens,0,Tok*)->line,vec_at(&pn->tokens,i,Tok*)->str.buffer);
|
||||
err_log("%u: expected ',' delimiting arguments (got '%s')",vec_at(&pn->tokens,0,const Tok*)->line,vec_at(&pn->tokens,i,const Tok*)->str.buffer);
|
||||
break;
|
||||
}
|
||||
else if(i==pn->tokens.size-1&&strcmp(vec_at(&pn->tokens,i,Tok*)->str.buffer,")")!=0)
|
||||
else if(i==pn->tokens.size-1&&strcmp(vec_at(&pn->tokens,i,const Tok*)->str.buffer,")")!=0)
|
||||
{
|
||||
err_log("%u: expected ')'",vec_at(&pn->tokens,0,Tok*)->line);
|
||||
err_log("%u: expected ')'",vec_at(&pn->tokens,0,const Tok*)->line);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -137,15 +136,15 @@ void gen_x86_64(Gen*gen,PNode*pn,FILE*file)
|
|||
{
|
||||
size_t max_reg=(sizeof(reg_order_sysv)/sizeof(char*));
|
||||
size_t use_reg=(n_args>=max_reg)?(max_reg-1):(n_args);
|
||||
fprintf(file,"\tmov %s,%s\n",reg_order_sysv[use_reg],vec_at(&pn->tokens,i,Tok*)->str.buffer);
|
||||
fprintf(file,"\tmov %s,%s\n",reg_order_sysv[use_reg],vec_at(&pn->tokens,i,const Tok*)->str.buffer);
|
||||
++n_args;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Call function
|
||||
/* fprintf(file,"\textern %s\n",vec_at(&pn->tokens,0,Tok*)->str.buffer); */
|
||||
fprintf(file,"\tcall %s\n",vec_at(&pn->tokens,0,Tok*)->str.buffer);
|
||||
/* fprintf(file,"\textern %s\n",vec_at(&pn->tokens,0,const Tok*)->str.buffer); */
|
||||
fprintf(file,"\tcall %s\n",vec_at(&pn->tokens,0,const Tok*)->str.buffer);
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -159,7 +158,7 @@ void gen_x86_64(Gen*gen,PNode*pn,FILE*file)
|
|||
fprintf(file,"\tjz .L%02lu\n",gen->labelno);
|
||||
|
||||
for(size_t i=0;i<pn->pnodes.size;++i)
|
||||
gen_x86_64(gen,vec_at(&pn->pnodes,i,PNode*),file);
|
||||
gen_x86_64(gen,vec_at(&pn->pnodes,i,const PNode*),file);
|
||||
|
||||
fprintf(file,".L%02lu:\n",gen->labelno);
|
||||
break;
|
||||
|
@ -175,19 +174,19 @@ void gen_x86_64(Gen*gen,PNode*pn,FILE*file)
|
|||
fprintf(file,"\tjz .L%02lu\n",++gen->labelno);
|
||||
|
||||
for(size_t i=0;i<pn->pnodes.size;++i)
|
||||
gen_x86_64(gen,vec_at(&pn->pnodes,i,PNode*),file);
|
||||
gen_x86_64(gen,vec_at(&pn->pnodes,i,const PNode*),file);
|
||||
|
||||
fprintf(file,"\tjmp .L%02lu\n",gen->labelno-1);
|
||||
fprintf(file,".L%02lu:\n",gen->labelno);
|
||||
break;
|
||||
|
||||
case PCOMMENT:
|
||||
fprintf(file,"\t;%s\n",vec_at(&pn->tokens,0,Tok*)->str.buffer);
|
||||
fprintf(file,"\t;%s\n",vec_at(&pn->tokens,0,const Tok*)->str.buffer);
|
||||
break;
|
||||
|
||||
case PBLOCK:
|
||||
for(size_t i=0;i<pn->pnodes.size;++i)
|
||||
gen_x86_64(gen,vec_at(&pn->pnodes,i,PNode*),file);
|
||||
gen_x86_64(gen,vec_at(&pn->pnodes,i,const PNode*),file);
|
||||
break;
|
||||
|
||||
case PEMPTY:
|
||||
|
@ -195,7 +194,7 @@ void gen_x86_64(Gen*gen,PNode*pn,FILE*file)
|
|||
if(pn->parentnode==NULL)
|
||||
{
|
||||
for(size_t i=0;i<pn->pnodes.size;++i)
|
||||
gen_x86_64(gen,vec_at(&pn->pnodes,i,PNode*),file);
|
||||
gen_x86_64(gen,vec_at(&pn->pnodes,i,const PNode*),file);
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -207,7 +206,7 @@ void gen_x86_64(Gen*gen,PNode*pn,FILE*file)
|
|||
fprintf(file,";PNODE ID: %s ",partype_names[pn->type]);
|
||||
for(size_t i=0;i<pn->tokens.size;++i)
|
||||
{
|
||||
fprintf(file,"%s",vec_at(&pn->tokens,i,Tok*)->str.buffer);
|
||||
fprintf(file,"%s",vec_at(&pn->tokens,i,const Tok*)->str.buffer);
|
||||
}
|
||||
fprintf(file,"\n");
|
||||
}
|
||||
|
@ -216,10 +215,10 @@ void gen_x86_64(Gen*gen,PNode*pn,FILE*file)
|
|||
}
|
||||
|
||||
//for(size_t i=0;i<pn->pnodes.size;++i)
|
||||
//gen_x86_64(gen,vec_at(&pn->pnodes,i,PNode*),file);
|
||||
//gen_x86_64(gen,vec_at(&pn->pnodes,i,const PNode*),file);
|
||||
}
|
||||
|
||||
void gen_x86_64_prolog(PNode*pn,FILE*file,size_t stacksize)
|
||||
void gen_x86_64_prolog(const PNode*pn,FILE*file,size_t stacksize)
|
||||
{
|
||||
if(!pn)
|
||||
{
|
||||
|
@ -238,7 +237,7 @@ void gen_x86_64_prolog(PNode*pn,FILE*file,size_t stacksize)
|
|||
fprintf(file,"\tsub rsp,%lu\n",stacksize);
|
||||
}
|
||||
|
||||
void gen_x86_64_epilog(PNode*pn,FILE*file)
|
||||
void gen_x86_64_epilog(const PNode*pn,FILE*file)
|
||||
{
|
||||
if(!pn)
|
||||
{
|
||||
|
@ -265,7 +264,7 @@ static void vec_print_tokens(Vec*tokens)
|
|||
printf("%p (%lu/%lu): [",tokens,tokens->size,tokens->capacity);
|
||||
for(size_t i=0;i<tokens->size;++i)
|
||||
{
|
||||
printf("%s",vec_at(tokens,i,Tok*)->str.buffer);
|
||||
printf("%s",vec_at(tokens,i,const Tok*)->str.buffer);
|
||||
if(i<tokens->size-1)
|
||||
printf(", ");
|
||||
}
|
||||
|
@ -274,10 +273,10 @@ static void vec_print_tokens(Vec*tokens)
|
|||
|
||||
// Convert infix to RPN
|
||||
// Vec<Tok>
|
||||
Vec gen_x86_64_i2r(Vec*tokens)
|
||||
Vec gen_x86_64_i2r(const Vec*tokens)
|
||||
{
|
||||
Vec stack=vec_new(sizeof(Tok));
|
||||
Vec operators=vec_new(sizeof(Tok));
|
||||
Vec stack=vec_new(sizeof(const Tok));
|
||||
Vec operators=vec_new(sizeof(const Tok));
|
||||
int precedence[1024]={0};
|
||||
precedence['(']=5;
|
||||
precedence[')']=5;
|
||||
|
@ -292,14 +291,14 @@ Vec gen_x86_64_i2r(Vec*tokens)
|
|||
{
|
||||
|
||||
// identifiers
|
||||
switch(vec_at(tokens,i,Tok*)->type)
|
||||
switch(vec_at(tokens,i,const Tok*)->type)
|
||||
{
|
||||
|
||||
// integers, keywords (true/false), identifiers
|
||||
case LIDENTIFIER:
|
||||
case LKEYWORD:
|
||||
case LINTEGER:
|
||||
vec_push(&stack,vec_at(tokens,i,Tok*));
|
||||
vec_push(&stack,vec_at(tokens,i,const Tok*));
|
||||
break;
|
||||
|
||||
// operators
|
||||
|
@ -307,17 +306,17 @@ Vec gen_x86_64_i2r(Vec*tokens)
|
|||
{
|
||||
|
||||
// '('
|
||||
if(vec_at(tokens,i,Tok*)->subtype==LLPAREN)
|
||||
if(vec_at(tokens,i,const Tok*)->subtype==LLPAREN)
|
||||
{
|
||||
vec_push(&operators,vec_at(tokens,i,Tok*));
|
||||
vec_push(&operators,vec_at(tokens,i,const Tok*));
|
||||
}
|
||||
|
||||
// ')'
|
||||
else if(vec_at(tokens,i,Tok*)->subtype==LRPAREN)
|
||||
else if(vec_at(tokens,i,const Tok*)->subtype==LRPAREN)
|
||||
{
|
||||
while(operators.size>0&&vec_at(&operators,operators.size-1,Tok*)->subtype!=LLPAREN)
|
||||
while(operators.size>0&&vec_at(&operators,operators.size-1,const Tok*)->subtype!=LLPAREN)
|
||||
{
|
||||
Tok*newtok=vec_at(&operators,operators.size-1,Tok*);
|
||||
const Tok*newtok=vec_at(&operators,operators.size-1,const Tok*);
|
||||
vec_push(&stack,newtok);
|
||||
vec_pop(&operators);
|
||||
}
|
||||
|
@ -331,13 +330,13 @@ Vec gen_x86_64_i2r(Vec*tokens)
|
|||
// Pop operators from operators ==> stack
|
||||
// who have higher precedence than current
|
||||
// operator
|
||||
while(operators.size>0&&precedence[(uint32_t)vec_at(&operators,operators.size-1,Tok*)->str.buffer[0]]>=precedence[(uint32_t)vec_at(tokens,i,Tok*)->str.buffer[0]])
|
||||
while(operators.size>0&&precedence[(uint32_t)vec_at(&operators,operators.size-1,const Tok*)->str.buffer[0]]>=precedence[(uint32_t)vec_at(tokens,i,const Tok*)->str.buffer[0]])
|
||||
{
|
||||
Tok*newtok=vec_at(&operators,operators.size-1,Tok*);
|
||||
const Tok*newtok=vec_at(&operators,operators.size-1,const Tok*);
|
||||
vec_push(&stack,newtok);
|
||||
vec_pop(&operators);
|
||||
}
|
||||
vec_push(&operators,vec_at(tokens,i,Tok*));
|
||||
vec_push(&operators,vec_at(tokens,i,const Tok*));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -348,7 +347,7 @@ Vec gen_x86_64_i2r(Vec*tokens)
|
|||
|
||||
while(operators.size>0)
|
||||
{
|
||||
Tok*newtok=vec_at(&operators,operators.size-1,Tok*);
|
||||
const Tok*newtok=vec_at(&operators,operators.size-1,const Tok*);
|
||||
vec_push(&stack,newtok);
|
||||
vec_pop(&operators);
|
||||
}
|
||||
|
@ -366,9 +365,9 @@ Vec gen_x86_64_i2r(Vec*tokens)
|
|||
}
|
||||
|
||||
// Convert to infix, then evaluate
|
||||
void gen_x86_64_eval(PNode*pn,FILE*file)
|
||||
void gen_x86_64_eval(const PNode*pn,FILE*file)
|
||||
{
|
||||
Vec*tokens=NULL;
|
||||
const Vec*tokens=NULL;
|
||||
Vec rpn_stack={0};
|
||||
Vec stack=vec_new(sizeof(int));
|
||||
|
||||
|
@ -383,14 +382,14 @@ void gen_x86_64_eval(PNode*pn,FILE*file)
|
|||
for(size_t i=0;i<rpn_stack.size;++i)
|
||||
{
|
||||
|
||||
switch(vec_at(&rpn_stack,i,Tok*)->type)
|
||||
switch(vec_at(&rpn_stack,i,const Tok*)->type)
|
||||
{
|
||||
|
||||
case LKEYWORD:
|
||||
{
|
||||
if(strcmp("true",vec_at(&rpn_stack,i,Tok*)->str.buffer)==0)
|
||||
if(strcmp("true",vec_at(&rpn_stack,i,const Tok*)->str.buffer)==0)
|
||||
vec_pushi(&stack,1);
|
||||
else if(strcmp("false",vec_at(&rpn_stack,i,Tok*)->str.buffer)==0)
|
||||
else if(strcmp("false",vec_at(&rpn_stack,i,const Tok*)->str.buffer)==0)
|
||||
vec_pushi(&stack,0);
|
||||
}
|
||||
break;
|
||||
|
@ -402,7 +401,7 @@ void gen_x86_64_eval(PNode*pn,FILE*file)
|
|||
|
||||
case LINTEGER:
|
||||
{
|
||||
int32_t d=atoi(vec_at(&rpn_stack,i,Tok*)->str.buffer);
|
||||
int32_t d=atoi(vec_at(&rpn_stack,i,const Tok*)->str.buffer);
|
||||
vec_pushi(&stack,d);
|
||||
}
|
||||
break;
|
||||
|
@ -416,12 +415,12 @@ void gen_x86_64_eval(PNode*pn,FILE*file)
|
|||
int32_t i2=0;
|
||||
int32_t res=0;
|
||||
|
||||
i1=*vec_at(&stack,stack.size-1,int*);
|
||||
i1=*vec_at(&stack,stack.size-1,const int*);
|
||||
vec_pop(&stack);
|
||||
i2=*vec_at(&stack,stack.size-1,int*);
|
||||
i2=*vec_at(&stack,stack.size-1,const int*);
|
||||
vec_pop(&stack);
|
||||
|
||||
switch(vec_at(&rpn_stack,i,Tok*)->str.buffer[0])
|
||||
switch(vec_at(&rpn_stack,i,const Tok*)->str.buffer[0])
|
||||
{
|
||||
|
||||
case '+':res=i2+i1;break;
|
||||
|
@ -442,7 +441,7 @@ void gen_x86_64_eval(PNode*pn,FILE*file)
|
|||
|
||||
if(stack.size>0)
|
||||
{
|
||||
int32_t res=*vec_at(&stack,stack.size-1,int32_t*);
|
||||
const int32_t res=*vec_at(&stack,stack.size-1,const int32_t*);
|
||||
fprintf(file,"\tmov eax,%d\n",res);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user