Compiler.cpp 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300
  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 Value cxx_unsigned_right_shift(VM& vm, Value lhs, Value rhs)
  699. {
  700. return TRY_OR_SET_EXCEPTION(unsigned_right_shift(vm, lhs, rhs));
  701. }
  702. void Compiler::compile_unsigned_right_shift(Bytecode::Op::UnsignedRightShift const& op)
  703. {
  704. load_vm_register(ARG1, op.lhs());
  705. load_accumulator(ARG2);
  706. Assembler::Label end {};
  707. branch_if_both_int32(ARG1, ARG2, [&] {
  708. // RCX = ARG2
  709. m_assembler.mov(
  710. Assembler::Operand::Register(Assembler::Reg::RCX),
  711. Assembler::Operand::Register(ARG2));
  712. // ARG1 >>>= CL (32-bit)
  713. m_assembler.shift_right32(Assembler::Operand::Register(ARG1), {});
  714. // accumulator = ARG1 | SHIFTED_INT32_TAG;
  715. m_assembler.mov(
  716. Assembler::Operand::Register(GPR0),
  717. Assembler::Operand::Imm(SHIFTED_INT32_TAG));
  718. m_assembler.bitwise_or(
  719. Assembler::Operand::Register(ARG1),
  720. Assembler::Operand::Register(GPR0));
  721. store_accumulator(ARG1);
  722. m_assembler.jump(end);
  723. });
  724. native_call((void*)cxx_unsigned_right_shift);
  725. store_accumulator(RET);
  726. check_exception();
  727. end.link(m_assembler);
  728. }
  729. static ThrowCompletionOr<Value> not_(VM&, Value value)
  730. {
  731. return Value(!value.to_boolean());
  732. }
  733. static ThrowCompletionOr<Value> typeof_(VM& vm, Value value)
  734. {
  735. return PrimitiveString::create(vm, value.typeof());
  736. }
  737. # define DO_COMPILE_COMMON_UNARY_OP(TitleCaseName, snake_case_name) \
  738. static Value cxx_##snake_case_name(VM& vm, Value value) \
  739. { \
  740. return TRY_OR_SET_EXCEPTION(snake_case_name(vm, value)); \
  741. } \
  742. \
  743. void Compiler::compile_##snake_case_name(Bytecode::Op::TitleCaseName const&) \
  744. { \
  745. load_accumulator(ARG1); \
  746. native_call((void*)cxx_##snake_case_name); \
  747. store_accumulator(RET); \
  748. check_exception(); \
  749. }
  750. JS_ENUMERATE_COMMON_UNARY_OPS(DO_COMPILE_COMMON_UNARY_OP)
  751. # undef DO_COMPILE_COMMON_UNARY_OP
  752. void Compiler::compile_return(Bytecode::Op::Return const&)
  753. {
  754. load_accumulator(GPR0);
  755. if (auto const* finalizer = current_block().finalizer(); finalizer) {
  756. store_vm_register(Bytecode::Register::saved_return_value(), GPR0);
  757. m_assembler.jump(label_for(*finalizer));
  758. } else {
  759. store_vm_register(Bytecode::Register::return_value(), GPR0);
  760. jump_to_exit();
  761. }
  762. }
  763. static Value cxx_new_string(VM& vm, DeprecatedString const& string)
  764. {
  765. return PrimitiveString::create(vm, string);
  766. }
  767. void Compiler::compile_new_string(Bytecode::Op::NewString const& op)
  768. {
  769. auto const& string = m_bytecode_executable.string_table->get(op.index());
  770. m_assembler.mov(
  771. Assembler::Operand::Register(ARG1),
  772. Assembler::Operand::Imm(bit_cast<u64>(&string)));
  773. native_call((void*)cxx_new_string);
  774. store_accumulator(RET);
  775. }
  776. void Compiler::compile_new_regexp(Bytecode::Op::NewRegExp const& op)
  777. {
  778. auto const& parsed_regex = m_bytecode_executable.regex_table->get(op.regex_index());
  779. auto const& pattern = m_bytecode_executable.string_table->get(op.source_index());
  780. auto const& flags = m_bytecode_executable.string_table->get(op.flags_index());
  781. m_assembler.mov(
  782. Assembler::Operand::Register(ARG1),
  783. Assembler::Operand::Imm(bit_cast<u64>(&parsed_regex)));
  784. m_assembler.mov(
  785. Assembler::Operand::Register(ARG2),
  786. Assembler::Operand::Imm(bit_cast<u64>(&pattern)));
  787. m_assembler.mov(
  788. Assembler::Operand::Register(ARG3),
  789. Assembler::Operand::Imm(bit_cast<u64>(&flags)));
  790. native_call((void*)Bytecode::new_regexp);
  791. store_accumulator(RET);
  792. }
  793. static Value cxx_new_bigint(VM& vm, Crypto::SignedBigInteger const& bigint)
  794. {
  795. return BigInt::create(vm, bigint);
  796. }
  797. void Compiler::compile_new_bigint(Bytecode::Op::NewBigInt const& op)
  798. {
  799. m_assembler.mov(
  800. Assembler::Operand::Register(ARG1),
  801. Assembler::Operand::Imm(bit_cast<u64>(&op.bigint())));
  802. native_call((void*)cxx_new_bigint);
  803. store_accumulator(RET);
  804. }
  805. static Value cxx_new_object(VM& vm)
  806. {
  807. auto& realm = *vm.current_realm();
  808. return Object::create(realm, realm.intrinsics().object_prototype());
  809. }
  810. void Compiler::compile_new_object(Bytecode::Op::NewObject const&)
  811. {
  812. native_call((void*)cxx_new_object);
  813. store_accumulator(RET);
  814. }
  815. static Value cxx_new_array(VM& vm, size_t element_count, u32 first_register_index)
  816. {
  817. auto& realm = *vm.current_realm();
  818. auto array = MUST(Array::create(realm, 0));
  819. for (size_t i = 0; i < element_count; ++i) {
  820. auto& value = vm.bytecode_interpreter().reg(Bytecode::Register(first_register_index + i));
  821. array->indexed_properties().put(i, value, default_attributes);
  822. }
  823. return array;
  824. }
  825. void Compiler::compile_new_array(Bytecode::Op::NewArray const& op)
  826. {
  827. m_assembler.mov(
  828. Assembler::Operand::Register(ARG1),
  829. Assembler::Operand::Imm(op.element_count()));
  830. m_assembler.mov(
  831. Assembler::Operand::Register(ARG2),
  832. Assembler::Operand::Imm(op.element_count() ? op.start().index() : 0));
  833. native_call((void*)cxx_new_array);
  834. store_accumulator(RET);
  835. }
  836. void Compiler::compile_new_function(Bytecode::Op::NewFunction const& op)
  837. {
  838. m_assembler.mov(
  839. Assembler::Operand::Register(ARG1),
  840. Assembler::Operand::Imm(bit_cast<u64>(&op.function_node())));
  841. m_assembler.mov(
  842. Assembler::Operand::Register(ARG2),
  843. Assembler::Operand::Imm(bit_cast<u64>(&op.lhs_name())));
  844. m_assembler.mov(
  845. Assembler::Operand::Register(ARG3),
  846. Assembler::Operand::Imm(bit_cast<u64>(&op.home_object())));
  847. native_call((void*)Bytecode::new_function);
  848. store_accumulator(RET);
  849. }
  850. static Value cxx_new_class(VM& vm, Value super_class, ClassExpression const& class_expression, Optional<Bytecode::IdentifierTableIndex> const& lhs_name)
  851. {
  852. return TRY_OR_SET_EXCEPTION(Bytecode::new_class(vm, super_class, class_expression, lhs_name));
  853. }
  854. void Compiler::compile_new_class(Bytecode::Op::NewClass const& op)
  855. {
  856. load_accumulator(ARG1);
  857. m_assembler.mov(
  858. Assembler::Operand::Register(ARG2),
  859. Assembler::Operand::Imm(bit_cast<u64>(&op.class_expression())));
  860. m_assembler.mov(
  861. Assembler::Operand::Register(ARG3),
  862. Assembler::Operand::Imm(bit_cast<u64>(&op.lhs_name())));
  863. native_call((void*)cxx_new_class);
  864. store_accumulator(RET);
  865. }
  866. static Value cxx_get_by_id(VM& vm, Value base, DeprecatedFlyString const& property, Bytecode::PropertyLookupCache& cache)
  867. {
  868. return TRY_OR_SET_EXCEPTION(Bytecode::get_by_id(vm, property, base, base, cache));
  869. }
  870. void Compiler::compile_get_by_id(Bytecode::Op::GetById const& op)
  871. {
  872. load_accumulator(ARG1);
  873. m_assembler.mov(
  874. Assembler::Operand::Register(ARG2),
  875. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  876. m_assembler.mov(
  877. Assembler::Operand::Register(ARG3),
  878. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.property_lookup_caches[op.cache_index()])));
  879. native_call((void*)cxx_get_by_id);
  880. store_accumulator(RET);
  881. check_exception();
  882. }
  883. static Value cxx_get_by_value(VM& vm, Value base, Value property)
  884. {
  885. return TRY_OR_SET_EXCEPTION(Bytecode::get_by_value(vm, base, property));
  886. }
  887. void Compiler::compile_get_by_value(Bytecode::Op::GetByValue const& op)
  888. {
  889. load_vm_register(ARG1, op.base());
  890. load_accumulator(ARG2);
  891. native_call((void*)cxx_get_by_value);
  892. store_accumulator(RET);
  893. check_exception();
  894. }
  895. static Value cxx_get_global(VM& vm, DeprecatedFlyString const& identifier, Bytecode::GlobalVariableCache& cache)
  896. {
  897. return TRY_OR_SET_EXCEPTION(Bytecode::get_global(vm.bytecode_interpreter(), identifier, cache));
  898. }
  899. void Compiler::compile_get_global(Bytecode::Op::GetGlobal const& op)
  900. {
  901. m_assembler.mov(
  902. Assembler::Operand::Register(ARG1),
  903. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier()))));
  904. m_assembler.mov(
  905. Assembler::Operand::Register(ARG2),
  906. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.global_variable_caches[op.cache_index()])));
  907. native_call((void*)cxx_get_global);
  908. store_accumulator(RET);
  909. check_exception();
  910. }
  911. static Value cxx_get_variable(VM& vm, DeprecatedFlyString const& name, Bytecode::EnvironmentVariableCache& cache)
  912. {
  913. return TRY_OR_SET_EXCEPTION(Bytecode::get_variable(vm.bytecode_interpreter(), name, cache));
  914. }
  915. void Compiler::compile_get_variable(Bytecode::Op::GetVariable const& op)
  916. {
  917. Assembler::Label slow_case;
  918. // if (!cache.has_value()) goto slow_case;
  919. m_assembler.mov(
  920. Assembler::Operand::Register(ARG2),
  921. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.environment_variable_caches[op.cache_index()])));
  922. // FIXME: Figure out a nicer way to load a single byte. :^)
  923. m_assembler.mov(
  924. Assembler::Operand::Register(GPR0),
  925. Assembler::Operand::Mem64BaseAndOffset(ARG2, Bytecode::EnvironmentVariableCache::has_value_offset()));
  926. m_assembler.bitwise_and(
  927. Assembler::Operand::Register(GPR0),
  928. Assembler::Operand::Imm(0xff));
  929. m_assembler.jump_if(
  930. Assembler::Operand::Register(GPR0),
  931. Assembler::Condition::EqualTo,
  932. Assembler::Operand::Imm(0),
  933. slow_case);
  934. // auto environment = vm.running_execution_context().lexical_environment;
  935. // GPR1 = current lexical environment
  936. m_assembler.mov(
  937. Assembler::Operand::Register(GPR1),
  938. Assembler::Operand::Mem64BaseAndOffset(RUNNING_EXECUTION_CONTEXT_BASE, ExecutionContext::lexical_environment_offset()));
  939. // for (size_t i = 0; i < cache->hops; ++i)
  940. // environment = environment->outer_environment();
  941. // GPR0 = hops
  942. // FIXME: Load 32 bits directly instead of 64 and masking.
  943. m_assembler.mov(
  944. Assembler::Operand::Register(GPR0),
  945. Assembler::Operand::Mem64BaseAndOffset(ARG2, Bytecode::EnvironmentVariableCache::value_offset() + EnvironmentCoordinate::hops_offset()));
  946. m_assembler.mov(
  947. Assembler::Operand::Register(GPR2),
  948. Assembler::Operand::Imm(0xffffffff));
  949. m_assembler.bitwise_and(
  950. Assembler::Operand::Register(GPR0),
  951. Assembler::Operand::Register(GPR2));
  952. {
  953. // while (GPR0--)
  954. // GPR1 = GPR1->outer_environment()
  955. Assembler::Label loop_start;
  956. Assembler::Label loop_end;
  957. loop_start.link(m_assembler);
  958. m_assembler.jump_if(
  959. Assembler::Operand::Register(GPR0),
  960. Assembler::Condition::EqualTo,
  961. Assembler::Operand::Imm(0),
  962. loop_end);
  963. m_assembler.sub(
  964. Assembler::Operand::Register(GPR0),
  965. Assembler::Operand::Imm(1));
  966. m_assembler.mov(
  967. Assembler::Operand::Register(GPR1),
  968. Assembler::Operand::Mem64BaseAndOffset(GPR1, Environment::outer_environment_offset()));
  969. m_assembler.jump(loop_start);
  970. loop_end.link(m_assembler);
  971. }
  972. // GPR1 now points to the environment holding our binding.
  973. // if (environment->is_permanently_screwed_by_eval()) goto slow_case;
  974. // FIXME: Load 8 bits here directly instead of loading 64 and masking.
  975. m_assembler.mov(
  976. Assembler::Operand::Register(GPR0),
  977. Assembler::Operand::Mem64BaseAndOffset(GPR1, Environment::is_permanently_screwed_by_eval_offset()));
  978. m_assembler.bitwise_and(
  979. Assembler::Operand::Register(GPR0),
  980. Assembler::Operand::Imm(0xff));
  981. m_assembler.jump_if(
  982. Assembler::Operand::Register(GPR0),
  983. Assembler::Condition::NotEqualTo,
  984. Assembler::Operand::Imm(0),
  985. slow_case);
  986. // GPR1 = environment->m_bindings.outline_buffer()
  987. m_assembler.mov(
  988. Assembler::Operand::Register(GPR1),
  989. Assembler::Operand::Mem64BaseAndOffset(GPR1, DeclarativeEnvironment::bindings_offset() + Vector<DeclarativeEnvironment::Binding>::outline_buffer_offset()));
  990. // GPR0 = index
  991. // FIXME: Load 32 bits directly instead of 64 and masking.
  992. m_assembler.mov(
  993. Assembler::Operand::Register(GPR0),
  994. Assembler::Operand::Mem64BaseAndOffset(ARG2, Bytecode::EnvironmentVariableCache::value_offset() + EnvironmentCoordinate::index_offset()));
  995. m_assembler.mov(
  996. Assembler::Operand::Register(GPR2),
  997. Assembler::Operand::Imm(0xffffffff));
  998. m_assembler.bitwise_and(
  999. Assembler::Operand::Register(GPR0),
  1000. Assembler::Operand::Register(GPR2));
  1001. // GPR0 *= sizeof(DeclarativeEnvironment::Binding)
  1002. m_assembler.mul32(
  1003. Assembler::Operand::Register(GPR0),
  1004. Assembler::Operand::Imm(sizeof(DeclarativeEnvironment::Binding)),
  1005. slow_case);
  1006. // GPR1 = &binding
  1007. m_assembler.add(
  1008. Assembler::Operand::Register(GPR1),
  1009. Assembler::Operand::Register(GPR0));
  1010. // if (!binding.initialized) goto slow_case;
  1011. m_assembler.mov(
  1012. Assembler ::Operand::Register(GPR0),
  1013. Assembler::Operand::Mem64BaseAndOffset(GPR1, DeclarativeEnvironment::Binding::initialized_offset()));
  1014. m_assembler.bitwise_and(
  1015. Assembler::Operand::Register(GPR0),
  1016. Assembler::Operand::Imm(0xff));
  1017. m_assembler.jump_if(
  1018. Assembler::Operand::Register(GPR0),
  1019. Assembler::Condition::EqualTo,
  1020. Assembler::Operand::Imm(0),
  1021. slow_case);
  1022. // accumulator = binding.value;
  1023. m_assembler.mov(
  1024. Assembler::Operand::Register(GPR0),
  1025. Assembler::Operand::Mem64BaseAndOffset(GPR1, DeclarativeEnvironment::Binding::value_offset()));
  1026. store_accumulator(GPR0);
  1027. Assembler::Label end;
  1028. m_assembler.jump(end);
  1029. // Slow case: Uncached access. Call C++ helper.
  1030. slow_case.link(m_assembler);
  1031. m_assembler.mov(
  1032. Assembler::Operand::Register(ARG1),
  1033. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier()))));
  1034. native_call((void*)cxx_get_variable);
  1035. store_accumulator(RET);
  1036. check_exception();
  1037. end.link(m_assembler);
  1038. }
  1039. 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)
  1040. {
  1041. auto& bytecode_interpreter = vm.bytecode_interpreter();
  1042. auto callee_and_this = TRY_OR_SET_EXCEPTION(Bytecode::get_callee_and_this_from_environment(
  1043. bytecode_interpreter,
  1044. name,
  1045. cache_index));
  1046. bytecode_interpreter.reg(callee_reg) = callee_and_this.callee;
  1047. bytecode_interpreter.reg(this_reg) = callee_and_this.this_value;
  1048. return {};
  1049. }
  1050. void Compiler::compile_get_callee_and_this_from_environment(Bytecode::Op::GetCalleeAndThisFromEnvironment const& op)
  1051. {
  1052. m_assembler.mov(
  1053. Assembler::Operand::Register(ARG1),
  1054. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier()))));
  1055. m_assembler.mov(
  1056. Assembler::Operand::Register(ARG2),
  1057. Assembler::Operand::Imm(op.cache_index()));
  1058. m_assembler.mov(
  1059. Assembler::Operand::Register(ARG3),
  1060. Assembler::Operand::Imm(op.callee().index()));
  1061. m_assembler.mov(
  1062. Assembler::Operand::Register(ARG4),
  1063. Assembler::Operand::Imm(op.this_().index()));
  1064. native_call((void*)cxx_get_callee_and_this_from_environment);
  1065. check_exception();
  1066. }
  1067. static Value cxx_to_numeric(VM& vm, Value value)
  1068. {
  1069. return TRY_OR_SET_EXCEPTION(value.to_numeric(vm));
  1070. }
  1071. void Compiler::compile_to_numeric(Bytecode::Op::ToNumeric const&)
  1072. {
  1073. Assembler::Label fast_case {};
  1074. load_accumulator(ARG1);
  1075. jump_if_int32(ARG1, fast_case);
  1076. native_call((void*)cxx_to_numeric);
  1077. store_accumulator(RET);
  1078. check_exception();
  1079. fast_case.link(m_assembler);
  1080. }
  1081. static Value cxx_resolve_this_binding(VM& vm)
  1082. {
  1083. auto this_value = TRY_OR_SET_EXCEPTION(vm.resolve_this_binding());
  1084. vm.bytecode_interpreter().reg(Bytecode::Register::this_value()) = this_value;
  1085. return this_value;
  1086. }
  1087. void Compiler::compile_resolve_this_binding(Bytecode::Op::ResolveThisBinding const&)
  1088. {
  1089. // OPTIMIZATION: We cache the `this` value in a special VM register.
  1090. // So first we check if the cache is non-empty, and if so,
  1091. // we can avoid calling out to C++ at all. :^)
  1092. load_vm_register(GPR0, Bytecode::Register::this_value());
  1093. m_assembler.mov(
  1094. Assembler::Operand::Register(GPR1),
  1095. Assembler::Operand::Imm(Value().encoded()));
  1096. Assembler::Label slow_case {};
  1097. m_assembler.jump_if(
  1098. Assembler::Operand::Register(GPR0),
  1099. Assembler::Condition::EqualTo,
  1100. Assembler::Operand::Register(GPR1),
  1101. slow_case);
  1102. // Fast case: We have a cached `this` value!
  1103. store_accumulator(GPR0);
  1104. auto end = m_assembler.jump();
  1105. slow_case.link(m_assembler);
  1106. native_call((void*)cxx_resolve_this_binding);
  1107. store_accumulator(RET);
  1108. check_exception();
  1109. end.link(m_assembler);
  1110. }
  1111. static Value cxx_put_by_id(VM& vm, Value base, Bytecode::IdentifierTableIndex property, Value value, Bytecode::Op::PropertyKind kind)
  1112. {
  1113. PropertyKey name = vm.bytecode_interpreter().current_executable().get_identifier(property);
  1114. TRY_OR_SET_EXCEPTION(Bytecode::put_by_property_key(vm, base, base, value, name, kind));
  1115. return value;
  1116. }
  1117. void Compiler::compile_put_by_id(Bytecode::Op::PutById const& op)
  1118. {
  1119. load_vm_register(ARG1, op.base());
  1120. m_assembler.mov(
  1121. Assembler::Operand::Register(ARG2),
  1122. Assembler::Operand::Imm(op.property().value()));
  1123. load_accumulator(ARG3);
  1124. m_assembler.mov(
  1125. Assembler::Operand::Register(ARG4),
  1126. Assembler::Operand::Imm(to_underlying(op.kind())));
  1127. native_call((void*)cxx_put_by_id);
  1128. store_accumulator(RET);
  1129. check_exception();
  1130. }
  1131. static Value cxx_put_by_value(VM& vm, Value base, Value property, Value value, Bytecode::Op::PropertyKind kind)
  1132. {
  1133. TRY_OR_SET_EXCEPTION(Bytecode::put_by_value(vm, base, property, value, kind));
  1134. return value;
  1135. }
  1136. void Compiler::compile_put_by_value(Bytecode::Op::PutByValue const& op)
  1137. {
  1138. load_vm_register(ARG1, op.base());
  1139. load_vm_register(ARG2, op.property());
  1140. load_accumulator(ARG3);
  1141. m_assembler.mov(
  1142. Assembler::Operand::Register(ARG4),
  1143. Assembler::Operand::Imm(to_underlying(op.kind())));
  1144. native_call((void*)cxx_put_by_value);
  1145. store_accumulator(RET);
  1146. check_exception();
  1147. }
  1148. 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)
  1149. {
  1150. TRY_OR_SET_EXCEPTION(throw_if_needed_for_call(vm.bytecode_interpreter(), callee, call_type, expression_string));
  1151. MarkedVector<Value> argument_values(vm.heap());
  1152. argument_values.ensure_capacity(argument_count);
  1153. for (u32 i = 0; i < argument_count; ++i) {
  1154. argument_values.unchecked_append(vm.bytecode_interpreter().reg(Bytecode::Register { first_argument_index + i }));
  1155. }
  1156. return TRY_OR_SET_EXCEPTION(perform_call(vm.bytecode_interpreter(), this_value, call_type, callee, move(argument_values)));
  1157. }
  1158. void Compiler::compile_call(Bytecode::Op::Call const& op)
  1159. {
  1160. load_vm_register(ARG1, op.callee());
  1161. m_assembler.mov(
  1162. Assembler::Operand::Register(ARG2),
  1163. Assembler::Operand::Imm(op.first_argument().index()));
  1164. m_assembler.mov(
  1165. Assembler::Operand::Register(ARG3),
  1166. Assembler::Operand::Imm(op.argument_count()));
  1167. load_vm_register(ARG4, op.this_value());
  1168. m_assembler.mov(
  1169. Assembler::Operand::Register(ARG5),
  1170. Assembler::Operand::Imm(to_underlying(op.call_type())));
  1171. m_assembler.mov(
  1172. Assembler::Operand::Register(GPR0),
  1173. Assembler::Operand::Imm(bit_cast<u64>(&op.expression_string())));
  1174. native_call((void*)cxx_call, { Assembler::Operand::Register(GPR0) });
  1175. store_accumulator(RET);
  1176. check_exception();
  1177. }
  1178. 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)
  1179. {
  1180. TRY_OR_SET_EXCEPTION(throw_if_needed_for_call(vm.bytecode_interpreter(), callee, call_type, expression_string));
  1181. auto argument_values = Bytecode::argument_list_evaluation(vm, arguments);
  1182. return TRY_OR_SET_EXCEPTION(perform_call(vm.bytecode_interpreter(), this_value, call_type, callee, move(argument_values)));
  1183. }
  1184. void Compiler::compile_call_with_argument_array(Bytecode::Op::CallWithArgumentArray const& op)
  1185. {
  1186. load_accumulator(ARG1);
  1187. load_vm_register(ARG2, op.callee());
  1188. load_vm_register(ARG3, op.this_value());
  1189. m_assembler.mov(
  1190. Assembler::Operand::Register(ARG4),
  1191. Assembler::Operand::Imm(to_underlying(op.call_type())));
  1192. m_assembler.mov(
  1193. Assembler::Operand::Register(ARG5),
  1194. Assembler::Operand::Imm(bit_cast<u64>(&op.expression_string())));
  1195. native_call((void*)cxx_call_with_argument_array);
  1196. store_accumulator(RET);
  1197. check_exception();
  1198. }
  1199. static Value cxx_typeof_variable(VM& vm, DeprecatedFlyString const& identifier)
  1200. {
  1201. return TRY_OR_SET_EXCEPTION(Bytecode::typeof_variable(vm, identifier));
  1202. }
  1203. void Compiler::compile_typeof_variable(Bytecode::Op::TypeofVariable const& op)
  1204. {
  1205. m_assembler.mov(
  1206. Assembler::Operand::Register(ARG1),
  1207. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier().value()))));
  1208. native_call((void*)cxx_typeof_variable);
  1209. store_accumulator(RET);
  1210. check_exception();
  1211. }
  1212. static Value cxx_create_variable(
  1213. VM& vm,
  1214. DeprecatedFlyString const& name,
  1215. Bytecode::Op::EnvironmentMode mode,
  1216. bool is_global,
  1217. bool is_immutable,
  1218. bool is_strict)
  1219. {
  1220. TRY_OR_SET_EXCEPTION(Bytecode::create_variable(vm, name, mode, is_global, is_immutable, is_strict));
  1221. return {};
  1222. }
  1223. void Compiler::compile_create_variable(Bytecode::Op::CreateVariable const& op)
  1224. {
  1225. m_assembler.mov(
  1226. Assembler::Operand::Register(ARG1),
  1227. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier().value()))));
  1228. m_assembler.mov(
  1229. Assembler::Operand::Register(ARG2),
  1230. Assembler::Operand::Imm(to_underlying(op.mode())));
  1231. m_assembler.mov(
  1232. Assembler::Operand::Register(ARG3),
  1233. Assembler::Operand::Imm(static_cast<u64>(op.is_global())));
  1234. m_assembler.mov(
  1235. Assembler::Operand::Register(ARG4),
  1236. Assembler::Operand::Imm(static_cast<u64>(op.is_immutable())));
  1237. m_assembler.mov(
  1238. Assembler::Operand::Register(ARG5),
  1239. Assembler::Operand::Imm(static_cast<u64>(op.is_strict())));
  1240. native_call((void*)cxx_create_variable);
  1241. check_exception();
  1242. }
  1243. static Value cxx_set_variable(
  1244. VM& vm,
  1245. DeprecatedFlyString const& identifier,
  1246. Value value,
  1247. Bytecode::Op::EnvironmentMode environment_mode,
  1248. Bytecode::Op::SetVariable::InitializationMode initialization_mode)
  1249. {
  1250. TRY_OR_SET_EXCEPTION(Bytecode::set_variable(vm, identifier, value, environment_mode, initialization_mode));
  1251. return {};
  1252. }
  1253. void Compiler::compile_set_variable(Bytecode::Op::SetVariable const& op)
  1254. {
  1255. m_assembler.mov(
  1256. Assembler::Operand::Register(ARG1),
  1257. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier().value()))));
  1258. load_accumulator(ARG2);
  1259. m_assembler.mov(
  1260. Assembler::Operand::Register(ARG3),
  1261. Assembler::Operand::Imm(to_underlying(op.mode())));
  1262. m_assembler.mov(
  1263. Assembler::Operand::Register(ARG4),
  1264. Assembler::Operand::Imm(to_underlying(op.initialization_mode())));
  1265. native_call((void*)cxx_set_variable);
  1266. check_exception();
  1267. }
  1268. void Compiler::compile_continue_pending_unwind(Bytecode::Op::ContinuePendingUnwind const& op)
  1269. {
  1270. // re-throw the exception if we reached the end of the finally block and there was no catch block to handle it
  1271. load_vm_register(GPR0, Bytecode::Register::saved_exception());
  1272. store_vm_register(Bytecode::Register::exception(), GPR0);
  1273. m_assembler.mov(Assembler::Operand::Register(GPR1), Assembler::Operand::Imm(Value().encoded()));
  1274. store_vm_register(Bytecode::Register::saved_exception(), GPR1);
  1275. check_exception();
  1276. // if (saved_return_value.is_empty()) goto resume_block;
  1277. load_vm_register(GPR0, Bytecode::Register::saved_return_value());
  1278. m_assembler.jump_if(
  1279. Assembler::Operand::Register(GPR0),
  1280. Assembler::Condition::EqualTo,
  1281. Assembler::Operand::Register(GPR1),
  1282. label_for(op.resume_target().block()));
  1283. // finish the pending return from the try block
  1284. store_vm_register(Bytecode::Register::return_value(), GPR0);
  1285. jump_to_exit();
  1286. }
  1287. static void cxx_create_lexical_environment(VM& vm)
  1288. {
  1289. auto make_and_swap_envs = [&](auto& old_environment) {
  1290. GCPtr<Environment> environment = new_declarative_environment(*old_environment).ptr();
  1291. swap(old_environment, environment);
  1292. return environment;
  1293. };
  1294. vm.bytecode_interpreter().saved_lexical_environment_stack().append(make_and_swap_envs(vm.running_execution_context().lexical_environment));
  1295. }
  1296. void Compiler::compile_create_lexical_environment(Bytecode::Op::CreateLexicalEnvironment const&)
  1297. {
  1298. native_call((void*)cxx_create_lexical_environment);
  1299. }
  1300. static void cxx_leave_lexical_environment(VM& vm)
  1301. {
  1302. vm.running_execution_context().lexical_environment = vm.bytecode_interpreter().saved_lexical_environment_stack().take_last();
  1303. }
  1304. void Compiler::compile_leave_lexical_environment(Bytecode::Op::LeaveLexicalEnvironment const&)
  1305. {
  1306. native_call((void*)cxx_leave_lexical_environment);
  1307. }
  1308. static Value cxx_concat_string(VM& vm, Value lhs, Value rhs)
  1309. {
  1310. auto string = TRY_OR_SET_EXCEPTION(rhs.to_primitive_string(vm));
  1311. return PrimitiveString::create(vm, lhs.as_string(), string);
  1312. }
  1313. void Compiler::compile_concat_string(Bytecode::Op::ConcatString const& op)
  1314. {
  1315. load_vm_register(ARG1, op.lhs());
  1316. load_accumulator(ARG2);
  1317. native_call((void*)cxx_concat_string);
  1318. store_vm_register(op.lhs(), RET);
  1319. check_exception();
  1320. }
  1321. static void cxx_block_declaration_instantiation(VM& vm, ScopeNode const& scope_node)
  1322. {
  1323. auto old_environment = vm.running_execution_context().lexical_environment;
  1324. vm.bytecode_interpreter().saved_lexical_environment_stack().append(old_environment);
  1325. vm.running_execution_context().lexical_environment = new_declarative_environment(*old_environment);
  1326. scope_node.block_declaration_instantiation(vm, vm.running_execution_context().lexical_environment);
  1327. }
  1328. void Compiler::compile_block_declaration_instantiation(Bytecode::Op::BlockDeclarationInstantiation const& op)
  1329. {
  1330. m_assembler.mov(
  1331. Assembler::Operand::Register(ARG1),
  1332. Assembler::Operand::Imm(bit_cast<u64>(&op.scope_node())));
  1333. native_call((void*)cxx_block_declaration_instantiation);
  1334. }
  1335. static Value cxx_super_call_with_argument_array(VM& vm, Value argument_array, bool is_synthetic)
  1336. {
  1337. return TRY_OR_SET_EXCEPTION(Bytecode::super_call_with_argument_array(vm, argument_array, is_synthetic));
  1338. }
  1339. void Compiler::compile_super_call_with_argument_array(Bytecode::Op::SuperCallWithArgumentArray const& op)
  1340. {
  1341. load_accumulator(ARG1);
  1342. m_assembler.mov(
  1343. Assembler::Operand::Register(ARG2),
  1344. Assembler::Operand::Imm(static_cast<u64>(op.is_synthetic())));
  1345. native_call((void*)cxx_super_call_with_argument_array);
  1346. store_accumulator(RET);
  1347. check_exception();
  1348. }
  1349. static Value cxx_get_iterator(VM& vm, Value value, IteratorHint hint)
  1350. {
  1351. auto iterator = TRY_OR_SET_EXCEPTION(get_iterator(vm, value, hint));
  1352. return Bytecode::iterator_to_object(vm, iterator);
  1353. }
  1354. void Compiler::compile_get_iterator(Bytecode::Op::GetIterator const& op)
  1355. {
  1356. load_accumulator(ARG1);
  1357. m_assembler.mov(
  1358. Assembler::Operand::Register(ARG2),
  1359. Assembler::Operand::Imm(to_underlying(op.hint())));
  1360. native_call((void*)cxx_get_iterator);
  1361. store_accumulator(RET);
  1362. check_exception();
  1363. }
  1364. static Value cxx_iterator_next(VM& vm, Value iterator)
  1365. {
  1366. auto iterator_object = TRY_OR_SET_EXCEPTION(iterator.to_object(vm));
  1367. auto iterator_record = Bytecode::object_to_iterator(vm, iterator_object);
  1368. return TRY_OR_SET_EXCEPTION(iterator_next(vm, iterator_record));
  1369. }
  1370. void Compiler::compile_iterator_next(Bytecode::Op::IteratorNext const&)
  1371. {
  1372. load_accumulator(ARG1);
  1373. native_call((void*)cxx_iterator_next);
  1374. store_accumulator(RET);
  1375. check_exception();
  1376. }
  1377. static Value cxx_iterator_result_done(VM& vm, Value iterator)
  1378. {
  1379. auto iterator_result = TRY_OR_SET_EXCEPTION(iterator.to_object(vm));
  1380. return Value(TRY_OR_SET_EXCEPTION(iterator_complete(vm, iterator_result)));
  1381. }
  1382. void Compiler::compile_iterator_result_done(Bytecode::Op::IteratorResultDone const&)
  1383. {
  1384. load_accumulator(ARG1);
  1385. native_call((void*)cxx_iterator_result_done);
  1386. store_accumulator(RET);
  1387. check_exception();
  1388. }
  1389. static Value cxx_throw_if_not_object(VM& vm, Value value)
  1390. {
  1391. if (!value.is_object())
  1392. TRY_OR_SET_EXCEPTION(vm.throw_completion<TypeError>(ErrorType::NotAnObject, value.to_string_without_side_effects()));
  1393. return {};
  1394. }
  1395. void Compiler::compile_throw_if_not_object(Bytecode::Op::ThrowIfNotObject const&)
  1396. {
  1397. load_accumulator(ARG1);
  1398. native_call((void*)cxx_throw_if_not_object);
  1399. check_exception();
  1400. }
  1401. static Value cxx_throw_if_nullish(VM& vm, Value value)
  1402. {
  1403. if (value.is_nullish())
  1404. TRY_OR_SET_EXCEPTION(vm.throw_completion<TypeError>(ErrorType::NotObjectCoercible, value.to_string_without_side_effects()));
  1405. return {};
  1406. }
  1407. void Compiler::compile_throw_if_nullish(Bytecode::Op::ThrowIfNullish const&)
  1408. {
  1409. load_accumulator(ARG1);
  1410. native_call((void*)cxx_throw_if_nullish);
  1411. check_exception();
  1412. }
  1413. static Value cxx_iterator_result_value(VM& vm, Value iterator)
  1414. {
  1415. auto iterator_result = TRY_OR_SET_EXCEPTION(iterator.to_object(vm));
  1416. return TRY_OR_SET_EXCEPTION(iterator_value(vm, iterator_result));
  1417. }
  1418. void Compiler::compile_iterator_result_value(Bytecode::Op::IteratorResultValue const&)
  1419. {
  1420. load_accumulator(ARG1);
  1421. native_call((void*)cxx_iterator_result_value);
  1422. store_accumulator(RET);
  1423. check_exception();
  1424. }
  1425. static Value cxx_iterator_close(VM& vm, Value iterator, Completion::Type completion_type, Optional<Value> const& completion_value)
  1426. {
  1427. auto iterator_object = TRY_OR_SET_EXCEPTION(iterator.to_object(vm));
  1428. auto iterator_record = Bytecode::object_to_iterator(vm, iterator_object);
  1429. // FIXME: Return the value of the resulting completion. (Note that m_completion_value can be empty!)
  1430. TRY_OR_SET_EXCEPTION(iterator_close(vm, iterator_record, Completion { completion_type, completion_value, {} }));
  1431. return {};
  1432. }
  1433. void Compiler::compile_iterator_close(Bytecode::Op::IteratorClose const& op)
  1434. {
  1435. load_accumulator(ARG1);
  1436. m_assembler.mov(
  1437. Assembler::Operand::Register(ARG2),
  1438. Assembler::Operand::Imm(to_underlying(op.completion_type())));
  1439. m_assembler.mov(
  1440. Assembler::Operand::Register(ARG3),
  1441. Assembler::Operand::Imm(bit_cast<u64>(&op.completion_value())));
  1442. native_call((void*)cxx_iterator_close);
  1443. check_exception();
  1444. }
  1445. static Value iterator_to_array(VM& vm, Value iterator)
  1446. {
  1447. return TRY_OR_SET_EXCEPTION(Bytecode::iterator_to_array(vm, iterator));
  1448. }
  1449. void Compiler::compile_iterator_to_array(Bytecode::Op::IteratorToArray const&)
  1450. {
  1451. load_accumulator(ARG1);
  1452. native_call((void*)iterator_to_array);
  1453. store_accumulator(RET);
  1454. check_exception();
  1455. }
  1456. static Value cxx_append(VM& vm, Value lhs, Value rhs, bool is_spread)
  1457. {
  1458. TRY_OR_SET_EXCEPTION(Bytecode::append(vm, lhs, rhs, is_spread));
  1459. return {};
  1460. }
  1461. void Compiler::compile_append(Bytecode::Op::Append const& op)
  1462. {
  1463. load_vm_register(ARG1, op.lhs());
  1464. load_accumulator(ARG2);
  1465. m_assembler.mov(
  1466. Assembler::Operand::Register(ARG3),
  1467. Assembler::Operand::Imm(static_cast<u64>(op.is_spread())));
  1468. native_call((void*)cxx_append);
  1469. check_exception();
  1470. }
  1471. static Value cxx_delete_by_id(VM& vm, Value base, Bytecode::IdentifierTableIndex property)
  1472. {
  1473. return TRY_OR_SET_EXCEPTION(Bytecode::delete_by_id(vm.bytecode_interpreter(), base, property));
  1474. }
  1475. void Compiler::compile_delete_by_id(Bytecode::Op::DeleteById const& op)
  1476. {
  1477. load_accumulator(ARG1);
  1478. m_assembler.mov(
  1479. Assembler::Operand::Register(ARG2),
  1480. Assembler::Operand::Imm(op.property().value()));
  1481. native_call((void*)cxx_delete_by_id);
  1482. store_accumulator(RET);
  1483. check_exception();
  1484. }
  1485. static Value cxx_delete_by_value(VM& vm, Value base_value, Value property_key_value)
  1486. {
  1487. return TRY_OR_SET_EXCEPTION(Bytecode::delete_by_value(vm.bytecode_interpreter(), base_value, property_key_value));
  1488. }
  1489. void Compiler::compile_delete_by_value(Bytecode::Op::DeleteByValue const& op)
  1490. {
  1491. load_vm_register(ARG1, op.base());
  1492. load_accumulator(ARG2);
  1493. native_call((void*)cxx_delete_by_value);
  1494. store_accumulator(RET);
  1495. check_exception();
  1496. }
  1497. static Value cxx_delete_by_value_with_this(VM& vm, Value base_value, Value property_key_value, Value this_value)
  1498. {
  1499. return TRY_OR_SET_EXCEPTION(Bytecode::delete_by_value_with_this(vm.bytecode_interpreter(), base_value, property_key_value, this_value));
  1500. }
  1501. void Compiler::compile_delete_by_value_with_this(Bytecode::Op::DeleteByValueWithThis const& op)
  1502. {
  1503. load_vm_register(ARG1, op.base());
  1504. load_accumulator(ARG2);
  1505. load_vm_register(ARG3, op.this_value());
  1506. native_call((void*)cxx_delete_by_value_with_this);
  1507. store_accumulator(RET);
  1508. check_exception();
  1509. }
  1510. static Value cxx_get_object_property_iterator(VM& vm, Value object)
  1511. {
  1512. return TRY_OR_SET_EXCEPTION(Bytecode::get_object_property_iterator(vm, object));
  1513. }
  1514. void Compiler::compile_get_object_property_iterator(Bytecode::Op::GetObjectPropertyIterator const&)
  1515. {
  1516. load_accumulator(ARG1);
  1517. native_call((void*)cxx_get_object_property_iterator);
  1518. store_accumulator(RET);
  1519. check_exception();
  1520. }
  1521. static Value cxx_get_private_by_id(VM& vm, Value base_value, DeprecatedFlyString& name)
  1522. {
  1523. auto private_reference = make_private_reference(vm, base_value, name);
  1524. return TRY_OR_SET_EXCEPTION(private_reference.get_value(vm));
  1525. }
  1526. void Compiler::compile_get_private_by_id(Bytecode::Op::GetPrivateById const& op)
  1527. {
  1528. load_accumulator(ARG1);
  1529. m_assembler.mov(
  1530. Assembler::Operand::Register(ARG2),
  1531. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1532. native_call((void*)cxx_get_private_by_id);
  1533. store_accumulator(RET);
  1534. check_exception();
  1535. }
  1536. static Value cxx_resolve_super_base(VM& vm)
  1537. {
  1538. // 1. Let env be GetThisEnvironment().
  1539. auto& env = verify_cast<FunctionEnvironment>(*get_this_environment(vm));
  1540. // 2. Assert: env.HasSuperBinding() is true.
  1541. VERIFY(env.has_super_binding());
  1542. // 3. Let baseValue be ? env.GetSuperBase().
  1543. return TRY_OR_SET_EXCEPTION(env.get_super_base());
  1544. }
  1545. void Compiler::compile_resolve_super_base(Bytecode::Op::ResolveSuperBase const&)
  1546. {
  1547. native_call((void*)cxx_resolve_super_base);
  1548. store_accumulator(RET);
  1549. check_exception();
  1550. }
  1551. static Value cxx_get_by_id_with_this(VM& vm, DeprecatedFlyString const& property, Value base_value, Value this_value, Bytecode::PropertyLookupCache& cache)
  1552. {
  1553. return TRY_OR_SET_EXCEPTION(Bytecode::get_by_id(vm, property, base_value, this_value, cache));
  1554. }
  1555. void Compiler::compile_get_by_id_with_this(Bytecode::Op::GetByIdWithThis const& op)
  1556. {
  1557. m_assembler.mov(
  1558. Assembler::Operand::Register(ARG1),
  1559. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1560. load_accumulator(ARG2);
  1561. load_vm_register(ARG3, op.this_value());
  1562. m_assembler.mov(
  1563. Assembler::Operand::Register(ARG4),
  1564. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.property_lookup_caches[op.cache_index()])));
  1565. native_call((void*)cxx_get_by_id_with_this);
  1566. store_accumulator(RET);
  1567. check_exception();
  1568. }
  1569. static Value cxx_get_by_value_with_this(VM& vm, Value property_key_value, Value base, Value this_value)
  1570. {
  1571. auto object = TRY_OR_SET_EXCEPTION(base.to_object(vm));
  1572. auto property_key = TRY_OR_SET_EXCEPTION(property_key_value.to_property_key(vm));
  1573. return TRY_OR_SET_EXCEPTION(object->internal_get(property_key, this_value));
  1574. }
  1575. void Compiler::compile_get_by_value_with_this(Bytecode::Op::GetByValueWithThis const& op)
  1576. {
  1577. load_accumulator(ARG1);
  1578. load_vm_register(ARG2, op.base());
  1579. load_vm_register(ARG3, op.this_value());
  1580. native_call((void*)cxx_get_by_value_with_this);
  1581. store_accumulator(RET);
  1582. check_exception();
  1583. }
  1584. static Value cxx_delete_by_id_with_this(VM& vm, Value base_value, DeprecatedFlyString const& identifier, Value this_value)
  1585. {
  1586. auto reference = Reference { base_value, identifier, this_value, vm.in_strict_mode() };
  1587. return Value(TRY_OR_SET_EXCEPTION(reference.delete_(vm)));
  1588. }
  1589. void Compiler::compile_delete_by_id_with_this(Bytecode::Op::DeleteByIdWithThis const& op)
  1590. {
  1591. load_accumulator(ARG1);
  1592. m_assembler.mov(
  1593. Assembler::Operand::Register(ARG2),
  1594. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1595. load_vm_register(ARG3, op.this_value());
  1596. native_call((void*)cxx_delete_by_id_with_this);
  1597. store_accumulator(RET);
  1598. }
  1599. static Value cxx_put_by_id_with_this(VM& vm, Value base, Value value, DeprecatedFlyString const& name, Value this_value, Bytecode::Op::PropertyKind kind)
  1600. {
  1601. TRY_OR_SET_EXCEPTION(Bytecode::put_by_property_key(vm, base, this_value, value, name, kind));
  1602. return {};
  1603. }
  1604. void Compiler::compile_put_by_id_with_this(Bytecode::Op::PutByIdWithThis const& op)
  1605. {
  1606. load_vm_register(ARG1, op.base());
  1607. load_accumulator(ARG2);
  1608. m_assembler.mov(
  1609. Assembler::Operand::Register(ARG3),
  1610. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1611. load_vm_register(ARG4, op.this_value());
  1612. m_assembler.mov(
  1613. Assembler::Operand::Register(ARG5),
  1614. Assembler::Operand::Imm(to_underlying(op.kind())));
  1615. native_call((void*)cxx_put_by_id_with_this);
  1616. check_exception();
  1617. }
  1618. static Value cxx_put_private_by_id(VM& vm, Value base, Value value, DeprecatedFlyString const& name)
  1619. {
  1620. auto object = TRY_OR_SET_EXCEPTION(base.to_object(vm));
  1621. auto private_reference = make_private_reference(vm, object, name);
  1622. TRY_OR_SET_EXCEPTION(private_reference.put_value(vm, value));
  1623. return value;
  1624. }
  1625. void Compiler::compile_put_private_by_id(Bytecode::Op::PutPrivateById const& op)
  1626. {
  1627. load_vm_register(ARG1, op.base());
  1628. load_accumulator(ARG2);
  1629. m_assembler.mov(
  1630. Assembler::Operand::Register(ARG3),
  1631. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1632. native_call((void*)cxx_put_private_by_id);
  1633. store_accumulator(RET);
  1634. check_exception();
  1635. }
  1636. static Value cxx_import_call(VM& vm, Value specifier, Value options)
  1637. {
  1638. return TRY_OR_SET_EXCEPTION(perform_import_call(vm, specifier, options));
  1639. }
  1640. void Compiler::compile_import_call(Bytecode::Op::ImportCall const& op)
  1641. {
  1642. load_vm_register(ARG1, op.specifier());
  1643. load_vm_register(ARG2, op.options());
  1644. native_call((void*)cxx_import_call);
  1645. store_accumulator(RET);
  1646. check_exception();
  1647. }
  1648. static Value cxx_get_import_meta(VM& vm)
  1649. {
  1650. return vm.get_import_meta();
  1651. }
  1652. void Compiler::compile_get_import_meta(Bytecode::Op::GetImportMeta const&)
  1653. {
  1654. native_call((void*)cxx_get_import_meta);
  1655. store_accumulator(RET);
  1656. }
  1657. static Value cxx_delete_variable(VM& vm, DeprecatedFlyString const& identifier)
  1658. {
  1659. auto reference = TRY_OR_SET_EXCEPTION(vm.resolve_binding(identifier));
  1660. return Value(TRY_OR_SET_EXCEPTION(reference.delete_(vm)));
  1661. }
  1662. void Compiler::compile_delete_variable(Bytecode::Op::DeleteVariable const& op)
  1663. {
  1664. m_assembler.mov(
  1665. Assembler::Operand::Register(ARG1),
  1666. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier().value()))));
  1667. native_call((void*)cxx_delete_variable);
  1668. store_accumulator(RET);
  1669. check_exception();
  1670. }
  1671. static Value cxx_get_method(VM& vm, Value value, DeprecatedFlyString const& identifier)
  1672. {
  1673. auto method = TRY_OR_SET_EXCEPTION(value.get_method(vm, identifier));
  1674. return method ?: js_undefined();
  1675. }
  1676. void Compiler::compile_get_method(Bytecode::Op::GetMethod const& op)
  1677. {
  1678. load_accumulator(ARG1);
  1679. m_assembler.mov(
  1680. Assembler::Operand::Register(ARG2),
  1681. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1682. native_call((void*)cxx_get_method);
  1683. store_accumulator(RET);
  1684. check_exception();
  1685. }
  1686. static Value cxx_get_new_target(VM& vm)
  1687. {
  1688. return vm.get_new_target();
  1689. }
  1690. void Compiler::compile_get_new_target(Bytecode::Op::GetNewTarget const&)
  1691. {
  1692. native_call((void*)cxx_get_new_target);
  1693. store_accumulator(RET);
  1694. }
  1695. static Value cxx_has_private_id(VM& vm, Value object, DeprecatedFlyString const& identifier)
  1696. {
  1697. if (!object.is_object())
  1698. TRY_OR_SET_EXCEPTION(vm.throw_completion<TypeError>(ErrorType::InOperatorWithObject));
  1699. auto private_environment = vm.running_execution_context().private_environment;
  1700. VERIFY(private_environment);
  1701. auto private_name = private_environment->resolve_private_identifier(identifier);
  1702. return Value(object.as_object().private_element_find(private_name) != nullptr);
  1703. }
  1704. void Compiler::compile_has_private_id(Bytecode::Op::HasPrivateId const& op)
  1705. {
  1706. load_accumulator(ARG1);
  1707. m_assembler.mov(
  1708. Assembler::Operand::Register(ARG2),
  1709. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.property()))));
  1710. native_call((void*)cxx_has_private_id);
  1711. store_accumulator(RET);
  1712. check_exception();
  1713. }
  1714. # define COMPILE_NEW_BUILTIN_ERROR_OP(NewErrorName, new_error_name, ErrorName) \
  1715. static Value cxx_##new_error_name(VM& vm, DeprecatedString const& error_string) \
  1716. { \
  1717. return ErrorName::create(*vm.current_realm(), error_string); \
  1718. } \
  1719. \
  1720. void Compiler::compile_##new_error_name(Bytecode::Op::NewErrorName const& op) \
  1721. { \
  1722. m_assembler.mov( \
  1723. Assembler::Operand::Register(ARG1), \
  1724. Assembler::Operand::Imm(bit_cast<u64>(&m_bytecode_executable.get_string(op.error_string())))); \
  1725. native_call((void*)cxx_##new_error_name); \
  1726. store_accumulator(RET); \
  1727. }
  1728. JS_ENUMERATE_NEW_BUILTIN_ERROR_BYTECODE_OPS(COMPILE_NEW_BUILTIN_ERROR_OP)
  1729. # undef COMPILE_NEW_BUILTIN_ERROR_OP
  1730. static Value cxx_put_by_value_with_this(VM& vm, Value base, Value value, Value name, Value this_value, Bytecode::Op::PropertyKind kind)
  1731. {
  1732. auto property_key = kind != Bytecode::Op::PropertyKind::Spread ? TRY_OR_SET_EXCEPTION(name.to_property_key(vm)) : PropertyKey {};
  1733. TRY_OR_SET_EXCEPTION(Bytecode::put_by_property_key(vm, base, this_value, value, property_key, kind));
  1734. return value;
  1735. }
  1736. void Compiler::compile_put_by_value_with_this(Bytecode::Op::PutByValueWithThis const& op)
  1737. {
  1738. load_vm_register(ARG1, op.base());
  1739. load_accumulator(ARG2);
  1740. if (op.kind() != Bytecode::Op::PropertyKind::Spread) {
  1741. load_vm_register(ARG3, op.property());
  1742. } else {
  1743. m_assembler.mov(
  1744. Assembler::Operand::Register(ARG3),
  1745. Assembler::Operand::Imm(Value().encoded()));
  1746. }
  1747. load_vm_register(ARG4, op.this_value());
  1748. m_assembler.mov(
  1749. Assembler::Operand::Register(ARG5),
  1750. Assembler::Operand::Imm(to_underlying(op.kind())));
  1751. native_call((void*)cxx_put_by_value_with_this);
  1752. store_accumulator(RET);
  1753. check_exception();
  1754. }
  1755. static Value cxx_copy_object_excluding_properties(VM& vm, Value from_object, u64 excluded_names_count, Value* excluded_names)
  1756. {
  1757. auto& realm = *vm.current_realm();
  1758. auto to_object = Object::create(realm, realm.intrinsics().object_prototype());
  1759. HashTable<PropertyKey> excluded_names_table;
  1760. for (size_t i = 0; i < excluded_names_count; ++i) {
  1761. excluded_names_table.set(TRY_OR_SET_EXCEPTION(excluded_names[i].to_property_key(vm)));
  1762. }
  1763. TRY_OR_SET_EXCEPTION(to_object->copy_data_properties(vm, from_object, excluded_names_table));
  1764. return to_object;
  1765. }
  1766. void Compiler::compile_copy_object_excluding_properties(Bytecode::Op::CopyObjectExcludingProperties const& op)
  1767. {
  1768. load_vm_register(ARG1, op.from_object());
  1769. m_assembler.mov(
  1770. Assembler::Operand::Register(ARG2),
  1771. Assembler::Operand::Imm(op.excluded_names_count()));
  1772. // Build `Value arg3[op.excluded_names_count()] {...}` on the stack.
  1773. auto stack_space = align_up_to(op.excluded_names_count() * sizeof(Value), 16);
  1774. m_assembler.sub(Assembler::Operand::Register(STACK_POINTER), Assembler::Operand::Imm(stack_space));
  1775. m_assembler.mov(Assembler::Operand::Register(ARG3), Assembler::Operand::Register(STACK_POINTER));
  1776. for (size_t i = 0; i < op.excluded_names_count(); ++i) {
  1777. load_vm_register(GPR0, op.excluded_names()[i]);
  1778. m_assembler.mov(Assembler::Operand::Mem64BaseAndOffset(ARG3, i * sizeof(Value)), Assembler::Operand::Register(GPR0));
  1779. }
  1780. native_call((void*)cxx_copy_object_excluding_properties);
  1781. // Restore the stack pointer / discard array.
  1782. m_assembler.add(Assembler::Operand::Register(STACK_POINTER), Assembler::Operand::Imm(stack_space));
  1783. store_accumulator(RET);
  1784. check_exception();
  1785. }
  1786. static Value cxx_async_iterator_close(VM& vm, Value iterator, Completion::Type completion_type, Optional<Value> const& completion_value)
  1787. {
  1788. auto iterator_object = TRY_OR_SET_EXCEPTION(iterator.to_object(vm));
  1789. auto iterator_record = Bytecode::object_to_iterator(vm, iterator_object);
  1790. // FIXME: Return the value of the resulting completion. (Note that completion_value can be empty!)
  1791. TRY_OR_SET_EXCEPTION(async_iterator_close(vm, iterator_record, Completion { completion_type, completion_value, {} }));
  1792. return {};
  1793. }
  1794. void Compiler::compile_async_iterator_close(Bytecode::Op::AsyncIteratorClose const& op)
  1795. {
  1796. load_accumulator(ARG1);
  1797. m_assembler.mov(
  1798. Assembler::Operand::Register(ARG2),
  1799. Assembler::Operand::Imm(to_underlying(op.completion_type())));
  1800. m_assembler.mov(
  1801. Assembler::Operand::Register(ARG3),
  1802. Assembler::Operand::Imm(bit_cast<u64>(&op.completion_value())));
  1803. native_call((void*)cxx_async_iterator_close);
  1804. check_exception();
  1805. }
  1806. static Value cxx_continuation(VM& vm, Value value, Value continuation, Value is_await)
  1807. {
  1808. auto object = Object::create(*vm.current_realm(), nullptr);
  1809. object->define_direct_property("result", value.value_or(js_undefined()), JS::default_attributes);
  1810. object->define_direct_property("continuation", continuation, JS::default_attributes);
  1811. object->define_direct_property("isAwait", is_await, JS::default_attributes);
  1812. return object;
  1813. }
  1814. void Compiler::compile_continuation(Optional<Bytecode::Label> continuation, bool is_await)
  1815. {
  1816. load_accumulator(ARG1);
  1817. if (continuation.has_value()) {
  1818. // FIXME: If we get a pointer, which is not accurately representable as a double
  1819. // will cause this to explode
  1820. auto continuation_value = Value(static_cast<double>(bit_cast<u64>(&continuation->block())));
  1821. m_assembler.mov(
  1822. Assembler::Operand::Register(ARG2),
  1823. Assembler::Operand::Imm(continuation_value.encoded()));
  1824. } else {
  1825. m_assembler.mov(
  1826. Assembler::Operand::Register(ARG2),
  1827. Assembler::Operand::Imm(Value(0).encoded()));
  1828. }
  1829. m_assembler.mov(
  1830. Assembler::Operand::Register(ARG3),
  1831. Assembler::Operand::Imm(Value(is_await).encoded()));
  1832. native_call((void*)cxx_continuation);
  1833. store_vm_register(Bytecode::Register::return_value(), RET);
  1834. // FIXME: This should run the finalizer if it is a return
  1835. jump_to_exit();
  1836. }
  1837. void Compiler::compile_yield(Bytecode::Op::Yield const& op)
  1838. {
  1839. compile_continuation(op.continuation(), false);
  1840. }
  1841. void Compiler::compile_await(Bytecode::Op::Await const& op)
  1842. {
  1843. compile_continuation(op.continuation(), true);
  1844. }
  1845. void Compiler::jump_to_exit()
  1846. {
  1847. m_assembler.jump(m_exit_label);
  1848. }
  1849. void Compiler::native_call(void* function_address, Vector<Assembler::Operand> const& stack_arguments)
  1850. {
  1851. // NOTE: We don't preserve caller-saved registers when making a native call.
  1852. // This means that they may have changed after we return from the call.
  1853. m_assembler.native_call(bit_cast<u64>(function_address), { Assembler::Operand::Register(ARG0) }, stack_arguments);
  1854. }
  1855. OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_executable)
  1856. {
  1857. if (!getenv("LIBJS_JIT"))
  1858. return nullptr;
  1859. Compiler compiler { bytecode_executable };
  1860. Vector<BytecodeMapping> mapping;
  1861. mapping.append({
  1862. .native_offset = compiler.m_output.size(),
  1863. .block_index = BytecodeMapping::EXECUTABLE,
  1864. .bytecode_offset = 0,
  1865. });
  1866. compiler.m_assembler.enter();
  1867. compiler.m_assembler.mov(
  1868. Assembler::Operand::Register(REGISTER_ARRAY_BASE),
  1869. Assembler::Operand::Register(ARG1));
  1870. compiler.m_assembler.mov(
  1871. Assembler::Operand::Register(LOCALS_ARRAY_BASE),
  1872. Assembler::Operand::Register(ARG2));
  1873. compiler.m_assembler.mov(
  1874. Assembler::Operand::Register(RUNNING_EXECUTION_CONTEXT_BASE),
  1875. Assembler::Operand::Register(ARG4));
  1876. compiler.reload_cached_accumulator();
  1877. Assembler::Label normal_entry {};
  1878. compiler.m_assembler.jump_if(
  1879. Assembler::Operand::Register(ARG3),
  1880. Assembler::Condition::EqualTo,
  1881. Assembler::Operand::Imm(0),
  1882. normal_entry);
  1883. compiler.m_assembler.jump(Assembler::Operand::Register(ARG3));
  1884. normal_entry.link(compiler.m_assembler);
  1885. for (size_t block_index = 0; block_index < bytecode_executable.basic_blocks.size(); block_index++) {
  1886. auto& block = bytecode_executable.basic_blocks[block_index];
  1887. compiler.block_data_for(*block).start_offset = compiler.m_output.size();
  1888. compiler.set_current_block(*block);
  1889. auto it = Bytecode::InstructionStreamIterator(block->instruction_stream());
  1890. if (it.at_end()) {
  1891. mapping.append({
  1892. .native_offset = compiler.m_output.size(),
  1893. .block_index = block_index,
  1894. .bytecode_offset = 0,
  1895. });
  1896. }
  1897. while (!it.at_end()) {
  1898. auto const& op = *it;
  1899. mapping.append({
  1900. .native_offset = compiler.m_output.size(),
  1901. .block_index = block_index,
  1902. .bytecode_offset = it.offset(),
  1903. });
  1904. switch (op.type()) {
  1905. # define CASE_BYTECODE_OP(OpTitleCase, op_snake_case, ...) \
  1906. case Bytecode::Instruction::Type::OpTitleCase: \
  1907. compiler.compile_##op_snake_case(static_cast<Bytecode::Op::OpTitleCase const&>(op)); \
  1908. break;
  1909. JS_ENUMERATE_IMPLEMENTED_JIT_OPS(CASE_BYTECODE_OP)
  1910. # undef CASE_BYTECODE_OP
  1911. default:
  1912. if constexpr (LOG_JIT_FAILURE) {
  1913. dbgln("\033[31;1mJIT compilation failed\033[0m: {}", bytecode_executable.name);
  1914. dbgln("Unsupported bytecode op: {}", op.to_deprecated_string(bytecode_executable));
  1915. }
  1916. return nullptr;
  1917. }
  1918. ++it;
  1919. }
  1920. if (!block->is_terminated())
  1921. compiler.jump_to_exit();
  1922. }
  1923. mapping.append({
  1924. .native_offset = compiler.m_output.size(),
  1925. .block_index = BytecodeMapping::EXECUTABLE,
  1926. .bytecode_offset = 1,
  1927. });
  1928. compiler.m_exit_label.link(compiler.m_assembler);
  1929. compiler.flush_cached_accumulator();
  1930. compiler.m_assembler.exit();
  1931. auto* executable_memory = mmap(nullptr, compiler.m_output.size(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  1932. if (executable_memory == MAP_FAILED) {
  1933. dbgln("mmap: {}", strerror(errno));
  1934. return nullptr;
  1935. }
  1936. for (auto& block : bytecode_executable.basic_blocks) {
  1937. auto& block_data = compiler.block_data_for(*block);
  1938. block_data.label.link_to(compiler.m_assembler, block_data.start_offset);
  1939. }
  1940. if constexpr (DUMP_JIT_MACHINE_CODE_TO_STDOUT) {
  1941. (void)write(STDOUT_FILENO, compiler.m_output.data(), compiler.m_output.size());
  1942. }
  1943. memcpy(executable_memory, compiler.m_output.data(), compiler.m_output.size());
  1944. if (mprotect(executable_memory, compiler.m_output.size(), PROT_READ | PROT_EXEC) < 0) {
  1945. dbgln("mprotect: {}", strerror(errno));
  1946. return nullptr;
  1947. }
  1948. if constexpr (LOG_JIT_SUCCESS) {
  1949. dbgln("\033[32;1mJIT compilation succeeded!\033[0m {}", bytecode_executable.name);
  1950. }
  1951. auto executable = make<NativeExecutable>(executable_memory, compiler.m_output.size(), mapping);
  1952. if constexpr (DUMP_JIT_DISASSEMBLY)
  1953. executable->dump_disassembly(bytecode_executable);
  1954. return executable;
  1955. }
  1956. }
  1957. #endif