types.hh revision 11990
110399Sstephan.diestelhorst@arm.com/*
211603Sandreas.hansson@arm.com * Copyright (c) 2003-2005 The Regents of The University of Michigan
310399Sstephan.diestelhorst@arm.com * All rights reserved.
410399Sstephan.diestelhorst@arm.com *
510399Sstephan.diestelhorst@arm.com * Redistribution and use in source and binary forms, with or without
610399Sstephan.diestelhorst@arm.com * modification, are permitted provided that the following conditions are
710399Sstephan.diestelhorst@arm.com * met: redistributions of source code must retain the above copyright
810399Sstephan.diestelhorst@arm.com * notice, this list of conditions and the following disclaimer;
910399Sstephan.diestelhorst@arm.com * redistributions in binary form must reproduce the above copyright
1010399Sstephan.diestelhorst@arm.com * notice, this list of conditions and the following disclaimer in the
1110399Sstephan.diestelhorst@arm.com * documentation and/or other materials provided with the distribution;
1210399Sstephan.diestelhorst@arm.com * neither the name of the copyright holders nor the names of its
1310399Sstephan.diestelhorst@arm.com * contributors may be used to endorse or promote products derived from
1410399Sstephan.diestelhorst@arm.com * this software without specific prior written permission.
1510399Sstephan.diestelhorst@arm.com *
1610399Sstephan.diestelhorst@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1710399Sstephan.diestelhorst@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1810399Sstephan.diestelhorst@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1910399Sstephan.diestelhorst@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2010399Sstephan.diestelhorst@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2110399Sstephan.diestelhorst@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2210399Sstephan.diestelhorst@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2310399Sstephan.diestelhorst@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2410399Sstephan.diestelhorst@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2510399Sstephan.diestelhorst@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2610399Sstephan.diestelhorst@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2710399Sstephan.diestelhorst@arm.com *
2810399Sstephan.diestelhorst@arm.com * Authors: Nathan Binkert
2910399Sstephan.diestelhorst@arm.com */
3010399Sstephan.diestelhorst@arm.com
3110399Sstephan.diestelhorst@arm.com/**
3210399Sstephan.diestelhorst@arm.com * @file
3310399Sstephan.diestelhorst@arm.com * Defines global host-dependent types:
3410399Sstephan.diestelhorst@arm.com * Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t.
3510399Sstephan.diestelhorst@arm.com */
3610399Sstephan.diestelhorst@arm.com
3711135Sandreas.hansson@arm.com#ifndef __BASE_TYPES_HH__
3810399Sstephan.diestelhorst@arm.com#define __BASE_TYPES_HH__
3910399Sstephan.diestelhorst@arm.com
4010399Sstephan.diestelhorst@arm.com#include <inttypes.h>
4110399Sstephan.diestelhorst@arm.com
4211135Sandreas.hansson@arm.com#include <cassert>
4310399Sstephan.diestelhorst@arm.com#include <memory>
4410399Sstephan.diestelhorst@arm.com#include <ostream>
4510399Sstephan.diestelhorst@arm.com#include <stdexcept>
4610399Sstephan.diestelhorst@arm.com
4710399Sstephan.diestelhorst@arm.com#include "base/refcnt.hh"
4810399Sstephan.diestelhorst@arm.com
4910399Sstephan.diestelhorst@arm.com/** uint64_t constant */
5010399Sstephan.diestelhorst@arm.com#define ULL(N)          ((uint64_t)N##ULL)
5111129Sali.jafri@arm.com/** int64_t constant */
5211129Sali.jafri@arm.com#define LL(N)           ((int64_t)N##LL)
5311129Sali.jafri@arm.com
5411129Sali.jafri@arm.com/** Statistics counter type.  Not much excuse for not using a 64-bit
5511129Sali.jafri@arm.com * integer here, but if you're desperate and only run short
5611129Sali.jafri@arm.com * simulations you could make this 32 bits.
5711129Sali.jafri@arm.com */
5811129Sali.jafri@arm.comtypedef int64_t Counter;
5911129Sali.jafri@arm.com
6011129Sali.jafri@arm.com/**
6111129Sali.jafri@arm.com * Tick count type.
6210399Sstephan.diestelhorst@arm.com */
6310399Sstephan.diestelhorst@arm.comtypedef uint64_t Tick;
6410399Sstephan.diestelhorst@arm.com
6511744Snikos.nikoleris@arm.comconst Tick MaxTick = ULL(0xffffffffffffffff);
6611744Snikos.nikoleris@arm.com
6710399Sstephan.diestelhorst@arm.com/**
6811603Sandreas.hansson@arm.com * Cycles is a wrapper class for representing cycle counts, i.e. a
6911603Sandreas.hansson@arm.com * relative difference between two points in time, expressed in a
7011603Sandreas.hansson@arm.com * number of clock cycles.
7111128Sali.jafri@arm.com *
7211605Snikos.nikoleris@arm.com * The Cycles wrapper class is a type-safe alternative to a
7311605Snikos.nikoleris@arm.com * typedef, aiming to avoid unintentional mixing of cycles and ticks
7411605Snikos.nikoleris@arm.com * in the code base.
7510399Sstephan.diestelhorst@arm.com *
7611131Sandreas.hansson@arm.com * Note that there is no overloading of the bool operator as the
7711131Sandreas.hansson@arm.com * compiler is allowed to turn booleans into integers and this causes
7811128Sali.jafri@arm.com * a whole range of issues in a handful locations. The solution to
7911128Sali.jafri@arm.com * this problem would be to use the safe bool idiom, but for now we
8011128Sali.jafri@arm.com * make do without the test and use the more elaborate comparison >
8111128Sali.jafri@arm.com * Cycles(0).
8211128Sali.jafri@arm.com */
8311128Sali.jafri@arm.comclass Cycles
8411128Sali.jafri@arm.com{
8511131Sandreas.hansson@arm.com
8611131Sandreas.hansson@arm.com  private:
8711131Sandreas.hansson@arm.com
8811131Sandreas.hansson@arm.com    /** Member holding the actual value. */
8910403Sstephan.diestelhorst@arm.com    uint64_t c;
9010403Sstephan.diestelhorst@arm.com
9111129Sali.jafri@arm.com  public:
9211129Sali.jafri@arm.com
9311129Sali.jafri@arm.com    /** Explicit constructor assigning a value. */
9411129Sali.jafri@arm.com    explicit constexpr Cycles(uint64_t _c) : c(_c) { }
9511129Sali.jafri@arm.com
9610403Sstephan.diestelhorst@arm.com    /** Default constructor for parameter classes. */
9710403Sstephan.diestelhorst@arm.com    Cycles() : c(0) { }
9810403Sstephan.diestelhorst@arm.com
9910403Sstephan.diestelhorst@arm.com    /** Converting back to the value type. */
10010403Sstephan.diestelhorst@arm.com    constexpr operator uint64_t() const { return c; }
10110403Sstephan.diestelhorst@arm.com
10210403Sstephan.diestelhorst@arm.com    /** Prefix increment operator. */
10310403Sstephan.diestelhorst@arm.com    Cycles& operator++()
10410399Sstephan.diestelhorst@arm.com    { ++c; return *this; }
10510399Sstephan.diestelhorst@arm.com
10610399Sstephan.diestelhorst@arm.com    /** Prefix decrement operator. Is only temporarily used in the O3 CPU. */
10710399Sstephan.diestelhorst@arm.com    Cycles& operator--()
10811129Sali.jafri@arm.com    { assert(c != 0); --c; return *this; }
10911129Sali.jafri@arm.com
11011129Sali.jafri@arm.com    /** In-place addition of cycles. */
11111129Sali.jafri@arm.com    Cycles& operator+=(const Cycles& cc)
11211129Sali.jafri@arm.com    { c += cc.c; return *this; }
11311129Sali.jafri@arm.com
11411284Sandreas.hansson@arm.com    /** Greater than comparison used for > Cycles(0). */
11510399Sstephan.diestelhorst@arm.com    constexpr bool operator>(const Cycles& cc) const
11611129Sali.jafri@arm.com    { return c > cc.c; }
11710399Sstephan.diestelhorst@arm.com
11810399Sstephan.diestelhorst@arm.com    constexpr Cycles operator +(const Cycles& b) const
11910399Sstephan.diestelhorst@arm.com    { return Cycles(c + b.c); }
12010399Sstephan.diestelhorst@arm.com
12111129Sali.jafri@arm.com    constexpr Cycles operator -(const Cycles& b) const
12211129Sali.jafri@arm.com    {
12310399Sstephan.diestelhorst@arm.com        return c >= b.c ? Cycles(c - b.c) :
12410399Sstephan.diestelhorst@arm.com            throw std::invalid_argument("RHS cycle value larger than LHS");
12510399Sstephan.diestelhorst@arm.com    }
12610399Sstephan.diestelhorst@arm.com
12710399Sstephan.diestelhorst@arm.com    constexpr Cycles operator <<(const int32_t shift) const
12810399Sstephan.diestelhorst@arm.com    { return Cycles(c << shift); }
12911129Sali.jafri@arm.com
13011129Sali.jafri@arm.com    constexpr Cycles operator >>(const int32_t shift) const
13110399Sstephan.diestelhorst@arm.com    { return Cycles(c >> shift); }
13210399Sstephan.diestelhorst@arm.com
13311129Sali.jafri@arm.com    friend std::ostream& operator<<(std::ostream &out, const Cycles & cycles);
13411199Sandreas.hansson@arm.com};
13511129Sali.jafri@arm.com
13611129Sali.jafri@arm.com/**
13711129Sali.jafri@arm.com * Address type
13811129Sali.jafri@arm.com * This will probably be moved somewhere else in the near future.
13911129Sali.jafri@arm.com * This should be at least as big as the biggest address width in use
14011129Sali.jafri@arm.com * in the system, which will probably be 64 bits.
14111129Sali.jafri@arm.com */
14211129Sali.jafri@arm.comtypedef uint64_t Addr;
14311129Sali.jafri@arm.com
14411129Sali.jafri@arm.comtypedef uint16_t MicroPC;
14511129Sali.jafri@arm.com
14610399Sstephan.diestelhorst@arm.comstatic const MicroPC MicroPCRomBit = 1 << (sizeof(MicroPC) * 8 - 1);
14711129Sali.jafri@arm.com
14810403Sstephan.diestelhorst@arm.comstatic inline MicroPC
14910399Sstephan.diestelhorst@arm.comromMicroPC(MicroPC upc)
15010399Sstephan.diestelhorst@arm.com{
15110399Sstephan.diestelhorst@arm.com    return upc | MicroPCRomBit;
15211605Snikos.nikoleris@arm.com}
15310399Sstephan.diestelhorst@arm.com
15411131Sandreas.hansson@arm.comstatic inline MicroPC
15511131Sandreas.hansson@arm.comnormalMicroPC(MicroPC upc)
15611131Sandreas.hansson@arm.com{
15711605Snikos.nikoleris@arm.com    return upc & ~MicroPCRomBit;
15811605Snikos.nikoleris@arm.com}
15911605Snikos.nikoleris@arm.com
16011605Snikos.nikoleris@arm.comstatic inline bool
16111605Snikos.nikoleris@arm.comisRomMicroPC(MicroPC upc)
16211131Sandreas.hansson@arm.com{
16311131Sandreas.hansson@arm.com    return MicroPCRomBit & upc;
16411131Sandreas.hansson@arm.com}
16511131Sandreas.hansson@arm.com
16611131Sandreas.hansson@arm.comconst Addr MaxAddr = (Addr)-1;
16710399Sstephan.diestelhorst@arm.com
16811131Sandreas.hansson@arm.com/**
16911131Sandreas.hansson@arm.com * Thread index/ID type
17011131Sandreas.hansson@arm.com */
17110821Sandreas.hansson@arm.comtypedef int16_t ThreadID;
17211131Sandreas.hansson@arm.comconst ThreadID InvalidThreadID = (ThreadID)-1;
17310399Sstephan.diestelhorst@arm.com
17410399Sstephan.diestelhorst@arm.com/** Globally unique thread context ID */
17510399Sstephan.diestelhorst@arm.comtypedef int ContextID;
17610399Sstephan.diestelhorst@arm.comconst ContextID InvalidContextID = (ContextID)-1;
17710399Sstephan.diestelhorst@arm.com
17810399Sstephan.diestelhorst@arm.com/**
17911744Snikos.nikoleris@arm.com * Port index/ID type, and a symbolic name for an invalid port id.
18010399Sstephan.diestelhorst@arm.com */
18110399Sstephan.diestelhorst@arm.comtypedef int16_t PortID;
18210399Sstephan.diestelhorst@arm.comconst PortID InvalidPortID = (PortID)-1;
18311128Sali.jafri@arm.com
18411605Snikos.nikoleris@arm.comclass FaultBase;
18511605Snikos.nikoleris@arm.comtypedef std::shared_ptr<FaultBase> Fault;
18611605Snikos.nikoleris@arm.com
18710403Sstephan.diestelhorst@arm.com// Rather than creating a shared_ptr instance and assigning it nullptr,
18810403Sstephan.diestelhorst@arm.com// we just create an alias.
18911129Sali.jafri@arm.comconstexpr decltype(nullptr) NoFault = nullptr;
19011132Sali.jafri@arm.com
19111132Sali.jafri@arm.comstruct AtomicOpFunctor
19211132Sali.jafri@arm.com{
19311132Sali.jafri@arm.com    virtual void operator()(uint8_t *p) = 0;
19411134Sandreas.hansson@arm.com    virtual ~AtomicOpFunctor() {}
19511134Sandreas.hansson@arm.com};
19611134Sandreas.hansson@arm.com
19711134Sandreas.hansson@arm.comtemplate <class T>
19811129Sali.jafri@arm.comstruct TypedAtomicOpFunctor : public AtomicOpFunctor
19911129Sali.jafri@arm.com{
20011129Sali.jafri@arm.com    void operator()(uint8_t *p) { execute((T *)p); }
20110399Sstephan.diestelhorst@arm.com    virtual void execute(T * p) = 0;
20210399Sstephan.diestelhorst@arm.com};
20310399Sstephan.diestelhorst@arm.com
20410399Sstephan.diestelhorst@arm.comenum ByteOrder {
20510399Sstephan.diestelhorst@arm.com    BigEndianByteOrder,
20610403Sstephan.diestelhorst@arm.com    LittleEndianByteOrder
20710403Sstephan.diestelhorst@arm.com};
20811134Sandreas.hansson@arm.com
20911134Sandreas.hansson@arm.com#endif // __BASE_TYPES_HH__
21011134Sandreas.hansson@arm.com