Compiler.cpp 71 KB

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