Compare commits

...

4 Commits

Author SHA1 Message Date
7b72e87df5 remove rpn test 2023-11-10 05:42:04 -06:00
c82cfc3b0b remove rpn module 2023-11-10 05:41:39 -06:00
49e6954094 pass Gen to all code generators 2023-11-10 05:34:54 -06:00
0760a1ce5b use Vec<Var> to store variable state 2023-11-10 05:21:58 -06:00
14 changed files with 43 additions and 238 deletions

7
gen.c
View File

@ -4,6 +4,11 @@ Gen gen_new(void)
{
Gen gen;
gen.labelno=0;
gen.vars=(Var){0};
gen.vars=vec_new(sizeof(Var));
return gen;
}
void gen_free(Gen*gen)
{
vec_free(&gen->vars);
}

6
gen.h
View File

@ -5,15 +5,13 @@
#pragma once
// #include"ir.h"
// #include"x86_64.h"
// #include"run.h"
#include"mem.h"
typedef struct Gen
{
size_t labelno;
Var vars;
Vec vars; //Vec<Var>
} Gen;
Gen gen_new(void);
void gen_free(Gen*gen);

34
ir.c
View File

@ -10,7 +10,7 @@
#include"ir.h"
#include"state.h"
void gen_ir(PNode*pn,FILE*file)
void gen_ir(Gen*gen,PNode*pn,FILE*file)
{
if(!pn)
{
@ -24,37 +24,27 @@ void gen_ir(PNode*pn,FILE*file)
return;
}
/* if(pn->type<=PCALL) */
/* printf("; %s\n",partype_names[pn->type]); */
switch(pn->type)
{
case PEMPTY:
goto recurse_nofinal;
break;
case PCOMMENT:
printf("; %s\n",((Tok*)pn->tokens.buffer)[0].str.buffer);
printf("; %s\n",vec_at(&pn->tokens,0,Tok*)->str.buffer);
break;
case PFUNDECL:
printf("%s:\n",((Tok*)pn->tokens.buffer)[0].str.buffer);
goto recurse_nofinal;
break;
case PBLOCK:
goto recurse_nofinal;
break;
case PRET:
printf("ld a,%s\n",((Tok*)pn->tokens.buffer)[0].str.buffer);
printf("ret\n");
printf("%s:\n",vec_at(&pn->tokens,0,Tok*)->str.buffer);
break;
default:
recurse_nofinal:
for(size_t i=0;i<pn->pnodes.size;++i)
gen_ir(((PNode*)pn->pnodes.buffer)+i,file);
break;
}
for(size_t i=0;i<pn->pnodes.size;++i)
gen_ir(gen,vec_at(&pn->pnodes,i,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);
puts("");
}
}

2
ir.h
View File

@ -8,4 +8,4 @@
#include"state.h"
#include"mem.h"
void gen_ir(PNode*pn,FILE*file);
void gen_ir(Gen*gen,PNode*pn,FILE*file);

4
main.c
View File

