Compiler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Simon Wanner <simon@skyrising.xyz>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Platform.h>
  9. #if ARCH(X86_64)
  10. # include <LibJIT/Assembler.h>
  11. # include <LibJS/Bytecode/Executable.h>
  12. # include <LibJS/Bytecode/Op.h>
  13. # include <LibJS/JIT/NativeExecutable.h>
  14. namespace JS::JIT {
  15. using ::JIT::Assembler;
  16. class Compiler {
  17. public:
  18. static OwnPtr<NativeExecutable> compile(Bytecode::Executable&);
  19. private:
  20. # if ARCH(X86_64)
  21. static constexpr auto GPR0 = Assembler::Reg::RAX;
  22. static constexpr auto GPR1 = Assembler::Reg::RCX;
  23. static constexpr auto ARG0 = Assembler::Reg::RDI;
  24. static constexpr auto ARG1 = Assembler::Reg::RSI;
  25. static constexpr auto ARG2 = Assembler::Reg::RDX;
  26. static constexpr auto ARG3 = Assembler::Reg::RCX;
  27. static constexpr auto ARG4 = Assembler::Reg::R8;
  28. static constexpr auto ARG5 = Assembler::Reg::R9;
  29. static constexpr auto RET = Assembler::Reg::RAX;
  30. static constexpr auto STACK_POINTER = Assembler::Reg::RSP;
  31. static constexpr auto REGISTER_ARRAY_BASE = Assembler::Reg::RBX;
  32. static constexpr auto LOCALS_ARRAY_BASE = Assembler::Reg::R14;
  33. static constexpr auto UNWIND_CONTEXT_BASE = Assembler::Reg::R15;
  34. # endif
  35. # define JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(O) \
  36. O(Sub, sub) \
  37. O(Mul, mul) \
  38. O(Div, div) \
  39. O(Exp, exp) \
  40. O(Mod, mod) \
  41. O(In, in) \
  42. O(InstanceOf, instance_of) \
  43. O(GreaterThan, greater_than) \
  44. O(GreaterThanEquals, greater_than_equals) \
  45. O(LessThanEquals, less_than_equals) \
  46. O(LooselyInequals, abstract_inequals) \
  47. O(LooselyEquals, abstract_equals) \
  48. O(StrictlyInequals, typed_inequals) \
  49. O(StrictlyEquals, typed_equals) \
  50. O(BitwiseAnd, bitwise_and) \
  51. O(BitwiseOr, bitwise_or) \
  52. O(BitwiseXor, bitwise_xor) \
  53. O(LeftShift, left_shift) \
  54. O(RightShift, right_shift) \
  55. O(UnsignedRightShift, unsigned_right_shift)
  56. # define JS_ENUMERATE_NEW_BUILTIN_ERROR_BYTECODE_OPS(O) \
  57. O(NewTypeError, new_type_error, TypeError)
  58. # define JS_ENUMERATE_IMPLEMENTED_JIT_OPS(O) \
  59. JS_ENUMERATE_COMMON_BINARY_OPS(O) \
  60. JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  61. JS_ENUMERATE_NEW_BUILTIN_ERROR_BYTECODE_OPS(O) \
  62. O(LoadImmediate, load_immediate) \
  63. O(Load, load) \
  64. O(Store, store) \
  65. O(GetLocal, get_local) \
  66. O(SetLocal, set_local) \
  67. O(TypeofLocal, typeof_local) \
  68. O(Jump, jump) \
  69. O(JumpConditional, jump_conditional) \
  70. O(JumpNullish, jump_nullish) \
  71. O(JumpUndefined, jump_undefined) \
  72. O(Increment, increment) \
  73. O(Decrement, decrement) \
  74. O(EnterUnwindContext, enter_unwind_context) \
  75. O(LeaveUnwindContext, leave_unwind_context) \
  76. O(Throw, throw) \
  77. O(CreateLexicalEnvironment, create_lexical_environment) \
  78. O(LeaveLexicalEnvironment, leave_lexical_environment) \
  79. O(ToNumeric, to_numeric) \
  80. O(ResolveThisBinding, resolve_this_binding) \
  81. O(Return, return) \
  82. O(NewString, new_string) \
  83. O(NewObject, new_object) \
  84. O(NewArray, new_array) \
  85. O(NewFunction, new_function) \
  86. O(NewRegExp, new_regexp) \
  87. O(NewBigInt, new_bigint) \
  88. O(NewClass, new_class) \
  89. O(CreateVariable, create_variable) \
  90. O(GetById, get_by_id) \
  91. O(GetByValue, get_by_value) \
  92. O(GetGlobal, get_global) \
  93. O(GetVariable, get_variable) \
  94. O(GetCalleeAndThisFromEnvironment, get_callee_and_this_from_environment) \
  95. O(PutById, put_by_id) \
  96. O(PutByValue, put_by_value) \
  97. O(Call, call) \
  98. O(CallWithArgumentArray, call_with_argument_array) \
  99. O(TypeofVariable, typeof_variable) \
  100. O(SetVariable, set_variable) \
  101. O(ContinuePendingUnwind, continue_pending_unwind) \
  102. O(ConcatString, concat_string) \
  103. O(BlockDeclarationInstantiation, block_declaration_instantiation) \
  104. O(SuperCallWithArgumentArray, super_call_with_argument_array) \
  105. O(GetIterator, get_iterator) \
  106. O(IteratorNext, iterator_next) \
  107. O(IteratorResultDone, iterator_result_done) \
  108. O(ThrowIfNotObject, throw_if_not_object) \
  109. O(ThrowIfNullish, throw_if_nullish) \
  110. O(IteratorResultValue, iterator_result_value) \
  111. O(IteratorClose, iterator_close) \
  112. O(IteratorToArray, iterator_to_array) \
  113. O(Append, append) \
  114. O(DeleteById, delete_by_id) \
  115. O(DeleteByValue, delete_by_value) \
  116. O(DeleteByValueWithThis, delete_by_value_with_this) \
  117. O(GetObjectPropertyIterator, get_object_property_iterator) \
  118. O(GetPrivateById, get_private_by_id) \
  119. O(ResolveSuperBase, resolve_super_base) \
  120. O(GetByIdWithThis, get_by_id_with_this) \
  121. O(GetByValueWithThis, get_by_value_with_this) \
  122. O(DeleteByIdWithThis, delete_by_id_with_this) \
  123. O(PutByIdWithThis, put_by_id_with_this) \
  124. O(PutPrivateById, put_private_by_id) \
  125. O(ImportCall, import_call) \
  126. O(GetImportMeta, get_import_meta) \
  127. O(DeleteVariable, delete_variable) \
  128. O(GetMethod, get_method) \
  129. O(GetNewTarget, get_new_target) \
  130. O(HasPrivateId, has_private_id) \
  131. O(PutByValueWithThis, put_by_value_with_this) \
  132. O(CopyObjectExcludingProperties, copy_object_excluding_properties)
  133. # define DECLARE_COMPILE_OP(OpTitleCase, op_snake_case, ...) \
  134. void compile_##op_snake_case(Bytecode::Op::OpTitleCase const&);
  135. JS_ENUMERATE_IMPLEMENTED_JIT_OPS(DECLARE_COMPILE_OP)
  136. # undef DECLARE_COMPILE_OP
  137. void store_vm_register(Bytecode::Register, Assembler::Reg);
  138. void load_vm_register(Assembler::Reg, Bytecode::Register);
  139. void store_vm_local(size_t, Assembler::Reg);
  140. void load_vm_local(Assembler::Reg, size_t);
  141. void compile_to_boolean(Assembler::Reg dst, Assembler::Reg src);
  142. void check_exception();
  143. void handle_exception();
  144. void push_unwind_context(bool valid, Optional<Bytecode::Label> const& handler, Optional<Bytecode::Label> const& finalizer);
  145. void pop_unwind_context();
  146. void jump_to_exit();
  147. void native_call(void* function_address, Vector<Assembler::Operand> const& stack_arguments = {});
  148. template<typename Codegen>
  149. void branch_if_int32(Assembler::Reg, Codegen);
  150. template<typename Codegen>
  151. void branch_if_both_int32(Assembler::Reg, Assembler::Reg, Codegen);
  152. explicit Compiler(Bytecode::Executable& bytecode_executable)
  153. : m_bytecode_executable(bytecode_executable)
  154. {
  155. }
  156. Assembler::Label& label_for(Bytecode::BasicBlock const& block)
  157. {
  158. return block_data_for(block).label;
  159. }
  160. struct BasicBlockData {
  161. size_t start_offset { 0 };
  162. Assembler::Label label;
  163. Vector<size_t> absolute_references_to_here;
  164. };
  165. BasicBlockData& block_data_for(Bytecode::BasicBlock const& block)
  166. {
  167. return *m_basic_block_data.ensure(&block, [] {
  168. return make<BasicBlockData>();
  169. });
  170. }
  171. HashMap<Bytecode::BasicBlock const*, NonnullOwnPtr<BasicBlockData>> m_basic_block_data;
  172. Vector<u8> m_output;
  173. Assembler m_assembler { m_output };
  174. Assembler::Label m_exit_label;
  175. Assembler::Label m_exception_handler;
  176. Bytecode::Executable& m_bytecode_executable;
  177. };
  178. }
  179. #else
  180. namespace JS::JIT {
  181. class Compiler {
  182. public:
  183. static OwnPtr<NativeExecutable> compile(Bytecode::Executable&) { return nullptr; }
  184. };
  185. }
  186. #endif