Op.cpp 10 KB

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