Value.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <deque>
  17. #include <exception>
  18. #include <ostream>
  19. #include <string>
  20. #include <boost/intrusive_ptr.hpp>
  21. #include "common/scummsys.h"
  22. #include "RefCounted.h"
  23. #include "Stack.h"
  24. #include "DecompilerException.h"
  25. class Value;
  26. /**
  27. * Precedence value for individual values with no operations.
  28. */
  29. const int PRECEDENCE_NO = 0;
  30. /**
  31. * Precedence value for a unary operation. (!, -, ~, etc.).
  32. */
  33. const int PRECEDENCE_UNARY = 1;
  34. /**
  35. * Precedence value for multiplication, division, modulus (*, /, %).
  36. */
  37. const int PRECEDENCE_MULT = 2;
  38. /**
  39. * Precedence value for addition and subtraction (+, -).
  40. */
  41. const int PRECEDENCE_ADD = 3;
  42. /**
  43. * Precedence value for bit shifting (<<, >>).
  44. */
  45. const int PRECEDENCE_SHIFT = 4;
  46. /**
  47. * Precedence value for relative comparison (<, <=, >=, >).
  48. */
  49. const int PRECEDENCE_RELATION = 5;
  50. /**
  51. * Precedence value for equality comparisons (==, !=).
  52. */
  53. const int PRECEDENCE_EQUALITY = 6;
  54. /**
  55. * Precedence value for bitwise AND (&).
  56. */
  57. const int PRECEDENCE_BIT_AND = 7;
  58. /**
  59. * Precedence value for bitwise XOR (^).
  60. */
  61. const int PRECEDENCE_BIT_XOR = 8;
  62. /**
  63. * Precedence value for bitwise OR (|).
  64. */
  65. const int PRECEDENCE_BIT_OR = 9;
  66. /**
  67. * Precedence value for logical AND (&&).
  68. */
  69. const int PRECEDENCE_LOGIC_AND = 10;
  70. /**
  71. * Precedence value for logical OR (||).
  72. */
  73. const int PRECEDENCE_LOGIC_OR = 11;
  74. /**
  75. * Pointer to a Value.
  76. */
  77. typedef boost::intrusive_ptr<Value> ValuePtr;
  78. /**
  79. * Type representing a list of values, e.g. for indexes used to access an array.
  80. */
  81. typedef std::deque<ValuePtr> ValueList;
  82. /**
  83. * Type representing a stack.
  84. */
  85. typedef Stack<ValuePtr> ValueStack;
  86. /**
  87. * Class representing a value (stack entry, parameter, etc.)
  88. */
  89. class Value : public RefCounted{
  90. public:
  91. /**
  92. * Destructor.
  93. */
  94. virtual ~Value();
  95. /**
  96. * Return whether or not the Value is an integer.
  97. *
  98. * @return True if the Value is an integer, otherwise false.
  99. */
  100. virtual bool IsInteger();
  101. /**
  102. * Return whether or not the Value is an address.
  103. *
  104. * @return True if the Value is an address, otherwise false.
  105. */
  106. virtual bool IsAddress();
  107. /**
  108. * Returns whether or not any stored integer value is signed.
  109. *
  110. * @return True if the integer value is signed, false if it is not.
  111. * @throws WrongTypeException if the value is not an integer.
  112. */
  113. virtual bool IsSignedValue();
  114. /**
  115. * Retrieves a signed integer representing the value, if possible.
  116. *
  117. * @return A signed integer representing the value, if possible.
  118. * @throws WrongTypeException if the value is not an integer.
  119. */
  120. virtual int32 GetSigned();
  121. /**
  122. * Retrieves an unsigned integer representing the value, if possible.
  123. *
  124. * @return An unsigned integer representing the value, if possible.
  125. * @throws WrongTypeException if the value is not an integer.
  126. */
  127. virtual uint32 GetUnsigned();
  128. /**
  129. * Print the value to a stream.
  130. *
  131. * @param output[out] The stream to write to.
  132. * @return The stream used for output.
  133. */
  134. virtual std::ostream& Print(std::ostream &output) const = 0;
  135. /**
  136. * Retrieves the string representation of the value.
  137. *
  138. * @return The string representation of the value.
  139. */
  140. virtual std::string GetString() const;
  141. /**
  142. * Duplicates a value.
  143. *
  144. * @param output[out] The stream to output any necessary assignment.
  145. * @return A Value corresponding to a duplicate of this entry.
  146. */
  147. virtual ValuePtr Dup(std::ostream &output);
  148. /**
  149. * Negates a value.
  150. *
  151. * @return The current Value, negated.
  152. * @throws WrongTypeException if negation is not possible.
  153. */
  154. virtual ValuePtr Negate();
  155. /**
  156. * Operator precedence for this value.
  157. *
  158. * Lower values bind stronger, i.e. they are resolved earlier.
  159. * If an operand has a higher precedence value than the operator,
  160. * parentheses are not required for that operand.
  161. *
  162. * @return The order of precedence.
  163. */
  164. virtual int GetPrecedence() const;
  165. /**
  166. * Output a value to a stream.
  167. *
  168. * @param output[out] The stream to output to.
  169. * @param value[in] Reference counted pointer to the value to output.
  170. * @return The stream used for output.
  171. */
  172. friend std::ostream &operator<<(std::ostream &output, Value *value) {
  173. return value->Print(output);
  174. }
  175. };
  176. /**
  177. * Value containing an integer.
  178. */
  179. class IntValue : public Value {
  180. public:
  181. /**
  182. * Copy constructor, disabled.
  183. *
  184. * @param value[in] The value to copy.
  185. */
  186. IntValue(const IntValue& value) = delete;
  187. /**
  188. * Copy constructor, disabled
  189. *
  190. * @param value[in] The value to copy.
  191. */
  192. IntValue& operator = (const IntValue& value) = delete;
  193. /**
  194. * Constructor for IntValue.
  195. *
  196. * @param val The integer value to be contained.
  197. * @param isSigned Whether or not the value is signed. This will affect output.
  198. */
  199. IntValue(int32 val, bool is_signed);
  200. /**
  201. * Constructor for IntValue.
  202. *
  203. * @param val The integer value to be contained.
  204. * @param isSigned Whether or not the value is signed. This will affect output.
  205. */
  206. IntValue(uint32 val, bool is_signed);
  207. /**
  208. * Return whether or not the Value is an integer.
  209. *
  210. * @return True.
  211. */
  212. bool IsInteger() override;
  213. /**
  214. * Returns whether or not the stored integer value is signed.
  215. *
  216. * @return True if the integer value is signed, false if it is not.
  217. */
  218. bool IsSignedValue() override;
  219. /**
  220. * Retrieves a signed integer representing the value, if possible.
  221. *
  222. * @return A signed integer representing the value, if possible.
  223. */
  224. int32 GetSigned() override;
  225. /**
  226. * Retrieves an unsigned integer representing the value, if possible.
  227. *
  228. * @return An unsigned integer representing the value, if possible.
  229. * @throws WrongTypeException if the value is not an integer.
  230. */
  231. uint32 GetUnsigned() override;
  232. /**
  233. * Duplicates the value.
  234. *
  235. * @param output[out] The stream to output any necessary assignment.
  236. * @return A Value corresponding to a duplicate of the value.
  237. */
  238. ValuePtr Dup(std::ostream &output) override;
  239. /**
  240. * Print the value to a stream.
  241. *
  242. * @param output[out] The stream to write to.
  243. * @return The stream used for output.
  244. */
  245. virtual std::ostream& Print(std::ostream &output) const override;
  246. protected:
  247. /**
  248. * The value of the integer.
  249. */
  250. const int32 val_;
  251. /**
  252. * True if the value is signed, false if it's not.
  253. */
  254. const bool signed_;
  255. };
  256. /**
  257. * Value containing an absolute address.
  258. */
  259. class AddressValue: public IntValue{
  260. public:
  261. /**
  262. * Copy constructor, disabled.
  263. *
  264. * @param value[in] The value to copy.
  265. */
  266. AddressValue(const AddressValue& value) = delete;
  267. /**
  268. * Copy constructor, disabled.
  269. *
  270. * @param value[in] The value to copy.
  271. */
  272. AddressValue& operator = (const AddressValue& value) = delete;
  273. /**
  274. * Constructor for AddressValue.
  275. *
  276. * @param addr The absolute address represented by the value.
  277. */
  278. explicit AddressValue(uint32 addr);
  279. /**
  280. * Return whether or not the Value is an address.
  281. *
  282. * @return True.
  283. */
  284. bool IsAddress() override;
  285. /**
  286. * Always throws {@see WrongTypeException}.
  287. *
  288. * A memory address can't ever be signed.
  289. *
  290. * @return Nothing.
  291. * @throws WrongTypeException always.
  292. */
  293. int32 GetSigned() override;
  294. /**
  295. * Duplicates the value.
  296. *
  297. * @param output[out] The stream to output any necessary assignment.
  298. * @return A Value corresponding to a duplicate of the value.
  299. */
  300. ValuePtr Dup(std::ostream &output) override;
  301. /**
  302. * Print the value to a stream.
  303. *
  304. * @param output[out] The stream to write to.
  305. * @return The stream used for output.
  306. */
  307. virtual std::ostream& Print(std::ostream &output) const override;
  308. };
  309. /**
  310. * Value containing a signed, relative address.
  311. *
  312. * When asking for unsigned integer value, exact address is returned; when
  313. * printing or getting signed value, relative address is used.
  314. */
  315. class RelAddressValue : public IntValue{
  316. public:
  317. /**
  318. * Copy constructor, disabled.
  319. *
  320. * @param value[in] The value to copy.
  321. */
  322. RelAddressValue(const RelAddressValue& value) = delete;
  323. /**
  324. * Copy constructor, disabled.
  325. *
  326. * @param value[in] The value to copy.
  327. */
  328. RelAddressValue& operator = (const RelAddressValue& value) = delete;
  329. /**
  330. * Constructor.
  331. *
  332. * @param base_addr[in] The base address for the offset.
  333. * @param offset[in] The relative offset to the base address.
  334. */
  335. RelAddressValue(uint32 base_addr, int32 offset);
  336. /**
  337. * Return whether or not the Value is an address.
  338. *
  339. * @return True.
  340. */
  341. bool IsAddress() override;
  342. /**
  343. * Retrieves the exact address.
  344. *
  345. * @return The exact address.
  346. * @throws WrongTypeException if the value is not an integer.
  347. */
  348. uint32 GetUnsigned() override;
  349. /**
  350. * Duplicates the value.
  351. *
  352. * @param output[out] The stream to output any necessary assignment.
  353. * @return A Value corresponding to a duplicate of the value.
  354. */
  355. ValuePtr Dup(std::ostream &output) override;
  356. /**
  357. * Print the relative address to a stream.
  358. *
  359. * @param output[out] The stream to write to.
  360. * @return The stream used for output.
  361. */
  362. virtual std::ostream& Print(std::ostream &output) const override;
  363. protected:
  364. /**
  365. * The base address for the offset.
  366. */
  367. const uint32 base_addr_;
  368. };
  369. /**
  370. * Duplicated value.
  371. */
  372. class DupValue : public Value {
  373. public:
  374. /**
  375. * Copy constructor, disabled.
  376. *
  377. * @param value[in] The value to copy.
  378. */
  379. DupValue(const DupValue& value) = delete;
  380. /**
  381. * Copy constructor, disabled.
  382. *
  383. * @param value[in] The value to copy.
  384. */
  385. DupValue& operator = (const DupValue& value) = delete;
  386. /**
  387. * Constructor.
  388. *
  389. * @param idx Index to distinguish multiple duplicated entries.
  390. */
  391. explicit DupValue(int idx);
  392. /**
  393. * Duplicates the value.
  394. *
  395. * @param output[out] The stream to output any necessary assignment.
  396. * @return A Value corresponding to a duplicate of the value.
  397. */
  398. ValuePtr Dup(std::ostream &output) override;
  399. /**
  400. * Print the value to a stream.
  401. *
  402. * @param output[out] The stream to write to.
  403. * @return The stream used for output.
  404. */
  405. virtual std::ostream& Print(std::ostream &output) const override;
  406. protected:
  407. /**
  408. * Index to distinguish multiple duplicated entries.
  409. */
  410. const int index_;
  411. };
  412. /**
  413. * String value.
  414. */
  415. class StringValue : public Value {
  416. public:
  417. /**
  418. * Copy constructor, disabled.
  419. *
  420. * @param value[in] The value to copy.
  421. */
  422. StringValue(const StringValue& value) = delete;
  423. /**
  424. * Copy constructor, disabled.
  425. *
  426. * @param value[in] The value to copy.
  427. */
  428. StringValue& operator = (const StringValue& value) = delete;
  429. /**
  430. * Constructor.
  431. *
  432. * @param str[in] The string value.
  433. */
  434. explicit StringValue(std::string str);
  435. /**
  436. * Print the value to a stream.
  437. *
  438. * @param output[out] The stream to write to.
  439. * @return The stream used for output.
  440. */
  441. virtual std::ostream& Print(std::ostream &output) const override;
  442. protected:
  443. /**
  444. * The string value.
  445. */
  446. const std::string str_;
  447. };
  448. /**
  449. * A string value, unquoted.
  450. */
  451. class UnquotedStringValue : public StringValue{
  452. public:
  453. /**
  454. * Constructor.
  455. *
  456. * @param str[in] The string value.
  457. */
  458. explicit UnquotedStringValue(std::string str);
  459. /**
  460. * Prints the value to a stream.
  461. *
  462. * @param output[out] The stream to write to.
  463. * @return The stream used for output.
  464. */
  465. virtual std::ostream& Print(std::ostream &output) const override;
  466. };
  467. /**
  468. * Value representing a variable.
  469. */
  470. class VarValue : public Value {
  471. public:
  472. /**
  473. * Constructor for VarValue.
  474. *
  475. * @param name The variable name.
  476. */
  477. explicit VarValue(std::string name);
  478. /**
  479. * Print the value to a stream.
  480. *
  481. * @param output[out] The stream to write to.
  482. * @return The stream used for output.
  483. */
  484. virtual std::ostream& Print(std::ostream &output) const override;
  485. protected:
  486. /**
  487. * The variable name.
  488. */
  489. std::string name_;
  490. };
  491. /**
  492. * Value representing array access.
  493. */
  494. class ArrayValue : public VarValue {
  495. public:
  496. /**
  497. * Copy constructor, disabled.
  498. *
  499. * @param value[in] The value to copy.
  500. */
  501. ArrayValue(const ArrayValue& value) = delete;
  502. /**
  503. * Copy constructor, disabled.
  504. *
  505. * @param value[in] The value to copy.
  506. */
  507. ArrayValue& operator = (const ArrayValue& value) = delete;
  508. /**
  509. * Constructor for ArrayValue.
  510. *
  511. * @param name The name of the array.
  512. * @param indexes List of stack entries representing the indexes used
  513. * (left-to-right).
  514. */
  515. ArrayValue(const std::string name, const ValueList indexes);
  516. /**
  517. * Print the value to a stream.
  518. *
  519. * Every item in the array will be printed.
  520. *
  521. * @param output[out] The stream to write to.
  522. * @return The stream used for output.
  523. */
  524. virtual std::ostream& Print(std::ostream &output) const override;
  525. protected:
  526. /**
  527. * Values representing the indexes used (left-to-right).
  528. */
  529. const ValueList indexes_;
  530. };
  531. /**
  532. * Value representing the result of a binary operation.
  533. */
  534. class BinaryOpValue : public Value {
  535. public:
  536. /**
  537. * Copy constructor, disabled.
  538. *
  539. * @param value[in] The value to copy.
  540. */
  541. BinaryOpValue(const BinaryOpValue& value) = delete;
  542. /**
  543. * Copy constructor, disabled.
  544. *
  545. * @param value[in] The value to copy.
  546. */
  547. BinaryOpValue& operator = (const BinaryOpValue& value) = delete;
  548. /**
  549. * Constructor.
  550. *
  551. * @param left[in] Value representing the left side of the operator.
  552. * @param right[in] Value representing the right side of the operator.
  553. * @param operator[in] The operator for this value.
  554. */
  555. BinaryOpValue(ValuePtr left, ValuePtr right, std::string oper);
  556. /**
  557. * Print the value to a stream.
  558. *
  559. * @param output[out] The stream to write to.
  560. * @return The stream used for output.
  561. */
  562. virtual std::ostream& Print(std::ostream &output) const override;
  563. /**
  564. * Negates a value.
  565. *
  566. * @return The value, negated.
  567. */
  568. virtual ValuePtr Negate() override;
  569. /**
  570. * Retrieves the operator precedence for this operation.
  571. *
  572. * Lower values bind stronger, i.e. they are resolved earlier.
  573. * If an operand has a higher precedence value than the operator,
  574. * parentheses are not required for that operand.
  575. *
  576. * @return the order of precedence for the operation.
  577. */
  578. virtual int GetPrecedence() const override;
  579. protected:
  580. /**
  581. * Value at the left side of the operator.
  582. */
  583. const ValuePtr left_val_;
  584. /**
  585. * Value at the right side of the operator.
  586. */
  587. const ValuePtr right_val_;
  588. /**
  589. * The operator.
  590. */
  591. const std::string oper_;
  592. };
  593. /**
  594. * Value representing the result of a unary operation.
  595. *
  596. * Used as base class for prefix and postfix variants.
  597. */
  598. class UnaryOpValue : public Value{
  599. public:
  600. /**
  601. * Copy constructor, disabled.
  602. *
  603. * @param value[in] The value to copy.
  604. */
  605. UnaryOpValue(const UnaryOpValue& value) = delete;
  606. /**
  607. * Copy constructor, disabled.
  608. *
  609. * @param value[in] The value to copy.
  610. */
  611. UnaryOpValue& operator = (const UnaryOpValue& value) = delete;
  612. /**
  613. * Constructor..
  614. *
  615. * @param operand[in] Value representing the operand of the operation.
  616. * @param oper[in] The operator for this value.
  617. * @param postfix[in] Whether or not the operator should be postfixed
  618. * to the operand.
  619. */
  620. UnaryOpValue(ValuePtr operand, std::string oper, bool postfix);
  621. /**
  622. * Print the value to a stream.
  623. *
  624. * @param output[out] The stream to write to.
  625. * @return The stream used for output.
  626. */
  627. virtual std::ostream& Print(std::ostream &output) const override;
  628. /**
  629. * Retrieves the operator precedence for this operation.
  630. *
  631. * Lower values bind stronger, i.e. they are resolved earlier.
  632. * If an operand has a higher precedence value than the operator,
  633. * parentheses are not required for that operand.
  634. *
  635. * @return {@see PRECEDENCE_UNARY_OP}.
  636. */
  637. virtual int GetPrecedence() const override;
  638. protected:
  639. /**
  640. * The operand of the operation
  641. */
  642. const ValuePtr operand_;
  643. /**
  644. * The operator for this value
  645. */
  646. const std::string oper_;
  647. /**
  648. * True if the operator is postfixed to the operand, false otherwise.
  649. */
  650. const bool postfix_;
  651. };
  652. /**
  653. * Negated value.
  654. */
  655. class NegatedValue : public UnaryOpValue{
  656. public:
  657. /**
  658. * Copy constructor, disabled.
  659. *
  660. * @param value[in] The value to copy.
  661. */
  662. NegatedValue(const NegatedValue& value) = delete;
  663. /**
  664. * Copy constructor, disabled.
  665. *
  666. * @param value[in] The value to copy.
  667. */
  668. NegatedValue& operator = (const NegatedValue& value) = delete;
  669. /**
  670. * Constructor.
  671. *
  672. * @param val[in] The value to negate.
  673. */
  674. explicit NegatedValue(ValuePtr val);
  675. /**
  676. * Negates the value.
  677. *
  678. * @return The value, negated.
  679. */
  680. virtual ValuePtr Negate() override;
  681. };
  682. /**
  683. * Value representing a function call.
  684. */
  685. class CallValue : public Value {
  686. public:
  687. /**
  688. * Copy constructor, disabled.
  689. *
  690. * @param value[in] The value to copy.
  691. */
  692. CallValue(const CallValue& value) = delete;
  693. /**
  694. * Copy constructor, disabled.
  695. *
  696. * @param value[in] The value to copy.
  697. */
  698. CallValue& operator = (const CallValue& value) = delete;
  699. /**
  700. * Constructor for CallValue.
  701. *
  702. * @param function[in] The name of the function.
  703. * @param args[in] List of values representing the arguments used.
  704. */
  705. CallValue(std::string function, ValueList args);
  706. /**
  707. * Print the value to a stream.
  708. *
  709. * @param output[out] The stream to write to.
  710. * @return The stream used for output.
  711. */
  712. virtual std::ostream& Print(std::ostream &output) const override;
  713. protected:
  714. /**
  715. * The name of the function.
  716. */
  717. const std::string function_;
  718. /**
  719. * List of values used as function arguments.
  720. */
  721. const ValueList args_;
  722. };