Constify `irep` members.

- `pool`
- `syms`
- `reps`
parent 744ba809
......@@ -46,7 +46,7 @@ typedef struct mrb_irep_debug_info {
* get line from irep's debug info and program counter
* @return returns NULL if not found
*/
MRB_API const char *mrb_debug_get_filename(mrb_state *mrb, mrb_irep *irep, ptrdiff_t pc);
MRB_API const char *mrb_debug_get_filename(mrb_state *mrb, const mrb_irep *irep, ptrdiff_t pc);
/*
* get line from irep's debug info and program counter
......
......@@ -18,10 +18,10 @@ MRB_BEGIN_DECL
#define DUMP_DEBUG_INFO 1
int mrb_dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size);
int mrb_dump_irep(mrb_state *mrb, const mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size);
#ifndef MRB_DISABLE_STDIO
int mrb_dump_irep_binary(mrb_state*, mrb_irep*, uint8_t, FILE*);
int mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep*, uint8_t flags, FILE *f, const char *initname);
int mrb_dump_irep_binary(mrb_state*, const mrb_irep*, uint8_t, FILE*);
int mrb_dump_irep_cfunc(mrb_state *mrb, const mrb_irep*, uint8_t flags, FILE *f, const char *initname);
mrb_irep *mrb_read_irep_file(mrb_state*, FILE*);
MRB_API mrb_value mrb_load_irep_file(mrb_state*,FILE*);
MRB_API mrb_value mrb_load_irep_file_cxt(mrb_state*, FILE*, mrbc_context*);
......
......@@ -33,11 +33,11 @@ typedef struct mrb_irep {
uint8_t flags;
const mrb_code *iseq;
mrb_value *pool;
mrb_sym *syms;
struct mrb_irep **reps;
const mrb_value *pool;
const mrb_sym *syms;
const struct mrb_irep **reps;
struct mrb_locals *lv;
const struct mrb_locals *lv;
/* debug info */
struct mrb_irep_debug_info* debug_info;
......@@ -46,6 +46,7 @@ typedef struct mrb_irep {
} mrb_irep;
#define MRB_ISEQ_NO_FREE 1
#define MRB_IREP_NO_FREE 2
MRB_API mrb_irep *mrb_add_irep(mrb_state *mrb);
......
......@@ -41,7 +41,7 @@ void mrb_env_unshare(mrb_state*, struct REnv*);
struct RProc {
MRB_OBJECT_HEADER;
union {
mrb_irep *irep;
const mrb_irep *irep;
mrb_func_t func;
} body;
struct RProc *upper;
......@@ -86,8 +86,8 @@ struct RProc {
#define mrb_proc_ptr(v) ((struct RProc*)(mrb_ptr(v)))
struct RProc *mrb_proc_new(mrb_state*, mrb_irep*);
struct RProc *mrb_closure_new(mrb_state*, mrb_irep*);
struct RProc *mrb_proc_new(mrb_state*, const mrb_irep*);
struct RProc *mrb_closure_new(mrb_state*, const mrb_irep*);
MRB_API struct RProc *mrb_proc_new_cfunc(mrb_state*, mrb_func_t);
MRB_API struct RProc *mrb_closure_new_cfunc(mrb_state *mrb, mrb_func_t func, int nlocals);
void mrb_proc_copy(struct RProc *a, struct RProc *b);
......
......@@ -238,10 +238,10 @@ static int
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;
const mrb_irep *irep = proc->body.irep;
if (args->remove_lv) {
mrb_irep_remove_lv(mrb, irep);
mrb_irep_remove_lv(mrb, (mrb_irep*)irep);
}
if (args->initname) {
n = mrb_dump_irep_cfunc(mrb, irep, args->flags, wfp, args->initname);
......
......@@ -70,6 +70,9 @@ typedef struct scope {
uint32_t icapa;
mrb_irep *irep;
mrb_value *pool;
mrb_sym *syms;
mrb_irep **reps;
uint32_t pcapa, scapa, rcapa;
uint16_t nlocals;
......@@ -98,10 +101,22 @@ static void raise_error(codegen_scope *s, const char *msg);
static void
codegen_error(codegen_scope *s, const char *message)
{
int i;
if (!s) return;
while (s->prev) {
codegen_scope *tmp = s->prev;
mrb_free(s->mrb, s->iseq);
mrb_free(s->mrb, s->pool);
mrb_free(s->mrb, s->syms);
if (s->reps) {
/* copied from mrb_irep_free() in state.c */
for (i=0; i<s->irep->rlen; i++) {
if (s->reps[i])
mrb_irep_decref(s->mrb, (mrb_irep*)s->reps[i]);
}
mrb_free(s->mrb, s->reps);
}
mrb_free(s->mrb, s->lines);
mrb_pool_close(s->mpool);
s = tmp;
......@@ -545,7 +560,7 @@ new_lit(codegen_scope *s, mrb_value val)
case MRB_TT_STRING:
for (i=0; i<s->irep->plen; i++) {
mrb_int len;
pv = &s->irep->pool[i];
pv = &s->pool[i];
if (!mrb_string_p(*pv)) continue;
if ((len = RSTRING_LEN(*pv)) != RSTRING_LEN(val)) continue;
......@@ -557,7 +572,7 @@ new_lit(codegen_scope *s, mrb_value val)
case MRB_TT_FLOAT:
for (i=0; i<s->irep->plen; i++) {
mrb_float f1, f2;
pv = &s->irep->pool[i];
pv = &s->pool[i];
if (!mrb_float_p(*pv)) continue;
f1 = mrb_float(*pv);
f2 = mrb_float(val);
......@@ -567,7 +582,7 @@ new_lit(codegen_scope *s, mrb_value val)
#endif
case MRB_TT_FIXNUM:
for (i=0; i<s->irep->plen; i++) {
pv = &s->irep->pool[i];
pv = &s->pool[i];
if (!mrb_fixnum_p(*pv)) continue;
if (mrb_fixnum(*pv) == mrb_fixnum(val)) return i;
}
......@@ -579,10 +594,10 @@ new_lit(codegen_scope *s, mrb_value val)
if (s->irep->plen == s->pcapa) {
s->pcapa *= 2;
s->irep->pool = (mrb_value *)codegen_realloc(s, s->irep->pool, sizeof(mrb_value)*s->pcapa);
s->pool = (mrb_value *)codegen_realloc(s, s->pool, sizeof(mrb_value)*s->pcapa);
}
pv = &s->irep->pool[s->irep->plen];
pv = &s->pool[s->irep->plen];
i = s->irep->plen++;
switch (mrb_type(val)) {
......@@ -620,13 +635,13 @@ new_sym(codegen_scope *s, mrb_sym sym)
len = s->irep->slen;
for (i=0; i<len; i++) {
if (s->irep->syms[i] == sym) return i;
if (s->syms[i] == sym) return i;
}
if (s->irep->slen >= s->scapa) {
s->scapa *= 2;
s->irep->syms = (mrb_sym*)codegen_realloc(s, s->irep->syms, sizeof(mrb_sym)*s->scapa);
s->syms = (mrb_sym*)codegen_realloc(s, s->syms, sizeof(mrb_sym)*s->scapa);
}
s->irep->syms[s->irep->slen] = sym;
s->syms[s->irep->slen] = sym;
return s->irep->slen++;
}
......@@ -681,7 +696,7 @@ search_upvar(codegen_scope *s, mrb_sym id, int *idx)
if (lv < 1) lv = 1;
u = s->parser->upper;
while (u && !MRB_PROC_CFUNC_P(u)) {
struct mrb_irep *ir = u->body.irep;
const struct mrb_irep *ir = u->body.irep;
uint_fast16_t n = ir->nlocals;
const struct mrb_locals *v = ir->lv;
for (; n > 1; n --, v ++) {
......@@ -2993,91 +3008,92 @@ scope_add_irep(codegen_scope *s, mrb_irep *irep)
}
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->reps = (mrb_irep**)codegen_realloc(s, s->reps, sizeof(mrb_irep*)*s->rcapa);
}
s->irep->reps[s->irep->rlen] = irep;
s->reps[s->irep->rlen] = irep;
s->irep->rlen++;
}
static codegen_scope*
scope_new(mrb_state *mrb, codegen_scope *prev, node *lv)
scope_new(mrb_state *mrb, codegen_scope *prev, node *nlv)
{
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));
codegen_scope *s = (codegen_scope *)mrb_pool_alloc(pool, sizeof(codegen_scope));
if (!p) {
if (!s) {
if (prev)
codegen_error(prev, "unexpected scope");
return NULL;
}
*p = codegen_scope_zero;
p->mrb = mrb;
p->mpool = pool;
if (!prev) return p;
p->prev = prev;
p->ainfo = -1;
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);
p->irep->iseq = NULL;
p->pcapa = 32;
p->irep->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value)*p->pcapa);
p->irep->plen = 0;
p->scapa = 256;
p->irep->syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*p->scapa);
p->irep->slen = 0;
p->lv = lv;
p->sp += node_len(lv)+1; /* add self */
p->nlocals = p->sp;
if (lv) {
node *n = lv;
*s = codegen_scope_zero;
s->mrb = mrb;
s->mpool = pool;
if (!prev) return s;
s->prev = prev;
s->ainfo = -1;
s->mscope = 0;
s->irep = mrb_add_irep(mrb);
scope_add_irep(prev, s->irep);
s->rcapa = 8;
s->reps = (mrb_irep**)mrb_malloc(mrb, sizeof(mrb_irep*)*s->rcapa);
s->icapa = 1024;
s->iseq = (mrb_code*)mrb_malloc(mrb, sizeof(mrb_code)*s->icapa);
s->irep->iseq = NULL;
s->pcapa = 32;
s->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value)*s->pcapa);
s->irep->plen = 0;
s->scapa = 256;
s->syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*s->scapa);
s->irep->slen = 0;
s->lv = nlv;
s->sp += node_len(nlv)+1; /* add self */
s->nlocals = s->sp;
if (nlv) {
struct mrb_locals *lv;
node *n = nlv;
size_t i = 0;
p->irep->lv = (struct mrb_locals*)mrb_malloc(mrb, sizeof(struct mrb_locals) * (p->nlocals - 1));
for (i=0, n=lv; n; i++,n=n->cdr) {
p->irep->lv[i].name = lv_name(n);
s->irep->lv = lv = (struct mrb_locals*)mrb_malloc(mrb, sizeof(struct mrb_locals)*(s->nlocals-1));
for (i=0, n=nlv; n; i++,n=n->cdr) {
lv[i].name = lv_name(n);
if (lv_name(n)) {
p->irep->lv[i].r = lv_idx(p, lv_name(n));
lv[i].r = lv_idx(s, lv_name(n));
}
else {
p->irep->lv[i].r = 0;
lv[i].r = 0;
}
}
mrb_assert(i + 1 == p->nlocals);
mrb_assert(i + 1 == s->nlocals);
}
p->ai = mrb_gc_arena_save(mrb);
s->ai = mrb_gc_arena_save(mrb);
p->filename_sym = prev->filename_sym;
if (p->filename_sym) {
p->lines = (uint16_t*)mrb_malloc(mrb, sizeof(short)*p->icapa);
s->filename_sym = prev->filename_sym;
if (s->filename_sym) {
s->lines = (uint16_t*)mrb_malloc(mrb, sizeof(short)*s->icapa);
}
p->lineno = prev->lineno;
s->lineno = prev->lineno;
/* debug setting */
p->debug_start_pos = 0;
if (p->filename_sym) {
mrb_debug_info_alloc(mrb, p->irep);
s->debug_start_pos = 0;
if (s->filename_sym) {
mrb_debug_info_alloc(mrb, s->irep);
}
else {
p->irep->debug_info = NULL;
s->irep->debug_info = NULL;
}
p->parser = prev->parser;
p->filename_index = prev->filename_index;
s->parser = prev->parser;
s->filename_index = prev->filename_index;
p->rlev = prev->rlev+1;
s->rlev = prev->rlev+1;
return p;
return s;
}
static void
......@@ -3091,12 +3107,12 @@ scope_finish(codegen_scope *s)
}
irep->flags = 0;
if (s->iseq) {
irep->iseq = (mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->pc);
irep->iseq = (const mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->pc);
irep->ilen = s->pc;
}
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);
irep->pool = (const mrb_value*)codegen_realloc(s, s->pool, sizeof(mrb_value)*irep->plen);
irep->syms = (const mrb_sym*)codegen_realloc(s, s->syms, sizeof(mrb_sym)*irep->slen);
irep->reps = (const mrb_irep**)codegen_realloc(s, s->reps, sizeof(mrb_irep*)*irep->rlen);
if (s->filename_sym) {
mrb_sym fname = mrb_parser_get_filename(s->parser, s->filename_index);
const char *filename = mrb_sym_name_len(s->mrb, fname, NULL);
......@@ -3242,13 +3258,14 @@ mrb_irep_remove_lv(mrb_state *mrb, mrb_irep *irep)
{
int i;
if (irep->flags & MRB_IREP_NO_FREE) return;
if (irep->lv) {
mrb_free(mrb, irep->lv);
mrb_free(mrb, (void*)irep->lv);
irep->lv = NULL;
}
if (!irep->reps) return;
for (i = 0; i < irep->rlen; ++i) {
mrb_irep_remove_lv(mrb, irep->reps[i]);
mrb_irep_remove_lv(mrb, (mrb_irep*)irep->reps[i]);
}
}
......
......@@ -279,7 +279,7 @@ local_var_p(parser_state *p, mrb_sym sym)
u = p->upper;
while (u && !MRB_PROC_CFUNC_P(u)) {
struct mrb_irep *ir = u->body.irep;
const struct mrb_irep *ir = u->body.irep;
uint_fast16_t n = ir->nlocals;
const struct mrb_locals *v = ir->lv;
for (; n > 1; n --, v ++) {
......
......@@ -342,7 +342,7 @@ local_var_p(parser_state *p, mrb_sym sym)
u = p->upper;
while (u && !MRB_PROC_CFUNC_P(u)) {
struct mrb_irep *ir = u->body.irep;
const struct mrb_irep *ir = u->body.irep;
uint_fast16_t n = ir->nlocals;
const struct mrb_locals *v = ir->lv;
for (; n > 1; n --, v ++) {
......
......@@ -133,7 +133,7 @@ static mrb_value
mrb_local_variables(mrb_state *mrb, mrb_value self)
{
struct RProc *proc;
mrb_irep *irep;
const mrb_irep *irep;
mrb_value vars;
size_t i;
......
......@@ -21,7 +21,7 @@ mrb_proc_source_location(mrb_state *mrb, mrb_value self)
return mrb_nil_value();
}
else {
mrb_irep *irep = p->body.irep;
const mrb_irep *irep = p->body.irep;
int32_t line;
const char *filename;
......@@ -41,7 +41,7 @@ mrb_proc_inspect(mrb_state *mrb, mrb_value self)
mrb_str_cat_str(mrb, str, mrb_ptr_to_str(mrb, mrb_cptr(self)));
if (!MRB_PROC_CFUNC_P(p)) {
mrb_irep *irep = p->body.irep;
const mrb_irep *irep = p->body.irep;
const char *filename;
int32_t line;
mrb_str_cat_lit(mrb, str, "@");
......
......@@ -1309,14 +1309,16 @@ init_ary_each(mrb_state *mrb, struct RClass *ary)
mrb_method_t m;
mrb_irep *each_irep = (mrb_irep*)mrb_malloc(mrb, sizeof(mrb_irep));
static const mrb_irep mrb_irep_zero = { 0 };
mrb_sym *syms;
*each_irep = mrb_irep_zero;
each_irep->syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*5);
each_irep->syms[0] = MRB_SYM(each);
each_irep->syms[1] = MRB_SYM(to_enum);
each_irep->syms[2] = MRB_QSYM(aref);
each_irep->syms[3] = MRB_SYM(call);
each_irep->syms[4] = MRB_SYM(length);
syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*5);
syms[0] = MRB_SYM(each);
syms[1] = MRB_SYM(to_enum);
syms[2] = MRB_QSYM(aref);
syms[3] = MRB_SYM(call);
syms[4] = MRB_SYM(length);
each_irep->syms = syms;
each_irep->slen = 5;
each_irep->flags = MRB_ISEQ_NO_FREE;
each_irep->iseq = each_iseq;
......
......@@ -39,7 +39,7 @@ each_backtrace(mrb_state *mrb, ptrdiff_t ciidx, const mrb_code *pc0, each_backtr
for (i=ciidx; i >= 0; i--) {
struct backtrace_location loc;
mrb_callinfo *ci;
mrb_irep *irep;
const mrb_irep *irep;
const mrb_code *pc;
ci = &mrb->c->cibase[i];
......
......@@ -2315,12 +2315,14 @@ init_class_new(mrb_state *mrb, struct RClass *cls)
struct RProc *p;
mrb_method_t m;
mrb_irep *new_irep = (mrb_irep*)mrb_malloc(mrb, sizeof(mrb_irep));
mrb_sym *syms;
static const mrb_irep mrb_irep_zero = { 0 };
*new_irep = mrb_irep_zero;
new_irep->syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*2);
new_irep->syms[0] = MRB_SYM(allocate);
new_irep->syms[1] = MRB_SYM(initialize);
syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*2);
syms[0] = MRB_SYM(allocate);
syms[1] = MRB_SYM(initialize);
new_irep->syms = syms;
new_irep->slen = 2;
new_irep->flags = MRB_ISEQ_NO_FREE;
new_irep->iseq = new_iseq;
......
......@@ -7,7 +7,7 @@
#ifndef MRB_DISABLE_STDIO
static void
print_r(mrb_state *mrb, mrb_irep *irep, size_t n)
print_r(mrb_state *mrb, const mrb_irep *irep, size_t n)
{
size_t i;
......@@ -23,7 +23,7 @@ print_r(mrb_state *mrb, mrb_irep *irep, size_t n)
}
static void
print_lv_a(mrb_state *mrb, mrb_irep *irep, uint16_t a)
print_lv_a(mrb_state *mrb, const mrb_irep *irep, uint16_t a)
{
if (!irep->lv || a >= irep->nlocals || a == 0) {
printf("\n");
......@@ -35,7 +35,7 @@ print_lv_a(mrb_state *mrb, mrb_irep *irep, uint16_t a)
}
static void
print_lv_ab(mrb_state *mrb, mrb_irep *irep, uint16_t a, uint16_t b)
print_lv_ab(mrb_state *mrb, const mrb_irep *irep, uint16_t a, uint16_t b)
{
if (!irep->lv || (a >= irep->nlocals && b >= irep->nlocals) || a+b == 0) {
printf("\n");
......@@ -48,7 +48,7 @@ print_lv_ab(mrb_state *mrb, mrb_irep *irep, uint16_t a, uint16_t b)
}
static void
print_header(mrb_state *mrb, mrb_irep *irep, ptrdiff_t i)
print_header(mrb_state *mrb, const mrb_irep *irep, ptrdiff_t i)
{
int32_t line;
......@@ -66,7 +66,7 @@ print_header(mrb_state *mrb, mrb_irep *irep, ptrdiff_t i)
#define CASE(insn,ops) case insn: FETCH_ ## ops (); L_ ## insn
static void
codedump(mrb_state *mrb, mrb_irep *irep)
codedump(mrb_state *mrb, const mrb_irep *irep)
{
int ai;
const mrb_code *pc, *pcend;
......@@ -533,7 +533,7 @@ codedump(mrb_state *mrb, mrb_irep *irep)
}
static void
codedump_recur(mrb_state *mrb, mrb_irep *irep)
codedump_recur(mrb_state *mrb, const mrb_irep *irep)
{
int i;
......
......@@ -51,7 +51,7 @@ select_line_type(const uint16_t *lines, size_t lines_len)
}
MRB_API char const*
mrb_debug_get_filename(mrb_state *mrb, mrb_irep *irep, ptrdiff_t pc)
mrb_debug_get_filename(mrb_state *mrb, const mrb_irep *irep, ptrdiff_t pc)
{
if (irep && pc >= 0 && pc < irep->ilen) {
mrb_irep_debug_info_file* f = NULL;
......
......@@ -21,7 +21,7 @@
#endif
#endif
static size_t get_irep_record_size_1(mrb_state *mrb, mrb_irep *irep);
static size_t get_irep_record_size_1(mrb_state *mrb, const mrb_irep *irep);
#if UINT32_MAX > SIZE_MAX
# error This code cannot be built on your environment.
......@@ -50,7 +50,7 @@ get_irep_header_size(mrb_state *mrb)
}
static ptrdiff_t
write_irep_header(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
write_irep_header(mrb_state *mrb, const mrb_irep *irep, uint8_t *buf)
{
uint8_t *cur = buf;
......@@ -64,7 +64,7 @@ write_irep_header(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
static size_t
get_iseq_block_size(mrb_state *mrb, mrb_irep *irep)
get_iseq_block_size(mrb_state *mrb, const mrb_irep *irep)
{
size_t size = 0;
......@@ -76,7 +76,7 @@ get_iseq_block_size(mrb_state *mrb, mrb_irep *irep)
}
static ptrdiff_t
write_iseq_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf, uint8_t flags)
write_iseq_block(mrb_state *mrb, const mrb_irep *irep, uint8_t *buf, uint8_t flags)
{
uint8_t *cur = buf;
......@@ -102,7 +102,7 @@ float_to_str(mrb_state *mrb, mrb_value flt)
#endif
static size_t
get_pool_block_size(mrb_state *mrb, mrb_irep *irep)
get_pool_block_size(mrb_state *mrb, const mrb_irep *irep)
{
int pool_no;
size_t size = 0;
......@@ -153,7 +153,7 @@ get_pool_block_size(mrb_state *mrb, mrb_irep *irep)
}
static ptrdiff_t
write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
write_pool_block(mrb_state *mrb, const mrb_irep *irep, uint8_t *buf)
{
int pool_no;
uint8_t *cur = buf;
......@@ -207,7 +207,7 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
static size_t
get_syms_block_size(mrb_state *mrb, mrb_irep *irep)
get_syms_block_size(mrb_state *mrb, const mrb_irep *irep)
{
size_t size = 0;
int sym_no;
......@@ -226,7 +226,7 @@ get_syms_block_size(mrb_state *mrb, mrb_irep *irep)
}
static ptrdiff_t
write_syms_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
write_syms_block(mrb_state *mrb, const mrb_irep *irep, uint8_t *buf)
{
int sym_no;
uint8_t *cur = buf;
......@@ -255,7 +255,7 @@ write_syms_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
}
static size_t
get_irep_record_size_1(mrb_state *mrb, mrb_irep *irep)
get_irep_record_size_1(mrb_state *mrb, const mrb_irep *irep)
{
size_t size = 0;
......@@ -267,7 +267,7 @@ get_irep_record_size_1(mrb_state *mrb, mrb_irep *irep)
}
static size_t
get_irep_record_size(mrb_state *mrb, mrb_irep *irep)
get_irep_record_size(mrb_state *mrb, const mrb_irep *irep)
{
size_t size = 0;
int irep_no;
......@@ -280,7 +280,7 @@ get_irep_record_size(mrb_state *mrb, mrb_irep *irep)
}
static int
write_irep_record(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, size_t *irep_record_size, uint8_t flags)
write_irep_record(mrb_state *mrb, const mrb_irep *irep, uint8_t *bin, size_t *irep_record_size, uint8_t flags)
{
int i;
uint8_t *src = bin;
......@@ -341,7 +341,7 @@ write_section_irep_header(mrb_state *mrb, size_t section_size, uint8_t *bin)
}
static int
write_section_irep(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, size_t *len_p, uint8_t flags)
write_section_irep(mrb_state *mrb, const mrb_irep *irep, uint8_t *bin, size_t *len_p, uint8_t flags)
{
int result;
size_t rsize = 0;
......@@ -364,7 +364,7 @@ write_section_irep(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, size_t *len_p,
}
static size_t
get_debug_record_size(mrb_state *mrb, mrb_irep *irep)
get_debug_record_size(mrb_state *mrb, const mrb_irep *irep)
{
size_t ret = 0;
uint16_t f_idx;
......@@ -413,11 +413,11 @@ find_filename_index(const mrb_sym *ary, int ary_len, mrb_sym s)
}
static size_t
get_filename_table_size(mrb_state *mrb, mrb_irep *irep, mrb_sym **fp, uint16_t *lp)
get_filename_table_size(mrb_state *mrb, const mrb_irep *irep, mrb_sym **fp, uint16_t *lp)
{
mrb_sym *filenames = *fp;
size_t size = 0;
mrb_irep_debug_info *di = irep->debug_info;
const mrb_irep_debug_info *di = irep->debug_info;
int i;
mrb_assert(lp);
......@@ -444,7 +444,7 @@ get_filename_table_size(mrb_state *mrb, mrb_irep *irep, mrb_sym **fp, uint16_t *
}
static size_t
write_debug_record_1(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, mrb_sym const* filenames, uint16_t filenames_len)
write_debug_record_1(mrb_state *mrb, const mrb_irep *irep, uint8_t *bin, mrb_sym const* filenames, uint16_t filenames_len)
{
uint8_t *cur;
uint16_t f_idx;
......@@ -498,7 +498,7 @@ write_debug_record_1(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, mrb_sym const
}
static size_t
write_debug_record(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, mrb_sym const* filenames, uint16_t filenames_len)
write_debug_record(mrb_state *mrb, const mrb_irep *irep, uint8_t *bin, mrb_sym const* filenames, uint16_t filenames_len)
{
size_t size, len;
int irep_no;
......@@ -516,7 +516,7 @@ write_debug_record(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, mrb_sym const*
}
static int
write_section_debug(mrb_state *mrb, mrb_irep *irep, uint8_t *cur, mrb_sym const *filenames, uint16_t filenames_len)
write_section_debug(mrb_state *mrb, const mrb_irep *irep, uint8_t *cur, mrb_sym const *filenames, uint16_t filenames_len)
{
size_t section_size = 0;
const uint8_t *bin = cur;
......@@ -632,7 +632,7 @@ write_lv_record(mrb_state *mrb, const mrb_irep *irep, uint8_t **start, mrb_sym c
}
static size_t
get_lv_record_size(mrb_state *mrb, mrb_irep *irep)
get_lv_record_size(mrb_state *mrb, const mrb_irep *irep)
{
size_t ret = 0;
int i;
......@@ -647,7 +647,7 @@ get_lv_record_size(mrb_state *mrb, mrb_irep *irep)
}
static size_t
get_lv_section_size(mrb_state *mrb, mrb_irep *irep, mrb_sym const *syms, uint32_t syms_len)
get_lv_section_size(mrb_state *mrb, const mrb_irep *irep, mrb_sym const *syms, uint32_t syms_len)
{
size_t ret = 0, i;
......@@ -665,7 +665,7 @@ get_lv_section_size(mrb_state *mrb, mrb_irep *irep, mrb_sym const *syms, uint32_
}
static int
write_section_lv(mrb_state *mrb, mrb_irep *irep, uint8_t *start, mrb_sym const *syms, uint32_t const syms_len)
write_section_lv(mrb_state *mrb, const mrb_irep *irep, uint8_t *start, mrb_sym const *syms, uint32_t const syms_len)
{
uint8_t *cur = start;
struct rite_section_lv_header *header;
......@@ -721,7 +721,7 @@ write_rite_binary_header(mrb_state *mrb, size_t binary_size, uint8_t *bin, uint8
}
static mrb_bool
debug_info_defined_p(mrb_irep *irep)
debug_info_defined_p(const mrb_irep *irep)
{
int i;
......@@ -733,7 +733,7 @@ debug_info_defined_p(mrb_irep *irep)
}
static mrb_bool
lv_defined_p(mrb_irep *irep)
lv_defined_p(const mrb_irep *irep)
{
int i;
......@@ -747,7 +747,7 @@ lv_defined_p(mrb_irep *irep)
}
static int
dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size)
dump_irep(mrb_state *mrb, const mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size)
{
int result = MRB_DUMP_GENERAL_FAILURE;
size_t malloc_size;
......@@ -835,7 +835,7 @@ error_exit:
}
int
mrb_dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size)
mrb_dump_irep(mrb_state *mrb, const mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size)
{
return dump_irep(mrb, irep, flags, bin, bin_size);
}
......@@ -843,7 +843,7 @@ mrb_dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size
#ifndef MRB_DISABLE_STDIO
int
mrb_dump_irep_binary(mrb_state *mrb, mrb_irep *irep, uint8_t flags, FILE* fp)
mrb_dump_irep_binary(mrb_state *mrb, const mrb_irep *irep, uint8_t flags, FILE* fp)
{
uint8_t *bin = NULL;
size_t bin_size = 0;
......@@ -865,7 +865,7 @@ mrb_dump_irep_binary(mrb_state *mrb, mrb_irep *irep, uint8_t flags, FILE* fp)
}
int
mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep *irep, uint8_t flags, FILE *fp, const char *initname)
mrb_dump_irep_cfunc(mrb_state *mrb, const mrb_irep *irep, uint8_t flags, FILE *fp, const char *initname)
{
uint8_t *bin = NULL;
size_t bin_size = 0, bin_idx = 0;
......
......@@ -868,7 +868,7 @@ obj_free(mrb_state *mrb, struct RBasic *obj, int end)
struct RProc *p = (struct RProc*)obj;
if (!MRB_PROC_CFUNC_P(p) && p->body.irep) {
mrb_irep *irep = p->body.irep;
mrb_irep *irep = (mrb_irep*)p->body.irep;
if (end) {
mrb_irep_cutref(mrb, irep);
}
......
......@@ -73,6 +73,8 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flag
uint16_t tt, pool_data_len, snl;
int plen;
struct RData *irep_obj = mrb_data_object_alloc(mrb, mrb->object_class, NULL, &tempirep_type);
mrb_value *pool;
mrb_sym *syms;
mrb_irep *irep = mrb_add_irep(mrb);
int ai = mrb_gc_arena_save(mrb);
......@@ -124,7 +126,7 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flag
if (SIZE_ERROR_MUL(plen, sizeof(mrb_value))) {
return NULL;
}
irep->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * plen);
irep->pool = pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * plen);
for (i = 0; i < plen; i++) {
const char *s;
......@@ -139,26 +141,26 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flag
case IREP_TT_FIXNUM: {
mrb_value num = mrb_str_len_to_inum(mrb, s, pool_data_len, 10, FALSE);
#ifdef MRB_WITHOUT_FLOAT
irep->pool[i] = num;
pool[i] = num;
#else
irep->pool[i] = mrb_float_p(num)? mrb_float_pool(mrb, mrb_float(num)) : num;
pool[i] = mrb_float_p(num)? mrb_float_pool(mrb, mrb_float(num)) : num;
#endif
}
break;
#ifndef MRB_WITHOUT_FLOAT
case IREP_TT_FLOAT:
irep->pool[i] = mrb_float_pool(mrb, str_to_double(mrb, s, pool_data_len));
pool[i] = mrb_float_pool(mrb, str_to_double(mrb, s, pool_data_len));
break;
#endif
case IREP_TT_STRING:
irep->pool[i] = mrb_str_pool(mrb, s, pool_data_len, st);
pool[i] = mrb_str_pool(mrb, s, pool_data_len, st);
break;
default:
/* should not happen */
irep->pool[i] = mrb_nil_value();
pool[i] = mrb_nil_value();
break;
}
irep->plen++;
......@@ -173,22 +175,22 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flag
if (SIZE_ERROR_MUL(irep->slen, sizeof(mrb_sym))) {
return NULL;
}
irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen);
irep->syms = syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen);
for (i = 0; i < irep->slen; i++) {
snl = bin_to_uint16(src); /* symbol name length */
src += sizeof(uint16_t);
if (snl == MRB_DUMP_NULL_SYM_LEN) {
irep->syms[i] = 0;
syms[i] = 0;
continue;
}
if (flags & FLAG_SRC_MALLOC) {
irep->syms[i] = mrb_intern(mrb, (char *)src, snl);
syms[i] = mrb_intern(mrb, (char *)src, snl);
}
else {
irep->syms[i] = mrb_intern_static(mrb, (char *)src, snl);
syms[i] = mrb_intern_static(mrb, (char *)src, snl);
}
src += snl + 1;
......@@ -196,8 +198,6 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flag
}
}
irep->reps = (mrb_irep**)mrb_calloc(mrb, irep->rlen, sizeof(mrb_irep*));
diff = src - bin;
mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
*len = (size_t)diff;
......@@ -214,21 +214,24 @@ read_irep_record(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flags)
int ai = mrb_gc_arena_save(mrb);
mrb_irep *irep = read_irep_record_1(mrb, bin, len, flags);
int i;
mrb_irep **reps;
mrb_gc_arena_restore(mrb, ai);
if (irep == NULL) {
return NULL;
}
reps = (mrb_irep**)mrb_calloc(mrb, irep->rlen, sizeof(mrb_irep*));
irep->reps = (const mrb_irep**)reps;
irep_obj->data = irep;
bin += *len;
for (i=0; i<irep->rlen; i++) {
size_t rlen;
irep->reps[i] = read_irep_record(mrb, bin, &rlen, flags);
reps[i] = read_irep_record(mrb, bin, &rlen, flags);
mrb_gc_arena_restore(mrb, ai);
if (irep->reps[i] == NULL) {
if (reps[i] == NULL) {
return NULL;
}
bin += rlen;
......@@ -257,25 +260,26 @@ read_debug_record(mrb_state *mrb, const uint8_t *start, mrb_irep* irep, size_t *
size_t record_size;
uint16_t f_idx;
int i;
mrb_irep_debug_info *debug;
if (irep->debug_info) { return MRB_DUMP_INVALID_IREP; }
irep->debug_info = (mrb_irep_debug_info*)mrb_calloc(mrb, 1, sizeof(mrb_irep_debug_info));
irep->debug_info->pc_count = (uint32_t)irep->ilen;
irep->debug_info = debug = (mrb_irep_debug_info*)mrb_calloc(mrb, 1, sizeof(mrb_irep_debug_info));
debug->pc_count = (uint32_t)irep->ilen;
record_size = (size_t)bin_to_uint32(bin);
bin += sizeof(uint32_t);
irep->debug_info->flen = bin_to_uint16(bin);
irep->debug_info->files = (mrb_irep_debug_info_file**)mrb_calloc(mrb, irep->debug_info->flen, sizeof(mrb_irep_debug_info*));
debug->flen = bin_to_uint16(bin);
debug->files = (mrb_irep_debug_info_file**)mrb_calloc(mrb, irep->debug_info->flen, sizeof(mrb_irep_debug_info*));
bin += sizeof(uint16_t);
for (f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) {
for (f_idx = 0; f_idx < debug->flen; ++f_idx) {
mrb_irep_debug_info_file *file;
uint16_t filename_idx;
file = (mrb_irep_debug_info_file *)mrb_calloc(mrb, 1, sizeof(*file));
irep->debug_info->files[f_idx] = file;
debug->files[f_idx] = file;
file->start_pos = bin_to_uint32(bin);
bin += sizeof(uint32_t);
......@@ -329,7 +333,7 @@ read_debug_record(mrb_state *mrb, const uint8_t *start, mrb_irep* irep, size_t *
size_t len;
int ret;
ret = read_debug_record(mrb, bin, irep->reps[i], &len, filenames, filenames_len);
ret = read_debug_record(mrb, bin, (mrb_irep*)irep->reps[i], &len, filenames, filenames_len);
if (ret != MRB_DUMP_OK) return ret;
bin += len;
}
......@@ -393,25 +397,26 @@ static int
read_lv_record(mrb_state *mrb, const uint8_t *start, mrb_irep *irep, size_t *record_len, mrb_sym const *syms, uint32_t syms_len)
{
const uint8_t *bin = start;
struct mrb_locals *lv;
ptrdiff_t diff;
int i;
irep->lv = (struct mrb_locals*)mrb_malloc(mrb, sizeof(struct mrb_locals) * (irep->nlocals - 1));
irep->lv = lv = (struct mrb_locals*)mrb_malloc(mrb, sizeof(struct mrb_locals) * (irep->nlocals - 1));
for (i = 0; i + 1< irep->nlocals; ++i) {
uint16_t const sym_idx = bin_to_uint16(bin);
bin += sizeof(uint16_t);
if (sym_idx == RITE_LV_NULL_MARK) {
irep->lv[i].name = 0;
irep->lv[i].r = 0;
lv[i].name = 0;
lv[i].r = 0;
}
else {
if (sym_idx >= syms_len) {
return MRB_DUMP_GENERAL_FAILURE;
}
irep->lv[i].name = syms[sym_idx];
lv[i].name = syms[sym_idx];
irep->lv[i].r = bin_to_uint16(bin);
lv[i].r = bin_to_uint16(bin);
}
bin += sizeof(uint16_t);
}
......@@ -420,7 +425,7 @@ read_lv_record(mrb_state *mrb, const uint8_t *start, mrb_irep *irep, size_t *rec
size_t len;
int ret;
ret = read_lv_record(mrb, bin, irep->reps[i], &len, syms, syms_len);
ret = read_lv_record(mrb, bin, (mrb_irep*)irep->reps[i], &len, syms, syms_len);
if (ret != MRB_DUMP_OK) return ret;
bin += len;
}
......
......@@ -15,7 +15,7 @@ static const mrb_code call_iseq[] = {
};
struct RProc*
mrb_proc_new(mrb_state *mrb, mrb_irep *irep)
mrb_proc_new(mrb_state *mrb, const mrb_irep *irep)
{
struct RProc *p;
mrb_callinfo *ci = mrb->c->ci;
......@@ -34,7 +34,7 @@ mrb_proc_new(mrb_state *mrb, mrb_irep *irep)
p->e.target_class = tc;
}
p->body.irep = irep;
mrb_irep_incref(mrb, irep);
mrb_irep_incref(mrb, (mrb_irep*)irep);
return p;
}
......@@ -90,7 +90,7 @@ closure_setup(mrb_state *mrb, struct RProc *p)
}
struct RProc*
mrb_closure_new(mrb_state *mrb, mrb_irep *irep)
mrb_closure_new(mrb_state *mrb, const mrb_irep *irep)
{
struct RProc *p = mrb_proc_new(mrb, irep);
......@@ -181,7 +181,7 @@ mrb_proc_copy(struct RProc *a, struct RProc *b)
a->flags = b->flags;
a->body = b->body;
if (!MRB_PROC_CFUNC_P(a) && a->body.irep) {
a->body.irep->refcnt++;
mrb_irep_incref(NULL, (mrb_irep*)a->body.irep);
}
a->upper = b->upper;
a->e.env = b->e.env;
......@@ -262,7 +262,7 @@ proc_lambda(mrb_state *mrb, mrb_value self)
mrb_int
mrb_proc_arity(const struct RProc *p)
{
struct mrb_irep *irep;
const mrb_irep *irep;
const mrb_code *pc;
mrb_aspec aspec;
int ma, op, ra, pa, arity;
......
......@@ -107,12 +107,14 @@ void mrb_free_symtbl(mrb_state *mrb);
void
mrb_irep_incref(mrb_state *mrb, mrb_irep *irep)
{
if (irep->flags & MRB_IREP_NO_FREE) return;
irep->refcnt++;
}
void
mrb_irep_decref(mrb_state *mrb, mrb_irep *irep)
{
if (irep->flags & MRB_IREP_NO_FREE) return;
irep->refcnt--;
if (irep->refcnt == 0) {
mrb_irep_free(mrb, irep);
......@@ -122,12 +124,14 @@ mrb_irep_decref(mrb_state *mrb, mrb_irep *irep)
void
mrb_irep_cutref(mrb_state *mrb, mrb_irep *irep)
{
mrb_irep *tmp;
mrb_irep **reps;
int i;
if (irep->flags & MRB_IREP_NO_FREE) return;
reps = (mrb_irep**)irep->reps;
for (i=0; i<irep->rlen; i++) {
tmp = irep->reps[i];
irep->reps[i] = NULL;
mrb_irep *tmp = reps[i];
reps[i] = NULL;
if (tmp) mrb_irep_decref(mrb, tmp);
}
}
......@@ -137,6 +141,7 @@ mrb_irep_free(mrb_state *mrb, mrb_irep *irep)
{
int i;
if (irep->flags & MRB_IREP_NO_FREE) return;
if (!(irep->flags & MRB_ISEQ_NO_FREE))
mrb_free(mrb, (void*)irep->iseq);
if (irep->pool) for (i=0; i<irep->plen; i++) {
......@@ -150,16 +155,16 @@ mrb_irep_free(mrb_state *mrb, mrb_irep *irep)
}
#endif
}
mrb_free(mrb, irep->pool);
mrb_free(mrb, irep->syms);
mrb_free(mrb, (void*)irep->pool);
mrb_free(mrb, (void*)irep->syms);
if (irep->reps) {
for (i=0; i<irep->rlen; i++) {
if (irep->reps[i])
mrb_irep_decref(mrb, irep->reps[i]);
mrb_irep_decref(mrb, (mrb_irep*)irep->reps[i]);
}
mrb_free(mrb, (void*)irep->reps);
}
mrb_free(mrb, irep->reps);
mrb_free(mrb, irep->lv);
mrb_free(mrb, (void*)irep->lv);
mrb_debug_info_free(mrb, irep->debug_info);
mrb_free(mrb, irep);
}
......
......@@ -920,7 +920,7 @@ argnum_error(mrb_state *mrb, mrb_int num)
MRB_API mrb_value
mrb_vm_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int stack_keep)
{
mrb_irep *irep = proc->body.irep;
const mrb_irep *irep = proc->body.irep;
mrb_value result;
struct mrb_context *c = mrb->c;
ptrdiff_t cioff = c->ci - c->cibase;
......@@ -965,9 +965,9 @@ mrb_vm_exec(mrb_state *mrb, struct RProc *proc, const mrb_code *pc)
{
/* mrb_assert(MRB_PROC_CFUNC_P(proc)) */
const mrb_code *pc0 = pc;
mrb_irep *irep = proc->body.irep;
mrb_value *pool = irep->pool;
mrb_sym *syms = irep->syms;
const mrb_irep *irep = proc->body.irep;
const mrb_value *pool = irep->pool;
const mrb_sym *syms = irep->syms;
mrb_code insn;
int ai = mrb_gc_arena_save(mrb);
struct mrb_jmpbuf *prev_jmp = mrb->jmp;
......@@ -2549,7 +2549,7 @@ RETRY_TRY_BLOCK:
L_MAKE_LAMBDA:
{
struct RProc *p;
mrb_irep *nirep = irep->reps[b];
const mrb_irep *nirep = irep->reps[b];
if (c & OP_L_CAPTURE) {
p = mrb_closure_new(mrb, nirep);
......@@ -2627,7 +2627,7 @@ RETRY_TRY_BLOCK:
CASE(OP_EXEC, BB) {
mrb_value recv = regs[a];
struct RProc *p;
mrb_irep *nirep = irep->reps[b];
const mrb_irep *nirep = irep->reps[b];
/* prepare closure */
p = mrb_proc_new(mrb, nirep);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment