Compiler.h 5.5 KB

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