Op.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <LibJS/Bytecode/Instruction.h>
  9. #include <LibJS/Bytecode/Label.h>
  10. #include <LibJS/Bytecode/Register.h>
  11. #include <LibJS/Heap/Cell.h>
  12. #include <LibJS/Runtime/Value.h>
  13. namespace JS::Bytecode::Op {
  14. class Load final : public Instruction {
  15. public:
  16. Load(Register dst, Value value)
  17. : Instruction(Type::Load)
  18. , m_dst(dst)
  19. , m_value(value)
  20. {
  21. }
  22. void execute(Bytecode::Interpreter&) const;
  23. String to_string() const;
  24. private:
  25. Register m_dst;
  26. Value m_value;
  27. };
  28. class Add final : public Instruction {
  29. public:
  30. Add(Register dst, Register src1, Register src2)
  31. : Instruction(Type::Add)
  32. , m_dst(dst)
  33. , m_src1(src1)
  34. , m_src2(src2)
  35. {
  36. }
  37. void execute(Bytecode::Interpreter&) const;
  38. String to_string() const;
  39. private:
  40. Register m_dst;
  41. Register m_src1;
  42. Register m_src2;
  43. };
  44. class Sub final : public Instruction {
  45. public:
  46. Sub(Register dst, Register src1, Register src2)
  47. : Instruction(Type::Sub)
  48. , m_dst(dst)
  49. , m_src1(src1)
  50. , m_src2(src2)
  51. {
  52. }
  53. void execute(Bytecode::Interpreter&) const;
  54. String to_string() const;
  55. private:
  56. Register m_dst;
  57. Register m_src1;
  58. Register m_src2;
  59. };
  60. class Mul final : public Instruction {
  61. public:
  62. Mul(Register dst, Register src1, Register src2)
  63. : Instruction(Type::Mul)
  64. , m_dst(dst)
  65. , m_src1(src1)
  66. , m_src2(src2)
  67. {
  68. }
  69. void execute(Bytecode::Interpreter&) const;
  70. String to_string() const;
  71. private:
  72. Register m_dst;
  73. Register m_src1;
  74. Register m_src2;
  75. };
  76. class Div final : public Instruction {
  77. public:
  78. Div(Register dst, Register src1, Register src2)
  79. : Instruction(Type::Div)
  80. , m_dst(dst)
  81. , m_src1(src1)
  82. , m_src2(src2)
  83. {
  84. }
  85. void execute(Bytecode::Interpreter&) const;
  86. String to_string() const;
  87. private:
  88. Register m_dst;
  89. Register m_src1;
  90. Register m_src2;
  91. };
  92. class Mod final : public Instruction {
  93. public:
  94. Mod(Register dst, Register src1, Register src2)
  95. : Instruction(Type::Mod)
  96. , m_dst(dst)
  97. , m_src1(src1)
  98. , m_src2(src2)
  99. {
  100. }
  101. void execute(Bytecode::Interpreter&) const;
  102. String to_string() const;
  103. private:
  104. Register m_dst;
  105. Register m_src1;
  106. Register m_src2;
  107. };
  108. class Exp final : public Instruction {
  109. public:
  110. Exp(Register dst, Register src1, Register src2)
  111. : Instruction(Type::Exp)
  112. , m_dst(dst)
  113. , m_src1(src1)
  114. , m_src2(src2)
  115. {
  116. }
  117. void execute(Bytecode::Interpreter&) const;
  118. String to_string() const;
  119. private:
  120. Register m_dst;
  121. Register m_src1;
  122. Register m_src2;
  123. };
  124. class GreaterThan final : public Instruction {
  125. public:
  126. GreaterThan(Register dst, Register src1, Register src2)
  127. : Instruction(Type::GreaterThan)
  128. , m_dst(dst)
  129. , m_src1(src1)
  130. , m_src2(src2)
  131. {
  132. }
  133. void execute(Bytecode::Interpreter&) const;
  134. String to_string() const;
  135. private:
  136. Register m_dst;
  137. Register m_src1;
  138. Register m_src2;
  139. };
  140. class GreaterThanEquals final : public Instruction {
  141. public:
  142. GreaterThanEquals(Register dst, Register src1, Register src2)
  143. : Instruction(Type::GreaterThanEquals)
  144. , m_dst(dst)
  145. , m_src1(src1)
  146. , m_src2(src2)
  147. {
  148. }
  149. void execute(Bytecode::Interpreter&) const;
  150. String to_string() const;
  151. private:
  152. Register m_dst;
  153. Register m_src1;
  154. Register m_src2;
  155. };
  156. class LessThan final : public Instruction {
  157. public:
  158. LessThan(Register dst, Register src1, Register src2)
  159. : Instruction(Type::LessThan)
  160. , m_dst(dst)
  161. , m_src1(src1)
  162. , m_src2(src2)
  163. {
  164. }
  165. void execute(Bytecode::Interpreter&) const;
  166. String to_string() const;
  167. private:
  168. Register m_dst;
  169. Register m_src1;
  170. Register m_src2;
  171. };
  172. class LessThanEquals final : public Instruction {
  173. public:
  174. LessThanEquals(Register dst, Register src1, Register src2)
  175. : Instruction(Type::LessThanEquals)
  176. , m_dst(dst)
  177. , m_src1(src1)
  178. , m_src2(src2)
  179. {
  180. }
  181. void execute(Bytecode::Interpreter&) const;
  182. String to_string() const;
  183. private:
  184. Register m_dst;
  185. Register m_src1;
  186. Register m_src2;
  187. };
  188. class AbstractInequals final : public Instruction {
  189. public:
  190. AbstractInequals(Register dst, Register src1, Register src2)
  191. : Instruction(Type::AbstractEquals)
  192. , m_dst(dst)
  193. , m_src1(src1)
  194. , m_src2(src2)
  195. {
  196. }
  197. void execute(Bytecode::Interpreter&) const;
  198. String to_string() const;
  199. private:
  200. Register m_dst;
  201. Register m_src1;
  202. Register m_src2;
  203. };
  204. class AbstractEquals final : public Instruction {
  205. public:
  206. AbstractEquals(Register dst, Register src1, Register src2)
  207. : Instruction(Type::AbstractEquals)
  208. , m_dst(dst)
  209. , m_src1(src1)
  210. , m_src2(src2)
  211. {
  212. }
  213. void execute(Bytecode::Interpreter&) const;
  214. String to_string() const;
  215. private:
  216. Register m_dst;
  217. Register m_src1;
  218. Register m_src2;
  219. };
  220. class NewString final : public Instruction {
  221. public:
  222. NewString(Register dst, String string)
  223. : Instruction(Type::NewString)
  224. , m_dst(dst)
  225. , m_string(move(string))
  226. {
  227. }
  228. void execute(Bytecode::Interpreter&) const;
  229. String to_string() const;
  230. private:
  231. Register m_dst;
  232. String m_string;
  233. };
  234. class NewObject final : public Instruction {
  235. public:
  236. explicit NewObject(Register dst)
  237. : Instruction(Type::NewObject)
  238. , m_dst(dst)
  239. {
  240. }
  241. void execute(Bytecode::Interpreter&) const;
  242. String to_string() const;
  243. private:
  244. Register m_dst;
  245. };
  246. class SetVariable final : public Instruction {
  247. public:
  248. SetVariable(FlyString identifier, Register src)
  249. : Instruction(Type::SetVariable)
  250. , m_identifier(move(identifier))
  251. , m_src(src)
  252. {
  253. }
  254. void execute(Bytecode::Interpreter&) const;
  255. String to_string() const;
  256. private:
  257. FlyString m_identifier;
  258. Register m_src;
  259. };
  260. class GetVariable final : public Instruction {
  261. public:
  262. GetVariable(Register dst, FlyString identifier)
  263. : Instruction(Type::GetVariable)
  264. , m_dst(dst)
  265. , m_identifier(move(identifier))
  266. {
  267. }
  268. void execute(Bytecode::Interpreter&) const;
  269. String to_string() const;
  270. private:
  271. Register m_dst;
  272. FlyString m_identifier;
  273. };
  274. class GetById final : public Instruction {
  275. public:
  276. GetById(Register dst, Register base, FlyString property)
  277. : Instruction(Type::GetById)
  278. , m_dst(dst)
  279. , m_base(base)
  280. , m_property(move(property))
  281. {
  282. }
  283. void execute(Bytecode::Interpreter&) const;
  284. String to_string() const;
  285. private:
  286. Register m_dst;
  287. Register m_base;
  288. FlyString m_property;
  289. };
  290. class PutById final : public Instruction {
  291. public:
  292. PutById(Register base, FlyString property, Register src)
  293. : Instruction(Type::PutById)
  294. , m_base(base)
  295. , m_property(move(property))
  296. , m_src(src)
  297. {
  298. }
  299. void execute(Bytecode::Interpreter&) const;
  300. String to_string() const;
  301. private:
  302. Register m_base;
  303. FlyString m_property;
  304. Register m_src;
  305. };
  306. class Jump final : public Instruction {
  307. public:
  308. explicit Jump(Optional<Label> target = {})
  309. : Instruction(Type::Jump)
  310. , m_target(move(target))
  311. {
  312. }
  313. void set_target(Optional<Label> target) { m_target = move(target); }
  314. void execute(Bytecode::Interpreter&) const;
  315. String to_string() const;
  316. private:
  317. Optional<Label> m_target;
  318. };
  319. class JumpIfFalse final : public Instruction {
  320. public:
  321. explicit JumpIfFalse(Register result, Optional<Label> target = {})
  322. : Instruction(Type::JumpIfFalse)
  323. , m_result(result)
  324. , m_target(move(target))
  325. {
  326. }
  327. void set_target(Optional<Label> target) { m_target = move(target); }
  328. void execute(Bytecode::Interpreter&) const;
  329. String to_string() const;
  330. private:
  331. Register m_result;
  332. Optional<Label> m_target;
  333. };
  334. class JumpIfTrue final : public Instruction {
  335. public:
  336. explicit JumpIfTrue(Register result, Optional<Label> target = {})
  337. : Instruction(Type::JumpIfTrue)
  338. , m_result(result)
  339. , m_target(move(target))
  340. {
  341. }
  342. void set_target(Optional<Label> target) { m_target = move(target); }
  343. void execute(Bytecode::Interpreter&) const;
  344. String to_string() const;
  345. private:
  346. Register m_result;
  347. Optional<Label> m_target;
  348. };
  349. // NOTE: This instruction is variable-width depending on the number of arguments!
  350. class Call final : public Instruction {
  351. public:
  352. Call(Register dst, Register callee, Register this_value, Vector<Register> const& arguments)
  353. : Instruction(Type::Call)
  354. , m_dst(dst)
  355. , m_callee(callee)
  356. , m_this_value(this_value)
  357. , m_argument_count(arguments.size())
  358. {
  359. for (size_t i = 0; i < m_argument_count; ++i)
  360. m_arguments[i] = arguments[i];
  361. }
  362. void execute(Bytecode::Interpreter&) const;
  363. String to_string() const;
  364. size_t length() const { return sizeof(*this) + sizeof(Register) * m_argument_count; }
  365. private:
  366. Register m_dst;
  367. Register m_callee;
  368. Register m_this_value;
  369. size_t m_argument_count { 0 };
  370. Register m_arguments[];
  371. };
  372. class EnterScope final : public Instruction {
  373. public:
  374. explicit EnterScope(ScopeNode const& scope_node)
  375. : Instruction(Type::EnterScope)
  376. , m_scope_node(scope_node)
  377. {
  378. }
  379. void execute(Bytecode::Interpreter&) const;
  380. String to_string() const;
  381. private:
  382. ScopeNode const& m_scope_node;
  383. };
  384. class Return final : public Instruction {
  385. public:
  386. explicit Return(Optional<Register> argument)
  387. : Instruction(Type::Return)
  388. , m_argument(move(argument))
  389. {
  390. }
  391. void execute(Bytecode::Interpreter&) const;
  392. String to_string() const;
  393. private:
  394. Optional<Register> m_argument;
  395. };
  396. }