Compiler.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. O(LooselyInequals, loosely_inequals) \
  46. O(LooselyEquals, loosely_equals) \
  47. O(StrictlyInequals, strict_inequals) \
  48. O(StrictlyEquals, strict_equals)
  49. # define JS_ENUMERATE_COMPARISON_OPS(O) \
  50. O(LessThan, less_than, SignedLessThan) \
  51. O(LessThanEquals, less_than_equals, SignedLessThanOrEqualTo) \
  52. O(GreaterThan, greater_than, SignedGreaterThan) \
  53. O(GreaterThanEquals, greater_than_equals, SignedGreaterThanOrEqualTo)
  54. # define JS_ENUMERATE_NEW_BUILTIN_ERROR_BYTECODE_OPS(O) \
  55. O(NewTypeError, new_type_error, TypeError)
  56. # define JS_ENUMERATE_IMPLEMENTED_JIT_OPS(O) \
  57. JS_ENUMERATE_COMMON_BINARY_OPS(O) \
  58. JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  59. JS_ENUMERATE_NEW_BUILTIN_ERROR_BYTECODE_OPS(O) \
  60. O(LoadImmediate, load_immediate) \
  61. O(Load, load) \
  62. O(Store, store) \
  63. O(GetLocal, get_local) \
  64. O(SetLocal, set_local) \
  65. O(TypeofLocal, typeof_local) \
  66. O(Jump, jump) \
  67. O(JumpConditional, jump_conditional) \
  68. O(JumpNullish, jump_nullish) \
  69. O(JumpUndefined, jump_undefined) \
  70. O(Increment, increment) \
  71. O(Decrement, decrement) \
  72. O(EnterUnwindContext, enter_unwind_context) \
  73. O(LeaveUnwindContext, leave_unwind_context) \
  74. O(Throw, throw) \
  75. O(CreateLexicalEnvironment, create_lexical_environment) \
  76. O(LeaveLexicalEnvironment, leave_lexical_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 store_vm_register(Bytecode::Register, Assembler::Reg);
  139. void load_vm_register(Assembler::Reg, Bytecode::Register);
  140. void store_vm_local(size_t, Assembler::Reg);
  141. void load_vm_local(Assembler::Reg, size_t);
  142. void reload_cached_accumulator();
  143. void flush_cached_accumulator();
  144. void load_accumulator(Assembler::Reg);
  145. void store_accumulator(Assembler::Reg);
  146. void compile_to_boolean(Assembler::Reg dst, Assembler::Reg src);
  147. void compile_continuation(Optional<Bytecode::Label>, bool is_await);
  148. void check_exception();
  149. void handle_exception();
  150. void jump_to_exit();
  151. void native_call(void* function_address, Vector<Assembler::Operand> const& stack_arguments = {});
  152. void jump_if_int32(Assembler::Reg, Assembler::Label&);
  153. template<typename Codegen>
  154. void branch_if_type(Assembler::Reg, u16 type_tag, Codegen);
  155. template<typename Codegen>
  156. void branch_if_int32(Assembler::Reg reg, Codegen codegen)
  157. {
  158. branch_if_type(reg, INT32_TAG, codegen);
  159. }
  160. template<typename Codegen>
  161. void branch_if_object(Assembler::Reg reg, Codegen codegen)
  162. {
  163. branch_if_type(reg, OBJECT_TAG, codegen);
  164. }
  165. void extract_object_pointer(Assembler::Reg dst_object, Assembler::Reg src_value);
  166. void convert_to_double(Assembler::Reg dst, Assembler::Reg src, Assembler::Reg nan, Assembler::Reg temp, Assembler::Label& not_number);
  167. template<typename Codegen>
  168. void branch_if_both_int32(Assembler::Reg, Assembler::Reg, Codegen);
  169. void jump_if_not_double(Assembler::Reg reg, Assembler::Reg nan, Assembler::Reg temp, Assembler::Label&);
  170. template<typename CodegenI32, typename CodegenDouble, typename CodegenValue>
  171. void branch_if_both_numbers(Assembler::Reg lhs, Assembler::Reg rhs, CodegenI32, CodegenDouble, CodegenValue);
  172. explicit Compiler(Bytecode::Executable& bytecode_executable)
  173. : m_bytecode_executable(bytecode_executable)
  174. {
  175. }
  176. Assembler::Label& label_for(Bytecode::BasicBlock const& block)
  177. {
  178. return block_data_for(block).label;
  179. }
  180. struct BasicBlockData {
  181. size_t start_offset { 0 };
  182. Assembler::Label label;
  183. };
  184. BasicBlockData& block_data_for(Bytecode::BasicBlock const& block)
  185. {
  186. return *m_basic_block_data.ensure(&block, [] {
  187. return make<BasicBlockData>();
  188. });
  189. }
  190. void set_current_block(Bytecode::BasicBlock const& block)
  191. {
  192. m_current_block = &block;
  193. }
  194. Bytecode::BasicBlock const& current_block()
  195. {
  196. return *m_current_block;
  197. }
  198. HashMap<Bytecode::BasicBlock const*, NonnullOwnPtr<BasicBlockData>> m_basic_block_data;
  199. Vector<u8> m_output;
  200. Assembler m_assembler { m_output };
  201. Assembler::Label m_exit_label;
  202. Bytecode::Executable& m_bytecode_executable;
  203. Bytecode::BasicBlock const* m_current_block;
  204. };
  205. }
  206. #else
  207. namespace JS::JIT {
  208. class Compiler {
  209. public:
  210. static OwnPtr<NativeExecutable> compile(Bytecode::Executable&) { return nullptr; }
  211. };
  212. }
  213. #endif