Op.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibJS/AST.h>
  9. #include <LibJS/Bytecode/Interpreter.h>
  10. #include <LibJS/Bytecode/Op.h>
  11. #include <LibJS/Runtime/Array.h>
  12. #include <LibJS/Runtime/BigInt.h>
  13. #include <LibJS/Runtime/GlobalObject.h>
  14. #include <LibJS/Runtime/LexicalEnvironment.h>
  15. #include <LibJS/Runtime/ScopeObject.h>
  16. #include <LibJS/Runtime/ScriptFunction.h>
  17. #include <LibJS/Runtime/Value.h>
  18. namespace JS::Bytecode {
  19. String Instruction::to_string(Bytecode::Executable const& executable) const
  20. {
  21. #define __BYTECODE_OP(op) \
  22. case Instruction::Type::op: \
  23. return static_cast<Bytecode::Op::op const&>(*this).to_string_impl(executable);
  24. switch (type()) {
  25. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  26. default:
  27. VERIFY_NOT_REACHED();
  28. }
  29. #undef __BYTECODE_OP
  30. }
  31. }
  32. namespace JS::Bytecode::Op {
  33. void Load::execute_impl(Bytecode::Interpreter& interpreter) const
  34. {
  35. interpreter.accumulator() = interpreter.reg(m_src);
  36. }
  37. void LoadImmediate::execute_impl(Bytecode::Interpreter& interpreter) const
  38. {
  39. interpreter.accumulator() = m_value;
  40. }
  41. void Store::execute_impl(Bytecode::Interpreter& interpreter) const
  42. {
  43. interpreter.reg(m_dst) = interpreter.accumulator();
  44. }
  45. static Value abstract_inequals(GlobalObject& global_object, Value src1, Value src2)
  46. {
  47. return Value(!abstract_eq(global_object, src1, src2));
  48. }
  49. static Value abstract_equals(GlobalObject& global_object, Value src1, Value src2)
  50. {
  51. return Value(abstract_eq(global_object, src1, src2));
  52. }
  53. static Value typed_inequals(GlobalObject&, Value src1, Value src2)
  54. {
  55. return Value(!strict_eq(src1, src2));
  56. }
  57. static Value typed_equals(GlobalObject&, Value src1, Value src2)
  58. {
  59. return Value(strict_eq(src1, src2));
  60. }
  61. #define JS_DEFINE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  62. void OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  63. { \
  64. auto lhs = interpreter.reg(m_lhs_reg); \
  65. auto rhs = interpreter.accumulator(); \
  66. interpreter.accumulator() = op_snake_case(interpreter.global_object(), lhs, rhs); \
  67. } \
  68. String OpTitleCase::to_string_impl(Bytecode::Executable const&) const \
  69. { \
  70. return String::formatted(#OpTitleCase " {}", m_lhs_reg); \
  71. }
  72. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DEFINE_COMMON_BINARY_OP)
  73. static Value not_(GlobalObject&, Value value)
  74. {
  75. return Value(!value.to_boolean());
  76. }
  77. static Value typeof_(GlobalObject& global_object, Value value)
  78. {
  79. return js_string(global_object.vm(), value.typeof());
  80. }
  81. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  82. void OpTitleCase::execute_impl(Bytecode::Interpreter& interpreter) const \
  83. { \
  84. interpreter.accumulator() = op_snake_case(interpreter.global_object(), interpreter.accumulator()); \
  85. } \
  86. String OpTitleCase::to_string_impl(Bytecode::Executable const&) const \
  87. { \
  88. return #OpTitleCase; \
  89. }
  90. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  91. void NewBigInt::execute_impl(Bytecode::Interpreter& interpreter) const
  92. {
  93. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), m_bigint);
  94. }
  95. void NewArray::execute_impl(Bytecode::Interpreter& interpreter) const
  96. {
  97. Vector<Value> elements;
  98. elements.ensure_capacity(m_element_count);
  99. for (size_t i = 0; i < m_element_count; i++)
  100. elements.append(interpreter.reg(m_elements[i]));
  101. interpreter.accumulator() = Array::create_from(interpreter.global_object(), elements);
  102. }
  103. void NewString::execute_impl(Bytecode::Interpreter& interpreter) const
  104. {
  105. interpreter.accumulator() = js_string(interpreter.vm(), interpreter.current_executable().get_string(m_string));
  106. }
  107. void NewObject::execute_impl(Bytecode::Interpreter& interpreter) const
  108. {
  109. interpreter.accumulator() = Object::create(interpreter.global_object(), interpreter.global_object().object_prototype());
  110. }
  111. void ConcatString::execute_impl(Bytecode::Interpreter& interpreter) const
  112. {
  113. interpreter.reg(m_lhs) = add(interpreter.global_object(), interpreter.reg(m_lhs), interpreter.accumulator());
  114. }
  115. void GetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  116. {
  117. interpreter.accumulator() = interpreter.vm().get_variable(interpreter.current_executable().get_string(m_identifier), interpreter.global_object());
  118. }
  119. void SetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
  120. {
  121. interpreter.vm().set_variable(interpreter.current_executable().get_string(m_identifier), interpreter.accumulator(), interpreter.global_object());
  122. }
  123. void GetById::execute_impl(Bytecode::Interpreter& interpreter) const
  124. {
  125. if (auto* object = interpreter.accumulator().to_object(interpreter.global_object()))
  126. interpreter.accumulator() = object->get(interpreter.current_executable().get_string(m_property));
  127. }
  128. void PutById::execute_impl(Bytecode::Interpreter& interpreter) const
  129. {
  130. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
  131. object->put(interpreter.current_executable().get_string(m_property), interpreter.accumulator());
  132. }
  133. void Jump::execute_impl(Bytecode::Interpreter& interpreter) const
  134. {
  135. interpreter.jump(*m_true_target);
  136. }
  137. void Jump::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  138. {
  139. if (m_true_target.has_value() && &m_true_target->block() == &from)
  140. m_true_target = Label { to };
  141. if (m_false_target.has_value() && &m_false_target->block() == &from)
  142. m_false_target = Label { to };
  143. }
  144. void JumpConditional::execute_impl(Bytecode::Interpreter& interpreter) const
  145. {
  146. VERIFY(m_true_target.has_value());
  147. VERIFY(m_false_target.has_value());
  148. auto result = interpreter.accumulator();
  149. if (result.to_boolean())
  150. interpreter.jump(m_true_target.value());
  151. else
  152. interpreter.jump(m_false_target.value());
  153. }
  154. void JumpNullish::execute_impl(Bytecode::Interpreter& interpreter) const
  155. {
  156. VERIFY(m_true_target.has_value());
  157. VERIFY(m_false_target.has_value());
  158. auto result = interpreter.accumulator();
  159. if (result.is_nullish())
  160. interpreter.jump(m_true_target.value());
  161. else
  162. interpreter.jump(m_false_target.value());
  163. }
  164. void Call::execute_impl(Bytecode::Interpreter& interpreter) const
  165. {
  166. auto callee = interpreter.reg(m_callee);
  167. if (!callee.is_function()) {
  168. TODO();
  169. }
  170. auto& function = callee.as_function();
  171. auto this_value = interpreter.reg(m_this_value);
  172. Value return_value;
  173. if (m_argument_count == 0 && m_type == CallType::Call) {
  174. return_value = interpreter.vm().call(function, this_value);
  175. } else {
  176. MarkedValueList argument_values { interpreter.vm().heap() };
  177. for (size_t i = 0; i < m_argument_count; ++i) {
  178. argument_values.append(interpreter.reg(m_arguments[i]));
  179. }
  180. if (m_type == CallType::Call)
  181. return_value = interpreter.vm().call(function, this_value, move(argument_values));
  182. else
  183. return_value = interpreter.vm().construct(function, function, move(argument_values));
  184. }
  185. interpreter.accumulator() = return_value;
  186. }
  187. void NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const
  188. {
  189. auto& vm = interpreter.vm();
  190. interpreter.accumulator() = ScriptFunction::create(interpreter.global_object(), m_function_node.name(), m_function_node.body(), m_function_node.parameters(), m_function_node.function_length(), vm.current_scope(), m_function_node.kind(), m_function_node.is_strict_mode(), m_function_node.is_arrow_function());
  191. }
  192. void Return::execute_impl(Bytecode::Interpreter& interpreter) const
  193. {
  194. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  195. }
  196. void Increment::execute_impl(Bytecode::Interpreter& interpreter) const
  197. {
  198. auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
  199. if (interpreter.vm().exception())
  200. return;
  201. if (old_value.is_number())
  202. interpreter.accumulator() = Value(old_value.as_double() + 1);
  203. else
  204. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  205. }
  206. void Decrement::execute_impl(Bytecode::Interpreter& interpreter) const
  207. {
  208. auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
  209. if (interpreter.vm().exception())
  210. return;
  211. if (old_value.is_number())
  212. interpreter.accumulator() = Value(old_value.as_double() - 1);
  213. else
  214. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  215. }
  216. void Throw::execute_impl(Bytecode::Interpreter& interpreter) const
  217. {
  218. interpreter.vm().throw_exception(interpreter.global_object(), interpreter.accumulator());
  219. }
  220. void EnterUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  221. {
  222. interpreter.enter_unwind_context(m_handler_target, m_finalizer_target);
  223. interpreter.jump(m_entry_point);
  224. }
  225. void EnterUnwindContext::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  226. {
  227. if (&m_entry_point.block() == &from)
  228. m_entry_point = Label { to };
  229. if (m_handler_target.has_value() && &m_handler_target->block() == &from)
  230. m_handler_target = Label { to };
  231. if (m_finalizer_target.has_value() && &m_finalizer_target->block() == &from)
  232. m_finalizer_target = Label { to };
  233. }
  234. void LeaveUnwindContext::execute_impl(Bytecode::Interpreter& interpreter) const
  235. {
  236. interpreter.leave_unwind_context();
  237. }
  238. void ContinuePendingUnwind::execute_impl(Bytecode::Interpreter& interpreter) const
  239. {
  240. interpreter.continue_pending_unwind(m_resume_target);
  241. }
  242. void ContinuePendingUnwind::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  243. {
  244. if (&m_resume_target.block() == &from)
  245. m_resume_target = Label { to };
  246. }
  247. void PushLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
  248. {
  249. HashMap<FlyString, Variable> resolved_variables;
  250. for (auto& it : m_variables)
  251. resolved_variables.set(interpreter.current_executable().get_string(it.key), it.value);
  252. auto* block_lexical_environment = interpreter.vm().heap().allocate<LexicalEnvironment>(interpreter.global_object(), move(resolved_variables), interpreter.vm().current_scope());
  253. interpreter.vm().call_frame().scope = block_lexical_environment;
  254. }
  255. void Yield::execute_impl(Bytecode::Interpreter& interpreter) const
  256. {
  257. auto yielded_value = interpreter.accumulator().value_or(js_undefined());
  258. auto object = JS::Object::create(interpreter.global_object(), nullptr);
  259. object->put("result", yielded_value);
  260. if (m_continuation_label.has_value())
  261. object->put("continuation", Value(static_cast<double>(reinterpret_cast<u64>(&m_continuation_label->block()))));
  262. else
  263. object->put("continuation", Value(0));
  264. interpreter.do_return(object);
  265. }
  266. void Yield::replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  267. {
  268. if (m_continuation_label.has_value() && &m_continuation_label->block() == &from)
  269. m_continuation_label = Label { to };
  270. }
  271. void GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  272. {
  273. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object())) {
  274. auto property_key = interpreter.accumulator().to_property_key(interpreter.global_object());
  275. if (interpreter.vm().exception())
  276. return;
  277. interpreter.accumulator() = object->get(property_key);
  278. }
  279. }
  280. void PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
  281. {
  282. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object())) {
  283. auto property_key = interpreter.reg(m_property).to_property_key(interpreter.global_object());
  284. if (interpreter.vm().exception())
  285. return;
  286. object->put(property_key, interpreter.accumulator());
  287. }
  288. }
  289. void LoadArgument::execute_impl(Bytecode::Interpreter& interpreter) const
  290. {
  291. interpreter.accumulator() = interpreter.vm().argument(m_index);
  292. }
  293. String Load::to_string_impl(Bytecode::Executable const&) const
  294. {
  295. return String::formatted("Load {}", m_src);
  296. }
  297. String LoadImmediate::to_string_impl(Bytecode::Executable const&) const
  298. {
  299. return String::formatted("LoadImmediate {}", m_value);
  300. }
  301. String Store::to_string_impl(Bytecode::Executable const&) const
  302. {
  303. return String::formatted("Store {}", m_dst);
  304. }
  305. String NewBigInt::to_string_impl(Bytecode::Executable const&) const
  306. {
  307. return String::formatted("NewBigInt \"{}\"", m_bigint.to_base10());
  308. }
  309. String NewArray::to_string_impl(Bytecode::Executable const&) const
  310. {
  311. StringBuilder builder;
  312. builder.append("NewArray");
  313. if (m_element_count != 0) {
  314. builder.append(" [");
  315. for (size_t i = 0; i < m_element_count; ++i) {
  316. builder.appendff("{}", m_elements[i]);
  317. if (i != m_element_count - 1)
  318. builder.append(',');
  319. }
  320. builder.append(']');
  321. }
  322. return builder.to_string();
  323. }
  324. String NewString::to_string_impl(Bytecode::Executable const& executable) const
  325. {
  326. return String::formatted("NewString {} (\"{}\")", m_string, executable.string_table->get(m_string));
  327. }
  328. String NewObject::to_string_impl(Bytecode::Executable const&) const
  329. {
  330. return "NewObject";
  331. }
  332. String ConcatString::to_string_impl(Bytecode::Executable const&) const
  333. {
  334. return String::formatted("ConcatString {}", m_lhs);
  335. }
  336. String GetVariable::to_string_impl(Bytecode::Executable const& executable) const
  337. {
  338. return String::formatted("GetVariable {} ({})", m_identifier, executable.string_table->get(m_identifier));
  339. }
  340. String SetVariable::to_string_impl(Bytecode::Executable const& executable) const
  341. {
  342. return String::formatted("SetVariable {} ({})", m_identifier, executable.string_table->get(m_identifier));
  343. }
  344. String PutById::to_string_impl(Bytecode::Executable const& executable) const
  345. {
  346. return String::formatted("PutById base:{}, property:{} ({})", m_base, m_property, executable.string_table->get(m_property));
  347. }
  348. String GetById::to_string_impl(Bytecode::Executable const& executable) const
  349. {
  350. return String::formatted("GetById {} ({})", m_property, executable.string_table->get(m_property));
  351. }
  352. String Jump::to_string_impl(Bytecode::Executable const&) const
  353. {
  354. if (m_true_target.has_value())
  355. return String::formatted("Jump {}", *m_true_target);
  356. return String::formatted("Jump <empty>");
  357. }
  358. String JumpConditional::to_string_impl(Bytecode::Executable const&) const
  359. {
  360. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  361. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  362. return String::formatted("JumpConditional true:{} false:{}", true_string, false_string);
  363. }
  364. String JumpNullish::to_string_impl(Bytecode::Executable const&) const
  365. {
  366. auto true_string = m_true_target.has_value() ? String::formatted("{}", *m_true_target) : "<empty>";
  367. auto false_string = m_false_target.has_value() ? String::formatted("{}", *m_false_target) : "<empty>";
  368. return String::formatted("JumpNullish null:{} nonnull:{}", true_string, false_string);
  369. }
  370. String Call::to_string_impl(Bytecode::Executable const&) const
  371. {
  372. StringBuilder builder;
  373. builder.appendff("Call callee:{}, this:{}", m_callee, m_this_value);
  374. if (m_argument_count != 0) {
  375. builder.append(", arguments:[");
  376. for (size_t i = 0; i < m_argument_count; ++i) {
  377. builder.appendff("{}", m_arguments[i]);
  378. if (i != m_argument_count - 1)
  379. builder.append(',');
  380. }
  381. builder.append(']');
  382. }
  383. return builder.to_string();
  384. }
  385. String NewFunction::to_string_impl(Bytecode::Executable const&) const
  386. {
  387. return "NewFunction";
  388. }
  389. String Return::to_string_impl(Bytecode::Executable const&) const
  390. {
  391. return "Return";
  392. }
  393. String Increment::to_string_impl(Bytecode::Executable const&) const
  394. {
  395. return "Increment";
  396. }
  397. String Decrement::to_string_impl(Bytecode::Executable const&) const
  398. {
  399. return "Decrement";
  400. }
  401. String Throw::to_string_impl(Bytecode::Executable const&) const
  402. {
  403. return "Throw";
  404. }
  405. String EnterUnwindContext::to_string_impl(Bytecode::Executable const&) const
  406. {
  407. auto handler_string = m_handler_target.has_value() ? String::formatted("{}", *m_handler_target) : "<empty>";
  408. auto finalizer_string = m_finalizer_target.has_value() ? String::formatted("{}", *m_finalizer_target) : "<empty>";
  409. return String::formatted("EnterUnwindContext handler:{} finalizer:{} entry:{}", handler_string, finalizer_string, m_entry_point);
  410. }
  411. String LeaveUnwindContext::to_string_impl(Bytecode::Executable const&) const
  412. {
  413. return "LeaveUnwindContext";
  414. }
  415. String ContinuePendingUnwind::to_string_impl(Bytecode::Executable const&) const
  416. {
  417. return String::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
  418. }
  419. String PushLexicalEnvironment::to_string_impl(const Bytecode::Executable& executable) const
  420. {
  421. StringBuilder builder;
  422. builder.append("PushLexicalEnvironment");
  423. if (!m_variables.is_empty()) {
  424. builder.append(" {");
  425. Vector<String> names;
  426. for (auto& it : m_variables)
  427. names.append(executable.get_string(it.key));
  428. builder.join(", ", names);
  429. builder.append("}");
  430. }
  431. return builder.to_string();
  432. }
  433. String Yield::to_string_impl(Bytecode::Executable const&) const
  434. {
  435. if (m_continuation_label.has_value())
  436. return String::formatted("Yield continuation:@{}", m_continuation_label->block().name());
  437. return String::formatted("Yield return");
  438. }
  439. String GetByValue::to_string_impl(const Bytecode::Executable&) const
  440. {
  441. return String::formatted("GetByValue base:{}", m_base);
  442. }
  443. String PutByValue::to_string_impl(const Bytecode::Executable&) const
  444. {
  445. return String::formatted("PutByValue base:{}, property:{}", m_base, m_property);
  446. }
  447. String LoadArgument::to_string_impl(const Bytecode::Executable&) const
  448. {
  449. return String::formatted("LoadArgument {}", m_index);
  450. }
  451. }