Compiler.cpp 71 KB

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