From ea269686b5d92889ccb7dad060a4ede7d11252d3 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 16 Jun 2024 22:17:36 +0800 Subject: [PATCH] remove vm --- include/pocketpy/objects/base.h | 6 ++-- include/pocketpy/objects/dict.h | 10 +++--- include/pocketpy/objects/dict.hpp | 10 +++--- src/objects/dict.c | 56 +++++++++++++++---------------- src/objects/pyvar.cpp | 17 ++++++---- 5 files changed, 53 insertions(+), 46 deletions(-) diff --git a/include/pocketpy/objects/base.h b/include/pocketpy/objects/base.h index 7c6607f1..ac4e1055 100644 --- a/include/pocketpy/objects/base.h +++ b/include/pocketpy/objects/base.h @@ -74,11 +74,13 @@ PK_INLINE bool PyVar__IS_OP(const PyVar* a, const PyVar* b){ #define pkpy_Var__is_null(self) ((self)->type == 0) #define pkpy_Var__set_null(self) do { (self)->type = 0; } while(0) -bool pkpy_Var__eq__(void *vm, PyVar a, PyVar b); -int64_t pkpy_Var__hash__(void *vm, PyVar a); extern PyVar pkpy_NULL, pkpy_OP_CALL, pkpy_OP_YIELD; +bool py_eq(const PyVar*, const PyVar*); +bool py_le(const PyVar*, const PyVar*); +int64_t py_hash(const PyVar*); + #ifdef __cplusplus } #endif diff --git a/include/pocketpy/objects/dict.h b/include/pocketpy/objects/dict.h index 03e7a2e1..fc8f9483 100644 --- a/include/pocketpy/objects/dict.h +++ b/include/pocketpy/objects/dict.h @@ -49,7 +49,7 @@ pkpy_Dict pkpy_Dict__copy(const pkpy_Dict* self); * @param val value to set * @return `true` if the key is newly added, `false` if the key already exists */ -bool pkpy_Dict__set(pkpy_Dict* self, void* vm, PyVar key, PyVar val); +bool pkpy_Dict__set(pkpy_Dict* self, PyVar key, PyVar val); /** * @brief Check if a key exists in the `pkpy_Dict` @@ -58,7 +58,7 @@ bool pkpy_Dict__set(pkpy_Dict* self, void* vm, PyVar key, PyVar val); * @param key key to check * @return `true` if the key exists, `false` otherwise */ -bool pkpy_Dict__contains(const pkpy_Dict* self, void* vm, PyVar key); +bool pkpy_Dict__contains(const pkpy_Dict* self, PyVar key); /** * @brief Remove a key from the `pkpy_Dict` @@ -67,7 +67,7 @@ bool pkpy_Dict__contains(const pkpy_Dict* self, void* vm, PyVar key); * @param key key to remove * @return `true` if the key was found and removed, `false` if the key doesn't exist */ -bool pkpy_Dict__del(pkpy_Dict* self, void* vm, PyVar key); +bool pkpy_Dict__del(pkpy_Dict* self, PyVar key); /** * @brief Try to get a value from the `pkpy_Dict` @@ -76,7 +76,7 @@ bool pkpy_Dict__del(pkpy_Dict* self, void* vm, PyVar key); * @param key key to get * @return the value associated with the key, `NULL` if the key doesn't exist */ -const PyVar* pkpy_Dict__try_get(const pkpy_Dict* self, void* vm, PyVar key); +const PyVar* pkpy_Dict__try_get(const pkpy_Dict* self, PyVar key); /** * @brief Update the `pkpy_Dict` with another one @@ -84,7 +84,7 @@ const PyVar* pkpy_Dict__try_get(const pkpy_Dict* self, void* vm, PyVar key); * @param vm __eq__ and __hash__ context * @param other `pkpy_Dict` instance to update with */ -void pkpy_Dict__update(pkpy_Dict* self, void *vm, const pkpy_Dict* other); +void pkpy_Dict__update(pkpy_Dict* self, const pkpy_Dict* other); /** * @brief Clear the `pkpy_Dict` diff --git a/include/pocketpy/objects/dict.hpp b/include/pocketpy/objects/dict.hpp index de4e212e..98b8b748 100644 --- a/include/pocketpy/objects/dict.hpp +++ b/include/pocketpy/objects/dict.hpp @@ -28,25 +28,25 @@ struct Dict : private pkpy_Dict { int size() const { return count; } void set(VM* vm, PyVar key, PyVar val) { - pkpy_Dict__set(this, vm, *(::PyVar*)(&key), *(::PyVar*)(&val)); + pkpy_Dict__set(this, *(::PyVar*)(&key), *(::PyVar*)(&val)); } PyVar try_get(VM* vm, PyVar key) const { - auto res = pkpy_Dict__try_get(this, vm, *(::PyVar*)(&key)); + auto res = pkpy_Dict__try_get(this, *(::PyVar*)(&key)); if (!res) return nullptr; return *(const PyVar*)(res); } bool contains(VM* vm, PyVar key) const { - return pkpy_Dict__contains(this, vm, *(::PyVar*)(&key)); + return pkpy_Dict__contains(this, *(::PyVar*)(&key)); } bool del(VM* vm, PyVar key) { - return pkpy_Dict__del(this, vm, *(::PyVar*)(&key)); + return pkpy_Dict__del(this, *(::PyVar*)(&key)); } void update(VM* vm, const Dict& other) { - pkpy_Dict__update(this, vm, &other); + pkpy_Dict__update(this, &other); } template diff --git a/src/objects/dict.c b/src/objects/dict.c index 64c401ae..37b10a69 100644 --- a/src/objects/dict.c +++ b/src/objects/dict.c @@ -73,7 +73,7 @@ static void pkpy_Dict__htset(pkpy_Dict* self, int h, int v) { #endif } -static int pkpy_Dict__probe0(const pkpy_Dict* self, void* vm, PyVar key, int hash) { +static int pkpy_Dict__probe0(const pkpy_Dict* self, PyVar key, int hash) { const int null = pkpy_Dict__idx_null(self); const int mask = self->_htcap - 1; for(int h = hash & mask;; h = DICT_HASH_NEXT(h) & mask) { @@ -86,7 +86,7 @@ static int pkpy_Dict__probe0(const pkpy_Dict* self, void* vm, PyVar key, int has PK_UNREACHABLE(); } -static int pkpy_Dict__probe1(const pkpy_Dict* self, void* vm, PyVar key, int hash) { +static int pkpy_Dict__probe1(const pkpy_Dict* self, PyVar key, int hash) { const int null = pkpy_Dict__idx_null(self); const int mask = self->_htcap - 1; for(int h = hash & mask;; h = DICT_HASH_NEXT(h) & mask) { @@ -95,12 +95,12 @@ static int pkpy_Dict__probe1(const pkpy_Dict* self, void* vm, PyVar key, int has struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); if(pkpy_Var__is_null(&entry->key)) continue; - if(pkpy_Var__eq__(vm, entry->key, key)) return h; + if(py_eq(&entry->key, &key)) return h; } PK_UNREACHABLE(); } -static void pkpy_Dict__extendht(pkpy_Dict* self, void* vm) { +static void pkpy_Dict__extendht(pkpy_Dict* self) { free(self->_hashtable); self->_htcap *= 2; self->_hashtable = malloc(pkpy_Dict__ht_byte_size(self)); @@ -110,15 +110,15 @@ static void pkpy_Dict__extendht(pkpy_Dict* self, void* vm) { struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i); if(pkpy_Var__is_null(&entry->key)) continue; - int rhash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, entry->key)); - int h = pkpy_Dict__probe0(self, vm, entry->key, rhash); + int rhash = DICT_HASH_TRANS(py_hash(&entry->key)); + int h = pkpy_Dict__probe0(self, entry->key, rhash); pkpy_Dict__htset(self, h, i); } } -bool pkpy_Dict__set(pkpy_Dict* self, void* vm, PyVar key, PyVar val) { - int hash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, key)); - int h = pkpy_Dict__probe1(self, vm, key, hash); +bool pkpy_Dict__set(pkpy_Dict* self, PyVar key, PyVar val) { + int hash = DICT_HASH_TRANS(py_hash(&key)); + int h = pkpy_Dict__probe1(self, key, hash); int idx = pkpy_Dict__htget(self, h); if(idx == pkpy_Dict__idx_null(self)) { @@ -129,20 +129,20 @@ bool pkpy_Dict__set(pkpy_Dict* self, void* vm, PyVar key, PyVar val) { .key = key, .val = val, })); - h = pkpy_Dict__probe0(self, vm, key, hash); + h = pkpy_Dict__probe0(self, key, hash); pkpy_Dict__htset(self, h, idx); self->count += 1; - if(self->count >= self->_htcap * DICT_MAX_LOAD) pkpy_Dict__extendht(self, vm); + if(self->count >= self->_htcap * DICT_MAX_LOAD) pkpy_Dict__extendht(self); return true; } struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); - if(pkpy_Var__eq__(vm, entry->key, key)) { + if(py_eq(&entry->key, &key)) { entry->val = val; } else { self->count += 1; - h = pkpy_Dict__probe0(self, vm, key, hash); + h = pkpy_Dict__probe0(self, key, hash); idx = pkpy_Dict__htget(self, h); struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); entry->key = key; @@ -151,16 +151,16 @@ bool pkpy_Dict__set(pkpy_Dict* self, void* vm, PyVar key, PyVar val) { return false; } -bool pkpy_Dict__contains(const pkpy_Dict* self, void* vm, PyVar key) { - int hash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, key)); - int h = pkpy_Dict__probe1(self, vm, key, hash); +bool pkpy_Dict__contains(const pkpy_Dict* self, PyVar key) { + int hash = DICT_HASH_TRANS(py_hash(&key)); + int h = pkpy_Dict__probe1(self, key, hash); int idx = pkpy_Dict__htget(self, h); if(idx == pkpy_Dict__idx_null(self)) return false; return true; } -static bool pkpy_Dict__refactor(pkpy_Dict* self, void* vm) { +static bool pkpy_Dict__refactor(pkpy_Dict* self) { int deleted_slots = self->_entries.count - self->count; if(deleted_slots <= 8 || deleted_slots < self->_entries.count * (1 - DICT_MAX_LOAD)) return false; @@ -184,29 +184,29 @@ static bool pkpy_Dict__refactor(pkpy_Dict* self, void* vm) { struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i); if(pkpy_Var__is_null(&entry->key)) continue; - int rhash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, entry->key)); - int h = pkpy_Dict__probe0(self, vm, entry->key, rhash); + int rhash = DICT_HASH_TRANS(py_hash(&entry->key)); + int h = pkpy_Dict__probe0(self, entry->key, rhash); pkpy_Dict__htset(self, h, i); } return true; } -bool pkpy_Dict__del(pkpy_Dict* self, void* vm, PyVar key) { - int hash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, key)); - int h = pkpy_Dict__probe1(self, vm, key, hash); +bool pkpy_Dict__del(pkpy_Dict* self, PyVar key) { + int hash = DICT_HASH_TRANS(py_hash(&key)); + int h = pkpy_Dict__probe1(self, key, hash); int idx = pkpy_Dict__htget(self, h), null = pkpy_Dict__idx_null(self); if(idx == null) return false; struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); pkpy_Var__set_null(&entry->key); self->count -= 1; - pkpy_Dict__refactor(self, vm); + pkpy_Dict__refactor(self); return true; } -const PyVar *pkpy_Dict__try_get(const pkpy_Dict* self, void* vm, PyVar key) { - int hash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, key)); - int h = pkpy_Dict__probe1(self, vm, key, hash); +const PyVar *pkpy_Dict__try_get(const pkpy_Dict* self, PyVar key) { + int hash = DICT_HASH_TRANS(py_hash(&key)); + int h = pkpy_Dict__probe1(self, key, hash); int idx = pkpy_Dict__htget(self, h); if(idx == pkpy_Dict__idx_null(self)) return NULL; @@ -215,11 +215,11 @@ const PyVar *pkpy_Dict__try_get(const pkpy_Dict* self, void* vm, PyVar key) { return &entry->val; } -void pkpy_Dict__update(pkpy_Dict *self, void *vm, const pkpy_Dict *other) { +void pkpy_Dict__update(pkpy_Dict *self, const pkpy_Dict *other) { for(int i = 0; i < other->_entries.count; i++) { struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &other->_entries, i); if(pkpy_Var__is_null(&entry->key)) continue; - pkpy_Dict__set(self, vm, entry->key, entry->val); + pkpy_Dict__set(self, entry->key, entry->val); } } diff --git a/src/objects/pyvar.cpp b/src/objects/pyvar.cpp index 3667d851..982d45c4 100644 --- a/src/objects/pyvar.cpp +++ b/src/objects/pyvar.cpp @@ -3,14 +3,19 @@ extern "C" { -bool pkpy_Var__eq__(void* vm_, PyVar a, PyVar b) { - auto vm = (pkpy::VM*)(vm_); - return vm->py_eq(*(PyVar*)(&a), *(PyVar*)(&b)); +bool py_eq(const PyVar* a, const PyVar* b){ + auto vm = (pkpy::VM*)pkpy_g.vm; + return vm->py_eq(*a, *b); } -int64_t pkpy_Var__hash__(void* vm_, PyVar a) { - auto vm = (pkpy::VM*)(vm_); - return vm->py_hash(*(PyVar*)(&a)); +bool py_le(const PyVar* a, const PyVar* b){ + auto vm = (pkpy::VM*)pkpy_g.vm; + return vm->py_le(*a, *b); +} + +int64_t py_hash(const PyVar* a){ + auto vm = (pkpy::VM*)pkpy_g.vm; + return vm->py_hash(*a); } }