Beispiel #1
0
        private static bool TryReplaceUsesInFlow(
            BlockFlow flow,
            IReadOnlyDictionary <ValueTag, ValueTag> replacementMap,
            out BlockFlow newFlow)
        {
            bool replacedAny     = false;
            var  instructions    = flow.Instructions;
            var  newInstructions = new Instruction[instructions.Count];

            for (int i = 0; i < newInstructions.Length; i++)
            {
                Instruction newInsn;
                if (TryReplaceUsesInInstruction(instructions[i], replacementMap, out newInsn))
                {
                    newInstructions[i] = newInsn;
                    replacedAny        = true;
                }
                else
                {
                    newInstructions[i] = instructions[i];
                }
            }

            var branches    = flow.Branches;
            var newBranches = new Branch[branches.Count];

            for (int i = 0; i < newBranches.Length; i++)
            {
                var branch  = branches[i];
                var newArgs = new BranchArgument[branch.Arguments.Count];
                for (int j = 0; j < newArgs.Length; j++)
                {
                    var      arg = branch.Arguments[j];
                    ValueTag value;
                    if (arg.IsValue &&
                        replacementMap.TryGetValue(arg.ValueOrNull, out value) &&
                        value != arg.ValueOrNull)
                    {
                        newArgs[j]  = BranchArgument.FromValue(value);
                        replacedAny = true;
                    }
                    else
                    {
                        newArgs[j] = arg;
                    }
                }
                newBranches[i] = branch.WithArguments(newArgs);
            }

            if (replacedAny)
            {
                newFlow = flow.WithBranches(newBranches).WithInstructions(newInstructions);
            }
            else
            {
                newFlow = flow;
            }
            return(replacedAny);
        }
Beispiel #2
0
 /// <summary>
 /// Creates a new branch by applying a mapping to every value in
 /// this branch's argument list.
 /// </summary>
 /// <param name="mapping">
 /// The mapping to apply to every value in this branch's
 /// argument list.
 /// </param>
 /// <returns>A new branch.</returns>
 public Branch MapArguments(Func <ValueTag, ValueTag> mapping)
 {
     return(MapArguments(
                arg =>
                arg.IsValue
             ? BranchArgument.FromValue(mapping(arg.ValueOrNull))
             : arg));
 }
Beispiel #3
0
        /// <summary>
        /// Creates a branch that is the result of appending
        /// an argument at the end of this branch's argument list.
        /// </summary>
        /// <param name="argument">The argument to add.</param>
        /// <returns>A new branch.</returns>
        public Branch AddArgument(BranchArgument argument)
        {
            var oldArgCount = Arguments.Count;
            var newArgs     = new BranchArgument[oldArgCount + 1];

            for (int i = 0; i < oldArgCount; i++)
            {
                newArgs[i] = Arguments[i];
            }
            newArgs[oldArgCount] = argument;
            return(WithArguments(newArgs));
        }
Beispiel #4
0
 /// <summary>
 /// Creates a branch that is the result of appending
 /// an argument at the end of this branch's argument list.
 /// </summary>
 /// <param name="argument">The argument to add.</param>
 /// <returns>A new branch.</returns>
 public Branch AddArgument(ValueTag argument)
 {
     return(AddArgument(BranchArgument.FromValue(argument)));
 }