Op.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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 TypedInequals final : public Instruction {
  221. public:
  222. TypedInequals(Register dst, Register src1, Register src2)
  223. : Instruction(Type::TypedInequals)
  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 TypedEquals final : public Instruction {
  237. public:
  238. TypedEquals(Register dst, Register src1, Register src2)
  239. : Instruction(Type::TypedEquals)
  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 BitwiseAnd final : public Instruction {
  253. public:
  254. BitwiseAnd(Register dst, Register src1, Register src2)
  255. : Instruction(Type::BitwiseAnd)
  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 BitwiseOr final : public Instruction {
  269. public:
  270. BitwiseOr(Register dst, Register src1, Register src2)
  271. : Instruction(Type::BitwiseOr)
  272. , m_dst(dst)
  273. , m_src1(src1)
  274. , m_src2(src2)
  275. {
  276. }
  277. void execute(Bytecode::Interpreter&) const;
  278. String to_string() const;
  279. private:
  280. Register m_dst;
  281. Register m_src1;
  282. Register m_src2;
  283. };
  284. class BitwiseXor final : public Instruction {
  285. public:
  286. BitwiseXor(Register dst, Register src1, Register src2)
  287. : Instruction(Type::BitwiseXor)
  288. , m_dst(dst)
  289. , m_src1(src1)
  290. , m_src2(src2)
  291. {
  292. }
  293. void execute(Bytecode::Interpreter&) const;
  294. String to_string() const;
  295. private:
  296. Register m_dst;
  297. Register m_src1;
  298. Register m_src2;
  299. };
  300. class LeftShift final : public Instruction {
  301. public:
  302. LeftShift(Register dst, Register src1, Register src2)
  303. : Instruction(Type::LeftShift)
  304. , m_dst(dst)
  305. , m_src1(src1)
  306. , m_src2(src2)
  307. {
  308. }
  309. void execute(Bytecode::Interpreter&) const;
  310. String to_string() const;
  311. private:
  312. Register m_dst;
  313. Register m_src1;
  314. Register m_src2;
  315. };
  316. class RightShift final : public Instruction {
  317. public:
  318. RightShift(Register dst, Register src1, Register src2)
  319. : Instruction(Type::RightShift)
  320. , m_dst(dst)
  321. , m_src1(src1)
  322. , m_src2(src2)
  323. {
  324. }
  325. void execute(Bytecode::Interpreter&) const;
  326. String to_string() const;
  327. private:
  328. Register m_dst;
  329. Register m_src1;
  330. Register m_src2;
  331. };
  332. class UnsignedRightShift final : public Instruction {
  333. public:
  334. UnsignedRightShift(Register dst, Register src1, Register src2)
  335. : Instruction(Type::UnsignedRightShift)
  336. , m_dst(dst)
  337. , m_src1(src1)
  338. , m_src2(src2)
  339. {
  340. }
  341. void execute(Bytecode::Interpreter&) const;
  342. String to_string() const;
  343. private:
  344. Register m_dst;
  345. Register m_src1;
  346. Register m_src2;
  347. };
  348. class BitwiseNot final : public Instruction {
  349. public:
  350. BitwiseNot(Register dst, Register src)
  351. : Instruction(Type::BitwiseNot)
  352. , m_dst(dst)
  353. , m_src(src)
  354. {
  355. }
  356. void execute(Bytecode::Interpreter&) const;
  357. String to_string() const;
  358. private:
  359. Register m_dst;
  360. Register m_src;
  361. };
  362. class Not final : public Instruction {
  363. public:
  364. Not(Register dst, Register src)
  365. : Instruction(Type::Not)
  366. , m_dst(dst)
  367. , m_src(src)
  368. {
  369. }
  370. void execute(Bytecode::Interpreter&) const;
  371. String to_string() const;
  372. private:
  373. Register m_dst;
  374. Register m_src;
  375. };
  376. class UnaryPlus final : public Instruction {
  377. public:
  378. UnaryPlus(Register dst, Register src)
  379. : Instruction(Type::UnaryPlus)
  380. , m_dst(dst)
  381. , m_src(src)
  382. {
  383. }
  384. void execute(Bytecode::Interpreter&) const;
  385. String to_string() const;
  386. private:
  387. Register m_dst;
  388. Register m_src;
  389. };
  390. class UnaryMinus final : public Instruction {
  391. public:
  392. UnaryMinus(Register dst, Register src)
  393. : Instruction(Type::UnaryMinus)
  394. , m_dst(dst)
  395. , m_src(src)
  396. {
  397. }
  398. void execute(Bytecode::Interpreter&) const;
  399. String to_string() const;
  400. private:
  401. Register m_dst;
  402. Register m_src;
  403. };
  404. class Typeof final : public Instruction {
  405. public:
  406. Typeof(Register dst, Register src)
  407. : Instruction(Type::Typeof)
  408. , m_dst(dst)
  409. , m_src(src)
  410. {
  411. }
  412. void execute(Bytecode::Interpreter&) const;
  413. String to_string() const;
  414. private:
  415. Register m_dst;
  416. Register m_src;
  417. };
  418. class NewString final : public Instruction {
  419. public:
  420. NewString(Register dst, String string)
  421. : Instruction(Type::NewString)
  422. , m_dst(dst)
  423. , m_string(move(string))
  424. {
  425. }
  426. void execute(Bytecode::Interpreter&) const;
  427. String to_string() const;
  428. private:
  429. Register m_dst;
  430. String m_string;
  431. };
  432. class NewObject final : public Instruction {
  433. public:
  434. explicit NewObject(Register dst)
  435. : Instruction(Type::NewObject)
  436. , m_dst(dst)
  437. {
  438. }
  439. void execute(Bytecode::Interpreter&) const;
  440. String to_string() const;
  441. private:
  442. Register m_dst;
  443. };
  444. class SetVariable final : public Instruction {
  445. public:
  446. SetVariable(FlyString identifier, Register src)
  447. : Instruction(Type::SetVariable)
  448. , m_identifier(move(identifier))
  449. , m_src(src)
  450. {
  451. }
  452. void execute(Bytecode::Interpreter&) const;
  453. String to_string() const;
  454. private:
  455. FlyString m_identifier;
  456. Register m_src;
  457. };
  458. class GetVariable final : public Instruction {
  459. public:
  460. GetVariable(Register dst, FlyString identifier)
  461. : Instruction(Type::GetVariable)
  462. , m_dst(dst)
  463. , m_identifier(move(identifier))
  464. {
  465. }
  466. void execute(Bytecode::Interpreter&) const;
  467. String to_string() const;
  468. private:
  469. Register m_dst;
  470. FlyString m_identifier;
  471. };
  472. class GetById final : public Instruction {
  473. public:
  474. GetById(Register dst, Register base, FlyString property)
  475. : Instruction(Type::GetById)
  476. , m_dst(dst)
  477. , m_base(base)
  478. , m_property(move(property))
  479. {
  480. }
  481. void execute(Bytecode::Interpreter&) const;
  482. String to_string() const;
  483. private:
  484. Register m_dst;
  485. Register m_base;
  486. FlyString m_property;
  487. };
  488. class PutById final : public Instruction {
  489. public:
  490. PutById(Register base, FlyString property, Register src)
  491. : Instruction(Type::PutById)
  492. , m_base(base)
  493. , m_property(move(property))
  494. , m_src(src)
  495. {
  496. }
  497. void execute(Bytecode::Interpreter&) const;
  498. String to_string() const;
  499. private:
  500. Register m_base;
  501. FlyString m_property;
  502. Register m_src;
  503. };
  504. class Jump final : public Instruction {
  505. public:
  506. explicit Jump(Optional<Label> target = {})
  507. : Instruction(Type::Jump)
  508. , m_target(move(target))
  509. {
  510. }
  511. void set_target(Optional<Label> target) { m_target = move(target); }
  512. void execute(Bytecode::Interpreter&) const;
  513. String to_string() const;
  514. private:
  515. Optional<Label> m_target;
  516. };
  517. class JumpIfFalse final : public Instruction {
  518. public:
  519. explicit JumpIfFalse(Register result, Optional<Label> target = {})
  520. : Instruction(Type::JumpIfFalse)
  521. , m_result(result)
  522. , m_target(move(target))
  523. {
  524. }
  525. void set_target(Optional<Label> target) { m_target = move(target); }
  526. void execute(Bytecode::Interpreter&) const;
  527. String to_string() const;
  528. private:
  529. Register m_result;
  530. Optional<Label> m_target;
  531. };
  532. class JumpIfTrue final : public Instruction {
  533. public:
  534. explicit JumpIfTrue(Register result, Optional<Label> target = {})
  535. : Instruction(Type::JumpIfTrue)
  536. , m_result(result)
  537. , m_target(move(target))
  538. {
  539. }
  540. void set_target(Optional<Label> target) { m_target = move(target); }
  541. void execute(Bytecode::Interpreter&) const;
  542. String to_string() const;
  543. private:
  544. Register m_result;
  545. Optional<Label> m_target;
  546. };
  547. // NOTE: This instruction is variable-width depending on the number of arguments!
  548. class Call final : public Instruction {
  549. public:
  550. Call(Register dst, Register callee, Register this_value, Vector<Register> const& arguments)
  551. : Instruction(Type::Call)
  552. , m_dst(dst)
  553. , m_callee(callee)
  554. , m_this_value(this_value)
  555. , m_argument_count(arguments.size())
  556. {
  557. for (size_t i = 0; i < m_argument_count; ++i)
  558. m_arguments[i] = arguments[i];
  559. }
  560. void execute(Bytecode::Interpreter&) const;
  561. String to_string() const;
  562. size_t length() const { return sizeof(*this) + sizeof(Register) * m_argument_count; }
  563. private:
  564. Register m_dst;
  565. Register m_callee;
  566. Register m_this_value;
  567. size_t m_argument_count { 0 };
  568. Register m_arguments[];
  569. };
  570. class EnterScope final : public Instruction {
  571. public:
  572. explicit EnterScope(ScopeNode const& scope_node)
  573. : Instruction(Type::EnterScope)
  574. , m_scope_node(scope_node)
  575. {
  576. }
  577. void execute(Bytecode::Interpreter&) const;
  578. String to_string() const;
  579. private:
  580. ScopeNode const& m_scope_node;
  581. };
  582. class Return final : public Instruction {
  583. public:
  584. explicit Return(Optional<Register> argument)
  585. : Instruction(Type::Return)
  586. , m_argument(move(argument))
  587. {
  588. }
  589. void execute(Bytecode::Interpreter&) const;
  590. String to_string() const;
  591. private:
  592. Optional<Register> m_argument;
  593. };
  594. }