public override Value FromStack(Type targetType) { object result = obj; if (isPrimitive) { TypeIndex resIndex = getTypeIndex((targetType.IsEnum) ? getEnumType(targetType) : targetType); DataModelUtils.FromStack(obj, (int)getTypeIndex(), (int)resIndex, out result); if (targetType.IsEnum) { result = newEnum(targetType, result); } } return(new StructValue(result as ValueType)); }
public override void SetReferencedValue(Value val) { Type t = obj.GetType(); StructValue structVal = val.FromStack(t) as StructValue; if (structVal.IsPrimitive) { DataModelUtils.StoreToBox(obj, (int)(StructValue.getTypeIndex(t)), structVal.Obj); } else { FieldInfo[] fields = obj.GetType().GetFields((BindingFlags) (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ); foreach (FieldInfo field in fields) { field.SetValue(obj, field.GetValue(structVal.Obj)); } } }
/* Performs conversion of primitive value on top of the stack * (corresponds to CILPE.CFG.ConvertValue class) */ public void Perform_ConvertValue(Type type, bool overflow, bool unsigned, out Exception exc) { exc = null; Value val = Pop(); if (!(val is StructValue && (val as StructValue).IsPrimitive)) { throw new InvalidOperandException(); } StructValue primVal = val as StructValue; StructValue.TypeIndex typeIndex = StructValue.getTypeIndex(type), valTypeIndex = primVal.getTypeIndex(); if (!overflow && unsigned && typeIndex != StructValue.TypeIndex.FLOAT64 || overflow && typeIndex == StructValue.TypeIndex.FLOAT32 || overflow && typeIndex == StructValue.TypeIndex.FLOAT64) { throw new InvalidConvertOpException(); } object res; bool success = DataModelUtils.Convert( (int)typeIndex, overflow, unsigned, primVal.Obj, (int)valTypeIndex, out res ); if (!success) { exc = res as Exception; } else { push(new StructValue(res as ValueType)); } }
/* Performs unary operation on the value on top of the stack * (corresponds to CILPE.CFG.UnaryOp class) */ public void Perform_UnaryOp(UnaryOp.ArithOp op) { Value val = Pop(); ValueType res = null; if (val is StructValue && (val as StructValue).IsPrimitive) { StructValue strVal = val as StructValue; ValueType obj = strVal.Obj; StructValue.TypeIndex typeIndex = strVal.getTypeIndex(); if (!DataModelUtils.Unary((int)op, obj, (int)typeIndex, out res)) { throw new InvalidOperandException(); } } else { throw new InvalidOperandException(); } push(new StructValue(res)); }
public override Value ToStack() { Value result = null; if (isPrimitive) { object res = null; DataModelUtils.ToStack(obj, (int)getTypeIndex(), out res); result = new StructValue(res as ValueType); } else { if (obj.GetType().IsEnum) { result = new StructValue(getEnumValue(obj)); } else { result = MakeCopy(); } } return(result); }
public int GetHashCode(object obj) { return(DataModelUtils.GetObjectHashCode(obj)); }
public override int GetHashCode() { return(DataModelUtils.GetObjectHashCode(val1) ^ DataModelUtils.GetObjectHashCode(val2)); }
/* Performs binary operation on two values on top of the stack * (corresponds to CILPE.CFG.BinaryOp class) */ public void Perform_BinaryOp(BinaryOp.ArithOp op, bool overflow, bool unsigned, out Exception exc) { exc = null; /* Determining category of binary operation */ OpCategory category = getCategory(op, overflow, unsigned); if (category == OpCategory.InvalidOp) { throw new InvalidBinaryOpException(); } /* Getting operands from stack */ Value val1, val2; val2 = Pop(); val1 = Pop(); /* Performing binary operation */ object res = null; if (val1 is StructValue && val2 is StructValue) { StructValue.TypeIndex typeA = (val1 as StructValue).getTypeIndex(), typeB = (val2 as StructValue).getTypeIndex(); if (typeA == StructValue.TypeIndex.INVALID || typeB == StructValue.TypeIndex.INVALID) { throw new InvalidOperandException(); } ValueType a = (val1 as StructValue).Obj, b = (val2 as StructValue).Obj; int operandsKind = 0; bool success = true; if (category == OpCategory.ShiftOp) /* shl, shr, shr_un */ { if (typeA == StructValue.TypeIndex.FLOAT64 || typeB == StructValue.TypeIndex.FLOAT64 || typeB == StructValue.TypeIndex.INT64) { throw new InvalidOperandException(); } operandsKind = ((typeB == StructValue.TypeIndex.INT32) ? 0 : 3) + (int)typeA; DataModelUtils.ShiftOp((int)op, unsigned, a, b, operandsKind, out res); } else { if (typeA == typeB) { operandsKind = (int)typeA; } else if (typeA == StructValue.TypeIndex.INT32 && typeB == StructValue.TypeIndex.NATIVEINT) { operandsKind = 4; } else if (typeA == StructValue.TypeIndex.NATIVEINT && typeB == StructValue.TypeIndex.INT32) { operandsKind = 5; } else { throw new InvalidOperandException(); } switch (category) { case OpCategory.NumericOp: /* add, div, mul, rem, sub */ success = DataModelUtils.NumericOp((int)op, a, b, operandsKind, out res); break; case OpCategory.ComparisonOp: /* ceq, cgt, cgt.un, clt, clt.un */ DataModelUtils.ComparisonOp((int)op, unsigned, a, b, operandsKind, out res); break; case OpCategory.IntegerOp: /* and, div.un, or, rem.un, xor */ case OpCategory.OverflowOp: /* add.ovf, add.ovf.un, mul.ovf, mul.ovf.un, sub.ovf, sub.ovf.un */ if (operandsKind == 3) { throw new InvalidOperandException(); } success = DataModelUtils.IntOvfOp((int)op, unsigned, a, b, operandsKind, out res); break; } } if (!success) { exc = res as Exception; res = null; } } else if (op == BinaryOp.ArithOp.CEQ) { if ((val1 is NullValue || val1 is ObjectReferenceValue) && (val2 is NullValue || val2 is ObjectReferenceValue) || (val1 is NullValue || val1 is PointerValue) && (val2 is NullValue || val2 is PointerValue)) { res = Equals(val1, val2) ? (Int32)1 : (Int32)0; } else { throw new InvalidOperandException(); } } else if (op == BinaryOp.ArithOp.CGT) { if (val1 is ObjectReferenceValue && val2 is NullValue && unsigned) { res = (Int32)1; } else if (val1 is NullValue && val2 is NullValue) { res = (Int32)0; } else if (val1 is PointerValue && val2 is NullValue) { res = (Int32)1; } else { throw new InvalidOperandException(); } } else if (op == BinaryOp.ArithOp.CLT) { if (val1 is NullValue && val2 is ObjectReferenceValue && unsigned) { res = (Int32)1; } else if (val1 is NullValue && val2 is NullValue) { res = (Int32)0; } else if (val1 is NullValue && val2 is PointerValue) { res = (Int32)1; } else { throw new InvalidOperandException(); } } else { throw new InvalidOperandException(); } if (res != null) { push(new StructValue(res as ValueType)); } }
public override int GetHashCode() { return(DataModelUtils.GetObjectHashCode(obj)); }
public override int GetHashCode() { return(DataModelUtils.GetObjectHashCode(arr) ^ index); }