remove VM::top_frame

This commit is contained in:
blueloveTH 2024-05-03 19:49:12 +08:00
parent 4f520a0c26
commit fd12deeec3
5 changed files with 14 additions and 18 deletions

View File

@ -168,10 +168,6 @@ public:
void set_main_argv(int argc, char** argv); void set_main_argv(int argc, char** argv);
void __breakpoint(); void __breakpoint();
Frame* top_frame(){
return &callstack.top();
}
void __pop_frame(){ void __pop_frame(){
s_data.reset(callstack.top()._sp_base); s_data.reset(callstack.top()._sp_base);
callstack.pop(); callstack.pop();

View File

@ -81,7 +81,7 @@ bool VM::py_ge(PyObject* _0, PyObject* _1){
#undef BINARY_F_COMPARE #undef BINARY_F_COMPARE
PyObject* VM::__run_top_frame(){ PyObject* VM::__run_top_frame(){
Frame* frame = top_frame(); Frame* frame = &callstack.top();
const Frame* base_frame = frame; const Frame* base_frame = frame;
bool need_raise = false; bool need_raise = false;
@ -667,7 +667,7 @@ __NEXT_STEP:;
true true
); );
if(_0 == PY_OP_CALL){ if(_0 == PY_OP_CALL){
frame = top_frame(); frame = &callstack.top();
goto __NEXT_FRAME; goto __NEXT_FRAME;
} }
PUSH(_0); PUSH(_0);
@ -702,7 +702,7 @@ __NEXT_STEP:;
); );
} }
if(_0 == PY_OP_CALL){ if(_0 == PY_OP_CALL){
frame = top_frame(); frame = &callstack.top();
goto __NEXT_FRAME; goto __NEXT_FRAME;
} }
PUSH(_0); PUSH(_0);
@ -713,7 +713,7 @@ __NEXT_STEP:;
if(frame == base_frame){ // [ frameBase<- ] if(frame == base_frame){ // [ frameBase<- ]
return _0; return _0;
}else{ }else{
frame = top_frame(); frame = &callstack.top();
PUSH(_0); PUSH(_0);
goto __NEXT_FRAME; goto __NEXT_FRAME;
} }
@ -983,7 +983,7 @@ __NEXT_STEP:;
bool is_base_frame_to_be_popped = frame == base_frame; bool is_base_frame_to_be_popped = frame == base_frame;
__pop_frame(); __pop_frame();
if(callstack.empty()) throw _e; // propagate to the top level if(callstack.empty()) throw _e; // propagate to the top level
frame = top_frame(); frame = &callstack.top();
PUSH(e_obj); PUSH(e_obj);
if(is_base_frame_to_be_popped) throw ToBeRaisedException(); if(is_base_frame_to_be_popped) throw ToBeRaisedException();
need_raise = true; need_raise = true;

View File

