diff --git a/include/pocketpy/config.h b/include/pocketpy/config.h index 284e68bf..5f6dc1b9 100644 --- a/include/pocketpy/config.h +++ b/include/pocketpy/config.h @@ -76,16 +76,10 @@ #endif #ifdef _MSC_VER -#define PK_ENABLE_COMPUTED_GOTO 0 #define PK_UNREACHABLE() __assume(0); #else -#define PK_ENABLE_COMPUTED_GOTO 1 #define PK_UNREACHABLE() __builtin_unreachable(); #endif -#if PK_DEBUG_CEVAL_STEP && defined(PK_ENABLE_COMPUTED_GOTO) -#undef PK_ENABLE_COMPUTED_GOTO -#endif - #endif \ No newline at end of file diff --git a/include/pocketpy/frame.h b/include/pocketpy/frame.h index 564fbf29..d56c82d5 100644 --- a/include/pocketpy/frame.h +++ b/include/pocketpy/frame.h @@ -100,12 +100,12 @@ struct Frame { Frame(ValueStack* _s, PyObject** p0, const CodeObject_& co, PyObject* _module) : _ip(-1), _next_ip(0), _s(_s), _sp_base(p0), co(co.get()), _module(_module), _callable(nullptr), _locals(co.get(), p0) {} - Bytecode next_bytecode() { + int next_bytecode() { _ip = _next_ip++; #if PK_DEBUG_EXTRA_CHECK if(_ip >= co->codes.size()) PK_FATAL_ERROR(); #endif - return co->codes[_ip]; + return _ip; } PyObject** actual_sp_base() const { return _locals.a; } diff --git a/src/ceval.cpp b/src/ceval.cpp index 9fc59fa4..ac5cda1a 100644 --- a/src/ceval.cpp +++ b/src/ceval.cpp @@ -72,27 +72,16 @@ PyObject* VM::_run_top_frame(){ #define DISPATCH_OP_CALL() { frame = top_frame(); goto __NEXT_FRAME; } __NEXT_FRAME: - Bytecode byte = frame->next_bytecode(); - CEVAL_STEP_CALLBACK(); - // cache const CodeObject* co = frame->co; const auto& co_consts = co->consts; + const Bytecode* co_codes = co->codes.data(); -#if PK_ENABLE_COMPUTED_GOTO -static void* OP_LABELS[] = { - #define OPCODE(name) &&CASE_OP_##name, - #include "pocketpy/opcodes.h" - #undef OPCODE -}; + Bytecode byte = co_codes[frame->next_bytecode()]; + CEVAL_STEP_CALLBACK(); -#define DISPATCH() { byte = frame->next_bytecode(); CEVAL_STEP_CALLBACK(); goto *OP_LABELS[byte.op];} -#define TARGET(op) CASE_OP_##op: -goto *OP_LABELS[byte.op]; - -#else #define TARGET(op) case OP_##op: -#define DISPATCH() { byte = frame->next_bytecode(); CEVAL_STEP_CALLBACK(); goto __NEXT_STEP;} +#define DISPATCH() { byte = co_codes[frame->next_bytecode()]; CEVAL_STEP_CALLBACK(); goto __NEXT_STEP;} __NEXT_STEP:; #if PK_DEBUG_CEVAL_STEP @@ -100,7 +89,6 @@ __NEXT_STEP:; #endif switch (byte.op) { -#endif TARGET(NO_OP) DISPATCH(); /*****************************************/ TARGET(POP_TOP) POP(); DISPATCH(); @@ -820,12 +808,11 @@ __NEXT_STEP:; if(p == nullptr) vm->NameError(_name); *p = VAR(CAST(i64, *p) - 1); } DISPATCH(); - -#if !PK_ENABLE_COMPUTED_GOTO + /*****************************************/ static_assert(OP_DEC_GLOBAL == 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 + } #undef DISPATCH