From 9902ab50fcc397d8a6a7dffff35db1310c6b7fa5 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Mon, 2 Aug 2021 08:53:50 +0900 Subject: codegen.c: fix a bug in bit shift constant folding. --- mrbgems/mruby-compiler/core/codegen.c | 21 +++++---------------- 1 file 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; -- cgit v1.2.3