Compiler.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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_COMMON_UNARY_OPS_WITHOUT_FAST_PATH(O) \
  48. O(BitwiseNot, bitwise_not) \
  49. O(Not, not_) \
  50. O(UnaryPlus, unary_plus) \
  51. O(Typeof, typeof_)
  52. # define JS_ENUMERATE_COMPARISON_OPS(O) \
  53. O(LessThan, less_than, SignedLessThan, Below) \
  54. O(LessThanEquals, less_than_equals, SignedLessThanOrEqualTo, BelowOrEqual) \
  55. O(GreaterThan, greater_than, SignedGreaterThan, Above) \
  56. O(GreaterThanEquals, greater_than_equals, SignedGreaterThanOrEqualTo, AboveOrEqual)
  57. # define JS_ENUMERATE_NEW_BUILTIN_ERROR_BYTECODE_OPS(O) \
  58. O(NewTypeError, new_type_error, TypeError)
  59. # define JS_ENUMERATE_IMPLEMENTED_JIT_OPS(O) \
  60. JS_ENUMERATE_COMMON_BINARY_OPS(O) \
  61. JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  62. JS_ENUMERATE_NEW_BUILTIN_ERROR_BYTECODE_OPS(O) \
  63. O(LoadImmediate, load_immediate) \
  64. O(Load, load) \
  65. O(Store, store) \
  66. O(GetLocal, get_local) \
  67. O(SetLocal, set_local) \
  68. O(TypeofLocal, typeof_local) \
  69. O(Jump, jump) \
  70. O(JumpConditional, jump_conditional) \
  71. O(JumpNullish, jump_nullish) \
  72. O(JumpUndefined, jump_undefined) \
  73. O(Increment, increment) \
  74. O(Decrement, decrement) \
  75. O(EnterUnwindContext, enter_unwind_context) \
  76. O(LeaveUnwindContext, leave_unwind_context) \
  77. O(Throw, throw) \
  78. O(Catch, catch) \
  79. O(CreateLexicalEnvironment, create_lexical_environment) \
  80. O(LeaveLexicalEnvironment, leave_lexical_environment) \
  81. O(EnterObjectEnvironment, enter_object_environment) \
  82. O(ToNumeric, to_numeric) \
  83. O(ResolveThisBinding, resolve_this_binding) \
  84. O(Return, return) \
  85. O(NewString, new_string) \
  86. O(NewObject, new_object) \
  87. O(NewArray, new_array) \
  88. O(NewPrimitiveArray, new_primitive_array) \
  89. O(NewFunction, new_function) \
  90. O(NewRegExp, new_regexp) \
  91. O(NewBigInt, new_bigint) \
  92. O(NewClass, new_class) \
  93. O(CreateVariable, create_variable) \
  94. O(GetById, get_by_id) \
  95. O(GetByValue, get_by_value) \
  96. O(GetGlobal, get_global) \
  97. O(GetVariable, get_variable) \
  98. O(GetCalleeAndThisFromEnvironment, get_callee_and_this_from_environment) \
  99. O(PutById, put_by_id) \
  100. O(PutByValue, put_by_value) \
  101. O(Call, call) \
  102. O(CallWithArgumentArray, call_with_argument_array) \
  103. O(TypeofVariable, typeof_variable) \
  104. O(SetVariable, set_variable) \
  105. O(ContinuePendingUnwind, continue_pending_unwind) \
  106. O(ConcatString, concat_string) \
  107. O(BlockDeclarationInstantiation, block_declaration_instantiation) \
  108. O(SuperCallWithArgumentArray, super_call_with_argument_array) \
  109. O(GetIterator, get_iterator) \
  110. O(GetObjectFromIteratorRecord, get_object_from_iterator_record) \
  111. O(GetNextMethodFromIteratorRecord, get_next_method_from_iterator_record) \
  112. O(IteratorNext, iterator_next) \
  113. O(ThrowIfNotObject, throw_if_not_object) \
  114. O(ThrowIfNullish, throw_if_nullish) \
  115. O(IteratorClose, iterator_close) \
  116. O(IteratorToArray, iterator_to_array) \
  117. O(Append, append) \
  118. O(DeleteById, delete_by_id) \
  119. O(DeleteByValue, delete_by_value) \
  120. O(DeleteByValueWithThis, delete_by_value_with_this) \
  121. O(GetObjectPropertyIterator, get_object_property_iterator) \
  122. O(GetPrivateById, get_private_by_id) \
  123. O(ResolveSuperBase, resolve_super_base) \
  124. O(GetByIdWithThis, get_by_id_with_this) \
  125. O(GetByValueWithThis, get_by_value_with_this) \
  126. O(DeleteByIdWithThis, delete_by_id_with_this) \
  127. O(PutByIdWithThis, put_by_id_with_this) \
  128. O(PutPrivateById, put_private_by_id) \
  129. O(ImportCall, import_call) \
  130. O(GetImportMeta, get_import_meta) \
  131. O(DeleteVariable, delete_variable) \
  132. O(GetMethod, get_method) \
  133. O(GetNewTarget, get_new_target) \
  134. O(HasPrivateId, has_private_id) \
  135. O(PutByValueWithThis, put_by_value_with_this) \
  136. O(CopyObjectExcludingProperties, copy_object_excluding_properties) \
  137. O(AsyncIteratorClose, async_iterator_close) \
  138. O(Yield, yield) \
  139. O(Await, await)
  140. # define DECLARE_COMPILE_OP(OpTitleCase, op_snake_case, ...) \
  141. void compile_##op_snake_case(Bytecode::Op::OpTitleCase const&);
  142. JS_ENUMERATE_IMPLEMENTED_JIT_OPS(DECLARE_COMPILE_OP)
  143. # undef DECLARE_COMPILE_OP
  144. void compile_builtin(Bytecode::Builtin, Assembler::Label& slow_case, Assembler::Label& end);
  145. # define DECLARE_COMPILE_BUILTIN(name, snake_case_name, ...) \
  146. void compile_builtin_##snake_case_name(Assembler::Label& slow_case, Assembler::Label& end);
  147. JS_ENUMERATE_BUILTINS(DECLARE_COMPILE_BUILTIN)
  148. # undef DECLARE_COMPILE_BUILTIN
  149. void store_vm_register(Bytecode::Register, Assembler::Reg);
  150. void load_vm_register(Assembler::Reg, Bytecode::Register);
  151. void store_vm_local(size_t, Assembler::Reg);
  152. void load_vm_local(Assembler::Reg, size_t);
  153. void reload_cached_accumulator();
  154. void flush_cached_accumulator();
  155. void load_accumulator(Assembler::Reg);
  156. void store_accumulator(Assembler::Reg);
  157. void compile_continuation(Optional<Bytecode::Label>, bool is_await);
  158. template<typename Codegen>
  159. void branch_if_same_type_for_equality(Assembler::Reg, Assembler::Reg, Codegen);
  160. void compile_is_strictly_equal(Assembler::Reg, Assembler::Reg, Assembler::Label& slow_case);
  161. void check_exception();
  162. void handle_exception();
  163. void jump_to_exit();
  164. void native_call(void* function_address, Vector<Assembler::Operand> const& stack_arguments = {});
  165. void jump_if_int32(Assembler::Reg, Assembler::Label&);
  166. template<typename Codegen>
  167. void branch_if_type(Assembler::Reg, u16 type_tag, Codegen);
  168. template<typename Codegen>
  169. void branch_if_int32(Assembler::Reg reg, Codegen codegen)
  170. {
  171. branch_if_type(reg, INT32_TAG, codegen);
  172. }
  173. template<typename Codegen>
  174. void branch_if_boolean(Assembler::Reg reg, Codegen codegen)
  175. {
  176. branch_if_type(reg, BOOLEAN_TAG, codegen);
  177. }
  178. template<typename Codegen>
  179. void branch_if_object(Assembler::Reg reg, Codegen codegen)
  180. {
  181. branch_if_type(reg, OBJECT_TAG, codegen);
  182. }
  183. void extract_object_pointer(Assembler::Reg dst_object, Assembler::Reg src_value);
  184. void convert_to_double(Assembler::Reg dst, Assembler::Reg src, Assembler::Reg nan, Assembler::Reg temp, Assembler::Label& not_number);
  185. template<typename Codegen>
  186. void branch_if_both_int32(Assembler::Reg, Assembler::Reg, Codegen);
  187. void jump_if_not_double(Assembler::Reg reg, Assembler::Reg nan, Assembler::Reg temp, Assembler::Label&);
  188. template<typename CodegenI32, typename CodegenDouble, typename CodegenValue>
  189. void compile_binary_op_fastpaths(Assembler::Reg lhs, Assembler::Reg rhs, CodegenI32, CodegenDouble, CodegenValue);
  190. template<typename CodegenI32, typename CodegenDouble, typename CodegenValue>
  191. void compiler_comparison_fastpaths(Assembler::Reg lhs, Assembler::Reg rhs, CodegenI32, CodegenDouble, CodegenValue);
  192. explicit Compiler(Bytecode::Executable& bytecode_executable)
  193. : m_bytecode_executable(bytecode_executable)
  194. {
  195. }
  196. Assembler::Label& label_for(Bytecode::BasicBlock const& block)
  197. {
  198. return block_data_for(block).label;
  199. }
  200. struct BasicBlockData {
  201. size_t start_offset { 0 };
  202. Assembler::Label label;
  203. };
  204. BasicBlockData& block_data_for(Bytecode::BasicBlock const& block)
  205. {
  206. return *m_basic_block_data.ensure(&block, [] {
  207. return make<BasicBlockData>();
  208. });
  209. }
  210. void set_current_block(Bytecode::BasicBlock const& block)
  211. {
  212. m_current_block = &block;
  213. }
  214. Bytecode::BasicBlock const& current_block()
  215. {
  216. return *m_current_block;
  217. }
  218. HashMap<Bytecode::BasicBlock const*, NonnullOwnPtr<BasicBlockData>> m_basic_block_data;
  219. Vector<u8> m_output;
  220. Assembler m_assembler { m_output };
  221. Assembler::Label m_exit_label;
  222. Bytecode::Executable& m_bytecode_executable;
  223. Bytecode::BasicBlock const* m_current_block;
  224. };
  225. }
  226. #else
  227. namespace JS::JIT {
  228. class Compiler {
  229. public:
  230. static OwnPtr<NativeExecutable> compile(Bytecode::Executable&) { return nullptr; }
  231. };
  232. }
  233. #endif