Пример #1
0
        public static string CreateDefaultValuesStaticVariablesProcedure()
        {
            #region Create body of the procedure
            StatementList body = new StatementList();

            foreach (IFieldReference field in FieldTranslator.GetFieldReferences())
            {
                if (field.IsStatic)
                {
                    BoogieGenerator   bg = BoogieGenerator.Instance();
                    StaticFieldAccess staticFieldAccess = new StaticFieldAccess(field);

                    body.Add(bg.WriteStaticField(staticFieldAccess, GetDefaultConstant(Helpers.GetBoogieType(field.Type))));
                }
            }
            #endregion

            string        procedureName       = "$default_values_static_fields";
            string        attributes          = String.Empty;
            StatementList localVariables      = new StatementList();
            String        parametersWithTypes = String.Empty;
            String        returnTypeIfAny     = String.Empty;

            BoogieProcedureTemplate temp = new BoogieProcedureTemplate(procedureName, attributes, localVariables, body, parametersWithTypes, returnTypeIfAny, false);
            return(temp.TransformText());
        }
Пример #2
0
        public StatementList  ReadStaticField(StaticFieldAccess staticFieldAccess, IVariable value)
        {
            StatementList stmts = new StatementList();

            var address = dispatcher.AddressOf(staticFieldAccess);

            stmts.Add(bg.VariableAssignment(value, dispatcher.ReadAddr(address)));

            return(stmts);
        }
Пример #3
0
 public override Addressable AddressOf(StaticFieldAccess staticFieldAccess)
 {
     if (RequiresAllocation(staticFieldAccess) || staticFieldAccess.Type is IManagedPointerType)
     {
         return(memAddr.AddressOf(staticFieldAccess));
     }
     else
     {
         return(memBCT.AddressOf(staticFieldAccess));
     }
 }
Пример #4
0
        // some field initialization can be missing in constructors
        // example:

        /*
         * class Foo{
         *      int x; // this initialization is not added by the compiler
         *      int y = y;
         *  }
         */

        // therefore we are adding the initialization for each field at the top of every constructor.

        public void Transform()
        {
            // these variables hold the default value
            IDictionary <Helpers.BoogieType, LocalVariable> boogieTypeToLocalVariable =
                new Dictionary <Helpers.BoogieType, LocalVariable>();

            // they are the same stored in boogieTypeToLocalVariable
            IList <LocalVariable> variables
                = new List <LocalVariable>();

            // assignment of constants
            IList <Instruction> instructions
                = new List <Instruction>();

            CreateLocalVariablesWithDefaultValues(boogieTypeToLocalVariable, variables, instructions);

            // we need to initialize local variables.

            foreach (var lv in methodBody.Variables)
            {
                if (lv.IsParameter ||
                    // in the delegate handling this type of variables are not used
                    // calling get boogie type will crash
                    lv.Type is Microsoft.Cci.Immutable.FunctionPointerType)
                {
                    continue;
                }

                var       varBoogieType    = Helpers.GetBoogieType(lv);
                IVariable initialValue     = boogieTypeToLocalVariable[varBoogieType];
                var       storeInstruction = new LoadInstruction(0, lv, initialValue);
                storeInstruction.Label = String.Empty;
                instructions.Add(storeInstruction);
            }


            var fields = methodBody.MethodDefinition.ContainingTypeDefinition.Fields;

            if (methodBody.MethodDefinition.IsStaticConstructor)
            {
                foreach (IFieldDefinition field in fields.Where(f => f.IsStatic))
                {
                    var       fieldBoogieType  = Helpers.GetBoogieType(field);
                    IVariable initialValue     = boogieTypeToLocalVariable[fieldBoogieType];
                    var       staticAccess     = new StaticFieldAccess(field);
                    var       storeInstruction = new StoreInstruction(0, staticAccess, initialValue);
                    storeInstruction.Label = String.Empty;
                    instructions.Add(storeInstruction);
                }
            }
            else if (methodBody.MethodDefinition.IsConstructor)
            {
                var thisVariable = methodBody.Parameters[0];

                foreach (IFieldDefinition field in fields.Where(f => !f.IsStatic))
                {
                    var       fieldBoogieType  = Helpers.GetBoogieType(field);
                    IVariable initialValue     = boogieTypeToLocalVariable[fieldBoogieType];
                    var       instanceAccess   = new InstanceFieldAccess(thisVariable, field);
                    var       storeInstruction = new StoreInstruction(0, instanceAccess, initialValue);
                    storeInstruction.Label = String.Empty;
                    instructions.Add(storeInstruction);
                }
            }

            methodBody.Variables.UnionWith(variables);
            int idx = 0;

            foreach (var i in instructions)
            {
                methodBody.Instructions.Insert(idx, i);
                idx++;
            }
        }
