Op.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/AST.h>
  8. #include <LibJS/Bytecode/Interpreter.h>
  9. #include <LibJS/Bytecode/Op.h>
  10. #include <LibJS/Runtime/BigInt.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <LibJS/Runtime/ScriptFunction.h>
  13. #include <LibJS/Runtime/Value.h>
  14. namespace JS::Bytecode {
  15. void Instruction::execute(Bytecode::Interpreter& interpreter) const
  16. {
  17. #define __BYTECODE_OP(op) \
  18. case Instruction::Type::op: \
  19. return static_cast<Bytecode::Op::op const&>(*this).execute(interpreter);
  20. switch (type()) {
  21. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  22. default:
  23. VERIFY_NOT_REACHED();
  24. }
  25. #undef __BYTECODE_OP
  26. }
  27. String Instruction::to_string() const
  28. {
  29. #define __BYTECODE_OP(op) \
  30. case Instruction::Type::op: \
  31. return static_cast<Bytecode::Op::op const&>(*this).to_string();
  32. switch (type()) {
  33. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  34. default:
  35. VERIFY_NOT_REACHED();
  36. }
  37. #undef __BYTECODE_OP
  38. }
  39. }
  40. namespace JS::Bytecode::Op {
  41. void Load::execute(Bytecode::Interpreter& interpreter) const
  42. {
  43. interpreter.accumulator() = interpreter.reg(m_src);
  44. }
  45. void LoadImmediate::execute(Bytecode::Interpreter& interpreter) const
  46. {
  47. interpreter.accumulator() = m_value;
  48. }
  49. void Store::execute(Bytecode::Interpreter& interpreter) const
  50. {
  51. interpreter.reg(m_dst) = interpreter.accumulator();
  52. }
  53. static Value abstract_inequals(GlobalObject& global_object, Value src1, Value src2)
  54. {
  55. return Value(!abstract_eq(global_object, src1, src2));
  56. }
  57. static Value abstract_equals(GlobalObject& global_object, Value src1, Value src2)
  58. {
  59. return Value(abstract_eq(global_object, src1, src2));
  60. }
  61. static Value typed_inequals(GlobalObject&, Value src1, Value src2)
  62. {
  63. return Value(!strict_eq(src1, src2));
  64. }
  65. static Value typed_equals(GlobalObject&, Value src1, Value src2)
  66. {
  67. return Value(strict_eq(src1, src2));
  68. }
  69. #define JS_DEFINE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  70. void OpTitleCase::execute(Bytecode::Interpreter& interpreter) const \
  71. { \
  72. auto lhs = interpreter.reg(m_lhs_reg); \
  73. auto rhs = interpreter.accumulator(); \
  74. interpreter.accumulator() = op_snake_case(interpreter.global_object(), lhs, rhs); \
  75. } \
  76. String OpTitleCase::to_string() const \
  77. { \
  78. return String::formatted(#OpTitleCase " lhs:{}", m_lhs_reg); \
  79. }
  80. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DEFINE_COMMON_BINARY_OP)
  81. static Value not_(GlobalObject&, Value value)
  82. {
  83. return Value(!value.to_boolean());
  84. }
  85. static Value typeof_(GlobalObject& global_object, Value value)
  86. {
  87. return js_string(global_object.vm(), value.typeof());
  88. }
  89. #define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  90. void OpTitleCase::execute(Bytecode::Interpreter& interpreter) const \
  91. { \
  92. interpreter.accumulator() = op_snake_case(interpreter.global_object(), interpreter.accumulator()); \
  93. } \
  94. String OpTitleCase::to_string() const \
  95. { \
  96. return #OpTitleCase; \
  97. }
  98. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP)
  99. void NewBigInt::execute(Bytecode::Interpreter& interpreter) const
  100. {
  101. interpreter.accumulator() = js_bigint(interpreter.vm().heap(), m_bigint);
  102. }
  103. void NewString::execute(Bytecode::Interpreter& interpreter) const
  104. {
  105. interpreter.accumulator() = js_string(interpreter.vm(), m_string);
  106. }
  107. void NewObject::execute(Bytecode::Interpreter& interpreter) const
  108. {
  109. interpreter.accumulator() = Object::create_empty(interpreter.global_object());
  110. }
  111. void ConcatString::execute(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(Bytecode::Interpreter& interpreter) const
  116. {
  117. interpreter.accumulator() = interpreter.vm().get_variable(m_identifier, interpreter.global_object());
  118. }
  119. void SetVariable::execute(Bytecode::Interpreter& interpreter) const
  120. {
  121. interpreter.vm().set_variable(m_identifier, interpreter.accumulator(), interpreter.global_object());
  122. }
  123. void GetById::execute(Bytecode::Interpreter& interpreter) const
  124. {
  125. if (auto* object = interpreter.accumulator().to_object(interpreter.global_object()))
  126. interpreter.accumulator() = object->get(m_property);
  127. }
  128. void PutById::execute(Bytecode::Interpreter& interpreter) const
  129. {
  130. if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
  131. object->put(m_property, interpreter.accumulator());
  132. }
  133. void Jump::execute(Bytecode::Interpreter& interpreter) const
  134. {
  135. interpreter.jump(*m_target);
  136. }
  137. void JumpIfFalse::execute(Bytecode::Interpreter& interpreter) const
  138. {
  139. VERIFY(m_target.has_value());
  140. auto result = interpreter.accumulator();
  141. if (!result.to_boolean())
  142. interpreter.jump(m_target.value());
  143. }
  144. void JumpIfTrue::execute(Bytecode::Interpreter& interpreter) const
  145. {
  146. VERIFY(m_target.has_value());
  147. auto result = interpreter.accumulator();
  148. if (result.to_boolean())
  149. interpreter.jump(m_target.value());
  150. }
  151. void JumpIfNotNullish::execute(Bytecode::Interpreter& interpreter) const
  152. {
  153. VERIFY(m_target.has_value());
  154. auto result = interpreter.accumulator();
  155. if (!result.is_nullish())
  156. interpreter.jump(m_target.value());
  157. }
  158. void Call::execute(Bytecode::Interpreter& interpreter) const
  159. {
  160. auto callee = interpreter.reg(m_callee);
  161. if (!callee.is_function()) {
  162. TODO();
  163. }
  164. auto& function = callee.as_function();
  165. auto this_value = interpreter.reg(m_this_value);
  166. Value return_value;
  167. if (m_argument_count == 0) {
  168. return_value = interpreter.vm().call(function, this_value);
  169. } else {
  170. MarkedValueList argument_values { interpreter.vm().heap() };
  171. for (size_t i = 0; i < m_argument_count; ++i) {
  172. argument_values.append(interpreter.reg(m_arguments[i]));
  173. }
  174. return_value = interpreter.vm().call(function, this_value, move(argument_values));
  175. }
  176. interpreter.accumulator() = return_value;
  177. }
  178. void EnterScope::execute(Bytecode::Interpreter& interpreter) const
  179. {
  180. auto& vm = interpreter.vm();
  181. auto& global_object = interpreter.global_object();
  182. for (auto& declaration : m_scope_node.functions())
  183. vm.current_scope()->put_to_scope(declaration.name(), { js_undefined(), DeclarationKind::Var });
  184. for (auto& declaration : m_scope_node.functions()) {
  185. auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), vm.current_scope(), declaration.is_strict_mode());
  186. vm.set_variable(declaration.name(), function, global_object);
  187. }
  188. // FIXME: Process variable declarations.
  189. // FIXME: Whatever else JS::Interpreter::enter_scope() does.
  190. }
  191. void Return::execute(Bytecode::Interpreter& interpreter) const
  192. {
  193. interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
  194. }
  195. String Load::to_string() const
  196. {
  197. return String::formatted("Load src:{}", m_src);
  198. }
  199. String LoadImmediate::to_string() const
  200. {
  201. return String::formatted("LoadImmediate value:{}", m_value);
  202. }
  203. String Store::to_string() const
  204. {
  205. return String::formatted("Store dst:{}", m_dst);
  206. }
  207. String NewBigInt::to_string() const
  208. {
  209. return String::formatted("NewBigInt bigint:\"{}\"", m_bigint.to_base10());
  210. }
  211. String NewString::to_string() const
  212. {
  213. return String::formatted("NewString string:\"{}\"", m_string);
  214. }
  215. String NewObject::to_string() const
  216. {
  217. return "NewObject";
  218. }
  219. String ConcatString::to_string() const
  220. {
  221. return String::formatted("ConcatString lhs:{}", m_lhs);
  222. }
  223. String GetVariable::to_string() const
  224. {
  225. return String::formatted("GetVariable identifier:{}", m_identifier);
  226. }
  227. String SetVariable::to_string() const
  228. {
  229. return String::formatted("SetVariable identifier:{}", m_identifier);
  230. }
  231. String PutById::to_string() const
  232. {
  233. return String::formatted("PutById base:{}, property:{}", m_base, m_property);
  234. }
  235. String GetById::to_string() const
  236. {
  237. return String::formatted("GetById property:{}", m_property);
  238. }
  239. String Jump::to_string() const
  240. {
  241. return String::formatted("Jump {}", *m_target);
  242. }
  243. String JumpIfFalse::to_string() const
  244. {
  245. if (m_target.has_value())
  246. return String::formatted("JumpIfFalse target:{}", m_target.value());
  247. return "JumpIfFalse target:<empty>";
  248. }
  249. String JumpIfTrue::to_string() const
  250. {
  251. if (m_target.has_value())
  252. return String::formatted("JumpIfTrue target:{}", m_target.value());
  253. return "JumpIfTrue result:{}, target:<empty>";
  254. }
  255. String JumpIfNotNullish::to_string() const
  256. {
  257. if (m_target.has_value())
  258. return String::formatted("JumpIfNotNullish target:{}", m_target.value());
  259. return "JumpIfNotNullish target:<empty>";
  260. }
  261. String Call::to_string() const
  262. {
  263. StringBuilder builder;
  264. builder.appendff("Call callee:{}, this:{}", m_callee, m_this_value);
  265. if (m_argument_count != 0) {
  266. builder.append(", arguments:[");
  267. for (size_t i = 0; i < m_argument_count; ++i) {
  268. builder.appendff("{}", m_arguments[i]);
  269. if (i != m_argument_count - 1)
  270. builder.append(',');
  271. }
  272. builder.append(']');
  273. }
  274. return builder.to_string();
  275. }
  276. String EnterScope::to_string() const
  277. {
  278. return "EnterScope";
  279. }
  280. String Return::to_string() const
  281. {
  282. return "Return";
  283. }
  284. }