Compiler.cpp 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Simon Wanner <simon@skyrising.xyz>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/OwnPtr.h>
  8. #include <AK/Platform.h>
  9. #include <LibJS/Bytecode/CommonImplementations.h>
  10. #include <LibJS/Bytecode/Instruction.h>
  11. #include <LibJS/Bytecode/Interpreter.h>
  12. #include <LibJS/Bytecode/RegexTable.h>
  13. #include <LibJS/JIT/Compiler.h>
  14. #include <LibJS/Runtime/AbstractOperations.h>
  15. #include <LibJS/Runtime/Array.h>
  16. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  17. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  18. #include <LibJS/Runtime/FunctionEnvironment.h>
  19. #include <LibJS/Runtime/VM.h>
  20. #include <LibJS/Runtime/ValueInlines.h>
  21. #include <sys/mman.h>
  22. #include <unistd.h>
  23. #if ARCH(X86_64)
  24. # define LOG_JIT_SUCCESS 0
  25. # define LOG_JIT_FAILURE 1
  26. # define DUMP_JIT_MACHINE_CODE_TO_STDOUT 0
  27. # define DUMP_JIT_DISASSEMBLY 0
  28. # define TRY_OR_SET_EXCEPTION(expression) \
  29. ({ \
  30. /* Ignore -Wshadow to allow nesting the macro. */ \
  31. AK_IGNORE_DIAGNOSTIC("-Wshadow", \
  32. auto&& _temporary_result = (expression)); \
  33. static_assert(!::AK::Detail::IsLvalueReference<decltype(_temporary_result.release_value())>, \
  34. "Do not return a reference from a fallible expression"); \
  35. if (_temporary_result.is_error()) [[unlikely]] { \
  36. vm.bytecode_interpreter().reg(Bytecode::Register::exception()) = _temporary_result.release_error().value().value(); \
  37. return {}; \
  38. } \
  39. _temporary_result.release_value(); \
  40. })
  41. namespace JS::JIT {
  42. void Compiler::store_vm_register(Bytecode::Register dst, Assembler::Reg src)
  43. {
  44. m_assembler.mov(
  45. Assembler::Operand::Mem64BaseAndOffset(REGISTER_ARRAY_BASE, dst.index() * sizeof(Value)),
  46. Assembler::Operand::Register(src));
  47. }
  48. void Compiler::load_vm_register(Assembler::Reg dst, Bytecode::Register src)
  49. {
  50. m_assembler.mov(
  51. Assembler::Operand::Register(dst),
  52. Assembler::Operand::Mem64BaseAndOffset(REGISTER_ARRAY_BASE, src.index() * sizeof(Value)));
  53. }
  54. void Compiler::load_accumulator(Assembler::Reg dst)
  55. {
  56. m_assembler.mov(
  57. Assembler::Operand::Register(dst),
  58. Assembler::Operand::Register(CACHED_ACCUMULATOR));
  59. }
  60. void Compiler::store_accumulator(Assembler::Reg src)
  61. {
  62. m_assembler.mov(
  63. Assembler::Operand::Register(CACHED_ACCUMULATOR),
  64. Assembler::Operand::Register(src));
  65. }
  66. void Compiler::reload_cached_accumulator()
  67. {
  68. m_assembler.mov(
  69. Assembler::Operand::Register(CACHED_ACCUMULATOR),
  70. Assembler::Operand::Mem64BaseAndOffset(REGISTER_ARRAY_BASE, Bytecode::Register::accumulator_index * sizeof(Value)));
  71. }
  72. void Compiler::flush_cached_accumulator()
  73. {
  74. m_assembler.mov(
  75. Assembler::Operand::Mem64BaseAndOffset(REGISTER_ARRAY_BASE, Bytecode::Register::accumulator_index * sizeof(Value)),
  76. Assembler::Operand::Register(CACHED_ACCUMULATOR));
  77. }
  78. void Compiler::store_vm_local(size_t dst, Assembler::Reg src)
  79. {
  80. m_assembler.mov(
  81. Assembler::Operand::Mem64BaseAndOffset(LOCALS_ARRAY_BASE, dst * sizeof(Value)),
  82. Assembler::Operand::Register(src));
  83. }
  84. void Compiler::load_vm_local(Assembler::Reg dst, size_t src)
  85. {
  86. m_assembler.mov(
  87. Assembler::Operand::Register(dst),
  88. Assembler::Operand::Mem64BaseAndOffset(LOCALS_ARRAY_BASE, src * sizeof(Value)));
  89. }
  90. void Compiler::compile_load_immediate(Bytecode::Op::LoadImmediate const& op)
  91. {
  92. m_assembler.mov(
  93. Assembler::Operand::Register(GPR0),
  94. Assembler::Operand::Imm(op.value().encoded()));
  95. store_accumulator(GPR0);
  96. }
  97. void Compiler::compile_load(Bytecode::Op::Load const& op)
  98. {
  99. load_vm_register(GPR0, op.src());
  100. store_accumulator(GPR0);
  101. }
  102. void Compiler::compile_store(Bytecode::Op::Store const& op)
  103. {
  104. load_accumulator(GPR0);
  105. store_vm_register(op.dst(), GPR0);
  106. }
  107. static Value cxx_throw_binding_not_initialized(VM& vm, size_t index)
  108. {
  109. auto const& variable_name = vm.running_execution_context().function->local_variables_names()[index];
  110. TRY_OR_SET_EXCEPTION(vm.throw_completion<ReferenceError>(ErrorType::BindingNotInitialized, variable_name));
  111. return {};
  112. }
  113. void Compiler::compile_get_local(Bytecode::Op::GetLocal const& op)
  114. {
  115. load_vm_local(GPR0, op.index());
  116. // if (GPR0 == <empty>) throw ReferenceError(BindingNotInitialized)
  117. Assembler::Label not_empty {};
  118. m_assembler.mov(
  119. Assembler::Operand::Register(GPR1),
  120. Assembler::Operand::Imm(Value().encoded()));
  121. m_assembler.jump_if(
  122. Assembler::Operand::Register(GPR0),
  123. Assembler::Condition::NotEqualTo,
  124. Assembler::Operand::Register(GPR1),
  125. not_empty);
  126. m_assembler.mov(Assembler::Operand::Register(ARG1), Assembler::Operand::Imm(op.index()));
  127. native_call((void*)cxx_throw_binding_not_initialized);
  128. check_exception();
  129. not_empty.link(m_assembler);
  130. store_accumulator(GPR0);
  131. }
  132. void Compiler::compile_set_local(Bytecode::Op::SetLocal const& op)
  133. {
  134. load_accumulator(GPR0);
  135. store_vm_local(op.index(), GPR0);
  136. }
  137. static Value cxx_typeof_local(VM& vm, Value value)
  138. {
  139. return PrimitiveString::create(vm, value.typeof());
  140. }
  141. void Compiler::compile_typeof_local(Bytecode::Op::TypeofLocal const& op)
  142. {
  143. load_vm_local(ARG1, op.index());
  144. native_call((void*)cxx_typeof_local);
  145. store_accumulator(GPR0);
  146. }
  147. void Compiler::compile_jump(Bytecode::Op::Jump const& op)
  148. {
  149. m_assembler.jump(label_for(op.true_target()->block()));
  150. }
  151. static bool cxx_to_boolean(VM&, Value value)
  152. {
  153. return value.to_boolean();
  154. }
  155. void Compiler::compile_to_boolean(Assembler::Reg dst, Assembler::Reg src)
  156. {
  157. // dst = src;
  158. m_assembler.mov(
  159. Assembler::Operand::Register(dst),
  160. Assembler::Operand::Register(src));
  161. // dst >>= 48;
  162. m_assembler.shift_right(
  163. Assembler::Operand::Register(dst),
  164. Assembler::Operand::Imm(48));
  165. // if (dst != BOOLEAN_TAG) goto slow_case;
  166. Assembler::Label slow_case {};
  167. m_assembler.jump_if(
  168. Assembler::Operand::Register(dst),
  169. Assembler::Condition::NotEqualTo,
  170. Assembler::Operand::Imm(BOOLEAN_TAG),
  171. slow_case);
  172. // Fast path for JS::Value booleans.
  173. // dst = src;
  174. m_assembler.mov(
  175. Assembler::Operand::Register(dst),
  176. Assembler::Operand::Register(src));
  177. // goto end;
  178. auto end = m_assembler.jump();
  179. // slow_case: // call C++ helper
  180. slow_case.link(m_assembler);
  181. m_assembler.mov(
  182. Assembler::Operand::Register(ARG1),
  183. Assembler::Operand::Register(src));
  184. native_call((void*)cxx_to_boolean);
  185. m_assembler.mov(
  186. Assembler::Operand::Register(dst),
  187. Assembler::Operand::Register(RET));
  188. // end:
  189. end.link(m_assembler);
  190. // dst &= 1;
  191. m_assembler.bitwise_and(
  192. Assembler::Operand::Register(dst),
  193. Assembler::Operand::Imm(1));
  194. }
  195. void Compiler::compile_jump_conditional(Bytecode::Op::JumpConditional const& op)
  196. {
  197. load_accumulator(GPR1);
  198. compile_to_boolean(GPR0, GPR1);
  199. m_assembler.jump_if(
  200. Assembler::Operand::Register(GPR0),
  201. Assembler::Condition::EqualTo,
  202. Assembler::Operand::Imm(0),
  203. label_for(op.false_target()->block()));
  204. m_assembler.jump(label_for(op.true_target()->block()));
  205. }
  206. void Compiler::compile_jump_nullish(Bytecode::Op::JumpNullish const& op)
  207. {
  208. load_accumulator(GPR0);
  209. m_assembler.shift_right(
  210. Assembler::Operand::Register(GPR0),
  211. Assembler::Operand::Imm(48));
  212. m_assembler.bitwise_and(
  213. Assembler::Operand::Register(GPR0),
  214. Assembler::Operand::Imm(IS_NULLISH_EXTRACT_PATTERN));
  215. m_assembler.jump_if(
  216. Assembler::Operand::Register(GPR0),
  217. Assembler::Condition::EqualTo,
  218. Assembler::Operand::Imm(IS_NULLISH_PATTERN),
  219. label_for(op.true_target()->block()));
  220. m_assembler.jump(label_for(op.false_target()->block()));
  221. }
  222. void Compiler::compile_jump_undefined(Bytecode::Op::JumpUndefined const& op)
  223. {
  224. load_accumulator(GPR0);
  225. m_assembler.shift_right(
  226. Assembler::Operand::Register(GPR0),
  227. Assembler::Operand::Imm(48));
  228. m_assembler.jump_if(
  229. Assembler::Operand::Register(GPR0),
  230. Assembler::Condition::EqualTo,
  231. Assembler::Operand::Imm(UNDEFINED_TAG),
  232. label_for(op.true_target()->block()));
  233. m_assembler.jump(label_for(op.false_target()->block()));
  234. }
  235. [[maybe_unused]] static Value cxx_increment(VM& vm, Value value)
  236. {
  237. auto old_value = TRY_OR_SET_EXCEPTION(value.to_numeric(vm));
  238. if (old_value.is_number())
  239. return Value(old_value.as_double() + 1);
  240. return BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  241. }
  242. void Compiler::jump_if_int32(Assembler::Reg reg, Assembler::Label& label)
  243. {
  244. // GPR0 = reg >> 48;
  245. m_assembler.mov(Assembler::Operand::Register(GPR0), Assembler::Operand::Register(reg));
  246. m_assembler.shift_right(Assembler::Operand::Register(GPR0), Assembler::Operand::Imm(48));
  247. m_assembler.jump_if(
  248. Assembler::Operand::Register(GPR0),
  249. Assembler::Condition::EqualTo,
  250. Assembler::Operand::Imm(INT32_TAG),
  251. label);
  252. }
  253. template<typename Codegen>
  254. void Compiler::branch_if_int32(Assembler::Reg reg, Codegen codegen)
  255. {
  256. // GPR0 = reg >> 48;
  257. m_assembler.mov(Assembler::Operand::Register(GPR0), Assembler::Operand::Register(reg));
  258. m_assembler.shift_right(Assembler::Operand::Register(GPR0), Assembler::Operand::Imm(48));
  259. Assembler::Label not_int32_case {};
  260. m_assembler.jump_if(
  261. Assembler::Operand::Register(GPR0),
  262. Assembler::Condition::NotEqualTo,
  263. Assembler::Operand::Imm(INT32_TAG),
  264. not_int32_case);
  265. codegen();
  266. not_int32_case.link(m_assembler);
  267. }
  268. template<typename Codegen>
  269. void Compiler::branch_if_both_int32(Assembler::Reg lhs, Assembler::Reg rhs, Codegen codegen)
  270. {
  271. // GPR0 = lhs >> 48;
  272. m_assembler.mov(Assembler::Operand::Register(GPR0), Assembler::Operand::Register(lhs));
  273. m_assembler.shift_right(Assembler::Operand::Register(GPR0), Assembler::Operand::Imm(48));
  274. // GPR1 = rhs >> 48;
  275. m_assembler.mov(Assembler::Operand::Register(GPR1), Assembler::Operand::Register(rhs));
  276. m_assembler.shift_right(Assembler::Operand::Register(GPR1), Assembler::Operand::Imm(48));
  277. Assembler::Label not_int32_case {};
  278. m_assembler.jump_if(
  279. Assembler::Operand::Register(GPR0),
  280. Assembler::Condition::NotEqualTo,
  281. Assembler::Operand::Imm(INT32_TAG),
  282. not_int32_case);
  283. m_assembler.jump_if(
  284. Assembler::Operand::Register(GPR1),
  285. Assembler::Condition::NotEqualTo,
  286. Assembler::Operand::Imm(INT32_TAG),
  287. not_int32_case);
  288. codegen();
  289. not_int32_case.link(m_assembler);
  290. }
  291. void Compiler::compile_increment(Bytecode::Op::Increment const&)
  292. {
  293. load_accumulator(ARG1);
  294. Assembler::Label end {};
  295. Assembler::Label slow_case {};
  296. branch_if_int32(ARG1, [&] {
  297. // GPR0 = ARG1 & 0xffffffff;
  298. m_assembler.mov(
  299. Assembler::Operand::Register(GPR0),
  300. Assembler::Operand::Register(ARG1));
  301. m_assembler.mov(
  302. Assembler::Operand::Register(GPR1),
  303. Assembler::Operand::Imm(0xffffffff));
  304. m_assembler.bitwise_and(
  305. Assembler::Operand::Register(GPR0),
  306. Assembler::Operand::Register(GPR1));
  307. // if (GPR0 == 0x7fffffff) goto slow_case;
  308. m_assembler.jump_if(
  309. Assembler::Operand::Register(GPR0),
  310. Assembler::Condition::EqualTo,
  311. Assembler::Operand::Imm(0x7fffffff),
  312. slow_case);
  313. // ARG1 += 1;
  314. m_assembler.add(
  315. Assembler::Operand::Register(ARG1),
  316. Assembler::Operand::Imm(1));
  317. // accumulator = ARG1;
  318. store_accumulator(ARG1);
  319. m_assembler.jump(end);
  320. });
  321. slow_case.link(m_assembler);
  322. native_call((void*)cxx_increment);
  323. store_accumulator(RET);
  324. check_exception();
  325. end.link(m_assembler);
  326. }
  327. static Value cxx_decrement(VM& vm, Value value)
  328. {
  329. auto old_value = TRY_OR_SET_EXCEPTION(value.to_numeric(vm));
  330. if (old_value.is_number())
  331. return Value(old_value.as_double() - 1);
  332. return BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  333. }
  334. void Compiler::compile_decrement(Bytecode::Op::Decrement const&)
  335. {
  336. load_accumulator(ARG1);
  337. native_call((void*)cxx_decrement);
  338. store_accumulator(RET);
  339. check_exception();
  340. }
  341. void Compiler::check_exception()
  342. {
  343. load_vm_register(GPR0, Bytecode::Register::exception());
  344. m_assembler.mov(Assembler::Operand::Register(GPR1), Assembler::Operand::Imm(Value().encoded()));
  345. if (auto const* handler = current_block().handler(); handler) {
  346. Assembler::Label no_exception;
  347. m_assembler.jump_if(
  348. Assembler::Operand::Register(GPR0),
  349. Assembler::Condition::EqualTo,
  350. Assembler::Operand::Register(GPR1),
  351. no_exception);
  352. store_accumulator(GPR0);
  353. store_vm_register(Bytecode::Register::exception(), GPR1);
  354. m_assembler.jump(label_for(*handler));
  355. no_exception.link(m_assembler);
  356. } else if (auto const* finalizer = current_block().finalizer(); finalizer) {
  357. store_vm_register(Bytecode::Register::exception(), GPR1);
  358. m_assembler.jump_if(Assembler::Operand::Register(GPR0),
  359. Assembler::Condition::NotEqualTo,
  360. Assembler::Operand::Register(GPR1),
  361. label_for(*finalizer));
  362. } else {
  363. m_assembler.jump_if(Assembler::Operand::Register(GPR0),
  364. Assembler::Condition::NotEqualTo,
  365. Assembler::Operand::Register(GPR1),
  366. m_exit_label);
  367. }
  368. }
  369. void Compiler::compile_enter_unwind_context(Bytecode::Op::EnterUnwindContext const& op)
  370. {
  371. m_assembler.jump(label_for(op.entry_point().block()));
  372. }
  373. void Compiler::compile_leave_unwind_context(Bytecode::Op::LeaveUnwindContext const&)
  374. {
  375. /* Nothing */
  376. }
  377. void Compiler::compile_throw(Bytecode::Op::Throw const&)
  378. {
  379. load_accumulator(GPR0);
  380. store_vm_register(Bytecode::Register::exception(), GPR0);
  381. check_exception();
  382. }
  383. static ThrowCompletionOr<Value> abstract_inequals(VM& vm, Value src1, Value src2)
  384. {
  385. return Value(!TRY(is_loosely_equal(vm, src1, src2)));
  386. }
  387. static ThrowCompletionOr<Value> abstract_equals(VM& vm, Value src1, Value src2)
  388. {
  389. return Value(TRY(is_loosely_equal(vm, src1, src2)));
  390. }
  391. static ThrowCompletionOr<Value> typed_inequals(VM&, Value src1, Value src2)
  392. {
  393. return Value(!is_strictly_equal(src1, src2));
  394. }
  395. static ThrowCompletionOr<Value> typed_equals(VM&, Value src1, Value src2)
  396. {
  397. return Value(is_strictly_equal(src1, src2));
  398. }
  399. # define DO_COMPILE_COMMON_BINARY_OP(TitleCaseName, snake_case_name) \
  400. static Value cxx_##snake_case_name(VM& vm, Value lhs, Value rhs) \
  401. { \
  402. return TRY_OR_SET_EXCEPTION(snake_case_name(vm, lhs, rhs)); \
  403. } \
  404. \
  405. void Compiler::compile_##snake_case_name(Bytecode::Op::TitleCaseName const& op) \
  406. { \
  407. load_vm_register(ARG1, op.lhs()); \
  408. load_accumulator(ARG2); \
  409. native_call((void*)cxx_##snake_case_name); \
  410. store_accumulator(RET); \
  411. check_exception(); \
  412. }
  413. JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(DO_COMPILE_COMMON_BINARY_OP)
  414. # undef DO_COMPILE_COMMON_BINARY_OP
  415. static Value cxx_add(VM& vm, Value lhs, Value rhs)
  416. {
  417. return TRY_OR_SET_EXCEPTION(add(vm, lhs, rhs));
  418. }
  419. void Compiler::compile_add(Bytecode::Op::Add const& op)
  420. {
  421. load_vm_register(ARG1, op.lhs());
  422. load_accumulator(ARG2);
  423. Assembler::Label end {};
  424. Assembler::Label slow_case {};
  425. branch_if_both_int32(ARG1, ARG2, [&] {
  426. // GPR0 = ARG1 + ARG2 (32-bit)
  427. // if (overflow) goto slow_case;
  428. m_assembler.mov(
  429. Assembler::Operand::Register(GPR0),
  430. Assembler::Operand::Register(ARG1));
  431. m_assembler.add32(
  432. Assembler::Operand::Register(GPR0),
  433. Assembler::Operand::Register(ARG2),
  434. slow_case);
  435. // accumulator = GPR0 | SHIFTED_INT32_TAG;
  436. m_assembler.mov(
  437. Assembler::Operand::Register(GPR1),
  438. Assembler::Operand::Imm(SHIFTED_INT32_TAG));
  439. m_assembler.bitwise_or(
  440. Assembler::Operand::Register(GPR0),
  441. Assembler::Operand::Register(GPR1));
  442. store_accumulator(GPR0);
  443. m_assembler.jump(end);
  444. });
  445. slow_case.link(m_assembler);
  446. native_call((void*)cxx_add);
  447. store_accumulator(RET);
  448. check_exception();
  449. end.link(m_assembler);
  450. }
  451. static Value cxx_less_than(VM& vm, Value lhs, Value rhs)
  452. {
  453. return TRY_OR_SET_EXCEPTION(less_than(vm, lhs, rhs));
  454. }
  455. void Compiler::compile_less_than(Bytecode::Op::LessThan const& op)
  456. {
  457. load_vm_register(ARG1, op.lhs());
  458. load_accumulator(ARG2);
  459. Assembler::Label end {};
  460. branch_if_both_int32(ARG1, ARG2, [&] {
  461. // if (ARG1 < ARG2) return true;
  462. // else return false;
  463. Assembler::Label true_case {};
  464. m_assembler.sign_extend_32_to_64_bits(ARG1);
  465. m_assembler.sign_extend_32_to_64_bits(ARG2);
  466. m_assembler.jump_if(
  467. Assembler::Operand::Register(ARG1),
  468. Assembler::Condition::SignedLessThan,
  469. Assembler::Operand::Register(ARG2),
  470. true_case);
  471. m_assembler.mov(
  472. Assembler::Operand::Register(GPR0),
  473. Assembler::Operand::Imm(Value(false).encoded()));
  474. store_accumulator(GPR0);
  475. m_assembler.jump(end);
  476. true_case.link(m_assembler);
  477. m_assembler.mov(
  478. Assembler::Operand::Register(GPR0),
  479. Assembler::Operand::Imm(Value(true).encoded()));
  480. store_accumulator(GPR0);
  481. m_assembler.jump(end);
  482. });
  483. native_call((void*)cxx_less_than);
  484. store_accumulator(RET);
  485. check_exception();
  486. end.link(m_assembler);
  487. }
  488. static ThrowCompletionOr<Value> not_(VM&, Value value)
  489. {
  490. return Value(!value.to_boolean());
  491. }
  492. static ThrowCompletionOr<Value> typeof_(VM& vm, Value value)
  493. {
  494. return PrimitiveString::create(vm, value.typeof());
  495. }
  496. # define DO_COMPILE_COMMON_UNARY_OP(TitleCaseName, snake_case_name) \
  497. static Value cxx_##snake_case_name(VM& vm, Value value) \
  498. { \
  499. return TRY_OR_SET_EXCEPTION(snake_case_name(vm, value)); \
  500. } \
  501. \
  502. void Compiler::compile_##snake_case_name(Bytecode::Op::TitleCaseName const&) \
  503. { \
  504. load_accumulator(ARG1); \
  505. native_call((void*)cxx_##snake_case_name); \
  506. store_accumulator(RET); \
  507. check_exception(); \
  508. }
  509. JS_ENUMERATE_COMMON_UNARY_OPS(DO_COMPILE_COMMON_UNARY_OP)
  510. # undef DO_COMPILE_COMMON_UNARY_OP
  511. void Compiler::compile_return(Bytecode::Op::Return const&)
  512. {
  513. load_accumulator(GPR0);
  514. if (auto const* finalizer = current_block().finalizer(); finalizer) {
  515. store_vm_register(Bytecode::Register::saved_return_value(), GPR0);
  516. m_assembler.jump(label_for(*finalizer));
  517. } else {
  518. store_vm_register(Bytecode::Register::return_value(), GPR0);
  519. jump_to_exit();
  520. }
  521. }
  522. static Value cxx_new_string(VM& vm, DeprecatedString const& string)
  523. {
  524. return PrimitiveString::create(vm, string);
  525. }
  526. void Compiler::compile_new_string(Bytecode::Op::NewString const& op)
  527. {
  528. auto const& string = m_bytecode_executable.string_table->get(op.index());
  529. m_assembler.mov(
  530. Assembler::Operand::Register(ARG1),
  531. Assembler::Operand::Imm(bit_cast<u64>(&string)));
  532. native_call((void*)cxx_new_string);
  533. store_accumulator(RET);
  534. }
  535. void Compiler::compile_new_regexp(Bytecode::Op::NewRegExp const& op)
  536. {
  537. auto const& parsed_regex = m_bytecode_executable.regex_table->get(op.regex_index());
  538. auto const& pattern = m_bytecode_executable.string_table->get(op.source_index());
  539. auto const& flags = m_bytecode_executable.string_table->get(op.flags_index());
  540. m_assembler.mov(
  541. Assembler::Operand::Register(ARG1),
  542. Assembler::Operand::Imm(bit_cast<u64>(&parsed_regex)));
  543. m_assembler.mov(
  544. Assembler::Operand::Register(ARG2),
  545. Assembler::Operand::Imm(bit_cast<u64>(&pattern)));
  546. m_assembler.mov(
  547. Assembler::Operand::Register(ARG3),
  548. Assembler::Operand::Imm(bit_cast<u64>(&flags)));
  549. native_call((void*)Bytecode::new_regexp);
  550. store_accumulator(RET);
  551. }
  552. static Value cxx_new_bigint(VM& vm, Crypto::SignedBigInteger const& bigint)
  553. {
  554. return BigInt::create(vm, bigint);
  555. }
  556. void Compiler::compile_new_bigint(Bytecode::Op::NewBigInt const& op)
  557. {
  558. m_assembler.mov(
  559. Assembler::Operand::Register(ARG1),
  560. Assembler::Operand::Imm(bit_cast<u64>(&op.bigint())));
  561. native_call((void*)cxx_new_bigint);
  562. store_accumulator(RET);
  563. }
  564. static Value cxx_new_object(VM& vm)
  565. {
  566. auto& realm = *vm.current_realm();
  567. return Object::create(realm, realm.intrinsics().object_prototype());
  568. }
  569. void Compiler::compile_new_object(Bytecode::Op::NewObject const&)
  570. {
  571. native_call((void*)cxx_new_object);
  572. store_accumulator(RET);
  573. }
  574. static Value cxx_new_array(VM& vm, size_t element_count, u32 first_register_index)
  575. {
  576. auto& realm = *vm.current_realm();
  577. auto array = MUST(Array::create(realm, 0));
  578. for (size_t i = 0; i < element_count; ++i) {
  579. auto& value = vm.bytecode_interpreter().reg(Bytecode::Register(first_register_index + i));
  580. array->indexed_properties().put(i, value, default_attributes);
  581. }
  582. return array;
  583. }
  584. void Compiler::compile_new_array(Bytecode::Op::NewArray const& op)
  585. {
  586. m_assembler.mov(
  587. Assembler::Operand::Register(ARG1),
  588. Assembler::Operand::Imm(op.element_count()));
  589. m_assembler.mov(
  590. Assembler::Operand::Register(ARG2),
  591. Assembler::Operand::Imm(op.element_count() ? op.start().index() : 0));
  592. native_call((void*)cxx_new_array);
  593. store_accumulator(RET);
  594. }
  595. void Compiler::compile_new_function(Bytecode::Op::NewFunction const& op)
  596. {
  597. m_assembler.mov(
  598. Assembler::Operand::Register(ARG1),
  599. Assembler::Operand::Imm(bit_cast<u64>(&op.function_node())));
  600. m_assembler.mov(
  601. Assembler::Operand::Register(ARG2),
  602. Assembler::Operand::Imm(bit_cast<u64>(&op.lhs_name())));
  603. m_assembler.mov(
  604. Assembler::Operand::Register(ARG3),
  605. Assembler::Operand::Imm(bit_cast<u64>(&op.home_object())));
  606. native_call((void*)Bytecode::new_function);
  607. store_accumulator(RET);
  608. }
  609. static Value cxx_new_class(VM& vm, Value super_class, ClassExpression const& class_expression, Optional<Bytecode::IdentifierTableIndex> const& lhs_name)
  610. {
  611. return TRY_OR_SET_EXCEPTION(Bytecode::new_class(vm, super_class, class_expression, lhs_name));
  612. }
  613. void Compiler::compile_new_class(Bytecode::Op::NewClass const& op)
  614. {
  615. load_accumulator(ARG1);
  616. m_assembler.mov(
  617. Assembler::Operand::Register(ARG2),
  618. Assembler::Operand::Imm(bit_cast<u64>(&op.class_expression())));
  619. m_assembler.mov(
  620. Assembler::Operand::Register(ARG3),
  621. Assembler::Operand::Imm(bit_cast<u64>(&op.lhs_name())));
  622. native_call((void*)cxx_new_class);
  623. store_accumulator(RET);
  624. }
  625. static Value cxx_get_by_id(VM& vm, Value base, Bytecode::IdentifierTableIndex property, u32 cache_index)
  626. {
  627. return TRY_OR_SET_EXCEPTION(Bytecode::get_by_id(vm.bytecode_interpreter(), property, base, base, cache_index));
  628. }
  629. void Compiler::compile_get_by_id(Bytecode::Op::GetById const& op)
  630. {
  631. load_accumulator(ARG1);
  632. m_assembler.mov(
  633. Assembler::Operand::Register(ARG2),
  634. Assembler::Operand::Imm(op.property().value()));
  635. m_assembler.mov(
  636. Assembler::Operand::Register(ARG3),
  637. Assembler::Operand::Imm(op.cache_index()));
  638. native_call((void*)cxx_get_by_id);
  639. store_accumulator(RET);
  640. check_exception();
  641. }
  642. static Value cxx_get_by_value(VM& vm, Value base, Value property)
  643. {
  644. return TRY_OR_SET_EXCEPTION(Bytecode::get_by_value(vm.bytecode_interpreter(), base, property));
  645. }
  646. void Compiler::compile_get_by_value(Bytecode::Op::GetByValue const& op)
  647. {
  648. load_vm_register(ARG1, op.base());
  649. load_accumulator(ARG2);
  650. native_call((void*)cxx_get_by_value);
  651. store_accumulator(RET);
  652. check_exception();
  653. }
  654. static Value cxx_get_global(VM& vm, Bytecode::IdentifierTableIndex identifier, u32 cache_index)
  655. {
  656. return TRY_OR_SET_EXCEPTION(Bytecode::get_global(vm.bytecode_interpreter(), identifier, cache_index));
  657. }
  658. void Compiler::compile_get_global(Bytecode::Op::GetGlobal const& op)
  659. {
  660. m_assembler.mov(
  661. Assembler::Operand::Register(ARG1),
  662. Assembler::Operand::Imm(op.identifier().value()));
  663. m_assembler.mov(
  664. Assembler::Operand::Register(ARG2),
  665. Assembler::Operand::Imm(op.cache_index()));
  666. native_call((void*)cxx_get_global);
  667. store_accumulator(RET);
  668. check_exception();
  669. }
  670. static Value cxx_get_variable(VM& vm, DeprecatedFlyString const& name, u32 cache_index)
  671. {
  672. return TRY_OR_SET_EXCEPTION(Bytecode::get_variable(vm.bytecode_interpreter(), name, cache_index));
  673. }
  674. void Compiler::compile_get_variable(Bytecode::Op::GetVariable const& op)
  675. {
  676. m_assembler.mov(
  677. Assembler::Operand::Register(ARG1),
  678. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier()))));
  679. m_assembler.mov(
  680. Assembler::Operand::Register(ARG2),
  681. Assembler::Operand::Imm(op.cache_index()));
  682. native_call((void*)cxx_get_variable);
  683. store_accumulator(RET);
  684. check_exception();
  685. }
  686. 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)
  687. {
  688. auto& bytecode_interpreter = vm.bytecode_interpreter();
  689. auto callee_and_this = TRY_OR_SET_EXCEPTION(Bytecode::get_callee_and_this_from_environment(
  690. bytecode_interpreter,
  691. name,
  692. cache_index));
  693. bytecode_interpreter.reg(callee_reg) = callee_and_this.callee;
  694. bytecode_interpreter.reg(this_reg) = callee_and_this.this_value;
  695. return {};
  696. }
  697. void Compiler::compile_get_callee_and_this_from_environment(Bytecode::Op::GetCalleeAndThisFromEnvironment const& op)
  698. {
  699. m_assembler.mov(
  700. Assembler::Operand::Register(ARG1),
  701. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier()))));
  702. m_assembler.mov(
  703. Assembler::Operand::Register(ARG2),
  704. Assembler::Operand::Imm(op.cache_index()));
  705. m_assembler.mov(
  706. Assembler::Operand::Register(ARG3),
  707. Assembler::Operand::Imm(op.callee().index()));
  708. m_assembler.mov(
  709. Assembler::Operand::Register(ARG4),
  710. Assembler::Operand::Imm(op.this_().index()));
  711. native_call((void*)cxx_get_callee_and_this_from_environment);
  712. check_exception();
  713. }
  714. static Value cxx_to_numeric(VM& vm, Value value)
  715. {
  716. return TRY_OR_SET_EXCEPTION(value.to_numeric(vm));
  717. }
  718. void Compiler::compile_to_numeric(Bytecode::Op::ToNumeric const&)
  719. {
  720. Assembler::Label fast_case {};
  721. load_accumulator(ARG1);
  722. jump_if_int32(ARG1, fast_case);
  723. native_call((void*)cxx_to_numeric);
  724. store_accumulator(RET);
  725. check_exception();
  726. fast_case.link(m_assembler);
  727. }
  728. static Value cxx_resolve_this_binding(VM& vm)
  729. {
  730. auto this_value = TRY_OR_SET_EXCEPTION(vm.resolve_this_binding());
  731. vm.bytecode_interpreter().reg(Bytecode::Register::this_value()) = this_value;
  732. return this_value;
  733. }
  734. void Compiler::compile_resolve_this_binding(Bytecode::Op::ResolveThisBinding const&)
  735. {
  736. // OPTIMIZATION: We cache the `this` value in a special VM register.
  737. // So first we check if the cache is non-empty, and if so,
  738. // we can avoid calling out to C++ at all. :^)
  739. load_vm_register(GPR0, Bytecode::Register::this_value());
  740. m_assembler.mov(
  741. Assembler::Operand::Register(GPR1),
  742. Assembler::Operand::Imm(Value().encoded()));
  743. Assembler::Label slow_case {};
  744. m_assembler.jump_if(
  745. Assembler::Operand::Register(GPR0),
  746. Assembler::Condition::EqualTo,
  747. Assembler::Operand::Register(GPR1),
  748. slow_case);
  749. // Fast case: We have a cached `this` value!
  750. store_accumulator(GPR0);
  751. auto end = m_assembler.jump();
  752. slow_case.link(m_assembler);
  753. native_call((void*)cxx_resolve_this_binding);
  754. store_accumulator(RET);
  755. check_exception();
  756. end.link(m_assembler);
  757. }
  758. static Value cxx_put_by_id(VM& vm, Value base, Bytecode::IdentifierTableIndex property, Value value, Bytecode::Op::PropertyKind kind)
  759. {
  760. PropertyKey name = vm.bytecode_interpreter().current_executable().get_identifier(property);
  761. TRY_OR_SET_EXCEPTION(Bytecode::put_by_property_key(vm, base, base, value, name, kind));
  762. return value;
  763. }
  764. void Compiler::compile_put_by_id(Bytecode::Op::PutById const& op)
  765. {
  766. load_vm_register(ARG1, op.base());
  767. m_assembler.mov(
  768. Assembler::Operand::Register(ARG2),
  769. Assembler::Operand::Imm(op.property().value()));
  770. load_accumulator(ARG3);
  771. m_assembler.mov(
  772. Assembler::Operand::Register(ARG4),
  773. Assembler::Operand::Imm(to_underlying(op.kind())));
  774. native_call((void*)cxx_put_by_id);
  775. store_accumulator(RET);
  776. check_exception();
  777. }
  778. static Value cxx_put_by_value(VM& vm, Value base, Value property, Value value, Bytecode::Op::PropertyKind kind)
  779. {
  780. TRY_OR_SET_EXCEPTION(Bytecode::put_by_value(vm, base, property, value, kind));
  781. return value;
  782. }
  783. void Compiler::compile_put_by_value(Bytecode::Op::PutByValue const& op)
  784. {
  785. load_vm_register(ARG1, op.base());
  786. load_vm_register(ARG2, op.property());
  787. load_accumulator(ARG3);
  788. m_assembler.mov(
  789. Assembler::Operand::Register(ARG4),
  790. Assembler::Operand::Imm(to_underlying(op.kind())));
  791. native_call((void*)cxx_put_by_value);
  792. store_accumulator(RET);
  793. check_exception();
  794. }
  795. static Value cxx_call(VM& vm, Value callee, u32 first_argument_index, u32 argument_count, Value this_value, Bytecode::Op::CallType call_type, Optional<Bytecode::StringTableIndex> const& expression_string)
  796. {
  797. TRY_OR_SET_EXCEPTION(throw_if_needed_for_call(vm.bytecode_interpreter(), callee, call_type, expression_string));
  798. MarkedVector<Value> argument_values(vm.heap());
  799. argument_values.ensure_capacity(argument_count);
  800. for (u32 i = 0; i < argument_count; ++i) {
  801. argument_values.unchecked_append(vm.bytecode_interpreter().reg(Bytecode::Register { first_argument_index + i }));
  802. }
  803. return TRY_OR_SET_EXCEPTION(perform_call(vm.bytecode_interpreter(), this_value, call_type, callee, move(argument_values)));
  804. }
  805. void Compiler::compile_call(Bytecode::Op::Call const& op)
  806. {
  807. load_vm_register(ARG1, op.callee());
  808. m_assembler.mov(
  809. Assembler::Operand::Register(ARG2),
  810. Assembler::Operand::Imm(op.first_argument().index()));
  811. m_assembler.mov(
  812. Assembler::Operand::Register(ARG3),
  813. Assembler::Operand::Imm(op.argument_count()));
  814. load_vm_register(ARG4, op.this_value());
  815. m_assembler.mov(
  816. Assembler::Operand::Register(ARG5),
  817. Assembler::Operand::Imm(to_underlying(op.call_type())));
  818. m_assembler.mov(
  819. Assembler::Operand::Register(GPR0),
  820. Assembler::Operand::Imm(bit_cast<u64>(&op.expression_string())));
  821. native_call((void*)cxx_call, { Assembler::Operand::Register(GPR0) });
  822. store_accumulator(RET);
  823. check_exception();
  824. }
  825. static Value cxx_call_with_argument_array(VM& vm, Value arguments, Value callee, Value this_value, Bytecode::Op::CallType call_type, Optional<Bytecode::StringTableIndex> const& expression_string)
  826. {
  827. TRY_OR_SET_EXCEPTION(throw_if_needed_for_call(vm.bytecode_interpreter(), callee, call_type, expression_string));
  828. auto argument_values = Bytecode::argument_list_evaluation(vm, arguments);
  829. return TRY_OR_SET_EXCEPTION(perform_call(vm.bytecode_interpreter(), this_value, call_type, callee, move(argument_values)));
  830. }
  831. void Compiler::compile_call_with_argument_array(Bytecode::Op::CallWithArgumentArray const& op)
  832. {
  833. load_accumulator(ARG1);
  834. load_vm_register(ARG2, op.callee());
  835. load_vm_register(ARG3, op.this_value());
  836. m_assembler.mov(
  837. Assembler::Operand::Register(ARG4),
  838. Assembler::Operand::Imm(to_underlying(op.call_type())));
  839. m_assembler.mov(
  840. Assembler::Operand::Register(ARG5),
  841. Assembler::Operand::Imm(bit_cast<u64>(&op.expression_string())));
  842. native_call((void*)cxx_call_with_argument_array);
  843. store_accumulator(RET);
  844. check_exception();
  845. }
  846. static Value cxx_typeof_variable(VM& vm, DeprecatedFlyString const& identifier)
  847. {
  848. return TRY_OR_SET_EXCEPTION(Bytecode::typeof_variable(vm, identifier));
  849. }
  850. void Compiler::compile_typeof_variable(Bytecode::Op::TypeofVariable const& op)
  851. {
  852. m_assembler.mov(
  853. Assembler::Operand::Register(ARG1),
  854. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier().value()))));
  855. native_call((void*)cxx_typeof_variable);
  856. store_accumulator(RET);
  857. check_exception();
  858. }
  859. static Value cxx_create_variable(
  860. VM& vm,
  861. DeprecatedFlyString const& name,
  862. Bytecode::Op::EnvironmentMode mode,
  863. bool is_global,
  864. bool is_immutable,
  865. bool is_strict)
  866. {
  867. TRY_OR_SET_EXCEPTION(Bytecode::create_variable(vm, name, mode, is_global, is_immutable, is_strict));
  868. return {};
  869. }
  870. void Compiler::compile_create_variable(Bytecode::Op::CreateVariable const& op)
  871. {
  872. m_assembler.mov(
  873. Assembler::Operand::Register(ARG1),
  874. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier().value()))));
  875. m_assembler.mov(
  876. Assembler::Operand::Register(ARG2),
  877. Assembler::Operand::Imm(to_underlying(op.mode())));
  878. m_assembler.mov(
  879. Assembler::Operand::Register(ARG3),
  880. Assembler::Operand::Imm(static_cast<u64>(op.is_global())));
  881. m_assembler.mov(
  882. Assembler::Operand::Register(ARG4),
  883. Assembler::Operand::Imm(static_cast<u64>(op.is_immutable())));
  884. m_assembler.mov(
  885. Assembler::Operand::Register(ARG5),
  886. Assembler::Operand::Imm(static_cast<u64>(op.is_strict())));
  887. native_call((void*)cxx_create_variable);
  888. check_exception();
  889. }
  890. static Value cxx_set_variable(
  891. VM& vm,
  892. DeprecatedFlyString const& identifier,
  893. Value value,
  894. Bytecode::Op::EnvironmentMode environment_mode,
  895. Bytecode::Op::SetVariable::InitializationMode initialization_mode)
  896. {
  897. TRY_OR_SET_EXCEPTION(Bytecode::set_variable(vm, identifier, value, environment_mode, initialization_mode));
  898. return {};
  899. }
  900. void Compiler::compile_set_variable(Bytecode::Op::SetVariable const& op)
  901. {
  902. m_assembler.mov(
  903. Assembler::Operand::Register(ARG1),
  904. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier().value()))));
  905. load_accumulator(ARG2);
  906. m_assembler.mov(
  907. Assembler::Operand::Register(ARG3),
  908. Assembler::Operand::Imm(to_underlying(op.mode())));
  909. m_assembler.mov(
  910. Assembler::Operand::Register(ARG4),
  911. Assembler::Operand::Imm(to_underlying(op.initialization_mode())));
  912. native_call((void*)cxx_set_variable);
  913. check_exception();
  914. }
  915. void Compiler::compile_continue_pending_unwind(Bytecode::Op::ContinuePendingUnwind const& op)
  916. {
  917. // re-throw the exception if we reached the end of the finally block and there was no catch block to handle it
  918. check_exception();
  919. // if (saved_return_value.is_empty()) goto resume_block;
  920. load_vm_register(GPR0, Bytecode::Register::saved_return_value());
  921. m_assembler.mov(Assembler::Operand::Register(GPR1), Assembler::Operand::Imm(Value().encoded()));
  922. m_assembler.jump_if(
  923. Assembler::Operand::Register(GPR0),
  924. Assembler::Condition::EqualTo,
  925. Assembler::Operand::Register(GPR1),
  926. label_for(op.resume_target().block()));
  927. // finish the pending return from the try block
  928. store_vm_register(Bytecode::Register::return_value(), GPR0);
  929. jump_to_exit();
  930. }
  931. static void cxx_create_lexical_environment(VM& vm)
  932. {
  933. auto make_and_swap_envs = [&](auto& old_environment) {
  934. GCPtr<Environment> environment = new_declarative_environment(*old_environment).ptr();
  935. swap(old_environment, environment);
  936. return environment;
  937. };
  938. vm.bytecode_interpreter().saved_lexical_environment_stack().append(make_and_swap_envs(vm.running_execution_context().lexical_environment));
  939. }
  940. void Compiler::compile_create_lexical_environment(Bytecode::Op::CreateLexicalEnvironment const&)
  941. {
  942. native_call((void*)cxx_create_lexical_environment);
  943. }
  944. static void cxx_leave_lexical_environment(VM& vm)
  945. {
  946. vm.running_execution_context().lexical_environment = vm.bytecode_interpreter().saved_lexical_environment_stack().take_last();
  947. }
  948. void Compiler::compile_leave_lexical_environment(Bytecode::Op::LeaveLexicalEnvironment const&)
  949. {
  950. native_call((void*)cxx_leave_lexical_environment);
  951. }
  952. static Value cxx_concat_string(VM& vm, Value lhs, Value rhs)
  953. {
  954. auto string = TRY_OR_SET_EXCEPTION(rhs.to_primitive_string(vm));
  955. return PrimitiveString::create(vm, lhs.as_string(), string);
  956. }
  957. void Compiler::compile_concat_string(Bytecode::Op::ConcatString const& op)
  958. {
  959. load_vm_register(ARG1, op.lhs());
  960. load_accumulator(ARG2);
  961. native_call((void*)cxx_concat_string);
  962. store_vm_register(op.lhs(), RET);
  963. check_exception();
  964. }
  965. static void cxx_block_declaration_instantiation(VM& vm, ScopeNode const& scope_node)
  966. {
  967. auto old_environment = vm.running_execution_context().lexical_environment;
  968. vm.bytecode_interpreter().saved_lexical_environment_stack().append(old_environment);
  969. vm.running_execution_context().lexical_environment = new_declarative_environment(*old_environment);
  970. scope_node.block_declaration_instantiation(vm, vm.running_execution_context().lexical_environment);
  971. }
  972. void Compiler::compile_block_declaration_instantiation(Bytecode::Op::BlockDeclarationInstantiation const& op)
  973. {
  974. m_assembler.mov(
  975. Assembler::Operand::Register(ARG1),
  976. Assembler::Operand::Imm(bit_cast<u64>(&op.scope_node())));
  977. native_call((void*)cxx_block_declaration_instantiation);
  978. }
  979. static Value cxx_super_call_with_argument_array(VM& vm, Value argument_array, bool is_synthetic)
  980. {
  981. return TRY_OR_SET_EXCEPTION(Bytecode::super_call_with_argument_array(vm, argument_array, is_synthetic));
  982. }
  983. void Compiler::compile_super_call_with_argument_array(Bytecode::Op::SuperCallWithArgumentArray const& op)
  984. {
  985. load_accumulator(ARG1);
  986. m_assembler.mov(
  987. Assembler::Operand::Register(ARG2),
  988. Assembler::Operand::Imm(static_cast<u64>(op.is_synthetic())));
  989. native_call((void*)cxx_super_call_with_argument_array);
  990. store_accumulator(RET);
  991. check_exception();
  992. }
  993. static Value cxx_get_iterator(VM& vm, Value value, IteratorHint hint)
  994. {
  995. auto iterator = TRY_OR_SET_EXCEPTION(get_iterator(vm, value, hint));
  996. return Bytecode::iterator_to_object(vm, iterator);
  997. }
  998. void Compiler::compile_get_iterator(Bytecode::Op::GetIterator const& op)
  999. {
  1000. load_accumulator(ARG1);
  1001. m_assembler.mov(
  1002. Assembler::Operand::Register(ARG2),
  1003. Assembler::Operand::Imm(to_underlying(op.hint())));
  1004. native_call((void*)cxx_get_iterator);
  1005. store_accumulator(RET);
  1006. check_exception();
  1007. }
  1008. static Value cxx_iterator_next(VM& vm, Value iterator)
  1009. {
  1010. auto iterator_object = TRY_OR_SET_EXCEPTION(iterator.to_object(vm));
  1011. auto iterator_record = Bytecode::object_to_iterator(vm, iterator_object);
  1012. return TRY_OR_SET_EXCEPTION(iterator_next(vm, iterator_record));
  1013. }
  1014. void Compiler::compile_iterator_next(Bytecode::Op::IteratorNext const&)
  1015. {
  1016. load_accumulator(ARG1);
  1017. native_call((void*)cxx_iterator_next);
  1018. store_accumulator(RET);
  1019. check_exception();
  1020. }
  1021. static Value cxx_iterator_result_done(VM& vm, Value iterator)
  1022. {
  1023. auto iterator_result = TRY_OR_SET_EXCEPTION(iterator.to_object(vm));
  1024. return Value(TRY_OR_SET_EXCEPTION(iterator_complete(vm, iterator_result)));
  1025. }
  1026. void Compiler::compile_iterator_result_done(Bytecode::Op::IteratorResultDone const&)
  1027. {
  1028. load_accumulator(ARG1);
  1029. native_call((void*)cxx_iterator_result_done);
  1030. store_accumulator(RET);
  1031. check_exception();
  1032. }
  1033. static Value cxx_throw_if_not_object(VM& vm, Value value)
  1034. {
  1035. if (!value.is_object())
  1036. TRY_OR_SET_EXCEPTION(vm.throw_completion<TypeError>(ErrorType::NotAnObject, value.to_string_without_side_effects()));
  1037. return {};
  1038. }
  1039. void Compiler::compile_throw_if_not_object(Bytecode::Op::ThrowIfNotObject const&)
  1040. {
  1041. load_accumulator(ARG1);
  1042. native_call((void*)cxx_throw_if_not_object);
  1043. check_exception();
  1044. }
  1045. static Value cxx_throw_if_nullish(VM& vm, Value value)
  1046. {
  1047. if (value.is_nullish())
  1048. TRY_OR_SET_EXCEPTION(vm.throw_completion<TypeError>(ErrorType::NotObjectCoercible, value.to_string_without_side_effects()));
  1049. return {};
  1050. }
  1051. void Compiler::compile_throw_if_nullish(Bytecode::Op::ThrowIfNullish const&)
  1052. {
  1053. load_accumulator(ARG1);
  1054. native_call((void*)cxx_throw_if_nullish);
  1055. check_exception();
  1056. }
  1057. static Value cxx_iterator_result_value(VM& vm, Value iterator)
  1058. {
  1059. auto iterator_result = TRY_OR_SET_EXCEPTION(iterator.to_object(vm));
  1060. return TRY_OR_SET_EXCEPTION(iterator_value(vm, iterator_result));
  1061. }
  1062. void Compiler::compile_iterator_result_value(Bytecode::Op::IteratorResultValue const&)
  1063. {
  1064. load_accumulator(ARG1);
  1065. native_call((void*)cxx_iterator_result_value);
  1066. store_accumulator(RET);
  1067. check_exception();
  1068. }
  1069. static Value cxx_iterator_close(VM& vm, Value iterator, Completion::Type completion_type, Optional<Value> const& completion_value)
  1070. {
  1071. auto iterator_object = TRY_OR_SET_EXCEPTION(iterator.to_object(vm));
  1072. auto iterator_record = Bytecode::object_to_iterator(vm, iterator_object);
  1073. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1074. TRY_OR_SET_EXCEPTION(iterator_close(vm, iterator_record, Completion { completion_type, completion_value, {} }));
  1075. return {};
  1076. }
  1077. void Compiler::compile_iterator_close(Bytecode::Op::IteratorClose const& op)
  1078. {
  1079. load_accumulator(ARG1);
  1080. m_assembler.mov(
  1081. Assembler::Operand::Register(ARG2),
  1082. Assembler::Operand::Imm(to_underlying(op.completion_type())));
  1083. m_assembler.mov(
  1084. Assembler::Operand::Register(ARG3),
  1085. Assembler::Operand::Imm(bit_cast<u64>(&op.completion_value())));
  1086. native_call((void*)cxx_iterator_close);
  1087. check_exception();
  1088. }
  1089. static Value iterator_to_array(VM& vm, Value iterator)
  1090. {
  1091. return TRY_OR_SET_EXCEPTION(Bytecode::iterator_to_array(vm, iterator));
  1092. }
  1093. void Compiler::compile_iterator_to_array(Bytecode::Op::IteratorToArray const&)
  1094. {
  1095. load_accumulator(ARG1);
  1096. native_call((void*)iterator_to_array);
  1097. store_accumulator(RET);
  1098. check_exception();
  1099. }
  1100. static Value cxx_append(VM& vm, Value lhs, Value rhs, bool is_spread)
  1101. {
  1102. TRY_OR_SET_EXCEPTION(Bytecode::append(vm, lhs, rhs, is_spread));
  1103. return {};
  1104. }
  1105. void Compiler::compile_append(Bytecode::Op::Append const& op)
  1106. {
  1107. load_vm_register(ARG1, op.lhs());
  1108. load_accumulator(ARG2);
  1109. m_assembler.mov(
  1110. Assembler::Operand::Register(ARG3),
  1111. Assembler::Operand::Imm(static_cast<u64>(op.is_spread())));
  1112. native_call((void*)cxx_append);
  1113. check_exception();
  1114. }
  1115. static Value cxx_delete_by_id(VM& vm, Value base, Bytecode::IdentifierTableIndex property)
  1116. {
  1117. return TRY_OR_SET_EXCEPTION(Bytecode::delete_by_id(vm.bytecode_interpreter(), base, property));
  1118. }
  1119. void Compiler::compile_delete_by_id(Bytecode::Op::DeleteById const& op)
  1120. {
  1121. load_accumulator(ARG1);
  1122. m_assembler.mov(
  1123. Assembler::Operand::Register(ARG2),
  1124. Assembler::Operand::Imm(op.property().value()));
  1125. native_call((void*)cxx_delete_by_id);
  1126. store_accumulator(RET);
  1127. check_exception();
  1128. }
  1129. static Value cxx_delete_by_value(VM& vm, Value base_value, Value property_key_value)
  1130. {
  1131. return TRY_OR_SET_EXCEPTION(Bytecode::delete_by_value(vm.bytecode_interpreter(), base_value, property_key_value));
  1132. }
  1133. void Compiler::compile_delete_by_value(Bytecode::Op::DeleteByValue const& op)
  1134. {
  1135. load_vm_register(ARG1, op.base());
  1136. load_accumulator(ARG2);
  1137. native_call((void*)cxx_delete_by_value);
  1138. store_accumulator(RET);
  1139. check_exception();
  1140. }
  1141. static Value cxx_delete_by_value_with_this(VM& vm, Value base_value, Value property_key_value, Value this_value)
  1142. {
  1143. return TRY_OR_SET_EXCEPTION(Bytecode::delete_by_value_with_this(vm.bytecode_interpreter(), base_value, property_key_value, this_value));
  1144. }
  1145. void Compiler::compile_delete_by_value_with_this(Bytecode::Op::DeleteByValueWithThis const& op)
  1146. {
  1147. load_vm_register(ARG1, op.base());
  1148. load_accumulator(ARG2);
  1149. load_vm_register(ARG3, op.this_value());
  1150. native_call((void*)cxx_delete_by_value_with_this);
  1151. store_accumulator(RET);
  1152. check_exception();
  1153. }
  1154. static Value cxx_get_object_property_iterator(VM& vm, Value object)
  1155. {
  1156. return TRY_OR_SET_EXCEPTION(Bytecode::get_object_property_iterator(vm, object));
  1157. }
  1158. void Compiler::compile_get_object_property_iterator(Bytecode::Op::GetObjectPropertyIterator const&)
  1159. {
  1160. load_accumulator(ARG1);
  1161. native_call((void*)cxx_get_object_property_iterator);
  1162. store_accumulator(RET);
  1163. check_exception();
  1164. }
  1165. static Value cxx_get_private_by_id(VM& vm, Value base_value, DeprecatedFlyString& name)
  1166. {
  1167. auto private_reference = make_private_reference(vm, base_value, name);
  1168. return TRY_OR_SET_EXCEPTION(private_reference.get_value(vm));
  1169. }
  1170. void Compiler::compile_get_private_by_id(Bytecode::Op::GetPrivateById const& op)
  1171. {
  1172. load_accumulator(ARG1);
  1173. m_assembler.mov(
  1174. Assembler::Operand::Register(ARG2),
  1175. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1176. native_call((void*)cxx_get_private_by_id);
  1177. store_accumulator(RET);
  1178. check_exception();
  1179. }
  1180. static Value cxx_resolve_super_base(VM& vm)
  1181. {
  1182. // 1. Let env be GetThisEnvironment().
  1183. auto& env = verify_cast<FunctionEnvironment>(*get_this_environment(vm));
  1184. // 2. Assert: env.HasSuperBinding() is true.
  1185. VERIFY(env.has_super_binding());
  1186. // 3. Let baseValue be ? env.GetSuperBase().
  1187. return TRY_OR_SET_EXCEPTION(env.get_super_base());
  1188. }
  1189. void Compiler::compile_resolve_super_base(Bytecode::Op::ResolveSuperBase const&)
  1190. {
  1191. native_call((void*)cxx_resolve_super_base);
  1192. store_accumulator(RET);
  1193. check_exception();
  1194. }
  1195. static Value cxx_get_by_id_with_this(VM& vm, Bytecode::IdentifierTableIndex property, Value base_value, Value this_value, u32 cache_index)
  1196. {
  1197. return TRY_OR_SET_EXCEPTION(get_by_id(vm.bytecode_interpreter(), property, base_value, this_value, cache_index));
  1198. }
  1199. void Compiler::compile_get_by_id_with_this(Bytecode::Op::GetByIdWithThis const& op)
  1200. {
  1201. m_assembler.mov(
  1202. Assembler::Operand::Register(ARG1),
  1203. Assembler::Operand::Imm(op.property().value()));
  1204. load_accumulator(ARG2);
  1205. load_vm_register(ARG3, op.this_value());
  1206. m_assembler.mov(
  1207. Assembler::Operand::Register(ARG4),
  1208. Assembler::Operand::Imm(op.cache_index()));
  1209. native_call((void*)cxx_get_by_id_with_this);
  1210. store_accumulator(RET);
  1211. check_exception();
  1212. }
  1213. static Value cxx_get_by_value_with_this(VM& vm, Value property_key_value, Value base, Value this_value)
  1214. {
  1215. auto object = TRY_OR_SET_EXCEPTION(base.to_object(vm));
  1216. auto property_key = TRY_OR_SET_EXCEPTION(property_key_value.to_property_key(vm));
  1217. return TRY_OR_SET_EXCEPTION(object->internal_get(property_key, this_value));
  1218. }
  1219. void Compiler::compile_get_by_value_with_this(Bytecode::Op::GetByValueWithThis const& op)
  1220. {
  1221. load_accumulator(ARG1);
  1222. load_vm_register(ARG2, op.base());
  1223. load_vm_register(ARG3, op.this_value());
  1224. native_call((void*)cxx_get_by_value_with_this);
  1225. store_accumulator(RET);
  1226. check_exception();
  1227. }
  1228. static Value cxx_delete_by_id_with_this(VM& vm, Value base_value, DeprecatedFlyString const& identifier, Value this_value)
  1229. {
  1230. auto reference = Reference { base_value, identifier, this_value, vm.in_strict_mode() };
  1231. return Value(TRY_OR_SET_EXCEPTION(reference.delete_(vm)));
  1232. }
  1233. void Compiler::compile_delete_by_id_with_this(Bytecode::Op::DeleteByIdWithThis const& op)
  1234. {
  1235. load_accumulator(ARG1);
  1236. m_assembler.mov(
  1237. Assembler::Operand::Register(ARG2),
  1238. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1239. load_vm_register(ARG3, op.this_value());
  1240. native_call((void*)cxx_delete_by_id_with_this);
  1241. store_accumulator(RET);
  1242. }
  1243. static Value cxx_put_by_id_with_this(VM& vm, Value base, Value value, DeprecatedFlyString const& name, Value this_value, Bytecode::Op::PropertyKind kind)
  1244. {
  1245. TRY_OR_SET_EXCEPTION(Bytecode::put_by_property_key(vm, base, this_value, value, name, kind));
  1246. return {};
  1247. }
  1248. void Compiler::compile_put_by_id_with_this(Bytecode::Op::PutByIdWithThis const& op)
  1249. {
  1250. load_vm_register(ARG1, op.base());
  1251. load_accumulator(ARG2);
  1252. m_assembler.mov(
  1253. Assembler::Operand::Register(ARG3),
  1254. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1255. load_vm_register(ARG4, op.this_value());
  1256. m_assembler.mov(
  1257. Assembler::Operand::Register(ARG5),
  1258. Assembler::Operand::Imm(to_underlying(op.kind())));
  1259. native_call((void*)cxx_put_by_id_with_this);
  1260. check_exception();
  1261. }
  1262. static Value cxx_put_private_by_id(VM& vm, Value base, Value value, DeprecatedFlyString const& name)
  1263. {
  1264. auto object = TRY_OR_SET_EXCEPTION(base.to_object(vm));
  1265. auto private_reference = make_private_reference(vm, object, name);
  1266. TRY_OR_SET_EXCEPTION(private_reference.put_value(vm, value));
  1267. return value;
  1268. }
  1269. void Compiler::compile_put_private_by_id(Bytecode::Op::PutPrivateById const& op)
  1270. {
  1271. load_vm_register(ARG1, op.base());
  1272. load_accumulator(ARG2);
  1273. m_assembler.mov(
  1274. Assembler::Operand::Register(ARG3),
  1275. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1276. native_call((void*)cxx_put_private_by_id);
  1277. store_accumulator(RET);
  1278. check_exception();
  1279. }
  1280. static Value cxx_import_call(VM& vm, Value specifier, Value options)
  1281. {
  1282. return TRY_OR_SET_EXCEPTION(perform_import_call(vm, specifier, options));
  1283. }
  1284. void Compiler::compile_import_call(Bytecode::Op::ImportCall const& op)
  1285. {
  1286. load_vm_register(ARG1, op.specifier());
  1287. load_vm_register(ARG2, op.options());
  1288. native_call((void*)cxx_import_call);
  1289. store_accumulator(RET);
  1290. check_exception();
  1291. }
  1292. static Value cxx_get_import_meta(VM& vm)
  1293. {
  1294. return vm.get_import_meta();
  1295. }
  1296. void Compiler::compile_get_import_meta(Bytecode::Op::GetImportMeta const&)
  1297. {
  1298. native_call((void*)cxx_get_import_meta);
  1299. store_accumulator(RET);
  1300. }
  1301. static Value cxx_delete_variable(VM& vm, DeprecatedFlyString const& identifier)
  1302. {
  1303. auto reference = TRY_OR_SET_EXCEPTION(vm.resolve_binding(identifier));
  1304. return Value(TRY_OR_SET_EXCEPTION(reference.delete_(vm)));
  1305. }
  1306. void Compiler::compile_delete_variable(Bytecode::Op::DeleteVariable const& op)
  1307. {
  1308. m_assembler.mov(
  1309. Assembler::Operand::Register(ARG1),
  1310. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier().value()))));
  1311. native_call((void*)cxx_delete_variable);
  1312. store_accumulator(RET);
  1313. check_exception();
  1314. }
  1315. static Value cxx_get_method(VM& vm, Value value, DeprecatedFlyString const& identifier)
  1316. {
  1317. auto method = TRY_OR_SET_EXCEPTION(value.get_method(vm, identifier));
  1318. return method ?: js_undefined();
  1319. }
  1320. void Compiler::compile_get_method(Bytecode::Op::GetMethod const& op)
  1321. {
  1322. load_accumulator(ARG1);
  1323. m_assembler.mov(
  1324. Assembler::Operand::Register(ARG2),
  1325. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1326. native_call((void*)cxx_get_method);
  1327. store_accumulator(RET);
  1328. check_exception();
  1329. }
  1330. static Value cxx_get_new_target(VM& vm)
  1331. {
  1332. return vm.get_new_target();
  1333. }
  1334. void Compiler::compile_get_new_target(Bytecode::Op::GetNewTarget const&)
  1335. {
  1336. native_call((void*)cxx_get_new_target);
  1337. store_accumulator(RET);
  1338. }
  1339. static Value cxx_has_private_id(VM& vm, Value object, DeprecatedFlyString const& identifier)
  1340. {
  1341. if (!object.is_object())
  1342. TRY_OR_SET_EXCEPTION(vm.throw_completion<TypeError>(ErrorType::InOperatorWithObject));
  1343. auto private_environment = vm.running_execution_context().private_environment;
  1344. VERIFY(private_environment);
  1345. auto private_name = private_environment->resolve_private_identifier(identifier);
  1346. return Value(object.as_object().private_element_find(private_name) != nullptr);
  1347. }
  1348. void Compiler::compile_has_private_id(Bytecode::Op::HasPrivateId const& op)
  1349. {
  1350. load_accumulator(ARG1);
  1351. m_assembler.mov(
  1352. Assembler::Operand::Register(ARG2),
  1353. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1354. native_call((void*)cxx_has_private_id);
  1355. store_accumulator(RET);
  1356. check_exception();
  1357. }
  1358. # define COMPILE_NEW_BUILTIN_ERROR_OP(NewErrorName, new_error_name, ErrorName) \
  1359. static Value cxx_##new_error_name(VM& vm, DeprecatedString const& error_string) \
  1360. { \
  1361. return ErrorName::create(*vm.current_realm(), error_string); \
  1362. } \
  1363. \
  1364. void Compiler::compile_##new_error_name(Bytecode::Op::NewErrorName const& op) \
  1365. { \
  1366. m_assembler.mov( \
  1367. Assembler::Operand::Register(ARG1), \
  1368. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_string(op.error_string())))); \
  1369. native_call((void*)cxx_##new_error_name); \
  1370. store_accumulator(RET); \
  1371. }
  1372. JS_ENUMERATE_NEW_BUILTIN_ERROR_BYTECODE_OPS(COMPILE_NEW_BUILTIN_ERROR_OP)
  1373. # undef COMPILE_NEW_BUILTIN_ERROR_OP
  1374. static Value cxx_put_by_value_with_this(VM& vm, Value base, Value value, Value name, Value this_value, Bytecode::Op::PropertyKind kind)
  1375. {
  1376. auto property_key = kind != Bytecode::Op::PropertyKind::Spread ? TRY_OR_SET_EXCEPTION(name.to_property_key(vm)) : PropertyKey {};
  1377. TRY_OR_SET_EXCEPTION(Bytecode::put_by_property_key(vm, base, this_value, value, property_key, kind));
  1378. return value;
  1379. }
  1380. void Compiler::compile_put_by_value_with_this(Bytecode::Op::PutByValueWithThis const& op)
  1381. {
  1382. load_vm_register(ARG1, op.base());
  1383. load_accumulator(ARG2);
  1384. if (op.kind() != Bytecode::Op::PropertyKind::Spread) {
  1385. load_vm_register(ARG3, op.property());
  1386. } else {
  1387. m_assembler.mov(
  1388. Assembler::Operand::Register(ARG3),
  1389. Assembler::Operand::Imm(Value().encoded()));
  1390. }
  1391. load_vm_register(ARG4, op.this_value());
  1392. m_assembler.mov(
  1393. Assembler::Operand::Register(ARG5),
  1394. Assembler::Operand::Imm(to_underlying(op.kind())));
  1395. native_call((void*)cxx_put_by_value_with_this);
  1396. store_accumulator(RET);
  1397. check_exception();
  1398. }
  1399. static Value cxx_copy_object_excluding_properties(VM& vm, Value from_object, u64 excluded_names_count, Value* excluded_names)
  1400. {
  1401. auto& realm = *vm.current_realm();
  1402. auto to_object = Object::create(realm, realm.intrinsics().object_prototype());
  1403. HashTable<PropertyKey> excluded_names_table;
  1404. for (size_t i = 0; i < excluded_names_count; ++i) {
  1405. excluded_names_table.set(TRY_OR_SET_EXCEPTION(excluded_names[i].to_property_key(vm)));
  1406. }
  1407. TRY_OR_SET_EXCEPTION(to_object->copy_data_properties(vm, from_object, excluded_names_table));
  1408. return to_object;
  1409. }
  1410. void Compiler::compile_copy_object_excluding_properties(Bytecode::Op::CopyObjectExcludingProperties const& op)
  1411. {
  1412. load_vm_register(ARG1, op.from_object());
  1413. m_assembler.mov(
  1414. Assembler::Operand::Register(ARG2),
  1415. Assembler::Operand::Imm(op.excluded_names_count()));
  1416. // Build `Value arg3[op.excluded_names_count()] {...}` on the stack.
  1417. auto stack_space = align_up_to(op.excluded_names_count() * sizeof(Value), 16);
  1418. m_assembler.sub(Assembler::Operand::Register(STACK_POINTER), Assembler::Operand::Imm(stack_space));
  1419. m_assembler.mov(Assembler::Operand::Register(ARG3), Assembler::Operand::Register(STACK_POINTER));
  1420. for (size_t i = 0; i < op.excluded_names_count(); ++i) {
  1421. load_vm_register(GPR0, op.excluded_names()[i]);
  1422. m_assembler.mov(Assembler::Operand::Mem64BaseAndOffset(ARG3, i * sizeof(Value)), Assembler::Operand::Register(GPR0));
  1423. }
  1424. native_call((void*)cxx_copy_object_excluding_properties);
  1425. // Restore the stack pointer / discard array.
  1426. m_assembler.add(Assembler::Operand::Register(STACK_POINTER), Assembler::Operand::Imm(stack_space));
  1427. store_accumulator(RET);
  1428. check_exception();
  1429. }
  1430. static Value cxx_async_iterator_close(VM& vm, Value iterator, Completion::Type completion_type, Optional<Value> const& completion_value)
  1431. {
  1432. auto iterator_object = TRY_OR_SET_EXCEPTION(iterator.to_object(vm));
  1433. auto iterator_record = Bytecode::object_to_iterator(vm, iterator_object);
  1434. // FIXME: Return the value of the resulting completion. (Note that completion_value can be empty!)
  1435. TRY_OR_SET_EXCEPTION(async_iterator_close(vm, iterator_record, Completion { completion_type, completion_value, {} }));
  1436. return {};
  1437. }
  1438. void Compiler::compile_async_iterator_close(Bytecode::Op::AsyncIteratorClose const& op)
  1439. {
  1440. load_accumulator(ARG1);
  1441. m_assembler.mov(
  1442. Assembler::Operand::Register(ARG2),
  1443. Assembler::Operand::Imm(to_underlying(op.completion_type())));
  1444. m_assembler.mov(
  1445. Assembler::Operand::Register(ARG3),
  1446. Assembler::Operand::Imm(bit_cast<u64>(&op.completion_value())));
  1447. native_call((void*)cxx_async_iterator_close);
  1448. check_exception();
  1449. }
  1450. void Compiler::jump_to_exit()
  1451. {
  1452. m_assembler.jump(m_exit_label);
  1453. }
  1454. void Compiler::native_call(void* function_address, Vector<Assembler::Operand> const& stack_arguments)
  1455. {
  1456. // Make sure we don't clobber the VM&.
  1457. m_assembler.push(Assembler::Operand::Register(ARG0));
  1458. // Align the stack pointer.
  1459. m_assembler.sub(Assembler::Operand::Register(STACK_POINTER), Assembler::Operand::Imm(8));
  1460. // NOTE: We don't preserve caller-saved registers when making a native call.
  1461. // This means that they may have changed after we return from the call.
  1462. m_assembler.native_call(function_address, stack_arguments);
  1463. // Restore the stack pointer.
  1464. m_assembler.add(Assembler::Operand::Register(STACK_POINTER), Assembler::Operand::Imm(8));
  1465. // Restore our VM&.
  1466. m_assembler.pop(Assembler::Operand::Register(ARG0));
  1467. }
  1468. OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_executable)
  1469. {
  1470. if (!getenv("LIBJS_JIT"))
  1471. return nullptr;
  1472. Compiler compiler { bytecode_executable };
  1473. Vector<BytecodeMapping> mapping;
  1474. mapping.append({
  1475. .native_offset = compiler.m_output.size(),
  1476. .block_index = BytecodeMapping::EXECUTABLE,
  1477. .bytecode_offset = 0,
  1478. });
  1479. compiler.m_assembler.enter();
  1480. compiler.m_assembler.mov(
  1481. Assembler::Operand::Register(REGISTER_ARRAY_BASE),
  1482. Assembler::Operand::Register(ARG1));
  1483. compiler.m_assembler.mov(
  1484. Assembler::Operand::Register(LOCALS_ARRAY_BASE),
  1485. Assembler::Operand::Register(ARG2));
  1486. compiler.reload_cached_accumulator();
  1487. Assembler::Label normal_entry {};
  1488. compiler.m_assembler.jump_if(
  1489. Assembler::Operand::Register(ARG3),
  1490. Assembler::Condition::EqualTo,
  1491. Assembler::Operand::Imm(0),
  1492. normal_entry);
  1493. compiler.m_assembler.jump(Assembler::Operand::Register(ARG3));
  1494. normal_entry.link(compiler.m_assembler);
  1495. for (size_t block_index = 0; block_index < bytecode_executable.basic_blocks.size(); block_index++) {
  1496. auto& block = bytecode_executable.basic_blocks[block_index];
  1497. compiler.block_data_for(*block).start_offset = compiler.m_output.size();
  1498. compiler.set_current_block(*block);
  1499. auto it = Bytecode::InstructionStreamIterator(block->instruction_stream());
  1500. if (it.at_end()) {
  1501. mapping.append({
  1502. .native_offset = compiler.m_output.size(),
  1503. .block_index = block_index,
  1504. .bytecode_offset = 0,
  1505. });
  1506. }
  1507. while (!it.at_end()) {
  1508. auto const& op = *it;
  1509. mapping.append({
  1510. .native_offset = compiler.m_output.size(),
  1511. .block_index = block_index,
  1512. .bytecode_offset = it.offset(),
  1513. });
  1514. switch (op.type()) {
  1515. # define CASE_BYTECODE_OP(OpTitleCase, op_snake_case, ...) \
  1516. case Bytecode::Instruction::Type::OpTitleCase: \
  1517. compiler.compile_##op_snake_case(static_cast<Bytecode::Op::OpTitleCase const&>(op)); \
  1518. break;
  1519. JS_ENUMERATE_IMPLEMENTED_JIT_OPS(CASE_BYTECODE_OP)
  1520. # undef CASE_BYTECODE_OP
  1521. default:
  1522. if constexpr (LOG_JIT_FAILURE) {
  1523. dbgln("\033[31;1mJIT compilation failed\033[0m: {}", bytecode_executable.name);
  1524. dbgln("Unsupported bytecode op: {}", op.to_deprecated_string(bytecode_executable));
  1525. }
  1526. return nullptr;
  1527. }
  1528. ++it;
  1529. }
  1530. if (!block->is_terminated())
  1531. compiler.jump_to_exit();
  1532. }
  1533. mapping.append({
  1534. .native_offset = compiler.m_output.size(),
  1535. .block_index = BytecodeMapping::EXECUTABLE,
  1536. .bytecode_offset = 1,
  1537. });
  1538. compiler.m_exit_label.link(compiler.m_assembler);
  1539. compiler.flush_cached_accumulator();
  1540. compiler.m_assembler.exit();
  1541. auto* executable_memory = mmap(nullptr, compiler.m_output.size(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  1542. if (executable_memory == MAP_FAILED) {
  1543. dbgln("mmap: {}", strerror(errno));
  1544. return nullptr;
  1545. }
  1546. for (auto& block : bytecode_executable.basic_blocks) {
  1547. auto& block_data = compiler.block_data_for(*block);
  1548. block_data.label.link_to(compiler.m_assembler, block_data.start_offset);
  1549. }
  1550. if constexpr (DUMP_JIT_MACHINE_CODE_TO_STDOUT) {
  1551. (void)write(STDOUT_FILENO, compiler.m_output.data(), compiler.m_output.size());
  1552. }
  1553. memcpy(executable_memory, compiler.m_output.data(), compiler.m_output.size());
  1554. if (mprotect(executable_memory, compiler.m_output.size(), PROT_READ | PROT_EXEC) < 0) {
  1555. dbgln("mprotect: {}", strerror(errno));
  1556. return nullptr;
  1557. }
  1558. if constexpr (LOG_JIT_SUCCESS) {
  1559. dbgln("\033[32;1mJIT compilation succeeded!\033[0m {}", bytecode_executable.name);
  1560. }
  1561. auto executable = make<NativeExecutable>(executable_memory, compiler.m_output.size(), mapping);
  1562. if constexpr (DUMP_JIT_DISASSEMBLY)
  1563. executable->dump_disassembly(bytecode_executable);
  1564. return executable;
  1565. }
  1566. }
  1567. #endif