static_inst.hh revision 12334
111723Sar4jc@virginia.edu/*
211723Sar4jc@virginia.edu * Copyright (c) 2003-2005 The Regents of The University of Michigan
311723Sar4jc@virginia.edu * Copyright (c) 2013 Advanced Micro Devices, Inc.
411723Sar4jc@virginia.edu * All rights reserved.
511723Sar4jc@virginia.edu *
611723Sar4jc@virginia.edu * Redistribution and use in source and binary forms, with or without
711723Sar4jc@virginia.edu * modification, are permitted provided that the following conditions are
811723Sar4jc@virginia.edu * met: redistributions of source code must retain the above copyright
911723Sar4jc@virginia.edu * notice, this list of conditions and the following disclaimer;
1011723Sar4jc@virginia.edu * redistributions in binary form must reproduce the above copyright
1111723Sar4jc@virginia.edu * notice, this list of conditions and the following disclaimer in the
1211723Sar4jc@virginia.edu * documentation and/or other materials provided with the distribution;
1311723Sar4jc@virginia.edu * neither the name of the copyright holders nor the names of its
1411723Sar4jc@virginia.edu * contributors may be used to endorse or promote products derived from
1511723Sar4jc@virginia.edu * this software without specific prior written permission.
1611723Sar4jc@virginia.edu *
1711723Sar4jc@virginia.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1811723Sar4jc@virginia.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1911723Sar4jc@virginia.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2011723Sar4jc@virginia.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2111723Sar4jc@virginia.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2211723Sar4jc@virginia.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2311723Sar4jc@virginia.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2411723Sar4jc@virginia.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2511723Sar4jc@virginia.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2611723Sar4jc@virginia.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2711723Sar4jc@virginia.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811723Sar4jc@virginia.edu *
2911723Sar4jc@virginia.edu * Authors: Steve Reinhardt
3011723Sar4jc@virginia.edu */
3111723Sar4jc@virginia.edu
3211723Sar4jc@virginia.edu#ifndef __CPU_STATIC_INST_HH__
3311723Sar4jc@virginia.edu#define __CPU_STATIC_INST_HH__
3411723Sar4jc@virginia.edu
3511723Sar4jc@virginia.edu#include <bitset>
3611723Sar4jc@virginia.edu#include <string>
3711723Sar4jc@virginia.edu
3811723Sar4jc@virginia.edu#include "arch/registers.hh"
3911723Sar4jc@virginia.edu#include "arch/types.hh"
40#include "base/logging.hh"
41#include "base/refcnt.hh"
42#include "base/types.hh"
43#include "config/the_isa.hh"
44#include "cpu/op_class.hh"
45#include "cpu/reg_class.hh"
46#include "cpu/reg_class_impl.hh"
47#include "cpu/static_inst_fwd.hh"
48#include "cpu/thread_context.hh"
49#include "enums/StaticInstFlags.hh"
50
51// forward declarations
52class Packet;
53
54class ExecContext;
55
56class SymbolTable;
57
58namespace Trace {
59    class InstRecord;
60}
61
62/**
63 * Base, ISA-independent static instruction class.
64 *
65 * The main component of this class is the vector of flags and the
66 * associated methods for reading them.  Any object that can rely
67 * solely on these flags can process instructions without being
68 * recompiled for multiple ISAs.
69 */
70class StaticInst : public RefCounted, public StaticInstFlags
71{
72  public:
73    /// Binary extended machine instruction type.
74    typedef TheISA::ExtMachInst ExtMachInst;
75
76    enum {
77        MaxInstSrcRegs = TheISA::MaxInstSrcRegs,        //< Max source regs
78        MaxInstDestRegs = TheISA::MaxInstDestRegs       //< Max dest regs
79    };
80
81  protected:
82
83    /// Flag values for this instruction.
84    std::bitset<Num_Flags> flags;
85
86    /// See opClass().
87    OpClass _opClass;
88
89    /// See numSrcRegs().
90    int8_t _numSrcRegs;
91
92    /// See numDestRegs().
93    int8_t _numDestRegs;
94
95    /// The following are used to track physical register usage
96    /// for machines with separate int & FP reg files.
97    //@{
98    int8_t _numFPDestRegs;
99    int8_t _numIntDestRegs;
100    int8_t _numCCDestRegs;
101    //@}
102
103    /** To use in architectures with vector register file. */
104    /** @{ */
105    int8_t _numVecDestRegs;
106    int8_t _numVecElemDestRegs;
107    /** @} */
108
109  public:
110
111    /// @name Register information.
112    /// The sum of numFPDestRegs(), numIntDestRegs(), numVecDestRegs() and
113    /// numVecelemDestRegs() equals numDestRegs().  The former two functions
114    /// are used to track physical register usage for machines with separate
115    /// int & FP reg files, the next two is for machines with vector register
116    /// file.
117    //@{
118    /// Number of source registers.
119    int8_t numSrcRegs()  const { return _numSrcRegs; }
120    /// Number of destination registers.
121    int8_t numDestRegs() const { return _numDestRegs; }
122    /// Number of floating-point destination regs.
123    int8_t numFPDestRegs()  const { return _numFPDestRegs; }
124    /// Number of integer destination regs.
125    int8_t numIntDestRegs() const { return _numIntDestRegs; }
126    /// Number of vector destination regs.
127    int8_t numVecDestRegs() const { return _numVecDestRegs; }
128    /// Number of vector element destination regs.
129    int8_t numVecElemDestRegs() const { return _numVecElemDestRegs; }
130    /// Number of coprocesor destination regs.
131    int8_t numCCDestRegs() const { return _numCCDestRegs; }
132    //@}
133
134    /// @name Flag accessors.
135    /// These functions are used to access the values of the various
136    /// instruction property flags.  See StaticInst::Flags for descriptions
137    /// of the individual flags.
138    //@{
139
140    bool isNop()          const { return flags[IsNop]; }
141
142    bool isMemRef()       const { return flags[IsMemRef]; }
143    bool isLoad()         const { return flags[IsLoad]; }
144    bool isStore()        const { return flags[IsStore]; }
145    bool isStoreConditional()     const { return flags[IsStoreConditional]; }
146    bool isInstPrefetch() const { return flags[IsInstPrefetch]; }
147    bool isDataPrefetch() const { return flags[IsDataPrefetch]; }
148    bool isPrefetch()     const { return isInstPrefetch() ||
149                                         isDataPrefetch(); }
150
151    bool isInteger()      const { return flags[IsInteger]; }
152    bool isFloating()     const { return flags[IsFloating]; }
153    bool isVector()       const { return flags[IsVector]; }
154    bool isCC()           const { return flags[IsCC]; }
155
156    bool isControl()      const { return flags[IsControl]; }
157    bool isCall()         const { return flags[IsCall]; }
158    bool isReturn()       const { return flags[IsReturn]; }
159    bool isDirectCtrl()   const { return flags[IsDirectControl]; }
160    bool isIndirectCtrl() const { return flags[IsIndirectControl]; }
161    bool isCondCtrl()     const { return flags[IsCondControl]; }
162    bool isUncondCtrl()   const { return flags[IsUncondControl]; }
163    bool isCondDelaySlot() const { return flags[IsCondDelaySlot]; }
164
165    bool isThreadSync()   const { return flags[IsThreadSync]; }
166    bool isSerializing()  const { return flags[IsSerializing] ||
167                                      flags[IsSerializeBefore] ||
168                                      flags[IsSerializeAfter]; }
169    bool isSerializeBefore() const { return flags[IsSerializeBefore]; }
170    bool isSerializeAfter() const { return flags[IsSerializeAfter]; }
171    bool isSquashAfter() const { return flags[IsSquashAfter]; }
172    bool isMemBarrier()   const { return flags[IsMemBarrier]; }
173    bool isWriteBarrier() const { return flags[IsWriteBarrier]; }
174    bool isNonSpeculative() const { return flags[IsNonSpeculative]; }
175    bool isQuiesce() const { return flags[IsQuiesce]; }
176    bool isIprAccess() const { return flags[IsIprAccess]; }
177    bool isUnverifiable() const { return flags[IsUnverifiable]; }
178    bool isSyscall() const { return flags[IsSyscall]; }
179    bool isMacroop() const { return flags[IsMacroop]; }
180    bool isMicroop() const { return flags[IsMicroop]; }
181    bool isDelayedCommit() const { return flags[IsDelayedCommit]; }
182    bool isLastMicroop() const { return flags[IsLastMicroop]; }
183    bool isFirstMicroop() const { return flags[IsFirstMicroop]; }
184    //This flag doesn't do anything yet
185    bool isMicroBranch() const { return flags[IsMicroBranch]; }
186    //@}
187
188    void setFirstMicroop() { flags[IsFirstMicroop] = true; }
189    void setLastMicroop() { flags[IsLastMicroop] = true; }
190    void setDelayedCommit() { flags[IsDelayedCommit] = true; }
191    void setFlag(Flags f) { flags[f] = true; }
192
193    /// Operation class.  Used to select appropriate function unit in issue.
194    OpClass opClass()     const { return _opClass; }
195
196
197    /// Return logical index (architectural reg num) of i'th destination reg.
198    /// Only the entries from 0 through numDestRegs()-1 are valid.
199    const RegId& destRegIdx(int i) const { return _destRegIdx[i]; }
200
201    /// Return logical index (architectural reg num) of i'th source reg.
202    /// Only the entries from 0 through numSrcRegs()-1 are valid.
203    const RegId& srcRegIdx(int i)  const { return _srcRegIdx[i]; }
204
205    /// Pointer to a statically allocated "null" instruction object.
206    /// Used to give eaCompInst() and memAccInst() something to return
207    /// when called on non-memory instructions.
208    static StaticInstPtr nullStaticInstPtr;
209
210    /**
211     * Memory references only: returns "fake" instruction representing
212     * the effective address part of the memory operation.  Used to
213     * obtain the dependence info (numSrcRegs and srcRegIdx[]) for
214     * just the EA computation.
215     */
216    virtual const
217    StaticInstPtr &eaCompInst() const { return nullStaticInstPtr; }
218
219    /**
220     * Memory references only: returns "fake" instruction representing
221     * the memory access part of the memory operation.  Used to
222     * obtain the dependence info (numSrcRegs and srcRegIdx[]) for
223     * just the memory access (not the EA computation).
224     */
225    virtual const
226    StaticInstPtr &memAccInst() const { return nullStaticInstPtr; }
227
228    /// The binary machine instruction.
229    const ExtMachInst machInst;
230
231  protected:
232
233    /// See destRegIdx().
234    RegId _destRegIdx[MaxInstDestRegs];
235    /// See srcRegIdx().
236    RegId _srcRegIdx[MaxInstSrcRegs];
237
238    /**
239     * Base mnemonic (e.g., "add").  Used by generateDisassembly()
240     * methods.  Also useful to readily identify instructions from
241     * within the debugger when #cachedDisassembly has not been
242     * initialized.
243     */
244    const char *mnemonic;
245
246    /**
247     * String representation of disassembly (lazily evaluated via
248     * disassemble()).
249     */
250    mutable std::string *cachedDisassembly;
251
252    /**
253     * Internal function to generate disassembly string.
254     */
255    virtual std::string
256    generateDisassembly(Addr pc, const SymbolTable *symtab) const = 0;
257
258    /// Constructor.
259    /// It's important to initialize everything here to a sane
260    /// default, since the decoder generally only overrides
261    /// the fields that are meaningful for the particular
262    /// instruction.
263    StaticInst(const char *_mnemonic, ExtMachInst _machInst, OpClass __opClass)
264        : _opClass(__opClass), _numSrcRegs(0), _numDestRegs(0),
265          _numFPDestRegs(0), _numIntDestRegs(0), _numCCDestRegs(0),
266          _numVecDestRegs(0), _numVecElemDestRegs(0), machInst(_machInst),
267          mnemonic(_mnemonic), cachedDisassembly(0)
268    { }
269
270  public:
271    virtual ~StaticInst();
272
273    virtual Fault execute(ExecContext *xc,
274                          Trace::InstRecord *traceData) const = 0;
275    virtual Fault eaComp(ExecContext *xc,
276                         Trace::InstRecord *traceData) const
277    {
278        panic("eaComp not defined!");
279    }
280
281    virtual Fault initiateAcc(ExecContext *xc,
282                              Trace::InstRecord *traceData) const
283    {
284        panic("initiateAcc not defined!");
285    }
286
287    virtual Fault completeAcc(Packet *pkt, ExecContext *xc,
288                              Trace::InstRecord *traceData) const
289    {
290        panic("completeAcc not defined!");
291    }
292
293    virtual void advancePC(TheISA::PCState &pcState) const = 0;
294
295    /**
296     * Return the microop that goes with a particular micropc. This should
297     * only be defined/used in macroops which will contain microops
298     */
299    virtual StaticInstPtr fetchMicroop(MicroPC upc) const;
300
301    /**
302     * Return the target address for a PC-relative branch.
303     * Invalid if not a PC-relative branch (i.e. isDirectCtrl()
304     * should be true).
305     */
306    virtual TheISA::PCState branchTarget(const TheISA::PCState &pc) const;
307
308    /**
309     * Return the target address for an indirect branch (jump).  The
310     * register value is read from the supplied thread context, so
311     * the result is valid only if the thread context is about to
312     * execute the branch in question.  Invalid if not an indirect
313     * branch (i.e. isIndirectCtrl() should be true).
314     */
315    virtual TheISA::PCState branchTarget(ThreadContext *tc) const;
316
317    /**
318     * Return true if the instruction is a control transfer, and if so,
319     * return the target address as well.
320     */
321    bool hasBranchTarget(const TheISA::PCState &pc, ThreadContext *tc,
322                         TheISA::PCState &tgt) const;
323
324    /**
325     * Return string representation of disassembled instruction.
326     * The default version of this function will call the internal
327     * virtual generateDisassembly() function to get the string,
328     * then cache it in #cachedDisassembly.  If the disassembly
329     * should not be cached, this function should be overridden directly.
330     */
331    virtual const std::string &disassemble(Addr pc,
332        const SymbolTable *symtab = 0) const;
333
334    /**
335     * Print a separator separated list of this instruction's set flag
336     * names on the given stream.
337     */
338    void printFlags(std::ostream &outs, const std::string &separator) const;
339
340    /// Return name of machine instruction
341    std::string getName() { return mnemonic; }
342};
343
344#endif // __CPU_STATIC_INST_HH__
345