This commit is contained in:
blueloveTH 2024-06-25 14:16:58 +08:00
parent efd98e6a6a
commit bb67505613
13 changed files with 173 additions and 110 deletions

View File

@ -9,6 +9,8 @@ extern "C" {
typedef uint16_t StrName;
#define py_name(name) pk_StrName__map(#name)
uint16_t pk_StrName__map(const char*);
uint16_t pk_StrName__map2(c11_string);
const char* pk_StrName__rmap(uint16_t index);

View File

@ -11,7 +11,7 @@
extern "C" {
#endif
PyVar* FastLocals__try_get_by_name(PyVar* locals, const CodeObject* co, StrName name);
PyVar* FastLocals__try_get_by_name(PyVar* locals, const CodeObject* co, py_Name name);
pk_NameDict* FastLocals__to_namedict(PyVar* locals, const CodeObject* co);
typedef struct ValueStack {
@ -46,7 +46,7 @@ typedef struct Frame {
} Frame;
Frame* Frame__new(const CodeObject* co, PyObject* module, PyObject* function, PyVar* p0, PyVar* locals, const CodeObject* locals_co);
Frame* Frame__new(const CodeObject* co, const PyVar* module, const PyVar* function, PyVar* p0, PyVar* locals, const CodeObject* locals_co);
void Frame__delete(Frame* self);
PK_INLINE int Frame__ip(const Frame* self){
@ -64,14 +64,14 @@ PK_INLINE int Frame__iblock(const Frame* self){
}
PK_INLINE pk_NameDict* Frame__f_globals(Frame* self){
return self->module->dict;
return PyObject__dict(self->module);
}
PK_INLINE PyVar* Frame__f_globals_try_get(Frame* self, StrName name){
return pk_NameDict__try_get(self->module->dict, name);
PK_INLINE PyVar* Frame__f_globals_try_get(Frame* self, py_Name name){
return pk_NameDict__try_get(Frame__f_globals(self), name);
}
PyVar* Frame__f_closure_try_get(Frame* self, StrName name);
PyVar* Frame__f_closure_try_get(Frame* self, py_Name name);
int Frame__prepare_jump_exception_handler(Frame* self, ValueStack*);
void Frame__prepare_jump_break(Frame* self, ValueStack*, int);

View File

@ -28,8 +28,8 @@ void pk_ManagedHeap__collect_if_needed(pk_ManagedHeap* self);
int pk_ManagedHeap__collect(pk_ManagedHeap* self);
int pk_ManagedHeap__sweep(pk_ManagedHeap* self);
PyObject* pk_ManagedHeap__new(pk_ManagedHeap* self, Type type, int size);
PyObject* pk_ManagedHeap__gcnew(pk_ManagedHeap* self, Type type, int size);
PyObject* pk_ManagedHeap__new(pk_ManagedHeap* self, Type type, int slots, int size);
PyObject* pk_ManagedHeap__gcnew(pk_ManagedHeap* self, Type type, int slots, int size);
// external implementation
void pk_ManagedHeap__mark(pk_ManagedHeap* self);

View File

@ -9,7 +9,7 @@ extern "C" {
#endif
typedef struct pk_TypeInfo{
StrName name;
py_Name name;
Type base;
PyVar self; // the type object itself
@ -38,7 +38,7 @@ typedef struct pk_TypeInfo{
py_CFunction on_end_subclass; // for enum module
} pk_TypeInfo;
void pk_TypeInfo__ctor(pk_TypeInfo* self, StrName name, Type base, PyObject* obj, const PyVar* module, bool subclass_enabled);
void pk_TypeInfo__ctor(pk_TypeInfo* self, py_Name name, Type base, PyObject* obj, const PyVar* module, bool subclass_enabled);
void pk_TypeInfo__dtor(pk_TypeInfo* self);
typedef struct pk_VM {

View File

@ -11,17 +11,24 @@ typedef struct PyObject{
Type type; // we have a duplicated type here for convenience
bool gc_is_large;
bool gc_marked;
pk_NameDict* dict; // gc will delete this on destruction
int slots; // number of slots in the object
} PyObject;
static_assert(sizeof(PyObject) <= 16, "!(sizeof(PyObject) <= 16)");
// slots >= 0, allocate N slots
// slots == -1, allocate a dict
#define PyObject__value_ptr(self) ((char*)self + 16)
#define PyObject__as(T, self) (T*)(PyObject__value_ptr(self))
#define PK_OBJ_GET(T, val) (*(T*)(PyObject__value_ptr((val)._obj)))
#define PK_OBJ_SIZEOF(T) (sizeof(T) + 16)
// | 8 bytes HEADER | <N slots> | <value>
// | 8 bytes HEADER | <dict> | <value>
PyObject* PyObject__new(Type type, int size);
static_assert(sizeof(PyObject) <= 8, "!(sizeof(PyObject) <= 8)");
PyVar* PyObject__slots(PyObject* self);
pk_NameDict* PyObject__dict(PyObject* self);
void* PyObject__value(PyObject* self);
#define PK_OBJ_HEADER_SIZE(slots) ((slots)>=0 ? 8+sizeof(PyVar)*(slots) : 8+sizeof(pk_NameDict))
PyObject* PyObject__new(Type type, int slots, int size);
void PyObject__delete(PyObject* self);
PK_INLINE PyVar PyVar__fromobj(PyObject* obj){

View File

@ -10,7 +10,14 @@ extern "C" {
typedef struct PyObject PyObject;
typedef struct PyVar PyVar;
typedef struct pk_VM pk_VM;
typedef struct py_Error py_Error;
typedef uint16_t py_Name;
typedef int16_t Type;
typedef PyVar* py_Ref;
typedef int (*py_CFunction)(const py_Ref, int);
typedef struct py_Error{
Type type;
} py_Error;
typedef enum BindType {
BindType_FUNCTION,
@ -18,55 +25,76 @@ typedef enum BindType {
BindType_CLASSMETHOD,
} BindType;
typedef int (*py_CFunction)(const PyVar*, int);
typedef uint16_t StrName;
typedef int16_t Type;
extern pk_VM* pk_current_vm;
void py_initialize();
// void py_switch_vm(const char* name);
void py_finalize();
py_Error* py_exec_simple(const char*);
py_Error* py_eval_simple(const char*, PyVar*);
int py_exec_simple(const char*);
int py_eval_simple(const char*, py_Ref);
/* py_error */
void py_Error__print(const py_Error*);
void py_Error__delete(py_Error*);
py_Error* py_getlasterror();
void py_Error__print(py_Error*);
int py_eq(const PyVar*, const PyVar*);
int py_le(const PyVar*, const PyVar*);
int py_hash(const PyVar*, int64_t* out);
int py_eq(const py_Ref, const py_Ref);
int py_le(const py_Ref, const py_Ref);
int py_hash(const py_Ref, int64_t* out);
/* py_var */
void py_new_int(PyVar*, int64_t);
void py_new_float(PyVar*, double);
void py_new_bool(PyVar*, bool);
void py_new_str(PyVar*, const char*);
void py_new_strn(PyVar*, const char*, int);
void py_new_fstr(PyVar*, const char*, ...);
void py_new_bytes(PyVar*, const uint8_t*, int);
void py_new_none(PyVar*);
void py_new_null(PyVar*);
void py_new_int(py_Ref, int64_t);
void py_new_float(py_Ref, double);
void py_new_bool(py_Ref, bool);
void py_new_str(py_Ref, const char*);
void py_new_strn(py_Ref, const char*, int);
void py_new_fstr(py_Ref, const char*, ...);
void py_new_bytes(py_Ref, const uint8_t*, int);
void py_new_none(py_Ref);
void py_new_null(py_Ref);
void py_new_tuple(PyVar, int);
// new style decl-based function
void py_new_function(PyVar*, py_CFunction, const char* sig, BindType bt);
void py_new_function2(PyVar*, py_CFunction, const char* sig, BindType bt, const char* docstring, const PyVar* userdata);
void py_new_function(py_Ref, py_CFunction, const char* sig, BindType bt);
void py_new_function2(py_Ref, py_CFunction, const char* sig, BindType bt, const char* docstring, const py_Ref userdata);
// old style argc-based function
void py_new_nativefunc(PyVar*, py_CFunction, int argc, BindType bt);
void py_new_nativefunc2(PyVar*, py_CFunction, int argc, BindType bt, const char* docstring, const PyVar* userdata);
void py_new_nativefunc(py_Ref, py_CFunction, int argc, BindType bt);
void py_new_nativefunc2(py_Ref, py_CFunction, int argc, BindType bt, const char* docstring, const py_Ref userdata);
int py_setattr(PyVar* self, StrName name, const PyVar* val);
PyVar py_new_module(const char* name);
PyVar pk_new_module(const char* name, const char* package);
// Type pk_new_type
py_Ref py_new_module(const char* name);
const py_Ref py_import(const char* name);
#define py_isnull(self) ((self)->type == 0)
/// Sets the name of the object to the given value.
void py_setdict(py_Ref self, py_Name name, const py_Ref val);
/// Returns a reference to the name of the object.
py_Ref py_getdict(const py_Ref self, py_Name name);
/// Sets the i-th slot of the object to the given value.
void py_setslot(py_Ref self, int i, const py_Ref val);
/// Returns a reference to the i-th slot of the object.
py_Ref py_getslot(const py_Ref self, int i);
/// Sets the attribute of the object to the given value.
/// This is equivalent to `self.name = val`.
/// Returns 0 | err
int py_setattr(py_Ref self, py_Name name, const py_Ref val);
/// Gets the attribute of the object.
/// This is equivalent to `self.name`.
/// Returns 0 | err
int py_getattr(const py_Ref self, py_Name name, py_Ref out);
void py_pushref(const py_Ref src);
void py_copyref(const py_Ref src, py_Ref dst);
#ifdef __cplusplus
}
#endif

View File

@ -10,7 +10,7 @@ void ValueStack__clear(ValueStack* self) {
self->sp = self->begin;
}
PyVar* FastLocals__try_get_by_name(PyVar* locals, const CodeObject* co, StrName name){
PyVar* FastLocals__try_get_by_name(PyVar* locals, const CodeObject* co, py_Name name){
int index = c11_smallmap_n2i__get(&co->varnames_inv, name, -1);
if(index == -1) return NULL;
return &locals[index];
@ -39,14 +39,14 @@ void UnwindTarget__delete(UnwindTarget* self){
free(self);
}
Frame* Frame__new(const CodeObject* co, PyObject* module, PyObject* function, PyVar* p0, PyVar* locals, const CodeObject* locals_co){
Frame* Frame__new(const CodeObject* co, const PyVar* module, const PyVar* function, PyVar* p0, PyVar* locals, const CodeObject* locals_co){
static_assert(sizeof(Frame) <= kPoolFrameBlockSize, "!(sizeof(Frame) <= kPoolFrameBlockSize)");
Frame* self = PoolFrame_alloc();
self->f_back = NULL;
self->ip = (Bytecode*)co->codes.data - 1;
self->co = co;
self->module = module;
self->function = function;
self->module = module->_obj;
self->function = function->_obj;
self->p0 = p0;
self->locals = locals;
self->locals_co = locals_co;

View File

@ -88,21 +88,23 @@ int pk_ManagedHeap__sweep(pk_ManagedHeap *self){
return freed;
}
PyObject* pk_ManagedHeap__new(pk_ManagedHeap *self, Type type, int size){
PyObject* obj = PyObject__new(type, size);
PyObject* pk_ManagedHeap__new(pk_ManagedHeap *self, Type type, int slots, int size){
PyObject* obj = PyObject__new(type, slots, size);
c11_vector__push(PyObject*, &self->no_gc, obj);
return obj;
}
PyObject* pk_ManagedHeap__gcnew(pk_ManagedHeap *self, Type type, int size){
PyObject* obj = PyObject__new(type, size);
PyObject* pk_ManagedHeap__gcnew(pk_ManagedHeap *self, Type type, int slots, int size){
PyObject* obj = PyObject__new(type, slots, size);
c11_vector__push(PyObject*, &self->gen, obj);
self->gc_counter++;
return obj;
}
PyObject* PyObject__new(Type type, int size){
PyObject* PyObject__new(Type type, int slots, int size){
assert(slots >= 0 || slots == -1);
PyObject* self;
size += PK_OBJ_HEADER_SIZE(slots);
if(size <= kPoolObjectBlockSize){
self = PoolObject_alloc();
self->gc_is_large = false;
@ -112,6 +114,6 @@ PyObject* PyObject__new(Type type, int size){
}
self->type = type;
self->gc_marked = false;
self->dict = NULL;
self->slots = slots;
return self;
}

View File

@ -15,7 +15,7 @@ static void pk_default_stderr(const char* s){
fflush(stderr);
}
void pk_TypeInfo__ctor(pk_TypeInfo *self, StrName name, Type base, PyObject* obj, const PyVar* module, bool subclass_enabled){
void pk_TypeInfo__ctor(pk_TypeInfo *self, py_Name name, Type base, PyObject* obj, const PyVar* module, bool subclass_enabled){
memset(self, 0, sizeof(pk_TypeInfo));
self->name = name;
@ -25,14 +25,14 @@ void pk_TypeInfo__ctor(pk_TypeInfo *self, StrName name, Type base, PyObject* obj
self->module = module ? *module : PY_NULL;
self->subclass_enabled = subclass_enabled;
c11_vector__ctor(&self->annotated_fields, sizeof(StrName));
c11_vector__ctor(&self->annotated_fields, sizeof(py_Name));
}
void pk_TypeInfo__dtor(pk_TypeInfo *self){
c11_vector__dtor(&self->annotated_fields);
}
static int _hello(const PyVar* args, int argc){
static int _hello(const py_Ref args, int argc){
return 0;
}
@ -40,7 +40,7 @@ static void do_builtin_bindings(){
pk_VM* vm = pk_current_vm;
py_new_nativefunc(&vm->reg[0], _hello, 2, BindType_FUNCTION);
py_setattr(&vm->builtins, pk_StrName__map("hello"), &vm->reg[0]);
py_setdict(&vm->builtins, py_name("hello"), &vm->reg[0]);
}
void pk_VM__ctor(pk_VM* self){
@ -69,19 +69,19 @@ void pk_VM__ctor(pk_VM* self){
ValueStack__ctor(&self->stack);
self->True = (PyVar){.type=tp_bool, .is_ptr=true, .extra=1,
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 1),
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 1),
};
self->False = (PyVar){.type=tp_bool, .is_ptr=true, .extra=0,
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 1),
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 1),
};
self->None = (PyVar){.type=tp_none_type, .is_ptr=true,
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_none_type, 1),
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_none_type, 0, 1),
};
self->NotImplemented = (PyVar){.type=tp_not_implemented_type, .is_ptr=true,
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_not_implemented_type, 1),
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_not_implemented_type, 0, 1),
};
self->Ellipsis = (PyVar){.type=tp_ellipsis, .is_ptr=true,
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_ellipsis, 1),
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_ellipsis, 0, 1),
};
/* Init Builtin Types */
@ -132,7 +132,7 @@ void pk_VM__ctor(pk_VM* self){
#undef validate
self->StopIteration = c11__at(pk_TypeInfo, &self->types, tp_stop_iteration)->self;
self->builtins = py_new_module("builtins");
self->builtins = *py_new_module("builtins");
/* Setup Public Builtin Types */
Type public_types[] = {
@ -147,13 +147,13 @@ void pk_VM__ctor(pk_VM* self){
for(int i=0; i<PK_ARRAY_COUNT(public_types); i++){
Type t = public_types[i];
pk_TypeInfo* ti = c11__at(pk_TypeInfo, &self->types, t);
pk_NameDict__set(self->builtins._obj->dict, ti->name, ti->self);
py_setdict(&self->builtins, ti->name, &ti->self);
}
pk_NameDict__set(self->builtins._obj->dict, pk_StrName__map("NotImplemented"), self->NotImplemented);
py_setdict(&self->builtins, py_name("NotImplemented"), &self->NotImplemented);
/* Do Buildin Bindings*/
do_builtin_bindings();
self->main = py_new_module("__main__");
self->main = *py_new_module("__main__");
}
void pk_VM__dtor(pk_VM* self){
@ -186,17 +186,18 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self){
Type pk_VM__new_type(pk_VM* self, const char* name, Type base, const PyVar* module, bool subclass_enabled){
Type type = self->types.count;
pk_TypeInfo* ti = c11_vector__emplace(&self->types);
PyObject* typeobj = pk_ManagedHeap__gcnew(&self->heap, tp_type, PK_OBJ_SIZEOF(Type));
*PyObject__as(Type, typeobj) = type;
pk_TypeInfo__ctor(ti, pk_StrName__map(name), base, typeobj, module, subclass_enabled);
PyObject* typeobj = pk_ManagedHeap__gcnew(&self->heap, tp_type, 0, sizeof(Type));
Type* value = PyObject__value(typeobj);
*value = type;
pk_TypeInfo__ctor(ti, py_name(name), base, typeobj, module, subclass_enabled);
return type;
}
/****************************************/
void PyObject__delete(PyObject *self){
pk_TypeInfo* ti = c11__getitem(pk_TypeInfo*, &pk_current_vm->types, self->type);
if(ti->dtor) ti->dtor(PyObject__value_ptr(self));
if(self->dict) pk_NameDict__delete(self->dict);
if(ti->dtor) ti->dtor(PyObject__value(self));
if(self->slots == -1) pk_NameDict__dtor(PyObject__dict(self));
if(self->gc_is_large){
free(self);
}else{

View File

@ -1,5 +1,6 @@
#include "pocketpy/objects/dict.h"
#include "pocketpy/common/utils.h"
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
@ -113,14 +114,18 @@ static void pkpy_Dict__extendht(pkpy_Dict* self) {
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i);
if(pkpy_Var__is_null(&entry->key)) continue;
int rhash = DICT_HASH_TRANS(py_hash(&entry->key));
int64_t out;
int err = py_hash(&entry->key, &out);
int rhash = DICT_HASH_TRANS(out);
int h = pkpy_Dict__probe0(self, entry->key, rhash);
pkpy_Dict__htset(self, h, i);
}
}
bool pkpy_Dict__set(pkpy_Dict* self, PyVar key, PyVar val) {
int hash = DICT_HASH_TRANS(py_hash(&key));
int64_t out;
int err = py_hash(&key, &out);
int hash = DICT_HASH_TRANS(out);
int h = pkpy_Dict__probe1(self, key, hash);
int idx = pkpy_Dict__htget(self, h);
@ -155,7 +160,9 @@ bool pkpy_Dict__set(pkpy_Dict* self, PyVar key, PyVar val) {
}
bool pkpy_Dict__contains(const pkpy_Dict* self, PyVar key) {
int hash = DICT_HASH_TRANS(py_hash(&key));
int64_t out;
int err = py_hash(&key, &out);
int hash = DICT_HASH_TRANS(out);
int h = pkpy_Dict__probe1(self, key, hash);
int idx = pkpy_Dict__htget(self, h);
@ -187,7 +194,9 @@ static bool pkpy_Dict__refactor(pkpy_Dict* self) {
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i);
if(pkpy_Var__is_null(&entry->key)) continue;
int rhash = DICT_HASH_TRANS(py_hash(&entry->key));
int64_t out;
py_hash(&entry->key, &out);
int rhash = DICT_HASH_TRANS(out);
int h = pkpy_Dict__probe0(self, entry->key, rhash);
pkpy_Dict__htset(self, h, i);
}
@ -195,7 +204,9 @@ static bool pkpy_Dict__refactor(pkpy_Dict* self) {
}
bool pkpy_Dict__del(pkpy_Dict* self, PyVar key) {
int hash = DICT_HASH_TRANS(py_hash(&key));
int64_t out;
int err = py_hash(&key, &out);
int hash = DICT_HASH_TRANS(out);
int h = pkpy_Dict__probe1(self, key, hash);
int idx = pkpy_Dict__htget(self, h), null = pkpy_Dict__idx_null(self);
if(idx == null) return false;
@ -208,7 +219,9 @@ bool pkpy_Dict__del(pkpy_Dict* self, PyVar key) {
}
const PyVar *pkpy_Dict__try_get(const pkpy_Dict* self, PyVar key) {
int hash = DICT_HASH_TRANS(py_hash(&key));
int64_t out;
int err = py_hash(&key, &out);
int hash = DICT_HASH_TRANS(out);
int h = pkpy_Dict__probe1(self, key, hash);
int idx = pkpy_Dict__htget(self, h);

View File

@ -1,4 +1,6 @@
#include "pocketpy/objects/object.h"
#include "pocketpy/pocketpy.h"
#include <assert.h>
void PyVar__ctor3(PyVar* self, PyObject* existing){
assert(existing);
@ -6,3 +8,17 @@ void PyVar__ctor3(PyVar* self, PyObject* existing){
self->is_ptr = true;
self->_obj = existing;
}
void* PyObject__value(PyObject* self){
return (char*)self + PK_OBJ_HEADER_SIZE(self->slots);
}
pk_NameDict* PyObject__dict(PyObject* self){
assert(self->slots == -1);
return (pk_NameDict*)((char*)self + 8);
}
PyVar* PyObject__slots(PyObject* self){
assert(self->slots >= 0);
return (PyVar*)((char*)self + 8);
}

View File

@ -14,12 +14,12 @@ void py_initialize(){
pk_VM__ctor(&pk_default_vm);
}
py_Error* py_exec_simple(const char* source){
int py_exec_simple(const char* source){
CodeObject* co = NULL;
pk_VM* vm = pk_current_vm;
Frame* frame = Frame__new(
co,
vm->main,
&vm->main,
NULL,
vm->stack.sp,
vm->stack.sp,
@ -27,11 +27,19 @@ py_Error* py_exec_simple(const char* source){
);
pk_VM__push_frame(vm, frame);
pk_FrameResult res = pk_VM__run_top_frame(vm);
if(res == RES_ERROR) return vm->last_error;
if(res == RES_RETURN) return NULL; // vm->last_retval;
if(res == RES_ERROR) return vm->last_error->type;
if(res == RES_RETURN) return 0; // vm->last_retval;
assert(0); // unreachable
}
py_Error* py_getlasterror(){
return pk_current_vm->last_error;
}
void py_Error__print(py_Error* self){
abort();
}
void py_finalize(){
pk_VM__dtor(&pk_default_vm);
pk_current_vm = NULL;
@ -39,31 +47,17 @@ void py_finalize(){
Pools_finalize();
}
void py_newint(PyVar* self, int64_t val){
void py_setdict(py_Ref self, py_Name name, const py_Ref val){
pk_NameDict__set(
PyObject__dict(self->_obj),
name,
*val
);
}
void py_newint(py_Ref self, int64_t val){
self->type = tp_int;
self->is_ptr = false;
self->_i64 = val;
}
void py_newfloat(PyVar* self, double val){
self->type = tp_float;
self->is_ptr = false;
self->_f64 = val;
}
void py_newbool(PyVar* self, bool val){
// return a global singleton
}
void py_newnone(PyVar* self){
// return a heap object
}
void py_newstr(PyVar* self, const char* val){
// return a heap object
}
void py_newstr2(PyVar*, const char*, int);
void py_newbytes(PyVar*, const uint8_t*, int);

View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "pocketpy.h"
#include "pocketpy/pocketpy.h"
char* read_file(const char* path) {
FILE* file = fopen(path, "r");
@ -27,10 +28,9 @@ int main(int argc, char** argv) {
char* source = read_file(argv[1]);
py_initialize();
py_Error* err = py_exec_simple(source);
if(err){
if(py_exec_simple(source)){
py_Error* err = py_getlasterror();
py_Error__print(err);
py_Error__delete(err);
}
py_finalize();