This commit is contained in:
blueloveTH 2024-06-26 11:28:38 +08:00
parent 13b1fbe17a
commit 73bca886b5
6 changed files with 31 additions and 7 deletions

View File

@ -5,7 +5,8 @@ python prebuild.py
SRC=$(find src/ -name "*.c") SRC=$(find src/ -name "*.c")
FLAGS="-std=c11 -lm -Iinclude -O0 -Wfatal-errors -g -DDEBUG -DPK_ENABLE_OS=1" FLAGS="-std=c11 -lm -Iinclude -O0 -Wfatal-errors -g -DDEBUG -DPK_ENABLE_OS=1"
SANITIZE_FLAGS="-fsanitize=address,leak,undefined" # SANITIZE_FLAGS="-fsanitize=address,leak,undefined"
SANITIZE_FLAGS=""
echo "Compiling C files..." echo "Compiling C files..."
clang $FLAGS $SANITIZE_FLAGS $SRC src2/main.c -o main clang $FLAGS $SANITIZE_FLAGS $SRC src2/main.c -o main

View File

@ -66,8 +66,6 @@ typedef struct pk_VM {
FuncDecl_ __dynamic_func_decl; FuncDecl_ __dynamic_func_decl;
PyVar __vectorcall_buffer[PK_MAX_CO_VARNAMES]; PyVar __vectorcall_buffer[PK_MAX_CO_VARNAMES];
void* userdata; // for user-defined data (unused by pkpy itself)
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
} pk_VM; } pk_VM;

View File

@ -51,10 +51,10 @@ void py_newtuple(py_Ref, int);
// new style decl-based function // new style decl-based function
void py_newfunction(py_Ref self, py_CFunction, const char* sig); void py_newfunction(py_Ref self, py_CFunction, const char* sig);
void py_newfunction2(py_Ref self, py_CFunction, const char* sig, BindType bt, const char* docstring, const py_Ref userdata); void py_newfunction2(py_Ref self, py_CFunction, const char* sig, BindType bt, const char* docstring, const py_Ref upvalue);
// old style argc-based function // old style argc-based function
void py_newnativefunc(py_Ref self, py_CFunction, int argc); void py_newnativefunc(py_Ref self, py_CFunction, int argc);
void py_newnativefunc2(py_Ref self, py_CFunction, int argc, BindType bt, const char* docstring, const py_Ref userdata); void py_newnativefunc2(py_Ref self, py_CFunction, int argc, BindType bt, const char* docstring, const py_Ref upvalue);
/************* Stack Values Creation *************/ /************* Stack Values Creation *************/
void py_pushint(int64_t); void py_pushint(int64_t);
@ -73,6 +73,8 @@ bool py_tobool(py_Ref);
const py_Str* py_tostr(py_Ref); const py_Str* py_tostr(py_Ref);
const char* py_tocstr(py_Ref); const char* py_tocstr(py_Ref);
void* py_touserdata(py_Ref);
#define py_isint(self) py_istype(self, tp_int) #define py_isint(self) py_istype(self, tp_int)
#define py_isfloat(self) py_istype(self, tp_float) #define py_isfloat(self) py_istype(self, tp_float)
#define py_isbool(self) py_istype(self, tp_bool) #define py_isbool(self) py_istype(self, tp_bool)
@ -89,6 +91,9 @@ void py_setdict(py_Ref self, py_Name name, const py_Ref val);
py_Ref py_getslot(const py_Ref self, int i); py_Ref py_getslot(const py_Ref self, int i);
void py_setslot(py_Ref self, int i, const py_Ref val); void py_setslot(py_Ref self, int i, const py_Ref val);
py_Ref py_getupvalue(py_Ref self);
void py_setupvalue(py_Ref self, const py_Ref val);
// int py_getattr(const py_Ref self, py_Name name, py_Ref out); // int py_getattr(const py_Ref self, py_Name name, py_Ref out);
// int py_setattr(py_Ref self, py_Name name, const py_Ref val); // int py_setattr(py_Ref self, py_Name name, const py_Ref val);
@ -100,6 +105,8 @@ py_Ref py_gettop();
void py_settop(const py_Ref); void py_settop(const py_Ref);
py_Ref py_getsecond(); py_Ref py_getsecond();
void py_setsecond(const py_Ref); void py_setsecond(const py_Ref);
void py_duptop();
void py_dupsecond();
/// Returns a reference to the i-th object from the top of the stack. /// Returns a reference to the i-th object from the top of the stack.
/// i should be negative, e.g. (-1) means TOS. /// i should be negative, e.g. (-1) means TOS.
py_Ref py_peek(int i); py_Ref py_peek(int i);

View File

@ -41,6 +41,11 @@ const char* py_tocstr(py_Ref self){
return py_Str__data(s); return py_Str__data(s);
} }
void* py_touserdata(py_Ref self){
assert(self && self->is_ptr);
return PyObject__value(self->_obj);
}
bool py_istype(const py_Ref self, py_Type type){ bool py_istype(const py_Ref self, py_Type type){
return self->type == type; return self->type == type;
} }

View File

@ -4,6 +4,7 @@
#include "pocketpy/objects/object.h" #include "pocketpy/objects/object.h"
#include "pocketpy/interpreter/vm.h" #include "pocketpy/interpreter/vm.h"
py_Ref py_getdict(const py_Ref self, py_Name name){ py_Ref py_getdict(const py_Ref self, py_Name name){
assert(self && self->is_ptr); assert(self && self->is_ptr);
return pk_NameDict__try_get(PyObject__dict(self->_obj), name); return pk_NameDict__try_get(PyObject__dict(self->_obj), name);
@ -47,6 +48,18 @@ void py_setsecond(const py_Ref val){
pk_current_vm->stack.sp[-2] = *val; pk_current_vm->stack.sp[-2] = *val;
} }
void py_duptop(){
pk_VM* vm = pk_current_vm;
*vm->stack.sp = vm->stack.sp[-1];
vm->stack.sp++;
}
void py_dupsecond(){
pk_VM* vm = pk_current_vm;
*vm->stack.sp = vm->stack.sp[-2];
vm->stack.sp++;
}
py_Ref py_peek(int i){ py_Ref py_peek(int i){
assert(i < 0); assert(i < 0);
return pk_current_vm->stack.sp + i; return pk_current_vm->stack.sp + i;

View File

@ -63,7 +63,7 @@ void py_newfunction2(py_Ref self,
const char* sig, const char* sig,
BindType bt, BindType bt,
const char* docstring, const char* docstring,
const py_Ref userdata) {} const py_Ref upvalue) {}
void py_newnativefunc(py_Ref self, py_CFunction f, int argc) { void py_newnativefunc(py_Ref self, py_CFunction f, int argc) {
py_newnativefunc2(self, f, argc, BindType_FUNCTION, NULL, NULL); py_newnativefunc2(self, f, argc, BindType_FUNCTION, NULL, NULL);
@ -74,7 +74,7 @@ void py_newnativefunc2(py_Ref self,
int argc, int argc,
BindType bt, BindType bt,
const char* docstring, const char* docstring,
const py_Ref userdata) {} const py_Ref upvalue) {}
void py_pushint(int64_t val) { py_newint(pk_current_vm->stack.sp++, val); } void py_pushint(int64_t val) { py_newint(pk_current_vm->stack.sp++, val); }