Compiler.cpp 82 KB

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