arch-power: Add PC-relative arithmetic instructions
[gem5.git] / src / arch / power / insts / integer.hh
1 /*
2 * Copyright (c) 2009 The University of Edinburgh
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef __ARCH_POWER_INSTS_INTEGER_HH__
30 #define __ARCH_POWER_INSTS_INTEGER_HH__
31
32 #include "arch/power/insts/static_inst.hh"
33 #include "base/bitfield.hh"
34 #include "base/cprintf.hh"
35
36 namespace PowerISA
37 {
38
39 /**
40 * We provide a base class for integer operations and then inherit for
41 * several other classes. These specialise for instructions using immediate
42 * values and also rotate instructions. We also need to have versions that
43 * consider the Rc and OE bits.
44 */
45
46 /**
47 * Base class for integer operations.
48 */
49 class IntOp : public PowerStaticInst
50 {
51 protected:
52
53 bool rcSet;
54 bool oeSet;
55
56 // Needed for srawi only
57 uint32_t sh;
58
59 /// Constructor
60 IntOp(const char *mnem, MachInst _machInst, OpClass __opClass)
61 : PowerStaticInst(mnem, _machInst, __opClass),
62 rcSet(false), oeSet(false)
63 {
64 }
65
66 /* Compute the CR (condition register) field using signed comparison */
67 inline uint32_t
68 makeCRField(int64_t a, int64_t b, uint32_t xerSO) const
69 {
70 uint32_t c = xerSO;
71
72 /* We've pre-shifted the immediate values here */
73 if (a < b) { c += 0x8; }
74 else if (a > b) { c += 0x4; }
75 else { c += 0x2; }
76 return c;
77 }
78
79 inline uint32_t
80 makeCRField(int64_t a, int32_t b, uint32_t xerSO) const
81 {
82 return makeCRField(a, (int64_t)b, xerSO);
83 }
84
85 inline uint32_t
86 makeCRField(int32_t a, int32_t b, uint32_t xerSO) const
87 {
88 return makeCRField((int64_t)a, (int64_t)b, xerSO);
89 }
90
91 /* Compute the CR (condition register) field using unsigned comparison */
92 inline uint32_t
93 makeCRField(uint64_t a, uint64_t b, uint32_t xerSO) const
94 {
95 uint32_t c = xerSO;
96
97 /* We've pre-shifted the immediate values here */
98 if (a < b) { c += 0x8; }
99 else if (a > b) { c += 0x4; }
100 else { c += 0x2; }
101 return c;
102 }
103
104 inline uint32_t
105 makeCRField(uint64_t a, uint32_t b, uint32_t xerSO) const
106 {
107 return makeCRField(a, (uint64_t)b, xerSO);
108 }
109
110 inline uint32_t
111 makeCRField(uint32_t a, uint32_t b, uint32_t xerSO) const
112 {
113 return makeCRField((uint64_t)a, (uint64_t)b, xerSO);
114 }
115
116 std::string generateDisassembly(
117 Addr pc, const Loader::SymbolTable *symtab) const override;
118 };
119
120
121 /**
122 * Class for integer immediate (signed and unsigned) operations.
123 */
124 class IntImmOp : public IntOp
125 {
126 protected:
127
128 int32_t imm;
129 uint32_t uimm;
130
131 /// Constructor
132 IntImmOp(const char *mnem, MachInst _machInst, OpClass __opClass)
133 : IntOp(mnem, _machInst, __opClass),
134 imm(sext<16>(machInst.si)),
135 uimm(machInst.si)
136 {
137 }
138
139 std::string generateDisassembly(
140 Addr pc, const Loader::SymbolTable *symtab) const override;
141 };
142
143
144 /**
145 * Class for integer arithmetic operations.
146 */
147 class IntArithOp : public IntOp
148 {
149 protected:
150
151 /// Constructor
152 IntArithOp(const char *mnem, MachInst _machInst, OpClass __opClass)
153 : IntOp(mnem, _machInst, __opClass)
154 {
155 }
156
157 std::string generateDisassembly(
158 Addr pc, const Loader::SymbolTable *symtab) const override;
159 };
160
161
162 /**
163 * Class for integer immediate arithmetic operations.
164 */
165 class IntImmArithOp : public IntArithOp
166 {
167 protected:
168
169 int32_t simm;
170
171 /// Constructor
172 IntImmArithOp(const char *mnem, MachInst _machInst, OpClass __opClass)
173 : IntArithOp(mnem, _machInst, __opClass),
174 simm((int16_t)machInst.si)
175 {
176 }
177
178 std::string generateDisassembly(
179 Addr pc, const Loader::SymbolTable *symtab) const override;
180 };
181
182
183 /**
184 * Class for integer arithmetic operations with displacement.
185 */
186 class IntDispArithOp : public IntArithOp
187 {
188 protected:
189
190 int32_t disp;
191
192 /// Constructor
193 IntDispArithOp(const char *mnem, MachInst _machInst, OpClass __opClass)
194 : IntArithOp(mnem, _machInst, __opClass),
195 disp((int16_t)((machInst.d0 << 6) | (machInst.d1 << 1) | machInst.d2))
196 {
197 }
198
199 std::string generateDisassembly(
200 Addr pc, const Loader::SymbolTable *symtab) const override;
201 };
202
203
204 /**
205 * Class for integer operations with a shift.
206 */
207 class IntShiftOp : public IntOp
208 {
209 protected:
210
211 uint32_t sh;
212
213 /// Constructor
214 IntShiftOp(const char *mnem, MachInst _machInst, OpClass __opClass)
215 : IntOp(mnem, _machInst, __opClass),
216 sh(machInst.sh)
217 {
218 }
219
220 std::string generateDisassembly(
221 Addr pc, const Loader::SymbolTable *symtab) const override;
222 };
223
224
225 /**
226 * Class for integer rotate operations.
227 */
228 class IntRotateOp : public IntShiftOp
229 {
230 protected:
231
232 uint32_t mb;
233 uint32_t me;
234 uint32_t fullMask;
235
236 /// Constructor
237 IntRotateOp(const char *mnem, MachInst _machInst, OpClass __opClass)
238 : IntShiftOp(mnem, _machInst, __opClass),
239 mb(machInst.mb),
240 me(machInst.me)
241 {
242 if (me >= mb) {
243 fullMask = mask(31 - mb, 31 - me);
244 } else {
245 fullMask = ~mask(31 - (me + 1), 31 - (mb - 1));
246 }
247 }
248
249 uint32_t
250 rotateValue(uint32_t rs, uint32_t shift) const
251 {
252 uint32_t n = shift & 31;
253 return (rs << n) | (rs >> (32 - n));
254 }
255
256 std::string generateDisassembly(
257 Addr pc, const Loader::SymbolTable *symtab) const override;
258 };
259
260 } // namespace PowerISA
261
262 #endif //__ARCH_POWER_INSTS_INTEGER_HH__