diff --git a/include/pocketpy/interpreter/frame.h b/include/pocketpy/interpreter/frame.h index ed9fd6cb..9cda5531 100644 --- a/include/pocketpy/interpreter/frame.h +++ b/include/pocketpy/interpreter/frame.h @@ -28,8 +28,8 @@ typedef struct UnwindTarget { UnwindTarget* UnwindTarget__new(UnwindTarget* next, int iblock, int offset); void UnwindTarget__delete(UnwindTarget* self); -typedef struct Frame { - struct Frame* f_back; +typedef struct py_Frame { + struct py_Frame* f_back; const CodeObject* co; py_StackRef p0; // unwinding base py_GlobalRef module; @@ -38,35 +38,35 @@ typedef struct Frame { bool is_locals_special; int ip; UnwindTarget* uw_list; -} Frame; +} py_Frame; typedef struct SourceLocation { SourceData_ src; int lineno; } SourceLocation; -Frame* Frame__new(const CodeObject* co, +py_Frame* Frame__new(const CodeObject* co, py_StackRef p0, py_GlobalRef module, py_Ref globals, py_Ref locals, bool is_locals_special); -void Frame__delete(Frame* self); +void Frame__delete(py_Frame* self); -int Frame__lineno(const Frame* self); -int Frame__iblock(const Frame* self); +int Frame__lineno(const py_Frame* self); +int Frame__iblock(const py_Frame* self); -int Frame__getglobal(Frame* self, py_Name name) PY_RAISE PY_RETURN; -bool Frame__setglobal(Frame* self, py_Name name, py_TValue* val) PY_RAISE; -int Frame__delglobal(Frame* self, py_Name name) PY_RAISE; +int Frame__getglobal(py_Frame* self, py_Name name) PY_RAISE PY_RETURN; +bool Frame__setglobal(py_Frame* self, py_Name name, py_TValue* val) PY_RAISE; +int Frame__delglobal(py_Frame* self, py_Name name) PY_RAISE; -py_Ref Frame__getclosure(Frame* self, py_Name name); -py_StackRef Frame__getlocal_noproxy(Frame* self, py_Name name); +py_Ref Frame__getclosure(py_Frame* self, py_Name name); +py_StackRef Frame__getlocal_noproxy(py_Frame* self, py_Name name); -int Frame__prepare_jump_exception_handler(Frame* self, ValueStack*); +int Frame__prepare_jump_exception_handler(py_Frame* self, ValueStack*); -UnwindTarget* Frame__find_unwind_target(Frame* self, int iblock); -void Frame__set_unwind_target(Frame* self, py_TValue* sp); +UnwindTarget* Frame__find_unwind_target(py_Frame* self, int iblock); +void Frame__set_unwind_target(py_Frame* self, py_TValue* sp); -void Frame__gc_mark(Frame* self); -SourceLocation Frame__source_location(Frame* self); \ No newline at end of file +void Frame__gc_mark(py_Frame* self); +SourceLocation Frame__source_location(py_Frame* self); \ No newline at end of file diff --git a/include/pocketpy/interpreter/generator.h b/include/pocketpy/interpreter/generator.h index 55616d39..bc8a53cc 100644 --- a/include/pocketpy/interpreter/generator.h +++ b/include/pocketpy/interpreter/generator.h @@ -4,10 +4,10 @@ #include "pocketpy/pocketpy.h" typedef struct Generator{ - Frame* frame; + py_Frame* frame; int state; } Generator; -void pk_newgenerator(py_Ref out, Frame* frame, py_TValue* begin, py_TValue* end); +void pk_newgenerator(py_Ref out, py_Frame* frame, py_TValue* begin, py_TValue* end); void Generator__dtor(Generator* ud); \ No newline at end of file diff --git a/include/pocketpy/interpreter/vm.h b/include/pocketpy/interpreter/vm.h index 836c0b38..6c3cfce5 100644 --- a/include/pocketpy/interpreter/vm.h +++ b/include/pocketpy/interpreter/vm.h @@ -24,7 +24,7 @@ typedef struct TraceInfo { } TraceInfo; typedef struct VM { - Frame* top_frame; + py_Frame* top_frame; ModuleDict modules; TypeList types; @@ -57,7 +57,7 @@ typedef struct VM { void VM__ctor(VM* self); void VM__dtor(VM* self); -void VM__push_frame(VM* self, Frame* frame); +void VM__push_frame(VM* self, py_Frame* frame); void VM__pop_frame(VM* self); bool pk__parse_int_slice(py_Ref slice, int length, int* restrict start, int* restrict stop, int* restrict step); @@ -108,7 +108,7 @@ bool pk_execdyn(CodeObject* co, py_Ref module, py_Ref globals, py_Ref locals); /// The stack remains unchanged. bool pk_stack_binaryop(VM* self, py_Name op, py_Name rop); -void pk_print_stack(VM* self, Frame* frame, Bytecode byte); +void pk_print_stack(VM* self, py_Frame* frame, Bytecode byte); // type registration void pk_object__register(); diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index f11d9fd3..6ebc864d 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -69,7 +69,7 @@ static bool stack_format_object(VM* self, c11_sv spec); case RES_CALL: { \ frame = self->top_frame; \ if(self->trace_info.tracefunc) { \ - self->trace_info.tracefunc((py_Frame*)frame, TRACE_EVENT_CALL); \ + self->trace_info.tracefunc(frame, TRACE_EVENT_CALL); \ } \ goto __NEXT_FRAME; \ } \ @@ -91,10 +91,10 @@ static bool unpack_dict_to_buffer(py_Ref key, py_Ref val, void* ctx) { } FrameResult VM__run_top_frame(VM* self) { - Frame* frame = self->top_frame; + py_Frame* frame = self->top_frame; Bytecode* codes; - const Frame* base_frame = frame; + const py_Frame* base_frame = frame; while(true) { Bytecode byte; @@ -112,7 +112,7 @@ FrameResult VM__run_top_frame(VM* self) { if(prev_loc.src) PK_DECREF(prev_loc.src); PK_INCREF(loc.src); self->trace_info.prev_loc = loc; - self->trace_info.tracefunc((py_Frame*)frame, TRACE_EVENT_LINE); + self->trace_info.tracefunc(frame, TRACE_EVENT_LINE); } } @@ -225,7 +225,7 @@ FrameResult VM__run_top_frame(VM* self) { // locals switch(frame->locals->type) { case tp_locals: { - Frame* noproxy = frame->locals->_ptr; + py_Frame* noproxy = frame->locals->_ptr; py_Ref slot = Frame__getlocal_noproxy(noproxy, name); if(slot == NULL) break; if(py_isnil(slot)) { @@ -376,7 +376,7 @@ FrameResult VM__run_top_frame(VM* self) { py_Name name = byte.arg; switch(frame->locals->type) { case tp_locals: { - Frame* noproxy = frame->locals->_ptr; + py_Frame* noproxy = frame->locals->_ptr; py_Ref slot = Frame__getlocal_noproxy(noproxy, name); if(slot == NULL) { UnboundLocalError(name); @@ -443,7 +443,7 @@ FrameResult VM__run_top_frame(VM* self) { py_Name name = byte.arg; switch(frame->locals->type) { case tp_locals: { - Frame* noproxy = frame->locals->_ptr; + py_Frame* noproxy = frame->locals->_ptr; py_Ref slot = Frame__getlocal_noproxy(noproxy, name); if(slot == NULL || py_isnil(slot)) { UnboundLocalError(name); @@ -758,7 +758,7 @@ FrameResult VM__run_top_frame(VM* self) { py_newnone(&self->last_retval); } if(self->trace_info.tracefunc) { - self->trace_info.tracefunc((py_Frame*)frame, TRACE_EVENT_RETURN); + self->trace_info.tracefunc(frame, TRACE_EVENT_RETURN); } VM__pop_frame(self); if(frame == base_frame) { // [ frameBase<- ] diff --git a/src/interpreter/frame.c b/src/interpreter/frame.c index 1ce45cb4..dbb98aa0 100644 --- a/src/interpreter/frame.c +++ b/src/interpreter/frame.c @@ -47,7 +47,7 @@ UnwindTarget* UnwindTarget__new(UnwindTarget* next, int iblock, int offset) { void UnwindTarget__delete(UnwindTarget* self) { PK_FREE(self); } -Frame* Frame__new(const CodeObject* co, +py_Frame* Frame__new(const CodeObject* co, py_StackRef p0, py_GlobalRef module, py_Ref globals, @@ -58,7 +58,7 @@ Frame* Frame__new(const CodeObject* co, if(is_locals_special) { assert(locals->type == tp_nil || locals->type == tp_locals || locals->type == tp_dict); } - Frame* self = FixedMemoryPool__alloc(&pk_current_vm->pool_frame); + py_Frame* self = FixedMemoryPool__alloc(&pk_current_vm->pool_frame); self->f_back = NULL; self->co = co; self->p0 = p0; @@ -71,7 +71,7 @@ Frame* Frame__new(const CodeObject* co, return self; } -void Frame__delete(Frame* self) { +void Frame__delete(py_Frame* self) { while(self->uw_list) { UnwindTarget* p = self->uw_list; self->uw_list = p->next; @@ -80,7 +80,7 @@ void Frame__delete(Frame* self) { FixedMemoryPool__dealloc(&pk_current_vm->pool_frame, self); } -int Frame__prepare_jump_exception_handler(Frame* self, ValueStack* _s) { +int Frame__prepare_jump_exception_handler(py_Frame* self, ValueStack* _s) { // try to find a parent try block int iblock = Frame__iblock(self); while(iblock >= 0) { @@ -94,7 +94,7 @@ int Frame__prepare_jump_exception_handler(Frame* self, ValueStack* _s) { return c11__at(CodeBlock, &self->co->blocks, iblock)->end; } -UnwindTarget* Frame__find_unwind_target(Frame* self, int iblock) { +UnwindTarget* Frame__find_unwind_target(py_Frame* self, int iblock) { UnwindTarget* uw; for(uw = self->uw_list; uw; uw = uw->next) { if(uw->iblock == iblock) return uw; @@ -102,7 +102,7 @@ UnwindTarget* Frame__find_unwind_target(Frame* self, int iblock) { return NULL; } -void Frame__set_unwind_target(Frame* self, py_TValue* sp) { +void Frame__set_unwind_target(py_Frame* self, py_TValue* sp) { int iblock = Frame__iblock(self); UnwindTarget* existing = Frame__find_unwind_target(self, iblock); if(existing) { @@ -113,23 +113,23 @@ void Frame__set_unwind_target(Frame* self, py_TValue* sp) { } } -void Frame__gc_mark(Frame* self) { +void Frame__gc_mark(py_Frame* self) { pk__mark_value(self->globals); if(self->is_locals_special) pk__mark_value(self->locals); CodeObject__gc_mark(self->co); } -int Frame__lineno(const Frame* self) { +int Frame__lineno(const py_Frame* self) { int ip = self->ip; return c11__getitem(BytecodeEx, &self->co->codes_ex, ip).lineno; } -int Frame__iblock(const Frame* self) { +int Frame__iblock(const py_Frame* self) { int ip = self->ip; return c11__getitem(BytecodeEx, &self->co->codes_ex, ip).iblock; } -int Frame__getglobal(Frame* self, py_Name name) { +int Frame__getglobal(py_Frame* self, py_Name name) { if(self->globals->type == tp_module) { py_ItemRef item = py_getdict(self->globals, name); if(item != NULL) { @@ -142,7 +142,7 @@ int Frame__getglobal(Frame* self, py_Name name) { } } -bool Frame__setglobal(Frame* self, py_Name name, py_TValue* val) { +bool Frame__setglobal(py_Frame* self, py_Name name, py_TValue* val) { if(self->globals->type == tp_module) { py_setdict(self->globals, name, val); return true; @@ -151,7 +151,7 @@ bool Frame__setglobal(Frame* self, py_Name name, py_TValue* val) { } } -int Frame__delglobal(Frame* self, py_Name name) { +int Frame__delglobal(py_Frame* self, py_Name name) { if(self->globals->type == tp_module) { bool found = py_deldict(self->globals, name); return found ? 1 : 0; @@ -160,14 +160,14 @@ int Frame__delglobal(Frame* self, py_Name name) { } } -py_StackRef Frame__getlocal_noproxy(Frame* self, py_Name name) { +py_StackRef Frame__getlocal_noproxy(py_Frame* self, py_Name name) { assert(!self->is_locals_special); int index = c11_smallmap_n2i__get(&self->co->varnames_inv, name, -1); if(index == -1) return NULL; return &self->locals[index]; } -py_Ref Frame__getclosure(Frame* self, py_Name name) { +py_Ref Frame__getclosure(py_Frame* self, py_Name name) { if(self->is_locals_special) return NULL; assert(self->p0->type == tp_function); Function* ud = py_touserdata(self->p0); @@ -175,7 +175,7 @@ py_Ref Frame__getclosure(Frame* self, py_Name name) { return NameDict__try_get(ud->closure, name); } -SourceLocation Frame__source_location(Frame* self) { +SourceLocation Frame__source_location(py_Frame* self) { SourceLocation loc; loc.lineno = Frame__lineno(self); loc.src = self->co->src; diff --git a/src/interpreter/generator.c b/src/interpreter/generator.c index 82eb4d48..5ca2cb6d 100644 --- a/src/interpreter/generator.c +++ b/src/interpreter/generator.c @@ -5,7 +5,7 @@ #include "pocketpy/pocketpy.h" #include -void pk_newgenerator(py_Ref out, Frame* frame, py_TValue* begin, py_TValue* end) { +void pk_newgenerator(py_Ref out, py_Frame* frame, py_TValue* begin, py_TValue* end) { Generator* ud = py_newobject(out, tp_generator, 1, sizeof(Generator)); ud->frame = frame; ud->state = 0; diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 7b11af2c..9f3f22d4 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -78,7 +78,7 @@ void VM__ctor(VM* self) { self->curr_function = NULL; memset(&self->trace_info, 0, sizeof(TraceInfo)); - FixedMemoryPool__ctor(&self->pool_frame, sizeof(Frame), 32); + FixedMemoryPool__ctor(&self->pool_frame, sizeof(py_Frame), 32); ManagedHeap__ctor(&self->heap); ValueStack__ctor(&self->stack); @@ -262,14 +262,14 @@ void VM__dtor(VM* self) { InternedNames__dtor(&self->names); } -void VM__push_frame(VM* self, Frame* frame) { +void VM__push_frame(VM* self, py_Frame* frame) { frame->f_back = self->top_frame; self->top_frame = frame; } void VM__pop_frame(VM* self) { assert(self->top_frame); - Frame* frame = self->top_frame; + py_Frame* frame = self->top_frame; // reset stack pointer self->stack.sp = frame->p0; @@ -532,7 +532,7 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall // copy buffer back to stack self->stack.sp = argv + co->nlocals; memcpy(argv, self->vectorcall_buffer, co->nlocals * sizeof(py_TValue)); - Frame* frame = Frame__new(co, p0, fn->module, fn->globals, argv, false); + py_Frame* frame = Frame__new(co, p0, fn->module, fn->globals, argv, false); pk_newgenerator(py_retval(), frame, p0, self->stack.sp); self->stack.sp = p0; // reset the stack return RES_RETURN; @@ -717,7 +717,7 @@ void ManagedHeap__mark(ManagedHeap* self) { pk__mark_value(&ti->annotations); } // mark frame - for(Frame* frame = vm->top_frame; frame; frame = frame->f_back) { + for(py_Frame* frame = vm->top_frame; frame; frame = frame->f_back) { Frame__gc_mark(frame); } // mark vm's registers @@ -733,7 +733,7 @@ void ManagedHeap__mark(ManagedHeap* self) { } } -void pk_print_stack(VM* self, Frame* frame, Bytecode byte) { +void pk_print_stack(VM* self, py_Frame* frame, Bytecode byte) { return; if(frame == NULL || py_isnil(&self->main)) return; diff --git a/src/public/exec.c b/src/public/exec.c index 0a69931c..0bf1d4e6 100644 --- a/src/public/exec.c +++ b/src/public/exec.c @@ -53,7 +53,7 @@ bool pk_exec(CodeObject* co, py_Ref module) { assert(module->type == tp_module); py_StackRef sp = vm->stack.sp; - Frame* frame = Frame__new(co, sp, module, module, py_NIL(), true); + py_Frame* frame = Frame__new(co, sp, module, module, py_NIL(), true); VM__push_frame(vm, frame); FrameResult res = VM__run_top_frame(vm); if(res == RES_ERROR) return false; @@ -84,7 +84,7 @@ bool pk_execdyn(CodeObject* co, py_Ref module, py_Ref globals, py_Ref locals) { default: return TypeError("locals must be a dict object"); } - Frame* frame = Frame__new(co, sp, module, globals, locals, true); + py_Frame* frame = Frame__new(co, sp, module, globals, locals, true); VM__push_frame(vm, frame); FrameResult res = VM__run_top_frame(vm); if(res == RES_ERROR) return false; diff --git a/src/public/modules.c b/src/public/modules.c index 35caf469..3f77d7db 100644 --- a/src/public/modules.c +++ b/src/public/modules.c @@ -493,7 +493,7 @@ static bool builtins_locals(int argc, py_Ref argv) { } void py_newglobals(py_Ref out) { - Frame* frame = pk_current_vm->top_frame; + py_Frame* frame = pk_current_vm->top_frame; if(!frame) { pk_mappingproxy__namedict(out, &pk_current_vm->main); return; @@ -506,7 +506,7 @@ void py_newglobals(py_Ref out) { } void py_newlocals(py_Ref out) { - Frame* frame = pk_current_vm->top_frame; + py_Frame* frame = pk_current_vm->top_frame; if(!frame) { py_newdict(out); return; @@ -524,7 +524,7 @@ void py_newlocals(py_Ref out) { } static void pk_push_special_locals() { - Frame* frame = pk_current_vm->top_frame; + py_Frame* frame = pk_current_vm->top_frame; if(!frame) { py_pushnil(); return; @@ -588,7 +588,7 @@ static bool _builtins_execdyn(const char* title, int argc, py_Ref argv, enum py_ return TypeError("%s() expected 'str' or 'code', got '%t'", title, argv->type); } - Frame* frame = pk_current_vm->top_frame; + py_Frame* frame = pk_current_vm->top_frame; // [globals, locals, code] CodeObject* code = py_touserdata(py_peek(-1)); if(code->src->is_dynamic) { @@ -818,7 +818,7 @@ py_Type pk_nativefunc__register() { static bool super__new__(int argc, py_Ref argv) { py_Type class_arg = 0; - Frame* frame = pk_current_vm->top_frame; + py_Frame* frame = pk_current_vm->top_frame; py_Ref self_arg = NULL; if(argc == 1) { // super() diff --git a/src/public/py_exception.c b/src/public/py_exception.c index 9b3f04c4..38d8afc4 100644 --- a/src/public/py_exception.c +++ b/src/public/py_exception.c @@ -252,8 +252,8 @@ bool py_raise(py_Ref exc) { vm->curr_exception = *exc; vm->is_curr_exc_handled = false; if(vm->trace_info.tracefunc && !py_istype(exc, tp_StopIteration)) { - Frame* frame = vm->top_frame; - vm->trace_info.tracefunc((py_Frame*)frame, TRACE_EVENT_EXCEPTION); + py_Frame* frame = vm->top_frame; + vm->trace_info.tracefunc(frame, TRACE_EVENT_EXCEPTION); } return false; } diff --git a/src/public/stack_ops.c b/src/public/stack_ops.c index 2d4ddf84..ccf5972a 100644 --- a/src/public/stack_ops.c +++ b/src/public/stack_ops.c @@ -73,7 +73,7 @@ py_StackRef py_inspect_currentfunction(){ } py_GlobalRef py_inspect_currentmodule(){ - Frame* frame = pk_current_vm->top_frame; + py_Frame* frame = pk_current_vm->top_frame; if(!frame) return NULL; return frame->module; }