2 * Copyright (c) 2009 The University of Edinburgh
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.
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.
29 #ifndef __ARCH_POWER_INSTS_INTEGER_HH__
30 #define __ARCH_POWER_INSTS_INTEGER_HH__
32 #include "arch/power/insts/static_inst.hh"
33 #include "base/bitfield.hh"
34 #include "base/cprintf.hh"
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.
47 * Base class for integer operations.
49 class IntOp : public PowerStaticInst
56 // Needed for srawi only
60 IntOp(const char *mnem, MachInst _machInst, OpClass __opClass)
61 : PowerStaticInst(mnem, _machInst, __opClass),
62 rcSet(false), oeSet(false)
66 /* Compute the CR (condition register) field using signed comparison */
68 makeCRField(int64_t a, int64_t b, uint32_t xerSO) const
72 /* We've pre-shifted the immediate values here */
73 if (a < b) { c += 0x8; }
74 else if (a > b) { c += 0x4; }
80 makeCRField(int64_t a, int32_t b, uint32_t xerSO) const
82 return makeCRField(a, (int64_t)b, xerSO);
86 makeCRField(int32_t a, int32_t b, uint32_t xerSO) const
88 return makeCRField((int64_t)a, (int64_t)b, xerSO);
91 /* Compute the CR (condition register) field using unsigned comparison */
93 makeCRField(uint64_t a, uint64_t b, uint32_t xerSO) const
97 /* We've pre-shifted the immediate values here */
98 if (a < b) { c += 0x8; }
99 else if (a > b) { c += 0x4; }
105 makeCRField(uint64_t a, uint32_t b, uint32_t xerSO) const
107 return makeCRField(a, (uint64_t)b, xerSO);
111 makeCRField(uint32_t a, uint32_t b, uint32_t xerSO) const
113 return makeCRField((uint64_t)a, (uint64_t)b, xerSO);
116 std::string generateDisassembly(
117 Addr pc, const Loader::SymbolTable *symtab) const override;
122 * Class for integer immediate (signed and unsigned) operations.
124 class IntImmOp : public IntOp
132 IntImmOp(const char *mnem, MachInst _machInst, OpClass __opClass)
133 : IntOp(mnem, _machInst, __opClass),
134 imm(sext<16>(machInst.si)),
139 std::string generateDisassembly(
140 Addr pc, const Loader::SymbolTable *symtab) const override;
145 * Class for integer arithmetic operations.
147 class IntArithOp : public IntOp
152 IntArithOp(const char *mnem, MachInst _machInst, OpClass __opClass)
153 : IntOp(mnem, _machInst, __opClass)
157 /* Compute 128-bit sum of 128-bit to 64-bit unsigned integer addition */
158 inline std::tuple<uint64_t, uint64_t>
159 add(uint64_t ralo, uint64_t rahi, uint64_t rb) const
162 #if defined(__SIZEOF_INT128__)
163 __uint128_t ra = ((__uint128_t)rahi << 64) | ralo;
164 __uint128_t sum = ra + rb;
168 shi = rahi + ((ralo + rb) < ralo);
171 return std::make_tuple(slo, shi);
174 /* Compute 128-bit sum of 128-bit to 64-bit signed integer addition */
175 inline std::tuple<uint64_t, int64_t>
176 add(uint64_t ralo, int64_t rahi, int64_t rb) const
180 #if defined(__SIZEOF_INT128__)
181 __int128_t ra = ((__int128_t)rahi << 64) | ralo;
182 __int128_t sum = (__int128_t)ra + rb;
200 return std::make_tuple(slo, shi);
204 * Compute 128-bit product of 64-bit unsigned integer multiplication
205 * based on https://stackoverflow.com/a/28904636
207 inline std::tuple<uint64_t, uint64_t>
208 multiply(uint64_t ra, uint64_t rb) const
211 #if defined(__SIZEOF_INT128__)
212 __uint128_t prod = (__uint128_t)ra * rb;
216 uint64_t ralo = (uint32_t)ra, rahi = ra >> 32;
217 uint64_t rblo = (uint32_t)rb, rbhi = rb >> 32;
218 uint64_t pp0 = ralo * rblo;
219 uint64_t pp1 = rahi * rblo;
220 uint64_t pp2 = ralo * rbhi;
221 uint64_t pp3 = rahi * rbhi;
222 uint64_t c = ((uint32_t)pp1) + ((uint32_t)pp2) + (pp0 >> 32);
223 phi = pp3 + (pp2 >> 32) + (pp1 >> 32) + (c >> 32);
224 plo = (c << 32) | ((uint32_t)pp0);
226 return std::make_tuple(plo, phi);
229 /* Compute 128-bit product of 64-bit signed integer multiplication */
230 inline std::tuple<uint64_t, int64_t>
231 multiply(int64_t ra, int64_t rb) const
234 #if defined(__SIZEOF_INT128__)
235 __int128_t prod = (__int128_t)ra * rb;
239 std::tie(plo, phi) = multiply((uint64_t)ra, (uint64_t)rb);
240 if (rb < 0) phi -= (uint64_t)ra;
241 if (ra < 0) phi -= (uint64_t)rb;
243 return std::make_tuple(plo, (int64_t)phi);
247 * Compute 128-bit result of 64-bit unsigned integer multiplication
248 * followed by addition
250 inline std::tuple<uint64_t, uint64_t>
251 multiplyAdd(uint64_t ra, uint64_t rb, uint64_t rc) const
254 #if defined(__SIZEOF_INT128__)
255 __uint128_t res = ((__uint128_t)ra * rb) + rc;
260 std::tie(plo, phi) = multiply(ra, rb);
261 std::tie(rlo, rhi) = add(plo, phi, rc);
263 return std::make_tuple(rlo, rhi);
267 * Compute 128-bit result of 64-bit signed integer multiplication
268 * followed by addition
270 inline std::tuple<uint64_t, int64_t>
271 multiplyAdd(int64_t ra, int64_t rb, int64_t rc) const
275 #if defined(__SIZEOF_INT128__)
276 __int128_t res = (__int128_t)ra * rb + rc;
282 std::tie(plo, phi) = multiply(ra, rb);
283 std::tie(rlo, rhi) = add(plo, phi, rc);
285 return std::make_tuple(rlo, rhi);
288 std::string generateDisassembly(
289 Addr pc, const Loader::SymbolTable *symtab) const override;
294 * Class for integer immediate arithmetic operations.
296 class IntImmArithOp : public IntArithOp
303 IntImmArithOp(const char *mnem, MachInst _machInst, OpClass __opClass)
304 : IntArithOp(mnem, _machInst, __opClass),
305 simm((int16_t)machInst.si)
309 std::string generateDisassembly(
310 Addr pc, const Loader::SymbolTable *symtab) const override;
315 * Class for integer arithmetic operations with displacement.
317 class IntDispArithOp : public IntArithOp
324 IntDispArithOp(const char *mnem, MachInst _machInst, OpClass __opClass)
325 : IntArithOp(mnem, _machInst, __opClass),
326 disp((int16_t)((machInst.d0 << 6) | (machInst.d1 << 1) | machInst.d2))
330 std::string generateDisassembly(
331 Addr pc, const Loader::SymbolTable *symtab) const override;
336 * Class for integer operations with a shift.
338 class IntShiftOp : public IntOp
345 IntShiftOp(const char *mnem, MachInst _machInst, OpClass __opClass)
346 : IntOp(mnem, _machInst, __opClass),
351 std::string generateDisassembly(
352 Addr pc, const Loader::SymbolTable *symtab) const override;
357 * Class for integer rotate operations.
359 class IntRotateOp : public IntShiftOp
368 IntRotateOp(const char *mnem, MachInst _machInst, OpClass __opClass)
369 : IntShiftOp(mnem, _machInst, __opClass),
374 fullMask = mask(31 - mb, 31 - me);
376 fullMask = ~mask(31 - (me + 1), 31 - (mb - 1));
381 rotateValue(uint32_t rs, uint32_t shift) const
383 uint32_t n = shift & 31;
384 return (rs << n) | (rs >> (32 - n));
387 std::string generateDisassembly(
388 Addr pc, const Loader::SymbolTable *symtab) const override;
391 } // namespace PowerISA
393 #endif //__ARCH_POWER_INSTS_INTEGER_HH__