Compiler.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_IMPLEMENTED_JIT_OPS(O) \
  57. JS_ENUMERATE_COMMON_BINARY_OPS(O) \
  58. JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  59. O(LoadImmediate, load_immediate) \
  60. O(Load, load) \
  61. O(Store, store) \
  62. O(GetLocal, get_local) \
  63. O(SetLocal, set_local) \
  64. O(TypeofLocal, typeof_local) \
  65. O(Jump, jump) \
  66. O(JumpConditional, jump_conditional) \
  67. O(JumpNullish, jump_nullish) \
  68. O(Increment, increment) \
  69. O(Decrement, decrement) \
  70. O(EnterUnwindContext, enter_unwind_context) \
  71. O(LeaveUnwindContext, leave_unwind_context) \
  72. O(Throw, throw) \
  73. O(CreateLexicalEnvironment, create_lexical_environment) \
  74. O(LeaveLexicalEnvironment, leave_lexical_environment) \
  75. O(ToNumeric, to_numeric) \
  76. O(ResolveThisBinding, resolve_this_binding) \
  77. O(Return, return) \
  78. O(NewString, new_string) \
  79. O(NewObject, new_object) \
  80. O(NewArray, new_array) \
  81. O(NewFunction, new_function) \
  82. O(NewRegExp, new_regexp) \
  83. O(NewBigInt, new_bigint) \
  84. O(NewClass, new_class) \
  85. O(CreateVariable, create_variable) \
  86. O(GetById, get_by_id) \
  87. O(GetByValue, get_by_value) \
  88. O(GetGlobal, get_global) \
  89. O(GetVariable, get_variable) \
  90. O(GetCalleeAndThisFromEnvironment, get_callee_and_this_from_environment) \
  91. O(PutById, put_by_id) \
  92. O(PutByValue, put_by_value) \
  93. O(Call, call) \
  94. O(CallWithArgumentArray, call_with_argument_array) \
  95. O(TypeofVariable, typeof_variable) \
  96. O(SetVariable, set_variable) \
  97. O(ContinuePendingUnwind, continue_pending_unwind) \
  98. O(ConcatString, concat_string) \
  99. O(BlockDeclarationInstantiation, block_declaration_instantiation) \
  100. O(SuperCallWithArgumentArray, super_call_with_argument_array) \
  101. O(GetIterator, get_iterator) \
  102. O(IteratorNext, iterator_next) \
  103. O(IteratorResultDone, iterator_result_done) \
  104. O(ThrowIfNotObject, throw_if_not_object) \
  105. O(ThrowIfNullish, throw_if_nullish) \
  106. O(IteratorResultValue, iterator_result_value) \
  107. O(IteratorClose, iterator_close) \
  108. O(IteratorToArray, iterator_to_array) \
  109. O(Append, append) \
  110. O(DeleteById, delete_by_id) \
  111. O(DeleteByValue, delete_by_value) \
  112. O(DeleteByValueWithThis, delete_by_value_with_this) \
  113. O(GetObjectPropertyIterator, get_object_property_iterator) \
  114. O(GetPrivateById, get_private_by_id) \
  115. O(ResolveSuperBase, resolve_super_base)
  116. # define DECLARE_COMPILE_OP(OpTitleCase, op_snake_case) \
  117. void compile_##op_snake_case(Bytecode::Op::OpTitleCase const&);
  118. JS_ENUMERATE_IMPLEMENTED_JIT_OPS(DECLARE_COMPILE_OP)
  119. # undef DECLARE_COMPILE_OP
  120. void store_vm_register(Bytecode::Register, Assembler::Reg);
  121. void load_vm_register(Assembler::Reg, Bytecode::Register);
  122. void store_vm_local(size_t, Assembler::Reg);
  123. void load_vm_local(Assembler::Reg, size_t);
  124. void compile_to_boolean(Assembler::Reg dst, Assembler::Reg src);
  125. void check_exception();
  126. void handle_exception();
  127. void push_unwind_context(bool valid, Optional<Bytecode::Label> const& handler, Optional<Bytecode::Label> const& finalizer);
  128. void pop_unwind_context();
  129. void jump_to_exit();
  130. void native_call(void* function_address, Vector<Assembler::Operand> const& stack_arguments = {});
  131. template<typename Codegen>
  132. void branch_if_int32(Assembler::Reg, Codegen);
  133. template<typename Codegen>
  134. void branch_if_both_int32(Assembler::Reg, Assembler::Reg, Codegen);
  135. explicit Compiler(Bytecode::Executable& bytecode_executable)
  136. : m_bytecode_executable(bytecode_executable)
  137. {
  138. }
  139. Assembler::Label& label_for(Bytecode::BasicBlock const& block)
  140. {
  141. return block_data_for(block).label;
  142. }
  143. struct BasicBlockData {
  144. size_t start_offset { 0 };
  145. Assembler::Label label;
  146. Vector<size_t> absolute_references_to_here;
  147. };
  148. BasicBlockData& block_data_for(Bytecode::BasicBlock const& block)
  149. {
  150. return *m_basic_block_data.ensure(&block, [] {
  151. return make<BasicBlockData>();
  152. });
  153. }
  154. HashMap<Bytecode::BasicBlock const*, NonnullOwnPtr<BasicBlockData>> m_basic_block_data;
  155. Vector<u8> m_output;
  156. Assembler m_assembler { m_output };
  157. Assembler::Label m_exit_label;
  158. Assembler::Label m_exception_handler;
  159. Bytecode::Executable& m_bytecode_executable;
  160. };
  161. }
  162. #else
  163. namespace JS::JIT {
  164. class Compiler {
  165. public:
  166. static OwnPtr<NativeExecutable> compile(Bytecode::Executable&) { return nullptr; }
  167. };
  168. }
  169. #endif