diff --git a/include/pocketpy/interpreter/vm.hpp b/include/pocketpy/interpreter/vm.hpp index b814a69d..79263eeb 100644 --- a/include/pocketpy/interpreter/vm.hpp +++ b/include/pocketpy/interpreter/vm.hpp @@ -3,7 +3,6 @@ #include "pocketpy/objects/object.hpp" #include "pocketpy/objects/dict.hpp" #include "pocketpy/objects/error.hpp" -#include "pocketpy/objects/stackmemory.hpp" #include "pocketpy/objects/builtins.hpp" #include "pocketpy/interpreter/gc.hpp" #include "pocketpy/interpreter/frame.hpp" diff --git a/include/pocketpy/objects/codeobject.hpp b/include/pocketpy/objects/codeobject.hpp index a6cf5eff..42939f44 100644 --- a/include/pocketpy/objects/codeobject.hpp +++ b/include/pocketpy/objects/codeobject.hpp @@ -86,9 +86,9 @@ struct CodeObject { small_vector_2 varnames; // local variables int nlocals; // varnames.size() - small_map varnames_inv; + c11_smallmap_uint16_t_int varnames_inv; vector blocks; - small_map labels; + c11_smallmap_uint16_t_int labels; vector func_decls; int start_line; @@ -96,8 +96,20 @@ struct CodeObject { const CodeBlock& _get_block_codei(int codei) const { return blocks[lines[codei].iblock]; } - CodeObject(std::shared_ptr src, const Str& name); void _gc_mark(VM*) const; + + CodeObject(std::shared_ptr src, const Str& name) : + src(src), name(name), nlocals(0), start_line(-1), end_line(-1) { + blocks.push_back(CodeBlock(CodeBlockType::NO_BLOCK, -1, 0)); + + c11_smallmap_uint16_t_int__ctor(&varnames_inv); + c11_smallmap_uint16_t_int__ctor(&labels); + } + + ~CodeObject() { + c11_smallmap_uint16_t_int__dtor(&varnames_inv); + c11_smallmap_uint16_t_int__dtor(&labels); + } }; enum class FuncType { diff --git a/include/pocketpy/objects/stackmemory.hpp b/include/pocketpy/objects/stackmemory.hpp deleted file mode 100644 index c68c1991..00000000 --- a/include/pocketpy/objects/stackmemory.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include "pocketpy/common/traits.hpp" - -namespace pkpy { - -struct StackMemory { - int count; - - StackMemory(int count) : count(count) {} -}; - -template <> -constexpr inline bool is_sso_v = true; - -const inline int kTpStackMemoryIndex = 27; - -} // namespace pkpy diff --git a/include/pocketpy/xmacros/smallmap.h b/include/pocketpy/xmacros/smallmap.h index 75475c37..a522c109 100644 --- a/include/pocketpy/xmacros/smallmap.h +++ b/include/pocketpy/xmacros/smallmap.h @@ -31,9 +31,9 @@ typedef c11_vector SMALLMAP; void SMALLMAP_METHOD(ctor)(SMALLMAP* self); void SMALLMAP_METHOD(dtor)(SMALLMAP* self); void SMALLMAP_METHOD(set)(SMALLMAP* self, K key, V value); -V* SMALLMAP_METHOD(try_get)(SMALLMAP* self, K key); -V SMALLMAP_METHOD(get)(SMALLMAP* self, K key, V default_value); -bool SMALLMAP_METHOD(contains)(SMALLMAP* self, K key); +V* SMALLMAP_METHOD(try_get)(const SMALLMAP* self, K key); +V SMALLMAP_METHOD(get)(const SMALLMAP* self, K key, V default_value); +bool SMALLMAP_METHOD(contains)(const SMALLMAP* self, K key); bool SMALLMAP_METHOD(del)(SMALLMAP* self, K key); void SMALLMAP_METHOD(clear)(SMALLMAP* self); @@ -60,7 +60,7 @@ void SMALLMAP_METHOD(set)(SMALLMAP* self, K key, V value) { } } -V* SMALLMAP_METHOD(try_get)(SMALLMAP* self, K key) { +V* SMALLMAP_METHOD(try_get)(const SMALLMAP* self, K key) { int index; c11__lower_bound(KV, self->data, self->count, key, less, &index); KV* it = c11__at(KV, self, index); @@ -71,12 +71,12 @@ V* SMALLMAP_METHOD(try_get)(SMALLMAP* self, K key) { } } -V SMALLMAP_METHOD(get)(SMALLMAP* self, K key, V default_value) { +V SMALLMAP_METHOD(get)(const SMALLMAP* self, K key, V default_value) { V* p = SMALLMAP_METHOD(try_get)(self, key); return p ? *p : default_value; } -bool SMALLMAP_METHOD(contains)(SMALLMAP* self, K key) { +bool SMALLMAP_METHOD(contains)(const SMALLMAP* self, K key) { return SMALLMAP_METHOD(try_get)(self, key) != NULL; } diff --git a/src/compiler/expr.cpp b/src/compiler/expr.cpp index 9327ea1a..98780bb0 100644 --- a/src/compiler/expr.cpp +++ b/src/compiler/expr.cpp @@ -105,19 +105,20 @@ void CodeEmitContext::patch_jump(int index) noexcept{ } bool CodeEmitContext::add_label(StrName name) noexcept{ - if(co->labels.contains(name)) return false; - co->labels.insert(name, co->codes.size()); + bool ok = c11_smallmap_uint16_t_int__contains(&co->labels, name.index); + if(ok) return false; + c11_smallmap_uint16_t_int__set(&co->labels, name.index, co->codes.size()); return true; } int CodeEmitContext::add_varname(StrName name) noexcept{ // PK_MAX_CO_VARNAMES will be checked when pop_context(), not here - int index = co->varnames_inv.get(name, -1); + int index = c11_smallmap_uint16_t_int__get(&co->varnames_inv, name.index, -1); if(index >= 0) return index; co->varnames.push_back(name); co->nlocals++; index = co->varnames.size() - 1; - co->varnames_inv.insert(name, index); + c11_smallmap_uint16_t_int__set(&co->varnames_inv, name.index, index); return index; } @@ -163,7 +164,7 @@ void CodeEmitContext::emit_store_name(NameScope scope, StrName name, int line) n } void NameExpr::emit_(CodeEmitContext* ctx) { - int index = ctx->co->varnames_inv.get(name, -1); + int index = c11_smallmap_uint16_t_int__get(&ctx->co->varnames_inv, name.index, -1); if(scope == NAME_LOCAL && index >= 0) { ctx->emit_(OP_LOAD_FAST, index, line); } else { diff --git a/src/interpreter/ceval.cpp b/src/interpreter/ceval.cpp index cc040851..51f29ca3 100644 --- a/src/interpreter/ceval.cpp +++ b/src/interpreter/ceval.cpp @@ -780,7 +780,7 @@ PyVar VM::__run_top_frame() { case OP_JUMP_ABSOLUTE_TOP: DISPATCH_JUMP_ABSOLUTE(_CAST(int, POPX())) case OP_GOTO: { StrName _name(byte.arg); - int target = frame->co->labels.get(_name, -1); + int target = c11_smallmap_uint16_t_int__get(&frame->co->labels, byte.arg, -1); if(target < 0) RuntimeError(_S("label ", _name.escape(), " not found")); frame->prepare_jump_break(&s_data, target); DISPATCH_JUMP_ABSOLUTE(target) diff --git a/src/interpreter/frame.cpp b/src/interpreter/frame.cpp index d46e5391..3fcbb4b5 100644 --- a/src/interpreter/frame.cpp +++ b/src/interpreter/frame.cpp @@ -1,18 +1,19 @@ -#include "pocketpy/objects/stackmemory.hpp" #include "pocketpy/interpreter/frame.hpp" +#include "pocketpy/common/smallmap.h" namespace pkpy { PyVar* FastLocals::try_get_name(StrName name) { - int index = co->varnames_inv.get(name, -1); + int index = c11_smallmap_uint16_t_int__get(&co->varnames_inv, name.index, -1); if(index == -1) return nullptr; return &a[index]; } NameDict_ FastLocals::to_namedict() { NameDict_ dict = std::make_shared(); - for(auto [name, index]: co->varnames_inv){ - PyVar value = a[index]; - if(value) dict->set(name, value); + for(int i=0; ivarnames_inv.count; i++){ + auto entry = c11__getitem(c11_smallmap_entry_uint16_t_int, &co->varnames_inv, i); + PyVar value = a[entry.value]; + if(value) dict->set(StrName(entry.key), value); } return dict; } @@ -43,13 +44,6 @@ int Frame::_exit_block(ValueStack* _s, int i) { auto type = co->blocks[i].type; if(type == CodeBlockType::FOR_LOOP) { _s->pop(); // pop the iterator - // pop possible stack memory slots - if(_s->top().type == kTpStackMemoryIndex) { - int count = _s->top().as().count; - assert(count < 0); - _s->_sp += count; - _s->_sp -= 2; // pop header and tail - } } else if(type == CodeBlockType::CONTEXT_MANAGER) { _s->pop(); } diff --git a/src/objects/codeobject.cpp b/src/objects/codeobject.cpp deleted file mode 100644 index 28087330..00000000 --- a/src/objects/codeobject.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "pocketpy/objects/codeobject.hpp" - -namespace pkpy { - -CodeObject::CodeObject(std::shared_ptr src, const Str& name) : - src(src), name(name), nlocals(0), start_line(-1), end_line(-1) { - blocks.push_back(CodeBlock(CodeBlockType::NO_BLOCK, -1, 0)); -} -} // namespace pkpy