expr.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. #include "pocketpy/expr.h"
  2. namespace pkpy{
  3. inline bool is_identifier(std::string_view s){
  4. if(s.empty()) return false;
  5. if(!isalpha(s[0]) && s[0] != '_') return false;
  6. for(char c: s) if(!isalnum(c) && c != '_') return false;
  7. return true;
  8. }
  9. inline bool is_small_int(i64 value){
  10. return value >= INT16_MIN && value <= INT16_MAX;
  11. }
  12. int CodeEmitContext::get_loop() const {
  13. int index = curr_block_i;
  14. while(index >= 0){
  15. if(co->blocks[index].type == CodeBlockType::FOR_LOOP) break;
  16. if(co->blocks[index].type == CodeBlockType::WHILE_LOOP) break;
  17. index = co->blocks[index].parent;
  18. }
  19. return index;
  20. }
  21. CodeBlock* CodeEmitContext::enter_block(CodeBlockType type){
  22. if(type==CodeBlockType::FOR_LOOP || type==CodeBlockType::CONTEXT_MANAGER) base_stack_size++;
  23. co->blocks.push_back(CodeBlock(
  24. type, curr_block_i, base_stack_size, (int)co->codes.size()
  25. ));
  26. curr_block_i = co->blocks.size()-1;
  27. return &co->blocks[curr_block_i];
  28. }
  29. void CodeEmitContext::exit_block(){
  30. auto curr_type = co->blocks[curr_block_i].type;
  31. if(curr_type == CodeBlockType::FOR_LOOP || curr_type==CodeBlockType::CONTEXT_MANAGER) base_stack_size--;
  32. co->blocks[curr_block_i].end = co->codes.size();
  33. curr_block_i = co->blocks[curr_block_i].parent;
  34. if(curr_block_i < 0) PK_FATAL_ERROR();
  35. if(curr_type == CodeBlockType::FOR_LOOP){
  36. // add a no op here to make block check work
  37. emit_(OP_NO_OP, BC_NOARG, BC_KEEPLINE, true);
  38. }
  39. }
  40. // clear the expression stack and generate bytecode
  41. void CodeEmitContext::emit_expr(){
  42. if(s_expr.size() != 1) throw std::runtime_error("s_expr.size() != 1");
  43. Expr_ expr = s_expr.popx();
  44. expr->emit_(this);
  45. }
  46. int CodeEmitContext::emit_(Opcode opcode, uint16_t arg, int line, bool is_virtual) {
  47. co->codes.push_back(Bytecode{(uint8_t)opcode, arg});
  48. co->iblocks.push_back(curr_block_i);
  49. co->lines.push_back(CodeObject::LineInfo{line, is_virtual});
  50. int i = co->codes.size() - 1;
  51. if(line == BC_KEEPLINE){
  52. if(i >= 1) co->lines[i].lineno = co->lines[i-1].lineno;
  53. else co->lines[i].lineno = 1;
  54. }
  55. return i;
  56. }
  57. void CodeEmitContext::revert_last_emit_(){
  58. co->codes.pop_back();
  59. co->iblocks.pop_back();
  60. co->lines.pop_back();
  61. }
  62. void CodeEmitContext::try_merge_for_iter_store(int i){
  63. // [FOR_ITER, STORE_?, ]
  64. if(co->codes[i].op != OP_FOR_ITER) return;
  65. if(co->codes.size() - i != 2) return;
  66. uint16_t arg = co->codes[i+1].arg;
  67. if(co->codes[i+1].op == OP_STORE_FAST){
  68. revert_last_emit_();
  69. co->codes[i].op = OP_FOR_ITER_STORE_FAST;
  70. co->codes[i].arg = arg;
  71. return;
  72. }
  73. if(co->codes[i+1].op == OP_STORE_GLOBAL){
  74. revert_last_emit_();
  75. co->codes[i].op = OP_FOR_ITER_STORE_GLOBAL;
  76. co->codes[i].arg = arg;
  77. return;
  78. }
  79. }
  80. int CodeEmitContext::emit_int(i64 value, int line){
  81. if(is_small_int(value)){
  82. return emit_(OP_LOAD_SMALL_INT, (uint16_t)value, line);
  83. }else{
  84. return emit_(OP_LOAD_CONST, add_const(VAR(value)), line);
  85. }
  86. }
  87. void CodeEmitContext::patch_jump(int index) {
  88. int target = co->codes.size();
  89. co->codes[index].arg = target;
  90. }
  91. bool CodeEmitContext::add_label(StrName name){
  92. if(co->labels.contains(name)) return false;
  93. co->labels.set(name, co->codes.size());
  94. return true;
  95. }
  96. int CodeEmitContext::add_varname(StrName name){
  97. // PK_MAX_CO_VARNAMES will be checked when pop_context(), not here
  98. int index = co->varnames_inv.try_get(name);
  99. if(index >= 0) return index;
  100. co->varnames.push_back(name);
  101. index = co->varnames.size() - 1;
  102. co->varnames_inv.set(name, index);
  103. return index;
  104. }
  105. int CodeEmitContext::add_const_string(std::string_view key){
  106. auto it = _co_consts_string_dedup_map.find(key);
  107. if(it != _co_consts_string_dedup_map.end()){
  108. return it->second;
  109. }else{
  110. co->consts.push_back(VAR(key));
  111. int index = co->consts.size() - 1;
  112. _co_consts_string_dedup_map[std::string(key)] = index;
  113. return index;
  114. }
  115. }
  116. int CodeEmitContext::add_const(PyVar v){
  117. if(is_type(v, vm->tp_str)){
  118. // warning: should use add_const_string() instead
  119. return add_const_string(PK_OBJ_GET(Str, v).sv());
  120. }else{
  121. // non-string deduplication
  122. auto it = _co_consts_nonstring_dedup_map.find(v);
  123. if(it != _co_consts_nonstring_dedup_map.end()){
  124. return it->second;
  125. }else{
  126. co->consts.push_back(v);
  127. int index = co->consts.size() - 1;
  128. _co_consts_nonstring_dedup_map[v] = index;
  129. return index;
  130. }
  131. }
  132. PK_UNREACHABLE()
  133. }
  134. int CodeEmitContext::add_func_decl(FuncDecl_ decl){
  135. co->func_decls.push_back(decl);
  136. return co->func_decls.size() - 1;
  137. }
  138. void CodeEmitContext::emit_store_name(NameScope scope, StrName name, int line){
  139. switch(scope){
  140. case NAME_LOCAL:
  141. emit_(OP_STORE_FAST, add_varname(name), line);
  142. break;
  143. case NAME_GLOBAL:
  144. emit_(OP_STORE_GLOBAL, StrName(name).index, line);
  145. break;
  146. case NAME_GLOBAL_UNKNOWN:
  147. emit_(OP_STORE_NAME, StrName(name).index, line);
  148. break;
  149. default: PK_FATAL_ERROR(); break;
  150. }
  151. }
  152. void NameExpr::emit_(CodeEmitContext* ctx) {
  153. int index = ctx->co->varnames_inv.try_get(name);
  154. if(scope == NAME_LOCAL && index >= 0){
  155. ctx->emit_(OP_LOAD_FAST, index, line);
  156. }else{
  157. Opcode op = ctx->level <= 1 ? OP_LOAD_GLOBAL : OP_LOAD_NONLOCAL;
  158. if(ctx->is_compiling_class && scope == NAME_GLOBAL){
  159. // if we are compiling a class, we should use OP_LOAD_ATTR_GLOBAL instead of OP_LOAD_GLOBAL
  160. // this supports @property.setter
  161. op = OP_LOAD_CLASS_GLOBAL;
  162. // exec()/eval() won't work with OP_LOAD_ATTR_GLOBAL in class body
  163. }else{
  164. // we cannot determine the scope when calling exec()/eval()
  165. if(scope == NAME_GLOBAL_UNKNOWN) op = OP_LOAD_NAME;
  166. }
  167. ctx->emit_(op, StrName(name).index, line);
  168. }
  169. }
  170. bool NameExpr::emit_del(CodeEmitContext* ctx) {
  171. switch(scope){
  172. case NAME_LOCAL:
  173. ctx->emit_(OP_DELETE_FAST, ctx->add_varname(name), line);
  174. break;
  175. case NAME_GLOBAL:
  176. ctx->emit_(OP_DELETE_GLOBAL, StrName(name).index, line);
  177. break;
  178. case NAME_GLOBAL_UNKNOWN:
  179. ctx->emit_(OP_DELETE_NAME, StrName(name).index, line);
  180. break;
  181. default: PK_FATAL_ERROR(); break;
  182. }
  183. return true;
  184. }
  185. bool NameExpr::emit_store(CodeEmitContext* ctx) {
  186. if(ctx->is_compiling_class){
  187. ctx->emit_(OP_STORE_CLASS_ATTR, name.index, line);
  188. return true;
  189. }
  190. ctx->emit_store_name(scope, name, line);
  191. return true;
  192. }
  193. void InvertExpr::emit_(CodeEmitContext* ctx) {
  194. child->emit_(ctx);
  195. ctx->emit_(OP_UNARY_INVERT, BC_NOARG, line);
  196. }
  197. void StarredExpr::emit_(CodeEmitContext* ctx) {
  198. child->emit_(ctx);
  199. ctx->emit_(OP_UNARY_STAR, level, line);
  200. }
  201. bool StarredExpr::emit_store(CodeEmitContext* ctx) {
  202. if(level != 1) return false;
  203. // simply proxy to child
  204. return child->emit_store(ctx);
  205. }
  206. void NotExpr::emit_(CodeEmitContext* ctx) {
  207. child->emit_(ctx);
  208. ctx->emit_(OP_UNARY_NOT, BC_NOARG, line);
  209. }
  210. void AndExpr::emit_(CodeEmitContext* ctx) {
  211. lhs->emit_(ctx);
  212. int patch = ctx->emit_(OP_JUMP_IF_FALSE_OR_POP, BC_NOARG, line);
  213. rhs->emit_(ctx);
  214. ctx->patch_jump(patch);
  215. }
  216. void OrExpr::emit_(CodeEmitContext* ctx) {
  217. lhs->emit_(ctx);
  218. int patch = ctx->emit_(OP_JUMP_IF_TRUE_OR_POP, BC_NOARG, line);
  219. rhs->emit_(ctx);
  220. ctx->patch_jump(patch);
  221. }
  222. void Literal0Expr::emit_(CodeEmitContext* ctx){
  223. switch (token) {
  224. case TK("None"): ctx->emit_(OP_LOAD_NONE, BC_NOARG, line); break;
  225. case TK("True"): ctx->emit_(OP_LOAD_TRUE, BC_NOARG, line); break;
  226. case TK("False"): ctx->emit_(OP_LOAD_FALSE, BC_NOARG, line); break;
  227. case TK("..."): ctx->emit_(OP_LOAD_ELLIPSIS, BC_NOARG, line); break;
  228. default: PK_FATAL_ERROR();
  229. }
  230. }
  231. void LongExpr::emit_(CodeEmitContext* ctx) {
  232. ctx->emit_(OP_LOAD_CONST, ctx->add_const_string(s.sv()), line);
  233. ctx->emit_(OP_BUILD_LONG, BC_NOARG, line);
  234. }
  235. void ImagExpr::emit_(CodeEmitContext* ctx) {
  236. VM* vm = ctx->vm;
  237. ctx->emit_(OP_LOAD_CONST, ctx->add_const(VAR(value)), line);
  238. ctx->emit_(OP_BUILD_IMAG, BC_NOARG, line);
  239. }
  240. void BytesExpr::emit_(CodeEmitContext* ctx) {
  241. ctx->emit_(OP_LOAD_CONST, ctx->add_const_string(s.sv()), line);
  242. ctx->emit_(OP_BUILD_BYTES, BC_NOARG, line);
  243. }
  244. void LiteralExpr::emit_(CodeEmitContext* ctx) {
  245. VM* vm = ctx->vm;
  246. if(std::holds_alternative<i64>(value)){
  247. i64 _val = std::get<i64>(value);
  248. ctx->emit_int(_val, line);
  249. return;
  250. }
  251. if(std::holds_alternative<f64>(value)){
  252. f64 _val = std::get<f64>(value);
  253. ctx->emit_(OP_LOAD_CONST, ctx->add_const(VAR(_val)), line);
  254. return;
  255. }
  256. if(std::holds_alternative<Str>(value)){
  257. std::string_view key = std::get<Str>(value).sv();
  258. ctx->emit_(OP_LOAD_CONST, ctx->add_const_string(key), line);
  259. return;
  260. }
  261. }
  262. void NegatedExpr::emit_(CodeEmitContext* ctx){
  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. ctx->emit_int(_val, line);
  270. return;
  271. }
  272. if(std::holds_alternative<f64>(lit->value)){
  273. f64 _val = -std::get<f64>(lit->value);
  274. ctx->emit_(OP_LOAD_CONST, ctx->add_const(VAR(_val)), line);
  275. return;
  276. }
  277. }
  278. child->emit_(ctx);
  279. ctx->emit_(OP_UNARY_NEGATIVE, BC_NOARG, line);
  280. }
  281. void SliceExpr::emit_(CodeEmitContext* ctx){
  282. if(start){
  283. start->emit_(ctx);
  284. }else{
  285. ctx->emit_(OP_LOAD_NONE, BC_NOARG, line);
  286. }
  287. if(stop){
  288. stop->emit_(ctx);
  289. }else{
  290. ctx->emit_(OP_LOAD_NONE, BC_NOARG, line);
  291. }
  292. if(step){
  293. step->emit_(ctx);
  294. }else{
  295. ctx->emit_(OP_LOAD_NONE, BC_NOARG, line);
  296. }
  297. ctx->emit_(OP_BUILD_SLICE, BC_NOARG, line);
  298. }
  299. void DictItemExpr::emit_(CodeEmitContext* ctx) {
  300. if(is_starred()){
  301. PK_ASSERT(key == nullptr);
  302. value->emit_(ctx);
  303. }else{
  304. value->emit_(ctx);
  305. key->emit_(ctx); // reverse order
  306. ctx->emit_(OP_BUILD_TUPLE, 2, line);
  307. }
  308. }
  309. bool TupleExpr::emit_store(CodeEmitContext* ctx) {
  310. // TOS is an iterable
  311. // items may contain StarredExpr, we should check it
  312. int starred_i = -1;
  313. for(int i=0; i<items.size(); i++){
  314. if(!items[i]->is_starred()) continue;
  315. if(starred_i == -1) starred_i = i;
  316. else return false; // multiple StarredExpr not allowed
  317. }
  318. if(starred_i == -1){
  319. Bytecode& prev = ctx->co->codes.back();
  320. if(prev.op == OP_BUILD_TUPLE && prev.arg == items.size()){
  321. // build tuple and unpack it is meaningless
  322. ctx->revert_last_emit_();
  323. }else{
  324. if(prev.op == OP_FOR_ITER){
  325. prev.op = OP_FOR_ITER_UNPACK;
  326. prev.arg = items.size();
  327. }else{
  328. ctx->emit_(OP_UNPACK_SEQUENCE, items.size(), line);
  329. }
  330. }
  331. }else{
  332. // starred assignment target must be in a tuple
  333. if(items.size() == 1) return false;
  334. // starred assignment target must be the last one (differ from cpython)
  335. if(starred_i != items.size()-1) return false;
  336. // a,*b = [1,2,3]
  337. // stack is [1,2,3] -> [1,[2,3]]
  338. ctx->emit_(OP_UNPACK_EX, items.size()-1, line);
  339. }
  340. // do reverse emit
  341. for(int i=items.size()-1; i>=0; i--){
  342. bool ok = items[i]->emit_store(ctx);
  343. if(!ok) return false;
  344. }
  345. return true;
  346. }
  347. bool TupleExpr::emit_del(CodeEmitContext* ctx){
  348. for(auto& e: items){
  349. bool ok = e->emit_del(ctx);
  350. if(!ok) return false;
  351. }
  352. return true;
  353. }
  354. void CompExpr::emit_(CodeEmitContext* ctx){
  355. ctx->emit_(op0(), 0, line);
  356. iter->emit_(ctx);
  357. ctx->emit_(OP_GET_ITER, BC_NOARG, BC_KEEPLINE);
  358. ctx->enter_block(CodeBlockType::FOR_LOOP);
  359. int for_codei = ctx->emit_(OP_FOR_ITER, BC_NOARG, BC_KEEPLINE);
  360. bool ok = vars->emit_store(ctx);
  361. // this error occurs in `vars` instead of this line, but...nevermind
  362. if(!ok) throw std::runtime_error("SyntaxError");
  363. ctx->try_merge_for_iter_store(for_codei);
  364. if(cond){
  365. cond->emit_(ctx);
  366. int patch = ctx->emit_(OP_POP_JUMP_IF_FALSE, BC_NOARG, BC_KEEPLINE);
  367. expr->emit_(ctx);
  368. ctx->emit_(op1(), BC_NOARG, BC_KEEPLINE);
  369. ctx->patch_jump(patch);
  370. }else{
  371. expr->emit_(ctx);
  372. ctx->emit_(op1(), BC_NOARG, BC_KEEPLINE);
  373. }
  374. ctx->emit_(OP_LOOP_CONTINUE, ctx->get_loop(), BC_KEEPLINE);
  375. ctx->exit_block();
  376. }
  377. void FStringExpr::_load_simple_expr(CodeEmitContext* ctx, Str expr){
  378. bool repr = false;
  379. if(expr.size>=2 && expr.end()[-2]=='!'){
  380. switch(expr.end()[-1]){
  381. case 'r': repr = true; expr = expr.substr(0, expr.size-2); break;
  382. case 's': repr = false; expr = expr.substr(0, expr.size-2); break;
  383. default: break; // nothing happens
  384. }
  385. }
  386. // name or name.name
  387. bool is_fastpath = false;
  388. if(is_identifier(expr.sv())){
  389. ctx->emit_(OP_LOAD_NAME, StrName(expr.sv()).index, line);
  390. is_fastpath = true;
  391. }else{
  392. int dot = expr.index(".");
  393. if(dot > 0){
  394. std::string_view a = expr.sv().substr(0, dot);
  395. std::string_view b = expr.sv().substr(dot+1);
  396. if(is_identifier(a) && is_identifier(b)){
  397. ctx->emit_(OP_LOAD_NAME, StrName(a).index, line);
  398. ctx->emit_(OP_LOAD_ATTR, StrName(b).index, line);
  399. is_fastpath = true;
  400. }
  401. }
  402. }
  403. if(!is_fastpath){
  404. int index = ctx->add_const_string(expr.sv());
  405. ctx->emit_(OP_FSTRING_EVAL, index, line);
  406. }
  407. if(repr){
  408. ctx->emit_(OP_REPR, BC_NOARG, line);
  409. }
  410. }
  411. void FStringExpr::emit_(CodeEmitContext* ctx){
  412. int i = 0; // left index
  413. int j = 0; // right index
  414. int count = 0; // how many string parts
  415. bool flag = false; // true if we are in a expression
  416. const char* fmt_valid_chars = "0-=*#@!~" "<>^" ".fds" "0123456789";
  417. PK_LOCAL_STATIC const std::set<char> fmt_valid_char_set(fmt_valid_chars, fmt_valid_chars + strlen(fmt_valid_chars));
  418. while(j < src.size){
  419. if(flag){
  420. if(src[j] == '}'){
  421. // add expression
  422. Str expr = src.substr(i, j-i);
  423. // BUG: ':' is not a format specifier in f"{stack[2:]}"
  424. int conon = expr.index(":");
  425. if(conon >= 0){
  426. Str spec = expr.substr(conon+1);
  427. // filter some invalid spec
  428. bool ok = true;
  429. for(char c: spec) if(!fmt_valid_char_set.count(c)){ ok = false; break; }
  430. if(ok){
  431. _load_simple_expr(ctx, expr.substr(0, conon));
  432. ctx->emit_(OP_FORMAT_STRING, ctx->add_const_string(spec.sv()), line);
  433. }else{
  434. // ':' is not a spec indicator
  435. _load_simple_expr(ctx, expr);
  436. }
  437. }else{
  438. _load_simple_expr(ctx, expr);
  439. }
  440. flag = false;
  441. count++;
  442. }
  443. }else{
  444. if(src[j] == '{'){
  445. // look at next char
  446. if(j+1 < src.size && src[j+1] == '{'){
  447. // {{ -> {
  448. j++;
  449. ctx->emit_(OP_LOAD_CONST, ctx->add_const_string("{"), line);
  450. count++;
  451. }else{
  452. // { -> }
  453. flag = true;
  454. i = j+1;
  455. }
  456. }else if(src[j] == '}'){
  457. // look at next char
  458. if(j+1 < src.size && src[j+1] == '}'){
  459. // }} -> }
  460. j++;
  461. ctx->emit_(OP_LOAD_CONST, ctx->add_const_string("}"), line);
  462. count++;
  463. }else{
  464. // } -> error
  465. // throw std::runtime_error("f-string: unexpected }");
  466. // just ignore
  467. }
  468. }else{
  469. // literal
  470. i = j;
  471. while(j < src.size && src[j] != '{' && src[j] != '}') j++;
  472. Str literal = src.substr(i, j-i);
  473. ctx->emit_(OP_LOAD_CONST, ctx->add_const_string(literal.sv()), line);
  474. count++;
  475. continue; // skip j++
  476. }
  477. }
  478. j++;
  479. }
  480. if(flag){
  481. // literal
  482. Str literal = src.substr(i, src.size-i);
  483. ctx->emit_(OP_LOAD_CONST, ctx->add_const_string(literal.sv()), line);
  484. count++;
  485. }
  486. ctx->emit_(OP_BUILD_STRING, count, line);
  487. }
  488. void SubscrExpr::emit_(CodeEmitContext* ctx){
  489. a->emit_(ctx);
  490. b->emit_(ctx);
  491. Bytecode last_bc = ctx->co->codes.back();
  492. if(b->is_name() && last_bc.op == OP_LOAD_FAST){
  493. ctx->revert_last_emit_();
  494. ctx->emit_(OP_LOAD_SUBSCR_FAST, last_bc.arg, line);
  495. }else if(b->is_literal() && last_bc.op == OP_LOAD_SMALL_INT){
  496. ctx->revert_last_emit_();
  497. ctx->emit_(OP_LOAD_SUBSCR_SMALL_INT, last_bc.arg, line);
  498. }else{
  499. ctx->emit_(OP_LOAD_SUBSCR, BC_NOARG, line);
  500. }
  501. }
  502. bool SubscrExpr::emit_store(CodeEmitContext* ctx){
  503. a->emit_(ctx);
  504. b->emit_(ctx);
  505. Bytecode last_bc = ctx->co->codes.back();
  506. if(b->is_name() && last_bc.op == OP_LOAD_FAST){
  507. ctx->revert_last_emit_();
  508. ctx->emit_(OP_STORE_SUBSCR_FAST, last_bc.arg, line);
  509. }else{
  510. ctx->emit_(OP_STORE_SUBSCR, BC_NOARG, line);
  511. }
  512. return true;
  513. }
  514. bool SubscrExpr::emit_del(CodeEmitContext* ctx){
  515. a->emit_(ctx);
  516. b->emit_(ctx);
  517. ctx->emit_(OP_DELETE_SUBSCR, BC_NOARG, line);
  518. return true;
  519. }
  520. void AttribExpr::emit_(CodeEmitContext* ctx){
  521. a->emit_(ctx);
  522. ctx->emit_(OP_LOAD_ATTR, b.index, line);
  523. }
  524. bool AttribExpr::emit_del(CodeEmitContext* ctx) {
  525. a->emit_(ctx);
  526. ctx->emit_(OP_DELETE_ATTR, b.index, line);
  527. return true;
  528. }
  529. bool AttribExpr::emit_store(CodeEmitContext* ctx){
  530. a->emit_(ctx);
  531. ctx->emit_(OP_STORE_ATTR, b.index, line);
  532. return true;
  533. }
  534. void AttribExpr::emit_method(CodeEmitContext* ctx) {
  535. a->emit_(ctx);
  536. ctx->emit_(OP_LOAD_METHOD, b.index, line);
  537. }
  538. void CallExpr::emit_(CodeEmitContext* ctx) {
  539. bool vargs = false;
  540. bool vkwargs = false;
  541. for(auto& arg: args) if(arg->is_starred()) vargs = true;
  542. for(auto& item: kwargs) if(item.second->is_starred()) vkwargs = true;
  543. // if callable is a AttrExpr, we should try to use `fast_call` instead of use `boundmethod` proxy
  544. if(callable->is_attrib()){
  545. auto p = static_cast<AttribExpr*>(callable.get());
  546. p->emit_method(ctx); // OP_LOAD_METHOD
  547. }else{
  548. callable->emit_(ctx);
  549. ctx->emit_(OP_LOAD_NULL, BC_NOARG, BC_KEEPLINE);
  550. }
  551. if(vargs || vkwargs){
  552. for(auto& item: args) item->emit_(ctx);
  553. ctx->emit_(OP_BUILD_TUPLE_UNPACK, (uint16_t)args.size(), line);
  554. if(!kwargs.empty()){
  555. for(auto& item: kwargs){
  556. if(item.second->is_starred()){
  557. PK_ASSERT(item.second->star_level() == 2)
  558. item.second->emit_(ctx);
  559. }else{
  560. // k=v
  561. int index = ctx->add_const_string(item.first.sv());
  562. ctx->emit_(OP_LOAD_CONST, index, line);
  563. item.second->emit_(ctx);
  564. ctx->emit_(OP_BUILD_TUPLE, 2, line);
  565. }
  566. }
  567. ctx->emit_(OP_BUILD_DICT_UNPACK, (int)kwargs.size(), line);
  568. ctx->emit_(OP_CALL_TP, 1, line);
  569. }else{
  570. ctx->emit_(OP_CALL_TP, 0, line);
  571. }
  572. }else{
  573. // vectorcall protocol
  574. for(auto& item: args) item->emit_(ctx);
  575. for(auto& item: kwargs){
  576. i64 _val = StrName(item.first.sv()).index;
  577. ctx->emit_int(_val, line);
  578. item.second->emit_(ctx);
  579. }
  580. int KWARGC = kwargs.size();
  581. int ARGC = args.size();
  582. ctx->emit_(OP_CALL, (KWARGC<<8)|ARGC, line);
  583. }
  584. }
  585. bool BinaryExpr::is_compare() const {
  586. switch(op){
  587. case TK("<"): case TK("<="): case TK("=="):
  588. case TK("!="): case TK(">"): case TK(">="): return true;
  589. default: return false;
  590. }
  591. }
  592. void BinaryExpr::_emit_compare(CodeEmitContext* ctx, small_vector_2<int, 6>& jmps){
  593. if(lhs->is_compare()){
  594. static_cast<BinaryExpr*>(lhs.get())->_emit_compare(ctx, jmps);
  595. }else{
  596. lhs->emit_(ctx); // [a]
  597. }
  598. rhs->emit_(ctx); // [a, b]
  599. ctx->emit_(OP_DUP_TOP, BC_NOARG, line); // [a, b, b]
  600. ctx->emit_(OP_ROT_THREE, BC_NOARG, line); // [b, a, b]
  601. switch(op){
  602. case TK("<"): ctx->emit_(OP_COMPARE_LT, BC_NOARG, line); break;
  603. case TK("<="): ctx->emit_(OP_COMPARE_LE, BC_NOARG, line); break;
  604. case TK("=="): ctx->emit_(OP_COMPARE_EQ, BC_NOARG, line); break;
  605. case TK("!="): ctx->emit_(OP_COMPARE_NE, BC_NOARG, line); break;
  606. case TK(">"): ctx->emit_(OP_COMPARE_GT, BC_NOARG, line); break;
  607. case TK(">="): ctx->emit_(OP_COMPARE_GE, BC_NOARG, line); break;
  608. default: PK_UNREACHABLE()
  609. }
  610. // [b, RES]
  611. int index = ctx->emit_(OP_SHORTCUT_IF_FALSE_OR_POP, BC_NOARG, line);
  612. jmps.push_back(index);
  613. }
  614. void BinaryExpr::emit_(CodeEmitContext* ctx) {
  615. small_vector_2<int, 6> jmps;
  616. if(is_compare() && lhs->is_compare()){
  617. // (a < b) < c
  618. static_cast<BinaryExpr*>(lhs.get())->_emit_compare(ctx, jmps);
  619. // [b, RES]
  620. }else{
  621. // (1 + 2) < c
  622. lhs->emit_(ctx);
  623. }
  624. rhs->emit_(ctx);
  625. switch (op) {
  626. case TK("+"): ctx->emit_(OP_BINARY_ADD, BC_NOARG, line); break;
  627. case TK("-"): ctx->emit_(OP_BINARY_SUB, BC_NOARG, line); break;
  628. case TK("*"): ctx->emit_(OP_BINARY_MUL, BC_NOARG, line); break;
  629. case TK("/"): ctx->emit_(OP_BINARY_TRUEDIV, BC_NOARG, line); break;
  630. case TK("//"): ctx->emit_(OP_BINARY_FLOORDIV, BC_NOARG, line); break;
  631. case TK("%"): ctx->emit_(OP_BINARY_MOD, BC_NOARG, line); break;
  632. case TK("**"): ctx->emit_(OP_BINARY_POW, BC_NOARG, line); break;
  633. case TK("<"): ctx->emit_(OP_COMPARE_LT, BC_NOARG, line); break;
  634. case TK("<="): ctx->emit_(OP_COMPARE_LE, BC_NOARG, line); break;
  635. case TK("=="): ctx->emit_(OP_COMPARE_EQ, BC_NOARG, line); break;
  636. case TK("!="): ctx->emit_(OP_COMPARE_NE, BC_NOARG, line); break;
  637. case TK(">"): ctx->emit_(OP_COMPARE_GT, BC_NOARG, line); break;
  638. case TK(">="): ctx->emit_(OP_COMPARE_GE, BC_NOARG, line); break;
  639. case TK("in"): ctx->emit_(OP_CONTAINS_OP, 0, line); break;
  640. case TK("not in"): ctx->emit_(OP_CONTAINS_OP, 1, line); break;
  641. case TK("is"): ctx->emit_(OP_IS_OP, 0, line); break;
  642. case TK("is not"): ctx->emit_(OP_IS_OP, 1, line); break;
  643. case TK("<<"): ctx->emit_(OP_BITWISE_LSHIFT, BC_NOARG, line); break;
  644. case TK(">>"): ctx->emit_(OP_BITWISE_RSHIFT, BC_NOARG, line); break;
  645. case TK("&"): ctx->emit_(OP_BITWISE_AND, BC_NOARG, line); break;
  646. case TK("|"): ctx->emit_(OP_BITWISE_OR, BC_NOARG, line); break;
  647. case TK("^"): ctx->emit_(OP_BITWISE_XOR, BC_NOARG, line); break;
  648. case TK("@"): ctx->emit_(OP_BINARY_MATMUL, BC_NOARG, line); break;
  649. default: PK_FATAL_ERROR();
  650. }
  651. for(int i: jmps) ctx->patch_jump(i);
  652. }
  653. void TernaryExpr::emit_(CodeEmitContext* ctx){
  654. cond->emit_(ctx);
  655. int patch = ctx->emit_(OP_POP_JUMP_IF_FALSE, BC_NOARG, cond->line);
  656. true_expr->emit_(ctx);
  657. int patch_2 = ctx->emit_(OP_JUMP_ABSOLUTE, BC_NOARG, true_expr->line);
  658. ctx->patch_jump(patch);
  659. false_expr->emit_(ctx);
  660. ctx->patch_jump(patch_2);
  661. }
  662. } // namespace pkpy