Compiler.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/OwnPtr.h>
  7. #include <LibJS/Bytecode/CommonImplementations.h>
  8. #include <LibJS/Bytecode/Instruction.h>
  9. #include <LibJS/Bytecode/Interpreter.h>
  10. #include <LibJS/JIT/Compiler.h>
  11. #include <LibJS/Runtime/VM.h>
  12. #include <LibJS/Runtime/ValueInlines.h>
  13. #include <sys/mman.h>
  14. #include <unistd.h>
  15. #define TRY_OR_SET_EXCEPTION(expression) \
  16. ({ \
  17. /* Ignore -Wshadow to allow nesting the macro. */ \
  18. AK_IGNORE_DIAGNOSTIC("-Wshadow", \
  19. auto&& _temporary_result = (expression)); \
  20. static_assert(!::AK::Detail::IsLvalueReference<decltype(_temporary_result.release_value())>, \
  21. "Do not return a reference from a fallible expression"); \
  22. if (_temporary_result.is_error()) [[unlikely]] { \
  23. vm.bytecode_interpreter().reg(Bytecode::Register::exception()) = _temporary_result.release_error().value().value(); \
  24. return {}; \
  25. } \
  26. _temporary_result.release_value(); \
  27. })
  28. namespace JS::JIT {
  29. void Compiler::store_vm_register(Bytecode::Register dst, Assembler::Reg src)
  30. {
  31. m_assembler.mov(
  32. Assembler::Operand::Mem64BaseAndOffset(REGISTER_ARRAY_BASE, dst.index() * sizeof(Value)),
  33. Assembler::Operand::Register(src));
  34. }
  35. void Compiler::load_vm_register(Assembler::Reg dst, Bytecode::Register src)
  36. {
  37. m_assembler.mov(
  38. Assembler::Operand::Register(dst),
  39. Assembler::Operand::Mem64BaseAndOffset(REGISTER_ARRAY_BASE, src.index() * sizeof(Value)));
  40. }
  41. void Compiler::store_vm_local(size_t dst, Assembler::Reg src)
  42. {
  43. m_assembler.mov(
  44. Assembler::Operand::Mem64BaseAndOffset(LOCALS_ARRAY_BASE, dst * sizeof(Value)),
  45. Assembler::Operand::Register(src));
  46. }
  47. void Compiler::load_vm_local(Assembler::Reg dst, size_t src)
  48. {
  49. m_assembler.mov(
  50. Assembler::Operand::Register(dst),
  51. Assembler::Operand::Mem64BaseAndOffset(LOCALS_ARRAY_BASE, src * sizeof(Value)));
  52. }
  53. void Compiler::compile_load_immediate(Bytecode::Op::LoadImmediate const& op)
  54. {
  55. m_assembler.mov(
  56. Assembler::Operand::Register(GPR0),
  57. Assembler::Operand::Imm64(op.value().encoded()));
  58. store_vm_register(Bytecode::Register::accumulator(), GPR0);
  59. }
  60. void Compiler::compile_load(Bytecode::Op::Load const& op)
  61. {
  62. load_vm_register(GPR0, op.src());
  63. store_vm_register(Bytecode::Register::accumulator(), GPR0);
  64. }
  65. void Compiler::compile_store(Bytecode::Op::Store const& op)
  66. {
  67. load_vm_register(GPR0, Bytecode::Register::accumulator());
  68. store_vm_register(op.dst(), GPR0);
  69. }
  70. void Compiler::compile_get_local(Bytecode::Op::GetLocal const& op)
  71. {
  72. load_vm_local(GPR0, op.index());
  73. store_vm_register(Bytecode::Register::accumulator(), GPR0);
  74. }
  75. void Compiler::compile_set_local(Bytecode::Op::SetLocal const& op)
  76. {
  77. load_vm_register(GPR0, Bytecode::Register::accumulator());
  78. store_vm_local(op.index(), GPR0);
  79. }
  80. void Compiler::compile_jump(Bytecode::Op::Jump const& op)
  81. {
  82. m_assembler.jump(const_cast<Bytecode::BasicBlock&>(op.true_target()->block()));
  83. }
  84. static bool cxx_to_boolean(VM&, Value value)
  85. {
  86. return value.to_boolean();
  87. }
  88. void Compiler::compile_to_boolean(Assembler::Reg dst, Assembler::Reg src)
  89. {
  90. // dst = src;
  91. m_assembler.mov(
  92. Assembler::Operand::Register(dst),
  93. Assembler::Operand::Register(src));
  94. // dst >>= 48;
  95. m_assembler.shift_right(
  96. Assembler::Operand::Register(dst),
  97. Assembler::Operand::Imm8(48));
  98. // if (dst != BOOLEAN_TAG) goto slow_case;
  99. auto slow_case = m_assembler.make_label();
  100. m_assembler.jump_if_not_equal(
  101. Assembler::Operand::Register(dst),
  102. Assembler::Operand::Imm32(BOOLEAN_TAG),
  103. slow_case);
  104. // Fast path for JS::Value booleans.
  105. // dst = src;
  106. m_assembler.mov(
  107. Assembler::Operand::Register(dst),
  108. Assembler::Operand::Register(src));
  109. // dst &= 1;
  110. m_assembler.bitwise_and(
  111. Assembler::Operand::Register(dst),
  112. Assembler::Operand::Imm32(1));
  113. // goto end;
  114. auto end = m_assembler.jump();
  115. // slow_case: // call C++ helper
  116. slow_case.link(m_assembler);
  117. m_assembler.mov(
  118. Assembler::Operand::Register(ARG1),
  119. Assembler::Operand::Register(src));
  120. m_assembler.native_call((void*)cxx_to_boolean);
  121. m_assembler.mov(
  122. Assembler::Operand::Register(dst),
  123. Assembler::Operand::Register(RET));
  124. // end:
  125. end.link(m_assembler);
  126. }
  127. void Compiler::compile_jump_conditional(Bytecode::Op::JumpConditional const& op)
  128. {
  129. load_vm_register(GPR1, Bytecode::Register::accumulator());
  130. compile_to_boolean(GPR0, GPR1);
  131. m_assembler.jump_conditional(GPR0,
  132. const_cast<Bytecode::BasicBlock&>(op.true_target()->block()),
  133. const_cast<Bytecode::BasicBlock&>(op.false_target()->block()));
  134. }
  135. [[maybe_unused]] static Value cxx_increment(VM& vm, Value value)
  136. {
  137. auto old_value = TRY_OR_SET_EXCEPTION(value.to_numeric(vm));
  138. if (old_value.is_number())
  139. return Value(old_value.as_double() + 1);
  140. return BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  141. }
  142. void Compiler::compile_increment(Bytecode::Op::Increment const&)
  143. {
  144. load_vm_register(ARG1, Bytecode::Register::accumulator());
  145. m_assembler.native_call((void*)cxx_increment);
  146. store_vm_register(Bytecode::Register::accumulator(), RET);
  147. check_exception();
  148. }
  149. static Value cxx_decrement(VM& vm, Value value)
  150. {
  151. auto old_value = TRY_OR_SET_EXCEPTION(value.to_numeric(vm));
  152. if (old_value.is_number())
  153. return Value(old_value.as_double() - 1);
  154. return BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  155. }
  156. void Compiler::compile_decrement(Bytecode::Op::Decrement const&)
  157. {
  158. load_vm_register(ARG1, Bytecode::Register::accumulator());
  159. m_assembler.native_call((void*)cxx_decrement);
  160. store_vm_register(Bytecode::Register::accumulator(), RET);
  161. check_exception();
  162. }
  163. void Compiler::check_exception()
  164. {
  165. // if (exception.is_empty()) goto no_exception;
  166. load_vm_register(GPR0, Bytecode::Register::exception());
  167. m_assembler.mov(Assembler::Operand::Register(GPR1), Assembler::Operand::Imm64(Value().encoded()));
  168. auto no_exception = m_assembler.make_label();
  169. m_assembler.jump_if_equal(Assembler::Operand::Register(GPR0), Assembler::Operand::Register(GPR1), no_exception);
  170. // We have an exception!
  171. // if (!unwind_context.valid) return;
  172. auto handle_exception = m_assembler.make_label();
  173. m_assembler.mov(
  174. Assembler::Operand::Register(GPR0),
  175. Assembler::Operand::Mem64BaseAndOffset(UNWIND_CONTEXT_BASE, 0));
  176. m_assembler.jump_if_not_equal(
  177. Assembler::Operand::Register(GPR0),
  178. Assembler::Operand::Imm32(0),
  179. handle_exception);
  180. m_assembler.exit();
  181. // handle_exception:
  182. handle_exception.link(m_assembler);
  183. // if (unwind_context.handler) {
  184. // accumulator = exception;
  185. // exception = Value();
  186. // goto handler;
  187. // }
  188. auto no_handler = m_assembler.make_label();
  189. m_assembler.mov(
  190. Assembler::Operand::Register(GPR0),
  191. Assembler::Operand::Mem64BaseAndOffset(UNWIND_CONTEXT_BASE, 8));
  192. m_assembler.jump_if_equal(
  193. Assembler::Operand::Register(GPR0),
  194. Assembler::Operand::Imm32(0),
  195. no_handler);
  196. load_vm_register(GPR1, Bytecode::Register::exception());
  197. store_vm_register(Bytecode::Register::accumulator(), GPR1);
  198. m_assembler.mov(
  199. Assembler::Operand::Register(GPR1),
  200. Assembler::Operand::Imm64(Value().encoded()));
  201. store_vm_register(Bytecode::Register::exception(), GPR1);
  202. m_assembler.jump(Assembler::Operand::Register(GPR0));
  203. // no_handler:
  204. no_handler.link(m_assembler);
  205. // if (unwind_context.finalizer) goto finalizer;
  206. auto no_finalizer = m_assembler.make_label();
  207. m_assembler.mov(
  208. Assembler::Operand::Register(GPR0),
  209. Assembler::Operand::Mem64BaseAndOffset(UNWIND_CONTEXT_BASE, 16));
  210. m_assembler.jump_if_equal(
  211. Assembler::Operand::Register(GPR0),
  212. Assembler::Operand::Imm32(0),
  213. no_finalizer);
  214. m_assembler.jump(Assembler::Operand::Register(GPR0));
  215. // no_finalizer:
  216. // NOTE: No catch and no finally!? Crash.
  217. no_finalizer.link(m_assembler);
  218. m_assembler.verify_not_reached();
  219. // no_exception:
  220. no_exception.link(m_assembler);
  221. }
  222. void Compiler::push_unwind_context(bool valid, Optional<Bytecode::Label> const& handler, Optional<Bytecode::Label> const& finalizer)
  223. {
  224. // Put this on the stack, and then point UNWIND_CONTEXT_BASE at it.
  225. // struct {
  226. // u64 valid;
  227. // u64 handler;
  228. // u64 finalizer;
  229. // };
  230. // push finalizer (patched later)
  231. m_assembler.mov(
  232. Assembler::Operand::Register(GPR0),
  233. Assembler::Operand::Imm64(0));
  234. if (finalizer.has_value())
  235. const_cast<Bytecode::BasicBlock&>(finalizer.value().block()).absolute_references_to_here.append(m_assembler.m_output.size() - 8);
  236. m_assembler.push(Assembler::Operand::Register(GPR0));
  237. // push handler (patched later)
  238. m_assembler.mov(
  239. Assembler::Operand::Register(GPR0),
  240. Assembler::Operand::Imm64(0));
  241. if (handler.has_value())
  242. const_cast<Bytecode::BasicBlock&>(handler.value().block()).absolute_references_to_here.append(m_assembler.m_output.size() - 8);
  243. m_assembler.push(Assembler::Operand::Register(GPR0));
  244. // push valid
  245. m_assembler.push(Assembler::Operand::Imm32(valid));
  246. // UNWIND_CONTEXT_BASE = STACK_POINTER
  247. m_assembler.mov(
  248. Assembler::Operand::Register(UNWIND_CONTEXT_BASE),
  249. Assembler::Operand::Register(STACK_POINTER));
  250. // align stack pointer
  251. m_assembler.sub(Assembler::Operand::Register(STACK_POINTER), Assembler::Operand::Imm8(8));
  252. }
  253. void Compiler::pop_unwind_context()
  254. {
  255. m_assembler.add(Assembler::Operand::Register(STACK_POINTER), Assembler::Operand::Imm8(32));
  256. m_assembler.add(Assembler::Operand::Register(UNWIND_CONTEXT_BASE), Assembler::Operand::Imm8(32));
  257. }
  258. void Compiler::compile_enter_unwind_context(Bytecode::Op::EnterUnwindContext const& op)
  259. {
  260. push_unwind_context(true, op.handler_target(), op.finalizer_target());
  261. m_assembler.jump(const_cast<Bytecode::BasicBlock&>(op.entry_point().block()));
  262. }
  263. void Compiler::compile_leave_unwind_context(Bytecode::Op::LeaveUnwindContext const&)
  264. {
  265. pop_unwind_context();
  266. }
  267. void Compiler::compile_throw(Bytecode::Op::Throw const&)
  268. {
  269. load_vm_register(GPR0, Bytecode::Register::accumulator());
  270. store_vm_register(Bytecode::Register::exception(), GPR0);
  271. check_exception();
  272. }
  273. static ThrowCompletionOr<Value> abstract_inequals(VM& vm, Value src1, Value src2)
  274. {
  275. return Value(!TRY(is_loosely_equal(vm, src1, src2)));
  276. }
  277. static ThrowCompletionOr<Value> abstract_equals(VM& vm, Value src1, Value src2)
  278. {
  279. return Value(TRY(is_loosely_equal(vm, src1, src2)));
  280. }
  281. static ThrowCompletionOr<Value> typed_inequals(VM&, Value src1, Value src2)
  282. {
  283. return Value(!is_strictly_equal(src1, src2));
  284. }
  285. static ThrowCompletionOr<Value> typed_equals(VM&, Value src1, Value src2)
  286. {
  287. return Value(is_strictly_equal(src1, src2));
  288. }
  289. #define DO_COMPILE_COMMON_BINARY_OP(TitleCaseName, snake_case_name) \
  290. static Value cxx_##snake_case_name(VM& vm, Value lhs, Value rhs) \
  291. { \
  292. return TRY_OR_SET_EXCEPTION(snake_case_name(vm, lhs, rhs)); \
  293. } \
  294. \
  295. void Compiler::compile_##snake_case_name(Bytecode::Op::TitleCaseName const& op) \
  296. { \
  297. load_vm_register(ARG1, op.lhs()); \
  298. load_vm_register(ARG2, Bytecode::Register::accumulator()); \
  299. m_assembler.native_call((void*)cxx_##snake_case_name); \
  300. store_vm_register(Bytecode::Register::accumulator(), RET); \
  301. check_exception(); \
  302. }
  303. JS_ENUMERATE_COMMON_BINARY_OPS(DO_COMPILE_COMMON_BINARY_OP)
  304. #undef DO_COMPILE_COMMON_BINARY_OP
  305. static ThrowCompletionOr<Value> not_(VM&, Value value)
  306. {
  307. return Value(!value.to_boolean());
  308. }
  309. static ThrowCompletionOr<Value> typeof_(VM& vm, Value value)
  310. {
  311. return PrimitiveString::create(vm, value.typeof());
  312. }
  313. #define DO_COMPILE_COMMON_UNARY_OP(TitleCaseName, snake_case_name) \
  314. static Value cxx_##snake_case_name(VM& vm, Value value) \
  315. { \
  316. return TRY_OR_SET_EXCEPTION(snake_case_name(vm, value)); \
  317. } \
  318. \
  319. void Compiler::compile_##snake_case_name(Bytecode::Op::TitleCaseName const&) \
  320. { \
  321. load_vm_register(ARG1, Bytecode::Register::accumulator()); \
  322. m_assembler.native_call((void*)cxx_##snake_case_name); \
  323. store_vm_register(Bytecode::Register::accumulator(), RET); \
  324. check_exception(); \
  325. }
  326. JS_ENUMERATE_COMMON_UNARY_OPS(DO_COMPILE_COMMON_UNARY_OP)
  327. #undef DO_COMPILE_COMMON_UNARY_OP
  328. void Compiler::compile_return(Bytecode::Op::Return const&)
  329. {
  330. load_vm_register(GPR0, Bytecode::Register::accumulator());
  331. store_vm_register(Bytecode::Register::return_value(), GPR0);
  332. m_assembler.exit();
  333. }
  334. static Value cxx_new_string(VM& vm, DeprecatedString const& string)
  335. {
  336. return PrimitiveString::create(vm, string);
  337. }
  338. void Compiler::compile_new_string(Bytecode::Op::NewString const& op)
  339. {
  340. auto const& string = m_bytecode_executable.string_table->get(op.index());
  341. m_assembler.mov(
  342. Assembler::Operand::Register(ARG1),
  343. Assembler::Operand::Imm64(bit_cast<u64>(&string)));
  344. m_assembler.native_call((void*)cxx_new_string);
  345. store_vm_register(Bytecode::Register::accumulator(), RET);
  346. }
  347. static Value cxx_new_object(VM& vm)
  348. {
  349. auto& realm = *vm.current_realm();
  350. return Object::create(realm, realm.intrinsics().object_prototype());
  351. }
  352. void Compiler::compile_new_object(Bytecode::Op::NewObject const&)
  353. {
  354. m_assembler.native_call((void*)cxx_new_object);
  355. store_vm_register(Bytecode::Register::accumulator(), RET);
  356. }
  357. static Value cxx_get_by_id(VM& vm, Value base, Bytecode::IdentifierTableIndex property, u32 cache_index)
  358. {
  359. return TRY_OR_SET_EXCEPTION(Bytecode::get_by_id(vm.bytecode_interpreter(), property, base, base, cache_index));
  360. }
  361. void Compiler::compile_get_by_id(Bytecode::Op::GetById const& op)
  362. {
  363. load_vm_register(ARG1, Bytecode::Register::accumulator());
  364. m_assembler.mov(
  365. Assembler::Operand::Register(ARG2),
  366. Assembler::Operand::Imm64(op.property().value()));
  367. m_assembler.mov(
  368. Assembler::Operand::Register(ARG3),
  369. Assembler::Operand::Imm64(op.cache_index()));
  370. m_assembler.native_call((void*)cxx_get_by_id);
  371. store_vm_register(Bytecode::Register::accumulator(), RET);
  372. check_exception();
  373. }
  374. static Value cxx_get_by_value(VM& vm, Value base, Value property)
  375. {
  376. return TRY_OR_SET_EXCEPTION(Bytecode::get_by_value(vm.bytecode_interpreter(), base, property));
  377. }
  378. void Compiler::compile_get_by_value(Bytecode::Op::GetByValue const& op)
  379. {
  380. load_vm_register(ARG1, op.base());
  381. load_vm_register(ARG2, Bytecode::Register::accumulator());
  382. m_assembler.native_call((void*)cxx_get_by_value);
  383. store_vm_register(Bytecode::Register::accumulator(), RET);
  384. check_exception();
  385. }
  386. static Value cxx_get_global(VM& vm, Bytecode::IdentifierTableIndex identifier, u32 cache_index)
  387. {
  388. return TRY_OR_SET_EXCEPTION(Bytecode::get_global(vm.bytecode_interpreter(), identifier, cache_index));
  389. }
  390. void Compiler::compile_get_global(Bytecode::Op::GetGlobal const& op)
  391. {
  392. m_assembler.mov(
  393. Assembler::Operand::Register(ARG1),
  394. Assembler::Operand::Imm64(op.identifier().value()));
  395. m_assembler.mov(
  396. Assembler::Operand::Register(ARG2),
  397. Assembler::Operand::Imm64(op.cache_index()));
  398. m_assembler.native_call((void*)cxx_get_global);
  399. store_vm_register(Bytecode::Register::accumulator(), RET);
  400. check_exception();
  401. }
  402. static Value cxx_to_numeric(VM& vm, Value value)
  403. {
  404. return TRY_OR_SET_EXCEPTION(value.to_numeric(vm));
  405. }
  406. void Compiler::compile_to_numeric(Bytecode::Op::ToNumeric const&)
  407. {
  408. load_vm_register(ARG1, Bytecode::Register::accumulator());
  409. m_assembler.native_call((void*)cxx_to_numeric);
  410. store_vm_register(Bytecode::Register::accumulator(), RET);
  411. check_exception();
  412. }
  413. static Value cxx_resolve_this_binding(VM& vm)
  414. {
  415. return TRY_OR_SET_EXCEPTION(vm.resolve_this_binding());
  416. }
  417. void Compiler::compile_resolve_this_binding(Bytecode::Op::ResolveThisBinding const&)
  418. {
  419. m_assembler.native_call((void*)cxx_resolve_this_binding);
  420. store_vm_register(Bytecode::Register::accumulator(), RET);
  421. check_exception();
  422. }
  423. static Value cxx_put_by_id(VM& vm, Value base, Bytecode::IdentifierTableIndex property, Value value, Bytecode::Op::PropertyKind kind)
  424. {
  425. PropertyKey name = vm.bytecode_interpreter().current_executable().get_identifier(property);
  426. TRY_OR_SET_EXCEPTION(Bytecode::put_by_property_key(vm, base, base, value, name, kind));
  427. return {};
  428. }
  429. void Compiler::compile_put_by_id(Bytecode::Op::PutById const& op)
  430. {
  431. load_vm_register(ARG1, op.base());
  432. m_assembler.mov(
  433. Assembler::Operand::Register(ARG2),
  434. Assembler::Operand::Imm64(op.property().value()));
  435. load_vm_register(ARG3, Bytecode::Register::accumulator());
  436. m_assembler.mov(
  437. Assembler::Operand::Register(ARG4),
  438. Assembler::Operand::Imm64(to_underlying(op.kind())));
  439. m_assembler.native_call((void*)cxx_put_by_id);
  440. check_exception();
  441. }
  442. static Value cxx_call(VM& vm, Value callee, u32 first_argument_index, u32 argument_count, Value this_value, Bytecode::Op::CallType call_type)
  443. {
  444. // FIXME: Uncomment this and deal with it.
  445. // TRY_OR_SET_EXCEPTION(throw_if_needed_for_call(vm.bytecode_interpreter(), *this, callee));
  446. MarkedVector<Value> argument_values(vm.heap());
  447. argument_values.ensure_capacity(argument_count);
  448. for (u32 i = 0; i < argument_count; ++i) {
  449. argument_values.unchecked_append(vm.bytecode_interpreter().reg(Bytecode::Register { first_argument_index + i }));
  450. }
  451. return TRY_OR_SET_EXCEPTION(perform_call(vm.bytecode_interpreter(), this_value, call_type, callee, move(argument_values)));
  452. }
  453. void Compiler::compile_call(Bytecode::Op::Call const& op)
  454. {
  455. load_vm_register(ARG1, op.callee());
  456. m_assembler.mov(
  457. Assembler::Operand::Register(ARG2),
  458. Assembler::Operand::Imm64(op.first_argument().index()));
  459. m_assembler.mov(
  460. Assembler::Operand::Register(ARG3),
  461. Assembler::Operand::Imm64(op.argument_count()));
  462. load_vm_register(ARG4, op.this_value());
  463. m_assembler.mov(
  464. Assembler::Operand::Register(ARG5),
  465. Assembler::Operand::Imm64(to_underlying(op.call_type())));
  466. m_assembler.native_call((void*)cxx_call);
  467. store_vm_register(Bytecode::Register::accumulator(), RET);
  468. check_exception();
  469. }
  470. OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_executable)
  471. {
  472. if (getenv("LIBJS_NO_JIT"))
  473. return nullptr;
  474. Compiler compiler { bytecode_executable };
  475. compiler.m_assembler.enter();
  476. compiler.m_assembler.mov(
  477. Assembler::Operand::Register(REGISTER_ARRAY_BASE),
  478. Assembler::Operand::Register(ARG1));
  479. compiler.m_assembler.mov(
  480. Assembler::Operand::Register(LOCALS_ARRAY_BASE),
  481. Assembler::Operand::Register(ARG2));
  482. compiler.push_unwind_context(false, {}, {});
  483. for (auto& block : bytecode_executable.basic_blocks) {
  484. block->offset = compiler.m_output.size();
  485. auto it = Bytecode::InstructionStreamIterator(block->instruction_stream());
  486. while (!it.at_end()) {
  487. auto const& op = *it;
  488. switch (op.type()) {
  489. case Bytecode::Instruction::Type::LoadImmediate:
  490. compiler.compile_load_immediate(static_cast<Bytecode::Op::LoadImmediate const&>(op));
  491. break;
  492. case Bytecode::Instruction::Type::Store:
  493. compiler.compile_store(static_cast<Bytecode::Op::Store const&>(op));
  494. break;
  495. case Bytecode::Instruction::Type::Load:
  496. compiler.compile_load(static_cast<Bytecode::Op::Load const&>(op));
  497. break;
  498. case Bytecode::Instruction::Type::GetLocal:
  499. compiler.compile_get_local(static_cast<Bytecode::Op::GetLocal const&>(op));
  500. break;
  501. case Bytecode::Instruction::Type::SetLocal:
  502. compiler.compile_set_local(static_cast<Bytecode::Op::SetLocal const&>(op));
  503. break;
  504. case Bytecode::Instruction::Type::Jump:
  505. compiler.compile_jump(static_cast<Bytecode::Op::Jump const&>(op));
  506. break;
  507. case Bytecode::Instruction::Type::JumpConditional:
  508. compiler.compile_jump_conditional(static_cast<Bytecode::Op::JumpConditional const&>(op));
  509. break;
  510. case Bytecode::Instruction::Type::Increment:
  511. compiler.compile_increment(static_cast<Bytecode::Op::Increment const&>(op));
  512. break;
  513. case Bytecode::Instruction::Type::Decrement:
  514. compiler.compile_decrement(static_cast<Bytecode::Op::Decrement const&>(op));
  515. break;
  516. case Bytecode::Instruction::Type::EnterUnwindContext:
  517. compiler.compile_enter_unwind_context(static_cast<Bytecode::Op::EnterUnwindContext const&>(op));
  518. break;
  519. case Bytecode::Instruction::Type::LeaveUnwindContext:
  520. compiler.compile_leave_unwind_context(static_cast<Bytecode::Op::LeaveUnwindContext const&>(op));
  521. break;
  522. case Bytecode::Instruction::Type::Throw:
  523. compiler.compile_throw(static_cast<Bytecode::Op::Throw const&>(op));
  524. break;
  525. case Bytecode::Instruction::Type::Return:
  526. compiler.compile_return(static_cast<Bytecode::Op::Return const&>(op));
  527. break;
  528. case Bytecode::Instruction::Type::NewString:
  529. compiler.compile_new_string(static_cast<Bytecode::Op::NewString const&>(op));
  530. break;
  531. case Bytecode::Instruction::Type::NewObject:
  532. compiler.compile_new_object(static_cast<Bytecode::Op::NewObject const&>(op));
  533. break;
  534. case Bytecode::Instruction::Type::GetById:
  535. compiler.compile_get_by_id(static_cast<Bytecode::Op::GetById const&>(op));
  536. break;
  537. case Bytecode::Instruction::Type::GetByValue:
  538. compiler.compile_get_by_value(static_cast<Bytecode::Op::GetByValue const&>(op));
  539. break;
  540. case Bytecode::Instruction::Type::GetGlobal:
  541. compiler.compile_get_global(static_cast<Bytecode::Op::GetGlobal const&>(op));
  542. break;
  543. case Bytecode::Instruction::Type::PutById:
  544. compiler.compile_put_by_id(static_cast<Bytecode::Op::PutById const&>(op));
  545. break;
  546. case Bytecode::Instruction::Type::ToNumeric:
  547. compiler.compile_to_numeric(static_cast<Bytecode::Op::ToNumeric const&>(op));
  548. break;
  549. case Bytecode::Instruction::Type::ResolveThisBinding:
  550. compiler.compile_resolve_this_binding(static_cast<Bytecode::Op::ResolveThisBinding const&>(op));
  551. break;
  552. case Bytecode::Instruction::Type::Call:
  553. compiler.compile_call(static_cast<Bytecode::Op::Call const&>(op));
  554. break;
  555. #define DO_COMPILE_COMMON_BINARY_OP(TitleCaseName, snake_case_name) \
  556. case Bytecode::Instruction::Type::TitleCaseName: \
  557. compiler.compile_##snake_case_name(static_cast<Bytecode::Op::TitleCaseName const&>(op)); \
  558. break;
  559. JS_ENUMERATE_COMMON_BINARY_OPS(DO_COMPILE_COMMON_BINARY_OP)
  560. #undef DO_COMPILE_COMMON_BINARY_OP
  561. #define DO_COMPILE_COMMON_UNARY_OP(TitleCaseName, snake_case_name) \
  562. case Bytecode::Instruction::Type::TitleCaseName: \
  563. compiler.compile_##snake_case_name(static_cast<Bytecode::Op::TitleCaseName const&>(op)); \
  564. break;
  565. JS_ENUMERATE_COMMON_UNARY_OPS(DO_COMPILE_COMMON_UNARY_OP)
  566. #undef DO_COMPILE_COMMON_UNARY_OP
  567. default:
  568. dbgln("\033[31;1mJIT compilation failed\033[0m: {}", bytecode_executable.name);
  569. dbgln("Unsupported bytecode op: {}", op.to_deprecated_string(bytecode_executable));
  570. return nullptr;
  571. }
  572. ++it;
  573. }
  574. if (!block->is_terminated())
  575. compiler.m_assembler.exit();
  576. }
  577. auto* executable_memory = mmap(nullptr, compiler.m_output.size(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  578. if (executable_memory == MAP_FAILED) {
  579. perror("mmap");
  580. return nullptr;
  581. }
  582. for (auto& block : bytecode_executable.basic_blocks) {
  583. // Patch up all the jumps
  584. for (auto& jump : block->jumps_to_here) {
  585. auto offset = block->offset - jump - 4;
  586. compiler.m_output[jump + 0] = (offset >> 0) & 0xff;
  587. compiler.m_output[jump + 1] = (offset >> 8) & 0xff;
  588. compiler.m_output[jump + 2] = (offset >> 16) & 0xff;
  589. compiler.m_output[jump + 3] = (offset >> 24) & 0xff;
  590. }
  591. // Patch up all the absolute references
  592. for (auto& absolute_reference : block->absolute_references_to_here) {
  593. auto offset = bit_cast<u64>(executable_memory) + block->offset;
  594. compiler.m_output[absolute_reference + 0] = (offset >> 0) & 0xff;
  595. compiler.m_output[absolute_reference + 1] = (offset >> 8) & 0xff;
  596. compiler.m_output[absolute_reference + 2] = (offset >> 16) & 0xff;
  597. compiler.m_output[absolute_reference + 3] = (offset >> 24) & 0xff;
  598. compiler.m_output[absolute_reference + 4] = (offset >> 32) & 0xff;
  599. compiler.m_output[absolute_reference + 5] = (offset >> 40) & 0xff;
  600. compiler.m_output[absolute_reference + 6] = (offset >> 48) & 0xff;
  601. compiler.m_output[absolute_reference + 7] = (offset >> 56) & 0xff;
  602. }
  603. }
  604. size_t res = write(STDOUT_FILENO, compiler.m_output.data(), compiler.m_output.size());
  605. if (!res) { }
  606. memcpy(executable_memory, compiler.m_output.data(), compiler.m_output.size());
  607. mprotect(executable_memory, compiler.m_output.size(), PROT_READ | PROT_EXEC);
  608. dbgln("\033[32;1mJIT compilation succeeded!\033[0m {}", bytecode_executable.name);
  609. return make<NativeExecutable>(executable_memory, compiler.m_output.size());
  610. }
  611. }