summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2021-08-02 08:53:50 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2021-08-02 08:53:50 +0900
commit9902ab50fcc397d8a6a7dffff35db1310c6b7fa5 (patch)
treefdb0b5c4dc1925f9e634a5fdd545a4e56ea5342c
parenta9d721ea8fcb8c725e26a1ac0884ba3227ea5a08 (diff)
downloadmruby-9902ab50fcc397d8a6a7dffff35db1310c6b7fa5.tar.gz
mruby-9902ab50fcc397d8a6a7dffff35db1310c6b7fa5.zip
codegen.c: fix a bug in bit shift constant folding.
-rw-r--r--mrbgems/mruby-compiler/core/codegen.c21
1 files changed, 5 insertions, 16 deletions
diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c
index 9221cd91b..00cdc0059 100644
--- a/mrbgems/mruby-compiler/core/codegen.c
+++ b/mrbgems/mruby-compiler/core/codegen.c
@@ -798,6 +798,8 @@ gen_muldiv(codegen_scope *s, uint8_t op, uint16_t dst)
}
}
+mrb_bool mrb_num_shift(mrb_state *mrb, mrb_int val, mrb_int width, mrb_int *num);
+
static mrb_bool
gen_binop(codegen_scope *s, mrb_sym op, uint16_t dst)
{
@@ -814,24 +816,11 @@ gen_binop(codegen_scope *s, mrb_sym op, uint16_t dst)
return FALSE;
}
if (op == MRB_OPSYM_2(s->mrb, lshift)) {
- if (n < 0) {
- if (-63 > n || -n >= sizeof(mrb_int)*8) return FALSE;
- n = n0 >> (-n);
- }
- else {
- if (n >= sizeof(mrb_int)*8-1) return FALSE;
- n = n0 << n;
- }
+ if (!mrb_num_shift(s->mrb, n0, n, &n)) return FALSE;
}
else if (op == MRB_OPSYM_2(s->mrb, rshift)) {
- if (n < 0) {
- if (-63 > n || -n > sizeof(mrb_int)*8-1) return FALSE;
- n = n0 << (-n);
- }
- else {
- if (n >= sizeof(mrb_int)*8-1) return FALSE;
- n = n0 >> n;
- }
+ if (n == MRB_INT_MIN) return FALSE;
+ if (!mrb_num_shift(s->mrb, n0, -n, &n)) return FALSE;
}
else if (op == MRB_OPSYM_2(s->mrb, mod)) {
if (n0 < 0 || n < 0) return FALSE;