V-Gears 0
Free Final Fantasy VII engine.
Value.h
Go to the documentation of this file.
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
16#pragma once
17
18#include <deque>
19#include <exception>
20#include <ostream>
21#include <string>
22#include <boost/intrusive_ptr.hpp>
24#include "RefCounted.h"
25#include "Stack.h"
26#include "DecompilerException.h"
27
28class Value;
29
33const int PRECEDENCE_NO = 0;
34
38const int PRECEDENCE_UNARY = 1;
39
43const int PRECEDENCE_MULT = 2;
44
48const int PRECEDENCE_ADD = 3;
49
53const int PRECEDENCE_SHIFT = 4;
54
58const int PRECEDENCE_RELATION = 5;
59
63const int PRECEDENCE_EQUALITY = 6;
64
68const int PRECEDENCE_BIT_AND = 7;
69
73const int PRECEDENCE_BIT_XOR = 8;
74
78const int PRECEDENCE_BIT_OR = 9;
79
83const int PRECEDENCE_LOGIC_AND = 10;
84
88const int PRECEDENCE_LOGIC_OR = 11;
89
93typedef boost::intrusive_ptr<Value> ValuePtr;
94
98typedef std::deque<ValuePtr> ValueList;
99
104
108class Value : public RefCounted{
109
110 public:
111
115 virtual ~Value();
116
122 virtual bool IsInteger();
123
129 virtual bool IsAddress();
130
137 virtual bool IsSignedValue();
138
145 virtual int32 GetSigned();
146
153 virtual uint32 GetUnsigned();
154
161 virtual std::ostream& Print(std::ostream &output) const = 0;
162
168 virtual std::string GetString() const;
169
176 virtual ValuePtr Dup(std::ostream &output);
177
184 virtual ValuePtr Negate();
185
195 virtual int GetPrecedence() const;
196
204 friend std::ostream &operator<<(std::ostream &output, Value *value) {
205 return value->Print(output);
206 }
207
208};
209
213class IntValue : public Value {
214
215 public:
216
222 IntValue(const IntValue& value) = delete;
223
229 IntValue& operator = (const IntValue& value) = delete;
230
237 IntValue(int32 val, bool is_signed);
238
245 IntValue(uint32 val, bool is_signed);
246
252 bool IsInteger() override;
253
259 bool IsSignedValue() override;
260
266 int32 GetSigned() override;
267
274 uint32 GetUnsigned() override;
275
282 ValuePtr Dup(std::ostream &output) override;
283
290 virtual std::ostream& Print(std::ostream &output) const override;
291
292 protected:
293
297 const int32 val_;
298
302 const bool signed_;
303};
304
308class AddressValue: public IntValue{
309
310 public:
311
317 AddressValue(const AddressValue& value) = delete;
318
324 AddressValue& operator = (const AddressValue& value) = delete;
325
331 explicit AddressValue(uint32 addr);
332
338 bool IsAddress() override;
339
348 int32 GetSigned() override;
349
356 ValuePtr Dup(std::ostream &output) override;
357
364 virtual std::ostream& Print(std::ostream &output) const override;
365};
366
374
375 public:
376
382 RelAddressValue(const RelAddressValue& value) = delete;
383
390
397 RelAddressValue(uint32 base_addr, int32 offset);
398
404 bool IsAddress() override;
405
412 uint32 GetUnsigned() override;
413
420 ValuePtr Dup(std::ostream &output) override;
421
428 virtual std::ostream& Print(std::ostream &output) const override;
429
430 protected:
431
436};
437
441class DupValue : public Value {
442
443 public:
444
450 DupValue(const DupValue& value) = delete;
451
457 DupValue& operator = (const DupValue& value) = delete;
458
464 explicit DupValue(int idx);
465
472 ValuePtr Dup(std::ostream &output) override;
473
480 virtual std::ostream& Print(std::ostream &output) const override;
481
482 protected:
483
487 const int index_;
488};
489
493class StringValue : public Value {
494
495 public:
496
502 StringValue(const StringValue& value) = delete;
503
509 StringValue& operator = (const StringValue& value) = delete;
510
516 explicit StringValue(std::string str);
517
524 virtual std::ostream& Print(std::ostream &output) const override;
525
526 protected:
527
531 const std::string str_;
532};
533
538
539 public:
540
546 explicit UnquotedStringValue(std::string str);
547
554 virtual std::ostream& Print(std::ostream &output) const override;
555};
556
560class VarValue : public Value {
561
562 public:
568 explicit VarValue(std::string name);
575 virtual std::ostream& Print(std::ostream &output) const override;
576
577 protected:
578
582 std::string name_;
583};
584
588class ArrayValue : public VarValue {
589
590 public:
591
597 ArrayValue(const ArrayValue& value) = delete;
598
604 ArrayValue& operator = (const ArrayValue& value) = delete;
605
613 ArrayValue(const std::string& name, const ValueList& indexes);
614
623 virtual std::ostream& Print(std::ostream &output) const override;
624
625 protected:
626
631};
632
636class BinaryOpValue : public Value {
637
638
639 public:
640
646 BinaryOpValue(const BinaryOpValue& value) = delete;
647
653 BinaryOpValue& operator = (const BinaryOpValue& value) = delete;
654
662 BinaryOpValue(ValuePtr left, ValuePtr right, std::string oper);
663
670 virtual std::ostream& Print(std::ostream &output) const override;
671
677 virtual ValuePtr Negate() override;
678
688 virtual int GetPrecedence() const override;
689
690 protected:
691
696
701
705 const std::string oper_;
706};
707
713class UnaryOpValue : public Value{
714
715 public:
716
722 UnaryOpValue(const UnaryOpValue& value) = delete;
723
729 UnaryOpValue& operator = (const UnaryOpValue& value) = delete;
730
739 UnaryOpValue(ValuePtr operand, std::string oper, bool postfix);
740
747 virtual std::ostream& Print(std::ostream &output) const override;
748
758 virtual int GetPrecedence() const override;
759
760 protected:
761
766
770 const std::string oper_;
771
775 const bool postfix_;
776};
777
782
783 public:
784
790 NegatedValue(const NegatedValue& value) = delete;
791
797 NegatedValue& operator = (const NegatedValue& value) = delete;
798
804 explicit NegatedValue(ValuePtr val);
805
811 virtual ValuePtr Negate() override;
812};
813
817class CallValue : public Value {
818
819 public:
820
826 CallValue(const CallValue& value) = delete;
827
833 CallValue& operator = (const CallValue& value) = delete;
834
841 CallValue(std::string function, ValueList args);
842
849 virtual std::ostream& Print(std::ostream &output) const override;
850
851 protected:
852
856 const std::string function_;
857
862
863};
const int PRECEDENCE_BIT_AND
Precedence value for bitwise AND (&).
Definition: Value.h:68
const int PRECEDENCE_BIT_OR
Precedence value for bitwise OR (|).
Definition: Value.h:78
Stack< ValuePtr > ValueStack
Type representing a stack.
Definition: Value.h:103
std::deque< ValuePtr > ValueList
Type representing a list of values, e.g.
Definition: Value.h:98
const int PRECEDENCE_LOGIC_OR
Precedence value for logical OR (||).
Definition: Value.h:88
const int PRECEDENCE_UNARY
Precedence value for a unary operation.
Definition: Value.h:38
const int PRECEDENCE_NO
Precedence value for individual values with no operations.
Definition: Value.h:33
const int PRECEDENCE_RELATION
Precedence value for relative comparison (<, <=, >=, >).
Definition: Value.h:58
const int PRECEDENCE_SHIFT
Precedence value for bit shifting (<<, >>).
Definition: Value.h:53
boost::intrusive_ptr< Value > ValuePtr
Pointer to a Value.
Definition: Value.h:93
const int PRECEDENCE_ADD
Precedence value for addition and subtraction (+, -).
Definition: Value.h:48
const int PRECEDENCE_LOGIC_AND
Precedence value for logical AND (&&).
Definition: Value.h:83
const int PRECEDENCE_BIT_XOR
Precedence value for bitwise XOR (^).
Definition: Value.h:73
const int PRECEDENCE_MULT
Precedence value for multiplication, division, modulus (*, /, %).
Definition: Value.h:43
const int PRECEDENCE_EQUALITY
Precedence value for equality comparisons (==, !=).
Definition: Value.h:63
Value containing an absolute address.
Definition: Value.h:308
int32 GetSigned() override
Always throws {.
Definition: Value.cpp:108
AddressValue(const AddressValue &value)=delete
Copy constructor, disabled.
AddressValue & operator=(const AddressValue &value)=delete
Copy constructor, disabled.
bool IsAddress() override
Return whether or not the Value is an address.
Definition: Value.cpp:106
ValuePtr Dup(std::ostream &output) override
Duplicates the value.
Definition: Value.cpp:110
virtual std::ostream & Print(std::ostream &output) const override
Print the value to a stream.
Definition: Value.cpp:112
Value representing array access.
Definition: Value.h:588
virtual std::ostream & Print(std::ostream &output) const override
Print the value to a stream.
Definition: Value.cpp:153
ArrayValue & operator=(const ArrayValue &value)=delete
Copy constructor, disabled.
const ValueList indexes_
Values representing the indexes used (left-to-right).
Definition: Value.h:630
ArrayValue(const ArrayValue &value)=delete
Copy constructor, disabled.
Value representing the result of a binary operation.
Definition: Value.h:636
const ValuePtr left_val_
Value at the left side of the operator.
Definition: Value.h:695
virtual int GetPrecedence() const override
Retrieves the operator precedence for this operation.
Definition: Value.cpp:172
const ValuePtr right_val_
Value at the right side of the operator.
Definition: Value.h:700
virtual std::ostream & Print(std::ostream &output) const override
Print the value to a stream.
Definition: Value.cpp:163
virtual ValuePtr Negate() override
Negates a value.
Definition: Value.cpp:177
BinaryOpValue(const BinaryOpValue &value)=delete
Copy constructor, disabled.
BinaryOpValue & operator=(const BinaryOpValue &value)=delete
Copy constructor, disabled.
const std::string oper_
The operator.
Definition: Value.h:705
Value representing a function call.
Definition: Value.h:817
CallValue(const CallValue &value)=delete
Copy constructor, disabled.
virtual std::ostream & Print(std::ostream &output) const override
Print the value to a stream.
Definition: Value.cpp:202
const ValueList args_
List of values used as function arguments.
Definition: Value.h:861
const std::string function_
The name of the function.
Definition: Value.h:856
CallValue & operator=(const CallValue &value)=delete
Copy constructor, disabled.
Duplicated value.
Definition: Value.h:441
ValuePtr Dup(std::ostream &output) override
Duplicates the value.
Definition: Value.cpp:132
DupValue & operator=(const DupValue &value)=delete
Copy constructor, disabled.
DupValue(const DupValue &value)=delete
Copy constructor, disabled.
const int index_
Index to distinguish multiple duplicated entries.
Definition: Value.h:487
virtual std::ostream & Print(std::ostream &output) const override
Print the value to a stream.
Definition: Value.cpp:134
Value containing an integer.
Definition: Value.h:213
IntValue(const IntValue &value)=delete
Copy constructor, disabled.
bool IsInteger() override
Return whether or not the Value is an integer.
Definition: Value.cpp:88
ValuePtr Dup(std::ostream &output) override
Duplicates the value.
Definition: Value.cpp:96
virtual std::ostream & Print(std::ostream &output) const override
Print the value to a stream.
Definition: Value.cpp:98
int32 GetSigned() override
Retrieves a signed integer representing the value, if possible.
Definition: Value.cpp:92
IntValue & operator=(const IntValue &value)=delete
Copy constructor, disabled.
const int32 val_
The value of the integer.
Definition: Value.h:297
const bool signed_
True if the value is signed, false if it's not.
Definition: Value.h:302
uint32 GetUnsigned() override
Retrieves an unsigned integer representing the value, if possible.
Definition: Value.cpp:94
bool IsSignedValue() override
Returns whether or not the stored integer value is signed.
Definition: Value.cpp:90
Negated value.
Definition: Value.h:781
NegatedValue(const NegatedValue &value)=delete
Copy constructor, disabled.
virtual ValuePtr Negate() override
Negates the value.
Definition: Value.cpp:198
NegatedValue & operator=(const NegatedValue &value)=delete
Copy constructor, disabled.
Provides a base implementation of reference counting.
Definition: RefCounted.h:28
Value containing a signed, relative address.
Definition: Value.h:373
RelAddressValue(const RelAddressValue &value)=delete
Copy constructor, disabled.
uint32 GetUnsigned() override
Retrieves the exact address.
Definition: Value.cpp:121
bool IsAddress() override
Return whether or not the Value is an address.
Definition: Value.cpp:119
const uint32 base_addr_
The base address for the offset.
Definition: Value.h:435
ValuePtr Dup(std::ostream &output) override
Duplicates the value.
Definition: Value.cpp:123
virtual std::ostream & Print(std::ostream &output) const override
Print the relative address to a stream.
Definition: Value.cpp:125
RelAddressValue & operator=(const RelAddressValue &value)=delete
Copy constructor, disabled.
String value.
Definition: Value.h:493
StringValue & operator=(const StringValue &value)=delete
Copy constructor, disabled.
StringValue(const StringValue &value)=delete
Copy constructor, disabled.
virtual std::ostream & Print(std::ostream &output) const override
Print the value to a stream.
Definition: Value.cpp:138
const std::string str_
The string value.
Definition: Value.h:531
Value representing the result of a unary operation.
Definition: Value.h:713
UnaryOpValue & operator=(const UnaryOpValue &value)=delete
Copy constructor, disabled.
virtual std::ostream & Print(std::ostream &output) const override
Print the value to a stream.
Definition: Value.cpp:186
const std::string oper_
The operator for this value.
Definition: Value.h:770
UnaryOpValue(const UnaryOpValue &value)=delete
Copy constructor, disabled.
const bool postfix_
True if the operator is postfixed to the operand, false otherwise.
Definition: Value.h:775
virtual int GetPrecedence() const override
Retrieves the operator precedence for this operation.
Definition: Value.cpp:194
const ValuePtr operand_
The operand of the operation.
Definition: Value.h:765
A string value, unquoted.
Definition: Value.h:537
virtual std::ostream & Print(std::ostream &output) const override
Prints the value to a stream.
Definition: Value.cpp:144
UnquotedStringValue(std::string str)
Constructor.
Definition: Value.cpp:142
Class representing a value (stack entry, parameter, etc.)
Definition: Value.h:108
virtual ~Value()
Destructor.
Definition: Value.cpp:56
friend std::ostream & operator<<(std::ostream &output, Value *value)
Output a value to a stream.
Definition: Value.h:204
virtual std::string GetString() const
Retrieves the string representation of the value.
Definition: Value.cpp:76
virtual int32 GetSigned()
Retrieves a signed integer representing the value, if possible.
Definition: Value.cpp:64
virtual bool IsInteger()
Return whether or not the Value is an integer.
Definition: Value.cpp:58
virtual ValuePtr Negate()
Negates a value.
Definition: Value.cpp:74
virtual bool IsSignedValue()
Returns whether or not any stored integer value is signed.
Definition: Value.cpp:62
virtual std::ostream & Print(std::ostream &output) const =0
Print the value to a stream.
virtual uint32 GetUnsigned()
Retrieves an unsigned integer representing the value, if possible.
Definition: Value.cpp:66
virtual int GetPrecedence() const
Operator precedence for this value.
Definition: Value.cpp:82
virtual bool IsAddress()
Return whether or not the Value is an address.
Definition: Value.cpp:60
virtual ValuePtr Dup(std::ostream &output)
Duplicates a value.
Definition: Value.cpp:68
Value representing a variable.
Definition: Value.h:560
virtual std::ostream & Print(std::ostream &output) const override
Print the value to a stream.
Definition: Value.cpp:148
std::string name_
The variable name.
Definition: Value.h:582
VarValue(std::string name)
Constructor for VarValue.
Definition: Value.cpp:146
unsigned int uint32
Definition: scummsys.h:435
signed int int32
Definition: scummsys.h:436