Compiler.cpp 100 KB

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