2 * Copyright (c) 2013-2014 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Authors: Andrew Bardsley
40 #include "base/intmath.hh"
41 #include "cpu/timing_expr.hh"
43 TimingExprEvalContext::TimingExprEvalContext(const StaticInstPtr
&inst_
,
44 ThreadContext
*thread_
,
45 TimingExprLet
*let_
) :
46 inst(inst_
), thread(thread_
), let(let_
)
48 /* Reserve space to hold the results of evaluating the
51 unsigned int num_defns
= let
->defns
.size();
53 results
.resize(num_defns
, 0);
54 resultAvailable
.resize(num_defns
, false);
58 uint64_t TimingExprSrcReg::eval(TimingExprEvalContext
&context
)
60 return context
.inst
->srcRegIdx(index
);
63 uint64_t TimingExprReadIntReg::eval(TimingExprEvalContext
&context
)
65 return context
.thread
->readIntReg(reg
->eval(context
));
68 uint64_t TimingExprLet::eval(TimingExprEvalContext
&context
)
70 TimingExprEvalContext
new_context(context
.inst
,
71 context
.thread
, this);
73 return expr
->eval(new_context
);
76 uint64_t TimingExprRef::eval(TimingExprEvalContext
&context
)
78 /* Lookup the result, evaluating if necessary. @todo, this
79 * should have more error checking */
80 if (!context
.resultAvailable
[index
]) {
81 context
.results
[index
] = context
.let
->defns
[index
]->eval(context
);
82 context
.resultAvailable
[index
] = true;
85 return context
.results
[index
];
88 uint64_t TimingExprUn::eval(TimingExprEvalContext
&context
)
90 uint64_t arg_value
= arg
->eval(context
);
94 case Enums::timingExprSizeInBits
:
98 ret
= ceilLog2(arg_value
);
100 case Enums::timingExprNot
:
101 ret
= arg_value
!= 0;
103 case Enums::timingExprInvert
:
106 case Enums::timingExprSignExtend32To64
:
107 ret
= static_cast<int64_t>(
108 static_cast<int32_t>(arg_value
));
110 case Enums::timingExprAbs
:
111 if (static_cast<int64_t>(arg_value
) < 0)
123 uint64_t TimingExprBin::eval(TimingExprEvalContext
&context
)
125 uint64_t left_value
= left
->eval(context
);
126 uint64_t right_value
= right
->eval(context
);
130 case Enums::timingExprAdd
:
131 ret
= left_value
+ right_value
;
133 case Enums::timingExprSub
:
134 ret
= left_value
- right_value
;
136 case Enums::timingExprUMul
:
137 ret
= left_value
* right_value
;
139 case Enums::timingExprUDiv
:
140 if (right_value
!= 0) {
141 ret
= left_value
/ right_value
;
144 case Enums::timingExprUCeilDiv
:
145 if (right_value
!= 0) {
146 ret
= (left_value
+ (right_value
- 1)) / right_value
;
149 case Enums::timingExprSMul
:
150 ret
= static_cast<int64_t>(left_value
) *
151 static_cast<int64_t>(right_value
);
153 case Enums::timingExprSDiv
:
154 if (right_value
!= 0) {
155 ret
= static_cast<int64_t>(left_value
) /
156 static_cast<int64_t>(right_value
);
159 case Enums::timingExprEqual
:
160 ret
= left_value
== right_value
;
162 case Enums::timingExprNotEqual
:
163 ret
= left_value
!= right_value
;
165 case Enums::timingExprULessThan
:
166 ret
= left_value
< right_value
;
168 case Enums::timingExprUGreaterThan
:
169 ret
= left_value
> right_value
;
171 case Enums::timingExprSLessThan
:
172 ret
= static_cast<int64_t>(left_value
) <
173 static_cast<int64_t>(right_value
);
175 case Enums::timingExprSGreaterThan
:
176 ret
= static_cast<int64_t>(left_value
) >
177 static_cast<int64_t>(right_value
);
179 case Enums::timingExprAnd
:
180 ret
= (left_value
!= 0) && (right_value
!= 0);
182 case Enums::timingExprOr
:
183 ret
= (left_value
!= 0) || (right_value
!= 0);
192 uint64_t TimingExprIf::eval(TimingExprEvalContext
&context
)
194 uint64_t cond_value
= cond
->eval(context
);
197 return trueExpr
->eval(context
);
199 return falseExpr
->eval(context
);
203 TimingExprLiteralParams::create()
205 return new TimingExprLiteral(this);
209 TimingExprSrcRegParams::create()
211 return new TimingExprSrcReg(this);
214 TimingExprReadIntReg
*
215 TimingExprReadIntRegParams::create()
217 return new TimingExprReadIntReg(this);
221 TimingExprLetParams::create()
223 return new TimingExprLet(this);
227 TimingExprRefParams::create()
229 return new TimingExprRef(this);
233 TimingExprUnParams::create()
235 return new TimingExprUn(this);
239 TimingExprBinParams::create()
241 return new TimingExprBin(this);
245 TimingExprIfParams::create()
247 return new TimingExprIf(this);