timing_expr.cc revision 10259
110259SAndrew.Bardsley@arm.com/*
210259SAndrew.Bardsley@arm.com * Copyright (c) 2013-2014 ARM Limited
310259SAndrew.Bardsley@arm.com * All rights reserved.
410259SAndrew.Bardsley@arm.com *
510259SAndrew.Bardsley@arm.com * The license below extends only to copyright in the software and shall
610259SAndrew.Bardsley@arm.com * not be construed as granting a license to any other intellectual
710259SAndrew.Bardsley@arm.com * property including but not limited to intellectual property relating
810259SAndrew.Bardsley@arm.com * to a hardware implementation of the functionality of the software
910259SAndrew.Bardsley@arm.com * licensed hereunder.  You may use the software subject to the license
1010259SAndrew.Bardsley@arm.com * terms below provided that you ensure that this notice is replicated
1110259SAndrew.Bardsley@arm.com * unmodified and in its entirety in all distributions of the software,
1210259SAndrew.Bardsley@arm.com * modified or unmodified, in source code or in binary form.
1310259SAndrew.Bardsley@arm.com *
1410259SAndrew.Bardsley@arm.com * Redistribution and use in source and binary forms, with or without
1510259SAndrew.Bardsley@arm.com * modification, are permitted provided that the following conditions are
1610259SAndrew.Bardsley@arm.com * met: redistributions of source code must retain the above copyright
1710259SAndrew.Bardsley@arm.com * notice, this list of conditions and the following disclaimer;
1810259SAndrew.Bardsley@arm.com * redistributions in binary form must reproduce the above copyright
1910259SAndrew.Bardsley@arm.com * notice, this list of conditions and the following disclaimer in the
2010259SAndrew.Bardsley@arm.com * documentation and/or other materials provided with the distribution;
2110259SAndrew.Bardsley@arm.com * neither the name of the copyright holders nor the names of its
2210259SAndrew.Bardsley@arm.com * contributors may be used to endorse or promote products derived from
2310259SAndrew.Bardsley@arm.com * this software without specific prior written permission.
2410259SAndrew.Bardsley@arm.com *
2510259SAndrew.Bardsley@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2610259SAndrew.Bardsley@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2710259SAndrew.Bardsley@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2810259SAndrew.Bardsley@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2910259SAndrew.Bardsley@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3010259SAndrew.Bardsley@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3110259SAndrew.Bardsley@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3210259SAndrew.Bardsley@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3310259SAndrew.Bardsley@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3410259SAndrew.Bardsley@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3510259SAndrew.Bardsley@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3610259SAndrew.Bardsley@arm.com *
3710259SAndrew.Bardsley@arm.com * Authors: Andrew Bardsley
3810259SAndrew.Bardsley@arm.com */
3910259SAndrew.Bardsley@arm.com
4010259SAndrew.Bardsley@arm.com#include "base/intmath.hh"
4110259SAndrew.Bardsley@arm.com#include "cpu/timing_expr.hh"
4210259SAndrew.Bardsley@arm.com
4310259SAndrew.Bardsley@arm.comTimingExprEvalContext::TimingExprEvalContext (StaticInstPtr inst_,
4410259SAndrew.Bardsley@arm.com    ThreadContext *thread_,
4510259SAndrew.Bardsley@arm.com    TimingExprLet *let_) :
4610259SAndrew.Bardsley@arm.com    inst(inst_), thread(thread_), let(let_)
4710259SAndrew.Bardsley@arm.com{
4810259SAndrew.Bardsley@arm.com    /* Reserve space to hold the results of evaluating the
4910259SAndrew.Bardsley@arm.com     *  let expressions */
5010259SAndrew.Bardsley@arm.com    if (let) {
5110259SAndrew.Bardsley@arm.com        unsigned int num_defns = let->defns.size();
5210259SAndrew.Bardsley@arm.com
5310259SAndrew.Bardsley@arm.com        results.resize(num_defns, 0);
5410259SAndrew.Bardsley@arm.com        resultAvailable.resize(num_defns, false);
5510259SAndrew.Bardsley@arm.com    }
5610259SAndrew.Bardsley@arm.com}
5710259SAndrew.Bardsley@arm.com
5810259SAndrew.Bardsley@arm.comuint64_t TimingExprSrcReg::eval(TimingExprEvalContext &context)
5910259SAndrew.Bardsley@arm.com{
6010259SAndrew.Bardsley@arm.com    return context.inst->srcRegIdx(index);
6110259SAndrew.Bardsley@arm.com}
6210259SAndrew.Bardsley@arm.com
6310259SAndrew.Bardsley@arm.comuint64_t TimingExprReadIntReg::eval(TimingExprEvalContext &context)
6410259SAndrew.Bardsley@arm.com{
6510259SAndrew.Bardsley@arm.com    return context.thread->readIntReg(reg->eval(context));
6610259SAndrew.Bardsley@arm.com}
6710259SAndrew.Bardsley@arm.com
6810259SAndrew.Bardsley@arm.comuint64_t TimingExprLet::eval(TimingExprEvalContext &context)
6910259SAndrew.Bardsley@arm.com{
7010259SAndrew.Bardsley@arm.com    TimingExprEvalContext new_context(context.inst,
7110259SAndrew.Bardsley@arm.com        context.thread, this);
7210259SAndrew.Bardsley@arm.com
7310259SAndrew.Bardsley@arm.com    return expr->eval(new_context);
7410259SAndrew.Bardsley@arm.com}
7510259SAndrew.Bardsley@arm.com
7610259SAndrew.Bardsley@arm.comuint64_t TimingExprRef::eval(TimingExprEvalContext &context)
7710259SAndrew.Bardsley@arm.com{
7810259SAndrew.Bardsley@arm.com    /* Lookup the result, evaluating if necessary.  @todo, this
7910259SAndrew.Bardsley@arm.com     *  should have more error checking */
8010259SAndrew.Bardsley@arm.com    if (!context.resultAvailable[index]) {
8110259SAndrew.Bardsley@arm.com        context.results[index] = context.let->defns[index]->eval(context);
8210259SAndrew.Bardsley@arm.com        context.resultAvailable[index] = true;
8310259SAndrew.Bardsley@arm.com    }
8410259SAndrew.Bardsley@arm.com
8510259SAndrew.Bardsley@arm.com    return context.results[index];
8610259SAndrew.Bardsley@arm.com}
8710259SAndrew.Bardsley@arm.com
8810259SAndrew.Bardsley@arm.comuint64_t TimingExprUn::eval(TimingExprEvalContext &context)
8910259SAndrew.Bardsley@arm.com{
9010259SAndrew.Bardsley@arm.com    uint64_t arg_value = arg->eval(context);
9110259SAndrew.Bardsley@arm.com    uint64_t ret = 0;
9210259SAndrew.Bardsley@arm.com
9310259SAndrew.Bardsley@arm.com    switch (op) {
9410259SAndrew.Bardsley@arm.com      case Enums::timingExprSizeInBits:
9510259SAndrew.Bardsley@arm.com        if (arg_value == 0)
9610259SAndrew.Bardsley@arm.com            ret = 0;
9710259SAndrew.Bardsley@arm.com        else
9810259SAndrew.Bardsley@arm.com            ret = ceilLog2(arg_value);
9910259SAndrew.Bardsley@arm.com        break;
10010259SAndrew.Bardsley@arm.com      case Enums::timingExprNot:
10110259SAndrew.Bardsley@arm.com        ret = arg_value != 0;
10210259SAndrew.Bardsley@arm.com        break;
10310259SAndrew.Bardsley@arm.com      case Enums::timingExprInvert:
10410259SAndrew.Bardsley@arm.com        ret = ~arg_value;
10510259SAndrew.Bardsley@arm.com        break;
10610259SAndrew.Bardsley@arm.com      case Enums::timingExprSignExtend32To64:
10710259SAndrew.Bardsley@arm.com        ret = static_cast<int64_t>(
10810259SAndrew.Bardsley@arm.com            static_cast<int32_t>(arg_value));
10910259SAndrew.Bardsley@arm.com        break;
11010259SAndrew.Bardsley@arm.com      case Enums::timingExprAbs:
11110259SAndrew.Bardsley@arm.com        if (static_cast<int64_t>(arg_value) < 0)
11210259SAndrew.Bardsley@arm.com            ret = -arg_value;
11310259SAndrew.Bardsley@arm.com        else
11410259SAndrew.Bardsley@arm.com            ret = arg_value;
11510259SAndrew.Bardsley@arm.com        break;
11610259SAndrew.Bardsley@arm.com      default:
11710259SAndrew.Bardsley@arm.com        break;
11810259SAndrew.Bardsley@arm.com    }
11910259SAndrew.Bardsley@arm.com
12010259SAndrew.Bardsley@arm.com    return ret;
12110259SAndrew.Bardsley@arm.com}
12210259SAndrew.Bardsley@arm.com
12310259SAndrew.Bardsley@arm.comuint64_t TimingExprBin::eval(TimingExprEvalContext &context)
12410259SAndrew.Bardsley@arm.com{
12510259SAndrew.Bardsley@arm.com    uint64_t left_value = left->eval(context);
12610259SAndrew.Bardsley@arm.com    uint64_t right_value = right->eval(context);
12710259SAndrew.Bardsley@arm.com    uint64_t ret = 0;
12810259SAndrew.Bardsley@arm.com
12910259SAndrew.Bardsley@arm.com    switch (op) {
13010259SAndrew.Bardsley@arm.com      case Enums::timingExprAdd:
13110259SAndrew.Bardsley@arm.com          ret = left_value + right_value;
13210259SAndrew.Bardsley@arm.com          break;
13310259SAndrew.Bardsley@arm.com      case Enums::timingExprSub:
13410259SAndrew.Bardsley@arm.com          ret = left_value - right_value;
13510259SAndrew.Bardsley@arm.com          break;
13610259SAndrew.Bardsley@arm.com      case Enums::timingExprUMul:
13710259SAndrew.Bardsley@arm.com          ret = left_value * right_value;
13810259SAndrew.Bardsley@arm.com          break;
13910259SAndrew.Bardsley@arm.com      case Enums::timingExprUDiv:
14010259SAndrew.Bardsley@arm.com          if (right_value != 0) {
14110259SAndrew.Bardsley@arm.com              ret = left_value / right_value;
14210259SAndrew.Bardsley@arm.com          }
14310259SAndrew.Bardsley@arm.com          break;
14410259SAndrew.Bardsley@arm.com      case Enums::timingExprUCeilDiv:
14510259SAndrew.Bardsley@arm.com          if (right_value != 0) {
14610259SAndrew.Bardsley@arm.com              ret = (left_value + (right_value - 1)) / right_value;
14710259SAndrew.Bardsley@arm.com          }
14810259SAndrew.Bardsley@arm.com          break;
14910259SAndrew.Bardsley@arm.com      case Enums::timingExprSMul:
15010259SAndrew.Bardsley@arm.com          ret = static_cast<int64_t>(left_value) *
15110259SAndrew.Bardsley@arm.com              static_cast<int64_t>(right_value);
15210259SAndrew.Bardsley@arm.com          break;
15310259SAndrew.Bardsley@arm.com      case Enums::timingExprSDiv:
15410259SAndrew.Bardsley@arm.com          if (right_value != 0) {
15510259SAndrew.Bardsley@arm.com              ret = static_cast<int64_t>(left_value) /
15610259SAndrew.Bardsley@arm.com                  static_cast<int64_t>(right_value);
15710259SAndrew.Bardsley@arm.com          }
15810259SAndrew.Bardsley@arm.com          break;
15910259SAndrew.Bardsley@arm.com      case Enums::timingExprEqual:
16010259SAndrew.Bardsley@arm.com          ret = left_value == right_value;
16110259SAndrew.Bardsley@arm.com          break;
16210259SAndrew.Bardsley@arm.com      case Enums::timingExprNotEqual:
16310259SAndrew.Bardsley@arm.com          ret = left_value != right_value;
16410259SAndrew.Bardsley@arm.com          break;
16510259SAndrew.Bardsley@arm.com      case Enums::timingExprULessThan:
16610259SAndrew.Bardsley@arm.com          ret = left_value < right_value;
16710259SAndrew.Bardsley@arm.com          break;
16810259SAndrew.Bardsley@arm.com      case Enums::timingExprUGreaterThan:
16910259SAndrew.Bardsley@arm.com          ret = left_value > right_value;
17010259SAndrew.Bardsley@arm.com          break;
17110259SAndrew.Bardsley@arm.com      case Enums::timingExprSLessThan:
17210259SAndrew.Bardsley@arm.com          ret = static_cast<int64_t>(left_value) <
17310259SAndrew.Bardsley@arm.com              static_cast<int64_t>(right_value);
17410259SAndrew.Bardsley@arm.com          break;
17510259SAndrew.Bardsley@arm.com      case Enums::timingExprSGreaterThan:
17610259SAndrew.Bardsley@arm.com          ret = static_cast<int64_t>(left_value) >
17710259SAndrew.Bardsley@arm.com              static_cast<int64_t>(right_value);
17810259SAndrew.Bardsley@arm.com          break;
17910259SAndrew.Bardsley@arm.com      case Enums::timingExprAnd:
18010259SAndrew.Bardsley@arm.com          ret = (left_value != 0) && (right_value != 0);
18110259SAndrew.Bardsley@arm.com          break;
18210259SAndrew.Bardsley@arm.com      case Enums::timingExprOr:
18310259SAndrew.Bardsley@arm.com          ret = (left_value != 0) || (right_value != 0);
18410259SAndrew.Bardsley@arm.com          break;
18510259SAndrew.Bardsley@arm.com      default:
18610259SAndrew.Bardsley@arm.com          break;
18710259SAndrew.Bardsley@arm.com    }
18810259SAndrew.Bardsley@arm.com
18910259SAndrew.Bardsley@arm.com    return ret;
19010259SAndrew.Bardsley@arm.com}
19110259SAndrew.Bardsley@arm.com
19210259SAndrew.Bardsley@arm.comuint64_t TimingExprIf::eval(TimingExprEvalContext &context)
19310259SAndrew.Bardsley@arm.com{
19410259SAndrew.Bardsley@arm.com    uint64_t cond_value = cond->eval(context);
19510259SAndrew.Bardsley@arm.com
19610259SAndrew.Bardsley@arm.com    if (cond_value != 0)
19710259SAndrew.Bardsley@arm.com        return trueExpr->eval(context);
19810259SAndrew.Bardsley@arm.com    else
19910259SAndrew.Bardsley@arm.com        return falseExpr->eval(context);
20010259SAndrew.Bardsley@arm.com}
20110259SAndrew.Bardsley@arm.com
20210259SAndrew.Bardsley@arm.comTimingExprLiteral *
20310259SAndrew.Bardsley@arm.comTimingExprLiteralParams::create()
20410259SAndrew.Bardsley@arm.com{
20510259SAndrew.Bardsley@arm.com    return new TimingExprLiteral(this);
20610259SAndrew.Bardsley@arm.com}
20710259SAndrew.Bardsley@arm.com
20810259SAndrew.Bardsley@arm.comTimingExprSrcReg *
20910259SAndrew.Bardsley@arm.comTimingExprSrcRegParams::create()
21010259SAndrew.Bardsley@arm.com{
21110259SAndrew.Bardsley@arm.com    return new TimingExprSrcReg(this);
21210259SAndrew.Bardsley@arm.com}
21310259SAndrew.Bardsley@arm.com
21410259SAndrew.Bardsley@arm.comTimingExprReadIntReg *
21510259SAndrew.Bardsley@arm.comTimingExprReadIntRegParams::create()
21610259SAndrew.Bardsley@arm.com{
21710259SAndrew.Bardsley@arm.com    return new TimingExprReadIntReg(this);
21810259SAndrew.Bardsley@arm.com}
21910259SAndrew.Bardsley@arm.com
22010259SAndrew.Bardsley@arm.comTimingExprLet *
22110259SAndrew.Bardsley@arm.comTimingExprLetParams::create()
22210259SAndrew.Bardsley@arm.com{
22310259SAndrew.Bardsley@arm.com    return new TimingExprLet(this);
22410259SAndrew.Bardsley@arm.com}
22510259SAndrew.Bardsley@arm.com
22610259SAndrew.Bardsley@arm.comTimingExprRef *
22710259SAndrew.Bardsley@arm.comTimingExprRefParams::create()
22810259SAndrew.Bardsley@arm.com{
22910259SAndrew.Bardsley@arm.com    return new TimingExprRef(this);
23010259SAndrew.Bardsley@arm.com}
23110259SAndrew.Bardsley@arm.com
23210259SAndrew.Bardsley@arm.comTimingExprUn *
23310259SAndrew.Bardsley@arm.comTimingExprUnParams::create()
23410259SAndrew.Bardsley@arm.com{
23510259SAndrew.Bardsley@arm.com    return new TimingExprUn(this);
23610259SAndrew.Bardsley@arm.com}
23710259SAndrew.Bardsley@arm.com
23810259SAndrew.Bardsley@arm.comTimingExprBin *
23910259SAndrew.Bardsley@arm.comTimingExprBinParams::create()
24010259SAndrew.Bardsley@arm.com{
24110259SAndrew.Bardsley@arm.com    return new TimingExprBin(this);
24210259SAndrew.Bardsley@arm.com}
24310259SAndrew.Bardsley@arm.com
24410259SAndrew.Bardsley@arm.comTimingExprIf *
24510259SAndrew.Bardsley@arm.comTimingExprIfParams::create()
24610259SAndrew.Bardsley@arm.com{
24710259SAndrew.Bardsley@arm.com    return new TimingExprIf(this);
24810259SAndrew.Bardsley@arm.com}
249