From 9c9faa5f89729f0db6977a4955e4bd60c197ad4f Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 23 Apr 2024 12:29:33 +0800 Subject: [PATCH] remove an UB --- include/pocketpy/common.h | 14 +++----------- include/pocketpy/opcodes.h | 5 ----- src/ceval.cpp | 5 ----- src/expr.cpp | 5 ++--- 4 files changed, 5 insertions(+), 24 deletions(-) diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index f71a4ac6..69622002 100644 --- a/include/pocketpy/common.h +++ b/include/pocketpy/common.h @@ -20,7 +20,7 @@ #include #include -#define PK_VERSION "1.4.5" +#define PK_VERSION "1.4.6" #include "config.h" #include "export.h" @@ -70,26 +70,18 @@ namespace std = ::std; template struct NumberTraits; -inline constexpr bool is_negative_shift_well_defined(){ -#ifdef __EMSCRIPTEN__ - return false; -#endif - // rshift does not affect the sign bit - return -1 >> 1 == -1; -} - template <> struct NumberTraits<4> { using int_t = int32_t; static constexpr int_t kMaxSmallInt = (1 << 28) - 1; - static constexpr int_t kMinSmallInt = is_negative_shift_well_defined() ? -(1 << 28) : 0; + static constexpr int_t kMinSmallInt = 0; }; template <> struct NumberTraits<8> { using int_t = int64_t; static constexpr int_t kMaxSmallInt = (1ll << 60) - 1; - static constexpr int_t kMinSmallInt = is_negative_shift_well_defined() ? -(1ll << 60) : 0; + static constexpr int_t kMinSmallInt = 0; }; using Number = NumberTraits; diff --git a/include/pocketpy/opcodes.h b/include/pocketpy/opcodes.h index 269cf094..82368558 100644 --- a/include/pocketpy/opcodes.h +++ b/include/pocketpy/opcodes.h @@ -14,11 +14,6 @@ OPCODE(LOAD_NONE) OPCODE(LOAD_TRUE) OPCODE(LOAD_FALSE) /**************************/ -OPCODE(LOAD_INT_NEG_5) -OPCODE(LOAD_INT_NEG_4) -OPCODE(LOAD_INT_NEG_3) -OPCODE(LOAD_INT_NEG_2) -OPCODE(LOAD_INT_NEG_1) OPCODE(LOAD_INT_0) OPCODE(LOAD_INT_1) OPCODE(LOAD_INT_2) diff --git a/src/ceval.cpp b/src/ceval.cpp index c8b420e4..2a45c169 100644 --- a/src/ceval.cpp +++ b/src/ceval.cpp @@ -119,11 +119,6 @@ __NEXT_STEP:; TARGET(LOAD_TRUE) PUSH(True); DISPATCH(); TARGET(LOAD_FALSE) PUSH(False); DISPATCH(); /*****************************************/ - TARGET(LOAD_INT_NEG_5) PUSH((PyObject*)-18); DISPATCH(); - TARGET(LOAD_INT_NEG_4) PUSH((PyObject*)-14); DISPATCH(); - TARGET(LOAD_INT_NEG_3) PUSH((PyObject*)-10); DISPATCH(); - TARGET(LOAD_INT_NEG_2) PUSH((PyObject*)-6); DISPATCH(); - TARGET(LOAD_INT_NEG_1) PUSH((PyObject*)-2); DISPATCH(); TARGET(LOAD_INT_0) PUSH(PK_SMALL_INT(0)); DISPATCH(); TARGET(LOAD_INT_1) PUSH(PK_SMALL_INT(1)); DISPATCH(); TARGET(LOAD_INT_2) PUSH(PK_SMALL_INT(2)); DISPATCH(); diff --git a/src/expr.cpp b/src/expr.cpp index 45ad6bcf..7e0781f2 100644 --- a/src/expr.cpp +++ b/src/expr.cpp @@ -86,9 +86,8 @@ namespace pkpy{ } int CodeEmitContext::emit_int(i64 value, int line){ - bool allow_neg_int = is_negative_shift_well_defined() || value >= 0; - if(allow_neg_int && value >= -5 && value <= 16){ - uint8_t op = OP_LOAD_INT_NEG_5 + (uint8_t)value + 5; + if(value >= 0 && value <= 16){ + uint8_t op = OP_LOAD_INT_0 + (uint8_t)value; return emit_((Opcode)op, BC_NOARG, line); }else{ return emit_(OP_LOAD_CONST, add_const(VAR(value)), line);