diff --git a/include/pocketpy/interpreter/vm.h b/include/pocketpy/interpreter/vm.h index 903d5347..b25fcda7 100644 --- a/include/pocketpy/interpreter/vm.h +++ b/include/pocketpy/interpreter/vm.h @@ -12,8 +12,8 @@ typedef struct pk_TypeInfo{ StrName name; Type base; - PyObject* obj; // the type object itself - PyObject* module; // the module where the type is defined + PyVar self; // the type object itself + PyVar module; // the module where the type is defined bool subclass_enabled; void (*dtor)(void*); @@ -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, PyObject* module, bool subclass_enabled); +void pk_TypeInfo__ctor(pk_TypeInfo* self, StrName name, Type base, PyObject* obj, const PyVar* module, bool subclass_enabled); void pk_TypeInfo__dtor(pk_TypeInfo* self); typedef struct pk_VM { @@ -47,14 +47,14 @@ typedef struct pk_VM { pk_NameDict modules; c11_vector/*T=pk_TypeInfo*/ types; - PyObject* StopIteration; // a special Exception class - PyObject* builtins; // builtins module - PyObject* main; // __main__ module + PyVar StopIteration; // a special Exception class + PyVar builtins; // builtins module + PyVar main; // __main__ module - void (*_ceval_on_step)(struct pk_VM*, Frame*, Bytecode); - unsigned char* (*_import_file)(struct pk_VM*, const char*); - void (*_stdout)(struct pk_VM*, const char*); - void (*_stderr)(struct pk_VM*, const char*); + void (*_ceval_on_step)(Frame*, Bytecode); + unsigned char* (*_import_file)(const char*); + void (*_stdout)(const char*); + void (*_stderr)(const char*); // singleton objects PyVar True, False, None, NotImplemented, Ellipsis; @@ -62,6 +62,8 @@ typedef struct pk_VM { py_Error* last_error; // last retval PyVar last_retval; + // registers + PyVar reg[8]; PyObject* __curr_class; PyObject* __cached_object_new; @@ -81,16 +83,15 @@ void pk_VM__push_frame(pk_VM* self, Frame* frame); void pk_VM__pop_frame(pk_VM* self); typedef enum pk_FrameResult{ - RES_ERROR, + RES_RETURN, RES_CALL, RES_YIELD, - RES_RETURN + RES_ERROR, } pk_FrameResult; pk_FrameResult pk_VM__run_top_frame(pk_VM* self); -Type pk_VM__new_type(pk_VM* self, const char* name, Type base, PyObject* module, bool subclass_enabled); -PyObject* pk_VM__new_module(pk_VM* self, const char* name, const char* package); +Type pk_VM__new_type(pk_VM* self, const char* name, Type base, const PyVar* module, bool subclass_enabled); #ifdef __cplusplus } diff --git a/include/pocketpy/interpreter/vm.hpp b/include/pocketpy/interpreter/vm.hpp deleted file mode 100644 index 0eef3b5f..00000000 --- a/include/pocketpy/interpreter/vm.hpp +++ /dev/null @@ -1,719 +0,0 @@ -#pragma once - -#include "pocketpy/objects/object.h" -#include "pocketpy/objects/base.h" -#include "pocketpy/objects/dict.hpp" -#include "pocketpy/objects/error.hpp" -#include "pocketpy/objects/builtins.hpp" -#include "pocketpy/interpreter/gc.h" -#include "pocketpy/interpreter/frame.h" -#include "pocketpy/interpreter/profiler.hpp" - -#include - -namespace pkpy { - -/* Stack manipulation macros */ -// https://github.com/python/cpython/blob/3.9/Python/ceval.c#L1123 -#define TOP() (s_data.sp[-1]) -#define SECOND() (s_data.sp[-2]) -#define THIRD() (s_data.sp[-3]) -#define STACK_SHRINK(n) (s_data.sp -= (n)) -#define PUSH(v) (*s_data.sp++ = (v)) -#define PUSH_NULL() memset(s_data.sp++, 0, sizeof(PyVar)) -#define POP() (--s_data.sp) -#define POPX() (*--s_data.sp) -#define STACK_VIEW(n) (ArgsView(s_data.sp - (n), s_data.sp)) - -typedef PyVar (*BinaryFuncC)(VM*, PyVar, PyVar); -typedef void (*RegisterFunc)(VM*, PyObject*, PyObject*); - -#if PK_ENABLE_PROFILER -struct NextBreakpoint { - int callstack_size; - int lineno; - bool should_step_into; - - NextBreakpoint() : callstack_size(0) {} - - NextBreakpoint(int callstack_size, int lineno, bool should_step_into) : - callstack_size(callstack_size), lineno(lineno), should_step_into(should_step_into) {} - - void _step(VM* vm); - - bool empty() const { return callstack_size == 0; } -}; -#endif - -struct PyTypeInfo { - struct Vt { - void (*_dtor)(void*); - void (*_gc_mark)(void*, VM*); - - Vt() : _dtor(nullptr), _gc_mark(nullptr) {} - - operator bool () const { return _dtor || _gc_mark; } - - template - inline static Vt get() { - static_assert(std::is_same_v>); - Vt vt; - if constexpr(!std::is_trivially_destructible_v) { - vt._dtor = [](void* p) { - ((T*)p)->~T(); - }; - } - if constexpr(has_gc_marker::value) { - vt._gc_mark = [](void* p, VM* vm) { - ((T*)p)->_gc_mark(vm); - }; - } - return vt; - } - }; - - PyObject* obj; // never be garbage collected - Type base; - PyObject* mod; // never be garbage collected - StrName name; - bool subclass_enabled; - Vt vt; - - PyTypeInfo(PyObject* obj, Type base, PyObject* mod, StrName name, bool subclass_enabled, Vt vt = {}) : - obj(obj), base(base), mod(mod), name(name), subclass_enabled(subclass_enabled), vt(vt) {} - - vector annotated_fields; - - // unary operators - Str (*m__repr__)(VM* vm, PyVar) = nullptr; - Str (*m__str__)(VM* vm, PyVar) = nullptr; - i64 (*m__hash__)(VM* vm, PyVar) = nullptr; - i64 (*m__len__)(VM* vm, PyVar) = nullptr; - PyVar (*m__iter__)(VM* vm, PyVar) = nullptr; - unsigned (*op__next__)(VM* vm, PyVar) = nullptr; - PyVar (*m__neg__)(VM* vm, PyVar) = nullptr; - PyVar (*m__invert__)(VM* vm, PyVar) = nullptr; - - BinaryFuncC m__eq__ = nullptr; - BinaryFuncC m__lt__ = nullptr; - BinaryFuncC m__le__ = nullptr; - BinaryFuncC m__gt__ = nullptr; - BinaryFuncC m__ge__ = nullptr; - BinaryFuncC m__contains__ = nullptr; - - // binary operators - BinaryFuncC m__add__ = nullptr; - BinaryFuncC m__sub__ = nullptr; - BinaryFuncC m__mul__ = nullptr; - BinaryFuncC m__truediv__ = nullptr; - BinaryFuncC m__floordiv__ = nullptr; - BinaryFuncC m__mod__ = nullptr; - BinaryFuncC m__pow__ = nullptr; - BinaryFuncC m__matmul__ = nullptr; - - BinaryFuncC m__lshift__ = nullptr; - BinaryFuncC m__rshift__ = nullptr; - BinaryFuncC m__and__ = nullptr; - BinaryFuncC m__or__ = nullptr; - BinaryFuncC m__xor__ = nullptr; - - // indexer - PyVar (*m__getitem__)(VM* vm, PyVar, PyVar) = nullptr; - void (*m__setitem__)(VM* vm, PyVar, PyVar, PyVar) = nullptr; - void (*m__delitem__)(VM* vm, PyVar, PyVar) = nullptr; - - // attributes - void (*m__setattr__)(VM* vm, PyVar, StrName, PyVar) = nullptr; - PyVar (*m__getattr__)(VM* vm, PyVar, StrName) = nullptr; - bool (*m__delattr__)(VM* vm, PyVar, StrName) = nullptr; - - // backdoors - void (*on_end_subclass)(VM* vm, PyTypeInfo*) = nullptr; -}; - -struct ImportContext { - PK_ALWAYS_PASS_BY_POINTER(ImportContext) - - vector pending; - vector pending_is_init; // a.k.a __init__.py - - ImportContext() {} - - struct Temp { - PK_ALWAYS_PASS_BY_POINTER(Temp) - - ImportContext* ctx; - - Temp(ImportContext* ctx, Str name, bool is_init) : ctx(ctx) { - ctx->pending.push_back(name); - ctx->pending_is_init.push_back(is_init); - } - - ~Temp() { - ctx->pending.pop_back(); - ctx->pending_is_init.pop_back(); - } - }; - - Temp scope(Str name, bool is_init) { return {this, name, is_init}; } -}; - -class VM { - PK_ALWAYS_PASS_BY_POINTER(VM) - - VM* vm; // self reference to simplify code - -public: - pk_ManagedHeap heap; - Frame* top_frame; - vector _all_types; - - pk_NameDict _modules; // loaded modules - small_map _lazy_modules; // lazy loaded modules - - struct { - PyObject* error; - vector s_view; - } __c; - - PyObject* StopIteration; // a special Exception class - PyObject* builtins; - PyObject* _main; - - // typeid -> Type - small_map _cxx_typeid_map; - // this is for repr() recursion detection (no need to mark) - vector _repr_recursion_set; - - ImportContext __import_context; - PyObject* __last_exception; - PyObject* __curr_class; - PyVar __cached_object_new; - FuncDecl_ __dynamic_func_decl; - PyVar __vectorcall_buffer[PK_MAX_CO_VARNAMES]; - -#if PK_ENABLE_PROFILER - LineProfiler* _profiler = nullptr; - NextBreakpoint _next_breakpoint; -#endif - - void (*_ceval_on_step)(VM*, Frame*, Bytecode bc); - void (*_stdout)(const char*, int); - void (*_stderr)(const char*, int); - unsigned char* (*_import_handler)(const char*, int*); - - // for quick access - constexpr static Type tp_object = Type(1), tp_type = Type(2); - constexpr static Type tp_int = Type(3), tp_float = Type(4), tp_bool = Type(5), - tp_str = Type(6); - constexpr static Type tp_list = Type(7), tp_tuple = Type(8); - constexpr static Type tp_slice = Type(9), tp_range = Type(10), tp_module = Type(11); - constexpr static Type tp_function = Type(12), tp_native_func = Type(13), tp_bound_method = Type(14); - constexpr static Type tp_super = Type(15), tp_exception = Type(16), tp_bytes = Type(17), tp_mappingproxy = Type(18); - constexpr static Type tp_dict = Type(19), tp_property = Type(20), tp_star_wrapper = Type(21); - constexpr static Type tp_staticmethod = Type(22), tp_classmethod = Type(23); - constexpr static Type tp_none_type = Type(24), tp_not_implemented_type = Type(25); - constexpr static Type tp_ellipsis = Type(26); - - PyVar True; - PyVar False; - PyVar None; - PyVar NotImplemented; - PyVar Ellipsis; - - const bool enable_os; - - // put s_data at the end of VM struct to improve cache locality - ValueStack s_data; - - VM(bool enable_os = true); - - // clang-format off -#if PK_REGION("Python Equivalents") - Str py_str(PyVar obj); // x -> str(x) - Str py_repr(PyVar obj); // x -> repr(x) - Str py_json(PyVar obj); // x -> json.dumps(x) - - PyVar py_iter(PyVar obj); // x -> iter(x) - PyVar py_next(PyVar); // x -> next(x) - PyVar _py_next(const PyTypeInfo*, PyVar); // x -> next(x) with type info cache - PyObject* py_import(Str path, bool throw_err=true); // x -> __import__(x) - PyVar py_negate(PyVar obj); // x -> -x - - List py_list(PyVar); // x -> list(x) - bool py_callable(PyVar obj); // x -> callable(x) - bool py_bool(PyVar obj){ // x -> bool(x) - if(obj.type == tp_bool) return (bool)obj.extra; - return __py_bool_non_trivial(obj); - } - i64 py_hash(PyVar obj); // x -> hash(x) - - bool py_eq(PyVar lhs, PyVar rhs); // (lhs, rhs) -> lhs == rhs - bool py_lt(PyVar lhs, PyVar rhs); // (lhs, rhs) -> lhs < rhs - bool py_le(PyVar lhs, PyVar rhs); // (lhs, rhs) -> lhs <= rhs - bool py_gt(PyVar lhs, PyVar rhs); // (lhs, rhs) -> lhs > rhs - bool py_ge(PyVar lhs, PyVar rhs); // (lhs, rhs) -> lhs >= rhs - bool py_ne(PyVar lhs, PyVar rhs){ // (lhs, rhs) -> lhs != rhs - return !py_eq(lhs, rhs); - } - - PyVar py_op(const char* name); // (name) -> operator.name - - void py_exec(const char*, PyVar, PyVar); // exec(source, globals, locals) - PyVar py_eval(const char*, PyVar, PyVar); // eval(source, globals, locals) -#endif - -#if PK_REGION("Utility Methods") - ArgsView cast_array_view(PyVar obj); - void set_main_argv(int argc, char** argv); - i64 normalized_index(i64 index, int size); - Str disassemble(CodeObject* co); - void parse_int_slice(const Slice& s, int length, int& start, int& stop, int& step); - void obj_gc_mark(PyVar obj) { if(obj.is_ptr) __obj_gc_mark(obj._obj); } - void obj_gc_mark(PyObject* p) { if(p) __obj_gc_mark(p); } -#endif - -#if PK_REGION("Name Lookup Methods") - PyVar* find_name_in_mro(Type cls, StrName name); - PyVar get_unbound_method(PyVar obj, StrName name, PyVar* self, bool throw_err=true, bool fallback=false); - PyVar getattr(PyVar obj, StrName name, bool throw_err=true); - void delattr(PyVar obj, StrName name); - void setattr(PyVar obj, StrName name, PyVar value); -#endif - -#if PK_REGION("Source Execution Methods") - CodeObject* compile(std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope=false); - Str precompile(std::string_view source, const Str& filename, CompileMode mode); - PyVar exec(std::string_view source, Str filename, CompileMode mode, PyObject* _module=nullptr); - PyVar exec(std::string_view source); - PyVar eval(std::string_view source); - - template - PyVar _exec(Args&&... args){ - callstack.emplace(s_data.sp, std::forward(args)...); - return __run_top_frame(); - } -#endif - -#if PK_REGION("Invocation Methods") - PyVar vectorcall(int ARGC, int KWARGC=0, bool op_call=false); - - template - PyVar call(PyVar callable, Args&&... args){ - PUSH(callable); PUSH_NULL(); - __push_varargs(args...); - return vectorcall(sizeof...(args)); - } - - template - PyVar call_method(PyVar self, PyVar callable, Args&&... args){ - PUSH(callable); PUSH(self); - __push_varargs(args...); - return vectorcall(sizeof...(args)); - } - - template - PyVar call_method(PyVar self, StrName name, Args&&... args){ - PyVar callable = get_unbound_method(self, name, &self); - return call_method(self, callable, args...); - } -#endif - -#if PK_REGION("Logging Methods") - virtual void stdout_write(const Str& s){ _stdout(s.c_str(), s.size); } - virtual void stderr_write(const Str& s){ _stderr(s.c_str(), s.size); } -#endif - -#if PK_REGION("Magic Bindings") - void bind__repr__(Type type, Str (*f)(VM*, PyVar)); - void bind__str__(Type type, Str (*f)(VM*, PyVar)); - void bind__iter__(Type type, PyVar (*f)(VM*, PyVar)); - - void bind__next__(Type type, unsigned (*f)(VM*, PyVar)); - [[deprecated]] void bind__next__(Type type, PyVar (*f)(VM*, PyVar)); - void bind__neg__(Type type, PyVar (*f)(VM*, PyVar)); - void bind__invert__(Type type, PyVar (*f)(VM*, PyVar)); - void bind__hash__(Type type, i64 (*f)(VM* vm, PyVar)); - void bind__len__(Type type, i64 (*f)(VM* vm, PyVar)); - - void bind__eq__(Type type, BinaryFuncC f); - void bind__lt__(Type type, BinaryFuncC f); - void bind__le__(Type type, BinaryFuncC f); - void bind__gt__(Type type, BinaryFuncC f); - void bind__ge__(Type type, BinaryFuncC f); - void bind__contains__(Type type, BinaryFuncC f); - - void bind__add__(Type type, BinaryFuncC f); - void bind__sub__(Type type, BinaryFuncC f); - void bind__mul__(Type type, BinaryFuncC f); - void bind__truediv__(Type type, BinaryFuncC f); - void bind__floordiv__(Type type, BinaryFuncC f); - void bind__mod__(Type type, BinaryFuncC f); - void bind__pow__(Type type, BinaryFuncC f); - void bind__matmul__(Type type, BinaryFuncC f); - - void bind__lshift__(Type type, BinaryFuncC f); - void bind__rshift__(Type type, BinaryFuncC f); - void bind__and__(Type type, BinaryFuncC f); - void bind__or__(Type type, BinaryFuncC f); - void bind__xor__(Type type, BinaryFuncC f); - - void bind__getitem__(Type type, PyVar (*f)(VM*, PyVar, PyVar)); - void bind__setitem__(Type type, void (*f)(VM*, PyVar, PyVar, PyVar)); - void bind__delitem__(Type type, void (*f)(VM*, PyVar, PyVar)); -#endif - -#if PK_REGION("General Bindings") - PyObject* bind_func(PyObject* obj, StrName name, int argc, NativeFuncC fn, any userdata={}, BindType bt=BindType_FUNCTION); - PyObject* bind_func(Type type, StrName name, int argc, NativeFuncC fn, any userdata={}, BindType bt=BindType_FUNCTION){ - return bind_func(_t(type), name, argc, fn, std::move(userdata), bt); - } - PyObject* bind_property(PyObject*, const char*, NativeFuncC fget, NativeFuncC fset=nullptr); - template - PyObject* bind_field(PyObject*, const char*, F T::*); - - PyObject* bind(PyObject*, const char*, NativeFuncC, any userdata={}, BindType bt=BindType_FUNCTION); - template - PyObject* bind(PyObject*, const char*, Ret(*)(Params...), BindType bt=BindType_FUNCTION); - template - PyObject* bind(PyObject*, const char*, Ret(T::*)(Params...), BindType bt=BindType_FUNCTION); - - PyObject* bind(PyObject*, const char*, const char*, NativeFuncC, any userdata={}, BindType bt=BindType_FUNCTION); - template - PyObject* bind(PyObject*, const char*, const char*, Ret(*)(Params...), BindType bt=BindType_FUNCTION); - template - PyObject* bind(PyObject*, const char*, const char*, Ret(T::*)(Params...), BindType bt=BindType_FUNCTION); -#endif - -#if PK_REGION("Error Reporting Methods") - [[noreturn]] void _error(PyVar); - [[noreturn]] void StackOverflowError() { __builtin_error("StackOverflowError"); } - [[noreturn]] void IOError(const Str& msg) { __builtin_error("IOError", msg); } - [[noreturn]] void NotImplementedError(){ __builtin_error("NotImplementedError"); } - [[noreturn]] void TypeError(const Str& msg){ __builtin_error("TypeError", msg); } - [[noreturn]] void TypeError(Type expected, Type actual) { TypeError("expected " + _type_name(vm, expected).escape() + ", got " + _type_name(vm, actual).escape()); } - [[noreturn]] void IndexError(const Str& msg){ __builtin_error("IndexError", msg); } - [[noreturn]] void ValueError(const Str& msg){ __builtin_error("ValueError", msg); } - [[noreturn]] void RuntimeError(const Str& msg){ __builtin_error("RuntimeError", msg); } - [[noreturn]] void ZeroDivisionError(const Str& msg){ __builtin_error("ZeroDivisionError", msg); } - [[noreturn]] void ZeroDivisionError(){ __builtin_error("ZeroDivisionError", "division by zero"); } - [[noreturn]] void NameError(StrName name){ __builtin_error("NameError", _S("name ", name.escape() + " is not defined")); } - [[noreturn]] void UnboundLocalError(StrName name){ __builtin_error("UnboundLocalError", _S("local variable ", name.escape() + " referenced before assignment")); } - [[noreturn]] void KeyError(PyVar obj){ __builtin_error("KeyError", obj); } - [[noreturn]] void ImportError(const Str& msg){ __builtin_error("ImportError", msg); } - [[noreturn]] void AssertionError(const Str& msg){ __builtin_error("AssertionError", msg); } - [[noreturn]] void AssertionError(){ __builtin_error("AssertionError"); } - [[noreturn]] void BinaryOptError(const char* op, PyVar _0, PyVar _1); - [[noreturn]] void AttributeError(PyVar obj, StrName name); - [[noreturn]] void AttributeError(const Str& msg){ __builtin_error("AttributeError", msg); } -#endif - -#if PK_REGION("Type Checking Methods") - bool isinstance(PyVar obj, Type base); - bool issubclass(Type cls, Type base); - void check_type(PyVar obj, Type type){ if(!is_type(obj, type)) TypeError(type, _tp(obj)); } - void check_compatible_type(PyVar obj, Type type){ if(!isinstance(obj, type)) TypeError(type, _tp(obj)); } - - Type _tp(PyVar obj){ return obj.type; } - const PyTypeInfo* _tp_info(PyVar obj) { return &_all_types[_tp(obj)]; } - const PyTypeInfo* _tp_info(Type type) { return &_all_types[type]; } - PyObject* _t(PyVar obj){ return _all_types[_tp(obj)].obj; } - PyObject* _t(Type type){ return _all_types[type].obj; } -#endif - -#if PK_REGION("User Type Registration") - PyObject* new_module(Str name, Str package=""); - PyObject* new_type_object(PyObject* mod, StrName name, Type base, bool subclass_enabled, PyTypeInfo::Vt vt={}); - - template - PyObject* new_type_object(PyObject* mod, StrName name, Type base, bool subclass_enabled){ - return new_type_object(mod, name, base, subclass_enabled, PyTypeInfo::Vt::get()); - } - - template - Type _tp_user(){ return _find_type_in_cxx_typeid_map(); } - template - bool is_user_type(PyVar obj){ return _tp(obj) == _tp_user(); } - - // template - // PyObject* register_user_class(PyObject*, StrName, RegisterFunc, Type base=tp_object, bool subclass_enabled=false); - // template - // PyObject* register_user_class(PyObject*, StrName, Type base=tp_object, bool subclass_enabled=false); - - // template - // PyVar new_user_object(Args&&... args){ - // return new_object(_tp_user(), std::forward(args)...); - // } - - template - PyVar new_object(Type type, Args&&... args){ - static_assert(!is_sso_v); - static_assert(std::is_same_v>); - PyObject* p = pk_ManagedHeap__new(&heap, type, py_sizeof, true); - new (PyObject__value_ptr(p)) T(std::forward(args)...); - // backdoor for important builtin types - if constexpr(std::is_same_v - || std::is_same_v - || std::is_same_v) { - p->_attr = pk_NameDict__new(); - } - return PyVar__fromobj(p); - } - - template - PyVar new_object_no_gc(Type type, Args&&... args){ - static_assert(!is_sso_v); - static_assert(std::is_same_v>); - PyObject* p = pk_ManagedHeap__new(&heap, type, py_sizeof, true); - new (PyObject__value_ptr(p)) T(std::forward(args)...); - // backdoor for important builtin types - if constexpr(std::is_same_v - || std::is_same_v - || std::is_same_v) { - p->_attr = pk_NameDict__new(); - } - return PyVar__fromobj(p); - } -#endif - - template - Type _find_type_in_cxx_typeid_map() { - auto it = _cxx_typeid_map.try_get(typeid(T)); - if(it == nullptr) PK_FATAL_ERROR("T not found in cxx_typeid_map\n") - return *it; - } - /********** old heap op **********/ - struct HeapScopeLock { - PK_ALWAYS_PASS_BY_POINTER(HeapScopeLock) - pk_ManagedHeap* heap; - HeapScopeLock(pk_ManagedHeap* heap) : heap(heap) { pk_ManagedHeap__push_lock(heap);} - ~HeapScopeLock() { pk_ManagedHeap__pop_lock(heap); } - }; - HeapScopeLock gc_scope_lock(){ return {&heap}; } - /********** private **********/ - virtual ~VM(); - -#if PK_DEBUG_CEVAL_STEP - void __log_s_data(const char* title = nullptr); -#endif - PyVar __py_exec_internal(const CodeObject* code, PyVar globals, PyVar locals); - void __breakpoint(); - PyVar __format_object(PyVar, Str); - PyVar __run_top_frame(); - void __pop_frame(); - PyVar __py_generator(Frame* frame, ArgsView buffer); - void __op_unpack_sequence(uint16_t arg); - void __prepare_py_call(PyVar*, ArgsView, ArgsView, const FuncDecl*); - void __unpack_as_list(ArgsView args, List& list); - void __unpack_as_dict(ArgsView args, Dict& dict); - [[noreturn]] void __raise_exc(bool re_raise = false); - [[noreturn]] void __builtin_error(StrName type); - [[noreturn]] void __builtin_error(StrName type, PyVar arg); - [[noreturn]] void __builtin_error(StrName type, const Str& msg); - [[noreturn]] void __compile_error(Error* err); - void __init_builtin_types(); - void __post_init_builtin_types(); - void __push_varargs() {} - void __push_varargs(PyVar _0) { PUSH(_0); } - void __push_varargs(PyVar _0, PyVar _1) { - PUSH(_0); - PUSH(_1); - } - void __push_varargs(PyVar _0, PyVar _1, PyVar _2) { - PUSH(_0); - PUSH(_1); - PUSH(_2); - } - void __push_varargs(PyVar _0, PyVar _1, PyVar _2, PyVar _3) { - PUSH(_0); - PUSH(_1); - PUSH(_2); - PUSH(_3); - } - PyVar __pack_next_retval(unsigned); - PyVar __minmax_reduce(bool (VM::*op)(PyVar, PyVar), PyVar args, PyVar key); - bool __py_bool_non_trivial(PyVar); - void __obj_gc_mark(PyObject*); - void __stack_gc_mark(PyVar* begin, PyVar* end); - void* __stack_alloc(int size); -}; - -template -constexpr inline bool is_immutable_v = - is_integral_v || is_floating_point_v || std::is_same_v || std::is_same_v || - std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v || - std::is_pointer_v || std::is_enum_v; - -template constexpr Type _tp_builtin() { return Type(); } -template<> constexpr Type _tp_builtin() { return VM::tp_str; } -template<> constexpr Type _tp_builtin() { return VM::tp_list; } -template<> constexpr Type _tp_builtin() { return VM::tp_tuple; } -template<> constexpr Type _tp_builtin() { return VM::tp_function; } -template<> constexpr Type _tp_builtin() { return VM::tp_native_func; } -template<> constexpr Type _tp_builtin() { return VM::tp_bound_method; } -template<> constexpr Type _tp_builtin() { return VM::tp_range; } -template<> constexpr Type _tp_builtin() { return VM::tp_slice; } -template<> constexpr Type _tp_builtin() { return VM::tp_exception; } -template<> constexpr Type _tp_builtin() { return VM::tp_bytes; } -template<> constexpr Type _tp_builtin() { return VM::tp_mappingproxy; } -template<> constexpr Type _tp_builtin() { return VM::tp_dict; } -template<> constexpr Type _tp_builtin() { return VM::tp_property; } -template<> constexpr Type _tp_builtin() { return VM::tp_star_wrapper; } -template<> constexpr Type _tp_builtin() { return VM::tp_staticmethod; } -template<> constexpr Type _tp_builtin() { return VM::tp_classmethod; } - -// clang-format on - -template -PyVar py_var(VM* vm, __T&& value) { - using T = std::decay_t<__T>; - - static_assert(!std::is_same_v, "py_var(VM*, PyVar) is not allowed"); - - if constexpr(std::is_same_v || std::is_same_v || - std::is_same_v) { - // str (shortcuts) - return VAR(Str(std::forward<__T>(value))); - } else if constexpr(std::is_same_v) { - // NoneType - return vm->None; - } else if constexpr(std::is_same_v) { - // bool - return value ? vm->True : vm->False; - } else if constexpr(is_integral_v) { - // int - ::PyVar retval; - retval.type = tp_int; - retval.is_ptr = false; - retval._i64 = (i64)value; - return retval; - } else if constexpr(is_floating_point_v) { - // float - ::PyVar retval; - retval.type = tp_float; - retval.is_ptr = false; - retval._f64 = (f64)value; - return retval; - } else if constexpr(std::is_pointer_v) { - return from_void_p(vm, (void*)value); - } else { - constexpr Type const_type = _tp_builtin(); - if constexpr((bool)const_type) { - if constexpr(is_sso_v) - return PyVar(const_type, value); - else - return vm->new_object(const_type, std::forward<__T>(value)); - } else { - Type type = vm->_find_type_in_cxx_typeid_map(); - if constexpr(is_sso_v) - return PyVar(type, value); - else - return vm->new_object(type, std::forward<__T>(value)); - } - } -} - -// fast path for bool if py_var<> cannot be inlined -inline PyVar py_var(VM* vm, bool value) { return value ? vm->True : vm->False; } - -template -__T _py_cast__internal(VM* vm, PyVar obj) { - static_assert(!std::is_rvalue_reference_v<__T>, "rvalue reference is not allowed"); - using T = std::decay_t<__T>; - static_assert(!(is_sso_v && std::is_reference_v<__T>), "SSO types cannot be reference"); - - if constexpr(std::is_same_v || std::is_same_v) { - static_assert(!std::is_reference_v<__T>); - // str (shortcuts) - if(is_none(obj)) return nullptr; - if constexpr(with_check) vm->check_type(obj, vm->tp_str); - return PK_OBJ_GET(Str, obj).c_str(); - } else if constexpr(std::is_same_v) { - static_assert(!std::is_reference_v<__T>); - // bool - if constexpr(with_check) { - if(obj.type != tp_bool){ - vm->TypeError("expected 'bool', got " + _type_name(vm, vm->_tp(obj)).escape()); - } - } - return (bool)obj.extra; - } else if constexpr(is_integral_v) { - static_assert(!std::is_reference_v<__T>); - // int - if constexpr(with_check) { - if(!is_int(obj)) vm->TypeError("expected 'int', got " + _type_name(vm, vm->_tp(obj)).escape()); - } - return (T)obj._i64; - } else if constexpr(is_floating_point_v) { - static_assert(!std::is_reference_v<__T>); - if(is_float(obj)) return (T)obj._f64; - if(is_int(obj)) return (T)obj._i64; - vm->TypeError("expected 'int' or 'float', got " + _type_name(vm, vm->_tp(obj)).escape()); - return 0.0f; - } else if constexpr(std::is_enum_v) { - static_assert(!std::is_reference_v<__T>); - return (__T)_py_cast__internal(vm, obj); - } else if constexpr(std::is_pointer_v) { - static_assert(!std::is_reference_v<__T>); - return to_void_p(vm, obj); - } else { - constexpr Type const_type = _tp_builtin(); - if constexpr((bool)const_type) { - if constexpr(with_check) { - if constexpr(std::is_same_v) { - // Exception is `subclass_enabled` - vm->check_compatible_type(obj, const_type); - } else { - vm->check_type(obj, const_type); - } - } - return PK_OBJ_GET(T, obj); - } else { - if constexpr(with_check) { - Type type = vm->_find_type_in_cxx_typeid_map(); - vm->check_compatible_type(obj, type); - } - return PK_OBJ_GET(T, obj); - } - } -} - -template -__T py_cast(VM* vm, PyVar obj) { - return _py_cast__internal<__T, true>(vm, obj); -} - -template -__T _py_cast(VM* vm, PyVar obj) { - return _py_cast__internal<__T, false>(vm, obj); -} - -// template -// PyObject* -// VM::register_user_class(PyObject* mod, StrName name, RegisterFunc _register, Type base, bool subclass_enabled) { -// PyObject* type = new_type_object(mod, name, base, subclass_enabled, PyTypeInfo::Vt::get()); -// mod->attr().set(name, type); -// _cxx_typeid_map.insert(typeid(T), type->as()); -// _register(this, mod, type); -// if(!type->attr().contains(__new__)) { -// if constexpr(std::is_default_constructible_v) { -// bind_func(type, __new__, -1, [](VM* vm, ArgsView args) { -// Type cls_t = args[0]->as(); -// return vm->new_object(cls_t); -// }); -// } else { -// bind_func(type, __new__, -1, [](VM* vm, ArgsView args) { -// vm->NotImplementedError(); -// return vm->None; -// }); -// } -// } -// return type; -// } - -// template -// PyObject* VM::register_user_class(PyObject* mod, StrName name, Type base, bool subclass_enabled) { -// return register_user_class(mod, name, &T::_register, base, subclass_enabled); -// } - -} // namespace pkpy diff --git a/include/pocketpy/objects/base.h b/include/pocketpy/objects/base.h index 834b6633..e6a28aa2 100644 --- a/include/pocketpy/objects/base.h +++ b/include/pocketpy/objects/base.h @@ -43,7 +43,7 @@ static const Type tp_object = {1}, tp_type = {2}; static const Type tp_int = {3}, tp_float = {4}, tp_bool = {5}, tp_str = {6}; static const Type tp_list = {7}, tp_tuple = {8}; static const Type tp_slice = {9}, tp_range = {10}, tp_module = {11}; -static const Type tp_function = {12}, tp_native_func = {13}, tp_bound_method = {14}; +static const Type tp_function = {12}, tp_nativefunc = {13}, tp_bound_method = {14}; static const Type tp_super = {15}, tp_exception = {16}, tp_bytes = {17}, tp_mappingproxy = {18}; static const Type tp_dict = {19}, tp_property = {20}, tp_star_wrapper = {21}; static const Type tp_staticmethod = {22}, tp_classmethod = {23}; diff --git a/include/pocketpy/objects/codeobject.h b/include/pocketpy/objects/codeobject.h index eab24093..34904c50 100644 --- a/include/pocketpy/objects/codeobject.h +++ b/include/pocketpy/objects/codeobject.h @@ -8,6 +8,7 @@ #include "pocketpy/objects/base.h" #include "pocketpy/objects/sourcedata.h" #include "pocketpy/common/refcount.h" +#include "pocketpy/pocketpy.h" #ifdef __cplusplus extern "C" { @@ -16,12 +17,6 @@ extern "C" { #define BC_NOARG 0 #define BC_KEEPLINE -1 -typedef enum BindType { - BindType_FUNCTION, - BindType_STATICMETHOD, - BindType_CLASSMETHOD, -} BindType; - typedef enum FuncType { FuncType_UNSET, FuncType_NORMAL, diff --git a/include/pocketpy/objects/object.h b/include/pocketpy/objects/object.h index a906df06..04f39fca 100644 --- a/include/pocketpy/objects/object.h +++ b/include/pocketpy/objects/object.h @@ -22,9 +22,10 @@ static_assert(sizeof(PyObject) <= 16, "!(sizeof(PyObject) <= 16)"); #define PK_OBJ_SIZEOF(T) (sizeof(T) + 16) PyObject* PyObject__new(Type type, int size); -void PyObject__delete(pk_VM* vm, PyObject* self); +void PyObject__delete(PyObject* self); PK_INLINE PyVar PyVar__fromobj(PyObject* obj){ + if(!obj) return PY_NULL; PyVar retval = { .type = obj->type, .is_ptr = true, diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 4531bb89..c58680f1 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -1,3 +1,5 @@ +#pragma once + #include "stdint.h" #include "stdbool.h" @@ -10,8 +12,17 @@ typedef struct PyVar PyVar; typedef struct pk_VM pk_VM; typedef struct py_Error py_Error; +typedef enum BindType { + BindType_FUNCTION, + BindType_STATICMETHOD, + 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(); @@ -25,20 +36,34 @@ py_Error* py_eval_simple(const char*, PyVar*); void py_Error__print(const py_Error*); void py_Error__delete(py_Error*); - -bool py_eq(const PyVar*, const PyVar*); -bool py_le(const PyVar*, const PyVar*); -int64_t py_hash(const PyVar*); - +int py_eq(const PyVar*, const PyVar*); +int py_le(const PyVar*, const PyVar*); +int py_hash(const PyVar*, int64_t* out); /* py_var */ -void py_newint(PyVar*, int64_t); -void py_newfloat(PyVar*, double); -void py_newbool(PyVar*, bool); -void py_newstr(PyVar*, const char*); -void py_newstr2(PyVar*, const char*, int); -void py_newbytes(PyVar*, const uint8_t*, int); -void py_newnone(PyVar*); +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*); + +// 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); +// 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); + +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 #define py_isnull(self) ((self)->type == 0) diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index b0089607..29c4816c 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -1,28 +1,28 @@ #include "pocketpy/interpreter/vm.h" #include "pocketpy/common/memorypool.h" -static unsigned char* pk_default_import_file(pk_VM* vm, const char* path){ +static unsigned char* pk_default_import_file(const char* path){ return NULL; } -static void pk_default_stdout(pk_VM* vm, const char* s){ +static void pk_default_stdout(const char* s){ fprintf(stdout, "%s", s); fflush(stdout); } -static void pk_default_stderr(pk_VM* vm, const char* s){ +static void pk_default_stderr(const char* s){ fprintf(stderr, "%s", s); fflush(stderr); } -void pk_TypeInfo__ctor(pk_TypeInfo *self, StrName name, Type base, PyObject* obj, PyObject* module, bool subclass_enabled){ +void pk_TypeInfo__ctor(pk_TypeInfo *self, StrName name, Type base, PyObject* obj, const PyVar* module, bool subclass_enabled){ memset(self, 0, sizeof(pk_TypeInfo)); self->name = name; self->base = base; - self->obj = obj; - self->module = module; + self->self = PyVar__fromobj(obj); + self->module = module ? *module : PY_NULL; self->subclass_enabled = subclass_enabled; c11_vector__ctor(&self->annotated_fields, sizeof(StrName)); @@ -32,22 +32,35 @@ void pk_TypeInfo__dtor(pk_TypeInfo *self){ c11_vector__dtor(&self->annotated_fields); } +static int _hello(const PyVar* args, int argc){ + return 0; +} + +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]); +} + void pk_VM__ctor(pk_VM* self){ self->top_frame = NULL; pk_NameDict__ctor(&self->modules); c11_vector__ctor(&self->types, sizeof(pk_TypeInfo)); - self->StopIteration = NULL; - self->builtins = NULL; - self->main = NULL; + self->StopIteration = PY_NULL; + self->builtins = PY_NULL; + self->main = PY_NULL; self->_ceval_on_step = NULL; self->_import_file = pk_default_import_file; self->_stdout = pk_default_stdout; self->_stderr = pk_default_stderr; - self->__last_exception = NULL; + self->last_error = NULL; + self->last_retval = PY_NULL; + self->__curr_class = NULL; self->__cached_object_new = NULL; self->__dynamic_func_decl = NULL; @@ -92,7 +105,7 @@ void pk_VM__ctor(pk_VM* self){ validate(tp_module, pk_VM__new_type(self, "module", tp_object, NULL, false)); validate(tp_function, pk_VM__new_type(self, "function", tp_object, NULL, false)); - validate(tp_native_func, pk_VM__new_type(self, "native_func", tp_object, NULL, false)); + validate(tp_nativefunc, pk_VM__new_type(self, "nativefunc", tp_object, NULL, false)); validate(tp_bound_method, pk_VM__new_type(self, "bound_method", tp_object, NULL, false)); validate(tp_super, pk_VM__new_type(self, "super", tp_object, NULL, false)); @@ -118,8 +131,8 @@ void pk_VM__ctor(pk_VM* self){ validate(tp_stop_iteration, pk_VM__new_type(self, "StopIteration", tp_exception, NULL, false)); #undef validate - self->StopIteration = c11__at(pk_TypeInfo, &self->types, tp_stop_iteration)->obj; - self->builtins = pk_VM__new_module(self, "builtins", NULL); + self->StopIteration = c11__at(pk_TypeInfo, &self->types, tp_stop_iteration)->self; + self->builtins = py_new_module("builtins"); /* Setup Public Builtin Types */ Type public_types[] = { @@ -134,13 +147,13 @@ void pk_VM__ctor(pk_VM* self){ for(int i=0; itypes, t); - pk_NameDict__set(self->builtins->dict, ti->name, PyVar__fromobj(ti->obj)); + pk_NameDict__set(self->builtins._obj->dict, ti->name, ti->self); } - pk_NameDict__set(self->builtins->dict, pk_StrName__map("NotImplemented"), self->NotImplemented); + pk_NameDict__set(self->builtins._obj->dict, pk_StrName__map("NotImplemented"), self->NotImplemented); /* Do Buildin Bindings*/ - // TODO: ... - self->main = pk_VM__new_module(self, "__main__", NULL); + do_builtin_bindings(); + self->main = py_new_module("__main__"); } void pk_VM__dtor(pk_VM* self){ @@ -170,7 +183,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self){ return RES_RETURN; } -Type pk_VM__new_type(pk_VM* self, const char* name, Type base, PyObject* module, bool subclass_enabled){ +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)); diff --git a/src/interpreter/vm.cpp b/src/interpreter/vm.cpp deleted file mode 100644 index 5354229e..00000000 --- a/src/interpreter/vm.cpp +++ /dev/null @@ -1,1973 +0,0 @@ -#include "pocketpy/interpreter/vm.hpp" -#include "pocketpy/common/memorypool.h" -#include "pocketpy/interpreter/frame.h" -#include "pocketpy/objects/base.h" -#include "pocketpy/objects/codeobject.h" -#include "pocketpy/objects/public.h" - -#include -#include -#include -#include - -#if PK_DEBUG_CEVAL_STEP -#include -#endif - -const static char* OP_NAMES[] = { -#define OPCODE(name) #name, -#include "pocketpy/xmacros/opcodes.h" -#undef OPCODE -}; - -namespace pkpy { - -struct JsonSerializer { - VM* vm; - PyVar root; - SStream ss; - - JsonSerializer(VM* vm, PyVar root) : vm(vm), root(root) {} - - template - void write_array(T& arr) { - ss << '['; - for(int i = 0; i < arr.size(); i++) { - if(i != 0) ss << ", "; - write_object(arr[i]); - } - ss << ']'; - } - - void write_dict(Dict& dict) { - ss << '{'; - bool first = true; - dict.apply([&](PyVar k, PyVar v) { - if(!first) ss << ", "; - first = false; - if(!is_type(k, VM::tp_str)) { - vm->TypeError(_S("json keys must be string, got ", _type_name(vm, vm->_tp(k)))); - } - ss << _CAST(Str&, k).escape('"') << ": "; - write_object(v); - }); - ss << '}'; - } - - void write_object(PyVar obj) { - Type obj_t = vm->_tp(obj); - if(is_none(obj)) { - ss << "null"; - } else if(obj_t == vm->tp_int) { - ss << _CAST(i64, obj); - } else if(obj_t == vm->tp_float) { - f64 val = _CAST(f64, obj); - if(std::isinf(val) || std::isnan(val)) vm->ValueError("cannot jsonify 'nan' or 'inf'"); - ss << val; - } else if(obj_t == vm->tp_bool) { - ss << (obj.extra ? "true" : "false"); - } else if(obj_t == vm->tp_str) { - ss << _CAST(Str&, obj).escape('"'); - } else if(obj_t == vm->tp_list) { - write_array(_CAST(List&, obj)); - } else if(obj_t == vm->tp_tuple) { - write_array(_CAST(Tuple&, obj)); - } else if(obj_t == vm->tp_dict) { - write_dict(_CAST(Dict&, obj)); - } else { - vm->TypeError(_S("unrecognized type ", _type_name(vm, obj_t).escape())); - } - } - - Str serialize() { - auto _lock = vm->gc_scope_lock(); - write_object(root); - return ss.str(); - } -}; - -VM::VM(bool enable_os) : enable_os(enable_os) { - pkpy_g.vm = (pk_VM*)this; // setup the current VM - Pools_initialize(); - pk_StrName__initialize(); - - pk_ManagedHeap__ctor(&heap, (pk_VM*)this); - - static ::PyObject __true_obj = {tp_bool, false, false, NULL}; - static ::PyObject __false_obj = {tp_bool, false, false, NULL}; - static ::PyObject __none_obj = {tp_none_type, false, false, NULL}; - static ::PyObject __not_implemented_obj = {tp_not_implemented_type, false, false, NULL}; - static ::PyObject __ellipsis_obj = {tp_ellipsis, false, false, NULL}; - - /* Must be heap objects to support `==` and `is` and `is not` */ - True.type = tp_bool; True.is_ptr = true; True.extra = 1; True._obj = &__true_obj; - False.type = tp_bool; False.is_ptr = true; False.extra = 0; False._obj = &__false_obj; - None.type = tp_none_type; None.is_ptr = true; None._obj = &__none_obj; - NotImplemented.type = tp_not_implemented_type; NotImplemented.is_ptr = true; NotImplemented._obj = &__not_implemented_obj; - Ellipsis.type = tp_ellipsis; Ellipsis.is_ptr = true; Ellipsis._obj = &__ellipsis_obj; - - this->vm = this; - this->__c.error = nullptr; - _ceval_on_step = nullptr; - _stdout = [](const char* buf, int size) { - std::cout.write(buf, size); - }; - _stderr = [](const char* buf, int size) { - std::cerr.write(buf, size); - }; - builtins = nullptr; - _main = nullptr; - __last_exception = nullptr; - _import_handler = [](const char* name, int* out_size) -> unsigned char* { - return nullptr; - }; - __init_builtin_types(); -} - -Str VM::py_str(PyVar obj) { - const PyTypeInfo* ti = _tp_info(obj); - if(ti->m__str__) return ti->m__str__(this, obj); - PyVar self; - PyVar f = get_unbound_method(obj, __str__, &self, false); - if(self.type) { - PyVar retval = call_method(self, f); - if(!is_type(retval, tp_str)) { throw std::runtime_error("object.__str__ must return str"); } - return PK_OBJ_GET(Str, retval); - } - return py_repr(obj); -} - -Str VM::py_repr(PyVar obj) { - const PyTypeInfo* ti = _tp_info(obj); - if(ti->m__repr__) return ti->m__repr__(this, obj); - PyVar retval = call_method(obj, __repr__); - if(!is_type(retval, tp_str)) { throw std::runtime_error("object.__repr__ must return str"); } - return PK_OBJ_GET(Str, retval); -} - -Str VM::py_json(PyVar obj) { - JsonSerializer j(this, obj); - return j.serialize(); -} - -PyVar VM::py_iter(PyVar obj) { - const PyTypeInfo* ti = _tp_info(obj); - if(ti->m__iter__) return ti->m__iter__(this, obj); - PyVar self; - PyVar iter_f = get_unbound_method(obj, __iter__, &self, false); - if(self.type) return call_method(self, iter_f); - TypeError(_type_name(vm, _tp(obj)).escape() + " object is not iterable"); - return nullptr; -} - -ArgsView VM::cast_array_view(PyVar obj) { - if(is_type(obj, VM::tp_list)) { - List& list = PK_OBJ_GET(List, obj); - return ArgsView(list.begin(), list.end()); - } else if(is_type(obj, VM::tp_tuple)) { - Tuple& tuple = PK_OBJ_GET(Tuple, obj); - return ArgsView(tuple.begin(), tuple.end()); - } - TypeError(_S("expected list or tuple, got ", _type_name(this, _tp(obj)).escape())); -} - -void VM::set_main_argv(int argc, char** argv) { - PyVar mod = vm->_modules["sys"]; - List argv_(argc); - for(int i = 0; i < argc; i++) - argv_[i] = VAR(std::string_view(argv[i])); - mod->attr().set("argv", VAR(std::move(argv_))); -} - -PyVar* VM::find_name_in_mro(Type cls, StrName name) { - PyVar* val; - do { - val = _t(cls)->attr().try_get_2(name); - if(val != nullptr) return val; - cls = _all_types[cls].base; - if(!cls) break; - } while(true); - return nullptr; -} - -bool VM::isinstance(PyVar obj, Type base) { return issubclass(_tp(obj), base); } - -bool VM::issubclass(Type cls, Type base) { - do { - if(cls == base) return true; - Type next = _all_types[cls].base; - if(!next) break; - cls = next; - } while(true); - return false; -} - -PyVar VM::exec(std::string_view source, Str filename, CompileMode mode, PyObject* _module) { - if(_module == nullptr) _module = _main; - CodeObject* code = NULL; - try { -#if PK_DEBUG_PRECOMPILED_EXEC == 1 - Str precompiled = vm->precompile(source, filename, mode); - source = precompiled.sv(); -#endif - code = compile(source, filename, mode); - PyVar retval = _exec(code, _module); - CodeObject__delete(code); - return retval; - } catch(TopLevelException e) { - stderr_write(e.summary() + "\n"); - } catch(const std::exception& e) { - Str msg = "An std::exception occurred! It could be a bug.\n"; - msg = msg + e.what() + "\n"; - stderr_write(msg); - } catch(NeedMoreLines) { throw; } catch(...) { - Str msg = "An unknown exception occurred! It could be a bug. Please report it to @blueloveTH on GitHub.\n"; - stderr_write(msg); - } - CodeObject__delete(code); - while(top_frame) __pop_frame(); // this changes s_data.sp, it must put before ValueStack__clear(); - ValueStack__clear(&s_data); - return nullptr; -} - -PyVar VM::exec(std::string_view source) { return exec(source, "main.py", EXEC_MODE); } - -PyVar VM::eval(std::string_view source) { return exec(source, "", EVAL_MODE); } - -PyObject* VM::new_type_object(PyObject* mod, StrName name, Type base, bool subclass_enabled, PyTypeInfo::Vt vt) { - PyObject* obj = new_object_no_gc(tp_type, Type(_all_types.size())).get(); - const PyTypeInfo& base_info = _all_types[base]; - if(!base_info.subclass_enabled) { - Str error = _S("type ", base_info.name.escape(), " is not `subclass_enabled`"); - throw std::runtime_error(error.c_str()); - } - if(base_info.vt) { - if(vt) { - Str error = _S("type ", base_info.name.escape(), " has a custom vtable, cannot override"); - throw std::runtime_error(error.c_str()); - } else { - // promote base vt to its subclass - vt = base_info.vt; - } - } - _all_types.emplace_back(obj, base, mod, name, subclass_enabled, vt); - return obj; -} - -bool VM::py_eq(PyVar lhs, PyVar rhs) { - if(is_int(lhs) && is_int(rhs)) return lhs._i64 == rhs._i64; - const PyTypeInfo* ti = _tp_info(lhs); - PyVar res; - if(ti->m__eq__) { - res = ti->m__eq__(this, lhs, rhs); - if(!is_not_implemented(res)) return res.extra; - } - res = call_method(lhs, __eq__, rhs); - if(!is_not_implemented(res)) return res.extra; - - ti = _tp_info(rhs); - if(ti->m__eq__) { - res = ti->m__eq__(this, rhs, lhs); - if(!is_not_implemented(res)) return res.extra; - } - res = call_method(rhs, __eq__, lhs); - if(!is_not_implemented(res)) return res.extra; - return false; -} - -PyVar VM::py_op(std::string_view name) { - // TODO: cache the result - return py_import("operator")->attr()[StrName::get(name)]; -} - -i64 VM::normalized_index(i64 index, int size) { - if(index < 0) index += size; - if(index < 0 || index >= size) { IndexError(std::to_string(index) + " not in [0, " + std::to_string(size) + ")"); } - return index; -} - -PyVar VM::_py_next(const PyTypeInfo* ti, PyVar obj) { - if(ti->op__next__) { - unsigned n = ti->op__next__(this, obj); - return __pack_next_retval(n); - } - return call_method(obj, __next__); -} - -PyVar VM::py_next(PyVar obj) { - const PyTypeInfo* ti = _tp_info(obj); - return _py_next(ti, obj); -} - -bool VM::py_callable(PyVar obj) { - Type cls = vm->_tp(obj); - switch(cls) { - case VM::tp_function: return true; - case VM::tp_native_func: return true; - case VM::tp_bound_method: return true; - case VM::tp_type: return true; - } - return vm->find_name_in_mro(cls, __call__) != nullptr; -} - -PyVar VM::__minmax_reduce(bool (VM::*op)(PyVar, PyVar), PyVar args, PyVar key) { - auto _lock = gc_scope_lock(); - const Tuple& args_tuple = PK_OBJ_GET(Tuple, args); // from *args, it must be a tuple - if(is_none(key) && args_tuple.size() == 2) { - // fast path - PyVar a = args_tuple[0]; - PyVar b = args_tuple[1]; - return (this->*op)(a, b) ? a : b; - } - - if(args_tuple.size() == 0) TypeError("expected at least 1 argument, got 0"); - - ArgsView view; - if(args_tuple.size() == 1) { - view = cast_array_view(args_tuple[0]); - } else { - view = ArgsView(args_tuple); - } - - if(view.empty()) ValueError("arg is an empty sequence"); - PyVar res = view[0]; - - if(is_none(key)) { - for(int i = 1; i < view.size(); i++) { - if((this->*op)(view[i], res)) res = view[i]; - } - } else { - auto _lock = gc_scope_lock(); - for(int i = 1; i < view.size(); i++) { - PyVar a = call(key, view[i]); - PyVar b = call(key, res); - if((this->*op)(a, b)) res = view[i]; - } - } - return res; -} - -PyObject* VM::py_import(Str path, bool throw_err) { - if(path.empty()) vm->ValueError("empty module name"); - static auto f_join = [](const vector& cpnts) { - SStream ss; - for(int i = 0; i < cpnts.size(); i++) { - if(i != 0) ss << "."; - ss << cpnts[i]; - } - return ss.str(); - }; - - if(path[0] == '.') { - if(__import_context.pending.empty()) { ImportError("relative import outside of package"); } - Str curr_path = __import_context.pending.back(); - bool curr_is_init = __import_context.pending_is_init.back(); - // convert relative path to absolute path - vector cpnts = curr_path.split('.'); - int prefix = 0; // how many dots in the prefix - for(int i = 0; i < path.length(); i++) { - if(path[i] == '.') - prefix++; - else - break; - } - if(prefix > cpnts.size()) ImportError("attempted relative import beyond top-level package"); - path = path.substr(prefix); // remove prefix - for(int i = (int)curr_is_init; i < prefix; i++) - cpnts.pop_back(); - if(!path.empty()) cpnts.push_back(path.sv()); - path = f_join(cpnts); - } - - assert(path.begin()[0] != '.' && path.end()[-1] != '.'); - - // check existing module - StrName name(path); - PyVar ext_mod = _modules.try_get(name); - if(ext_mod) return ext_mod.get(); - - vector path_cpnts = path.split('.'); - // check circular import - if(__import_context.pending.size() > 128) { ImportError("maximum recursion depth exceeded while importing"); } - - // try import - Str filename = path.replace('.', PK_PLATFORM_SEP) + ".py"; - Str source; - bool is_init = false; - auto it = _lazy_modules.try_get(name); - if(it == nullptr) { - int out_size; - unsigned char* out = _import_handler(filename.c_str(), &out_size); - if(out == nullptr) { - filename = path.replace('.', PK_PLATFORM_SEP).str() + PK_PLATFORM_SEP + "__init__.py"; - is_init = true; - out = _import_handler(filename.c_str(), &out_size); - } - if(out == nullptr) { - if(throw_err) - ImportError(_S("module ", path.escape(), " not found")); - else - return nullptr; - } - assert(out_size >= 0); - source = Str(std::string_view((char*)out, out_size)); - std::free(out); - } else { - source = *it; - // _lazy_modules.erase(it); // no need to erase - } - auto _ = __import_context.scope(path, is_init); - Str name_cpnt = path_cpnts.back(); - path_cpnts.pop_back(); - PyObject* new_mod = new_module(name_cpnt, f_join(path_cpnts)); - CodeObject* code = compile(source, filename, EXEC_MODE); - _exec(code, new_mod); - CodeObject__delete(code); - return new_mod; -} - -VM::~VM() { - PK_DECREF(__dynamic_func_decl); - // destroy all objects - pk_ManagedHeap__dtor(&heap); - pk_NameDict__dtor(&_modules); - // clear everything - while(top_frame) __pop_frame(); // this changes s_data.sp, it must put before ValueStack__clear(); - ValueStack__clear(&s_data); - _all_types.clear(); - _lazy_modules.clear(); -} - -PyVar VM::py_negate(PyVar obj) { - const PyTypeInfo* ti = _tp_info(obj); - if(ti->m__neg__) return ti->m__neg__(this, obj); - return call_method(obj, __neg__); -} - -bool VM::__py_bool_non_trivial(PyVar obj) { - if(is_none(obj)) return false; - if(is_int(obj)) return _CAST(i64, obj) != 0; - if(is_float(obj)) return _CAST(f64, obj) != 0.0; - PyVar self; - PyVar len_f = get_unbound_method(obj, __len__, &self, false); - if(self) { - PyVar ret = call_method(self, len_f); - return CAST(i64, ret) != 0; - } - return true; -} - -void VM::__obj_gc_mark(PyObject* obj) { - if(obj->gc_marked) return; - obj->gc_marked = true; - const PyTypeInfo* ti = _tp_info(obj->type); - if(ti->vt._gc_mark) ti->vt._gc_mark(obj->_value_ptr(), this); - if(obj->is_attr_valid()) { - obj->attr().apply([](StrName _, PyVar obj, void* userdata) { - VM* vm = (VM*)userdata; - if(obj.is_ptr) vm->__obj_gc_mark((obj).get()); - }, vm); - } -} - -void VM::__stack_gc_mark(PyVar* begin, PyVar* end) { - for(PyVar* it = begin; it != end; it++) { - if(it->is_ptr) { - __obj_gc_mark(it->get()); - } - } -} - -List VM::py_list(PyVar it) { - auto _lock = gc_scope_lock(); - it = py_iter(it); - List list; - const PyTypeInfo* info = _tp_info(it); - PyVar obj = _py_next(info, it); - while(obj != StopIteration) { - list.push_back(obj); - obj = _py_next(info, it); - } - return list; -} - -void VM::parse_int_slice(const Slice& s, int length, int& start, int& stop, int& step) { - auto clip = [](int value, int min, int max) { - if(value < min) return min; - if(value > max) return max; - return value; - }; - if(is_none(s.step)) - step = 1; - else - step = CAST(int, s.step); - if(step == 0) ValueError("slice step cannot be zero"); - if(step > 0) { - if(is_none(s.start)) { - start = 0; - } else { - start = CAST(int, s.start); - if(start < 0) start += length; - start = clip(start, 0, length); - } - if(is_none(s.stop)) { - stop = length; - } else { - stop = CAST(int, s.stop); - if(stop < 0) stop += length; - stop = clip(stop, 0, length); - } - } else { - if(is_none(s.start)) { - start = length - 1; - } else { - start = CAST(int, s.start); - if(start < 0) start += length; - start = clip(start, -1, length - 1); - } - if(is_none(s.stop)) { - stop = -1; - } else { - stop = CAST(int, s.stop); - if(stop < 0) stop += length; - stop = clip(stop, -1, length - 1); - } - } -} - -i64 VM::py_hash(PyVar obj) { - // https://docs.python.org/3.10/reference/datamodel.html#object.__hash__ - const PyTypeInfo* ti = _tp_info(obj); - if(ti->m__hash__) return ti->m__hash__(this, obj); - - PyVar self; - PyVar f = get_unbound_method(obj, __hash__, &self, false); - if(f) { - PyVar ret = call_method(self, f); - return CAST(i64, ret); - } - // if it is trivial `object`, return PK_BITS - if(ti == &_all_types[tp_object]) return obj.hash(); - // otherwise, we check if it has a custom __eq__ other than object.__eq__ - bool has_custom_eq = false; - if(ti->m__eq__) - has_custom_eq = true; - else { - f = get_unbound_method(obj, __eq__, &self, false); - PyVar base_eq = _t(tp_object)->attr()[__eq__]; - has_custom_eq = !PyVar__IS_OP(&f, &base_eq); - } - if(has_custom_eq) { - TypeError(_S("unhashable type: ", ti->name.escape())); - } else { - return obj.hash(); - } -} - -PyVar VM::__py_exec_internal(const CodeObject* code, PyVar globals, PyVar locals) { - Frame* frame = nullptr; - if(!callstack.empty()) frame = &callstack.top(); - - // fast path - if(frame && is_none(globals) && is_none(locals)) { - return vm->_exec(code, frame->_module, frame->_callable, frame->_locals); - } - - auto _lock = gc_scope_lock(); // for safety - - PyObject* globals_obj = nullptr; - Dict* globals_dict = nullptr; - - NameDict* locals_closure = nullptr; - Dict* locals_dict = nullptr; - - if(is_none(globals)){ - globals_obj = frame ? frame->_module: _main; - } else { - if(is_type(globals, VM::tp_mappingproxy)) { - globals_obj = PK_OBJ_GET(MappingProxy, globals).obj; - } else { - check_compatible_type(globals, VM::tp_dict); - // make a temporary object and copy globals into it - globals_obj = new_object(VM::tp_object).get(); - globals_dict = &PK_OBJ_GET(Dict, globals); - globals_dict->apply([&](PyVar k, PyVar v) { - globals_obj->attr().set(CAST(Str&, k), v); - }); - } - } - - PyVar retval = nullptr; - - if(is_none(locals)) { - retval = vm->_exec(code, globals_obj); // only globals - } else { - check_compatible_type(locals, VM::tp_dict); - locals_dict = &PK_OBJ_GET(Dict, locals); - locals_closure = new NameDict(); - locals_dict->apply([&](PyVar k, PyVar v) { - locals_closure->set(CAST(Str&, k), v); - }); - PyObject* _callable = - new_object(tp_function, __dynamic_func_decl, globals_obj, nullptr, locals_closure).get(); - retval = vm->_exec(code, globals_obj, _callable, vm->s_data.sp); - } - - if(globals_dict) { - globals_dict->clear(); - for(auto [k, v]: globals_obj->attr().items()){ - globals_dict->set(vm, VAR(k.sv()), v); - } - } - - if(locals_dict) { - locals_dict->clear(); - for(auto [k, v]: locals_closure->items()){ - locals_dict->set(vm, VAR(k.sv()), v); - } - } - return retval; -} - -void VM::py_exec(std::string_view source, PyVar globals, PyVar locals) { - CodeObject* code = vm->compile(source, "", EXEC_MODE, true); - __py_exec_internal(code, globals, locals); - CodeObject__delete(code); -} - -PyVar VM::py_eval(std::string_view source, PyVar globals, PyVar locals) { - CodeObject* code = vm->compile(source, "", EVAL_MODE, true); - PyVar retval = __py_exec_internal(code, globals, locals); - CodeObject__delete(code); - return retval; -} - -PyVar VM::__format_object(PyVar obj, Str spec) { - if(spec.empty()) return VAR(py_str(obj)); - char type; - switch(spec.end()[-1]) { - case 'f': - case 'd': - case 's': - type = spec.end()[-1]; - spec = spec.slice(0, spec.length() - 1); - break; - default: type = ' '; break; - } - - char pad_c = ' '; - for(char c: std::string_view("0-=*#@!~")) { - if(spec[0] == c) { - pad_c = c; - spec = spec.substr(1); - break; - } - } - char align; - if(spec[0] == '^') { - align = '^'; - spec = spec.substr(1); - } else if(spec[0] == '>') { - align = '>'; - spec = spec.substr(1); - } else if(spec[0] == '<') { - align = '<'; - spec = spec.substr(1); - } else { - if(is_int(obj) || is_float(obj)) - align = '>'; - else - align = '<'; - } - - int dot = spec.index("."); - int width, precision; - try { - if(dot >= 0) { - if(dot == 0) { - width = -1; - } else { - width = std::stoi(spec.slice(0, dot).str()); - } - precision = std::stoi(spec.slice(dot + 1).str()); - } else { - width = std::stoi(spec.str()); - precision = -1; - } - } catch(...) { ValueError("invalid format specifer"); } - - if(type != 'f' && dot >= 0) ValueError("precision not allowed in the format specifier"); - Str ret; - if(type == 'f') { - f64 val = CAST(f64, obj); - if(precision < 0) precision = 6; - SStream ss; - ss.setprecision(precision); - ss << val; - ret = ss.str(); - } else if(type == 'd') { - ret = std::to_string(CAST(i64, obj)); - } else if(type == 's') { - ret = CAST(Str&, obj); - } else { - ret = py_str(obj); - } - if(width != -1 && width > ret.length()) { - int pad = width - ret.length(); - if(align == '>' || align == '<') { - std::string padding(pad, pad_c); - if(align == '>') - ret = padding.c_str() + ret; - else - ret = ret + padding.c_str(); - } else { // ^ - int pad_left = pad / 2; - int pad_right = pad - pad_left; - std::string padding_left(pad_left, pad_c); - std::string padding_right(pad_right, pad_c); - ret = padding_left.c_str() + ret + padding_right.c_str(); - } - } - return VAR(ret); -} - -PyObject* VM::new_module(Str name, Str package) { - PyObject* obj = new_object_no_gc(tp_module).get(); - obj->attr().set(__name__, VAR(name)); - obj->attr().set(__package__, VAR(package)); - // convert to fullname - if(!package.empty()) name = package + "." + name; - obj->attr().set(__path__, VAR(name)); - - // we do not allow override in order to avoid memory leak - // it is because Module objects are not garbage collected - if(_modules.contains(name)) { throw std::runtime_error(_S("module ", name.escape(), " already exists").str()); } - // set it into _modules - _modules.set(name, obj); - return obj; -} - -static std::string _opcode_argstr(VM* vm, int i, Bytecode byte, const CodeObject* co) { - SStream ss; - if(Bytecode__is_forward_jump(&byte)){ - std::string argStr = std::to_string((int16_t)byte.arg); - ss << (i64)(int16_t)byte.arg; - ss << " (to " << (i64)((int16_t)byte.arg + i) << ")"; - return ss.str().str(); - } - ss << (i64)byte.arg; - switch(byte.op) { - case OP_LOAD_CONST: - case OP_FORMAT_STRING: - case OP_IMPORT_PATH: { - PyVar obj = c11__getitem(PyVar, &co->consts, byte.arg); - if(vm != nullptr) ss << " (" << vm->py_repr(obj) << ")"; - break; - } - case OP_LOAD_NAME: - case OP_LOAD_GLOBAL: - case OP_LOAD_NONLOCAL: - case OP_STORE_GLOBAL: - case OP_LOAD_ATTR: - case OP_LOAD_METHOD: - case OP_STORE_ATTR: - case OP_DELETE_ATTR: - case OP_BEGIN_CLASS: - case OP_GOTO: - case OP_DELETE_GLOBAL: - case OP_STORE_CLASS_ATTR: - case OP_FOR_ITER_STORE_GLOBAL: ss << " (" << StrName(byte.arg).sv() << ")"; break; - case OP_LOAD_FAST: - case OP_STORE_FAST: - case OP_DELETE_FAST: - case OP_FOR_ITER_STORE_FAST: - case OP_LOAD_SUBSCR_FAST: - case OP_STORE_SUBSCR_FAST:{ - StrName name = c11__getitem(StrName, &co->varnames, byte.arg); - ss << " (" << name.sv() << ")"; - break; - } - case OP_LOAD_FUNCTION: { - const FuncDecl* decl = c11__getitem(FuncDecl*, &co->func_decls, byte.arg); - ss << " (" << pkpy_Str__data(&decl->code->name) << ")"; - break; - } - } - return ss.str().str(); -} - -Str VM::disassemble(CodeObject* co) { - auto pad = [](const Str& s, const int n) { - if(s.length() >= n) return s.slice(0, n); - return s + std::string(n - s.length(), ' '); - }; - - vector jumpTargets; - for(int i=0; icodes.count; i++) { - Bytecode* bc = c11__at(Bytecode, &co->codes, i); - if(Bytecode__is_forward_jump(bc)) { - jumpTargets.push_back((int16_t)bc->arg + i); - } - } - SStream ss; - int prev_line = -1; - for(int i = 0; i < co->codes.count; i++) { - Bytecode byte = c11__getitem(Bytecode, &co->codes, i); - BytecodeEx ex = c11__getitem(BytecodeEx, &co->codes_ex, i); - Str line = std::to_string(ex.lineno); - if(ex.lineno == prev_line) - line = ""; - else { - if(prev_line != -1) ss << "\n"; - prev_line = ex.lineno; - } - - std::string pointer; - if(jumpTargets.contains(i)) { - pointer = "-> "; - } else { - pointer = " "; - } - ss << pad(line, 8) << pointer << pad(std::to_string(i), 3); - std::string bc_name(OP_NAMES[byte.op]); - if(ex.is_virtual) bc_name += '*'; - ss << " " << pad(bc_name, 25) << " "; - std::string argStr = _opcode_argstr(this, i, byte, co); - ss << argStr; - if(i != co->codes.count - 1) ss << '\n'; - } - - c11_vector__foreach(FuncDecl*, &co->func_decls, it) { - FuncDecl* decl = *it; - ss << "\n\n" - << "Disassembly of " << pkpy_Str__data(&decl->code->name) << ":\n"; - ss << disassemble(decl->code); - } - ss << "\n"; - return Str(ss.str()); -} - -#if PK_DEBUG_CEVAL_STEP -void VM::__log_s_data(const char* title) { - if(_main == nullptr) return; - if(callstack.empty()) return; - SStream ss; - if(title) ss << title << " | "; - std::map sp_bases; - callstack.apply([&](Frame& f) { - assert(f._sp_base != nullptr); - sp_bases[f._sp_base] += 1; - }); - Frame* frame = &callstack.top(); - int line = frame->curr_lineno(); - ss << pkpy_Str__data(&frame->co->name) << ":" << line << " ["; - for(PyVar* p = s_data.begin(); p != s_data.end(); p++) { - ss << std::string(sp_bases[p], '|'); - if(sp_bases[p] > 0) ss << " "; - if(!(*p)) - ss << "NULL"; - else { - switch(p->type) { - case tp_none_type: ss << "None"; break; - case tp_int: ss << _CAST(i64, *p); break; - case tp_float: ss << _CAST(f64, *p); break; - case tp_bool: ss << (p->extra ? "True" : "False"); break; - case tp_str: ss << _CAST(Str, *p).escape(); break; - case tp_function: ss << pkpy_Str__data(&p->obj_get().decl->code->name) << "()"; break; - case tp_type: ss << "obj_get()).escape() + ">"; break; - case tp_list: ss << "list(size=" << p->obj_get().size() << ")"; break; - case tp_tuple: ss << "tuple(size=" << p->obj_get().size() << ")"; break; - default: ss << "(" << _type_name(this, p->type) << ")"; break; - } - } - ss << ", "; - } - std::string output = ss.str().str(); - if(!s_data.empty()) { - output.pop_back(); - output.pop_back(); - } - output.push_back(']'); - Bytecode byte = *frame->_ip; - std::cout << output << " " << OP_NAMES[byte.op] << " " << _opcode_argstr(nullptr, frame->ip(), byte, frame->co) - << std::endl; -} -#endif - -void VM::__init_builtin_types() { - _all_types.emplace_back(nullptr, Type(), nullptr, "", false); // 0 is not used - _all_types.emplace_back(new_object_no_gc(tp_type, tp_object).get(), Type(), nullptr, "object", true); - _all_types.emplace_back(new_object_no_gc(tp_type, tp_type).get(), tp_object, nullptr, "type", false); - - auto validate = [](Type type, PyObject* ret) { - Type ret_t = ret->as(); - if(ret_t != type) exit(-3); - }; - - validate(tp_int, new_type_object(nullptr, "int", tp_object, false)); - validate(tp_float, new_type_object(nullptr, "float", tp_object, false)); - validate(tp_bool, new_type_object(nullptr, "bool", tp_object, false)); - - validate(tp_str, new_type_object(nullptr, "str", tp_object, false)); - validate(tp_list, new_type_object(nullptr, "list", tp_object, false)); - validate(tp_tuple, new_type_object(nullptr, "tuple", tp_object, false)); - - validate(tp_slice, new_type_object(nullptr, "slice", tp_object, false)); - validate(tp_range, new_type_object(nullptr, "range", tp_object, false)); - validate(tp_module, new_type_object(nullptr, "module", tp_object, false)); - validate(tp_function, new_type_object(nullptr, "function", tp_object, false)); - validate(tp_native_func, new_type_object(nullptr, "native_func", tp_object, false)); - validate(tp_bound_method, new_type_object(nullptr, "bound_method", tp_object, false)); - - validate(tp_super, new_type_object(nullptr, "super", tp_object, false)); - validate(tp_exception, new_type_object(nullptr, "Exception", tp_object, true)); - validate(tp_bytes, new_type_object(nullptr, "bytes", tp_object, false)); - validate(tp_mappingproxy, new_type_object(nullptr, "mappingproxy", tp_object, false)); - validate(tp_dict, new_type_object(nullptr, "dict", tp_object, true)); - validate(tp_property, new_type_object(nullptr, "property", tp_object, false)); - validate(tp_star_wrapper, new_type_object(nullptr, "_star_wrapper", tp_object, false)); - - validate(tp_staticmethod, new_type_object(nullptr, "staticmethod", tp_object, false)); - validate(tp_classmethod, new_type_object(nullptr, "classmethod", tp_object, false)); - - validate(tp_none_type, new_type_object(nullptr, "NoneType", tp_object, false)); - validate(tp_not_implemented_type, new_type_object(nullptr, "NotImplementedType", tp_object, false)); - validate(tp_ellipsis, new_type_object(nullptr, "ellipsis", tp_object, false)); - - validate(::tp_op_call, new_type_object(nullptr, "__op_call", tp_object, false)); - validate(::tp_op_yield, new_type_object(nullptr, "__op_yield", tp_object, false)); - - // SyntaxError and IndentationError must be created here - PyObject* SyntaxError = new_type_object(nullptr, "SyntaxError", tp_exception, true); - PyObject* IndentationError = new_type_object(nullptr, "IndentationError", SyntaxError->as(), true); - this->StopIteration = new_type_object(nullptr, "StopIteration", tp_exception, true); - - this->builtins = new_module("builtins"); - - // setup public types - builtins->attr().set("type", _t(tp_type)); - builtins->attr().set("object", _t(tp_object)); - builtins->attr().set("bool", _t(tp_bool)); - builtins->attr().set("int", _t(tp_int)); - builtins->attr().set("float", _t(tp_float)); - builtins->attr().set("str", _t(tp_str)); - builtins->attr().set("list", _t(tp_list)); - builtins->attr().set("tuple", _t(tp_tuple)); - builtins->attr().set("range", _t(tp_range)); - builtins->attr().set("bytes", _t(tp_bytes)); - builtins->attr().set("dict", _t(tp_dict)); - builtins->attr().set("property", _t(tp_property)); - builtins->attr().set("StopIteration", StopIteration); - builtins->attr().set("NotImplemented", NotImplemented); - builtins->attr().set("slice", _t(tp_slice)); - builtins->attr().set("Exception", _t(tp_exception)); - builtins->attr().set("SyntaxError", SyntaxError); - builtins->attr().set("IndentationError", IndentationError); - - __post_init_builtin_types(); - this->_main = new_module("__main__"); -} - -void VM::__unpack_as_list(ArgsView args, List& list) { - auto _lock = gc_scope_lock(); - for(PyVar obj: args) { - if(is_type(obj, tp_star_wrapper)) { - const StarWrapper& w = _CAST(StarWrapper&, obj); - // maybe this check should be done in the compile time - if(w.level != 1) TypeError("expected level 1 star wrapper"); - PyVar _0 = py_iter(w.obj); - const PyTypeInfo* info = _tp_info(_0); - PyVar _1 = _py_next(info, _0); - while(_1 != StopIteration) { - list.push_back(_1); - _1 = _py_next(info, _0); - } - } else { - list.push_back(obj); - } - } -} - -void VM::__unpack_as_dict(ArgsView args, Dict& dict) { - auto _lock = gc_scope_lock(); - for(PyVar obj: args) { - if(is_type(obj, tp_star_wrapper)) { - const StarWrapper& w = _CAST(StarWrapper&, obj); - // maybe this check should be done in the compile time - if(w.level != 2) TypeError("expected level 2 star wrapper"); - const Dict& other = CAST(Dict&, w.obj); - dict.update(this, other); - } else { - const Tuple& t = CAST(Tuple&, obj); - if(t.size() != 2) TypeError("expected tuple of length 2"); - dict.set(this, t[0], t[1]); - } - } -} - -void VM::__prepare_py_call(PyVar* buffer, ArgsView args, ArgsView kwargs, const FuncDecl* decl) { - const CodeObject* co = decl->code; - int decl_argc = decl->args.count; - - if(args.size() < decl_argc) { - vm->TypeError(_S(pkpy_Str__data(&co->name), "() takes ", decl_argc, " positional arguments but ", args.size(), " were given")); - } - - int i = 0; - // prepare args - std::memset(buffer, 0, co->nlocals * sizeof(PyVar)); - c11_vector__foreach(int, &decl->args, index) { - buffer[*index] = args[i++]; - } - // prepare kwdefaults - c11_vector__foreach(FuncDeclKwArg, &decl->kwargs, kv) { - buffer[kv->index] = kv->value; - } - - // handle *args - if(decl->starred_arg != -1) { - ArgsView vargs(args.begin() + i, args.end()); - buffer[decl->starred_arg] = VAR(vargs.to_tuple()); - i += vargs.size(); - } else { - // kwdefaults override - c11_vector__foreach(FuncDeclKwArg, &decl->kwargs, kv) { - if(i >= args.size()) break; - buffer[kv->index] = args[i++]; - } - if(i < args.size()) TypeError(_S("too many arguments", " (", pkpy_Str__data(&decl->code->name), ')')); - } - - PyVar vkwargs; - if(decl->starred_kwarg != -1) { - vkwargs = VAR(Dict()); - buffer[decl->starred_kwarg] = vkwargs; - } else { - vkwargs = nullptr; - } - - for(int j = 0; j < kwargs.size(); j += 2) { - StrName key(_CAST(uint16_t, kwargs[j])); - 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]; - } else { - // otherwise, set as **kwargs if possible - if(!vkwargs) { - TypeError(_S(key.escape(), " is an invalid keyword argument for ", pkpy_Str__data(&co->name), "()")); - } else { - Dict& dict = _CAST(Dict&, vkwargs); - dict.set(this, VAR(key.sv()), kwargs[j + 1]); - } - } - } -} - -PyVar VM::vectorcall(int ARGC, int KWARGC, bool op_call) { - PyVar* p1 = (PyVar*)(s_data.sp - KWARGC * 2); - PyVar* p0 = p1 - ARGC - 2; - // [callable, , args..., kwargs...] - // ^p0 ^p1 ^_sp - PyVar callable = p1[-ARGC - 2]; - Type callable_t = _tp(callable); - - // handle boundmethod, do a patch - if(callable_t == tp_bound_method) { - assert(!p0[1]); - BoundMethod& bm = PK_OBJ_GET(BoundMethod, callable); - callable = bm.func; // get unbound method - callable_t = _tp(callable); - p1[-(ARGC + 2)] = bm.func; - p1[-(ARGC + 1)] = bm.self; - // [unbound, self, args..., kwargs...] - } - - ArgsView args((!p0[1]) ? (p0 + 2) : (p0 + 1), p1); - ArgsView kwargs(p1, s_data.sp); - - PyVar* _base = args.begin(); - - if(callable_t == tp_function) { - /*****************_py_call*****************/ - // check stack overflow - if(s_data.sp > s_data.end) StackOverflowError(); - - const Function& fn = PK_OBJ_GET(Function, callable); - const CodeObject* co = fn.decl->code; - - switch(fn.decl->type) { - case FuncType_NORMAL: - __prepare_py_call(__vectorcall_buffer, args, kwargs, fn.decl); - // copy buffer back to stack - s_data.sp = (_base + co->nlocals); - for(int j = 0; j < co->nlocals; j++) - _base[j] = __vectorcall_buffer[j]; - break; - case FuncType_SIMPLE: - if(args.size() != fn.decl->args.count){ - TypeError(pk_format( - "{} takes {} positional arguments but {} were given", - &co->name, fn.decl->args.count, args.size() - )); - } - if(!kwargs.empty()){ - TypeError(pk_format("{} takes no keyword arguments", &co->name)); - } - // [callable, , args..., local_vars...] - // ^p0 ^p1 ^_sp - s_data.sp = (_base + co->nlocals); - // initialize local variables to PY_NULL - std::memset(p1, 0, (char*)s_data.sp - (char*)p1); - break; - case FuncType_EMPTY: - if(args.size() != fn.decl->args.count){ - TypeError(pk_format( - "{} takes {} positional arguments but {} were given", - &co->name, fn.decl->args.count, args.size() - )); - } - if(!kwargs.empty()){ - TypeError(pk_format("{} takes no keyword arguments", &co->name)); - } - s_data.sp = p0; - return None; - case FuncType_GENERATOR: - __prepare_py_call(__vectorcall_buffer, args, kwargs, fn.decl); - s_data.sp = p0; - callstack.emplace(nullptr, co, fn._module, callable.get(), nullptr); - return __py_generator(callstack.popx(), - ArgsView(__vectorcall_buffer, __vectorcall_buffer + co->nlocals)); - default: PK_UNREACHABLE() - }; - - // simple or normal - callstack.emplace(p0, co, fn._module, callable.get(), args.begin()); - if(op_call) return PY_OP_CALL; - return __run_top_frame(); - /*****************_py_call*****************/ - } - - if(callable_t == tp_native_func) { - const auto& f = PK_OBJ_GET(NativeFunc, callable); - PyVar ret; - if(f.decl != nullptr) { - int co_nlocals = f.decl->code->nlocals; - __prepare_py_call(__vectorcall_buffer, args, kwargs, f.decl); - // copy buffer back to stack - s_data.sp = (_base + co_nlocals); - for(int j = 0; j < co_nlocals; j++) - _base[j] = __vectorcall_buffer[j]; - ret = f.call(vm, ArgsView(s_data.sp - co_nlocals, s_data.sp)); - } else { - if(f.argc != -1) { - if(KWARGC != 0) - TypeError( - "old-style native_func does not accept keyword arguments. If you want to skip this check, specify `argc` to -1"); - if(args.size() != f.argc) { vm->TypeError(_S("expected ", f.argc, " arguments, got ", args.size())); } - } - ret = f.call(this, args); - } - s_data.sp = (p0); - return ret; - } - - if(callable_t == tp_type) { - // [type, NULL, args..., kwargs...] - PyVar new_f = *find_name_in_mro(PK_OBJ_GET(Type, callable), __new__); - PyVar obj; - assert(new_f && (!p0[1])); - if(PyVar__IS_OP(&new_f, &__cached_object_new)) { - // fast path for object.__new__ - obj = vm->new_object(PK_OBJ_GET(Type, callable)); - } else { - PUSH(new_f); - PUSH_NULL(); - PUSH(callable); // cls - for(PyVar o: args) - PUSH(o); - for(PyVar o: kwargs) - PUSH(o); - // if obj is not an instance of `cls`, the behavior is undefined - obj = vectorcall(ARGC + 1, KWARGC); - } - - // __init__ - PyVar self; - callable = get_unbound_method(obj, __init__, &self, false); - if(callable) { - callable_t = _tp(callable); - // replace `NULL` with `self` - p1[-(ARGC + 2)] = callable; - p1[-(ARGC + 1)] = self; - // [init_f, self, args..., kwargs...] - vectorcall(ARGC, KWARGC); - // We just discard the return value of `__init__` - // in cpython it raises a TypeError if the return value is not None - } else { - // manually reset the stack - s_data.sp = (p0); - } - return obj; - } - - // handle `__call__` overload - PyVar self; - PyVar call_f = get_unbound_method(callable, __call__, &self, false); - if(self) { - p1[-(ARGC + 2)] = call_f; - p1[-(ARGC + 1)] = self; - // [call_f, self, args..., kwargs...] - return vectorcall(ARGC, KWARGC, op_call); - } - TypeError(_type_name(vm, callable_t).escape() + " object is not callable"); -} - -void VM::delattr(PyVar _0, StrName _name) { - const PyTypeInfo* ti = _tp_info(_0); - if(ti->m__delattr__ && ti->m__delattr__(this, _0, _name)) return; - if(is_tagged(_0) || !_0->is_attr_valid()) TypeError("cannot delete attribute"); - if(!_0->attr().del(_name)) AttributeError(_0, _name); -} - -// https://docs.python.org/3/howto/descriptor.html#invocation-from-an-instance -PyVar VM::getattr(PyVar obj, StrName name, bool throw_err) { - Type objtype(0); - // handle super() proxy - if(is_type(obj, tp_super)) { - const Super& super = PK_OBJ_GET(Super, obj); - obj = super.first; - objtype = super.second; - } else { - objtype = _tp(obj); - } - PyVar* cls_var = find_name_in_mro(objtype, name); - if(cls_var != nullptr) { - // handle descriptor - if(is_type(*cls_var, tp_property)) { - const Property& prop = PK_OBJ_GET(Property, *cls_var); - return call(prop.getter, obj); - } - } - // handle instance __dict__ - if(!is_tagged(obj) && obj->is_attr_valid()) { - PyVar* val; - if(obj.type == tp_type) { - val = find_name_in_mro(PK_OBJ_GET(Type, obj), name); - if(val != nullptr) { - if(is_tagged(*val)) return *val; - if(val->type == tp_staticmethod) return PK_OBJ_GET(StaticMethod, *val).func; - if(val->type == tp_classmethod) return VAR(BoundMethod(obj, PK_OBJ_GET(ClassMethod, *val).func)); - return *val; - } - } else { - val = obj->attr().try_get_2_likely_found(name); - if(val != nullptr) return *val; - } - } - if(cls_var != nullptr) { - // bound method is non-data descriptor - if(!is_tagged(*cls_var)) { - switch(cls_var->type) { - case tp_function: return VAR(BoundMethod(obj, *cls_var)); - case tp_native_func: return VAR(BoundMethod(obj, *cls_var)); - case tp_staticmethod: return PK_OBJ_GET(StaticMethod, *cls_var).func; - case tp_classmethod: return VAR(BoundMethod(_t(objtype), PK_OBJ_GET(ClassMethod, *cls_var).func)); - } - } - return *cls_var; - } - - const PyTypeInfo* ti = &_all_types[objtype]; - if(ti->m__getattr__) { - PyVar ret = ti->m__getattr__(this, obj, name); - if(ret) return ret; - } - - if(throw_err) AttributeError(obj, name); - return nullptr; -} - -// used by OP_LOAD_METHOD -// try to load a unbound method (fallback to `getattr` if not found) -PyVar VM::get_unbound_method(PyVar obj, StrName name, PyVar* self, bool throw_err, bool fallback) { - self->set_null(); - Type objtype(0); - // handle super() proxy - if(is_type(obj, tp_super)) { - const Super& super = PK_OBJ_GET(Super, obj); - obj = super.first; - objtype = super.second; - } else { - objtype = _tp(obj); - } - PyVar* cls_var = find_name_in_mro(objtype, name); - - if(fallback) { - if(cls_var != nullptr) { - // handle descriptor - if(is_type(*cls_var, tp_property)) { - const Property& prop = PK_OBJ_GET(Property, *cls_var); - return call(prop.getter, obj); - } - } - // handle instance __dict__ - if(!is_tagged(obj) && obj->is_attr_valid()) { - PyVar* val; - if(obj.type == tp_type) { - val = find_name_in_mro(PK_OBJ_GET(Type, obj), name); - if(val != nullptr) { - if(is_tagged(*val)) return *val; - if(val->type == tp_staticmethod) return PK_OBJ_GET(StaticMethod, *val).func; - if(val->type == tp_classmethod) return VAR(BoundMethod(obj, PK_OBJ_GET(ClassMethod, *val).func)); - return *val; - } - } else { - val = obj->attr().try_get_2_likely_found(name); - if(val != nullptr) return *val; - } - } - } - - if(cls_var != nullptr) { - if(!is_tagged(*cls_var)) { - switch(cls_var->type) { - case tp_function: *self = obj; break; - case tp_native_func: *self = obj; break; - case tp_staticmethod: self->set_null(); return PK_OBJ_GET(StaticMethod, *cls_var).func; - case tp_classmethod: *self = _t(objtype); return PK_OBJ_GET(ClassMethod, *cls_var).func; - } - } - return *cls_var; - } - - const PyTypeInfo* ti = &_all_types[objtype]; - if(fallback && ti->m__getattr__) { - PyVar ret = ti->m__getattr__(this, obj, name); - if(ret) return ret; - } - - if(throw_err) AttributeError(obj, name); - return nullptr; -} - -void VM::setattr(PyVar obj, StrName name, PyVar value) { - Type objtype(0); - // handle super() proxy - if(is_type(obj, tp_super)) { - Super& super = PK_OBJ_GET(Super, obj); - obj = super.first; - objtype = super.second; - } else { - objtype = _tp(obj); - } - PyVar* cls_var = find_name_in_mro(objtype, name); - if(cls_var != nullptr) { - // handle descriptor - if(is_type(*cls_var, tp_property)) { - const Property& prop = _CAST(Property&, *cls_var); - if(!is_none(prop.setter)) { - call(prop.setter, obj, value); - } else { - TypeError(_S("readonly attribute: ", name.escape())); - } - return; - } - } - - const PyTypeInfo* ti = &_all_types[objtype]; - if(ti->m__setattr__) { - ti->m__setattr__(this, obj, name, value); - return; - } - - // handle instance __dict__ - if(is_tagged(obj) || !obj->is_attr_valid()) TypeError("cannot set attribute"); - obj->attr().set(name, value); -} - -PyObject* VM::bind_func(PyObject* obj, StrName name, int argc, NativeFuncC fn, any userdata, BindType bt) { - PyObject* nf = new_object(tp_native_func, fn, argc, std::move(userdata)).get(); - switch(bt) { - case BindType_FUNCTION: break; - case BindType_STATICMETHOD: nf = new_object(tp_staticmethod, nf).get(); break; - case BindType_CLASSMETHOD: nf = new_object(tp_classmethod, nf).get(); break; - } - if(obj != nullptr) obj->attr().set(name, nf); - return nf; -} - -PyObject* VM::bind(PyObject* obj, const char* sig, NativeFuncC fn, any userdata, BindType bt) { - return bind(obj, sig, nullptr, fn, std::move(userdata), bt); -} - -PyObject* VM::bind(PyObject* obj, const char* sig, const char* docstring, NativeFuncC fn, any userdata, BindType bt) { - char buffer[256]; - int length = snprintf(buffer, sizeof(buffer), "def %s : pass", sig); - std::string_view source(buffer, length); - // fn(a, b, *c, d=1) -> None - CodeObject* code = compile(source, "", EXEC_MODE); - assert(code->func_decls.count == 1); - FuncDecl_ decl = c11__getitem(FuncDecl_, &code->func_decls, 0); - c11_vector__clear(&code->func_decls); // move decl - CodeObject__delete(code); // may leak if exception occurs - decl->docstring = docstring; - PyObject* f_obj = new_object(tp_native_func, fn, decl, std::move(userdata)).get(); - - switch(bt) { - case BindType_STATICMETHOD: f_obj = new_object(tp_staticmethod, f_obj).get(); break; - case BindType_CLASSMETHOD: f_obj = new_object(tp_classmethod, f_obj).get(); break; - case BindType_FUNCTION: break; - } - if(obj != nullptr){ - StrName name = pkpy_Str__data(&decl->code->name); - obj->attr().set(name, f_obj); - } - return f_obj; -} - -PyObject* VM::bind_property(PyObject* obj, const char* name, NativeFuncC fget, NativeFuncC fset) { - assert(is_type(obj, tp_type)); - std::string_view name_sv(name); - int pos = name_sv.find(':'); - if(pos > 0) name_sv = name_sv.substr(0, pos); - PyVar _0 = new_object(tp_native_func, fget, 1); - PyVar _1 = vm->None; - if(fset != nullptr) _1 = new_object(tp_native_func, fset, 2); - PyObject* prop = new_object(tp_property, _0, _1).get(); - obj->attr().set(StrName(name_sv), prop); - return prop; -} - -void VM::__builtin_error(StrName type) { _error(call(builtins->attr()[type])); } - -void VM::__builtin_error(StrName type, PyVar arg) { _error(call(builtins->attr()[type], arg)); } - -void VM::__builtin_error(StrName type, const Str& msg) { __builtin_error(type, VAR(msg)); } - -void VM::BinaryOptError(const char* op, PyVar _0, PyVar _1) { - StrName name_0 = _type_name(vm, _tp(_0)); - StrName name_1 = _type_name(vm, _tp(_1)); - TypeError(_S("unsupported operand type(s) for ", op, ": ", name_0.escape(), " and ", name_1.escape())); -} - -void VM::AttributeError(PyVar obj, StrName name) { - if(isinstance(obj, vm->tp_type)) { - __builtin_error( - "AttributeError", - _S("type object ", _type_name(vm, PK_OBJ_GET(Type, obj)).escape(), " has no attribute ", name.escape())); - } else { - __builtin_error("AttributeError", - _S(_type_name(vm, _tp(obj)).escape(), " object has no attribute ", name.escape())); - } -} - -void VM::_error(PyVar e_obj) { - assert(isinstance(e_obj, tp_exception)); - Exception& e = PK_OBJ_GET(Exception, e_obj); - if(callstack.empty()) { - e.is_re = false; - __last_exception = e_obj.get(); - throw TopLevelException(this, &e); - } - PUSH(e_obj); - __raise_exc(); -} - -void VM::__raise_exc(bool re_raise) { - Frame* frame = &callstack.top(); - Exception& e = PK_OBJ_GET(Exception, (PyVar&)s_data.sp[-1]); - if(!re_raise) { - e._ip_on_error = frame->ip(); - e._code_on_error = (void*)frame->co; - } - int next_ip = frame->prepare_jump_exception_handler(&s_data); - - int actual_ip = frame->ip(); - if(e._ip_on_error >= 0 && e._code_on_error == (void*)frame->co) actual_ip = e._ip_on_error; - int current_line = c11__at(BytecodeEx, &frame->co->codes_ex, actual_ip)->lineno; - const char* current_f_name = pkpy_Str__data(&frame->co->name); // current function name - if(frame->_callable == nullptr) current_f_name = ""; // not in a function - e.stpush(frame->co->src, current_line, nullptr, current_f_name); - - if(next_ip >= 0) { - throw InternalException(InternalExceptionType::Handled, next_ip); - } else { - throw InternalException(InternalExceptionType::Unhandled); - } -} - -StrName _type_name(VM* vm, Type type) { return vm->_all_types[type].name; } - -void VM::bind__getitem__(Type type, PyVar (*f)(VM*, PyVar, PyVar)) { - _all_types[type].m__getitem__ = f; - bind_func( - type, - __getitem__, - 2, - [](VM* vm, ArgsView args) { - return lambda_get_userdata(args.begin())(vm, args[0], args[1]); - }, - f); -} - -void VM::bind__setitem__(Type type, void (*f)(VM*, PyVar, PyVar, PyVar)) { - _all_types[type].m__setitem__ = f; - bind_func( - type, - __setitem__, - 3, - [](VM* vm, ArgsView args) { - lambda_get_userdata(args.begin())(vm, args[0], args[1], args[2]); - return vm->None; - }, - f); -} - -void VM::bind__delitem__(Type type, void (*f)(VM*, PyVar, PyVar)) { - _all_types[type].m__delitem__ = f; - bind_func( - type, - __delitem__, - 2, - [](VM* vm, ArgsView args) { - lambda_get_userdata(args.begin())(vm, args[0], args[1]); - return vm->None; - }, - f); -} - -PyVar VM::__pack_next_retval(unsigned n) { - if(n == 0) return StopIteration; - if(n == 1) return *--s_data.sp; - ArgsView view(s_data.sp - n, s_data.sp); - PyVar retval = VAR(view.to_tuple()); - s_data.sp -= n; - return retval; -} - -void VM::bind__next__(Type type, unsigned (*f)(VM*, PyVar)) { - _all_types[type].op__next__ = f; - bind_func( - type, - __next__, - 1, - [](VM* vm, ArgsView args) { - int n = lambda_get_userdata(args.begin())(vm, args[0]); - return vm->__pack_next_retval(n); - }, - f); -} - -void VM::bind__next__(Type type, PyVar (*f)(VM*, PyVar)) { - bind_func( - type, - __next__, - 1, - [](VM* vm, ArgsView args) { - auto f = lambda_get_userdata(args.begin()); - return f(vm, args[0]); - }, - f); -} - -#define BIND_UNARY_SPECIAL(name) \ - void VM::bind##name(Type type, PyVar (*f)(VM*, PyVar)) { \ - _all_types[type].m##name = f; \ - bind_func( \ - type, \ - name, \ - 1, \ - [](VM* vm, ArgsView args) { \ - return lambda_get_userdata(args.begin())(vm, args[0]); \ - }, \ - f); \ - } -BIND_UNARY_SPECIAL(__iter__) -BIND_UNARY_SPECIAL(__neg__) -BIND_UNARY_SPECIAL(__invert__) -#undef BIND_UNARY_SPECIAL - -void VM::bind__str__(Type type, Str (*f)(VM*, PyVar)) { - _all_types[type].m__str__ = f; - bind_func( - type, - __str__, - 1, - [](VM* vm, ArgsView args) { - Str s = lambda_get_userdata(args.begin())(vm, args[0]); - return VAR(s); - }, - f); -} - -void VM::bind__repr__(Type type, Str (*f)(VM*, PyVar)) { - _all_types[type].m__repr__ = f; - bind_func( - type, - __repr__, - 1, - [](VM* vm, ArgsView args) { - Str s = lambda_get_userdata(args.begin())(vm, args[0]); - return VAR(s); - }, - f); -} - -void VM::bind__hash__(Type type, i64 (*f)(VM*, PyVar)) { - _all_types[type].m__hash__ = f; - bind_func( - type, - __hash__, - 1, - [](VM* vm, ArgsView args) { - i64 ret = lambda_get_userdata(args.begin())(vm, args[0]); - return VAR(ret); - }, - f); -} - -void VM::bind__len__(Type type, i64 (*f)(VM*, PyVar)) { - _all_types[type].m__len__ = f; - bind_func( - type, - __len__, - 1, - [](VM* vm, ArgsView args) { - i64 ret = lambda_get_userdata(args.begin())(vm, args[0]); - return VAR(ret); - }, - f); -} - -#define BIND_BINARY_SPECIAL(name) \ - void VM::bind##name(Type type, BinaryFuncC f) { \ - _all_types[type].m##name = f; \ - bind_func( \ - type, \ - name, \ - 2, \ - [](VM* vm, ArgsView args) { \ - return lambda_get_userdata(args.begin())(vm, args[0], args[1]); \ - }, \ - f); \ - } - -BIND_BINARY_SPECIAL(__eq__) -BIND_BINARY_SPECIAL(__lt__) -BIND_BINARY_SPECIAL(__le__) -BIND_BINARY_SPECIAL(__gt__) -BIND_BINARY_SPECIAL(__ge__) -BIND_BINARY_SPECIAL(__contains__) - -BIND_BINARY_SPECIAL(__add__) -BIND_BINARY_SPECIAL(__sub__) -BIND_BINARY_SPECIAL(__mul__) -BIND_BINARY_SPECIAL(__truediv__) -BIND_BINARY_SPECIAL(__floordiv__) -BIND_BINARY_SPECIAL(__mod__) -BIND_BINARY_SPECIAL(__pow__) -BIND_BINARY_SPECIAL(__matmul__) - -BIND_BINARY_SPECIAL(__lshift__) -BIND_BINARY_SPECIAL(__rshift__) -BIND_BINARY_SPECIAL(__and__) -BIND_BINARY_SPECIAL(__or__) -BIND_BINARY_SPECIAL(__xor__) - -#undef BIND_BINARY_SPECIAL - -#if PK_ENABLE_PROFILER -void NextBreakpoint::_step(VM* vm) { - int curr_callstack_size = vm->callstack.size(); - int curr_lineno = vm->callstack.top().curr_lineno(); - if(should_step_into) { - if(curr_callstack_size != callstack_size || curr_lineno != lineno) { vm->__breakpoint(); } - } else { - if(curr_callstack_size == callstack_size) { - if(curr_lineno != lineno) vm->__breakpoint(); - } else if(curr_callstack_size < callstack_size) { - // returning - vm->__breakpoint(); - } - } -} -#endif - -void VM::__pop_frame() { - assert(top_frame); - s_data.sp = top_frame->p0; - // callstack.pop() - Frame* p = top_frame; - top_frame = p->f_back; - Frame__delete(p); - -#if PK_ENABLE_PROFILER - if(!_next_breakpoint.empty() && callstack.size() < _next_breakpoint.callstack_size) { - _next_breakpoint = NextBreakpoint(); - } -#endif -} - -void VM::__breakpoint() { -#if PK_ENABLE_PROFILER - _next_breakpoint = NextBreakpoint(); - - bool show_where = false; - bool show_headers = true; - - while(true) { - vector frames; - LinkedFrame* lf = callstack._tail; - while(lf != nullptr) { - frames.push_back(lf); - lf = lf->f_back; - if(frames.size() >= 4) break; - } - - if(show_headers) { - for(int i = frames.size() - 1; i >= 0; i--) { - if(!show_where && i != 0) continue; - - SStream ss; - Frame* frame = &frames[i]->frame; - int lineno = frame->curr_lineno(); - ss << "File \"" << frame->co->src.filename() << "\", line " << lineno; - if(frame->_callable) { - ss << ", in "; - ss << frame->_callable->as().decl->code->name; - } - ss << '\n'; - ss << "-> " << frame->co->src->get_line(lineno) << '\n'; - stdout_write(ss.str()); - } - show_headers = false; - } - - vm->stdout_write("(Pdb) "); - Frame* frame_0 = &frames[0]->frame; - - std::string line; - if(!std::getline(std::cin, line)) { - stdout_write("--KeyboardInterrupt--\n"); - continue; - } - - if(line == "h" || line == "help") { - stdout_write("h, help: show this help message\n"); - stdout_write("q, quit: exit the debugger\n"); - stdout_write("n, next: execute next line\n"); - stdout_write("s, step: step into\n"); - stdout_write("w, where: show current stack frame\n"); - stdout_write("c, continue: continue execution\n"); - stdout_write("a, args: show local variables\n"); - stdout_write("p, print : evaluate expression\n"); - stdout_write("l, list: show lines around current line\n"); - stderr_write("ll, longlist: show all lines\n"); - stdout_write("!: execute statement\n"); - continue; - } - if(line == "q" || line == "quit") { vm->RuntimeError("pdb quit"); } - if(line == "n" || line == "next") { - vm->_next_breakpoint = NextBreakpoint(vm->callstack.size(), frame_0->curr_lineno(), false); - break; - } - if(line == "s" || line == "step") { - vm->_next_breakpoint = NextBreakpoint(vm->callstack.size(), frame_0->curr_lineno(), true); - break; - } - if(line == "w" || line == "where") { - show_where = !show_where; - show_headers = true; - continue; - } - if(line == "c" || line == "continue") break; - if(line == "a" || line == "args") { - int i = 0; - for(PyVar obj: frame_0->_locals) { - if(obj == PY_NULL) continue; - StrName name = frame_0->co->varnames[i++]; - stdout_write(_S(name.sv(), " = ", vm->py_repr(obj), '\n')); - } - continue; - } - - bool is_list = line == "l" || line == "list"; - bool is_longlist = line == "ll" || line == "longlist"; - - if(is_list || is_longlist) { - if(frame_0->co->src->is_precompiled) continue; - int lineno = frame_0->curr_lineno(); - int start, end; - - if(is_list) { - int max_line = frame_0->co->src->line_starts.size() + 1; - start = (std::max)(1, lineno - 5); - end = (std::min)(max_line, lineno + 5); - } else { - start = frame_0->co->start_line; - end = frame_0->co->end_line; - if(start == -1 || end == -1) continue; - } - - SStream ss; - int max_width = std::to_string(end).size(); - for(int i = start; i <= end; i++) { - int spaces = max_width - std::to_string(i).size(); - ss << std::string(spaces, ' ') << std::to_string(i); - if(i == lineno) - ss << " -> "; - else - ss << " "; - ss << frame_0->co->src->get_line(i) << '\n'; - } - stdout_write(ss.str()); - continue; - } - - int space = line.find_first_of(' '); - if(space != -1) { - std::string cmd = line.substr(0, space); - std::string arg = line.substr(space + 1); - if(arg.empty()) continue; // ignore empty command - if(cmd == "p" || cmd == "print") { - CodeObject* code = compile(arg, "", EVAL_MODE, true); - PyVar retval = vm->_exec(code, frame_0->_module, frame_0->_callable, frame_0->_locals); - CodeObject__delete(code); - stdout_write(vm->py_repr(retval)); - stdout_write("\n"); - } else if(cmd == "!") { - CodeObject* code = compile(arg, "", EXEC_MODE, true); - vm->_exec(code, frame_0->_module, frame_0->_callable, frame_0->_locals); - CodeObject__delete(code); - } - continue; - } - } -#endif -} - -PyVar PyObject::attr(StrName name) const{ - return attr()[name]; -} - -/**************************************************************************/ -void Function::_gc_mark(VM* vm) const { - FuncDecl__gc_mark(decl); - if(_closure) { - _closure->apply([](StrName _, PyVar obj, void* userdata) { - VM* vm = (VM*)userdata; - vm->obj_gc_mark(obj); - }, vm); - } -} - -void NativeFunc::_gc_mark(VM* vm) const { - if(decl){ - FuncDecl__gc_mark(decl); - } -} - -extern "C"{ - void FuncDecl__gc_mark(const FuncDecl *self){ - VM* vm = (VM*)pkpy_g.vm; - CodeObject__gc_mark(self->code); - c11_vector__foreach(FuncDeclKwArg, &self->kwargs, kv) { - vm->obj_gc_mark(kv->value); - } - } - - void CodeObject__gc_mark(const CodeObject* self) { - VM* vm = (VM*)pkpy_g.vm; - c11_vector__foreach(PyVar, &self->consts, v) { - vm->obj_gc_mark(*v); - } - c11_vector__foreach(FuncDecl*, &self->func_decls, decl) { - FuncDecl__gc_mark(*decl); - } - } -} - -void List::_gc_mark(VM* vm) const { - for(PyVar obj: *this) - vm->obj_gc_mark(obj); -} - -void Tuple::_gc_mark(VM* vm) const { - for(PyVar obj: *this) - vm->obj_gc_mark(obj); -} - -void MappingProxy::_gc_mark(VM* vm) const { vm->__obj_gc_mark(obj); } - -void BoundMethod::_gc_mark(VM* vm) const { - vm->obj_gc_mark(func); - vm->obj_gc_mark(self); -} - -void StarWrapper::_gc_mark(VM* vm) const { vm->obj_gc_mark(obj); } - -void StaticMethod::_gc_mark(VM* vm) const { vm->obj_gc_mark(func); } - -void ClassMethod::_gc_mark(VM* vm) const { vm->obj_gc_mark(func); } - -void Property::_gc_mark(VM* vm) const { - vm->obj_gc_mark(getter); - vm->obj_gc_mark(setter); -} - -void Slice::_gc_mark(VM* vm) const { - vm->obj_gc_mark(start); - vm->obj_gc_mark(stop); - vm->obj_gc_mark(step); -} - -void Super::_gc_mark(VM* vm) const { vm->obj_gc_mark(first); } - -void Frame::_gc_mark(VM* vm) const { - vm->obj_gc_mark(_module); - CodeObject__gc_mark(co); - // Frame could be stored in a generator, so mark _callable for safety - vm->obj_gc_mark(_callable); -} - -extern "C"{ - void pk_ManagedHeap__mark(pk_ManagedHeap* self){ - VM* vm = (VM*)self->vm; - for(int i=0; ino_gc.count; i++){ - PyObject* obj = c11__getitem(PyObject*, &self->no_gc, i); - vm->__obj_gc_mark(obj); - } - vm->callstack.apply([vm](Frame& frame) { - frame._gc_mark(vm); - }); - vm->obj_gc_mark(vm->__last_exception); - vm->obj_gc_mark(vm->__curr_class); - vm->obj_gc_mark(vm->__c.error); - vm->__stack_gc_mark((PyVar*)vm->s_data.begin, (PyVar*)vm->s_data.end); - if(self->_gc_marker_ex) self->_gc_marker_ex((pk_VM*)vm); - } - - void pk_ManagedHeap__delete_obj(pk_ManagedHeap* self, ::PyObject* __obj){ - PyObject* obj = (PyObject*)__obj; - const PyTypeInfo* ti = ((VM*)(self->vm))->_tp_info(obj->type); - if(ti->vt._dtor) ti->vt._dtor(obj->_value_ptr()); - if (obj->_attr) - c11_vector__dtor(obj->_attr); - delete obj->_attr; // delete __dict__ if exists - if(obj->gc_is_large){ - std::free(obj); - }else{ - PoolObject_dealloc(obj); - } - } -} - -void Dict::_gc_mark(VM* vm) const { - apply([vm](PyVar k, PyVar v) { - vm->obj_gc_mark(k); - vm->obj_gc_mark(v); - }); -} - -} // namespace pkpy