@ -100,7 +100,7 @@ void add_module_json(VM* vm){
sv = CAST(Str&, args[0]).sv(); sv = CAST(Str&, args[0]).sv();
} }
CodeObject_ code = vm->compile(sv, "<json>", JSON_MODE); CodeObject_ code = vm->compile(sv, "<json>", JSON_MODE);
return vm->_exec(code, vm->top_frame()->_module); return vm->_exec(code, vm->callstack.top()._module);
}); });
vm->bind_func<1>(mod, "dumps", [](VM* vm, ArgsView args) { vm->bind_func<1>(mod, "dumps", [](VM* vm, ArgsView args) {

View File

@ -74,7 +74,7 @@ void init_builtins(VM* _vm) {
// builtin functions // builtin functions
_vm->bind_func<0>(_vm->builtins, "breakpoint", [](VM* vm, ArgsView args) { _vm->bind_func<0>(_vm->builtins, "breakpoint", [](VM* vm, ArgsView args) {
#if PK_ENABLE_PROFILER #if PK_ENABLE_PROFILER
vm->_next_breakpoint = NextBreakpoint(vm->callstack.size(), vm->top_frame()->curr_lineno(), false); vm->_next_breakpoint = NextBreakpoint(vm->callstack.size(), vm->callstack.top().curr_lineno(), false);
#endif #endif
return vm->None; return vm->None;
}); });
@ -86,7 +86,7 @@ void init_builtins(VM* _vm) {
class_arg = args[0]; class_arg = args[0];
self_arg = args[1]; self_arg = args[1];
}else if(args.size() == 0){ }else if(args.size() == 0){
Frame* frame = vm->top_frame(); Frame* frame = &vm->callstack.top();
if(frame->_callable != nullptr){ if(frame->_callable != nullptr){
class_arg = PK_OBJ_GET(Function, frame->_callable)._class; class_arg = PK_OBJ_GET(Function, frame->_callable)._class;
if(frame->_locals.size() > 0) self_arg = frame->_locals[0]; if(frame->_locals.size() > 0) self_arg = frame->_locals[0];
@ -140,7 +140,7 @@ void init_builtins(VM* _vm) {
}); });
_vm->bind_func<0>(_vm->builtins, "globals", [](VM* vm, ArgsView args) { _vm->bind_func<0>(_vm->builtins, "globals", [](VM* vm, ArgsView args) {
PyObject* mod = vm->top_frame()->_module; PyObject* mod = vm->callstack.top()._module;
return VAR(MappingProxy(mod)); return VAR(MappingProxy(mod));
}); });
@ -201,7 +201,7 @@ void init_builtins(VM* _vm) {
CodeObject_ code = vm->compile(CAST(Str&, args[0]), "<eval>", EVAL_MODE, true); CodeObject_ code = vm->compile(CAST(Str&, args[0]), "<eval>", EVAL_MODE, true);
PyObject* globals = args[1]; PyObject* globals = args[1];
if(globals == vm->None){ if(globals == vm->None){
Frame* frame = vm->top_frame(); Frame* frame = &vm->callstack.top();
return vm->_exec(code.get(), frame->_module, frame->_callable, frame->_locals); return vm->_exec(code.get(), frame->_module, frame->_callable, frame->_locals);
} }
vm->check_type(globals, VM::tp_mappingproxy); vm->check_type(globals, VM::tp_mappingproxy);
@ -213,7 +213,7 @@ void init_builtins(VM* _vm) {
CodeObject_ code = vm->compile(CAST(Str&, args[0]), "<exec>", EXEC_MODE, true); CodeObject_ code = vm->compile(CAST(Str&, args[0]), "<exec>", EXEC_MODE, true);
PyObject* globals = args[1]; PyObject* globals = args[1];
if(globals == vm->None){ if(globals == vm->None){
Frame* frame = vm->top_frame(); Frame* frame = &vm->callstack.top();
vm->_exec(code.get(), frame->_module, frame->_callable, frame->_locals); vm->_exec(code.get(), frame->_module, frame->_callable, frame->_locals);
return vm->None; return vm->None;
} }

View File

@ -694,7 +694,7 @@ void VM::_log_s_data(const char* title) {
if(f._sp_base == nullptr) PK_FATAL_ERROR(); if(f._sp_base == nullptr) PK_FATAL_ERROR();
sp_bases[f._sp_base] += 1; sp_bases[f._sp_base] += 1;
} }
Frame* frame = top_frame(); Frame* frame = &callstack.top();
int line = frame->co->lines[frame->_ip]; int line = frame->co->lines[frame->_ip];
ss << frame->co->name << ":" << line << " ["; ss << frame->co->name << ":" << line << " [";
for(PyObject** p=s_data.begin(); p!=s_data.end(); p++){ for(PyObject** p=s_data.begin(); p!=s_data.end(); p++){
@ -1298,7 +1298,7 @@ void VM::_error(PyObject* e_obj){
} }
void VM::__raise(bool re_raise){ void VM::__raise(bool re_raise){
Frame* frame = top_frame(); Frame* frame = &callstack.top();
Exception& e = PK_OBJ_GET(Exception, s_data.top()); Exception& e = PK_OBJ_GET(Exception, s_data.top());
if(!re_raise){ if(!re_raise){
e._ip_on_error = frame->_ip; e._ip_on_error = frame->_ip;
@ -1497,7 +1497,7 @@ void NativeFunc::check_size(VM* vm, ArgsView args) const{
#if PK_ENABLE_PROFILER #if PK_ENABLE_PROFILER
void NextBreakpoint::_step(VM* vm){ void NextBreakpoint::_step(VM* vm){
int curr_callstack_size = vm->callstack.size(); int curr_callstack_size = vm->callstack.size();
int curr_lineno = vm->top_frame()->curr_lineno(); int curr_lineno = vm->callstack.top().curr_lineno();
if(should_step_into){ if(should_step_into){
if(curr_callstack_size != callstack_size || curr_lineno != lineno){ if(curr_callstack_size != callstack_size || curr_lineno != lineno){
vm->__breakpoint(); vm->__breakpoint();