/// <summary> /// Returns the operand name at the source level. /// </summary> /// /// <param name="operand">Operand whose name needs to be returned.</param> /// <returns>Operand name at the source level.</returns> public static string GetOperandName(Phx.IR.Operand operand) { Trace.Assert(operand.Symbol != null, "PHOENIX: The operand should have an associated symbol."); string operandString = operand.Symbol.NameString; if (operandString.StartsWith("_")) { operandString = operandString.Substring(1); } else if (operandString.StartsWith("&_")) { operandString = operandString.Substring(2); } else if (operandString.StartsWith("&")) { operandString = operandString.Substring(1); } return(Phx.Utility.Undecorate(operandString, false)); }
/// <summary> /// Returns a variable Expression with the input value, which represents the variable /// corresponding to the input operand. /// </summary> /// /// <param name="operand">Operand for the variable Expression.</param> /// <param name="value">Value of the variable Expression.</param> /// <param name="path">Path that contains the operand.</param> /// <returns>Variable Expression with value <paramref name="value"/>, /// which represents the operand <paramref name="operand"/>.</returns> public static Expression MakeVariableExpression(Phx.IR.Operand operand, string value, Path path) { uint nativeWordBitSize = path.Config.WORD_BITSIZE; Expression result = null; /* Find the type of this variable, if the operand is not an address, or * the type of the variable the operand refers to, otherwise. */ Phx.Types.Type variableType = operand.IsAddress ? operand.Type.AsPointerType.ReferentType : operand.Type; /* Add an identifier to the names of aggregate objects. */ value = variableType.IsAggregateType ? path.Config.IDENT_AGGREGATE + value : value; result = ExpressionHelper.IsPointerExpressionType(variableType) ? new Expression(OperatorStore.ArrayVariableOp, value, nativeWordBitSize) : new Expression(OperatorStore.VariableOp, value, operand.IsAggregate ? nativeWordBitSize : operand.BitSize); result.Type = variableType; /* Replace a pointer Expression with the corresponding "dereferencing function". */ result = ExpressionHelper.IsPointerExpression(result) ? ExpressionHelper.MakeDereferencingFunction(result, path) : result; /* If the operand uses the address of an existing variable, determine if there * is already a temporary pointer that points to this "address-taken" variable. * If not, make a new temporary pointer that points to this variable. */ if (operand.IsAddress) { if (path.AddressTaken.ContainsKey(result)) { result = path.AddressTaken[result].Clone(); } else { Phx.Types.Type referentType = ExpressionHelper.GetPointerReferentType(operand.Type); /* Determine the basic block that contains the operand and the associated * information. */ BasicBlock operandBasicBlock = operand.Instruction.BasicBlock; BasicBlockAddendum operandBasicBlockAddendum = operandBasicBlock.FindExtensionObject(typeof(BasicBlockAddendum)) as BasicBlockAddendum; /* Generate a new temporary pointer and log the relationship between * the temporary pointer and the "address-taken" variable. */ Expression newTempPtr = path.GetNewTemporaryPointer(operand.Type); path.AddressTaken[result] = newTempPtr; /* Generate and log the assignment between the dereferenced temporary pointer * and the "address-taken" variable. This way, the generated SMT queries will * never have to use the Address-Of operator. */ Expression dereferencedNewTempPtr = ExpressionHelper.DereferencePointer(newTempPtr, operand.Type, false, path); List <Expression> assignExprs = path.GenerateAndLogAssignment(dereferencedNewTempPtr, result, operandBasicBlock); /* Add the conditional Expressions that correspond to this new assignment. */ foreach (Expression assignExpr in assignExprs) { path.AddCondition(assignExpr, operandBasicBlock.Id); } result = newTempPtr; } } /* If the variable is an "address-taken" variable, which means that its address * has been taken before, then we replace the variable with a dereference of * the temporary pointer that refers to the variable. */ if (path.AddressTaken.ContainsKey(result)) { result = ExpressionHelper.DereferencePointer(path.AddressTaken[result], operand.Type, false, path); } path.AddVariable(result); return(result); }