This commit is contained in:
blueloveTH 2024-06-15 20:19:01 +08:00
parent 0e25f34135
commit 59afaf8263
8 changed files with 42 additions and 21 deletions

View File

@ -1,3 +1,5 @@
#pragma once
#include "pocketpy/common/vector.h" #include "pocketpy/common/vector.h"
#include <stdint.h> #include <stdint.h>
@ -8,10 +10,18 @@ extern "C" {
#define SMALLMAP_T__HEADER #define SMALLMAP_T__HEADER
#define K uint16_t #define K uint16_t
#define V int #define V int
#define TAG n2i
#include "pocketpy/xmacros/smallmap.h" #include "pocketpy/xmacros/smallmap.h"
#undef SMALLMAP_T__HEADER #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 #ifdef __cplusplus
} }
#endif #endif

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()
c11_smallmap_uint16_t_int varnames_inv; c11_smallmap_n2i varnames_inv;
vector<CodeBlock> blocks; vector<CodeBlock> blocks;
c11_smallmap_uint16_t_int labels; c11_smallmap_n2i labels;
vector<FuncDecl_> func_decls; vector<FuncDecl_> func_decls;
int start_line; int start_line;
@ -102,13 +102,13 @@ struct CodeObject {
src(src), name(name), nlocals(0), start_line(-1), end_line(-1) { src(src), name(name), nlocals(0), start_line(-1), end_line(-1) {
blocks.push_back(CodeBlock(CodeBlockType::NO_BLOCK, -1, 0)); blocks.push_back(CodeBlock(CodeBlockType::NO_BLOCK, -1, 0));
c11_smallmap_uint16_t_int__ctor(&varnames_inv); c11_smallmap_n2i__ctor(&varnames_inv);
c11_smallmap_uint16_t_int__ctor(&labels); c11_smallmap_n2i__ctor(&labels);
} }
~CodeObject() { ~CodeObject() {
c11_smallmap_uint16_t_int__dtor(&varnames_inv); c11_smallmap_n2i__dtor(&varnames_inv);
c11_smallmap_uint16_t_int__dtor(&labels); c11_smallmap_n2i__dtor(&labels);
} }
}; };
@ -139,21 +139,21 @@ struct FuncDecl {
const char* docstring; // docstring of this function (weak ref) const char* docstring; // docstring of this function (weak ref)
FuncType type = FuncType::UNSET; 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) { 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}); kwargs.push_back(KwArg{index, key, value});
} }
void _gc_mark(VM*) const; void _gc_mark(VM*) const;
FuncDecl(){ FuncDecl(){
c11_smallmap_uint16_t_int__ctor(&kw_to_index); c11_smallmap_n2i__ctor(&kw_to_index);
} }
~FuncDecl(){ ~FuncDecl(){
c11_smallmap_uint16_t_int__dtor(&kw_to_index); c11_smallmap_n2i__dtor(&kw_to_index);
} }
}; };

View File

@ -6,6 +6,7 @@
/* Input */ /* Input */
#define K int #define K int
#define V float #define V float
#define TAG int_float
#endif #endif
/* Optional Input */ /* Optional Input */
@ -17,8 +18,8 @@
#define CONCAT(A, B) CONCAT_(A, B) #define CONCAT(A, B) CONCAT_(A, B)
#define CONCAT_(A, B) A##B #define CONCAT_(A, B) A##B
#define KV CONCAT(CONCAT(c11_smallmap_entry_, K), CONCAT(_, V)) #define KV CONCAT(c11_smallmap_entry_, TAG)
#define SMALLMAP CONCAT(CONCAT(c11_smallmap_, K), CONCAT(_, V)) #define SMALLMAP CONCAT(c11_smallmap_, TAG)
#define SMALLMAP_METHOD(name) CONCAT(SMALLMAP, CONCAT(__, name)) #define SMALLMAP_METHOD(name) CONCAT(SMALLMAP, CONCAT(__, name))
typedef struct { typedef struct {
@ -105,4 +106,5 @@ void SMALLMAP_METHOD(clear)(SMALLMAP* self) {
#undef K #undef K
#undef V #undef V
#undef TAG
#undef less #undef less

View File

@ -4,5 +4,14 @@
#define SMALLMAP_T__SOURCE #define SMALLMAP_T__SOURCE
#define K uint16_t #define K uint16_t
#define V int #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" #include "pocketpy/xmacros/smallmap.h"
#undef SMALLMAP_T__SOURCE #undef SMALLMAP_T__SOURCE

View File

@ -105,20 +105,20 @@ void CodeEmitContext::patch_jump(int index) noexcept{
} }
bool CodeEmitContext::add_label(StrName name) 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; 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; 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 = 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; 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;
c11_smallmap_uint16_t_int__set(&co->varnames_inv, name.index, index); c11_smallmap_n2i__set(&co->varnames_inv, name.index, index);
return index; return index;
} }
@ -164,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 = 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) { 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 = 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")); 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

@ -3,7 +3,7 @@
namespace pkpy { namespace pkpy {
PyVar* FastLocals::try_get_name(StrName name) { 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; if(index == -1) return nullptr;
return &a[index]; return &a[index];
} }
@ -11,7 +11,7 @@ PyVar* FastLocals::try_get_name(StrName name) {
NameDict_ FastLocals::to_namedict() { NameDict_ FastLocals::to_namedict() {
NameDict_ dict = std::make_shared<NameDict>(); NameDict_ dict = std::make_shared<NameDict>();
for(int i=0; i<co->varnames_inv.count; i++){ for(int i=0; i<co->varnames_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]; PyVar value = a[entry.value];
if(value) dict->set(StrName(entry.key), value); if(value) dict->set(StrName(entry.key), value);
} }

View File

@ -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) { for(int j = 0; j < kwargs.size(); j += 2) {
StrName key(_CAST(uint16_t, kwargs[j])); 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 key is an explicit key, set as local variable
if(index >= 0) { if(index >= 0) {
buffer[index] = kwargs[j + 1]; buffer[index] = kwargs[j + 1];