Commit 54c5b12f authored by Miura Hideki's avatar Miura Hideki

irep->pool struct pool -> mrb_value

parent 2bda9652
...@@ -26,17 +26,7 @@ typedef struct mrb_irep { ...@@ -26,17 +26,7 @@ typedef struct mrb_irep {
uint8_t flags; uint8_t flags;
mrb_code *iseq; mrb_code *iseq;
struct irep_pool { mrb_value *pool;
union {
mrb_float f;
struct irep_pool_string {
mrb_int len;
char buf[1];
} *s;
mrb_int i;
} value;
enum irep_pool_type type;
} *pool;
mrb_sym *syms; mrb_sym *syms;
struct mrb_irep **reps; struct mrb_irep **reps;
......
...@@ -50,6 +50,7 @@ char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr); ...@@ -50,6 +50,7 @@ char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr);
char *mrb_string_value_ptr(mrb_state *mrb, mrb_value ptr); char *mrb_string_value_ptr(mrb_state *mrb, mrb_value ptr);
int mrb_str_offset(mrb_state *mrb, mrb_value str, int pos); int mrb_str_offset(mrb_state *mrb, mrb_value str, int pos);
mrb_value mrb_str_dup(mrb_state *mrb, mrb_value str); mrb_value mrb_str_dup(mrb_state *mrb, mrb_value str);
mrb_value mrb_str_dup_static(mrb_state *mrb, mrb_value str);
mrb_value mrb_str_intern(mrb_state *mrb, mrb_value self); mrb_value mrb_str_intern(mrb_state *mrb, mrb_value self);
mrb_value mrb_str_cat_cstr(mrb_state *, mrb_value, const char *); mrb_value mrb_str_cat_cstr(mrb_state *, mrb_value, const char *);
mrb_value mrb_str_to_inum(mrb_state *mrb, mrb_value str, int base, int badcheck); mrb_value mrb_str_to_inum(mrb_state *mrb, mrb_value str, int base, int badcheck);
......
...@@ -316,8 +316,8 @@ genop_peep(codegen_scope *s, mrb_code i, int val) ...@@ -316,8 +316,8 @@ genop_peep(codegen_scope *s, mrb_code i, int val)
if (c0 == OP_STRING) { if (c0 == OP_STRING) {
int i = GETARG_Bx(i0); int i = GETARG_Bx(i0);
if (s->irep->pool[i].type == IREP_TT_STRING && if (mrb_type(s->irep->pool[i]) == MRB_TT_STRING &&
s->irep->pool[i].value.s->len == 0) { RSTRING_LEN(s->irep->pool[i]) == 0) {
s->pc--; s->pc--;
return; return;
} }
...@@ -397,7 +397,7 @@ static inline int ...@@ -397,7 +397,7 @@ static inline int
new_lit(codegen_scope *s, mrb_value val) new_lit(codegen_scope *s, mrb_value val)
{ {
size_t i; size_t i;
struct irep_pool *pv; mrb_value *pv;
switch (mrb_type(val)) { switch (mrb_type(val)) {
case MRB_TT_STRING: case MRB_TT_STRING:
...@@ -405,24 +405,24 @@ new_lit(codegen_scope *s, mrb_value val) ...@@ -405,24 +405,24 @@ new_lit(codegen_scope *s, mrb_value val)
mrb_int len; mrb_int len;
pv = &s->irep->pool[i]; pv = &s->irep->pool[i];
if (pv->type != IREP_TT_STRING) continue; if (mrb_type(*pv) != MRB_TT_STRING) continue;
if ((len = pv->value.s->len) != RSTRING_LEN(val)) continue; if ((len = RSTRING_LEN(*pv)) != RSTRING_LEN(val)) continue;
if (memcmp(pv->value.s->buf, RSTRING_PTR(val), len) == 0) if (memcmp(RSTRING_PTR(*pv), RSTRING_PTR(val), len) == 0)
return i; return i;
} }
break; break;
case MRB_TT_FLOAT: case MRB_TT_FLOAT:
for (i=0; i<s->irep->plen; i++) { for (i=0; i<s->irep->plen; i++) {
pv = &s->irep->pool[i]; pv = &s->irep->pool[i];
if (pv->type != IREP_TT_FLOAT) continue; if (mrb_type(*pv) != MRB_TT_FLOAT) continue;
if (pv->value.f == mrb_float(val)) return i; if (mrb_float(*pv) == mrb_float(val)) return i;
} }
break; break;
case MRB_TT_FIXNUM: case MRB_TT_FIXNUM:
for (i=0; i<s->irep->plen; i++) { for (i=0; i<s->irep->plen; i++) {
pv = &s->irep->pool[i]; pv = &s->irep->pool[i];
if (pv->type != IREP_TT_FIXNUM) continue; if (mrb_type(*pv) != MRB_TT_FIXNUM) continue;
if (pv->value.i == mrb_fixnum(val)) return i; if (mrb_fixnum(*pv) == mrb_fixnum(val)) return i;
} }
break; break;
default: default:
...@@ -432,7 +432,7 @@ new_lit(codegen_scope *s, mrb_value val) ...@@ -432,7 +432,7 @@ new_lit(codegen_scope *s, mrb_value val)
if (s->irep->plen == s->pcapa) { if (s->irep->plen == s->pcapa) {
s->pcapa *= 2; 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]; pv = &s->irep->pool[s->irep->plen];
...@@ -440,19 +440,14 @@ new_lit(codegen_scope *s, mrb_value val) ...@@ -440,19 +440,14 @@ new_lit(codegen_scope *s, mrb_value val)
switch (mrb_type(val)) { switch (mrb_type(val)) {
case MRB_TT_STRING: case MRB_TT_STRING:
pv->type = IREP_TT_STRING; *pv = mrb_str_dup_static(s->mrb, val);
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));
break; break;
case MRB_TT_FLOAT: case MRB_TT_FLOAT:
pv->type = IREP_TT_FLOAT;
pv->value.f = mrb_float(val);
break;
case MRB_TT_FIXNUM: case MRB_TT_FIXNUM:
pv->type = IREP_TT_FIXNUM; *pv = val;
pv->value.i = mrb_fixnum(val);
break; break;
default: default:
/* should not happen */ /* should not happen */
break; break;
...@@ -2414,7 +2409,7 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv) ...@@ -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->iseq = (mrb_code*)mrb_malloc(mrb, sizeof(mrb_code)*p->icapa);
p->pcapa = 32; 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->irep->plen = 0;
p->scapa = 256; p->scapa = 256;
...@@ -2467,7 +2462,7 @@ scope_finish(codegen_scope *s) ...@@ -2467,7 +2462,7 @@ scope_finish(codegen_scope *s)
irep->lines = 0; 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->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->reps = (mrb_irep**)codegen_realloc(s, irep->reps, sizeof(mrb_irep*)*irep->rlen);
if (s->filename) { if (s->filename) {
...@@ -2809,8 +2804,8 @@ codedump(mrb_state *mrb, mrb_irep *irep) ...@@ -2809,8 +2804,8 @@ codedump(mrb_state *mrb, mrb_irep *irep)
break; break;
case OP_STRING: case OP_STRING:
{ {
struct irep_pool *pv = &irep->pool[GETARG_Bx(c)]; mrb_value v = 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 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)); printf("OP_STRING\tR%d\t%s\n", GETARG_A(c), RSTRING_PTR(s));
} }
break; break;
......
...@@ -79,19 +79,19 @@ get_pool_block_size(mrb_state *mrb, mrb_irep *irep) ...@@ -79,19 +79,19 @@ get_pool_block_size(mrb_state *mrb, mrb_irep *irep)
for (pool_no = 0; pool_no < irep->plen; pool_no++) { for (pool_no = 0; pool_no < irep->plen; pool_no++) {
int ai = mrb_gc_arena_save(mrb); int ai = mrb_gc_arena_save(mrb);
switch (irep->pool[pool_no].type) { switch (mrb_type(irep->pool[pool_no])) {
case IREP_TT_FIXNUM: case MRB_TT_FIXNUM:
str = mrb_fixnum_to_str(mrb, mrb_fixnum_value(irep->pool[pool_no].value.i), 10); str = mrb_fixnum_to_str(mrb, irep->pool[pool_no], 10);
size += RSTRING_LEN(str); size += RSTRING_LEN(str);
break; break;
case IREP_TT_FLOAT: case MRB_TT_FLOAT:
len = mrb_float_to_str(buf, irep->pool[pool_no].value.f); len = mrb_float_to_str(buf, mrb_float(irep->pool[pool_no]));
size += len; size += len;
break; break;
case IREP_TT_STRING: case MRB_TT_STRING:
size += irep->pool[pool_no].value.s->len; size += RSTRING_LEN(irep->pool[pool_no]);
break; break;
default: default:
...@@ -118,23 +118,23 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) ...@@ -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++) { for (pool_no = 0; pool_no < irep->plen; pool_no++) {
int ai = mrb_gc_arena_save(mrb); 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) { switch (mrb_type(irep->pool[pool_no])) {
case IREP_TT_FIXNUM: case MRB_TT_FIXNUM:
str = mrb_fixnum_to_str(mrb, mrb_fixnum_value(irep->pool[pool_no].value.i), 10); str = mrb_fixnum_to_str(mrb, irep->pool[pool_no], 10);
char_ptr = RSTRING_PTR(str); char_ptr = RSTRING_PTR(str);
len = RSTRING_LEN(str); len = RSTRING_LEN(str);
break; break;
case IREP_TT_FLOAT: case MRB_TT_FLOAT:
len = mrb_float_to_str(char_buf, irep->pool[pool_no].value.f); len = mrb_float_to_str(char_buf, mrb_float(irep->pool[pool_no]));
char_ptr = &char_buf[0]; char_ptr = &char_buf[0];
break; break;
case IREP_TT_STRING: case MRB_TT_STRING:
char_ptr = irep->pool[pool_no].value.s->buf; char_ptr = RSTRING_PTR(irep->pool[pool_no]);
len = irep->pool[pool_no].value.s->len; len = RSTRING_LEN(irep->pool[pool_no]);
break; break;
default: default:
......
...@@ -87,7 +87,7 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, uint32_t *len) ...@@ -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)) { if (SIZE_ERROR_MUL(sizeof(mrb_value), plen)) {
return NULL; 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) { if (irep->pool == NULL) {
return NULL; return NULL;
} }
...@@ -100,38 +100,22 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, uint32_t *len) ...@@ -100,38 +100,22 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, uint32_t *len)
src += sizeof(uint16_t); src += sizeof(uint16_t);
s = mrb_str_new(mrb, (char *)src, pool_data_len); s = mrb_str_new(mrb, (char *)src, pool_data_len);
src += pool_data_len; src += pool_data_len;
irep->pool[i].type = tt;
switch (tt) { //pool data switch (tt) { //pool data
case IREP_TT_FIXNUM: case MRB_TT_FIXNUM:
{ irep->pool[i] = mrb_str_to_inum(mrb, s, 10, FALSE);
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;
}
}
break; break;
case IREP_TT_FLOAT: case MRB_TT_FLOAT:
irep->pool[i].value.f = mrb_str_to_dbl(mrb, s, FALSE); irep->pool[i] = mrb_float_value(mrb, mrb_str_to_dbl(mrb, s, FALSE));
break; break;
case IREP_TT_STRING: case MRB_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] = mrb_str_dup_static(mrb, s);
irep->pool[i].value.s->len = pool_data_len;
memcpy(irep->pool[i].value.s->buf, src-pool_data_len, pool_data_len);
break; break;
default: default:
/* should not happen */ /* should not happen */
irep->pool[i] = mrb_nil_value();
break; break;
} }
irep->plen++; irep->plen++;
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "mruby/irep.h" #include "mruby/irep.h"
#include "mruby/variable.h" #include "mruby/variable.h"
#include "mruby/debug.h" #include "mruby/debug.h"
#include "mruby/string.h"
void mrb_init_heap(mrb_state*); void mrb_init_heap(mrb_state*);
void mrb_init_core(mrb_state*); void mrb_init_core(mrb_state*);
...@@ -129,8 +130,8 @@ mrb_irep_free(mrb_state *mrb, mrb_irep *irep) ...@@ -129,8 +130,8 @@ mrb_irep_free(mrb_state *mrb, mrb_irep *irep)
if (!(irep->flags & MRB_ISEQ_NO_FREE)) if (!(irep->flags & MRB_ISEQ_NO_FREE))
mrb_free(mrb, irep->iseq); mrb_free(mrb, irep->iseq);
for (i=0; i<irep->plen; i++) { for (i=0; i<irep->plen; i++) {
if (irep->pool[i].type == IREP_TT_STRING) if (mrb_type(irep->pool[i]) == MRB_TT_STRING)
mrb_free(mrb, irep->pool[i].value.s); mrb_free(mrb, mrb_obj_ptr(irep->pool[i]));
} }
mrb_free(mrb, irep->pool); mrb_free(mrb, irep->pool);
mrb_free(mrb, irep->syms); mrb_free(mrb, irep->syms);
......
...@@ -734,6 +734,28 @@ mrb_str_dup(mrb_state *mrb, mrb_value str) ...@@ -734,6 +734,28 @@ mrb_str_dup(mrb_state *mrb, mrb_value str)
return mrb_str_new(mrb, s->ptr, s->len); 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 static mrb_value
mrb_str_aref(mrb_state *mrb, mrb_value str, mrb_value indx) 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 ...@@ -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_assert(mrb_proc_cfunc_p(proc)) */
mrb_irep *irep = proc->body.irep; mrb_irep *irep = proc->body.irep;
mrb_code *pc = irep->iseq; mrb_code *pc = irep->iseq;
struct irep_pool *pool = irep->pool; mrb_value *pool = irep->pool;
mrb_sym *syms = irep->syms; mrb_sym *syms = irep->syms;
mrb_value *regs = NULL; mrb_value *regs = NULL;
mrb_code i; mrb_code i;
...@@ -618,10 +618,7 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int ...@@ -618,10 +618,7 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int
CASE(OP_LOADL) { CASE(OP_LOADL) {
/* A Bx R(A) := Pool(Bx) */ /* A Bx R(A) := Pool(Bx) */
if (pool[GETARG_Bx(i)].type == IREP_TT_FLOAT) regs[GETARG_A(i)] = pool[GETARG_Bx(i)];
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);
NEXT; NEXT;
} }
...@@ -1939,7 +1936,7 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int ...@@ -1939,7 +1936,7 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int
CASE(OP_STRING) { CASE(OP_STRING) {
/* A Bx R(A) := str_new(Lit(Bx)) */ /* 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); mrb_gc_arena_restore(mrb, ai);
NEXT; NEXT;
} }
...@@ -2134,7 +2131,7 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int ...@@ -2134,7 +2131,7 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int
CASE(OP_ERR) { CASE(OP_ERR) {
/* Bx raise RuntimeError with message Lit(Bx) */ /* 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; mrb_value exc;
if (GETARG_A(i) == 0) { if (GETARG_A(i) == 0) {
......
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