Compiler.h 6.4 KB

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