expr.h 26 KB

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