expr.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. #pragma once
  2. #include "codeobject.h"
  3. #include "common.h"
  4. #include "lexer.h"
  5. #include "error.h"
  6. #include "ceval.h"
  7. #include "str.h"
  8. namespace pkpy{
  9. struct CodeEmitContext;
  10. struct Expr;
  11. typedef std::unique_ptr<Expr> Expr_;
  12. struct Expr{
  13. int line = 0;
  14. virtual ~Expr() = default;
  15. virtual void emit(CodeEmitContext* ctx) = 0;
  16. virtual std::string str() const = 0;
  17. virtual bool is_starred() const { return false; }
  18. virtual bool is_literal() const { return false; }
  19. virtual bool is_json_object() const { return false; }
  20. virtual bool is_attrib() const { return false; }
  21. virtual bool is_compare() const { return false; }
  22. // for OP_DELETE_XXX
  23. [[nodiscard]] virtual bool emit_del(CodeEmitContext* ctx) { return false; }
  24. // for OP_STORE_XXX
  25. [[nodiscard]] virtual bool emit_store(CodeEmitContext* ctx) { return false; }
  26. };
  27. struct CodeEmitContext{
  28. VM* vm;
  29. CodeObject_ co;
  30. stack<Expr_> s_expr;
  31. int level;
  32. CodeEmitContext(VM* vm, CodeObject_ co, int level): vm(vm), co(co), level(level) {}
  33. int curr_block_i = 0;
  34. bool is_compiling_class = false;
  35. int for_loop_depth = 0;
  36. bool is_curr_block_loop() const {
  37. return co->blocks[curr_block_i].type == FOR_LOOP || co->blocks[curr_block_i].type == WHILE_LOOP;
  38. }
  39. void enter_block(CodeBlockType type){
  40. if(type == FOR_LOOP) for_loop_depth++;
  41. co->blocks.push_back(CodeBlock(
  42. type, curr_block_i, for_loop_depth, (int)co->codes.size()
  43. ));
  44. curr_block_i = co->blocks.size()-1;
  45. }
  46. void exit_block(){
  47. if(co->blocks[curr_block_i].type == FOR_LOOP) for_loop_depth--;
  48. co->blocks[curr_block_i].end = co->codes.size();
  49. curr_block_i = co->blocks[curr_block_i].parent;
  50. if(curr_block_i < 0) FATAL_ERROR();
  51. }
  52. // clear the expression stack and generate bytecode
  53. void emit_expr(){
  54. if(s_expr.size() != 1){
  55. throw std::runtime_error("s_expr.size() != 1\n" + _log_s_expr());
  56. }
  57. Expr_ expr = s_expr.popx();
  58. expr->emit(this);
  59. }
  60. std::string _log_s_expr(){
  61. std::stringstream ss;
  62. for(auto& e: s_expr.data()) ss << e->str() << " ";
  63. return ss.str();
  64. }
  65. int emit(Opcode opcode, int arg, int line) {
  66. co->codes.push_back(
  67. Bytecode{(uint16_t)opcode, (uint16_t)curr_block_i, arg}
  68. );
  69. co->lines.push_back(line);
  70. int i = co->codes.size() - 1;
  71. if(line==BC_KEEPLINE){
  72. if(i>=1) co->lines[i] = co->lines[i-1];
  73. else co->lines[i] = 1;
  74. }
  75. return i;
  76. }
  77. void patch_jump(int index) {
  78. int target = co->codes.size();
  79. co->codes[index].arg = target;
  80. }
  81. bool add_label(StrName name){
  82. if(co->labels.contains(name)) return false;
  83. co->labels.set(name, co->codes.size());
  84. return true;
  85. }
  86. int add_varname(StrName name){
  87. int index = co->varnames_inv.try_get(name);
  88. if(index >= 0) return index;
  89. co->varnames.push_back(name);
  90. index = co->varnames.size() - 1;
  91. co->varnames_inv.set(name, index);
  92. return index;
  93. }
  94. int add_const(PyObject* v){
  95. // simple deduplication, only works for int/float
  96. for(int i=0; i<co->consts.size(); i++){
  97. if(co->consts[i] == v) return i;
  98. }
  99. co->consts.push_back(v);
  100. return co->consts.size() - 1;
  101. }
  102. int add_func_decl(FuncDecl_ decl){
  103. co->func_decls.push_back(decl);
  104. return co->func_decls.size() - 1;
  105. }
  106. };
  107. struct NameExpr: Expr{
  108. StrName name;
  109. NameScope scope;
  110. NameExpr(StrName name, NameScope scope): name(name), scope(scope) {}
  111. std::string str() const override { return fmt("Name(", name.escape(), ")"); }
  112. void emit(CodeEmitContext* ctx) override {
  113. int index = ctx->co->varnames_inv.try_get(name);
  114. if(scope == NAME_LOCAL && index >= 0){
  115. ctx->emit(OP_LOAD_FAST, index, line);
  116. }else{
  117. Opcode op = ctx->level <= 1 ? OP_LOAD_GLOBAL : OP_LOAD_NONLOCAL;
  118. // we cannot determine the scope when calling exec()/eval()
  119. if(scope == NAME_GLOBAL_UNKNOWN) op = OP_LOAD_NAME;
  120. ctx->emit(op, StrName(name).index, line);
  121. }
  122. }
  123. bool emit_del(CodeEmitContext* ctx) override {
  124. switch(scope){
  125. case NAME_LOCAL:
  126. ctx->emit(OP_DELETE_FAST, ctx->add_varname(name), line);
  127. break;
  128. case NAME_GLOBAL:
  129. ctx->emit(OP_DELETE_GLOBAL, StrName(name).index, line);
  130. break;
  131. case NAME_GLOBAL_UNKNOWN:
  132. ctx->emit(OP_DELETE_NAME, StrName(name).index, line);
  133. break;
  134. default: FATAL_ERROR(); break;
  135. }
  136. return true;
  137. }
  138. bool emit_store(CodeEmitContext* ctx) override {
  139. if(ctx->is_compiling_class){
  140. int index = StrName(name).index;
  141. ctx->emit(OP_STORE_CLASS_ATTR, index, line);
  142. return true;
  143. }
  144. switch(scope){
  145. case NAME_LOCAL:
  146. ctx->emit(OP_STORE_FAST, ctx->add_varname(name), line);
  147. break;
  148. case NAME_GLOBAL:
  149. ctx->emit(OP_STORE_GLOBAL, StrName(name).index, line);
  150. break;
  151. case NAME_GLOBAL_UNKNOWN:
  152. ctx->emit(OP_STORE_NAME, StrName(name).index, line);
  153. break;
  154. default: FATAL_ERROR(); break;
  155. }
  156. return true;
  157. }
  158. };
  159. struct StarredExpr: Expr{
  160. Expr_ child;
  161. StarredExpr(Expr_&& child): child(std::move(child)) {}
  162. std::string str() const override { return "Starred()"; }
  163. bool is_starred() const override { return true; }
  164. void emit(CodeEmitContext* ctx) override {
  165. child->emit(ctx);
  166. ctx->emit(OP_UNPACK_UNLIMITED, BC_NOARG, line);
  167. }
  168. bool emit_store(CodeEmitContext* ctx) override {
  169. // simply proxy to child
  170. return child->emit_store(ctx);
  171. }
  172. };
  173. struct NotExpr: Expr{
  174. Expr_ child;
  175. NotExpr(Expr_&& child): child(std::move(child)) {}
  176. std::string str() const override { return "Not()"; }
  177. void emit(CodeEmitContext* ctx) override {
  178. child->emit(ctx);
  179. ctx->emit(OP_UNARY_NOT, BC_NOARG, line);
  180. }
  181. };
  182. struct AndExpr: Expr{
  183. Expr_ lhs;
  184. Expr_ rhs;
  185. std::string str() const override { return "And()"; }
  186. void emit(CodeEmitContext* ctx) override {
  187. lhs->emit(ctx);
  188. int patch = ctx->emit(OP_JUMP_IF_FALSE_OR_POP, BC_NOARG, line);
  189. rhs->emit(ctx);
  190. ctx->patch_jump(patch);
  191. }
  192. };
  193. struct OrExpr: Expr{
  194. Expr_ lhs;
  195. Expr_ rhs;
  196. std::string str() const override { return "Or()"; }
  197. void emit(CodeEmitContext* ctx) override {
  198. lhs->emit(ctx);
  199. int patch = ctx->emit(OP_JUMP_IF_TRUE_OR_POP, BC_NOARG, line);
  200. rhs->emit(ctx);
  201. ctx->patch_jump(patch);
  202. }
  203. };
  204. // [None, True, False, ...]
  205. struct Literal0Expr: Expr{
  206. TokenIndex token;
  207. Literal0Expr(TokenIndex token): token(token) {}
  208. std::string str() const override { return TK_STR(token); }
  209. void emit(CodeEmitContext* ctx) override {
  210. switch (token) {
  211. case TK("None"): ctx->emit(OP_LOAD_NONE, BC_NOARG, line); break;
  212. case TK("True"): ctx->emit(OP_LOAD_TRUE, BC_NOARG, line); break;
  213. case TK("False"): ctx->emit(OP_LOAD_FALSE, BC_NOARG, line); break;
  214. case TK("..."): ctx->emit(OP_LOAD_ELLIPSIS, BC_NOARG, line); break;
  215. default: FATAL_ERROR();
  216. }
  217. }
  218. bool is_json_object() const override { return true; }
  219. };
  220. // @num, @str which needs to invoke OP_LOAD_CONST
  221. struct LiteralExpr: Expr{
  222. TokenValue value;
  223. LiteralExpr(TokenValue value): value(value) {}
  224. std::string str() const override {
  225. if(std::holds_alternative<i64>(value)){
  226. return std::to_string(std::get<i64>(value));
  227. }
  228. if(std::holds_alternative<f64>(value)){
  229. return std::to_string(std::get<f64>(value));
  230. }
  231. if(std::holds_alternative<Str>(value)){
  232. Str s = std::get<Str>(value).escape();
  233. return s.str();
  234. }
  235. FATAL_ERROR();
  236. }
  237. void emit(CodeEmitContext* ctx) override {
  238. VM* vm = ctx->vm;
  239. PyObject* obj = nullptr;
  240. if(std::holds_alternative<i64>(value)){
  241. i64 _val = std::get<i64>(value);
  242. if(_val >= INT16_MIN && _val <= INT16_MAX){
  243. ctx->emit(OP_LOAD_INTEGER, (int)_val, line);
  244. return;
  245. }
  246. obj = VAR(_val);
  247. }
  248. if(std::holds_alternative<f64>(value)){
  249. obj = VAR(std::get<f64>(value));
  250. }
  251. if(std::holds_alternative<Str>(value)){
  252. obj = VAR(std::get<Str>(value));
  253. }
  254. if(obj == nullptr) FATAL_ERROR();
  255. ctx->emit(OP_LOAD_CONST, ctx->add_const(obj), line);
  256. }
  257. bool is_literal() const override { return true; }
  258. bool is_json_object() const override { return true; }
  259. };
  260. struct NegatedExpr: Expr{
  261. Expr_ child;
  262. NegatedExpr(Expr_&& child): child(std::move(child)) {}
  263. std::string str() const override { return "Negated()"; }
  264. void emit(CodeEmitContext* ctx) override {
  265. VM* vm = ctx->vm;
  266. // if child is a int of float, do constant folding
  267. if(child->is_literal()){
  268. LiteralExpr* lit = static_cast<LiteralExpr*>(child.get());
  269. if(std::holds_alternative<i64>(lit->value)){
  270. i64 _val = -std::get<i64>(lit->value);
  271. if(_val >= INT16_MIN && _val <= INT16_MAX){
  272. ctx->emit(OP_LOAD_INTEGER, (int)_val, line);
  273. }else{
  274. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(_val)), line);
  275. }
  276. return;
  277. }
  278. if(std::holds_alternative<f64>(lit->value)){
  279. PyObject* obj = VAR(-std::get<f64>(lit->value));
  280. ctx->emit(OP_LOAD_CONST, ctx->add_const(obj), line);
  281. return;
  282. }
  283. }
  284. child->emit(ctx);
  285. ctx->emit(OP_UNARY_NEGATIVE, BC_NOARG, line);
  286. }
  287. bool is_json_object() const override {
  288. return child->is_literal();
  289. }
  290. };
  291. struct SliceExpr: Expr{
  292. Expr_ start;
  293. Expr_ stop;
  294. Expr_ step;
  295. std::string str() const override { return "Slice()"; }
  296. void emit(CodeEmitContext* ctx) override {
  297. if(start){
  298. start->emit(ctx);
  299. }else{
  300. ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
  301. }
  302. if(stop){
  303. stop->emit(ctx);
  304. }else{
  305. ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
  306. }
  307. if(step){
  308. step->emit(ctx);
  309. }else{
  310. ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
  311. }
  312. ctx->emit(OP_BUILD_SLICE, BC_NOARG, line);
  313. }
  314. };
  315. struct DictItemExpr: Expr{
  316. Expr_ key;
  317. Expr_ value;
  318. std::string str() const override { return "DictItem()"; }
  319. void emit(CodeEmitContext* ctx) override {
  320. value->emit(ctx);
  321. key->emit(ctx); // reverse order
  322. ctx->emit(OP_BUILD_TUPLE, 2, line);
  323. }
  324. };
  325. struct SequenceExpr: Expr{
  326. std::vector<Expr_> items;
  327. SequenceExpr(std::vector<Expr_>&& items): items(std::move(items)) {}
  328. virtual Opcode opcode() const = 0;
  329. void emit(CodeEmitContext* ctx) override {
  330. for(auto& item: items) item->emit(ctx);
  331. ctx->emit(opcode(), items.size(), line);
  332. }
  333. };
  334. struct ListExpr: SequenceExpr{
  335. using SequenceExpr::SequenceExpr;
  336. std::string str() const override { return "List()"; }
  337. Opcode opcode() const override { return OP_BUILD_LIST; }
  338. bool is_json_object() const override { return true; }
  339. };
  340. struct DictExpr: SequenceExpr{
  341. using SequenceExpr::SequenceExpr;
  342. std::string str() const override { return "Dict()"; }
  343. Opcode opcode() const override { return OP_BUILD_DICT; }
  344. bool is_json_object() const override { return true; }
  345. };
  346. struct SetExpr: SequenceExpr{
  347. using SequenceExpr::SequenceExpr;
  348. std::string str() const override { return "Set()"; }
  349. Opcode opcode() const override { return OP_BUILD_SET; }
  350. };
  351. struct TupleExpr: SequenceExpr{
  352. using SequenceExpr::SequenceExpr;
  353. std::string str() const override { return "Tuple()"; }
  354. Opcode opcode() const override { return OP_BUILD_TUPLE; }
  355. bool emit_store(CodeEmitContext* ctx) override {
  356. // TOS is an iterable
  357. // items may contain StarredExpr, we should check it
  358. int starred_i = -1;
  359. for(int i=0; i<items.size(); i++){
  360. if(!items[i]->is_starred()) continue;
  361. if(starred_i == -1) starred_i = i;
  362. else return false; // multiple StarredExpr not allowed
  363. }
  364. if(starred_i == -1){
  365. Bytecode& prev = ctx->co->codes.back();
  366. if(prev.op == OP_BUILD_TUPLE && prev.arg == items.size()){
  367. // build tuple and unpack it is meaningless
  368. prev.op = OP_NO_OP;
  369. prev.arg = BC_NOARG;
  370. }else{
  371. ctx->emit(OP_UNPACK_SEQUENCE, items.size(), line);
  372. }
  373. }else{
  374. // starred assignment target must be in a tuple
  375. if(items.size() == 1) return false;
  376. // starred assignment target must be the last one (differ from cpython)
  377. if(starred_i != items.size()-1) return false;
  378. // a,*b = [1,2,3]
  379. // stack is [1,2,3] -> [1,[2,3]]
  380. ctx->emit(OP_UNPACK_EX, items.size()-1, line);
  381. }
  382. // do reverse emit
  383. for(int i=items.size()-1; i>=0; i--){
  384. bool ok = items[i]->emit_store(ctx);
  385. if(!ok) return false;
  386. }
  387. return true;
  388. }
  389. bool emit_del(CodeEmitContext* ctx) override{
  390. for(auto& e: items){
  391. bool ok = e->emit_del(ctx);
  392. if(!ok) return false;
  393. }
  394. return true;
  395. }
  396. };
  397. struct CompExpr: Expr{
  398. Expr_ expr; // loop expr
  399. Expr_ vars; // loop vars
  400. Expr_ iter; // loop iter
  401. Expr_ cond; // optional if condition
  402. virtual Opcode op0() = 0;
  403. virtual Opcode op1() = 0;
  404. void emit(CodeEmitContext* ctx){
  405. ctx->emit(op0(), 0, line);
  406. iter->emit(ctx);
  407. ctx->emit(OP_GET_ITER, BC_NOARG, BC_KEEPLINE);
  408. ctx->enter_block(FOR_LOOP);
  409. ctx->emit(OP_FOR_ITER, BC_NOARG, BC_KEEPLINE);
  410. bool ok = vars->emit_store(ctx);
  411. // this error occurs in `vars` instead of this line, but...nevermind
  412. if(!ok) FATAL_ERROR(); // TODO: raise a SyntaxError instead
  413. if(cond){
  414. cond->emit(ctx);
  415. int patch = ctx->emit(OP_POP_JUMP_IF_FALSE, BC_NOARG, BC_KEEPLINE);
  416. expr->emit(ctx);
  417. ctx->emit(op1(), BC_NOARG, BC_KEEPLINE);
  418. ctx->patch_jump(patch);
  419. }else{
  420. expr->emit(ctx);
  421. ctx->emit(op1(), BC_NOARG, BC_KEEPLINE);
  422. }
  423. ctx->emit(OP_LOOP_CONTINUE, BC_NOARG, BC_KEEPLINE);
  424. ctx->exit_block();
  425. }
  426. };
  427. struct ListCompExpr: CompExpr{
  428. Opcode op0() override { return OP_BUILD_LIST; }
  429. Opcode op1() override { return OP_LIST_APPEND; }
  430. std::string str() const override { return "ListComp()"; }
  431. };
  432. struct DictCompExpr: CompExpr{
  433. Opcode op0() override { return OP_BUILD_DICT; }
  434. Opcode op1() override { return OP_DICT_ADD; }
  435. std::string str() const override { return "DictComp()"; }
  436. };
  437. struct SetCompExpr: CompExpr{
  438. Opcode op0() override { return OP_BUILD_SET; }
  439. Opcode op1() override { return OP_SET_ADD; }
  440. std::string str() const override { return "SetComp()"; }
  441. };
  442. struct LambdaExpr: Expr{
  443. FuncDecl_ decl;
  444. std::string str() const override { return "Lambda()"; }
  445. LambdaExpr(FuncDecl_ decl): decl(decl) {}
  446. void emit(CodeEmitContext* ctx) override {
  447. int index = ctx->add_func_decl(decl);
  448. ctx->emit(OP_LOAD_FUNCTION, index, line);
  449. }
  450. };
  451. struct FStringExpr: Expr{
  452. Str src;
  453. FStringExpr(const Str& src): src(src) {}
  454. std::string str() const override {
  455. return fmt("f", src.escape());
  456. }
  457. void _load_simple_expr(CodeEmitContext* ctx, Str expr){
  458. // TODO: pre compile this into a function
  459. int dot = expr.index(".");
  460. if(dot < 0){
  461. ctx->emit(OP_LOAD_NAME, StrName(expr.sv()).index, line);
  462. }else{
  463. StrName name(expr.substr(0, dot).sv());
  464. StrName attr(expr.substr(dot+1).sv());
  465. ctx->emit(OP_LOAD_NAME, name.index, line);
  466. ctx->emit(OP_LOAD_ATTR, attr.index, line);
  467. }
  468. }
  469. void emit(CodeEmitContext* ctx) override {
  470. VM* vm = ctx->vm;
  471. static const std::regex pattern(R"(\{(.*?)\})");
  472. std::cregex_iterator begin(src.begin(), src.end(), pattern);
  473. std::cregex_iterator end;
  474. int size = 0;
  475. int i = 0;
  476. for(auto it = begin; it != end; it++) {
  477. std::cmatch m = *it;
  478. if (i < m.position()) {
  479. Str literal = src.substr(i, m.position() - i);
  480. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(literal)), line);
  481. size++;
  482. }
  483. Str expr = m[1].str();
  484. int conon = expr.index(":");
  485. if(conon >= 0){
  486. _load_simple_expr(ctx, expr.substr(0, conon));
  487. Str spec = expr.substr(conon+1);
  488. ctx->emit(OP_FORMAT_STRING, ctx->add_const(VAR(spec)), line);
  489. }else{
  490. _load_simple_expr(ctx, expr);
  491. }
  492. size++;
  493. i = (int)(m.position() + m.length());
  494. }
  495. if (i < src.length()) {
  496. Str literal = src.substr(i, src.length() - i);
  497. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(literal)), line);
  498. size++;
  499. }
  500. ctx->emit(OP_BUILD_STRING, size, line);
  501. }
  502. };
  503. struct SubscrExpr: Expr{
  504. Expr_ a;
  505. Expr_ b;
  506. std::string str() const override { return "Subscr()"; }
  507. void emit(CodeEmitContext* ctx) override{
  508. a->emit(ctx);
  509. b->emit(ctx);
  510. ctx->emit(OP_LOAD_SUBSCR, BC_NOARG, line);
  511. }
  512. bool emit_del(CodeEmitContext* ctx) override {
  513. a->emit(ctx);
  514. b->emit(ctx);
  515. ctx->emit(OP_DELETE_SUBSCR, BC_NOARG, line);
  516. return true;
  517. }
  518. bool emit_store(CodeEmitContext* ctx) override {
  519. a->emit(ctx);
  520. b->emit(ctx);
  521. ctx->emit(OP_STORE_SUBSCR, BC_NOARG, line);
  522. return true;
  523. }
  524. };
  525. struct AttribExpr: Expr{
  526. Expr_ a;
  527. Str b;
  528. AttribExpr(Expr_ a, const Str& b): a(std::move(a)), b(b) {}
  529. AttribExpr(Expr_ a, Str&& b): a(std::move(a)), b(std::move(b)) {}
  530. std::string str() const override { return "Attrib()"; }
  531. void emit(CodeEmitContext* ctx) override{
  532. a->emit(ctx);
  533. int index = StrName(b).index;
  534. ctx->emit(OP_LOAD_ATTR, index, line);
  535. }
  536. bool emit_del(CodeEmitContext* ctx) override {
  537. a->emit(ctx);
  538. int index = StrName(b).index;
  539. ctx->emit(OP_DELETE_ATTR, index, line);
  540. return true;
  541. }
  542. bool emit_store(CodeEmitContext* ctx) override {
  543. a->emit(ctx);
  544. int index = StrName(b).index;
  545. ctx->emit(OP_STORE_ATTR, index, line);
  546. return true;
  547. }
  548. void emit_method(CodeEmitContext* ctx) {
  549. a->emit(ctx);
  550. int index = StrName(b).index;
  551. ctx->emit(OP_LOAD_METHOD, index, line);
  552. }
  553. bool is_attrib() const override { return true; }
  554. };
  555. struct CallExpr: Expr{
  556. Expr_ callable;
  557. std::vector<Expr_> args;
  558. std::vector<std::pair<Str, Expr_>> kwargs;
  559. std::string str() const override { return "Call()"; }
  560. bool need_unpack() const {
  561. for(auto& item: args) if(item->is_starred()) return true;
  562. return false;
  563. }
  564. void emit(CodeEmitContext* ctx) override {
  565. VM* vm = ctx->vm;
  566. if(need_unpack()) ctx->emit(OP_BEGIN_CALL, BC_NOARG, line);
  567. // if callable is a AttrExpr, we should try to use `fast_call` instead of use `boundmethod` proxy
  568. if(callable->is_attrib()){
  569. auto p = static_cast<AttribExpr*>(callable.get());
  570. p->emit_method(ctx);
  571. }else{
  572. callable->emit(ctx);
  573. ctx->emit(OP_LOAD_NULL, BC_NOARG, BC_KEEPLINE);
  574. }
  575. // emit args
  576. for(auto& item: args) item->emit(ctx);
  577. // emit kwargs
  578. for(auto& item: kwargs){
  579. int index = StrName::get(item.first.sv()).index;
  580. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(index)), line);
  581. item.second->emit(ctx);
  582. }
  583. int KWARGC = (int)kwargs.size();
  584. int ARGC = (int)args.size();
  585. if(need_unpack()) ARGC = 0xFFFF;
  586. ctx->emit(OP_CALL, (KWARGC<<16)|ARGC, line);
  587. }
  588. };
  589. struct BinaryExpr: Expr{
  590. TokenIndex op;
  591. Expr_ lhs;
  592. Expr_ rhs;
  593. std::string str() const override { return TK_STR(op); }
  594. bool is_compare() const override {
  595. switch(op){
  596. case TK("<"): case TK("<="): case TK("=="):
  597. case TK("!="): case TK(">"): case TK(">="): return true;
  598. default: return false;
  599. }
  600. }
  601. void _emit_compare(CodeEmitContext* ctx, std::vector<int>& jmps){
  602. if(lhs->is_compare()){
  603. static_cast<BinaryExpr*>(lhs.get())->_emit_compare(ctx, jmps);
  604. }else{
  605. lhs->emit(ctx); // [a]
  606. }
  607. rhs->emit(ctx); // [a, b]
  608. ctx->emit(OP_DUP_TOP, BC_NOARG, line); // [a, b, b]
  609. ctx->emit(OP_ROT_THREE, BC_NOARG, line); // [b, a, b]
  610. switch(op){
  611. case TK("<"): ctx->emit(OP_COMPARE_LT, BC_NOARG, line); break;
  612. case TK("<="): ctx->emit(OP_COMPARE_LE, BC_NOARG, line); break;
  613. case TK("=="): ctx->emit(OP_COMPARE_EQ, BC_NOARG, line); break;
  614. case TK("!="): ctx->emit(OP_COMPARE_NE, BC_NOARG, line); break;
  615. case TK(">"): ctx->emit(OP_COMPARE_GT, BC_NOARG, line); break;
  616. case TK(">="): ctx->emit(OP_COMPARE_GE, BC_NOARG, line); break;
  617. default: UNREACHABLE();
  618. }
  619. // [b, RES]
  620. int index = ctx->emit(OP_SHORTCUT_IF_FALSE_OR_POP, BC_NOARG, line);
  621. jmps.push_back(index);
  622. }
  623. void emit(CodeEmitContext* ctx) override {
  624. std::vector<int> jmps;
  625. if(is_compare() && lhs->is_compare()){
  626. // (a < b) < c
  627. static_cast<BinaryExpr*>(lhs.get())->_emit_compare(ctx, jmps);
  628. // [b, RES]
  629. }else{
  630. // (1 + 2) < c
  631. lhs->emit(ctx);
  632. }
  633. rhs->emit(ctx);
  634. switch (op) {
  635. case TK("+"): ctx->emit(OP_BINARY_ADD, BC_NOARG, line); break;
  636. case TK("-"): ctx->emit(OP_BINARY_SUB, BC_NOARG, line); break;
  637. case TK("*"): ctx->emit(OP_BINARY_MUL, BC_NOARG, line); break;
  638. case TK("/"): ctx->emit(OP_BINARY_TRUEDIV, BC_NOARG, line); break;
  639. case TK("//"): ctx->emit(OP_BINARY_FLOORDIV, BC_NOARG, line); break;
  640. case TK("%"): ctx->emit(OP_BINARY_MOD, BC_NOARG, line); break;
  641. case TK("**"): ctx->emit(OP_BINARY_POW, BC_NOARG, line); break;
  642. case TK("<"): ctx->emit(OP_COMPARE_LT, BC_NOARG, line); break;
  643. case TK("<="): ctx->emit(OP_COMPARE_LE, BC_NOARG, line); break;
  644. case TK("=="): ctx->emit(OP_COMPARE_EQ, BC_NOARG, line); break;
  645. case TK("!="): ctx->emit(OP_COMPARE_NE, BC_NOARG, line); break;
  646. case TK(">"): ctx->emit(OP_COMPARE_GT, BC_NOARG, line); break;
  647. case TK(">="): ctx->emit(OP_COMPARE_GE, BC_NOARG, line); break;
  648. case TK("in"): ctx->emit(OP_CONTAINS_OP, 0, line); break;
  649. case TK("not in"): ctx->emit(OP_CONTAINS_OP, 1, line); break;
  650. case TK("is"): ctx->emit(OP_IS_OP, 0, line); break;
  651. case TK("is not"): ctx->emit(OP_IS_OP, 1, line); break;
  652. case TK("<<"): ctx->emit(OP_BITWISE_LSHIFT, BC_NOARG, line); break;
  653. case TK(">>"): ctx->emit(OP_BITWISE_RSHIFT, BC_NOARG, line); break;
  654. case TK("&"): ctx->emit(OP_BITWISE_AND, BC_NOARG, line); break;
  655. case TK("|"): ctx->emit(OP_BITWISE_OR, BC_NOARG, line); break;
  656. case TK("^"): ctx->emit(OP_BITWISE_XOR, BC_NOARG, line); break;
  657. case TK("@"): ctx->emit(OP_BINARY_MATMUL, BC_NOARG, line); break;
  658. default: FATAL_ERROR();
  659. }
  660. for(int i: jmps) ctx->patch_jump(i);
  661. }
  662. };
  663. struct TernaryExpr: Expr{
  664. Expr_ cond;
  665. Expr_ true_expr;
  666. Expr_ false_expr;
  667. std::string str() const override { return "Ternary()"; }
  668. void emit(CodeEmitContext* ctx) override {
  669. cond->emit(ctx);
  670. int patch = ctx->emit(OP_POP_JUMP_IF_FALSE, BC_NOARG, cond->line);
  671. true_expr->emit(ctx);
  672. int patch_2 = ctx->emit(OP_JUMP_ABSOLUTE, BC_NOARG, true_expr->line);
  673. ctx->patch_jump(patch);
  674. false_expr->emit(ctx);
  675. ctx->patch_jump(patch_2);
  676. }
  677. };
  678. } // namespace pkpy