This commit is contained in:
blueloveTH 2024-06-15 19:55:06 +08:00
parent 0ffacbbbe8
commit 0e25f34135
8 changed files with 34 additions and 55 deletions

View File

@ -3,7 +3,6 @@
#include "pocketpy/objects/object.hpp" #include "pocketpy/objects/object.hpp"
#include "pocketpy/objects/dict.hpp" #include "pocketpy/objects/dict.hpp"
#include "pocketpy/objects/error.hpp" #include "pocketpy/objects/error.hpp"
#include "pocketpy/objects/stackmemory.hpp"
#include "pocketpy/objects/builtins.hpp" #include "pocketpy/objects/builtins.hpp"
#include "pocketpy/interpreter/gc.hpp" #include "pocketpy/interpreter/gc.hpp"
#include "pocketpy/interpreter/frame.hpp" #include "pocketpy/interpreter/frame.hpp"

View File

@ -86,9 +86,9 @@ struct CodeObject {
small_vector_2<StrName, 8> varnames; // local variables small_vector_2<StrName, 8> varnames; // local variables
int nlocals; // varnames.size() int nlocals; // varnames.size()
small_map<StrName, int> varnames_inv; c11_smallmap_uint16_t_int varnames_inv;
vector<CodeBlock> blocks; vector<CodeBlock> blocks;
small_map<StrName, int> labels; c11_smallmap_uint16_t_int labels;
vector<FuncDecl_> func_decls; vector<FuncDecl_> func_decls;
int start_line; int start_line;
@ -96,8 +96,20 @@ struct CodeObject {
const CodeBlock& _get_block_codei(int codei) const { return blocks[lines[codei].iblock]; } 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; 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 { enum class FuncType {

View File

@ -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

View File

@ -31,9 +31,9 @@ typedef c11_vector SMALLMAP;
void SMALLMAP_METHOD(ctor)(SMALLMAP* self); void SMALLMAP_METHOD(ctor)(SMALLMAP* self);
void SMALLMAP_METHOD(dtor)(SMALLMAP* self); void SMALLMAP_METHOD(dtor)(SMALLMAP* self);
void SMALLMAP_METHOD(set)(SMALLMAP* self, K key, V value); 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);
V SMALLMAP_METHOD(get)(SMALLMAP* self, K key, V default_value); V SMALLMAP_METHOD(get)(const SMALLMAP* self, K key, V default_value);
bool SMALLMAP_METHOD(contains)(SMALLMAP* self, K key); bool SMALLMAP_METHOD(contains)(const SMALLMAP* self, K key);
bool SMALLMAP_METHOD(del)(SMALLMAP* self, K key); bool SMALLMAP_METHOD(del)(SMALLMAP* self, K key);
void SMALLMAP_METHOD(clear)(SMALLMAP* self); 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; int index;
c11__lower_bound(KV, self->data, self->count, key, less, &index); c11__lower_bound(KV, self->data, self->count, key, less, &index);
KV* it = c11__at(KV, self, 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); V* p = SMALLMAP_METHOD(try_get)(self, key);
return p ? *p : default_value; 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; return SMALLMAP_METHOD(try_get)(self, key) != NULL;
} }

View File

@ -105,19 +105,20 @@ void CodeEmitContext::patch_jump(int index) noexcept{
} }
bool CodeEmitContext::add_label(StrName name) noexcept{ bool CodeEmitContext::add_label(StrName name) noexcept{
if(co->labels.contains(name)) return false; bool ok = c11_smallmap_uint16_t_int__contains(&co->labels, name.index);
co->labels.insert(name, co->codes.size()); if(ok) return false;
c11_smallmap_uint16_t_int__set(&co->labels, name.index, co->codes.size());
return true; return true;
} }
int CodeEmitContext::add_varname(StrName name) noexcept{ int CodeEmitContext::add_varname(StrName name) noexcept{
// PK_MAX_CO_VARNAMES will be checked when pop_context(), not here // 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; if(index >= 0) return index;
co->varnames.push_back(name); co->varnames.push_back(name);
co->nlocals++; co->nlocals++;
index = co->varnames.size() - 1; 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; return index;
} }
@ -163,7 +164,7 @@ void CodeEmitContext::emit_store_name(NameScope scope, StrName name, int line) n
} }
void NameExpr::emit_(CodeEmitContext* ctx) { 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) { if(scope == NAME_LOCAL && index >= 0) {
ctx->emit_(OP_LOAD_FAST, index, line); ctx->emit_(OP_LOAD_FAST, index, line);
} else { } else {

View File

@ -780,7 +780,7 @@ PyVar VM::__run_top_frame() {
case OP_JUMP_ABSOLUTE_TOP: DISPATCH_JUMP_ABSOLUTE(_CAST(int, POPX())) case OP_JUMP_ABSOLUTE_TOP: DISPATCH_JUMP_ABSOLUTE(_CAST(int, POPX()))
case OP_GOTO: { case OP_GOTO: {
StrName _name(byte.arg); 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")); if(target < 0) RuntimeError(_S("label ", _name.escape(), " not found"));
frame->prepare_jump_break(&s_data, target); frame->prepare_jump_break(&s_data, target);
DISPATCH_JUMP_ABSOLUTE(target) DISPATCH_JUMP_ABSOLUTE(target)

View File

@ -1,18 +1,19 @@
#include "pocketpy/objects/stackmemory.hpp"
#include "pocketpy/interpreter/frame.hpp" #include "pocketpy/interpreter/frame.hpp"
#include "pocketpy/common/smallmap.h"
namespace pkpy { namespace pkpy {
PyVar* FastLocals::try_get_name(StrName name) { 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; if(index == -1) return nullptr;
return &a[index]; return &a[index];
} }
NameDict_ FastLocals::to_namedict() { NameDict_ FastLocals::to_namedict() {
NameDict_ dict = std::make_shared<NameDict>(); NameDict_ dict = std::make_shared<NameDict>();
for(auto [name, index]: co->varnames_inv){ for(int i=0; i<co->varnames_inv.count; i++){
PyVar value = a[index]; auto entry = c11__getitem(c11_smallmap_entry_uint16_t_int, &co->varnames_inv, i);
if(value) dict->set(name, value); PyVar value = a[entry.value];
if(value) dict->set(StrName(entry.key), value);
} }
return dict; return dict;
} }
@ -43,13 +44,6 @@ int Frame::_exit_block(ValueStack* _s, int i) {
auto type = co->blocks[i].type; auto type = co->blocks[i].type;
if(type == CodeBlockType::FOR_LOOP) { if(type == CodeBlockType::FOR_LOOP) {
_s->pop(); // pop the iterator _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) { } else if(type == CodeBlockType::CONTEXT_MANAGER) {
_s->pop(); _s->pop();
} }

View File

@ -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