Commit 4c7f9897 authored by take_cheeze's avatar take_cheeze

Support local variables information dumping.

parent 0bdc87d2
...@@ -52,6 +52,7 @@ mrb_irep *mrb_read_irep(mrb_state*, const uint8_t*); ...@@ -52,6 +52,7 @@ mrb_irep *mrb_read_irep(mrb_state*, const uint8_t*);
#define RITE_SECTION_IREP_IDENTIFIER "IREP" #define RITE_SECTION_IREP_IDENTIFIER "IREP"
#define RITE_SECTION_LINENO_IDENTIFIER "LINE" #define RITE_SECTION_LINENO_IDENTIFIER "LINE"
#define RITE_SECTION_DEBUG_IDENTIFIER "DBG\0" #define RITE_SECTION_DEBUG_IDENTIFIER "DBG\0"
#define RITE_SECTION_LV_IDENTIFIER "LOCV"
#define MRB_DUMP_DEFAULT_STR_LEN 128 #define MRB_DUMP_DEFAULT_STR_LEN 128
...@@ -88,6 +89,10 @@ struct rite_section_debug_header { ...@@ -88,6 +89,10 @@ struct rite_section_debug_header {
RITE_SECTION_HEADER; RITE_SECTION_HEADER;
}; };
struct rite_section_lv_header {
RITE_SECTION_HEADER;
};
struct rite_binary_footer { struct rite_binary_footer {
RITE_SECTION_HEADER; RITE_SECTION_HEADER;
}; };
......
...@@ -41,7 +41,7 @@ typedef struct mrb_irep { ...@@ -41,7 +41,7 @@ typedef struct mrb_irep {
uint16_t *lines; uint16_t *lines;
struct mrb_irep_debug_info* debug_info; struct mrb_irep_debug_info* debug_info;
size_t ilen, plen, slen, rlen, refcnt; size_t ilen, plen, slen, rlen, lv_len, refcnt;
} mrb_irep; } mrb_irep;
#define MRB_ISEQ_NO_FREE 1 #define MRB_ISEQ_NO_FREE 1
......
...@@ -2496,11 +2496,16 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv) ...@@ -2496,11 +2496,16 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv)
p->lv = lv; p->lv = lv;
p->sp += node_len(lv)+1; /* add self */ p->sp += node_len(lv)+1; /* add self */
p->nlocals = p->sp; p->nlocals = p->sp;
p->irep->lv_len = 0;
if (lv) { if (lv) {
node *n = lv; node *n = lv;
size_t i = 0; size_t i = 0;
p->irep->lv = (struct mrb_locals*)mrb_malloc(mrb, sizeof(struct mrb_locals)*p->nlocals); 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);
for (i=0, n=lv; n; i++,n=n->cdr) { for (i=0, n=lv; n; i++,n=n->cdr) {
p->irep->lv[i].name = lv_name(n); p->irep->lv[i].name = lv_name(n);
p->irep->lv[i].r = lv_idx(p, lv_name(n)); p->irep->lv[i].r = lv_idx(p, lv_name(n));
......
...@@ -500,7 +500,7 @@ get_debug_record_size(mrb_state *mrb, mrb_irep *irep) ...@@ -500,7 +500,7 @@ get_debug_record_size(mrb_state *mrb, mrb_irep *irep)
} }
static int static int
find_filename_index(const mrb_sym *ary, uint16_t ary_len, mrb_sym s) find_filename_index(const mrb_sym *ary, int ary_len, mrb_sym s)
{ {
int i; int i;
...@@ -692,6 +692,143 @@ write_section_debug(mrb_state *mrb, mrb_irep *irep, uint8_t *cur) ...@@ -692,6 +692,143 @@ write_section_debug(mrb_state *mrb, mrb_irep *irep, uint8_t *cur)
return MRB_DUMP_OK; return MRB_DUMP_OK;
} }
static void
create_lv_sym_table(mrb_state *mrb, mrb_irep *irep, mrb_sym **syms, uint32_t *syms_len)
{
size_t i;
if (*syms == NULL) {
*syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym) * 1);
}
for (i = 0; i < irep->lv_len; ++i) {
mrb_sym const name = irep->lv[i].name;
if (find_filename_index(*syms, *syms_len, name) != -1) continue;
++(*syms_len);
*syms = (mrb_sym*)mrb_realloc(mrb, *syms, sizeof(mrb_sym) * (*syms_len));
(*syms)[*syms_len - 1] = name;
}
for (i = 0; i < irep->rlen; ++i) {
create_lv_sym_table(mrb, irep->reps[i], syms, syms_len);
}
}
static int
write_lv_sym_table(mrb_state *mrb, uint8_t **start, mrb_sym const *syms, uint32_t syms_len)
{
uint8_t *cur = *start;
uint32_t i;
const char *str;
mrb_int str_len;
cur += uint32_to_bin(syms_len, cur);
for (i = 0; i < syms_len; ++i) {
str = mrb_sym2name_len(mrb, syms[i], &str_len);
cur += uint16_to_bin(str_len, cur);
memcpy(cur, str, str_len);
cur += str_len;
}
*start = cur;
return MRB_DUMP_OK;
}
static int
write_lv_record(mrb_state *mrb, 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) {
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 */
cur += uint16_to_bin(sym_idx, cur);
cur += uint32_to_bin(irep->lv[i].r, cur);
}
for (i = 0; i < irep->rlen; ++i) {
write_lv_record(mrb, irep->reps[i], &cur, syms, syms_len);
}
*start = cur;
return MRB_DUMP_OK;
}
static size_t
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;
for (i = 0; i < irep->rlen; ++i) {
ret += get_lv_record_size(mrb, irep->reps[i]);
}
return ret;
}
static size_t
get_lv_section_size(mrb_state *mrb, mrb_irep *irep, mrb_sym const *syms, uint32_t syms_len)
{
size_t ret = 0, i;
ret += sizeof(uint32_t); /* syms_len */
ret += sizeof(uint16_t) * syms_len; /* symbol name lengths */
for (i = 0; i < syms_len; ++i) {
mrb_int str_len;
mrb_sym2name_len(mrb, syms[i], &str_len);
ret += str_len;
}
ret += get_lv_record_size(mrb, irep);
return ret;
}
static int
write_section_lv(mrb_state *mrb, 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;
ptrdiff_t diff;
int result = MRB_DUMP_OK;
if (mrb == NULL || cur == NULL) {
return MRB_DUMP_INVALID_ARGUMENT;
}
header = (struct rite_section_lv_header*)cur;
cur += sizeof(struct rite_section_lv_header);
result = write_lv_sym_table(mrb, &cur, syms, syms_len);
if (result != MRB_DUMP_OK) {
goto lv_section_exit;
}
result = write_lv_record(mrb, irep, &cur, syms, syms_len);
if (result != MRB_DUMP_OK) {
goto lv_section_exit;
}
memcpy(header->section_identify, RITE_SECTION_LV_IDENTIFIER, sizeof(header->section_identify));
diff = cur - start;
mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
uint32_to_bin(diff, header->section_size);
lv_section_exit:
return result;
}
static int static int
write_rite_binary_header(mrb_state *mrb, size_t binary_size, uint8_t *bin) write_rite_binary_header(mrb_state *mrb, size_t binary_size, uint8_t *bin)
{ {
...@@ -730,9 +867,10 @@ dump_irep(mrb_state *mrb, mrb_irep *irep, int debug_info, uint8_t **bin, size_t ...@@ -730,9 +867,10 @@ dump_irep(mrb_state *mrb, mrb_irep *irep, int debug_info, uint8_t **bin, size_t
{ {
int result = MRB_DUMP_GENERAL_FAILURE; int result = MRB_DUMP_GENERAL_FAILURE;
size_t section_irep_size; size_t section_irep_size;
size_t section_lineno_size = 0; size_t section_lineno_size = 0, section_lv_size = 0;
uint8_t *cur = NULL; 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(irep);
mrb_sym *lv_syms = NULL; uint32_t lv_syms_len = 0;
if (mrb == NULL) { if (mrb == NULL) {
*bin = NULL; *bin = NULL;
...@@ -764,8 +902,12 @@ dump_irep(mrb_state *mrb, mrb_irep *irep, int debug_info, uint8_t **bin, size_t ...@@ -764,8 +902,12 @@ dump_irep(mrb_state *mrb, mrb_irep *irep, int debug_info, uint8_t **bin, size_t
} }
} }
section_lv_size += sizeof(struct rite_section_lv_header);
create_lv_sym_table(mrb, irep, &lv_syms, &lv_syms_len);
section_lv_size += get_lv_section_size(mrb, irep, lv_syms, lv_syms_len);
*bin_size = sizeof(struct rite_binary_header) + *bin_size = sizeof(struct rite_binary_header) +
section_irep_size + section_lineno_size + section_irep_size + section_lineno_size + section_lv_size +
sizeof(struct rite_binary_footer); sizeof(struct rite_binary_footer);
cur = *bin = (uint8_t*)mrb_malloc(mrb, *bin_size); cur = *bin = (uint8_t*)mrb_malloc(mrb, *bin_size);
if (cur == NULL) { if (cur == NULL) {
...@@ -793,6 +935,12 @@ dump_irep(mrb_state *mrb, mrb_irep *irep, int debug_info, uint8_t **bin, size_t ...@@ -793,6 +935,12 @@ dump_irep(mrb_state *mrb, mrb_irep *irep, int debug_info, uint8_t **bin, size_t
cur += section_lineno_size; cur += section_lineno_size;
} }
result = write_section_lv(mrb, irep, cur, lv_syms, lv_syms_len);
if (result != MRB_DUMP_OK) {
goto error_exit;
}
cur += section_lv_size;
write_footer(mrb, cur); write_footer(mrb, cur);
write_rite_binary_header(mrb, *bin_size, *bin); write_rite_binary_header(mrb, *bin_size, *bin);
...@@ -801,6 +949,9 @@ error_exit: ...@@ -801,6 +949,9 @@ error_exit:
mrb_free(mrb, *bin); mrb_free(mrb, *bin);
*bin = NULL; *bin = NULL;
} }
if (lv_syms) {
mrb_free(mrb, lv_syms);
}
return result; return result;
} }
......
...@@ -401,6 +401,88 @@ debug_exit: ...@@ -401,6 +401,88 @@ debug_exit:
return result; return result;
} }
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;
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);
for (i = 0; i < irep->lv_len; ++i) {
uint16_t const sym_idx = bin_to_uint16(bin);
bin += sizeof(uint16_t);
if (sym_idx >= syms_len) {
return MRB_DUMP_GENERAL_FAILURE;
}
irep->lv[i].name = syms[sym_idx];
irep->lv[i].r = bin_to_uint32(bin);
bin += sizeof(uint32_t);
}
for (i = 0; i < irep->rlen; ++i) {
size_t len;
int ret;
ret = read_lv_record(mrb, bin, irep->reps[i], &len, syms, syms_len);
if (ret != MRB_DUMP_OK) return ret;
bin += len;
}
diff = bin - start;
mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
*record_len = (size_t)diff;
return MRB_DUMP_OK;
}
static int
read_section_lv(mrb_state *mrb, const uint8_t *start, mrb_irep *irep, mrb_bool alloc)
{
const uint8_t *bin;
ptrdiff_t diff;
struct rite_section_lv_header const *header;
uint32_t i;
size_t len = 0;
int result;
uint32_t syms_len;
mrb_sym *syms;
mrb_sym (*intern_func)(mrb_state*, const char*, size_t) = alloc? mrb_intern : mrb_intern_static;
bin = start;
header = (struct rite_section_lv_header const*)bin;
bin += sizeof(struct rite_section_lv_header);
syms_len = bin_to_uint32(bin);
bin += sizeof(uint32_t);
syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym) * (size_t)syms_len);
for (i = 0; i < syms_len; ++i) {
uint16_t const str_len = bin_to_uint16(bin);
bin += sizeof(uint16_t);
syms[i] = intern_func(mrb, (const char*)bin, str_len);
bin += str_len;
}
result = read_lv_record(mrb, bin, irep, &len, syms, syms_len);
if (result != MRB_DUMP_OK) goto lv_exit;
bin += len;
diff = bin - start;
mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
if ((uint32_t)diff != bin_to_uint32(header->section_size)) {
result = MRB_DUMP_GENERAL_FAILURE;
}
lv_exit:
mrb_free(mrb, syms);
return result;
}
static int static int
read_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)
{ {
...@@ -467,6 +549,13 @@ mrb_read_irep(mrb_state *mrb, const uint8_t *bin) ...@@ -467,6 +549,13 @@ mrb_read_irep(mrb_state *mrb, const uint8_t *bin)
return NULL; return NULL;
} }
} }
else if (memcmp(section_header->section_identify, RITE_SECTION_LV_IDENTIFIER, sizeof(section_header->section_identify)) == 0) {
if (!irep) return NULL;
result = read_section_lv(mrb, bin, irep, FALSE);
if (result < MRB_DUMP_OK) {
return NULL;
}
}
bin += bin_to_uint32(section_header->section_size); bin += bin_to_uint32(section_header->section_size);
} while (memcmp(section_header->section_identify, RITE_BINARY_EOF, sizeof(section_header->section_identify)) != 0); } while (memcmp(section_header->section_identify, RITE_BINARY_EOF, sizeof(section_header->section_identify)) != 0);
...@@ -686,6 +775,21 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp) ...@@ -686,6 +775,21 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp)
} }
if (result < MRB_DUMP_OK) return NULL; if (result < MRB_DUMP_OK) return NULL;
} }
else if (memcmp(section_header.section_identify, RITE_SECTION_LV_IDENTIFIER, sizeof(section_header.section_identify)) == 0) {
if (!irep) return NULL;
else {
uint8_t* const bin = (uint8_t*)mrb_malloc(mrb, section_size);
fseek(fp, fpos, SEEK_SET);
if (fread((char*)bin, section_size, 1, fp) != 1) {
mrb_free(mrb, bin);
return NULL;
}
result = read_section_lv(mrb, bin, irep, TRUE);
mrb_free(mrb, bin);
}
if (result < MRB_DUMP_OK) return NULL;
}
fseek(fp, fpos + section_size, SEEK_SET); fseek(fp, fpos + section_size, SEEK_SET);
} while (memcmp(section_header.section_identify, RITE_BINARY_EOF, sizeof(section_header.section_identify)) != 0); } while (memcmp(section_header.section_identify, RITE_BINARY_EOF, sizeof(section_header.section_identify)) != 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