Jelajahi Sumber

remove an UB

blueloveTH 1 tahun lalu
induk
melakukan
9c9faa5f89
4 mengubah file dengan 5 tambahan dan 24 penghapusan
  1. 3 11
      include/pocketpy/common.h
  2. 0 5
      include/pocketpy/opcodes.h
  3. 0 5
      src/ceval.cpp
  4. 2 3
      src/expr.cpp

+ 3 - 11
include/pocketpy/common.h

@@ -20,7 +20,7 @@
 #include <typeindex>
 #include <initializer_list>
 
-#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 <size_t T>
 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<sizeof(void*)>;

+ 0 - 5
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)

+ 0 - 5
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();

+ 2 - 3
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);