Compiler.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #include <LibJIT/Assembler.h>
  10. #include <LibJS/Bytecode/Executable.h>
  11. #include <LibJS/Bytecode/Op.h>
  12. #include <LibJS/JIT/NativeExecutable.h>
  13. #ifdef JIT_ARCH_SUPPORTED
  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 GPR2 = Assembler::Reg::R13;
  24. static constexpr auto ARG0 = Assembler::Reg::RDI;
  25. static constexpr auto ARG1 = Assembler::Reg::RSI;
  26. static constexpr auto ARG2 = Assembler::Reg::RDX;
  27. static constexpr auto ARG3 = Assembler::Reg::RCX;
  28. static constexpr auto ARG4 = Assembler::Reg::R8;
  29. static constexpr auto ARG5 = Assembler::Reg::R9;
  30. static constexpr auto FPR0 = Assembler::Reg::XMM0;
  31. static constexpr auto FPR1 = Assembler::Reg::XMM1;
  32. static constexpr auto RET = Assembler::Reg::RAX;
  33. static constexpr auto STACK_POINTER = Assembler::Reg::RSP;
  34. static constexpr auto REGISTER_ARRAY_BASE = Assembler::Reg::RBX;
  35. static constexpr auto LOCALS_ARRAY_BASE = Assembler::Reg::R14;
  36. static constexpr auto CACHED_ACCUMULATOR = Assembler::Reg::R12;
  37. static constexpr auto RUNNING_EXECUTION_CONTEXT_BASE = Assembler::Reg::R15;
  38. # endif
  39. # define JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(O) \
  40. O(Div, div) \
  41. O(Exp, exp) \
  42. O(Mod, mod) \
  43. O(In, in) \
  44. O(InstanceOf, instance_of)
  45. # define JS_ENUMERATE_COMPARISON_OPS(O) \
  46. O(LessThan, less_than, SignedLessThan, Below) \
  47. O(LessThanEquals, less_than_equals, SignedLessThanOrEqualTo, BelowOrEqual) \
  48. O(GreaterThan, greater_than, SignedGreaterThan, Above) \
  49. O(GreaterThanEquals, greater_than_equals, SignedGreaterThanOrEqualTo, AboveOrEqual)
  50. # define JS_ENUMERATE_NEW_BUILTIN_ERROR_BYTECODE_OPS(O) \
  51. O(NewTypeError, new_type_error, TypeError)
  52. # define JS_ENUMERATE_IMPLEMENTED_JIT_OPS(O) \
  53. JS_ENUMERATE_COMMON_BINARY_OPS(O) \
  54. JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  55. JS_ENUMERATE_NEW_BUILTIN_ERROR_BYTECODE_OPS(O) \
  56. O(LoadImmediate, load_immediate) \
  57. O(Load, load) \
  58. O(Store, store) \
  59. O(GetLocal, get_local) \
  60. O(SetLocal, set_local) \
  61. O(TypeofLocal, typeof_local) \
  62. O(Jump, jump) \
  63. O(JumpConditional, jump_conditional) \
  64. O(JumpNullish, jump_nullish) \
  65. O(JumpUndefined, jump_undefined) \
  66. O(Increment, increment) \
  67. O(Decrement, decrement) \
  68. O(EnterUnwindContext, enter_unwind_context) \
  69. O(LeaveUnwindContext, leave_unwind_context) \
  70. O(Throw, throw) \
  71. O(Catch, catch) \
  72. O(CreateLexicalEnvironment, create_lexical_environment) \
  73. O(LeaveLexicalEnvironment, leave_lexical_environment) \
  74. O(EnterObjectEnvironment, enter_object_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. O(GetByIdWithThis, get_by_id_with_this) \
  117. O(GetByValueWithThis, get_by_value_with_this) \
  118. O(DeleteByIdWithThis, delete_by_id_with_this) \
  119. O(PutByIdWithThis, put_by_id_with_this) \
  120. O(PutPrivateById, put_private_by_id) \
  121. O(ImportCall, import_call) \
  122. O(GetImportMeta, get_import_meta) \
  123. O(DeleteVariable, delete_variable) \
  124. O(GetMethod, get_method) \
  125. O(GetNewTarget, get_new_target) \
  126. O(HasPrivateId, has_private_id) \
  127. O(PutByValueWithThis, put_by_value_with_this) \
  128. O(CopyObjectExcludingProperties, copy_object_excluding_properties) \
  129. O(AsyncIteratorClose, async_iterator_close) \
  130. O(Yield, yield) \
  131. O(Await, await)
  132. # define DECLARE_COMPILE_OP(OpTitleCase, op_snake_case, ...) \
  133. void compile_##op_snake_case(Bytecode::Op::OpTitleCase const&);
  134. JS_ENUMERATE_IMPLEMENTED_JIT_OPS(DECLARE_COMPILE_OP)
  135. # undef DECLARE_COMPILE_OP
  136. void store_vm_register(Bytecode::Register, Assembler::Reg);
  137. void load_vm_register(Assembler::Reg, Bytecode::Register);
  138. void store_vm_local(size_t, Assembler::Reg);
  139. void load_vm_local(Assembler::Reg, size_t);
  140. void reload_cached_accumulator();
  141. void flush_cached_accumulator();
  142. void load_accumulator(Assembler::Reg);
  143. void store_accumulator(Assembler::Reg);
  144. void compile_to_boolean(Assembler::Reg dst, Assembler::Reg src);
  145. void compile_continuation(Optional<Bytecode::Label>, bool is_await);
  146. template<typename Codegen>
  147. void branch_if_same_type_for_equality(Assembler::Reg, Assembler::Reg, Codegen);
  148. void compile_is_strictly_equal(Assembler::Reg, Assembler::Reg, Assembler::Label& slow_case);
  149. void check_exception();
  150. void handle_exception();
  151. void jump_to_exit();
  152. void native_call(void* function_address, Vector<Assembler::Operand> const& stack_arguments = {});
  153. void jump_if_int32(Assembler::Reg, Assembler::Label&);
  154. template<typename Codegen>
  155. void branch_if_type(Assembler::Reg, u16 type_tag, Codegen);
  156. template<typename Codegen>
  157. void branch_if_int32(Assembler::Reg reg, Codegen codegen)
  158. {
  159. branch_if_type(reg, INT32_TAG, codegen);
  160. }
  161. template<typename Codegen>
  162. void branch_if_object(Assembler::Reg reg, Codegen codegen)
  163. {
  164. branch_if_type(reg, OBJECT_TAG, codegen);
  165. }
  166. void extract_object_pointer(Assembler::Reg dst_object, Assembler::Reg src_value);
  167. void convert_to_double(Assembler::Reg dst, Assembler::Reg src, Assembler::Reg nan, Assembler::Reg temp, Assembler::Label& not_number);
  168. template<typename Codegen>
  169. void branch_if_both_int32(Assembler::Reg, Assembler::Reg, Codegen);
  170. void jump_if_not_double(Assembler::Reg reg, Assembler::Reg nan, Assembler::Reg temp, Assembler::Label&);
  171. template<typename CodegenI32, typename CodegenDouble, typename CodegenValue>
  172. void compile_binary_op_fastpaths(Assembler::Reg lhs, Assembler::Reg rhs, CodegenI32, CodegenDouble, CodegenValue);
  173. template<typename CodegenI32, typename CodegenDouble, typename CodegenValue>
  174. void compiler_comparison_fastpaths(Assembler::Reg lhs, Assembler::Reg rhs, CodegenI32, CodegenDouble, CodegenValue);
  175. explicit Compiler(Bytecode::Executable& bytecode_executable)
  176. : m_bytecode_executable(bytecode_executable)
  177. {
  178. }
  179. Assembler::Label& label_for(Bytecode::BasicBlock const& block)
  180. {
  181. return block_data_for(block).label;
  182. }
  183. struct BasicBlockData {
  184. size_t start_offset { 0 };
  185. Assembler::Label label;
  186. };
  187. BasicBlockData& block_data_for(Bytecode::BasicBlock const& block)
  188. {
  189. return *m_basic_block_data.ensure(&block, [] {
  190. return make<BasicBlockData>();
  191. });
  192. }
  193. void set_current_block(Bytecode::BasicBlock const& block)
  194. {
  195. m_current_block = &block;
  196. }
  197. Bytecode::BasicBlock const& current_block()
  198. {
  199. return *m_current_block;
  200. }
  201. HashMap<Bytecode::BasicBlock const*, NonnullOwnPtr<BasicBlockData>> m_basic_block_data;
  202. Vector<u8> m_output;
  203. Assembler m_assembler { m_output };
  204. Assembler::Label m_exit_label;
  205. Bytecode::Executable& m_bytecode_executable;
  206. Bytecode::BasicBlock const* m_current_block;
  207. };
  208. }
  209. #else
  210. namespace JS::JIT {
  211. class Compiler {
  212. public:
  213. static OwnPtr<NativeExecutable> compile(Bytecode::Executable&) { return nullptr; }
  214. };
  215. }
  216. #endif