Compiler.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/Builtins.h>
  11. #include <LibJS/Bytecode/Executable.h>
  12. #include <LibJS/Bytecode/Op.h>
  13. #include <LibJS/JIT/NativeExecutable.h>
  14. #ifdef JIT_ARCH_SUPPORTED
  15. namespace JS::JIT {
  16. using ::JIT::Assembler;
  17. class Compiler {
  18. public:
  19. static OwnPtr<NativeExecutable> compile(Bytecode::Executable&);
  20. private:
  21. # if ARCH(X86_64)
  22. static constexpr auto GPR0 = Assembler::Reg::RAX;
  23. static constexpr auto GPR1 = Assembler::Reg::RCX;
  24. static constexpr auto GPR2 = Assembler::Reg::R13;
  25. static constexpr auto ARG0 = Assembler::Reg::RDI;
  26. static constexpr auto ARG1 = Assembler::Reg::RSI;
  27. static constexpr auto ARG2 = Assembler::Reg::RDX;
  28. static constexpr auto ARG3 = Assembler::Reg::RCX;
  29. static constexpr auto ARG4 = Assembler::Reg::R8;
  30. static constexpr auto ARG5 = Assembler::Reg::R9;
  31. static constexpr auto FPR0 = Assembler::Reg::XMM0;
  32. static constexpr auto FPR1 = Assembler::Reg::XMM1;
  33. static constexpr auto RET = Assembler::Reg::RAX;
  34. static constexpr auto STACK_POINTER = Assembler::Reg::RSP;
  35. static constexpr auto REGISTER_ARRAY_BASE = Assembler::Reg::RBX;
  36. static constexpr auto LOCALS_ARRAY_BASE = Assembler::Reg::R14;
  37. static constexpr auto CACHED_ACCUMULATOR = Assembler::Reg::R12;
  38. static constexpr auto RUNNING_EXECUTION_CONTEXT_BASE = Assembler::Reg::R15;
  39. # endif
  40. static Assembler::Reg argument_register(u32);
  41. # define JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(O) \
  42. O(Div, div) \
  43. O(Exp, exp) \
  44. O(Mod, mod) \
  45. O(In, in) \
  46. O(InstanceOf, instance_of)
  47. # define JS_ENUMERATE_COMPARISON_OPS(O) \
  48. O(LessThan, less_than, SignedLessThan, Below) \
  49. O(LessThanEquals, less_than_equals, SignedLessThanOrEqualTo, BelowOrEqual) \
  50. O(GreaterThan, greater_than, SignedGreaterThan, Above) \
  51. O(GreaterThanEquals, greater_than_equals, SignedGreaterThanOrEqualTo, AboveOrEqual)
  52. # define JS_ENUMERATE_NEW_BUILTIN_ERROR_BYTECODE_OPS(O) \
  53. O(NewTypeError, new_type_error, TypeError)
  54. # define JS_ENUMERATE_IMPLEMENTED_JIT_OPS(O) \
  55. JS_ENUMERATE_COMMON_BINARY_OPS(O) \
  56. JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  57. JS_ENUMERATE_NEW_BUILTIN_ERROR_BYTECODE_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(JumpUndefined, jump_undefined) \
  68. O(Increment, increment) \
  69. O(Decrement, decrement) \
  70. O(EnterUnwindContext, enter_unwind_context) \
  71. O(LeaveUnwindContext, leave_unwind_context) \
  72. O(Throw, throw) \
  73. O(Catch, catch) \
  74. O(CreateLexicalEnvironment, create_lexical_environment) \
  75. O(LeaveLexicalEnvironment, leave_lexical_environment) \
  76. O(EnterObjectEnvironment, enter_object_environment) \
  77. O(ToNumeric, to_numeric) \
  78. O(ResolveThisBinding, resolve_this_binding) \
  79. O(Return, return) \
  80. O(NewString, new_string) \
  81. O(NewObject, new_object) \
  82. O(NewArray, new_array) \
  83. O(NewFunction, new_function) \
  84. O(NewRegExp, new_regexp) \
  85. O(NewBigInt, new_bigint) \
  86. O(NewClass, new_class) \
  87. O(CreateVariable, create_variable) \
  88. O(GetById, get_by_id) \
  89. O(GetByValue, get_by_value) \
  90. O(GetGlobal, get_global) \
  91. O(GetVariable, get_variable) \
  92. O(GetCalleeAndThisFromEnvironment, get_callee_and_this_from_environment) \
  93. O(PutById, put_by_id) \
  94. O(PutByValue, put_by_value) \
  95. O(Call, call) \
  96. O(CallWithArgumentArray, call_with_argument_array) \
  97. O(TypeofVariable, typeof_variable) \
  98. O(SetVariable, set_variable) \
  99. O(ContinuePendingUnwind, continue_pending_unwind) \
  100. O(ConcatString, concat_string) \
  101. O(BlockDeclarationInstantiation, block_declaration_instantiation) \
  102. O(SuperCallWithArgumentArray, super_call_with_argument_array) \
  103. O(GetIterator, get_iterator) \
  104. O(IteratorNext, iterator_next) \
  105. O(IteratorResultDone, iterator_result_done) \
  106. O(ThrowIfNotObject, throw_if_not_object) \
  107. O(ThrowIfNullish, throw_if_nullish) \
  108. O(IteratorResultValue, iterator_result_value) \
  109. O(IteratorClose, iterator_close) \
  110. O(IteratorToArray, iterator_to_array) \
  111. O(Append, append) \
  112. O(DeleteById, delete_by_id) \
  113. O(DeleteByValue, delete_by_value) \
  114. O(DeleteByValueWithThis, delete_by_value_with_this) \
  115. O(GetObjectPropertyIterator, get_object_property_iterator) \
  116. O(GetPrivateById, get_private_by_id) \
  117. O(ResolveSuperBase, resolve_super_base) \
  118. O(GetByIdWithThis, get_by_id_with_this) \
  119. O(GetByValueWithThis, get_by_value_with_this) \
  120. O(DeleteByIdWithThis, delete_by_id_with_this) \
  121. O(PutByIdWithThis, put_by_id_with_this) \
  122. O(PutPrivateById, put_private_by_id) \
  123. O(ImportCall, import_call) \
  124. O(GetImportMeta, get_import_meta) \
  125. O(DeleteVariable, delete_variable) \
  126. O(GetMethod, get_method) \
  127. O(GetNewTarget, get_new_target) \
  128. O(HasPrivateId, has_private_id) \
  129. O(PutByValueWithThis, put_by_value_with_this) \
  130. O(CopyObjectExcludingProperties, copy_object_excluding_properties) \
  131. O(AsyncIteratorClose, async_iterator_close) \
  132. O(Yield, yield) \
  133. O(Await, await)
  134. # define DECLARE_COMPILE_OP(OpTitleCase, op_snake_case, ...) \
  135. void compile_##op_snake_case(Bytecode::Op::OpTitleCase const&);
  136. JS_ENUMERATE_IMPLEMENTED_JIT_OPS(DECLARE_COMPILE_OP)
  137. # undef DECLARE_COMPILE_OP
  138. void compile_builtin(Bytecode::Builtin, Assembler::Label& slow_case, Assembler::Label& end);
  139. # define DECLARE_COMPILE_BUILTIN(name, snake_case_name, ...) \
  140. void compile_builtin_##snake_case_name(Assembler::Label& slow_case, Assembler::Label& end);
  141. JS_ENUMERATE_BUILTINS(DECLARE_COMPILE_BUILTIN)
  142. # undef DECLARE_COMPILE_BUILTIN
  143. void store_vm_register(Bytecode::Register, Assembler::Reg);
  144. void load_vm_register(Assembler::Reg, Bytecode::Register);
  145. void store_vm_local(size_t, Assembler::Reg);
  146. void load_vm_local(Assembler::Reg, size_t);
  147. void reload_cached_accumulator();
  148. void flush_cached_accumulator();
  149. void load_accumulator(Assembler::Reg);
  150. void store_accumulator(Assembler::Reg);
  151. void compile_to_boolean(Assembler::Reg dst, Assembler::Reg src);
  152. void compile_continuation(Optional<Bytecode::Label>, bool is_await);
  153. template<typename Codegen>
  154. void branch_if_same_type_for_equality(Assembler::Reg, Assembler::Reg, Codegen);
  155. void compile_is_strictly_equal(Assembler::Reg, Assembler::Reg, Assembler::Label& slow_case);
  156. void check_exception();
  157. void handle_exception();
  158. void jump_to_exit();
  159. void native_call(void* function_address, Vector<Assembler::Operand> const& stack_arguments = {});
  160. void jump_if_int32(Assembler::Reg, Assembler::Label&);
  161. template<typename Codegen>
  162. void branch_if_type(Assembler::Reg, u16 type_tag, Codegen);
  163. template<typename Codegen>
  164. void branch_if_int32(Assembler::Reg reg, Codegen codegen)
  165. {
  166. branch_if_type(reg, INT32_TAG, codegen);
  167. }
  168. template<typename Codegen>
  169. void branch_if_object(Assembler::Reg reg, Codegen codegen)
  170. {
  171. branch_if_type(reg, OBJECT_TAG, codegen);
  172. }
  173. void extract_object_pointer(Assembler::Reg dst_object, Assembler::Reg src_value);
  174. void convert_to_double(Assembler::Reg dst, Assembler::Reg src, Assembler::Reg nan, Assembler::Reg temp, Assembler::Label& not_number);
  175. template<typename Codegen>
  176. void branch_if_both_int32(Assembler::Reg, Assembler::Reg, Codegen);
  177. void jump_if_not_double(Assembler::Reg reg, Assembler::Reg nan, Assembler::Reg temp, Assembler::Label&);
  178. template<typename CodegenI32, typename CodegenDouble, typename CodegenValue>
  179. void compile_binary_op_fastpaths(Assembler::Reg lhs, Assembler::Reg rhs, CodegenI32, CodegenDouble, CodegenValue);
  180. template<typename CodegenI32, typename CodegenDouble, typename CodegenValue>
  181. void compiler_comparison_fastpaths(Assembler::Reg lhs, Assembler::Reg rhs, CodegenI32, CodegenDouble, CodegenValue);
  182. explicit Compiler(Bytecode::Executable& bytecode_executable)
  183. : m_bytecode_executable(bytecode_executable)
  184. {
  185. }
  186. Assembler::Label& label_for(Bytecode::BasicBlock const& block)
  187. {
  188. return block_data_for(block).label;
  189. }
  190. struct BasicBlockData {
  191. size_t start_offset { 0 };
  192. Assembler::Label label;
  193. };
  194. BasicBlockData& block_data_for(Bytecode::BasicBlock const& block)
  195. {
  196. return *m_basic_block_data.ensure(&block, [] {
  197. return make<BasicBlockData>();
  198. });
  199. }
  200. void set_current_block(Bytecode::BasicBlock const& block)
  201. {
  202. m_current_block = &block;
  203. }
  204. Bytecode::BasicBlock const& current_block()
  205. {
  206. return *m_current_block;
  207. }
  208. HashMap<Bytecode::BasicBlock const*, NonnullOwnPtr<BasicBlockData>> m_basic_block_data;
  209. Vector<u8> m_output;
  210. Assembler m_assembler { m_output };
  211. Assembler::Label m_exit_label;
  212. Bytecode::Executable& m_bytecode_executable;
  213. Bytecode::BasicBlock const* m_current_block;
  214. };
  215. }
  216. #else
  217. namespace JS::JIT {
  218. class Compiler {
  219. public:
  220. static OwnPtr<NativeExecutable> compile(Bytecode::Executable&) { return nullptr; }
  221. };
  222. }
  223. #endif