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 * Authors: Timothy M. Jones
29 */
30
31#ifndef __ARCH_POWER_INSTS_INTEGER_HH__
32#define __ARCH_POWER_INSTS_INTEGER_HH__
33
34#include "arch/power/insts/static_inst.hh"
35#include "base/bitfield.hh"
36#include "base/cprintf.hh"
37
38namespace PowerISA
39{
40
41/**
42 * We provide a base class for integer operations and then inherit for
43 * several other classes. These specialise for instructions using immediate
44 * values and also rotate instructions. We also need to have versions that
45 * consider the Rc and OE bits.
46 */
47
48/**
49 * Base class for integer operations.
50 */
51class IntOp : public PowerStaticInst
52{
53  protected:
54
55    bool rcSet;
56    bool oeSet;
57
58    // Needed for srawi only
59    uint32_t sh;
60
61    /// Constructor
62    IntOp(const char *mnem, MachInst _machInst, OpClass __opClass)
63      : PowerStaticInst(mnem, _machInst, __opClass),
64        rcSet(false), oeSet(false)
65    {
66    }
67
68    /* Compute the CR (condition register) field using signed comparison */
69    inline uint32_t
70    makeCRField(int32_t a, int32_t b, uint32_t xerSO) const
71    {
72        uint32_t c = xerSO;
73
74        /* We've pre-shifted the immediate values here */
75        if (a < b)      { c += 0x8; }
76        else if (a > b) { c += 0x4; }
77        else            { c += 0x2; }
78        return c;
79    }
80
81    /* Compute the CR (condition register) field using unsigned comparison */
82    inline uint32_t
83    makeCRField(uint32_t a, uint32_t b, uint32_t xerSO) const
84    {
85        uint32_t c = xerSO;
86
87        /* We've pre-shifted the immediate values here */
88        if (a < b)      { c += 0x8; }
89        else if (a > b) { c += 0x4; }
90        else            { c += 0x2; }
91        return c;
92    }
93
94    std::string generateDisassembly(
95            Addr pc, const SymbolTable *symtab) const override;
96};
97
98
99/**
100 * Class for integer immediate (signed and unsigned) operations.
101 */
102class IntImmOp : public IntOp
103{
104  protected:
105
106    int32_t imm;
107    uint32_t uimm;
108
109    /// Constructor
110    IntImmOp(const char *mnem, MachInst _machInst, OpClass __opClass)
111      : IntOp(mnem, _machInst, __opClass),
112        imm(sext<16>(machInst.si)),
113        uimm(machInst.si)
114    {
115    }
116
117    std::string generateDisassembly(
118            Addr pc, const SymbolTable *symtab) const override;
119};
120
121
122/**
123 * Class for integer operations with a shift.
124 */
125class IntShiftOp : public IntOp
126{
127  protected:
128
129    uint32_t sh;
130
131    /// Constructor
132    IntShiftOp(const char *mnem, MachInst _machInst, OpClass __opClass)
133      : IntOp(mnem, _machInst, __opClass),
134        sh(machInst.sh)
135    {
136    }
137
138    std::string generateDisassembly(
139            Addr pc, const SymbolTable *symtab) const override;
140};
141
142
143/**
144 * Class for integer rotate operations.
145 */
146class IntRotateOp : public IntShiftOp
147{
148  protected:
149
150    uint32_t mb;
151    uint32_t me;
152    uint32_t fullMask;
153
154    /// Constructor
155    IntRotateOp(const char *mnem, MachInst _machInst, OpClass __opClass)
156      : IntShiftOp(mnem, _machInst, __opClass),
157        mb(machInst.mb),
158        me(machInst.me)
159    {
160        if (me >= mb) {
161            fullMask = mask(31 - mb, 31 - me);
162        } else {
163            fullMask = ~mask(31 - (me + 1), 31 - (mb - 1));
164        }
165    }
166
167    uint32_t
168    rotateValue(uint32_t rs, uint32_t shift) const
169    {
170        uint32_t n = shift & 31;
171        return (rs << n) | (rs >> (32 - n));
172    }
173
174    std::string generateDisassembly(
175            Addr pc, const SymbolTable *symtab) const override;
176};
177
178} // namespace PowerISA
179
180#endif //__ARCH_POWER_INSTS_INTEGER_HH__
181