Op.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/AST.h>
  7. #include <LibJS/Bytecode/Interpreter.h>
  8. #include <LibJS/Bytecode/Op.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/ScriptFunction.h>
  11. #include <LibJS/Runtime/Value.h>
  12. namespace JS::Bytecode {
  13. void Instruction::execute(Bytecode::Interpreter& interpreter) const
  14. {
  15. #define __BYTECODE_OP(op) \
  16. case Instruction::Type::op: \
  17. return static_cast<Bytecode::Op::op const&>(*this).execute(interpreter);
  18. switch (type()) {
  19. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  20. default:
  21. VERIFY_NOT_REACHED();
  22. }
  23. #undef __BYTECODE_OP
  24. }
  25. String Instruction::to_string() const
  26. {
  27. #define __BYTECODE_OP(op) \
  28. case Instruction::Type::op: \
  29. return static_cast<Bytecode::Op::op const&>(*this).to_string();
  30. switch (type()) {
  31. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  32. default:
  33. VERIFY_NOT_REACHED();
  34. }
  35. #undef __BYTECODE_OP
  36. }
  37. }
  38. namespace JS::Bytecode::Op {
  39. void Load::execute(Bytecode::Interpreter& interpreter) const
  40. {
  41. interpreter.reg(m_dst) = m_value;
  42. }
  43. void Add::execute(Bytecode::Interpreter& interpreter) const
  44. {
  45. interpreter.reg(m_dst) = add(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  46. }
  47. void Sub::execute(Bytecode::Interpreter& interpreter) const
  48. {
  49. interpreter.reg(m_dst) = sub(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  50. }
  51. void Mul::execute(Bytecode::Interpreter& interpreter) const
  52. {
  53. interpreter.reg(m_dst) = mul(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  54. }
  55. void Div::execute(Bytecode::Interpreter& interpreter) const
  56. {
  57. interpreter.reg(m_dst) = div(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  58. }
  59. void Mod::execute(Bytecode::Interpreter& interpreter) const
  60. {
  61. interpreter.reg(m_dst) = mod(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  62. }
  63. void Exp::execute(Bytecode::Interpreter& interpreter) const
  64. {
  65. interpreter.reg(m_dst) = exp(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  66. }
  67. void GreaterThan::execute(Bytecode::Interpreter& interpreter) const
  68. {
  69. interpreter.reg(m_dst) = greater_than(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  70. }
  71. void GreaterThanEquals::execute(Bytecode::Interpreter& interpreter) const
  72. {
  73. interpreter.reg(m_dst) = greater_than_equals(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  74. }
  75. void LessThan::execute(Bytecode::Interpreter& interpreter) const
  76. {
  77. interpreter.reg(m_dst) = less_than(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  78. }
  79. void LessThanEquals::execute(Bytecode::Interpreter& interpreter) const
  80. {
  81. interpreter.reg(m_dst) = less_than_equals(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  82. }
  83. void AbstractInequals::execute(Bytecode::Interpreter& interpreter) const
  84. {
  85. interpreter.reg(m_dst) = Value(!abstract_eq(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2)));
  86. }
  87. void AbstractEquals::execute(Bytecode::Interpreter& interpreter) const
  88. {
  89. interpreter.reg(m_dst) = Value(abstract_eq(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2)));
  90. }
  91. void BitwiseAnd::execute(Bytecode::Interpreter& interpreter) const
  92. {
  93. interpreter.reg(m_dst) = bitwise_and(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  94. }
  95. void BitwiseOr::execute(Bytecode::Interpreter& interpreter) const
  96. {
  97. interpreter.reg(m_dst) = bitwise_or(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  98. }
  99. void BitwiseXor::execute(Bytecode::Interpreter& interpreter) const
  100. {
  101. interpreter.reg(m_dst) = bitwise_xor(interpreter.global_object(), interpreter.reg(m_src1), interpreter.reg(m_src2));
  102. }
  103. void BitwiseNot::execute(Bytecode::Interpreter& interpreter) const
  104. {
  105. interpreter.reg(m_dst) = bitwise_not(interpreter.global_object(), interpreter.reg(m_src));
  106. }
  107. void Not::execute(Bytecode::Interpreter& interpreter) const
  108. {
  109. interpreter.reg(m_dst) = Value(!interpreter.reg(m_src).to_boolean());
  110. }
  111. void UnaryPlus::execute(Bytecode::Interpreter& interpreter) const
  112. {
  113. interpreter.reg(m_dst) = Value(unary_plus(interpreter.global_object(), interpreter.reg(m_src)));
  114. }
  115. void UnaryMinus::execute(Bytecode::Interpreter& interpreter) const
  116. {
  117. interpreter.reg(m_dst) = Value(unary_minus(interpreter.global_object(), interpreter.reg(m_src)));
  118. }
  119. void Typeof::execute(Bytecode::Interpreter& interpreter) const
  120. {
  121. auto& vm = interpreter.global_object().vm();
  122. interpreter.reg(m_dst) = Value(js_string(vm, interpreter.reg(m_src).typeof()));
  123. }
  124. void NewString::execute(Bytecode::Interpreter& interpreter) const
  125. {
  126. interpreter.reg(m_dst) = js_string(interpreter.vm(), m_string);
  127. }
  128. void NewObject::execute(Bytecode::Interpreter& interpreter) const
  129. {
  130. interpreter.reg(m_dst) = Object::create_empty(interpreter.global_object());
  131. }
  132. void GetVariable::execute(Bytecode::Interpreter& interpreter) const
  133. {
  134. interpreter.reg(m_dst) = interpreter.vm().get_variable(m_identifier, interpreter.global_object());
  135. }
  136. void SetVariable::execute(Bytecode::Interpreter& interpreter) const
  137. {
  138. interpreter.vm().set_variable(m_identifier, interpreter.reg(m_src), interpreter.global_object());
  139. }
  140. void GetById::execute(Bytecode::Interpreter& interpreter) const
  141. {
  142. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
  143. interpreter.reg(m_dst) = object->get(m_property);
  144. }
  145. void PutById::execute(Bytecode::Interpreter& interpreter) const
  146. {
  147. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
  148. object->put(m_property, interpreter.reg(m_src));
  149. }
  150. void Jump::execute(Bytecode::Interpreter& interpreter) const
  151. {
  152. interpreter.jump(*m_target);
  153. }
  154. void JumpIfFalse::execute(Bytecode::Interpreter& interpreter) const
  155. {
  156. VERIFY(m_target.has_value());
  157. auto result = interpreter.reg(m_result);
  158. if (!result.as_bool())
  159. interpreter.jump(m_target.value());
  160. }
  161. void JumpIfTrue::execute(Bytecode::Interpreter& interpreter) const
  162. {
  163. VERIFY(m_target.has_value());
  164. auto result = interpreter.reg(m_result);
  165. if (result.as_bool())
  166. interpreter.jump(m_target.value());
  167. }
  168. void Call::execute(Bytecode::Interpreter& interpreter) const
  169. {
  170. auto callee = interpreter.reg(m_callee);
  171. if (!callee.is_function()) {
  172. TODO();
  173. }
  174. auto& function = callee.as_function();
  175. auto this_value = interpreter.reg(m_this_value);
  176. Value return_value;
  177. if (m_argument_count == 0) {
  178. return_value = interpreter.vm().call(function, this_value);
  179. } else {
  180. MarkedValueList argument_values { interpreter.vm().heap() };
  181. for (size_t i = 0; i < m_argument_count; ++i) {
  182. argument_values.append(interpreter.reg(m_arguments[i]));
  183. }
  184. return_value = interpreter.vm().call(function, this_value, move(argument_values));
  185. }
  186. interpreter.reg(m_dst) = return_value;
  187. }
  188. void EnterScope::execute(Bytecode::Interpreter& interpreter) const
  189. {
  190. auto& vm = interpreter.vm();
  191. auto& global_object = interpreter.global_object();
  192. for (auto& declaration : m_scope_node.functions())
  193. vm.current_scope()->put_to_scope(declaration.name(), { js_undefined(), DeclarationKind::Var });
  194. for (auto& declaration : m_scope_node.functions()) {
  195. auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), vm.current_scope(), declaration.is_strict_mode());
  196. vm.set_variable(declaration.name(), function, global_object);
  197. }
  198. // FIXME: Process variable declarations.
  199. // FIXME: Whatever else JS::Interpreter::enter_scope() does.
  200. }
  201. void Return::execute(Bytecode::Interpreter& interpreter) const
  202. {
  203. auto return_value = m_argument.has_value() ? interpreter.reg(m_argument.value()) : js_undefined();
  204. interpreter.do_return(return_value);
  205. }
  206. String Load::to_string() const
  207. {
  208. return String::formatted("Load dst:{}, value:{}", m_dst, m_value.to_string_without_side_effects());
  209. }
  210. String Add::to_string() const
  211. {
  212. return String::formatted("Add dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  213. }
  214. String Sub::to_string() const
  215. {
  216. return String::formatted("Sub dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  217. }
  218. String Mul::to_string() const
  219. {
  220. return String::formatted("Mul dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  221. }
  222. String Div::to_string() const
  223. {
  224. return String::formatted("Div dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  225. }
  226. String Mod::to_string() const
  227. {
  228. return String::formatted("Mod dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  229. }
  230. String Exp::to_string() const
  231. {
  232. return String::formatted("Exp dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  233. }
  234. String GreaterThan::to_string() const
  235. {
  236. return String::formatted("GreaterThan dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  237. }
  238. String GreaterThanEquals::to_string() const
  239. {
  240. return String::formatted("GreaterThanEquals dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  241. }
  242. String LessThan::to_string() const
  243. {
  244. return String::formatted("LessThan dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  245. }
  246. String LessThanEquals::to_string() const
  247. {
  248. return String::formatted("LessThanEquals dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  249. }
  250. String AbstractInequals::to_string() const
  251. {
  252. return String::formatted("AbstractInequals dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  253. }
  254. String AbstractEquals::to_string() const
  255. {
  256. return String::formatted("AbstractEquals dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  257. }
  258. String BitwiseAnd::to_string() const
  259. {
  260. return String::formatted("BitwiseAnd dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  261. }
  262. String BitwiseOr::to_string() const
  263. {
  264. return String::formatted("BitwiseOr dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  265. }
  266. String BitwiseXor::to_string() const
  267. {
  268. return String::formatted("BitwiseXor dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  269. }
  270. String BitwiseNot::to_string() const
  271. {
  272. return String::formatted("BitwiseNot dst:{}, src:{}", m_dst, m_src);
  273. }
  274. String Not::to_string() const
  275. {
  276. return String::formatted("Not dst:{}, src:{}", m_dst, m_src);
  277. }
  278. String UnaryPlus::to_string() const
  279. {
  280. return String::formatted("UnaryPlus dst:{}, src:{}", m_dst, m_src);
  281. }
  282. String UnaryMinus::to_string() const
  283. {
  284. return String::formatted("UnaryMinus dst:{}, src:{}", m_dst, m_src);
  285. }
  286. String Typeof::to_string() const
  287. {
  288. return String::formatted("Typeof dst:{}, src:{}", m_dst, m_src);
  289. }
  290. String NewString::to_string() const
  291. {
  292. return String::formatted("NewString dst:{}, string:\"{}\"", m_dst, m_string);
  293. }
  294. String NewObject::to_string() const
  295. {
  296. return String::formatted("NewObject dst:{}", m_dst);
  297. }
  298. String GetVariable::to_string() const
  299. {
  300. return String::formatted("GetVariable dst:{}, identifier:{}", m_dst, m_identifier);
  301. }
  302. String SetVariable::to_string() const
  303. {
  304. return String::formatted("SetVariable identifier:{}, src:{}", m_identifier, m_src);
  305. }
  306. String PutById::to_string() const
  307. {
  308. return String::formatted("PutById base:{}, property:{}, src:{}", m_base, m_property, m_src);
  309. }
  310. String GetById::to_string() const
  311. {
  312. return String::formatted("GetById dst:{}, base:{}, property:{}", m_dst, m_base, m_property);
  313. }
  314. String Jump::to_string() const
  315. {
  316. return String::formatted("Jump {}", *m_target);
  317. }
  318. String JumpIfFalse::to_string() const
  319. {
  320. if (m_target.has_value())
  321. return String::formatted("JumpIfFalse result:{}, target:{}", m_result, m_target.value());
  322. return String::formatted("JumpIfFalse result:{}, target:<empty>", m_result);
  323. }
  324. String JumpIfTrue::to_string() const
  325. {
  326. if (m_target.has_value())
  327. return String::formatted("JumpIfTrue result:{}, target:{}", m_result, m_target.value());
  328. return String::formatted("JumpIfTrue result:{}, target:<empty>", m_result);
  329. }
  330. String Call::to_string() const
  331. {
  332. StringBuilder builder;
  333. builder.appendff("Call dst:{}, callee:{}, this:{}", m_dst, m_callee, m_this_value);
  334. if (m_argument_count != 0) {
  335. builder.append(", arguments:[");
  336. for (size_t i = 0; i < m_argument_count; ++i) {
  337. builder.appendff("{}", m_arguments[i]);
  338. if (i != m_argument_count - 1)
  339. builder.append(',');
  340. }
  341. builder.append(']');
  342. }
  343. return builder.to_string();
  344. }
  345. String EnterScope::to_string() const
  346. {
  347. return "EnterScope";
  348. }
  349. String Return::to_string() const
  350. {
  351. if (m_argument.has_value())
  352. return String::formatted("Return {}", m_argument.value());
  353. return "Return";
  354. }
  355. }