Compiler.cpp 33 KB

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