blueloveTH пре 1 година
родитељ
комит
0e25f34135

+ 0 - 1
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"

+ 15 - 3
include/pocketpy/objects/codeobject.hpp

@@ -86,9 +86,9 @@ struct CodeObject {
     small_vector_2<StrName, 8> varnames;  // local variables
     int nlocals;                          // varnames.size()
 
-    small_map<StrName, int> varnames_inv;
+    c11_smallmap_uint16_t_int varnames_inv;
     vector<CodeBlock> blocks;
-    small_map<StrName, int> labels;
+    c11_smallmap_uint16_t_int labels;
     vector<FuncDecl_> 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<SourceData> src, const Str& name);
     void _gc_mark(VM*) const;
+
+    CodeObject(std::shared_ptr<SourceData> 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 {

+ 0 - 18
include/pocketpy/objects/stackmemory.hpp

@@ -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<StackMemory> = true;
-
-const inline int kTpStackMemoryIndex = 27;
-
-}  // namespace pkpy

+ 6 - 6
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;
 }
 

+ 6 - 5
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 {

+ 1 - 1
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)

+ 6 - 12
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<NameDict>();
-    for(auto [name, index]: co->varnames_inv){
-        PyVar value = a[index];
-        if(value) dict->set(name, value);
+    for(int i=0; i<co->varnames_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<StackMemory>().count;
-            assert(count < 0);
-            _s->_sp += count;
-            _s->_sp -= 2;  // pop header and tail
-        }
     } else if(type == CodeBlockType::CONTEXT_MANAGER) {
         _s->pop();
     }

+ 0 - 9
src/objects/codeobject.cpp

@@ -1,9 +0,0 @@
-#include "pocketpy/objects/codeobject.hpp"
-
-namespace pkpy {
-
-CodeObject::CodeObject(std::shared_ptr<SourceData> 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