expr.h 23 KB

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