global replace

This commit is contained in:
blueloveTH 2024-06-29 18:34:38 +08:00
parent 773a05e25c
commit a55d3a5340
20 changed files with 1376 additions and 1233 deletions

View File

@ -43,8 +43,8 @@
/*************** internal settings ***************/ /*************** internal settings ***************/
// This is the maximum size of the value stack in PyVar units // This is the maximum size of the value stack in py_TValue units
// The actual size in bytes equals `sizeof(PyVar) * PK_VM_STACK_SIZE` // The actual size in bytes equals `sizeof(py_TValue) * PK_VM_STACK_SIZE`
#define PK_VM_STACK_SIZE 16384 #define PK_VM_STACK_SIZE 16384
// This is the maximum number of local variables in a function // This is the maximum number of local variables in a function

View File

@ -11,14 +11,14 @@
extern "C" { extern "C" {
#endif #endif
PyVar* FastLocals__try_get_by_name(PyVar* locals, const CodeObject* co, py_Name name); py_TValue* FastLocals__try_get_by_name(py_TValue* locals, const CodeObject* co, py_Name name);
pk_NameDict* FastLocals__to_namedict(PyVar* locals, const CodeObject* co); pk_NameDict* FastLocals__to_namedict(py_TValue* locals, const CodeObject* co);
typedef struct ValueStack { typedef struct ValueStack {
// We allocate extra PK_VM_STACK_SIZE/128 places to keep `_sp` valid when `is_overflow() == true`. // We allocate extra PK_VM_STACK_SIZE/128 places to keep `_sp` valid when `is_overflow() == true`.
PyVar* sp; py_TValue* sp;
PyVar* end; py_TValue* end;
PyVar begin[PK_VM_STACK_SIZE + PK_VM_STACK_SIZE / 128]; py_TValue begin[PK_VM_STACK_SIZE + PK_VM_STACK_SIZE / 128];
} ValueStack; } ValueStack;
void ValueStack__ctor(ValueStack* self); void ValueStack__ctor(ValueStack* self);
@ -39,14 +39,14 @@ typedef struct Frame {
const CodeObject* co; const CodeObject* co;
PyObject* module; PyObject* module;
PyObject* function; // a function object or NULL (global scope) PyObject* function; // a function object or NULL (global scope)
PyVar* p0; // unwinding base py_TValue* p0; // unwinding base
PyVar* locals; // locals base py_TValue* locals; // locals base
const CodeObject* locals_co; const CodeObject* locals_co;
UnwindTarget* uw_list; UnwindTarget* uw_list;
} Frame; } Frame;
Frame* Frame__new(const CodeObject* co, const PyVar* module, const PyVar* function, PyVar* p0, PyVar* locals, const CodeObject* locals_co); Frame* Frame__new(const CodeObject* co, const py_TValue* module, const py_TValue* function, py_TValue* p0, py_TValue* locals, const CodeObject* locals_co);
void Frame__delete(Frame* self); void Frame__delete(Frame* self);
PK_INLINE int Frame__ip(const Frame* self){ PK_INLINE int Frame__ip(const Frame* self){
@ -67,11 +67,11 @@ PK_INLINE pk_NameDict* Frame__f_globals(Frame* self){
return PyObject__dict(self->module); return PyObject__dict(self->module);
} }
PK_INLINE PyVar* Frame__f_globals_try_get(Frame* self, py_Name name){ PK_INLINE py_TValue* Frame__f_globals_try_get(Frame* self, py_Name name){
return pk_NameDict__try_get(Frame__f_globals(self), name); return pk_NameDict__try_get(Frame__f_globals(self), name);
} }
PyVar* Frame__f_closure_try_get(Frame* self, py_Name name); py_TValue* Frame__f_closure_try_get(Frame* self, py_Name name);
int Frame__prepare_jump_exception_handler(Frame* self, ValueStack*); int Frame__prepare_jump_exception_handler(Frame* self, ValueStack*);
void Frame__prepare_jump_break(Frame* self, ValueStack*, int); void Frame__prepare_jump_break(Frame* self, ValueStack*, int);
@ -80,7 +80,7 @@ int Frame__exit_block(Frame* self, ValueStack*, int);
void Frame__gc_mark(Frame* self); void Frame__gc_mark(Frame* self);
UnwindTarget* Frame__find_unwind_target(Frame* self, int iblock); UnwindTarget* Frame__find_unwind_target(Frame* self, int iblock);
void Frame__set_unwind_target(Frame* self, PyVar* sp); void Frame__set_unwind_target(Frame* self, py_TValue* sp);
#ifdef __cplusplus #ifdef __cplusplus
} }
@ -92,7 +92,7 @@ void Frame__set_unwind_target(Frame* self, PyVar* sp);
#include "pocketpy/objects/codeobject.hpp" #include "pocketpy/objects/codeobject.hpp"
extern "C"{ extern "C"{
inline PyVar* Frame__f_closure_try_get(Frame* self, StrName name){ inline py_TValue* Frame__f_closure_try_get(Frame* self, StrName name){
if(self->function == NULL) return NULL; if(self->function == NULL) return NULL;
pkpy::Function* fn = PyObject__as(pkpy::Function, self->function); pkpy::Function* fn = PyObject__as(pkpy::Function, self->function);
if(fn->_closure == nullptr) return nullptr; if(fn->_closure == nullptr) return nullptr;

View File

@ -12,8 +12,8 @@ typedef struct pk_TypeInfo{
py_Name name; py_Name name;
py_Type base; py_Type base;
PyVar self; // the type object itself py_TValue self; // the type object itself
PyVar module; // the module where the type is defined py_TValue module; // the module where the type is defined
bool subclass_enabled; bool subclass_enabled;
void (*dtor)(void*); void (*dtor)(void*);
@ -38,7 +38,7 @@ typedef struct pk_TypeInfo{
py_CFunction on_end_subclass; // for enum module py_CFunction on_end_subclass; // for enum module
} pk_TypeInfo; } pk_TypeInfo;
void pk_TypeInfo__ctor(pk_TypeInfo* self, py_Name name, py_Type base, PyObject* obj, const PyVar* module, bool subclass_enabled); void pk_TypeInfo__ctor(pk_TypeInfo* self, py_Name name, py_Type base, PyObject* obj, const py_TValue* module, bool subclass_enabled);
void pk_TypeInfo__dtor(pk_TypeInfo* self); void pk_TypeInfo__dtor(pk_TypeInfo* self);
typedef struct pk_VM { typedef struct pk_VM {
@ -47,24 +47,24 @@ typedef struct pk_VM {
pk_NameDict modules; pk_NameDict modules;
c11_vector/*T=pk_TypeInfo*/ types; c11_vector/*T=pk_TypeInfo*/ types;
PyVar StopIteration; // a special Exception class py_TValue StopIteration; // a special Exception class
PyVar builtins; // builtins module py_TValue builtins; // builtins module
PyVar main; // __main__ module py_TValue main; // __main__ module
void (*_ceval_on_step)(Frame*, Bytecode); void (*_ceval_on_step)(Frame*, Bytecode);
unsigned char* (*_import_file)(const char*); unsigned char* (*_import_file)(const char*);
void (*_stdout)(const char*); void (*_stdout)(const char*, ...);
void (*_stderr)(const char*); void (*_stderr)(const char*, ...);
// singleton objects // singleton objects
PyVar True, False, None, NotImplemented, Ellipsis; py_TValue True, False, None, NotImplemented, Ellipsis;
// last error // last error
py_Error* last_error; py_Error* last_error;
PyObject* __curr_class; PyObject* __curr_class;
PyObject* __cached_object_new; PyObject* __cached_object_new;
FuncDecl_ __dynamic_func_decl; FuncDecl_ __dynamic_func_decl;
PyVar __vectorcall_buffer[PK_MAX_CO_VARNAMES]; py_TValue __vectorcall_buffer[PK_MAX_CO_VARNAMES];
pk_ManagedHeap heap; pk_ManagedHeap heap;
ValueStack stack; // put `stack` at the end for better cache locality ValueStack stack; // put `stack` at the end for better cache locality
@ -87,7 +87,7 @@ typedef enum pk_FrameResult{
pk_FrameResult pk_VM__run_top_frame(pk_VM* self); pk_FrameResult pk_VM__run_top_frame(pk_VM* self);
py_Type pk_VM__new_type(pk_VM* self, const char* name, py_Type base, const PyVar* module, bool subclass_enabled); py_Type pk_VM__new_type(pk_VM* self, const char* name, py_Type base, const py_TValue* module, bool subclass_enabled);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -14,8 +14,9 @@ extern "C" {
#endif #endif
typedef int16_t py_Type; typedef int16_t py_Type;
typedef struct PyObject PyObject;
typedef struct PyVar{ typedef struct py_TValue{
py_Type type; py_Type type;
bool is_ptr; bool is_ptr;
int extra; int extra;
@ -26,9 +27,9 @@ typedef struct PyVar{
void* _ptr; void* _ptr;
// Vec2 // Vec2
}; };
} PyVar; } py_TValue;
static_assert(sizeof(PyVar) <= 16, "!sizeof(PyVar) <= 16"); static_assert(sizeof(py_TValue) <= 16, "!sizeof(py_TValue) <= 16");
/* predefined vars */ /* predefined vars */
static const py_Type tp_object = {1}, tp_type = {2}; static const py_Type tp_object = {1}, tp_type = {2};
@ -44,7 +45,7 @@ static const py_Type tp_ellipsis = {26};
static const py_Type tp_op_call = {27}, tp_op_yield = {28}; static const py_Type tp_op_call = {27}, tp_op_yield = {28};
static const py_Type tp_syntax_error = {29}, tp_stop_iteration = {30}; static const py_Type tp_syntax_error = {29}, tp_stop_iteration = {30};
extern PyVar PY_NULL, PY_OP_CALL, PY_OP_YIELD; extern py_TValue PY_NULL, PY_OP_CALL, PY_OP_YIELD;
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -74,7 +74,7 @@ typedef struct CodeObject {
c11_vector/*T=Bytecode*/ codes; c11_vector/*T=Bytecode*/ codes;
c11_vector/*T=CodeObjectByteCodeEx*/ codes_ex; c11_vector/*T=CodeObjectByteCodeEx*/ codes_ex;
c11_vector/*T=PyVar*/ consts; // constants c11_vector/*T=py_TValue*/ consts; // constants
c11_vector/*T=StrName*/ varnames; // local variables c11_vector/*T=StrName*/ varnames; // local variables
int nlocals; // cached varnames.size() int nlocals; // cached varnames.size()
@ -95,7 +95,7 @@ void CodeObject__gc_mark(const CodeObject* self);
typedef struct FuncDeclKwArg{ typedef struct FuncDeclKwArg{
int index; // index in co->varnames int index; // index in co->varnames
uint16_t key; // name of this argument uint16_t key; // name of this argument
PyVar value; // default value py_TValue value; // default value
} FuncDeclKwArg; } FuncDeclKwArg;
typedef struct FuncDecl { typedef struct FuncDecl {
@ -119,7 +119,7 @@ typedef FuncDecl* FuncDecl_;
FuncDecl_ FuncDecl__rcnew(pk_SourceData_ src, c11_string name); FuncDecl_ FuncDecl__rcnew(pk_SourceData_ src, c11_string name);
void FuncDecl__dtor(FuncDecl* self); void FuncDecl__dtor(FuncDecl* self);
void FuncDecl__add_kwarg(FuncDecl* self, int index, uint16_t key, const PyVar* value); void FuncDecl__add_kwarg(FuncDecl* self, int index, uint16_t key, const py_TValue* value);
void FuncDecl__gc_mark(const FuncDecl* self); void FuncDecl__gc_mark(const FuncDecl* self);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -49,7 +49,7 @@ pkpy_Dict pkpy_Dict__copy(const pkpy_Dict* self);
* @param val value to set * @param val value to set
* @return `true` if the key is newly added, `false` if the key already exists * @return `true` if the key is newly added, `false` if the key already exists
*/ */
bool pkpy_Dict__set(pkpy_Dict* self, PyVar key, PyVar val); bool pkpy_Dict__set(pkpy_Dict* self, py_TValue key, py_TValue val);
/** /**
* @brief Check if a key exists in the `pkpy_Dict` * @brief Check if a key exists in the `pkpy_Dict`
@ -58,7 +58,7 @@ bool pkpy_Dict__set(pkpy_Dict* self, PyVar key, PyVar val);
* @param key key to check * @param key key to check
* @return `true` if the key exists, `false` otherwise * @return `true` if the key exists, `false` otherwise
*/ */
bool pkpy_Dict__contains(const pkpy_Dict* self, PyVar key); bool pkpy_Dict__contains(const pkpy_Dict* self, py_TValue key);
/** /**
* @brief Remove a key from the `pkpy_Dict` * @brief Remove a key from the `pkpy_Dict`
@ -67,7 +67,7 @@ bool pkpy_Dict__contains(const pkpy_Dict* self, PyVar key);
* @param key key to remove * @param key key to remove
* @return `true` if the key was found and removed, `false` if the key doesn't exist * @return `true` if the key was found and removed, `false` if the key doesn't exist
*/ */
bool pkpy_Dict__del(pkpy_Dict* self, PyVar key); bool pkpy_Dict__del(pkpy_Dict* self, py_TValue key);
/** /**
* @brief Try to get a value from the `pkpy_Dict` * @brief Try to get a value from the `pkpy_Dict`
@ -76,7 +76,7 @@ bool pkpy_Dict__del(pkpy_Dict* self, PyVar key);
* @param key key to get * @param key key to get
* @return the value associated with the key, `NULL` if the key doesn't exist * @return the value associated with the key, `NULL` if the key doesn't exist
*/ */
const PyVar* pkpy_Dict__try_get(const pkpy_Dict* self, PyVar key); const py_TValue* pkpy_Dict__try_get(const pkpy_Dict* self, py_TValue key);
/** /**
* @brief Update the `pkpy_Dict` with another one * @brief Update the `pkpy_Dict` with another one
@ -106,7 +106,7 @@ pkpy_DictIter pkpy_Dict__iter(const pkpy_Dict* self);
* @param value value will be filled with the current value, can be `NULL` if not needed * @param value value will be filled with the current value, can be `NULL` if not needed
* @return `true` if the iteration is still valid, `false` otherwise * @return `true` if the iteration is still valid, `false` otherwise
*/ */
bool pkpy_DictIter__next(pkpy_DictIter* self, PyVar* key, PyVar* value); bool pkpy_DictIter__next(pkpy_DictIter* self, py_TValue* key, py_TValue* value);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -11,7 +11,7 @@ extern "C" {
#define SMALLMAP_T__HEADER #define SMALLMAP_T__HEADER
#define K uint16_t #define K uint16_t
#define V PyVar #define V py_TValue
#define NAME pk_NameDict #define NAME pk_NameDict
#include "pocketpy/xmacros/smallmap.h" #include "pocketpy/xmacros/smallmap.h"
#undef SMALLMAP_T__HEADER #undef SMALLMAP_T__HEADER

View File

@ -22,18 +22,18 @@ typedef struct PyObject{
static_assert(sizeof(PyObject) <= 8, "!(sizeof(PyObject) <= 8)"); static_assert(sizeof(PyObject) <= 8, "!(sizeof(PyObject) <= 8)");
PyVar* PyObject__slots(PyObject* self); py_TValue* PyObject__slots(PyObject* self);
pk_NameDict* PyObject__dict(PyObject* self); pk_NameDict* PyObject__dict(PyObject* self);
void* PyObject__value(PyObject* self); void* PyObject__value(PyObject* self);
#define PK_OBJ_HEADER_SIZE(slots) ((slots)>=0 ? 8+sizeof(PyVar)*(slots) : 8+sizeof(pk_NameDict)) #define PK_OBJ_HEADER_SIZE(slots) ((slots)>=0 ? 8+sizeof(py_TValue)*(slots) : 8+sizeof(pk_NameDict))
PyObject* PyObject__new(py_Type type, int slots, int size); PyObject* PyObject__new(py_Type type, int slots, int size);
void PyObject__delete(PyObject* self); void PyObject__delete(PyObject* self);
PK_INLINE PyVar PyVar__fromobj(PyObject* obj){ PK_INLINE py_TValue PyVar__fromobj(PyObject* obj){
if(!obj) return PY_NULL; if(!obj) return PY_NULL;
PyVar retval = { py_TValue retval = {
.type = obj->type, .type = obj->type,
.is_ptr = true, .is_ptr = true,
._obj = obj ._obj = obj

View File

@ -4,12 +4,12 @@
#include <stdbool.h> #include <stdbool.h>
/************* Public Types *************/ /************* Public Types *************/
typedef struct PyObject PyObject; typedef struct py_TValue py_TValue;
typedef struct PyVar PyVar;
typedef struct pk_VM pk_VM; typedef struct pk_VM pk_VM;
typedef uint16_t py_Name; typedef uint16_t py_Name;
typedef int16_t py_Type; typedef int16_t py_Type;
typedef PyVar* py_Ref; typedef py_TValue* py_Ref;
typedef struct py_Str py_Str;
typedef int (*py_CFunction)(int argc, py_Ref argv); typedef int (*py_CFunction)(int argc, py_Ref argv);
typedef struct py_Error{ typedef struct py_Error{
@ -175,8 +175,8 @@ void py_dict__setitem(py_Ref self, const py_Ref key, const py_Ref val);
void py_dict__delitem(py_Ref self, const py_Ref key); void py_dict__delitem(py_Ref self, const py_Ref key);
void py_dict__clear(py_Ref self); void py_dict__clear(py_Ref self);
int py_str(const py_Ref, char* out); int py_str(const py_Ref, py_Str* out);
int py_repr(const py_Ref, char* out); int py_repr(const py_Ref, py_Str* out);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -251,7 +251,7 @@ typedef struct ImagExpr {
void ImagExpr__emit_(Expr* self_, Ctx* ctx) { void ImagExpr__emit_(Expr* self_, Ctx* ctx) {
ImagExpr* self = (ImagExpr*)self_; ImagExpr* self = (ImagExpr*)self_;
PyVar value; py_TValue value;
py_newfloat(&value, self->value); py_newfloat(&value, self->value);
int index = Ctx__add_const(ctx, &value); int index = Ctx__add_const(ctx, &value);
Ctx__emit_(ctx, OP_LOAD_CONST, index, self->line); Ctx__emit_(ctx, OP_LOAD_CONST, index, self->line);
@ -282,7 +282,7 @@ void LiteralExpr__emit_(Expr* self_, Ctx* ctx) {
break; break;
} }
case TokenValue_F64: { case TokenValue_F64: {
PyVar value; py_TValue value;
py_newfloat(&value, self->value->_f64); py_newfloat(&value, self->value->_f64);
int index = Ctx__add_const(ctx, &value); int index = Ctx__add_const(ctx, &value);
Ctx__emit_(ctx, OP_LOAD_CONST, index, self->line); Ctx__emit_(ctx, OP_LOAD_CONST, index, self->line);
@ -1331,7 +1331,7 @@ int Ctx__emit_int(Ctx* self, int64_t value, int line) {
if(is_small_int(value)) { if(is_small_int(value)) {
return Ctx__emit_(self, OP_LOAD_SMALL_INT, (uint16_t)value, line); return Ctx__emit_(self, OP_LOAD_SMALL_INT, (uint16_t)value, line);
} else { } else {
PyVar tmp; py_TValue tmp;
py_newint(&tmp, value); py_newint(&tmp, value);
return Ctx__emit_(self, OP_LOAD_CONST, Ctx__add_const(self, &tmp), line); return Ctx__emit_(self, OP_LOAD_CONST, Ctx__add_const(self, &tmp), line);
} }
@ -1366,9 +1366,9 @@ int Ctx__add_const_string(Ctx* self, c11_string key) {
if(val) { if(val) {
return *val; return *val;
} else { } else {
PyVar tmp; py_TValue tmp;
py_newstrn(&tmp, key.data, key.size); py_newstrn(&tmp, key.data, key.size);
c11_vector__push(PyVar, &self->co->consts, tmp); c11_vector__push(py_TValue, &self->co->consts, tmp);
int index = self->co->consts.count - 1; int index = self->co->consts.count - 1;
c11_smallmap_s2n__set(&self->co_consts_string_dedup_map, c11_smallmap_s2n__set(&self->co_consts_string_dedup_map,
py_Str__sv(PyObject__value(tmp._obj)), py_Str__sv(PyObject__value(tmp._obj)),
@ -1379,7 +1379,7 @@ int Ctx__add_const_string(Ctx* self, c11_string key) {
int Ctx__add_const(Ctx* self, py_Ref v) { int Ctx__add_const(Ctx* self, py_Ref v) {
assert(v->type != tp_str); assert(v->type != tp_str);
c11_vector__push(PyVar, &self->co->consts, *v); c11_vector__push(py_TValue, &self->co->consts, *v);
return self->co->consts.count - 1; return self->co->consts.count - 1;
} }

File diff suppressed because it is too large Load Diff

1151
src/interpreter/ceval.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -10,16 +10,16 @@ void ValueStack__clear(ValueStack* self) {
self->sp = self->begin; self->sp = self->begin;
} }
PyVar* FastLocals__try_get_by_name(PyVar* locals, const CodeObject* co, py_Name name){ py_TValue* FastLocals__try_get_by_name(py_TValue* locals, const CodeObject* co, py_Name name){
int index = c11_smallmap_n2i__get(&co->varnames_inv, name, -1); int index = c11_smallmap_n2i__get(&co->varnames_inv, name, -1);
if(index == -1) return NULL; if(index == -1) return NULL;
return &locals[index]; return &locals[index];
} }
pk_NameDict* FastLocals__to_namedict(PyVar* locals, const CodeObject* co) { pk_NameDict* FastLocals__to_namedict(py_TValue* locals, const CodeObject* co) {
pk_NameDict* dict = pk_NameDict__new(); pk_NameDict* dict = pk_NameDict__new();
c11__foreach(c11_smallmap_n2i_KV, &co->varnames_inv, entry) { c11__foreach(c11_smallmap_n2i_KV, &co->varnames_inv, entry) {
PyVar value = locals[entry->value]; py_TValue value = locals[entry->value];
if(!py_isnull(&value)){ if(!py_isnull(&value)){
pk_NameDict__set(dict, entry->key, value); pk_NameDict__set(dict, entry->key, value);
} }
@ -39,7 +39,7 @@ void UnwindTarget__delete(UnwindTarget* self){
free(self); free(self);
} }
Frame* Frame__new(const CodeObject* co, const PyVar* module, const PyVar* function, PyVar* p0, PyVar* locals, const CodeObject* locals_co){ Frame* Frame__new(const CodeObject* co, const py_TValue* module, const py_TValue* function, py_TValue* p0, py_TValue* locals, const CodeObject* locals_co){
static_assert(sizeof(Frame) <= kPoolFrameBlockSize, "!(sizeof(Frame) <= kPoolFrameBlockSize)"); static_assert(sizeof(Frame) <= kPoolFrameBlockSize, "!(sizeof(Frame) <= kPoolFrameBlockSize)");
Frame* self = PoolFrame_alloc(); Frame* self = PoolFrame_alloc();
self->f_back = NULL; self->f_back = NULL;
@ -72,7 +72,7 @@ int Frame__prepare_jump_exception_handler(Frame* self, ValueStack* _s){
iblock = block->parent; iblock = block->parent;
} }
if(iblock < 0) return -1; if(iblock < 0) return -1;
PyVar obj = *--_s->sp; // pop exception object py_TValue obj = *--_s->sp; // pop exception object
UnwindTarget* uw = Frame__find_unwind_target(self, iblock); UnwindTarget* uw = Frame__find_unwind_target(self, iblock);
_s->sp = (self->locals + uw->offset); // unwind the stack _s->sp = (self->locals + uw->offset); // unwind the stack
*(_s->sp++) = obj; // push it back *(_s->sp++) = obj; // push it back
@ -121,7 +121,7 @@ UnwindTarget* Frame__find_unwind_target(Frame* self, int iblock){
return NULL; return NULL;
} }
void Frame__set_unwind_target(Frame* self, PyVar* sp) { void Frame__set_unwind_target(Frame* self, py_TValue* sp) {
int iblock = Frame__iblock(self); int iblock = Frame__iblock(self);
UnwindTarget* existing = Frame__find_unwind_target(self, iblock); UnwindTarget* existing = Frame__find_unwind_target(self, iblock);
if(existing) { if(existing) {

View File

@ -119,7 +119,7 @@ PyObject* PyObject__new(py_Type type, int slots, int size){
// initialize slots or dict // initialize slots or dict
void* p = (char*)self + 8; void* p = (char*)self + 8;
if(slots >= 0){ if(slots >= 0){
memset(p, 0, slots*sizeof(PyVar)); memset(p, 0, slots*sizeof(py_TValue));
}else{ }else{
pk_NameDict__ctor(p); pk_NameDict__ctor(p);
} }

View File

@ -7,17 +7,23 @@ static unsigned char* pk_default_import_file(const char* path){
return NULL; return NULL;
} }
static void pk_default_stdout(const char* s){ static void pk_default_stdout(const char* fmt, ...){
fprintf(stdout, "%s", s); va_list args;
va_start(args, fmt);
vfprintf(stdout, fmt, args);
va_end(args);
fflush(stdout); fflush(stdout);
} }
static void pk_default_stderr(const char* s){ static void pk_default_stderr(const char* fmt, ...){
fprintf(stderr, "%s", s); va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fflush(stderr); fflush(stderr);
} }
void pk_TypeInfo__ctor(pk_TypeInfo *self, py_Name name, py_Type base, PyObject* obj, const PyVar* module, bool subclass_enabled){ void pk_TypeInfo__ctor(pk_TypeInfo *self, py_Name name, py_Type base, PyObject* obj, const py_TValue* module, bool subclass_enabled){
memset(self, 0, sizeof(pk_TypeInfo)); memset(self, 0, sizeof(pk_TypeInfo));
self->name = name; self->name = name;
@ -58,19 +64,19 @@ void pk_VM__ctor(pk_VM* self){
pk_ManagedHeap__ctor(&self->heap, self); pk_ManagedHeap__ctor(&self->heap, self);
ValueStack__ctor(&self->stack); ValueStack__ctor(&self->stack);
self->True = (PyVar){.type=tp_bool, .is_ptr=true, .extra=1, self->True = (py_TValue){.type=tp_bool, .is_ptr=true, .extra=1,
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 0), ._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 0),
}; };
self->False = (PyVar){.type=tp_bool, .is_ptr=true, .extra=0, self->False = (py_TValue){.type=tp_bool, .is_ptr=true, .extra=0,
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 0), ._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 0),
}; };
self->None = (PyVar){.type=tp_none_type, .is_ptr=true, self->None = (py_TValue){.type=tp_none_type, .is_ptr=true,
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_none_type, 0, 0), ._obj=pk_ManagedHeap__gcnew(&self->heap, tp_none_type, 0, 0),
}; };
self->NotImplemented = (PyVar){.type=tp_not_implemented_type, .is_ptr=true, self->NotImplemented = (py_TValue){.type=tp_not_implemented_type, .is_ptr=true,
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_not_implemented_type, 0, 0), ._obj=pk_ManagedHeap__gcnew(&self->heap, tp_not_implemented_type, 0, 0),
}; };
self->Ellipsis = (PyVar){.type=tp_ellipsis, .is_ptr=true, self->Ellipsis = (py_TValue){.type=tp_ellipsis, .is_ptr=true,
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_ellipsis, 0, 0), ._obj=pk_ManagedHeap__gcnew(&self->heap, tp_ellipsis, 0, 0),
}; };
@ -169,11 +175,7 @@ void pk_VM__pop_frame(pk_VM* self){
Frame__delete(frame); Frame__delete(frame);
} }
pk_FrameResult pk_VM__run_top_frame(pk_VM* self){ py_Type pk_VM__new_type(pk_VM* self, const char* name, py_Type base, const py_TValue* module, bool subclass_enabled){
return RES_RETURN;
}
py_Type pk_VM__new_type(pk_VM* self, const char* name, py_Type base, const PyVar* module, bool subclass_enabled){
py_Type type = self->types.count; py_Type type = self->types.count;
pk_TypeInfo* ti = c11_vector__emplace(&self->types); pk_TypeInfo* ti = c11_vector__emplace(&self->types);
PyObject* typeobj = pk_ManagedHeap__gcnew(&self->heap, tp_type, -1, sizeof(py_Type)); PyObject* typeobj = pk_ManagedHeap__gcnew(&self->heap, tp_type, -1, sizeof(py_Type));

View File

@ -1,6 +1,6 @@
#include "pocketpy/objects/base.h" #include "pocketpy/objects/base.h"
PyVar PY_NULL = {.type=0, .is_ptr=false, .extra=0, ._i64=0}; py_TValue PY_NULL = {.type=0, .is_ptr=false, .extra=0, ._i64=0};
PyVar PY_OP_CALL = {.type=27, .is_ptr=false, .extra=0, ._i64=0}; py_TValue PY_OP_CALL = {.type=27, .is_ptr=false, .extra=0, ._i64=0};
PyVar PY_OP_YIELD = {.type=28, .is_ptr=false, .extra=0, ._i64=0}; py_TValue PY_OP_YIELD = {.type=28, .is_ptr=false, .extra=0, ._i64=0};

View File

@ -40,7 +40,7 @@ void FuncDecl__dtor(FuncDecl* self) {
c11_smallmap_n2i__dtor(&self->kw_to_index); c11_smallmap_n2i__dtor(&self->kw_to_index);
} }
void FuncDecl__add_kwarg(FuncDecl* self, int index, uint16_t key, const PyVar* value) { void FuncDecl__add_kwarg(FuncDecl* self, int index, uint16_t key, const py_TValue* value) {
c11_smallmap_n2i__set(&self->kw_to_index, key, index); c11_smallmap_n2i__set(&self->kw_to_index, key, index);
FuncDeclKwArg item = {index, key, *value}; FuncDeclKwArg item = {index, key, *value};
c11_vector__push(FuncDeclKwArg, &self->kwargs, item); c11_vector__push(FuncDeclKwArg, &self->kwargs, item);
@ -54,7 +54,7 @@ void CodeObject__ctor(CodeObject* self, pk_SourceData_ src, c11_string name) {
c11_vector__ctor(&self->codes, sizeof(Bytecode)); c11_vector__ctor(&self->codes, sizeof(Bytecode));
c11_vector__ctor(&self->codes_ex, sizeof(BytecodeEx)); c11_vector__ctor(&self->codes_ex, sizeof(BytecodeEx));
c11_vector__ctor(&self->consts, sizeof(PyVar)); c11_vector__ctor(&self->consts, sizeof(py_TValue));
c11_vector__ctor(&self->varnames, sizeof(uint16_t)); c11_vector__ctor(&self->varnames, sizeof(uint16_t));
self->nlocals = 0; self->nlocals = 0;

View File

@ -13,8 +13,8 @@
#define pkpy_Var__set_null(self) do { (self)->type = 0; } while(0) #define pkpy_Var__set_null(self) do { (self)->type = 0; } while(0)
struct pkpy_DictEntry { struct pkpy_DictEntry {
PyVar key; py_TValue key;
PyVar val; py_TValue val;
}; };
inline extern int pkpy_Dict__idx_size(const pkpy_Dict* self) { inline extern int pkpy_Dict__idx_size(const pkpy_Dict* self) {
@ -79,7 +79,7 @@ static void pkpy_Dict__htset(pkpy_Dict* self, int h, int v) {
#endif #endif
} }
static int pkpy_Dict__probe0(const pkpy_Dict* self, PyVar key, int hash) { static int pkpy_Dict__probe0(const pkpy_Dict* self, py_TValue key, int hash) {
const int null = pkpy_Dict__idx_null(self); const int null = pkpy_Dict__idx_null(self);
const int mask = self->_htcap - 1; const int mask = self->_htcap - 1;
for(int h = hash & mask;; h = DICT_HASH_NEXT(h) & mask) { for(int h = hash & mask;; h = DICT_HASH_NEXT(h) & mask) {
@ -92,7 +92,7 @@ static int pkpy_Dict__probe0(const pkpy_Dict* self, PyVar key, int hash) {
PK_UNREACHABLE(); PK_UNREACHABLE();
} }
static int pkpy_Dict__probe1(const pkpy_Dict* self, PyVar key, int hash) { static int pkpy_Dict__probe1(const pkpy_Dict* self, py_TValue key, int hash) {
const int null = pkpy_Dict__idx_null(self); const int null = pkpy_Dict__idx_null(self);
const int mask = self->_htcap - 1; const int mask = self->_htcap - 1;
for(int h = hash & mask;; h = DICT_HASH_NEXT(h) & mask) { for(int h = hash & mask;; h = DICT_HASH_NEXT(h) & mask) {
@ -124,7 +124,7 @@ static void pkpy_Dict__extendht(pkpy_Dict* self) {
} }
} }
bool pkpy_Dict__set(pkpy_Dict* self, PyVar key, PyVar val) { bool pkpy_Dict__set(pkpy_Dict* self, py_TValue key, py_TValue val) {
int64_t out; int64_t out;
int err = py_hash(&key, &out); int err = py_hash(&key, &out);
int hash = DICT_HASH_TRANS(out); int hash = DICT_HASH_TRANS(out);
@ -161,7 +161,7 @@ bool pkpy_Dict__set(pkpy_Dict* self, PyVar key, PyVar val) {
return false; return false;
} }
bool pkpy_Dict__contains(const pkpy_Dict* self, PyVar key) { bool pkpy_Dict__contains(const pkpy_Dict* self, py_TValue key) {
int64_t out; int64_t out;
int err = py_hash(&key, &out); int err = py_hash(&key, &out);
int hash = DICT_HASH_TRANS(out); int hash = DICT_HASH_TRANS(out);
@ -205,7 +205,7 @@ static bool pkpy_Dict__refactor(pkpy_Dict* self) {
return true; return true;
} }
bool pkpy_Dict__del(pkpy_Dict* self, PyVar key) { bool pkpy_Dict__del(pkpy_Dict* self, py_TValue key) {
int64_t out; int64_t out;
int err = py_hash(&key, &out); int err = py_hash(&key, &out);
int hash = DICT_HASH_TRANS(out); int hash = DICT_HASH_TRANS(out);
@ -220,7 +220,7 @@ bool pkpy_Dict__del(pkpy_Dict* self, PyVar key) {
return true; return true;
} }
const PyVar *pkpy_Dict__try_get(const pkpy_Dict* self, PyVar key) { const py_TValue *pkpy_Dict__try_get(const pkpy_Dict* self, py_TValue key) {
int64_t out; int64_t out;
int err = py_hash(&key, &out); int err = py_hash(&key, &out);
int hash = DICT_HASH_TRANS(out); int hash = DICT_HASH_TRANS(out);
@ -263,7 +263,7 @@ pkpy_DictIter pkpy_Dict__iter(const pkpy_Dict *self) {
}; };
} }
bool pkpy_DictIter__next(pkpy_DictIter *self, PyVar *key, PyVar *val) { bool pkpy_DictIter__next(pkpy_DictIter *self, py_TValue *key, py_TValue *val) {
if(self->_index >= self->_dict->_entries.count) return false; if(self->_index >= self->_dict->_entries.count) return false;
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_dict->_entries, self->_index); struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_dict->_entries, self->_index);

View File

@ -2,7 +2,7 @@
#define SMALLMAP_T__SOURCE #define SMALLMAP_T__SOURCE
#define K uint16_t #define K uint16_t
#define V PyVar #define V py_TValue
#define NAME pk_NameDict #define NAME pk_NameDict
#include "pocketpy/xmacros/smallmap.h" #include "pocketpy/xmacros/smallmap.h"
#undef SMALLMAP_T__SOURCE #undef SMALLMAP_T__SOURCE

View File

@ -11,7 +11,7 @@ pk_NameDict* PyObject__dict(PyObject* self){
return (pk_NameDict*)((char*)self + 8); return (pk_NameDict*)((char*)self + 8);
} }
PyVar* PyObject__slots(PyObject* self){ py_TValue* PyObject__slots(PyObject* self){
assert(self->slots >= 0); assert(self->slots >= 0);
return (PyVar*)((char*)self + 8); return (py_TValue*)((char*)self + 8);
} }