1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
//
// ValueExpressionDecoder.cs
//
// Authors:
// Alexander Chebaturkin (chebaturkin@gmail.com)
//
// Copyright (C) 2012 Alexander Chebaturkin
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using Mono.CodeContracts.Static.AST;
using Mono.CodeContracts.Static.AST.Visitors;
using Mono.CodeContracts.Static.Analysis.ExpressionAnalysis.Decoding;
using Mono.CodeContracts.Static.DataStructures;
using Mono.CodeContracts.Static.Lattices;
using Mono.CodeContracts.Static.Providers;
namespace Mono.CodeContracts.Static.Analysis.Numerical {
class ValueExpressionDecoder<TVar, TExpr> : FullExpressionDecoder<TVar, TExpr>, IExpressionDecoder<TVar, TExpr>
where TVar : IEquatable<TVar>
where TExpr : IEquatable<TExpr> {
readonly VisitorForOperatorFor operator_for;
readonly VisitorForTypeOf type_of;
public ValueExpressionDecoder (IMetaDataProvider metaDataProvider, IExpressionContextProvider<TExpr, TVar> contextProvider)
: base (metaDataProvider, contextProvider)
{
operator_for = new VisitorForOperatorFor ();
type_of = new VisitorForTypeOf ();
}
IExpressionContext<TExpr, TVar> ExpressionContext { get { return ContextProvider.ExpressionContext; } }
#region IExpressionDecoder<TVar,TExpr> Members
public ExpressionOperator OperatorFor (TExpr expr)
{
return ExpressionContext.Decode<TExpr, ExpressionOperator, VisitorForOperatorFor> (expr, operator_for, expr);
}
public TExpr LeftExpressionFor (TExpr expr)
{
UnaryOperator op;
BinaryOperator bop;
TExpr left;
TExpr right;
if (IsUnaryExpression (expr, out op, out left) || IsBinaryExpression (expr, out bop, out left, out right))
return left;
throw new InvalidOperationException ();
}
public TExpr RightExpressionFor (TExpr expr)
{
BinaryOperator bop;
TExpr left;
TExpr right;
if (IsBinaryExpression (expr, out bop, out left, out right))
return right;
throw new InvalidOperationException ();
}
public bool IsConstant (TExpr expr)
{
return OperatorFor (expr) == ExpressionOperator.Constant;
}
public bool IsVariable (TExpr expr)
{
object variable;
return base.IsVariable (expr, out variable);
}
public bool TryValueOf<T> (TExpr e, ExpressionType expectedType, out T result)
{
object value;
TypeNode actualType;
if (base.IsConstant (e, out value, out actualType)) {
if (value is T)
return true.With ((T) value, out result);
if (value is int && expectedType == ExpressionType.Bool) {
result = (T) (object) ((int) value != 0);
return true;
}
}
return false.Without (out result);
}
public bool TrySizeOf (TExpr expr, out int size)
{
TypeNode type;
if (VisitorForSizeOf<TVar, TExpr>.IsSizeOf (expr, out type, this)) {
size = MetaDataProvider.TypeSize (type);
return size != -1;
}
return false.Without (out size);
}
public ExpressionType TypeOf (TExpr expr)
{
if (IsConstant (expr))
return ExpressionContext.Decode<Dummy, ExpressionType, VisitorForTypeOf> (expr, type_of, Dummy.Value);
var abstractType = ExpressionContext.GetType (expr);
if (abstractType.IsNormal ()) {
var type = abstractType.Value;
if (MetaDataProvider.IsPrimitive (type)) {
if (MetaDataProvider.Equal (type, MetaDataProvider.System_Int32))
return ExpressionType.Int32;
if (MetaDataProvider.Equal (type, MetaDataProvider.System_Single))
return ExpressionType.Float32;
if (MetaDataProvider.Equal (type, MetaDataProvider.System_Double))
return ExpressionType.Float64;
if (MetaDataProvider.Equal (type, MetaDataProvider.System_Boolean))
return ExpressionType.Bool;
}
}
return ExpressionType.Unknown;
}
public bool IsConstantInt (TExpr expr, out int value)
{
TypeNode type;
object objValue;
if (IsConstant (expr, out objValue, out type))
return true.With ((int) objValue, out value);
return false.Without (out value);
}
public string NameOf (TVar variable)
{
return variable.ToString ();
}
public bool IsBinaryExpression (TExpr expr)
{
BinaryOperator op;
TExpr left;
TExpr right;
return IsBinaryExpression (expr, out op, out left, out right);
}
#endregion
#region Nested type: VisitorForOperatorFor
class VisitorForOperatorFor : ISymbolicExpressionVisitor<TExpr, TExpr, TVar, TExpr, ExpressionOperator> {
#region ISymbolicExpressionVisitor<TExpr,TExpr,TVar,TExpr,ExpressionOperator> Members
public ExpressionOperator Binary (TExpr pc, BinaryOperator bop, TVar dest, TExpr src1, TExpr src2, TExpr data)
{
switch (bop) {
case BinaryOperator.Add:
case BinaryOperator.Add_Ovf:
case BinaryOperator.Add_Ovf_Un:
return ExpressionOperator.Add;
case BinaryOperator.And:
return ExpressionOperator.And;
case BinaryOperator.Ceq:
return ExpressionOperator.Equal;
case BinaryOperator.Cobjeq:
return ExpressionOperator.Equal_Obj;
case BinaryOperator.Cne_Un:
return ExpressionOperator.NotEqual;
case BinaryOperator.Cge:
case BinaryOperator.Cge_Un:
return ExpressionOperator.GreaterEqualThan;
case BinaryOperator.Cgt:
case BinaryOperator.Cgt_Un:
return ExpressionOperator.GreaterThan;
case BinaryOperator.Cle:
case BinaryOperator.Cle_Un:
return ExpressionOperator.LessEqualThan;
case BinaryOperator.Clt:
case BinaryOperator.Clt_Un:
return ExpressionOperator.LessThan;
case BinaryOperator.Div:
case BinaryOperator.Div_Un:
return ExpressionOperator.Div;
case BinaryOperator.LogicalAnd:
return ExpressionOperator.LogicalAnd;
case BinaryOperator.LogicalOr:
return ExpressionOperator.LogicalOr;
case BinaryOperator.Mul:
case BinaryOperator.Mul_Ovf:
case BinaryOperator.Mul_Ovf_Un:
return ExpressionOperator.Mult;
case BinaryOperator.Or:
return ExpressionOperator.Or;
case BinaryOperator.Rem:
case BinaryOperator.Rem_Un:
return ExpressionOperator.Mod;
case BinaryOperator.Sub:
case BinaryOperator.Sub_Ovf:
case BinaryOperator.Sub_Ovf_Un:
return ExpressionOperator.Sub;
case BinaryOperator.Xor:
return ExpressionOperator.Xor;
default:
return ExpressionOperator.Unknown;
}
}
public ExpressionOperator Unary (TExpr pc, UnaryOperator uop, bool unsigned, TVar dest, TExpr source, TExpr data)
{
switch (uop) {
case UnaryOperator.Conv_i:
case UnaryOperator.Conv_i1:
case UnaryOperator.Conv_i2:
case UnaryOperator.Conv_i4:
case UnaryOperator.Conv_i8:
return ExpressionOperator.ConvertToInt32;
case UnaryOperator.Neg:
return ExpressionOperator.Not;
case UnaryOperator.Not:
return ExpressionOperator.Not;
default:
return ExpressionOperator.Unknown;
}
}
public ExpressionOperator LoadNull (TExpr pc, TVar dest, TExpr polarity)
{
return ExpressionOperator.Constant;
}
public ExpressionOperator LoadConst (TExpr pc, TypeNode type, object constant, TVar dest, TExpr data)
{
return ExpressionOperator.Constant;
}
public ExpressionOperator Sizeof (TExpr pc, TypeNode type, TVar dest, TExpr data)
{
return ExpressionOperator.SizeOf;
}
public ExpressionOperator Isinst (TExpr pc, TypeNode type, TVar dest, TExpr obj, TExpr data)
{
return ExpressionOperator.Unknown;
}
public ExpressionOperator SymbolicConstant (TExpr pc, TVar variable, TExpr data)
{
return ExpressionOperator.Variable;
}
#endregion
}
#endregion
#region Nested type: VisitorForTypeOf
class VisitorForTypeOf : ISymbolicExpressionVisitor<TExpr, TExpr, TVar, Dummy, ExpressionType> {
#region ISymbolicExpressionVisitor<TExpr,TExpr,TVar,Dummy,ExpressionType> Members
public ExpressionType Binary (TExpr pc, BinaryOperator bop, TVar dest, TExpr src1, TExpr src2, Dummy data)
{
return ExpressionType.Unknown;
}
public ExpressionType Unary (TExpr pc, UnaryOperator uop, bool unsigned, TVar dest, TExpr source, Dummy data)
{
return ExpressionType.Unknown;
}
public ExpressionType LoadNull (TExpr pc, TVar dest, Dummy polarity)
{
return ExpressionType.Unknown;
}
public ExpressionType LoadConst (TExpr pc, TypeNode type, object constant, TVar dest, Dummy data)
{
if (constant is int)
return ExpressionType.Int32;
if (constant is float)
return ExpressionType.Float32;
if (constant is double)
return ExpressionType.Float64;
if (constant is bool)
return ExpressionType.Bool;
return ExpressionType.Unknown;
}
public ExpressionType Sizeof (TExpr pc, TypeNode type, TVar dest, Dummy data)
{
return ExpressionType.Int32;
}
public ExpressionType Isinst (TExpr pc, TypeNode type, TVar dest, TExpr obj, Dummy data)
{
return ExpressionType.Unknown;
}
public ExpressionType SymbolicConstant (TExpr pc, TVar variable, Dummy data)
{
return ExpressionType.Unknown;
}
#endregion
}
#endregion
}
}
|