vm.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. #include "pocketpy/vm.h"
  2. namespace pkpy{
  3. VM::VM(bool enable_os) : heap(this), enable_os(enable_os) {
  4. this->vm = this;
  5. this->_c.error = nullptr;
  6. _stdout = [](VM* vm, const Str& s) {
  7. PK_UNUSED(vm);
  8. std::cout << s;
  9. };
  10. _stderr = [](VM* vm, const Str& s) {
  11. PK_UNUSED(vm);
  12. std::cerr << s;
  13. };
  14. callstack.reserve(8);
  15. _main = nullptr;
  16. _last_exception = nullptr;
  17. _import_handler = [](const Str& name) {
  18. PK_UNUSED(name);
  19. return Bytes();
  20. };
  21. init_builtin_types();
  22. }
  23. PyObject* VM::py_str(PyObject* obj){
  24. const PyTypeInfo* ti = _inst_type_info(obj);
  25. if(ti->m__str__) return ti->m__str__(this, obj);
  26. PyObject* self;
  27. PyObject* f = get_unbound_method(obj, __str__, &self, false);
  28. if(self != PY_NULL) return call_method(self, f);
  29. return py_repr(obj);
  30. }
  31. PyObject* VM::py_repr(PyObject* obj){
  32. const PyTypeInfo* ti = _inst_type_info(obj);
  33. if(ti->m__repr__) return ti->m__repr__(this, obj);
  34. return call_method(obj, __repr__);
  35. }
  36. PyObject* VM::py_json(PyObject* obj){
  37. const PyTypeInfo* ti = _inst_type_info(obj);
  38. if(ti->m__json__) return ti->m__json__(this, obj);
  39. return call_method(obj, __json__);
  40. }
  41. PyObject* VM::py_iter(PyObject* obj){
  42. const PyTypeInfo* ti = _inst_type_info(obj);
  43. if(ti->m__iter__) return ti->m__iter__(this, obj);
  44. PyObject* self;
  45. PyObject* iter_f = get_unbound_method(obj, __iter__, &self, false);
  46. if(self != PY_NULL) return call_method(self, iter_f);
  47. TypeError(OBJ_NAME(_t(obj)).escape() + " object is not iterable");
  48. return nullptr;
  49. }
  50. FrameId VM::top_frame(){
  51. #if PK_DEBUG_EXTRA_CHECK
  52. if(callstack.empty()) FATAL_ERROR();
  53. #endif
  54. return FrameId(&callstack.data(), callstack.size()-1);
  55. }
  56. void VM::_pop_frame(){
  57. Frame* frame = &callstack.top();
  58. s_data.reset(frame->_sp_base);
  59. callstack.pop();
  60. }
  61. PyObject* VM::find_name_in_mro(PyObject* cls, StrName name){
  62. PyObject* val;
  63. do{
  64. val = cls->attr().try_get(name);
  65. if(val != nullptr) return val;
  66. Type base = _all_types[PK_OBJ_GET(Type, cls)].base;
  67. if(base.index == -1) break;
  68. cls = _all_types[base].obj;
  69. }while(true);
  70. return nullptr;
  71. }
  72. bool VM::isinstance(PyObject* obj, Type cls_t){
  73. Type obj_t = PK_OBJ_GET(Type, _t(obj));
  74. do{
  75. if(obj_t == cls_t) return true;
  76. Type base = _all_types[obj_t].base;
  77. if(base.index == -1) break;
  78. obj_t = base;
  79. }while(true);
  80. return false;
  81. }
  82. PyObject* VM::exec(Str source, Str filename, CompileMode mode, PyObject* _module){
  83. if(_module == nullptr) _module = _main;
  84. try {
  85. CodeObject_ code = compile(source, filename, mode);
  86. #if PK_DEBUG_DIS_EXEC
  87. if(_module == _main) std::cout << disassemble(code) << '\n';
  88. #endif
  89. return _exec(code, _module);
  90. }catch (const Exception& e){
  91. _stderr(this, e.summary() + "\n");
  92. }
  93. #if !PK_DEBUG_FULL_EXCEPTION
  94. catch (const std::exception& e) {
  95. Str msg = "An std::exception occurred! It could be a bug.\n";
  96. msg = msg + e.what();
  97. _stderr(this, msg + "\n");
  98. }
  99. #endif
  100. callstack.clear();
  101. s_data.clear();
  102. return nullptr;
  103. }
  104. PyObject* VM::property(NativeFuncC fget, NativeFuncC fset, const char* type_hint){
  105. PyObject* _0 = heap.gcnew(tp_native_func, NativeFunc(fget, 1, false));
  106. PyObject* _1 = vm->None;
  107. if(fset != nullptr) _1 = heap.gcnew(tp_native_func, NativeFunc(fset, 2, false));
  108. return VAR(Property(_0, _1, type_hint));
  109. }
  110. PyObject* VM::new_type_object(PyObject* mod, StrName name, Type base, bool subclass_enabled){
  111. PyObject* obj = heap._new<Type>(tp_type, _all_types.size());
  112. const PyTypeInfo& base_info = _all_types[base];
  113. if(!base_info.subclass_enabled){
  114. TypeError(fmt("type ", base_info.name.escape(), " is not `subclass_enabled`"));
  115. }
  116. PyTypeInfo info{
  117. obj,
  118. base,
  119. (mod!=nullptr && mod!=builtins) ? Str(OBJ_NAME(mod)+"."+name.sv()): name.sv(),
  120. subclass_enabled,
  121. };
  122. if(mod != nullptr) mod->attr().set(name, obj);
  123. _all_types.push_back(info);
  124. return obj;
  125. }
  126. Type VM::_new_type_object(StrName name, Type base) {
  127. PyObject* obj = new_type_object(nullptr, name, base, false);
  128. return PK_OBJ_GET(Type, obj);
  129. }
  130. PyObject* VM::_find_type_object(const Str& type){
  131. PyObject* obj = builtins->attr().try_get(type);
  132. if(obj == nullptr){
  133. for(auto& t: _all_types) if(t.name == type) return t.obj;
  134. throw std::runtime_error(fmt("type not found: ", type));
  135. }
  136. check_non_tagged_type(obj, tp_type);
  137. return obj;
  138. }
  139. Type VM::_type(const Str& type){
  140. PyObject* obj = _find_type_object(type);
  141. return PK_OBJ_GET(Type, obj);
  142. }
  143. PyTypeInfo* VM::_type_info(const Str& type){
  144. PyObject* obj = builtins->attr().try_get(type);
  145. if(obj == nullptr){
  146. for(auto& t: _all_types) if(t.name == type) return &t;
  147. FATAL_ERROR();
  148. }
  149. return &_all_types[PK_OBJ_GET(Type, obj)];
  150. }
  151. PyTypeInfo* VM::_type_info(Type type){
  152. return &_all_types[type];
  153. }
  154. const PyTypeInfo* VM::_inst_type_info(PyObject* obj){
  155. if(is_int(obj)) return &_all_types[tp_int];
  156. if(is_float(obj)) return &_all_types[tp_float];
  157. return &_all_types[obj->type];
  158. }
  159. bool VM::py_equals(PyObject* lhs, PyObject* rhs){
  160. if(lhs == rhs) return true;
  161. const PyTypeInfo* ti = _inst_type_info(lhs);
  162. PyObject* res;
  163. if(ti->m__eq__){
  164. res = ti->m__eq__(this, lhs, rhs);
  165. if(res != vm->NotImplemented) return res == vm->True;
  166. }
  167. res = call_method(lhs, __eq__, rhs);
  168. if(res != vm->NotImplemented) return res == vm->True;
  169. ti = _inst_type_info(rhs);
  170. if(ti->m__eq__){
  171. res = ti->m__eq__(this, rhs, lhs);
  172. if(res != vm->NotImplemented) return res == vm->True;
  173. }
  174. res = call_method(rhs, __eq__, lhs);
  175. if(res != vm->NotImplemented) return res == vm->True;
  176. return false;
  177. }
  178. int VM::normalized_index(int index, int size){
  179. if(index < 0) index += size;
  180. if(index < 0 || index >= size){
  181. IndexError(std::to_string(index) + " not in [0, " + std::to_string(size) + ")");
  182. }
  183. return index;
  184. }
  185. PyObject* VM::py_next(PyObject* obj){
  186. const PyTypeInfo* ti = _inst_type_info(obj);
  187. if(ti->m__next__) return ti->m__next__(this, obj);
  188. return call_method(obj, __next__);
  189. }
  190. PyObject* VM::py_import(StrName name, bool relative){
  191. Str filename;
  192. int type;
  193. if(relative){
  194. ImportContext* ctx = &_import_context;
  195. type = 2;
  196. for(auto it=ctx->pending.rbegin(); it!=ctx->pending.rend(); ++it){
  197. if(it->second == 2) continue;
  198. if(it->second == 1){
  199. filename = fmt(it->first, kPlatformSep, name, ".py");
  200. name = fmt(it->first, '.', name).c_str();
  201. break;
  202. }
  203. }
  204. if(filename.length() == 0) _error("ImportError", "relative import outside of package");
  205. }else{
  206. type = 0;
  207. filename = fmt(name, ".py");
  208. }
  209. for(auto& [k, v]: _import_context.pending){
  210. if(k == name){
  211. vm->_error("ImportError", fmt("circular import ", name.escape()));
  212. }
  213. }
  214. PyObject* ext_mod = _modules.try_get(name);
  215. if(ext_mod == nullptr){
  216. Str source;
  217. auto it = _lazy_modules.find(name);
  218. if(it == _lazy_modules.end()){
  219. Bytes b = _import_handler(filename);
  220. if(!relative && !b){
  221. filename = fmt(name, kPlatformSep, "__init__.py");
  222. b = _import_handler(filename);
  223. if(b) type = 1;
  224. }
  225. if(!b) _error("ImportError", fmt("module ", name.escape(), " not found"));
  226. source = Str(b.str());
  227. }else{
  228. source = it->second;
  229. _lazy_modules.erase(it);
  230. }
  231. auto _ = _import_context.temp(this, name, type);
  232. CodeObject_ code = compile(source, filename, EXEC_MODE);
  233. PyObject* new_mod = new_module(name);
  234. _exec(code, new_mod);
  235. new_mod->attr()._try_perfect_rehash();
  236. return new_mod;
  237. }else{
  238. return ext_mod;
  239. }
  240. }
  241. VM::~VM() {
  242. callstack.clear();
  243. s_data.clear();
  244. _all_types.clear();
  245. _modules.clear();
  246. _lazy_modules.clear();
  247. }
  248. PyObject* VM::py_negate(PyObject* obj){
  249. const PyTypeInfo* ti = _inst_type_info(obj);
  250. if(ti->m__neg__) return ti->m__neg__(this, obj);
  251. return call_method(obj, __neg__);
  252. }
  253. void VM::check_int_or_float(PyObject *obj){
  254. if(!is_tagged(obj)){
  255. TypeError("expected 'int' or 'float', got " + OBJ_NAME(_t(obj)).escape());
  256. }
  257. }
  258. bool VM::py_bool(PyObject* obj){
  259. if(is_non_tagged_type(obj, tp_bool)) return obj == True;
  260. if(obj == None) return false;
  261. if(is_int(obj)) return _CAST(i64, obj) != 0;
  262. if(is_float(obj)) return _CAST(f64, obj) != 0.0;
  263. PyObject* self;
  264. PyObject* len_f = get_unbound_method(obj, __len__, &self, false);
  265. if(self != PY_NULL){
  266. PyObject* ret = call_method(self, len_f);
  267. return CAST(i64, ret) > 0;
  268. }
  269. return true;
  270. }
  271. PyObject* VM::py_list(PyObject* it){
  272. auto _lock = heap.gc_scope_lock();
  273. it = py_iter(it);
  274. List list;
  275. PyObject* obj = py_next(it);
  276. while(obj != StopIteration){
  277. list.push_back(obj);
  278. obj = py_next(it);
  279. }
  280. return VAR(std::move(list));
  281. }
  282. void VM::parse_int_slice(const Slice& s, int length, int& start, int& stop, int& step){
  283. auto clip = [](int value, int min, int max){
  284. if(value < min) return min;
  285. if(value > max) return max;
  286. return value;
  287. };
  288. if(s.step == None) step = 1;
  289. else step = CAST(int, s.step);
  290. if(step == 0) ValueError("slice step cannot be zero");
  291. if(step > 0){
  292. if(s.start == None){
  293. start = 0;
  294. }else{
  295. start = CAST(int, s.start);
  296. if(start < 0) start += length;
  297. start = clip(start, 0, length);
  298. }
  299. if(s.stop == None){
  300. stop = length;
  301. }else{
  302. stop = CAST(int, s.stop);
  303. if(stop < 0) stop += length;
  304. stop = clip(stop, 0, length);
  305. }
  306. }else{
  307. if(s.start == None){
  308. start = length - 1;
  309. }else{
  310. start = CAST(int, s.start);
  311. if(start < 0) start += length;
  312. start = clip(start, -1, length - 1);
  313. }
  314. if(s.stop == None){
  315. stop = -1;
  316. }else{
  317. stop = CAST(int, s.stop);
  318. if(stop < 0) stop += length;
  319. stop = clip(stop, -1, length - 1);
  320. }
  321. }
  322. }
  323. i64 VM::py_hash(PyObject* obj){
  324. const PyTypeInfo* ti = _inst_type_info(obj);
  325. if(ti->m__hash__) return ti->m__hash__(this, obj);
  326. PyObject* ret = call_method(obj, __hash__);
  327. return CAST(i64, ret);
  328. }
  329. PyObject* VM::format(Str spec, PyObject* obj){
  330. if(spec.empty()) return py_str(obj);
  331. char type;
  332. switch(spec.end()[-1]){
  333. case 'f': case 'd': case 's':
  334. type = spec.end()[-1];
  335. spec = spec.substr(0, spec.length() - 1);
  336. break;
  337. default: type = ' '; break;
  338. }
  339. char pad_c = ' ';
  340. if(spec[0] == '0'){
  341. pad_c = '0';
  342. spec = spec.substr(1);
  343. }
  344. char align;
  345. if(spec[0] == '>'){
  346. align = '>';
  347. spec = spec.substr(1);
  348. }else if(spec[0] == '<'){
  349. align = '<';
  350. spec = spec.substr(1);
  351. }else{
  352. if(is_int(obj) || is_float(obj)) align = '>';
  353. else align = '<';
  354. }
  355. int dot = spec.index(".");
  356. int width, precision;
  357. try{
  358. if(dot >= 0){
  359. if(dot == 0){
  360. width = -1;
  361. }else{
  362. width = Number::stoi(spec.substr(0, dot).str());
  363. }
  364. precision = Number::stoi(spec.substr(dot+1).str());
  365. }else{
  366. width = Number::stoi(spec.str());
  367. precision = -1;
  368. }
  369. }catch(...){
  370. ValueError("invalid format specifer");
  371. UNREACHABLE();
  372. }
  373. if(type != 'f' && dot >= 0) ValueError("precision not allowed in the format specifier");
  374. Str ret;
  375. if(type == 'f'){
  376. f64 val = CAST(f64, obj);
  377. if(precision < 0) precision = 6;
  378. std::stringstream ss;
  379. ss << std::fixed << std::setprecision(precision) << val;
  380. ret = ss.str();
  381. }else if(type == 'd'){
  382. ret = std::to_string(CAST(i64, obj));
  383. }else if(type == 's'){
  384. ret = CAST(Str&, obj);
  385. }else{
  386. ret = CAST(Str&, py_str(obj));
  387. }
  388. if(width != -1 && width > ret.length()){
  389. int pad = width - ret.length();
  390. std::string padding(pad, pad_c);
  391. if(align == '>') ret = padding.c_str() + ret;
  392. else ret = ret + padding.c_str();
  393. }
  394. return VAR(ret);
  395. }
  396. PyObject* VM::new_module(StrName name) {
  397. PyObject* obj = heap._new<DummyModule>(tp_module, DummyModule());
  398. obj->attr().set("__name__", VAR(name.sv()));
  399. // we do not allow override in order to avoid memory leak
  400. // it is because Module objects are not garbage collected
  401. if(_modules.contains(name)) throw std::runtime_error("module already exists");
  402. _modules.set(name, obj);
  403. return obj;
  404. }
  405. static std::string _opcode_argstr(VM* vm, Bytecode byte, const CodeObject* co){
  406. std::string argStr = byte.arg == -1 ? "" : std::to_string(byte.arg);
  407. switch(byte.op){
  408. case OP_LOAD_CONST: case OP_FORMAT_STRING:
  409. if(vm != nullptr){
  410. argStr += fmt(" (", CAST(Str, vm->py_repr(co->consts[byte.arg])), ")");
  411. }
  412. break;
  413. case OP_LOAD_NAME: case OP_LOAD_GLOBAL: case OP_LOAD_NONLOCAL: case OP_STORE_GLOBAL:
  414. case OP_LOAD_ATTR: case OP_LOAD_METHOD: case OP_STORE_ATTR: case OP_DELETE_ATTR:
  415. case OP_IMPORT_NAME: case OP_BEGIN_CLASS: case OP_RAISE:
  416. case OP_DELETE_GLOBAL: case OP_INC_GLOBAL: case OP_DEC_GLOBAL: case OP_STORE_CLASS_ATTR:
  417. argStr += fmt(" (", StrName(byte.arg).sv(), ")");
  418. break;
  419. case OP_LOAD_FAST: case OP_STORE_FAST: case OP_DELETE_FAST: case OP_INC_FAST: case OP_DEC_FAST:
  420. argStr += fmt(" (", co->varnames[byte.arg].sv(), ")");
  421. break;
  422. case OP_LOAD_FUNCTION:
  423. argStr += fmt(" (", co->func_decls[byte.arg]->code->name, ")");
  424. break;
  425. }
  426. return argStr;
  427. }
  428. Str VM::disassemble(CodeObject_ co){
  429. auto pad = [](const Str& s, const int n){
  430. if(s.length() >= n) return s.substr(0, n);
  431. return s + std::string(n - s.length(), ' ');
  432. };
  433. std::vector<int> jumpTargets;
  434. for(auto byte : co->codes){
  435. if(byte.op == OP_JUMP_ABSOLUTE || byte.op == OP_POP_JUMP_IF_FALSE || byte.op == OP_SHORTCUT_IF_FALSE_OR_POP){
  436. jumpTargets.push_back(byte.arg);
  437. }
  438. }
  439. std::stringstream ss;
  440. int prev_line = -1;
  441. for(int i=0; i<co->codes.size(); i++){
  442. const Bytecode& byte = co->codes[i];
  443. Str line = std::to_string(co->lines[i]);
  444. if(co->lines[i] == prev_line) line = "";
  445. else{
  446. if(prev_line != -1) ss << "\n";
  447. prev_line = co->lines[i];
  448. }
  449. std::string pointer;
  450. if(std::find(jumpTargets.begin(), jumpTargets.end(), i) != jumpTargets.end()){
  451. pointer = "-> ";
  452. }else{
  453. pointer = " ";
  454. }
  455. ss << pad(line, 8) << pointer << pad(std::to_string(i), 3);
  456. ss << " " << pad(OP_NAMES[byte.op], 25) << " ";
  457. // ss << pad(byte.arg == -1 ? "" : std::to_string(byte.arg), 5);
  458. std::string argStr = _opcode_argstr(this, byte, co.get());
  459. ss << argStr;
  460. // ss << pad(argStr, 40); // may overflow
  461. // ss << co->blocks[byte.block].type;
  462. if(i != co->codes.size() - 1) ss << '\n';
  463. }
  464. for(auto& decl: co->func_decls){
  465. ss << "\n\n" << "Disassembly of " << decl->code->name << ":\n";
  466. ss << disassemble(decl->code);
  467. }
  468. ss << "\n";
  469. return Str(ss.str());
  470. }
  471. #if PK_DEBUG_CEVAL_STEP
  472. void VM::_log_s_data(const char* title) {
  473. if(_main == nullptr) return;
  474. if(callstack.empty()) return;
  475. std::stringstream ss;
  476. if(title) ss << title << " | ";
  477. std::map<PyObject**, int> sp_bases;
  478. for(Frame& f: callstack.data()){
  479. if(f._sp_base == nullptr) FATAL_ERROR();
  480. sp_bases[f._sp_base] += 1;
  481. }
  482. FrameId frame = top_frame();
  483. int line = frame->co->lines[frame->_ip];
  484. ss << frame->co->name << ":" << line << " [";
  485. for(PyObject** p=s_data.begin(); p!=s_data.end(); p++){
  486. ss << std::string(sp_bases[p], '|');
  487. if(sp_bases[p] > 0) ss << " ";
  488. PyObject* obj = *p;
  489. if(obj == nullptr) ss << "(nil)";
  490. else if(obj == PY_NULL) ss << "NULL";
  491. else if(is_int(obj)) ss << CAST(i64, obj);
  492. else if(is_float(obj)) ss << CAST(f64, obj);
  493. else if(is_type(obj, tp_str)) ss << CAST(Str, obj).escape();
  494. else if(obj == None) ss << "None";
  495. else if(obj == True) ss << "True";
  496. else if(obj == False) ss << "False";
  497. else if(is_type(obj, tp_function)){
  498. auto& f = CAST(Function&, obj);
  499. ss << f.decl->code->name << "(...)";
  500. } else if(is_type(obj, tp_type)){
  501. Type t = PK_OBJ_GET(Type, obj);
  502. ss << "<class " + _all_types[t].name.escape() + ">";
  503. } else if(is_type(obj, tp_list)){
  504. auto& t = CAST(List&, obj);
  505. ss << "list(size=" << t.size() << ")";
  506. } else if(is_type(obj, tp_tuple)){
  507. auto& t = CAST(Tuple&, obj);
  508. ss << "tuple(size=" << t.size() << ")";
  509. } else ss << "(" << obj_type_name(this, obj->type) << ")";
  510. ss << ", ";
  511. }
  512. std::string output = ss.str();
  513. if(!s_data.empty()) {
  514. output.pop_back(); output.pop_back();
  515. }
  516. output.push_back(']');
  517. Bytecode byte = frame->co->codes[frame->_ip];
  518. std::cout << output << " " << OP_NAMES[byte.op] << " " << _opcode_argstr(nullptr, byte, frame->co) << std::endl;
  519. }
  520. #endif
  521. void VM::init_builtin_types(){
  522. _all_types.push_back({heap._new<Type>(Type(1), Type(0)), -1, "object", true});
  523. _all_types.push_back({heap._new<Type>(Type(1), Type(1)), 0, "type", false});
  524. tp_object = 0; tp_type = 1;
  525. tp_int = _new_type_object("int");
  526. tp_float = _new_type_object("float");
  527. if(tp_int.index != kTpIntIndex || tp_float.index != kTpFloatIndex) FATAL_ERROR();
  528. tp_bool = _new_type_object("bool");
  529. tp_str = _new_type_object("str");
  530. tp_list = _new_type_object("list");
  531. tp_tuple = _new_type_object("tuple");
  532. tp_slice = _new_type_object("slice");
  533. tp_range = _new_type_object("range");
  534. tp_module = _new_type_object("module");
  535. tp_function = _new_type_object("function");
  536. tp_native_func = _new_type_object("native_func");
  537. tp_bound_method = _new_type_object("bound_method");
  538. tp_super = _new_type_object("super");
  539. tp_exception = _new_type_object("Exception");
  540. tp_bytes = _new_type_object("bytes");
  541. tp_mappingproxy = _new_type_object("mappingproxy");
  542. tp_dict = _new_type_object("dict");
  543. tp_property = _new_type_object("property");
  544. tp_star_wrapper = _new_type_object("_star_wrapper");
  545. this->None = heap._new<Dummy>(_new_type_object("NoneType"), {});
  546. this->NotImplemented = heap._new<Dummy>(_new_type_object("NotImplementedType"), {});
  547. this->Ellipsis = heap._new<Dummy>(_new_type_object("ellipsis"), {});
  548. this->True = heap._new<Dummy>(tp_bool, {});
  549. this->False = heap._new<Dummy>(tp_bool, {});
  550. this->StopIteration = heap._new<Dummy>(_new_type_object("StopIterationType"), {});
  551. this->builtins = new_module("builtins");
  552. // setup public types
  553. builtins->attr().set("type", _t(tp_type));
  554. builtins->attr().set("object", _t(tp_object));
  555. builtins->attr().set("bool", _t(tp_bool));
  556. builtins->attr().set("int", _t(tp_int));
  557. builtins->attr().set("float", _t(tp_float));
  558. builtins->attr().set("str", _t(tp_str));
  559. builtins->attr().set("list", _t(tp_list));
  560. builtins->attr().set("tuple", _t(tp_tuple));
  561. builtins->attr().set("range", _t(tp_range));
  562. builtins->attr().set("bytes", _t(tp_bytes));
  563. builtins->attr().set("dict", _t(tp_dict));
  564. builtins->attr().set("property", _t(tp_property));
  565. builtins->attr().set("StopIteration", StopIteration);
  566. builtins->attr().set("NotImplemented", NotImplemented);
  567. builtins->attr().set("slice", _t(tp_slice));
  568. post_init();
  569. for(int i=0; i<_all_types.size(); i++){
  570. _all_types[i].obj->attr()._try_perfect_rehash();
  571. }
  572. for(auto [k, v]: _modules.items()) v->attr()._try_perfect_rehash();
  573. this->_main = new_module("__main__");
  574. }
  575. // `heap.gc_scope_lock();` needed before calling this function
  576. void VM::_unpack_as_list(ArgsView args, List& list){
  577. for(PyObject* obj: args){
  578. if(is_non_tagged_type(obj, tp_star_wrapper)){
  579. const StarWrapper& w = _CAST(StarWrapper&, obj);
  580. // maybe this check should be done in the compile time
  581. if(w.level != 1) TypeError("expected level 1 star wrapper");
  582. PyObject* _0 = py_iter(w.obj);
  583. PyObject* _1 = py_next(_0);
  584. while(_1 != StopIteration){
  585. list.push_back(_1);
  586. _1 = py_next(_0);
  587. }
  588. }else{
  589. list.push_back(obj);
  590. }
  591. }
  592. }
  593. // `heap.gc_scope_lock();` needed before calling this function
  594. void VM::_unpack_as_dict(ArgsView args, Dict& dict){
  595. for(PyObject* obj: args){
  596. if(is_non_tagged_type(obj, tp_star_wrapper)){
  597. const StarWrapper& w = _CAST(StarWrapper&, obj);
  598. // maybe this check should be done in the compile time
  599. if(w.level != 2) TypeError("expected level 2 star wrapper");
  600. const Dict& other = CAST(Dict&, w.obj);
  601. dict.update(other);
  602. }else{
  603. const Tuple& t = CAST(Tuple&, obj);
  604. if(t.size() != 2) TypeError("expected tuple of length 2");
  605. dict.set(t[0], t[1]);
  606. }
  607. }
  608. }
  609. void VM::_prepare_py_call(PyObject** buffer, ArgsView args, ArgsView kwargs, const FuncDecl_& decl){
  610. const CodeObject* co = decl->code.get();
  611. int co_nlocals = co->varnames.size();
  612. int decl_argc = decl->args.size();
  613. if(args.size() < decl_argc){
  614. vm->TypeError(fmt(
  615. "expected ", decl_argc, " positional arguments, got ", args.size(),
  616. " (", co->name, ')'
  617. ));
  618. }
  619. int i = 0;
  620. // prepare args
  621. for(int index: decl->args) buffer[index] = args[i++];
  622. // set extra varnames to nullptr
  623. for(int j=i; j<co_nlocals; j++) buffer[j] = PY_NULL;
  624. // prepare kwdefaults
  625. for(auto& kv: decl->kwargs) buffer[kv.key] = kv.value;
  626. // handle *args
  627. if(decl->starred_arg != -1){
  628. ArgsView vargs(args.begin() + i, args.end());
  629. buffer[decl->starred_arg] = VAR(vargs.to_tuple());
  630. i += vargs.size();
  631. }else{
  632. // kwdefaults override
  633. for(auto& kv: decl->kwargs){
  634. if(i >= args.size()) break;
  635. buffer[kv.key] = args[i++];
  636. }
  637. if(i < args.size()) TypeError(fmt("too many arguments", " (", decl->code->name, ')'));
  638. }
  639. PyObject* vkwargs;
  640. if(decl->starred_kwarg != -1){
  641. vkwargs = VAR(Dict(this));
  642. buffer[decl->starred_kwarg] = vkwargs;
  643. }else{
  644. vkwargs = nullptr;
  645. }
  646. for(int j=0; j<kwargs.size(); j+=2){
  647. StrName key(CAST(int, kwargs[j]));
  648. int index = co->varnames_inv.try_get(key);
  649. if(index < 0){
  650. if(vkwargs == nullptr){
  651. TypeError(fmt(key.escape(), " is an invalid keyword argument for ", co->name, "()"));
  652. }else{
  653. Dict& dict = _CAST(Dict&, vkwargs);
  654. dict.set(VAR(key.sv()), kwargs[j+1]);
  655. }
  656. }else{
  657. buffer[index] = kwargs[j+1];
  658. }
  659. }
  660. }
  661. PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){
  662. PyObject** p1 = s_data._sp - KWARGC*2;
  663. PyObject** p0 = p1 - ARGC - 2;
  664. // [callable, <self>, args..., kwargs...]
  665. // ^p0 ^p1 ^_sp
  666. PyObject* callable = p1[-(ARGC + 2)];
  667. bool method_call = p1[-(ARGC + 1)] != PY_NULL;
  668. // handle boundmethod, do a patch
  669. if(is_non_tagged_type(callable, tp_bound_method)){
  670. if(method_call) FATAL_ERROR();
  671. auto& bm = CAST(BoundMethod&, callable);
  672. callable = bm.func; // get unbound method
  673. p1[-(ARGC + 2)] = bm.func;
  674. p1[-(ARGC + 1)] = bm.self;
  675. method_call = true;
  676. // [unbound, self, args..., kwargs...]
  677. }
  678. ArgsView args(p1 - ARGC - int(method_call), p1);
  679. ArgsView kwargs(p1, s_data._sp);
  680. PyObject* buffer[PK_MAX_CO_VARNAMES];
  681. if(is_non_tagged_type(callable, tp_native_func)){
  682. const auto& f = PK_OBJ_GET(NativeFunc, callable);
  683. PyObject* ret;
  684. if(f.decl != nullptr){
  685. int co_nlocals = f.decl->code->varnames.size();
  686. _prepare_py_call(buffer, args, kwargs, f.decl);
  687. // copy buffer back to stack
  688. s_data.reset(args.begin());
  689. for(int j=0; j<co_nlocals; j++) PUSH(buffer[j]);
  690. ret = f.call(vm, ArgsView(s_data._sp - co_nlocals, s_data._sp));
  691. }else{
  692. if(KWARGC != 0) TypeError("old-style native_func does not accept keyword arguments");
  693. f.check_size(this, args);
  694. ret = f.call(this, args);
  695. }
  696. s_data.reset(p0);
  697. return ret;
  698. }
  699. if(is_non_tagged_type(callable, tp_function)){
  700. /*****************_py_call*****************/
  701. // callable must be a `function` object
  702. if(s_data.is_overflow()) StackOverflowError();
  703. const Function& fn = PK_OBJ_GET(Function, callable);
  704. const FuncDecl_& decl = fn.decl;
  705. const CodeObject* co = decl->code.get();
  706. int co_nlocals = co->varnames.size();
  707. _prepare_py_call(buffer, args, kwargs, decl);
  708. if(co->is_generator){
  709. s_data.reset(p0);
  710. return _py_generator(
  711. Frame(&s_data, nullptr, co, fn._module, callable),
  712. ArgsView(buffer, buffer + co_nlocals)
  713. );
  714. }
  715. // copy buffer back to stack
  716. s_data.reset(args.begin());
  717. for(int j=0; j<co_nlocals; j++) PUSH(buffer[j]);
  718. callstack.emplace(&s_data, p0, co, fn._module, callable, FastLocals(co, args.begin()));
  719. if(op_call) return PY_OP_CALL;
  720. return _run_top_frame();
  721. /*****************_py_call*****************/
  722. }
  723. if(is_non_tagged_type(callable, tp_type)){
  724. if(method_call) FATAL_ERROR();
  725. // [type, NULL, args..., kwargs...]
  726. PyObject* new_f = find_name_in_mro(callable, __new__);
  727. PyObject* obj;
  728. #if PK_DEBUG_EXTRA_CHECK
  729. PK_ASSERT(new_f != nullptr);
  730. #endif
  731. if(new_f == cached_object__new__) {
  732. // fast path for object.__new__
  733. Type t = PK_OBJ_GET(Type, callable);
  734. obj= vm->heap.gcnew<DummyInstance>(t, {});
  735. }else{
  736. PUSH(new_f);
  737. PUSH(PY_NULL);
  738. PUSH(callable); // cls
  739. for(PyObject* o: args) PUSH(o);
  740. for(PyObject* o: kwargs) PUSH(o);
  741. // if obj is not an instance of callable, the behavior is undefined
  742. obj = vectorcall(ARGC+1, KWARGC);
  743. }
  744. // __init__
  745. PyObject* self;
  746. callable = get_unbound_method(obj, __init__, &self, false);
  747. if (self != PY_NULL) {
  748. // replace `NULL` with `self`
  749. p1[-(ARGC + 2)] = callable;
  750. p1[-(ARGC + 1)] = self;
  751. // [init_f, self, args..., kwargs...]
  752. vectorcall(ARGC, KWARGC);
  753. // We just discard the return value of `__init__`
  754. // in cpython it raises a TypeError if the return value is not None
  755. }else{
  756. // manually reset the stack
  757. s_data.reset(p0);
  758. }
  759. return obj;
  760. }
  761. // handle `__call__` overload
  762. PyObject* self;
  763. PyObject* call_f = get_unbound_method(callable, __call__, &self, false);
  764. if(self != PY_NULL){
  765. p1[-(ARGC + 2)] = call_f;
  766. p1[-(ARGC + 1)] = self;
  767. // [call_f, self, args..., kwargs...]
  768. return vectorcall(ARGC, KWARGC, false);
  769. }
  770. TypeError(OBJ_NAME(_t(callable)).escape() + " object is not callable");
  771. return nullptr;
  772. }
  773. // https://docs.python.org/3/howto/descriptor.html#invocation-from-an-instance
  774. PyObject* VM::getattr(PyObject* obj, StrName name, bool throw_err){
  775. PyObject* objtype;
  776. // handle super() proxy
  777. if(is_non_tagged_type(obj, tp_super)){
  778. const Super& super = PK_OBJ_GET(Super, obj);
  779. obj = super.first;
  780. objtype = _t(super.second);
  781. }else{
  782. objtype = _t(obj);
  783. }
  784. PyObject* cls_var = find_name_in_mro(objtype, name);
  785. if(cls_var != nullptr){
  786. // handle descriptor
  787. if(is_non_tagged_type(cls_var, tp_property)){
  788. const Property& prop = _CAST(Property&, cls_var);
  789. return call(prop.getter, obj);
  790. }
  791. }
  792. // handle instance __dict__
  793. if(!is_tagged(obj) && obj->is_attr_valid()){
  794. PyObject* val = obj->attr().try_get(name);
  795. if(val != nullptr) return val;
  796. }
  797. if(cls_var != nullptr){
  798. // bound method is non-data descriptor
  799. if(is_non_tagged_type(cls_var, tp_function) || is_non_tagged_type(cls_var, tp_native_func)){
  800. return VAR(BoundMethod(obj, cls_var));
  801. }
  802. return cls_var;
  803. }
  804. if(throw_err) AttributeError(obj, name);
  805. return nullptr;
  806. }
  807. // used by OP_LOAD_METHOD
  808. // try to load a unbound method (fallback to `getattr` if not found)
  809. PyObject* VM::get_unbound_method(PyObject* obj, StrName name, PyObject** self, bool throw_err, bool fallback){
  810. *self = PY_NULL;
  811. PyObject* objtype;
  812. // handle super() proxy
  813. if(is_non_tagged_type(obj, tp_super)){
  814. const Super& super = PK_OBJ_GET(Super, obj);
  815. obj = super.first;
  816. objtype = _t(super.second);
  817. }else{
  818. objtype = _t(obj);
  819. }
  820. PyObject* cls_var = find_name_in_mro(objtype, name);
  821. if(fallback){
  822. if(cls_var != nullptr){
  823. // handle descriptor
  824. if(is_non_tagged_type(cls_var, tp_property)){
  825. const Property& prop = _CAST(Property&, cls_var);
  826. return call(prop.getter, obj);
  827. }
  828. }
  829. // handle instance __dict__
  830. if(!is_tagged(obj) && obj->is_attr_valid()){
  831. PyObject* val = obj->attr().try_get(name);
  832. if(val != nullptr) return val;
  833. }
  834. }
  835. if(cls_var != nullptr){
  836. if(is_non_tagged_type(cls_var, tp_function) || is_non_tagged_type(cls_var, tp_native_func)){
  837. *self = obj;
  838. }
  839. return cls_var;
  840. }
  841. if(throw_err) AttributeError(obj, name);
  842. return nullptr;
  843. }
  844. void VM::setattr(PyObject* obj, StrName name, PyObject* value){
  845. PyObject* objtype;
  846. // handle super() proxy
  847. if(is_non_tagged_type(obj, tp_super)){
  848. Super& super = PK_OBJ_GET(Super, obj);
  849. obj = super.first;
  850. objtype = _t(super.second);
  851. }else{
  852. objtype = _t(obj);
  853. }
  854. PyObject* cls_var = find_name_in_mro(objtype, name);
  855. if(cls_var != nullptr){
  856. // handle descriptor
  857. if(is_non_tagged_type(cls_var, tp_property)){
  858. const Property& prop = _CAST(Property&, cls_var);
  859. if(prop.setter != vm->None){
  860. call(prop.setter, obj, value);
  861. }else{
  862. TypeError(fmt("readonly attribute: ", name.escape()));
  863. }
  864. return;
  865. }
  866. }
  867. // handle instance __dict__
  868. if(is_tagged(obj) || !obj->is_attr_valid()) TypeError("cannot set attribute");
  869. obj->attr().set(name, value);
  870. }
  871. PyObject* VM::bind(PyObject* obj, const char* sig, NativeFuncC fn, UserData userdata){
  872. return bind(obj, sig, nullptr, fn, userdata);
  873. }
  874. PyObject* VM::bind(PyObject* obj, const char* sig, const char* docstring, NativeFuncC fn, UserData userdata){
  875. CodeObject_ co;
  876. try{
  877. // fn(a, b, *c, d=1) -> None
  878. co = compile("def " + Str(sig) + " : pass", "<bind>", EXEC_MODE);
  879. }catch(Exception&){
  880. throw std::runtime_error("invalid signature: " + std::string(sig));
  881. }
  882. if(co->func_decls.size() != 1){
  883. throw std::runtime_error("expected 1 function declaration");
  884. }
  885. FuncDecl_ decl = co->func_decls[0];
  886. decl->signature = Str(sig);
  887. if(docstring != nullptr){
  888. decl->docstring = Str(docstring).strip();
  889. }
  890. PyObject* f_obj = VAR(NativeFunc(fn, decl));
  891. PK_OBJ_GET(NativeFunc, f_obj).set_userdata(userdata);
  892. if(obj != nullptr) obj->attr().set(decl->code->name, f_obj);
  893. return f_obj;
  894. }
  895. PyObject* VM::bind_property(PyObject* obj, StrName name, const char* type_hint, NativeFuncC fget, NativeFuncC fset){
  896. PyObject* prop = property(fget, fset, type_hint);
  897. obj->attr().set(name, prop);
  898. return prop;
  899. }
  900. void VM::_error(Exception e){
  901. if(callstack.empty()){
  902. e.is_re = false;
  903. throw e;
  904. }
  905. PUSH(VAR(e));
  906. _raise();
  907. }
  908. void ManagedHeap::mark() {
  909. for(PyObject* obj: _no_gc) PK_OBJ_MARK(obj);
  910. for(auto& frame : vm->callstack.data()) frame._gc_mark();
  911. for(PyObject* obj: vm->s_data) PK_OBJ_MARK(obj);
  912. if(_gc_marker_ex) _gc_marker_ex(vm);
  913. if(vm->_last_exception) PK_OBJ_MARK(vm->_last_exception);
  914. if(vm->_c.error != nullptr) PK_OBJ_MARK(vm->_c.error);
  915. }
  916. Str obj_type_name(VM *vm, Type type){
  917. return vm->_all_types[type].name;
  918. }
  919. void VM::bind__hash__(Type type, i64 (*f)(VM*, PyObject*)){
  920. PyObject* obj = _t(type);
  921. _all_types[type].m__hash__ = f;
  922. PyObject* nf = bind_method<0>(obj, "__hash__", [](VM* vm, ArgsView args){
  923. i64 ret = lambda_get_userdata<i64(*)(VM*, PyObject*)>(args.begin())(vm, args[0]);
  924. return VAR(ret);
  925. });
  926. PK_OBJ_GET(NativeFunc, nf).set_userdata(f);
  927. }
  928. void VM::bind__len__(Type type, i64 (*f)(VM*, PyObject*)){
  929. PyObject* obj = _t(type);
  930. _all_types[type].m__len__ = f;
  931. PyObject* nf = bind_method<0>(obj, "__len__", [](VM* vm, ArgsView args){
  932. i64 ret = lambda_get_userdata<i64(*)(VM*, PyObject*)>(args.begin())(vm, args[0]);
  933. return VAR(ret);
  934. });
  935. PK_OBJ_GET(NativeFunc, nf).set_userdata(f);
  936. }
  937. void Dict::_probe(PyObject *key, bool &ok, int &i) const{
  938. ok = false;
  939. i = vm->py_hash(key) & _mask;
  940. while(_items[i].first != nullptr) {
  941. if(vm->py_equals(_items[i].first, key)) { ok = true; break; }
  942. // https://github.com/python/cpython/blob/3.8/Objects/dictobject.c#L166
  943. i = ((5*i) + 1) & _mask;
  944. }
  945. }
  946. void CodeObjectSerializer::write_object(VM *vm, PyObject *obj){
  947. if(is_int(obj)) write_int(_CAST(i64, obj));
  948. else if(is_float(obj)) write_float(_CAST(f64, obj));
  949. else if(is_type(obj, vm->tp_str)) write_str(_CAST(Str&, obj));
  950. else if(is_type(obj, vm->tp_bool)) write_bool(_CAST(bool, obj));
  951. else if(obj == vm->None) write_none();
  952. else if(obj == vm->Ellipsis) write_ellipsis();
  953. else{
  954. throw std::runtime_error(fmt(OBJ_NAME(vm->_t(obj)).escape(), " is not serializable"));
  955. }
  956. }
  957. void NativeFunc::check_size(VM* vm, ArgsView args) const{
  958. if(args.size() != argc && argc != -1) {
  959. vm->TypeError(fmt("expected ", argc, " arguments, got ", args.size()));
  960. }
  961. }
  962. PyObject* NativeFunc::call(VM *vm, ArgsView args) const {
  963. return f(vm, args);
  964. }
  965. } // namespace pkpy