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 <stdint.h>
@ -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

View File

@ -86,9 +86,9 @@ struct CodeObject {
small_vector_2<StrName, 8> varnames; // local variables
int nlocals; // varnames.size()
c11_smallmap_uint16_t_int varnames_inv;
c11_smallmap_n2i varnames_inv;
vector<CodeBlock> blocks;
c11_smallmap_uint16_t_int labels;
c11_smallmap_n2i labels;
vector<FuncDecl_> 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);
}
};

View File

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

View File

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

View File

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

View File

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

View File

@ -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<NameDict>();
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];
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) {
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];