push PVARDECL vars to parent PNode
This commit is contained in:
parent
f788aa2d81
commit
b3577b17af
3
TODO
3
TODO
@ -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
|
||||
|
3
mem.c
3
mem.c
@ -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
18
mem.h
@ -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);
|
||||
|
1
pnode.c
1
pnode.c
@ -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,
|
||||
|
42
x86_64.c
42
x86_64.c
@ -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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user