public Opcode(string name, object paramval) { Name = name; ParamVal = paramval; Prefix = null; IsPrefix = false; NeedsLabel = false; }
Opcode OpcodeNoParamVal(string s) { var NewOp = new Opcode(s); MakeLabel(NewOp); return NewOp; }
public Opcode(string name) { Name = name; Prefix = null; IsPrefix = false; NeedsLabel = false; }
Opcode OpcodeFourByte(string s) { // This gets the parameter (stored as b) from the byte[] byte[] b = new byte[4]; for (int k = 0; k < 4; k++) { b[k] = IL[pos]; pos++; } int i = BitConverter.ToInt32(b,0); var NewOp = new Opcode(s,i); MakeLabel(NewOp); return NewOp; }
Opcode OpcodeIndx(string s) { // This gets the parameter (stored as b) from the byte[] byte[] b = new byte[2]; for (int k = 0; k < 2; k++) { b[k] = IL[pos]; pos++; } short i = BitConverter.ToInt16(b,0); var ind = new Index("V_" + i); var NewOp = new Opcode(s,i); MakeLabel(NewOp); return NewOp; }
void MakeLabel(Opcode op) { op.Label = string.Format("IL_{0:x4}", pos1); }
Opcode OpcodeEightByte(string s) { // This gets the parameter (stored as b) from the byte[] byte[] b = new byte[8]; for (int k = 0; k < 8; k++) { b[k] = IL[pos]; pos++; } long i = BitConverter.ToInt64(b,0); var NewOp = new Opcode(s,i); MakeLabel(NewOp); return NewOp; }
Opcode OpcodeTok(string s) { // This gets the parameter (stored as b) from the byte[] byte[] b = new byte[4]; for (int k = 0; k < 4; k++) { b[k] = IL[pos]; pos++; } // This calls the appropriate Opcode method depending on what sort of parameter we're dealing with. // This is determined by the least byte of the byte[] if (b[3] == 4) { pos -=4; return OpcodeFld(s); } if ((b[3] == 6)||(b[3] == 10)) { pos -=4; return OpcodeMeth(s); } if ((b[3] == 1)||(b[3]==2) || (b[3]==0x1b)) //0x1b is testspec. { pos -=4; return OpcodeType(s); } Opcode NewOp = new Opcode(s,b); MakeLabel(NewOp); return NewOp; }
Opcode OpcodeTwoByte(string s) { // This gets the parameter (stored as b) from the byte[] byte[] b = new byte[2]; for (int k = 0; k < 2; k++) { b[k] = IL[pos]; pos++; } ushort i = BitConverter.ToUInt16(b,0); var NewOp = new Opcode(s,i); MakeLabel(NewOp); // This labels the Opcode return NewOp; }
Opcode OpcodeTarget(string s) { // This gets the parameter (stored as b) from the byte[] byte[] b = new byte[4]; for (int k = 0; k < 4; k++) { b[k] = IL[pos]; pos++; } int l = BitConverter.ToInt32(b,0); int TPos = l + pos; string s1 =string.Format("IL_{0:x4}", TPos); var NewOp = new Opcode(s,new Target(s1)); MakeLabel(NewOp); return NewOp; }
Opcode OpcodeTargetS(string s) { // This gets the parameter (stored as b) from the byte[] sbyte k = (sbyte)IL[pos]; pos++; int b = (int)((sbyte)k + (int)pos); string s1 =string.Format("IL_{0:x4}", b); var NewOp = new Opcode(s,new Target(s1)); MakeLabel(NewOp); return NewOp; }
Opcode OpcodeSwitch(string s) { // This gets the number of parameters (stored as b) from the byte[] // Each parameter is then added to the ArrayList T var T = new ArrayList(); byte[] b = new byte[4]; for (int k = 0; k < 4; k++) { b[k] = IL[pos]; pos++; } int i = BitConverter.ToInt32(b,0); // This tells us how many parameters to expect. for (int k = 0; k < i; k++) // Each parameter will be a four-byte offset { for (int k1 = 0; k1 < 4; k1++) { b[k1] = IL[pos]; pos++; } int j = BitConverter.ToInt32(b,0); string s1 =string.Format("IL_{0:x4}", pos1 + j + 4*i + 5); T.Add(new Target(s1)); } var NewOp = new Opcode(s,T); MakeLabel(NewOp); return NewOp; }
Opcode OpcodeSig(string s) { // TODO : Resolve this token! // This gets the parameter (stored as b) from the byte[] byte[] b = new byte[4]; for (int k = 0; k < 4; k++) { b[k] = IL[pos]; pos++; } var NewOp = new Opcode(s,b); MakeLabel(NewOp); return NewOp; }
Opcode OpcodeOneByte(string s) { // This gets the parameter (stored as b) from the byte[] byte b = IL[pos]; pos++; var NewOp = new Opcode(s,b); MakeLabel(NewOp); return NewOp; }
public Opcode(string name, ArrayList paramlist) { Name = name; ParamVal = paramlist; Prefix = null; IsPrefix = false; NeedsLabel = false; }
Opcode OpcodeType(string s) { // This gets the parameter (stored as b) from the byte[] byte[] b = new byte[4]; for (int k = 0; k < 4; k++) { b[k] = IL[pos]; pos++; } Type RetType = (Type)curModule.ResolveMember(BitConverter.ToInt32(b,0)); var NewOp = new Opcode(s,RetType); MakeLabel(NewOp); return NewOp; }
public Label ToLabel(ArrayList Opcodes) { Opcode Op1 = new Opcode(); foreach(Opcode OP in Opcodes) { if (Name == OP.Label) return OP.ILLabel; } return Op1.ILLabel; }
Opcode Prefix(string s) { var OpPre = new Opcode(s); MakeLabel(OpPre); // This labels the Opcode OpPre.IsPrefix = true; // Here we designate the Opcode as a prefix Opcode Next = GetOpcode(); Next.Label = ""; Next.Prefix = OpPre; return Next; }
void MakeLabel(Opcode op, bool label) { if (!label) op.Label = "IL_XXXX"; else MakeLabel(op); }
Opcode PrefixParamVal(string s) { // This gets the parameter (stored as b) from the byte[] byte b = IL[pos]; pos++; var OpPre = new Opcode(s,b); MakeLabel(OpPre); // This labels the Opcode OpPre.IsPrefix = true; // Here we designate the Opcode as a prefix Opcode Next = GetOpcode(); Next.Label = ""; Next.Prefix = OpPre; return Next; }
Opcode OpcodeFld(string s) { // This gets the parameter (stored as b) from the byte[] byte[] b = new byte[4]; for (int k = 0; k < 4; k++) { b[k] = IL[pos]; pos++; } // We have to temporarily take out stfld field here. // the stfld's order must be fixed up FieldInfo ReturnField = (FieldInfo)curModule.ResolveMember(BitConverter.ToInt32(b,0)); var NewOp = new Opcode(s,ReturnField); if (s.Equals("stfld")) { StfldList.Add(NewOp.ParamVal); return NewOp; } else { MakeLabel(NewOp); return NewOp; } }
public ILReader(MethodBase InputMethod, FieldInfo[] FieldsInModule, MethodBase[] MethodsInModule, Assembly currentAssm) { workingAssm = currentAssm; MethodOpcodes = new ArrayList(); InputMeth = InputMethod; ExceptionHandlers = new ArrayList(); if (InputMethod == null) { Console.WriteLine("Error, input method is not specified, can not continue"); return; } methodBody = InputMethod.GetMethodBody(); curModule = InputMethod.DeclaringType.Module; pos = 0; pos1 = 0; IL = methodBody.GetILAsByteArray(); IList<ExceptionHandlingClause> ehClauses = methodBody.ExceptionHandlingClauses; foreach( ExceptionHandlingClause ehclause in ehClauses ) { ExceptionHandlers.Add(new ExceptionInstruction(ehclause.TryOffset,ExceptionHandler.Try,null)); switch (ehclause.Flags) { //case 0: case ExceptionHandlingClauseOptions.Clause: ExceptionHandlers.Add(new ExceptionInstruction(ehclause.HandlerOffset,ExceptionHandler.Catch,ehclause.CatchType)); ExceptionHandlers.Add(new ExceptionInstruction(ehclause.HandlerOffset+ehclause.HandlerLength,ExceptionHandler.EndException,null)); break; //case 1: case ExceptionHandlingClauseOptions.Filter: ExceptionHandlers.Add(new ExceptionInstruction(ehclause.FilterOffset,ExceptionHandler.Filter,null)); ExceptionHandlers.Add(new ExceptionInstruction(ehclause.HandlerOffset,ExceptionHandler.EndFilter,null)); ExceptionHandlers.Add(new ExceptionInstruction(ehclause.HandlerOffset+ehclause.HandlerLength,ExceptionHandler.EndException,null)); break; //case 2: case ExceptionHandlingClauseOptions.Finally: ExceptionHandlers.Add(new ExceptionInstruction(ehclause.HandlerOffset,ExceptionHandler.Finally,null)); ExceptionHandlers.Add(new ExceptionInstruction(ehclause.HandlerOffset+ehclause.HandlerLength,ExceptionHandler.EndException,null)); break; //case 4: case ExceptionHandlingClauseOptions.Fault: ExceptionHandlers.Add(new ExceptionInstruction(ehclause.HandlerOffset,ExceptionHandler.Fault,null)); ExceptionHandlers.Add(new ExceptionInstruction(ehclause.HandlerOffset+ehclause.HandlerLength,ExceptionHandler.EndException,null)); break; } } // populate opcode Opcode Op = GetOpcode(); while (Op.Name != "") // This cycles through all the Opcodes { if (Op.Name != "stfld") MethodOpcodes.Add(Op); Op = GetOpcode(); } // sort the stfld opcodes and output // this will make the opcode invalid for run. // but out purpose is simply compare the assemblies. StfldList.Sort(new StringCompare()); foreach (object i in StfldList) { Opcode temp = new Opcode("stfld", i); MakeLabel(temp, false); MethodOpcodes.Add(temp); } }
Opcode OpcodeFourByteF(string s) { // This gets the parameter (stored as b) from the byte[] byte[] b = new byte[8]; for (int k = 0; k < 4; k++) { b[k] = IL[pos]; pos++; } var NewOp = new Opcode(s,BitConverter.ToDouble(b,0)); MakeLabel(NewOp); // This labels the Opcode return NewOp; }
public Opcode() { ParamVal = null; Prefix = null; IsPrefix = false; NeedsLabel = false; }
Opcode OpcodeIndxS(string s) { // This gets the parameter (stored as b) from the byte[] byte b = IL[pos]; pos++; var ind = new Index("V_" + b); var NewOp = new Opcode(s,b); MakeLabel(NewOp); return NewOp; }
Opcode OpcodeMeth(string s) { // This gets the parameter (stored as b) from the byte[] byte[] b = new byte[4]; Array.Copy(IL, pos, b, 0, 4); pos+=4; MethodBase mbase = (MethodBase)curModule.ResolveMember(BitConverter.ToInt32(b,0)); var NewOp = new Opcode(s, mbase); NewOp.workingAssm = this.workingAssm; NewOp.workingType = this.InputMeth.DeclaringType; MakeLabel(NewOp); // This labels the Opcode return NewOp; }