Port more code

This commit is contained in:
de4dot 2012-10-31 17:43:51 +01:00
parent 04e1568c61
commit 301a2fab97
9 changed files with 49 additions and 49 deletions

View File

@ -19,7 +19,7 @@
using System;
using System.Collections.Generic;
using Mono.Cecil.Cil;
using dot10.DotNet.Emit;
namespace de4dot.blocks {
public class Block : BaseBlock {
@ -269,7 +269,7 @@ namespace de4dot.blocks {
for (int i = 0; i < instrs.Count; i++) {
var instr = instrs[i];
if (instr.OpCode != OpCodes.Nop)
dest.Add(clone ? new Instr(DotNetUtils.clone(instr.Instruction)) : instr);
dest.Add(clone ? new Instr(instr.Instruction.Clone()) : instr);
}
}

View File

@ -19,35 +19,35 @@
using System;
using System.Collections.Generic;
using Mono.Cecil;
using Mono.Cecil.Cil;
using dot10.DotNet;
using dot10.DotNet.Emit;
namespace de4dot.blocks {
public class Blocks {
MethodDefinition method;
IList<VariableDefinition> locals;
MethodDef method;
IList<Local> locals;
MethodBlocks methodBlocks;
public MethodBlocks MethodBlocks {
get { return methodBlocks; }
}
public IList<VariableDefinition> Locals {
public IList<Local> Locals {
get { return locals; }
}
public MethodDefinition Method {
public MethodDef Method {
get { return method; }
}
public Blocks(MethodDefinition method) {
public Blocks(MethodDef method) {
this.method = method;
updateBlocks();
}
public void updateBlocks() {
var body = method.Body;
locals = body.Variables;
var body = method.CilBody;
locals = body.LocalList;
methodBlocks = new InstructionListParser(body.Instructions, body.ExceptionHandlers).parse();
}
@ -79,11 +79,11 @@ namespace de4dot.blocks {
if (locals.Count == 0)
return 0;
var usedLocals = new Dictionary<VariableDefinition, List<LocalVariableInfo>>();
var usedLocals = new Dictionary<Local, List<LocalVariableInfo>>();
foreach (var block in methodBlocks.getAllBlocks()) {
for (int i = 0; i < block.Instructions.Count; i++) {
var instr = block.Instructions[i];
VariableDefinition local;
Local local;
switch (instr.OpCode.Code) {
case Code.Ldloc:
case Code.Ldloc_S:
@ -102,7 +102,7 @@ namespace de4dot.blocks {
case Code.Ldloca_S:
case Code.Ldloca:
local = (VariableDefinition)instr.Operand;
local = (Local)instr.Operand;
break;
default:
@ -122,7 +122,7 @@ namespace de4dot.blocks {
}
int newIndex = -1;
var newLocals = new List<VariableDefinition>(usedLocals.Count);
var newLocals = new List<Local>(usedLocals.Count);
foreach (var local in usedLocals.Keys) {
newIndex++;
newLocals.Add(local);
@ -137,7 +137,7 @@ namespace de4dot.blocks {
return numRemoved;
}
static Instruction optimizeLocalInstr(Instr instr, VariableDefinition local, uint newIndex) {
static Instruction optimizeLocalInstr(Instr instr, Local local, uint newIndex) {
switch (instr.OpCode.Code) {
case Code.Ldloc:
case Code.Ldloc_S:
@ -194,7 +194,7 @@ namespace de4dot.blocks {
}
catch (NullReferenceException) {
//TODO: Send this message to the log
Console.WriteLine("Null ref exception! Invalid metadata token in code? Method: {0:X8}: {1}", method.MetadataToken.ToUInt32(), method.FullName);
Console.WriteLine("Null ref exception! Invalid metadata token in code? Method: {0:X8}: {1}", method.MDToken.Raw, method.FullName);
return;
}
}

View File

@ -19,8 +19,8 @@
using System;
using System.Collections.Generic;
using Mono.Cecil;
using Mono.Cecil.Cil;
using dot10.DotNet;
using dot10.DotNet.Emit;
namespace de4dot.blocks {
class CodeGenerator {
@ -45,11 +45,11 @@ namespace de4dot.blocks {
public int filterStart;
public int handlerStart;
public int handlerEnd;
public TypeReference catchType;
public ExceptionHandlerType handlerType;
public ITypeDefOrRef catchType;
public ExceptionClause handlerType;
public ExceptionInfo(int tryStart, int tryEnd, int filterStart,
int handlerStart, int handlerEnd, TypeReference catchType,
ExceptionHandlerType handlerType) {
int handlerStart, int handlerEnd, ITypeDefOrRef catchType,
ExceptionClause handlerType) {
if (tryStart > tryEnd || filterStart > handlerStart ||
tryStart < 0 || tryEnd < 0 || filterStart < 0 || handlerStart < 0 || handlerEnd < 0)
throw new ApplicationException("Invalid start/end/filter/handler indexes");
@ -99,10 +99,10 @@ namespace de4dot.blocks {
}
void recalculateInstructionOffsets(IList<Instruction> allInstructions) {
int offset = 0;
uint offset = 0;
foreach (var instr in allInstructions) {
instr.Offset = offset;
offset += instr.GetSize();
offset += (uint)instr.GetSize();
}
}
@ -137,7 +137,7 @@ namespace de4dot.blocks {
if (getShortBranch(instruction, out opcode)) {
const int instrSize = 5; // It's a long branch instruction
var target = (Instruction)instruction.Operand;
int distance = target == null ? int.MaxValue : target.Offset - (instruction.Offset + instrSize);
int distance = target == null ? int.MaxValue : (int)(target.Offset - (instruction.Offset + instrSize));
if (-0x80 <= distance && distance <= 0x7F) {
instruction.OpCode = opcode;
changed = true;

View File

@ -52,7 +52,7 @@ namespace de4dot.blocks {
int stack = stackStart;
foreach (var instr in block.Instructions)
DotNetUtils.updateStack(instr.Instruction, ref stack, false);
instr.Instruction.UpdateStack(ref stack, false);
stackEnd = stack;
}
}

View File

@ -18,8 +18,8 @@
*/
using System;
using Mono.Cecil.Cil;
using System.Collections.Generic;
using dot10.DotNet.Emit;
namespace de4dot.blocks {
public class Instr {
@ -44,8 +44,8 @@ namespace de4dot.blocks {
// Returns the variable or null if it's not a ldloc/stloc instruction. It does not return
// a local variable if it's a ldloca/ldloca.s instruction.
public static VariableDefinition getLocalVar(IList<VariableDefinition> locals, Instr instr) {
return DotNetUtils.getLocalVar(locals, instr.Instruction);
public static Local getLocalVar(IList<Local> locals, Instr instr) {
return instr.Instruction.GetLocal(locals);
}
static public bool isFallThrough(OpCode opCode) {
@ -104,23 +104,23 @@ namespace de4dot.blocks {
}
public bool isLdcI4() {
return DotNetUtils.isLdcI4(OpCode.Code);
return instruction.IsLdcI4();
}
public int getLdcI4Value() {
return DotNetUtils.getLdcI4Value(instruction);
return instruction.GetLdcI4Value();
}
public bool isLdarg() {
return DotNetUtils.isLdarg(instruction);
return instruction.IsLdarg();
}
public bool isStloc() {
return DotNetUtils.isStloc(instruction);
return instruction.IsStloc();
}
public bool isLdloc() {
return DotNetUtils.isLdloc(instruction);
return instruction.IsLdloc();
}
public bool isNop() {
@ -132,23 +132,23 @@ namespace de4dot.blocks {
}
public bool isLeave() {
return DotNetUtils.isLeave(instruction);
return instruction.IsLeave();
}
public bool isBr() {
return DotNetUtils.isBr(instruction);
return instruction.IsBr();
}
public bool isBrfalse() {
return DotNetUtils.isBrfalse(instruction);
return instruction.IsBrfalse();
}
public bool isBrtrue() {
return DotNetUtils.isBrtrue(instruction);
return instruction.IsBrtrue();
}
public bool isConditionalBranch() {
return DotNetUtils.isConditionalBranch(OpCode.Code);
return instruction.IsConditionalBranch();
}
public bool getFlippedBranchOpCode(out OpCode opcode) {

View File

@ -19,7 +19,7 @@
using System;
using System.Collections.Generic;
using Mono.Cecil.Cil;
using dot10.DotNet.Emit;
namespace de4dot.blocks {
class InstructionListParser {

View File

@ -19,7 +19,7 @@
using System;
using System.Collections.Generic;
using Mono.Cecil.Cil;
using dot10.DotNet.Emit;
namespace de4dot.blocks {
// A normal branch may not transfer out of a protected block (try block), filter handler,

View File

@ -17,8 +17,8 @@
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
*/
using Mono.Cecil;
using Mono.Cecil.Cil;
using dot10.DotNet;
using dot10.DotNet.Emit;
namespace de4dot.blocks {
// Contains the filter handler block and the catch handler block.
@ -27,14 +27,14 @@ namespace de4dot.blocks {
HandlerBlock handlerBlock = new HandlerBlock();
// State for an ExceptionHandler instance
TypeReference catchType;
ExceptionHandlerType handlerType;
ITypeDefOrRef catchType;
ExceptionClause handlerType;
public TypeReference CatchType {
public ITypeDefOrRef CatchType {
get { return catchType; }
}
public ExceptionHandlerType HandlerType {
public ExceptionClause HandlerType {
get { return handlerType; }
}

2
dot10

@ -1 +1 @@
Subproject commit ec48311025fd4e9a56da7881857d90eaa0e3b5ca
Subproject commit 526604c81df777accde4e45327cffcba954c356c