Integrate refactoring of commit f1ed143624

This commit is contained in:
Daniel Bovensiepen
2012-12-07 16:19:52 +08:00
8 changed files with 99 additions and 115 deletions
+1 -1
View File
@@ -44,7 +44,7 @@ struct mrb_state;
typedef void* (*mrb_allocf) (struct mrb_state *mrb, void*, size_t, void *ud);
#ifndef MRB_ARENA_SIZE
#define MRB_ARENA_SIZE 1024
#define MRB_ARENA_SIZE 100
#endif
typedef struct {
+1 -1
View File
@@ -30,7 +30,7 @@ typedef struct mrb_irep {
#define MRB_ISEQ_NO_FREE 1
void mrb_add_irep(mrb_state *mrb, int n);
mrb_irep *mrb_add_irep(mrb_state *mrb);
#if defined(__cplusplus)
} /* extern "C" { */
+2 -4
View File
@@ -311,8 +311,7 @@ make_gem_mrblib(char argv[1024], char active_gems[1024])
printf("\n"
"void\n"
"GENERATED_TMP_mrb_%s_gem_init(mrb_state *mrb) {\n"
" int n = mrb_read_irep(mrb, gem_mrblib_irep_%s);\n"
" mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));\n"
" mrb_load_irep(mrb, gem_mrblib_irep_%s);\n"
" if (mrb->exc) {\n"
" mrb_p(mrb, mrb_obj_value(mrb->exc));\n"
" exit(0);\n"
@@ -368,8 +367,7 @@ make_gem_mixlib(char argv[1024], char active_gems[1024])
"void\n"
"GENERATED_TMP_mrb_%s_gem_init(mrb_state *mrb) {\n"
" mrb_%s_gem_init(mrb);\n"
" int n = mrb_read_irep(mrb, gem_mrblib_irep_%s);\n"
" mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));\n"
" mrb_load_irep(mrb, gem_mrblib_irep_%s);\n"
" if (mrb->exc) {\n"
" mrb_p(mrb, mrb_obj_value(mrb->exc));\n"
" exit(0);\n"
+1 -3
View File
@@ -9,8 +9,6 @@ extern const char mrblib_irep[];
void
mrb_init_mrblib(mrb_state *mrb)
{
int n = mrb_read_irep(mrb, mrblib_irep);
mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
mrb_load_irep(mrb, mrblib_irep);
}
+43 -54
View File
@@ -60,12 +60,9 @@ typedef struct scope {
short *lines;
int icapa;
mrb_value *pool;
int plen;
mrb_irep *irep;
int pcapa;
mrb_sym *syms;
int slen;
int scapa;
int nlocals;
int nregs;
@@ -75,7 +72,7 @@ typedef struct scope {
} codegen_scope;
static codegen_scope* scope_new(mrb_state *mrb, codegen_scope *prev, node *lv);
static void scope_finish(codegen_scope *s, int idx);
static void scope_finish(codegen_scope *s);
static struct loopinfo *loop_push(codegen_scope *s, enum looptype t);
static void loop_break(codegen_scope *s, node *tree);
static void loop_pop(codegen_scope *s, int val);
@@ -413,15 +410,17 @@ new_lit(codegen_scope *s, mrb_value val)
{
int i;
for (i=0; i<s->plen; i++) {
if (mrb_obj_equal(s->mrb, s->pool[i], val)) return i;
for (i=0; i<s->irep->plen; i++) {
if (mrb_obj_equal(s->mrb, s->irep->pool[i], val)) return i;
}
if (s->plen == s->pcapa) {
if (s->irep->plen == s->pcapa) {
s->pcapa *= 2;
s->pool = (mrb_value *)codegen_realloc(s, s->pool, sizeof(mrb_value)*s->pcapa);
s->irep->pool = (mrb_value *)codegen_realloc(s, s->irep->pool, sizeof(mrb_value)*s->pcapa);
}
s->pool[s->plen] = val;
return s->plen++;
s->irep->pool[s->irep->plen] = val;
i = s->irep->plen++;
return i;
}
static inline int
@@ -429,17 +428,17 @@ new_msym(codegen_scope *s, mrb_sym sym)
{
int i, len;
len = s->slen;
len = s->irep->slen;
if (len > 255) len = 255;
for (i=0; i<len; i++) {
if (s->syms[i] == sym) return i;
if (s->syms[i] == 0) break;
if (s->irep->syms[i] == sym) return i;
if (s->irep->syms[i] == 0) break;
}
if (i > 255) {
codegen_error(s, "too many symbols (max 256)");
}
s->syms[i] = sym;
if (i == s->slen) s->slen++;
s->irep->syms[i] = sym;
if (i == s->irep->slen) s->irep->slen++;
return i;
}
@@ -448,19 +447,19 @@ new_sym(codegen_scope *s, mrb_sym sym)
{
int i;
for (i=0; i<s->slen; i++) {
if (s->syms[i] == sym) return i;
for (i=0; i<s->irep->slen; i++) {
if (s->irep->syms[i] == sym) return i;
}
if (s->slen > 125 && s->slen < 256) {
s->syms = (mrb_sym *)codegen_realloc(s, s->syms, sizeof(mrb_sym)*65536);
for (i = 0; i < 256 - s->slen; i++) {
if (s->irep->slen > 125 && s->irep->slen < 256) {
s->irep->syms = (mrb_sym *)codegen_realloc(s, s->irep->syms, sizeof(mrb_sym)*65536);
for (i = 0; i < 256 - s->irep->slen; i++) {
static const mrb_sym mrb_sym_zero = { 0 };
s->syms[i + s->slen] = mrb_sym_zero;
s->irep->syms[i + s->irep->slen] = mrb_sym_zero;
}
s->slen = 256;
s->irep->slen = 256;
}
s->syms[s->slen] = sym;
return s->slen++;
s->irep->syms[s->irep->slen] = sym;
return s->irep->slen++;
}
static int
@@ -527,7 +526,7 @@ for_body(codegen_scope *s, node *tree)
genop_peep(s, MKOP_AB(OP_RETURN, cursp(), OP_R_NORMAL), NOVAL);
}
loop_pop(s, NOVAL);
scope_finish(s, idx);
scope_finish(s);
s = prev;
genop(s, MKOP_Abc(OP_LAMBDA, cursp(), idx - base, OP_L_BLOCK));
pop();
@@ -619,7 +618,7 @@ lambda_body(codegen_scope *s, node *tree, int blk)
if (blk) {
loop_pop(s, NOVAL);
}
scope_finish(s, idx);
scope_finish(s);
return idx - base;
}
@@ -643,7 +642,7 @@ scope_body(codegen_scope *s, node *tree)
genop_peep(scope, MKOP_AB(OP_RETURN, scope->sp, OP_R_NORMAL), NOVAL);
}
}
scope_finish(scope, idx);
scope_finish(scope);
return idx - s->idx;
}
@@ -1813,8 +1812,10 @@ codegen(codegen_scope *s, node *tree, int val)
if (val) {
char *p = (char*)tree->car;
size_t len = (intptr_t)tree->cdr;
int ai = s->mrb->arena_idx;
int off = new_lit(s, mrb_str_new(s->mrb, p, len));
s->mrb->arena_idx = ai;
genop(s, MKOP_ABx(OP_STRING, cursp(), off));
push();
}
@@ -2055,8 +2056,8 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv)
static const codegen_scope codegen_scope_zero = { 0 };
mrb_pool *pool = mrb_pool_open(mrb);
codegen_scope *p = (codegen_scope *)mrb_pool_alloc(pool, sizeof(codegen_scope));
if (!p) return 0;
if (!p) return 0;
*p = codegen_scope_zero;
p->mrb = mrb;
p->mpool = pool;
@@ -2065,25 +2066,25 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv)
p->ainfo = -1;
p->mscope = 0;
p->mrb = prev->mrb;
p->irep = mrb_add_irep(mrb);
p->idx = p->irep->idx;
p->icapa = 1024;
p->iseq = (mrb_code*)mrb_malloc(mrb, sizeof(mrb_code)*p->icapa);
p->pcapa = 32;
p->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value)*p->pcapa);
p->irep->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value)*p->pcapa);
p->irep->plen = 0;
p->syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*256);
p->scapa = 256;
p->irep->syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*256);
p->irep->slen = 0;
p->lv = lv;
p->sp += node_len(lv)+1; /* add self */
p->nlocals = p->sp;
p->ai = mrb->arena_idx;
//because of a potential bad memory access in case of gc let's allocate the irep right now
mrb_add_irep(mrb, mrb->irep_len);
mrb->irep[mrb->irep_len] = (mrb_irep *)mrb_malloc(mrb, sizeof(mrb_irep));
mrb->irep[mrb->irep_len]->plen = 0;
p->idx = mrb->irep_len++;
p->filename = prev->filename;
if (p->filename) {
p->lines = (short*)mrb_malloc(mrb, sizeof(short)*p->icapa);
@@ -2092,18 +2093,12 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv)
}
static void
scope_finish(codegen_scope *s, int idx)
scope_finish(codegen_scope *s)
{
mrb_state *mrb = s->mrb;
mrb_irep *irep;
//Comment out these instructions already done in scope_new
//mrb_add_irep(mrb, idx);
//irep = mrb->irep[idx] = (mrb_irep *)mrb_malloc(mrb, sizeof(mrb_irep));
irep = mrb->irep[idx];
mrb_irep *irep = s->irep;
irep->flags = 0;
irep->idx = idx;
if (s->iseq) {
irep->iseq = (mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->pc);
irep->ilen = s->pc;
@@ -2114,14 +2109,8 @@ scope_finish(codegen_scope *s, int idx)
irep->lines = 0;
}
}
if (s->pool) {
irep->pool = (mrb_value *)codegen_realloc(s, s->pool, sizeof(mrb_value)*s->plen);
irep->plen = s->plen;
}
if (s->syms) {
irep->syms = (mrb_sym *)codegen_realloc(s, s->syms, sizeof(mrb_sym)*s->slen);
irep->slen = s->slen;
}
irep->pool = (mrb_value *)codegen_realloc(s, irep->pool, sizeof(mrb_value)*irep->plen);
irep->syms = (mrb_sym *)codegen_realloc(s, irep->syms, sizeof(mrb_sym)*irep->slen);
if (s->filename) {
irep->filename = s->filename;
}
+23 -26
View File
@@ -29,47 +29,44 @@ void mrb_init_math(mrb_state*);
void mrb_init_mrblib(mrb_state*);
void mrb_init_mrbgems(mrb_state*);
#define DONE mrb_gc_arena_restore(mrb, 0);
void
mrb_init_core(mrb_state *mrb)
{
mrb_init_symtbl(mrb);
mrb_init_symtbl(mrb); DONE;
mrb_init_class(mrb);
mrb_init_object(mrb);
mrb_init_kernel(mrb);
mrb_init_comparable(mrb);
mrb_init_enumerable(mrb);
mrb_init_class(mrb); DONE;
mrb_init_object(mrb); DONE;
mrb_init_kernel(mrb); DONE;
mrb_init_comparable(mrb); DONE;
mrb_init_enumerable(mrb); DONE;
mrb_init_symbol(mrb);
mrb_init_exception(mrb);
mrb_init_proc(mrb);
mrb_init_string(mrb);
mrb_init_array(mrb);
mrb_init_hash(mrb);
mrb_init_numeric(mrb);
mrb_init_range(mrb);
mrb_init_symbol(mrb); DONE;
mrb_init_exception(mrb); DONE;
mrb_init_proc(mrb); DONE;
mrb_init_string(mrb); DONE;
mrb_init_array(mrb); DONE;
mrb_init_hash(mrb); DONE;
mrb_init_numeric(mrb); DONE;
mrb_init_range(mrb); DONE;
#ifdef ENABLE_STRUCT
mrb_init_struct(mrb);
mrb_init_struct(mrb); DONE;
#endif
mrb_init_gc(mrb);
mrb_init_gc(mrb); DONE;
#ifdef ENABLE_REGEXP
mrb_init_regexp(mrb);
mrb_init_regexp(mrb); DONE;
#endif
#ifdef ENABLE_STDIO
mrb_init_print(mrb);
mrb_init_print(mrb); DONE;
#endif
#ifdef ENABLE_TIME
mrb_init_time(mrb);
mrb_init_time(mrb); DONE;
#endif
#ifdef ENABLE_MATH
mrb_init_math(mrb);
mrb_init_math(mrb); DONE;
#endif
mrb_init_mrblib(mrb);
mrb_init_mrblib(mrb); DONE;
#ifdef ENABLE_GEMS
mrb_init_mrbgems(mrb);
mrb_init_mrbgems(mrb); DONE;
#endif
mrb_gc_arena_restore(mrb, 0);
}
+14 -21
View File
@@ -47,7 +47,7 @@ static unsigned char* rite_fgets(RiteFILE*,unsigned char*,int,int);
static int load_rite_header(FILE*,rite_binary_header*,unsigned char*);
static int load_rite_irep_record(mrb_state*, RiteFILE*,unsigned char*,uint32_t*);
static int read_rite_header(mrb_state*,unsigned char*,rite_binary_header*);
static int read_rite_irep_record(mrb_state*,unsigned char*,mrb_irep*,uint32_t*);
static int read_rite_irep_record(mrb_state*,unsigned char*,uint32_t*);
static unsigned char
@@ -321,7 +321,7 @@ read_rite_header(mrb_state *mrb, unsigned char *bin, rite_binary_header* bin_he
}
static int
read_rite_irep_record(mrb_state *mrb, unsigned char *src, mrb_irep *irep, uint32_t* len)
read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len)
{
int i, ret = MRB_DUMP_OK;
char *buf;
@@ -329,7 +329,9 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, mrb_irep *irep, uint32
uint16_t crc, tt, pdl, snl, offset, bufsize=MRB_DUMP_DEFAULT_STR_LEN;
mrb_int fix_num;
mrb_float f;
int plen;
int ai = mrb_gc_arena_save(mrb);
mrb_irep *irep = mrb_add_irep(mrb);
recordStart = src;
buf = (char *)mrb_malloc(mrb, bufsize);
@@ -378,16 +380,16 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, mrb_irep *irep, uint32
//POOL BLOCK
pStart = src;
irep->plen = bin_to_uint32(src); //pool length
plen = bin_to_uint32(src); //pool length
src += MRB_DUMP_SIZE_OF_LONG;
if (irep->plen > 0) {
irep->pool = (mrb_value *)mrb_malloc(mrb, sizeof(mrb_value) * irep->plen);
if (plen > 0) {
irep->pool = (mrb_value *)mrb_malloc(mrb, sizeof(mrb_value) * plen);
if (irep->pool == NULL) {
ret = MRB_DUMP_INVALID_IREP;
goto error_exit;
}
for (i=0; i<irep->plen; i++) {
for (i=0; i<plen; i++) {
tt = *src; //pool TT
src += sizeof(unsigned char);
pdl = bin_to_uint16(src); //pool data length
@@ -430,6 +432,8 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, mrb_irep *irep, uint32
irep->pool[i] = mrb_nil_value();
break;
}
irep->plen++;
mrb_gc_arena_restore(mrb, ai);
}
}
crc = calc_crc_16_ccitt((unsigned char*)pStart, src - pStart); //Calculate CRC
@@ -485,7 +489,6 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, mrb_irep *irep, uint32
*len = src - recordStart;
error_exit:
mrb_gc_arena_restore(mrb, ai);
if (buf)
mrb_free(mrb, buf);
@@ -509,25 +512,14 @@ mrb_read_irep(mrb_state *mrb, const char *bin)
//Read File Header Section
if ((nirep = read_rite_header(mrb, src, &bin_header)) < 0)
return nirep;
mrb_add_irep(mrb, sirep + nirep);
for (n=0,i=sirep; n<nirep; n++,i++) {
static const mrb_irep mrb_irep_zero = { 0 };
if ((mrb->irep[i] = (mrb_irep *)mrb_malloc(mrb, sizeof(mrb_irep))) == NULL) {
ret = MRB_DUMP_GENERAL_FAILURE;
goto error_exit;
}
*mrb->irep[i] = mrb_irep_zero;
}
src += sizeof(bin_header) + MRB_DUMP_SIZE_OF_SHORT; //header + crc
//Read Binary Data Section
for (n=0,i=sirep; n<nirep; n++,i++) {
src += MRB_DUMP_SIZE_OF_LONG; //record ren
if ((ret = read_rite_irep_record(mrb, src, mrb->irep[i], &len)) != MRB_DUMP_OK)
if ((ret = read_rite_irep_record(mrb, src, &len)) != MRB_DUMP_OK)
goto error_exit;
mrb->irep[mrb->irep_len++]->idx = i;
src += len;
}
if (0 != bin_to_uint32(src)) { //dummy record len
@@ -536,7 +528,7 @@ mrb_read_irep(mrb_state *mrb, const char *bin)
error_exit:
if (ret != MRB_DUMP_OK) {
for (n=0,i=sirep; n<nirep; n++,i++) {
for (n=0,i=sirep; i<mrb->irep_len; n++,i++) {
if (mrb->irep[i]) {
if (mrb->irep[i]->iseq)
mrb_free(mrb, mrb->irep[i]->iseq);
@@ -550,6 +542,7 @@ error_exit:
mrb_free(mrb, mrb->irep[i]);
}
}
// mrb->irep_len = sirep;
return ret;
}
return sirep + hex_to_uint8(bin_header.sirep);
+14 -5
View File
@@ -109,20 +109,23 @@ mrb_close(mrb_state *mrb)
mrb_free(mrb, mrb);
}
void
mrb_add_irep(mrb_state *mrb, int idx)
mrb_irep*
mrb_add_irep(mrb_state *mrb)
{
static const mrb_irep mrb_irep_zero = { 0 };
mrb_irep *irep;
if (!mrb->irep) {
int max = 256;
if (idx > max) max = idx+1;
if (mrb->irep_len > max) max = mrb->irep_len+1;
mrb->irep = (mrb_irep **)mrb_calloc(mrb, max, sizeof(mrb_irep*));
mrb->irep_capa = max;
}
else if (mrb->irep_capa <= idx) {
else if (mrb->irep_capa <= mrb->irep_len) {
int i;
size_t old_capa = mrb->irep_capa;
while (mrb->irep_capa <= idx) {
while (mrb->irep_capa <= mrb->irep_len) {
mrb->irep_capa *= 2;
}
mrb->irep = (mrb_irep **)mrb_realloc(mrb, mrb->irep, sizeof(mrb_irep*)*mrb->irep_capa);
@@ -130,6 +133,12 @@ mrb_add_irep(mrb_state *mrb, int idx)
mrb->irep[i] = NULL;
}
}
irep = (mrb_irep *)mrb_malloc(mrb, sizeof(mrb_irep));
*irep = mrb_irep_zero;
mrb->irep[mrb->irep_len] = irep;
irep->idx = mrb->irep_len++;
return irep;
}
mrb_value