Op.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/FlyString.h>
  9. #include <LibCrypto/BigInt/SignedBigInteger.h>
  10. #include <LibJS/Bytecode/Instruction.h>
  11. #include <LibJS/Bytecode/Label.h>
  12. #include <LibJS/Bytecode/Register.h>
  13. #include <LibJS/Heap/Cell.h>
  14. #include <LibJS/Runtime/Value.h>
  15. namespace JS::Bytecode::Op {
  16. class Load final : public Instruction {
  17. public:
  18. Load(Register src)
  19. : Instruction(Type::Load)
  20. , m_src(src)
  21. {
  22. }
  23. void execute(Bytecode::Interpreter&) const;
  24. String to_string() const;
  25. private:
  26. Register m_src;
  27. };
  28. class LoadImmediate final : public Instruction {
  29. public:
  30. LoadImmediate(Value value)
  31. : Instruction(Type::LoadImmediate)
  32. , m_value(value)
  33. {
  34. }
  35. void execute(Bytecode::Interpreter&) const;
  36. String to_string() const;
  37. private:
  38. Value m_value;
  39. };
  40. class Store final : public Instruction {
  41. public:
  42. Store(Register dst)
  43. : Instruction(Type::Store)
  44. , m_dst(dst)
  45. {
  46. }
  47. void execute(Bytecode::Interpreter&) const;
  48. String to_string() const;
  49. private:
  50. Register m_dst;
  51. };
  52. #define JS_ENUMERATE_COMMON_BINARY_OPS(O) \
  53. O(Add, add) \
  54. O(Sub, sub) \
  55. O(Mul, mul) \
  56. O(Div, div) \
  57. O(Exp, exp) \
  58. O(Mod, mod) \
  59. O(In, in) \
  60. O(InstanceOf, instance_of) \
  61. O(GreaterThan, greater_than) \
  62. O(GreaterThanEquals, greater_than_equals) \
  63. O(LessThan, less_than) \
  64. O(LessThanEquals, less_than_equals) \
  65. O(AbstractInequals, abstract_inequals) \
  66. O(AbstractEquals, abstract_equals) \
  67. O(TypedInequals, typed_inequals) \
  68. O(TypedEquals, typed_equals) \
  69. O(BitwiseAnd, bitwise_and) \
  70. O(BitwiseOr, bitwise_or) \
  71. O(BitwiseXor, bitwise_xor) \
  72. O(LeftShift, left_shift) \
  73. O(RightShift, right_shift) \
  74. O(UnsignedRightShift, unsigned_right_shift)
  75. #define JS_DECLARE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  76. class OpTitleCase final : public Instruction { \
  77. public: \
  78. OpTitleCase(Register lhs_reg) \
  79. : Instruction(Type::OpTitleCase) \
  80. , m_lhs_reg(lhs_reg) \
  81. { \
  82. } \
  83. \
  84. void execute(Bytecode::Interpreter&) const; \
  85. String to_string() const; \
  86. \
  87. private: \
  88. Register m_lhs_reg; \
  89. };
  90. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DECLARE_COMMON_BINARY_OP)
  91. #undef JS_DECLARE_COMMON_BINARY_OP
  92. #define JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  93. O(BitwiseNot, bitwise_not) \
  94. O(Not, not_) \
  95. O(UnaryPlus, unary_plus) \
  96. O(UnaryMinus, unary_minus) \
  97. O(Typeof, typeof_)
  98. #define JS_DECLARE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  99. class OpTitleCase final : public Instruction { \
  100. public: \
  101. OpTitleCase() \
  102. : Instruction(Type::OpTitleCase) \
  103. { \
  104. } \
  105. \
  106. void execute(Bytecode::Interpreter&) const; \
  107. String to_string() const; \
  108. };
  109. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DECLARE_COMMON_UNARY_OP)
  110. #undef JS_DECLARE_COMMON_UNARY_OP
  111. class NewString final : public Instruction {
  112. public:
  113. NewString(String string)
  114. : Instruction(Type::NewString)
  115. , m_string(move(string))
  116. {
  117. }
  118. void execute(Bytecode::Interpreter&) const;
  119. String to_string() const;
  120. private:
  121. String m_string;
  122. };
  123. class NewObject final : public Instruction {
  124. public:
  125. NewObject()
  126. : Instruction(Type::NewObject)
  127. {
  128. }
  129. void execute(Bytecode::Interpreter&) const;
  130. String to_string() const;
  131. };
  132. class NewBigInt final : public Instruction {
  133. public:
  134. explicit NewBigInt(Crypto::SignedBigInteger bigint)
  135. : Instruction(Type::NewBigInt)
  136. , m_bigint(move(bigint))
  137. {
  138. }
  139. void execute(Bytecode::Interpreter&) const;
  140. String to_string() const;
  141. private:
  142. Crypto::SignedBigInteger m_bigint;
  143. };
  144. // NOTE: This instruction is variable-width depending on the number of elements!
  145. class NewArray final : public Instruction {
  146. public:
  147. NewArray(Vector<Register> const& elements)
  148. : Instruction(Type::NewArray)
  149. , m_element_count(elements.size())
  150. {
  151. for (size_t i = 0; i < m_element_count; ++i)
  152. m_elements[i] = elements[i];
  153. }
  154. void execute(Bytecode::Interpreter&) const;
  155. String to_string() const;
  156. size_t length() const { return sizeof(*this) + sizeof(Register) * m_element_count; }
  157. private:
  158. size_t m_element_count { 0 };
  159. Register m_elements[];
  160. };
  161. class ConcatString final : public Instruction {
  162. public:
  163. ConcatString(Register lhs)
  164. : Instruction(Type::ConcatString)
  165. , m_lhs(lhs)
  166. {
  167. }
  168. void execute(Bytecode::Interpreter&) const;
  169. String to_string() const;
  170. private:
  171. Register m_lhs;
  172. };
  173. class SetVariable final : public Instruction {
  174. public:
  175. SetVariable(FlyString identifier)
  176. : Instruction(Type::SetVariable)
  177. , m_identifier(move(identifier))
  178. {
  179. }
  180. void execute(Bytecode::Interpreter&) const;
  181. String to_string() const;
  182. private:
  183. FlyString m_identifier;
  184. };
  185. class GetVariable final : public Instruction {
  186. public:
  187. GetVariable(FlyString identifier)
  188. : Instruction(Type::GetVariable)
  189. , m_identifier(move(identifier))
  190. {
  191. }
  192. void execute(Bytecode::Interpreter&) const;
  193. String to_string() const;
  194. private:
  195. FlyString m_identifier;
  196. };
  197. class GetById final : public Instruction {
  198. public:
  199. GetById(FlyString property)
  200. : Instruction(Type::GetById)
  201. , m_property(move(property))
  202. {
  203. }
  204. void execute(Bytecode::Interpreter&) const;
  205. String to_string() const;
  206. private:
  207. FlyString m_property;
  208. };
  209. class PutById final : public Instruction {
  210. public:
  211. PutById(Register base, FlyString property)
  212. : Instruction(Type::PutById)
  213. , m_base(base)
  214. , m_property(move(property))
  215. {
  216. }
  217. void execute(Bytecode::Interpreter&) const;
  218. String to_string() const;
  219. private:
  220. Register m_base;
  221. FlyString m_property;
  222. };
  223. class Jump : public Instruction {
  224. public:
  225. constexpr static bool IsTerminator = true;
  226. explicit Jump(Type type, Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  227. : Instruction(type)
  228. , m_true_target(move(taken_target))
  229. , m_false_target(move(nontaken_target))
  230. {
  231. }
  232. explicit Jump(Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  233. : Instruction(Type::Jump)
  234. , m_true_target(move(taken_target))
  235. , m_false_target(move(nontaken_target))
  236. {
  237. }
  238. void set_targets(Optional<Label> true_target, Optional<Label> false_target)
  239. {
  240. m_true_target = move(true_target);
  241. m_false_target = move(false_target);
  242. }
  243. void execute(Bytecode::Interpreter&) const;
  244. String to_string() const;
  245. protected:
  246. Optional<Label> m_true_target;
  247. Optional<Label> m_false_target;
  248. };
  249. class JumpConditional final : public Jump {
  250. public:
  251. explicit JumpConditional(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  252. : Jump(Type::JumpConditional, move(true_target), move(false_target))
  253. {
  254. }
  255. void execute(Bytecode::Interpreter&) const;
  256. String to_string() const;
  257. };
  258. class JumpNullish final : public Jump {
  259. public:
  260. explicit JumpNullish(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  261. : Jump(Type::JumpNullish, move(true_target), move(false_target))
  262. {
  263. }
  264. void execute(Bytecode::Interpreter&) const;
  265. String to_string() const;
  266. };
  267. // NOTE: This instruction is variable-width depending on the number of arguments!
  268. class Call final : public Instruction {
  269. public:
  270. Call(Register callee, Register this_value, Vector<Register> const& arguments)
  271. : Instruction(Type::Call)
  272. , m_callee(callee)
  273. , m_this_value(this_value)
  274. , m_argument_count(arguments.size())
  275. {
  276. for (size_t i = 0; i < m_argument_count; ++i)
  277. m_arguments[i] = arguments[i];
  278. }
  279. void execute(Bytecode::Interpreter&) const;
  280. String to_string() const;
  281. size_t length() const { return sizeof(*this) + sizeof(Register) * m_argument_count; }
  282. private:
  283. Register m_callee;
  284. Register m_this_value;
  285. size_t m_argument_count { 0 };
  286. Register m_arguments[];
  287. };
  288. class EnterScope final : public Instruction {
  289. public:
  290. explicit EnterScope(ScopeNode const& scope_node)
  291. : Instruction(Type::EnterScope)
  292. , m_scope_node(scope_node)
  293. {
  294. }
  295. void execute(Bytecode::Interpreter&) const;
  296. String to_string() const;
  297. private:
  298. ScopeNode const& m_scope_node;
  299. };
  300. class Return final : public Instruction {
  301. public:
  302. constexpr static bool IsTerminator = true;
  303. Return()
  304. : Instruction(Type::Return)
  305. {
  306. }
  307. void execute(Bytecode::Interpreter&) const;
  308. String to_string() const;
  309. };
  310. }
  311. namespace JS::Bytecode {
  312. ALWAYS_INLINE void Instruction::execute(Bytecode::Interpreter& interpreter) const
  313. {
  314. #define __BYTECODE_OP(op) \
  315. case Instruction::Type::op: \
  316. return static_cast<Bytecode::Op::op const&>(*this).execute(interpreter);
  317. switch (type()) {
  318. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  319. default:
  320. VERIFY_NOT_REACHED();
  321. }
  322. #undef __BYTECODE_OP
  323. }
  324. ALWAYS_INLINE size_t Instruction::length() const
  325. {
  326. if (type() == Type::Call)
  327. return static_cast<Op::Call const&>(*this).length();
  328. else if (type() == Type::NewArray)
  329. return static_cast<Op::NewArray const&>(*this).length();
  330. #define __BYTECODE_OP(op) \
  331. case Type::op: \
  332. return sizeof(Op::op);
  333. switch (type()) {
  334. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  335. default:
  336. VERIFY_NOT_REACHED();
  337. }
  338. #undef __BYTECODE_OP
  339. }
  340. }