Пример #5
0
 public override Addressable AddressOf(StaticFieldAccess staticFieldAccess)
 {
     return(new StaticField(staticFieldAccess));
 }
Пример #6
0
        public override Addressable AddressOf(StaticFieldAccess staticFieldAccess)
        {
            var address = new AddressExpression(staticFieldAccess.Field.Type, BoogieVariable.From(new StaticField(staticFieldAccess)));

            return(address);
        }
Пример #7
0
 public StatementList WriteStaticField(StaticFieldAccess staticFieldAccess, IVariable value)
 {
     return(dispatcher.WriteStaticField(staticFieldAccess, dispatcher.ReadAddr(value)));
 }
Пример #8
0
 public StatementList WriteStaticField(StaticFieldAccess staticFieldAccess, Expression expr)
 {
     return(dispatcher.WriteAddr(dispatcher.AddressOf(staticFieldAccess), expr));
 }
Пример #9
0
 public abstract Addressable AddressOf(StaticFieldAccess staticFieldAccess);
Пример #10
0
        public void VisitMethod(MethodBody mBody, ControlFlowGraph cfg)
        {
            VisitLocals(mBody);

            // Going through the instructions via cfg nodes instead of directly iterating over the instructions
            // of the methodBody becuase Phi instructions may not have been inserted in the insts of the methodBody.
            foreach (var node in cfg.Nodes)
            {
                foreach (var instruction in node.Instructions)
                {
                    // System.Console.WriteLine("{0}", instruction.ToString());
                    // System.Console.WriteLine("{0}", instruction.GetType().FullName());
                    // System.Console.WriteLine();

                    if (instruction is LoadInstruction)
                    {
                        LoadInstruction lInst      = instruction as LoadInstruction;
                        IValue          rhsOperand = lInst.Operand;
                        if (rhsOperand is StaticFieldAccess)
                        {
                            StaticFieldAccess rhsAcc  = rhsOperand as StaticFieldAccess;
                            IFieldReference   fld     = rhsAcc.Field;
                            ITypeDefinition   fldType = fld.ContainingType.ResolvedType;
                            Stubber.CheckAndAdd(fldType);
                        }
                        // Note: calls to static methods and instance methods appear as a StaticMethodReference
                        else if (rhsOperand is StaticMethodReference)
                        {
                            StaticMethodReference sMethAddr    = rhsOperand as StaticMethodReference;
                            IMethodDefinition     tgtMeth      = sMethAddr.Method.ResolvedMethod;
                            ITypeDefinition       containingTy = tgtMeth.ContainingTypeDefinition;
                            Stubber.CheckAndAdd(containingTy);
                            IMethodDefinition addedMeth = Stubber.CheckAndAdd(tgtMeth);
                            // addrTakenMethods do not contain templates.
                            if (addedMeth != null)
                            {
                                addrTakenMethods.Add(addedMeth);
                            }
                        }
                        //Note: calls to virtual, abstract or interface methods appear as VirtualMethodReference
                        else if (rhsOperand is VirtualMethodReference)
                        {
                            VirtualMethodReference sMethAddr    = rhsOperand as VirtualMethodReference;
                            IMethodDefinition      tgtMeth      = sMethAddr.Method.ResolvedMethod;
                            ITypeDefinition        containingTy = tgtMeth.ContainingTypeDefinition;
                            ITypeDefinition        addedTy      = Stubber.CheckAndAdd(containingTy);
                            IMethodDefinition      addedMeth    = Stubber.CheckAndAdd(tgtMeth);
                            if (addedTy != null && addedMeth != null)
                            {
                                // addrTakenMethods do not contain templates.
                                addrTakenMethods.Add(addedMeth);
                                ProcessVirtualInvoke(addedMeth, addedTy, true);
                            }
                        }
                        else if (rhsOperand is Reference)
                        {
                            Reference      rhsRef = rhsOperand as Reference;
                            IReferenceable refOf  = rhsRef.Value;
                            if (refOf is StaticFieldAccess)
                            {
                                StaticFieldAccess refAcc  = refOf as StaticFieldAccess;
                                IFieldDefinition  fld     = refAcc.Field.ResolvedField;
                                ITypeDefinition   fldType = fld.ContainingType.ResolvedType;
                                Stubber.CheckAndAdd(fldType);
                                addrTakenStatFlds.Add(fld);
                            }
                            else if (refOf is IVariable)
                            {
                                IVariable refVar = refOf as IVariable;
                                if (!refVar.Type.IsValueType || refVar.Type.ResolvedType.IsStruct)
                                {
                                    addrTakenLocals.Add(refVar);
                                }
                            }
                            else if (refOf is InstanceFieldAccess)
                            {
                                InstanceFieldAccess refAcc = refOf as InstanceFieldAccess;
                                IFieldDefinition    fld    = refAcc.Field.ResolvedField;
                                addrTakenInstFlds.Add(fld);
                            }
                            else if (refOf is ArrayElementAccess)
                            {
                                // All arrays will be added into domX as potential address taken.
                            }
                        }
                    }
                    else if (instruction is StoreInstruction)
                    {
                        StoreInstruction sInst = instruction as StoreInstruction;
                        IAssignableValue lhs   = sInst.Result;
                        if (lhs is StaticFieldAccess)
                        {
                            StaticFieldAccess lhsAcc  = lhs as StaticFieldAccess;
                            IFieldReference   fld     = lhsAcc.Field;
                            ITypeDefinition   fldType = fld.ContainingType.ResolvedType;
                            Stubber.CheckAndAdd(fldType);
                        }
                    }
                    else if (instruction is CreateObjectInstruction)
                    {
                        CreateObjectInstruction newObjInst = instruction as CreateObjectInstruction;
                        ITypeReference          objType    = newObjInst.AllocationType;
                        ITypeDefinition         objTypeDef = objType.ResolvedType;
                        if (objTypeDef is IGenericTypeInstance)
                        {
                            objTypeDef = objTypeDef.ResolvedType;
                        }
                        ITypeDefinition addedTy = Stubber.CheckAndAdd(objTypeDef);
                        if (addedTy != null && !allocClasses.Contains(addedTy))
                        {
                            allocClasses.Add(addedTy);
                        }
                    }
                    else if (instruction is CreateArrayInstruction)
                    {
                        CreateArrayInstruction newArrInst  = instruction as CreateArrayInstruction;
                        ITypeReference         elemType    = newArrInst.ElementType;
                        ITypeDefinition        elemTypeDef = elemType.ResolvedType;
                        ITypeDefinition        addedTy     = Stubber.CheckAndAdd(elemTypeDef);
                        if (addedTy != null && !allocClasses.Contains(addedTy))
                        {
                            allocClasses.Add(addedTy);
                        }
                    }
                    else if (instruction is MethodCallInstruction)
                    {
                        MethodCallInstruction invkInst       = instruction as MethodCallInstruction;
                        IMethodReference      callTgt        = invkInst.Method;
                        ITypeReference        containingType = callTgt.ContainingType;
                        ITypeDefinition       declType       = containingType.ResolvedType;
                        IMethodDefinition     callTgtDef     = callTgt.ResolvedMethod;
                        ITypeDefinition       addedType      = Stubber.CheckAndAdd(declType);
                        IMethodDefinition     addedMeth      = Stubber.CheckAndAdd(callTgtDef);
                        MethodCallOperation   callType       = invkInst.Operation;
                        if (callType == MethodCallOperation.Virtual && addedType != null && addedMeth != null)
                        {
                            ProcessVirtualInvoke(addedMeth, addedType, false);
                        }
                    }
                    else
                    {
                        // System.Console.WriteLine("{0}", instruction.ToString());
                        // System.Console.WriteLine("Not currently handled: {0}", instruction.GetType().ToString());
                        // System.Console.WriteLine();
                    }
                }
            }
        }