cleanup codegen.c
This commit is contained in:
parent
79ee6f6ae4
commit
2ec7f381d0
|
@ -142,31 +142,14 @@ scope_global_define(pic_state *pic, const char *name)
|
||||||
return e->val;
|
return e->val;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pic_gen_call(codegen_state *, pic_value);
|
static void codegen_call(codegen_state *, pic_value);
|
||||||
static struct pic_irep *pic_gen_lambda(codegen_state *, pic_value);
|
static struct pic_irep *codegen_lambda(codegen_state *, pic_value);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
pic_gen(codegen_state *state, pic_value obj)
|
codegen(codegen_state *state, pic_value obj)
|
||||||
{
|
{
|
||||||
pic_state *pic = state->pic;
|
pic_state *pic = state->pic;
|
||||||
struct pic_irep *irep = state->irep;
|
struct pic_irep *irep = state->irep;
|
||||||
pic_value sDEFINE, sLAMBDA, sIF, sBEGIN, sQUOTE;
|
|
||||||
pic_value sCONS, sCAR, sCDR, sNILP;
|
|
||||||
pic_value sADD, sSUB, sMUL, sDIV;
|
|
||||||
|
|
||||||
sDEFINE = pic->sDEFINE;
|
|
||||||
sLAMBDA = pic->sLAMBDA;
|
|
||||||
sIF = pic->sIF;
|
|
||||||
sBEGIN = pic->sBEGIN;
|
|
||||||
sQUOTE = pic->sQUOTE;
|
|
||||||
sCONS = pic->sCONS;
|
|
||||||
sCAR = pic->sCAR;
|
|
||||||
sCDR = pic->sCDR;
|
|
||||||
sNILP = pic->sNILP;
|
|
||||||
sADD = pic->sADD;
|
|
||||||
sSUB = pic->sSUB;
|
|
||||||
sMUL = pic->sMUL;
|
|
||||||
sDIV = pic->sDIV;
|
|
||||||
|
|
||||||
switch (pic_type(obj)) {
|
switch (pic_type(obj)) {
|
||||||
case PIC_TT_SYMBOL: {
|
case PIC_TT_SYMBOL: {
|
||||||
|
@ -199,14 +182,14 @@ pic_gen(codegen_state *state, pic_value obj)
|
||||||
pic_value proc;
|
pic_value proc;
|
||||||
|
|
||||||
proc = pic_car(pic, obj);
|
proc = pic_car(pic, obj);
|
||||||
if (pic_eq_p(pic, proc, sDEFINE)) {
|
if (pic_eq_p(pic, proc, pic->sDEFINE)) {
|
||||||
int idx;
|
int idx;
|
||||||
const char *name;
|
const char *name;
|
||||||
|
|
||||||
name = pic_symbol_ptr(pic_car(pic, pic_cdr(pic, obj)))->name;
|
name = pic_symbol_ptr(pic_car(pic, pic_cdr(pic, obj)))->name;
|
||||||
idx = scope_global_define(pic, name);
|
idx = scope_global_define(pic, name);
|
||||||
|
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, obj))));
|
codegen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, obj))));
|
||||||
|
|
||||||
irep->code[irep->clen].insn = OP_GSET;
|
irep->code[irep->clen].insn = OP_GSET;
|
||||||
irep->code[irep->clen].u.i = idx;
|
irep->code[irep->clen].u.i = idx;
|
||||||
|
@ -215,47 +198,47 @@ pic_gen(codegen_state *state, pic_value obj)
|
||||||
irep->clen++;
|
irep->clen++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (pic_eq_p(pic, proc, sLAMBDA)) {
|
else if (pic_eq_p(pic, proc, pic->sLAMBDA)) {
|
||||||
irep->code[irep->clen].insn = OP_LAMBDA;
|
irep->code[irep->clen].insn = OP_LAMBDA;
|
||||||
irep->code[irep->clen].u.i = pic->ilen;
|
irep->code[irep->clen].u.i = pic->ilen;
|
||||||
irep->clen++;
|
irep->clen++;
|
||||||
|
|
||||||
pic->irep[pic->ilen++] = pic_gen_lambda(state, obj);
|
pic->irep[pic->ilen++] = codegen_lambda(state, obj);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (pic_eq_p(pic, proc, sIF)) {
|
else if (pic_eq_p(pic, proc, pic->sIF)) {
|
||||||
int s,t;
|
int s,t;
|
||||||
|
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, obj)));
|
codegen(state, pic_car(pic, pic_cdr(pic, obj)));
|
||||||
|
|
||||||
irep->code[irep->clen].insn = OP_JMPIF;
|
irep->code[irep->clen].insn = OP_JMPIF;
|
||||||
s = irep->clen++;
|
s = irep->clen++;
|
||||||
|
|
||||||
/* if false branch */
|
/* if false branch */
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, pic_cdr(pic, obj)))));
|
codegen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, pic_cdr(pic, obj)))));
|
||||||
irep->code[irep->clen].insn = OP_JMP;
|
irep->code[irep->clen].insn = OP_JMP;
|
||||||
t = irep->clen++;
|
t = irep->clen++;
|
||||||
|
|
||||||
irep->code[s].u.i = irep->clen - s;
|
irep->code[s].u.i = irep->clen - s;
|
||||||
|
|
||||||
/* if true branch */
|
/* if true branch */
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, obj))));
|
codegen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, obj))));
|
||||||
irep->code[t].u.i = irep->clen - t;
|
irep->code[t].u.i = irep->clen - t;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (pic_eq_p(pic, proc, sBEGIN)) {
|
else if (pic_eq_p(pic, proc, pic->sBEGIN)) {
|
||||||
pic_value v, seq;
|
pic_value v, seq;
|
||||||
|
|
||||||
seq = pic_cdr(pic, obj);
|
seq = pic_cdr(pic, obj);
|
||||||
for (v = seq; ! pic_nil_p(v); v = pic_cdr(pic, v)) {
|
for (v = seq; ! pic_nil_p(v); v = pic_cdr(pic, v)) {
|
||||||
pic_gen(state, pic_car(pic, v));
|
codegen(state, pic_car(pic, v));
|
||||||
irep->code[irep->clen].insn = OP_POP;
|
irep->code[irep->clen].insn = OP_POP;
|
||||||
irep->clen++;
|
irep->clen++;
|
||||||
}
|
}
|
||||||
irep->clen--;
|
irep->clen--;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (pic_eq_p(pic, proc, sQUOTE)) {
|
else if (pic_eq_p(pic, proc, pic->sQUOTE)) {
|
||||||
int pidx;
|
int pidx;
|
||||||
pidx = pic->plen++;
|
pidx = pic->plen++;
|
||||||
pic->pool[pidx] = pic_car(pic, pic_cdr(pic, obj));
|
pic->pool[pidx] = pic_car(pic, pic_cdr(pic, obj));
|
||||||
|
@ -264,61 +247,61 @@ pic_gen(codegen_state *state, pic_value obj)
|
||||||
irep->clen++;
|
irep->clen++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (pic_eq_p(pic, proc, sCONS)) {
|
else if (pic_eq_p(pic, proc, pic->sCONS)) {
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, obj)));
|
codegen(state, pic_car(pic, pic_cdr(pic, obj)));
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, obj))));
|
codegen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, obj))));
|
||||||
irep->code[irep->clen].insn = OP_CONS;
|
irep->code[irep->clen].insn = OP_CONS;
|
||||||
irep->clen++;
|
irep->clen++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (pic_eq_p(pic, proc, sCAR)) {
|
else if (pic_eq_p(pic, proc, pic->sCAR)) {
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, obj)));
|
codegen(state, pic_car(pic, pic_cdr(pic, obj)));
|
||||||
irep->code[irep->clen].insn = OP_CAR;
|
irep->code[irep->clen].insn = OP_CAR;
|
||||||
irep->clen++;
|
irep->clen++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (pic_eq_p(pic, proc, sCDR)) {
|
else if (pic_eq_p(pic, proc, pic->sCDR)) {
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, obj)));
|
codegen(state, pic_car(pic, pic_cdr(pic, obj)));
|
||||||
irep->code[irep->clen].insn = OP_CDR;
|
irep->code[irep->clen].insn = OP_CDR;
|
||||||
irep->clen++;
|
irep->clen++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (pic_eq_p(pic, proc, sNILP)) {
|
else if (pic_eq_p(pic, proc, pic->sNILP)) {
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, obj)));
|
codegen(state, pic_car(pic, pic_cdr(pic, obj)));
|
||||||
irep->code[irep->clen].insn = OP_NILP;
|
irep->code[irep->clen].insn = OP_NILP;
|
||||||
irep->clen++;
|
irep->clen++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (pic_eq_p(pic, proc, sADD)) {
|
else if (pic_eq_p(pic, proc, pic->sADD)) {
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, obj)));
|
codegen(state, pic_car(pic, pic_cdr(pic, obj)));
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, obj))));
|
codegen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, obj))));
|
||||||
irep->code[irep->clen].insn = OP_ADD;
|
irep->code[irep->clen].insn = OP_ADD;
|
||||||
irep->clen++;
|
irep->clen++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (pic_eq_p(pic, proc, sSUB)) {
|
else if (pic_eq_p(pic, proc, pic->sSUB)) {
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, obj)));
|
codegen(state, pic_car(pic, pic_cdr(pic, obj)));
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, obj))));
|
codegen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, obj))));
|
||||||
irep->code[irep->clen].insn = OP_SUB;
|
irep->code[irep->clen].insn = OP_SUB;
|
||||||
irep->clen++;
|
irep->clen++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (pic_eq_p(pic, proc, sMUL)) {
|
else if (pic_eq_p(pic, proc, pic->sMUL)) {
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, obj)));
|
codegen(state, pic_car(pic, pic_cdr(pic, obj)));
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, obj))));
|
codegen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, obj))));
|
||||||
irep->code[irep->clen].insn = OP_MUL;
|
irep->code[irep->clen].insn = OP_MUL;
|
||||||
irep->clen++;
|
irep->clen++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (pic_eq_p(pic, proc, sDIV)) {
|
else if (pic_eq_p(pic, proc, pic->sDIV)) {
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, obj)));
|
codegen(state, pic_car(pic, pic_cdr(pic, obj)));
|
||||||
pic_gen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, obj))));
|
codegen(state, pic_car(pic, pic_cdr(pic, pic_cdr(pic, obj))));
|
||||||
irep->code[irep->clen].insn = OP_DIV;
|
irep->code[irep->clen].insn = OP_DIV;
|
||||||
irep->clen++;
|
irep->clen++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
pic_gen_call(state, obj);
|
codegen_call(state, obj);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -362,7 +345,7 @@ pic_gen(codegen_state *state, pic_value obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
pic_gen_call(codegen_state *state, pic_value obj)
|
codegen_call(codegen_state *state, pic_value obj)
|
||||||
{
|
{
|
||||||
pic_state *pic = state->pic;
|
pic_state *pic = state->pic;
|
||||||
struct pic_irep *irep = state->irep;
|
struct pic_irep *irep = state->irep;
|
||||||
|
@ -373,7 +356,7 @@ pic_gen_call(codegen_state *state, pic_value obj)
|
||||||
pic_value v;
|
pic_value v;
|
||||||
|
|
||||||
v = pic_car(pic, seq);
|
v = pic_car(pic, seq);
|
||||||
pic_gen(state, v);
|
codegen(state, v);
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
irep->code[irep->clen].insn = OP_CALL;
|
irep->code[irep->clen].insn = OP_CALL;
|
||||||
|
@ -382,7 +365,7 @@ pic_gen_call(codegen_state *state, pic_value obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct pic_irep *
|
static struct pic_irep *
|
||||||
pic_gen_lambda(codegen_state *state, pic_value obj)
|
codegen_lambda(codegen_state *state, pic_value obj)
|
||||||
{
|
{
|
||||||
pic_state *pic = state->pic;
|
pic_state *pic = state->pic;
|
||||||
codegen_scope *prev_scope;
|
codegen_scope *prev_scope;
|
||||||
|
@ -399,7 +382,7 @@ pic_gen_lambda(codegen_state *state, pic_value obj)
|
||||||
/* body */
|
/* body */
|
||||||
body = pic_cdr(pic, pic_cdr(pic, obj));
|
body = pic_cdr(pic, pic_cdr(pic, obj));
|
||||||
for (v = body; ! pic_nil_p(v); v = pic_cdr(pic, v)) {
|
for (v = body; ! pic_nil_p(v); v = pic_cdr(pic, v)) {
|
||||||
pic_gen(state, pic_car(pic, v));
|
codegen(state, pic_car(pic, v));
|
||||||
irep->code[irep->clen].insn = OP_POP;
|
irep->code[irep->clen].insn = OP_POP;
|
||||||
irep->clen++;
|
irep->clen++;
|
||||||
}
|
}
|
||||||
|
@ -441,7 +424,7 @@ pic_codegen(pic_state *pic, pic_value obj)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pic_gen(state, obj);
|
codegen(state, obj);
|
||||||
state->irep->code[state->irep->clen].insn = OP_STOP;
|
state->irep->code[state->irep->clen].insn = OP_STOP;
|
||||||
state->irep->clen++;
|
state->irep->clen++;
|
||||||
proc = pic_proc_new(pic, state->irep);
|
proc = pic_proc_new(pic, state->irep);
|
||||||
|
|
Loading…
Reference in New Issue