mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
some optimize
This commit is contained in:
parent
beaf05f6c1
commit
2b43cbd25c
@ -181,6 +181,7 @@ struct Type {
|
|||||||
|
|
||||||
struct PyObject;
|
struct PyObject;
|
||||||
#define PK_BITS(p) (reinterpret_cast<Number::int_t>(p))
|
#define PK_BITS(p) (reinterpret_cast<Number::int_t>(p))
|
||||||
|
#define PK_SMALL_INT(val) (reinterpret_cast<PyObject*>(val << 2 | 0b10))
|
||||||
|
|
||||||
inline PyObject* tag_float(f64 val){
|
inline PyObject* tag_float(f64 val){
|
||||||
BitsCvt decomposed(val);
|
BitsCvt decomposed(val);
|
||||||
|
@ -13,7 +13,30 @@ OPCODE(LOAD_CONST)
|
|||||||
OPCODE(LOAD_NONE)
|
OPCODE(LOAD_NONE)
|
||||||
OPCODE(LOAD_TRUE)
|
OPCODE(LOAD_TRUE)
|
||||||
OPCODE(LOAD_FALSE)
|
OPCODE(LOAD_FALSE)
|
||||||
OPCODE(LOAD_INTEGER)
|
/**************************/
|
||||||
|
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)
|
||||||
|
OPCODE(LOAD_INT_3)
|
||||||
|
OPCODE(LOAD_INT_4)
|
||||||
|
OPCODE(LOAD_INT_5)
|
||||||
|
OPCODE(LOAD_INT_6)
|
||||||
|
OPCODE(LOAD_INT_7)
|
||||||
|
OPCODE(LOAD_INT_8)
|
||||||
|
OPCODE(LOAD_INT_9)
|
||||||
|
OPCODE(LOAD_INT_10)
|
||||||
|
OPCODE(LOAD_INT_11)
|
||||||
|
OPCODE(LOAD_INT_12)
|
||||||
|
OPCODE(LOAD_INT_13)
|
||||||
|
OPCODE(LOAD_INT_14)
|
||||||
|
OPCODE(LOAD_INT_15)
|
||||||
|
OPCODE(LOAD_INT_16)
|
||||||
|
/**************************/
|
||||||
OPCODE(LOAD_ELLIPSIS)
|
OPCODE(LOAD_ELLIPSIS)
|
||||||
OPCODE(LOAD_FUNCTION)
|
OPCODE(LOAD_FUNCTION)
|
||||||
OPCODE(LOAD_NULL)
|
OPCODE(LOAD_NULL)
|
||||||
|
@ -10,12 +10,11 @@ namespace pkpy{
|
|||||||
|
|
||||||
#define PREDICT_INT_DIV_OP(op) \
|
#define PREDICT_INT_DIV_OP(op) \
|
||||||
if(is_small_int(_0) && is_small_int(_1)){ \
|
if(is_small_int(_0) && is_small_int(_1)){ \
|
||||||
if(_1 == reinterpret_cast<PyObject*>(0b10)) ZeroDivisionError(); \
|
if(_1 == PK_SMALL_INT(0)) ZeroDivisionError(); \
|
||||||
TOP() = VAR((i64)(PK_BITS(_0)>>2) op (i64)(PK_BITS(_1)>>2)); \
|
TOP() = VAR((i64)(PK_BITS(_0)>>2) op (i64)(PK_BITS(_1)>>2)); \
|
||||||
DISPATCH() \
|
DISPATCH() \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#define BINARY_F_COMPARE(func, op, rfunc) \
|
#define BINARY_F_COMPARE(func, op, rfunc) \
|
||||||
PyObject* ret; \
|
PyObject* ret; \
|
||||||
const PyTypeInfo* _ti = _inst_type_info(_0); \
|
const PyTypeInfo* _ti = _inst_type_info(_0); \
|
||||||
@ -119,7 +118,30 @@ __NEXT_STEP:;
|
|||||||
TARGET(LOAD_NONE) PUSH(None); DISPATCH();
|
TARGET(LOAD_NONE) PUSH(None); DISPATCH();
|
||||||
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_INTEGER) PUSH(VAR((int16_t)byte.arg)); DISPATCH();
|
/*****************************************/
|
||||||
|
TARGET(LOAD_INT_NEG_5) PUSH(PK_SMALL_INT(-5)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_NEG_4) PUSH(PK_SMALL_INT(-4)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_NEG_3) PUSH(PK_SMALL_INT(-3)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_NEG_2) PUSH(PK_SMALL_INT(-2)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_NEG_1) PUSH(PK_SMALL_INT(-1)); 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();
|
||||||
|
TARGET(LOAD_INT_3) PUSH(PK_SMALL_INT(3)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_4) PUSH(PK_SMALL_INT(4)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_5) PUSH(PK_SMALL_INT(5)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_6) PUSH(PK_SMALL_INT(6)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_7) PUSH(PK_SMALL_INT(7)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_8) PUSH(PK_SMALL_INT(8)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_9) PUSH(PK_SMALL_INT(9)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_10) PUSH(PK_SMALL_INT(10)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_11) PUSH(PK_SMALL_INT(11)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_12) PUSH(PK_SMALL_INT(12)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_13) PUSH(PK_SMALL_INT(13)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_14) PUSH(PK_SMALL_INT(14)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_15) PUSH(PK_SMALL_INT(15)); DISPATCH();
|
||||||
|
TARGET(LOAD_INT_16) PUSH(PK_SMALL_INT(16)); DISPATCH();
|
||||||
|
/*****************************************/
|
||||||
TARGET(LOAD_ELLIPSIS) PUSH(Ellipsis); DISPATCH();
|
TARGET(LOAD_ELLIPSIS) PUSH(Ellipsis); DISPATCH();
|
||||||
TARGET(LOAD_FUNCTION) {
|
TARGET(LOAD_FUNCTION) {
|
||||||
FuncDecl_ decl = co->func_decls[byte.arg];
|
FuncDecl_ decl = co->func_decls[byte.arg];
|
||||||
@ -856,8 +878,8 @@ __NEXT_STEP:;
|
|||||||
*p = VAR(CAST(i64, *p) - 1);
|
*p = VAR(CAST(i64, *p) - 1);
|
||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
/*****************************************/
|
/*****************************************/
|
||||||
static_assert(OP_DEC_GLOBAL == 112);
|
static_assert(OP_DEC_GLOBAL == 133);
|
||||||
case 113: case 114: case 115: case 116: case 117: case 118: case 119: case 120: case 121: case 122: case 123: case 124: case 125: case 126: case 127: case 128: case 129: case 130: case 131: case 132: case 133: case 134: case 135: case 136: case 137: case 138: case 139: case 140: case 141: case 142: case 143: case 144: case 145: case 146: case 147: case 148: case 149: case 150: case 151: case 152: case 153: case 154: case 155: case 156: case 157: case 158: case 159: case 160: case 161: case 162: case 163: case 164: case 165: case 166: case 167: case 168: case 169: case 170: case 171: case 172: case 173: case 174: case 175: case 176: case 177: case 178: case 179: case 180: case 181: case 182: case 183: case 184: case 185: case 186: case 187: case 188: case 189: case 190: case 191: case 192: case 193: case 194: case 195: case 196: case 197: case 198: case 199: case 200: case 201: case 202: case 203: case 204: case 205: case 206: case 207: case 208: case 209: case 210: case 211: case 212: case 213: case 214: case 215: case 216: case 217: case 218: case 219: case 220: case 221: case 222: case 223: case 224: case 225: case 226: case 227: case 228: case 229: case 230: case 231: case 232: case 233: case 234: case 235: case 236: case 237: case 238: case 239: case 240: case 241: case 242: case 243: case 244: case 245: case 246: case 247: case 248: case 249: case 250: case 251: case 252: case 253: case 254: case 255: PK_UNREACHABLE() break;
|
case 134: case 135: case 136: case 137: case 138: case 139: case 140: case 141: case 142: case 143: case 144: case 145: case 146: case 147: case 148: case 149: case 150: case 151: case 152: case 153: case 154: case 155: case 156: case 157: case 158: case 159: case 160: case 161: case 162: case 163: case 164: case 165: case 166: case 167: case 168: case 169: case 170: case 171: case 172: case 173: case 174: case 175: case 176: case 177: case 178: case 179: case 180: case 181: case 182: case 183: case 184: case 185: case 186: case 187: case 188: case 189: case 190: case 191: case 192: case 193: case 194: case 195: case 196: case 197: case 198: case 199: case 200: case 201: case 202: case 203: case 204: case 205: case 206: case 207: case 208: case 209: case 210: case 211: case 212: case 213: case 214: case 215: case 216: case 217: case 218: case 219: case 220: case 221: case 222: case 223: case 224: case 225: case 226: case 227: case 228: case 229: case 230: case 231: case 232: case 233: case 234: case 235: case 236: case 237: case 238: case 239: case 240: case 241: case 242: case 243: case 244: case 245: case 246: case 247: case 248: case 249: case 250: case 251: case 252: case 253: case 254: case 255: PK_UNREACHABLE() break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
13
src/expr.cpp
13
src/expr.cpp
@ -2,10 +2,6 @@
|
|||||||
|
|
||||||
namespace pkpy{
|
namespace pkpy{
|
||||||
|
|
||||||
inline bool is_imm_int(i64 v){
|
|
||||||
return v >= INT16_MIN && v <= INT16_MAX;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool is_identifier(std::string_view s){
|
inline bool is_identifier(std::string_view s){
|
||||||
if(s.empty()) return false;
|
if(s.empty()) return false;
|
||||||
if(!isalpha(s[0]) && s[0] != '_') return false;
|
if(!isalpha(s[0]) && s[0] != '_') return false;
|
||||||
@ -64,11 +60,12 @@ namespace pkpy{
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CodeEmitContext::emit_int(i64 _val, int line){
|
int CodeEmitContext::emit_int(i64 value, int line){
|
||||||
if(is_imm_int(_val)){
|
if(value >= -5 && value <= 16){
|
||||||
return emit_(OP_LOAD_INTEGER, (uint16_t)_val, line);
|
uint8_t op = OP_LOAD_INT_NEG_5 + (uint8_t)value + 5;
|
||||||
|
return emit_((Opcode)op, (uint16_t)value, line);
|
||||||
}else{
|
}else{
|
||||||
return emit_(OP_LOAD_CONST, add_const(VAR(_val)), line);
|
return emit_(OP_LOAD_CONST, add_const(VAR(value)), line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,3 +131,6 @@ try:
|
|||||||
exit(1)
|
exit(1)
|
||||||
except SyntaxError:
|
except SyntaxError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
for i in range(-5, 16+1):
|
||||||
|
assert i+1 == i*2//2+1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user