|
@@ -6,11 +6,9 @@ void ValueStack__ctor(ValueStack* self) {
|
|
|
self->end = self->begin + PK_VM_STACK_SIZE;
|
|
self->end = self->begin + PK_VM_STACK_SIZE;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void ValueStack__clear(ValueStack* self) {
|
|
|
|
|
- self->sp = self->begin;
|
|
|
|
|
-}
|
|
|
|
|
|
|
+void ValueStack__clear(ValueStack* self) { self->sp = self->begin; }
|
|
|
|
|
|
|
|
-py_TValue* FastLocals__try_get_by_name(py_TValue* locals, const CodeObject* co, py_Name name){
|
|
|
|
|
|
|
+py_TValue* FastLocals__try_get_by_name(py_TValue* locals, const CodeObject* co, py_Name name) {
|
|
|
int index = c11_smallmap_n2i__get(&co->varnames_inv, name, -1);
|
|
int index = c11_smallmap_n2i__get(&co->varnames_inv, name, -1);
|
|
|
if(index == -1) return NULL;
|
|
if(index == -1) return NULL;
|
|
|
return &locals[index];
|
|
return &locals[index];
|
|
@@ -20,14 +18,12 @@ pk_NameDict* FastLocals__to_namedict(py_TValue* locals, const CodeObject* co) {
|
|
|
pk_NameDict* dict = pk_NameDict__new();
|
|
pk_NameDict* dict = pk_NameDict__new();
|
|
|
c11__foreach(c11_smallmap_n2i_KV, &co->varnames_inv, entry) {
|
|
c11__foreach(c11_smallmap_n2i_KV, &co->varnames_inv, entry) {
|
|
|
py_TValue value = locals[entry->value];
|
|
py_TValue value = locals[entry->value];
|
|
|
- if(!py_isnil(&value)){
|
|
|
|
|
- pk_NameDict__set(dict, entry->key, value);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if(!py_isnil(&value)) { pk_NameDict__set(dict, entry->key, value); }
|
|
|
}
|
|
}
|
|
|
return dict;
|
|
return dict;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-UnwindTarget* UnwindTarget__new(UnwindTarget* next, int iblock, int offset){
|
|
|
|
|
|
|
+UnwindTarget* UnwindTarget__new(UnwindTarget* next, int iblock, int offset) {
|
|
|
UnwindTarget* self = malloc(sizeof(UnwindTarget));
|
|
UnwindTarget* self = malloc(sizeof(UnwindTarget));
|
|
|
self->next = next;
|
|
self->next = next;
|
|
|
self->iblock = iblock;
|
|
self->iblock = iblock;
|
|
@@ -35,11 +31,14 @@ UnwindTarget* UnwindTarget__new(UnwindTarget* next, int iblock, int offset){
|
|
|
return self;
|
|
return self;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void UnwindTarget__delete(UnwindTarget* self){
|
|
|
|
|
- free(self);
|
|
|
|
|
-}
|
|
|
|
|
|
|
+void UnwindTarget__delete(UnwindTarget* self) { free(self); }
|
|
|
|
|
|
|
|
-Frame* Frame__new(const CodeObject* co, const py_TValue* module, const py_TValue* function, py_TValue* p0, py_TValue* locals, const CodeObject* locals_co){
|
|
|
|
|
|
|
+Frame* Frame__new(const CodeObject* co,
|
|
|
|
|
+ const py_TValue* module,
|
|
|
|
|
+ const py_TValue* function,
|
|
|
|
|
+ py_TValue* p0,
|
|
|
|
|
+ py_TValue* locals,
|
|
|
|
|
+ const CodeObject* locals_co) {
|
|
|
static_assert(sizeof(Frame) <= kPoolFrameBlockSize, "!(sizeof(Frame) <= kPoolFrameBlockSize)");
|
|
static_assert(sizeof(Frame) <= kPoolFrameBlockSize, "!(sizeof(Frame) <= kPoolFrameBlockSize)");
|
|
|
Frame* self = PoolFrame_alloc();
|
|
Frame* self = PoolFrame_alloc();
|
|
|
self->f_back = NULL;
|
|
self->f_back = NULL;
|
|
@@ -54,7 +53,7 @@ Frame* Frame__new(const CodeObject* co, const py_TValue* module, const py_TValue
|
|
|
return self;
|
|
return self;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void Frame__delete(Frame* self){
|
|
|
|
|
|
|
+void Frame__delete(Frame* self) {
|
|
|
while(self->uw_list) {
|
|
while(self->uw_list) {
|
|
|
UnwindTarget* p = self->uw_list;
|
|
UnwindTarget* p = self->uw_list;
|
|
|
self->uw_list = p->next;
|
|
self->uw_list = p->next;
|
|
@@ -63,7 +62,7 @@ void Frame__delete(Frame* self){
|
|
|
PoolFrame_dealloc(self);
|
|
PoolFrame_dealloc(self);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-int Frame__prepare_jump_exception_handler(Frame* self, ValueStack* _s){
|
|
|
|
|
|
|
+int Frame__prepare_jump_exception_handler(Frame* self, ValueStack* _s) {
|
|
|
// try to find a parent try block
|
|
// try to find a parent try block
|
|
|
int iblock = Frame__iblock(self);
|
|
int iblock = Frame__iblock(self);
|
|
|
while(iblock >= 0) {
|
|
while(iblock >= 0) {
|
|
@@ -74,15 +73,16 @@ int Frame__prepare_jump_exception_handler(Frame* self, ValueStack* _s){
|
|
|
if(iblock < 0) return -1;
|
|
if(iblock < 0) return -1;
|
|
|
py_TValue obj = *--_s->sp; // pop exception object
|
|
py_TValue obj = *--_s->sp; // pop exception object
|
|
|
UnwindTarget* uw = Frame__find_unwind_target(self, iblock);
|
|
UnwindTarget* uw = Frame__find_unwind_target(self, iblock);
|
|
|
- _s->sp = (self->locals + uw->offset); // unwind the stack
|
|
|
|
|
- *(_s->sp++) = obj; // push it back
|
|
|
|
|
|
|
+ _s->sp = (self->locals + uw->offset); // unwind the stack
|
|
|
|
|
+ *(_s->sp++) = obj; // push it back
|
|
|
return c11__at(CodeBlock, &self->co->blocks, iblock)->end;
|
|
return c11__at(CodeBlock, &self->co->blocks, iblock)->end;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void Frame__prepare_jump_break(Frame* self, ValueStack* _s, int target){
|
|
|
|
|
|
|
+void Frame__prepare_jump_break(Frame* self, ValueStack* _s, int target) {
|
|
|
int iblock = Frame__iblock(self);
|
|
int iblock = Frame__iblock(self);
|
|
|
if(target >= self->co->codes.count) {
|
|
if(target >= self->co->codes.count) {
|
|
|
- while(iblock >= 0) iblock = Frame__exit_block(self, _s, iblock);
|
|
|
|
|
|
|
+ while(iblock >= 0)
|
|
|
|
|
+ iblock = Frame__exit_block(self, _s, iblock);
|
|
|
} else {
|
|
} else {
|
|
|
// BUG (solved)
|
|
// BUG (solved)
|
|
|
// for i in range(4):
|
|
// for i in range(4):
|
|
@@ -96,14 +96,14 @@ void Frame__prepare_jump_break(Frame* self, ValueStack* _s, int target){
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-int Frame__prepare_loop_break(Frame* self, ValueStack* _s){
|
|
|
|
|
|
|
+int Frame__prepare_loop_break(Frame* self, ValueStack* _s) {
|
|
|
int iblock = Frame__iblock(self);
|
|
int iblock = Frame__iblock(self);
|
|
|
int target = c11__getitem(CodeBlock, &self->co->blocks, iblock).end;
|
|
int target = c11__getitem(CodeBlock, &self->co->blocks, iblock).end;
|
|
|
Frame__prepare_jump_break(self, _s, target);
|
|
Frame__prepare_jump_break(self, _s, target);
|
|
|
return target;
|
|
return target;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-int Frame__exit_block(Frame* self, ValueStack* _s, int iblock){
|
|
|
|
|
|
|
+int Frame__exit_block(Frame* self, ValueStack* _s, int iblock) {
|
|
|
CodeBlock* block = c11__at(CodeBlock, &self->co->blocks, iblock);
|
|
CodeBlock* block = c11__at(CodeBlock, &self->co->blocks, iblock);
|
|
|
if(block->type == CodeBlockType_FOR_LOOP) {
|
|
if(block->type == CodeBlockType_FOR_LOOP) {
|
|
|
_s->sp--; // pop iterator
|
|
_s->sp--; // pop iterator
|
|
@@ -113,7 +113,7 @@ int Frame__exit_block(Frame* self, ValueStack* _s, int iblock){
|
|
|
return block->parent;
|
|
return block->parent;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-UnwindTarget* Frame__find_unwind_target(Frame* self, int iblock){
|
|
|
|
|
|
|
+UnwindTarget* Frame__find_unwind_target(Frame* self, int iblock) {
|
|
|
UnwindTarget* uw;
|
|
UnwindTarget* uw;
|
|
|
for(uw = self->uw_list; uw; uw = uw->next) {
|
|
for(uw = self->uw_list; uw; uw = uw->next) {
|
|
|
if(uw->iblock == iblock) return uw;
|
|
if(uw->iblock == iblock) return uw;
|
|
@@ -132,7 +132,7 @@ void Frame__set_unwind_target(Frame* self, py_TValue* sp) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-py_TValue* Frame__f_closure_try_get(Frame* self, py_Name name){
|
|
|
|
|
|
|
+py_TValue* Frame__f_closure_try_get(Frame* self, py_Name name) {
|
|
|
// if(self->function == NULL) return NULL;
|
|
// if(self->function == NULL) return NULL;
|
|
|
// pkpy::Function* fn = PyObject__as(pkpy::Function, self->function);
|
|
// pkpy::Function* fn = PyObject__as(pkpy::Function, self->function);
|
|
|
// if(fn->_closure == nullptr) return nullptr;
|
|
// if(fn->_closure == nullptr) return nullptr;
|