From 3248de83b6754eecc99d9ab6fdef8bb3575fcd04 Mon Sep 17 00:00:00 2001 From: take-cheeze Date: Mon, 29 Oct 2018 19:17:05 +0900 Subject: Reduce instruction size --- src/symbol.c | 26 +++++++++++++++++++++++++ src/vm.c | 62 ++++++++++++++++++++++++++++++++++++++---------------------- 2 files changed, 65 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/symbol.c b/src/symbol.c index 6b4c7200c..f7dbc1be1 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -11,6 +11,7 @@ #include #include #include +#include /* ------------------------------------------------------ */ typedef struct symbol_name { @@ -170,10 +171,35 @@ mrb_free_symtbl(mrb_state *mrb) kh_destroy(n2s, mrb, mrb->name2sym); } +struct reserved_symbol_t { + mrb_reserved_symbol sym; + char const *str; +}; + +static struct reserved_symbol_t reserved_symbols[] = { + { mrb_sym_add, "+" }, + { mrb_sym_sub, "-" }, + { mrb_sym_mul, "*" }, + { mrb_sym_div, "/" }, + { mrb_sym_eq, "==" }, + { mrb_sym_lt, "<" }, + { mrb_sym_le, "<=" }, + { mrb_sym_gt, ">" }, + { mrb_sym_ge, ">=" }, + { mrb_sym_method_missing, "method_missing" }, + { 0, NULL }, +}; + void mrb_init_symtbl(mrb_state *mrb) { + int i; mrb->name2sym = kh_init(n2s, mrb); + + for (i = 0; reserved_symbols[i].sym != 0; ++i) { + mrb_sym s = mrb_intern_static(mrb, reserved_symbols[i].str, strlen(reserved_symbols[i].str)); + mrb_assert(s == reserved_symbols[i].sym); + } } /********************************************************************** diff --git a/src/vm.c b/src/vm.c index 067dd90f7..9792c3fbd 100644 --- a/src/vm.c +++ b/src/vm.c @@ -21,6 +21,7 @@ #include #include "value_array.h" #include +#include #ifdef MRB_DISABLE_STDIO #if defined(__cplusplus) @@ -1009,6 +1010,7 @@ mrb_vm_exec(mrb_state *mrb, struct RProc *proc, mrb_code *pc) uint32_t a; uint16_t b; uint8_t c; + mrb_sym mid; #ifdef DIRECT_THREADED static void *optable[] = { @@ -1386,9 +1388,18 @@ RETRY_TRY_BLOCK: SET_NIL_VALUE(regs[bidx]); goto L_SENDB; }; + L_SEND_CONSTSYM: + { + /* push nil after arguments */ + int bidx = (c == CALL_MAXARGS) ? a+2 : a+c+1; + SET_NIL_VALUE(regs[bidx]); + goto L_SENDB_CONSTSYM; + }; CASE(OP_SENDB, BBB) L_SENDB: + mid = syms[b]; + L_SENDB_CONSTSYM: { int argc = (c == CALL_MAXARGS) ? -1 : c; int bidx = (argc < 0) ? a+2 : a+c+1; @@ -1396,7 +1407,6 @@ RETRY_TRY_BLOCK: struct RClass *cls; mrb_callinfo *ci = mrb->c->ci; mrb_value recv, blk; - mrb_sym mid = syms[b]; mrb_assert(bidx < irep->nregs); @@ -2204,7 +2214,7 @@ RETRY_TRY_BLOCK: v1(regs[a]) = v1(regs[a]) op v2(regs[a+1]);\ } while(0) - CASE(OP_ADD, BB) { + CASE(OP_ADD, B) { /* need to check if op is overridden */ switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) { case TYPES2(MRB_TT_FIXNUM,MRB_TT_FIXNUM): @@ -2259,13 +2269,14 @@ RETRY_TRY_BLOCK: break; default: c = 1; - goto L_SEND; + mid = mrb_sym_add; + goto L_SEND_CONSTSYM; } mrb_gc_arena_restore(mrb, ai); NEXT; } - CASE(OP_SUB, BB) { + CASE(OP_SUB, B) { /* need to check if op is overridden */ switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) { case TYPES2(MRB_TT_FIXNUM,MRB_TT_FIXNUM): @@ -2316,12 +2327,13 @@ RETRY_TRY_BLOCK: #endif default: c = 1; - goto L_SEND; + mid = mrb_sym_sub; + goto L_SEND_CONSTSYM; } NEXT; } - CASE(OP_MUL, BB) { + CASE(OP_MUL, B) { /* need to check if op is overridden */ switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) { case TYPES2(MRB_TT_FIXNUM,MRB_TT_FIXNUM): @@ -2372,12 +2384,13 @@ RETRY_TRY_BLOCK: #endif default: c = 1; - goto L_SEND; + mid = mrb_sym_mul; + goto L_SEND_CONSTSYM; } NEXT; } - CASE(OP_DIV, BB) { + CASE(OP_DIV, B) { #ifndef MRB_WITHOUT_FLOAT double x, y, f; #endif @@ -2411,7 +2424,8 @@ RETRY_TRY_BLOCK: #endif default: c = 1; - goto L_SEND; + mid = mrb_sym_div; + goto L_SEND_CONSTSYM; } #ifndef MRB_WITHOUT_FLOAT @@ -2509,7 +2523,7 @@ RETRY_TRY_BLOCK: #define OP_CMP_BODY(op,v1,v2) (v1(regs[a]) op v2(regs[a+1])) #ifdef MRB_WITHOUT_FLOAT -#define OP_CMP(op) do {\ +#define OP_CMP(op, sym) do {\ int result;\ /* need to check if - is overridden */\ switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) {\ @@ -2518,7 +2532,8 @@ RETRY_TRY_BLOCK: break;\ default:\ c = 1;\ - goto L_SEND;\ + mid = mrb_sym_ ## sym;\ + goto L_SEND_CONSTSYM;\ }\ if (result) {\ SET_TRUE_VALUE(regs[a]);\ @@ -2528,7 +2543,7 @@ RETRY_TRY_BLOCK: }\ } while(0) #else -#define OP_CMP(op) do {\ +#define OP_CMP(op, sym) do {\ int result;\ /* need to check if - is overridden */\ switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) {\ @@ -2546,7 +2561,8 @@ RETRY_TRY_BLOCK: break;\ default:\ c = 1;\ - goto L_SEND;\ + mid = mrb_sym_ ## sym;\ + goto L_SEND_CONSTSYM;\ }\ if (result) {\ SET_TRUE_VALUE(regs[a]);\ @@ -2557,33 +2573,33 @@ RETRY_TRY_BLOCK: } while(0) #endif - CASE(OP_EQ, BB) { + CASE(OP_EQ, B) { if (mrb_obj_eq(mrb, regs[a], regs[a+1])) { SET_TRUE_VALUE(regs[a]); } else { - OP_CMP(==); + OP_CMP(==, eq); } NEXT; } - CASE(OP_LT, BB) { - OP_CMP(<); + CASE(OP_LT, B) { + OP_CMP(<, gt); NEXT; } - CASE(OP_LE, BB) { - OP_CMP(<=); + CASE(OP_LE, B) { + OP_CMP(<=, ge); NEXT; } - CASE(OP_GT, BB) { - OP_CMP(>); + CASE(OP_GT, B) { + OP_CMP(>, lt); NEXT; } - CASE(OP_GE, BB) { - OP_CMP(>=); + CASE(OP_GE, B) { + OP_CMP(>=, le); NEXT; } -- cgit v1.2.3 From e022080390107e6d746bc24a3651eb6b65daa509 Mon Sep 17 00:00:00 2001 From: take-cheeze Date: Mon, 29 Oct 2018 19:22:43 +0900 Subject: Define null symbol --- include/mruby/symbol.h | 1 + src/symbol.c | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/include/mruby/symbol.h b/include/mruby/symbol.h index 595caeefd..f11346830 100644 --- a/include/mruby/symbol.h +++ b/include/mruby/symbol.h @@ -15,6 +15,7 @@ MRB_BEGIN_DECL typedef enum mrb_reserved_symbol { + mrb_sym_null = 0, // NULL symbol mrb_sym_add = 1, // + mrb_sym_sub = 2, // - mrb_sym_mul = 3, // * diff --git a/src/symbol.c b/src/symbol.c index f7dbc1be1..8ec300e20 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -187,7 +187,7 @@ static struct reserved_symbol_t reserved_symbols[] = { { mrb_sym_gt, ">" }, { mrb_sym_ge, ">=" }, { mrb_sym_method_missing, "method_missing" }, - { 0, NULL }, + { mrb_sym_null, NULL }, }; void @@ -196,7 +196,7 @@ mrb_init_symtbl(mrb_state *mrb) int i; mrb->name2sym = kh_init(n2s, mrb); - for (i = 0; reserved_symbols[i].sym != 0; ++i) { + for (i = 0; reserved_symbols[i].sym != mrb_sym_null; ++i) { mrb_sym s = mrb_intern_static(mrb, reserved_symbols[i].str, strlen(reserved_symbols[i].str)); mrb_assert(s == reserved_symbols[i].sym); } -- cgit v1.2.3 From 68714ab648356695bc235aaeb29fc43e8bad31d1 Mon Sep 17 00:00:00 2001 From: take-cheeze Date: Mon, 29 Oct 2018 19:26:00 +0900 Subject: Fix SEGV --- src/vm.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/vm.c b/src/vm.c index 9792c3fbd..85e2f6c59 100644 --- a/src/vm.c +++ b/src/vm.c @@ -2442,13 +2442,13 @@ RETRY_TRY_BLOCK: NEXT; } - CASE(OP_ADDI, BBB) { + CASE(OP_ADDI, BB) { /* need to check if + is overridden */ switch (mrb_type(regs[a])) { case MRB_TT_FIXNUM: { mrb_int x = mrb_fixnum(regs[a]); - mrb_int y = (mrb_int)c; + mrb_int y = (mrb_int)b; mrb_int z; if (mrb_int_add_overflow(x, y, &z)) { @@ -2465,22 +2465,23 @@ RETRY_TRY_BLOCK: #ifdef MRB_WORD_BOXING { mrb_float x = mrb_float(regs[a]); - SET_FLOAT_VALUE(mrb, regs[a], x + c); + SET_FLOAT_VALUE(mrb, regs[a], x + b); } #else - mrb_float(regs[a]) += c; + mrb_float(regs[a]) += b; #endif break; #endif default: - SET_INT_VALUE(regs[a+1], c); + SET_INT_VALUE(regs[a+1], b); c = 1; - goto L_SEND; + mid = mrb_sym_add; + goto L_SEND_CONSTSYM; } NEXT; } - CASE(OP_SUBI, BBB) { + CASE(OP_SUBI, BB) { mrb_value *regs_a = regs + a; /* need to check if + is overridden */ @@ -2488,7 +2489,7 @@ RETRY_TRY_BLOCK: case MRB_TT_FIXNUM: { mrb_int x = mrb_fixnum(regs_a[0]); - mrb_int y = (mrb_int)c; + mrb_int y = (mrb_int)b; mrb_int z; if (mrb_int_sub_overflow(x, y, &z)) { @@ -2505,17 +2506,18 @@ RETRY_TRY_BLOCK: #ifdef MRB_WORD_BOXING { mrb_float x = mrb_float(regs[a]); - SET_FLOAT_VALUE(mrb, regs[a], (mrb_float)x - (mrb_float)c); + SET_FLOAT_VALUE(mrb, regs[a], (mrb_float)x - (mrb_float)b); } #else - mrb_float(regs_a[0]) -= c; + mrb_float(regs_a[0]) -= b; #endif break; #endif default: - SET_INT_VALUE(regs_a[1], c); + SET_INT_VALUE(regs_a[1], b); c = 1; - goto L_SEND; + mid = mrb_sym_sub; + goto L_SEND_CONSTSYM; } NEXT; } -- cgit v1.2.3 From 557805da071e2dc685a7893107ca06a8e3787af0 Mon Sep 17 00:00:00 2001 From: take-cheeze Date: Mon, 29 Oct 2018 19:29:43 +0900 Subject: Fix operator --- src/vm.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/vm.c b/src/vm.c index 85e2f6c59..005396816 100644 --- a/src/vm.c +++ b/src/vm.c @@ -2586,22 +2586,22 @@ RETRY_TRY_BLOCK: } CASE(OP_LT, B) { - OP_CMP(<, gt); + OP_CMP(<, lt); NEXT; } CASE(OP_LE, B) { - OP_CMP(<=, ge); + OP_CMP(<=, le); NEXT; } CASE(OP_GT, B) { - OP_CMP(>, lt); + OP_CMP(>, gt); NEXT; } CASE(OP_GE, B) { - OP_CMP(>=, le); + OP_CMP(>=, ge); NEXT; } -- cgit v1.2.3 From 486c9d902dfc525f5843dbd3d166e5ccf58e1c81 Mon Sep 17 00:00:00 2001 From: take-cheeze Date: Mon, 29 Oct 2018 19:35:31 +0900 Subject: Fix codedumper --- src/codedump.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/codedump.c b/src/codedump.c index 30a14f937..7d1950f75 100644 --- a/src/codedump.c +++ b/src/codedump.c @@ -328,38 +328,38 @@ codedump(mrb_state *mrb, mrb_irep *irep) CASE(OP_ALIAS, BB): printf("OP_ALIAS\t:%s\t%s\n", mrb_sym2name(mrb, irep->syms[a]), mrb_sym2name(mrb, irep->syms[b])); break; - CASE(OP_ADD, BB): - printf("OP_ADD\tR%d\t:%s\t\n", a, mrb_sym2name(mrb, irep->syms[b])); + CASE(OP_ADD, B): + printf("OP_ADD\tR%d\t\n", a); break; - CASE(OP_ADDI, BBB): - printf("OP_ADDI\tR%d\t:%s\t%d\n", a, mrb_sym2name(mrb, irep->syms[b]), c); + CASE(OP_ADDI, BB): + printf("OP_ADDI\tR%d\t%d\n", a, b); break; - CASE(OP_SUB, BB): - printf("OP_SUB\tR%d\t:%s\t\n", a, mrb_sym2name(mrb, irep->syms[b])); + CASE(OP_SUB, B): + printf("OP_SUB\tR%d\t\n", a); break; - CASE(OP_SUBI, BBB): - printf("OP_SUBI\tR%d\t:%s\t%d\n", a, mrb_sym2name(mrb, irep->syms[b]), c); + CASE(OP_SUBI, BB): + printf("OP_SUBI\tR%d\t%d\n", a, b); break; - CASE(OP_MUL, BB): - printf("OP_MUL\tR%d\t:%s\t\n", a, mrb_sym2name(mrb, irep->syms[b])); + CASE(OP_MUL, B): + printf("OP_MUL\tR%d\t\n", a); break; - CASE(OP_DIV, BB): - printf("OP_DIV\tR%d\t:%s\t\n", a, mrb_sym2name(mrb, irep->syms[b])); + CASE(OP_DIV, B): + printf("OP_DIV\tR%d\t\n", a); break; - CASE(OP_LT, BB): - printf("OP_LT\t\tR%d\t:%s\t\n", a, mrb_sym2name(mrb, irep->syms[b])); + CASE(OP_LT, B): + printf("OP_LT\t\tR%d\t\n", a); break; - CASE(OP_LE, BB): - printf("OP_LE\t\tR%d\t:%s\t\n", a, mrb_sym2name(mrb, irep->syms[b])); + CASE(OP_LE, B): + printf("OP_LE\t\tR%d\t\n", a); break; - CASE(OP_GT, BB): - printf("OP_GT\t\tR%d\t:%s\t\n", a, mrb_sym2name(mrb, irep->syms[b])); + CASE(OP_GT, B): + printf("OP_GT\t\tR%d\t\n", a); break; - CASE(OP_GE, BB): - printf("OP_GE\t\tR%d\t:%s\t\n", a, mrb_sym2name(mrb, irep->syms[b])); + CASE(OP_GE, B): + printf("OP_GE\t\tR%d\t\n", a); break; - CASE(OP_EQ, BB): - printf("OP_EQ\t\tR%d\t:%s\t\n", a, mrb_sym2name(mrb, irep->syms[b])); + CASE(OP_EQ, B): + printf("OP_EQ\t\tR%d\t\n", a); break; CASE(OP_ARRAY, BB): printf("OP_ARRAY\tR%d\t%d\t", a, b); -- cgit v1.2.3