expr.h 27 KB

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