@ -193,11 +193,11 @@ int main(int argc,char**argv)
{
case M_IR:
gen_ir(&state.parser.root,state.outfile);
gen_ir(&state.gen,&state.parser.root,state.outfile);
break;
case M_RUN:
gen_run(&state.parser.root,state.outfile);
gen_run(&state.gen,&state.parser.root,state.outfile);
break;
case M_X86_64:

11
mem.c
View File

@ -3,7 +3,8 @@
Var var_new(void)
{
Var v={
.name=str_new(),
.value.u64=0,
.name=NULL,
.type=I32,
};
return v;
@ -12,5 +13,11 @@ Var var_new(void)
void var_free(Var*v)
{
if(!v)return;
str_free(&v->name);
v->name=NULL;
}
void var_set_fromtoken(Var*v,Tok*t)
{
if(!v||!t)return;
v->name=t->str.buffer;
}

4
mem.h
View File

@ -4,6 +4,7 @@
#include<stdio.h>
#include<stdlib.h>
#include"str.h"
#include"tok.h"
// Data types
enum {I8,I16,I32,I64,U8,U16,U32,U64,U8P,U16P,U32P,U64P};
@ -31,9 +32,10 @@ typedef union Mem
typedef struct Var
{
Mem value;
Str name;
char*name;
uint32_t type;
} Var;
Var var_new(void);
void var_free(Var*v);
void var_set_fromtoken(Var*v,Tok*t);

92
rpn.c
View File

@ -1,92 +0,0 @@
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<ctype.h>
#include<string.h>
#include"rpn.h"
#define VSIZE 256
//int main(int argc,char**argv)
//{
//struct state st={0};
//char*str="7-1+2";
//
//if(argc>1)str=argv[1];
//
//printf("str: '%s'\n",str);
//
//rpn_parse(&st,str);
//printf("parse:\n");
//rpn_print(&(st.nums),"%d");
//rpn_print(&(st.ops),"%c");
//
//while(st.ops.n)rpn_math(&st);
//printf("math:\n");
//rpn_print(&(st.nums),"%d");
//rpn_print(&(st.ops),"%c");
//}
void rpn_push(struct vec*v,int c){(v)->b[(v)->n]=(c);++(v)->n;}
int rpn_pop(struct vec*v){if((v)->n){--(v)->n;return v->b[v->n];}return -1;}
void rpn_parse(struct state*st,char*string)
{
//bool found=false;
if(!st||!string)return;
for(int i=0;string[i];++i)
{
if(isdigit(string[i]))
rpn_push((&st->nums),string[i]-'0');
else if(strchr("+-*/",string[i]))
{
while(st->ops.n>0)
rpn_math(st);
rpn_push((&st->ops),string[i]);
}
}
}
void rpn_math(struct state*st)
{
if(!st)return;
//printf("math: ");
for(int i=0;i<st->ops.n;++i)
{
//printf("(%c ",st->ops.b[i]);
int x=rpn_pop(&st->nums);
int y=rpn_pop(&st->nums);
int z;
switch(st->ops.b[i])
{
case '+':z=y+x;break;
case '-':z=y-x;break;
case '*':z=y*x;break;
case '/':z=y/x;break;
default:break;
}
//printf("%d,%d): %d ",y,x,z);
rpn_push(&st->nums,z);
rpn_pop(&st->ops);
//printf("N:");rpn_print(&st->nums,"%d");
}
//printf("\n");
}
void rpn_print(struct vec*v,char*fmt)
{
#define p printf
p("(%d/%d) [",v->n,VSIZE);
for(int i=0;i<v->n;++i)
{
p(fmt,v->b[i]);
if(i<v->n-1)
p(", ");
}
p("]\n");
#undef p
}

18
rpn.h
View File

@ -1,18 +0,0 @@
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<ctype.h>
#include<string.h>
#define VSIZE 256
struct vec{int b[VSIZE];int n;};
struct state{struct vec nums;struct vec ops;};
int rpn_pop(struct vec*v);
void rpn_math(struct state*st);
void rpn_parse(struct state*st,char*string);
void rpn_print(struct vec*v,char*fmt);
void rpn_push(struct vec*v,int c);

View File

@ -1,91 +0,0 @@
#!/usr/bin/ruby
def evalrpn(str)
# puts "eval '#{str}'"
s=[]
str.split.each do|t|
case t
when /[0-9]/
s+=[t.to_i]
when /[-+\*\/]/
i1=s.pop.to_i
i2=s.pop.to_i
r=0
case t
when '+'
r=i2+i1
when '-'
r=i2-i1
when '*'
r=i2*i1
when '/'
r=i2/i1
end
s.push r
end
end
# puts "s: '#{s}'"
# puts "o: '#{o}'"
s.pop
end
def i2r(str)
# puts "i2r '#{str}'"
pre={
'(' => 5,
')' => 5,
'+' => 10,
'-' => 10,
'*' => 20,
'/' => 20,
}
o=[]
s=[]
str.split.each do |t|
case t
when /[0-9]/
s+=[t.to_i]
when /[-+\*\/]/
while o.size>0&&pre[o[-1]]>=pre[t]
s.push o.pop
end
o+=[t]
when '('
o.push t
when ')'
while o[-1]!='('
s.push o.pop
end
o.pop
end
end
while o.size>0
s.push o.pop
end
# puts "s: '#{s}'"
# puts "o: '#{o}'"
s.join' '
end
def dotest(str)
print "'#{str}' => "
res=i2r str
print "'#{res}' => "
print "'#{evalrpn res}'\n"
end
def main
dotest '1 + 2 + 3'
dotest '1 + 2 - 3'
dotest '1 - 2 + 3'
dotest '2 * 5 - 3'
dotest '2 - 5 * 3'
dotest '21 * 2 / 3'
dotest '21 / 2 * 3'
dotest '2 * ( 1 + 2 )'
end
main

6
run.c
View File

@ -5,7 +5,7 @@
void gen_run_eval(Vec*tokens);
void gen_run(PNode*pn,FILE*file)
void gen_run(Gen*gen,PNode*pn,FILE*file)
{
if(!pn)
{
@ -42,7 +42,7 @@ void gen_run(PNode*pn,FILE*file)
if(pn->pnodes.size>0)
{
for(size_t i=0;i<pn->pnodes.size;++i)
gen_run(vec_at(&pn->pnodes,i,PNode*),file);
gen_run(gen,vec_at(&pn->pnodes,i,PNode*),file);
printf("\n");
}
}
@ -63,7 +63,7 @@ void gen_run(PNode*pn,FILE*file)
if(pn->pnodes.size>0)
{
for(size_t i=0;i<pn->pnodes.size;++i)
gen_run(vec_at(&pn->pnodes,i,PNode*),file);
gen_run(gen,vec_at(&pn->pnodes,i,PNode*),file);
printf("\n");
}
}

2
run.h
View File

@ -9,4 +9,4 @@
#include"state.h"
#include"mem.h"
void gen_run(PNode*pn,FILE*file);
void gen_run(Gen*gen,PNode*pn,FILE*file);

View File

@ -42,6 +42,7 @@ void state_free(State*st)
lex_free(&st->lexer);
parser_free(&st->parser);
str_free(&st->input_buffer);
gen_free(&st->gen);
}
void state_open_outfile(State*st,char*filename)

View File

@ -13,6 +13,9 @@
#include"mem.h"
#include"err.h"
#include"gen.h"
#include"ir.h"
#include"run.h"
#include"x86_64.h"
// Build architecture types
enum {M_IR,M_RUN,M_X86_64};