From e92d4e2680716d3e16a264e46394cb6e458699f9 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Thu, 7 Nov 2013 03:54:22 +0900 Subject: modified to use irep->reps to reference child ireps. preparation for removing irep array from mrb_state. note that instructions OP_LAMBDA, OP_EXEC and OP_EPUSH are incompatible, and dumped mrb format has changed. --- include/mruby.h | 1 - include/mruby/dump.h | 22 +- include/mruby/irep.h | 5 +- mrbgems/mruby-bin-mruby/tools/mruby/mruby.c | 6 +- src/codegen.c | 87 +++--- src/dump.c | 342 +++++++++++---------- src/gc.c | 32 ++ src/load.c | 456 ++++++++++++---------------- src/parse.y | 2 +- src/proc.c | 1 - src/state.c | 5 +- src/vm.c | 8 +- tools/mrbc/mrbc.c | 22 +- 13 files changed, 497 insertions(+), 492 deletions(-) diff --git a/include/mruby.h b/include/mruby.h index 445bc5e11..6f2f12320 100644 --- a/include/mruby.h +++ b/include/mruby.h @@ -256,7 +256,6 @@ mrb_value mrb_str_new_static(mrb_state *mrb, const char *p, size_t len); mrb_state* mrb_open(void); mrb_state* mrb_open_allocf(mrb_allocf, void *ud); -void mrb_irep_free(mrb_state*, struct mrb_irep*); void mrb_close(mrb_state*); mrb_value mrb_top_self(mrb_state *); diff --git a/include/mruby/dump.h b/include/mruby/dump.h index e75be9f07..c21f225c0 100644 --- a/include/mruby/dump.h +++ b/include/mruby/dump.h @@ -12,17 +12,15 @@ extern "C" { #endif #include "mruby.h" +#include "mruby/irep.h" #ifdef ENABLE_STDIO -int mrb_dump_irep_binary(mrb_state*, size_t, int, FILE*); -int mrb_dump_irep_cfunc(mrb_state *mrb, size_t n, int, FILE *f, const char *initname); -int32_t mrb_read_irep_file(mrb_state*, FILE*); -#endif -int32_t mrb_read_irep(mrb_state*, const uint8_t*); - -#ifdef ENABLE_STDIO +int mrb_dump_irep_binary(mrb_state*, mrb_irep*, int, FILE*); +int mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep*, int, FILE *f, const char *initname); +mrb_irep *mrb_read_irep_file(mrb_state*, FILE*); mrb_value mrb_load_irep_file(mrb_state*,FILE*); #endif +mrb_irep *mrb_read_irep(mrb_state*, const uint8_t*); /* dump/load error code * @@ -43,7 +41,7 @@ mrb_value mrb_load_irep_file(mrb_state*,FILE*); /* Rite Binary File header */ #define RITE_BINARY_IDENTIFIER "RITE" -#define RITE_BINARY_FORMAT_VER "0001" +#define RITE_BINARY_FORMAT_VER "0002" #define RITE_COMPILER_NAME "MATZ" #define RITE_COMPILER_VERSION "0000" @@ -79,22 +77,14 @@ struct rite_section_irep_header { RITE_SECTION_HEADER; uint8_t rite_version[4]; // Rite Instruction Specification Version - uint8_t nirep[2]; // Number of ireps - uint8_t sirep[2]; // Start index }; struct rite_section_lineno_header { RITE_SECTION_HEADER; - - uint8_t nirep[2]; // Number of ireps - uint8_t sirep[2]; // Start index }; struct rite_section_debug_header { RITE_SECTION_HEADER; - - uint8_t nirep[2]; // Number of ireps - uint8_t sirep[2]; // Start index }; struct rite_binary_footer { diff --git a/include/mruby/irep.h b/include/mruby/irep.h index 56e043e74..83cd4b9e8 100644 --- a/include/mruby/irep.h +++ b/include/mruby/irep.h @@ -13,7 +13,6 @@ extern "C" { /* Program data array struct */ typedef struct mrb_irep { - uint32_t idx; uint16_t nlocals; /* Number of local variables */ uint16_t nregs; /* Number of register variables */ uint8_t flags; @@ -21,19 +20,21 @@ typedef struct mrb_irep { mrb_code *iseq; mrb_value *pool; mrb_sym *syms; + struct mrb_irep **reps; /* debug info */ const char *filename; uint16_t *lines; struct mrb_irep_debug_info* debug_info; - size_t ilen, plen, slen; + size_t ilen, plen, slen, rlen; } mrb_irep; #define MRB_ISEQ_NO_FREE 1 mrb_irep *mrb_add_irep(mrb_state *mrb); mrb_value mrb_load_irep(mrb_state*, const uint8_t*); +void mrb_irep_free(mrb_state*, struct mrb_irep*); #if defined(__cplusplus) } /* extern "C" { */ diff --git a/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c b/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c index 290f9e25d..6b4e5c500 100644 --- a/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c +++ b/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c @@ -198,12 +198,12 @@ main(int argc, char **argv) mrb_define_global_const(mrb, "ARGV", ARGV); if (args.mrbfile) { - n = mrb_read_irep_file(mrb, args.rfp); - if (n < 0) { + mrb_irep *irep = mrb_read_irep_file(mrb, args.rfp); + if (!irep) { fprintf(stderr, "failed to load mrb file: %s\n", args.cmdline); } else if (!args.check_syntax) { - mrb_context_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb), 0); + mrb_context_run(mrb, mrb_proc_new(mrb, irep), mrb_top_self(mrb), 0); n = 0; if (mrb->exc) { mrb_print_error(mrb); diff --git a/src/codegen.c b/src/codegen.c index 548d8d8f8..2afcb340e 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -62,6 +62,7 @@ typedef struct scope { mrb_irep *irep; size_t pcapa; int scapa; + int rcapa; int nlocals; int nregs; @@ -498,7 +499,7 @@ static void for_body(codegen_scope *s, node *tree) { codegen_scope *prev = s; - int idx, base = s->irep->idx; + int idx; struct loopinfo *lp; node *n2; mrb_code c; @@ -507,7 +508,6 @@ for_body(codegen_scope *s, node *tree) codegen(s, tree->cdr->car, VAL); // generate loop-block s = scope_new(s->mrb, s, tree->car); - idx = s->irep->idx; lp = loop_push(s, LOOP_FOR); lp->pc1 = new_label(s); @@ -532,7 +532,7 @@ for_body(codegen_scope *s, node *tree) loop_pop(s, NOVAL); scope_finish(s); s = prev; - genop(s, MKOP_Abc(OP_LAMBDA, cursp(), idx - base, OP_L_BLOCK)); + genop(s, MKOP_Abc(OP_LAMBDA, cursp(), s->irep->rlen-1, OP_L_BLOCK)); pop(); idx = new_msym(s, mrb_intern2(s->mrb, "each", 4)); genop(s, MKOP_ABC(OP_SENDB, cursp(), idx, 0)); @@ -541,11 +541,9 @@ for_body(codegen_scope *s, node *tree) static int lambda_body(codegen_scope *s, node *tree, int blk) { - int idx, base = s->irep->idx; mrb_code c; - + codegen_scope *parent = s; s = scope_new(s->mrb, s, tree->car); - idx = s->irep->idx; s->mscope = !blk; if (blk) { @@ -624,15 +622,13 @@ lambda_body(codegen_scope *s, node *tree, int blk) loop_pop(s, NOVAL); } scope_finish(s); - - return idx - base; + return parent->irep->rlen - 1; } static int scope_body(codegen_scope *s, node *tree) { codegen_scope *scope = scope_new(s->mrb, s, tree->car); - int idx = scope->irep->idx; codegen(scope, tree->cdr, VAL); if (!s->iseq) { @@ -650,9 +646,11 @@ scope_body(codegen_scope *s, node *tree) } } scope_finish(scope); - if (s->irep) - return idx - s->irep->idx; - return 0; + if (!s->irep) { + /* should not happen */ + return 0; + } + return s->irep->rlen - 1; } static mrb_bool @@ -2343,6 +2341,21 @@ codegen(codegen_scope *s, node *tree, int val) } } +static void +scope_add_irep(codegen_scope *s, mrb_irep *irep) +{ + if (s->irep == NULL) { + s->irep = irep; + return; + } + if (s->irep->rlen == s->rcapa) { + s->rcapa *= 2; + s->irep->reps = (mrb_irep**)codegen_realloc(s, s->irep->reps, sizeof(mrb_irep*)*s->rcapa); + } + s->irep->reps[s->irep->rlen] = irep; + s->irep->rlen++; +} + static codegen_scope* scope_new(mrb_state *mrb, codegen_scope *prev, node *lv) { @@ -2360,6 +2373,10 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv) p->mscope = 0; p->irep = mrb_add_irep(mrb); + scope_add_irep(prev, p->irep); + + p->rcapa = 8; + p->irep->reps = (mrb_irep**)mrb_malloc(mrb, sizeof(mrb_irep*)*p->rcapa); p->icapa = 1024; p->iseq = (mrb_code*)mrb_malloc(mrb, sizeof(mrb_code)*p->icapa); @@ -2420,6 +2437,7 @@ scope_finish(codegen_scope *s) } 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); + irep->reps = (mrb_irep **)codegen_realloc(s, irep->reps, sizeof(mrb_irep*)*irep->rlen); if (s->filename) { s->irep->filename = mrb_parser_get_filename(s->parser, s->filename_index); mrb_debug_info_append_file(mrb, s->irep, s->debug_start_pos, s->pc); @@ -2515,8 +2533,8 @@ codedump(mrb_state *mrb, mrb_irep *irep) mrb_code c; if (!irep) return; - printf("irep %d nregs=%d nlocals=%d pools=%d syms=%d\n", irep->idx, - irep->nregs, irep->nlocals, (int)irep->plen, (int)irep->slen); + printf("irep %p nregs=%d nlocals=%d pools=%d syms=%d reps=%d\n", irep, + irep->nregs, irep->nlocals, (int)irep->plen, (int)irep->slen, (int)irep->rlen); for (i=0; iilen; i++) { ai = mrb_gc_arena_save(mrb); printf("%03d ", i); @@ -2784,7 +2802,7 @@ codedump(mrb_state *mrb, mrb_irep *irep) mrb_sym2name(mrb, irep->syms[GETARG_B(c)])); break; case OP_EXEC: - printf("OP_EXEC\tR%d\tI(%d)\n", GETARG_A(c), irep->idx+GETARG_Bx(c)); + printf("OP_EXEC\tR%d\tI(%+d)\n", GETARG_A(c), GETARG_Bx(c)); break; case OP_SCLASS: printf("OP_SCLASS\tR%d\tR%d\n", GETARG_A(c), GETARG_B(c)); @@ -2796,7 +2814,7 @@ codedump(mrb_state *mrb, mrb_irep *irep) printf("OP_ERR\tL(%d)\n", GETARG_Bx(c)); break; case OP_EPUSH: - printf("OP_EPUSH\t:I(%d)\n", irep->idx+GETARG_Bx(c)); + printf("OP_EPUSH\t:I(%+d)\n", GETARG_Bx(c)); break; case OP_ONERR: printf("OP_ONERR\t%03d\n", i+GETARG_sBx(c)); @@ -2825,24 +2843,30 @@ codedump(mrb_state *mrb, mrb_irep *irep) #endif } -void -codedump_all(mrb_state *mrb, struct RProc *proc) +static void +codedump_recur(mrb_state *mrb, mrb_irep *irep) { size_t i; - mrb_irep *irep = proc->body.irep; - for (i=irep->idx; iirep_len; i++) { - codedump(mrb, mrb->irep[i]); + codedump(mrb, irep); + for (i=0; irlen; i++) { + codedump_recur(mrb, irep->reps[i]); } } -static int -codegen_start(mrb_state *mrb, parser_state *p) +void +codedump_all(mrb_state *mrb, struct RProc *proc) +{ + return codedump_recur(mrb, proc->body.irep); +} + +struct RProc* +mrb_generate_code(mrb_state *mrb, parser_state *p) { codegen_scope *scope = scope_new(mrb, 0, 0); if (!scope) { - return -1; + return NULL; } scope->mrb = mrb; scope->parser = p; @@ -2852,20 +2876,9 @@ codegen_start(mrb_state *mrb, parser_state *p) // prepare irep codegen(scope, p->tree, NOVAL); mrb_pool_close(scope->mpool); - return 0; + return mrb_proc_new(mrb, scope->irep); } else { - return -1; + return NULL; } } - -struct RProc* -mrb_generate_code(mrb_state *mrb, parser_state *p) -{ - int start = mrb->irep_len; - int n; - - n = codegen_start(mrb, p); - if (n < 0) return NULL; - return mrb_proc_new(mrb, mrb->irep[start]); -} diff --git a/src/dump.c b/src/dump.c index 73c3ed553..65b60b17c 100644 --- a/src/dump.c +++ b/src/dump.c @@ -13,7 +13,7 @@ #include "mruby/numeric.h" #include "mruby/debug.h" -static size_t get_irep_record_size(mrb_state *mrb, mrb_irep *irep); +static size_t get_irep_record_size_1(mrb_state *mrb, mrb_irep *irep); static uint32_t get_irep_header_size(mrb_state *mrb) @@ -21,7 +21,7 @@ get_irep_header_size(mrb_state *mrb) uint32_t size = 0; size += sizeof(uint32_t) * 1; - size += sizeof(uint16_t) * 2; + size += sizeof(uint16_t) * 3; return size; } @@ -31,9 +31,10 @@ write_irep_header(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) { uint8_t *cur = buf; - cur += uint32_to_bin(get_irep_record_size(mrb, irep), cur); /* record size */ + cur += uint32_to_bin(get_irep_record_size_1(mrb, irep), cur); /* record size */ cur += uint16_to_bin((uint16_t)irep->nlocals, cur); /* number of local variable */ cur += uint16_to_bin((uint16_t)irep->nregs, cur); /* number of register variable */ + cur += uint16_to_bin((uint16_t)irep->rlen, cur); /* number of child irep */ return (cur - buf); } @@ -204,47 +205,68 @@ write_syms_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) return (int)(cur - buf); } - - static size_t -get_irep_record_size(mrb_state *mrb, mrb_irep *irep) +get_irep_record_size_1(mrb_state *mrb, mrb_irep *irep) { uint32_t size = 0; - //size += sizeof(uint16_t); /* rlen */ size += get_irep_header_size(mrb); size += get_iseq_block_size(mrb, irep); size += get_pool_block_size(mrb, irep); size += get_syms_block_size(mrb, irep); + return size; +} +static size_t +get_irep_record_size(mrb_state *mrb, mrb_irep *irep) +{ + uint32_t size = 0; + size_t irep_no; + + size = get_irep_record_size_1(mrb, irep); + for (irep_no = 0; irep_no < irep->rlen; irep_no++) { + size += get_irep_record_size(mrb, irep->reps[irep_no]); + } return size; } static int write_irep_record(mrb_state *mrb, mrb_irep *irep, uint8_t* bin, uint32_t *irep_record_size) { + size_t i; + if (irep == NULL) { return MRB_DUMP_INVALID_IREP; } - *irep_record_size = get_irep_record_size(mrb, irep); + *irep_record_size = get_irep_record_size_1(mrb, irep); if (*irep_record_size == 0) { return MRB_DUMP_GENERAL_FAILURE; } memset(bin, 0, *irep_record_size); - //bin += uint16_to_bin(*irep_record_size, bin); bin += write_irep_header(mrb, irep, bin); bin += write_iseq_block(mrb, irep, bin); bin += write_pool_block(mrb, irep, bin); bin += write_syms_block(mrb, irep, bin); + for (i = 0; i < irep->rlen; i++) { + int result; + uint32_t rlen; + + result = write_irep_record(mrb, irep->reps[i], bin, &rlen); + if (result != MRB_DUMP_OK) { + return result; + } + *irep_record_size += rlen; + bin += rlen; + } return MRB_DUMP_OK; } static size_t -mrb_write_eof(mrb_state *mrb, uint8_t *bin) +write_footer(mrb_state *mrb, uint8_t *bin) { struct rite_binary_footer footer; @@ -257,58 +279,50 @@ mrb_write_eof(mrb_state *mrb, uint8_t *bin) static int -mrb_write_section_irep_header(mrb_state *mrb, uint32_t section_size, uint16_t nirep, uint16_t sirep, uint8_t *bin) +write_section_irep_header(mrb_state *mrb, uint32_t section_size, uint8_t *bin) { struct rite_section_irep_header *header = (struct rite_section_irep_header*)bin; memcpy(header->section_identify, RITE_SECTION_IREP_IDENTIFIER, sizeof(header->section_identify)); uint32_to_bin(section_size, header->section_size); memcpy(header->rite_version, RITE_VM_VER, sizeof(header->rite_version)); - uint16_to_bin(nirep, header->nirep); - uint16_to_bin(sirep, header->sirep); return MRB_DUMP_OK; } static int -mrb_write_section_irep(mrb_state *mrb, size_t start_index, uint8_t *bin) +write_section_irep(mrb_state *mrb, mrb_irep *irep, uint8_t *bin) { int result; - size_t irep_no; uint32_t section_size = 0, rlen = 0; /* size of irep record */ uint8_t *cur = bin; - if (mrb == NULL || start_index >= mrb->irep_len || bin == NULL) { + if (mrb == NULL || bin == NULL) { return MRB_DUMP_INVALID_ARGUMENT; } cur += sizeof(struct rite_section_irep_header); section_size += sizeof(struct rite_section_irep_header); - for (irep_no = start_index; irep_no < mrb->irep_len; irep_no++) { - result = write_irep_record(mrb, mrb->irep[irep_no], cur, &rlen); - if (result != MRB_DUMP_OK) { - return result; - } - cur += rlen; - section_size += rlen; + result = write_irep_record(mrb, irep, cur, &rlen); + if (result != MRB_DUMP_OK) { + return result; } - - mrb_write_section_irep_header(mrb, section_size, mrb->irep_len - start_index, start_index, bin); + cur += rlen; + section_size += rlen; + write_section_irep_header(mrb, section_size, bin); return MRB_DUMP_OK; } static int -mrb_write_section_lineno_header(mrb_state *mrb, uint32_t section_size, uint16_t nirep, uint16_t sirep, uint8_t *bin) +write_section_lineno_header(mrb_state *mrb, uint32_t section_size, uint8_t *bin) { struct rite_section_lineno_header *header = (struct rite_section_lineno_header*)bin; // TODO memcpy(header->section_identify, RITE_SECTION_LINENO_IDENTIFIER, sizeof(header->section_identify)); uint32_to_bin(section_size, header->section_size); - uint16_to_bin(nirep, header->nirep); - uint16_to_bin(sirep, header->sirep); return MRB_DUMP_OK; } @@ -327,12 +341,11 @@ get_lineno_record_size(mrb_state *mrb, mrb_irep *irep) if (irep->lines) { size += sizeof(uint16_t) * irep->ilen; // lineno } - return size; } static int -write_lineno_record(mrb_state *mrb, mrb_irep *irep, uint8_t* bin) +write_lineno_record_1(mrb_state *mrb, mrb_irep *irep, uint8_t* bin) { uint8_t *cur = bin; size_t filename_len = 0, iseq_no; @@ -365,26 +378,40 @@ write_lineno_record(mrb_state *mrb, mrb_irep *irep, uint8_t* bin) } static int -mrb_write_section_lineno(mrb_state *mrb, size_t start_index, uint8_t *bin) +write_lineno_record(mrb_state *mrb, mrb_irep *irep, uint8_t* bin) +{ + size_t i; + uint32_t rlen, size = 0; + + rlen = write_lineno_record_1(mrb, irep, bin); + bin += rlen; + size += rlen; + for (i=0; irlen; i++) { + rlen = write_lineno_record_1(mrb, irep, bin); + bin += rlen; + size += rlen; + } + return size; +} + +static int +write_section_lineno(mrb_state *mrb, mrb_irep *irep, uint8_t *bin) { - size_t irep_no; uint32_t section_size = 0, rlen = 0; /* size of irep record */ uint8_t *cur = bin; - if (mrb == NULL || start_index >= mrb->irep_len || bin == NULL) { + if (mrb == NULL || bin == NULL) { return MRB_DUMP_INVALID_ARGUMENT; } cur += sizeof(struct rite_section_lineno_header); section_size += sizeof(struct rite_section_lineno_header); - for (irep_no = start_index; irep_no < mrb->irep_len; irep_no++) { - rlen = write_lineno_record(mrb, mrb->irep[irep_no], cur); - cur += rlen; - section_size += rlen; - } + rlen = write_lineno_record(mrb, irep, cur); + cur += rlen; + section_size += rlen; - mrb_write_section_lineno_header(mrb, section_size, mrb->irep_len - start_index, start_index, bin); + write_section_lineno_header(mrb, section_size, bin); return MRB_DUMP_OK; } @@ -394,11 +421,12 @@ get_debug_record_size(mrb_state *mrb, mrb_irep *irep) { size_t ret = 0; uint32_t f_idx; + size_t i; ret += sizeof(uint32_t); // record size ret += sizeof(uint16_t); // file count - for(f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) { + for (f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) { mrb_irep_debug_info_file const* file = irep->debug_info->files[f_idx]; ret += sizeof(uint32_t); // position @@ -419,6 +447,9 @@ get_debug_record_size(mrb_state *mrb, mrb_irep *irep) default: mrb_assert(0); break; } } + for (i=0; irlen; i++) { + ret += get_debug_record_size(mrb, irep->reps[i]); + } return ret; } @@ -428,12 +459,47 @@ find_filename_index(const mrb_sym *ary, size_t ary_len, mrb_sym s) { size_t i; - for(i = 0; i < ary_len; ++i) { - if(ary[i] == s) { return i; } + for (i = 0; i < ary_len; ++i) { + if (ary[i] == s) { return i; } } return -1; } +static size_t +get_filename_table_size(mrb_state *mrb, mrb_irep *irep, mrb_sym **fp, size_t *lp) +{ + mrb_sym *filenames = *fp; + size_t tsize = 0; + size_t file_i; + size_t size = 0; + mrb_irep_debug_info *di = irep->debug_info; + + if (lp == NULL) { + lp = &tsize; + } + for (file_i = 0; file_i < di->flen; ++file_i) { + mrb_irep_debug_info_file *file; + size_t filename_len; + size_t i; + + file = di->files[file_i]; + if (find_filename_index(filenames, *lp, file->filename_sym) == -1) { + // register filename + *lp += 1; + *fp = filenames = (mrb_sym *)mrb_realloc(mrb, filenames, sizeof(mrb_sym*) * (*lp)); + filenames[*lp - 1] = file->filename_sym; + + // filename + mrb_sym2name_len(mrb, file->filename_sym, &filename_len); + size += sizeof(uint16_t) + filename_len; + } + for (i=0; irlen; i++) { + size += get_filename_table_size(mrb, irep->reps[i], fp, lp); + } + } + return size; +} + static int write_debug_record(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, mrb_sym const* filenames, size_t filenames_len) { @@ -444,7 +510,7 @@ write_debug_record(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, mrb_sym const* cur = bin + sizeof(uint32_t); // skip record size cur += uint16_to_bin(irep->debug_info->flen, cur); // file count - for(f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) { + for (f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) { int filename_idx; const mrb_irep_debug_info_file *file = irep->debug_info->files[f_idx]; @@ -453,7 +519,7 @@ write_debug_record(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, mrb_sym const* // filename index filename_idx = find_filename_index(filenames, filenames_len, - file->filename_sym); + file->filename_sym); mrb_assert(filename_idx != -1); cur += uint16_to_bin(filename_idx, cur); @@ -463,14 +529,14 @@ write_debug_record(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, mrb_sym const* switch(file->line_type) { case mrb_debug_line_ary: { size_t l; - for(l = 0; l < file->line_entry_count; ++l) { + for (l = 0; l < file->line_entry_count; ++l) { cur += uint16_to_bin(file->line_ary[l], cur); } } break; case mrb_debug_line_flat_map: { uint32_t line; - for(line = 0; line < file->line_entry_count; ++line) { + for (line = 0; line < file->line_entry_count; ++line) { cur += uint32_to_bin(file->line_flat_map[line].start_pos, cur); cur += uint16_to_bin(file->line_flat_map[line].line, cur); } @@ -489,20 +555,48 @@ write_debug_record(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, mrb_sym const* } static int -mrb_write_section_debug(mrb_state *mrb, size_t start_index, uint8_t *cur) +write_filename_table(mrb_state *mrb, mrb_irep *irep, uint8_t **cp, mrb_sym **fp, size_t *lp) +{ + uint8_t *cur = *cp; + mrb_sym *filenames = *fp; + size_t file_i; + uint16_t fn_len; + size_t size = 0; + mrb_irep_debug_info *debug_info = irep->debug_info; + + for (file_i = 0; file_i < debug_info->flen; ++file_i) { + mrb_irep_debug_info_file *file = debug_info->files[file_i]; + if (find_filename_index(filenames, *lp, file->filename_sym) != -1) continue; + + // register filename + *lp += 1; + *fp = filenames = (mrb_sym*)mrb_realloc(mrb, filenames, sizeof(mrb_sym*) * (*lp)); + filenames[*lp - 1] = file->filename_sym; + + // filename + fn_len = (uint16_t)strlen(file->filename); + cur += uint16_to_bin(fn_len, cur); + memcpy(cur, file->filename, fn_len); + cur += fn_len; + + size += sizeof(uint16_t) + fn_len; + } + *cp = cur; + return size; +} + +static int +write_section_debug(mrb_state *mrb, mrb_irep *irep, uint8_t *cur) { uint32_t section_size = 0; const uint8_t *bin = cur; struct rite_section_debug_header *header; mrb_sym *filenames; - size_t filenames_len; + size_t filenames_len, i; uint8_t *filenames_len_out; - size_t irep_i; - size_t file_i; - uint16_t fn_len; - size_t i; + uint32_t dlen; - if (mrb == NULL || start_index >= mrb->irep_len || cur == NULL) { + if (mrb == NULL || cur == NULL) { return MRB_DUMP_INVALID_ARGUMENT; } @@ -512,45 +606,23 @@ mrb_write_section_debug(mrb_state *mrb, size_t start_index, uint8_t *cur) // filename table filenames = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym *) * 1); - filenames_len = 0; filenames_len_out = cur; cur += sizeof(uint16_t); section_size += sizeof(uint16_t); - for (irep_i = start_index; irep_i < mrb->irep_len; ++irep_i) { - mrb_irep_debug_info *debug_info = mrb->irep[irep_i]->debug_info; - - for(file_i = 0; file_i < debug_info->flen; ++file_i) { - mrb_irep_debug_info_file *file = debug_info->files[file_i]; - if(find_filename_index(filenames, filenames_len, file->filename_sym) != -1) continue; - - // register filename - filenames = (mrb_sym*)mrb_realloc(mrb, filenames, sizeof(mrb_sym*) * ++filenames_len); - filenames[filenames_len - 1] = file->filename_sym; - - // filename - fn_len = (uint16_t)strlen(file->filename); - cur += uint16_to_bin(fn_len, cur); - memcpy(cur, file->filename, fn_len); - cur += fn_len; - - section_size += sizeof(uint16_t) + fn_len; - } + section_size += write_filename_table(mrb, irep, &cur, &filenames, &filenames_len); + for (i=0; irlen; i++) { + section_size += write_filename_table(mrb, irep->reps[i], &cur, &filenames, &filenames_len); } + mrb_free(mrb, filenames); uint16_to_bin(filenames_len, filenames_len_out); - // records - for (i = start_index; i < mrb->irep_len; ++i) { - uint32_t rlen = write_debug_record(mrb, mrb->irep[i], cur, filenames, filenames_len); - cur += rlen; - section_size += rlen; - } + // debug records + dlen = write_debug_record(mrb, irep, cur, filenames, filenames_len); + cur += dlen; + section_size += dlen; memcpy(header->section_identify, RITE_SECTION_DEBUG_IDENTIFIER, sizeof(header->section_identify)); uint32_to_bin(section_size, header->section_size); - uint16_to_bin(mrb->irep_len - start_index, header->nirep); - uint16_to_bin(start_index, header->sirep); - - mrb_free(mrb, filenames); return MRB_DUMP_OK; } @@ -575,124 +647,88 @@ write_rite_binary_header(mrb_state *mrb, size_t binary_size, uint8_t *bin) return MRB_DUMP_OK; } -mrb_bool is_debug_info_defined(mrb_state *mrb, size_t const start_index) +static mrb_bool +is_debug_info_defined(mrb_irep *irep) { size_t i; - for (i = start_index; i < mrb->irep_len; ++i) { - if (!mrb->irep[i]->debug_info) { return 0; } + + if (!irep->debug_info) return 0; + for (i=0; irlen; i++) { + if (!is_debug_info_defined(irep->reps[i])) return 0; } return 1; } static int -mrb_dump_irep(mrb_state *mrb, size_t start_index, int debug_info, uint8_t **bin, size_t *bin_size) +dump_irep(mrb_state *mrb, mrb_irep *irep, int debug_info, uint8_t **bin, size_t *bin_size) { int result = MRB_DUMP_GENERAL_FAILURE; - size_t section_size = 0; size_t section_irep_size; size_t section_lineno_size = 0; - size_t irep_no; uint8_t *cur = NULL; + mrb_bool const debug_info_defined = is_debug_info_defined(irep); - mrb_bool const debug_info_defined = is_debug_info_defined(mrb, start_index); - - if (mrb == NULL || start_index >= mrb->irep_len) { + if (mrb == NULL) { *bin = NULL; return MRB_DUMP_GENERAL_FAILURE; } section_irep_size = sizeof(struct rite_section_irep_header); - for (irep_no = start_index; irep_no < mrb->irep_len; irep_no++) { - section_irep_size += get_irep_record_size(mrb, mrb->irep[irep_no]); - } - section_size += section_irep_size; + section_irep_size += get_irep_record_size(mrb, irep); /* DEBUG section size */ if (debug_info) { if (debug_info_defined) { mrb_sym *filenames; - size_t filenames_len; - size_t irep_i; - size_t file_i; section_lineno_size += sizeof(struct rite_section_debug_header); - // filename table - filenames = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym *) + 1); - filenames_len = 0; + filenames = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym *) + 1); + // filename table size section_lineno_size += sizeof(uint16_t); - for (irep_i = start_index; irep_i < mrb->irep_len; ++irep_i) { - mrb_irep_debug_info *di = mrb->irep[irep_i]->debug_info; - - for(file_i = 0; file_i < di->flen; ++file_i) { - mrb_irep_debug_info_file *file; - size_t filename_len; - - file = di->files[file_i]; - if(find_filename_index(filenames, filenames_len, file->filename_sym) != -1) continue; - - // register filename - filenames = (mrb_sym *)mrb_realloc(mrb, filenames, sizeof(mrb_sym*) * ++filenames_len); - filenames[filenames_len - 1] = file->filename_sym; - - // filename - mrb_sym2name_len(mrb, file->filename_sym, &filename_len); - section_lineno_size += sizeof(uint16_t) + filename_len; - } - } + section_lineno_size += get_filename_table_size(mrb, irep, &filenames, NULL); mrb_free(mrb, filenames); - for(irep_no = start_index; irep_no < mrb->irep_len; ++irep_no) { - section_lineno_size += get_debug_record_size(mrb, mrb->irep[irep_no]); - } - section_size += section_lineno_size; + section_lineno_size += get_debug_record_size(mrb, irep); } else { section_lineno_size += sizeof(struct rite_section_lineno_header); - for (irep_no = start_index; irep_no < mrb->irep_len; irep_no++) { - section_lineno_size += get_lineno_record_size(mrb, mrb->irep[irep_no]); - } - section_size += section_lineno_size; + section_lineno_size += get_lineno_record_size(mrb, irep); } } - *bin_size += sizeof(struct rite_binary_header) + section_size + sizeof(struct rite_binary_footer); - cur = *bin = (uint8_t *)mrb_malloc(mrb, *bin_size); + *bin_size = sizeof(struct rite_binary_header) + + section_irep_size + section_lineno_size + + sizeof(struct rite_binary_footer); + cur = *bin = (uint8_t*)mrb_malloc(mrb, *bin_size); if (cur == NULL) { goto error_exit; } - cur += sizeof(struct rite_binary_header); - result = mrb_write_section_irep(mrb, start_index, cur); + result = write_section_irep(mrb, irep, cur); if (result != MRB_DUMP_OK) { goto error_exit; } - cur += section_irep_size; /* write DEBUG section */ if (debug_info) { - if(debug_info_defined) { - result = mrb_write_section_debug(mrb, start_index, cur); - if(result != MRB_DUMP_OK) { - goto error_exit; - } - cur += section_lineno_size; + if (debug_info_defined) { + result = write_section_debug(mrb, irep, cur); } else { - result = mrb_write_section_lineno(mrb, start_index, cur); - if (result != MRB_DUMP_OK) { - goto error_exit; - } - cur += section_lineno_size; + result = write_section_lineno(mrb, irep, cur); } + if (result != MRB_DUMP_OK) { + goto error_exit; + } + cur += section_lineno_size; } - mrb_write_eof(mrb, cur); - - result = write_rite_binary_header(mrb, *bin_size, *bin); + write_footer(mrb, cur); + write_rite_binary_header(mrb, *bin_size, *bin); error_exit: if (result != MRB_DUMP_OK) { @@ -706,7 +742,7 @@ error_exit: #ifdef ENABLE_STDIO int -mrb_dump_irep_binary(mrb_state *mrb, size_t start_index, int debug_info, FILE* fp) +mrb_dump_irep_binary(mrb_state *mrb, mrb_irep *irep, int debug_info, FILE* fp) { uint8_t *bin = NULL; size_t bin_size = 0; @@ -716,7 +752,7 @@ mrb_dump_irep_binary(mrb_state *mrb, size_t start_index, int debug_info, FILE* f return MRB_DUMP_INVALID_ARGUMENT; } - result = mrb_dump_irep(mrb, start_index, debug_info, &bin, &bin_size); + result = dump_irep(mrb, irep, debug_info, &bin, &bin_size); if (result == MRB_DUMP_OK) { fwrite(bin, bin_size, 1, fp); } @@ -742,7 +778,7 @@ is_valid_c_symbol_name(const char *name) } int -mrb_dump_irep_cfunc(mrb_state *mrb, size_t start_index, int debug_info, FILE *fp, const char *initname) +mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep *irep, int debug_info, FILE *fp, const char *initname) { uint8_t *bin = NULL; size_t bin_size = 0, bin_idx = 0; @@ -752,12 +788,12 @@ mrb_dump_irep_cfunc(mrb_state *mrb, size_t start_index, int debug_info, FILE *fp return MRB_DUMP_INVALID_ARGUMENT; } - result = mrb_dump_irep(mrb, start_index, debug_info, &bin, &bin_size); + result = dump_irep(mrb, irep, debug_info, &bin, &bin_size); if (result == MRB_DUMP_OK) { fprintf(fp, "#include \n"); // for uint8_t under at least Darwin fprintf(fp, "const uint8_t %s[] = {", initname); while (bin_idx < bin_size) { - if (bin_idx % 16 == 0 ) fputs("\n", fp); + if (bin_idx % 16 == 0) fputs("\n", fp); fprintf(fp, "0x%02x,", bin[bin_idx++]); } fputs("\n};\n", fp); diff --git a/src/gc.c b/src/gc.c index ccd925da8..e8dce1fdf 100644 --- a/src/gc.c +++ b/src/gc.c @@ -470,6 +470,31 @@ mark_context(mrb_state *mrb, struct mrb_context *c) } } +static size_t +mark_irep_pool_size(mrb_state *mrb, mrb_irep *irep) +{ + size_t size = irep->plen; + size_t i; + + for (i=0; irlen; i++) { + size += mark_irep_pool_size(mrb, irep->reps[i]); + } + return size; +} + +static void +mark_irep_pool(mrb_state *mrb, mrb_irep *irep) +{ + size_t i; + + for (i=0; iplen; i++) { + mrb_gc_mark_value(mrb, irep->pool[i]); + } + for (i=0; irlen; i++) { + mark_irep_pool(mrb, irep->reps[i]); + } +} + static void gc_mark_children(mrb_state *mrb, struct RBasic *obj) { @@ -504,6 +529,9 @@ gc_mark_children(mrb_state *mrb, struct RBasic *obj) mrb_gc_mark(mrb, (struct RBasic*)p->env); mrb_gc_mark(mrb, (struct RBasic*)p->target_class); + if (!MRB_PROC_CFUNC_P(p)) { + mark_irep_pool(mrb, p->body.irep); + } } break; @@ -771,6 +799,10 @@ gc_gray_mark(mrb_state *mrb, struct RBasic *obj) break; case MRB_TT_PROC: + if (!MRB_PROC_CFUNC_P((struct RProc*)obj)) { + children += mark_irep_pool_size(mrb, ((struct RProc*)obj)->body.irep); + } + /* fall through */ case MRB_TT_RANGE: children+=2; break; diff --git a/src/load.c b/src/load.c index d6397d652..9a229b8ee 100644 --- a/src/load.c +++ b/src/load.c @@ -30,31 +30,6 @@ # error This code assumes CHAR_BIT == 8 #endif -static void -irep_free(size_t sirep, mrb_state *mrb) -{ - size_t i; - void *p; - - for (i = sirep; i < mrb->irep_len; i++) { - if (mrb->irep[i]) { - p = mrb->irep[i]->iseq; - if (p) - mrb_free(mrb, p); - - p = mrb->irep[i]->pool; - if (p) - mrb_free(mrb, p); - - p = mrb->irep[i]->syms; - if (p) - mrb_free(mrb, p); - - mrb_free(mrb, mrb->irep[i]); - } - } -} - static size_t offset_crc_body(void) { @@ -62,10 +37,9 @@ offset_crc_body(void) return ((uint8_t *)header.binary_crc - (uint8_t *)&header) + sizeof(header.binary_crc); } -static int -read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) +static mrb_irep* +read_irep_record_1(mrb_state *mrb, const uint8_t *bin, uint32_t *len) { - int ret; size_t i; const uint8_t *src = bin; uint16_t tt, pool_data_len, snl; @@ -84,19 +58,21 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) irep->nregs = bin_to_uint16(src); src += sizeof(uint16_t); + // number of child irep + irep->rlen = bin_to_uint16(src); + src += sizeof(uint16_t); + // Binary Data Section // ISEQ BLOCK irep->ilen = bin_to_uint32(src); src += sizeof(uint32_t); if (irep->ilen > 0) { if (SIZE_ERROR_MUL(sizeof(mrb_code), irep->ilen)) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; + return NULL; } irep->iseq = (mrb_code *)mrb_malloc(mrb, sizeof(mrb_code) * irep->ilen); if (irep->iseq == NULL) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; + return NULL; } for (i = 0; i < irep->ilen; i++) { irep->iseq[i] = bin_to_uint32(src); //iseq @@ -109,13 +85,11 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) src += sizeof(uint32_t); if (plen > 0) { if (SIZE_ERROR_MUL(sizeof(mrb_value), plen)) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; + return NULL; } irep->pool = (mrb_value *)mrb_malloc(mrb, sizeof(mrb_value) * plen); if (irep->pool == NULL) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; + return NULL; } for (i = 0; i < plen; i++) { @@ -152,13 +126,11 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) src += sizeof(uint32_t); if (irep->slen > 0) { if (SIZE_ERROR_MUL(sizeof(mrb_sym), irep->slen)) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; + return NULL; } irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen); if (irep->syms == NULL) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; + return NULL; } for (i = 0; i < irep->slen; i++) { @@ -176,47 +148,41 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) mrb_gc_arena_restore(mrb, ai); } } + + irep->reps = (mrb_irep**)mrb_malloc(mrb, sizeof(mrb_irep*)*irep->rlen); *len = src - bin; - ret = MRB_DUMP_OK; -error_exit: - return ret; + return irep; } -static int -read_rite_section_irep(mrb_state *mrb, const uint8_t *bin) +static mrb_irep* +read_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) { - int result; - size_t sirep; - uint32_t len; - uint16_t nirep; - uint16_t n; - const struct rite_section_irep_header *header; - - header = (const struct rite_section_irep_header*)bin; - bin += sizeof(struct rite_section_irep_header); + mrb_irep *irep = read_irep_record_1(mrb, bin, len); + size_t i; - sirep = mrb->irep_len; - nirep = bin_to_uint16(header->nirep); + bin += *len; + for (i=0; irlen; i++) { + uint32_t rlen; - //Read Binary Data Section - for (n = 0; n < nirep; n++) { - result = read_rite_irep_record(mrb, bin, &len); - if (result != MRB_DUMP_OK) - goto error_exit; - bin += len; + irep->reps[i] = read_irep_record(mrb, bin, &rlen); + bin += rlen; + *len += rlen; } + return irep; +} - result = nirep; -error_exit: - if (result < MRB_DUMP_OK) { - irep_free(sirep, mrb); - } - return result; +static mrb_irep* +read_section_irep(mrb_state *mrb, const uint8_t *bin) +{ + uint32_t len; + + bin += sizeof(struct rite_section_irep_header); + return read_irep_record(mrb, bin, &len); } static int -read_rite_lineno_record(mrb_state *mrb, const uint8_t *bin, size_t irepno, uint32_t *len) +read_lineno_record_1(mrb_state *mrb, const uint8_t *bin, mrb_irep *irep, uint32_t *len) { int ret; size_t i, fname_len, niseq; @@ -231,13 +197,11 @@ read_rite_lineno_record(mrb_state *mrb, const uint8_t *bin, size_t irepno, uint3 bin += sizeof(uint16_t); *len += sizeof(uint16_t); if (SIZE_ERROR(fname_len + 1)) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; + return MRB_DUMP_GENERAL_FAILURE; } fname = (char *)mrb_malloc(mrb, fname_len + 1); if (fname == NULL) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; + return MRB_DUMP_GENERAL_FAILURE; } memcpy(fname, bin, fname_len); fname[fname_len] = '\0'; @@ -249,63 +213,56 @@ read_rite_lineno_record(mrb_state *mrb, const uint8_t *bin, size_t irepno, uint3 *len += sizeof(uint32_t); if (SIZE_ERROR_MUL(niseq, sizeof(uint16_t))) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; + return MRB_DUMP_GENERAL_FAILURE; } lines = (uint16_t *)mrb_malloc(mrb, niseq * sizeof(uint16_t)); if (lines == NULL) { - ret = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; + return MRB_DUMP_GENERAL_FAILURE; } for (i = 0; i < niseq; i++) { lines[i] = bin_to_uint16(bin); bin += sizeof(uint16_t); // niseq *len += sizeof(uint16_t); - } - - mrb->irep[irepno]->filename = fname; - mrb->irep[irepno]->lines = lines; - -error_exit: + } + irep->filename = fname; + irep->lines = lines; return ret; } static int -read_rite_section_lineno(mrb_state *mrb, const uint8_t *bin, size_t sirep) +read_lineno_record(mrb_state *mrb, const uint8_t *bin, mrb_irep *irep, uint32_t *len) { - int result; + int result = read_lineno_record_1(mrb, bin, irep, len); size_t i; + + if (result != MRB_DUMP_OK) return result; + for (i = 0; i < irep->rlen; i++) { + uint32_t len; + + result = read_lineno_record(mrb, bin, irep->reps[i], &len); + if (result != MRB_DUMP_OK) break; + bin += len; + } +} + +static int +read_section_lineno(mrb_state *mrb, const uint8_t *bin, mrb_irep *irep) +{ uint32_t len; - uint16_t nirep; - uint16_t n; - const struct rite_section_lineno_header *header; len = 0; - header = (const struct rite_section_lineno_header*)bin; bin += sizeof(struct rite_section_lineno_header); - nirep = bin_to_uint16(header->nirep); - //Read Binary Data Section - for (n = 0, i = sirep; n < nirep; n++, i++) { - result = read_rite_lineno_record(mrb, bin, i, &len); - if (result != MRB_DUMP_OK) - goto error_exit; - bin += len; - } - - result = sirep + bin_to_uint16(header->sirep); -error_exit: - return result; + return read_lineno_record(mrb, bin, irep, &len); } static int -read_rite_debug_record(mrb_state *mrb, const uint8_t *start, size_t irepno, uint32_t *len, const mrb_sym *filenames, size_t filenames_len) +read_debug_record(mrb_state *mrb, const uint8_t *start, mrb_irep* irep, uint32_t *len, const mrb_sym *filenames, size_t filenames_len) { const uint8_t *bin = start; - mrb_irep *irep = mrb->irep[irepno]; - size_t record_size; + size_t record_size, i; uint16_t f_idx; if(irep->debug_info) { return MRB_DUMP_INVALID_IREP; } @@ -368,19 +325,28 @@ read_rite_debug_record(mrb_state *mrb, const uint8_t *start, size_t irepno, uint return MRB_DUMP_GENERAL_FAILURE; } + for (i = 0; i < irep->rlen; i++) { + uint32_t len; + int ret; + + ret =read_debug_record(mrb, bin, irep->reps[i], &len, filenames, filenames_len); + if (ret != MRB_DUMP_OK) return ret; + bin += len; + } + *len = bin - start; return MRB_DUMP_OK; } static int -read_rite_section_debug(mrb_state *mrb, const uint8_t *start, size_t sirep) +read_section_debug(mrb_state *mrb, const uint8_t *start, mrb_irep *irep) { const uint8_t *bin; struct rite_section_debug_header *header; uint16_t i; + uint32_t len = 0; int result; - uint16_t nirep; size_t filenames_len; mrb_sym *filenames; @@ -388,8 +354,6 @@ read_rite_section_debug(mrb_state *mrb, const uint8_t *start, size_t sirep) header = (struct rite_section_debug_header *)bin; bin += sizeof(struct rite_section_debug_header); - nirep = bin_to_uint16(header->nirep); - filenames_len = bin_to_uint16(bin); bin += sizeof(uint16_t); filenames = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym*) * filenames_len); @@ -400,25 +364,20 @@ read_rite_section_debug(mrb_state *mrb, const uint8_t *start, size_t sirep) bin += f_len; } - for(i = sirep; i < (sirep + nirep); ++i) { - uint32_t len = 0; - result = read_rite_debug_record(mrb, bin, i, &len, filenames, filenames_len); - if (result != MRB_DUMP_OK) { goto debug_exit; } - bin += len; - } + result = read_debug_record(mrb, bin, irep, &len, filenames, filenames_len); + if (result != MRB_DUMP_OK) goto debug_exit; if ((bin - start) != bin_to_uint32(header->section_size)) { - return MRB_DUMP_GENERAL_FAILURE; + result = MRB_DUMP_GENERAL_FAILURE; } - result = sirep + bin_to_uint16(header->sirep); debug_exit: mrb_free(mrb, filenames); return result; } static int -read_rite_binary_header(const uint8_t *bin, size_t *bin_size, uint16_t *crc) +read_binary_header(const uint8_t *bin, size_t *bin_size, uint16_t *crc) { const struct rite_binary_header *header = (const struct rite_binary_header *)bin; @@ -438,63 +397,57 @@ read_rite_binary_header(const uint8_t *bin, size_t *bin_size, uint16_t *crc) return MRB_DUMP_OK; } -int32_t +mrb_irep* mrb_read_irep(mrb_state *mrb, const uint8_t *bin) { int result; - int32_t total_nirep = 0; + mrb_irep *irep; const struct rite_section_header *section_header; uint16_t crc; size_t bin_size = 0; size_t n; - size_t sirep; if ((mrb == NULL) || (bin == NULL)) { - return MRB_DUMP_INVALID_ARGUMENT; + return NULL; } - result = read_rite_binary_header(bin, &bin_size, &crc); + result = read_binary_header(bin, &bin_size, &crc); if (result != MRB_DUMP_OK) { - return result; + return NULL; } n = offset_crc_body(); if (crc != calc_crc_16_ccitt(bin + n, bin_size - n, 0)) { - return MRB_DUMP_INVALID_FILE_HEADER; + return NULL; } bin += sizeof(struct rite_binary_header); - sirep = mrb->irep_len; - do { section_header = (const struct rite_section_header *)bin; if (memcmp(section_header->section_identify, RITE_SECTION_IREP_IDENTIFIER, sizeof(section_header->section_identify)) == 0) { - result = read_rite_section_irep(mrb, bin); - if (result < MRB_DUMP_OK) { - return result; - } - total_nirep += result; + irep = read_section_irep(mrb, bin); + if (!irep) return NULL; } else if (memcmp(section_header->section_identify, RITE_SECTION_LINENO_IDENTIFIER, sizeof(section_header->section_identify)) == 0) { - result = read_rite_section_lineno(mrb, bin, sirep); + result = read_section_lineno(mrb, bin, irep); if (result < MRB_DUMP_OK) { - return result; + return NULL; } } else if (memcmp(section_header->section_identify, RITE_SECTION_DEBUG_IDENTIFIER, sizeof(section_header->section_identify)) == 0) { - result = read_rite_section_debug(mrb, bin, sirep); + result = read_section_debug(mrb, bin, irep); if (result < MRB_DUMP_OK) { - return result; + return NULL; } } bin += bin_to_uint32(section_header->section_size); } while (memcmp(section_header->section_identify, RITE_BINARY_EOF, sizeof(section_header->section_identify)) != 0); - return sirep; + return irep; } static void -irep_error(mrb_state *mrb, int n) +irep_error(mrb_state *mrb) { static const char msg[] = "irep load error"; mrb->exc = mrb_obj_ptr(mrb_exc_new(mrb, E_SCRIPT_ERROR, msg, sizeof(msg) - 1)); @@ -503,26 +456,55 @@ irep_error(mrb_state *mrb, int n) mrb_value mrb_load_irep(mrb_state *mrb, const uint8_t *bin) { - int32_t n; + mrb_irep *irep = mrb_read_irep(mrb, bin); - n = mrb_read_irep(mrb, bin); - if (n < 0) { - irep_error(mrb, n); + if (!irep) { + irep_error(mrb); return mrb_nil_value(); } - return mrb_context_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb), 0); + return mrb_context_run(mrb, mrb_proc_new(mrb, irep), mrb_top_self(mrb), 0); } #ifdef ENABLE_STDIO +static int +read_lineno_record_file(mrb_state *mrb, FILE *fp, mrb_irep *irep, uint8_t *buf) +{ + const size_t record_header_size = 4; + int result; + size_t i, len, buf_size; + void *ptr; + + if (fread(buf, record_header_size, 1, fp) == 0) { + return MRB_DUMP_READ_FAULT; + } + buf_size = bin_to_uint32(&buf[0]); + if (SIZE_ERROR(buf_size)) { + return MRB_DUMP_GENERAL_FAILURE; + } + ptr = mrb_realloc(mrb, buf, buf_size); + if (!ptr) { + return MRB_DUMP_GENERAL_FAILURE; + } + buf = (uint8_t *)ptr; + + if (fread(&buf[record_header_size], buf_size - record_header_size, 1, fp) == 0) { + return MRB_DUMP_READ_FAULT; + } + result = read_lineno_record_1(mrb, buf, irep, &len); + if (result != MRB_DUMP_OK) return result; + for (i = 0; i < irep->rlen; i++) { + result = read_lineno_record_file(mrb, fp, irep->reps[i], buf); + if (result != MRB_DUMP_OK) break; + } + return result; +} + static int32_t -read_rite_section_lineno_file(mrb_state *mrb, FILE *fp, size_t sirep) +read_section_lineno_file(mrb_state *mrb, FILE *fp, mrb_irep *irep) { int32_t result; - size_t i; - uint16_t nirep; - uint16_t n; - uint32_t len, buf_size; + uint32_t buf_size; uint8_t *buf = NULL; const size_t record_header_size = 4; @@ -531,8 +513,6 @@ read_rite_section_lineno_file(mrb_state *mrb, FILE *fp, size_t sirep) return MRB_DUMP_READ_FAULT; } - nirep = bin_to_uint16(header.nirep); - buf_size = record_header_size; /* We don't need to check buf_size. As it is enough small. */ buf = (uint8_t *)mrb_malloc(mrb, buf_size); @@ -542,122 +522,84 @@ read_rite_section_lineno_file(mrb_state *mrb, FILE *fp, size_t sirep) } //Read Binary Data Section - for (n = 0, i = sirep; n < nirep; n++, i++) { - void *ptr; - - if (fread(buf, record_header_size, 1, fp) == 0) { - result = MRB_DUMP_READ_FAULT; - goto error_exit; - } - buf_size = bin_to_uint32(&buf[0]); - if (SIZE_ERROR(buf_size)) { - result = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; - } - ptr = mrb_realloc(mrb, buf, buf_size); - if (!ptr) { - result = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; - } - buf = (uint8_t *)ptr; - - if (fread(&buf[record_header_size], buf_size - record_header_size, 1, fp) == 0) { - result = MRB_DUMP_READ_FAULT; - goto error_exit; - } - result = read_rite_lineno_record(mrb, buf, i, &len); - if (result != MRB_DUMP_OK) - goto error_exit; - } + result = read_lineno_record_file(mrb, fp, irep, buf); + if (result != MRB_DUMP_OK) + goto error_exit; - result = sirep + bin_to_uint16(header.sirep); error_exit: if (buf) { mrb_free(mrb, buf); } - if (result < MRB_DUMP_OK) { - irep_free(sirep, mrb); - } return result; } -static int32_t -read_rite_section_irep_file(mrb_state *mrb, FILE *fp) +static mrb_irep* +read_irep_record_file(mrb_state *mrb, FILE *fp, uint8_t *buf) { - int32_t result; - size_t sirep; - uint16_t nirep; - uint16_t n; - uint32_t len, buf_size; + const size_t record_header_size = 1 + 4; + size_t buf_size, len, i; + mrb_irep *irep = NULL; + void *ptr; + + if (fread(buf, record_header_size, 1, fp) == 0) { + return NULL; + } + buf_size = bin_to_uint32(&buf[0]); + if (SIZE_ERROR(buf_size)) { + return NULL; + } + ptr = mrb_realloc(mrb, buf, buf_size); + if (!ptr) return NULL; + buf = (uint8_t *)ptr; + + if (fread(&buf[record_header_size], buf_size - record_header_size, 1, fp) == 0) { + return NULL; + } + irep = read_irep_record_1(mrb, buf, &len); + if (!irep) return NULL; + for (i=0; irlen; i++) { + irep->reps[i] = read_irep_record_file(mrb, fp, buf); + if (!irep->reps[i]) return NULL; + } + return irep; +} + +static mrb_irep* +read_section_irep_file(mrb_state *mrb, FILE *fp) +{ + mrb_irep *irep = NULL; + uint32_t buf_size; uint8_t *buf = NULL; const size_t record_header_size = 1 + 4; struct rite_section_irep_header header; if (fread(&header, sizeof(struct rite_section_irep_header), 1, fp) == 0) { - return MRB_DUMP_READ_FAULT; + return NULL; } - sirep = mrb->irep_len; - nirep = bin_to_uint16(header.nirep); - buf_size = record_header_size; /* You don't need use SIZE_ERROR as buf_size is enough small. */ - buf = (uint8_t *)mrb_malloc(mrb, buf_size); - if (!buf) { - result = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; - } + buf = (uint8_t*)mrb_malloc(mrb, buf_size); + if (!buf) return NULL; //Read Binary Data Section - for (n = 0; n < nirep; n++) { - void *ptr; - - if (fread(buf, record_header_size, 1, fp) == 0) { - result = MRB_DUMP_READ_FAULT; - goto error_exit; - } - buf_size = bin_to_uint32(&buf[0]); - if (SIZE_ERROR(buf_size)) { - result = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; - } - ptr = mrb_realloc(mrb, buf, buf_size); - if (!ptr) { - result = MRB_DUMP_GENERAL_FAILURE; - goto error_exit; - } - buf = (uint8_t *)ptr; + irep = read_irep_record_file(mrb, fp, buf); - if (fread(&buf[record_header_size], buf_size - record_header_size, 1, fp) == 0) { - result = MRB_DUMP_READ_FAULT; - goto error_exit; - } - result = read_rite_irep_record(mrb, buf, &len); - if (result != MRB_DUMP_OK) - goto error_exit; - } - - result = nirep; -error_exit: if (buf) { mrb_free(mrb, buf); } - if (result < MRB_DUMP_OK) { - irep_free(sirep, mrb); - } - return result; + return irep; } -int32_t +mrb_irep* mrb_read_irep_file(mrb_state *mrb, FILE* fp) { + mrb_irep *irep; int result; - int32_t total_nirep = 0; uint8_t *buf; uint16_t crc, crcwk = 0; uint32_t section_size = 0; size_t nbytes; - size_t sirep; struct rite_section_header section_header; long fpos; size_t block_size = 1 << 14; @@ -666,22 +608,22 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp) const size_t buf_size = sizeof(struct rite_binary_header); if ((mrb == NULL) || (fp == NULL)) { - return MRB_DUMP_INVALID_ARGUMENT; + return NULL; } /* You don't need use SIZE_ERROR as buf_size is enough small. */ buf = mrb_malloc(mrb, buf_size); if (!buf) { - return MRB_DUMP_GENERAL_FAILURE; + return NULL; } if (fread(buf, buf_size, 1, fp) == 0) { mrb_free(mrb, buf); - return MRB_DUMP_READ_FAULT; + return NULL; } - result = read_rite_binary_header(buf, NULL, &crc); + result = read_binary_header(buf, NULL, &crc); mrb_free(mrb, buf); if (result != MRB_DUMP_OK) { - return result; + return NULL; } /* verify CRC */ @@ -692,7 +634,7 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp) if (buf) break; } if (!buf) { - return MRB_DUMP_GENERAL_FAILURE; + return NULL; } fseek(fp, offset_crc_body(), SEEK_SET); while ((nbytes = fread(buf, 1, block_size, fp)) > 0) { @@ -700,66 +642,58 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp) } mrb_free(mrb, buf); if (nbytes == 0 && ferror(fp)) { - return MRB_DUMP_READ_FAULT; + return NULL; } if (crcwk != crc) { - return MRB_DUMP_INVALID_FILE_HEADER; + return NULL; } fseek(fp, fpos + section_size, SEEK_SET); - sirep = mrb->irep_len; // read sections do { fpos = ftell(fp); if (fread(§ion_header, sizeof(struct rite_section_header), 1, fp) == 0) { - return MRB_DUMP_READ_FAULT; + return NULL; } section_size = bin_to_uint32(section_header.section_size); if (memcmp(section_header.section_identify, RITE_SECTION_IREP_IDENTIFIER, sizeof(section_header.section_identify)) == 0) { fseek(fp, fpos, SEEK_SET); - result = read_rite_section_irep_file(mrb, fp); - if (result < MRB_DUMP_OK) { - return result; - } - total_nirep += result; + irep = read_section_irep_file(mrb, fp); + if (!irep) return NULL; } else if (memcmp(section_header.section_identify, RITE_SECTION_LINENO_IDENTIFIER, sizeof(section_header.section_identify)) == 0) { fseek(fp, fpos, SEEK_SET); - result = read_rite_section_lineno_file(mrb, fp, sirep); - if (result < MRB_DUMP_OK) { - return result; - } + result = read_section_lineno_file(mrb, fp, irep); + if (result < MRB_DUMP_OK) return NULL; } else if (memcmp(section_header.section_identify, RITE_SECTION_DEBUG_IDENTIFIER, sizeof(section_header.section_identify)) == 0) { uint8_t* const bin = mrb_malloc(mrb, section_size); fseek(fp, fpos, SEEK_SET); if(fread((char*)bin, section_size, 1, fp) != 1) { mrb_free(mrb, bin); - return MRB_DUMP_READ_FAULT; + return NULL; } - result = read_rite_section_debug(mrb, bin, sirep); + result = read_section_debug(mrb, bin, irep); mrb_free(mrb, bin); - if (result < MRB_DUMP_OK) { - return result; - } + if (result < MRB_DUMP_OK) return NULL; } fseek(fp, fpos + section_size, SEEK_SET); } while (memcmp(section_header.section_identify, RITE_BINARY_EOF, sizeof(section_header.section_identify)) != 0); - return sirep; + return irep; } mrb_value mrb_load_irep_file(mrb_state *mrb, FILE* fp) { - int n = mrb_read_irep_file(mrb, fp); + mrb_irep *irep = mrb_read_irep_file(mrb, fp); - if (n < 0) { - irep_error(mrb, n); + if (!irep) { + irep_error(mrb); return mrb_nil_value(); } - return mrb_context_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb), 0); + return mrb_context_run(mrb, mrb_proc_new(mrb, irep), mrb_top_self(mrb), 0); } #endif /* ENABLE_STDIO */ diff --git a/src/parse.y b/src/parse.y index 168850bcc..7a1bd4760 100644 --- a/src/parse.y +++ b/src/parse.y @@ -5378,7 +5378,7 @@ load_exec(mrb_state *mrb, parser_state *p, mrbc_context *c) } if (c) { if (c->dump_result) codedump_all(mrb, proc); - if (c->no_exec) return mrb_fixnum_value(0); + if (c->no_exec) return mrb_obj_value(proc); if (c->target_class) { target = c->target_class; } diff --git a/src/proc.c b/src/proc.c index c8d09e62d..0481e9790 100644 --- a/src/proc.c +++ b/src/proc.c @@ -189,7 +189,6 @@ mrb_init_proc(mrb_state *mrb) *call_irep = mrb_irep_zero; call_irep->flags = MRB_ISEQ_NO_FREE; - call_irep->idx = -1; call_irep->iseq = call_iseq; call_irep->ilen = 1; diff --git a/src/state.c b/src/state.c index 59cb51b08..1ea76a107 100644 --- a/src/state.c +++ b/src/state.c @@ -107,7 +107,7 @@ void mrb_free_symtbl(mrb_state *mrb); void mrb_free_heap(mrb_state *mrb); void -mrb_irep_free(mrb_state *mrb, struct mrb_irep *irep) +mrb_irep_free(mrb_state *mrb, mrb_irep *irep) { if (!(irep->flags & MRB_ISEQ_NO_FREE)) mrb_free(mrb, irep->iseq); @@ -180,8 +180,7 @@ mrb_add_irep(mrb_state *mrb) } irep = (mrb_irep *)mrb_malloc(mrb, sizeof(mrb_irep)); *irep = mrb_irep_zero; - mrb->irep[mrb->irep_len] = irep; - irep->idx = mrb->irep_len++; + mrb->irep[mrb->irep_len++] = irep; return irep; } diff --git a/src/vm.c b/src/vm.c index d35ab59f2..be3518e8d 100644 --- a/src/vm.c +++ b/src/vm.c @@ -830,7 +830,7 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int /* Bx ensure_push(SEQ[Bx]) */ struct RProc *p; - p = mrb_closure_new(mrb, mrb->irep[irep->idx+GETARG_Bx(i)]); + p = mrb_closure_new(mrb, irep->reps[GETARG_Bx(i)]); /* push ensure_stack */ if (mrb->c->esize <= mrb->c->ci->eidx) { if (mrb->c->esize == 0) mrb->c->esize = 16; @@ -1967,10 +1967,10 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int int c = GETARG_c(i); if (c & OP_L_CAPTURE) { - p = mrb_closure_new(mrb, mrb->irep[irep->idx+GETARG_b(i)]); + p = mrb_closure_new(mrb, irep->reps[GETARG_b(i)]); } else { - p = mrb_proc_new(mrb, mrb->irep[irep->idx+GETARG_b(i)]); + p = mrb_proc_new(mrb, irep->reps[GETARG_b(i)]); } if (c & OP_L_STRICT) p->flags |= MRB_PROC_STRICT; regs[GETARG_A(i)] = mrb_obj_value(p); @@ -2038,7 +2038,7 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int /* prepare stack */ mrb->c->stack += a; - p = mrb_proc_new(mrb, mrb->irep[irep->idx+GETARG_Bx(i)]); + p = mrb_proc_new(mrb, irep->reps[GETARG_Bx(i)]); p->target_class = ci->target_class; ci->proc = p; diff --git a/tools/mrbc/mrbc.c b/tools/mrbc/mrbc.c index e985dcdd3..df64d6b7d 100644 --- a/tools/mrbc/mrbc.c +++ b/tools/mrbc/mrbc.c @@ -12,7 +12,6 @@ void mrb_show_version(mrb_state *); void mrb_show_copyright(mrb_state *); void parser_dump(mrb_state*, struct mrb_ast_node*, int); -void codedump_all(mrb_state*, int); struct mrbc_args { int argc; @@ -181,7 +180,7 @@ partial_hook(struct mrb_parser_state *p) return 0; } -static int +static mrb_value load_file(mrb_state *mrb, struct mrbc_args *args) { mrbc_context *c; @@ -198,7 +197,7 @@ load_file(mrb_state *mrb, struct mrbc_args *args) } else if ((infile = fopen(input, "r")) == NULL) { fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, input); - return EXIT_FAILURE; + return mrb_nil_value(); } mrbc_filename(mrb, c, input); args->idx++; @@ -209,25 +208,26 @@ load_file(mrb_state *mrb, struct mrbc_args *args) result = mrb_load_file_cxt(mrb, infile, c); if (mrb_undef_p(result) || mrb_fixnum(result) < 0) { mrbc_context_free(mrb, c); - return EXIT_FAILURE; + return mrb_nil_value(); } mrbc_context_free(mrb, c); - return EXIT_SUCCESS; + return result; } static int -dump_file(mrb_state *mrb, FILE *wfp, const char *outfile, struct mrbc_args *args) +dump_file(mrb_state *mrb, FILE *wfp, const char *outfile, struct RProc *proc, struct mrbc_args *args) { int n = MRB_DUMP_OK; + mrb_irep *irep = proc->body.irep; if (args->initname) { - n = mrb_dump_irep_cfunc(mrb, 0, args->debug_info, wfp, args->initname); + n = mrb_dump_irep_cfunc(mrb, irep, args->debug_info, wfp, args->initname); if (n == MRB_DUMP_INVALID_ARGUMENT) { fprintf(stderr, "%s: invalid C language symbol name\n", args->initname); } } else { - n = mrb_dump_irep_binary(mrb, 0, args->debug_info, wfp); + n = mrb_dump_irep_binary(mrb, irep, args->debug_info, wfp); } if (n != MRB_DUMP_OK) { fprintf(stderr, "%s: error in mrb dump (%s) %d\n", args->prog, outfile, n); @@ -242,6 +242,7 @@ main(int argc, char **argv) int n, result; struct mrbc_args args; FILE *wfp; + mrb_value load; if (mrb == NULL) { fputs("Invalid mrb_state, exiting mrbc\n", stderr); @@ -269,7 +270,8 @@ main(int argc, char **argv) } args.idx = n; - if (load_file(mrb, &args) == EXIT_FAILURE) { + load = load_file(mrb, &args); + if (mrb_nil_p(load)) { cleanup(mrb, &args); return EXIT_FAILURE; } @@ -295,7 +297,7 @@ main(int argc, char **argv) fprintf(stderr, "Output file is required\n"); return EXIT_FAILURE; } - result = dump_file(mrb, wfp, args.outfile, &args); + result = dump_file(mrb, wfp, args.outfile, mrb_proc_ptr(load), &args); fclose(wfp); cleanup(mrb, &args); if (result != MRB_DUMP_OK) { -- cgit v1.2.3