Commit 5073d14e authored by take_cheeze's avatar take_cheeze

Remove `lv_len` and use `nlocals - 1` instead.

Check that `lv`'s length is always `nlocals - 1`.
parent 7ea82fed
......@@ -41,7 +41,7 @@ typedef struct mrb_irep {
uint16_t *lines;
struct mrb_irep_debug_info* debug_info;
size_t ilen, plen, slen, rlen, lv_len, refcnt;
size_t ilen, plen, slen, rlen, refcnt;
} mrb_irep;
#define MRB_ISEQ_NO_FREE 1
......
......@@ -137,8 +137,8 @@ mrb_local_variables(mrb_state *mrb, mrb_value self)
}
irep = proc->body.irep;
ret = mrb_ary_new_capa(mrb, irep->lv_len);
for (i = 0; i < irep->lv_len; ++i) {
ret = mrb_ary_new_capa(mrb, irep->nlocals - 1);
for (i = 0; i < (irep->nlocals - 1); ++i) {
mrb_ary_push(mrb, ret, mrb_symbol_value(irep->lv[i].name));
}
......
......@@ -2496,20 +2496,16 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv)
p->lv = lv;
p->sp += node_len(lv)+1; /* add self */
p->nlocals = p->sp;
p->irep->lv_len = 0;
if (lv) {
node *n = lv;
size_t i = 0;
for (n=lv; n; n=n->cdr) {
p->irep->lv_len++;
}
p->irep->lv = (struct mrb_locals*)mrb_malloc(mrb, sizeof(struct mrb_locals)*p->irep->lv_len);
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);
p->irep->lv[i].r = lv_idx(p, lv_name(n));
}
mrb_assert(i == (p->nlocals - 1));
}
p->ai = mrb_gc_arena_save(mrb);
......
......@@ -693,7 +693,7 @@ write_section_debug(mrb_state *mrb, mrb_irep *irep, uint8_t *cur)
}
static void
create_lv_sym_table(mrb_state *mrb, mrb_irep *irep, mrb_sym **syms, uint32_t *syms_len)
create_lv_sym_table(mrb_state *mrb, const mrb_irep *irep, mrb_sym **syms, uint32_t *syms_len)
{
size_t i;
......@@ -701,7 +701,7 @@ create_lv_sym_table(mrb_state *mrb, mrb_irep *irep, mrb_sym **syms, uint32_t *sy
*syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym) * 1);
}
for (i = 0; i < irep->lv_len; ++i) {
for (i = 0; i < (irep->nlocals - 1); ++i) {
mrb_sym const name = irep->lv[i].name;
if (find_filename_index(*syms, *syms_len, name) != -1) continue;
......@@ -738,13 +738,12 @@ write_lv_sym_table(mrb_state *mrb, uint8_t **start, mrb_sym const *syms, uint32_
}
static int
write_lv_record(mrb_state *mrb, mrb_irep *irep, uint8_t **start, mrb_sym const *syms, uint32_t syms_len)
write_lv_record(mrb_state *mrb, const mrb_irep *irep, uint8_t **start, mrb_sym const *syms, uint32_t syms_len)
{
uint8_t *cur = *start;
size_t i;
cur += uint16_to_bin(irep->lv_len, cur);
for (i = 0; i < irep->lv_len; ++i) {
for (i = 0; i < (irep->nlocals - 1); ++i) {
int const sym_idx = find_filename_index(syms, syms_len, irep->lv[i].name);
mrb_assert(sym_idx != -1); /* local variable name must be in syms */
......@@ -766,8 +765,7 @@ get_lv_record_size(mrb_state *mrb, mrb_irep *irep)
{
size_t ret = 0, i;
ret += sizeof(uint16_t); /* lv_len */
ret += (sizeof(uint16_t) + sizeof(uint32_t)) * irep->lv_len;
ret += (sizeof(uint16_t) + sizeof(uint32_t)) * (irep->nlocals - 1);
for (i = 0; i < irep->rlen; ++i) {
ret += get_lv_record_size(mrb, irep->reps[i]);
......
......@@ -408,11 +408,9 @@ read_lv_record(mrb_state *mrb, const uint8_t *start, mrb_irep *irep, size_t *rec
size_t i;
ptrdiff_t diff;
irep->lv_len = bin_to_uint16(bin);
bin += sizeof(uint16_t);
irep->lv = (struct mrb_locals*)mrb_malloc(mrb, sizeof(struct mrb_locals) * irep->lv_len);
irep->lv = (struct mrb_locals*)mrb_malloc(mrb, sizeof(struct mrb_locals) * (irep->nlocals - 1));
for (i = 0; i < irep->lv_len; ++i) {
for (i = 0; i < (irep->nlocals - 1); ++i) {
uint16_t const sym_idx = bin_to_uint16(bin);
bin += sizeof(uint16_t);
if (sym_idx >= syms_len) {
......
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