示例#1
0
        private static void InitObj(MethodVariables variables, Stack <Expression> stack, Instruction instruction, SyntaxGraphNode node)
        {
            var addr  = Pop(stack);
            var value = new InitObjExpression((TypeReference)instruction.Operand, instruction);

            node.AddStatement(new StoreIndirectStatement(addr, value, MetadataType.Object, instruction));
        }
示例#2
0
        private static void Dup(MethodVariables variables, Stack <Expression> stack, Instruction instruction, SyntaxGraphNode node)
        {
            // Store the current value in a temporary, then load it into the stack twice
            var value = Pop(stack);
            var temp  = variables.CreateTemporary();

            node.AddStatement(new StoreTemporaryStatement(temp, value, instruction));
            stack.Push(new TemporaryExpression(temp, instruction));
            stack.Push(new TemporaryExpression(temp, instruction));
        }
示例#3
0
        public static SyntaxGraph Create(ControlFlowGraph controlFlowGraph, MethodVariables variables)
        {
            var syntaxNodes = new SortedDictionary <int, SyntaxGraphNode>();

            // Populate the nodes list
            foreach (var controlFlowNode in controlFlowGraph.Nodes)
            {
                syntaxNodes.Add(controlFlowNode.Offset, new SyntaxGraphNode(controlFlowNode.Offset));
            }

            // Now fill them
            foreach (var controlFlowNode in controlFlowGraph.Nodes)
            {
                var stack      = new Stack <Expression>();
                var syntaxNode = syntaxNodes[controlFlowNode.Offset];

                foreach (var instruction in controlFlowNode.Instructions)
                {
                    if (!_opCodeHandlers.TryGetValue(instruction.OpCode, out var handler))
                    {
                        throw new NotSupportedException($"Unsupported opcode: {instruction.OpCode}");
                    }
                    handler?.Invoke(variables, stack, instruction, syntaxNode);
                }

                // Copy the edges
                if (controlFlowNode.OutboundLinks.Any())
                {
                    var unconditional = controlFlowNode.OutboundLinks.Single(l => l.Condition == Condition.Unconditional);
                    var conditional   = controlFlowNode.OutboundLinks.SingleOrDefault(l => l.Condition == Condition.Conditional);

                    syntaxNode.AddLink(null, syntaxNodes[unconditional.Target.Offset]);

                    if (conditional != null)
                    {
                        syntaxNode.AddLink(Pop(stack), syntaxNodes[conditional.Target.Offset]);
                    }
                }

                if (stack.Count > 0)
                {
                    throw new NotImplementedException("Not yet implemented: forwarding stacks");
                }
            }

            return(new SyntaxGraph(syntaxNodes, variables));
        }
示例#4
0
        private static void StArg(MethodVariables variables, Stack <Expression> stack, Instruction instruction, SyntaxGraphNode node)
        {
            var value = Pop(stack);

            node.AddStatement(new StoreParameterStatement((ParameterReference)instruction.Operand, value, instruction));
        }
示例#5
0
        private static void Return(MethodVariables variables, Stack <Expression> stack, Instruction instruction, SyntaxGraphNode node)
        {
            var value = PopOrDefault(stack);

            node.AddStatement(new ReturnStatement(value, instruction));
        }
示例#6
0
        private static void CastClass(MethodVariables variables, Stack <Expression> stack, Instruction instruction, SyntaxGraphNode node)
        {
            var value = Pop(stack);

            stack.Push(new CastExpression(value, (TypeReference)instruction.Operand, instruction));
        }
示例#7
0
 private static void LdToken(MethodVariables variables, Stack <Expression> stack, Instruction instruction, SyntaxGraphNode node)
 {
     stack.Push(new TokenExpression((MemberReference)instruction.Operand, instruction));
 }
示例#8
0
 public static SyntaxGraph Create(MethodVariables variables, params SyntaxGraphNode[] nodes)
 => Create(variables, (IEnumerable <SyntaxGraphNode>)nodes);
示例#9
0
 private static void LdNull(MethodVariables variables, Stack <Expression> stack, Instruction instruction, SyntaxGraphNode node)
 {
     stack.Push(new ConstantExpression(null, MetadataType.Object, instruction));
 }
示例#10
0
        private static void LocAlloc(MethodVariables variables, Stack <Expression> stack, Instruction instruction, SyntaxGraphNode node)
        {
            var size = Pop(stack);

            stack.Push(new LocallocExpression(size, instruction));
        }
示例#11
0
 private static void ArgList(MethodVariables variables, Stack <Expression> stack, Instruction instruction, SyntaxGraphNode node)
 {
     stack.Push(new ArglistExpression(instruction));
 }
示例#12
0
 private static void Pop(MethodVariables variables, Stack <Expression> stack, Instruction instruction, SyntaxGraphNode node)
 {
     node.AddStatement(new DiscardStatement(Pop(stack), instruction));
 }
示例#13
0
 public static SyntaxGraph Create(MethodVariables variables, IEnumerable <SyntaxGraphNode> nodes)
 {
     return(new SyntaxGraph(
                new SortedDictionary <int, SyntaxGraphNode>(nodes.ToDictionary(n => n.Offset)),
                variables));
 }
示例#14
0
        private static void IsInst(MethodVariables variables, Stack <Expression> stack, Instruction instruction, SyntaxGraphNode node)
        {
            var obj = Pop(stack);

            stack.Push(new IsTypeExpression(obj, (TypeReference)instruction.Operand, instruction));
        }
示例#15
0
        private static void LdObj(MethodVariables variables, Stack <Expression> stack, Instruction instruction, SyntaxGraphNode node)
        {
            var addr = Pop(stack);

            stack.Push(new DereferenceExpression(addr, (TypeReference)instruction.Operand, instruction));
        }
示例#16
0
        private static void LdLen(MethodVariables variables, Stack <Expression> stack, Instruction instruction, SyntaxGraphNode node)
        {
            var array = Pop(stack);

            stack.Push(new ArrayLengthExpression(array, instruction));
        }
示例#17
0
 public SyntaxGraph(SortedDictionary <int, SyntaxGraphNode> nodes, MethodVariables variables)
 {
     _nodes     = nodes;
     _variables = variables;
 }