summaryrefslogtreecommitdiffhomepage
path: root/compiler.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-06-08 13:51:44 +0200
committerJo-Philipp Wich <jo@mein.io>2021-06-08 13:54:50 +0200
commit2a838d1f9f58d05bc1e15084efa42ad86fc105e4 (patch)
tree4fea33b29a7e01aafcb105ccc220a65b63819a3d /compiler.c
parent9872f652114c826c37ac6d9d92202c84943c5f23 (diff)
compiler: improve mapping of binary operator tokens to instructions
Instead of relying on a switch/case mapping of token values to corresponding VM instructions, infer the instruction number arithmetically. This shrinks the compiled size on x86/64 by about 250 bytes. Also emit I_LE and I_GE instructions for `<=` and `>=` comparisons instead of transforming these into I_GT and I_LT negations. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'compiler.c')
-rw-r--r--compiler.c31
1 files changed, 1 insertions, 30 deletions
diff --git a/compiler.c b/compiler.c
index 4f20be0..dfd0d63 100644
--- a/compiler.c
+++ b/compiler.c
@@ -952,36 +952,7 @@ uc_compiler_compile_binary(uc_compiler *compiler, bool assignable)
uc_tokentype_t type = compiler->parser->prev.type;
uc_compiler_parse_precedence(compiler, uc_compiler_parse_rule(type)->precedence + 1);
-
- switch (type) {
- case TK_ADD: uc_compiler_emit_insn(compiler, 0, I_ADD); break;
- case TK_SUB: uc_compiler_emit_insn(compiler, 0, I_SUB); break;
- case TK_MUL: uc_compiler_emit_insn(compiler, 0, I_MUL); break;
- case TK_DIV: uc_compiler_emit_insn(compiler, 0, I_DIV); break;
- case TK_MOD: uc_compiler_emit_insn(compiler, 0, I_MOD); break;
- case TK_LSHIFT: uc_compiler_emit_insn(compiler, 0, I_LSHIFT); break;
- case TK_RSHIFT: uc_compiler_emit_insn(compiler, 0, I_RSHIFT); break;
- case TK_BAND: uc_compiler_emit_insn(compiler, 0, I_BAND); break;
- case TK_BXOR: uc_compiler_emit_insn(compiler, 0, I_BXOR); break;
- case TK_BOR: uc_compiler_emit_insn(compiler, 0, I_BOR); break;
- case TK_LT: uc_compiler_emit_insn(compiler, 0, I_LT); break;
- case TK_LE:
- uc_compiler_emit_insn(compiler, 0, I_GT);
- uc_compiler_emit_insn(compiler, 0, I_NOT);
- break;
- case TK_GT: uc_compiler_emit_insn(compiler, 0, I_GT); break;
- case TK_GE:
- uc_compiler_emit_insn(compiler, 0, I_LT);
- uc_compiler_emit_insn(compiler, 0, I_NOT);
- break;
- case TK_EQ: uc_compiler_emit_insn(compiler, 0, I_EQ); break;
- case TK_NE: uc_compiler_emit_insn(compiler, 0, I_NE); break;
- case TK_EQS: uc_compiler_emit_insn(compiler, 0, I_EQS); break;
- case TK_NES: uc_compiler_emit_insn(compiler, 0, I_NES); break;
- case TK_IN: uc_compiler_emit_insn(compiler, 0, I_IN); break;
- default:
- return;
- }
+ uc_compiler_emit_insn(compiler, 0, I_BOR + (type - TK_BOR));
}
static void