1 /* This file is part of the dynarmic project.
2  * Copyright (c) 2016 MerryMage
3  * SPDX-License-Identifier: 0BSD
4  */
5 
6 #pragma once
7 
8 #include <type_traits>
9 
10 #include "common/assert.h"
11 #include "common/common_types.h"
12 #include "frontend/ir/type.h"
13 
14 namespace Dynarmic::A32 {
15 enum class ExtReg;
16 enum class Reg;
17 }
18 
19 namespace Dynarmic::A64 {
20 enum class Reg;
21 enum class Vec;
22 }
23 
24 namespace Dynarmic::IR {
25 
26 class Inst;
27 enum class Cond;
28 
29 /**
30  * A representation of a value in the IR.
31  * A value may either be an immediate or the result of a microinstruction.
32  */
33 class Value {
34 public:
35     using CoprocessorInfo = std::array<u8, 8>;
36 
Value()37     Value() : type(Type::Void) {}
38     explicit Value(Inst* value);
39     explicit Value(A32::Reg value);
40     explicit Value(A32::ExtReg value);
41     explicit Value(A64::Reg value);
42     explicit Value(A64::Vec value);
43     explicit Value(bool value);
44     explicit Value(u8 value);
45     explicit Value(u16 value);
46     explicit Value(u32 value);
47     explicit Value(u64 value);
48     explicit Value(CoprocessorInfo value);
49     explicit Value(Cond value);
50 
51     bool IsIdentity() const;
52     bool IsEmpty() const;
53     bool IsImmediate() const;
54     Type GetType() const;
55 
56     Inst* GetInst() const;
57     Inst* GetInstRecursive() const;
58     A32::Reg GetA32RegRef() const;
59     A32::ExtReg GetA32ExtRegRef() const;
60     A64::Reg GetA64RegRef() const;
61     A64::Vec GetA64VecRef() const;
62     bool GetU1() const;
63     u8 GetU8() const;
64     u16 GetU16() const;
65     u32 GetU32() const;
66     u64 GetU64() const;
67     CoprocessorInfo GetCoprocInfo() const;
68     Cond GetCond() const;
69 
70     /**
71      * Retrieves the immediate of a Value instance as a signed 64-bit value.
72      *
73      * @pre The value contains either a U1, U8, U16, U32, or U64 value.
74      *      Breaking this precondition will cause an assertion to be invoked.
75      */
76     s64 GetImmediateAsS64() const;
77 
78     /**
79      * Retrieves the immediate of a Value instance as an unsigned 64-bit value.
80      *
81      * @pre The value contains either a U1, U8, U16, U32, or U64 value.
82      *      Breaking this precondition will cause an assertion to be invoked.
83      */
84     u64 GetImmediateAsU64() const;
85 
86     /**
87      * Determines whether or not the contained value matches the provided signed one.
88      *
89      * Note that this function will always return false if the contained
90      * value is not a a constant value. In other words, if IsImmediate()
91      * would return false on an instance, then so will this function.
92      *
93      * @param value The value to check against the contained value.
94      */
95     bool IsSignedImmediate(s64 value) const;
96 
97     /**
98      * Determines whether or not the contained value matches the provided unsigned one.
99      *
100      * Note that this function will always return false if the contained
101      * value is not a a constant value. In other words, if IsImmediate()
102      * would return false on an instance, then so will this function.
103      *
104      * @param value The value to check against the contained value.
105      */
106     bool IsUnsignedImmediate(u64 value) const;
107 
108     /**
109      * Determines whether or not the contained constant value has all bits set.
110      *
111      * @pre The value contains either a U1, U8, U16, U32, or U64 value.
112      *      Breaking this precondition will cause an assertion to be invoked.
113      */
114     bool HasAllBitsSet() const;
115 
116     /**
117      * Whether or not the current value contains a representation of zero.
118      *
119      * Note that this function will always return false if the contained
120      * value is not a a constant value. In other words, if IsImmediate()
121      * would return false on an instance, then so will this function.
122      */
123     bool IsZero() const;
124 
125 private:
126     Type type;
127 
128     union {
129         Inst* inst; // type == Type::Opaque
130         A32::Reg imm_a32regref;
131         A32::ExtReg imm_a32extregref;
132         A64::Reg imm_a64regref;
133         A64::Vec imm_a64vecref;
134         bool imm_u1;
135         u8 imm_u8;
136         u16 imm_u16;
137         u32 imm_u32;
138         u64 imm_u64;
139         CoprocessorInfo imm_coproc;
140         Cond imm_cond;
141     } inner;
142 };
143 static_assert(sizeof(Value) <= 2 * sizeof(u64), "IR::Value should be kept small in size");
144 
145 template <Type type_>
146 class TypedValue final : public Value {
147 public:
148     TypedValue() = default;
149 
150     template <Type other_type, typename = std::enable_if_t<(other_type & type_) != Type::Void>>
TypedValue(const TypedValue<other_type> & value)151     /* implicit */ TypedValue(const TypedValue<other_type>& value) : Value(value) {
152         ASSERT((value.GetType() & type_) != Type::Void);
153     }
154 
TypedValue(const Value & value)155     explicit TypedValue(const Value& value) : Value(value) {
156         ASSERT((value.GetType() & type_) != Type::Void);
157     }
158 
TypedValue(Inst * inst)159     explicit TypedValue(Inst* inst) : TypedValue(Value(inst)) {}
160 };
161 
162 using U1 = TypedValue<Type::U1>;
163 using U8 = TypedValue<Type::U8>;
164 using U16 = TypedValue<Type::U16>;
165 using U32 = TypedValue<Type::U32>;
166 using U64 = TypedValue<Type::U64>;
167 using U128 = TypedValue<Type::U128>;
168 using U32U64 = TypedValue<Type::U32 | Type::U64>;
169 using U16U32U64 = TypedValue<Type::U16 | Type::U32 | Type::U64>;
170 using UAny = TypedValue<Type::U8 | Type::U16 | Type::U32 | Type::U64>;
171 using UAnyU128 = TypedValue<Type::U8 | Type::U16 | Type::U32 | Type::U64 | Type::U128>;
172 using NZCV = TypedValue<Type::NZCVFlags>;
173 using Table = TypedValue<Type::Table>;
174 
175 } // namespace Dynarmic::IR
176