From 59afaf82634358a088d6c675e2eb984928a9150e Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 15 Jun 2024 20:19:01 +0800 Subject: [PATCH] some fix --- include/pocketpy/common/smallmap.h | 10 ++++++++++ include/pocketpy/objects/codeobject.hpp | 20 ++++++++++---------- include/pocketpy/xmacros/smallmap.h | 6 ++++-- src/common/smallmap.c | 9 +++++++++ src/compiler/expr.cpp | 10 +++++----- src/interpreter/ceval.cpp | 2 +- src/interpreter/frame.cpp | 4 ++-- src/interpreter/vm.cpp | 2 +- 8 files changed, 42 insertions(+), 21 deletions(-) diff --git a/include/pocketpy/common/smallmap.h b/include/pocketpy/common/smallmap.h index 5ca6d38b..43002cc5 100644 --- a/include/pocketpy/common/smallmap.h +++ b/include/pocketpy/common/smallmap.h @@ -1,3 +1,5 @@ +#pragma once + #include "pocketpy/common/vector.h" #include @@ -8,10 +10,18 @@ extern "C" { #define SMALLMAP_T__HEADER #define K uint16_t #define V int +#define TAG n2i #include "pocketpy/xmacros/smallmap.h" #undef SMALLMAP_T__HEADER +#define SMALLMAP_T__HEADER +#define K const char* +#define V uint16_t +#define TAG s2n +#include "pocketpy/xmacros/smallmap.h" +#undef SMALLMAP_T__HEADER + #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/include/pocketpy/objects/codeobject.hpp b/include/pocketpy/objects/codeobject.hpp index 42939f44..3e8ce800 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() - c11_smallmap_uint16_t_int varnames_inv; + c11_smallmap_n2i varnames_inv; vector blocks; - c11_smallmap_uint16_t_int labels; + c11_smallmap_n2i labels; vector func_decls; int start_line; @@ -102,13 +102,13 @@ struct CodeObject { 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); + c11_smallmap_n2i__ctor(&varnames_inv); + c11_smallmap_n2i__ctor(&labels); } ~CodeObject() { - c11_smallmap_uint16_t_int__dtor(&varnames_inv); - c11_smallmap_uint16_t_int__dtor(&labels); + c11_smallmap_n2i__dtor(&varnames_inv); + c11_smallmap_n2i__dtor(&labels); } }; @@ -139,21 +139,21 @@ struct FuncDecl { const char* docstring; // docstring of this function (weak ref) FuncType type = FuncType::UNSET; - c11_smallmap_uint16_t_int kw_to_index; + c11_smallmap_n2i kw_to_index; void add_kwarg(int index, StrName key, PyVar value) { - c11_smallmap_uint16_t_int__set(&kw_to_index, key.index, index); + c11_smallmap_n2i__set(&kw_to_index, key.index, index); kwargs.push_back(KwArg{index, key, value}); } void _gc_mark(VM*) const; FuncDecl(){ - c11_smallmap_uint16_t_int__ctor(&kw_to_index); + c11_smallmap_n2i__ctor(&kw_to_index); } ~FuncDecl(){ - c11_smallmap_uint16_t_int__dtor(&kw_to_index); + c11_smallmap_n2i__dtor(&kw_to_index); } }; diff --git a/include/pocketpy/xmacros/smallmap.h b/include/pocketpy/xmacros/smallmap.h index a522c109..e0819549 100644 --- a/include/pocketpy/xmacros/smallmap.h +++ b/include/pocketpy/xmacros/smallmap.h @@ -6,6 +6,7 @@ /* Input */ #define K int #define V float + #define TAG int_float #endif /* Optional Input */ @@ -17,8 +18,8 @@ #define CONCAT(A, B) CONCAT_(A, B) #define CONCAT_(A, B) A##B -#define KV CONCAT(CONCAT(c11_smallmap_entry_, K), CONCAT(_, V)) -#define SMALLMAP CONCAT(CONCAT(c11_smallmap_, K), CONCAT(_, V)) +#define KV CONCAT(c11_smallmap_entry_, TAG) +#define SMALLMAP CONCAT(c11_smallmap_, TAG) #define SMALLMAP_METHOD(name) CONCAT(SMALLMAP, CONCAT(__, name)) typedef struct { @@ -105,4 +106,5 @@ void SMALLMAP_METHOD(clear)(SMALLMAP* self) { #undef K #undef V +#undef TAG #undef less diff --git a/src/common/smallmap.c b/src/common/smallmap.c index 19b340f0..d8a01b84 100644 --- a/src/common/smallmap.c +++ b/src/common/smallmap.c @@ -4,5 +4,14 @@ #define SMALLMAP_T__SOURCE #define K uint16_t #define V int +#define TAG n2i +#include "pocketpy/xmacros/smallmap.h" +#undef SMALLMAP_T__SOURCE + + +#define SMALLMAP_T__SOURCE +#define K const char* +#define V uint16_t +#define TAG s2n #include "pocketpy/xmacros/smallmap.h" #undef SMALLMAP_T__SOURCE diff --git a/src/compiler/expr.cpp b/src/compiler/expr.cpp index 98780bb0..ebccda71 100644 --- a/src/compiler/expr.cpp +++ b/src/compiler/expr.cpp @@ -105,20 +105,20 @@ void CodeEmitContext::patch_jump(int index) noexcept{ } bool CodeEmitContext::add_label(StrName name) noexcept{ - bool ok = c11_smallmap_uint16_t_int__contains(&co->labels, name.index); + bool ok = c11_smallmap_n2i__contains(&co->labels, name.index); if(ok) return false; - c11_smallmap_uint16_t_int__set(&co->labels, name.index, co->codes.size()); + c11_smallmap_n2i__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 = c11_smallmap_uint16_t_int__get(&co->varnames_inv, name.index, -1); + int index = c11_smallmap_n2i__get(&co->varnames_inv, name.index, -1); if(index >= 0) return index; co->varnames.push_back(name); co->nlocals++; index = co->varnames.size() - 1; - c11_smallmap_uint16_t_int__set(&co->varnames_inv, name.index, index); + c11_smallmap_n2i__set(&co->varnames_inv, name.index, index); return index; } @@ -164,7 +164,7 @@ void CodeEmitContext::emit_store_name(NameScope scope, StrName name, int line) n } void NameExpr::emit_(CodeEmitContext* ctx) { - int index = c11_smallmap_uint16_t_int__get(&ctx->co->varnames_inv, name.index, -1); + int index = c11_smallmap_n2i__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 51f29ca3..7a1ff9d6 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 = c11_smallmap_uint16_t_int__get(&frame->co->labels, byte.arg, -1); + int target = c11_smallmap_n2i__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 3fcbb4b5..71178de6 100644 --- a/src/interpreter/frame.cpp +++ b/src/interpreter/frame.cpp @@ -3,7 +3,7 @@ namespace pkpy { PyVar* FastLocals::try_get_name(StrName name) { - int index = c11_smallmap_uint16_t_int__get(&co->varnames_inv, name.index, -1); + int index = c11_smallmap_n2i__get(&co->varnames_inv, name.index, -1); if(index == -1) return nullptr; return &a[index]; } @@ -11,7 +11,7 @@ PyVar* FastLocals::try_get_name(StrName name) { NameDict_ FastLocals::to_namedict() { NameDict_ dict = std::make_shared(); for(int i=0; ivarnames_inv.count; i++){ - auto entry = c11__getitem(c11_smallmap_entry_uint16_t_int, &co->varnames_inv, i); + auto entry = c11__getitem(c11_smallmap_entry_n2i, &co->varnames_inv, i); PyVar value = a[entry.value]; if(value) dict->set(StrName(entry.key), value); } diff --git a/src/interpreter/vm.cpp b/src/interpreter/vm.cpp index 6cd04d61..56b24dde 100644 --- a/src/interpreter/vm.cpp +++ b/src/interpreter/vm.cpp @@ -1009,7 +1009,7 @@ void VM::__prepare_py_call(PyVar* buffer, ArgsView args, ArgsView kwargs, const for(int j = 0; j < kwargs.size(); j += 2) { StrName key(_CAST(uint16_t, kwargs[j])); - int index = c11_smallmap_uint16_t_int__get(&decl->kw_to_index, key.index, -1); + int index = c11_smallmap_n2i__get(&decl->kw_to_index, key.index, -1); // if key is an explicit key, set as local variable if(index >= 0) { buffer[index] = kwargs[j + 1];