Compiler.h 8.6 KB

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