Op.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 NewString::execute(Bytecode::Interpreter& interpreter) const
  104. {
  105. interpreter.reg(m_dst) = js_string(interpreter.vm(), m_string);
  106. }
  107. void NewObject::execute(Bytecode::Interpreter& interpreter) const
  108. {
  109. interpreter.reg(m_dst) = Object::create_empty(interpreter.global_object());
  110. }
  111. void GetVariable::execute(Bytecode::Interpreter& interpreter) const
  112. {
  113. interpreter.reg(m_dst) = interpreter.vm().get_variable(m_identifier, interpreter.global_object());
  114. }
  115. void SetVariable::execute(Bytecode::Interpreter& interpreter) const
  116. {
  117. interpreter.vm().set_variable(m_identifier, interpreter.reg(m_src), interpreter.global_object());
  118. }
  119. void GetById::execute(Bytecode::Interpreter& interpreter) const
  120. {
  121. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
  122. interpreter.reg(m_dst) = object->get(m_property);
  123. }
  124. void PutById::execute(Bytecode::Interpreter& interpreter) const
  125. {
  126. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
  127. object->put(m_property, interpreter.reg(m_src));
  128. }
  129. void Jump::execute(Bytecode::Interpreter& interpreter) const
  130. {
  131. interpreter.jump(*m_target);
  132. }
  133. void JumpIfFalse::execute(Bytecode::Interpreter& interpreter) const
  134. {
  135. VERIFY(m_target.has_value());
  136. auto result = interpreter.reg(m_result);
  137. if (!result.as_bool())
  138. interpreter.jump(m_target.value());
  139. }
  140. void JumpIfTrue::execute(Bytecode::Interpreter& interpreter) const
  141. {
  142. VERIFY(m_target.has_value());
  143. auto result = interpreter.reg(m_result);
  144. if (result.as_bool())
  145. interpreter.jump(m_target.value());
  146. }
  147. void Call::execute(Bytecode::Interpreter& interpreter) const
  148. {
  149. auto callee = interpreter.reg(m_callee);
  150. if (!callee.is_function()) {
  151. TODO();
  152. }
  153. auto& function = callee.as_function();
  154. auto this_value = interpreter.reg(m_this_value);
  155. Value return_value;
  156. if (m_argument_count == 0) {
  157. return_value = interpreter.vm().call(function, this_value);
  158. } else {
  159. MarkedValueList argument_values { interpreter.vm().heap() };
  160. for (size_t i = 0; i < m_argument_count; ++i) {
  161. argument_values.append(interpreter.reg(m_arguments[i]));
  162. }
  163. return_value = interpreter.vm().call(function, this_value, move(argument_values));
  164. }
  165. interpreter.reg(m_dst) = return_value;
  166. }
  167. void EnterScope::execute(Bytecode::Interpreter& interpreter) const
  168. {
  169. auto& vm = interpreter.vm();
  170. auto& global_object = interpreter.global_object();
  171. for (auto& declaration : m_scope_node.functions())
  172. vm.current_scope()->put_to_scope(declaration.name(), { js_undefined(), DeclarationKind::Var });
  173. for (auto& declaration : m_scope_node.functions()) {
  174. auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), vm.current_scope(), declaration.is_strict_mode());
  175. vm.set_variable(declaration.name(), function, global_object);
  176. }
  177. // FIXME: Process variable declarations.
  178. // FIXME: Whatever else JS::Interpreter::enter_scope() does.
  179. }
  180. void Return::execute(Bytecode::Interpreter& interpreter) const
  181. {
  182. auto return_value = m_argument.has_value() ? interpreter.reg(m_argument.value()) : js_undefined();
  183. interpreter.do_return(return_value);
  184. }
  185. String Load::to_string() const
  186. {
  187. return String::formatted("Load dst:{}, value:{}", m_dst, m_value.to_string_without_side_effects());
  188. }
  189. String Add::to_string() const
  190. {
  191. return String::formatted("Add dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  192. }
  193. String Sub::to_string() const
  194. {
  195. return String::formatted("Sub dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  196. }
  197. String Mul::to_string() const
  198. {
  199. return String::formatted("Mul dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  200. }
  201. String Div::to_string() const
  202. {
  203. return String::formatted("Div dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  204. }
  205. String Mod::to_string() const
  206. {
  207. return String::formatted("Mod dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  208. }
  209. String Exp::to_string() const
  210. {
  211. return String::formatted("Exp dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  212. }
  213. String GreaterThan::to_string() const
  214. {
  215. return String::formatted("GreaterThan dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  216. }
  217. String GreaterThanEquals::to_string() const
  218. {
  219. return String::formatted("GreaterThanEquals dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  220. }
  221. String LessThan::to_string() const
  222. {
  223. return String::formatted("LessThan dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  224. }
  225. String LessThanEquals::to_string() const
  226. {
  227. return String::formatted("LessThanEquals dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  228. }
  229. String AbstractInequals::to_string() const
  230. {
  231. return String::formatted("AbstractInequals dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  232. }
  233. String AbstractEquals::to_string() const
  234. {
  235. return String::formatted("AbstractEquals dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  236. }
  237. String BitwiseAnd::to_string() const
  238. {
  239. return String::formatted("BitwiseAnd dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  240. }
  241. String BitwiseOr::to_string() const
  242. {
  243. return String::formatted("BitwiseOr dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  244. }
  245. String BitwiseXor::to_string() const
  246. {
  247. return String::formatted("BitwiseXor dst:{}, src1:{}, src2:{}", m_dst, m_src1, m_src2);
  248. }
  249. String NewString::to_string() const
  250. {
  251. return String::formatted("NewString dst:{}, string:\"{}\"", m_dst, m_string);
  252. }
  253. String NewObject::to_string() const
  254. {
  255. return String::formatted("NewObject dst:{}", m_dst);
  256. }
  257. String GetVariable::to_string() const
  258. {
  259. return String::formatted("GetVariable dst:{}, identifier:{}", m_dst, m_identifier);
  260. }
  261. String SetVariable::to_string() const
  262. {
  263. return String::formatted("SetVariable identifier:{}, src:{}", m_identifier, m_src);
  264. }
  265. String PutById::to_string() const
  266. {
  267. return String::formatted("PutById base:{}, property:{}, src:{}", m_base, m_property, m_src);
  268. }
  269. String GetById::to_string() const
  270. {
  271. return String::formatted("GetById dst:{}, base:{}, property:{}", m_dst, m_base, m_property);
  272. }
  273. String Jump::to_string() const
  274. {
  275. return String::formatted("Jump {}", *m_target);
  276. }
  277. String JumpIfFalse::to_string() const
  278. {
  279. if (m_target.has_value())
  280. return String::formatted("JumpIfFalse result:{}, target:{}", m_result, m_target.value());
  281. return String::formatted("JumpIfFalse result:{}, target:<empty>", m_result);
  282. }
  283. String JumpIfTrue::to_string() const
  284. {
  285. if (m_target.has_value())
  286. return String::formatted("JumpIfTrue result:{}, target:{}", m_result, m_target.value());
  287. return String::formatted("JumpIfTrue result:{}, target:<empty>", m_result);
  288. }
  289. String Call::to_string() const
  290. {
  291. StringBuilder builder;
  292. builder.appendff("Call dst:{}, callee:{}, this:{}", m_dst, m_callee, m_this_value);
  293. if (m_argument_count != 0) {
  294. builder.append(", arguments:[");
  295. for (size_t i = 0; i < m_argument_count; ++i) {
  296. builder.appendff("{}", m_arguments[i]);
  297. if (i != m_argument_count - 1)
  298. builder.append(',');
  299. }
  300. builder.append(']');
  301. }
  302. return builder.to_string();
  303. }
  304. String EnterScope::to_string() const
  305. {
  306. return "EnterScope";
  307. }
  308. String Return::to_string() const
  309. {
  310. if (m_argument.has_value())
  311. return String::formatted("Return {}", m_argument.value());
  312. return "Return";
  313. }
  314. }