Compiler.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #if ARCH(X86_64)
  8. # include <LibJIT/Assembler.h>
  9. # include <LibJS/Bytecode/Executable.h>
  10. # include <LibJS/Bytecode/Op.h>
  11. # include <LibJS/JIT/NativeExecutable.h>
  12. namespace JS::JIT {
  13. using ::JIT::Assembler;
  14. class Compiler {
  15. public:
  16. static OwnPtr<NativeExecutable> compile(Bytecode::Executable&);
  17. private:
  18. # if ARCH(X86_64)
  19. static constexpr auto GPR0 = Assembler::Reg::RAX;
  20. static constexpr auto GPR1 = Assembler::Reg::RCX;
  21. static constexpr auto ARG0 = Assembler::Reg::RDI;
  22. static constexpr auto ARG1 = Assembler::Reg::RSI;
  23. static constexpr auto ARG2 = Assembler::Reg::RDX;
  24. static constexpr auto ARG3 = Assembler::Reg::RCX;
  25. static constexpr auto ARG4 = Assembler::Reg::R8;
  26. static constexpr auto ARG5 = Assembler::Reg::R9;
  27. static constexpr auto RET = Assembler::Reg::RAX;
  28. static constexpr auto STACK_POINTER = Assembler::Reg::RSP;
  29. static constexpr auto REGISTER_ARRAY_BASE = Assembler::Reg::RBX;
  30. static constexpr auto LOCALS_ARRAY_BASE = Assembler::Reg::R14;
  31. static constexpr auto UNWIND_CONTEXT_BASE = Assembler::Reg::R15;
  32. # endif
  33. void compile_load_immediate(Bytecode::Op::LoadImmediate const&);
  34. void compile_load(Bytecode::Op::Load const&);
  35. void compile_store(Bytecode::Op::Store const&);
  36. void compile_get_local(Bytecode::Op::GetLocal const&);
  37. void compile_set_local(Bytecode::Op::SetLocal const&);
  38. void compile_typeof_local(Bytecode::Op::TypeofLocal const&);
  39. void compile_jump(Bytecode::Op::Jump const&);
  40. void compile_jump_conditional(Bytecode::Op::JumpConditional const&);
  41. void compile_jump_nullish(Bytecode::Op::JumpNullish const&);
  42. void compile_increment(Bytecode::Op::Increment const&);
  43. void compile_decrement(Bytecode::Op::Decrement const&);
  44. void compile_enter_unwind_context(Bytecode::Op::EnterUnwindContext const&);
  45. void compile_leave_unwind_context(Bytecode::Op::LeaveUnwindContext const&);
  46. void compile_throw(Bytecode::Op::Throw const&);
  47. void compile_create_lexical_environment(Bytecode::Op::CreateLexicalEnvironment const&);
  48. void compile_leave_lexical_environment(Bytecode::Op::LeaveLexicalEnvironment const&);
  49. void compile_to_numeric(Bytecode::Op::ToNumeric const&);
  50. void compile_resolve_this_binding(Bytecode::Op::ResolveThisBinding const&);
  51. # define JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(O) \
  52. O(Add, add) \
  53. O(Sub, sub) \
  54. O(Mul, mul) \
  55. O(Div, div) \
  56. O(Exp, exp) \
  57. O(Mod, mod) \
  58. O(In, in) \
  59. O(InstanceOf, instance_of) \
  60. O(GreaterThan, greater_than) \
  61. O(GreaterThanEquals, greater_than_equals) \
  62. O(LessThanEquals, less_than_equals) \
  63. O(LooselyInequals, abstract_inequals) \
  64. O(LooselyEquals, abstract_equals) \
  65. O(StrictlyInequals, typed_inequals) \
  66. O(StrictlyEquals, typed_equals) \
  67. O(BitwiseAnd, bitwise_and) \
  68. O(BitwiseOr, bitwise_or) \
  69. O(BitwiseXor, bitwise_xor) \
  70. O(LeftShift, left_shift) \
  71. O(RightShift, right_shift) \
  72. O(UnsignedRightShift, unsigned_right_shift)
  73. # define DO_COMPILE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  74. void compile_##op_snake_case(Bytecode::Op::OpTitleCase const&);
  75. JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(DO_COMPILE_COMMON_BINARY_OP)
  76. # undef DO_COMPILE_COMMON_BINARY_OP
  77. # define DO_COMPILE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  78. void compile_##op_snake_case(Bytecode::Op::OpTitleCase const&);
  79. JS_ENUMERATE_COMMON_UNARY_OPS(DO_COMPILE_COMMON_UNARY_OP)
  80. # undef DO_COMPILE_COMMON_UNARY_OP
  81. void compile_less_than(Bytecode::Op::LessThan const&);
  82. void compile_return(Bytecode::Op::Return const&);
  83. void compile_new_string(Bytecode::Op::NewString const&);
  84. void compile_new_object(Bytecode::Op::NewObject const&);
  85. void compile_new_array(Bytecode::Op::NewArray const&);
  86. void compile_new_function(Bytecode::Op::NewFunction const&);
  87. void compile_new_regexp(Bytecode::Op::NewRegExp const&);
  88. void compile_new_bigint(Bytecode::Op::NewBigInt const&);
  89. void compile_get_by_id(Bytecode::Op::GetById const&);
  90. void compile_get_by_value(Bytecode::Op::GetByValue const&);
  91. void compile_get_global(Bytecode::Op::GetGlobal const&);
  92. void compile_get_variable(Bytecode::Op::GetVariable const&);
  93. void compile_get_callee_and_this_from_environment(Bytecode::Op::GetCalleeAndThisFromEnvironment const&);
  94. void compile_put_by_id(Bytecode::Op::PutById const&);
  95. void compile_put_by_value(Bytecode::Op::PutByValue const&);
  96. void compile_call(Bytecode::Op::Call const&);
  97. void compile_typeof_variable(Bytecode::Op::TypeofVariable const&);
  98. void compile_set_variable(Bytecode::Op::SetVariable const&);
  99. void compile_continue_pending_unwind(Bytecode::Op::ContinuePendingUnwind const&);
  100. void store_vm_register(Bytecode::Register, Assembler::Reg);
  101. void load_vm_register(Assembler::Reg, Bytecode::Register);
  102. void store_vm_local(size_t, Assembler::Reg);
  103. void load_vm_local(Assembler::Reg, size_t);
  104. void compile_to_boolean(Assembler::Reg dst, Assembler::Reg src);
  105. void check_exception();
  106. void push_unwind_context(bool valid, Optional<Bytecode::Label> const& handler, Optional<Bytecode::Label> const& finalizer);
  107. void pop_unwind_context();
  108. void jump_to_exit();
  109. void native_call(void* function_address, Vector<Assembler::Operand> const& stack_arguments = {});
  110. template<typename Codegen>
  111. void branch_if_int32(Assembler::Reg, Codegen);
  112. template<typename Codegen>
  113. void branch_if_both_int32(Assembler::Reg, Assembler::Reg, Codegen);
  114. explicit Compiler(Bytecode::Executable& bytecode_executable)
  115. : m_bytecode_executable(bytecode_executable)
  116. {
  117. }
  118. Assembler::Label& label_for(Bytecode::BasicBlock const& block)
  119. {
  120. return block_data_for(block).label;
  121. }
  122. struct BasicBlockData {
  123. size_t start_offset { 0 };
  124. Assembler::Label label;
  125. Vector<size_t> absolute_references_to_here;
  126. };
  127. BasicBlockData& block_data_for(Bytecode::BasicBlock const& block)
  128. {
  129. return *m_basic_block_data.ensure(&block, [] {
  130. return make<BasicBlockData>();
  131. });
  132. }
  133. HashMap<Bytecode::BasicBlock const*, NonnullOwnPtr<BasicBlockData>> m_basic_block_data;
  134. Vector<u8> m_output;
  135. Assembler m_assembler { m_output };
  136. Assembler::Label m_exit_label;
  137. Bytecode::Executable& m_bytecode_executable;
  138. };
  139. }
  140. #else
  141. namespace JS::JIT {
  142. class Compiler {
  143. public:
  144. static OwnPtr<NativeExecutable> compile(Bytecode::Executable&) { return nullptr; }
  145. };
  146. }
  147. #endif