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 --- mrbgems/mruby-compiler/core/codegen.c | 43 ++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 21 deletions(-) (limited to 'mrbgems/mruby-compiler') diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c index 8ab1b9bc3..759e7267e 100644 --- a/mrbgems/mruby-compiler/core/codegen.c +++ b/mrbgems/mruby-compiler/core/codegen.c @@ -468,11 +468,11 @@ gen_return(codegen_scope *s, uint8_t op, uint16_t src) } static void -gen_addsub(codegen_scope *s, uint8_t op, uint16_t dst, uint16_t idx) +gen_addsub(codegen_scope *s, uint8_t op, uint16_t dst) { if (no_peephole(s)) { normal: - genop_2(s, op, dst, idx); + genop_1(s, op, dst); return; } else { @@ -493,10 +493,10 @@ gen_addsub(codegen_scope *s, uint8_t op, uint16_t dst, uint16_t idx) if (data.b >= 128) goto normal; s->pc = s->lastpc; if (op == OP_ADD) { - genop_3(s, OP_ADDI, dst, idx, (uint8_t)data.b); + genop_2(s, OP_ADDI, dst, (uint8_t)data.b); } else { - genop_3(s, OP_SUBI, dst, idx, (uint8_t)data.b); + genop_2(s, OP_SUBI, dst, (uint8_t)data.b); } break; default: @@ -982,6 +982,7 @@ gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val, int safe) gen_move(s, cursp(), recv, 1); skip = genjmp2(s, OP_JMPNIL, cursp(), 0, val); } + // TODO: don't new when unused idx = new_sym(s, sym); tree = tree->cdr->cdr->car; if (tree) { @@ -1017,31 +1018,31 @@ gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val, int safe) const char *symname = mrb_sym2name_len(s->mrb, sym, &symlen); if (!noop && symlen == 1 && symname[0] == '+' && n == 1) { - gen_addsub(s, OP_ADD, cursp(), idx); + gen_addsub(s, OP_ADD, cursp()); } else if (!noop && symlen == 1 && symname[0] == '-' && n == 1) { - gen_addsub(s, OP_SUB, cursp(), idx); + gen_addsub(s, OP_SUB, cursp()); } else if (!noop && symlen == 1 && symname[0] == '*' && n == 1) { - genop_2(s, OP_MUL, cursp(), idx); + genop_1(s, OP_MUL, cursp()); } else if (!noop && symlen == 1 && symname[0] == '/' && n == 1) { - genop_2(s, OP_DIV, cursp(), idx); + genop_1(s, OP_DIV, cursp()); } else if (!noop && symlen == 1 && symname[0] == '<' && n == 1) { - genop_2(s, OP_LT, cursp(), idx); + genop_1(s, OP_LT, cursp()); } else if (!noop && symlen == 2 && symname[0] == '<' && symname[1] == '=' && n == 1) { - genop_2(s, OP_LE, cursp(), idx); + genop_1(s, OP_LE, cursp()); } else if (!noop && symlen == 1 && symname[0] == '>' && n == 1) { - genop_2(s, OP_GT, cursp(), idx); + genop_1(s, OP_GT, cursp()); } else if (!noop && symlen == 2 && symname[0] == '>' && symname[1] == '=' && n == 1) { - genop_2(s, OP_GE, cursp(), idx); + genop_1(s, OP_GE, cursp()); } else if (!noop && symlen == 2 && symname[0] == '=' && symname[1] == '=' && n == 1) { - genop_2(s, OP_EQ, cursp(), idx); + genop_1(s, OP_EQ, cursp()); } else { if (sendv) { @@ -2025,28 +2026,28 @@ codegen(codegen_scope *s, node *tree, int val) idx = new_sym(s, sym); if (len == 1 && name[0] == '+') { - gen_addsub(s, OP_ADD, cursp(), idx); + gen_addsub(s, OP_ADD, cursp()); } else if (len == 1 && name[0] == '-') { - gen_addsub(s, OP_SUB, cursp(), idx); + gen_addsub(s, OP_SUB, cursp()); } else if (len == 1 && name[0] == '*') { - genop_2(s, OP_MUL, cursp(), idx); + genop_1(s, OP_MUL, cursp()); } else if (len == 1 && name[0] == '/') { - genop_2(s, OP_DIV, cursp(), idx); + genop_1(s, OP_DIV, cursp()); } else if (len == 1 && name[0] == '<') { - genop_2(s, OP_LT, cursp(), idx); + genop_1(s, OP_LT, cursp()); } else if (len == 2 && name[0] == '<' && name[1] == '=') { - genop_2(s, OP_LE, cursp(), idx); + genop_1(s, OP_LE, cursp()); } else if (len == 1 && name[0] == '>') { - genop_2(s, OP_GT, cursp(), idx); + genop_1(s, OP_GT, cursp()); } else if (len == 2 && name[0] == '>' && name[1] == '=') { - genop_2(s, OP_GE, cursp(), idx); + genop_1(s, OP_GE, cursp()); } else { genop_3(s, OP_SEND, cursp(), idx, 1); -- cgit v1.2.3 From 6a6586ca84c6a9a691c9968320eed5e60e824b3c Mon Sep 17 00:00:00 2001 From: take-cheeze Date: Mon, 29 Oct 2018 19:40:33 +0900 Subject: Reduce unnecessary symbol table entry --- include/mruby/symbol.h | 7 +++++++ mrbgems/mruby-compiler/core/codegen.c | 10 +++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) (limited to 'mrbgems/mruby-compiler') diff --git a/include/mruby/symbol.h b/include/mruby/symbol.h index f11346830..b0feccc51 100644 --- a/include/mruby/symbol.h +++ b/include/mruby/symbol.h @@ -16,6 +16,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, // * @@ -25,9 +26,15 @@ typedef enum mrb_reserved_symbol { mrb_sym_le = 7, // <= mrb_sym_gt = 8, // > mrb_sym_ge = 9, // >= + mrb_sym_method_missing = 10, // method_missing } mrb_reserved_symbol; +static inline mrb_bool +mrb_symbol_constsym_send_p(mrb_sym sym) { + return mrb_sym_add <= sym && sym <= mrb_sym_ge; +} + MRB_END_DECL #endif /* MRUBY_SYMBOL_H */ diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c index 759e7267e..22ac51239 100644 --- a/mrbgems/mruby-compiler/core/codegen.c +++ b/mrbgems/mruby-compiler/core/codegen.c @@ -18,6 +18,7 @@ #include #include #include +#include #ifndef MRB_CODEGEN_LEVEL_MAX #define MRB_CODEGEN_LEVEL_MAX 1024 @@ -982,8 +983,9 @@ gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val, int safe) gen_move(s, cursp(), recv, 1); skip = genjmp2(s, OP_JMPNIL, cursp(), 0, val); } - // TODO: don't new when unused - idx = new_sym(s, sym); + if (!mrb_symbol_constsym_send_p(sym)) { + idx = new_sym(s, sym); + } tree = tree->cdr->cdr->car; if (tree) { n = gen_values(s, tree->car, VAL, sp?1:0); @@ -2024,7 +2026,9 @@ codegen(codegen_scope *s, node *tree, int val) push(); pop(); pop(); pop(); - idx = new_sym(s, sym); + if (!mrb_symbol_constsym_send_p(sym)) { + idx = new_sym(s, sym); + } if (len == 1 && name[0] == '+') { gen_addsub(s, OP_ADD, cursp()); } -- cgit v1.2.3