| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- #pragma once
- #include "common.h"
- #include "vm.h"
- namespace pkpy {
- #define PY_CLASS(T, mod, name) \
- static Type _type(VM* vm) { \
- static const StrName __x0(#mod); \
- static const StrName __x1(#name); \
- return PK_OBJ_GET(Type, vm->_modules[__x0]->attr(__x1)); \
- } \
- static void _check_type(VM* vm, PyObject* val){ \
- if(!vm->isinstance(val, T::_type(vm))){ \
- vm->TypeError("expected '" #mod "." #name "', got " + OBJ_NAME(vm->_t(val)).escape()); \
- } \
- } \
- static PyObject* register_class(VM* vm, PyObject* mod) { \
- if(OBJ_NAME(mod) != #mod) { \
- auto msg = fmt("register_class() failed: ", OBJ_NAME(mod), " != ", #mod); \
- throw std::runtime_error(msg); \
- } \
- PyObject* type = vm->new_type_object(mod, #name, vm->tp_object); \
- T::_register(vm, mod, type); \
- type->attr()._try_perfect_rehash(); \
- return type; \
- }
- #define VAR_T(T, ...) vm->heap.gcnew<T>(T::_type(vm), T(__VA_ARGS__))
- static int c99_sizeof(VM*, const Str&);
- struct VoidP{
- PY_CLASS(VoidP, c, void_p)
- void* ptr;
- int base_offset;
- VoidP(void* ptr): ptr(ptr), base_offset(1){}
- VoidP(): ptr(nullptr), base_offset(1){}
- bool operator==(const VoidP& other) const {
- return ptr == other.ptr && base_offset == other.base_offset;
- }
- bool operator!=(const VoidP& other) const {
- return ptr != other.ptr || base_offset != other.base_offset;
- }
- bool operator<(const VoidP& other) const { return ptr < other.ptr; }
- bool operator<=(const VoidP& other) const { return ptr <= other.ptr; }
- bool operator>(const VoidP& other) const { return ptr > other.ptr; }
- bool operator>=(const VoidP& other) const { return ptr >= other.ptr; }
- Str hex() const{
- std::stringstream ss;
- ss << std::hex << reinterpret_cast<intptr_t>(ptr);
- return "0x" + ss.str();
- }
- static void _register(VM* vm, PyObject* mod, PyObject* type){
- vm->bind_default_constructor<VoidP>(type);
- vm->bind_func<1>(type, "from_hex", [](VM* vm, ArgsView args){
- std::string s = CAST(Str&, args[0]).str();
- size_t size;
- intptr_t ptr = std::stoll(s, &size, 16);
- if(size != s.size()) vm->ValueError("invalid literal for void_p(): " + s);
- return VAR_T(VoidP, (void*)ptr);
- });
- vm->bind_method<0>(type, "hex", [](VM* vm, ArgsView args){
- VoidP& self = _CAST(VoidP&, args[0]);
- return VAR(self.hex());
- });
- vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
- VoidP& self = _CAST(VoidP&, obj);
- std::stringstream ss;
- ss << "<void* at " << self.hex();
- if(self.base_offset != 1) ss << ", base_offset=" << self.base_offset;
- ss << ">";
- return VAR(ss.str());
- });
- #define BIND_CMP(name, op) \
- vm->bind##name(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){ \
- if(!is_non_tagged_type(rhs, VoidP::_type(vm))) return vm->NotImplemented; \
- return VAR(_CAST(VoidP&, lhs) op _CAST(VoidP&, rhs)); \
- });
- BIND_CMP(__eq__, ==)
- BIND_CMP(__lt__, <)
- BIND_CMP(__le__, <=)
- BIND_CMP(__gt__, >)
- BIND_CMP(__ge__, >=)
- #undef BIND_CMP
- vm->bind__hash__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
- VoidP& self = _CAST(VoidP&, obj);
- return reinterpret_cast<i64>(self.ptr);
- });
- vm->bind_method<1>(type, "set_base_offset", [](VM* vm, ArgsView args){
- VoidP& self = _CAST(VoidP&, args[0]);
- if(is_non_tagged_type(args[1], vm->tp_str)){
- const Str& type = _CAST(Str&, args[1]);
- self.base_offset = c99_sizeof(vm, type);
- }else{
- self.base_offset = CAST(int, args[1]);
- }
- return vm->None;
- });
- vm->bind_method<0>(type, "get_base_offset", [](VM* vm, ArgsView args){
- VoidP& self = _CAST(VoidP&, args[0]);
- return VAR(self.base_offset);
- });
- vm->bind_method<1>(type, "offset", [](VM* vm, ArgsView args){
- VoidP& self = _CAST(VoidP&, args[0]);
- i64 offset = CAST(i64, args[1]);
- return VAR_T(VoidP, (char*)self.ptr + offset * self.base_offset);
- });
- vm->bind__add__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
- VoidP& self = _CAST(VoidP&, lhs);
- i64 offset = CAST(i64, rhs);
- return VAR_T(VoidP, (char*)self.ptr + offset);
- });
- vm->bind__sub__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
- VoidP& self = _CAST(VoidP&, lhs);
- i64 offset = CAST(i64, rhs);
- return VAR_T(VoidP, (char*)self.ptr - offset);
- });
- #define BIND_SETGET(T, name) \
- vm->bind_method<0>(type, "read_" name, [](VM* vm, ArgsView args){ \
- VoidP& self = _CAST(VoidP&, args[0]); \
- return VAR(*(T*)self.ptr); \
- }); \
- vm->bind_method<1>(type, "write_" name, [](VM* vm, ArgsView args){ \
- VoidP& self = _CAST(VoidP&, args[0]); \
- *(T*)self.ptr = CAST(T, args[1]); \
- return vm->None; \
- });
- BIND_SETGET(char, "char")
- BIND_SETGET(unsigned char, "uchar")
- BIND_SETGET(short, "short")
- BIND_SETGET(unsigned short, "ushort")
- BIND_SETGET(int, "int")
- BIND_SETGET(unsigned int, "uint")
- BIND_SETGET(long, "long")
- BIND_SETGET(unsigned long, "ulong")
- BIND_SETGET(long long, "longlong")
- BIND_SETGET(unsigned long long, "ulonglong")
- BIND_SETGET(float, "float")
- BIND_SETGET(double, "double")
- BIND_SETGET(bool, "bool")
- vm->bind_method<0>(type, "read_void_p", [](VM* vm, ArgsView args){
- VoidP& self = _CAST(VoidP&, args[0]);
- return VAR_T(VoidP, *(void**)self.ptr);
- });
- vm->bind_method<1>(type, "write_void_p", [](VM* vm, ArgsView args){
- VoidP& self = _CAST(VoidP&, args[0]);
- VoidP& other = CAST(VoidP&, args[0]);
- self.ptr = other.ptr;
- return vm->None;
- });
- vm->bind_method<1>(type, "read_bytes", [](VM* vm, ArgsView args){
- VoidP& self = _CAST(VoidP&, args[0]);
- i64 size = CAST(i64, args[1]);
- std::vector<char> buffer(size);
- memcpy(buffer.data(), self.ptr, size);
- return VAR(Bytes(std::move(buffer)));
- });
- vm->bind_method<1>(type, "write_bytes", [](VM* vm, ArgsView args){
- VoidP& self = _CAST(VoidP&, args[0]);
- Bytes& bytes = CAST(Bytes&, args[1]);
- memcpy(self.ptr, bytes.data(), bytes.size());
- return vm->None;
- });
- }
- };
- struct C99Struct{
- PY_CLASS(C99Struct, c, struct)
- static constexpr int INLINE_SIZE = 24;
- char _inlined[INLINE_SIZE];
- char* p;
- int size;
- void _init(int new_size){
- this->size = new_size;
- if(size <= INLINE_SIZE){
- p = _inlined;
- }else{
- p = (char*)malloc(size);
- }
- }
- template<typename T>
- C99Struct(const T& data){
- static_assert(std::is_pod_v<T>);
- static_assert(!std::is_pointer_v<T>);
- _init(sizeof(T));
- memcpy(p, &data, this->size);
- }
- C99Struct() { p = _inlined; }
- C99Struct(void* p, int size){
- _init(size);
- if(p!=nullptr) memcpy(this->p, p, size);
- }
- ~C99Struct(){ if(p!=_inlined) free(p); }
- C99Struct(const C99Struct& other){
- _init(other.size);
- memcpy(p, other.p, size);
- }
- static void _register(VM* vm, PyObject* mod, PyObject* type){
- vm->bind_default_constructor<C99Struct>(type);
- vm->bind_method<0>(type, "addr", [](VM* vm, ArgsView args){
- C99Struct& self = _CAST(C99Struct&, args[0]);
- return VAR_T(VoidP, self.p);
- });
- vm->bind_method<0>(type, "size", [](VM* vm, ArgsView args){
- C99Struct& self = _CAST(C99Struct&, args[0]);
- return VAR(self.size);
- });
- vm->bind_method<0>(type, "copy", [](VM* vm, ArgsView args){
- const C99Struct& self = _CAST(C99Struct&, args[0]);
- return VAR_T(C99Struct, self);
- });
- vm->bind__eq__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
- C99Struct& self = _CAST(C99Struct&, lhs);
- if(!is_non_tagged_type(rhs, C99Struct::_type(vm))) return vm->NotImplemented;
- C99Struct& other = _CAST(C99Struct&, rhs);
- bool ok = self.size == other.size && memcmp(self.p, other.p, self.size) == 0;
- return VAR(ok);
- });
- // patch VoidP
- type = vm->_t(VoidP::_type(vm));
- vm->bind_method<1>(type, "read_struct", [](VM* vm, ArgsView args){
- VoidP& self = _CAST(VoidP&, args[0]);
- const Str& type = CAST(Str&, args[1]);
- int size = c99_sizeof(vm, type);
- return VAR_T(C99Struct, self.ptr, size);
- });
- vm->bind_method<1>(type, "write_struct", [](VM* vm, ArgsView args){
- VoidP& self = _CAST(VoidP&, args[0]);
- C99Struct& other = CAST(C99Struct&, args[1]);
- memcpy(self.ptr, other.p, other.size);
- return vm->None;
- });
- }
- };
- struct ReflField{
- std::string_view name;
- int offset;
- bool operator<(const ReflField& other) const{ return name < other.name; }
- bool operator==(const ReflField& other) const{ return name == other.name; }
- bool operator!=(const ReflField& other) const{ return name != other.name; }
- bool operator<(std::string_view other) const{ return name < other; }
- bool operator==(std::string_view other) const{ return name == other; }
- bool operator!=(std::string_view other) const{ return name != other; }
- };
- struct ReflType{
- std::string_view name;
- size_t size;
- std::vector<ReflField> fields;
- };
- inline static std::map<std::string_view, ReflType> _refl_types;
- inline void add_refl_type(std::string_view name, size_t size, std::vector<ReflField> fields){
- ReflType type{name, size, std::move(fields)};
- std::sort(type.fields.begin(), type.fields.end());
- _refl_types[name] = std::move(type);
- }
- inline static int c99_sizeof(VM* vm, const Str& type){
- auto it = _refl_types.find(type.sv());
- if(it != _refl_types.end()) return it->second.size;
- vm->ValueError("not a valid c99 type");
- return 0;
- }
- struct C99ReflType final: ReflType{
- PY_CLASS(C99ReflType, c, _refl)
- C99ReflType(const ReflType& type){
- this->name = type.name;
- this->size = type.size;
- this->fields = type.fields;
- }
- static void _register(VM* vm, PyObject* mod, PyObject* type){
- vm->bind_notimplemented_constructor<C99ReflType>(type);
- vm->bind_method<0>(type, "__call__", [](VM* vm, ArgsView args){
- C99ReflType& self = _CAST(C99ReflType&, args[0]);
- return VAR_T(C99Struct, nullptr, self.size);
- });
- vm->bind_method<0>(type, "name", [](VM* vm, ArgsView args){
- C99ReflType& self = _CAST(C99ReflType&, args[0]);
- return VAR(self.name);
- });
- vm->bind_method<0>(type, "size", [](VM* vm, ArgsView args){
- C99ReflType& self = _CAST(C99ReflType&, args[0]);
- return VAR(self.size);
- });
- vm->bind__getitem__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj, PyObject* key){
- C99ReflType& self = _CAST(C99ReflType&, obj);
- const Str& name = CAST(Str&, key);
- auto it = std::lower_bound(self.fields.begin(), self.fields.end(), name.sv());
- if(it == self.fields.end() || it->name != name.sv()){
- vm->KeyError(key);
- return vm->None;
- }
- return VAR(it->offset);
- });
- }
- };
- static_assert(sizeof(Py_<C99Struct>) <= 64);
- static_assert(sizeof(Py_<Tuple>) <= 64);
- inline PyObject* py_var(VM* vm, void* p){
- return VAR_T(VoidP, p);
- }
- inline PyObject* py_var(VM* vm, char* p){
- return VAR_T(VoidP, p);
- }
- /***********************************************/
- template<typename T>
- T to_void_p(VM* vm, PyObject* var){
- static_assert(std::is_pointer_v<T>);
- VoidP& p = CAST(VoidP&, var);
- return reinterpret_cast<T>(p.ptr);
- }
- template<typename T>
- T to_c99_struct(VM* vm, PyObject* var){
- static_assert(std::is_pod_v<T>);
- C99Struct& pod = CAST(C99Struct&, var);
- return *reinterpret_cast<T*>(pod.p);
- }
- template<typename T>
- std::enable_if_t<std::is_pod_v<T> && !std::is_pointer_v<T>, PyObject*> py_var(VM* vm, const T& data){
- return VAR_T(C99Struct, data);
- }
- /*****************************************************************/
- struct NativeProxyFuncCBase {
- virtual PyObject* operator()(VM* vm, ArgsView args) = 0;
- static void check_args_size(VM* vm, ArgsView args, int n){
- if (args.size() != n){
- vm->TypeError("expected " + std::to_string(n) + " arguments, got " + std::to_string(args.size()));
- }
- }
- };
- template<typename Ret, typename... Params>
- struct NativeProxyFuncC final: NativeProxyFuncCBase {
- static constexpr int N = sizeof...(Params);
- using _Fp = Ret(*)(Params...);
- _Fp func;
- NativeProxyFuncC(_Fp func) : func(func) {}
- PyObject* operator()(VM* vm, ArgsView args) override {
- check_args_size(vm, args, N);
- return call<Ret>(vm, args, std::make_index_sequence<N>());
- }
- template<typename __Ret, size_t... Is>
- PyObject* call(VM* vm, ArgsView args, std::index_sequence<Is...>){
- if constexpr(std::is_void_v<__Ret>){
- func(py_cast<Params>(vm, args[Is])...);
- return vm->None;
- }else{
- __Ret ret = func(py_cast<Params>(vm, args[Is])...);
- return VAR(std::move(ret));
- }
- }
- };
- inline PyObject* _any_c_wrapper(VM* vm, ArgsView args){
- NativeProxyFuncCBase* pf = lambda_get_userdata<NativeProxyFuncCBase*>(args.begin());
- return (*pf)(vm, args);
- }
- template<typename T>
- inline void bind_any_c_fp(VM* vm, PyObject* obj, Str name, T fp){
- static_assert(std::is_pod_v<T>);
- static_assert(std::is_pointer_v<T>);
- auto proxy = new NativeProxyFuncC(fp);
- PyObject* func = VAR(NativeFunc(_any_c_wrapper, proxy->N, false));
- _CAST(NativeFunc&, func).set_userdata(proxy);
- obj->attr().set(name, func);
- }
- inline void add_module_c(VM* vm){
- PyObject* mod = vm->new_module("c");
-
- vm->bind_func<1>(mod, "malloc", [](VM* vm, ArgsView args){
- i64 size = CAST(i64, args[0]);
- return VAR(malloc(size));
- });
- vm->bind_func<1>(mod, "free", [](VM* vm, ArgsView args){
- void* p = CAST(void*, args[0]);
- free(p);
- return vm->None;
- });
- vm->bind_func<1>(mod, "sizeof", [](VM* vm, ArgsView args){
- const Str& type = CAST(Str&, args[0]);
- i64 size = c99_sizeof(vm, type);
- return VAR(size);
- });
- vm->bind_func<1>(mod, "refl", [](VM* vm, ArgsView args){
- const Str& key = CAST(Str&, args[0]);
- auto it = _refl_types.find(key.sv());
- if(it == _refl_types.end()) vm->ValueError("reflection type not found");
- const ReflType& rt = it->second;
- return VAR_T(C99ReflType, rt);
- });
- vm->bind_func<3>(mod, "memset", [](VM* vm, ArgsView args){
- void* p = CAST(void*, args[0]);
- memset(p, CAST(int, args[1]), CAST(size_t, args[2]));
- return vm->None;
- });
- vm->bind_func<3>(mod, "memcpy", [](VM* vm, ArgsView args){
- void* dst = CAST(void*, args[0]);
- void* src = CAST(void*, args[1]);
- i64 size = CAST(i64, args[2]);
- memcpy(dst, src, size);
- return vm->None;
- });
- VoidP::register_class(vm, mod);
- C99Struct::register_class(vm, mod);
- C99ReflType::register_class(vm, mod);
- mod->attr().set("NULL", VAR_T(VoidP, nullptr));
- add_refl_type("char", sizeof(char), {});
- add_refl_type("uchar", sizeof(unsigned char), {});
- add_refl_type("short", sizeof(short), {});
- add_refl_type("ushort", sizeof(unsigned short), {});
- add_refl_type("int", sizeof(int), {});
- add_refl_type("uint", sizeof(unsigned int), {});
- add_refl_type("long", sizeof(long), {});
- add_refl_type("ulong", sizeof(unsigned long), {});
- add_refl_type("longlong", sizeof(long long), {});
- add_refl_type("ulonglong", sizeof(unsigned long long), {});
- add_refl_type("float", sizeof(float), {});
- add_refl_type("double", sizeof(double), {});
- add_refl_type("bool", sizeof(bool), {});
- add_refl_type("void_p", sizeof(void*), {});
- }
- struct Int32Flags{
- PY_CLASS(Int32Flags, builtins, int32flags)
- uint32_t value;
- Int32Flags(uint32_t value) : value(value) {}
- static void _register(VM* vm, PyObject* mod, PyObject* type){
- vm->bind_constructor<-1>(type, [](VM* vm, ArgsView args){
- vm->check_args_size(args.size(), 1+0, 1+1);
- uint32_t value = CAST_DEFAULT(uint32_t, 1, 0);
- return VAR_T(Int32Flags, value);
- });
- vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* self){
- uint32_t value = _CAST(Int32Flags&, self).value;
- std::string str;
- for(int i=0; i<32; ++i) str += (value & (1<<i)) ? '1' : '0';
- std::reverse(str.begin(), str.end());
- return VAR(str);
- });
- vm->bind__and__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
- uint32_t value = _CAST(Int32Flags&, lhs).value;
- if(!is_non_tagged_type(rhs, Int32Flags::_type(vm))) return vm->NotImplemented;
- return VAR_T(Int32Flags, value & _CAST(Int32Flags&, rhs).value);
- });
- vm->bind__or__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
- uint32_t value = _CAST(Int32Flags&, lhs).value;
- if(!is_non_tagged_type(rhs, Int32Flags::_type(vm))) return vm->NotImplemented;
- return VAR_T(Int32Flags, value | _CAST(Int32Flags&, rhs).value);
- });
- vm->bind__xor__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
- uint32_t value = _CAST(Int32Flags&, lhs).value;
- if(!is_non_tagged_type(rhs, Int32Flags::_type(vm))) return vm->NotImplemented;
- return VAR_T(Int32Flags, value ^ _CAST(Int32Flags&, rhs).value);
- });
- vm->bind__lshift__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
- uint32_t value = _CAST(Int32Flags&, lhs).value;
- if(!is_int(rhs)) return vm->NotImplemented;
- return VAR_T(Int32Flags, value << _CAST(i64, rhs));
- });
- vm->bind__rshift__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
- uint32_t value = _CAST(Int32Flags&, lhs).value;
- if(!is_int(rhs)) return vm->NotImplemented;
- return VAR_T(Int32Flags, value >> _CAST(i64, rhs));
- });
- vm->bind__getitem__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* self, PyObject* i){
- uint32_t value = _CAST(Int32Flags&, self).value;
- int index = CAST(i64, i);
- index = vm->normalized_index(index, 32);
- int ok = value & (1 << index);
- return VAR(ok);
- });
- vm->bind__setitem__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* self, PyObject* i, PyObject* v){
- uint32_t& value = _CAST(Int32Flags&, self).value;
- int index = CAST(i64, i);
- index = vm->normalized_index(index, 32);
- bool ok = CAST(i64, v) != 0;
- if(ok) value |= (1 << index);
- else value &= ~(1 << index);
- });
- }
- };
- } // namespace pkpy
|