expr.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 patch_jump(int index);
  88. bool add_label(StrName name);
  89. int add_varname(StrName name);
  90. int add_const(PyObject*);
  91. int add_const_string(std::string_view);
  92. int add_func_decl(FuncDecl_ decl);
  93. void emit_store_name(NameScope scope, StrName name, int line);
  94. };
  95. struct NameExpr: Expr{
  96. StrName name;
  97. NameScope scope;
  98. NameExpr(StrName name, NameScope scope): name(name), scope(scope) {}
  99. void emit_(CodeEmitContext* ctx) override;
  100. bool emit_del(CodeEmitContext* ctx) override;
  101. bool emit_store(CodeEmitContext* ctx) override;
  102. bool is_name() const override { return true; }
  103. };
  104. struct InvertExpr: Expr{
  105. Expr_ child;
  106. InvertExpr(Expr_&& child): child(std::move(child)) {}
  107. void emit_(CodeEmitContext* ctx) override;
  108. };
  109. struct StarredExpr: Expr{
  110. int level;
  111. Expr_ child;
  112. StarredExpr(int level, Expr_&& child): level(level), child(std::move(child)) {}
  113. int star_level() const override { return level; }
  114. void emit_(CodeEmitContext* ctx) override;
  115. bool emit_store(CodeEmitContext* ctx) override;
  116. };
  117. struct NotExpr: Expr{
  118. Expr_ child;
  119. NotExpr(Expr_&& child): child(std::move(child)) {}
  120. void emit_(CodeEmitContext* ctx) override;
  121. };
  122. struct AndExpr: Expr{
  123. Expr_ lhs;
  124. Expr_ rhs;
  125. void emit_(CodeEmitContext* ctx) override;
  126. };
  127. struct OrExpr: Expr{
  128. Expr_ lhs;
  129. Expr_ rhs;
  130. void emit_(CodeEmitContext* ctx) override;
  131. };
  132. // [None, True, False, ...]
  133. struct Literal0Expr: Expr{
  134. TokenIndex token;
  135. Literal0Expr(TokenIndex token): token(token) {}
  136. bool is_json_object() const override { return true; }
  137. void emit_(CodeEmitContext* ctx) override;
  138. };
  139. struct LongExpr: Expr{
  140. Str s;
  141. LongExpr(const Str& s): s(s) {}
  142. void emit_(CodeEmitContext* ctx) override;
  143. };
  144. struct BytesExpr: Expr{
  145. Str s;
  146. BytesExpr(const Str& s): s(s) {}
  147. void emit_(CodeEmitContext* ctx) override;
  148. };
  149. struct ImagExpr: Expr{
  150. f64 value;
  151. ImagExpr(f64 value): value(value) {}
  152. void emit_(CodeEmitContext* ctx) override;
  153. };
  154. // @num, @str which needs to invoke OP_LOAD_CONST
  155. struct LiteralExpr: Expr{
  156. TokenValue value;
  157. LiteralExpr(TokenValue value): value(value) {}
  158. void emit_(CodeEmitContext* ctx) override;
  159. bool is_literal() const override { return true; }
  160. bool is_json_object() const override { return true; }
  161. };
  162. struct NegatedExpr: Expr{
  163. Expr_ child;
  164. NegatedExpr(Expr_&& child): child(std::move(child)) {}
  165. void emit_(CodeEmitContext* ctx) override;
  166. bool is_json_object() const override { return child->is_literal(); }
  167. };
  168. struct SliceExpr: Expr{
  169. Expr_ start;
  170. Expr_ stop;
  171. Expr_ step;
  172. void emit_(CodeEmitContext* ctx) override;
  173. };
  174. struct DictItemExpr: Expr{
  175. Expr_ key; // maybe nullptr if it is **kwargs
  176. Expr_ value;
  177. int star_level() const override { return value->star_level(); }
  178. void emit_(CodeEmitContext* ctx) override;
  179. };
  180. struct SequenceExpr: Expr{
  181. Expr_vector items;
  182. SequenceExpr(Expr_vector&& items): items(std::move(items)) {}
  183. virtual Opcode opcode() const = 0;
  184. void emit_(CodeEmitContext* ctx) override {
  185. for(auto& item: items) item->emit_(ctx);
  186. ctx->emit_(opcode(), items.size(), line);
  187. }
  188. };
  189. struct ListExpr: SequenceExpr{
  190. using SequenceExpr::SequenceExpr;
  191. Opcode opcode() const override {
  192. for(auto& e: items) if(e->is_starred()) return OP_BUILD_LIST_UNPACK;
  193. return OP_BUILD_LIST;
  194. }
  195. bool is_json_object() const override { return true; }
  196. };
  197. struct DictExpr: SequenceExpr{
  198. using SequenceExpr::SequenceExpr;
  199. Opcode opcode() const override {
  200. for(auto& e: items) if(e->is_starred()) return OP_BUILD_DICT_UNPACK;
  201. return OP_BUILD_DICT;
  202. }
  203. bool is_json_object() const override { return true; }
  204. };
  205. struct SetExpr: SequenceExpr{
  206. using SequenceExpr::SequenceExpr;
  207. Opcode opcode() const override {
  208. for(auto& e: items) if(e->is_starred()) return OP_BUILD_SET_UNPACK;
  209. return OP_BUILD_SET;
  210. }
  211. };
  212. struct TupleExpr: SequenceExpr{
  213. using SequenceExpr::SequenceExpr;
  214. bool is_tuple() const override { return true; }
  215. Opcode opcode() const override {
  216. for(auto& e: items) if(e->is_starred()) return OP_BUILD_TUPLE_UNPACK;
  217. return OP_BUILD_TUPLE;
  218. }
  219. bool emit_store(CodeEmitContext* ctx) override;
  220. bool emit_del(CodeEmitContext* ctx) override;
  221. };
  222. struct CompExpr: Expr{
  223. Expr_ expr; // loop expr
  224. Expr_ vars; // loop vars
  225. Expr_ iter; // loop iter
  226. Expr_ cond; // optional if condition
  227. virtual Opcode op0() = 0;
  228. virtual Opcode op1() = 0;
  229. void emit_(CodeEmitContext* ctx) override;
  230. };
  231. struct ListCompExpr: CompExpr{
  232. Opcode op0() override { return OP_BUILD_LIST; }
  233. Opcode op1() override { return OP_LIST_APPEND; }
  234. };
  235. struct DictCompExpr: CompExpr{
  236. Opcode op0() override { return OP_BUILD_DICT; }
  237. Opcode op1() override { return OP_DICT_ADD; }
  238. };
  239. struct SetCompExpr: CompExpr{
  240. Opcode op0() override { return OP_BUILD_SET; }
  241. Opcode op1() override { return OP_SET_ADD; }
  242. };
  243. struct LambdaExpr: Expr{
  244. FuncDecl_ decl;
  245. LambdaExpr(FuncDecl_ decl): decl(decl) {}
  246. void emit_(CodeEmitContext* ctx) override {
  247. int index = ctx->add_func_decl(decl);
  248. ctx->emit_(OP_LOAD_FUNCTION, index, line);
  249. }
  250. };
  251. struct FStringExpr: Expr{
  252. Str src;
  253. FStringExpr(const Str& src): src(src) {}
  254. void _load_simple_expr(CodeEmitContext* ctx, Str expr);
  255. void emit_(CodeEmitContext* ctx) override;
  256. };
  257. struct SubscrExpr: Expr{
  258. Expr_ a;
  259. Expr_ b;
  260. void emit_(CodeEmitContext* ctx) override;
  261. bool emit_del(CodeEmitContext* ctx) override;
  262. bool emit_store(CodeEmitContext* ctx) override;
  263. };
  264. struct AttribExpr: Expr{
  265. Expr_ a;
  266. StrName b;
  267. AttribExpr(Expr_ a, StrName b): a(std::move(a)), b(b) {}
  268. void emit_(CodeEmitContext* ctx) override;
  269. bool emit_del(CodeEmitContext* ctx) override;
  270. bool emit_store(CodeEmitContext* ctx) override;
  271. void emit_method(CodeEmitContext* ctx);
  272. bool is_attrib() const override { return true; }
  273. };
  274. struct CallExpr: Expr{
  275. Expr_ callable;
  276. Expr_vector args;
  277. // **a will be interpreted as a special keyword argument: {"**": a}
  278. std::vector<std::pair<Str, Expr_>> kwargs;
  279. void emit_(CodeEmitContext* ctx) override;
  280. };
  281. struct GroupedExpr: Expr{
  282. Expr_ a;
  283. GroupedExpr(Expr_&& a): a(std::move(a)) {}
  284. void emit_(CodeEmitContext* ctx) override{
  285. a->emit_(ctx);
  286. }
  287. bool emit_del(CodeEmitContext* ctx) override {
  288. return a->emit_del(ctx);
  289. }
  290. bool emit_store(CodeEmitContext* ctx) override {
  291. return a->emit_store(ctx);
  292. }
  293. };
  294. struct BinaryExpr: Expr{
  295. TokenIndex op;
  296. Expr_ lhs;
  297. Expr_ rhs;
  298. bool is_compare() const override;
  299. void _emit_compare(CodeEmitContext* ctx, pod_vector<int>& jmps);
  300. void emit_(CodeEmitContext* ctx) override;
  301. };
  302. struct TernaryExpr: Expr{
  303. Expr_ cond;
  304. Expr_ true_expr;
  305. Expr_ false_expr;
  306. void emit_(CodeEmitContext* ctx) override;
  307. };
  308. } // namespace pkpy