Commit 6b67f083 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #1104 from monaka/pr-simplify-dump-load

Simplify dump/load
parents cb946da7 24eb1201
...@@ -83,13 +83,11 @@ ...@@ -83,13 +83,11 @@
# define PRIoMRB_INT PRIo64 # define PRIoMRB_INT PRIo64
# define PRIxMRB_INT PRIx64 # define PRIxMRB_INT PRIx64
# define PRIXMRB_INT PRIX64 # define PRIXMRB_INT PRIX64
# define str_to_mrb_int(buf) strtoll(buf, NULL, 10)
# endif # endif
#elif defined(MRB_INT16) #elif defined(MRB_INT16)
typedef int16_t mrb_int; typedef int16_t mrb_int;
# define MRB_INT_MIN INT16_MIN # define MRB_INT_MIN INT16_MIN
# define MRB_INT_MAX INT16_MAX # define MRB_INT_MAX INT16_MAX
# define str_to_mrb_int(buf) strtol(buf, NULL, 10)
#else #else
typedef int32_t mrb_int; typedef int32_t mrb_int;
# define MRB_INT_MIN INT32_MIN # define MRB_INT_MIN INT32_MIN
...@@ -99,7 +97,6 @@ ...@@ -99,7 +97,6 @@
# define PRIoMRB_INT PRIo32 # define PRIoMRB_INT PRIo32
# define PRIxMRB_INT PRIx32 # define PRIxMRB_INT PRIx32
# define PRIXMRB_INT PRIX32 # define PRIXMRB_INT PRIX32
# define str_to_mrb_int(buf) strtol(buf, NULL, 10)
#endif #endif
typedef short mrb_sym; typedef short mrb_sym;
......
...@@ -23,7 +23,7 @@ typedef struct mrb_irep { ...@@ -23,7 +23,7 @@ typedef struct mrb_irep {
/* debug info */ /* debug info */
const char *filename; const char *filename;
short *lines; uint16_t *lines;
size_t ilen, plen, slen; size_t ilen, plen, slen;
} mrb_irep; } mrb_irep;
......
...@@ -2401,7 +2401,7 @@ scope_finish(codegen_scope *s) ...@@ -2401,7 +2401,7 @@ scope_finish(codegen_scope *s)
irep->iseq = (mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->pc); irep->iseq = (mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->pc);
irep->ilen = s->pc; irep->ilen = s->pc;
if (s->lines) { if (s->lines) {
irep->lines = (short *)codegen_realloc(s, s->lines, sizeof(short)*s->pc); irep->lines = (uint16_t *)codegen_realloc(s, s->lines, sizeof(uint16_t)*s->pc);
} }
else { else {
irep->lines = 0; irep->lines = 0;
......
...@@ -107,19 +107,12 @@ get_pool_block_size(mrb_state *mrb, mrb_irep *irep) ...@@ -107,19 +107,12 @@ get_pool_block_size(mrb_state *mrb, mrb_irep *irep)
static int static int
write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
{ {
int result;
size_t pool_no; size_t pool_no;
uint8_t *cur = buf; uint8_t *cur = buf;
size_t buf_size, len; size_t len;
mrb_value str; mrb_value str;
char *char_buf = NULL; const char *char_ptr;
char char_buf[30];
buf_size = MRB_DUMP_DEFAULT_STR_LEN;
char_buf = (char *)mrb_malloc(mrb, buf_size);
if (char_buf == NULL) {
result = MRB_DUMP_GENERAL_FAILURE;
goto error_exit;
}
cur += uint32_to_bin(irep->plen, cur); /* number of pool */ cur += uint32_to_bin(irep->plen, cur); /* number of pool */
...@@ -127,51 +120,37 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) ...@@ -127,51 +120,37 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
int ai = mrb_gc_arena_save(mrb); int ai = mrb_gc_arena_save(mrb);
cur += uint8_to_bin(mrb_type(irep->pool[pool_no]), cur); /* data type */ cur += uint8_to_bin(mrb_type(irep->pool[pool_no]), cur); /* data type */
memset(char_buf, 0, buf_size);
switch (mrb_type(irep->pool[pool_no])) { switch (mrb_type(irep->pool[pool_no])) {
case MRB_TT_FIXNUM: case MRB_TT_FIXNUM:
str = mrb_fix2str(mrb, irep->pool[pool_no], 10); str = mrb_fix2str(mrb, irep->pool[pool_no], 10);
memcpy(char_buf, RSTRING_PTR(str), RSTRING_LEN(str)); char_ptr = RSTRING_PTR(str);
len = RSTRING_LEN(str); len = RSTRING_LEN(str);
break; break;
case MRB_TT_FLOAT: case MRB_TT_FLOAT:
len = mrb_float_to_str(char_buf, mrb_float(irep->pool[pool_no])); len = mrb_float_to_str(char_buf, mrb_float(irep->pool[pool_no]));
char_ptr = &char_buf[0];
break; break;
case MRB_TT_STRING: case MRB_TT_STRING:
str = irep->pool[pool_no]; str = irep->pool[pool_no];
char_ptr = RSTRING_PTR(str);
len = RSTRING_LEN(str); len = RSTRING_LEN(str);
if (len > buf_size - 1) {
buf_size = len + 1;
char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size);
if (char_buf == NULL) {
mrb_gc_arena_restore(mrb, ai);
result = MRB_DUMP_GENERAL_FAILURE;
goto error_exit;
}
memset(char_buf, 0, buf_size);
}
memcpy(char_buf, RSTRING_PTR(str), RSTRING_LEN(str));
break; break;
default: default:
len = 0;
continue; continue;
} }
cur += uint16_to_bin(len, cur); /* data length */ cur += uint16_to_bin(len, cur); /* data length */
memcpy(cur, char_buf, len); memcpy(cur, char_ptr, len);
cur += len; cur += len;
mrb_gc_arena_restore(mrb, ai); mrb_gc_arena_restore(mrb, ai);
} }
result = (int)(cur - buf);
error_exit: return (int)(cur - buf);
mrb_free(mrb, char_buf);
return result;
} }
...@@ -197,21 +176,10 @@ get_syms_block_size(mrb_state *mrb, mrb_irep *irep) ...@@ -197,21 +176,10 @@ get_syms_block_size(mrb_state *mrb, mrb_irep *irep)
static int static int
write_syms_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) write_syms_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
{ {
int result;
size_t sym_no; size_t sym_no;
size_t buf_size;
uint8_t *cur = buf; uint8_t *cur = buf;
uint16_t nlen;
char *char_buf = NULL;
const char *name; const char *name;
buf_size = MRB_DUMP_DEFAULT_STR_LEN;
char_buf = (char *)mrb_malloc(mrb, buf_size);
if (char_buf == NULL) {
result = MRB_DUMP_GENERAL_FAILURE;
goto error_exit;
}
cur += uint32_to_bin(irep->slen, cur); /* number of symbol */ cur += uint32_to_bin(irep->slen, cur); /* number of symbol */
for (sym_no = 0; sym_no < irep->slen; sym_no++) { for (sym_no = 0; sym_no < irep->slen; sym_no++) {
...@@ -219,32 +187,20 @@ write_syms_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) ...@@ -219,32 +187,20 @@ write_syms_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
size_t len; size_t len;
name = mrb_sym2name_len(mrb, irep->syms[sym_no], &len); name = mrb_sym2name_len(mrb, irep->syms[sym_no], &len);
if (len > UINT16_MAX) goto error_exit; if (len > UINT16_MAX) {
nlen = (uint16_t)len; return MRB_DUMP_GENERAL_FAILURE;
if (nlen > buf_size - 1) {
buf_size = nlen + 1;
char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size);
if (char_buf == NULL) {
result = MRB_DUMP_GENERAL_FAILURE;
goto error_exit;
}
} }
memset(char_buf, 0, buf_size);
memcpy(char_buf, name, len);
cur += uint16_to_bin(nlen, cur); /* length of symbol name */ cur += uint16_to_bin((uint16_t)len, cur); /* length of symbol name */
memcpy(cur, char_buf, nlen); /* symbol name */ memcpy(cur, name, len); /* symbol name */
cur += nlen; cur += (uint16_t)len;
} }
else { else {
cur += uint16_to_bin(MRB_DUMP_NULL_SYM_LEN, cur); /* length of symbol name */ cur += uint16_to_bin(MRB_DUMP_NULL_SYM_LEN, cur); /* length of symbol name */
} }
} }
result = (int)(cur - buf);
error_exit: return (int)(cur - buf);
mrb_free(mrb, char_buf);
return result;
} }
......
...@@ -17,22 +17,16 @@ ...@@ -17,22 +17,16 @@
#include "mruby/proc.h" #include "mruby/proc.h"
#include "mruby/string.h" #include "mruby/string.h"
#ifdef ENABLE_STDIO
typedef struct _RiteFILE
{
FILE* fp;
unsigned char buf[256];
int cnt;
int readlen;
} RiteFILE;
#endif
#ifndef _WIN32 #ifndef _WIN32
# if SIZE_MAX < UINT32_MAX # if SIZE_MAX < UINT32_MAX
# error "It can't be run this code on this environment (SIZE_MAX < UINT32_MAX)" # error "It can't be run this code on this environment (SIZE_MAX < UINT32_MAX)"
# endif # endif
#endif #endif
#if CHAR_BIT != 8
# error This code assumes CHAR_BIT == 8
#endif
static size_t static size_t
offset_crc_body() offset_crc_body()
{ {
...@@ -45,21 +39,12 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) ...@@ -45,21 +39,12 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len)
{ {
int ret; int ret;
size_t i; size_t i;
char *char_buf;
const uint8_t *src = bin; const uint8_t *src = bin;
uint16_t tt, pool_data_len, snl, buf_size = MRB_DUMP_DEFAULT_STR_LEN; uint16_t tt, pool_data_len, snl;
mrb_int fix_num;
mrb_float f;
size_t plen; size_t plen;
int ai = mrb_gc_arena_save(mrb); int ai = mrb_gc_arena_save(mrb);
mrb_irep *irep = mrb_add_irep(mrb); mrb_irep *irep = mrb_add_irep(mrb);
char_buf = (char *)mrb_malloc(mrb, buf_size);
if (char_buf == NULL) {
ret = MRB_DUMP_GENERAL_FAILURE;
goto error_exit;
}
// skip record size // skip record size
src += sizeof(uint32_t); src += sizeof(uint32_t);
...@@ -98,34 +83,23 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) ...@@ -98,34 +83,23 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len)
} }
for (i = 0; i < plen; i++) { for (i = 0; i < plen; i++) {
mrb_value s;
tt = *src++; //pool TT tt = *src++; //pool TT
pool_data_len = bin_to_uint16(src); //pool data length pool_data_len = bin_to_uint16(src); //pool data length
src += sizeof(uint16_t); src += sizeof(uint16_t);
if (pool_data_len > buf_size - 1) { s = mrb_str_new(mrb, (char *)src, pool_data_len);
mrb_free(mrb, char_buf);
buf_size = pool_data_len + 1;
char_buf = (char *)mrb_malloc(mrb, buf_size);
if (char_buf == NULL) {
ret = MRB_DUMP_GENERAL_FAILURE;
goto error_exit;
}
}
memcpy(char_buf, src, pool_data_len);
src += pool_data_len; src += pool_data_len;
char_buf[pool_data_len] = '\0';
switch (tt) { //pool data switch (tt) { //pool data
case MRB_TT_FIXNUM: case MRB_TT_FIXNUM:
fix_num = str_to_mrb_int(char_buf); irep->pool[i] = mrb_str_to_inum(mrb, s, 10, FALSE);
irep->pool[i] = mrb_fixnum_value(fix_num);
break; break;
case MRB_TT_FLOAT: case MRB_TT_FLOAT:
f = str_to_mrb_float(char_buf); irep->pool[i] = mrb_float_value(mrb_str_to_dbl(mrb, s, FALSE));
irep->pool[i] = mrb_float_value(f);
break; break;
case MRB_TT_STRING: case MRB_TT_STRING:
irep->pool[i] = mrb_str_new(mrb, char_buf, pool_data_len); irep->pool[i] = s;
break; break;
default: default:
...@@ -147,10 +121,6 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) ...@@ -147,10 +121,6 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len)
goto error_exit; goto error_exit;
} }
for (i = 0; i < irep->slen; i++) {
static const mrb_sym mrb_sym_zero = { 0 };
*irep->syms = mrb_sym_zero;
}
for (i = 0; i < irep->slen; i++) { for (i = 0; i < irep->slen; i++) {
snl = bin_to_uint16(src); //symbol name length snl = bin_to_uint16(src); //symbol name length
src += sizeof(uint16_t); src += sizeof(uint16_t);
...@@ -160,26 +130,16 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) ...@@ -160,26 +130,16 @@ read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len)
continue; continue;
} }
if (snl > buf_size - 1) { irep->syms[i] = mrb_intern2(mrb, (char *)src, snl);
mrb_free(mrb, char_buf);
buf_size = snl + 1;
char_buf = (char *)mrb_malloc(mrb, buf_size);
if (char_buf == NULL) {
ret = MRB_DUMP_GENERAL_FAILURE;
goto error_exit;
}
}
memcpy(char_buf, src, snl); //symbol name
src += snl; src += snl;
char_buf[snl] = '\0';
irep->syms[i] = mrb_intern2(mrb, char_buf, snl); mrb_gc_arena_restore(mrb, ai);
} }
} }
*len = src - bin; *len = src - bin;
ret = MRB_DUMP_OK; ret = MRB_DUMP_OK;
error_exit: error_exit:
mrb_free(mrb, char_buf);
return ret; return ret;
} }
...@@ -236,7 +196,7 @@ read_rite_lineno_record(mrb_state *mrb, const uint8_t *bin, size_t irepno, uint3 ...@@ -236,7 +196,7 @@ read_rite_lineno_record(mrb_state *mrb, const uint8_t *bin, size_t irepno, uint3
int ret; int ret;
size_t i, fname_len, niseq; size_t i, fname_len, niseq;
char *fname; char *fname;
short *lines; uint16_t *lines;
ret = MRB_DUMP_OK; ret = MRB_DUMP_OK;
*len = 0; *len = 0;
...@@ -259,7 +219,7 @@ read_rite_lineno_record(mrb_state *mrb, const uint8_t *bin, size_t irepno, uint3 ...@@ -259,7 +219,7 @@ read_rite_lineno_record(mrb_state *mrb, const uint8_t *bin, size_t irepno, uint3
bin += sizeof(uint32_t); // niseq bin += sizeof(uint32_t); // niseq
*len += sizeof(uint32_t); *len += sizeof(uint32_t);
lines = (short *)mrb_malloc(mrb, niseq * sizeof(short)); lines = (uint16_t *)mrb_malloc(mrb, niseq * sizeof(uint16_t));
for (i = 0; i < niseq; i++) { for (i = 0; i < niseq; i++) {
lines[i] = bin_to_uint16(bin); lines[i] = bin_to_uint16(bin);
bin += sizeof(uint16_t); // niseq bin += sizeof(uint16_t); // niseq
......
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