From 866abe2c63e522f75231fb417c07d7f8adc6e5d1 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 5 Jan 2024 22:27:03 +0800 Subject: [PATCH] ... --- 3rd/cjson/src/cJSONw.cpp | 4 +--- src/ceval.cpp | 13 ++++++------- src/expr.cpp | 15 ++++++++++----- src/vm.cpp | 6 ------ 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/3rd/cjson/src/cJSONw.cpp b/3rd/cjson/src/cJSONw.cpp index ac192ecf..2bdf8849 100644 --- a/3rd/cjson/src/cJSONw.cpp +++ b/3rd/cjson/src/cJSONw.cpp @@ -47,9 +47,8 @@ static cJSON* convert_python_object_to_cjson(PyObject* obj, VM* vm){ return convert_list_to_cjson(_CAST(Tuple&, obj), vm); }else if(obj == vm->None){ return cJSON_CreateNull(); - }else{ - vm->TypeError(fmt("unrecognized type ", obj_type_name(vm, obj_t).escape())); } + vm->TypeError(fmt("unrecognized type ", obj_type_name(vm, obj_t).escape())); PK_UNREACHABLE(); } @@ -124,7 +123,6 @@ void add_module_cjson(VM* vm){ const char* end = start; while(*end != '\0' && *end != '\n') end++; vm->IOError(fmt("cjson: ", std::string_view(start, end-start))); - PK_UNREACHABLE(); } PyObject* output = convert_cjson_to_python_object(json, vm); cJSON_Delete(json); diff --git a/src/ceval.cpp b/src/ceval.cpp index eb746984..6268044b 100644 --- a/src/ceval.cpp +++ b/src/ceval.cpp @@ -121,11 +121,11 @@ __NEXT_STEP:; if(heap._should_auto_collect()) heap._auto_collect(); PUSH(co_consts[byte.arg]); DISPATCH(); - TARGET(LOAD_NONE) PUSH(None); DISPATCH(); - TARGET(LOAD_TRUE) PUSH(True); DISPATCH(); - TARGET(LOAD_FALSE) PUSH(False); DISPATCH(); - TARGET(LOAD_INTEGER) PUSH(VAR((int16_t)byte.arg)); DISPATCH(); - TARGET(LOAD_ELLIPSIS) PUSH(Ellipsis); DISPATCH(); + TARGET(LOAD_NONE) PUSH(None); DISPATCH(); + TARGET(LOAD_TRUE) PUSH(True); DISPATCH(); + TARGET(LOAD_FALSE) PUSH(False); DISPATCH(); + TARGET(LOAD_INTEGER) PUSH(VAR((int16_t)byte.arg)); DISPATCH(); + TARGET(LOAD_ELLIPSIS) PUSH(Ellipsis); DISPATCH(); TARGET(LOAD_FUNCTION) { FuncDecl_ decl = co->func_decls[byte.arg]; PyObject* obj; @@ -799,7 +799,6 @@ __NEXT_STEP:; } if(!isinstance(TOP(), tp_exception)){ _builtin_error("TypeError", "exceptions must derive from Exception"); - PK_UNREACHABLE(); } _error(POPX()); } DISPATCH(); @@ -845,7 +844,7 @@ __NEXT_STEP:; #if !PK_ENABLE_COMPUTED_GOTO static_assert(OP_DEC_GLOBAL == 111); - case 112: 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_FATAL_ERROR(); break; + case 112: 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; } #endif } diff --git a/src/expr.cpp b/src/expr.cpp index 523bd222..96ef64ca 100644 --- a/src/expr.cpp +++ b/src/expr.cpp @@ -2,6 +2,10 @@ namespace pkpy{ + inline bool is_imm_int(i64 v){ + return v >= INT16_MIN && v <= INT16_MAX; + } + int CodeEmitContext::get_loop() const { int index = curr_block_i; while(index >= 0){ @@ -243,14 +247,15 @@ namespace pkpy{ PyObject* obj = nullptr; if(std::holds_alternative(value)){ i64 _val = std::get(value); - if(_val >= INT16_MIN && _val <= INT16_MAX){ + if(is_imm_int(_val)){ ctx->emit_(OP_LOAD_INTEGER, (uint16_t)_val, line); return; } obj = VAR(_val); } if(std::holds_alternative(value)){ - obj = VAR(std::get(value)); + f64 _val = std::get(value); + obj = VAR(_val); } if(std::holds_alternative(value)){ obj = VAR(std::get(value)); @@ -266,7 +271,7 @@ namespace pkpy{ LiteralExpr* lit = static_cast(child.get()); if(std::holds_alternative(lit->value)){ i64 _val = -std::get(lit->value); - if(_val >= INT16_MIN && _val <= INT16_MAX){ + if(is_imm_int(_val)){ ctx->emit_(OP_LOAD_INTEGER, (uint16_t)_val, line); }else{ ctx->emit_(OP_LOAD_CONST, ctx->add_const(VAR(_val)), line); @@ -274,8 +279,8 @@ namespace pkpy{ return; } if(std::holds_alternative(lit->value)){ - PyObject* obj = VAR(-std::get(lit->value)); - ctx->emit_(OP_LOAD_CONST, ctx->add_const(obj), line); + f64 _val = -std::get(lit->value); + ctx->emit_(OP_LOAD_CONST, ctx->add_const(VAR(_val)), line); return; } } diff --git a/src/vm.cpp b/src/vm.cpp index 7938549b..7b688c12 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -28,7 +28,6 @@ namespace pkpy{ first = false; if(!is_non_tagged_type(k, vm->tp_str)){ vm->TypeError(fmt("json keys must be string, got ", obj_type_name(vm, vm->_tp(k)))); - PK_UNREACHABLE(); } ss << _CAST(Str&, k).escape(false) << ": "; write_object(v); @@ -58,7 +57,6 @@ namespace pkpy{ write_dict(_CAST(Dict&, obj)); }else{ vm->TypeError(fmt("unrecognized type ", obj_type_name(vm, obj_t).escape())); - PK_UNREACHABLE(); } } @@ -524,7 +522,6 @@ PyObject* VM::_format_string(Str spec, PyObject* obj){ } }catch(...){ ValueError("invalid format specifer"); - PK_UNREACHABLE(); } if(type != 'f' && dot >= 0) ValueError("precision not allowed in the format specifier"); @@ -808,7 +805,6 @@ void VM::_prepare_py_call(PyObject** buffer, ArgsView args, ArgsView kwargs, con vm->TypeError(fmt( co->name, "() takes ", decl_argc, " positional arguments but ", args.size(), " were given" )); - PK_UNREACHABLE(); } int i = 0; @@ -917,11 +913,9 @@ PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){ TypeError(fmt( co->name, "() takes ", decl->args.size(), " positional arguments but ", args.size(), " were given" )); - PK_UNREACHABLE(); } if(!kwargs.empty()){ TypeError(fmt(co->name, "() takes no keyword arguments")); - PK_UNREACHABLE(); } s_data.reset(_base + co_nlocals); int i = 0;