Compiler.cpp 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128
  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. #ifdef JIT_ARCH_SUPPORTED
  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::saved_exception(), GPR0);
  358. store_vm_register(Bytecode::Register::exception(), GPR1);
  359. m_assembler.jump_if(Assembler::Operand::Register(GPR0),
  360. Assembler::Condition::NotEqualTo,
  361. Assembler::Operand::Register(GPR1),
  362. label_for(*finalizer));
  363. } else {
  364. m_assembler.jump_if(Assembler::Operand::Register(GPR0),
  365. Assembler::Condition::NotEqualTo,
  366. Assembler::Operand::Register(GPR1),
  367. m_exit_label);
  368. }
  369. }
  370. void Compiler::compile_enter_unwind_context(Bytecode::Op::EnterUnwindContext const& op)
  371. {
  372. m_assembler.jump(label_for(op.entry_point().block()));
  373. }
  374. void Compiler::compile_leave_unwind_context(Bytecode::Op::LeaveUnwindContext const&)
  375. {
  376. /* Nothing */
  377. }
  378. void Compiler::compile_throw(Bytecode::Op::Throw const&)
  379. {
  380. load_accumulator(GPR0);
  381. store_vm_register(Bytecode::Register::exception(), GPR0);
  382. check_exception();
  383. }
  384. static ThrowCompletionOr<Value> abstract_inequals(VM& vm, Value src1, Value src2)
  385. {
  386. return Value(!TRY(is_loosely_equal(vm, src1, src2)));
  387. }
  388. static ThrowCompletionOr<Value> abstract_equals(VM& vm, Value src1, Value src2)
  389. {
  390. return Value(TRY(is_loosely_equal(vm, src1, src2)));
  391. }
  392. static ThrowCompletionOr<Value> typed_inequals(VM&, Value src1, Value src2)
  393. {
  394. return Value(!is_strictly_equal(src1, src2));
  395. }
  396. static ThrowCompletionOr<Value> typed_equals(VM&, Value src1, Value src2)
  397. {
  398. return Value(is_strictly_equal(src1, src2));
  399. }
  400. # define DO_COMPILE_COMMON_BINARY_OP(TitleCaseName, snake_case_name) \
  401. static Value cxx_##snake_case_name(VM& vm, Value lhs, Value rhs) \
  402. { \
  403. return TRY_OR_SET_EXCEPTION(snake_case_name(vm, lhs, rhs)); \
  404. } \
  405. \
  406. void Compiler::compile_##snake_case_name(Bytecode::Op::TitleCaseName const& op) \
  407. { \
  408. load_vm_register(ARG1, op.lhs()); \
  409. load_accumulator(ARG2); \
  410. native_call((void*)cxx_##snake_case_name); \
  411. store_accumulator(RET); \
  412. check_exception(); \
  413. }
  414. JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(DO_COMPILE_COMMON_BINARY_OP)
  415. # undef DO_COMPILE_COMMON_BINARY_OP
  416. static Value cxx_add(VM& vm, Value lhs, Value rhs)
  417. {
  418. return TRY_OR_SET_EXCEPTION(add(vm, lhs, rhs));
  419. }
  420. void Compiler::compile_add(Bytecode::Op::Add const& op)
  421. {
  422. load_vm_register(ARG1, op.lhs());
  423. load_accumulator(ARG2);
  424. Assembler::Label end {};
  425. Assembler::Label slow_case {};
  426. branch_if_both_int32(ARG1, ARG2, [&] {
  427. // GPR0 = ARG1 + ARG2 (32-bit)
  428. // if (overflow) goto slow_case;
  429. m_assembler.mov(
  430. Assembler::Operand::Register(GPR0),
  431. Assembler::Operand::Register(ARG1));
  432. m_assembler.add32(
  433. Assembler::Operand::Register(GPR0),
  434. Assembler::Operand::Register(ARG2),
  435. slow_case);
  436. // accumulator = GPR0 | SHIFTED_INT32_TAG;
  437. m_assembler.mov(
  438. Assembler::Operand::Register(GPR1),
  439. Assembler::Operand::Imm(SHIFTED_INT32_TAG));
  440. m_assembler.bitwise_or(
  441. Assembler::Operand::Register(GPR0),
  442. Assembler::Operand::Register(GPR1));
  443. store_accumulator(GPR0);
  444. m_assembler.jump(end);
  445. });
  446. slow_case.link(m_assembler);
  447. native_call((void*)cxx_add);
  448. store_accumulator(RET);
  449. check_exception();
  450. end.link(m_assembler);
  451. }
  452. static Value cxx_sub(VM& vm, Value lhs, Value rhs)
  453. {
  454. return TRY_OR_SET_EXCEPTION(sub(vm, lhs, rhs));
  455. }
  456. void Compiler::compile_sub(Bytecode::Op::Sub const& op)
  457. {
  458. load_vm_register(ARG1, op.lhs());
  459. load_accumulator(ARG2);
  460. Assembler::Label end {};
  461. Assembler::Label slow_case {};
  462. branch_if_both_int32(ARG1, ARG2, [&] {
  463. // GPR0 = ARG1 + ARG2 (32-bit)
  464. // if (overflow) goto slow_case;
  465. m_assembler.mov(
  466. Assembler::Operand::Register(GPR0),
  467. Assembler::Operand::Register(ARG1));
  468. m_assembler.sub32(
  469. Assembler::Operand::Register(GPR0),
  470. Assembler::Operand::Register(ARG2),
  471. slow_case);
  472. // accumulator = GPR0 | SHIFTED_INT32_TAG;
  473. m_assembler.mov(
  474. Assembler::Operand::Register(GPR1),
  475. Assembler::Operand::Imm(SHIFTED_INT32_TAG));
  476. m_assembler.bitwise_or(
  477. Assembler::Operand::Register(GPR0),
  478. Assembler::Operand::Register(GPR1));
  479. store_accumulator(GPR0);
  480. m_assembler.jump(end);
  481. });
  482. slow_case.link(m_assembler);
  483. native_call((void*)cxx_sub);
  484. store_accumulator(RET);
  485. check_exception();
  486. end.link(m_assembler);
  487. }
  488. static Value cxx_mul(VM& vm, Value lhs, Value rhs)
  489. {
  490. return TRY_OR_SET_EXCEPTION(mul(vm, lhs, rhs));
  491. }
  492. void Compiler::compile_mul(Bytecode::Op::Mul const& op)
  493. {
  494. load_vm_register(ARG1, op.lhs());
  495. load_accumulator(ARG2);
  496. Assembler::Label end {};
  497. Assembler::Label slow_case {};
  498. branch_if_both_int32(ARG1, ARG2, [&] {
  499. // GPR0 = ARG1 * ARG2 (32-bit)
  500. // if (overflow) goto slow_case;
  501. m_assembler.mov(
  502. Assembler::Operand::Register(GPR0),
  503. Assembler::Operand::Register(ARG1));
  504. m_assembler.mul32(
  505. Assembler::Operand::Register(GPR0),
  506. Assembler::Operand::Register(ARG2),
  507. slow_case);
  508. // accumulator = GPR0 | SHIFTED_INT32_TAG;
  509. m_assembler.mov(
  510. Assembler::Operand::Register(GPR1),
  511. Assembler::Operand::Imm(SHIFTED_INT32_TAG));
  512. m_assembler.bitwise_or(
  513. Assembler::Operand::Register(GPR0),
  514. Assembler::Operand::Register(GPR1));
  515. store_accumulator(GPR0);
  516. m_assembler.jump(end);
  517. });
  518. slow_case.link(m_assembler);
  519. native_call((void*)cxx_mul);
  520. store_accumulator(RET);
  521. check_exception();
  522. end.link(m_assembler);
  523. }
  524. static Value cxx_less_than(VM& vm, Value lhs, Value rhs)
  525. {
  526. return TRY_OR_SET_EXCEPTION(less_than(vm, lhs, rhs));
  527. }
  528. void Compiler::compile_less_than(Bytecode::Op::LessThan const& op)
  529. {
  530. load_vm_register(ARG1, op.lhs());
  531. load_accumulator(ARG2);
  532. Assembler::Label end {};
  533. branch_if_both_int32(ARG1, ARG2, [&] {
  534. // if (ARG1 < ARG2) return true;
  535. // else return false;
  536. Assembler::Label true_case {};
  537. m_assembler.sign_extend_32_to_64_bits(ARG1);
  538. m_assembler.sign_extend_32_to_64_bits(ARG2);
  539. m_assembler.jump_if(
  540. Assembler::Operand::Register(ARG1),
  541. Assembler::Condition::SignedLessThan,
  542. Assembler::Operand::Register(ARG2),
  543. true_case);
  544. m_assembler.mov(
  545. Assembler::Operand::Register(GPR0),
  546. Assembler::Operand::Imm(Value(false).encoded()));
  547. store_accumulator(GPR0);
  548. m_assembler.jump(end);
  549. true_case.link(m_assembler);
  550. m_assembler.mov(
  551. Assembler::Operand::Register(GPR0),
  552. Assembler::Operand::Imm(Value(true).encoded()));
  553. store_accumulator(GPR0);
  554. m_assembler.jump(end);
  555. });
  556. native_call((void*)cxx_less_than);
  557. store_accumulator(RET);
  558. check_exception();
  559. end.link(m_assembler);
  560. }
  561. static Value cxx_bitwise_and(VM& vm, Value lhs, Value rhs)
  562. {
  563. return TRY_OR_SET_EXCEPTION(bitwise_and(vm, lhs, rhs));
  564. }
  565. void Compiler::compile_bitwise_and(Bytecode::Op::BitwiseAnd const& op)
  566. {
  567. load_vm_register(ARG1, op.lhs());
  568. load_accumulator(ARG2);
  569. Assembler::Label end {};
  570. branch_if_both_int32(ARG1, ARG2, [&] {
  571. // NOTE: Since both sides are Int32, we know that the upper 32 bits are nothing but the INT32_TAG.
  572. // This means we can get away with just a simple 64-bit bitwise and.
  573. m_assembler.bitwise_and(
  574. Assembler::Operand::Register(ARG1),
  575. Assembler::Operand::Register(ARG2));
  576. store_accumulator(ARG1);
  577. m_assembler.jump(end);
  578. });
  579. native_call((void*)cxx_bitwise_and);
  580. store_accumulator(RET);
  581. check_exception();
  582. end.link(m_assembler);
  583. }
  584. static Value cxx_bitwise_or(VM& vm, Value lhs, Value rhs)
  585. {
  586. return TRY_OR_SET_EXCEPTION(bitwise_or(vm, lhs, rhs));
  587. }
  588. void Compiler::compile_bitwise_or(Bytecode::Op::BitwiseOr const& op)
  589. {
  590. load_vm_register(ARG1, op.lhs());
  591. load_accumulator(ARG2);
  592. Assembler::Label end {};
  593. branch_if_both_int32(ARG1, ARG2, [&] {
  594. // NOTE: Since both sides are Int32, we know that the upper 32 bits are nothing but the INT32_TAG.
  595. // This means we can get away with just a simple 64-bit bitwise or.
  596. m_assembler.bitwise_or(
  597. Assembler::Operand::Register(ARG1),
  598. Assembler::Operand::Register(ARG2));
  599. store_accumulator(ARG1);
  600. m_assembler.jump(end);
  601. });
  602. native_call((void*)cxx_bitwise_or);
  603. store_accumulator(RET);
  604. check_exception();
  605. end.link(m_assembler);
  606. }
  607. static Value cxx_bitwise_xor(VM& vm, Value lhs, Value rhs)
  608. {
  609. return TRY_OR_SET_EXCEPTION(bitwise_xor(vm, lhs, rhs));
  610. }
  611. void Compiler::compile_bitwise_xor(Bytecode::Op::BitwiseXor const& op)
  612. {
  613. load_vm_register(ARG1, op.lhs());
  614. load_accumulator(ARG2);
  615. Assembler::Label end {};
  616. branch_if_both_int32(ARG1, ARG2, [&] {
  617. // ARG1 ^= ARG2 (32-bit)
  618. m_assembler.bitwise_xor32(
  619. Assembler::Operand::Register(ARG1),
  620. Assembler::Operand::Register(ARG2));
  621. // accumulator = ARG1 | SHIFTED_INT32_TAG;
  622. m_assembler.mov(
  623. Assembler::Operand::Register(GPR0),
  624. Assembler::Operand::Imm(SHIFTED_INT32_TAG));
  625. m_assembler.bitwise_or(
  626. Assembler::Operand::Register(ARG1),
  627. Assembler::Operand::Register(GPR0));
  628. store_accumulator(ARG1);
  629. m_assembler.jump(end);
  630. });
  631. native_call((void*)cxx_bitwise_xor);
  632. store_accumulator(RET);
  633. check_exception();
  634. end.link(m_assembler);
  635. }
  636. static Value cxx_left_shift(VM& vm, Value lhs, Value rhs)
  637. {
  638. return TRY_OR_SET_EXCEPTION(left_shift(vm, lhs, rhs));
  639. }
  640. void Compiler::compile_left_shift(Bytecode::Op::LeftShift const& op)
  641. {
  642. load_vm_register(ARG1, op.lhs());
  643. load_accumulator(ARG2);
  644. Assembler::Label end {};
  645. branch_if_both_int32(ARG1, ARG2, [&] {
  646. // RCX = ARG2
  647. m_assembler.mov(
  648. Assembler::Operand::Register(Assembler::Reg::RCX),
  649. Assembler::Operand::Register(ARG2));
  650. // ARG1 <<= CL (32-bit)
  651. m_assembler.shift_left32(Assembler::Operand::Register(ARG1), {});
  652. // accumulator = ARG1 | SHIFTED_INT32_TAG;
  653. m_assembler.mov(
  654. Assembler::Operand::Register(GPR0),
  655. Assembler::Operand::Imm(SHIFTED_INT32_TAG));
  656. m_assembler.bitwise_or(
  657. Assembler::Operand::Register(ARG1),
  658. Assembler::Operand::Register(GPR0));
  659. store_accumulator(ARG1);
  660. m_assembler.jump(end);
  661. });
  662. native_call((void*)cxx_left_shift);
  663. store_accumulator(RET);
  664. check_exception();
  665. end.link(m_assembler);
  666. }
  667. static Value cxx_right_shift(VM& vm, Value lhs, Value rhs)
  668. {
  669. return TRY_OR_SET_EXCEPTION(right_shift(vm, lhs, rhs));
  670. }
  671. void Compiler::compile_right_shift(Bytecode::Op::RightShift const& op)
  672. {
  673. load_vm_register(ARG1, op.lhs());
  674. load_accumulator(ARG2);
  675. Assembler::Label end {};
  676. branch_if_both_int32(ARG1, ARG2, [&] {
  677. // RCX = ARG2
  678. m_assembler.mov(
  679. Assembler::Operand::Register(Assembler::Reg::RCX),
  680. Assembler::Operand::Register(ARG2));
  681. // ARG1 >>= CL (32-bit)
  682. m_assembler.arithmetic_right_shift32(Assembler::Operand::Register(ARG1), {});
  683. // accumulator = ARG1 | SHIFTED_INT32_TAG;
  684. m_assembler.mov(
  685. Assembler::Operand::Register(GPR0),
  686. Assembler::Operand::Imm(SHIFTED_INT32_TAG));
  687. m_assembler.bitwise_or(
  688. Assembler::Operand::Register(ARG1),
  689. Assembler::Operand::Register(GPR0));
  690. store_accumulator(ARG1);
  691. m_assembler.jump(end);
  692. });
  693. native_call((void*)cxx_right_shift);
  694. store_accumulator(RET);
  695. check_exception();
  696. end.link(m_assembler);
  697. }
  698. static ThrowCompletionOr<Value> not_(VM&, Value value)
  699. {
  700. return Value(!value.to_boolean());
  701. }
  702. static ThrowCompletionOr<Value> typeof_(VM& vm, Value value)
  703. {
  704. return PrimitiveString::create(vm, value.typeof());
  705. }
  706. # define DO_COMPILE_COMMON_UNARY_OP(TitleCaseName, snake_case_name) \
  707. static Value cxx_##snake_case_name(VM& vm, Value value) \
  708. { \
  709. return TRY_OR_SET_EXCEPTION(snake_case_name(vm, value)); \
  710. } \
  711. \
  712. void Compiler::compile_##snake_case_name(Bytecode::Op::TitleCaseName const&) \
  713. { \
  714. load_accumulator(ARG1); \
  715. native_call((void*)cxx_##snake_case_name); \
  716. store_accumulator(RET); \
  717. check_exception(); \
  718. }
  719. JS_ENUMERATE_COMMON_UNARY_OPS(DO_COMPILE_COMMON_UNARY_OP)
  720. # undef DO_COMPILE_COMMON_UNARY_OP
  721. void Compiler::compile_return(Bytecode::Op::Return const&)
  722. {
  723. load_accumulator(GPR0);
  724. if (auto const* finalizer = current_block().finalizer(); finalizer) {
  725. store_vm_register(Bytecode::Register::saved_return_value(), GPR0);
  726. m_assembler.jump(label_for(*finalizer));
  727. } else {
  728. store_vm_register(Bytecode::Register::return_value(), GPR0);
  729. jump_to_exit();
  730. }
  731. }
  732. static Value cxx_new_string(VM& vm, DeprecatedString const& string)
  733. {
  734. return PrimitiveString::create(vm, string);
  735. }
  736. void Compiler::compile_new_string(Bytecode::Op::NewString const& op)
  737. {
  738. auto const& string = m_bytecode_executable.string_table->get(op.index());
  739. m_assembler.mov(
  740. Assembler::Operand::Register(ARG1),
  741. Assembler::Operand::Imm(bit_cast<u64>(&string)));
  742. native_call((void*)cxx_new_string);
  743. store_accumulator(RET);
  744. }
  745. void Compiler::compile_new_regexp(Bytecode::Op::NewRegExp const& op)
  746. {
  747. auto const& parsed_regex = m_bytecode_executable.regex_table->get(op.regex_index());
  748. auto const& pattern = m_bytecode_executable.string_table->get(op.source_index());
  749. auto const& flags = m_bytecode_executable.string_table->get(op.flags_index());
  750. m_assembler.mov(
  751. Assembler::Operand::Register(ARG1),
  752. Assembler::Operand::Imm(bit_cast<u64>(&parsed_regex)));
  753. m_assembler.mov(
  754. Assembler::Operand::Register(ARG2),
  755. Assembler::Operand::Imm(bit_cast<u64>(&pattern)));
  756. m_assembler.mov(
  757. Assembler::Operand::Register(ARG3),
  758. Assembler::Operand::Imm(bit_cast<u64>(&flags)));
  759. native_call((void*)Bytecode::new_regexp);
  760. store_accumulator(RET);
  761. }
  762. static Value cxx_new_bigint(VM& vm, Crypto::SignedBigInteger const& bigint)
  763. {
  764. return BigInt::create(vm, bigint);
  765. }
  766. void Compiler::compile_new_bigint(Bytecode::Op::NewBigInt const& op)
  767. {
  768. m_assembler.mov(
  769. Assembler::Operand::Register(ARG1),
  770. Assembler::Operand::Imm(bit_cast<u64>(&op.bigint())));
  771. native_call((void*)cxx_new_bigint);
  772. store_accumulator(RET);
  773. }
  774. static Value cxx_new_object(VM& vm)
  775. {
  776. auto& realm = *vm.current_realm();
  777. return Object::create(realm, realm.intrinsics().object_prototype());
  778. }
  779. void Compiler::compile_new_object(Bytecode::Op::NewObject const&)
  780. {
  781. native_call((void*)cxx_new_object);
  782. store_accumulator(RET);
  783. }
  784. static Value cxx_new_array(VM& vm, size_t element_count, u32 first_register_index)
  785. {
  786. auto& realm = *vm.current_realm();
  787. auto array = MUST(Array::create(realm, 0));
  788. for (size_t i = 0; i < element_count; ++i) {
  789. auto& value = vm.bytecode_interpreter().reg(Bytecode::Register(first_register_index + i));
  790. array->indexed_properties().put(i, value, default_attributes);
  791. }
  792. return array;
  793. }
  794. void Compiler::compile_new_array(Bytecode::Op::NewArray const& op)
  795. {
  796. m_assembler.mov(
  797. Assembler::Operand::Register(ARG1),
  798. Assembler::Operand::Imm(op.element_count()));
  799. m_assembler.mov(
  800. Assembler::Operand::Register(ARG2),
  801. Assembler::Operand::Imm(op.element_count() ? op.start().index() : 0));
  802. native_call((void*)cxx_new_array);
  803. store_accumulator(RET);
  804. }
  805. void Compiler::compile_new_function(Bytecode::Op::NewFunction const& op)
  806. {
  807. m_assembler.mov(
  808. Assembler::Operand::Register(ARG1),
  809. Assembler::Operand::Imm(bit_cast<u64>(&op.function_node())));
  810. m_assembler.mov(
  811. Assembler::Operand::Register(ARG2),
  812. Assembler::Operand::Imm(bit_cast<u64>(&op.lhs_name())));
  813. m_assembler.mov(
  814. Assembler::Operand::Register(ARG3),
  815. Assembler::Operand::Imm(bit_cast<u64>(&op.home_object())));
  816. native_call((void*)Bytecode::new_function);
  817. store_accumulator(RET);
  818. }
  819. static Value cxx_new_class(VM& vm, Value super_class, ClassExpression const& class_expression, Optional<Bytecode::IdentifierTableIndex> const& lhs_name)
  820. {
  821. return TRY_OR_SET_EXCEPTION(Bytecode::new_class(vm, super_class, class_expression, lhs_name));
  822. }
  823. void Compiler::compile_new_class(Bytecode::Op::NewClass const& op)
  824. {
  825. load_accumulator(ARG1);
  826. m_assembler.mov(
  827. Assembler::Operand::Register(ARG2),
  828. Assembler::Operand::Imm(bit_cast<u64>(&op.class_expression())));
  829. m_assembler.mov(
  830. Assembler::Operand::Register(ARG3),
  831. Assembler::Operand::Imm(bit_cast<u64>(&op.lhs_name())));
  832. native_call((void*)cxx_new_class);
  833. store_accumulator(RET);
  834. }
  835. static Value cxx_get_by_id(VM& vm, Value base, DeprecatedFlyString const& property, Bytecode::PropertyLookupCache& cache)
  836. {
  837. return TRY_OR_SET_EXCEPTION(Bytecode::get_by_id(vm, property, base, base, cache));
  838. }
  839. void Compiler::compile_get_by_id(Bytecode::Op::GetById const& op)
  840. {
  841. load_accumulator(ARG1);
  842. m_assembler.mov(
  843. Assembler::Operand::Register(ARG2),
  844. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  845. m_assembler.mov(
  846. Assembler::Operand::Register(ARG3),
  847. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.property_lookup_caches[op.cache_index()])));
  848. native_call((void*)cxx_get_by_id);
  849. store_accumulator(RET);
  850. check_exception();
  851. }
  852. static Value cxx_get_by_value(VM& vm, Value base, Value property)
  853. {
  854. return TRY_OR_SET_EXCEPTION(Bytecode::get_by_value(vm, base, property));
  855. }
  856. void Compiler::compile_get_by_value(Bytecode::Op::GetByValue const& op)
  857. {
  858. load_vm_register(ARG1, op.base());
  859. load_accumulator(ARG2);
  860. native_call((void*)cxx_get_by_value);
  861. store_accumulator(RET);
  862. check_exception();
  863. }
  864. static Value cxx_get_global(VM& vm, DeprecatedFlyString const& identifier, Bytecode::GlobalVariableCache& cache)
  865. {
  866. return TRY_OR_SET_EXCEPTION(Bytecode::get_global(vm.bytecode_interpreter(), identifier, cache));
  867. }
  868. void Compiler::compile_get_global(Bytecode::Op::GetGlobal const& op)
  869. {
  870. m_assembler.mov(
  871. Assembler::Operand::Register(ARG1),
  872. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier()))));
  873. m_assembler.mov(
  874. Assembler::Operand::Register(ARG2),
  875. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.global_variable_caches[op.cache_index()])));
  876. native_call((void*)cxx_get_global);
  877. store_accumulator(RET);
  878. check_exception();
  879. }
  880. static Value cxx_get_variable(VM& vm, DeprecatedFlyString const& name, Bytecode::EnvironmentVariableCache& cache)
  881. {
  882. return TRY_OR_SET_EXCEPTION(Bytecode::get_variable(vm.bytecode_interpreter(), name, cache));
  883. }
  884. void Compiler::compile_get_variable(Bytecode::Op::GetVariable const& op)
  885. {
  886. m_assembler.mov(
  887. Assembler::Operand::Register(ARG1),
  888. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier()))));
  889. m_assembler.mov(
  890. Assembler::Operand::Register(ARG2),
  891. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.environment_variable_caches[op.cache_index()])));
  892. native_call((void*)cxx_get_variable);
  893. store_accumulator(RET);
  894. check_exception();
  895. }
  896. 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)
  897. {
  898. auto& bytecode_interpreter = vm.bytecode_interpreter();
  899. auto callee_and_this = TRY_OR_SET_EXCEPTION(Bytecode::get_callee_and_this_from_environment(
  900. bytecode_interpreter,
  901. name,
  902. cache_index));
  903. bytecode_interpreter.reg(callee_reg) = callee_and_this.callee;
  904. bytecode_interpreter.reg(this_reg) = callee_and_this.this_value;
  905. return {};
  906. }
  907. void Compiler::compile_get_callee_and_this_from_environment(Bytecode::Op::GetCalleeAndThisFromEnvironment const& op)
  908. {
  909. m_assembler.mov(
  910. Assembler::Operand::Register(ARG1),
  911. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier()))));
  912. m_assembler.mov(
  913. Assembler::Operand::Register(ARG2),
  914. Assembler::Operand::Imm(op.cache_index()));
  915. m_assembler.mov(
  916. Assembler::Operand::Register(ARG3),
  917. Assembler::Operand::Imm(op.callee().index()));
  918. m_assembler.mov(
  919. Assembler::Operand::Register(ARG4),
  920. Assembler::Operand::Imm(op.this_().index()));
  921. native_call((void*)cxx_get_callee_and_this_from_environment);
  922. check_exception();
  923. }
  924. static Value cxx_to_numeric(VM& vm, Value value)
  925. {
  926. return TRY_OR_SET_EXCEPTION(value.to_numeric(vm));
  927. }
  928. void Compiler::compile_to_numeric(Bytecode::Op::ToNumeric const&)
  929. {
  930. Assembler::Label fast_case {};
  931. load_accumulator(ARG1);
  932. jump_if_int32(ARG1, fast_case);
  933. native_call((void*)cxx_to_numeric);
  934. store_accumulator(RET);
  935. check_exception();
  936. fast_case.link(m_assembler);
  937. }
  938. static Value cxx_resolve_this_binding(VM& vm)
  939. {
  940. auto this_value = TRY_OR_SET_EXCEPTION(vm.resolve_this_binding());
  941. vm.bytecode_interpreter().reg(Bytecode::Register::this_value()) = this_value;
  942. return this_value;
  943. }
  944. void Compiler::compile_resolve_this_binding(Bytecode::Op::ResolveThisBinding const&)
  945. {
  946. // OPTIMIZATION: We cache the `this` value in a special VM register.
  947. // So first we check if the cache is non-empty, and if so,
  948. // we can avoid calling out to C++ at all. :^)
  949. load_vm_register(GPR0, Bytecode::Register::this_value());
  950. m_assembler.mov(
  951. Assembler::Operand::Register(GPR1),
  952. Assembler::Operand::Imm(Value().encoded()));
  953. Assembler::Label slow_case {};
  954. m_assembler.jump_if(
  955. Assembler::Operand::Register(GPR0),
  956. Assembler::Condition::EqualTo,
  957. Assembler::Operand::Register(GPR1),
  958. slow_case);
  959. // Fast case: We have a cached `this` value!
  960. store_accumulator(GPR0);
  961. auto end = m_assembler.jump();
  962. slow_case.link(m_assembler);
  963. native_call((void*)cxx_resolve_this_binding);
  964. store_accumulator(RET);
  965. check_exception();
  966. end.link(m_assembler);
  967. }
  968. static Value cxx_put_by_id(VM& vm, Value base, Bytecode::IdentifierTableIndex property, Value value, Bytecode::Op::PropertyKind kind)
  969. {
  970. PropertyKey name = vm.bytecode_interpreter().current_executable().get_identifier(property);
  971. TRY_OR_SET_EXCEPTION(Bytecode::put_by_property_key(vm, base, base, value, name, kind));
  972. return value;
  973. }
  974. void Compiler::compile_put_by_id(Bytecode::Op::PutById const& op)
  975. {
  976. load_vm_register(ARG1, op.base());
  977. m_assembler.mov(
  978. Assembler::Operand::Register(ARG2),
  979. Assembler::Operand::Imm(op.property().value()));
  980. load_accumulator(ARG3);
  981. m_assembler.mov(
  982. Assembler::Operand::Register(ARG4),
  983. Assembler::Operand::Imm(to_underlying(op.kind())));
  984. native_call((void*)cxx_put_by_id);
  985. store_accumulator(RET);
  986. check_exception();
  987. }
  988. static Value cxx_put_by_value(VM& vm, Value base, Value property, Value value, Bytecode::Op::PropertyKind kind)
  989. {
  990. TRY_OR_SET_EXCEPTION(Bytecode::put_by_value(vm, base, property, value, kind));
  991. return value;
  992. }
  993. void Compiler::compile_put_by_value(Bytecode::Op::PutByValue const& op)
  994. {
  995. load_vm_register(ARG1, op.base());
  996. load_vm_register(ARG2, op.property());
  997. load_accumulator(ARG3);
  998. m_assembler.mov(
  999. Assembler::Operand::Register(ARG4),
  1000. Assembler::Operand::Imm(to_underlying(op.kind())));
  1001. native_call((void*)cxx_put_by_value);
  1002. store_accumulator(RET);
  1003. check_exception();
  1004. }
  1005. 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)
  1006. {
  1007. TRY_OR_SET_EXCEPTION(throw_if_needed_for_call(vm.bytecode_interpreter(), callee, call_type, expression_string));
  1008. MarkedVector<Value> argument_values(vm.heap());
  1009. argument_values.ensure_capacity(argument_count);
  1010. for (u32 i = 0; i < argument_count; ++i) {
  1011. argument_values.unchecked_append(vm.bytecode_interpreter().reg(Bytecode::Register { first_argument_index + i }));
  1012. }
  1013. return TRY_OR_SET_EXCEPTION(perform_call(vm.bytecode_interpreter(), this_value, call_type, callee, move(argument_values)));
  1014. }
  1015. void Compiler::compile_call(Bytecode::Op::Call const& op)
  1016. {
  1017. load_vm_register(ARG1, op.callee());
  1018. m_assembler.mov(
  1019. Assembler::Operand::Register(ARG2),
  1020. Assembler::Operand::Imm(op.first_argument().index()));
  1021. m_assembler.mov(
  1022. Assembler::Operand::Register(ARG3),
  1023. Assembler::Operand::Imm(op.argument_count()));
  1024. load_vm_register(ARG4, op.this_value());
  1025. m_assembler.mov(
  1026. Assembler::Operand::Register(ARG5),
  1027. Assembler::Operand::Imm(to_underlying(op.call_type())));
  1028. m_assembler.mov(
  1029. Assembler::Operand::Register(GPR0),
  1030. Assembler::Operand::Imm(bit_cast<u64>(&op.expression_string())));
  1031. native_call((void*)cxx_call, { Assembler::Operand::Register(GPR0) });
  1032. store_accumulator(RET);
  1033. check_exception();
  1034. }
  1035. 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)
  1036. {
  1037. TRY_OR_SET_EXCEPTION(throw_if_needed_for_call(vm.bytecode_interpreter(), callee, call_type, expression_string));
  1038. auto argument_values = Bytecode::argument_list_evaluation(vm, arguments);
  1039. return TRY_OR_SET_EXCEPTION(perform_call(vm.bytecode_interpreter(), this_value, call_type, callee, move(argument_values)));
  1040. }
  1041. void Compiler::compile_call_with_argument_array(Bytecode::Op::CallWithArgumentArray const& op)
  1042. {
  1043. load_accumulator(ARG1);
  1044. load_vm_register(ARG2, op.callee());
  1045. load_vm_register(ARG3, op.this_value());
  1046. m_assembler.mov(
  1047. Assembler::Operand::Register(ARG4),
  1048. Assembler::Operand::Imm(to_underlying(op.call_type())));
  1049. m_assembler.mov(
  1050. Assembler::Operand::Register(ARG5),
  1051. Assembler::Operand::Imm(bit_cast<u64>(&op.expression_string())));
  1052. native_call((void*)cxx_call_with_argument_array);
  1053. store_accumulator(RET);
  1054. check_exception();
  1055. }
  1056. static Value cxx_typeof_variable(VM& vm, DeprecatedFlyString const& identifier)
  1057. {
  1058. return TRY_OR_SET_EXCEPTION(Bytecode::typeof_variable(vm, identifier));
  1059. }
  1060. void Compiler::compile_typeof_variable(Bytecode::Op::TypeofVariable const& op)
  1061. {
  1062. m_assembler.mov(
  1063. Assembler::Operand::Register(ARG1),
  1064. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier().value()))));
  1065. native_call((void*)cxx_typeof_variable);
  1066. store_accumulator(RET);
  1067. check_exception();
  1068. }
  1069. static Value cxx_create_variable(
  1070. VM& vm,
  1071. DeprecatedFlyString const& name,
  1072. Bytecode::Op::EnvironmentMode mode,
  1073. bool is_global,
  1074. bool is_immutable,
  1075. bool is_strict)
  1076. {
  1077. TRY_OR_SET_EXCEPTION(Bytecode::create_variable(vm, name, mode, is_global, is_immutable, is_strict));
  1078. return {};
  1079. }
  1080. void Compiler::compile_create_variable(Bytecode::Op::CreateVariable const& op)
  1081. {
  1082. m_assembler.mov(
  1083. Assembler::Operand::Register(ARG1),
  1084. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier().value()))));
  1085. m_assembler.mov(
  1086. Assembler::Operand::Register(ARG2),
  1087. Assembler::Operand::Imm(to_underlying(op.mode())));
  1088. m_assembler.mov(
  1089. Assembler::Operand::Register(ARG3),
  1090. Assembler::Operand::Imm(static_cast<u64>(op.is_global())));
  1091. m_assembler.mov(
  1092. Assembler::Operand::Register(ARG4),
  1093. Assembler::Operand::Imm(static_cast<u64>(op.is_immutable())));
  1094. m_assembler.mov(
  1095. Assembler::Operand::Register(ARG5),
  1096. Assembler::Operand::Imm(static_cast<u64>(op.is_strict())));
  1097. native_call((void*)cxx_create_variable);
  1098. check_exception();
  1099. }
  1100. static Value cxx_set_variable(
  1101. VM& vm,
  1102. DeprecatedFlyString const& identifier,
  1103. Value value,
  1104. Bytecode::Op::EnvironmentMode environment_mode,
  1105. Bytecode::Op::SetVariable::InitializationMode initialization_mode)
  1106. {
  1107. TRY_OR_SET_EXCEPTION(Bytecode::set_variable(vm, identifier, value, environment_mode, initialization_mode));
  1108. return {};
  1109. }
  1110. void Compiler::compile_set_variable(Bytecode::Op::SetVariable const& op)
  1111. {
  1112. m_assembler.mov(
  1113. Assembler::Operand::Register(ARG1),
  1114. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier().value()))));
  1115. load_accumulator(ARG2);
  1116. m_assembler.mov(
  1117. Assembler::Operand::Register(ARG3),
  1118. Assembler::Operand::Imm(to_underlying(op.mode())));
  1119. m_assembler.mov(
  1120. Assembler::Operand::Register(ARG4),
  1121. Assembler::Operand::Imm(to_underlying(op.initialization_mode())));
  1122. native_call((void*)cxx_set_variable);
  1123. check_exception();
  1124. }
  1125. void Compiler::compile_continue_pending_unwind(Bytecode::Op::ContinuePendingUnwind const& op)
  1126. {
  1127. // re-throw the exception if we reached the end of the finally block and there was no catch block to handle it
  1128. load_vm_register(GPR0, Bytecode::Register::saved_exception());
  1129. store_vm_register(Bytecode::Register::exception(), GPR0);
  1130. m_assembler.mov(Assembler::Operand::Register(GPR1), Assembler::Operand::Imm(Value().encoded()));
  1131. store_vm_register(Bytecode::Register::saved_exception(), GPR1);
  1132. check_exception();
  1133. // if (saved_return_value.is_empty()) goto resume_block;
  1134. load_vm_register(GPR0, Bytecode::Register::saved_return_value());
  1135. m_assembler.jump_if(
  1136. Assembler::Operand::Register(GPR0),
  1137. Assembler::Condition::EqualTo,
  1138. Assembler::Operand::Register(GPR1),
  1139. label_for(op.resume_target().block()));
  1140. // finish the pending return from the try block
  1141. store_vm_register(Bytecode::Register::return_value(), GPR0);
  1142. jump_to_exit();
  1143. }
  1144. static void cxx_create_lexical_environment(VM& vm)
  1145. {
  1146. auto make_and_swap_envs = [&](auto& old_environment) {
  1147. GCPtr<Environment> environment = new_declarative_environment(*old_environment).ptr();
  1148. swap(old_environment, environment);
  1149. return environment;
  1150. };
  1151. vm.bytecode_interpreter().saved_lexical_environment_stack().append(make_and_swap_envs(vm.running_execution_context().lexical_environment));
  1152. }
  1153. void Compiler::compile_create_lexical_environment(Bytecode::Op::CreateLexicalEnvironment const&)
  1154. {
  1155. native_call((void*)cxx_create_lexical_environment);
  1156. }
  1157. static void cxx_leave_lexical_environment(VM& vm)
  1158. {
  1159. vm.running_execution_context().lexical_environment = vm.bytecode_interpreter().saved_lexical_environment_stack().take_last();
  1160. }
  1161. void Compiler::compile_leave_lexical_environment(Bytecode::Op::LeaveLexicalEnvironment const&)
  1162. {
  1163. native_call((void*)cxx_leave_lexical_environment);
  1164. }
  1165. static Value cxx_concat_string(VM& vm, Value lhs, Value rhs)
  1166. {
  1167. auto string = TRY_OR_SET_EXCEPTION(rhs.to_primitive_string(vm));
  1168. return PrimitiveString::create(vm, lhs.as_string(), string);
  1169. }
  1170. void Compiler::compile_concat_string(Bytecode::Op::ConcatString const& op)
  1171. {
  1172. load_vm_register(ARG1, op.lhs());
  1173. load_accumulator(ARG2);
  1174. native_call((void*)cxx_concat_string);
  1175. store_vm_register(op.lhs(), RET);
  1176. check_exception();
  1177. }
  1178. static void cxx_block_declaration_instantiation(VM& vm, ScopeNode const& scope_node)
  1179. {
  1180. auto old_environment = vm.running_execution_context().lexical_environment;
  1181. vm.bytecode_interpreter().saved_lexical_environment_stack().append(old_environment);
  1182. vm.running_execution_context().lexical_environment = new_declarative_environment(*old_environment);
  1183. scope_node.block_declaration_instantiation(vm, vm.running_execution_context().lexical_environment);
  1184. }
  1185. void Compiler::compile_block_declaration_instantiation(Bytecode::Op::BlockDeclarationInstantiation const& op)
  1186. {
  1187. m_assembler.mov(
  1188. Assembler::Operand::Register(ARG1),
  1189. Assembler::Operand::Imm(bit_cast<u64>(&op.scope_node())));
  1190. native_call((void*)cxx_block_declaration_instantiation);
  1191. }
  1192. static Value cxx_super_call_with_argument_array(VM& vm, Value argument_array, bool is_synthetic)
  1193. {
  1194. return TRY_OR_SET_EXCEPTION(Bytecode::super_call_with_argument_array(vm, argument_array, is_synthetic));
  1195. }
  1196. void Compiler::compile_super_call_with_argument_array(Bytecode::Op::SuperCallWithArgumentArray const& op)
  1197. {
  1198. load_accumulator(ARG1);
  1199. m_assembler.mov(
  1200. Assembler::Operand::Register(ARG2),
  1201. Assembler::Operand::Imm(static_cast<u64>(op.is_synthetic())));
  1202. native_call((void*)cxx_super_call_with_argument_array);
  1203. store_accumulator(RET);
  1204. check_exception();
  1205. }
  1206. static Value cxx_get_iterator(VM& vm, Value value, IteratorHint hint)
  1207. {
  1208. auto iterator = TRY_OR_SET_EXCEPTION(get_iterator(vm, value, hint));
  1209. return Bytecode::iterator_to_object(vm, iterator);
  1210. }
  1211. void Compiler::compile_get_iterator(Bytecode::Op::GetIterator const& op)
  1212. {
  1213. load_accumulator(ARG1);
  1214. m_assembler.mov(
  1215. Assembler::Operand::Register(ARG2),
  1216. Assembler::Operand::Imm(to_underlying(op.hint())));
  1217. native_call((void*)cxx_get_iterator);
  1218. store_accumulator(RET);
  1219. check_exception();
  1220. }
  1221. static Value cxx_iterator_next(VM& vm, Value iterator)
  1222. {
  1223. auto iterator_object = TRY_OR_SET_EXCEPTION(iterator.to_object(vm));
  1224. auto iterator_record = Bytecode::object_to_iterator(vm, iterator_object);
  1225. return TRY_OR_SET_EXCEPTION(iterator_next(vm, iterator_record));
  1226. }
  1227. void Compiler::compile_iterator_next(Bytecode::Op::IteratorNext const&)
  1228. {
  1229. load_accumulator(ARG1);
  1230. native_call((void*)cxx_iterator_next);
  1231. store_accumulator(RET);
  1232. check_exception();
  1233. }
  1234. static Value cxx_iterator_result_done(VM& vm, Value iterator)
  1235. {
  1236. auto iterator_result = TRY_OR_SET_EXCEPTION(iterator.to_object(vm));
  1237. return Value(TRY_OR_SET_EXCEPTION(iterator_complete(vm, iterator_result)));
  1238. }
  1239. void Compiler::compile_iterator_result_done(Bytecode::Op::IteratorResultDone const&)
  1240. {
  1241. load_accumulator(ARG1);
  1242. native_call((void*)cxx_iterator_result_done);
  1243. store_accumulator(RET);
  1244. check_exception();
  1245. }
  1246. static Value cxx_throw_if_not_object(VM& vm, Value value)
  1247. {
  1248. if (!value.is_object())
  1249. TRY_OR_SET_EXCEPTION(vm.throw_completion<TypeError>(ErrorType::NotAnObject, value.to_string_without_side_effects()));
  1250. return {};
  1251. }
  1252. void Compiler::compile_throw_if_not_object(Bytecode::Op::ThrowIfNotObject const&)
  1253. {
  1254. load_accumulator(ARG1);
  1255. native_call((void*)cxx_throw_if_not_object);
  1256. check_exception();
  1257. }
  1258. static Value cxx_throw_if_nullish(VM& vm, Value value)
  1259. {
  1260. if (value.is_nullish())
  1261. TRY_OR_SET_EXCEPTION(vm.throw_completion<TypeError>(ErrorType::NotObjectCoercible, value.to_string_without_side_effects()));
  1262. return {};
  1263. }
  1264. void Compiler::compile_throw_if_nullish(Bytecode::Op::ThrowIfNullish const&)
  1265. {
  1266. load_accumulator(ARG1);
  1267. native_call((void*)cxx_throw_if_nullish);
  1268. check_exception();
  1269. }
  1270. static Value cxx_iterator_result_value(VM& vm, Value iterator)
  1271. {
  1272. auto iterator_result = TRY_OR_SET_EXCEPTION(iterator.to_object(vm));
  1273. return TRY_OR_SET_EXCEPTION(iterator_value(vm, iterator_result));
  1274. }
  1275. void Compiler::compile_iterator_result_value(Bytecode::Op::IteratorResultValue const&)
  1276. {
  1277. load_accumulator(ARG1);
  1278. native_call((void*)cxx_iterator_result_value);
  1279. store_accumulator(RET);
  1280. check_exception();
  1281. }
  1282. static Value cxx_iterator_close(VM& vm, Value iterator, Completion::Type completion_type, Optional<Value> const& completion_value)
  1283. {
  1284. auto iterator_object = TRY_OR_SET_EXCEPTION(iterator.to_object(vm));
  1285. auto iterator_record = Bytecode::object_to_iterator(vm, iterator_object);
  1286. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1287. TRY_OR_SET_EXCEPTION(iterator_close(vm, iterator_record, Completion { completion_type, completion_value, {} }));
  1288. return {};
  1289. }
  1290. void Compiler::compile_iterator_close(Bytecode::Op::IteratorClose const& op)
  1291. {
  1292. load_accumulator(ARG1);
  1293. m_assembler.mov(
  1294. Assembler::Operand::Register(ARG2),
  1295. Assembler::Operand::Imm(to_underlying(op.completion_type())));
  1296. m_assembler.mov(
  1297. Assembler::Operand::Register(ARG3),
  1298. Assembler::Operand::Imm(bit_cast<u64>(&op.completion_value())));
  1299. native_call((void*)cxx_iterator_close);
  1300. check_exception();
  1301. }
  1302. static Value iterator_to_array(VM& vm, Value iterator)
  1303. {
  1304. return TRY_OR_SET_EXCEPTION(Bytecode::iterator_to_array(vm, iterator));
  1305. }
  1306. void Compiler::compile_iterator_to_array(Bytecode::Op::IteratorToArray const&)
  1307. {
  1308. load_accumulator(ARG1);
  1309. native_call((void*)iterator_to_array);
  1310. store_accumulator(RET);
  1311. check_exception();
  1312. }
  1313. static Value cxx_append(VM& vm, Value lhs, Value rhs, bool is_spread)
  1314. {
  1315. TRY_OR_SET_EXCEPTION(Bytecode::append(vm, lhs, rhs, is_spread));
  1316. return {};
  1317. }
  1318. void Compiler::compile_append(Bytecode::Op::Append const& op)
  1319. {
  1320. load_vm_register(ARG1, op.lhs());
  1321. load_accumulator(ARG2);
  1322. m_assembler.mov(
  1323. Assembler::Operand::Register(ARG3),
  1324. Assembler::Operand::Imm(static_cast<u64>(op.is_spread())));
  1325. native_call((void*)cxx_append);
  1326. check_exception();
  1327. }
  1328. static Value cxx_delete_by_id(VM& vm, Value base, Bytecode::IdentifierTableIndex property)
  1329. {
  1330. return TRY_OR_SET_EXCEPTION(Bytecode::delete_by_id(vm.bytecode_interpreter(), base, property));
  1331. }
  1332. void Compiler::compile_delete_by_id(Bytecode::Op::DeleteById const& op)
  1333. {
  1334. load_accumulator(ARG1);
  1335. m_assembler.mov(
  1336. Assembler::Operand::Register(ARG2),
  1337. Assembler::Operand::Imm(op.property().value()));
  1338. native_call((void*)cxx_delete_by_id);
  1339. store_accumulator(RET);
  1340. check_exception();
  1341. }
  1342. static Value cxx_delete_by_value(VM& vm, Value base_value, Value property_key_value)
  1343. {
  1344. return TRY_OR_SET_EXCEPTION(Bytecode::delete_by_value(vm.bytecode_interpreter(), base_value, property_key_value));
  1345. }
  1346. void Compiler::compile_delete_by_value(Bytecode::Op::DeleteByValue const& op)
  1347. {
  1348. load_vm_register(ARG1, op.base());
  1349. load_accumulator(ARG2);
  1350. native_call((void*)cxx_delete_by_value);
  1351. store_accumulator(RET);
  1352. check_exception();
  1353. }
  1354. static Value cxx_delete_by_value_with_this(VM& vm, Value base_value, Value property_key_value, Value this_value)
  1355. {
  1356. return TRY_OR_SET_EXCEPTION(Bytecode::delete_by_value_with_this(vm.bytecode_interpreter(), base_value, property_key_value, this_value));
  1357. }
  1358. void Compiler::compile_delete_by_value_with_this(Bytecode::Op::DeleteByValueWithThis const& op)
  1359. {
  1360. load_vm_register(ARG1, op.base());
  1361. load_accumulator(ARG2);
  1362. load_vm_register(ARG3, op.this_value());
  1363. native_call((void*)cxx_delete_by_value_with_this);
  1364. store_accumulator(RET);
  1365. check_exception();
  1366. }
  1367. static Value cxx_get_object_property_iterator(VM& vm, Value object)
  1368. {
  1369. return TRY_OR_SET_EXCEPTION(Bytecode::get_object_property_iterator(vm, object));
  1370. }
  1371. void Compiler::compile_get_object_property_iterator(Bytecode::Op::GetObjectPropertyIterator const&)
  1372. {
  1373. load_accumulator(ARG1);
  1374. native_call((void*)cxx_get_object_property_iterator);
  1375. store_accumulator(RET);
  1376. check_exception();
  1377. }
  1378. static Value cxx_get_private_by_id(VM& vm, Value base_value, DeprecatedFlyString& name)
  1379. {
  1380. auto private_reference = make_private_reference(vm, base_value, name);
  1381. return TRY_OR_SET_EXCEPTION(private_reference.get_value(vm));
  1382. }
  1383. void Compiler::compile_get_private_by_id(Bytecode::Op::GetPrivateById const& op)
  1384. {
  1385. load_accumulator(ARG1);
  1386. m_assembler.mov(
  1387. Assembler::Operand::Register(ARG2),
  1388. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1389. native_call((void*)cxx_get_private_by_id);
  1390. store_accumulator(RET);
  1391. check_exception();
  1392. }
  1393. static Value cxx_resolve_super_base(VM& vm)
  1394. {
  1395. // 1. Let env be GetThisEnvironment().
  1396. auto& env = verify_cast<FunctionEnvironment>(*get_this_environment(vm));
  1397. // 2. Assert: env.HasSuperBinding() is true.
  1398. VERIFY(env.has_super_binding());
  1399. // 3. Let baseValue be ? env.GetSuperBase().
  1400. return TRY_OR_SET_EXCEPTION(env.get_super_base());
  1401. }
  1402. void Compiler::compile_resolve_super_base(Bytecode::Op::ResolveSuperBase const&)
  1403. {
  1404. native_call((void*)cxx_resolve_super_base);
  1405. store_accumulator(RET);
  1406. check_exception();
  1407. }
  1408. static Value cxx_get_by_id_with_this(VM& vm, DeprecatedFlyString const& property, Value base_value, Value this_value, Bytecode::PropertyLookupCache& cache)
  1409. {
  1410. return TRY_OR_SET_EXCEPTION(Bytecode::get_by_id(vm, property, base_value, this_value, cache));
  1411. }
  1412. void Compiler::compile_get_by_id_with_this(Bytecode::Op::GetByIdWithThis const& op)
  1413. {
  1414. m_assembler.mov(
  1415. Assembler::Operand::Register(ARG1),
  1416. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1417. load_accumulator(ARG2);
  1418. load_vm_register(ARG3, op.this_value());
  1419. m_assembler.mov(
  1420. Assembler::Operand::Register(ARG4),
  1421. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.property_lookup_caches[op.cache_index()])));
  1422. native_call((void*)cxx_get_by_id_with_this);
  1423. store_accumulator(RET);
  1424. check_exception();
  1425. }
  1426. static Value cxx_get_by_value_with_this(VM& vm, Value property_key_value, Value base, Value this_value)
  1427. {
  1428. auto object = TRY_OR_SET_EXCEPTION(base.to_object(vm));
  1429. auto property_key = TRY_OR_SET_EXCEPTION(property_key_value.to_property_key(vm));
  1430. return TRY_OR_SET_EXCEPTION(object->internal_get(property_key, this_value));
  1431. }
  1432. void Compiler::compile_get_by_value_with_this(Bytecode::Op::GetByValueWithThis const& op)
  1433. {
  1434. load_accumulator(ARG1);
  1435. load_vm_register(ARG2, op.base());
  1436. load_vm_register(ARG3, op.this_value());
  1437. native_call((void*)cxx_get_by_value_with_this);
  1438. store_accumulator(RET);
  1439. check_exception();
  1440. }
  1441. static Value cxx_delete_by_id_with_this(VM& vm, Value base_value, DeprecatedFlyString const& identifier, Value this_value)
  1442. {
  1443. auto reference = Reference { base_value, identifier, this_value, vm.in_strict_mode() };
  1444. return Value(TRY_OR_SET_EXCEPTION(reference.delete_(vm)));
  1445. }
  1446. void Compiler::compile_delete_by_id_with_this(Bytecode::Op::DeleteByIdWithThis const& op)
  1447. {
  1448. load_accumulator(ARG1);
  1449. m_assembler.mov(
  1450. Assembler::Operand::Register(ARG2),
  1451. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1452. load_vm_register(ARG3, op.this_value());
  1453. native_call((void*)cxx_delete_by_id_with_this);
  1454. store_accumulator(RET);
  1455. }
  1456. static Value cxx_put_by_id_with_this(VM& vm, Value base, Value value, DeprecatedFlyString const& name, Value this_value, Bytecode::Op::PropertyKind kind)
  1457. {
  1458. TRY_OR_SET_EXCEPTION(Bytecode::put_by_property_key(vm, base, this_value, value, name, kind));
  1459. return {};
  1460. }
  1461. void Compiler::compile_put_by_id_with_this(Bytecode::Op::PutByIdWithThis const& op)
  1462. {
  1463. load_vm_register(ARG1, op.base());
  1464. load_accumulator(ARG2);
  1465. m_assembler.mov(
  1466. Assembler::Operand::Register(ARG3),
  1467. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1468. load_vm_register(ARG4, op.this_value());
  1469. m_assembler.mov(
  1470. Assembler::Operand::Register(ARG5),
  1471. Assembler::Operand::Imm(to_underlying(op.kind())));
  1472. native_call((void*)cxx_put_by_id_with_this);
  1473. check_exception();
  1474. }
  1475. static Value cxx_put_private_by_id(VM& vm, Value base, Value value, DeprecatedFlyString const& name)
  1476. {
  1477. auto object = TRY_OR_SET_EXCEPTION(base.to_object(vm));
  1478. auto private_reference = make_private_reference(vm, object, name);
  1479. TRY_OR_SET_EXCEPTION(private_reference.put_value(vm, value));
  1480. return value;
  1481. }
  1482. void Compiler::compile_put_private_by_id(Bytecode::Op::PutPrivateById const& op)
  1483. {
  1484. load_vm_register(ARG1, op.base());
  1485. load_accumulator(ARG2);
  1486. m_assembler.mov(
  1487. Assembler::Operand::Register(ARG3),
  1488. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1489. native_call((void*)cxx_put_private_by_id);
  1490. store_accumulator(RET);
  1491. check_exception();
  1492. }
  1493. static Value cxx_import_call(VM& vm, Value specifier, Value options)
  1494. {
  1495. return TRY_OR_SET_EXCEPTION(perform_import_call(vm, specifier, options));
  1496. }
  1497. void Compiler::compile_import_call(Bytecode::Op::ImportCall const& op)
  1498. {
  1499. load_vm_register(ARG1, op.specifier());
  1500. load_vm_register(ARG2, op.options());
  1501. native_call((void*)cxx_import_call);
  1502. store_accumulator(RET);
  1503. check_exception();
  1504. }
  1505. static Value cxx_get_import_meta(VM& vm)
  1506. {
  1507. return vm.get_import_meta();
  1508. }
  1509. void Compiler::compile_get_import_meta(Bytecode::Op::GetImportMeta const&)
  1510. {
  1511. native_call((void*)cxx_get_import_meta);
  1512. store_accumulator(RET);
  1513. }
  1514. static Value cxx_delete_variable(VM& vm, DeprecatedFlyString const& identifier)
  1515. {
  1516. auto reference = TRY_OR_SET_EXCEPTION(vm.resolve_binding(identifier));
  1517. return Value(TRY_OR_SET_EXCEPTION(reference.delete_(vm)));
  1518. }
  1519. void Compiler::compile_delete_variable(Bytecode::Op::DeleteVariable const& op)
  1520. {
  1521. m_assembler.mov(
  1522. Assembler::Operand::Register(ARG1),
  1523. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier().value()))));
  1524. native_call((void*)cxx_delete_variable);
  1525. store_accumulator(RET);
  1526. check_exception();
  1527. }
  1528. static Value cxx_get_method(VM& vm, Value value, DeprecatedFlyString const& identifier)
  1529. {
  1530. auto method = TRY_OR_SET_EXCEPTION(value.get_method(vm, identifier));
  1531. return method ?: js_undefined();
  1532. }
  1533. void Compiler::compile_get_method(Bytecode::Op::GetMethod const& op)
  1534. {
  1535. load_accumulator(ARG1);
  1536. m_assembler.mov(
  1537. Assembler::Operand::Register(ARG2),
  1538. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1539. native_call((void*)cxx_get_method);
  1540. store_accumulator(RET);
  1541. check_exception();
  1542. }
  1543. static Value cxx_get_new_target(VM& vm)
  1544. {
  1545. return vm.get_new_target();
  1546. }
  1547. void Compiler::compile_get_new_target(Bytecode::Op::GetNewTarget const&)
  1548. {
  1549. native_call((void*)cxx_get_new_target);
  1550. store_accumulator(RET);
  1551. }
  1552. static Value cxx_has_private_id(VM& vm, Value object, DeprecatedFlyString const& identifier)
  1553. {
  1554. if (!object.is_object())
  1555. TRY_OR_SET_EXCEPTION(vm.throw_completion<TypeError>(ErrorType::InOperatorWithObject));
  1556. auto private_environment = vm.running_execution_context().private_environment;
  1557. VERIFY(private_environment);
  1558. auto private_name = private_environment->resolve_private_identifier(identifier);
  1559. return Value(object.as_object().private_element_find(private_name) != nullptr);
  1560. }
  1561. void Compiler::compile_has_private_id(Bytecode::Op::HasPrivateId const& op)
  1562. {
  1563. load_accumulator(ARG1);
  1564. m_assembler.mov(
  1565. Assembler::Operand::Register(ARG2),
  1566. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1567. native_call((void*)cxx_has_private_id);
  1568. store_accumulator(RET);
  1569. check_exception();
  1570. }
  1571. # define COMPILE_NEW_BUILTIN_ERROR_OP(NewErrorName, new_error_name, ErrorName) \
  1572. static Value cxx_##new_error_name(VM& vm, DeprecatedString const& error_string) \
  1573. { \
  1574. return ErrorName::create(*vm.current_realm(), error_string); \
  1575. } \
  1576. \
  1577. void Compiler::compile_##new_error_name(Bytecode::Op::NewErrorName const& op) \
  1578. { \
  1579. m_assembler.mov( \
  1580. Assembler::Operand::Register(ARG1), \
  1581. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_string(op.error_string())))); \
  1582. native_call((void*)cxx_##new_error_name); \
  1583. store_accumulator(RET); \
  1584. }
  1585. JS_ENUMERATE_NEW_BUILTIN_ERROR_BYTECODE_OPS(COMPILE_NEW_BUILTIN_ERROR_OP)
  1586. # undef COMPILE_NEW_BUILTIN_ERROR_OP
  1587. static Value cxx_put_by_value_with_this(VM& vm, Value base, Value value, Value name, Value this_value, Bytecode::Op::PropertyKind kind)
  1588. {
  1589. auto property_key = kind != Bytecode::Op::PropertyKind::Spread ? TRY_OR_SET_EXCEPTION(name.to_property_key(vm)) : PropertyKey {};
  1590. TRY_OR_SET_EXCEPTION(Bytecode::put_by_property_key(vm, base, this_value, value, property_key, kind));
  1591. return value;
  1592. }
  1593. void Compiler::compile_put_by_value_with_this(Bytecode::Op::PutByValueWithThis const& op)
  1594. {
  1595. load_vm_register(ARG1, op.base());
  1596. load_accumulator(ARG2);
  1597. if (op.kind() != Bytecode::Op::PropertyKind::Spread) {
  1598. load_vm_register(ARG3, op.property());
  1599. } else {
  1600. m_assembler.mov(
  1601. Assembler::Operand::Register(ARG3),
  1602. Assembler::Operand::Imm(Value().encoded()));
  1603. }
  1604. load_vm_register(ARG4, op.this_value());
  1605. m_assembler.mov(
  1606. Assembler::Operand::Register(ARG5),
  1607. Assembler::Operand::Imm(to_underlying(op.kind())));
  1608. native_call((void*)cxx_put_by_value_with_this);
  1609. store_accumulator(RET);
  1610. check_exception();
  1611. }
  1612. static Value cxx_copy_object_excluding_properties(VM& vm, Value from_object, u64 excluded_names_count, Value* excluded_names)
  1613. {
  1614. auto& realm = *vm.current_realm();
  1615. auto to_object = Object::create(realm, realm.intrinsics().object_prototype());
  1616. HashTable<PropertyKey> excluded_names_table;
  1617. for (size_t i = 0; i < excluded_names_count; ++i) {
  1618. excluded_names_table.set(TRY_OR_SET_EXCEPTION(excluded_names[i].to_property_key(vm)));
  1619. }
  1620. TRY_OR_SET_EXCEPTION(to_object->copy_data_properties(vm, from_object, excluded_names_table));
  1621. return to_object;
  1622. }
  1623. void Compiler::compile_copy_object_excluding_properties(Bytecode::Op::CopyObjectExcludingProperties const& op)
  1624. {
  1625. load_vm_register(ARG1, op.from_object());
  1626. m_assembler.mov(
  1627. Assembler::Operand::Register(ARG2),
  1628. Assembler::Operand::Imm(op.excluded_names_count()));
  1629. // Build `Value arg3[op.excluded_names_count()] {...}` on the stack.
  1630. auto stack_space = align_up_to(op.excluded_names_count() * sizeof(Value), 16);
  1631. m_assembler.sub(Assembler::Operand::Register(STACK_POINTER), Assembler::Operand::Imm(stack_space));
  1632. m_assembler.mov(Assembler::Operand::Register(ARG3), Assembler::Operand::Register(STACK_POINTER));
  1633. for (size_t i = 0; i < op.excluded_names_count(); ++i) {
  1634. load_vm_register(GPR0, op.excluded_names()[i]);
  1635. m_assembler.mov(Assembler::Operand::Mem64BaseAndOffset(ARG3, i * sizeof(Value)), Assembler::Operand::Register(GPR0));
  1636. }
  1637. native_call((void*)cxx_copy_object_excluding_properties);
  1638. // Restore the stack pointer / discard array.
  1639. m_assembler.add(Assembler::Operand::Register(STACK_POINTER), Assembler::Operand::Imm(stack_space));
  1640. store_accumulator(RET);
  1641. check_exception();
  1642. }
  1643. static Value cxx_async_iterator_close(VM& vm, Value iterator, Completion::Type completion_type, Optional<Value> const& completion_value)
  1644. {
  1645. auto iterator_object = TRY_OR_SET_EXCEPTION(iterator.to_object(vm));
  1646. auto iterator_record = Bytecode::object_to_iterator(vm, iterator_object);
  1647. // FIXME: Return the value of the resulting completion. (Note that completion_value can be empty!)
  1648. TRY_OR_SET_EXCEPTION(async_iterator_close(vm, iterator_record, Completion { completion_type, completion_value, {} }));
  1649. return {};
  1650. }
  1651. void Compiler::compile_async_iterator_close(Bytecode::Op::AsyncIteratorClose const& op)
  1652. {
  1653. load_accumulator(ARG1);
  1654. m_assembler.mov(
  1655. Assembler::Operand::Register(ARG2),
  1656. Assembler::Operand::Imm(to_underlying(op.completion_type())));
  1657. m_assembler.mov(
  1658. Assembler::Operand::Register(ARG3),
  1659. Assembler::Operand::Imm(bit_cast<u64>(&op.completion_value())));
  1660. native_call((void*)cxx_async_iterator_close);
  1661. check_exception();
  1662. }
  1663. static Value cxx_continuation(VM& vm, Value value, Value continuation, Value is_await)
  1664. {
  1665. auto object = Object::create(*vm.current_realm(), nullptr);
  1666. object->define_direct_property("result", value.value_or(js_undefined()), JS::default_attributes);
  1667. object->define_direct_property("continuation", continuation, JS::default_attributes);
  1668. object->define_direct_property("isAwait", is_await, JS::default_attributes);
  1669. return object;
  1670. }
  1671. void Compiler::compile_continuation(Optional<Bytecode::Label> continuation, bool is_await)
  1672. {
  1673. load_accumulator(ARG1);
  1674. if (continuation.has_value()) {
  1675. // FIXME: If we get a pointer, which is not accurately representable as a double
  1676. // will cause this to explode
  1677. auto continuation_value = Value(static_cast<double>(bit_cast<u64>(&continuation->block())));
  1678. m_assembler.mov(
  1679. Assembler::Operand::Register(ARG2),
  1680. Assembler::Operand::Imm(continuation_value.encoded()));
  1681. } else {
  1682. m_assembler.mov(
  1683. Assembler::Operand::Register(ARG2),
  1684. Assembler::Operand::Imm(Value(0).encoded()));
  1685. }
  1686. m_assembler.mov(
  1687. Assembler::Operand::Register(ARG3),
  1688. Assembler::Operand::Imm(Value(is_await).encoded()));
  1689. native_call((void*)cxx_continuation);
  1690. store_vm_register(Bytecode::Register::return_value(), RET);
  1691. // FIXME: This should run the finalizer if it is a return
  1692. jump_to_exit();
  1693. }
  1694. void Compiler::compile_yield(Bytecode::Op::Yield const& op)
  1695. {
  1696. compile_continuation(op.continuation(), false);
  1697. }
  1698. void Compiler::compile_await(Bytecode::Op::Await const& op)
  1699. {
  1700. compile_continuation(op.continuation(), true);
  1701. }
  1702. void Compiler::jump_to_exit()
  1703. {
  1704. m_assembler.jump(m_exit_label);
  1705. }
  1706. void Compiler::native_call(void* function_address, Vector<Assembler::Operand> const& stack_arguments)
  1707. {
  1708. // NOTE: We don't preserve caller-saved registers when making a native call.
  1709. // This means that they may have changed after we return from the call.
  1710. m_assembler.native_call(bit_cast<u64>(function_address), { Assembler::Operand::Register(ARG0) }, stack_arguments);
  1711. }
  1712. OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_executable)
  1713. {
  1714. if (!getenv("LIBJS_JIT"))
  1715. return nullptr;
  1716. Compiler compiler { bytecode_executable };
  1717. Vector<BytecodeMapping> mapping;
  1718. mapping.append({
  1719. .native_offset = compiler.m_output.size(),
  1720. .block_index = BytecodeMapping::EXECUTABLE,
  1721. .bytecode_offset = 0,
  1722. });
  1723. compiler.m_assembler.enter();
  1724. compiler.m_assembler.mov(
  1725. Assembler::Operand::Register(REGISTER_ARRAY_BASE),
  1726. Assembler::Operand::Register(ARG1));
  1727. compiler.m_assembler.mov(
  1728. Assembler::Operand::Register(LOCALS_ARRAY_BASE),
  1729. Assembler::Operand::Register(ARG2));
  1730. compiler.reload_cached_accumulator();
  1731. Assembler::Label normal_entry {};
  1732. compiler.m_assembler.jump_if(
  1733. Assembler::Operand::Register(ARG3),
  1734. Assembler::Condition::EqualTo,
  1735. Assembler::Operand::Imm(0),
  1736. normal_entry);
  1737. compiler.m_assembler.jump(Assembler::Operand::Register(ARG3));
  1738. normal_entry.link(compiler.m_assembler);
  1739. for (size_t block_index = 0; block_index < bytecode_executable.basic_blocks.size(); block_index++) {
  1740. auto& block = bytecode_executable.basic_blocks[block_index];
  1741. compiler.block_data_for(*block).start_offset = compiler.m_output.size();
  1742. compiler.set_current_block(*block);
  1743. auto it = Bytecode::InstructionStreamIterator(block->instruction_stream());
  1744. if (it.at_end()) {
  1745. mapping.append({
  1746. .native_offset = compiler.m_output.size(),
  1747. .block_index = block_index,
  1748. .bytecode_offset = 0,
  1749. });
  1750. }
  1751. while (!it.at_end()) {
  1752. auto const& op = *it;
  1753. mapping.append({
  1754. .native_offset = compiler.m_output.size(),
  1755. .block_index = block_index,
  1756. .bytecode_offset = it.offset(),
  1757. });
  1758. switch (op.type()) {
  1759. # define CASE_BYTECODE_OP(OpTitleCase, op_snake_case, ...) \
  1760. case Bytecode::Instruction::Type::OpTitleCase: \
  1761. compiler.compile_##op_snake_case(static_cast<Bytecode::Op::OpTitleCase const&>(op)); \
  1762. break;
  1763. JS_ENUMERATE_IMPLEMENTED_JIT_OPS(CASE_BYTECODE_OP)
  1764. # undef CASE_BYTECODE_OP
  1765. default:
  1766. if constexpr (LOG_JIT_FAILURE) {
  1767. dbgln("\033[31;1mJIT compilation failed\033[0m: {}", bytecode_executable.name);
  1768. dbgln("Unsupported bytecode op: {}", op.to_deprecated_string(bytecode_executable));
  1769. }
  1770. return nullptr;
  1771. }
  1772. ++it;
  1773. }
  1774. if (!block->is_terminated())
  1775. compiler.jump_to_exit();
  1776. }
  1777. mapping.append({
  1778. .native_offset = compiler.m_output.size(),
  1779. .block_index = BytecodeMapping::EXECUTABLE,
  1780. .bytecode_offset = 1,
  1781. });
  1782. compiler.m_exit_label.link(compiler.m_assembler);
  1783. compiler.flush_cached_accumulator();
  1784. compiler.m_assembler.exit();
  1785. auto* executable_memory = mmap(nullptr, compiler.m_output.size(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  1786. if (executable_memory == MAP_FAILED) {
  1787. dbgln("mmap: {}", strerror(errno));
  1788. return nullptr;
  1789. }
  1790. for (auto& block : bytecode_executable.basic_blocks) {
  1791. auto& block_data = compiler.block_data_for(*block);
  1792. block_data.label.link_to(compiler.m_assembler, block_data.start_offset);
  1793. }
  1794. if constexpr (DUMP_JIT_MACHINE_CODE_TO_STDOUT) {
  1795. (void)write(STDOUT_FILENO, compiler.m_output.data(), compiler.m_output.size());
  1796. }
  1797. memcpy(executable_memory, compiler.m_output.data(), compiler.m_output.size());
  1798. if (mprotect(executable_memory, compiler.m_output.size(), PROT_READ | PROT_EXEC) < 0) {
  1799. dbgln("mprotect: {}", strerror(errno));
  1800. return nullptr;
  1801. }
  1802. if constexpr (LOG_JIT_SUCCESS) {
  1803. dbgln("\033[32;1mJIT compilation succeeded!\033[0m {}", bytecode_executable.name);
  1804. }
  1805. auto executable = make<NativeExecutable>(executable_memory, compiler.m_output.size(), mapping);
  1806. if constexpr (DUMP_JIT_DISASSEMBLY)
  1807. executable->dump_disassembly(bytecode_executable);
  1808. return executable;
  1809. }
  1810. }
  1811. #endif