From 3d1e3e6ca9815986b70bfc2588c36f3473b83a93 Mon Sep 17 00:00:00 2001 From: de4dot Date: Wed, 9 May 2012 18:25:43 +0200 Subject: [PATCH] Add method to get instrs pushing args --- blocks/DotNetUtils.cs | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/blocks/DotNetUtils.cs b/blocks/DotNetUtils.cs index 97355e48..4eadfe64 100644 --- a/blocks/DotNetUtils.cs +++ b/blocks/DotNetUtils.cs @@ -1208,5 +1208,50 @@ namespace de4dot.blocks { return false; } + + public static IList getArgPushes(IList instrs, int index) { + return getArgPushes(instrs, ref index); + } + + public static IList getArgPushes(IList instrs, ref int index) { + if (index < 0 || index >= instrs.Count) + return null; + var startInstr = instrs[index]; + int pushes, pops; + calculateStackUsage(startInstr, false, out pushes, out pops); + + index--; + int numArgs = pops; + var args = new List(numArgs); + int stackSize = numArgs; + while (index >= 0 && args.Count != numArgs) { + var instr = instrs[index--]; + calculateStackUsage(instr, false, out pushes, out pops); + if (instr.OpCode.Code == Code.Dup) { + args.Add(instr); + stackSize--; + } + else { + if (pushes == 1) + args.Add(instr); + else if (pushes > 1) + throw new NotImplementedException(); + stackSize -= pushes; + + if (pops != 0) { + index++; + if (getArgPushes(instrs, ref index) == null) + return null; + } + } + + if (stackSize < 0) + return null; + } + if (args.Count != numArgs) + return null; + args.Reverse(); + return args; + } } }