expr.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #pragma once
  2. #include "codeobject.h"
  3. #include "common.h"
  4. #include "lexer.h"
  5. #include "error.h"
  6. #include "vm.h"
  7. namespace pkpy{
  8. struct CodeEmitContext;
  9. struct Expr;
  10. #define PK_POOL128_DELETE(ptr) if(ptr != nullptr) { ptr->~T(); pool128_dealloc(ptr); ptr = nullptr; }
  11. template<typename T>
  12. class unique_ptr_128{
  13. T* ptr;
  14. public:
  15. unique_ptr_128(): ptr(nullptr) {}
  16. unique_ptr_128(T* ptr): ptr(ptr) {}
  17. T* operator->() const { return ptr; }
  18. T* get() const { return ptr; }
  19. T* detach() { T* p = ptr; ptr = nullptr; return p; }
  20. unique_ptr_128(const unique_ptr_128&) = delete;
  21. unique_ptr_128& operator=(const unique_ptr_128&) = delete;
  22. bool operator==(std::nullptr_t) const { return ptr == nullptr; }
  23. bool operator!=(std::nullptr_t) const { return ptr != nullptr; }
  24. ~unique_ptr_128(){ PK_POOL128_DELETE(ptr) }
  25. template<typename U>
  26. unique_ptr_128(unique_ptr_128<U>&& other): ptr(other.detach()) {}
  27. operator bool() const { return ptr != nullptr; }
  28. template<typename U>
  29. unique_ptr_128& operator=(unique_ptr_128<U>&& other) {
  30. PK_POOL128_DELETE(ptr)
  31. ptr = other.detach();
  32. return *this;
  33. }
  34. unique_ptr_128& operator=(std::nullptr_t) {
  35. PK_POOL128_DELETE(ptr)
  36. ptr = nullptr;
  37. return *this;
  38. }
  39. };
  40. typedef unique_ptr_128<Expr> Expr_;
  41. typedef small_vector<Expr_, 4> Expr_vector;
  42. template<>
  43. struct TriviallyRelocatable<Expr_>{
  44. constexpr static bool value = true;
  45. };
  46. struct Expr{
  47. int line = 0;
  48. virtual ~Expr() = default;
  49. virtual void emit_(CodeEmitContext* ctx) = 0;
  50. virtual bool is_literal() const { return false; }
  51. virtual bool is_json_object() const { return false; }
  52. virtual bool is_attrib() const { return false; }
  53. virtual bool is_compare() const { return false; }
  54. virtual int star_level() const { return 0; }
  55. virtual bool is_tuple() const { return false; }
  56. virtual bool is_name() const { return false; }
  57. bool is_starred() const { return star_level() > 0; }
  58. // for OP_DELETE_XXX
  59. [[nodiscard]] virtual bool emit_del(CodeEmitContext* ctx) {
  60. return false;
  61. }
  62. // for OP_STORE_XXX
  63. [[nodiscard]] virtual bool emit_store(CodeEmitContext* ctx) {
  64. return false;
  65. }
  66. };
  67. struct CodeEmitContext{
  68. VM* vm;
  69. FuncDecl_ func; // optional
  70. CodeObject_ co; // 1 CodeEmitContext <=> 1 CodeObject_
  71. // some bugs on MSVC (error C2280) when using Expr_vector
  72. // so we use stack_no_copy instead
  73. stack_no_copy<Expr_> s_expr;
  74. int level;
  75. std::set<Str> global_names;
  76. CodeEmitContext(VM* vm, CodeObject_ co, int level): vm(vm), co(co), level(level) {}
  77. int curr_block_i = 0;
  78. bool is_compiling_class = false;
  79. int base_stack_size = 0;
  80. std::map<void*, int> _co_consts_nonstring_dedup_map;
  81. std::map<std::string, int, std::less<>> _co_consts_string_dedup_map;
  82. int get_loop() const;
  83. CodeBlock* enter_block(CodeBlockType type);
  84. void exit_block();
  85. void emit_expr(); // clear the expression stack and generate bytecode
  86. int emit_(Opcode opcode, uint16_t arg, int line, bool is_virtual=false);
  87. void revert_last_emit_();
  88. int emit_int(i64 value, int line);
  89. void patch_jump(int index);
  90. bool add_label(StrName name);
  91. int add_varname(StrName name);
  92. int add_const(PyObject*);
  93. int add_const_string(std::string_view);
  94. int add_func_decl(FuncDecl_ decl);
  95. void emit_store_name(NameScope scope, StrName name, int line);
  96. void try_merge_for_iter_store(int);
  97. };
  98. struct NameExpr: Expr{
  99. StrName name;
  100. NameScope scope;
  101. NameExpr(StrName name, NameScope scope): name(name), scope(scope) {}
  102. void emit_(CodeEmitContext* ctx) override;
  103. bool emit_del(CodeEmitContext* ctx) override;
  104. bool emit_store(CodeEmitContext* ctx) override;
  105. bool is_name() const override { return true; }
  106. };
  107. struct InvertExpr: Expr{
  108. Expr_ child;
  109. InvertExpr(Expr_&& child): child(std::move(child)) {}
  110. void emit_(CodeEmitContext* ctx) override;
  111. };
  112. struct StarredExpr: Expr{
  113. int level;
  114. Expr_ child;
  115. StarredExpr(int level, Expr_&& child): level(level), child(std::move(child)) {}
  116. int star_level() const override { return level; }
  117. void emit_(CodeEmitContext* ctx) override;
  118. bool emit_store(CodeEmitContext* ctx) override;
  119. };
  120. struct NotExpr: Expr{
  121. Expr_ child;
  122. NotExpr(Expr_&& child): child(std::move(child)) {}
  123. void emit_(CodeEmitContext* ctx) override;
  124. };
  125. struct AndExpr: Expr{
  126. Expr_ lhs;
  127. Expr_ rhs;
  128. void emit_(CodeEmitContext* ctx) override;
  129. };
  130. struct OrExpr: Expr{
  131. Expr_ lhs;
  132. Expr_ rhs;
  133. void emit_(CodeEmitContext* ctx) override;
  134. };
  135. // [None, True, False, ...]
  136. struct Literal0Expr: Expr{
  137. TokenIndex token;
  138. Literal0Expr(TokenIndex token): token(token) {}
  139. bool is_json_object() const override { return true; }
  140. void emit_(CodeEmitContext* ctx) override;
  141. };
  142. struct LongExpr: Expr{
  143. Str s;
  144. LongExpr(const Str& s): s(s) {}
  145. void emit_(CodeEmitContext* ctx) override;
  146. };
  147. struct BytesExpr: Expr{
  148. Str s;
  149. BytesExpr(const Str& s): s(s) {}
  150. void emit_(CodeEmitContext* ctx) override;
  151. };
  152. struct ImagExpr: Expr{
  153. f64 value;
  154. ImagExpr(f64 value): value(value) {}
  155. void emit_(CodeEmitContext* ctx) override;
  156. };
  157. // @num, @str which needs to invoke OP_LOAD_CONST
  158. struct LiteralExpr: Expr{
  159. TokenValue value;
  160. LiteralExpr(TokenValue value): value(value) {}
  161. void emit_(CodeEmitContext* ctx) override;
  162. bool is_literal() const override { return true; }
  163. bool is_json_object() const override { return true; }
  164. };
  165. struct NegatedExpr: Expr{
  166. Expr_ child;
  167. NegatedExpr(Expr_&& child): child(std::move(child)) {}
  168. void emit_(CodeEmitContext* ctx) override;
  169. bool is_json_object() const override { return child->is_literal(); }
  170. };
  171. struct SliceExpr: Expr{
  172. Expr_ start;
  173. Expr_ stop;
  174. Expr_ step;
  175. void emit_(CodeEmitContext* ctx) override;
  176. };
  177. struct DictItemExpr: Expr{
  178. Expr_ key; // maybe nullptr if it is **kwargs
  179. Expr_ value;
  180. int star_level() const override { return value->star_level(); }
  181. void emit_(CodeEmitContext* ctx) override;
  182. };
  183. struct SequenceExpr: Expr{
  184. Expr_vector items;
  185. SequenceExpr(Expr_vector&& items): items(std::move(items)) {}
  186. virtual Opcode opcode() const = 0;
  187. void emit_(CodeEmitContext* ctx) override {
  188. for(auto& item: items) item->emit_(ctx);
  189. ctx->emit_(opcode(), items.size(), line);
  190. }
  191. };
  192. struct ListExpr: SequenceExpr{
  193. using SequenceExpr::SequenceExpr;
  194. Opcode opcode() const override {
  195. for(auto& e: items) if(e->is_starred()) return OP_BUILD_LIST_UNPACK;
  196. return OP_BUILD_LIST;
  197. }
  198. bool is_json_object() const override { return true; }
  199. };
  200. struct DictExpr: SequenceExpr{
  201. using SequenceExpr::SequenceExpr;
  202. Opcode opcode() const override {
  203. for(auto& e: items) if(e->is_starred()) return OP_BUILD_DICT_UNPACK;
  204. return OP_BUILD_DICT;
  205. }
  206. bool is_json_object() const override { return true; }
  207. };
  208. struct SetExpr: SequenceExpr{
  209. using SequenceExpr::SequenceExpr;
  210. Opcode opcode() const override {
  211. for(auto& e: items) if(e->is_starred()) return OP_BUILD_SET_UNPACK;
  212. return OP_BUILD_SET;
  213. }
  214. };
  215. struct TupleExpr: SequenceExpr{
  216. using SequenceExpr::SequenceExpr;
  217. bool is_tuple() const override { return true; }
  218. Opcode opcode() const override {
  219. for(auto& e: items) if(e->is_starred()) return OP_BUILD_TUPLE_UNPACK;
  220. return OP_BUILD_TUPLE;
  221. }
  222. bool emit_store(CodeEmitContext* ctx) override;
  223. bool emit_del(CodeEmitContext* ctx) override;
  224. };
  225. struct CompExpr: Expr{
  226. Expr_ expr; // loop expr
  227. Expr_ vars; // loop vars
  228. Expr_ iter; // loop iter
  229. Expr_ cond; // optional if condition
  230. virtual Opcode op0() = 0;
  231. virtual Opcode op1() = 0;
  232. void emit_(CodeEmitContext* ctx) override;
  233. };
  234. struct ListCompExpr: CompExpr{
  235. Opcode op0() override { return OP_BUILD_LIST; }
  236. Opcode op1() override { return OP_LIST_APPEND; }
  237. };
  238. struct DictCompExpr: CompExpr{
  239. Opcode op0() override { return OP_BUILD_DICT; }
  240. Opcode op1() override { return OP_DICT_ADD; }
  241. };
  242. struct SetCompExpr: CompExpr{
  243. Opcode op0() override { return OP_BUILD_SET; }
  244. Opcode op1() override { return OP_SET_ADD; }
  245. };
  246. struct LambdaExpr: Expr{
  247. FuncDecl_ decl;
  248. LambdaExpr(FuncDecl_ decl): decl(decl) {}
  249. void emit_(CodeEmitContext* ctx) override {
  250. int index = ctx->add_func_decl(decl);
  251. ctx->emit_(OP_LOAD_FUNCTION, index, line);
  252. }
  253. };
  254. struct FStringExpr: Expr{
  255. Str src;
  256. FStringExpr(const Str& src): src(src) {}
  257. void _load_simple_expr(CodeEmitContext* ctx, Str expr);
  258. void emit_(CodeEmitContext* ctx) override;
  259. };
  260. struct SubscrExpr: Expr{
  261. Expr_ a;
  262. Expr_ b;
  263. void emit_(CodeEmitContext* ctx) override;
  264. bool emit_del(CodeEmitContext* ctx) override;
  265. bool emit_store(CodeEmitContext* ctx) override;
  266. };
  267. struct AttribExpr: Expr{
  268. Expr_ a;
  269. StrName b;
  270. AttribExpr(Expr_ a, StrName b): a(std::move(a)), b(b) {}
  271. void emit_(CodeEmitContext* ctx) override;
  272. bool emit_del(CodeEmitContext* ctx) override;
  273. bool emit_store(CodeEmitContext* ctx) override;
  274. void emit_method(CodeEmitContext* ctx);
  275. bool is_attrib() const override { return true; }
  276. };
  277. struct CallExpr: Expr{
  278. Expr_ callable;
  279. Expr_vector args;
  280. // **a will be interpreted as a special keyword argument: {"**": a}
  281. std::vector<std::pair<Str, Expr_>> kwargs;
  282. void emit_(CodeEmitContext* ctx) override;
  283. };
  284. struct GroupedExpr: Expr{
  285. Expr_ a;
  286. GroupedExpr(Expr_&& a): a(std::move(a)) {}
  287. void emit_(CodeEmitContext* ctx) override{
  288. a->emit_(ctx);
  289. }
  290. bool emit_del(CodeEmitContext* ctx) override {
  291. return a->emit_del(ctx);
  292. }
  293. bool emit_store(CodeEmitContext* ctx) override {
  294. return a->emit_store(ctx);
  295. }
  296. };
  297. struct BinaryExpr: Expr{
  298. TokenIndex op;
  299. Expr_ lhs;
  300. Expr_ rhs;
  301. bool is_compare() const override;
  302. void _emit_compare(CodeEmitContext* ctx, pod_vector<int>& jmps);
  303. void emit_(CodeEmitContext* ctx) override;
  304. };
  305. struct TernaryExpr: Expr{
  306. Expr_ cond;
  307. Expr_ true_expr;
  308. Expr_ false_expr;
  309. void emit_(CodeEmitContext* ctx) override;
  310. };
  311. } // namespace pkpy