inst_res.hh revision 12109
112107SRekai.GonzalezAlberquilla@arm.com/*
212107SRekai.GonzalezAlberquilla@arm.com * Copyright (c) 2016 ARM Limited
312107SRekai.GonzalezAlberquilla@arm.com * All rights reserved
412107SRekai.GonzalezAlberquilla@arm.com *
512107SRekai.GonzalezAlberquilla@arm.com * The license below extends only to copyright in the software and shall
612107SRekai.GonzalezAlberquilla@arm.com * not be construed as granting a license to any other intellectual
712107SRekai.GonzalezAlberquilla@arm.com * property including but not limited to intellectual property relating
812107SRekai.GonzalezAlberquilla@arm.com * to a hardware implementation of the functionality of the software
912107SRekai.GonzalezAlberquilla@arm.com * licensed hereunder.  You may use the software subject to the license
1012107SRekai.GonzalezAlberquilla@arm.com * terms below provided that you ensure that this notice is replicated
1112107SRekai.GonzalezAlberquilla@arm.com * unmodified and in its entirety in all distributions of the software,
1212107SRekai.GonzalezAlberquilla@arm.com * modified or unmodified, in source code or in binary form.
1312107SRekai.GonzalezAlberquilla@arm.com *
1412107SRekai.GonzalezAlberquilla@arm.com * Redistribution and use in source and binary forms, with or without
1512107SRekai.GonzalezAlberquilla@arm.com * modification, are permitted provided that the following conditions are
1612107SRekai.GonzalezAlberquilla@arm.com * met: redistributions of source code must retain the above copyright
1712107SRekai.GonzalezAlberquilla@arm.com * notice, this list of conditions and the following disclaimer;
1812107SRekai.GonzalezAlberquilla@arm.com * redistributions in binary form must reproduce the above copyright
1912107SRekai.GonzalezAlberquilla@arm.com * notice, this list of conditions and the following disclaimer in the
2012107SRekai.GonzalezAlberquilla@arm.com * documentation and/or other materials provided with the distribution;
2112107SRekai.GonzalezAlberquilla@arm.com * neither the name of the copyright holders nor the names of its
2212107SRekai.GonzalezAlberquilla@arm.com * contributors may be used to endorse or promote products derived from
2312107SRekai.GonzalezAlberquilla@arm.com * this software without specific prior written permission.
2412107SRekai.GonzalezAlberquilla@arm.com *
2512107SRekai.GonzalezAlberquilla@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2612107SRekai.GonzalezAlberquilla@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2712107SRekai.GonzalezAlberquilla@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2812107SRekai.GonzalezAlberquilla@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2912107SRekai.GonzalezAlberquilla@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3012107SRekai.GonzalezAlberquilla@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3112107SRekai.GonzalezAlberquilla@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3212107SRekai.GonzalezAlberquilla@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3312107SRekai.GonzalezAlberquilla@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3412107SRekai.GonzalezAlberquilla@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3512107SRekai.GonzalezAlberquilla@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3612107SRekai.GonzalezAlberquilla@arm.com *
3712107SRekai.GonzalezAlberquilla@arm.com * Authors: Nathanael Premillieu
3812107SRekai.GonzalezAlberquilla@arm.com */
3912107SRekai.GonzalezAlberquilla@arm.com
4012107SRekai.GonzalezAlberquilla@arm.com#ifndef __CPU_INST_RES_HH__
4112107SRekai.GonzalezAlberquilla@arm.com#define __CPU_INST_RES_HH__
4212107SRekai.GonzalezAlberquilla@arm.com
4312107SRekai.GonzalezAlberquilla@arm.com#include <type_traits>
4412107SRekai.GonzalezAlberquilla@arm.com
4512107SRekai.GonzalezAlberquilla@arm.com#include "arch/generic/types.hh"
4612109SRekai.GonzalezAlberquilla@arm.com#include "arch/generic/vec_reg.hh"
4712107SRekai.GonzalezAlberquilla@arm.com
4812107SRekai.GonzalezAlberquilla@arm.comclass InstResult {
4912109SRekai.GonzalezAlberquilla@arm.com    using VecRegContainer = TheISA::VecRegContainer;
5012109SRekai.GonzalezAlberquilla@arm.com    using VecElem = TheISA::VecElem;
5112107SRekai.GonzalezAlberquilla@arm.com  public:
5212107SRekai.GonzalezAlberquilla@arm.com    union MultiResult {
5312107SRekai.GonzalezAlberquilla@arm.com        uint64_t integer;
5412107SRekai.GonzalezAlberquilla@arm.com        double dbl;
5512109SRekai.GonzalezAlberquilla@arm.com        VecRegContainer vector;
5612109SRekai.GonzalezAlberquilla@arm.com        VecElem vecElem;
5712107SRekai.GonzalezAlberquilla@arm.com        MultiResult() {}
5812107SRekai.GonzalezAlberquilla@arm.com    };
5912107SRekai.GonzalezAlberquilla@arm.com
6012107SRekai.GonzalezAlberquilla@arm.com    enum class ResultType {
6112107SRekai.GonzalezAlberquilla@arm.com        Scalar,
6212109SRekai.GonzalezAlberquilla@arm.com        VecElem,
6312109SRekai.GonzalezAlberquilla@arm.com        VecReg,
6412107SRekai.GonzalezAlberquilla@arm.com        NumResultTypes,
6512107SRekai.GonzalezAlberquilla@arm.com        Invalid
6612107SRekai.GonzalezAlberquilla@arm.com    };
6712107SRekai.GonzalezAlberquilla@arm.com
6812107SRekai.GonzalezAlberquilla@arm.com  private:
6912107SRekai.GonzalezAlberquilla@arm.com    MultiResult result;
7012107SRekai.GonzalezAlberquilla@arm.com    ResultType type;
7112107SRekai.GonzalezAlberquilla@arm.com
7212107SRekai.GonzalezAlberquilla@arm.com  public:
7312107SRekai.GonzalezAlberquilla@arm.com    /** Default constructor creates an invalid result. */
7412107SRekai.GonzalezAlberquilla@arm.com    InstResult() : type(ResultType::Invalid) { }
7512107SRekai.GonzalezAlberquilla@arm.com    /** Scalar result from scalar. */
7612107SRekai.GonzalezAlberquilla@arm.com    template<typename T>
7712107SRekai.GonzalezAlberquilla@arm.com    explicit InstResult(T i, const ResultType& t) : type(t) {
7812107SRekai.GonzalezAlberquilla@arm.com        static_assert(std::is_integral<T>::value ^
7912107SRekai.GonzalezAlberquilla@arm.com                        std::is_floating_point<T>::value,
8012107SRekai.GonzalezAlberquilla@arm.com                "Parameter type is neither integral nor fp, or it is both");
8112107SRekai.GonzalezAlberquilla@arm.com        if (std::is_integral<T>::value) {
8212107SRekai.GonzalezAlberquilla@arm.com            result.integer = i;
8312107SRekai.GonzalezAlberquilla@arm.com        } else if (std::is_floating_point<T>::value) {
8412107SRekai.GonzalezAlberquilla@arm.com            result.dbl = i;
8512107SRekai.GonzalezAlberquilla@arm.com        }
8612107SRekai.GonzalezAlberquilla@arm.com    }
8712109SRekai.GonzalezAlberquilla@arm.com    /** Vector result. */
8812109SRekai.GonzalezAlberquilla@arm.com    explicit InstResult(const VecRegContainer& v, const ResultType& t)
8912109SRekai.GonzalezAlberquilla@arm.com        : type(t) { result.vector = v; }
9012107SRekai.GonzalezAlberquilla@arm.com
9112109SRekai.GonzalezAlberquilla@arm.com    InstResult& operator=(const InstResult& that) {
9212109SRekai.GonzalezAlberquilla@arm.com        type = that.type;
9312109SRekai.GonzalezAlberquilla@arm.com        switch (type) {
9412109SRekai.GonzalezAlberquilla@arm.com        /* Given that misc regs are not written to, there may be invalids in
9512109SRekai.GonzalezAlberquilla@arm.com         * the result stack. */
9612109SRekai.GonzalezAlberquilla@arm.com        case ResultType::Invalid:
9712109SRekai.GonzalezAlberquilla@arm.com            break;
9812109SRekai.GonzalezAlberquilla@arm.com        case ResultType::Scalar:
9912109SRekai.GonzalezAlberquilla@arm.com            result.integer = that.result.integer;
10012109SRekai.GonzalezAlberquilla@arm.com            break;
10112109SRekai.GonzalezAlberquilla@arm.com        case ResultType::VecElem:
10212109SRekai.GonzalezAlberquilla@arm.com            result.vecElem = that.result.vecElem;
10312109SRekai.GonzalezAlberquilla@arm.com            break;
10412109SRekai.GonzalezAlberquilla@arm.com        case ResultType::VecReg:
10512109SRekai.GonzalezAlberquilla@arm.com            result.vector = that.result.vector;
10612109SRekai.GonzalezAlberquilla@arm.com            break;
10712109SRekai.GonzalezAlberquilla@arm.com        default:
10812109SRekai.GonzalezAlberquilla@arm.com            panic("Assigning result from unknown result type");
10912109SRekai.GonzalezAlberquilla@arm.com            break;
11012109SRekai.GonzalezAlberquilla@arm.com        }
11112109SRekai.GonzalezAlberquilla@arm.com        return *this;
11212109SRekai.GonzalezAlberquilla@arm.com    }
11312107SRekai.GonzalezAlberquilla@arm.com    /**
11412107SRekai.GonzalezAlberquilla@arm.com     * Result comparison
11512107SRekai.GonzalezAlberquilla@arm.com     * Two invalid results always differ.
11612107SRekai.GonzalezAlberquilla@arm.com     */
11712107SRekai.GonzalezAlberquilla@arm.com    bool operator==(const InstResult& that) const {
11812107SRekai.GonzalezAlberquilla@arm.com        if (this->type != that.type)
11912107SRekai.GonzalezAlberquilla@arm.com            return false;
12012107SRekai.GonzalezAlberquilla@arm.com        switch (type) {
12112107SRekai.GonzalezAlberquilla@arm.com        case ResultType::Scalar:
12212107SRekai.GonzalezAlberquilla@arm.com            return result.integer == that.result.integer;
12312109SRekai.GonzalezAlberquilla@arm.com        case ResultType::VecElem:
12412109SRekai.GonzalezAlberquilla@arm.com            return result.vecElem == that.result.vecElem;
12512109SRekai.GonzalezAlberquilla@arm.com        case ResultType::VecReg:
12612109SRekai.GonzalezAlberquilla@arm.com            return result.vector == that.result.vector;
12712107SRekai.GonzalezAlberquilla@arm.com        case ResultType::Invalid:
12812107SRekai.GonzalezAlberquilla@arm.com            return false;
12912107SRekai.GonzalezAlberquilla@arm.com        default:
13012107SRekai.GonzalezAlberquilla@arm.com            panic("Unknown type of result: %d\n", (int)type);
13112107SRekai.GonzalezAlberquilla@arm.com        }
13212107SRekai.GonzalezAlberquilla@arm.com    }
13312107SRekai.GonzalezAlberquilla@arm.com
13412107SRekai.GonzalezAlberquilla@arm.com    bool operator!=(const InstResult& that) const {
13512107SRekai.GonzalezAlberquilla@arm.com        return !operator==(that);
13612107SRekai.GonzalezAlberquilla@arm.com    }
13712107SRekai.GonzalezAlberquilla@arm.com
13812107SRekai.GonzalezAlberquilla@arm.com    /** Checks */
13912107SRekai.GonzalezAlberquilla@arm.com    /** @{ */
14012107SRekai.GonzalezAlberquilla@arm.com    /** Is this a scalar result?. */
14112107SRekai.GonzalezAlberquilla@arm.com    bool isScalar() const { return type == ResultType::Scalar; }
14212109SRekai.GonzalezAlberquilla@arm.com    /** Is this a vector result?. */
14312109SRekai.GonzalezAlberquilla@arm.com    bool isVector() const { return type == ResultType::VecReg; }
14412109SRekai.GonzalezAlberquilla@arm.com    /** Is this a vector element result?. */
14512109SRekai.GonzalezAlberquilla@arm.com    bool isVecElem() const { return type == ResultType::VecElem; }
14612107SRekai.GonzalezAlberquilla@arm.com    /** Is this a valid result?. */
14712107SRekai.GonzalezAlberquilla@arm.com    bool isValid() const { return type != ResultType::Invalid; }
14812107SRekai.GonzalezAlberquilla@arm.com    /** @} */
14912107SRekai.GonzalezAlberquilla@arm.com
15012107SRekai.GonzalezAlberquilla@arm.com    /** Explicit cast-like operations. */
15112107SRekai.GonzalezAlberquilla@arm.com    /** @{ */
15212107SRekai.GonzalezAlberquilla@arm.com    const uint64_t&
15312107SRekai.GonzalezAlberquilla@arm.com    asInteger() const
15412107SRekai.GonzalezAlberquilla@arm.com    {
15512107SRekai.GonzalezAlberquilla@arm.com        assert(isScalar());
15612107SRekai.GonzalezAlberquilla@arm.com        return result.integer;
15712107SRekai.GonzalezAlberquilla@arm.com    }
15812107SRekai.GonzalezAlberquilla@arm.com
15912107SRekai.GonzalezAlberquilla@arm.com    /** Cast to integer without checking type.
16012107SRekai.GonzalezAlberquilla@arm.com     * This is required to have the o3 cpu checker happy, as it
16112107SRekai.GonzalezAlberquilla@arm.com     * compares results as integers without being fully aware of
16212107SRekai.GonzalezAlberquilla@arm.com     * their nature. */
16312107SRekai.GonzalezAlberquilla@arm.com    const uint64_t&
16412107SRekai.GonzalezAlberquilla@arm.com    asIntegerNoAssert() const
16512107SRekai.GonzalezAlberquilla@arm.com    {
16612107SRekai.GonzalezAlberquilla@arm.com        return result.integer;
16712107SRekai.GonzalezAlberquilla@arm.com    }
16812109SRekai.GonzalezAlberquilla@arm.com    const VecRegContainer&
16912109SRekai.GonzalezAlberquilla@arm.com    asVector() const
17012109SRekai.GonzalezAlberquilla@arm.com    {
17112109SRekai.GonzalezAlberquilla@arm.com        panic_if(!isVector(), "Converting scalar (or invalid) to vector!!");
17212109SRekai.GonzalezAlberquilla@arm.com        return result.vector;
17312109SRekai.GonzalezAlberquilla@arm.com    }
17412109SRekai.GonzalezAlberquilla@arm.com    const VecElem&
17512109SRekai.GonzalezAlberquilla@arm.com    asVectorElem() const
17612109SRekai.GonzalezAlberquilla@arm.com    {
17712109SRekai.GonzalezAlberquilla@arm.com        panic_if(!isVecElem(), "Converting scalar (or invalid) to vector!!");
17812109SRekai.GonzalezAlberquilla@arm.com        return result.vecElem;
17912109SRekai.GonzalezAlberquilla@arm.com    }
18012107SRekai.GonzalezAlberquilla@arm.com    /** @} */
18112107SRekai.GonzalezAlberquilla@arm.com};
18212107SRekai.GonzalezAlberquilla@arm.com
18312107SRekai.GonzalezAlberquilla@arm.com#endif // __CPU_INST_RES_HH__
184