remove an UB

This commit is contained in:
blueloveTH 2024-04-23 12:29:33 +08:00
parent 4ba44fa51b
commit 9c9faa5f89
4 changed files with 5 additions and 24 deletions

View File

@ -20,7 +20,7 @@
#include <typeindex> #include <typeindex>
#include <initializer_list> #include <initializer_list>
#define PK_VERSION "1.4.5" #define PK_VERSION "1.4.6"
#include "config.h" #include "config.h"
#include "export.h" #include "export.h"
@ -70,26 +70,18 @@ namespace std = ::std;
template <size_t T> template <size_t T>
struct NumberTraits; 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 <> template <>
struct NumberTraits<4> { struct NumberTraits<4> {
using int_t = int32_t; using int_t = int32_t;
static constexpr int_t kMaxSmallInt = (1 << 28) - 1; 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 <> template <>
struct NumberTraits<8> { struct NumberTraits<8> {
using int_t = int64_t; using int_t = int64_t;
static constexpr int_t kMaxSmallInt = (1ll << 60) - 1; 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*)>; using Number = NumberTraits<sizeof(void*)>;

View File

@ -14,11 +14,6 @@ OPCODE(LOAD_NONE)
OPCODE(LOAD_TRUE) OPCODE(LOAD_TRUE)
OPCODE(LOAD_FALSE) 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_0)
OPCODE(LOAD_INT_1) OPCODE(LOAD_INT_1)
OPCODE(LOAD_INT_2) OPCODE(LOAD_INT_2)

View File

@ -119,11 +119,6 @@ __NEXT_STEP:;
TARGET(LOAD_TRUE) PUSH(True); DISPATCH(); TARGET(LOAD_TRUE) PUSH(True); DISPATCH();
TARGET(LOAD_FALSE) PUSH(False); 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_0) PUSH(PK_SMALL_INT(0)); DISPATCH();
TARGET(LOAD_INT_1) PUSH(PK_SMALL_INT(1)); DISPATCH(); TARGET(LOAD_INT_1) PUSH(PK_SMALL_INT(1)); DISPATCH();
TARGET(LOAD_INT_2) PUSH(PK_SMALL_INT(2)); DISPATCH(); TARGET(LOAD_INT_2) PUSH(PK_SMALL_INT(2)); DISPATCH();

View File

@ -86,9 +86,8 @@ namespace pkpy{
} }
int CodeEmitContext::emit_int(i64 value, int line){ int CodeEmitContext::emit_int(i64 value, int line){
bool allow_neg_int = is_negative_shift_well_defined() || value >= 0; if(value >= 0 && value <= 16){
if(allow_neg_int && value >= -5 && value <= 16){ uint8_t op = OP_LOAD_INT_0 + (uint8_t)value;
uint8_t op = OP_LOAD_INT_NEG_5 + (uint8_t)value + 5;
return emit_((Opcode)op, BC_NOARG, line); return emit_((Opcode)op, BC_NOARG, line);
}else{ }else{
return emit_(OP_LOAD_CONST, add_const(VAR(value)), line); return emit_(OP_LOAD_CONST, add_const(VAR(value)), line);