vm.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. #pragma once
  2. #include "codeobject.h"
  3. #include "error.h"
  4. #define DEF_NATIVE(type, ctype, ptype) \
  5. inline ctype& Py##type##_AS_C(const PyVar& obj) { \
  6. check_type(obj, ptype); \
  7. return OBJ_GET(ctype, obj); \
  8. } \
  9. inline PyVar Py##type(ctype value) { \
  10. return new_object(ptype, value); \
  11. }
  12. class VM {
  13. std::stack< std::unique_ptr<Frame> > callstack;
  14. PyVar _py_op_call;
  15. PyVar _ascii_str_pool[128];
  16. PyVar run_frame(Frame* frame){
  17. while(frame->has_next_bytecode()){
  18. const Bytecode& byte = frame->next_bytecode();
  19. // if(true || frame->_module != builtins){
  20. // printf("%d: %s (%d) %s\n", frame->_ip, OP_NAMES[byte.op], byte.arg, frame->stack_info().c_str());
  21. // }
  22. switch (byte.op)
  23. {
  24. case OP_NO_OP: break; // do nothing
  25. case OP_LOAD_CONST: frame->push(frame->co->consts[byte.arg]); break;
  26. case OP_LOAD_LAMBDA: {
  27. PyVar obj = frame->co->consts[byte.arg];
  28. setattr(obj, __module__, frame->_module);
  29. frame->push(obj);
  30. } break;
  31. case OP_LOAD_NAME_REF: {
  32. frame->push(PyRef(NameRef(frame->co->names[byte.arg])));
  33. } break;
  34. case OP_LOAD_NAME: {
  35. frame->push(NameRef(frame->co->names[byte.arg]).get(this, frame));
  36. } break;
  37. case OP_STORE_NAME: {
  38. auto& p = frame->co->names[byte.arg];
  39. NameRef(p).set(this, frame, frame->pop_value(this));
  40. } break;
  41. case OP_BUILD_ATTR: {
  42. int name = byte.arg >> 1;
  43. bool _rvalue = byte.arg % 2 == 1;
  44. auto& attr = frame->co->names[name];
  45. PyVar obj = frame->pop_value(this);
  46. AttrRef ref = AttrRef(obj, NameRef(attr));
  47. if(_rvalue) frame->push(ref.get(this, frame));
  48. else frame->push(PyRef(ref));
  49. } break;
  50. case OP_BUILD_INDEX: {
  51. PyVar index = frame->pop_value(this);
  52. auto ref = IndexRef(frame->pop_value(this), index);
  53. if(byte.arg == 0) frame->push(PyRef(ref));
  54. else frame->push(ref.get(this, frame));
  55. } break;
  56. case OP_STORE_REF: {
  57. PyVar obj = frame->pop_value(this);
  58. PyVarRef r = frame->pop();
  59. PyRef_AS_C(r)->set(this, frame, std::move(obj));
  60. } break;
  61. case OP_DELETE_REF: {
  62. PyVarRef r = frame->pop();
  63. PyRef_AS_C(r)->del(this, frame);
  64. } break;
  65. case OP_BUILD_SMART_TUPLE:
  66. {
  67. pkpy::Args items = frame->pop_n_reversed(byte.arg);
  68. bool done = false;
  69. for(int i=0; i<items.size(); i++){
  70. if(!items[i]->is_type(tp_ref)) {
  71. done = true;
  72. for(int j=i; j<items.size(); j++) frame->try_deref(this, items[j]);
  73. frame->push(PyTuple(std::move(items)));
  74. break;
  75. }
  76. }
  77. if(done) break;
  78. frame->push(PyRef(TupleRef(std::move(items))));
  79. } break;
  80. case OP_BUILD_STRING:
  81. {
  82. pkpy::Args items = frame->pop_n_values_reversed(this, byte.arg);
  83. StrStream ss;
  84. for(int i=0; i<items.size(); i++) ss << PyStr_AS_C(asStr(items[i]));
  85. frame->push(PyStr(ss.str()));
  86. } break;
  87. case OP_LOAD_EVAL_FN: {
  88. frame->push(builtins->attribs[m_eval]);
  89. } break;
  90. case OP_LIST_APPEND: {
  91. pkpy::Args args(2);
  92. args[1] = frame->pop_value(this); // obj
  93. args[0] = frame->top_value_offset(this, -2); // list
  94. fast_call(m_append, std::move(args));
  95. } break;
  96. case OP_STORE_FUNCTION:
  97. {
  98. PyVar obj = frame->pop_value(this);
  99. const pkpy::Function_& fn = PyFunction_AS_C(obj);
  100. setattr(obj, __module__, frame->_module);
  101. frame->f_globals()[fn->name] = obj;
  102. } break;
  103. case OP_BUILD_CLASS:
  104. {
  105. const Str& clsName = frame->co->names[byte.arg].first;
  106. PyVar clsBase = frame->pop_value(this);
  107. if(clsBase == None) clsBase = tp_object;
  108. check_type(clsBase, tp_type);
  109. PyVar cls = new_type_object(frame->_module, clsName, clsBase);
  110. while(true){
  111. PyVar fn = frame->pop_value(this);
  112. if(fn == None) break;
  113. const pkpy::Function_& f = PyFunction_AS_C(fn);
  114. setattr(fn, __module__, frame->_module);
  115. setattr(cls, f->name, fn);
  116. }
  117. } break;
  118. case OP_RETURN_VALUE: return frame->pop_value(this);
  119. case OP_PRINT_EXPR:
  120. {
  121. const PyVar expr = frame->top_value(this);
  122. if(expr == None) break;
  123. *_stdout << PyStr_AS_C(asRepr(expr)) << '\n';
  124. } break;
  125. case OP_POP_TOP: frame->_pop(); break;
  126. case OP_BINARY_OP:
  127. {
  128. pkpy::Args args(2);
  129. args[1] = frame->pop_value(this);
  130. args[0] = frame->top_value(this);
  131. frame->top() = fast_call(BINARY_SPECIAL_METHODS[byte.arg], std::move(args));
  132. } break;
  133. case OP_BITWISE_OP:
  134. {
  135. frame->push(
  136. fast_call(BITWISE_SPECIAL_METHODS[byte.arg],
  137. frame->pop_n_values_reversed(this, 2))
  138. );
  139. } break;
  140. case OP_COMPARE_OP:
  141. {
  142. pkpy::Args args(2);
  143. args[1] = frame->pop_value(this);
  144. args[0] = frame->top_value(this);
  145. frame->top() = fast_call(CMP_SPECIAL_METHODS[byte.arg], std::move(args));
  146. } break;
  147. case OP_IS_OP:
  148. {
  149. PyVar rhs = frame->pop_value(this);
  150. bool ret_c = rhs == frame->top_value(this);
  151. if(byte.arg == 1) ret_c = !ret_c;
  152. frame->top() = PyBool(ret_c);
  153. } break;
  154. case OP_CONTAINS_OP:
  155. {
  156. PyVar rhs = frame->pop_value(this);
  157. bool ret_c = PyBool_AS_C(call(rhs, __contains__, pkpy::one_arg(frame->pop_value(this))));
  158. if(byte.arg == 1) ret_c = !ret_c;
  159. frame->push(PyBool(ret_c));
  160. } break;
  161. case OP_UNARY_NEGATIVE:
  162. frame->top() = num_negated(frame->top_value(this));
  163. break;
  164. case OP_UNARY_NOT:
  165. {
  166. PyVar obj = frame->pop_value(this);
  167. const PyVar& obj_bool = asBool(obj);
  168. frame->push(PyBool(!PyBool_AS_C(obj_bool)));
  169. } break;
  170. case OP_POP_JUMP_IF_FALSE:
  171. if(!PyBool_AS_C(asBool(frame->pop_value(this)))) frame->jump_abs(byte.arg);
  172. break;
  173. case OP_LOAD_NONE: frame->push(None); break;
  174. case OP_LOAD_TRUE: frame->push(True); break;
  175. case OP_LOAD_FALSE: frame->push(False); break;
  176. case OP_LOAD_ELLIPSIS: frame->push(Ellipsis); break;
  177. case OP_ASSERT:
  178. {
  179. PyVar expr = frame->pop_value(this);
  180. if(asBool(expr) != True) _error("AssertionError", "");
  181. } break;
  182. case OP_EXCEPTION_MATCH:
  183. {
  184. const auto& _e = PyException_AS_C(frame->top());
  185. Str name = frame->co->names[byte.arg].first;
  186. frame->push(PyBool(_e.match_type(name)));
  187. } break;
  188. case OP_RAISE:
  189. {
  190. PyVar obj = frame->pop_value(this);
  191. Str msg = obj == None ? "" : PyStr_AS_C(asStr(obj));
  192. Str type = frame->co->names[byte.arg].first;
  193. _error(type, msg);
  194. } break;
  195. case OP_RE_RAISE: _raise(); break;
  196. case OP_BUILD_LIST:
  197. frame->push(PyList(
  198. frame->pop_n_values_reversed(this, byte.arg).to_list()));
  199. break;
  200. case OP_BUILD_MAP:
  201. {
  202. pkpy::Args items = frame->pop_n_values_reversed(this, byte.arg*2);
  203. PyVar obj = call(builtins->attribs["dict"]);
  204. for(int i=0; i<items.size(); i+=2){
  205. call(obj, __setitem__, pkpy::two_args(items[i], items[i+1]));
  206. }
  207. frame->push(obj);
  208. } break;
  209. case OP_BUILD_SET:
  210. {
  211. PyVar list = PyList(
  212. frame->pop_n_values_reversed(this, byte.arg).to_list()
  213. );
  214. PyVar obj = call(builtins->attribs["set"], pkpy::one_arg(list));
  215. frame->push(obj);
  216. } break;
  217. case OP_DUP_TOP: frame->push(frame->top_value(this)); break;
  218. case OP_CALL:
  219. {
  220. int ARGC = byte.arg & 0xFFFF;
  221. int KWARGC = (byte.arg >> 16) & 0xFFFF;
  222. pkpy::Args kwargs(0);
  223. if(KWARGC > 0) kwargs = frame->pop_n_values_reversed(this, KWARGC*2);
  224. pkpy::Args args = frame->pop_n_values_reversed(this, ARGC);
  225. PyVar callable = frame->pop_value(this);
  226. PyVar ret = call(callable, std::move(args), kwargs, true);
  227. if(ret == _py_op_call) return ret;
  228. frame->push(std::move(ret));
  229. } break;
  230. case OP_JUMP_ABSOLUTE: frame->jump_abs(byte.arg); break;
  231. case OP_SAFE_JUMP_ABSOLUTE: frame->jump_abs_safe(byte.arg); break;
  232. case OP_GOTO: {
  233. const Str& label = frame->co->names[byte.arg].first;
  234. int* target = frame->co->labels.try_get(label);
  235. if(target == nullptr) _error("KeyError", "label '" + label + "' not found");
  236. frame->jump_abs_safe(*target);
  237. } break;
  238. case OP_GET_ITER:
  239. {
  240. PyVar obj = frame->pop_value(this);
  241. PyVarOrNull iter_fn = getattr(obj, __iter__, false);
  242. if(iter_fn != nullptr){
  243. PyVar tmp = call(iter_fn);
  244. PyVarRef var = frame->pop();
  245. check_type(var, tp_ref);
  246. PyIter_AS_C(tmp)->var = var;
  247. frame->push(std::move(tmp));
  248. }else{
  249. TypeError(OBJ_TP_NAME(obj).escape(true) + " object is not iterable");
  250. }
  251. } break;
  252. case OP_FOR_ITER:
  253. {
  254. // top() must be PyIter, so no need to try_deref()
  255. auto& it = PyIter_AS_C(frame->top());
  256. if(it->has_next()){
  257. PyRef_AS_C(it->var)->set(this, frame, it->next());
  258. }else{
  259. int blockEnd = frame->co->blocks[byte.block].end;
  260. frame->jump_abs_safe(blockEnd);
  261. }
  262. } break;
  263. case OP_LOOP_CONTINUE:
  264. {
  265. int blockStart = frame->co->blocks[byte.block].start;
  266. frame->jump_abs(blockStart);
  267. } break;
  268. case OP_LOOP_BREAK:
  269. {
  270. int blockEnd = frame->co->blocks[byte.block].end;
  271. frame->jump_abs_safe(blockEnd);
  272. } break;
  273. case OP_JUMP_IF_FALSE_OR_POP:
  274. {
  275. const PyVar expr = frame->top_value(this);
  276. if(asBool(expr)==False) frame->jump_abs(byte.arg);
  277. else frame->pop_value(this);
  278. } break;
  279. case OP_JUMP_IF_TRUE_OR_POP:
  280. {
  281. const PyVar expr = frame->top_value(this);
  282. if(asBool(expr)==True) frame->jump_abs(byte.arg);
  283. else frame->pop_value(this);
  284. } break;
  285. case OP_BUILD_SLICE:
  286. {
  287. PyVar stop = frame->pop_value(this);
  288. PyVar start = frame->pop_value(this);
  289. pkpy::Slice s;
  290. if(start != None) {check_type(start, tp_int); s.start = (int)PyInt_AS_C(start);}
  291. if(stop != None) {check_type(stop, tp_int); s.stop = (int)PyInt_AS_C(stop);}
  292. frame->push(PySlice(s));
  293. } break;
  294. case OP_IMPORT_NAME:
  295. {
  296. const Str& name = frame->co->names[byte.arg].first;
  297. auto it = _modules.find(name);
  298. if(it == _modules.end()){
  299. auto it2 = _lazy_modules.find(name);
  300. if(it2 == _lazy_modules.end()){
  301. _error("ImportError", "module '" + name + "' not found");
  302. }else{
  303. const Str& source = it2->second;
  304. CodeObject_ code = compile(source, name, EXEC_MODE);
  305. PyVar _m = new_module(name);
  306. _exec(code, _m, pkpy::make_shared<pkpy::NameDict>());
  307. frame->push(_m);
  308. _lazy_modules.erase(it2);
  309. }
  310. }else{
  311. frame->push(it->second);
  312. }
  313. } break;
  314. // TODO: using "goto" inside with block may cause __exit__ not called
  315. case OP_WITH_ENTER: call(frame->pop_value(this), __enter__); break;
  316. case OP_WITH_EXIT: call(frame->pop_value(this), __exit__); break;
  317. case OP_TRY_BLOCK_ENTER: frame->on_try_block_enter(); break;
  318. case OP_TRY_BLOCK_EXIT: frame->on_try_block_exit(); break;
  319. default:
  320. throw std::runtime_error(Str("opcode ") + OP_NAMES[byte.op] + " is not implemented");
  321. break;
  322. }
  323. }
  324. if(frame->co->src->mode == EVAL_MODE || frame->co->src->mode == JSON_MODE){
  325. if(frame->_data.size() != 1) throw std::runtime_error("_data.size() != 1 in EVAL/JSON_MODE");
  326. return frame->pop_value(this);
  327. }
  328. if(!frame->_data.empty()) throw std::runtime_error("_data.size() != 0 in EXEC_MODE");
  329. return None;
  330. }
  331. public:
  332. pkpy::NameDict _types;
  333. pkpy::NameDict _modules; // loaded modules
  334. emhash8::HashMap<Str, Str> _lazy_modules; // lazy loaded modules
  335. PyVar None, True, False, Ellipsis;
  336. bool use_stdio;
  337. std::ostream* _stdout;
  338. std::ostream* _stderr;
  339. PyVar builtins; // builtins module
  340. PyVar _main; // __main__ module
  341. int maxRecursionDepth = 1000;
  342. VM(bool use_stdio){
  343. this->use_stdio = use_stdio;
  344. if(use_stdio){
  345. this->_stdout = &std::cout;
  346. this->_stderr = &std::cerr;
  347. }else{
  348. this->_stdout = new StrStream();
  349. this->_stderr = new StrStream();
  350. }
  351. init_builtin_types();
  352. for(int i=0; i<128; i++) _ascii_str_pool[i] = new_object(tp_str, std::string(1, (char)i));
  353. }
  354. PyVar asStr(const PyVar& obj){
  355. PyVarOrNull f = getattr(obj, __str__, false);
  356. if(f != nullptr) return call(f);
  357. return asRepr(obj);
  358. }
  359. inline Frame* top_frame() const {
  360. if(callstack.empty()) UNREACHABLE();
  361. return callstack.top().get();
  362. }
  363. PyVar asRepr(const PyVar& obj){
  364. if(obj->is_type(tp_type)) return PyStr("<class '" + OBJ_GET(Str, obj->attribs[__name__]) + "'>");
  365. return call(obj, __repr__);
  366. }
  367. const PyVar& asBool(const PyVar& obj){
  368. if(obj->is_type(tp_bool)) return obj;
  369. if(obj == None) return False;
  370. if(obj->is_type(tp_int)) return PyBool(PyInt_AS_C(obj) != 0);
  371. if(obj->is_type(tp_float)) return PyBool(PyFloat_AS_C(obj) != 0.0);
  372. PyVarOrNull len_fn = getattr(obj, __len__, false);
  373. if(len_fn != nullptr){
  374. PyVar ret = call(len_fn);
  375. return PyBool(PyInt_AS_C(ret) > 0);
  376. }
  377. return True;
  378. }
  379. PyVar fast_call(const Str& name, pkpy::Args&& args){
  380. PyObject* cls = args[0]->type.get();
  381. while(cls != None.get()) {
  382. PyVar* val = cls->attribs.try_get(name);
  383. if(val != nullptr) return call(*val, std::move(args));
  384. cls = cls->attribs[__base__].get();
  385. }
  386. AttributeError(args[0], name);
  387. return nullptr;
  388. }
  389. inline PyVar call(const PyVar& _callable){
  390. return call(_callable, pkpy::no_arg(), pkpy::no_arg(), false);
  391. }
  392. template<typename ArgT>
  393. inline std::enable_if_t<std::is_same_v<std::remove_const_t<std::remove_reference_t<ArgT>>, pkpy::Args>, PyVar>
  394. call(const PyVar& _callable, ArgT&& args){
  395. return call(_callable, std::forward<ArgT>(args), pkpy::no_arg(), false);
  396. }
  397. template<typename ArgT>
  398. inline std::enable_if_t<std::is_same_v<std::remove_const_t<std::remove_reference_t<ArgT>>, pkpy::Args>, PyVar>
  399. call(const PyVar& obj, const Str& func, ArgT&& args){
  400. return call(getattr(obj, func), std::forward<ArgT>(args), pkpy::no_arg(), false);
  401. }
  402. inline PyVar call(const PyVar& obj, const Str& func){
  403. return call(getattr(obj, func), pkpy::no_arg(), pkpy::no_arg(), false);
  404. }
  405. PyVar call(const PyVar& _callable, pkpy::Args args, const pkpy::Args& kwargs, bool opCall){
  406. if(_callable->is_type(tp_type)){
  407. auto it = _callable->attribs.find(__new__);
  408. PyVar obj;
  409. if(it != _callable->attribs.end()){
  410. obj = call(it->second, args, kwargs, false);
  411. }else{
  412. obj = new_object(_callable, DUMMY_VAL);
  413. PyVarOrNull init_fn = getattr(obj, __init__, false);
  414. if (init_fn != nullptr) call(init_fn, args, kwargs, false);
  415. }
  416. return obj;
  417. }
  418. const PyVar* callable = &_callable;
  419. if((*callable)->is_type(tp_bound_method)){
  420. auto& bm = PyBoundMethod_AS_C((*callable));
  421. callable = &bm.method; // get unbound method
  422. args.extend_self(bm.obj);
  423. }
  424. if((*callable)->is_type(tp_native_function)){
  425. const auto& f = OBJ_GET(pkpy::NativeFunc, *callable);
  426. if(kwargs.size() != 0) TypeError("native_function does not accept keyword arguments");
  427. return f(this, args);
  428. } else if((*callable)->is_type(tp_function)){
  429. const pkpy::Function_& fn = PyFunction_AS_C((*callable));
  430. pkpy::shared_ptr<pkpy::NameDict> _locals = pkpy::make_shared<pkpy::NameDict>();
  431. pkpy::NameDict& locals = *_locals;
  432. int i = 0;
  433. for(const auto& name : fn->args){
  434. if(i < args.size()){
  435. locals.emplace(name, args[i++]);
  436. continue;
  437. }
  438. TypeError("missing positional argument '" + name + "'");
  439. }
  440. locals.insert(fn->kwArgs.begin(), fn->kwArgs.end());
  441. std::vector<Str> positional_overrided_keys;
  442. if(!fn->starredArg.empty()){
  443. pkpy::List vargs; // handle *args
  444. while(i < args.size()) vargs.push_back(args[i++]);
  445. locals.emplace(fn->starredArg, PyTuple(std::move(vargs)));
  446. }else{
  447. for(const auto& key : fn->kwArgsOrder){
  448. if(i < args.size()){
  449. locals[key] = args[i++];
  450. positional_overrided_keys.push_back(key);
  451. }else{
  452. break;
  453. }
  454. }
  455. if(i < args.size()) TypeError("too many arguments");
  456. }
  457. for(int i=0; i<kwargs.size(); i+=2){
  458. const Str& key = PyStr_AS_C(kwargs[i]);
  459. if(!fn->kwArgs.contains(key)){
  460. TypeError(key.escape(true) + " is an invalid keyword argument for " + fn->name + "()");
  461. }
  462. const PyVar& val = kwargs[i+1];
  463. if(!positional_overrided_keys.empty()){
  464. auto it = std::find(positional_overrided_keys.begin(), positional_overrided_keys.end(), key);
  465. if(it != positional_overrided_keys.end()){
  466. TypeError("multiple values for argument '" + key + "'");
  467. }
  468. }
  469. locals[key] = val;
  470. }
  471. PyVar* it_m = (*callable)->attribs.try_get(__module__);
  472. PyVar _module = it_m != nullptr ? *it_m : top_frame()->_module;
  473. if(opCall){
  474. _new_frame(fn->code, _module, _locals);
  475. return _py_op_call;
  476. }
  477. return _exec(fn->code, _module, _locals);
  478. }
  479. TypeError("'" + OBJ_TP_NAME(*callable) + "' object is not callable");
  480. return None;
  481. }
  482. // repl mode is only for setting `frame->id` to 0
  483. PyVarOrNull exec(Str source, Str filename, CompileMode mode, PyVar _module=nullptr){
  484. if(_module == nullptr) _module = _main;
  485. try {
  486. CodeObject_ code = compile(source, filename, mode);
  487. return _exec(code, _module, pkpy::make_shared<pkpy::NameDict>());
  488. }catch (const pkpy::Exception& e){
  489. *_stderr << e.summary() << '\n';
  490. }
  491. catch (const std::exception& e) {
  492. *_stderr << "A std::exception occurred! It may be a bug, please report it!!\n";
  493. *_stderr << e.what() << '\n';
  494. }
  495. callstack = {};
  496. return nullptr;
  497. }
  498. template<typename ...Args>
  499. Frame* _new_frame(Args&&... args){
  500. if(callstack.size() > maxRecursionDepth){
  501. _error("RecursionError", "maximum recursion depth exceeded");
  502. }
  503. callstack.emplace(std::make_unique<Frame>(std::forward<Args>(args)...));
  504. return callstack.top().get();
  505. }
  506. template<typename ...Args>
  507. PyVar _exec(Args&&... args){
  508. Frame* frame = _new_frame(std::forward<Args>(args)...);
  509. i64 base_id = frame->id;
  510. PyVar ret = nullptr;
  511. bool need_raise = false;
  512. while(true){
  513. if(frame->id < base_id) UNREACHABLE();
  514. try{
  515. if(need_raise){ need_raise = false; _raise(); }
  516. ret = run_frame(frame);
  517. if(ret != _py_op_call){
  518. if(frame->id == base_id){ // [ frameBase<- ]
  519. callstack.pop();
  520. return ret;
  521. }else{
  522. callstack.pop();
  523. frame = callstack.top().get();
  524. frame->push(ret);
  525. }
  526. }else{
  527. frame = callstack.top().get(); // [ frameBase, newFrame<- ]
  528. }
  529. }catch(HandledException& e){
  530. continue;
  531. }catch(UnhandledException& e){
  532. PyVar obj = frame->pop();
  533. pkpy::Exception& _e = PyException_AS_C(obj);
  534. _e.st_push(frame->snapshot());
  535. callstack.pop();
  536. if(callstack.empty()) throw _e;
  537. frame = callstack.top().get();
  538. frame->push(obj);
  539. if(frame->id < base_id) throw ToBeRaisedException();
  540. need_raise = true;
  541. }catch(ToBeRaisedException& e){
  542. need_raise = true;
  543. }
  544. }
  545. }
  546. PyVar new_type_object(PyVar mod, Str name, PyVar base){
  547. if(!base->is_type(tp_type)) UNREACHABLE();
  548. PyVar obj = pkpy::make_shared<PyObject, Py_<Dummy>>(tp_type, DUMMY_VAL);
  549. setattr(obj, __base__, base);
  550. Str fullName = name;
  551. if(mod != builtins) fullName = OBJ_NAME(mod) + "." + name;
  552. setattr(obj, __name__, PyStr(fullName));
  553. setattr(mod, name, obj);
  554. return obj;
  555. }
  556. PyVar _new_type_object(Str name, PyVar base=nullptr) {
  557. if(base == nullptr) base = tp_object;
  558. PyVar obj = pkpy::make_shared<PyObject, Py_<Dummy>>(tp_type, DUMMY_VAL);
  559. setattr(obj, __base__, base);
  560. _types[name] = obj;
  561. return obj;
  562. }
  563. template<typename T>
  564. inline PyVar new_object(PyVar type, T _value) {
  565. if(!type->is_type(tp_type)) UNREACHABLE();
  566. if constexpr (std::is_same_v<T, Dummy>) return pkpy::make_shared<PyObject, Py_<T>>(type, _value);
  567. std::vector<int*>& pool = _obj_pool[tid<T>()];
  568. if(pool.empty()) return pkpy::make_shared<PyObject, Py_<T>>(type, _value);
  569. int* counter = pool.back(); pool.pop_back();
  570. *counter = 1;
  571. Py_<T>* obj = (Py_<T>*)(counter + 1);
  572. obj->_value = std::move(_value);
  573. obj->attribs.clear();
  574. return PyVar(counter);
  575. }
  576. template<typename T, typename... Args>
  577. inline PyVar new_object(Args&&... args) {
  578. return new_object(T::_type(this), T(std::forward<Args>(args)...));
  579. }
  580. PyVar new_module(const Str& name) {
  581. PyVar obj = new_object(tp_module, DUMMY_VAL);
  582. setattr(obj, __name__, PyStr(name));
  583. _modules[name] = obj;
  584. return obj;
  585. }
  586. PyVarOrNull getattr(const PyVar& obj, const Str& name, bool throw_err=true) {
  587. pkpy::NameDict::iterator it;
  588. PyObject* cls;
  589. if(obj->is_type(tp_super)){
  590. const PyVar* root = &obj;
  591. int depth = 1;
  592. while(true){
  593. root = &OBJ_GET(PyVar, *root);
  594. if(!(*root)->is_type(tp_super)) break;
  595. depth++;
  596. }
  597. cls = (*root)->type.get();
  598. for(int i=0; i<depth; i++) cls = cls->attribs[__base__].get();
  599. it = (*root)->attribs.find(name);
  600. if(it != (*root)->attribs.end()) return it->second;
  601. }else{
  602. it = obj->attribs.find(name);
  603. if(it != obj->attribs.end()) return it->second;
  604. cls = obj->type.get();
  605. }
  606. while(cls != None.get()) {
  607. it = cls->attribs.find(name);
  608. if(it != cls->attribs.end()){
  609. PyVar valueFromCls = it->second;
  610. if(valueFromCls->is_type(tp_function) || valueFromCls->is_type(tp_native_function)){
  611. return PyBoundMethod({obj, std::move(valueFromCls)});
  612. }else{
  613. return valueFromCls;
  614. }
  615. }
  616. cls = cls->attribs[__base__].get();
  617. }
  618. if(throw_err) AttributeError(obj, name);
  619. return nullptr;
  620. }
  621. template<typename T>
  622. inline void setattr(PyVar& obj, const Str& name, T&& value) {
  623. PyObject* p = obj.get();
  624. while(p->is_type(tp_super)) p = static_cast<PyVar*>(p->value())->get();
  625. p->attribs[name] = std::forward<T>(value);
  626. }
  627. template<int ARGC>
  628. void bind_method(PyVar obj, Str funcName, NativeFuncRaw fn) {
  629. check_type(obj, tp_type);
  630. setattr(obj, funcName, PyNativeFunc(pkpy::NativeFunc(fn, ARGC, true)));
  631. }
  632. template<int ARGC>
  633. void bind_func(PyVar obj, Str funcName, NativeFuncRaw fn) {
  634. setattr(obj, funcName, PyNativeFunc(pkpy::NativeFunc(fn, ARGC, false)));
  635. }
  636. template<int ARGC>
  637. void bind_method(Str typeName, Str funcName, NativeFuncRaw fn) {
  638. bind_method<ARGC>(_types[typeName], funcName, fn);
  639. }
  640. template<int ARGC>
  641. void bind_static_method(Str typeName, Str funcName, NativeFuncRaw fn) {
  642. bind_func<ARGC>(_types[typeName], funcName, fn);
  643. }
  644. template<int ARGC>
  645. void _bind_methods(std::vector<Str> typeNames, Str funcName, NativeFuncRaw fn) {
  646. for(auto& typeName : typeNames) bind_method<ARGC>(typeName, funcName, fn);
  647. }
  648. template<int ARGC>
  649. void bind_builtin_func(Str funcName, NativeFuncRaw fn) {
  650. bind_func<ARGC>(builtins, funcName, fn);
  651. }
  652. inline f64 num_to_float(const PyVar& obj){
  653. if (obj->is_type(tp_int)){
  654. return (f64)PyInt_AS_C(obj);
  655. }else if(obj->is_type(tp_float)){
  656. return PyFloat_AS_C(obj);
  657. }
  658. TypeError("expected 'int' or 'float', got " + OBJ_TP_NAME(obj).escape(true));
  659. return 0;
  660. }
  661. PyVar num_negated(const PyVar& obj){
  662. if (obj->is_type(tp_int)){
  663. return PyInt(-PyInt_AS_C(obj));
  664. }else if(obj->is_type(tp_float)){
  665. return PyFloat(-PyFloat_AS_C(obj));
  666. }
  667. TypeError("unsupported operand type(s) for -");
  668. return nullptr;
  669. }
  670. int normalized_index(int index, int size){
  671. if(index < 0) index += size;
  672. if(index < 0 || index >= size){
  673. IndexError(std::to_string(index) + " not in [0, " + std::to_string(size) + ")");
  674. }
  675. return index;
  676. }
  677. Str disassemble(CodeObject_ co){
  678. std::vector<int> jumpTargets;
  679. for(auto byte : co->codes){
  680. if(byte.op == OP_JUMP_ABSOLUTE || byte.op == OP_SAFE_JUMP_ABSOLUTE || byte.op == OP_POP_JUMP_IF_FALSE){
  681. jumpTargets.push_back(byte.arg);
  682. }
  683. }
  684. StrStream ss;
  685. ss << std::string(54, '-') << '\n';
  686. ss << co->name << ":\n";
  687. int prev_line = -1;
  688. for(int i=0; i<co->codes.size(); i++){
  689. const Bytecode& byte = co->codes[i];
  690. Str line = std::to_string(byte.line);
  691. if(byte.line == prev_line) line = "";
  692. else{
  693. if(prev_line != -1) ss << "\n";
  694. prev_line = byte.line;
  695. }
  696. std::string pointer;
  697. if(std::find(jumpTargets.begin(), jumpTargets.end(), i) != jumpTargets.end()){
  698. pointer = "-> ";
  699. }else{
  700. pointer = " ";
  701. }
  702. ss << pad(line, 8) << pointer << pad(std::to_string(i), 3);
  703. ss << " " << pad(OP_NAMES[byte.op], 20) << " ";
  704. // ss << pad(byte.arg == -1 ? "" : std::to_string(byte.arg), 5);
  705. std::string argStr = byte.arg == -1 ? "" : std::to_string(byte.arg);
  706. if(byte.op == OP_LOAD_CONST){
  707. argStr += " (" + PyStr_AS_C(asRepr(co->consts[byte.arg])) + ")";
  708. }
  709. if(byte.op == OP_LOAD_NAME_REF || byte.op == OP_LOAD_NAME || byte.op == OP_RAISE){
  710. argStr += " (" + co->names[byte.arg].first.escape(true) + ")";
  711. }
  712. ss << pad(argStr, 20); // may overflow
  713. ss << co->blocks[byte.block].to_string();
  714. if(i != co->codes.size() - 1) ss << '\n';
  715. }
  716. StrStream consts;
  717. consts << "co_consts: ";
  718. consts << PyStr_AS_C(asRepr(PyList(co->consts)));
  719. StrStream names;
  720. names << "co_names: ";
  721. pkpy::List list;
  722. for(int i=0; i<co->names.size(); i++){
  723. list.push_back(PyStr(co->names[i].first));
  724. }
  725. names << PyStr_AS_C(asRepr(PyList(list)));
  726. ss << '\n' << consts.str() << '\n' << names.str() << '\n';
  727. for(int i=0; i<co->consts.size(); i++){
  728. PyVar obj = co->consts[i];
  729. if(obj->is_type(tp_function)){
  730. const auto& f = PyFunction_AS_C(obj);
  731. ss << disassemble(f->code);
  732. }
  733. }
  734. return Str(ss.str());
  735. }
  736. // for quick access
  737. PyVar tp_object, tp_type, tp_int, tp_float, tp_bool, tp_str;
  738. PyVar tp_list, tp_tuple;
  739. PyVar tp_function, tp_native_function, tp_native_iterator, tp_bound_method;
  740. PyVar tp_slice, tp_range, tp_module, tp_ref;
  741. PyVar tp_super, tp_exception;
  742. template<typename P>
  743. inline PyVarRef PyRef(P&& value) {
  744. static_assert(std::is_base_of<BaseRef, std::remove_reference_t<P>>::value, "P should derive from BaseRef");
  745. return new_object(tp_ref, std::forward<P>(value));
  746. }
  747. inline const BaseRef* PyRef_AS_C(const PyVar& obj)
  748. {
  749. if(!obj->is_type(tp_ref)) TypeError("expected an l-value");
  750. return (const BaseRef*)(obj->value());
  751. }
  752. inline const Str& PyStr_AS_C(const PyVar& obj) {
  753. check_type(obj, tp_str);
  754. return OBJ_GET(Str, obj);
  755. }
  756. inline PyVar PyStr(const Str& value) {
  757. if(value.size() == 1){
  758. char c = value.c_str()[0];
  759. if(c > 0) return _ascii_str_pool[(int)c];
  760. }
  761. return new_object(tp_str, value);
  762. }
  763. DEF_NATIVE(Int, i64, tp_int)
  764. DEF_NATIVE(Float, f64, tp_float)
  765. DEF_NATIVE(List, pkpy::List, tp_list)
  766. DEF_NATIVE(Tuple, pkpy::Tuple, tp_tuple)
  767. DEF_NATIVE(Function, pkpy::Function_, tp_function)
  768. DEF_NATIVE(NativeFunc, pkpy::NativeFunc, tp_native_function)
  769. DEF_NATIVE(Iter, pkpy::shared_ptr<BaseIter>, tp_native_iterator)
  770. DEF_NATIVE(BoundMethod, pkpy::BoundMethod, tp_bound_method)
  771. DEF_NATIVE(Range, pkpy::Range, tp_range)
  772. DEF_NATIVE(Slice, pkpy::Slice, tp_slice)
  773. DEF_NATIVE(Exception, pkpy::Exception, tp_exception)
  774. // there is only one True/False, so no need to copy them!
  775. inline bool PyBool_AS_C(const PyVar& obj){return obj == True;}
  776. inline const PyVar& PyBool(bool value){return value ? True : False;}
  777. void init_builtin_types(){
  778. tp_object = pkpy::make_shared<PyObject, Py_<Dummy>>(nullptr, DUMMY_VAL);
  779. tp_type = pkpy::make_shared<PyObject, Py_<Dummy>>(nullptr, DUMMY_VAL);
  780. _types["object"] = tp_object;
  781. _types["type"] = tp_type;
  782. tp_bool = _new_type_object("bool");
  783. tp_int = _new_type_object("int");
  784. tp_float = _new_type_object("float");
  785. tp_str = _new_type_object("str");
  786. tp_list = _new_type_object("list");
  787. tp_tuple = _new_type_object("tuple");
  788. tp_slice = _new_type_object("slice");
  789. tp_range = _new_type_object("range");
  790. tp_module = _new_type_object("module");
  791. tp_ref = _new_type_object("_ref");
  792. tp_function = _new_type_object("function");
  793. tp_native_function = _new_type_object("native_function");
  794. tp_native_iterator = _new_type_object("native_iterator");
  795. tp_bound_method = _new_type_object("bound_method");
  796. tp_super = _new_type_object("super");
  797. tp_exception = _new_type_object("Exception");
  798. this->None = new_object(_new_type_object("NoneType"), DUMMY_VAL);
  799. this->Ellipsis = new_object(_new_type_object("ellipsis"), DUMMY_VAL);
  800. this->True = new_object(tp_bool, true);
  801. this->False = new_object(tp_bool, false);
  802. this->builtins = new_module("builtins");
  803. this->_main = new_module("__main__");
  804. this->_py_op_call = new_object(_new_type_object("_internal"), DUMMY_VAL);
  805. setattr(tp_type, __base__, tp_object);
  806. tp_type->type = tp_type;
  807. setattr(tp_object, __base__, None);
  808. tp_object->type = tp_type;
  809. for (auto& [name, type] : _types) {
  810. setattr(type, __name__, PyStr(name));
  811. }
  812. std::vector<Str> publicTypes = {"type", "object", "bool", "int", "float", "str", "list", "tuple", "range"};
  813. for (auto& name : publicTypes) {
  814. setattr(builtins, name, _types[name]);
  815. }
  816. }
  817. i64 hash(const PyVar& obj){
  818. if (obj->is_type(tp_int)) return PyInt_AS_C(obj);
  819. if (obj->is_type(tp_bool)) return PyBool_AS_C(obj) ? 1 : 0;
  820. if (obj->is_type(tp_float)){
  821. f64 val = PyFloat_AS_C(obj);
  822. return (i64)std::hash<f64>()(val);
  823. }
  824. if (obj->is_type(tp_str)) return PyStr_AS_C(obj).hash();
  825. if (obj->is_type(tp_type)) return (i64)obj.get();
  826. if (obj->is_type(tp_tuple)) {
  827. i64 x = 1000003;
  828. const pkpy::Tuple& items = PyTuple_AS_C(obj);
  829. for (int i=0; i<items.size(); i++) {
  830. i64 y = hash(items[i]);
  831. x = x ^ (y + 0x9e3779b9 + (x << 6) + (x >> 2)); // recommended by Github Copilot
  832. }
  833. return x;
  834. }
  835. TypeError("unhashable type: " + OBJ_TP_NAME(obj).escape(true));
  836. return 0;
  837. }
  838. /***** Error Reporter *****/
  839. private:
  840. void _error(const Str& name, const Str& msg){
  841. _error(pkpy::Exception(name, msg));
  842. }
  843. void _error(pkpy::Exception e){
  844. if(callstack.empty()){
  845. e.is_re = false;
  846. throw e;
  847. }
  848. top_frame()->push(PyException(e));
  849. _raise();
  850. }
  851. void _raise(){
  852. bool ok = top_frame()->jump_to_exception_handler();
  853. if(ok) throw HandledException();
  854. else throw UnhandledException();
  855. }
  856. public:
  857. void NotImplementedError(){ _error("NotImplementedError", ""); }
  858. void TypeError(const Str& msg){ _error("TypeError", msg); }
  859. void ZeroDivisionError(){ _error("ZeroDivisionError", "division by zero"); }
  860. void IndexError(const Str& msg){ _error("IndexError", msg); }
  861. void ValueError(const Str& msg){ _error("ValueError", msg); }
  862. void NameError(const Str& name){ _error("NameError", "name " + name.escape(true) + " is not defined"); }
  863. void AttributeError(PyVar obj, const Str& name){
  864. _error("AttributeError", "type " + OBJ_TP_NAME(obj).escape(true) + " has no attribute " + name.escape(true));
  865. }
  866. inline void check_type(const PyVar& obj, const PyVar& type){
  867. if(!obj->is_type(type)) TypeError("expected " + OBJ_NAME(type).escape(true) + ", but got " + OBJ_TP_NAME(obj).escape(true));
  868. }
  869. template<typename T>
  870. PyVar register_class(PyVar mod){
  871. PyVar type = new_type_object(mod, T::_name(), tp_object);
  872. if(OBJ_NAME(mod) != T::_mod()) UNREACHABLE();
  873. T::_register(this, mod, type);
  874. return type;
  875. }
  876. template<typename T>
  877. inline T& py_cast(const PyVar& obj){
  878. check_type(obj, T::_type(this));
  879. return OBJ_GET(T, obj);
  880. }
  881. ~VM() {
  882. if(!use_stdio){
  883. delete _stdout;
  884. delete _stderr;
  885. }
  886. }
  887. CodeObject_ compile(Str source, Str filename, CompileMode mode);
  888. };
  889. /***** Pointers' Impl *****/
  890. PyVar NameRef::get(VM* vm, Frame* frame) const{
  891. PyVar* val;
  892. val = frame->f_locals().try_get(name());
  893. if(val) return *val;
  894. val = frame->f_globals().try_get(name());
  895. if(val) return *val;
  896. val = vm->builtins->attribs.try_get(name());
  897. if(val) return *val;
  898. vm->NameError(name());
  899. return nullptr;
  900. }
  901. void NameRef::set(VM* vm, Frame* frame, PyVar val) const{
  902. switch(scope()) {
  903. case NAME_LOCAL: frame->f_locals()[name()] = std::move(val); break;
  904. case NAME_GLOBAL:
  905. {
  906. PyVar* existing = frame->f_locals().try_get(name());
  907. if(existing != nullptr){
  908. *existing = std::move(val);
  909. }else{
  910. frame->f_globals()[name()] = std::move(val);
  911. }
  912. } break;
  913. default: UNREACHABLE();
  914. }
  915. }
  916. void NameRef::del(VM* vm, Frame* frame) const{
  917. switch(scope()) {
  918. case NAME_LOCAL: {
  919. if(frame->f_locals().contains(name())){
  920. frame->f_locals().erase(name());
  921. }else{
  922. vm->NameError(name());
  923. }
  924. } break;
  925. case NAME_GLOBAL:
  926. {
  927. if(frame->f_locals().contains(name())){
  928. frame->f_locals().erase(name());
  929. }else{
  930. if(frame->f_globals().contains(name())){
  931. frame->f_globals().erase(name());
  932. }else{
  933. vm->NameError(name());
  934. }
  935. }
  936. } break;
  937. default: UNREACHABLE();
  938. }
  939. }
  940. PyVar AttrRef::get(VM* vm, Frame* frame) const{
  941. return vm->getattr(obj, attr.name());
  942. }
  943. void AttrRef::set(VM* vm, Frame* frame, PyVar val) const{
  944. vm->setattr(obj, attr.name(), val);
  945. }
  946. void AttrRef::del(VM* vm, Frame* frame) const{
  947. vm->TypeError("cannot delete attribute");
  948. }
  949. PyVar IndexRef::get(VM* vm, Frame* frame) const{
  950. return vm->call(obj, __getitem__, pkpy::one_arg(index));
  951. }
  952. void IndexRef::set(VM* vm, Frame* frame, PyVar val) const{
  953. vm->call(obj, __setitem__, pkpy::two_args(index, val));
  954. }
  955. void IndexRef::del(VM* vm, Frame* frame) const{
  956. vm->call(obj, __delitem__, pkpy::one_arg(index));
  957. }
  958. PyVar TupleRef::get(VM* vm, Frame* frame) const{
  959. pkpy::Tuple args(objs.size());
  960. for (int i = 0; i < objs.size(); i++) {
  961. args[i] = vm->PyRef_AS_C(objs[i])->get(vm, frame);
  962. }
  963. return vm->PyTuple(std::move(args));
  964. }
  965. void TupleRef::set(VM* vm, Frame* frame, PyVar val) const{
  966. #define TUPLE_REF_SET() \
  967. if(args.size() > objs.size()) vm->ValueError("too many values to unpack"); \
  968. if(args.size() < objs.size()) vm->ValueError("not enough values to unpack"); \
  969. for (int i = 0; i < objs.size(); i++) vm->PyRef_AS_C(objs[i])->set(vm, frame, args[i]);
  970. if(val->is_type(vm->tp_tuple)){
  971. const pkpy::Tuple& args = OBJ_GET(pkpy::Tuple, val);
  972. TUPLE_REF_SET()
  973. }else if(val->is_type(vm->tp_list)){
  974. const pkpy::List& args = OBJ_GET(pkpy::List, val);
  975. TUPLE_REF_SET()
  976. }else{
  977. vm->TypeError("only tuple or list can be unpacked");
  978. }
  979. #undef TUPLE_REF_SET
  980. }
  981. void TupleRef::del(VM* vm, Frame* frame) const{
  982. for(int i=0; i<objs.size(); i++) vm->PyRef_AS_C(objs[i])->del(vm, frame);
  983. }
  984. /***** Frame's Impl *****/
  985. inline void Frame::try_deref(VM* vm, PyVar& v){
  986. if(v->is_type(vm->tp_ref)) v = vm->PyRef_AS_C(v)->get(vm, this);
  987. }
  988. PyVar pkpy::NativeFunc::operator()(VM* vm, const pkpy::Args& args) const{
  989. int args_size = args.size() - (int)method; // remove self
  990. if(argc != -1 && args_size != argc) {
  991. vm->TypeError("expected " + std::to_string(argc) + " arguments, but got " + std::to_string(args_size));
  992. }
  993. return f(vm, args);
  994. }