vm.h 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. #pragma once
  2. #include "codeobject.h"
  3. #include "common.h"
  4. #include "frame.h"
  5. #include "error.h"
  6. #include "gc.h"
  7. #include "memory.h"
  8. #include "obj.h"
  9. #include "str.h"
  10. #include "tuplelist.h"
  11. #include "dict.h"
  12. namespace pkpy{
  13. /* Stack manipulation macros */
  14. // https://github.com/python/cpython/blob/3.9/Python/ceval.c#L1123
  15. #define TOP() (s_data.top())
  16. #define SECOND() (s_data.second())
  17. #define THIRD() (s_data.third())
  18. #define PEEK(n) (s_data.peek(n))
  19. #define STACK_SHRINK(n) (s_data.shrink(n))
  20. #define PUSH(v) (s_data.push(v))
  21. #define POP() (s_data.pop())
  22. #define POPX() (s_data.popx())
  23. #define STACK_VIEW(n) (s_data.view(n))
  24. typedef Bytes (*ReadFileCwdFunc)(const Str& name);
  25. inline ReadFileCwdFunc _read_file_cwd = [](const Str& name) { return Bytes(); };
  26. inline int set_read_file_cwd(ReadFileCwdFunc func) { _read_file_cwd = func; return 0; }
  27. #define DEF_NATIVE_2(ctype, ptype) \
  28. template<> inline ctype py_cast<ctype>(VM* vm, PyObject* obj) { \
  29. vm->check_non_tagged_type(obj, vm->ptype); \
  30. return OBJ_GET(ctype, obj); \
  31. } \
  32. template<> inline ctype _py_cast<ctype>(VM* vm, PyObject* obj) { \
  33. return OBJ_GET(ctype, obj); \
  34. } \
  35. template<> inline ctype& py_cast<ctype&>(VM* vm, PyObject* obj) { \
  36. vm->check_non_tagged_type(obj, vm->ptype); \
  37. return OBJ_GET(ctype, obj); \
  38. } \
  39. template<> inline ctype& _py_cast<ctype&>(VM* vm, PyObject* obj) { \
  40. return OBJ_GET(ctype, obj); \
  41. } \
  42. inline PyObject* py_var(VM* vm, const ctype& value) { return vm->heap.gcnew(vm->ptype, value);} \
  43. inline PyObject* py_var(VM* vm, ctype&& value) { return vm->heap.gcnew(vm->ptype, std::move(value));}
  44. class Generator final: public BaseIter {
  45. Frame frame;
  46. int state; // 0,1,2
  47. List s_backup;
  48. public:
  49. Generator(VM* vm, Frame&& frame, ArgsView buffer): BaseIter(vm), frame(std::move(frame)), state(0) {
  50. for(PyObject* obj: buffer) s_backup.push_back(obj);
  51. }
  52. PyObject* next() override;
  53. void _gc_mark() const;
  54. };
  55. struct PyTypeInfo{
  56. PyObject* obj;
  57. Type base;
  58. Str name;
  59. bool subclass_enabled;
  60. // cached special methods
  61. // unary operators
  62. PyObject* (*m__repr__)(VM* vm, PyObject*) = nullptr;
  63. PyObject* (*m__str__)(VM* vm, PyObject*) = nullptr;
  64. PyObject* (*m__hash__)(VM* vm, PyObject*) = nullptr;
  65. PyObject* (*m__len__)(VM* vm, PyObject*) = nullptr;
  66. PyObject* (*m__iter__)(VM* vm, PyObject*) = nullptr;
  67. PyObject* (*m__next__)(VM* vm, PyObject*) = nullptr;
  68. PyObject* (*m__json__)(VM* vm, PyObject*) = nullptr;
  69. PyObject* (*m__neg__)(VM* vm, PyObject*) = nullptr;
  70. PyObject* (*m__bool__)(VM* vm, PyObject*) = nullptr;
  71. bool (*m__eq__)(VM* vm, PyObject*, PyObject*) = nullptr;
  72. bool (*m__ne__)(VM* vm, PyObject*, PyObject*) = nullptr;
  73. bool (*m__lt__)(VM* vm, PyObject*, PyObject*) = nullptr;
  74. bool (*m__le__)(VM* vm, PyObject*, PyObject*) = nullptr;
  75. bool (*m__gt__)(VM* vm, PyObject*, PyObject*) = nullptr;
  76. bool (*m__ge__)(VM* vm, PyObject*, PyObject*) = nullptr;
  77. bool (*m__contains__)(VM* vm, PyObject*, PyObject*) = nullptr;
  78. // binary operators
  79. PyObject* (*m__add__)(VM* vm, PyObject*, PyObject*) = nullptr;
  80. PyObject* (*m__sub__)(VM* vm, PyObject*, PyObject*) = nullptr;
  81. PyObject* (*m__mul__)(VM* vm, PyObject*, PyObject*) = nullptr;
  82. PyObject* (*m__truediv__)(VM* vm, PyObject*, PyObject*) = nullptr;
  83. PyObject* (*m__floordiv__)(VM* vm, PyObject*, PyObject*) = nullptr;
  84. PyObject* (*m__mod__)(VM* vm, PyObject*, PyObject*) = nullptr;
  85. PyObject* (*m__pow__)(VM* vm, PyObject*, PyObject*) = nullptr;
  86. PyObject* (*m__matmul__)(VM* vm, PyObject*, PyObject*) = nullptr;
  87. PyObject* (*m__lshift__)(VM* vm, PyObject*, PyObject*) = nullptr;
  88. PyObject* (*m__rshift__)(VM* vm, PyObject*, PyObject*) = nullptr;
  89. PyObject* (*m__and__)(VM* vm, PyObject*, PyObject*) = nullptr;
  90. PyObject* (*m__or__)(VM* vm, PyObject*, PyObject*) = nullptr;
  91. PyObject* (*m__xor__)(VM* vm, PyObject*, PyObject*) = nullptr;
  92. // indexer
  93. PyObject* (*m__getitem__)(VM* vm, PyObject*, PyObject*) = nullptr;
  94. PyObject* (*m__setitem__)(VM* vm, PyObject*, PyObject*, PyObject*) = nullptr;
  95. PyObject* (*m__delitem__)(VM* vm, PyObject*, PyObject*) = nullptr;
  96. };
  97. struct FrameId{
  98. std::vector<pkpy::Frame>* data;
  99. int index;
  100. FrameId(std::vector<pkpy::Frame>* data, int index) : data(data), index(index) {}
  101. Frame* operator->() const { return &data->operator[](index); }
  102. };
  103. typedef void(*PrintFunc)(VM*, const Str&);
  104. class VM {
  105. VM* vm; // self reference for simplify code
  106. public:
  107. ManagedHeap heap;
  108. ValueStack s_data;
  109. stack< Frame > callstack;
  110. std::vector<PyTypeInfo> _all_types;
  111. void (*_gc_marker_ex)(VM*) = nullptr;
  112. NameDict _modules; // loaded modules
  113. std::map<StrName, Str> _lazy_modules; // lazy loaded modules
  114. PyObject* None;
  115. PyObject* True;
  116. PyObject* False;
  117. PyObject* Ellipsis;
  118. PyObject* builtins; // builtins module
  119. PyObject* StopIteration;
  120. PyObject* _main; // __main__ module
  121. PrintFunc _stdout;
  122. PrintFunc _stderr;
  123. // for quick access
  124. Type tp_object, tp_type, tp_int, tp_float, tp_bool, tp_str;
  125. Type tp_list, tp_tuple;
  126. Type tp_function, tp_native_func, tp_iterator, tp_bound_method;
  127. Type tp_slice, tp_range, tp_module;
  128. Type tp_super, tp_exception, tp_bytes, tp_mappingproxy;
  129. const bool enable_os;
  130. VM(bool enable_os=true) : heap(this), enable_os(enable_os) {
  131. this->vm = this;
  132. _stdout = [](VM* vm, const Str& s) { std::cout << s; };
  133. _stderr = [](VM* vm, const Str& s) { std::cerr << s; };
  134. callstack.reserve(8);
  135. _main = nullptr;
  136. init_builtin_types();
  137. }
  138. FrameId top_frame() {
  139. #if DEBUG_EXTRA_CHECK
  140. if(callstack.empty()) FATAL_ERROR();
  141. #endif
  142. return FrameId(&callstack.data(), callstack.size()-1);
  143. }
  144. PyObject* asStr(PyObject* obj){
  145. PyObject* self;
  146. PyObject* f = get_unbound_method(obj, __str__, &self, false);
  147. if(self != PY_NULL) return call_method(self, f);
  148. return asRepr(obj);
  149. }
  150. PyObject* asIter(PyObject* obj){
  151. if(is_type(obj, tp_iterator)) return obj;
  152. PyObject* self;
  153. PyObject* iter_f = get_unbound_method(obj, __iter__, &self, false);
  154. if(self != PY_NULL) return call_method(self, iter_f);
  155. TypeError(OBJ_NAME(_t(obj)).escape() + " object is not iterable");
  156. return nullptr;
  157. }
  158. PyObject* find_name_in_mro(PyObject* cls, StrName name){
  159. PyObject* val;
  160. do{
  161. val = cls->attr().try_get(name);
  162. if(val != nullptr) return val;
  163. Type base = _all_types[OBJ_GET(Type, cls)].base;
  164. if(base.index == -1) break;
  165. cls = _all_types[base].obj;
  166. }while(true);
  167. return nullptr;
  168. }
  169. bool isinstance(PyObject* obj, Type cls_t){
  170. Type obj_t = OBJ_GET(Type, _t(obj));
  171. do{
  172. if(obj_t == cls_t) return true;
  173. Type base = _all_types[obj_t].base;
  174. if(base.index == -1) break;
  175. obj_t = base;
  176. }while(true);
  177. return false;
  178. }
  179. PyObject* exec(Str source, Str filename, CompileMode mode, PyObject* _module=nullptr){
  180. if(_module == nullptr) _module = _main;
  181. try {
  182. CodeObject_ code = compile(source, filename, mode);
  183. #if DEBUG_DIS_EXEC
  184. if(_module == _main) std::cout << disassemble(code) << '\n';
  185. #endif
  186. return _exec(code, _module);
  187. }catch (const Exception& e){
  188. _stderr(this, e.summary() + "\n");
  189. }
  190. #if !DEBUG_FULL_EXCEPTION
  191. catch (const std::exception& e) {
  192. _stderr(this, "An std::exception occurred! It could be a bug.\n");
  193. _stderr(this, e.what());
  194. _stderr(this, "\n");
  195. }
  196. #endif
  197. callstack.clear();
  198. s_data.clear();
  199. return nullptr;
  200. }
  201. template<typename ...Args>
  202. PyObject* _exec(Args&&... args){
  203. callstack.emplace(&s_data, s_data._sp, std::forward<Args>(args)...);
  204. return _run_top_frame();
  205. }
  206. void _pop_frame(){
  207. Frame* frame = &callstack.top();
  208. s_data.reset(frame->_sp_base);
  209. callstack.pop();
  210. }
  211. void _push_varargs(){ }
  212. void _push_varargs(PyObject* _0){ PUSH(_0); }
  213. void _push_varargs(PyObject* _0, PyObject* _1){ PUSH(_0); PUSH(_1); }
  214. void _push_varargs(PyObject* _0, PyObject* _1, PyObject* _2){ PUSH(_0); PUSH(_1); PUSH(_2); }
  215. void _push_varargs(PyObject* _0, PyObject* _1, PyObject* _2, PyObject* _3){ PUSH(_0); PUSH(_1); PUSH(_2); PUSH(_3); }
  216. template<typename... Args>
  217. PyObject* call(PyObject* callable, Args&&... args){
  218. PUSH(callable);
  219. PUSH(PY_NULL);
  220. _push_varargs(args...);
  221. return vectorcall(sizeof...(args));
  222. }
  223. template<typename... Args>
  224. PyObject* call_method(PyObject* self, PyObject* callable, Args&&... args){
  225. PUSH(callable);
  226. PUSH(self);
  227. _push_varargs(args...);
  228. return vectorcall(sizeof...(args));
  229. }
  230. template<typename... Args>
  231. PyObject* call_method(PyObject* self, StrName name, Args&&... args){
  232. PyObject* callable = get_unbound_method(self, name, &self);
  233. return call_method(self, callable, args...);
  234. }
  235. PyObject* property(NativeFuncC fget, NativeFuncC fset=nullptr){
  236. PyObject* p = builtins->attr("property");
  237. PyObject* _0 = heap.gcnew(tp_native_func, NativeFunc(fget, 1, false));
  238. PyObject* _1 = vm->None;
  239. if(fset != nullptr) _1 = heap.gcnew(tp_native_func, NativeFunc(fset, 2, false));
  240. return call(p, _0, _1);
  241. }
  242. PyObject* new_type_object(PyObject* mod, StrName name, Type base, bool subclass_enabled=true){
  243. PyObject* obj = heap._new<Type>(tp_type, _all_types.size());
  244. const PyTypeInfo& base_info = _all_types[base];
  245. if(!base_info.subclass_enabled){
  246. TypeError(fmt("type ", base_info.name.escape(), " is not `subclass_enabled`"));
  247. }
  248. PyTypeInfo info{
  249. obj,
  250. base,
  251. (mod!=nullptr && mod!=builtins) ? Str(OBJ_NAME(mod)+"."+name.sv()): name.sv(),
  252. subclass_enabled,
  253. };
  254. if(mod != nullptr) mod->attr().set(name, obj);
  255. _all_types.push_back(info);
  256. return obj;
  257. }
  258. Type _new_type_object(StrName name, Type base=0) {
  259. PyObject* obj = new_type_object(nullptr, name, base, false);
  260. return OBJ_GET(Type, obj);
  261. }
  262. PyObject* _find_type(const Str& type){
  263. PyObject* obj = builtins->attr().try_get(type);
  264. if(obj == nullptr){
  265. for(auto& t: _all_types) if(t.name == type) return t.obj;
  266. throw std::runtime_error(fmt("type not found: ", type));
  267. }
  268. return obj;
  269. }
  270. PyTypeInfo* _type_info(const Str& type){
  271. PyObject* obj = builtins->attr().try_get(type);
  272. if(obj == nullptr){
  273. for(auto& t: _all_types) if(t.name == type) return &t;
  274. FATAL_ERROR();
  275. }
  276. return &_all_types[OBJ_GET(Type, obj)];
  277. }
  278. const PyTypeInfo* _inst_type_info(PyObject* obj){
  279. if(is_int(obj)) return &_all_types[tp_int];
  280. if(is_float(obj)) return &_all_types[tp_float];
  281. return &_all_types[obj->type];
  282. }
  283. #define BIND_LOGICAL_SPECIAL(name) \
  284. void bind##name(Type type, bool (*f)(VM* vm, PyObject* lhs, PyObject* rhs)){ \
  285. PyObject* obj = _t(type); \
  286. _all_types[type].m##name = f; \
  287. bind_method<1>(obj, #name, [](VM* vm, ArgsView args){ \
  288. bool ok = vm->_inst_type_info(args[0])->m##name(vm, args[0], args[1]); \
  289. return ok ? vm->True : vm->False; \
  290. }); \
  291. }
  292. BIND_LOGICAL_SPECIAL(__eq__)
  293. BIND_LOGICAL_SPECIAL(__ne__)
  294. BIND_LOGICAL_SPECIAL(__lt__)
  295. BIND_LOGICAL_SPECIAL(__le__)
  296. BIND_LOGICAL_SPECIAL(__gt__)
  297. BIND_LOGICAL_SPECIAL(__ge__)
  298. BIND_LOGICAL_SPECIAL(__contains__)
  299. #undef BIND_LOGICAL_SPECIAL
  300. #define BIND_BINARY_SPECIAL(name) \
  301. void bind##name(Type type, PyObject* (*f)(VM* vm, PyObject* lhs, PyObject* rhs)){ \
  302. PyObject* obj = _t(type); \
  303. _all_types[type].m##name = f; \
  304. bind_method<1>(obj, #name, [](VM* vm, ArgsView args){ \
  305. return vm->_inst_type_info(args[0])->m##name(vm, args[0], args[1]); \
  306. }); \
  307. }
  308. BIND_BINARY_SPECIAL(__add__)
  309. BIND_BINARY_SPECIAL(__sub__)
  310. BIND_BINARY_SPECIAL(__mul__)
  311. BIND_BINARY_SPECIAL(__truediv__)
  312. bool py_equals(PyObject* lhs, PyObject* rhs){
  313. if(lhs == rhs) return true;
  314. const PyTypeInfo* ti = _inst_type_info(lhs);
  315. if(ti->m__eq__) return ti->m__eq__(this, lhs, rhs);
  316. return call_method(lhs, __eq__, rhs) == True;
  317. }
  318. bool py_not_equals(PyObject* lhs, PyObject* rhs){
  319. if(lhs == rhs) return false;
  320. const PyTypeInfo* ti = _inst_type_info(lhs);
  321. if(ti->m__ne__) return ti->m__ne__(this, lhs, rhs);
  322. return call_method(lhs, __ne__, rhs) == True;
  323. }
  324. template<int ARGC>
  325. void bind_func(Str type, Str name, NativeFuncC fn) {
  326. bind_func<ARGC>(_find_type(type), name, fn);
  327. }
  328. template<int ARGC>
  329. void bind_method(Str type, Str name, NativeFuncC fn) {
  330. bind_method<ARGC>(_find_type(type), name, fn);
  331. }
  332. template<int ARGC, typename __T>
  333. void bind_constructor(__T&& type, NativeFuncC fn) {
  334. static_assert(ARGC==-1 || ARGC>=1);
  335. bind_func<ARGC>(std::forward<__T>(type), "__new__", fn);
  336. }
  337. template<typename T, typename __T>
  338. void bind_default_constructor(__T&& type) {
  339. bind_constructor<-1>(std::forward<__T>(type), [](VM* vm, ArgsView args){
  340. Type t = OBJ_GET(Type, args[0]);
  341. return vm->heap.gcnew<T>(t, T());
  342. });
  343. }
  344. template<int ARGC>
  345. void bind_builtin_func(Str name, NativeFuncC fn) {
  346. bind_func<ARGC>(builtins, name, fn);
  347. }
  348. int normalized_index(int index, int size){
  349. if(index < 0) index += size;
  350. if(index < 0 || index >= size){
  351. IndexError(std::to_string(index) + " not in [0, " + std::to_string(size) + ")");
  352. }
  353. return index;
  354. }
  355. template<typename P>
  356. PyObject* PyIter(P&& value) {
  357. static_assert(std::is_base_of_v<BaseIter, std::decay_t<P>>);
  358. return heap.gcnew<P>(tp_iterator, std::forward<P>(value));
  359. }
  360. PyObject* PyIterNext(PyObject* obj){
  361. if(is_non_tagged_type(obj, tp_iterator)){
  362. BaseIter* iter = static_cast<BaseIter*>(obj->value());
  363. return iter->next();
  364. }
  365. return call_method(obj, __next__);
  366. }
  367. /***** Error Reporter *****/
  368. void _error(StrName name, const Str& msg){
  369. _error(Exception(name, msg));
  370. }
  371. void _raise(){
  372. bool ok = top_frame()->jump_to_exception_handler();
  373. if(ok) throw HandledException();
  374. else throw UnhandledException();
  375. }
  376. void StackOverflowError() { _error("StackOverflowError", ""); }
  377. void IOError(const Str& msg) { _error("IOError", msg); }
  378. void NotImplementedError(){ _error("NotImplementedError", ""); }
  379. void TypeError(const Str& msg){ _error("TypeError", msg); }
  380. void ZeroDivisionError(){ _error("ZeroDivisionError", "division by zero"); }
  381. void IndexError(const Str& msg){ _error("IndexError", msg); }
  382. void ValueError(const Str& msg){ _error("ValueError", msg); }
  383. void NameError(StrName name){ _error("NameError", fmt("name ", name.escape() + " is not defined")); }
  384. void KeyError(const Str& msg){ _error("KeyError", msg); }
  385. void AttributeError(PyObject* obj, StrName name){
  386. // OBJ_NAME calls getattr, which may lead to a infinite recursion
  387. _error("AttributeError", fmt("type ", OBJ_NAME(_t(obj)).escape(), " has no attribute ", name.escape()));
  388. }
  389. void AttributeError(Str msg){ _error("AttributeError", msg); }
  390. void check_type(PyObject* obj, Type type){
  391. if(is_type(obj, type)) return;
  392. TypeError("expected " + OBJ_NAME(_t(type)).escape() + ", but got " + OBJ_NAME(_t(obj)).escape());
  393. }
  394. void check_non_tagged_type(PyObject* obj, Type type){
  395. if(is_non_tagged_type(obj, type)) return;
  396. TypeError("expected " + OBJ_NAME(_t(type)).escape() + ", but got " + OBJ_NAME(_t(obj)).escape());
  397. }
  398. void check_int(PyObject* obj){
  399. if(is_int(obj)) return;
  400. check_type(obj, tp_int); // if failed, redirect to check_type to raise TypeError
  401. }
  402. void check_float(PyObject* obj){
  403. if(is_float(obj)) return;
  404. check_type(obj, tp_float); // if failed, redirect to check_type to raise TypeError
  405. }
  406. PyObject* _t(Type t){
  407. return _all_types[t.index].obj;
  408. }
  409. PyObject* _t(PyObject* obj){
  410. if(is_int(obj)) return _t(tp_int);
  411. if(is_float(obj)) return _t(tp_float);
  412. return _all_types[obj->type].obj;
  413. }
  414. ~VM() {
  415. callstack.clear();
  416. s_data.clear();
  417. _all_types.clear();
  418. _modules.clear();
  419. _lazy_modules.clear();
  420. }
  421. void _log_s_data(const char* title = nullptr);
  422. PyObject* vectorcall(int ARGC, int KWARGC=0, bool op_call=false);
  423. CodeObject_ compile(Str source, Str filename, CompileMode mode, bool unknown_global_scope=false);
  424. PyObject* num_negated(PyObject* obj);
  425. f64 num_to_float(PyObject* obj);
  426. bool asBool(PyObject* obj);
  427. i64 hash(PyObject* obj);
  428. PyObject* asRepr(PyObject*);
  429. PyObject* asList(PyObject*);
  430. PyObject* new_module(StrName name);
  431. Str disassemble(CodeObject_ co);
  432. void init_builtin_types();
  433. PyObject* _py_call(PyObject** sp_base, PyObject* callable, ArgsView args, ArgsView kwargs);
  434. PyObject* getattr(PyObject* obj, StrName name, bool throw_err=true);
  435. PyObject* get_unbound_method(PyObject* obj, StrName name, PyObject** self, bool throw_err=true, bool fallback=false);
  436. void parse_int_slice(const Slice& s, int length, int& start, int& stop, int& step);
  437. PyObject* format(Str, PyObject*);
  438. void setattr(PyObject* obj, StrName name, PyObject* value);
  439. template<int ARGC>
  440. void bind_method(PyObject*, Str, NativeFuncC);
  441. template<int ARGC>
  442. void bind_func(PyObject*, Str, NativeFuncC);
  443. void _error(Exception);
  444. PyObject* _run_top_frame();
  445. void post_init();
  446. };
  447. inline PyObject* NativeFunc::operator()(VM* vm, ArgsView args) const{
  448. int args_size = args.size() - (int)method; // remove self
  449. if(argc != -1 && args_size != argc) {
  450. vm->TypeError(fmt("expected ", argc, " arguments, but got ", args_size));
  451. }
  452. return f(vm, args);
  453. }
  454. inline void CodeObject::optimize(VM* vm){
  455. // uint32_t base_n = (uint32_t)(names.size() / kLocalsLoadFactor + 0.5);
  456. // perfect_locals_capacity = std::max(find_next_capacity(base_n), NameDict::__Capacity);
  457. // perfect_hash_seed = find_perfect_hash_seed(perfect_locals_capacity, names);
  458. }
  459. DEF_NATIVE_2(Str, tp_str)
  460. DEF_NATIVE_2(List, tp_list)
  461. DEF_NATIVE_2(Tuple, tp_tuple)
  462. DEF_NATIVE_2(Function, tp_function)
  463. DEF_NATIVE_2(NativeFunc, tp_native_func)
  464. DEF_NATIVE_2(BoundMethod, tp_bound_method)
  465. DEF_NATIVE_2(Range, tp_range)
  466. DEF_NATIVE_2(Slice, tp_slice)
  467. DEF_NATIVE_2(Exception, tp_exception)
  468. DEF_NATIVE_2(Bytes, tp_bytes)
  469. DEF_NATIVE_2(MappingProxy, tp_mappingproxy)
  470. #define PY_CAST_INT(T) \
  471. template<> inline T py_cast<T>(VM* vm, PyObject* obj){ \
  472. vm->check_int(obj); \
  473. return (T)(BITS(obj) >> 2); \
  474. } \
  475. template<> inline T _py_cast<T>(VM* vm, PyObject* obj){ \
  476. return (T)(BITS(obj) >> 2); \
  477. }
  478. PY_CAST_INT(char)
  479. PY_CAST_INT(short)
  480. PY_CAST_INT(int)
  481. PY_CAST_INT(long)
  482. PY_CAST_INT(long long)
  483. PY_CAST_INT(unsigned char)
  484. PY_CAST_INT(unsigned short)
  485. PY_CAST_INT(unsigned int)
  486. PY_CAST_INT(unsigned long)
  487. PY_CAST_INT(unsigned long long)
  488. template<> inline float py_cast<float>(VM* vm, PyObject* obj){
  489. vm->check_float(obj);
  490. i64 bits = BITS(obj);
  491. bits = (bits >> 2) << 2;
  492. return BitsCvt(bits)._float;
  493. }
  494. template<> inline float _py_cast<float>(VM* vm, PyObject* obj){
  495. i64 bits = BITS(obj);
  496. bits = (bits >> 2) << 2;
  497. return BitsCvt(bits)._float;
  498. }
  499. template<> inline double py_cast<double>(VM* vm, PyObject* obj){
  500. vm->check_float(obj);
  501. i64 bits = BITS(obj);
  502. bits = (bits >> 2) << 2;
  503. return BitsCvt(bits)._float;
  504. }
  505. template<> inline double _py_cast<double>(VM* vm, PyObject* obj){
  506. i64 bits = BITS(obj);
  507. bits = (bits >> 2) << 2;
  508. return BitsCvt(bits)._float;
  509. }
  510. #define PY_VAR_INT(T) \
  511. inline PyObject* py_var(VM* vm, T _val){ \
  512. i64 val = static_cast<i64>(_val); \
  513. if(((val << 2) >> 2) != val){ \
  514. vm->_error("OverflowError", std::to_string(val) + " is out of range"); \
  515. } \
  516. val = (val << 2) | 0b01; \
  517. return reinterpret_cast<PyObject*>(val); \
  518. }
  519. PY_VAR_INT(char)
  520. PY_VAR_INT(short)
  521. PY_VAR_INT(int)
  522. PY_VAR_INT(long)
  523. PY_VAR_INT(long long)
  524. PY_VAR_INT(unsigned char)
  525. PY_VAR_INT(unsigned short)
  526. PY_VAR_INT(unsigned int)
  527. PY_VAR_INT(unsigned long)
  528. PY_VAR_INT(unsigned long long)
  529. #define PY_VAR_FLOAT(T) \
  530. inline PyObject* py_var(VM* vm, T _val){ \
  531. f64 val = static_cast<f64>(_val); \
  532. i64 bits = BitsCvt(val)._int; \
  533. bits = (bits >> 2) << 2; \
  534. bits |= 0b10; \
  535. return reinterpret_cast<PyObject*>(bits); \
  536. }
  537. PY_VAR_FLOAT(float)
  538. PY_VAR_FLOAT(double)
  539. inline PyObject* py_var(VM* vm, bool val){
  540. return val ? vm->True : vm->False;
  541. }
  542. template<> inline bool py_cast<bool>(VM* vm, PyObject* obj){
  543. vm->check_non_tagged_type(obj, vm->tp_bool);
  544. return obj == vm->True;
  545. }
  546. template<> inline bool _py_cast<bool>(VM* vm, PyObject* obj){
  547. return obj == vm->True;
  548. }
  549. inline PyObject* py_var(VM* vm, const char val[]){
  550. return VAR(Str(val));
  551. }
  552. inline PyObject* py_var(VM* vm, std::string val){
  553. return VAR(Str(std::move(val)));
  554. }
  555. inline PyObject* py_var(VM* vm, std::string_view val){
  556. return VAR(Str(val));
  557. }
  558. inline PyObject* py_var(VM* vm, NoReturn val){
  559. return vm->None;
  560. }
  561. inline PyObject* VM::num_negated(PyObject* obj){
  562. if (is_int(obj)){
  563. return VAR(-CAST(i64, obj));
  564. }else if(is_float(obj)){
  565. return VAR(-CAST(f64, obj));
  566. }
  567. TypeError("expected 'int' or 'float', got " + OBJ_NAME(_t(obj)).escape());
  568. return nullptr;
  569. }
  570. inline f64 VM::num_to_float(PyObject* obj){
  571. if(is_float(obj)){
  572. return _CAST(f64, obj);
  573. } else if (is_int(obj)){
  574. return (f64)_CAST(i64, obj);
  575. }
  576. TypeError("expected 'int' or 'float', got " + OBJ_NAME(_t(obj)).escape());
  577. return 0;
  578. }
  579. inline bool VM::asBool(PyObject* obj){
  580. if(is_non_tagged_type(obj, tp_bool)) return obj == True;
  581. if(obj == None) return false;
  582. if(is_int(obj)) return _CAST(i64, obj) != 0;
  583. if(is_float(obj)) return _CAST(f64, obj) != 0.0;
  584. PyObject* self;
  585. PyObject* len_f = get_unbound_method(obj, __len__, &self, false);
  586. if(self != PY_NULL){
  587. PyObject* ret = call_method(self, len_f);
  588. return CAST(i64, ret) > 0;
  589. }
  590. return true;
  591. }
  592. inline PyObject* VM::asList(PyObject* it){
  593. auto _lock = heap.gc_scope_lock();
  594. it = asIter(it);
  595. List list;
  596. PyObject* obj = PyIterNext(it);
  597. while(obj != StopIteration){
  598. list.push_back(obj);
  599. obj = PyIterNext(it);
  600. }
  601. return VAR(std::move(list));
  602. }
  603. inline void VM::parse_int_slice(const Slice& s, int length, int& start, int& stop, int& step){
  604. auto clip = [](int value, int min, int max){
  605. if(value < min) return min;
  606. if(value > max) return max;
  607. return value;
  608. };
  609. if(s.step == None) step = 1;
  610. else step = CAST(int, s.step);
  611. if(step == 0) ValueError("slice step cannot be zero");
  612. if(step > 0){
  613. if(s.start == None){
  614. start = 0;
  615. }else{
  616. start = CAST(int, s.start);
  617. if(start < 0) start += length;
  618. start = clip(start, 0, length);
  619. }
  620. if(s.stop == None){
  621. stop = length;
  622. }else{
  623. stop = CAST(int, s.stop);
  624. if(stop < 0) stop += length;
  625. stop = clip(stop, 0, length);
  626. }
  627. }else{
  628. if(s.start == None){
  629. start = length - 1;
  630. }else{
  631. start = CAST(int, s.start);
  632. if(start < 0) start += length;
  633. start = clip(start, -1, length - 1);
  634. }
  635. if(s.stop == None){
  636. stop = -1;
  637. }else{
  638. stop = CAST(int, s.stop);
  639. if(stop < 0) stop += length;
  640. stop = clip(stop, -1, length - 1);
  641. }
  642. }
  643. }
  644. inline i64 VM::hash(PyObject* obj){
  645. if (is_non_tagged_type(obj, tp_str)) return CAST(Str&, obj).hash();
  646. if (is_int(obj)) return CAST(i64, obj);
  647. if (is_non_tagged_type(obj, tp_tuple)) {
  648. i64 x = 1000003;
  649. const Tuple& items = CAST(Tuple&, obj);
  650. for (int i=0; i<items.size(); i++) {
  651. i64 y = hash(items[i]);
  652. // recommended by Github Copilot
  653. x = x ^ (y + 0x9e3779b9 + (x << 6) + (x >> 2));
  654. }
  655. return x;
  656. }
  657. if (is_non_tagged_type(obj, tp_type)) return BITS(obj);
  658. if (is_non_tagged_type(obj, tp_iterator)) return BITS(obj);
  659. if (is_non_tagged_type(obj, tp_bool)) return _CAST(bool, obj) ? 1 : 0;
  660. if (is_float(obj)){
  661. f64 val = CAST(f64, obj);
  662. return (i64)std::hash<f64>()(val);
  663. }
  664. TypeError("unhashable type: " + OBJ_NAME(_t(obj)).escape());
  665. return 0;
  666. }
  667. inline PyObject* VM::asRepr(PyObject* obj){
  668. return call_method(obj, __repr__);
  669. }
  670. inline PyObject* VM::format(Str spec, PyObject* obj){
  671. if(spec.empty()) return asStr(obj);
  672. char type;
  673. switch(spec.end()[-1]){
  674. case 'f': case 'd': case 's':
  675. type = spec.end()[-1];
  676. spec = spec.substr(0, spec.length() - 1);
  677. break;
  678. default: type = ' '; break;
  679. }
  680. char pad_c = ' ';
  681. if(spec[0] == '0'){
  682. pad_c = '0';
  683. spec = spec.substr(1);
  684. }
  685. char align;
  686. if(spec[0] == '>'){
  687. align = '>';
  688. spec = spec.substr(1);
  689. }else if(spec[0] == '<'){
  690. align = '<';
  691. spec = spec.substr(1);
  692. }else{
  693. if(is_int(obj) || is_float(obj)) align = '>';
  694. else align = '<';
  695. }
  696. int dot = spec.index(".");
  697. int width, precision;
  698. try{
  699. if(dot >= 0){
  700. width = Number::stoi(spec.substr(0, dot).str());
  701. precision = Number::stoi(spec.substr(dot+1).str());
  702. }else{
  703. width = Number::stoi(spec.str());
  704. precision = -1;
  705. }
  706. }catch(...){
  707. ValueError("invalid format specifer");
  708. }
  709. if(type != 'f' && dot >= 0) ValueError("precision not allowed in the format specifier");
  710. Str ret;
  711. if(type == 'f'){
  712. f64 val = num_to_float(obj);
  713. if(precision < 0) precision = 6;
  714. std::stringstream ss;
  715. ss << std::fixed << std::setprecision(precision) << val;
  716. ret = ss.str();
  717. }else if(type == 'd'){
  718. ret = std::to_string(CAST(i64, obj));
  719. }else if(type == 's'){
  720. ret = CAST(Str&, obj);
  721. }else{
  722. ret = CAST(Str&, asStr(obj));
  723. }
  724. if(width > ret.length()){
  725. int pad = width - ret.length();
  726. std::string padding(pad, pad_c);
  727. if(align == '>') ret = padding.c_str() + ret;
  728. else ret = ret + padding.c_str();
  729. }
  730. return VAR(ret);
  731. }
  732. inline PyObject* VM::new_module(StrName name) {
  733. PyObject* obj = heap._new<DummyModule>(tp_module, DummyModule());
  734. obj->attr().set(__name__, VAR(name.sv()));
  735. // we do not allow override in order to avoid memory leak
  736. // it is because Module objects are not garbage collected
  737. if(_modules.contains(name)) FATAL_ERROR();
  738. _modules.set(name, obj);
  739. return obj;
  740. }
  741. inline std::string _opcode_argstr(VM* vm, Bytecode byte, const CodeObject* co){
  742. std::string argStr = byte.arg == -1 ? "" : std::to_string(byte.arg);
  743. switch(byte.op){
  744. case OP_LOAD_CONST:
  745. if(vm != nullptr){
  746. argStr += fmt(" (", CAST(Str, vm->asRepr(co->consts[byte.arg])), ")");
  747. }
  748. break;
  749. case OP_LOAD_NAME: case OP_LOAD_GLOBAL: case OP_LOAD_NONLOCAL: case OP_STORE_GLOBAL:
  750. case OP_LOAD_ATTR: case OP_LOAD_METHOD: case OP_STORE_ATTR: case OP_DELETE_ATTR:
  751. case OP_IMPORT_NAME: case OP_BEGIN_CLASS:
  752. case OP_DELETE_GLOBAL:
  753. argStr += fmt(" (", StrName(byte.arg).sv(), ")");
  754. break;
  755. case OP_LOAD_FAST: case OP_STORE_FAST: case OP_DELETE_FAST:
  756. argStr += fmt(" (", co->varnames[byte.arg].sv(), ")");
  757. break;
  758. case OP_LOAD_FUNCTION:
  759. argStr += fmt(" (", co->func_decls[byte.arg]->code->name, ")");
  760. break;
  761. }
  762. return argStr;
  763. }
  764. inline Str VM::disassemble(CodeObject_ co){
  765. auto pad = [](const Str& s, const int n){
  766. if(s.length() >= n) return s.substr(0, n);
  767. return s + std::string(n - s.length(), ' ');
  768. };
  769. std::vector<int> jumpTargets;
  770. for(auto byte : co->codes){
  771. if(byte.op == OP_JUMP_ABSOLUTE || byte.op == OP_POP_JUMP_IF_FALSE){
  772. jumpTargets.push_back(byte.arg);
  773. }
  774. }
  775. std::stringstream ss;
  776. int prev_line = -1;
  777. for(int i=0; i<co->codes.size(); i++){
  778. const Bytecode& byte = co->codes[i];
  779. Str line = std::to_string(co->lines[i]);
  780. if(co->lines[i] == prev_line) line = "";
  781. else{
  782. if(prev_line != -1) ss << "\n";
  783. prev_line = co->lines[i];
  784. }
  785. std::string pointer;
  786. if(std::find(jumpTargets.begin(), jumpTargets.end(), i) != jumpTargets.end()){
  787. pointer = "-> ";
  788. }else{
  789. pointer = " ";
  790. }
  791. ss << pad(line, 8) << pointer << pad(std::to_string(i), 3);
  792. ss << " " << pad(OP_NAMES[byte.op], 20) << " ";
  793. // ss << pad(byte.arg == -1 ? "" : std::to_string(byte.arg), 5);
  794. std::string argStr = _opcode_argstr(this, byte, co.get());
  795. ss << pad(argStr, 40); // may overflow
  796. ss << co->blocks[byte.block].type;
  797. if(i != co->codes.size() - 1) ss << '\n';
  798. }
  799. for(auto& decl: co->func_decls){
  800. ss << "\n\n" << "Disassembly of " << decl->code->name << ":\n";
  801. ss << disassemble(decl->code);
  802. }
  803. ss << "\n";
  804. return Str(ss.str());
  805. }
  806. inline void VM::_log_s_data(const char* title) {
  807. if(_main == nullptr) return;
  808. if(callstack.empty()) return;
  809. std::stringstream ss;
  810. if(title) ss << title << " | ";
  811. std::map<PyObject**, int> sp_bases;
  812. for(Frame& f: callstack.data()){
  813. if(f._sp_base == nullptr) FATAL_ERROR();
  814. sp_bases[f._sp_base] += 1;
  815. }
  816. FrameId frame = top_frame();
  817. int line = frame->co->lines[frame->_ip];
  818. ss << frame->co->name << ":" << line << " [";
  819. for(PyObject** p=s_data.begin(); p!=s_data.end(); p++){
  820. ss << std::string(sp_bases[p], '|');
  821. if(sp_bases[p] > 0) ss << " ";
  822. PyObject* obj = *p;
  823. if(obj == nullptr) ss << "(nil)";
  824. else if(obj == PY_BEGIN_CALL) ss << "BEGIN_CALL";
  825. else if(obj == PY_NULL) ss << "NULL";
  826. else if(is_int(obj)) ss << CAST(i64, obj);
  827. else if(is_float(obj)) ss << CAST(f64, obj);
  828. else if(is_type(obj, tp_str)) ss << CAST(Str, obj).escape();
  829. else if(obj == None) ss << "None";
  830. else if(obj == True) ss << "True";
  831. else if(obj == False) ss << "False";
  832. else if(is_type(obj, tp_function)){
  833. auto& f = CAST(Function&, obj);
  834. ss << f.decl->code->name << "(...)";
  835. } else if(is_type(obj, tp_type)){
  836. Type t = OBJ_GET(Type, obj);
  837. ss << "<class " + _all_types[t].name.escape() + ">";
  838. } else if(is_type(obj, tp_list)){
  839. auto& t = CAST(List&, obj);
  840. ss << "list(size=" << t.size() << ")";
  841. } else if(is_type(obj, tp_tuple)){
  842. auto& t = CAST(Tuple&, obj);
  843. ss << "tuple(size=" << t.size() << ")";
  844. } else ss << "(" << obj_type_name(this, obj->type) << ")";
  845. ss << ", ";
  846. }
  847. std::string output = ss.str();
  848. if(!s_data.empty()) {
  849. output.pop_back(); output.pop_back();
  850. }
  851. output.push_back(']');
  852. Bytecode byte = frame->co->codes[frame->_ip];
  853. std::cout << output << " " << OP_NAMES[byte.op] << " " << _opcode_argstr(nullptr, byte, frame->co) << std::endl;
  854. }
  855. inline void VM::init_builtin_types(){
  856. _all_types.push_back({heap._new<Type>(Type(1), Type(0)), -1, "object", true});
  857. _all_types.push_back({heap._new<Type>(Type(1), Type(1)), 0, "type", false});
  858. tp_object = 0; tp_type = 1;
  859. tp_int = _new_type_object("int");
  860. tp_float = _new_type_object("float");
  861. if(tp_int.index != kTpIntIndex || tp_float.index != kTpFloatIndex) FATAL_ERROR();
  862. tp_bool = _new_type_object("bool");
  863. tp_str = _new_type_object("str");
  864. tp_list = _new_type_object("list");
  865. tp_tuple = _new_type_object("tuple");
  866. tp_slice = _new_type_object("slice");
  867. tp_range = _new_type_object("range");
  868. tp_module = _new_type_object("module");
  869. tp_function = _new_type_object("function");
  870. tp_native_func = _new_type_object("native_func");
  871. tp_iterator = _new_type_object("iterator");
  872. tp_bound_method = _new_type_object("bound_method");
  873. tp_super = _new_type_object("super");
  874. tp_exception = _new_type_object("Exception");
  875. tp_bytes = _new_type_object("bytes");
  876. tp_mappingproxy = _new_type_object("mappingproxy");
  877. this->None = heap._new<Dummy>(_new_type_object("NoneType"), {});
  878. this->Ellipsis = heap._new<Dummy>(_new_type_object("ellipsis"), {});
  879. this->True = heap._new<Dummy>(tp_bool, {});
  880. this->False = heap._new<Dummy>(tp_bool, {});
  881. this->StopIteration = heap._new<Dummy>(_new_type_object("StopIterationType"), {});
  882. this->builtins = new_module("builtins");
  883. // setup public types
  884. builtins->attr().set("type", _t(tp_type));
  885. builtins->attr().set("object", _t(tp_object));
  886. builtins->attr().set("bool", _t(tp_bool));
  887. builtins->attr().set("int", _t(tp_int));
  888. builtins->attr().set("float", _t(tp_float));
  889. builtins->attr().set("str", _t(tp_str));
  890. builtins->attr().set("list", _t(tp_list));
  891. builtins->attr().set("tuple", _t(tp_tuple));
  892. builtins->attr().set("range", _t(tp_range));
  893. builtins->attr().set("bytes", _t(tp_bytes));
  894. builtins->attr().set("StopIteration", StopIteration);
  895. builtins->attr().set("slice", _t(tp_slice));
  896. post_init();
  897. for(int i=0; i<_all_types.size(); i++){
  898. _all_types[i].obj->attr()._try_perfect_rehash();
  899. }
  900. for(auto [k, v]: _modules.items()) v->attr()._try_perfect_rehash();
  901. this->_main = new_module("__main__");
  902. }
  903. inline PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){
  904. bool is_varargs = ARGC == 0xFFFF;
  905. PyObject** p0;
  906. PyObject** p1 = s_data._sp - KWARGC*2;
  907. if(is_varargs){
  908. p0 = p1 - 1;
  909. while(*p0 != PY_BEGIN_CALL) p0--;
  910. // [BEGIN_CALL, callable, <self>, args..., kwargs...]
  911. // ^p0 ^p1 ^_sp
  912. ARGC = p1 - (p0 + 3);
  913. }else{
  914. p0 = p1 - ARGC - 2 - (int)is_varargs;
  915. // [callable, <self>, args..., kwargs...]
  916. // ^p0 ^p1 ^_sp
  917. }
  918. PyObject* callable = p1[-(ARGC + 2)];
  919. bool method_call = p1[-(ARGC + 1)] != PY_NULL;
  920. // handle boundmethod, do a patch
  921. if(is_non_tagged_type(callable, tp_bound_method)){
  922. if(method_call) FATAL_ERROR();
  923. auto& bm = CAST(BoundMethod&, callable);
  924. callable = bm.func; // get unbound method
  925. p1[-(ARGC + 2)] = bm.func;
  926. p1[-(ARGC + 1)] = bm.self;
  927. method_call = true;
  928. // [unbound, self, args..., kwargs...]
  929. }
  930. ArgsView args(p1 - ARGC - int(method_call), p1);
  931. if(is_non_tagged_type(callable, tp_native_func)){
  932. const auto& f = OBJ_GET(NativeFunc, callable);
  933. if(KWARGC != 0) TypeError("native_func does not accept keyword arguments");
  934. PyObject* ret = f(this, args);
  935. s_data.reset(p0);
  936. return ret;
  937. }
  938. ArgsView kwargs(p1, s_data._sp);
  939. if(is_non_tagged_type(callable, tp_function)){
  940. // ret is nullptr or a generator
  941. PyObject* ret = _py_call(p0, callable, args, kwargs);
  942. // stack resetting is handled by _py_call
  943. if(ret != nullptr) return ret;
  944. if(op_call) return PY_OP_CALL;
  945. return _run_top_frame();
  946. }
  947. if(is_non_tagged_type(callable, tp_type)){
  948. if(method_call) FATAL_ERROR();
  949. // [type, NULL, args..., kwargs...]
  950. // __new__
  951. const static StrName m_new("__new__");
  952. PyObject* new_f = find_name_in_mro(callable, m_new);
  953. PyObject* obj;
  954. if(new_f != nullptr){
  955. PUSH(new_f);
  956. PUSH(PY_NULL);
  957. PUSH(callable); // cls
  958. for(PyObject* obj: args) PUSH(obj);
  959. for(PyObject* obj: kwargs) PUSH(obj);
  960. // if obj is not an instance of callable, the behavior is undefined
  961. obj = vectorcall(ARGC+1, KWARGC);
  962. }else{
  963. // fast path for object.__new__
  964. Type t = OBJ_GET(Type, callable);
  965. obj= vm->heap.gcnew<DummyInstance>(t, {});
  966. }
  967. // __init__
  968. PyObject* self;
  969. callable = get_unbound_method(obj, __init__, &self, false);
  970. if (self != PY_NULL) {
  971. // replace `NULL` with `self`
  972. p1[-(ARGC + 2)] = callable;
  973. p1[-(ARGC + 1)] = self;
  974. // [init_f, self, args..., kwargs...]
  975. vectorcall(ARGC, KWARGC);
  976. // We just discard the return value of `__init__`
  977. // in cpython it raises a TypeError if the return value is not None
  978. }else{
  979. // manually reset the stack
  980. s_data.reset(p0);
  981. }
  982. return obj;
  983. }
  984. // handle `__call__` overload
  985. PyObject* self;
  986. PyObject* call_f = get_unbound_method(callable, __call__, &self, false);
  987. if(self != PY_NULL){
  988. p1[-(ARGC + 2)] = call_f;
  989. p1[-(ARGC + 1)] = self;
  990. // [call_f, self, args..., kwargs...]
  991. return vectorcall(ARGC, KWARGC, false);
  992. }
  993. TypeError(OBJ_NAME(_t(callable)).escape() + " object is not callable");
  994. return nullptr;
  995. }
  996. inline PyObject* VM::_py_call(PyObject** p0, PyObject* callable, ArgsView args, ArgsView kwargs){
  997. // callable must be a `function` object
  998. if(s_data.is_overflow()) StackOverflowError();
  999. const Function& fn = CAST(Function&, callable);
  1000. const CodeObject* co = fn.decl->code.get();
  1001. int co_nlocals = co->varnames.size();
  1002. if(args.size() < fn.argc){
  1003. vm->TypeError(fmt(
  1004. "expected ",
  1005. fn.argc,
  1006. " positional arguments, but got ",
  1007. args.size(),
  1008. " (", fn.decl->code->name, ')'
  1009. ));
  1010. }
  1011. // if this function is simple, a.k.a, no kwargs and no *args and not a generator
  1012. // we can use a fast path to avoid using buffer copy
  1013. if(fn.is_simple){
  1014. if(args.size() > fn.argc) TypeError("too many positional arguments");
  1015. int spaces = co_nlocals - fn.argc;
  1016. for(int j=0; j<spaces; j++) PUSH(nullptr);
  1017. callstack.emplace(&s_data, p0, co, fn._module, callable, FastLocals(co, args.begin()));
  1018. return nullptr;
  1019. }
  1020. int i = 0;
  1021. static THREAD_LOCAL PyObject* buffer[PK_MAX_CO_VARNAMES];
  1022. // prepare args
  1023. for(int index: fn.decl->args) buffer[index] = args[i++];
  1024. // set extra varnames to nullptr
  1025. for(int j=i; j<co_nlocals; j++) buffer[j] = nullptr;
  1026. // prepare kwdefaults
  1027. for(auto& kv: fn.decl->kwargs) buffer[kv.key] = kv.value;
  1028. // handle *args
  1029. if(fn.decl->starred_arg != -1){
  1030. ArgsView vargs(args.begin() + i, args.end());
  1031. buffer[fn.decl->starred_arg] = VAR(vargs.to_tuple());
  1032. i += vargs.size();
  1033. }else{
  1034. // kwdefaults override
  1035. for(auto& kv: fn.decl->kwargs){
  1036. if(i >= args.size()) break;
  1037. buffer[kv.key] = args[i++];
  1038. }
  1039. if(i < args.size()) TypeError(fmt("too many arguments", " (", fn.decl->code->name, ')'));
  1040. }
  1041. for(int i=0; i<kwargs.size(); i+=2){
  1042. StrName key = CAST(int, kwargs[i]);
  1043. int index = co->varnames_inv.try_get(key);
  1044. if(index<0) TypeError(fmt(key.escape(), " is an invalid keyword argument for ", co->name, "()"));
  1045. buffer[index] = kwargs[i+1];
  1046. }
  1047. if(co->is_generator){
  1048. s_data.reset(p0);
  1049. PyObject* ret = PyIter(Generator(
  1050. this,
  1051. Frame(&s_data, nullptr, co, fn._module, callable),
  1052. ArgsView(buffer, buffer + co_nlocals)
  1053. ));
  1054. return ret;
  1055. }
  1056. // copy buffer back to stack
  1057. s_data.reset(args.begin());
  1058. for(int i=0; i<co_nlocals; i++) PUSH(buffer[i]);
  1059. callstack.emplace(&s_data, p0, co, fn._module, callable, FastLocals(co, args.begin()));
  1060. return nullptr;
  1061. }
  1062. // https://docs.python.org/3/howto/descriptor.html#invocation-from-an-instance
  1063. inline PyObject* VM::getattr(PyObject* obj, StrName name, bool throw_err){
  1064. PyObject* objtype = _t(obj);
  1065. // handle super() proxy
  1066. if(is_non_tagged_type(obj, tp_super)){
  1067. const Super& super = OBJ_GET(Super, obj);
  1068. obj = super.first;
  1069. objtype = _t(super.second);
  1070. }
  1071. PyObject* cls_var = find_name_in_mro(objtype, name);
  1072. if(cls_var != nullptr){
  1073. // handle descriptor
  1074. PyObject* descr_get = _t(cls_var)->attr().try_get(__get__);
  1075. if(descr_get != nullptr) return call_method(cls_var, descr_get, obj);
  1076. }
  1077. // handle instance __dict__
  1078. if(!is_tagged(obj) && obj->is_attr_valid()){
  1079. PyObject* val = obj->attr().try_get(name);
  1080. if(val != nullptr) return val;
  1081. }
  1082. if(cls_var != nullptr){
  1083. // bound method is non-data descriptor
  1084. if(is_non_tagged_type(cls_var, tp_function) || is_non_tagged_type(cls_var, tp_native_func)){
  1085. return VAR(BoundMethod(obj, cls_var));
  1086. }
  1087. return cls_var;
  1088. }
  1089. if(throw_err) AttributeError(obj, name);
  1090. return nullptr;
  1091. }
  1092. // used by OP_LOAD_METHOD
  1093. // try to load a unbound method (fallback to `getattr` if not found)
  1094. inline PyObject* VM::get_unbound_method(PyObject* obj, StrName name, PyObject** self, bool throw_err, bool fallback){
  1095. *self = PY_NULL;
  1096. PyObject* objtype = _t(obj);
  1097. // handle super() proxy
  1098. if(is_non_tagged_type(obj, tp_super)){
  1099. const Super& super = OBJ_GET(Super, obj);
  1100. obj = super.first;
  1101. objtype = _t(super.second);
  1102. }
  1103. PyObject* cls_var = find_name_in_mro(objtype, name);
  1104. if(fallback){
  1105. if(cls_var != nullptr){
  1106. // handle descriptor
  1107. PyObject* descr_get = _t(cls_var)->attr().try_get(__get__);
  1108. if(descr_get != nullptr) return call_method(cls_var, descr_get, obj);
  1109. }
  1110. // handle instance __dict__
  1111. if(!is_tagged(obj) && obj->is_attr_valid()){
  1112. PyObject* val = obj->attr().try_get(name);
  1113. if(val != nullptr) return val;
  1114. }
  1115. }
  1116. if(cls_var != nullptr){
  1117. if(is_non_tagged_type(cls_var, tp_function) || is_non_tagged_type(cls_var, tp_native_func)){
  1118. *self = obj;
  1119. }
  1120. return cls_var;
  1121. }
  1122. if(throw_err) AttributeError(obj, name);
  1123. return nullptr;
  1124. }
  1125. inline void VM::setattr(PyObject* obj, StrName name, PyObject* value){
  1126. PyObject* objtype = _t(obj);
  1127. // handle super() proxy
  1128. if(is_non_tagged_type(obj, tp_super)){
  1129. Super& super = OBJ_GET(Super, obj);
  1130. obj = super.first;
  1131. objtype = _t(super.second);
  1132. }
  1133. PyObject* cls_var = find_name_in_mro(objtype, name);
  1134. if(cls_var != nullptr){
  1135. // handle descriptor
  1136. PyObject* cls_var_t = _t(cls_var);
  1137. if(cls_var_t->attr().contains(__get__)){
  1138. PyObject* descr_set = cls_var_t->attr().try_get(__set__);
  1139. if(descr_set != nullptr){
  1140. call_method(cls_var, descr_set, obj, value);
  1141. }else{
  1142. TypeError(fmt("readonly attribute: ", name.escape()));
  1143. }
  1144. return;
  1145. }
  1146. }
  1147. // handle instance __dict__
  1148. if(is_tagged(obj) || !obj->is_attr_valid()) TypeError("cannot set attribute");
  1149. obj->attr().set(name, value);
  1150. }
  1151. template<int ARGC>
  1152. void VM::bind_method(PyObject* obj, Str name, NativeFuncC fn) {
  1153. check_non_tagged_type(obj, tp_type);
  1154. obj->attr().set(name, VAR(NativeFunc(fn, ARGC, true)));
  1155. }
  1156. template<int ARGC>
  1157. void VM::bind_func(PyObject* obj, Str name, NativeFuncC fn) {
  1158. obj->attr().set(name, VAR(NativeFunc(fn, ARGC, false)));
  1159. }
  1160. inline void VM::_error(Exception e){
  1161. if(callstack.empty()){
  1162. e.is_re = false;
  1163. throw e;
  1164. }
  1165. PUSH(VAR(e));
  1166. _raise();
  1167. }
  1168. inline void ManagedHeap::mark() {
  1169. for(PyObject* obj: _no_gc) OBJ_MARK(obj);
  1170. for(auto& frame : vm->callstack.data()) frame._gc_mark();
  1171. for(PyObject* obj: vm->s_data) if(obj!=nullptr) OBJ_MARK(obj);
  1172. if(vm->_gc_marker_ex != nullptr) vm->_gc_marker_ex(vm);
  1173. }
  1174. inline Str obj_type_name(VM *vm, Type type){
  1175. return vm->_all_types[type].name;
  1176. }
  1177. #undef PY_VAR_INT
  1178. #undef PY_VAR_FLOAT
  1179. /***************************************************/
  1180. template<typename T>
  1181. PyObject* PyArrayGetItem(VM* vm, PyObject* obj, PyObject* index){
  1182. static_assert(std::is_same_v<T, List> || std::is_same_v<T, Tuple>);
  1183. const T& self = _CAST(T&, obj);
  1184. if(is_type(index, vm->tp_slice)){
  1185. const Slice& s = _CAST(Slice&, index);
  1186. int start, stop, step;
  1187. vm->parse_int_slice(s, self.size(), start, stop, step);
  1188. List new_list;
  1189. for(int i=start; step>0?i<stop:i>stop; i+=step) new_list.push_back(self[i]);
  1190. return VAR(T(std::move(new_list)));
  1191. }
  1192. int i = CAST(int, index);
  1193. i = vm->normalized_index(i, self.size());
  1194. return self[i];
  1195. }
  1196. inline PyObject* PyListSetItem(VM* vm, PyObject* obj, PyObject* index, PyObject* value){
  1197. List& self = _CAST(List&, obj);
  1198. int i = CAST(int, index);
  1199. i = vm->normalized_index(i, self.size());
  1200. self[i] = value;
  1201. return vm->None;
  1202. }
  1203. inline PyObject* PyStrGetItem(VM* vm, PyObject* obj, PyObject* index){
  1204. const Str& self = _CAST(Str&, obj);
  1205. if(is_type(index, vm->tp_slice)){
  1206. const Slice& s = _CAST(Slice&, index);
  1207. int start, stop, step;
  1208. vm->parse_int_slice(s, self.u8_length(), start, stop, step);
  1209. return VAR(self.u8_slice(start, stop, step));
  1210. }
  1211. int i = CAST(int, index);
  1212. i = vm->normalized_index(i, self.u8_length());
  1213. return VAR(self.u8_getitem(i));
  1214. }
  1215. inline void Dict::_probe(PyObject *key, bool &ok, int &i) const{
  1216. ok = false;
  1217. i = vm->hash(key) & _mask;
  1218. while(_items[i].first != nullptr) {
  1219. if(vm->py_equals(_items[i].first, key)) { ok = true; break; }
  1220. i = (i + 1) & _mask;
  1221. }
  1222. }
  1223. } // namespace pkpy