diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/codegen.c | 43 | ||||
| -rw-r--r-- | src/dump.c | 32 | ||||
| -rw-r--r-- | src/load.c | 32 | ||||
| -rw-r--r-- | src/state.c | 5 | ||||
| -rw-r--r-- | src/string.c | 22 | ||||
| -rw-r--r-- | src/vm.c | 11 |
6 files changed, 72 insertions, 73 deletions
diff --git a/src/codegen.c b/src/codegen.c index 2072d278a..3162c44dd 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -316,8 +316,8 @@ genop_peep(codegen_scope *s, mrb_code i, int val) if (c0 == OP_STRING) { int i = GETARG_Bx(i0); - if (s->irep->pool[i].type == IREP_TT_STRING && - s->irep->pool[i].value.s->len == 0) { + if (mrb_type(s->irep->pool[i]) == MRB_TT_STRING && + RSTRING_LEN(s->irep->pool[i]) == 0) { s->pc--; return; } @@ -397,7 +397,7 @@ static inline int new_lit(codegen_scope *s, mrb_value val) { size_t i; - struct irep_pool *pv; + mrb_value *pv; switch (mrb_type(val)) { case MRB_TT_STRING: @@ -405,24 +405,24 @@ new_lit(codegen_scope *s, mrb_value val) mrb_int len; pv = &s->irep->pool[i]; - if (pv->type != IREP_TT_STRING) continue; - if ((len = pv->value.s->len) != RSTRING_LEN(val)) continue; - if (memcmp(pv->value.s->buf, RSTRING_PTR(val), len) == 0) + if (mrb_type(*pv) != MRB_TT_STRING) continue; + if ((len = RSTRING_LEN(*pv)) != RSTRING_LEN(val)) continue; + if (memcmp(RSTRING_PTR(*pv), RSTRING_PTR(val), len) == 0) return i; } break; case MRB_TT_FLOAT: for (i=0; i<s->irep->plen; i++) { pv = &s->irep->pool[i]; - if (pv->type != IREP_TT_FLOAT) continue; - if (pv->value.f == mrb_float(val)) return i; + if (mrb_type(*pv) != MRB_TT_FLOAT) continue; + if (mrb_float(*pv) == mrb_float(val)) return i; } break; case MRB_TT_FIXNUM: for (i=0; i<s->irep->plen; i++) { pv = &s->irep->pool[i]; - if (pv->type != IREP_TT_FIXNUM) continue; - if (pv->value.i == mrb_fixnum(val)) return i; + if (mrb_type(*pv) != MRB_TT_FIXNUM) continue; + if (mrb_fixnum(*pv) == mrb_fixnum(val)) return i; } break; default: @@ -432,7 +432,7 @@ new_lit(codegen_scope *s, mrb_value val) if (s->irep->plen == s->pcapa) { s->pcapa *= 2; - s->irep->pool = (struct irep_pool*)codegen_realloc(s, s->irep->pool, sizeof(struct irep_pool)*s->pcapa); + s->irep->pool = (mrb_value *)codegen_realloc(s, s->irep->pool, sizeof(mrb_value)*s->pcapa); } pv = &s->irep->pool[s->irep->plen]; @@ -440,19 +440,14 @@ new_lit(codegen_scope *s, mrb_value val) switch (mrb_type(val)) { case MRB_TT_STRING: - pv->type = IREP_TT_STRING; - pv->value.s = (struct irep_pool_string*)codegen_malloc(s, sizeof(struct irep_pool_string) + RSTRING_LEN(val)); - pv->value.s->len = RSTRING_LEN(val); - memcpy(pv->value.s->buf, RSTRING_PTR(val), RSTRING_LEN(val)); + *pv = mrb_str_dup_static(s->mrb, val); break; + case MRB_TT_FLOAT: - pv->type = IREP_TT_FLOAT; - pv->value.f = mrb_float(val); - break; case MRB_TT_FIXNUM: - pv->type = IREP_TT_FIXNUM; - pv->value.i = mrb_fixnum(val); + *pv = val; break; + default: /* should not happen */ break; @@ -2414,7 +2409,7 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv) p->iseq = (mrb_code*)mrb_malloc(mrb, sizeof(mrb_code)*p->icapa); p->pcapa = 32; - p->irep->pool = (struct irep_pool*)mrb_malloc(mrb, sizeof(struct irep_pool)*p->pcapa); + p->irep->pool = (struct mrb_value*)mrb_malloc(mrb, sizeof(struct mrb_value)*p->pcapa); p->irep->plen = 0; p->scapa = 256; @@ -2467,7 +2462,7 @@ scope_finish(codegen_scope *s) irep->lines = 0; } } - irep->pool = (struct irep_pool*)codegen_realloc(s, irep->pool, sizeof(struct irep_pool)*irep->plen); + 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) { @@ -2809,8 +2804,8 @@ codedump(mrb_state *mrb, mrb_irep *irep) break; case OP_STRING: { - struct irep_pool *pv = &irep->pool[GETARG_Bx(c)]; - mrb_value s = mrb_str_dump(mrb, mrb_str_new(mrb, pv->value.s->buf, pv->value.s->len)); + mrb_value v = irep->pool[GETARG_Bx(c)]; + mrb_value s = mrb_str_dump(mrb, mrb_str_new(mrb, RSTRING_PTR(v), RSTRING_LEN(v))); printf("OP_STRING\tR%d\t%s\n", GETARG_A(c), RSTRING_PTR(s)); } break; diff --git a/src/dump.c b/src/dump.c index 77555f8ee..95c7d51e0 100644 --- a/src/dump.c +++ b/src/dump.c @@ -79,19 +79,19 @@ get_pool_block_size(mrb_state *mrb, mrb_irep *irep) for (pool_no = 0; pool_no < irep->plen; pool_no++) { int ai = mrb_gc_arena_save(mrb); - switch (irep->pool[pool_no].type) { - case IREP_TT_FIXNUM: - str = mrb_fixnum_to_str(mrb, mrb_fixnum_value(irep->pool[pool_no].value.i), 10); + switch (mrb_type(irep->pool[pool_no])) { + case MRB_TT_FIXNUM: + str = mrb_fixnum_to_str(mrb, irep->pool[pool_no], 10); size += RSTRING_LEN(str); break; - case IREP_TT_FLOAT: - len = mrb_float_to_str(buf, irep->pool[pool_no].value.f); + case MRB_TT_FLOAT: + len = mrb_float_to_str(buf, mrb_float(irep->pool[pool_no])); size += len; break; - case IREP_TT_STRING: - size += irep->pool[pool_no].value.s->len; + case MRB_TT_STRING: + size += RSTRING_LEN(irep->pool[pool_no]); break; default: @@ -118,23 +118,23 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) for (pool_no = 0; pool_no < irep->plen; pool_no++) { int ai = mrb_gc_arena_save(mrb); - cur += uint8_to_bin(irep->pool[pool_no].type, cur); /* data type */ + cur += uint8_to_bin(mrb_type(irep->pool[pool_no]), cur); /* data type */ - switch (irep->pool[pool_no].type) { - case IREP_TT_FIXNUM: - str = mrb_fixnum_to_str(mrb, mrb_fixnum_value(irep->pool[pool_no].value.i), 10); + switch (mrb_type(irep->pool[pool_no])) { + case MRB_TT_FIXNUM: + str = mrb_fixnum_to_str(mrb, irep->pool[pool_no], 10); char_ptr = RSTRING_PTR(str); len = RSTRING_LEN(str); break; - case IREP_TT_FLOAT: - len = mrb_float_to_str(char_buf, irep->pool[pool_no].value.f); + case MRB_TT_FLOAT: + len = mrb_float_to_str(char_buf, mrb_float(irep->pool[pool_no])); char_ptr = &char_buf[0]; break; - case IREP_TT_STRING: - char_ptr = irep->pool[pool_no].value.s->buf; - len = irep->pool[pool_no].value.s->len; + case MRB_TT_STRING: + char_ptr = RSTRING_PTR(irep->pool[pool_no]); + len = RSTRING_LEN(irep->pool[pool_no]); break; default: diff --git a/src/load.c b/src/load.c index 68ebb1f7b..989325f0b 100644 --- a/src/load.c +++ b/src/load.c @@ -87,7 +87,7 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, uint32_t *len) if (SIZE_ERROR_MUL(sizeof(mrb_value), plen)) { return NULL; } - irep->pool = (struct irep_pool*)mrb_malloc(mrb, sizeof(struct irep_pool) * plen); + irep->pool = (struct mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * plen); if (irep->pool == NULL) { return NULL; } @@ -100,38 +100,22 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, uint32_t *len) src += sizeof(uint16_t); s = mrb_str_new(mrb, (char *)src, pool_data_len); src += pool_data_len; - irep->pool[i].type = tt; switch (tt) { //pool data - case IREP_TT_FIXNUM: - { - mrb_value v = mrb_str_to_inum(mrb, s, 10, FALSE); - - switch (mrb_type(v)) { - case MRB_TT_FIXNUM: - irep->pool[i].value.i = mrb_fixnum(v); - break; - case MRB_TT_FLOAT: - irep->pool[i].type = MRB_TT_FLOAT; - irep->pool[i].value.f = mrb_float(v); - default: - /* broken data; should not happen */ - irep->pool[i].value.i = 0; - } - } + case MRB_TT_FIXNUM: + irep->pool[i] = mrb_str_to_inum(mrb, s, 10, FALSE); break; - case IREP_TT_FLOAT: - irep->pool[i].value.f = mrb_str_to_dbl(mrb, s, FALSE); + case MRB_TT_FLOAT: + irep->pool[i] = mrb_float_value(mrb, mrb_str_to_dbl(mrb, s, FALSE)); break; - case IREP_TT_STRING: - irep->pool[i].value.s = (struct irep_pool_string*)mrb_malloc(mrb, sizeof(struct irep_pool_string) + pool_data_len); - irep->pool[i].value.s->len = pool_data_len; - memcpy(irep->pool[i].value.s->buf, src-pool_data_len, pool_data_len); + case MRB_TT_STRING: + irep->pool[i] = mrb_str_dup_static(mrb, s); break; default: /* should not happen */ + irep->pool[i] = mrb_nil_value(); break; } irep->plen++; diff --git a/src/state.c b/src/state.c index 9ec96068e..c8f3c48f8 100644 --- a/src/state.c +++ b/src/state.c @@ -11,6 +11,7 @@ #include "mruby/irep.h" #include "mruby/variable.h" #include "mruby/debug.h" +#include "mruby/string.h" void mrb_init_heap(mrb_state*); void mrb_init_core(mrb_state*); @@ -129,8 +130,8 @@ mrb_irep_free(mrb_state *mrb, mrb_irep *irep) if (!(irep->flags & MRB_ISEQ_NO_FREE)) mrb_free(mrb, irep->iseq); for (i=0; i<irep->plen; i++) { - if (irep->pool[i].type == IREP_TT_STRING) - mrb_free(mrb, irep->pool[i].value.s); + if (mrb_type(irep->pool[i]) == MRB_TT_STRING) + mrb_free(mrb, mrb_obj_ptr(irep->pool[i])); } mrb_free(mrb, irep->pool); mrb_free(mrb, irep->syms); diff --git a/src/string.c b/src/string.c index af70e8e45..6ec98c778 100644 --- a/src/string.c +++ b/src/string.c @@ -734,6 +734,28 @@ mrb_str_dup(mrb_state *mrb, mrb_value str) return mrb_str_new(mrb, s->ptr, s->len); } +mrb_value +mrb_str_dup_static(mrb_state *mrb, mrb_value str) +{ + struct RString *s = mrb_str_ptr(str); + struct RString *ns; + mrb_int len; + + ns = (struct RString *)mrb_malloc(mrb, sizeof(struct RString)); + ns->tt = MRB_TT_STRING; + ns->c = mrb->string_class; + + len = s->len; + ns->len = len; + ns->ptr = (char *)mrb_malloc(mrb, (size_t)len+1); + if (s->ptr) { + memcpy(ns->ptr, s->ptr, len); + } + ns->ptr[len] = '\0'; + + return mrb_obj_value(ns); +} + static mrb_value mrb_str_aref(mrb_state *mrb, mrb_value str, mrb_value indx) { @@ -554,7 +554,7 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int /* mrb_assert(mrb_proc_cfunc_p(proc)) */ mrb_irep *irep = proc->body.irep; mrb_code *pc = irep->iseq; - struct irep_pool *pool = irep->pool; + mrb_value *pool = irep->pool; mrb_sym *syms = irep->syms; mrb_value *regs = NULL; mrb_code i; @@ -618,10 +618,7 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int CASE(OP_LOADL) { /* A Bx R(A) := Pool(Bx) */ - if (pool[GETARG_Bx(i)].type == IREP_TT_FLOAT) - SET_FLT_VALUE(mrb, regs[GETARG_A(i)], pool[GETARG_Bx(i)].value.f); - else - SET_INT_VALUE(regs[GETARG_A(i)], pool[GETARG_Bx(i)].value.i); + regs[GETARG_A(i)] = pool[GETARG_Bx(i)]; NEXT; } @@ -1939,7 +1936,7 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int CASE(OP_STRING) { /* A Bx R(A) := str_new(Lit(Bx)) */ - regs[GETARG_A(i)] = mrb_str_new(mrb, pool[GETARG_Bx(i)].value.s->buf, pool[GETARG_Bx(i)].value.s->len); + regs[GETARG_A(i)] = mrb_str_dup(mrb, pool[GETARG_Bx(i)]); mrb_gc_arena_restore(mrb, ai); NEXT; } @@ -2134,7 +2131,7 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int CASE(OP_ERR) { /* Bx raise RuntimeError with message Lit(Bx) */ - mrb_value msg = mrb_str_new(mrb, pool[GETARG_Bx(i)].value.s->buf, pool[GETARG_Bx(i)].value.s->len); + mrb_value msg = mrb_str_dup(mrb, pool[GETARG_Bx(i)]); mrb_value exc; if (GETARG_A(i) == 0) { |
