Esempio n. 1
0
        private static IR.MethodEntry BuildIRTree(MethodDefinition mdef)
        {
            Stack<IR.InstructionElement> stack = new Stack<IR.InstructionElement>();
            List<IR.InstructionElement> roots = new List<IR.InstructionElement>();

            int returnElements = 1;
            if (mdef.ReturnType.ReturnType.FullName == "System.Void")
                returnElements = 0;

            foreach (Mono.Cecil.Cil.Instruction x in mdef.Body.Instructions)
            {
                IR.InstructionElement[] childnodes;
                if (x.OpCode.Code == Mono.Cecil.Cil.Code.Ret)
                    childnodes = new IR.InstructionElement[returnElements];
                else
                    childnodes = new IR.InstructionElement[NumberOfElementsPoped(x.OpCode.StackBehaviourPop, x.Operand)];

                for (int i = childnodes.Length - 1; i >= 0; i--)
                {
                    System.Diagnostics.Trace.Assert(stack.Count > 0);
                    childnodes[i] = stack.Pop();
                }

                int elementsPushed = NumberOfElementsPushed(x.OpCode.StackBehaviourPush, x.Operand);

                if (elementsPushed == 0)
                {
                    if (stack.Count != 0 && x.OpCode.FlowControl != Mono.Cecil.Cil.FlowControl.Next && x.OpCode.FlowControl != Mono.Cecil.Cil.FlowControl.Call)
                        throw new InvalidProgramException();

                    roots.Add(new IR.InstructionElement(mdef, childnodes, x));
                }
                else
                {
                    IR.InstructionElement ins = new IR.InstructionElement(mdef, childnodes, x);
                    for(int i = 0; i < elementsPushed; i++)
                        stack.Push(ins);
                }
            }

            if (stack.Count != 0)
                throw new InvalidProgramException();

            IR.MethodEntry m = new IR.MethodEntry(mdef) { Childnodes = roots.ToArray() };
            m.ResetVirtualRegisters();
            return m;
        }
Esempio n. 2
0
        private static IR.MethodEntry BuildIRTree(MethodDefinition mdef)
        {
            Stack <IR.InstructionElement> stack = new Stack <IR.InstructionElement>();
            List <IR.InstructionElement>  roots = new List <IR.InstructionElement>();

            int returnElements = 1;

            if (mdef.ReturnType.ReturnType.FullName == "System.Void")
            {
                returnElements = 0;
            }

            foreach (Mono.Cecil.Cil.Instruction x in mdef.Body.Instructions)
            {
                IR.InstructionElement[] childnodes;
                if (x.OpCode.Code == Mono.Cecil.Cil.Code.Ret)
                {
                    childnodes = new IR.InstructionElement[returnElements];
                }
                else
                {
                    childnodes = new IR.InstructionElement[NumberOfElementsPoped(x.OpCode.StackBehaviourPop, x.Operand)];
                }

                for (int i = childnodes.Length - 1; i >= 0; i--)
                {
                    System.Diagnostics.Trace.Assert(stack.Count > 0);
                    childnodes[i] = stack.Pop();
                }

                int elementsPushed = NumberOfElementsPushed(x.OpCode.StackBehaviourPush, x.Operand);

                if (elementsPushed == 0)
                {
                    if (stack.Count != 0 && x.OpCode.FlowControl != Mono.Cecil.Cil.FlowControl.Next && x.OpCode.FlowControl != Mono.Cecil.Cil.FlowControl.Call)
                    {
                        throw new InvalidProgramException();
                    }

                    roots.Add(new IR.InstructionElement(mdef, childnodes, x));
                }
                else
                {
                    IR.InstructionElement ins = new IR.InstructionElement(mdef, childnodes, x);
                    for (int i = 0; i < elementsPushed; i++)
                    {
                        stack.Push(ins);
                    }
                }
            }

            if (stack.Count != 0)
            {
                throw new InvalidProgramException();
            }

            IR.MethodEntry m = new IR.MethodEntry(mdef)
            {
                Childnodes = roots.ToArray()
            };
            m.ResetVirtualRegisters();
            return(m);
        }