move generic code away from buildarches
This commit is contained in:
parent
161d440e8c
commit
ff72983974
55
gen.c
55
gen.c
|
@ -161,6 +161,21 @@ bool gen_findret(const PNode*pn,bool found)
|
|||
/* fprintf(file,"\n"); */
|
||||
/* } */
|
||||
|
||||
void vec_print_tokens(Vec*tokens)
|
||||
{
|
||||
if(!tokens)return;
|
||||
if(tokens->size<1)return;
|
||||
|
||||
printf("%p (%lu/%lu): [",tokens,tokens->size,tokens->capacity);
|
||||
for(size_t i=0;i<tokens->size;++i)
|
||||
{
|
||||
printf("%s",vec_at(tokens,i,const Tok*)->str.buffer);
|
||||
if(i<tokens->size-1)
|
||||
printf(", ");
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
|
||||
void gen_declare_variable(Gen*gen,PNode*pn,FILE*file)
|
||||
{
|
||||
if(pn->tokens.size>1)
|
||||
|
@ -303,3 +318,43 @@ void gen_declare_function(Gen*gen,PNode*pn,FILE*file)
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
// For function invocation arglist evaluation
|
||||
// testing
|
||||
void gen_eval_analyze(const PNode*pn)
|
||||
{
|
||||
Vec rpn_stack={0};
|
||||
Vec stack=vec_new(sizeof(eval_elem));
|
||||
|
||||
rpn_stack=gen_i2r(&pn->tokens);
|
||||
|
||||
// Evaluate RPN
|
||||
if(rpn_stack.buffer&&rpn_stack.size>0)
|
||||
{
|
||||
|
||||
for(size_t i=0;i<rpn_stack.size;++i)
|
||||
{
|
||||
|
||||
switch(vec_at(&rpn_stack,i,const Tok*)->type)
|
||||
{
|
||||
|
||||
// TODO: Find function names here and
|
||||
// evaluate (?) its arglist
|
||||
case LIDENTIFIER:
|
||||
printf("!%s ",vec_at(&rpn_stack,i,const Tok*)->str.buffer);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("%s ",vec_at(&rpn_stack,i,const Tok*)->str.buffer);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
printf(";\n");
|
||||
|
||||
}
|
||||
|
||||
vec_free(&rpn_stack);
|
||||
vec_free(&stack);
|
||||
}
|
||||
|
|
2
gen.h
2
gen.h
|
@ -39,5 +39,7 @@ Vec gen_i2r(const Vec*tokens);
|
|||
bool gen_findret(const PNode*pn,bool found);
|
||||
void gen_declare_function(Gen*gen,PNode*pn,FILE*file);
|
||||
void gen_declare_variable(Gen*gen,PNode*pn,FILE*file);
|
||||
void gen_eval_analyze(const PNode*pn);
|
||||
void gen_free(Gen*gen);
|
||||
void gen_setrootnode(Gen*gen,PNode*rootnode);
|
||||
void vec_print_tokens(Vec*tokens);
|
||||
|
|
87
i386.c
87
i386.c
|
@ -239,78 +239,6 @@ void gen_i386_epilog(const PNode*pn,FILE*file)
|
|||
|
||||
}
|
||||
|
||||
/* void vec_print_tokens(Vec*tokens) */
|
||||
/* { */
|
||||
/* if(!tokens)return; */
|
||||
/* if(tokens->size<1)return; */
|
||||
/* */
|
||||
/* printf("%p (%lu/%lu): [",tokens,tokens->size,tokens->capacity); */
|
||||
/* for(size_t i=0;i<tokens->size;++i) */
|
||||
/* { */
|
||||
/* printf("%s",vec_at(tokens,i,const Tok*)->str.buffer); */
|
||||
/* if(i<tokens->size-1) */
|
||||
/* printf(", "); */
|
||||
/* } */
|
||||
/* printf("]\n"); */
|
||||
/* } */
|
||||
|
||||
size_t gen_i386_stacksize(const PNode*pn)
|
||||
{
|
||||
size_t stacksize=0;
|
||||
|
||||
if(pn->pnodes.size==0)
|
||||
{
|
||||
if(pn->type==PVARDECL)
|
||||
return stacksize+sizeof(uint32_t);
|
||||
else return 0;
|
||||
}
|
||||
|
||||
for(size_t i=0;i<pn->pnodes.size;++i)
|
||||
stacksize+=gen_i386_stacksize(vec_at(&pn->pnodes,i,PNode*));
|
||||
return stacksize;
|
||||
}
|
||||
|
||||
|
||||
// For function invocation arglist evaluation
|
||||
// testing
|
||||
void gen_i386_eval_analyze(const PNode*pn)
|
||||
{
|
||||
Vec rpn_stack={0};
|
||||
Vec stack=vec_new(sizeof(eval_elem));
|
||||
|
||||
rpn_stack=gen_i2r(&pn->tokens);
|
||||
|
||||
// Evaluate RPN
|
||||
if(rpn_stack.buffer&&rpn_stack.size>0)
|
||||
{
|
||||
|
||||
for(size_t i=0;i<rpn_stack.size;++i)
|
||||
{
|
||||
|
||||
switch(vec_at(&rpn_stack,i,const Tok*)->type)
|
||||
{
|
||||
|
||||
// TODO: Find function names here and
|
||||
// evaluate (?) its arglist
|
||||
case LIDENTIFIER:
|
||||
printf("!%s ",vec_at(&rpn_stack,i,const Tok*)->str.buffer);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("%s ",vec_at(&rpn_stack,i,const Tok*)->str.buffer);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
printf(";\n");
|
||||
|
||||
}
|
||||
|
||||
vec_free(&rpn_stack);
|
||||
vec_free(&stack);
|
||||
}
|
||||
|
||||
// Convert to infix, then evaluate
|
||||
void gen_i386_eval(Gen*gen,const PNode*pn,FILE*file)
|
||||
{
|
||||
|
@ -818,3 +746,18 @@ void gen_i386_eval(Gen*gen,const PNode*pn,FILE*file)
|
|||
vec_free(&stack);
|
||||
}
|
||||
|
||||
size_t gen_i386_stacksize(const PNode*pn)
|
||||
{
|
||||
size_t stacksize=0;
|
||||
|
||||
if(pn->pnodes.size==0)
|
||||
{
|
||||
if(pn->type==PVARDECL)
|
||||
return stacksize+sizeof(uint32_t);
|
||||
else return 0;
|
||||
}
|
||||
|
||||
for(size_t i=0;i<pn->pnodes.size;++i)
|
||||
stacksize+=gen_i386_stacksize(vec_at(&pn->pnodes,i,PNode*));
|
||||
return stacksize;
|
||||
}
|
||||
|
|
1
i386.h
1
i386.h
|
@ -11,4 +11,3 @@ void gen_i386(struct Gen*gen,PNode*pn,FILE*file);
|
|||
void gen_i386_epilog(const PNode*pn,FILE*file);
|
||||
void gen_i386_eval(struct Gen*gen,const PNode*pn,FILE*file);
|
||||
void gen_i386_prolog(const PNode*pn,FILE*file,size_t stacksize);
|
||||
|
||||
|
|
15
run.c
15
run.c
|
@ -72,21 +72,6 @@ void gen_run(Gen*gen,const PNode*pn,FILE*file)
|
|||
}
|
||||
}
|
||||
|
||||
static void vec_print_tokens(const Vec*tokens)
|
||||
{
|
||||
if(!tokens)return;
|
||||
if(tokens->size<1)return;
|
||||
|
||||
printf("%p (%lu/%lu): [",tokens,tokens->size,tokens->capacity);
|
||||
for(size_t i=0;i<tokens->size;++i)
|
||||
{
|
||||
printf("%s",vec_at(tokens,i,const Tok*)->str.buffer);
|
||||
if(i<tokens->size-1)
|
||||
printf(", ");
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
|
||||
// Convert infix to RPN
|
||||
// Vec<Tok>
|
||||
Vec gen_run_i2r(const Vec*tokens)
|
||||
|
|
16
x86_64.c
16
x86_64.c
|
@ -1,5 +1,6 @@
|
|||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include"gen.h"
|
||||
#include"x86_64.h"
|
||||
#include"state.h"
|
||||
|
||||
|
@ -243,21 +244,6 @@ void gen_x86_64_epilog(const PNode*pn,FILE*file)
|
|||
|
||||
}
|
||||
|
||||
/* void vec_print_tokens(Vec*tokens) */
|
||||
/* { */
|
||||
/* if(!tokens)return; */
|
||||
/* if(tokens->size<1)return; */
|
||||
/* */
|
||||
/* printf("%p (%lu/%lu): [",tokens,tokens->size,tokens->capacity); */
|
||||
/* for(size_t i=0;i<tokens->size;++i) */
|
||||
/* { */
|
||||
/* printf("%s",vec_at(tokens,i,const Tok*)->str.buffer); */
|
||||
/* if(i<tokens->size-1) */
|
||||
/* printf(", "); */
|
||||
/* } */
|
||||
/* printf("]\n"); */
|
||||
/* } */
|
||||
|
||||
size_t gen_x86_64_stacksize(const PNode*pn)
|
||||
{
|
||||
size_t stacksize=0;
|
||||
|
|
Loading…
Reference in New Issue
Block a user