Skip to content
Browse files

ibniz2c now compiles julia example

  • Loading branch information...
1 parent a41586e commit 19f75c79b32cc72ec8cfff522bfd5394cf41c767 @viznut committed
Showing with 367 additions and 54 deletions.
  1. +218 −9 src/compiler.c
  2. +41 −28 src/gen.h
  3. +108 −17 src/gen_c.c
View
227 src/compiler.c
@@ -11,6 +11,8 @@
# define NONATIVECODE
#endif
+//#define COMPILERDEBUG
+
void compiler_parse(char*src)
{
char*d=vm.parsed_code;
@@ -217,6 +219,7 @@ struct {
uint8_t*co0;
uint8_t*co;
+ int srcidx;
// structure for label-address mapping
// (store labels as co0-relative!)
} gen;
@@ -260,8 +263,10 @@ int allocreg_withprefs(int preferred,int forbidden)
int i;
int excluded;
+#ifdef COMPILERDEBUG
printf("// alloc: prefer %x forbid %x used %x\n",
preferred,forbidden,gen.usedregs);
+#endif
while((gen.usedregs|forbidden)==(1<<NUMREGS)-1)
flushstackbottom();
@@ -328,7 +333,7 @@ int popintoreg()
exit(-1);
}
r=allocreg();
- growstack(GSV_REG,r);
+ //growstack(GSV_REG,r);
gen_pop_reg(r);
return r;
}
@@ -377,7 +382,7 @@ void popsv(gsv_t*ret)
/*** user-callable ***/
-gen_flushstack()
+flushstack()
{
gen_flushpartialstack(gen.gsp+1);
}
@@ -574,10 +579,175 @@ UNOP(isneg,IBNIZ_ISNEG(i));
UNOP(ispos,IBNIZ_ISPOS(i));
UNOP(iszero,IBNIZ_ISZERO(i));
-// gen_load_reg_imm(int r,uint32_t imm)
-// gen_add_reg_reg(int r,int r)
-// gen_add_reg_imm(int r,uint32_t imm)
+/*
+// we must harmonize post-if and post-else gs structure (and post-a and post-b as well)
+// cond IF a ELSE b ENDIF c
+// 0 1 0 1 1
+// cond IF a ENDIF
+// 0 0 0
+// trivial: just flushstack() at these points (before if-jump, before else-jump, before endif-label)
+*/
+
+/*
+// when looping: harmonize stack state in both ends
+// a [ b ] c
+// 0 0
+*/
+
+/*
+// when jumping to subroutines:
+// A { xxx }
+*/
+
+gen_if(int skipto)
+{
+ gsv_t cond;
+ popsv(&cond);
+ flushstack();
+ if(cond.type==GSV_REG)
+ {
+ gen_beq_reg_lab(cond.val,skipto);
+ } else
+ {
+ if(cond.val==0) gen_jmp_lab(skipto);
+ }
+}
+
+gen_else(int skipto)
+{
+ flushstack();
+ gen_jmp_lab(skipto);
+ gen_label(gen.srcidx+1);
+}
+
+gen_endif()
+{
+ flushstack();
+ gen_label(gen.srcidx+1);
+}
+
+gen_load()
+{
+ int r;
+ gsv_t address;
+ popsv(&address);
+ r=allocreg();
+ growstack(GSV_REG,r);
+ if(address.type==GSV_REG)
+ gen_load_reg_reg(r,address.val);
+ else
+ gen_load_reg_imm(r,address.val);
+}
+
+gen_store()
+{
+ int r;
+ gsv_t address;
+ gsv_t value;
+ popsv(&address);
+ popsv(&value);
+ if(address.type==GSV_REG)
+ {
+ if(value.type==GSV_REG)
+ gen_store_reg_reg(address.val,value.val);
+ else
+ gen_store_reg_imm(address.val,value.val);
+ } else
+ {
+ if(value.type==GSV_REG)
+ gen_store_imm_reg(address.val,value.val);
+ else
+ gen_store_imm_imm(address.val,value.val);
+ }
+}
+
+gen_do()
+{
+ flushstack();
+ gen_rpush_lab(gen.srcidx+1);
+ gen_label(gen.srcidx+1);
+
+ // later: use temp storage for loop address, check loop sanity
+ // analyzer will unroll small immediate loops
+}
+
+gen_times()
+{
+ gen_rpush();
+ gen_do();
+}
+
+gen_while()
+{
+ gsv_t cond;
+ popsv(&cond);
+ if(cond.type==GSV_REG)
+ {
+ flushstack();
+ gen_bne_reg_rstack(cond.val);
+ gen_rpop_noreg();
+ } else
+ {
+ if(cond.val)
+ {
+ flushstack();
+ gen_jmp_rstack();
+ } else
+ {
+ gen_rpop_noreg();
+ }
+ }
+}
+
+gen_loop()
+{
+ /*
+ in c:
+ if(--(rstack[(rsp-1)])) goto l0;
+ in x86:
+ dec dword [rsp-4]
+ jne .l0
+ */
+}
+
+gen_rpush()
+{
+ gsv_t v;
+ popsv(&v);
+ if(v.type==GSV_REG)
+ gen_rpush_reg(v.val);
+ else
+ gen_rpush_imm(v.val);
+}
+
+gen_defsub()
+{
+ /*
+ in c:
+ mem[X]=((&&l7)-main);
+ goto l6; // use skip to seek it
+ // (compiler: store current gs and clear gs)
+ l2:
+ */
+}
+
+gen_return()
+{
+ /*
+ flushstack();
+ gen_jmp_rpop();
+ compiler: restore stored gs
+ l6:
+ */
+}
+
+gen_rpop()
+{
+ int r=allocreg();
+ growstack(GSV_REG,r);
+ gen_rpop_reg(r);
+}
gen_whereami()
{
@@ -618,7 +788,7 @@ gen_tyxloop_init()
gen_tyxloop_iterator()
{
int t,y,x;
- gen_flushstack();
+ flushstack();
t=allocreg();
y=allocreg();
x=allocreg();
@@ -633,9 +803,10 @@ gen_tyxloop_iterator()
//gen_cmpjne_reg_inc_imm_lab(x,0x00010000,0);
}
+
gen_finish()
{
- gen_flushstack();
+ flushstack();
//gen_tyxloop_iterator();
gen_nativefinish();
}
@@ -649,13 +820,14 @@ int compiler_compile()
for(i=0;i<vm.codelgt;i++)
{
char a=vm.parsed_code[i];
-
+ gen.srcidx=i;
+#ifdef COMPILERDEBUG
printf("// op %c, stack now: ",a);
for(j=0;j<=gen.gsp;j++)
if(gen.gs[j].type==GSV_REG) printf("%c ",'A'+gen.gs[j].val);
else printf("%x ",gen.gs[j].val);
printf("\n");
-
+#endif
checkregusage();
switch(a)
@@ -729,6 +901,43 @@ int compiler_compile()
gen_atan2();
break;
+ case('?'):
+ gen_if(vm.parsed_hints[i]);
+ break;
+ case(':'):
+ gen_else(vm.parsed_hints[i]);
+ break;
+ case(';'):
+ gen_endif();
+ break;
+
+ case('!'):
+ gen_store();
+ break;
+ case('@'):
+ gen_load();
+ break;
+
+ case('R'):
+ gen_rpop();
+ break;
+ case('P'):
+ gen_rpush();
+ break;
+
+ case('['):
+ gen_do();
+ break;
+ case(']'):
+ gen_while();
+ break;
+ case('X'):
+ gen_times();
+ break;
+ case('L'):
+ gen_loop();
+ break;
+
case('w'):
gen_whereami();
break;
View
69 src/gen.h
@@ -10,46 +10,46 @@ void gen_nativerun(void*a);
/* reg moves */
void gen_mov_reg_reg(int t, int s);
-void gen_mov_reg_imm(int t, uint32_t i);
+void gen_mov_reg_imm(int t, int32_t i);
/* load & store */
-void gen_load_reg_reg_reg(int t, int s1, int s);
-void gen_load_reg_reg_imm(int t, int s1, uint32_t a);
-void gen_store_reg_reg_reg(int t, int s1, int s);
-void gen_store_reg_reg_imm(int t, int s1, uint32_t s);
-void gen_store_imm_reg_reg(uint32_t t, int s);
-void gen_store_imm_reg_imm(uint32_t t, uint32_t s);
+void gen_load_reg_reg(int t, int a);
+void gen_load_reg_imm(int t, int32_t a);
+void gen_store_reg_reg(int a, int s);
+void gen_store_reg_imm(int a, int32_t s);
+void gen_store_imm_reg(int32_t a, int s);
+void gen_store_imm_imm(int32_t a, int32_t s);
void gen_mov_reg_ivar(int r, int v);
-void gen_mov_ivar_reg_reg(int v, int r);
+void gen_mov_ivar_reg(int v, int r);
/* arithmetic */
void gen_add_reg_reg_reg(int t, int s1, int s);
-void gen_add_reg_reg_imm(int t, int s1, uint32_t i);
+void gen_add_reg_reg_imm(int t, int s1, int32_t i);
void gen_sub_reg_reg_reg(int t, int s1, int s);
-void gen_sub_reg_reg_imm(int t, int s1, uint32_t i);
+void gen_sub_reg_reg_imm(int t, int s1, int32_t i);
void gen_mul_reg_reg_reg(int t, int s1, int s);
-void gen_mul_reg_reg_imm(int t, int s1, uint32_t i);
+void gen_mul_reg_reg_imm(int t, int s1, int32_t i);
void gen_div_reg_reg_reg(int t, int s1, int s);
-void gen_div_reg_reg_imm(int t, int s1, uint32_t i);
+void gen_div_reg_reg_imm(int t, int s1, int32_t i);
void gen_mod_reg_reg_reg(int t, int s1, int s);
-void gen_mod_reg_reg_imm(int t, int s1, uint32_t i);
+void gen_mod_reg_reg_imm(int t, int s1, int32_t i);
void gen_and_reg_reg_reg(int t, int s1, int s);
-void gen_and_reg_reg_imm(int t, int s1, uint32_t i);
+void gen_and_reg_reg_imm(int t, int s1, int32_t i);
void gen_or_reg_reg_reg(int t, int s1, int s);
-void gen_or_reg_reg_imm(int t, int s1, uint32_t i);
+void gen_or_reg_reg_imm(int t, int s1, int32_t i);
void gen_xor_reg_reg_reg(int t, int s1, int s);
-void gen_xor_reg_reg_imm(int t, int s1, uint32_t i);
+void gen_xor_reg_reg_imm(int t, int s1, int32_t i);
void gen_ror_reg_reg_reg(int t, int s1, int s);
-void gen_ror_reg_reg_imm(int t, int s1, uint32_t i);
+void gen_ror_reg_reg_imm(int t, int s1, int32_t i);
void gen_shl_reg_reg_reg(int t, int s1, int s);
-void gen_shl_reg_reg_imm(int t, int s1, uint32_t i);
+void gen_shl_reg_reg_imm(int t, int s1, int32_t i);
void gen_neg_reg_reg(int t, int s);
void gen_atan2_reg_reg_reg(int t, int s1, int s);
-void gen_atan2_reg_reg_imm(int t, int s1, uint32_t i);
+void gen_atan2_reg_reg_imm(int t, int s1, int32_t i);
void gen_sin_reg_reg(int t, int s);
void gen_sqrt_reg_reg(int t, int s);
@@ -60,32 +60,45 @@ void gen_iszero_reg_reg(int t, int s);
/* stack */
void gen_push_reg(int s);
-void gen_push_imm(uint32_t i);
+void gen_push_imm(int32_t i);
void gen_pop_reg(int t);
void gen_pop_noreg();
void gen_dup_reg(int t);
void gen_pick_reg_reg(int t,int i);
-void gen_pick_reg_imm(int t,uint32_t i);
+void gen_pick_reg_imm(int t,int32_t i);
void gen_bury_regreg(int t,int s);
-void gen_bury_reg_imm(int t,uint32_t s);
-void gen_bury_imm_reg(uint32_t i,int s);
-void gen_bury_imm_imm(uint32_t i,uint32_t s);
+void gen_bury_reg_imm(int t,int32_t s);
+void gen_bury_imm_reg(int32_t i,int s);
+void gen_bury_imm_imm(int32_t i,int32_t s);
/* rstack */
void gen_rpush_reg_reg(int s);
-void gen_rpush_reg_imm(uint32_t i);
+void gen_rpush_reg_imm(int32_t i);
+void gen_rpush_reg_lab(int l);
void gen_rpop_reg_reg(int t);
/* conditionals */
-void gen_cmp_reg_reg(int r0,int r1);
-void gen_cmp_reg_imm(int r,uint32_t i);
-void gen_test_reg(int r);
+//void gen_cmp_reg_reg(int r0,int r1);
+//void gen_cmp_reg_imm(int r,uint32_t i);
+//void gen_test_reg(int r);
+// blt_reg_reg_lab
+// bgt_reg_reg_lab
+// btestnz_reg_reg_lab
+// btestnz_reg_
+// ...
+
+void gen_beq_reg_lab(int s,int l);
+void gen_bne_reg_lab(int s,int l);
+void gen_beq_reg_rstack(int s);
+void gen_bne_reg_rstack(int s);
+/* not used */
void gen_bne_lab(int l);
void gen_beq_lab(int l);
void gen_bmi_lab(int l);
void gen_bpl_lab(int l);
+/**/
/* jumps */
View
125 src/gen_c.c
@@ -29,9 +29,11 @@ void gen_nativeinit()
"#define IBNIZ_ISPOS(a) ((a)>0?(a):0)\n"
"#define IBNIZ_ISZERO(a) ((a)==0?0x10000:0)\n"
"\n"
-"uint32_t mem[0x100000];\n"
-"uint32_t*stack=mem+0xE0000;\n"
-"uint32_t sp;\n"
+"int32_t mem[0x100000];\n"
+"int32_t*stack=mem+0xE0000;\n"
+"uint32_t*rstack=mem+0xC0000;\n"
+"int32_t sp;\n"
+"uint32_t rsp;\n"
"\n"
"int32_t A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z;\n"
"SDL_Surface*s;\n"
@@ -64,6 +66,7 @@ void* gen_nativefinish()
{
printf(
"/********************** generated code ends ************************/\n"
+"{}"
"}\n"
" {\n"
" uint32_t*src=stack+0x10000-(sp&0x10000),*trg=(uint32_t*)(o->pixels[0]);\n"
@@ -96,7 +99,7 @@ void gen_mov_reg_reg_reg(int t, int s)
printf("%c=%c;\n",t+'A',s+'A');
}
-void gen_mov_reg_imm(int t, uint32_t i)
+void gen_mov_reg_imm(int t, int32_t i)
{
printf("%c=0x%X;\n",t+'A',i);
}
@@ -115,6 +118,36 @@ void gen_mov_ivar_reg(int v, int r)
/* load & store */
+void gen_load_reg_reg(int t, int a)
+{
+ printf("%c=mem[ROR(%c,16)&0xFFFFF];\n",t+'A',a+'A');
+}
+
+void gen_load_reg_imm(int t, int32_t a)
+{
+ printf("%c=mem[0x%X];\n",t+'A',ROR(a,16)&0xFFFFF);
+}
+
+void gen_store_reg_reg(int a, int s)
+{
+ printf("mem[ROR(%c,16)&0xFFFFF]=%c;\n",a+'A',s+'A');
+}
+
+void gen_store_reg_imm(int a, int32_t s)
+{
+ printf("mem[ROR(%c,16)&0xFFFFF]=0x%X;\n",a+'A',s);
+}
+
+void gen_store_imm_reg(int32_t a, int s)
+{
+ printf("mem[0x%X]=%c;\n",ROR(a,16)&0xFFFFFF,s+'A');
+}
+
+void gen_store_imm_imm(int32_t a, int32_t s)
+{
+ printf("mem[0x%X]=0x%X;\n",ROR(a,16)&0xFFFFFF,s);
+}
+
/* arithmetic */
void gen_add_reg_reg_reg(int t, int s1, int s)
@@ -122,7 +155,7 @@ void gen_add_reg_reg_reg(int t, int s1, int s)
printf("%c=%c+%c;\n",t+'A',s1+'A',s+'A');
}
-void gen_add_reg_reg_imm(int t, int s1, uint32_t i)
+void gen_add_reg_reg_imm(int t, int s1, int32_t i)
{
printf("%c=%c+0x%X;\n",t+'A',s1+'A',i);
}
@@ -132,7 +165,7 @@ void gen_sub_reg_reg_reg(int t, int s1, int s)
printf("%c=%c-%c;\n",t+'A',s1+'A',s+'A');
}
-void gen_sub_reg_reg_imm(int t, int s1, uint32_t i)
+void gen_sub_reg_reg_imm(int t, int s1, int32_t i)
{
printf("%c=%c-0x%X;\n",t+'A',s1+'A',i);
}
@@ -142,7 +175,7 @@ void gen_mul_reg_reg_reg(int t, int s1, int s)
printf("%c=IBNIZ_MUL(%c,%c);\n",t+'A',s1+'A',s+'A');
}
-void gen_mul_reg_reg_imm(int t, int s1, uint32_t i)
+void gen_mul_reg_reg_imm(int t, int s1, int32_t i)
{
printf("%c=IBNIZ_MUL(%c,0x%X);\n",t+'A',s1+'A',i);
}
@@ -152,7 +185,7 @@ void gen_div_reg_reg_reg(int t, int s1, int s)
printf("%c=IBNIZ_DIV(%c,%c);\n",t+'A',s1+'A',s+'A');
}
-void gen_div_reg_reg_imm(int t, int s1, uint32_t i)
+void gen_div_reg_reg_imm(int t, int s1, int32_t i)
{
printf("%c=IBNIZ_DIV(%c,0x%X);\n",t+'A',s1+'A',i);
}
@@ -162,7 +195,7 @@ void gen_mod_reg_reg_reg(int t, int s1, int s)
printf("%c=IBNIZ_MOD(%c,%c);\n",t+'A',s1+'A',s+'A');
}
-void gen_mod_reg_reg_imm(int t, int s1, uint32_t i)
+void gen_mod_reg_reg_imm(int t, int s1, int32_t i)
{
printf("%c=IBNIZ_MOD(%c,0x%X);\n",t+'A',s1+'A',i);
}
@@ -172,7 +205,7 @@ void gen_and_reg_reg_reg(int t, int s1, int s)
printf("%c=%c&%c;\n",t+'A',s1+'A',s+'A');
}
-void gen_and_reg_reg_imm(int t, int s1, uint32_t i)
+void gen_and_reg_reg_imm(int t, int s1, int32_t i)
{
printf("%c=%c&0x%X;\n",t+'A',s1+'A',i);
}
@@ -182,7 +215,7 @@ void gen_or_reg_reg_reg(int t, int s1, int s)
printf("%c=%c|%c;\n",t+'A',s1+'A',s+'A');
}
-void gen_or_reg_reg_imm(int t, int s1, uint32_t i)
+void gen_or_reg_reg_imm(int t, int s1, int32_t i)
{
printf("%c=%c|0x%X;\n",t+'A',s1+'A',i);
}
@@ -192,7 +225,7 @@ void gen_xor_reg_reg_reg(int t, int s1, int s)
printf("%c=%c^%c;\n",t+'A',s1+'A',s+'A');
}
-void gen_xor_reg_reg_imm(int t, int s1, uint32_t i)
+void gen_xor_reg_reg_imm(int t, int s1, int32_t i)
{
printf("%c=%c^0x%X;\n",t+'A',s1+'A',i);
}
@@ -202,7 +235,7 @@ void gen_ror_reg_reg_reg(int t,int s1,int s)
printf("%c=IBNIZ_ROR(%c,%c);\n",t+'A',s1+'A',s+'A');
}
-void gen_ror_reg_reg_imm(int t,int s1,uint32_t i)
+void gen_ror_reg_reg_imm(int t,int s1,int32_t i)
{
printf("%c=IBNIZ_ROR(%c,0x%X);\n",t+'A',s1+'A',i);
}
@@ -212,7 +245,7 @@ void gen_shl_reg_reg_reg(int t,int s1,int s)
printf("%c=IBNIZ_SHL(%c,%c);\n",t+'A',s1+'A',s+'A');
}
-void gen_shl_reg_reg_imm(int t,int s1,uint32_t i)
+void gen_shl_reg_reg_imm(int t,int s1,int32_t i)
{
printf("%c=IBNIZ_SHL(%c,0x%X);\n",t+'A',s1+'A',i);
}
@@ -227,7 +260,7 @@ void gen_atan2_reg_reg_reg(int t,int s1,int s)
printf("%c=IBNIZ_ATAN2(%c,%c);\n",t+'A',s1+'A',s+'A');
}
-void gen_atan2_reg_reg_imm(int t,int s1,uint32_t i)
+void gen_atan2_reg_reg_imm(int t,int s1,int32_t i)
{
printf("%c=IBNIZ_ATAN2(%c,0x%X);\n",t+'A',s1+'A',i);
}
@@ -264,7 +297,7 @@ void gen_push_reg(int s)
printf("stack[(++sp)&0x1FFFF]=%c;\n",s+'A');
}
-void gen_push_imm(uint32_t i)
+void gen_push_imm(int32_t i)
{
printf("stack[(++sp)&0x1FFFF]=0x%X;\n",i);
}
@@ -288,10 +321,55 @@ void gen_dup_reg(int t)
/* rstack */
+void gen_rpush_reg(int s)
+{
+ printf("rsp=(rsp+1)&0x7FFF;rstack[rsp]=%c;\n",s+'A');
+}
+
+void gen_rpush_imm(int32_t s)
+{
+ printf("rsp=(rsp+1)&0x7FFF;rstack[rsp]=0x%X;\n",s);
+}
+
+void gen_rpush_lab(int l)
+{
+ printf("rsp=(rsp+1)&0x7FFF;rstack[rsp]=(&&l%d)-(void*)main;\n",l);
+}
+
+void gen_rpop_reg(int t)
+{
+ printf("rstack[rsp]=%c;rsp=(rsp-1)&0x7FFF;\n",t+'A');
+}
+
+void gen_rpop_noreg()
+{
+ printf("rsp=(rsp-1)&0x7FFF;\n");
+}
+
// todo
/* conditionals */
+void gen_beq_reg_lab(int s,int l)
+{
+ printf("if(!%c) goto l%d;\n",s+'A',l);
+}
+
+void gen_bne_reg_lab(int s,int l)
+{
+ printf("if(%c) goto l%d;\n",s+'A',l);
+}
+
+void gen_beq_reg_rstack(int s)
+{
+ printf("if(!%c) goto *((void*)main+rstack[rsp]);\n",s+'A');
+}
+
+void gen_bne_reg_rstack(int s)
+{
+ printf("if(%c) goto *((void*)main+rstack[rsp]);\n",s+'A');
+}
+
// todo
/* jumps */
@@ -301,7 +379,20 @@ void gen_label(int l)
printf("l%d:\n",l);
}
-// todo
+void gen_jmp_lab(int l)
+{
+ printf("goto l%d;\n",l);
+}
+
+void gen_jmp_rstack()
+{
+ printf("goto (main+rstack[rsp]);\n");
+}
+
+void gen_jmp_rpop()
+{
+ printf("tmp=rstack[rsp];rsp=(rsp-1)&0x7FFFF;goto (main+tmp);\n");
+}
/* some more complex things */

0 comments on commit 19f75c7

Please sign in to comment.
Something went wrong with that request. Please try again.