Op.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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::AbstractInequals)
  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 BitwiseAnd final : public Instruction {
  221. public:
  222. BitwiseAnd(Register dst, Register src1, Register src2)
  223. : Instruction(Type::BitwiseAnd)
  224. , m_dst(dst)
  225. , m_src1(src1)
  226. , m_src2(src2)
  227. {
  228. }
  229. void execute(Bytecode::Interpreter&) const;
  230. String to_string() const;
  231. private:
  232. Register m_dst;
  233. Register m_src1;
  234. Register m_src2;
  235. };
  236. class BitwiseOr final : public Instruction {
  237. public:
  238. BitwiseOr(Register dst, Register src1, Register src2)
  239. : Instruction(Type::BitwiseOr)
  240. , m_dst(dst)
  241. , m_src1(src1)
  242. , m_src2(src2)
  243. {
  244. }
  245. void execute(Bytecode::Interpreter&) const;
  246. String to_string() const;
  247. private:
  248. Register m_dst;
  249. Register m_src1;
  250. Register m_src2;
  251. };
  252. class BitwiseXor final : public Instruction {
  253. public:
  254. BitwiseXor(Register dst, Register src1, Register src2)
  255. : Instruction(Type::BitwiseXor)
  256. , m_dst(dst)
  257. , m_src1(src1)
  258. , m_src2(src2)
  259. {
  260. }
  261. void execute(Bytecode::Interpreter&) const;
  262. String to_string() const;
  263. private:
  264. Register m_dst;
  265. Register m_src1;
  266. Register m_src2;
  267. };
  268. class BitwiseNot final : public Instruction {
  269. public:
  270. BitwiseNot(Register dst, Register src)
  271. : Instruction(Type::BitwiseNot)
  272. , m_dst(dst)
  273. , m_src(src)
  274. {
  275. }
  276. void execute(Bytecode::Interpreter&) const;
  277. String to_string() const;
  278. private:
  279. Register m_dst;
  280. Register m_src;
  281. };
  282. class Not final : public Instruction {
  283. public:
  284. Not(Register dst, Register src)
  285. : Instruction(Type::Not)
  286. , m_dst(dst)
  287. , m_src(src)
  288. {
  289. }
  290. void execute(Bytecode::Interpreter&) const;
  291. String to_string() const;
  292. private:
  293. Register m_dst;
  294. Register m_src;
  295. };
  296. class UnaryPlus final : public Instruction {
  297. public:
  298. UnaryPlus(Register dst, Register src)
  299. : Instruction(Type::UnaryPlus)
  300. , m_dst(dst)
  301. , m_src(src)
  302. {
  303. }
  304. void execute(Bytecode::Interpreter&) const;
  305. String to_string() const;
  306. private:
  307. Register m_dst;
  308. Register m_src;
  309. };
  310. class UnaryMinus final : public Instruction {
  311. public:
  312. UnaryMinus(Register dst, Register src)
  313. : Instruction(Type::UnaryMinus)
  314. , m_dst(dst)
  315. , m_src(src)
  316. {
  317. }
  318. void execute(Bytecode::Interpreter&) const;
  319. String to_string() const;
  320. private:
  321. Register m_dst;
  322. Register m_src;
  323. };
  324. class Typeof final : public Instruction {
  325. public:
  326. Typeof(Register dst, Register src)
  327. : Instruction(Type::Typeof)
  328. , m_dst(dst)
  329. , m_src(src)
  330. {
  331. }
  332. void execute(Bytecode::Interpreter&) const;
  333. String to_string() const;
  334. private:
  335. Register m_dst;
  336. Register m_src;
  337. };
  338. class NewString final : public Instruction {
  339. public:
  340. NewString(Register dst, String string)
  341. : Instruction(Type::NewString)
  342. , m_dst(dst)
  343. , m_string(move(string))
  344. {
  345. }
  346. void execute(Bytecode::Interpreter&) const;
  347. String to_string() const;
  348. private:
  349. Register m_dst;
  350. String m_string;
  351. };
  352. class NewObject final : public Instruction {
  353. public:
  354. explicit NewObject(Register dst)
  355. : Instruction(Type::NewObject)
  356. , m_dst(dst)
  357. {
  358. }
  359. void execute(Bytecode::Interpreter&) const;
  360. String to_string() const;
  361. private:
  362. Register m_dst;
  363. };
  364. class SetVariable final : public Instruction {
  365. public:
  366. SetVariable(FlyString identifier, Register src)
  367. : Instruction(Type::SetVariable)
  368. , m_identifier(move(identifier))
  369. , m_src(src)
  370. {
  371. }
  372. void execute(Bytecode::Interpreter&) const;
  373. String to_string() const;
  374. private:
  375. FlyString m_identifier;
  376. Register m_src;
  377. };
  378. class GetVariable final : public Instruction {
  379. public:
  380. GetVariable(Register dst, FlyString identifier)
  381. : Instruction(Type::GetVariable)
  382. , m_dst(dst)
  383. , m_identifier(move(identifier))
  384. {
  385. }
  386. void execute(Bytecode::Interpreter&) const;
  387. String to_string() const;
  388. private:
  389. Register m_dst;
  390. FlyString m_identifier;
  391. };
  392. class GetById final : public Instruction {
  393. public:
  394. GetById(Register dst, Register base, FlyString property)
  395. : Instruction(Type::GetById)
  396. , m_dst(dst)
  397. , m_base(base)
  398. , m_property(move(property))
  399. {
  400. }
  401. void execute(Bytecode::Interpreter&) const;
  402. String to_string() const;
  403. private:
  404. Register m_dst;
  405. Register m_base;
  406. FlyString m_property;
  407. };
  408. class PutById final : public Instruction {
  409. public:
  410. PutById(Register base, FlyString property, Register src)
  411. : Instruction(Type::PutById)
  412. , m_base(base)
  413. , m_property(move(property))
  414. , m_src(src)
  415. {
  416. }
  417. void execute(Bytecode::Interpreter&) const;
  418. String to_string() const;
  419. private:
  420. Register m_base;
  421. FlyString m_property;
  422. Register m_src;
  423. };
  424. class Jump final : public Instruction {
  425. public:
  426. explicit Jump(Optional<Label> target = {})
  427. : Instruction(Type::Jump)
  428. , m_target(move(target))
  429. {
  430. }
  431. void set_target(Optional<Label> target) { m_target = move(target); }
  432. void execute(Bytecode::Interpreter&) const;
  433. String to_string() const;
  434. private:
  435. Optional<Label> m_target;
  436. };
  437. class JumpIfFalse final : public Instruction {
  438. public:
  439. explicit JumpIfFalse(Register result, Optional<Label> target = {})
  440. : Instruction(Type::JumpIfFalse)
  441. , m_result(result)
  442. , m_target(move(target))
  443. {
  444. }
  445. void set_target(Optional<Label> target) { m_target = move(target); }
  446. void execute(Bytecode::Interpreter&) const;
  447. String to_string() const;
  448. private:
  449. Register m_result;
  450. Optional<Label> m_target;
  451. };
  452. class JumpIfTrue final : public Instruction {
  453. public:
  454. explicit JumpIfTrue(Register result, Optional<Label> target = {})
  455. : Instruction(Type::JumpIfTrue)
  456. , m_result(result)
  457. , m_target(move(target))
  458. {
  459. }
  460. void set_target(Optional<Label> target) { m_target = move(target); }
  461. void execute(Bytecode::Interpreter&) const;
  462. String to_string() const;
  463. private:
  464. Register m_result;
  465. Optional<Label> m_target;
  466. };
  467. // NOTE: This instruction is variable-width depending on the number of arguments!
  468. class Call final : public Instruction {
  469. public:
  470. Call(Register dst, Register callee, Register this_value, Vector<Register> const& arguments)
  471. : Instruction(Type::Call)
  472. , m_dst(dst)
  473. , m_callee(callee)
  474. , m_this_value(this_value)
  475. , m_argument_count(arguments.size())
  476. {
  477. for (size_t i = 0; i < m_argument_count; ++i)
  478. m_arguments[i] = arguments[i];
  479. }
  480. void execute(Bytecode::Interpreter&) const;
  481. String to_string() const;
  482. size_t length() const { return sizeof(*this) + sizeof(Register) * m_argument_count; }
  483. private:
  484. Register m_dst;
  485. Register m_callee;
  486. Register m_this_value;
  487. size_t m_argument_count { 0 };
  488. Register m_arguments[];
  489. };
  490. class EnterScope final : public Instruction {
  491. public:
  492. explicit EnterScope(ScopeNode const& scope_node)
  493. : Instruction(Type::EnterScope)
  494. , m_scope_node(scope_node)
  495. {
  496. }
  497. void execute(Bytecode::Interpreter&) const;
  498. String to_string() const;
  499. private:
  500. ScopeNode const& m_scope_node;
  501. };
  502. class Return final : public Instruction {
  503. public:
  504. explicit Return(Optional<Register> argument)
  505. : Instruction(Type::Return)
  506. , m_argument(move(argument))
  507. {
  508. }
  509. void execute(Bytecode::Interpreter&) const;
  510. String to_string() const;
  511. private:
  512. Optional<Register> m_argument;
  513. };
  514. }