push PVARDECL vars to parent PNode

This commit is contained in:
corey 2023-11-12 06:59:42 -06:00
parent f788aa2d81
commit b3577b17af
6 changed files with 42 additions and 26 deletions

3
TODO
View File

@ -29,8 +29,9 @@ Code Generator
* Code generator state
- Variable data
> Binding vars to memory locations
>> register, immediate, RAM
>> register, immediate, stack
> Connect to stack frames
>> Attach to PFUNDECL nodes?
- Function data
- "Run" build architecture
> Virtual machine for intermediate representation/language

1
gen.c
View File

@ -9,4 +9,5 @@ Gen gen_new(void)
void gen_free(Gen*gen)
{
if(!gen)return;
}

3
mem.c
View File

@ -3,9 +3,10 @@
Var var_new(void)
{
Var v={
.value.u64=0,
.name=NULL,
.type=I32,
.location=STACK,
.regnum=0,
};
return v;
}

18
mem.h
View File

@ -7,7 +7,15 @@
#include"tok.h"
// Data types
enum {I8,I16,I32,I64,U8,U16,U32,U64,U8P,U16P,U32P,U64P};
typedef enum{I8,I16,I32,I64,U8,U16,U32,U64,U8P,U16P,U32P,U64P}VTYPE;
// Memory location
// "Where do I look to find this value?"
// REG: Register
// STACK: On stack
// HEAP: Dynamically allocated
// IMM: Immediate/constant value
typedef enum{REG,STACK,HEAP,IMM}VLOC;
// Generic memory location
typedef union Mem
@ -28,12 +36,14 @@ typedef union Mem
int8_t i8;
} Mem;
// Variable
// Variable descriptor
typedef struct Var
{
Mem value;
char*name;
uint32_t type;
VTYPE type;
VLOC location;
size_t regnum; // REG: which register?
size_t stackloc; // STACK: rsp-???
} Var;
Var var_new(void);

View File

@ -32,6 +32,7 @@ PNode pnode_new(void)
PNode n={
.pnodes=vec_new(sizeof(PNode)), // Empty Vec
.tokens=vec_new(sizeof(Tok)), // Empty Vec
.vars=vec_new(sizeof(Var)), // Empty Vec
.parentnode=NULL,
.type=PEMPTY,
.firstline=0,

View File

@ -44,12 +44,14 @@ void gen_x86_64(Gen*gen,const PNode*pn,FILE*file)
{
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,const Tok*)->str.buffer, */
/* .type=I32, */
/* }; */
/* vec_push(&gen->var_desc,&var); */
Var var={
.name=vec_at(&pn->tokens,0,const Tok*)->str.buffer,
.type=I32,
.location=STACK,
.regnum=0,
.stackloc=4,
};
vec_push((Vec*)&pn->vars,&var);
}
else if(pn->tokens.size==0)
err_log("%u: expected identifier",pn->firstline);
@ -261,20 +263,20 @@ static void gen_x86_64_epilog(const PNode*pn,FILE*file)
}
static 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");
}
/* static 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"); */
/* } */
static size_t gen_x86_64_stacksize(const PNode*pn)
{