Compiler.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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/AbstractOperations.h>
  12. #include <LibJS/Runtime/Array.h>
  13. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  14. #include <LibJS/Runtime/VM.h>
  15. #include <LibJS/Runtime/ValueInlines.h>
  16. #include <sys/mman.h>
  17. #include <unistd.h>
  18. #define LOG_JIT_SUCCESS 1
  19. #define LOG_JIT_FAILURE 1
  20. #define DUMP_JIT_MACHINE_CODE_TO_STDOUT 0
  21. #define TRY_OR_SET_EXCEPTION(expression) \
  22. ({ \
  23. /* Ignore -Wshadow to allow nesting the macro. */ \
  24. AK_IGNORE_DIAGNOSTIC("-Wshadow", \
  25. auto&& _temporary_result = (expression)); \
  26. static_assert(!::AK::Detail::IsLvalueReference<decltype(_temporary_result.release_value())>, \
  27. "Do not return a reference from a fallible expression"); \
  28. if (_temporary_result.is_error()) [[unlikely]] { \
  29. vm.bytecode_interpreter().reg(Bytecode::Register::exception()) = _temporary_result.release_error().value().value(); \
  30. return {}; \
  31. } \
  32. _temporary_result.release_value(); \
  33. })
  34. namespace JS::JIT {
  35. void Compiler::store_vm_register(Bytecode::Register dst, Assembler::Reg src)
  36. {
  37. m_assembler.mov(
  38. Assembler::Operand::Mem64BaseAndOffset(REGISTER_ARRAY_BASE, dst.index() * sizeof(Value)),
  39. Assembler::Operand::Register(src));
  40. }
  41. void Compiler::load_vm_register(Assembler::Reg dst, Bytecode::Register src)
  42. {
  43. m_assembler.mov(
  44. Assembler::Operand::Register(dst),
  45. Assembler::Operand::Mem64BaseAndOffset(REGISTER_ARRAY_BASE, src.index() * sizeof(Value)));
  46. }
  47. void Compiler::store_vm_local(size_t dst, Assembler::Reg src)
  48. {
  49. m_assembler.mov(
  50. Assembler::Operand::Mem64BaseAndOffset(LOCALS_ARRAY_BASE, dst * sizeof(Value)),
  51. Assembler::Operand::Register(src));
  52. }
  53. void Compiler::load_vm_local(Assembler::Reg dst, size_t src)
  54. {
  55. m_assembler.mov(
  56. Assembler::Operand::Register(dst),
  57. Assembler::Operand::Mem64BaseAndOffset(LOCALS_ARRAY_BASE, src * sizeof(Value)));
  58. }
  59. void Compiler::compile_load_immediate(Bytecode::Op::LoadImmediate const& op)
  60. {
  61. m_assembler.mov(
  62. Assembler::Operand::Register(GPR0),
  63. Assembler::Operand::Imm64(op.value().encoded()));
  64. store_vm_register(Bytecode::Register::accumulator(), GPR0);
  65. }
  66. void Compiler::compile_load(Bytecode::Op::Load const& op)
  67. {
  68. load_vm_register(GPR0, op.src());
  69. store_vm_register(Bytecode::Register::accumulator(), GPR0);
  70. }
  71. void Compiler::compile_store(Bytecode::Op::Store const& op)
  72. {
  73. load_vm_register(GPR0, Bytecode::Register::accumulator());
  74. store_vm_register(op.dst(), GPR0);
  75. }
  76. void Compiler::compile_get_local(Bytecode::Op::GetLocal const& op)
  77. {
  78. load_vm_local(GPR0, op.index());
  79. store_vm_register(Bytecode::Register::accumulator(), GPR0);
  80. }
  81. void Compiler::compile_set_local(Bytecode::Op::SetLocal const& op)
  82. {
  83. load_vm_register(GPR0, Bytecode::Register::accumulator());
  84. store_vm_local(op.index(), GPR0);
  85. }
  86. void Compiler::compile_jump(Bytecode::Op::Jump const& op)
  87. {
  88. m_assembler.jump(label_for(op.true_target()->block()));
  89. }
  90. static bool cxx_to_boolean(VM&, Value value)
  91. {
  92. return value.to_boolean();
  93. }
  94. void Compiler::compile_to_boolean(Assembler::Reg dst, Assembler::Reg src)
  95. {
  96. // dst = src;
  97. m_assembler.mov(
  98. Assembler::Operand::Register(dst),
  99. Assembler::Operand::Register(src));
  100. // dst >>= 48;
  101. m_assembler.shift_right(
  102. Assembler::Operand::Register(dst),
  103. Assembler::Operand::Imm8(48));
  104. // if (dst != BOOLEAN_TAG) goto slow_case;
  105. auto slow_case = m_assembler.make_label();
  106. m_assembler.jump_if_not_equal(
  107. Assembler::Operand::Register(dst),
  108. Assembler::Operand::Imm32(BOOLEAN_TAG),
  109. slow_case);
  110. // Fast path for JS::Value booleans.
  111. // dst = src;
  112. m_assembler.mov(
  113. Assembler::Operand::Register(dst),
  114. Assembler::Operand::Register(src));
  115. // goto end;
  116. auto end = m_assembler.jump();
  117. // slow_case: // call C++ helper
  118. slow_case.link(m_assembler);
  119. m_assembler.mov(
  120. Assembler::Operand::Register(ARG1),
  121. Assembler::Operand::Register(src));
  122. m_assembler.native_call((void*)cxx_to_boolean);
  123. m_assembler.mov(
  124. Assembler::Operand::Register(dst),
  125. Assembler::Operand::Register(RET));
  126. // end:
  127. end.link(m_assembler);
  128. // dst &= 1;
  129. m_assembler.bitwise_and(
  130. Assembler::Operand::Register(dst),
  131. Assembler::Operand::Imm32(1));
  132. }
  133. void Compiler::compile_jump_conditional(Bytecode::Op::JumpConditional const& op)
  134. {
  135. load_vm_register(GPR1, Bytecode::Register::accumulator());
  136. compile_to_boolean(GPR0, GPR1);
  137. m_assembler.jump_if_zero(
  138. Assembler::Operand::Register(GPR0),
  139. label_for(op.false_target()->block()));
  140. m_assembler.jump(label_for(op.true_target()->block()));
  141. }
  142. [[maybe_unused]] static Value cxx_increment(VM& vm, Value value)
  143. {
  144. auto old_value = TRY_OR_SET_EXCEPTION(value.to_numeric(vm));
  145. if (old_value.is_number())
  146. return Value(old_value.as_double() + 1);
  147. return BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  148. }
  149. template<typename Codegen>
  150. void Compiler::branch_if_int32(Assembler::Reg reg, Codegen codegen)
  151. {
  152. // GPR0 = reg >> 48;
  153. m_assembler.mov(Assembler::Operand::Register(GPR0), Assembler::Operand::Register(reg));
  154. m_assembler.shift_right(Assembler::Operand::Register(GPR0), Assembler::Operand::Imm8(48));
  155. auto not_int32_case = m_assembler.make_label();
  156. m_assembler.jump_if_not_equal(
  157. Assembler::Operand::Register(GPR0),
  158. Assembler::Operand::Imm32(INT32_TAG),
  159. not_int32_case);
  160. codegen();
  161. not_int32_case.link(m_assembler);
  162. }
  163. template<typename Codegen>
  164. void Compiler::branch_if_both_int32(Assembler::Reg lhs, Assembler::Reg rhs, Codegen codegen)
  165. {
  166. // GPR0 = lhs >> 48;
  167. m_assembler.mov(Assembler::Operand::Register(GPR0), Assembler::Operand::Register(lhs));
  168. m_assembler.shift_right(Assembler::Operand::Register(GPR0), Assembler::Operand::Imm8(48));
  169. // GPR1 = rhs >> 48;
  170. m_assembler.mov(Assembler::Operand::Register(GPR1), Assembler::Operand::Register(rhs));
  171. m_assembler.shift_right(Assembler::Operand::Register(GPR1), Assembler::Operand::Imm8(48));
  172. auto not_int32_case = m_assembler.make_label();
  173. m_assembler.jump_if_not_equal(
  174. Assembler::Operand::Register(GPR0),
  175. Assembler::Operand::Imm32(INT32_TAG),
  176. not_int32_case);
  177. m_assembler.jump_if_not_equal(
  178. Assembler::Operand::Register(GPR1),
  179. Assembler::Operand::Imm32(INT32_TAG),
  180. not_int32_case);
  181. codegen();
  182. not_int32_case.link(m_assembler);
  183. }
  184. void Compiler::compile_increment(Bytecode::Op::Increment const&)
  185. {
  186. load_vm_register(ARG1, Bytecode::Register::accumulator());
  187. auto end = m_assembler.make_label();
  188. auto slow_case = m_assembler.make_label();
  189. branch_if_int32(ARG1, [&] {
  190. // GPR0 = ARG1 & 0xffffffff;
  191. m_assembler.mov(
  192. Assembler::Operand::Register(GPR0),
  193. Assembler::Operand::Register(ARG1));
  194. m_assembler.mov(
  195. Assembler::Operand::Register(GPR1),
  196. Assembler::Operand::Imm64(0xffffffff));
  197. m_assembler.bitwise_and(
  198. Assembler::Operand::Register(GPR0),
  199. Assembler::Operand::Register(GPR1));
  200. // if (GPR0 == 0x7fffffff) goto slow_case;
  201. m_assembler.jump_if_equal(
  202. Assembler::Operand::Register(GPR0),
  203. Assembler::Operand::Imm32(0x7fffffff),
  204. slow_case);
  205. // ARG1 += 1;
  206. m_assembler.add(
  207. Assembler::Operand::Register(ARG1),
  208. Assembler::Operand::Imm32(1));
  209. // accumulator = ARG1;
  210. store_vm_register(Bytecode::Register::accumulator(), ARG1);
  211. m_assembler.jump(end);
  212. });
  213. slow_case.link(m_assembler);
  214. m_assembler.native_call((void*)cxx_increment);
  215. store_vm_register(Bytecode::Register::accumulator(), RET);
  216. check_exception();
  217. end.link(m_assembler);
  218. }
  219. static Value cxx_decrement(VM& vm, Value value)
  220. {
  221. auto old_value = TRY_OR_SET_EXCEPTION(value.to_numeric(vm));
  222. if (old_value.is_number())
  223. return Value(old_value.as_double() - 1);
  224. return BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  225. }
  226. void Compiler::compile_decrement(Bytecode::Op::Decrement const&)
  227. {
  228. load_vm_register(ARG1, Bytecode::Register::accumulator());
  229. m_assembler.native_call((void*)cxx_decrement);
  230. store_vm_register(Bytecode::Register::accumulator(), RET);
  231. check_exception();
  232. }
  233. void Compiler::check_exception()
  234. {
  235. // if (exception.is_empty()) goto no_exception;
  236. load_vm_register(GPR0, Bytecode::Register::exception());
  237. m_assembler.mov(Assembler::Operand::Register(GPR1), Assembler::Operand::Imm64(Value().encoded()));
  238. auto no_exception = m_assembler.make_label();
  239. m_assembler.jump_if_equal(Assembler::Operand::Register(GPR0), Assembler::Operand::Register(GPR1), no_exception);
  240. // We have an exception!
  241. // if (!unwind_context.valid) return;
  242. auto handle_exception = m_assembler.make_label();
  243. m_assembler.mov(
  244. Assembler::Operand::Register(GPR0),
  245. Assembler::Operand::Mem64BaseAndOffset(UNWIND_CONTEXT_BASE, 0));
  246. m_assembler.jump_if_not_equal(
  247. Assembler::Operand::Register(GPR0),
  248. Assembler::Operand::Imm32(0),
  249. handle_exception);
  250. m_assembler.exit();
  251. // handle_exception:
  252. handle_exception.link(m_assembler);
  253. // if (unwind_context.handler) {
  254. // accumulator = exception;
  255. // exception = Value();
  256. // goto handler;
  257. // }
  258. auto no_handler = m_assembler.make_label();
  259. m_assembler.mov(
  260. Assembler::Operand::Register(GPR0),
  261. Assembler::Operand::Mem64BaseAndOffset(UNWIND_CONTEXT_BASE, 8));
  262. m_assembler.jump_if_equal(
  263. Assembler::Operand::Register(GPR0),
  264. Assembler::Operand::Imm32(0),
  265. no_handler);
  266. load_vm_register(GPR1, Bytecode::Register::exception());
  267. store_vm_register(Bytecode::Register::accumulator(), GPR1);
  268. m_assembler.mov(
  269. Assembler::Operand::Register(GPR1),
  270. Assembler::Operand::Imm64(Value().encoded()));
  271. store_vm_register(Bytecode::Register::exception(), GPR1);
  272. m_assembler.jump(Assembler::Operand::Register(GPR0));
  273. // no_handler:
  274. no_handler.link(m_assembler);
  275. // if (unwind_context.finalizer) goto finalizer;
  276. auto no_finalizer = m_assembler.make_label();
  277. m_assembler.mov(
  278. Assembler::Operand::Register(GPR0),
  279. Assembler::Operand::Mem64BaseAndOffset(UNWIND_CONTEXT_BASE, 16));
  280. m_assembler.jump_if_equal(
  281. Assembler::Operand::Register(GPR0),
  282. Assembler::Operand::Imm32(0),
  283. no_finalizer);
  284. m_assembler.jump(Assembler::Operand::Register(GPR0));
  285. // no_finalizer:
  286. // NOTE: No catch and no finally!? Crash.
  287. no_finalizer.link(m_assembler);
  288. m_assembler.verify_not_reached();
  289. // no_exception:
  290. no_exception.link(m_assembler);
  291. }
  292. void Compiler::push_unwind_context(bool valid, Optional<Bytecode::Label> const& handler, Optional<Bytecode::Label> const& finalizer)
  293. {
  294. // Put this on the stack, and then point UNWIND_CONTEXT_BASE at it.
  295. // struct {
  296. // u64 valid;
  297. // u64 handler;
  298. // u64 finalizer;
  299. // };
  300. // push finalizer (patched later)
  301. m_assembler.mov(
  302. Assembler::Operand::Register(GPR0),
  303. Assembler::Operand::Imm64(0),
  304. Assembler::Patchable::Yes);
  305. if (finalizer.has_value())
  306. block_data_for(finalizer.value().block()).absolute_references_to_here.append(m_assembler.m_output.size() - 8);
  307. m_assembler.push(Assembler::Operand::Register(GPR0));
  308. // push handler (patched later)
  309. m_assembler.mov(
  310. Assembler::Operand::Register(GPR0),
  311. Assembler::Operand::Imm64(0),
  312. Assembler::Patchable::Yes);
  313. if (handler.has_value())
  314. block_data_for(handler.value().block()).absolute_references_to_here.append(m_assembler.m_output.size() - 8);
  315. m_assembler.push(Assembler::Operand::Register(GPR0));
  316. // push valid
  317. m_assembler.push(Assembler::Operand::Imm32(valid));
  318. // UNWIND_CONTEXT_BASE = STACK_POINTER
  319. m_assembler.mov(
  320. Assembler::Operand::Register(UNWIND_CONTEXT_BASE),
  321. Assembler::Operand::Register(STACK_POINTER));
  322. // align stack pointer
  323. m_assembler.sub(Assembler::Operand::Register(STACK_POINTER), Assembler::Operand::Imm8(8));
  324. }
  325. void Compiler::pop_unwind_context()
  326. {
  327. m_assembler.add(Assembler::Operand::Register(STACK_POINTER), Assembler::Operand::Imm8(32));
  328. m_assembler.add(Assembler::Operand::Register(UNWIND_CONTEXT_BASE), Assembler::Operand::Imm8(32));
  329. }
  330. void Compiler::compile_enter_unwind_context(Bytecode::Op::EnterUnwindContext const& op)
  331. {
  332. push_unwind_context(true, op.handler_target(), op.finalizer_target());
  333. m_assembler.jump(label_for(op.entry_point().block()));
  334. }
  335. void Compiler::compile_leave_unwind_context(Bytecode::Op::LeaveUnwindContext const&)
  336. {
  337. pop_unwind_context();
  338. }
  339. void Compiler::compile_throw(Bytecode::Op::Throw const&)
  340. {
  341. load_vm_register(GPR0, Bytecode::Register::accumulator());
  342. store_vm_register(Bytecode::Register::exception(), GPR0);
  343. check_exception();
  344. }
  345. static ThrowCompletionOr<Value> abstract_inequals(VM& vm, Value src1, Value src2)
  346. {
  347. return Value(!TRY(is_loosely_equal(vm, src1, src2)));
  348. }
  349. static ThrowCompletionOr<Value> abstract_equals(VM& vm, Value src1, Value src2)
  350. {
  351. return Value(TRY(is_loosely_equal(vm, src1, src2)));
  352. }
  353. static ThrowCompletionOr<Value> typed_inequals(VM&, Value src1, Value src2)
  354. {
  355. return Value(!is_strictly_equal(src1, src2));
  356. }
  357. static ThrowCompletionOr<Value> typed_equals(VM&, Value src1, Value src2)
  358. {
  359. return Value(is_strictly_equal(src1, src2));
  360. }
  361. #define DO_COMPILE_COMMON_BINARY_OP(TitleCaseName, snake_case_name) \
  362. static Value cxx_##snake_case_name(VM& vm, Value lhs, Value rhs) \
  363. { \
  364. return TRY_OR_SET_EXCEPTION(snake_case_name(vm, lhs, rhs)); \
  365. } \
  366. \
  367. void Compiler::compile_##snake_case_name(Bytecode::Op::TitleCaseName const& op) \
  368. { \
  369. load_vm_register(ARG1, op.lhs()); \
  370. load_vm_register(ARG2, Bytecode::Register::accumulator()); \
  371. m_assembler.native_call((void*)cxx_##snake_case_name); \
  372. store_vm_register(Bytecode::Register::accumulator(), RET); \
  373. check_exception(); \
  374. }
  375. JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(DO_COMPILE_COMMON_BINARY_OP)
  376. #undef DO_COMPILE_COMMON_BINARY_OP
  377. static Value cxx_less_than(VM& vm, Value lhs, Value rhs)
  378. {
  379. return TRY_OR_SET_EXCEPTION(less_than(vm, lhs, rhs));
  380. }
  381. void Compiler::compile_less_than(Bytecode::Op::LessThan const& op)
  382. {
  383. load_vm_register(ARG1, op.lhs());
  384. load_vm_register(ARG2, Bytecode::Register::accumulator());
  385. auto end = m_assembler.make_label();
  386. branch_if_both_int32(ARG1, ARG2, [&] {
  387. // if (ARG1 < ARG2) return true;
  388. // else return false;
  389. auto true_case = m_assembler.make_label();
  390. m_assembler.sign_extend_32_to_64_bits(ARG1);
  391. m_assembler.sign_extend_32_to_64_bits(ARG2);
  392. m_assembler.jump_if_less_than(
  393. Assembler::Operand::Register(ARG1),
  394. Assembler::Operand::Register(ARG2),
  395. true_case);
  396. m_assembler.mov(
  397. Assembler::Operand::Register(GPR0),
  398. Assembler::Operand::Imm64(Value(false).encoded()));
  399. store_vm_register(Bytecode::Register::accumulator(), GPR0);
  400. m_assembler.jump(end);
  401. true_case.link(m_assembler);
  402. m_assembler.mov(
  403. Assembler::Operand::Register(GPR0),
  404. Assembler::Operand::Imm64(Value(true).encoded()));
  405. store_vm_register(Bytecode::Register::accumulator(), GPR0);
  406. m_assembler.jump(end);
  407. });
  408. m_assembler.native_call((void*)cxx_less_than);
  409. store_vm_register(Bytecode::Register::accumulator(), RET);
  410. check_exception();
  411. end.link(m_assembler);
  412. }
  413. static ThrowCompletionOr<Value> not_(VM&, Value value)
  414. {
  415. return Value(!value.to_boolean());
  416. }
  417. static ThrowCompletionOr<Value> typeof_(VM& vm, Value value)
  418. {
  419. return PrimitiveString::create(vm, value.typeof());
  420. }
  421. #define DO_COMPILE_COMMON_UNARY_OP(TitleCaseName, snake_case_name) \
  422. static Value cxx_##snake_case_name(VM& vm, Value value) \
  423. { \
  424. return TRY_OR_SET_EXCEPTION(snake_case_name(vm, value)); \
  425. } \
  426. \
  427. void Compiler::compile_##snake_case_name(Bytecode::Op::TitleCaseName const&) \
  428. { \
  429. load_vm_register(ARG1, Bytecode::Register::accumulator()); \
  430. m_assembler.native_call((void*)cxx_##snake_case_name); \
  431. store_vm_register(Bytecode::Register::accumulator(), RET); \
  432. check_exception(); \
  433. }
  434. JS_ENUMERATE_COMMON_UNARY_OPS(DO_COMPILE_COMMON_UNARY_OP)
  435. #undef DO_COMPILE_COMMON_UNARY_OP
  436. void Compiler::compile_return(Bytecode::Op::Return const&)
  437. {
  438. load_vm_register(GPR0, Bytecode::Register::accumulator());
  439. store_vm_register(Bytecode::Register::return_value(), GPR0);
  440. m_assembler.exit();
  441. }
  442. static Value cxx_new_string(VM& vm, DeprecatedString const& string)
  443. {
  444. return PrimitiveString::create(vm, string);
  445. }
  446. void Compiler::compile_new_string(Bytecode::Op::NewString const& op)
  447. {
  448. auto const& string = m_bytecode_executable.string_table->get(op.index());
  449. m_assembler.mov(
  450. Assembler::Operand::Register(ARG1),
  451. Assembler::Operand::Imm64(bit_cast<u64>(&string)));
  452. m_assembler.native_call((void*)cxx_new_string);
  453. store_vm_register(Bytecode::Register::accumulator(), RET);
  454. }
  455. static Value cxx_new_object(VM& vm)
  456. {
  457. auto& realm = *vm.current_realm();
  458. return Object::create(realm, realm.intrinsics().object_prototype());
  459. }
  460. void Compiler::compile_new_object(Bytecode::Op::NewObject const&)
  461. {
  462. m_assembler.native_call((void*)cxx_new_object);
  463. store_vm_register(Bytecode::Register::accumulator(), RET);
  464. }
  465. static Value cxx_new_array(VM& vm, size_t element_count, u32 first_register_index)
  466. {
  467. auto& realm = *vm.current_realm();
  468. auto array = MUST(Array::create(realm, 0));
  469. for (size_t i = 0; i < element_count; ++i) {
  470. auto& value = vm.bytecode_interpreter().reg(Bytecode::Register(first_register_index + i));
  471. array->indexed_properties().put(i, value, default_attributes);
  472. }
  473. return array;
  474. }
  475. void Compiler::compile_new_array(Bytecode::Op::NewArray const& op)
  476. {
  477. m_assembler.mov(
  478. Assembler::Operand::Register(ARG1),
  479. Assembler::Operand::Imm64(op.element_count()));
  480. m_assembler.mov(
  481. Assembler::Operand::Register(ARG2),
  482. Assembler::Operand::Imm64(op.element_count() ? op.start().index() : 0));
  483. m_assembler.native_call((void*)cxx_new_array);
  484. store_vm_register(Bytecode::Register::accumulator(), RET);
  485. }
  486. static Value cxx_new_function(
  487. VM& vm,
  488. FunctionExpression const& function_node,
  489. Optional<Bytecode::IdentifierTableIndex> const& lhs_name,
  490. Optional<Bytecode::Register> const& home_object)
  491. {
  492. return Bytecode::new_function(vm, function_node, lhs_name, home_object);
  493. }
  494. void Compiler::compile_new_function(Bytecode::Op::NewFunction const& op)
  495. {
  496. m_assembler.mov(
  497. Assembler::Operand::Register(ARG1),
  498. Assembler::Operand::Imm64(bit_cast<u64>(&op.function_node())));
  499. m_assembler.mov(
  500. Assembler::Operand::Register(ARG2),
  501. Assembler::Operand::Imm64(bit_cast<u64>(&op.lhs_name())));
  502. m_assembler.mov(
  503. Assembler::Operand::Register(ARG3),
  504. Assembler::Operand::Imm64(bit_cast<u64>(&op.home_object())));
  505. m_assembler.native_call((void*)cxx_new_function);
  506. store_vm_register(Bytecode::Register::accumulator(), RET);
  507. }
  508. static Value cxx_get_by_id(VM& vm, Value base, Bytecode::IdentifierTableIndex property, u32 cache_index)
  509. {
  510. return TRY_OR_SET_EXCEPTION(Bytecode::get_by_id(vm.bytecode_interpreter(), property, base, base, cache_index));
  511. }
  512. void Compiler::compile_get_by_id(Bytecode::Op::GetById const& op)
  513. {
  514. load_vm_register(ARG1, Bytecode::Register::accumulator());
  515. m_assembler.mov(
  516. Assembler::Operand::Register(ARG2),
  517. Assembler::Operand::Imm64(op.property().value()));
  518. m_assembler.mov(
  519. Assembler::Operand::Register(ARG3),
  520. Assembler::Operand::Imm64(op.cache_index()));
  521. m_assembler.native_call((void*)cxx_get_by_id);
  522. store_vm_register(Bytecode::Register::accumulator(), RET);
  523. check_exception();
  524. }
  525. static Value cxx_get_by_value(VM& vm, Value base, Value property)
  526. {
  527. return TRY_OR_SET_EXCEPTION(Bytecode::get_by_value(vm.bytecode_interpreter(), base, property));
  528. }
  529. void Compiler::compile_get_by_value(Bytecode::Op::GetByValue const& op)
  530. {
  531. load_vm_register(ARG1, op.base());
  532. load_vm_register(ARG2, Bytecode::Register::accumulator());
  533. m_assembler.native_call((void*)cxx_get_by_value);
  534. store_vm_register(Bytecode::Register::accumulator(), RET);
  535. check_exception();
  536. }
  537. static Value cxx_get_global(VM& vm, Bytecode::IdentifierTableIndex identifier, u32 cache_index)
  538. {
  539. return TRY_OR_SET_EXCEPTION(Bytecode::get_global(vm.bytecode_interpreter(), identifier, cache_index));
  540. }
  541. void Compiler::compile_get_global(Bytecode::Op::GetGlobal const& op)
  542. {
  543. m_assembler.mov(
  544. Assembler::Operand::Register(ARG1),
  545. Assembler::Operand::Imm64(op.identifier().value()));
  546. m_assembler.mov(
  547. Assembler::Operand::Register(ARG2),
  548. Assembler::Operand::Imm64(op.cache_index()));
  549. m_assembler.native_call((void*)cxx_get_global);
  550. store_vm_register(Bytecode::Register::accumulator(), RET);
  551. check_exception();
  552. }
  553. static Value cxx_get_variable(VM& vm, DeprecatedFlyString const& name, u32 cache_index)
  554. {
  555. return TRY_OR_SET_EXCEPTION(Bytecode::get_variable(vm.bytecode_interpreter(), name, cache_index));
  556. }
  557. void Compiler::compile_get_variable(Bytecode::Op::GetVariable const& op)
  558. {
  559. m_assembler.mov(
  560. Assembler::Operand::Register(ARG1),
  561. Assembler::Operand::Imm64(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier()))));
  562. m_assembler.mov(
  563. Assembler::Operand::Register(ARG2),
  564. Assembler::Operand::Imm64(op.cache_index()));
  565. m_assembler.native_call((void*)cxx_get_variable);
  566. store_vm_register(Bytecode::Register::accumulator(), RET);
  567. check_exception();
  568. }
  569. static Value cxx_get_callee_and_this_from_environment(VM& vm, DeprecatedFlyString const& name, u32 cache_index, Bytecode::Register callee_reg, Bytecode::Register this_reg)
  570. {
  571. auto& bytecode_interpreter = vm.bytecode_interpreter();
  572. auto callee_and_this = TRY_OR_SET_EXCEPTION(Bytecode::get_callee_and_this_from_environment(
  573. bytecode_interpreter,
  574. name,
  575. cache_index));
  576. bytecode_interpreter.reg(callee_reg) = callee_and_this.callee;
  577. bytecode_interpreter.reg(this_reg) = callee_and_this.this_value;
  578. return {};
  579. }
  580. void Compiler::compile_get_callee_and_this_from_environment(Bytecode::Op::GetCalleeAndThisFromEnvironment const& op)
  581. {
  582. m_assembler.mov(
  583. Assembler::Operand::Register(ARG1),
  584. Assembler::Operand::Imm64(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier()))));
  585. m_assembler.mov(
  586. Assembler::Operand::Register(ARG2),
  587. Assembler::Operand::Imm64(op.cache_index()));
  588. m_assembler.mov(
  589. Assembler::Operand::Register(ARG3),
  590. Assembler::Operand::Imm64(op.callee().index()));
  591. m_assembler.mov(
  592. Assembler::Operand::Register(ARG4),
  593. Assembler::Operand::Imm64(op.this_().index()));
  594. m_assembler.native_call((void*)cxx_get_callee_and_this_from_environment);
  595. check_exception();
  596. }
  597. static Value cxx_to_numeric(VM& vm, Value value)
  598. {
  599. return TRY_OR_SET_EXCEPTION(value.to_numeric(vm));
  600. }
  601. void Compiler::compile_to_numeric(Bytecode::Op::ToNumeric const&)
  602. {
  603. load_vm_register(ARG1, Bytecode::Register::accumulator());
  604. m_assembler.native_call((void*)cxx_to_numeric);
  605. store_vm_register(Bytecode::Register::accumulator(), RET);
  606. check_exception();
  607. }
  608. static Value cxx_resolve_this_binding(VM& vm)
  609. {
  610. auto this_value = TRY_OR_SET_EXCEPTION(vm.resolve_this_binding());
  611. vm.bytecode_interpreter().reg(Bytecode::Register::this_value()) = this_value;
  612. return this_value;
  613. }
  614. void Compiler::compile_resolve_this_binding(Bytecode::Op::ResolveThisBinding const&)
  615. {
  616. // OPTIMIZATION: We cache the `this` value in a special VM register.
  617. // So first we check if the cache is non-empty, and if so,
  618. // we can avoid calling out to C++ at all. :^)
  619. load_vm_register(GPR0, Bytecode::Register::this_value());
  620. m_assembler.mov(
  621. Assembler::Operand::Register(GPR1),
  622. Assembler::Operand::Imm64(Value().encoded()));
  623. auto slow_case = m_assembler.make_label();
  624. m_assembler.jump_if_equal(
  625. Assembler::Operand::Register(GPR0),
  626. Assembler::Operand::Register(GPR1),
  627. slow_case);
  628. // Fast case: We have a cached `this` value!
  629. store_vm_register(Bytecode::Register::accumulator(), GPR0);
  630. auto end = m_assembler.jump();
  631. slow_case.link(m_assembler);
  632. m_assembler.native_call((void*)cxx_resolve_this_binding);
  633. store_vm_register(Bytecode::Register::accumulator(), RET);
  634. check_exception();
  635. end.link(m_assembler);
  636. }
  637. static Value cxx_put_by_id(VM& vm, Value base, Bytecode::IdentifierTableIndex property, Value value, Bytecode::Op::PropertyKind kind)
  638. {
  639. PropertyKey name = vm.bytecode_interpreter().current_executable().get_identifier(property);
  640. TRY_OR_SET_EXCEPTION(Bytecode::put_by_property_key(vm, base, base, value, name, kind));
  641. vm.bytecode_interpreter().accumulator() = value;
  642. return {};
  643. }
  644. void Compiler::compile_put_by_id(Bytecode::Op::PutById const& op)
  645. {
  646. load_vm_register(ARG1, op.base());
  647. m_assembler.mov(
  648. Assembler::Operand::Register(ARG2),
  649. Assembler::Operand::Imm64(op.property().value()));
  650. load_vm_register(ARG3, Bytecode::Register::accumulator());
  651. m_assembler.mov(
  652. Assembler::Operand::Register(ARG4),
  653. Assembler::Operand::Imm64(to_underlying(op.kind())));
  654. m_assembler.native_call((void*)cxx_put_by_id);
  655. check_exception();
  656. }
  657. static Value cxx_put_by_value(VM& vm, Value base, Value property, Value value, Bytecode::Op::PropertyKind kind)
  658. {
  659. TRY_OR_SET_EXCEPTION(Bytecode::put_by_value(vm, base, property, value, kind));
  660. vm.bytecode_interpreter().accumulator() = value;
  661. return {};
  662. }
  663. void Compiler::compile_put_by_value(Bytecode::Op::PutByValue const& op)
  664. {
  665. load_vm_register(ARG1, op.base());
  666. load_vm_register(ARG2, op.property());
  667. load_vm_register(ARG3, Bytecode::Register::accumulator());
  668. m_assembler.mov(
  669. Assembler::Operand::Register(ARG4),
  670. Assembler::Operand::Imm64(to_underlying(op.kind())));
  671. m_assembler.native_call((void*)cxx_put_by_value);
  672. check_exception();
  673. }
  674. static Value cxx_call(VM& vm, Value callee, u32 first_argument_index, u32 argument_count, Value this_value, Bytecode::Op::CallType call_type)
  675. {
  676. // FIXME: Get the expression_string() here as well.
  677. TRY_OR_SET_EXCEPTION(throw_if_needed_for_call(vm.bytecode_interpreter(), callee, call_type, {}));
  678. MarkedVector<Value> argument_values(vm.heap());
  679. argument_values.ensure_capacity(argument_count);
  680. for (u32 i = 0; i < argument_count; ++i) {
  681. argument_values.unchecked_append(vm.bytecode_interpreter().reg(Bytecode::Register { first_argument_index + i }));
  682. }
  683. return TRY_OR_SET_EXCEPTION(perform_call(vm.bytecode_interpreter(), this_value, call_type, callee, move(argument_values)));
  684. }
  685. void Compiler::compile_call(Bytecode::Op::Call const& op)
  686. {
  687. load_vm_register(ARG1, op.callee());
  688. m_assembler.mov(
  689. Assembler::Operand::Register(ARG2),
  690. Assembler::Operand::Imm64(op.first_argument().index()));
  691. m_assembler.mov(
  692. Assembler::Operand::Register(ARG3),
  693. Assembler::Operand::Imm64(op.argument_count()));
  694. load_vm_register(ARG4, op.this_value());
  695. m_assembler.mov(
  696. Assembler::Operand::Register(ARG5),
  697. Assembler::Operand::Imm64(to_underlying(op.call_type())));
  698. m_assembler.native_call((void*)cxx_call);
  699. store_vm_register(Bytecode::Register::accumulator(), RET);
  700. check_exception();
  701. }
  702. static Value cxx_typeof_variable(VM& vm, DeprecatedFlyString const& identifier)
  703. {
  704. return TRY_OR_SET_EXCEPTION(Bytecode::typeof_variable(vm, identifier));
  705. }
  706. void Compiler::compile_typeof_variable(Bytecode::Op::TypeofVariable const& op)
  707. {
  708. m_assembler.mov(
  709. Assembler::Operand::Register(ARG1),
  710. Assembler::Operand::Imm64(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier().value()))));
  711. m_assembler.native_call((void*)cxx_typeof_variable);
  712. store_vm_register(Bytecode::Register::accumulator(), RET);
  713. check_exception();
  714. }
  715. static Value cxx_set_variable(
  716. VM& vm,
  717. DeprecatedFlyString const& identifier,
  718. Value value,
  719. Bytecode::Op::EnvironmentMode environment_mode,
  720. Bytecode::Op::SetVariable::InitializationMode initialization_mode)
  721. {
  722. TRY_OR_SET_EXCEPTION(Bytecode::set_variable(vm, identifier, value, environment_mode, initialization_mode));
  723. return {};
  724. }
  725. void Compiler::compile_set_variable(Bytecode::Op::SetVariable const& op)
  726. {
  727. m_assembler.mov(
  728. Assembler::Operand::Register(ARG1),
  729. Assembler::Operand::Imm64(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier().value()))));
  730. load_vm_register(ARG2, Bytecode::Register::accumulator());
  731. m_assembler.mov(
  732. Assembler::Operand::Register(ARG3),
  733. Assembler::Operand::Imm64(to_underlying(op.mode())));
  734. m_assembler.mov(
  735. Assembler::Operand::Register(ARG4),
  736. Assembler::Operand::Imm64(to_underlying(op.initialization_mode())));
  737. m_assembler.native_call((void*)cxx_set_variable);
  738. check_exception();
  739. }
  740. static void cxx_create_lexical_environment(VM& vm)
  741. {
  742. auto make_and_swap_envs = [&](auto& old_environment) {
  743. GCPtr<Environment> environment = new_declarative_environment(*old_environment).ptr();
  744. swap(old_environment, environment);
  745. return environment;
  746. };
  747. vm.bytecode_interpreter().saved_lexical_environment_stack().append(make_and_swap_envs(vm.running_execution_context().lexical_environment));
  748. }
  749. void Compiler::compile_create_lexical_environment(Bytecode::Op::CreateLexicalEnvironment const&)
  750. {
  751. m_assembler.native_call((void*)cxx_create_lexical_environment);
  752. }
  753. static void cxx_leave_lexical_environment(VM& vm)
  754. {
  755. vm.running_execution_context().lexical_environment = vm.bytecode_interpreter().saved_lexical_environment_stack().take_last();
  756. }
  757. void Compiler::compile_leave_lexical_environment(Bytecode::Op::LeaveLexicalEnvironment const&)
  758. {
  759. m_assembler.native_call((void*)cxx_leave_lexical_environment);
  760. }
  761. OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_executable)
  762. {
  763. if (!getenv("LIBJS_JIT"))
  764. return nullptr;
  765. Compiler compiler { bytecode_executable };
  766. compiler.m_assembler.enter();
  767. compiler.m_assembler.mov(
  768. Assembler::Operand::Register(REGISTER_ARRAY_BASE),
  769. Assembler::Operand::Register(ARG1));
  770. compiler.m_assembler.mov(
  771. Assembler::Operand::Register(LOCALS_ARRAY_BASE),
  772. Assembler::Operand::Register(ARG2));
  773. compiler.push_unwind_context(false, {}, {});
  774. for (auto& block : bytecode_executable.basic_blocks) {
  775. compiler.block_data_for(*block).start_offset = compiler.m_output.size();
  776. auto it = Bytecode::InstructionStreamIterator(block->instruction_stream());
  777. while (!it.at_end()) {
  778. auto const& op = *it;
  779. switch (op.type()) {
  780. case Bytecode::Instruction::Type::LoadImmediate:
  781. compiler.compile_load_immediate(static_cast<Bytecode::Op::LoadImmediate const&>(op));
  782. break;
  783. case Bytecode::Instruction::Type::Store:
  784. compiler.compile_store(static_cast<Bytecode::Op::Store const&>(op));
  785. break;
  786. case Bytecode::Instruction::Type::Load:
  787. compiler.compile_load(static_cast<Bytecode::Op::Load const&>(op));
  788. break;
  789. case Bytecode::Instruction::Type::GetLocal:
  790. compiler.compile_get_local(static_cast<Bytecode::Op::GetLocal const&>(op));
  791. break;
  792. case Bytecode::Instruction::Type::SetLocal:
  793. compiler.compile_set_local(static_cast<Bytecode::Op::SetLocal const&>(op));
  794. break;
  795. case Bytecode::Instruction::Type::Jump:
  796. compiler.compile_jump(static_cast<Bytecode::Op::Jump const&>(op));
  797. break;
  798. case Bytecode::Instruction::Type::JumpConditional:
  799. compiler.compile_jump_conditional(static_cast<Bytecode::Op::JumpConditional const&>(op));
  800. break;
  801. case Bytecode::Instruction::Type::Increment:
  802. compiler.compile_increment(static_cast<Bytecode::Op::Increment const&>(op));
  803. break;
  804. case Bytecode::Instruction::Type::Decrement:
  805. compiler.compile_decrement(static_cast<Bytecode::Op::Decrement const&>(op));
  806. break;
  807. case Bytecode::Instruction::Type::EnterUnwindContext:
  808. compiler.compile_enter_unwind_context(static_cast<Bytecode::Op::EnterUnwindContext const&>(op));
  809. break;
  810. case Bytecode::Instruction::Type::LeaveUnwindContext:
  811. compiler.compile_leave_unwind_context(static_cast<Bytecode::Op::LeaveUnwindContext const&>(op));
  812. break;
  813. case Bytecode::Instruction::Type::Throw:
  814. compiler.compile_throw(static_cast<Bytecode::Op::Throw const&>(op));
  815. break;
  816. case Bytecode::Instruction::Type::Return:
  817. compiler.compile_return(static_cast<Bytecode::Op::Return const&>(op));
  818. break;
  819. case Bytecode::Instruction::Type::CreateLexicalEnvironment:
  820. compiler.compile_create_lexical_environment(static_cast<Bytecode::Op::CreateLexicalEnvironment const&>(op));
  821. break;
  822. case Bytecode::Instruction::Type::LeaveLexicalEnvironment:
  823. compiler.compile_leave_lexical_environment(static_cast<Bytecode::Op::LeaveLexicalEnvironment const&>(op));
  824. break;
  825. case Bytecode::Instruction::Type::NewString:
  826. compiler.compile_new_string(static_cast<Bytecode::Op::NewString const&>(op));
  827. break;
  828. case Bytecode::Instruction::Type::NewObject:
  829. compiler.compile_new_object(static_cast<Bytecode::Op::NewObject const&>(op));
  830. break;
  831. case Bytecode::Instruction::Type::NewArray:
  832. compiler.compile_new_array(static_cast<Bytecode::Op::NewArray const&>(op));
  833. break;
  834. case Bytecode::Instruction::Type::NewFunction:
  835. compiler.compile_new_function(static_cast<Bytecode::Op::NewFunction const&>(op));
  836. break;
  837. case Bytecode::Instruction::Type::GetById:
  838. compiler.compile_get_by_id(static_cast<Bytecode::Op::GetById const&>(op));
  839. break;
  840. case Bytecode::Instruction::Type::GetByValue:
  841. compiler.compile_get_by_value(static_cast<Bytecode::Op::GetByValue const&>(op));
  842. break;
  843. case Bytecode::Instruction::Type::GetGlobal:
  844. compiler.compile_get_global(static_cast<Bytecode::Op::GetGlobal const&>(op));
  845. break;
  846. case Bytecode::Instruction::Type::GetVariable:
  847. compiler.compile_get_variable(static_cast<Bytecode::Op::GetVariable const&>(op));
  848. break;
  849. case Bytecode::Instruction::Type::GetCalleeAndThisFromEnvironment:
  850. compiler.compile_get_callee_and_this_from_environment(static_cast<Bytecode::Op::GetCalleeAndThisFromEnvironment const&>(op));
  851. break;
  852. case Bytecode::Instruction::Type::PutById:
  853. compiler.compile_put_by_id(static_cast<Bytecode::Op::PutById const&>(op));
  854. break;
  855. case Bytecode::Instruction::Type::PutByValue:
  856. compiler.compile_put_by_value(static_cast<Bytecode::Op::PutByValue const&>(op));
  857. break;
  858. case Bytecode::Instruction::Type::ToNumeric:
  859. compiler.compile_to_numeric(static_cast<Bytecode::Op::ToNumeric const&>(op));
  860. break;
  861. case Bytecode::Instruction::Type::ResolveThisBinding:
  862. compiler.compile_resolve_this_binding(static_cast<Bytecode::Op::ResolveThisBinding const&>(op));
  863. break;
  864. case Bytecode::Instruction::Type::Call:
  865. compiler.compile_call(static_cast<Bytecode::Op::Call const&>(op));
  866. break;
  867. case Bytecode::Instruction::Type::TypeofVariable:
  868. compiler.compile_typeof_variable(static_cast<Bytecode::Op::TypeofVariable const&>(op));
  869. break;
  870. case Bytecode::Instruction::Type::SetVariable:
  871. compiler.compile_set_variable(static_cast<Bytecode::Op::SetVariable const&>(op));
  872. break;
  873. case Bytecode::Instruction::Type::LessThan:
  874. compiler.compile_less_than(static_cast<Bytecode::Op::LessThan const&>(op));
  875. break;
  876. #define DO_COMPILE_COMMON_BINARY_OP(TitleCaseName, snake_case_name) \
  877. case Bytecode::Instruction::Type::TitleCaseName: \
  878. compiler.compile_##snake_case_name(static_cast<Bytecode::Op::TitleCaseName const&>(op)); \
  879. break;
  880. JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(DO_COMPILE_COMMON_BINARY_OP)
  881. #undef DO_COMPILE_COMMON_BINARY_OP
  882. #define DO_COMPILE_COMMON_UNARY_OP(TitleCaseName, snake_case_name) \
  883. case Bytecode::Instruction::Type::TitleCaseName: \
  884. compiler.compile_##snake_case_name(static_cast<Bytecode::Op::TitleCaseName const&>(op)); \
  885. break;
  886. JS_ENUMERATE_COMMON_UNARY_OPS(DO_COMPILE_COMMON_UNARY_OP)
  887. #undef DO_COMPILE_COMMON_UNARY_OP
  888. default:
  889. if constexpr (LOG_JIT_FAILURE) {
  890. dbgln("\033[31;1mJIT compilation failed\033[0m: {}", bytecode_executable.name);
  891. dbgln("Unsupported bytecode op: {}", op.to_deprecated_string(bytecode_executable));
  892. }
  893. return nullptr;
  894. }
  895. ++it;
  896. }
  897. if (!block->is_terminated())
  898. compiler.m_assembler.exit();
  899. }
  900. auto* executable_memory = mmap(nullptr, compiler.m_output.size(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  901. if (executable_memory == MAP_FAILED) {
  902. dbgln("mmap: {}", strerror(errno));
  903. return nullptr;
  904. }
  905. for (auto& block : bytecode_executable.basic_blocks) {
  906. auto& block_data = compiler.block_data_for(*block);
  907. block_data.label.link_to(compiler.m_assembler, block_data.start_offset);
  908. // Patch up all the absolute references
  909. for (auto& absolute_reference : block_data.absolute_references_to_here) {
  910. auto offset = bit_cast<u64>(executable_memory) + block_data.start_offset;
  911. compiler.m_output[absolute_reference + 0] = (offset >> 0) & 0xff;
  912. compiler.m_output[absolute_reference + 1] = (offset >> 8) & 0xff;
  913. compiler.m_output[absolute_reference + 2] = (offset >> 16) & 0xff;
  914. compiler.m_output[absolute_reference + 3] = (offset >> 24) & 0xff;
  915. compiler.m_output[absolute_reference + 4] = (offset >> 32) & 0xff;
  916. compiler.m_output[absolute_reference + 5] = (offset >> 40) & 0xff;
  917. compiler.m_output[absolute_reference + 6] = (offset >> 48) & 0xff;
  918. compiler.m_output[absolute_reference + 7] = (offset >> 56) & 0xff;
  919. }
  920. }
  921. if constexpr (DUMP_JIT_MACHINE_CODE_TO_STDOUT) {
  922. (void)write(STDOUT_FILENO, compiler.m_output.data(), compiler.m_output.size());
  923. }
  924. memcpy(executable_memory, compiler.m_output.data(), compiler.m_output.size());
  925. if (mprotect(executable_memory, compiler.m_output.size(), PROT_READ | PROT_EXEC) < 0) {
  926. dbgln("mprotect: {}", strerror(errno));
  927. return nullptr;
  928. }
  929. if constexpr (LOG_JIT_SUCCESS) {
  930. dbgln("\033[32;1mJIT compilation succeeded!\033[0m {}", bytecode_executable.name);
  931. }
  932. return make<NativeExecutable>(executable_memory, compiler.m_output.size());
  933. }
  934. }