diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-06-11 12:31:49 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-06-11 16:44:47 +0900 |
| commit | f7777f64e6f8bbbe7048306e2834eb5726d2676b (patch) | |
| tree | 86706f3213854e7b2319ad71c455f519a59fd7bb /mrbgems/mruby-compiler/core/codegen.c | |
| parent | 49af1fca03173f415c5634dafdce3ee81cc01401 (diff) | |
| download | mruby-f7777f64e6f8bbbe7048306e2834eb5726d2676b.tar.gz mruby-f7777f64e6f8bbbe7048306e2834eb5726d2676b.zip | |
codegen.c: refactor `readint()`
* renamed from redundant `readint_mrb_int()`
* supports only base upto 16
* no base validation (already done in parser)
* no negative read (negate after read)
* overflow detection using `mrb_int_{mul,add}_overflow()`
Diffstat (limited to 'mrbgems/mruby-compiler/core/codegen.c')
| -rw-r--r-- | mrbgems/mruby-compiler/core/codegen.c | 45 |
1 files changed, 19 insertions, 26 deletions
diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c index d271ca638..06696850f 100644 --- a/mrbgems/mruby-compiler/core/codegen.c +++ b/mrbgems/mruby-compiler/core/codegen.c @@ -1448,7 +1448,7 @@ raise_error(codegen_scope *s, const char *msg) } static mrb_int -readint_mrb_int(codegen_scope *s, const char *p, int base, mrb_bool neg, mrb_bool *overflow) +readint(codegen_scope *s, const char *p, int base, mrb_bool *overflow) { const char *e = p + strlen(p); mrb_int result = 0; @@ -1458,31 +1458,23 @@ readint_mrb_int(codegen_scope *s, const char *p, int base, mrb_bool neg, mrb_boo if (*p == '+') p++; while (p < e) { char c = *p; - c = tolower((unsigned char)c); - for (n=0; n<base; n++) { - if (mrb_digitmap[n] == c) { - break; - } - } - if (n == base) { + switch (c) { + case '0': case '1': case '2': case '3': + case '4': case '5': case '6': case '7': + n = c - '0'; break; + case '8': case '9': + n = c - '0'; break; + case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': + n = c - 'a' + 10; break; + case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': + n = c - 'A' + 10; break; + default: codegen_error(s, "malformed readint input"); } - - if (neg) { - if ((MRB_INT_MIN + n)/base > result) { - *overflow = TRUE; - return 0; - } - result *= base; - result -= n; - } - else { - if ((MRB_INT_MAX - n)/base < result) { - *overflow = TRUE; - return 0; - } - result *= base; - result += n; + if (mrb_int_mul_overflow(result, base, &result) || + mrb_int_add_overflow(result, n, &result)) { + *overflow = TRUE; + return 0; } p++; } @@ -2536,7 +2528,7 @@ codegen(codegen_scope *s, node *tree, int val) mrb_int i; mrb_bool overflow; - i = readint_mrb_int(s, p, base, FALSE, &overflow); + i = readint(s, p, base, &overflow); if (overflow) { int off = new_litbn(s, p, base, FALSE); genop_bs(s, OP_LOADL, cursp(), off); @@ -2601,12 +2593,13 @@ codegen(codegen_scope *s, node *tree, int val) mrb_int i; mrb_bool overflow; - i = readint_mrb_int(s, p, base, TRUE, &overflow); + i = readint(s, p, base, &overflow); if (overflow) { int off = new_litbn(s, p, base, TRUE); genop_bs(s, OP_LOADL, cursp(), off); } else { + i = -i; if (i == -1) genop_1(s, OP_LOADI__1, cursp()); else if (i >= -0xff) { genop_2(s, OP_LOADINEG, cursp(), (uint16_t)-i); |
