remove _s in Frame

This commit is contained in:
blueloveTH 2024-03-16 15:32:17 +08:00
parent 638ce2699a
commit 1456fb5e20
6 changed files with 30 additions and 30 deletions

View File

@ -79,7 +79,6 @@ using ValueStack = ValueStackImpl<PK_VM_STACK_SIZE>;
struct Frame { struct Frame {
int _ip; int _ip;
int _next_ip; int _next_ip;
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;
@ -91,14 +90,14 @@ struct Frame {
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(PyObject** p0, const CodeObject* co, PyObject* _module, PyObject* _callable)
: _ip(-1), _next_ip(0), _s(_s), _sp_base(p0), co(co), _module(_module), _callable(_callable), _locals(co, p0) { } : _ip(-1), _next_ip(0), _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(PyObject** p0, const CodeObject* co, PyObject* _module, PyObject* _callable, FastLocals _locals)
: _ip(-1), _next_ip(0), _s(_s), _sp_base(p0), co(co), _module(_module), _callable(_callable), _locals(_locals) { } : _ip(-1), _next_ip(0), _sp_base(p0), co(co), _module(_module), _callable(_callable), _locals(_locals) { }
Frame(ValueStack* _s, PyObject** p0, const CodeObject_& co, PyObject* _module) Frame(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) {} : _ip(-1), _next_ip(0), _sp_base(p0), co(co.get()), _module(_module), _callable(nullptr), _locals(co.get(), p0) {}
int next_bytecode() { int next_bytecode() {
_ip = _next_ip++; _ip = _next_ip++;
@ -109,13 +108,14 @@ struct Frame {
} }
PyObject** actual_sp_base() const { return _locals.a; } PyObject** actual_sp_base() const { return _locals.a; }
int stack_size() const { return _s->_sp - actual_sp_base(); }
ArgsView stack_view() const { return ArgsView(actual_sp_base(), _s->_sp); } int stack_size(ValueStack* _s) const { return _s->_sp - actual_sp_base(); }
ArgsView stack_view(ValueStack* _s) const { return ArgsView(actual_sp_base(), _s->_sp); }
void jump_abs(int i){ _next_ip = i; } void jump_abs(int i){ _next_ip = i; }
bool jump_to_exception_handler(); bool jump_to_exception_handler(ValueStack*);
int _exit_block(int i); int _exit_block(ValueStack*, int);
void jump_abs_break(int target); void jump_abs_break(ValueStack*, int);
void _gc_mark() const { void _gc_mark() const {
PK_OBJ_MARK(_module); PK_OBJ_MARK(_module);

View File

@ -172,7 +172,7 @@ public:
template<typename ...Args> template<typename ...Args>
PyObject* _exec(Args&&... args){ PyObject* _exec(Args&&... args){
callstack.emplace(&s_data, s_data._sp, std::forward<Args>(args)...); callstack.emplace(s_data._sp, std::forward<Args>(args)...);
return _run_top_frame(); return _run_top_frame();
} }

View File

@ -529,13 +529,13 @@ __NEXT_STEP:;
frame->jump_abs(byte.arg); frame->jump_abs(byte.arg);
DISPATCH(); DISPATCH();
TARGET(LOOP_BREAK) TARGET(LOOP_BREAK)
frame->jump_abs_break(byte.arg); frame->jump_abs_break(&s_data, byte.arg);
DISPATCH(); DISPATCH();
TARGET(GOTO) { TARGET(GOTO) {
StrName _name(byte.arg); StrName _name(byte.arg);
int index = co->labels.try_get_likely_found(_name); int index = co->labels.try_get_likely_found(_name);
if(index < 0) RuntimeError(_S("label ", _name.escape(), " not found")); if(index < 0) RuntimeError(_S("label ", _name.escape(), " not found"));
frame->jump_abs_break(index); frame->jump_abs_break(&s_data, index);
} DISPATCH(); } DISPATCH();
/*****************************************/ /*****************************************/
TARGET(FSTRING_EVAL){ TARGET(FSTRING_EVAL){
@ -651,7 +651,7 @@ __NEXT_STEP:;
if(_0 != StopIteration){ if(_0 != StopIteration){
PUSH(_0); PUSH(_0);
}else{ }else{
frame->jump_abs_break(byte.arg); frame->jump_abs_break(&s_data, byte.arg);
} }
} DISPATCH(); } DISPATCH();
/*****************************************/ /*****************************************/

View File

@ -23,7 +23,7 @@ namespace pkpy{
return fn._closure->try_get(name); return fn._closure->try_get(name);
} }
bool Frame::jump_to_exception_handler(){ bool Frame::jump_to_exception_handler(ValueStack* _s){
// try to find a parent try block // try to find a parent try block
int block = co->iblocks[_ip]; int block = co->iblocks[_ip];
while(block >= 0){ while(block >= 0){
@ -34,24 +34,24 @@ namespace pkpy{
PyObject* obj = _s->popx(); // pop exception object PyObject* obj = _s->popx(); // pop exception object
// get the stack size of the try block // get the stack size of the try block
int _stack_size = co->blocks[block].base_stack_size; int _stack_size = co->blocks[block].base_stack_size;
if(stack_size() < _stack_size) throw std::runtime_error(_S("invalid state: ", stack_size(), '<', _stack_size).str()); if(stack_size(_s) < _stack_size) throw std::runtime_error(_S("invalid state: ", stack_size(_s), '<', _stack_size).str());
_s->reset(actual_sp_base() + _locals.size() + _stack_size); // rollback the stack _s->reset(actual_sp_base() + _locals.size() + _stack_size); // rollback the stack
_s->push(obj); // push exception object _s->push(obj); // push exception object
_next_ip = co->blocks[block].end; _next_ip = co->blocks[block].end;
return true; return true;
} }
int Frame::_exit_block(int i){ int Frame::_exit_block(ValueStack* _s, int i){
auto type = co->blocks[i].type; auto type = co->blocks[i].type;
if(type==CodeBlockType::FOR_LOOP || type==CodeBlockType::CONTEXT_MANAGER) _s->pop(); if(type==CodeBlockType::FOR_LOOP || type==CodeBlockType::CONTEXT_MANAGER) _s->pop();
return co->blocks[i].parent; return co->blocks[i].parent;
} }
void Frame::jump_abs_break(int target){ void Frame::jump_abs_break(ValueStack* _s, int target){
int i = co->iblocks[_ip]; int i = co->iblocks[_ip];
_next_ip = target; _next_ip = target;
if(_next_ip >= co->codes.size()){ if(_next_ip >= co->codes.size()){
while(i>=0) i = _exit_block(i); while(i>=0) i = _exit_block(_s, i);
}else{ }else{
// BUG (solved) // BUG (solved)
// for i in range(4): // for i in range(4):
@ -59,7 +59,7 @@ namespace pkpy{
// # if there is no op here, the block check will fail // # if there is no op here, the block check will fail
// while i: --i // while i: --i
int next_block = co->iblocks[target]; int next_block = co->iblocks[target];
while(i>=0 && i!=next_block) i = _exit_block(i); while(i>=0 && i!=next_block) i = _exit_block(_s, i);
if(i!=next_block) throw std::runtime_error("invalid jump"); if(i!=next_block) throw std::runtime_error("invalid jump");
} }
} }

View File

@ -43,10 +43,10 @@ namespace pkpy{
PyObject* Generator::next(VM* vm){ PyObject* Generator::next(VM* vm){
if(state == 2) return vm->StopIteration; if(state == 2) return vm->StopIteration;
// reset frame._sp_base // reset frame._sp_base
frame._sp_base = frame._s->_sp; frame._sp_base = vm->s_data._sp;
frame._locals.a = frame._s->_sp; frame._locals.a = vm->s_data._sp;
// restore the context // restore the context
for(PyObject* obj: s_backup) frame._s->push(obj); for(PyObject* obj: s_backup) vm->s_data.push(obj);
s_backup.clear(); s_backup.clear();
vm->callstack.push(std::move(frame)); vm->callstack.push(std::move(frame));
@ -61,8 +61,8 @@ namespace pkpy{
if(ret == PY_OP_YIELD){ if(ret == PY_OP_YIELD){
// backup the context // backup the context
frame = std::move(vm->callstack.top()); frame = std::move(vm->callstack.top());
ret = frame._s->popx(); ret = vm->s_data.popx();
for(PyObject* obj: frame.stack_view()) s_backup.push_back(obj); for(PyObject* obj: frame.stack_view(&vm->s_data)) s_backup.push_back(obj);
vm->_pop_frame(); vm->_pop_frame();
state = 1; state = 1;
if(ret == vm->StopIteration) state = 2; if(ret == vm->StopIteration) state = 2;

View File

@ -931,7 +931,7 @@ PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){
if(co->is_generator){ if(co->is_generator){
s_data.reset(p0); s_data.reset(p0);
return _py_generator( return _py_generator(
Frame(&s_data, nullptr, co, fn._module, callable), Frame(nullptr, co, fn._module, callable),
ArgsView(buffer, buffer + co_nlocals) ArgsView(buffer, buffer + co_nlocals)
); );
} }
@ -941,7 +941,7 @@ PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){
for(int j=0; j<co_nlocals; j++) _base[j] = buffer[j]; for(int j=0; j<co_nlocals; j++) _base[j] = buffer[j];
__FAST_CALL: __FAST_CALL:
callstack.emplace(&s_data, p0, co, fn._module, callable, FastLocals(co, args.begin())); callstack.emplace(p0, co, fn._module, callable, FastLocals(co, args.begin()));
if(op_call) return PY_OP_CALL; if(op_call) return PY_OP_CALL;
return _run_top_frame(); return _run_top_frame();
/*****************_py_call*****************/ /*****************_py_call*****************/
@ -1244,7 +1244,7 @@ void VM::_raise(bool re_raise){
e._ip_on_error = frame->_ip; e._ip_on_error = frame->_ip;
e._code_on_error = (void*)frame->co; e._code_on_error = (void*)frame->co;
} }
bool ok = frame->jump_to_exception_handler(); bool ok = frame->jump_to_exception_handler(&s_data);
int actual_ip = frame->_ip; int actual_ip = frame->_ip;
if(e._ip_on_error >= 0 && e._code_on_error == (void*)frame->co) actual_ip = e._ip_on_error; if(e._ip_on_error >= 0 && e._code_on_error == (void*)frame->co) actual_ip = e._ip_on_error;