wrap irep fields with unions

This commit is contained in:
Yuichi Nishiwaki 2016-02-06 18:15:15 +09:00
parent 0fd529c968
commit 5af1b44456
6 changed files with 48 additions and 35 deletions

View File

@ -610,7 +610,7 @@ typedef struct codegen_context {
pic_code *code;
size_t clen, ccapa;
/* child ireps */
struct pic_irep **irep;
union irep_node *irep;
size_t ilen, icapa;
/* constant object pool */
int *ints;
@ -637,7 +637,7 @@ codegen_context_init(pic_state *pic, codegen_context *cxt, codegen_context *up,
cxt->clen = 0;
cxt->ccapa = PIC_ISEQ_SIZE;
cxt->irep = pic_calloc(pic, PIC_IREP_SIZE, sizeof(struct pic_irep *));
cxt->irep = pic_calloc(pic, PIC_IREP_SIZE, sizeof(union irep_node));
cxt->ilen = 0;
cxt->icapa = PIC_IREP_SIZE;
@ -664,13 +664,14 @@ codegen_context_destroy(pic_state *pic, codegen_context *cxt)
irep->argc = (int)cxt->args->len + 1;
irep->localc = (int)cxt->locals->len;
irep->capturec = (int)cxt->captures->len;
irep->code = pic_realloc(pic, cxt->code, sizeof(pic_code) * cxt->clen);
irep->irep = pic_realloc(pic, cxt->irep, sizeof(struct pic_irep *) * (cxt->ilen + 1));
irep->irep[cxt->ilen] = NULL;
irep->u.s.code = pic_realloc(pic, cxt->code, sizeof(pic_code) * cxt->clen);
irep->u.s.irep = pic_realloc(pic, cxt->irep, sizeof(union irep_node) * cxt->ilen);
irep->u.s.ints = pic_realloc(pic, cxt->ints, sizeof(int) * cxt->klen);
irep->pool = pic_realloc(pic, cxt->pool, sizeof(pic_value) * cxt->plen);
irep->plen = cxt->plen;
irep->ints = pic_realloc(pic, cxt->ints, sizeof(int) * cxt->klen);
irep->ilen = cxt->klen;
irep->ncode = cxt->clen;
irep->nirep = cxt->ilen;
irep->nints = cxt->klen;
irep->npool = cxt->plen;
irep->list.next = pic->ireps.next;
irep->list.prev = &pic->ireps;
@ -887,7 +888,7 @@ codegen_lambda(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos
/* emit irep */
codegen_context_init(pic, inner_cxt, cxt, rest, args, locals, captures);
codegen(pic, inner_cxt, body, true);
cxt->irep[cxt->ilen] = codegen_context_destroy(pic, inner_cxt);
cxt->irep[cxt->ilen].i = codegen_context_destroy(pic, inner_cxt);
/* emit OP_LAMBDA */
emit_i(pic, cxt, OP_LAMBDA, cxt->ilen++);

View File

@ -465,7 +465,7 @@ gc_mark_phase(pic_state *pic)
/* ireps */
for (list = pic->ireps.next; list != &pic->ireps; list = list->next) {
struct pic_irep *irep = (struct pic_irep *)list;
for (j = 0; j < irep->plen; ++j) {
for (j = 0; j < irep->npool; ++j) {
gc_mark(pic, irep->pool[j]);
}
}

View File

@ -21,16 +21,26 @@ struct pic_list {
struct pic_irep {
struct pic_list list;
int refc;
pic_code *code;
unsigned refc;
int argc, localc, capturec;
bool varg;
struct pic_irep **irep;
/* constants pool */
int *ints;
size_t ilen;
pic_value *pool;
size_t plen;
union {
struct {
int code_offset;
int ints_offset;
int irep_offset;
} p;
struct {
pic_code *code;
int *ints;
union irep_node {
int offset;
struct pic_irep *i;
} *irep;
} s;
} u;
pic_value *pool; /* pool of heap objects */
size_t ncode, nirep, nints, npool;
};
void pic_irep_incref(pic_state *, struct pic_irep *);

View File

@ -181,17 +181,20 @@ pic_dump_code(pic_code c)
PIC_INLINE void
pic_dump_irep(struct pic_irep *irep)
{
unsigned i;
size_t i;
printf("## irep %p\n", (void *)irep);
printf("[clen = %zd, argc = %d, localc = %d, capturec = %d]\n", irep->clen, irep->argc, irep->localc, irep->capturec);
for (i = 0; i < irep->clen; ++i) {
printf("# argc = %d\n", irep->argc);
printf("# localc = %d\n", irep->localc);
printf("# capturec = %d\n", irep->capturec);
for (i = 0; i < irep->ncode; ++i) {
printf("%02x: ", i);
pic_dump_code(irep->code[i]);
pic_dump_code(irep->u.s.code[i]);
}
for (i = 0; i < irep->ilen; ++i) {
pic_dump_irep(irep->irep[i]);
for (i = 0; i < irep->nirep; ++i) {
pic_dump_irep(irep->u.s.irep[i].i);
}
}

View File

@ -13,22 +13,21 @@ pic_irep_incref(pic_state PIC_UNUSED(*pic), struct pic_irep *irep)
void
pic_irep_decref(pic_state *pic, struct pic_irep *irep)
{
struct pic_irep **i;
size_t i;
if (--irep->refc == 0) {
pic_free(pic, irep->code);
pic_free(pic, irep->u.s.code);
pic_free(pic, irep->u.s.ints);
pic_free(pic, irep->pool);
pic_free(pic, irep->ints);
/* unchain before decref children ireps */
irep->list.prev->next = irep->list.next;
irep->list.next->prev = irep->list.prev;
i = irep->irep;
while (*i) {
pic_irep_decref(pic, *i++);
for (i = 0; i < irep->nirep; ++i) {
pic_irep_decref(pic, irep->u.s.irep[i].i);
}
pic_free(pic, irep->irep);
pic_free(pic, irep->u.s.irep);
pic_free(pic, irep);
}
}

View File

@ -407,11 +407,11 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv)
NEXT;
}
CASE(OP_PUSHINT) {
PUSH(pic_int_value(pic->ci->irep->ints[c.a]));
PUSH(pic_int_value(pic->ci->irep->u.s.ints[c.a]));
NEXT;
}
CASE(OP_PUSHCHAR) {
PUSH(pic_char_value(pic->ci->irep->ints[c.a]));
PUSH(pic_char_value(pic->ci->irep->u.s.ints[c.a]));
NEXT;
}
CASE(OP_PUSHCONST) {
@ -567,7 +567,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv)
ci->regc = irep->capturec;
ci->regs = ci->fp + irep->argc + irep->localc;
pic->ip = irep->code;
pic->ip = irep->u.s.code;
pic_gc_arena_restore(pic, ai);
JUMP;
}
@ -629,7 +629,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv)
vm_push_cxt(pic);
}
proc = pic_make_proc_irep(pic, pic->ci->irep->irep[c.a], pic->ci->cxt);
proc = pic_make_proc_irep(pic, pic->ci->irep->u.s.irep[c.a].i, pic->ci->cxt);
PUSH(pic_obj_value(proc));
pic_gc_arena_restore(pic, ai);
NEXT;