This commit is contained in:
blueloveTH 2024-03-16 14:40:01 +08:00
parent cd0542df96
commit 37453a8502
3 changed files with 12 additions and 11 deletions

View File

@ -77,8 +77,8 @@ struct ValueStackImpl {
using ValueStack = ValueStackImpl<PK_VM_STACK_SIZE>; using ValueStack = ValueStackImpl<PK_VM_STACK_SIZE>;
struct Frame { struct Frame {
int _ip = -1; int _ip;
int _next_ip = 0; int _next_ip;
ValueStack* _s; ValueStack* _s;
// This is for unwinding only, use `actual_sp_base()` for value stack access // This is for unwinding only, use `actual_sp_base()` for value stack access
PyObject** _sp_base; PyObject** _sp_base;
@ -89,17 +89,16 @@ struct Frame {
FastLocals _locals; FastLocals _locals;
NameDict& f_globals() noexcept { return _module->attr(); } NameDict& f_globals() noexcept { return _module->attr(); }
PyObject* f_closure_try_get(StrName name); PyObject* f_closure_try_get(StrName name);
Frame(ValueStack* _s, PyObject** p0, const CodeObject* co, PyObject* _module, PyObject* _callable) Frame(ValueStack* _s, PyObject** p0, const CodeObject* co, PyObject* _module, PyObject* _callable)
: _s(_s), _sp_base(p0), co(co), _module(_module), _callable(_callable), _locals(co, p0) { } : _ip(-1), _next_ip(0), _s(_s), _sp_base(p0), co(co), _module(_module), _callable(_callable), _locals(co, p0) { }
Frame(ValueStack* _s, PyObject** p0, const CodeObject* co, PyObject* _module, PyObject* _callable, FastLocals _locals) Frame(ValueStack* _s, PyObject** p0, const CodeObject* co, PyObject* _module, PyObject* _callable, FastLocals _locals)
: _s(_s), _sp_base(p0), co(co), _module(_module), _callable(_callable), _locals(_locals) { } : _ip(-1), _next_ip(0), _s(_s), _sp_base(p0), co(co), _module(_module), _callable(_callable), _locals(_locals) { }
Frame(ValueStack* _s, PyObject** p0, const CodeObject_& co, PyObject* _module) Frame(ValueStack* _s, PyObject** p0, const CodeObject_& co, PyObject* _module)
: _s(_s), _sp_base(p0), co(co.get()), _module(_module), _callable(nullptr), _locals(co.get(), p0) {} : _ip(-1), _next_ip(0), _s(_s), _sp_base(p0), co(co.get()), _module(_module), _callable(nullptr), _locals(co.get(), p0) {}
Bytecode next_bytecode() { Bytecode next_bytecode() {
_ip = _next_ip++; _ip = _next_ip++;

View File

@ -512,7 +512,7 @@ __NEXT_STEP:;
frame->jump_abs(byte.arg); frame->jump_abs(byte.arg);
DISPATCH(); DISPATCH();
TARGET(JUMP_ABSOLUTE_TOP) TARGET(JUMP_ABSOLUTE_TOP)
frame->jump_abs(_CAST(uint16_t, POPX())); frame->jump_abs(_CAST(int, POPX()));
DISPATCH(); DISPATCH();
TARGET(POP_JUMP_IF_FALSE){ TARGET(POP_JUMP_IF_FALSE){
if(!py_bool(TOP())) frame->jump_abs(byte.arg); if(!py_bool(TOP())) frame->jump_abs(byte.arg);

View File

@ -43,10 +43,10 @@ namespace pkpy{
if(ctx()->co->varnames.size() > PK_MAX_CO_VARNAMES){ if(ctx()->co->varnames.size() > PK_MAX_CO_VARNAMES){
SyntaxError("maximum number of local variables exceeded"); SyntaxError("maximum number of local variables exceeded");
} }
if(ctx()->co->consts.size() > 65535){ if(ctx()->co->consts.size() > 65530){
SyntaxError("maximum number of constants exceeded"); SyntaxError("maximum number of constants exceeded");
} }
if(codes.size() > 65535 && ctx()->co->src->mode != JSON_MODE){ if(codes.size() > 65530 && ctx()->co->src->mode != JSON_MODE){
// json mode does not contain jump instructions, so it is safe to ignore this check // json mode does not contain jump instructions, so it is safe to ignore this check
SyntaxError("maximum number of opcodes exceeded"); SyntaxError("maximum number of opcodes exceeded");
} }
@ -718,7 +718,8 @@ __EAT_DOTS_END:
} }
// no match, re-raise // no match, re-raise
if(finally_entry != -1){ if(finally_entry != -1){
ctx()->emit_(OP_LOAD_INTEGER, (uint16_t)ctx()->co->codes.size()+2, BC_KEEPLINE); i64 target = ctx()->co->codes.size()+2;
ctx()->emit_(OP_LOAD_CONST, ctx()->add_const(VAR(target)), BC_KEEPLINE);
ctx()->emit_(OP_JUMP_ABSOLUTE, finally_entry, BC_KEEPLINE); ctx()->emit_(OP_JUMP_ABSOLUTE, finally_entry, BC_KEEPLINE);
} }
ctx()->emit_(OP_RE_RAISE, BC_NOARG, BC_KEEPLINE); ctx()->emit_(OP_RE_RAISE, BC_NOARG, BC_KEEPLINE);
@ -726,7 +727,8 @@ __EAT_DOTS_END:
// no exception or no match, jump to the end // no exception or no match, jump to the end
for (int patch : patches) ctx()->patch_jump(patch); for (int patch : patches) ctx()->patch_jump(patch);
if(finally_entry != -1){ if(finally_entry != -1){
ctx()->emit_(OP_LOAD_INTEGER, (uint16_t)ctx()->co->codes.size()+2, BC_KEEPLINE); i64 target = ctx()->co->codes.size()+2;
ctx()->emit_(OP_LOAD_CONST, ctx()->add_const(VAR(target)), BC_KEEPLINE);
ctx()->emit_(OP_JUMP_ABSOLUTE, finally_entry, BC_KEEPLINE); ctx()->emit_(OP_JUMP_ABSOLUTE, finally_entry, BC_KEEPLINE);
} }
} }