Beispiel #1
0
        /// <summary>
        /// Gets the IL block for the specified method.
        /// </summary>
        /// <param name="theInfo">The method to get the IL block of.</param>
        /// <param name="checkDepLibs">Whether to check dependencies or not.</param>
        /// <returns>The IL block or null if none found.</returns>
        public ILBlock GetILBlock(Types.MethodInfo theInfo, bool checkDepLibs = true)
        {
            if (ILBlocks.ContainsKey(theInfo))
            {
                return(ILBlocks[theInfo]);
            }

            if (checkDepLibs)
            {
                foreach (ILLibrary depLib in Dependencies)
                {
                    ILBlock result = depLib.GetILBlock(theInfo);
                    if (result != null)
                    {
                        return(result);
                    }
                }
            }

            return(null);
        }
Beispiel #2
0
        /// <summary>
        /// Reads a non-plugged method.
        /// </summary>
        /// <param name="aMethodInfo">The method to read.</param>
        /// <returns>The new IL block for the method.</returns>
        public static ILBlock ReadNonPlugged(Types.MethodInfo aMethodInfo)
        {
            ILBlock result = new ILBlock()
            {
                TheMethodInfo = aMethodInfo
            };

            MethodBody methodBody = aMethodInfo.MethodBody;

            //Method body for something like [DelegateType].Invoke() is null.
            //  So just return an empty method if that is this case.
            if (methodBody != null)
            {
                foreach (LocalVariableInfo aLocal in methodBody.LocalVariables)
                {
                    aMethodInfo.LocalInfos.Add(new Types.VariableInfo()
                    {
                        UnderlyingType = aLocal.LocalType,
                        Position       = aLocal.LocalIndex
                    });
                }

                byte[] ILBytes    = methodBody.GetILAsByteArray();
                int    ILBytesPos = 0;

                while (ILBytesPos < ILBytes.Length)
                {
                    OpCode currOpCode;
                    ushort currOpCodeID    = 0;
                    int    currOpBytesSize = 0;

                    if (ILBytes[ILBytesPos] == 0xFE)
                    {
                        currOpCodeID     = (ushort)(0xFE00 + (short)ILBytes[ILBytesPos + 1]);
                        currOpBytesSize += 2;
                    }
                    else
                    {
                        currOpCodeID = (ushort)ILBytes[ILBytesPos];
                        currOpBytesSize++;
                    }
                    currOpCode = AllOpCodes[currOpCodeID];

                    int operandSize = 0;
                    switch (currOpCode.OperandType)
                    {
                        #region Operands

                    case OperandType.InlineBrTarget:
                        operandSize = 4;
                        break;

                    case OperandType.InlineField:
                        operandSize = 4;
                        break;

                    case OperandType.InlineI:
                        operandSize = 4;
                        break;

                    case OperandType.InlineI8:
                        operandSize = 8;
                        break;

                    case OperandType.InlineMethod:
                        operandSize = 4;
                        break;

                    case OperandType.InlineNone:
                        //No operands = no op size
                        break;

                    case OperandType.InlineR:
                        operandSize = 8;
                        break;

                    case OperandType.InlineSig:
                        operandSize = 4;
                        break;

                    case OperandType.InlineString:
                        operandSize = 4;
                        break;

                    case OperandType.InlineSwitch:
                    {
                        uint count = Utilities.ReadUInt32(ILBytes, ILBytesPos + currOpBytesSize);
                        currOpBytesSize += 4;
                        operandSize      = (int)(count * 4);
                    }
                    break;

                    case OperandType.InlineTok:
                        operandSize = 4;
                        break;

                    case OperandType.InlineType:
                        operandSize = 4;
                        break;

                    case OperandType.InlineVar:
                        operandSize = 2;
                        break;

                    case OperandType.ShortInlineBrTarget:
                        operandSize = 1;
                        break;

                    case OperandType.ShortInlineI:
                        operandSize = 1;
                        break;

                    case OperandType.ShortInlineR:
                        operandSize = 4;
                        break;

                    case OperandType.ShortInlineVar:
                        operandSize = 1;
                        break;

                    default:
                        throw new Exception("Unrecognised operand type!");

                        #endregion
                    }

                    MethodBase methodToCall = null;
                    byte[]     valueBytes   = new byte[operandSize];
                    if (operandSize > 0)
                    {
                        Array.Copy(ILBytes, ILBytesPos + currOpBytesSize, valueBytes, 0, operandSize);

                        if (currOpCode.Equals(OpCodes.Call) ||
                            currOpCode.Equals(OpCodes.Calli) ||
                            currOpCode.Equals(OpCodes.Callvirt) ||
                            currOpCode.Equals(OpCodes.Ldftn) ||
                            currOpCode.Equals(OpCodes.Newobj))
                        {
                            int MethodMetadataToken = Utilities.ReadInt32(valueBytes, 0);
                            methodToCall = aMethodInfo.UnderlyingInfo.Module.ResolveMethod(MethodMetadataToken);
                        }
                    }

                    result.ILOps.Add(new ILOp()
                    {
                        opCode       = currOpCode,
                        Offset       = ILBytesPos,
                        BytesSize    = currOpBytesSize + operandSize,
                        ValueBytes   = valueBytes,
                        MethodToCall = methodToCall
                    });

                    ILBytesPos += currOpBytesSize + operandSize;
                }

                foreach (ExceptionHandlingClause aClause in methodBody.ExceptionHandlingClauses)
                {
                    ExceptionHandledBlock exBlock = result.GetExactExceptionHandledBlock(aClause.TryOffset);
                    if (exBlock == null)
                    {
                        exBlock        = new ExceptionHandledBlock();
                        exBlock.Offset = aClause.TryOffset;
                        exBlock.Length = aClause.TryLength;
                        result.ExceptionHandledBlocks.Add(exBlock);
                    }

                    switch (aClause.Flags)
                    {
                    case ExceptionHandlingClauseOptions.Fault:
                    case ExceptionHandlingClauseOptions.Clause:
                    {
                        CatchBlock catchBlock = new CatchBlock()
                        {
                            Offset = aClause.HandlerOffset,
                            Length = aClause.HandlerLength,
                            //Though not used, we may as well set it anyway
                            FilterType = aClause.CatchType
                        };
                        exBlock.CatchBlocks.Add(catchBlock);
                    }
                    break;

                    case ExceptionHandlingClauseOptions.Finally:
                    {
                        FinallyBlock finallyBlock = new FinallyBlock()
                        {
                            Offset = aClause.HandlerOffset,
                            Length = aClause.HandlerLength
                        };
                        exBlock.FinallyBlocks.Add(finallyBlock);
                    }
                    break;

                    default:
                        Logger.LogError("IL0010", "", 0,
                                        "Exception handling clause not supported! Type: " + aClause.Flags.ToString());
                        break;
                    }
                }
            }

            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Scans the specified library and any dependencies.
        /// </summary>
        /// <param name="TheLibrary">The library to scan.</param>
        /// <returns>
        /// CompileResult.OK if completed successfully.
        /// Otherwise CompileResult.PartialFail or CompileResult.Error depending on
        /// the extent of the problem.
        /// </returns>
        public static CompileResult Scan(ILLibrary TheLibrary)
        {
            CompileResult result = CompileResult.OK;

            if (TheLibrary.ILScanned)
            {
                return(result);
            }
            TheLibrary.ILScanned = true;

            foreach (ILLibrary depLib in TheLibrary.Dependencies)
            {
                Scan(depLib);
            }

            // Create / Add Static Fields ASM Block
            ASM.ASMBlock StaticFieldsBlock = new ASM.ASMBlock()
            {
                Priority = (long.MinValue / 2) - 9
            };
            TheLibrary.TheASMLibrary.ASMBlocks.Add(StaticFieldsBlock);

            // Create / Add Types Table ASM Block
            ASM.ASMBlock TypesTableBlock = new ASM.ASMBlock()
            {
                Priority = (long.MinValue / 2) - 8
            };
            TheLibrary.TheASMLibrary.ASMBlocks.Add(TypesTableBlock);

            // Create / Add Method Tables ASM Block
            ASM.ASMBlock MethodTablesBlock = new ASM.ASMBlock()
            {
                Priority = (long.MinValue / 2) + 0
            };
            TheLibrary.TheASMLibrary.ASMBlocks.Add(MethodTablesBlock);

            // Create / Add Field Tables ASM Block
            ASM.ASMBlock FieldTablesBlock = new ASM.ASMBlock()
            {
                Priority = (long.MinValue / 2) + 1
            };
            TheLibrary.TheASMLibrary.ASMBlocks.Add(FieldTablesBlock);

            // Don't use foreach or you get collection modified exceptions
            for (int i = 0; i < TheLibrary.TypeInfos.Count; i++)
            {
                Types.TypeInfo aTypeInfo = TheLibrary.TypeInfos[i];
                if (!ScannedTypes.ContainsKey(aTypeInfo.ID))
                {
                    ScannedTypes.Add(aTypeInfo.ID, TheLibrary);
                    ScanStaticFields(TheLibrary, aTypeInfo, StaticFieldsBlock);
                    ScanType(TheLibrary, aTypeInfo, TypesTableBlock);
                    ScanMethods(TheLibrary, aTypeInfo, MethodTablesBlock);
                    ScanFields(TheLibrary, aTypeInfo, FieldTablesBlock);
                }
            }

            foreach (Types.MethodInfo aMethodInfo in TheLibrary.ILBlocks.Keys)
            {
                ILBlock       anILBlock    = TheLibrary.ILBlocks[aMethodInfo];
                CompileResult singleResult = CompileResult.OK;

                if (anILBlock.Plugged)
                {
                    singleResult = ScanPluggedILBlock(TheLibrary, aMethodInfo, anILBlock);
                }
                else
                {
                    singleResult = ScanNonpluggedILBlock(TheLibrary, aMethodInfo, anILBlock);
                }

                if (result != CompileResult.OK)
                {
                    result = singleResult;
                }
            }

            // Create / Add String Literals ASM Block
            #region String Literals Block

            ASM.ASMBlock StringLiteralsBlock = new ASM.ASMBlock()
            {
                Priority = (long.MinValue / 2) - 10
            };
            TheLibrary.TheASMLibrary.ASMBlocks.Add(StringLiteralsBlock);

            string StringTypeId = ILLibrary.SpecialClasses[typeof(Attributes.StringClassAttribute)].First().ID;
            StringLiteralsBlock.AddExternalLabel(StringTypeId);
            foreach (KeyValuePair <string, string> aStringLiteral in TheLibrary.StringLiterals)
            {
                string value       = aStringLiteral.Value;
                byte[] lengthBytes = BitConverter.GetBytes(value.Length);

                ASM.ASMOp newLiteralOp = TargetArchitecture.CreateASMOp(ASM.OpCodes.StringLiteral,
                                                                        aStringLiteral.Key, StringTypeId, lengthBytes, value.ToCharArray());

                StringLiteralsBlock.Append(newLiteralOp);
            }

            #endregion

            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Scans the specified non-plugged IL block.
        /// </summary>
        /// <param name="TheLibrary">The library currently being compiled.</param>
        /// <param name="theMethodInfo">The method which generated the IL block.</param>
        /// <param name="theILBlock">The IL block to scan.</param>
        /// <returns>CompileResult.OK.</returns>
        private static CompileResult ScanNonpluggedILBlock(ILLibrary TheLibrary, Types.MethodInfo theMethodInfo, ILBlock theILBlock)
        {
            CompileResult result = CompileResult.OK;

            ASM.ASMBlock TheASMBlock = new ASM.ASMBlock()
            {
                OriginMethodInfo = theMethodInfo,
                Priority         = theMethodInfo.Priority
            };

            ILConversionState convState = new ILConversionState()
            {
                TheILLibrary      = TheLibrary,
                CurrentStackFrame = new StackFrame(),
                Input             = theILBlock,
                Result            = TheASMBlock
            };

            foreach (ILOp anOp in theILBlock.ILOps)
            {
                try
                {
                    string commentText = TheASMBlock.GenerateILOpLabel(convState.PositionOf(anOp), "") + "  --  " + anOp.opCode.ToString() + " -- Offset: " + anOp.Offset.ToString("X2");

                    ASM.ASMOp newCommentOp = TargetArchitecture.CreateASMOp(ASM.OpCodes.Comment, commentText);
                    TheASMBlock.ASMOps.Add(newCommentOp);

                    int currCount = TheASMBlock.ASMOps.Count;
                    if (anOp is ILOps.MethodStart)
                    {
                        TargetArchitecture.MethodStartOp.Convert(convState, anOp);
                    }
                    else if (anOp is ILOps.MethodEnd)
                    {
                        TargetArchitecture.MethodEndOp.Convert(convState, anOp);
                    }
                    else if (anOp is ILOps.StackSwitch)
                    {
                        TargetArchitecture.StackSwitchOp.Convert(convState, anOp);
                    }
                    else
                    {
                        ILOp ConverterOp = TargetArchitecture.TargetILOps[(ILOp.OpCodes)anOp.opCode.Value];
                        ConverterOp.Convert(convState, anOp);
                    }

                    if (anOp.LabelRequired)
                    {
                        if (currCount < TheASMBlock.ASMOps.Count)
                        {
                            TheASMBlock.ASMOps[currCount].ILLabelPosition = convState.PositionOf(anOp);
                            TheASMBlock.ASMOps[currCount].RequiresILLabel = true;
                        }
                    }
                }
                catch (KeyNotFoundException)
                {
                    result = CompileResult.PartialFailure;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                                    string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], "Conversion IL op not found: " + Enum.GetName(typeof(ILOp.OpCodes), anOp.opCode.Value) + "."));
                }
                catch (InvalidOperationException)
                {
                    result = CompileResult.PartialFailure;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                                    string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], Enum.GetName(typeof(ILOp.OpCodes), anOp.opCode.Value)));
                }
                catch (NotSupportedException ex)
                {
                    result = CompileResult.PartialFailure;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                                    string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], "An IL op reported something as not supported. " + Enum.GetName(typeof(ILOp.OpCodes), anOp.opCode.Value) + ". " + ex.Message));
                }
                catch (Exception ex)
                {
                    result = CompileResult.Fail;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                                    string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], ex.Message));
                }
            }

            TheLibrary.TheASMLibrary.ASMBlocks.Add(TheASMBlock);

            return(result);
        }
Beispiel #5
0
        /// <summary>
        /// Scans the specified plugged IL block.
        /// </summary>
        /// <param name="TheLibrary">The library currently being compiled.</param>
        /// <param name="theMethodInfo">The method which generated the IL block.</param>
        /// <param name="theILBlock">The IL block to scan.</param>
        /// <returns>CompileResult.OK.</returns>
        private static CompileResult ScanPluggedILBlock(ILLibrary TheLibrary, Types.MethodInfo theMethodInfo, ILBlock theILBlock)
        {
            TheLibrary.TheASMLibrary.ASMBlocks.Add(new ASM.ASMBlock()
            {
                PlugPath         = theILBlock.PlugPath,
                OriginMethodInfo = theMethodInfo,
                Priority         = theMethodInfo.Priority
            });

            return(CompileResult.OK);
        }
Beispiel #6
0
        /// <summary>
        /// Injects the garbage collector related IL ops into the specified method.
        /// </summary>
        /// <param name="TheLibrary">The library being compiled.</param>
        /// <param name="theMethodInfo">The method to inject ops into.</param>
        /// <param name="theILBlock">The IL block for the method to inject ops into.</param>
        private static void InjectGC(ILLibrary TheLibrary, Types.MethodInfo theMethodInfo, ILBlock theILBlock)
        {
            if (theMethodInfo.ApplyGC)
            {
                // Find the index of the MethodStart op
                int MethodStartOpPos = theILBlock.PositionOf(theILBlock.ILOps.Where(x => x is ILOps.MethodStart).First());

                // Inject ops for incrementing ref. count of args at start of method
                int InjectIncArgsRefCountPos = MethodStartOpPos + 1;
                foreach (Types.VariableInfo argInfo in theMethodInfo.ArgumentInfos)
                {
                    if (argInfo.TheTypeInfo.IsGCManaged)
                    {
                        theILBlock.ILOps.Insert(InjectIncArgsRefCountPos, new ILOp()
                        {
                            opCode = System.Reflection.Emit.OpCodes.Ldarg,
                            ValueBytes = BitConverter.GetBytes(argInfo.Position)
                        });
                        theILBlock.ILOps.Insert(InjectIncArgsRefCountPos + 1, new ILOp()
                        {
                            opCode = System.Reflection.Emit.OpCodes.Call,
                            MethodToCall = ILLibrary.SpecialMethods[typeof(Attributes.IncrementRefCountMethodAttribute)].First().UnderlyingInfo
                        });
                    }
                }

                // The following two things can be done within the same loop:
                
                // Inject ops for inc./dec. ref. counts of objects when written to:
                //      - Arguments / Locals
                //      - Fields / Static Fields
                //      - Elements of Arrays

                // Add Cleanup Block and Inject finally-block ops for it
                //      - Also remember the op for storing return value (if any)

                ILPreprocessState preprocessState = new ILPreprocessState()
                {
                    TheILLibrary = TheLibrary,
                    Input = theILBlock,
                    CurrentStackFrame = new StackFrame()
                };

                ExceptionHandledBlock CleanupExBlock = new ExceptionHandledBlock();
                
                for (int opIndx = 0; opIndx < theILBlock.ILOps.Count; opIndx++)
                {
                    ILOp currOp = theILBlock.ILOps[opIndx];
                    bool IncRefCount = false;
                    int incOpIndexBy = 0;
                    
                    switch ((ILOp.OpCodes)currOp.opCode.Value)
                    {
                        case ILOp.OpCodes.Stsfld:
                            #region Stsfld
                            {
                                int metadataToken = Utilities.ReadInt32(currOp.ValueBytes, 0);
                                System.Reflection.FieldInfo theField = theMethodInfo.UnderlyingInfo.Module.ResolveField(metadataToken);
                                Types.TypeInfo theFieldTypeInfo = TheLibrary.GetTypeInfo(theField.FieldType);
                                if (theFieldTypeInfo.IsGCManaged)
                                {
                                    theILBlock.ILOps.Insert(opIndx, new ILOp()
                                    {
                                        opCode = System.Reflection.Emit.OpCodes.Ldsfld,
                                        ValueBytes = currOp.ValueBytes
                                    });
                                    theILBlock.ILOps.Insert(opIndx + 1, new ILOp()
                                    {
                                        opCode = System.Reflection.Emit.OpCodes.Call,
                                        MethodToCall = ILLibrary.SpecialMethods[typeof(Attributes.DecrementRefCountMethodAttribute)].First().UnderlyingInfo
                                    });
                                    
                                    IncRefCount = true;
                                    incOpIndexBy = 2;
                                }
                            }
                            #endregion
                            break;
                        case ILOp.OpCodes.Stloc:
                        case ILOp.OpCodes.Stloc_0:
                        case ILOp.OpCodes.Stloc_1:
                        case ILOp.OpCodes.Stloc_2:
                        case ILOp.OpCodes.Stloc_3:
                        case ILOp.OpCodes.Stloc_S:
                            #region Stloc
                            {
                                UInt16 localIndex = 0;
                                switch ((ILOp.OpCodes)currOp.opCode.Value)
                                {
                                    case ILOp.OpCodes.Stloc:
                                        localIndex = (UInt16)Utilities.ReadInt16(currOp.ValueBytes, 0);
                                        break;
                                    case ILOp.OpCodes.Stloc_0:
                                        localIndex = 0;
                                        break;
                                    case ILOp.OpCodes.Stloc_1:
                                        localIndex = 1;
                                        break;
                                    case ILOp.OpCodes.Stloc_2:
                                        localIndex = 2;
                                        break;
                                    case ILOp.OpCodes.Stloc_3:
                                        localIndex = 3;
                                        break;
                                    case ILOp.OpCodes.Stloc_S:
                                        localIndex = (UInt16)currOp.ValueBytes[0];
                                        break;
                                }
                                Types.TypeInfo LocalTypeInfo = theMethodInfo.LocalInfos[localIndex].TheTypeInfo;
                                if (LocalTypeInfo.IsGCManaged)
                                {
                                    theILBlock.ILOps.Insert(opIndx, new ILOp()
                                    {
                                        opCode = System.Reflection.Emit.OpCodes.Ldloc,
                                        ValueBytes = BitConverter.GetBytes(localIndex)
                                    });
                                    theILBlock.ILOps.Insert(opIndx + 1, new ILOp()
                                    {
                                        opCode = System.Reflection.Emit.OpCodes.Call,
                                        MethodToCall = ILLibrary.SpecialMethods[typeof(Attributes.DecrementRefCountMethodAttribute)].First().UnderlyingInfo
                                    });
                                    
                                    IncRefCount = true;
                                    incOpIndexBy = 2;
                                }
                            }
                            #endregion
                            break;
                        case ILOp.OpCodes.Stfld:
                            #region Stfld
                            {
                                int metadataToken = Utilities.ReadInt32(currOp.ValueBytes, 0);
                                System.Reflection.FieldInfo theField = theMethodInfo.UnderlyingInfo.Module.ResolveField(metadataToken);
                                Types.TypeInfo theFieldTypeInfo = TheLibrary.GetTypeInfo(theField.FieldType);
                                if (theFieldTypeInfo.IsGCManaged)
                                {
                                    // Items on stack:
                                    //  - Object reference
                                    //  - (New) Value to store
                                    //
                                    // We want to load the current value of the field
                                    //  for which we must duplicate the object ref
                                    // But first, we must remove the (new) value to store
                                    //  off the stack, while also storing it to put back
                                    //  on the stack after so the store can continue
                                    //
                                    // So:
                                    //      1. Switch value to store and object ref on stack
                                    //      3. Duplicate the object ref
                                    //      4. Load the field value
                                    //      5. Call dec ref count
                                    //      6. Switch value to store and object ref back again

                                    //USE A SWITCH STACK ITEMS OP!!

                                    theILBlock.ILOps.Insert(opIndx, new ILOps.StackSwitch()
                                    {
                                        StackSwitch_Items = 2
                                    });
                                    
                                    theILBlock.ILOps.Insert(opIndx + 1, new ILOp()
                                    {
                                        opCode = System.Reflection.Emit.OpCodes.Dup
                                    });
                                    theILBlock.ILOps.Insert(opIndx + 2, new ILOp()
                                    {
                                        opCode = System.Reflection.Emit.OpCodes.Ldfld,
                                        ValueBytes = currOp.ValueBytes
                                    });
                                    theILBlock.ILOps.Insert(opIndx + 3, new ILOp()
                                    {
                                        opCode = System.Reflection.Emit.OpCodes.Call,
                                        MethodToCall = ILLibrary.SpecialMethods[typeof(Attributes.DecrementRefCountMethodAttribute)].First().UnderlyingInfo
                                    });

                                    theILBlock.ILOps.Insert(opIndx + 4, new ILOps.StackSwitch()
                                    {
                                        StackSwitch_Items = 2
                                    });

                                    IncRefCount = true;
                                    incOpIndexBy = 5;
                                }
                            }
                            #endregion
                            break;
                        case ILOp.OpCodes.Stelem:
                        case ILOp.OpCodes.Stelem_Ref:
                            #region Stelem / Stelem_Ref
                            {
                                bool doDecrement = false;
                                bool isRefOp = false;
                                if ((ILOp.OpCodes)currOp.opCode.Value == ILOp.OpCodes.Stelem_Ref)
                                {
                                    doDecrement = preprocessState.CurrentStackFrame.Stack.Peek().isGCManaged;
                                    isRefOp = true;
                                }
                                else
                                {
                                    int metadataToken = Utilities.ReadInt32(currOp.ValueBytes, 0);
                                    Type elementType = theMethodInfo.UnderlyingInfo.Module.ResolveType(metadataToken);
                                    doDecrement = TheLibrary.GetTypeInfo(elementType).IsGCManaged;
                                }

                                if (doDecrement)
                                {
                                    // Items on stack:
                                    //  - Array reference
                                    //  - Index
                                    //  - (New) Value to store
                                    //
                                    // We want to load the current value of the element at Index in the array
                                    //  for which we must duplicate the array ref and index
                                    // But first, we must remove the (new) value to store
                                    //  off the stack, while also storing it to put back
                                    //  on the stack after so the store can continue
                                    //
                                    // So:
                                    //      1. Switch (rotate) 1 times the top 3 values so that index is on top
                                    //      2. Duplicate the index
                                    //      3. Switch (rotate) 2 times the top 4 values so that array ref is on top
                                    //      4. Duplicate the array ref
                                    //      5. Switch (rotate) 4 times the top 5 values so that duplicate array ref and index are on top
                                    //      6. Do LdElem op to load existing element value
                                    //      7. Call GC.DecrementRefCount
                                    //      8. Switch (rotate) 1 times the top 3 values so that the stack is in its original state
                                    //      (9. Continue to increment ref count as normal)
                                    //
                                    // The following is a diagram of the stack manipulation occurring here:
                                    //      Key: A=Array ref, I=Index, V=Value to store, E=Loaded element
                                    //      Top-most stack item appears last
                                    //  
                                    //     1) Rotate x 1    2) Duplicate       3)  Rot x 2         4)  Dup
                                    //  A,I,V ---------> V,A,I ---------> V,A,I,I ---------> I,I,V,A ---------> I,I,V,A,A
                                    //
                                    //
                                    //          5) Rot x 4           6) Ldelem        7) Call GC (Dec)
                                    //  I,I,V,A,A ---------> I,V,A,A,I ---------> I,V,A,E ---------> I,V,A
                                    //
                                    //
                                    //      8) Rot x 1       9)  Dup         10) Call GC (Inc)
                                    //  I,V,A ---------> A,I,V ---------> A,I,V,V ---------> A,I,V

                                    #region 1.
                                    theILBlock.ILOps.Insert(opIndx, new ILOps.StackSwitch()
                                    {
                                        ValueBytes = BitConverter.GetBytes(3),
                                        StackSwitch_Items = 3
                                    });
                                    incOpIndexBy++;
                                    #endregion
                                    #region 2.
                                    theILBlock.ILOps.Insert(opIndx + incOpIndexBy, new ILOp()
                                    {
                                        opCode = System.Reflection.Emit.OpCodes.Dup
                                    });
                                    incOpIndexBy++;
                                    #endregion
                                    #region 3.
                                    theILBlock.ILOps.Insert(opIndx + incOpIndexBy, new ILOps.StackSwitch()
                                    {
                                        ValueBytes = BitConverter.GetBytes(4),
                                        StackSwitch_Items = 4
                                    });
                                    incOpIndexBy++;

                                    theILBlock.ILOps.Insert(opIndx + incOpIndexBy, new ILOps.StackSwitch()
                                    {
                                        ValueBytes = BitConverter.GetBytes(4),
                                        StackSwitch_Items = 4
                                    });
                                    incOpIndexBy++;
                                    #endregion
                                    #region 4.
                                    theILBlock.ILOps.Insert(opIndx + incOpIndexBy, new ILOp()
                                    {
                                        opCode = System.Reflection.Emit.OpCodes.Dup
                                    });
                                    incOpIndexBy++;
                                    #endregion
                                    #region 5.
                                    theILBlock.ILOps.Insert(opIndx + incOpIndexBy, new ILOps.StackSwitch()
                                    {
                                        ValueBytes = BitConverter.GetBytes(5),
                                        StackSwitch_Items = 5
                                    });
                                    incOpIndexBy++;
                                    theILBlock.ILOps.Insert(opIndx + incOpIndexBy, new ILOps.StackSwitch()
                                    {
                                        ValueBytes = BitConverter.GetBytes(5),
                                        StackSwitch_Items = 5
                                    });
                                    incOpIndexBy++;
                                    theILBlock.ILOps.Insert(opIndx + incOpIndexBy, new ILOps.StackSwitch()
                                    {
                                        ValueBytes = BitConverter.GetBytes(5),
                                        StackSwitch_Items = 5
                                    });
                                    incOpIndexBy++;
                                    theILBlock.ILOps.Insert(opIndx + incOpIndexBy, new ILOps.StackSwitch()
                                    {
                                        ValueBytes = BitConverter.GetBytes(5),
                                        StackSwitch_Items = 5
                                    });
                                    incOpIndexBy++;
                                    #endregion
                                    #region 6.
                                    theILBlock.ILOps.Insert(opIndx + incOpIndexBy, new ILOp()
                                    {
                                        opCode = isRefOp ? System.Reflection.Emit.OpCodes.Ldelem_Ref : System.Reflection.Emit.OpCodes.Ldelem,
                                        ValueBytes = currOp.ValueBytes
                                    });
                                    incOpIndexBy++;
                                    #endregion
                                    #region 7.
                                    theILBlock.ILOps.Insert(opIndx + incOpIndexBy, new ILOp()
                                    {
                                        opCode = System.Reflection.Emit.OpCodes.Call,
                                        MethodToCall = ILLibrary.SpecialMethods[typeof(Attributes.DecrementRefCountMethodAttribute)].First().UnderlyingInfo
                                    });
                                    incOpIndexBy++;
                                    #endregion
                                    #region 8.
                                    theILBlock.ILOps.Insert(opIndx + incOpIndexBy, new ILOps.StackSwitch()
                                    {
                                        ValueBytes = BitConverter.GetBytes(3),
                                        StackSwitch_Items = 3
                                    });
                                    incOpIndexBy++;
                                    #endregion

                                    IncRefCount = true;
                                }
                            }
                            #endregion
                            break;
                        case ILOp.OpCodes.Starg:
                        case ILOp.OpCodes.Starg_S:
                            #region Starg
                            {
                                UInt16 index = (ILOp.OpCodes)currOp.opCode.Value == ILOp.OpCodes.Starg_S ? 
                                    (UInt16)currOp.ValueBytes[0] : (UInt16)Utilities.ReadInt16(currOp.ValueBytes, 0);
                                if (theMethodInfo.ArgumentInfos[index].TheTypeInfo.IsGCManaged)
                                {
                                    theILBlock.ILOps.Insert(opIndx, new ILOp()
                                    {
                                        opCode = System.Reflection.Emit.OpCodes.Ldarg,
                                        ValueBytes = BitConverter.GetBytes(index)
                                    });
                                    theILBlock.ILOps.Insert(opIndx + 1, new ILOp()
                                    {
                                        opCode = System.Reflection.Emit.OpCodes.Call,
                                        MethodToCall = ILLibrary.SpecialMethods[typeof(Attributes.DecrementRefCountMethodAttribute)].First().UnderlyingInfo
                                    });

                                    IncRefCount = true;
                                    incOpIndexBy = 2;
                                }
                            }
                            #endregion
                            break;
                    }

                    if (IncRefCount &&
                        !preprocessState.CurrentStackFrame.Stack.Peek().isNewGCObject)
                    {
                        theILBlock.ILOps.Insert(opIndx + incOpIndexBy, new ILOp()
                        {
                            opCode = System.Reflection.Emit.OpCodes.Dup
                        });
                        incOpIndexBy++;
                        theILBlock.ILOps.Insert(opIndx + incOpIndexBy, new ILOp()
                        {
                            opCode = System.Reflection.Emit.OpCodes.Call,
                            MethodToCall = ILLibrary.SpecialMethods[typeof(Attributes.IncrementRefCountMethodAttribute)].First().UnderlyingInfo
                        });
                        incOpIndexBy++;
                    }

                    // If op changed
                    if (theILBlock.ILOps[opIndx] != currOp)
                    {
                        theILBlock.ILOps[opIndx].Offset = currOp.Offset;
                        theILBlock.ILOps[opIndx].BytesSize = currOp.BytesSize;
                    }

                    // <= is correct. E.g. if 1 extra op added, incOpIndex=1 so <= results in currOp processed
                    //      + extra op processed
                    bool UseNextOpAsCleanupStart = false;
                    for (int i = 0; i <= incOpIndexBy; i++)
                    {
                        currOp = theILBlock.ILOps[opIndx];

                        if (UseNextOpAsCleanupStart)
                        {
                            UseNextOpAsCleanupStart = false;

                            CleanupExBlock.Offset = currOp.Offset;
                        }

                        if (currOp is ILOps.MethodStart)
                        {
                            TargetArchitecture.MethodStartOp.PerformStackOperations(preprocessState, theILBlock.ILOps[opIndx]);
                            UseNextOpAsCleanupStart = true;
                        }
                        else if (currOp is ILOps.MethodEnd)
                        {
                            TargetArchitecture.MethodEndOp.PerformStackOperations(preprocessState, theILBlock.ILOps[opIndx]);
                        }
                        else if (currOp is ILOps.StackSwitch)
                        {
                            TargetArchitecture.StackSwitchOp.PerformStackOperations(preprocessState, theILBlock.ILOps[opIndx]);
                        }
                        else
                        {
                            // Leave unsupported ops for the IL Scanner to deal with (or later code e.g. castclass op)
                            if (TargetArchitecture.TargetILOps.ContainsKey((ILOp.OpCodes)currOp.opCode.Value))
                            {
                                ILOp ConverterOp = TargetArchitecture.TargetILOps[(ILOp.OpCodes)currOp.opCode.Value];
                                ConverterOp.PerformStackOperations(preprocessState, currOp);
                            }
                        }

                        if (i > 0)
                        {
                            opIndx++;
                        }
                    }
                }

                if (theMethodInfo.ArgumentInfos.Count > 0 ||
                    theMethodInfo.LocalInfos.Count > 0)
                {
                    bool AddCleanupBlock = false;
                    foreach (Types.VariableInfo anArgInfo in theMethodInfo.ArgumentInfos)
                    {
                        if (anArgInfo.TheTypeInfo.IsGCManaged)
                        {
                            AddCleanupBlock = true;
                            break;
                        }
                    }
                    if (!AddCleanupBlock)
                    {
                        foreach (Types.VariableInfo aLocInfo in theMethodInfo.LocalInfos)
                        {
                            if (aLocInfo.TheTypeInfo.IsGCManaged)
                            {
                                AddCleanupBlock = true;
                                break;
                            }
                        }
                    }

                    if (AddCleanupBlock)
                    {
                        ILOp lastOp = theILBlock.ILOps.Last();
                        int lastOpOffset = lastOp.Offset;
                        int lastOpIndex = theILBlock.ILOps.Count - 1;
                        bool MethodHasReturnValue = false;

                        // If there is a return value, we will need to temp store it
                        if (theMethodInfo.UnderlyingInfo is System.Reflection.MethodInfo)
                        {
                            Type returnType = ((System.Reflection.MethodInfo)theMethodInfo.UnderlyingInfo).ReturnType;
                            //Void return type = no return value
                            if (returnType != typeof(void))
                            {
                                // Add local variable for storing return value
                                int lastLocalOffset = theMethodInfo.LocalInfos.Count > 0 ? theMethodInfo.LocalInfos.Last().Offset : 0;
                                int lastLocalSize = theMethodInfo.LocalInfos.Count > 0 ? theMethodInfo.LocalInfos.Last().TheTypeInfo.SizeOnStackInBytes : 0;
                                theMethodInfo.LocalInfos.Add(new Types.VariableInfo()
                                {
                                    UnderlyingType = returnType,
                                    TheTypeInfo = TheLibrary.GetTypeInfo(returnType),
                                    Position = theMethodInfo.LocalInfos.Count,
                                    Offset = lastLocalOffset + lastLocalSize
                                });

                                // Add op for storing return value, update op offsets
                                theILBlock.ILOps.Insert(lastOpIndex, new ILOp()
                                {
                                    opCode = System.Reflection.Emit.OpCodes.Stloc,
                                    Offset = lastOpOffset,
                                    BytesSize = lastOp.BytesSize,
                                    ValueBytes = BitConverter.GetBytes(theMethodInfo.LocalInfos.Count - 1)
                                });
                                lastOpIndex++;

                                MethodHasReturnValue = true;
                            }
                        }

                        ILOp leaveOp;
                        // Add the Leave op of the try-block
                        theILBlock.ILOps.Insert(lastOpIndex, leaveOp = new ILOp()
                        {
                            opCode = System.Reflection.Emit.OpCodes.Leave,
                            Offset = lastOpOffset,
                            BytesSize = lastOp.BytesSize,
                            ValueBytes = BitConverter.GetBytes(0)
                        });
                        lastOpIndex++;

                        FinallyBlock CleanupFinallyBlock = new FinallyBlock()
                        {
                            Offset = lastOpOffset + lastOp.BytesSize,
                            Length = 0
                        };
                        CleanupExBlock.Length = lastOpOffset - CleanupExBlock.Offset;
                        CleanupExBlock.FinallyBlocks.Add(CleanupFinallyBlock);

                        int cleanupOpsOffset = lastOpOffset + 1;

                        // Add cleanup code for local variables (including the return value local)
                        foreach (Types.VariableInfo aLocInfo in theMethodInfo.LocalInfos)
                        {
                            if (aLocInfo.TheTypeInfo.IsGCManaged)
                            {
                                theILBlock.ILOps.Insert(lastOpIndex, new ILOp()
                                {
                                    opCode = System.Reflection.Emit.OpCodes.Ldloc,
                                    Offset = cleanupOpsOffset,
                                    BytesSize = 1,
                                    ValueBytes = BitConverter.GetBytes(aLocInfo.Position)
                                });
                                cleanupOpsOffset++;
                                lastOpIndex++;

                                theILBlock.ILOps.Insert(lastOpIndex, new ILOp()
                                {
                                    opCode = System.Reflection.Emit.OpCodes.Call,
                                    Offset = cleanupOpsOffset,
                                    BytesSize = 1,
                                    MethodToCall = ILLibrary.SpecialMethods[typeof(Attributes.DecrementRefCountMethodAttribute)].First().UnderlyingInfo
                                });
                                cleanupOpsOffset++;
                                lastOpIndex++;

                                CleanupFinallyBlock.Length += 2;
                            }
                        }

                        // Add cleanup code for arguments
                        foreach (Types.VariableInfo anArgInfo in theMethodInfo.ArgumentInfos)
                        {
                            if (anArgInfo.TheTypeInfo.IsGCManaged)
                            {
                                theILBlock.ILOps.Insert(lastOpIndex, new ILOp()
                                {
                                    opCode = System.Reflection.Emit.OpCodes.Ldarg,
                                    Offset = cleanupOpsOffset,
                                    BytesSize = 1,
                                    ValueBytes = BitConverter.GetBytes(anArgInfo.Position)
                                });
                                cleanupOpsOffset++;
                                lastOpIndex++;

                                theILBlock.ILOps.Insert(lastOpIndex, new ILOp()
                                {
                                    opCode = System.Reflection.Emit.OpCodes.Call,
                                    Offset = cleanupOpsOffset,
                                    BytesSize = 1,
                                    MethodToCall = ILLibrary.SpecialMethods[typeof(Attributes.DecrementRefCountMethodAttribute)].First().UnderlyingInfo
                                });
                                cleanupOpsOffset++;
                                lastOpIndex++;

                                CleanupFinallyBlock.Length += 2;
                            }
                        }

                        // Add end finally op
                        theILBlock.ILOps.Insert(lastOpIndex, leaveOp.LoadAtILOpAfterOp = new ILOp()
                        {
                            opCode = System.Reflection.Emit.OpCodes.Endfinally,
                            Offset = cleanupOpsOffset,
                            BytesSize = 1
                        });
                        cleanupOpsOffset++;
                        lastOpIndex++;
                        
                        CleanupFinallyBlock.Length++;

                        // Add restore return value op
                        if (MethodHasReturnValue)
                        {
                            theILBlock.ILOps.Insert(lastOpIndex, new ILOp()
                            {
                                opCode = System.Reflection.Emit.OpCodes.Ldloc,
                                Offset = cleanupOpsOffset,
                                BytesSize = 1,
                                ValueBytes = BitConverter.GetBytes(theMethodInfo.LocalInfos.Count - 1)
                            });
                            cleanupOpsOffset++;
                            lastOpIndex++;
                        }

                        // Add ex block to the method
                        theILBlock.ExceptionHandledBlocks.Add(CleanupExBlock);

                        // Replace any Ret ops contained within Cleanup Block with:
                        //      - Op to store return value (if any)
                        //      - Leave op
                        bool Inside = false;
                        for(int opIndx = 0; opIndx < theILBlock.ILOps.Count; opIndx++)
                        {
                            if (theILBlock.ILOps[opIndx].Offset > CleanupExBlock.Offset + CleanupExBlock.Length)
                            {
                                break;
                            }
                            else if (theILBlock.ILOps[opIndx].Offset >= CleanupExBlock.Offset)
                            {
                                Inside = true;
                            }

                            if (Inside &&
                                (ILOp.OpCodes)theILBlock.ILOps[opIndx].opCode.Value == ILOp.OpCodes.Ret)
                            {
                                ILOp ARetOp = theILBlock.ILOps[opIndx];
                                theILBlock.ILOps.RemoveAt(opIndx);

                                if (MethodHasReturnValue)
                                {
                                    theILBlock.ILOps.Insert(opIndx, new ILOp()
                                    {
                                        opCode = System.Reflection.Emit.OpCodes.Stloc,
                                        Offset = ARetOp.Offset,
                                        ValueBytes = BitConverter.GetBytes(theMethodInfo.LocalInfos.Count - 1)
                                    });
                                    theILBlock.ILOps.Insert(opIndx + 1, new ILOp()
                                    {
                                        Offset = ARetOp.Offset,
                                        opCode = System.Reflection.Emit.OpCodes.Leave,
                                        ValueBytes = BitConverter.GetBytes(0),
                                        LoadAtILOpAfterOp = leaveOp.LoadAtILOpAfterOp
                                    });
                                }
                                else
                                {
                                    theILBlock.ILOps.Insert(opIndx, new ILOp()
                                    {
                                        Offset = ARetOp.Offset,
                                        opCode = System.Reflection.Emit.OpCodes.Leave,
                                        ValueBytes = BitConverter.GetBytes(0),
                                        LoadAtILOpAfterOp = leaveOp.LoadAtILOpAfterOp
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// Injects the second set of general IL ops.
        /// </summary>
        /// <remarks>
        /// Ops that must be inserted after the GC ops are inserted.
        /// </remarks>
        /// <param name="theMethodInfo">The method to inject ops into.</param>
        /// <param name="theILBlock">The IL block for the method to inject ops into.</param>
        private static void InjectGeneral2(Types.MethodInfo theMethodInfo, ILBlock theILBlock)
        {
            // Inject MethodEnd op just before anywhere where there is a ret
            for (int i = 0; i < theILBlock.ILOps.Count; i++)
            {
                ILOp theOp = theILBlock.ILOps[i];

                if ((ILOp.OpCodes)theOp.opCode.Value == ILOp.OpCodes.Ret)
                {
                    theILBlock.ILOps.Insert(i, new ILOps.MethodEnd()
                    {
                        Offset = theOp.Offset,
                        BytesSize = theOp.BytesSize
                    });
                    theOp.Offset = -1;
                    i++;
                }
            }
        }
Beispiel #8
0
 /// <summary>
 /// Injects the first set of general IL ops.
 /// </summary>
 /// <remarks>
 /// Ops that must be inserted before the GC ops are inserted.
 /// </remarks>
 /// <param name="theMethodInfo">The method to inject ops into.</param>
 /// <param name="theILBlock">The IL block for the method to inject ops into.</param>
 private static void InjectGeneral1(Types.MethodInfo theMethodInfo, ILBlock theILBlock)
 {
     // Inject MethodStart op
     theILBlock.ILOps.Insert(0, new ILOps.MethodStart()
         {
             Offset = -1
         });
 }
Beispiel #9
0
        /// <summary>
        /// Preprocesses the IL ops of the specified method/IL block.
        /// </summary>
        /// <param name="TheLibrary">The library being compiled.</param>
        /// <param name="theMethodInfo">The method to preprocess.</param>
        /// <param name="theILBlock">The IL block for the method to preprocess.</param>
        private static void PreprocessILOps(ILLibrary TheLibrary, Types.MethodInfo theMethodInfo, ILBlock theILBlock)
        {
            StaticConstructorDependency staticConstructorDependencyRoot = null;
            if (theMethodInfo.UnderlyingInfo is System.Reflection.ConstructorInfo &&
                        theMethodInfo.IsStatic)
            {
                System.Reflection.ConstructorInfo aConstructor = (System.Reflection.ConstructorInfo)theMethodInfo.UnderlyingInfo;
                staticConstructorDependencyRoot = ILLibrary.TheStaticConstructorDependencyTree[aConstructor];
                if (staticConstructorDependencyRoot == null)
                {
                    staticConstructorDependencyRoot = new StaticConstructorDependency()
                    {
                        TheConstructor = aConstructor
                    };
                    ILLibrary.TheStaticConstructorDependencyTree.Children.Add(staticConstructorDependencyRoot);
                }
            }

            ILPreprocessState preprosState = new ILPreprocessState()
            {
                TheILLibrary = TheLibrary,
                Input = theILBlock
            };

            for (int i = 0; i < theILBlock.ILOps.Count; i++)
            {
                ILOp theOp = theILBlock.ILOps[i];

                // Remove cast class ops
                if ((ILOp.OpCodes)theOp.opCode.Value == ILOp.OpCodes.Castclass)
                {
                    theILBlock.ILOps.RemoveAt(i);
                    i--;
                    continue;
                }
                else if ((ILOp.OpCodes)theOp.opCode.Value == ILOp.OpCodes.Call)
                {
                    if (theOp.MethodToCall != null && 
                        theOp.MethodToCall.DeclaringType.AssemblyQualifiedName.Contains("mscorlib"))
                    {
                        //We do not want to process ops which attempt to call methods in mscorlib!
                        theILBlock.ILOps.RemoveAt(i);
                        i--;

                        //We do not allow calls to methods declared in MSCorLib.
                        //Some of these calls can just be ignored (e.g. GetTypeFromHandle is
                        //  called by typeof operator).
                        //Ones which can't be ignored, will result in an error...by virtue of
                        //  the fact that they were ignored when they were required.

                        //But just to make sure we save ourselves a headache later when
                        //  runtime debugging, output a message saying we ignored the call.

                        // TODO - IL level comments
                        // result.ASM.AppendLine("; Call to method defined in MSCorLib ignored:"); // DEBUG INFO
                        // result.ASM.AppendLine("; " + anILOpInfo.MethodToCall.DeclaringType.FullName + "." + anILOpInfo.MethodToCall.Name); // DEBUG INFO

                        //If the method is a call to a constructor in MsCorLib:
                        if (theOp.MethodToCall is System.Reflection.ConstructorInfo)
                        {
                            //Then we can presume it was a call to a base-class constructor (e.g. the Object constructor)
                            //  and so we just need to remove any args that were loaded onto the stack.
                            // TODO: result.ASM.AppendLine("; Method to call was constructor so removing params"); // DEBUG INFO
                            
                            //Remove args from stack
                            //If the constructor was non-static, then the first arg is the instance reference.
                            if (!theOp.MethodToCall.IsStatic)
                            {
                                i++;
                                theILBlock.ILOps.Insert(i, new ILOp()
                                {
                                    opCode = System.Reflection.Emit.OpCodes.Pop
                                });
                            }
                            foreach (System.Reflection.ParameterInfo anInfo in theOp.MethodToCall.GetParameters())
                            {
                                i++;
                                theILBlock.ILOps.Insert(i, new ILOp()
                                {
                                    opCode = System.Reflection.Emit.OpCodes.Pop
                                });
                            }
                        }
                    }
                }

                try
                {
                    ILOp ConverterOp = TargetArchitecture.TargetILOps[(ILOp.OpCodes)theOp.opCode.Value];

                    ConverterOp.Preprocess(preprosState, theOp);

                    if (staticConstructorDependencyRoot != null)
                    {
                        //Create our static constructor dependency tree

                        //Each of these ops could try to access a static method or field
                        switch ((ILOp.OpCodes)theOp.opCode.Value)
                        {
                            case ILOp.OpCodes.Call:
                                //Check if the method to call is static and not a constructor itself
                                //If so, we must add the declaring type's static constructors to the tree
                                if(theOp.ValueBytes != null && theOp.ValueBytes.Length > 0)
                                {
                                    int metadataToken = Utilities.ReadInt32(theOp.ValueBytes, 0);
                                    System.Reflection.MethodBase methodBaseInf = theMethodInfo.UnderlyingInfo.Module.ResolveMethod(metadataToken);
                                    if (!(methodBaseInf.IsConstructor || methodBaseInf is System.Reflection.ConstructorInfo))
                                    {
                                        System.Reflection.MethodInfo methodInf = (System.Reflection.MethodInfo)methodBaseInf;
                                        System.Reflection.ConstructorInfo[] staticConstructors = 
                                            methodInf.DeclaringType.GetConstructors(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)
                                                .Concat(methodInf.DeclaringType.GetConstructors(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic))
                                                .ToArray();
                                        if (staticConstructors.Length > 0)
                                        {
                                            System.Reflection.ConstructorInfo TheConstructor = staticConstructors[0];
                                            if (staticConstructorDependencyRoot[TheConstructor] == null)
                                            {
                                                staticConstructorDependencyRoot.Children.Add(new StaticConstructorDependency()
                                                {
                                                    TheConstructor = TheConstructor
                                                });
                                            }
                                        }
                                    }
                                }
                                break;
                            case ILOp.OpCodes.Ldsfld:
                            case ILOp.OpCodes.Ldsflda:
                            case ILOp.OpCodes.Stsfld:
                                {
                                    int metadataToken = Utilities.ReadInt32(theOp.ValueBytes, 0);
                                    System.Reflection.FieldInfo fieldInf = theMethodInfo.UnderlyingInfo.Module.ResolveField(metadataToken);
                                    System.Reflection.ConstructorInfo[] staticConstructors = fieldInf.DeclaringType.GetConstructors(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)
                                                                   .Concat(fieldInf.DeclaringType.GetConstructors(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic))
                                                                   .ToArray();
                                    if (staticConstructors.Length > 0)
                                    {
                                        System.Reflection.ConstructorInfo TheConstructor = staticConstructors[0];
                                        if (staticConstructorDependencyRoot[TheConstructor] == null)
                                        {
                                            staticConstructorDependencyRoot.Children.Add(new StaticConstructorDependency()
                                            {
                                                TheConstructor = TheConstructor
                                            });
                                        }
                                    }
                                }
                                break;
                        }
                    }                    
                }
                catch (KeyNotFoundException)
                {
                    //Ignore - will be caught by Il scanner
                }
                catch (Exception ex)
                {
                    Logger.LogError("ILPRE", theILBlock.TheMethodInfo.ToString(), 0,
                        "Il Preprocessor error: PreprocessILOps: " + ex.Message);
                }
            }
        }
Beispiel #10
0
        /// <summary>
        /// Injects the try-catch-finally related IL ops into the specified method.
        /// </summary>
        /// <param name="theMethodInfo">The method to inject ops into.</param>
        /// <param name="theILBlock">The IL block for the method to inject ops into.</param>
        private static void InjectTryCatchFinally(Types.MethodInfo theMethodInfo, ILBlock theILBlock)
        {
            // Replace Leave and Leave_S ops
            for (int i = 0; i < theILBlock.ILOps.Count; i++)
            {
                ILOp theOp = theILBlock.ILOps[i];

                if ((ILOp.OpCodes)theOp.opCode.Value == ILOp.OpCodes.Leave ||
                    (ILOp.OpCodes)theOp.opCode.Value == ILOp.OpCodes.Leave_S)
                {
                    theILBlock.ILOps.RemoveAt(i);

                    int ILOffset = 0;
                    if (theOp.LoadAtILOpAfterOp == null)
                    {
                        if ((int)theOp.opCode.Value == (int)ILOp.OpCodes.Leave)
                        {
                            ILOffset = BitConverter.ToInt32(theOp.ValueBytes, 0);
                        }
                        else
                        {
                            ILOffset = (int)theOp.ValueBytes[0];
                        }
                    }

                    theILBlock.ILOps.Insert(i, new ILOp()
                    {
                        Offset = theOp.Offset,
                        BytesSize = theOp.BytesSize,
                        opCode = System.Reflection.Emit.OpCodes.Ldftn,
                        LoadAtILOffset = theOp.NextOffset + ILOffset,
                        LoadAtILOpAfterOp = theOp.LoadAtILOpAfterOp,
                        MethodToCall = theILBlock.TheMethodInfo.UnderlyingInfo
                    });
                    theILBlock.ILOps.Insert(i + 1, new ILOp()
                    {
                        opCode = System.Reflection.Emit.OpCodes.Call,
                        MethodToCall = ILLibrary.SpecialMethods[typeof(Attributes.ExceptionsHandleLeaveMethodAttribute)].First().UnderlyingInfo
                    });

                    i++;
                }
                else if ((int)theOp.opCode.Value == (int)ILOp.OpCodes.Endfinally)
                {
                    //Endfinally is for leaving a (critical) finally section
                    //We handle it by a higher-level implementation rather than 
                    //  leaving it to each architecture to implement.

                    //Endfinally is handled by inserting a call to the Exceptions.HandleEndFinally method

                    theILBlock.ILOps.RemoveAt(i);

                    ILOp newOp;
                    theILBlock.ILOps.Insert(i, newOp = new ILOp()
                    {
                        Offset = theOp.Offset,
                        BytesSize = theOp.BytesSize,
                        opCode = System.Reflection.Emit.OpCodes.Call,
                        MethodToCall = ILLibrary.SpecialMethods[typeof(Attributes.ExceptionsHandleEndFinallyMethodAttribute)].First().UnderlyingInfo
                    });

                    //Replace references to this endfinally op
                    for (int j = 0; j < theILBlock.ILOps.Count; j++)
                    {
                        if (theILBlock.ILOps[j].LoadAtILOpAfterOp == theOp)
                        {
                            theILBlock.ILOps[j].LoadAtILOpAfterOp = newOp;
                        }
                    }
                }
            }


            foreach (ExceptionHandledBlock exBlock in theILBlock.ExceptionHandledBlocks)
            {
                #region Try-sections
                //      Insert the start of try-block
                //          Also remember that other ops (e.g. branch and leave) can hold references
                //          to the first op INSIDE a try block, even if they themselves are OUTSIDE 
                //          the block. This means we need to correct which op has the Offset value
                //          set so those ops point at the new start op of the try block.

                //Consists of adding a new ExceptionHandlerInfos
                //  built from the info in exBlock so we:
                //      - Add infos for all finally blocks first
                //      - Then add infos for all catch blocks
                //  Since finally code is always run after catch code in C#,
                //      by adding catch handlers after finally handlers, they 
                //      appear as the inner-most exception handlers and so get 
                //      run before finally handlers.

                //To add a new ExceptionHandlerInfo we must set up args for 
                //  calling Exceptions.AddExceptionHandlerInfo:
                // 1. We load a pointer to the handler
                //      - This is calculated from an offset from the start of the function to the handler
                // 2. We load a pointer to the filter
                //      - This is calculated from an offset from the start of the function to the filter
                //      Note: Filter has not been implemented as an actual filter. 
                //            At the moment, 0x00000000 indicates a finally handler,
                //                           0xFFFFFFFF indicates no filter block 
                //                                      (i.e. an unfiltered catch handler)
                //                           0xXXXXXXXX has undetermined behaviour!

                //For each finally block:
                int insertPos = theILBlock.PositionOf(theILBlock.At(exBlock.Offset));
                foreach (FinallyBlock finBlock in exBlock.FinallyBlocks)
                {
                    // 1. Load the pointer to the handler code:
                    theILBlock.ILOps.Insert(insertPos++, new ILOp()
                    {
                        opCode = System.Reflection.Emit.OpCodes.Ldftn, 
                        MethodToCall = theMethodInfo.UnderlyingInfo,
                        LoadAtILOffset = finBlock.Offset
                    });
                    theILBlock.ILOps.Insert(insertPos++, new ILOp()
                    {
                        opCode = System.Reflection.Emit.OpCodes.Ldc_I4,
                        ValueBytes = BitConverter.GetBytes(0x00000000)
                    });
                    theILBlock.ILOps.Insert(insertPos++, new ILOp()
                    {
                        opCode = System.Reflection.Emit.OpCodes.Call,
                        MethodToCall = ILLibrary.SpecialMethods[typeof(Attributes.AddExceptionHandlerInfoMethodAttribute)].First().UnderlyingInfo
                    });
                }
                foreach (CatchBlock catchBlock in exBlock.CatchBlocks)
                {
                    theILBlock.ILOps.Insert(insertPos++, new ILOp()
                    {
                        opCode = System.Reflection.Emit.OpCodes.Ldftn,
                        MethodToCall = theMethodInfo.UnderlyingInfo,
                        LoadAtILOffset = catchBlock.Offset
                    });
                    theILBlock.ILOps.Insert(insertPos++, new ILOp()
                    {
                        opCode = System.Reflection.Emit.OpCodes.Ldc_I4,
                        ValueBytes = BitConverter.GetBytes(0xFFFFFFFF)
                    });
                    theILBlock.ILOps.Insert(insertPos++, new ILOp()
                    {
                        opCode = System.Reflection.Emit.OpCodes.Call,
                        MethodToCall = ILLibrary.SpecialMethods[typeof(Attributes.AddExceptionHandlerInfoMethodAttribute)].First().UnderlyingInfo
                    });
                }
                #endregion
            }
        }
Beispiel #11
0
        /// <summary>
        /// Handles catch handles (of exception blocks) for the specified method.
        /// </summary>
        /// <param name="theMethodInfo">The method to handle.</param>
        /// <param name="theILBlock">The IL block for the method to handle.</param>
        private static void DealWithCatchHandlers(Types.MethodInfo theMethodInfo, ILBlock theILBlock)
        {
            foreach (ExceptionHandledBlock exBlock in theILBlock.ExceptionHandledBlocks)
            {
                #region Catch-sections

                // Strip the first pop of catch-handler

                foreach (CatchBlock aCatchBlock in exBlock.CatchBlocks)
                {
                    ILOp catchPopOp = theILBlock.At(aCatchBlock.Offset);
                    int pos = theILBlock.PositionOf(catchPopOp);
                    theILBlock.ILOps.RemoveAt(pos);
                    theILBlock.ILOps.Insert(pos, new ILOp()
                    {
                        opCode = System.Reflection.Emit.OpCodes.Nop,
                        Offset = catchPopOp.Offset,
                        BytesSize = catchPopOp.BytesSize
                    });
                }

                #endregion
            }
        }
Beispiel #12
0
        /// <summary>
        /// Scans the specified non-plugged IL block.
        /// </summary>
        /// <param name="TheLibrary">The library currently being compiled.</param>
        /// <param name="theMethodInfo">The method which generated the IL block.</param>
        /// <param name="theILBlock">The IL block to scan.</param>
        /// <returns>CompileResult.OK.</returns>
        private static CompileResult ScanNonpluggedILBlock(ILLibrary TheLibrary, Types.MethodInfo theMethodInfo, ILBlock theILBlock)
        {
            CompileResult result = CompileResult.OK;

            ASM.ASMBlock TheASMBlock = new ASM.ASMBlock()
            {
                OriginMethodInfo = theMethodInfo,
                Priority = theMethodInfo.Priority
            };
            
            ILConversionState convState = new ILConversionState()
            {
                TheILLibrary = TheLibrary,
                CurrentStackFrame = new StackFrame(),
                Input = theILBlock,
                Result = TheASMBlock
            };
            foreach (ILOp anOp in theILBlock.ILOps)
            {
                try
                {
                    string commentText = TheASMBlock.GenerateILOpLabel(convState.PositionOf(anOp), "") + "  --  " + anOp.opCode.ToString() + " -- Offset: " + anOp.Offset.ToString("X2");
                    
                    ASM.ASMOp newCommentOp = TargetArchitecture.CreateASMOp(ASM.OpCodes.Comment, commentText);
                    TheASMBlock.ASMOps.Add(newCommentOp);
                    
                    int currCount = TheASMBlock.ASMOps.Count;
                    if (anOp is ILOps.MethodStart)
                    {
                        TargetArchitecture.MethodStartOp.Convert(convState, anOp);
                    }
                    else if (anOp is ILOps.MethodEnd)
                    {
                        TargetArchitecture.MethodEndOp.Convert(convState, anOp);
                    }
                    else if (anOp is ILOps.StackSwitch)
                    {
                        TargetArchitecture.StackSwitchOp.Convert(convState, anOp);
                    }
                    else
                    {
                        ILOp ConverterOp = TargetArchitecture.TargetILOps[(ILOp.OpCodes)anOp.opCode.Value];
                        ConverterOp.Convert(convState, anOp);
                    }

                    if (anOp.LabelRequired)
                    {
                        if (currCount < TheASMBlock.ASMOps.Count)
                        {
                            TheASMBlock.ASMOps[currCount].ILLabelPosition = convState.PositionOf(anOp);
                            TheASMBlock.ASMOps[currCount].RequiresILLabel = true;
                        }
                    }
                }
                catch (KeyNotFoundException)
                {
                    result = CompileResult.PartialFailure;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                        string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], Enum.GetName(typeof(ILOp.OpCodes), anOp.opCode.Value), "Conversion for IL op not found."));
                }
                catch (InvalidOperationException ex)
                {
                    result = CompileResult.PartialFailure;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                        string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], Enum.GetName(typeof(ILOp.OpCodes), anOp.opCode.Value), ex.Message));
                }
                catch (NotSupportedException ex)
                {
                    result = CompileResult.PartialFailure;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                        string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], Enum.GetName(typeof(ILOp.OpCodes), anOp.opCode.Value), "An IL op reported something as not supported : " + ex.Message));
                }
                catch (Exception ex)
                {
                    result = CompileResult.Fail;

                    Logger.LogError(Errors.ILCompiler_ScanILOpFailure_ErrorCode, theMethodInfo.ToString(), anOp.Offset,
                        string.Format(Errors.ErrorMessages[Errors.ILCompiler_ScanILOpFailure_ErrorCode], Enum.GetName(typeof(ILOp.OpCodes), anOp.opCode.Value), ex.Message));
                }
            }

            TheLibrary.TheASMLibrary.ASMBlocks.Add(TheASMBlock);

            return result;
        }
Beispiel #13
0
        /// <summary>
        /// Scans the specified plugged IL block.
        /// </summary>
        /// <param name="TheLibrary">The library currently being compiled.</param>
        /// <param name="theMethodInfo">The method which generated the IL block.</param>
        /// <param name="theILBlock">The IL block to scan.</param>
        /// <returns>CompileResult.OK.</returns>
        private static CompileResult ScanPluggedILBlock(ILLibrary TheLibrary, Types.MethodInfo theMethodInfo, ILBlock theILBlock)
        {
            TheLibrary.TheASMLibrary.ASMBlocks.Add(new ASM.ASMBlock()
            {
                PlugPath = theILBlock.PlugPath,
                OriginMethodInfo = theMethodInfo,
                Priority = theMethodInfo.Priority
            });

            return CompileResult.OK;
        }
Beispiel #14
0
        /// <summary>
        /// Performs the link.
        /// </summary>
        /// <param name="TheLibrary">The root library to link.</param>
        /// <returns>CompileResult.OK if the link succeeded. Otherwise, CompileResult.Fail.</returns>
        public static CompileResult Link(IL.ILLibrary TheLibrary, bool dependency = false, string Name = null)
        {
            bool OK = true;

            // If: Link to ELF and Libraries
            //      - Link sub-libs to .a files
            //      - Link main lib to .elf file (if present)
            // If: Link to ISO
            //      - Generate basic link-script
            //      - Generate full link-script by inserting necessary file location instructions for all object files
            //      - Execute ld to build bin file
            //      - Execute ISO9660Generator to build .ISO file

            if (Options.LinkMode == Options.LinkModes.ELF)
            {
                // Check for main method. If found, that library gets linked to Executable not Shared Lib

                List <string> depLibNames = new List <string>();
                foreach (IL.ILLibrary depLib in TheLibrary.Dependencies)
                {
                    string depLibName = Utilities.CleanFileName(depLib.TheAssembly.GetName().Name);
                    if (Options.ShortenDependencyNames)
                    {
                        if (!DependencyNameMapping.ContainsKey(depLibName))
                        {
                            DependencyNameMapping.Add(depLibName, (NameGenerator++).ToString());
                        }

                        depLibName = DependencyNameMapping[depLibName];
                    }
                    depLibNames.Add(depLibName);

                    OK = OK && (Link(depLib, true, depLibName) == CompileResult.OK);
                    if (!OK)
                    {
                        break;
                    }
                }

                if (!OK)
                {
                    return(CompileResult.Fail);
                }

                List <ASM.ASMBlock> SequencedASMBlocks = new List <ASM.ASMBlock>();
                SequencedASMBlocks.AddRange(TheLibrary.TheASMLibrary.ASMBlocks);
                SequencedASMBlocks.Sort(GetOrder);
                SequencedASMBlocks.ForEach(delegate(ASM.ASMBlock block)
                {
                    if (block != null && block.OriginMethodInfo != null)
                    {
                        DebugDataWriter.AddMethodMapping(block.OriginMethodInfo.ID, block.ASMOutputFilePath);
                    }
                });

                // Find start method if any, use as ENTRY point
                bool   ExecutableOutput = false;
                string EntryPoint       = null;
                if (IL.ILLibrary.SpecialMethods.ContainsKey(typeof(Attributes.MainMethodAttribute)))
                {
                    Types.MethodInfo mainMethodInfo  = IL.ILLibrary.SpecialMethods[typeof(Attributes.MainMethodAttribute)].First();
                    IL.ILBlock       mainMethodBlock = TheLibrary.GetILBlock(mainMethodInfo, false);
                    if (mainMethodBlock != null)
                    {
                        ExecutableOutput = true;
                        EntryPoint       = mainMethodInfo.ID;
                    }
                }

                if (Options.ShortenDependencyNames && string.IsNullOrWhiteSpace(Name))
                {
                    Name = "Driver";
                }

                string AssemblyName = string.IsNullOrWhiteSpace(Name) ? Utilities.CleanFileName(TheLibrary.TheAssembly.GetName().Name) : Name;

                DebugDataWriter.SaveDataFiles(Options.OutputPath, AssemblyName);
                DebugDataWriter.SaveLibraryInfo(Options.OutputPath, TheLibrary);

                LinkInformation LinkInfo = new LinkInformation()
                {
                    ToolsPath          = Options.ToolsPath,
                    LinkScriptCmdPath  = Path.Combine(Options.OutputPath, @"DriversCompiler\" + AssemblyName + "_linker_command.txt"),
                    LinkScriptPath     = Path.Combine(Options.OutputPath, @"DriversCompiler\" + AssemblyName + "_linker.ld"),
                    BinPath            = Path.Combine(Options.OutputPath, "Output\\" + (ExecutableOutput ? AssemblyName + ".elf" : "Lib" + AssemblyName + ".a")),
                    MapPath            = Path.Combine(Options.OutputPath, AssemblyName + ".map"),
                    ASMPath            = Path.Combine(Options.OutputPath, AssemblyName + ".new.asm"),
                    LdWorkingDir       = Path.Combine(Options.OutputPath, "") + "\\",
                    ExecutableOutput   = ExecutableOutput,
                    EntryPoint         = EntryPoint,
                    SequencedASMBlocks = SequencedASMBlocks,
                    depLibNames        = depLibNames
                };

                if (!Directory.Exists(Path.GetDirectoryName(LinkInfo.BinPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(LinkInfo.BinPath));
                }

                OK = TargetArchitecture.TargetFunctions.LinkELF(TheLibrary, LinkInfo);

                if (OK)
                {
                    DebugDataWriter.ProcessMapFile(LinkInfo.MapPath);
                }
            }
            else if (Options.LinkMode == Options.LinkModes.ISO)
            {
                List <ASM.ASMBlock> SequencedASMBlocks = new List <ASM.ASMBlock>();
                List <IL.ILLibrary> FlattenedLibs      = TheLibrary.Flatten();
                foreach (IL.ILLibrary depLib in FlattenedLibs)
                {
                    SequencedASMBlocks.AddRange(depLib.TheASMLibrary.ASMBlocks);
                    DebugDataWriter.SaveLibraryInfo(Options.OutputPath, depLib);
                }
                SequencedASMBlocks.Sort(GetOrder);
                SequencedASMBlocks.ForEach(delegate(ASM.ASMBlock block)
                {
                    if (block != null && block.OriginMethodInfo != null)
                    {
                        DebugDataWriter.AddMethodMapping(block.OriginMethodInfo.ID, block.ASMOutputFilePath);
                    }
                });

                string AssemblyName = Utilities.CleanFileName(TheLibrary.TheAssembly.GetName().Name);

                DebugDataWriter.SaveDataFiles(Options.OutputPath, AssemblyName);
                DebugDataWriter.SaveLibraryInfo(Options.OutputPath, TheLibrary);

                LinkInformation LinkInfo = new LinkInformation()
                {
                    ToolsPath          = Options.ToolsPath,
                    ISOGenPath         = Path.Combine(Options.ToolsPath, @"ISO9660Generator.exe"),
                    ISOToolsDirPath    = Path.Combine(Options.ToolsPath, @"ISO"),
                    ISODirPath         = Path.Combine(Options.OutputPath, @"DriversCompiler\ISO"),
                    LinkScriptPath     = Path.Combine(Options.OutputPath, @"DriversCompiler\linker.ld"),
                    BinPath            = Path.Combine(Options.OutputPath, @"DriversCompiler\ISO\Kernel.bin"),
                    ISOLinuxPath       = Path.Combine(Options.OutputPath, @"DriversCompiler\ISO\" + (Options.BuildMode == Options.BuildModes.Debug ? "isolinux-debug.bin" : "isolinux.bin")),
                    ISOPath            = Path.Combine(Options.OutputPath, AssemblyName + ".iso"),
                    MapPath            = Path.Combine(Options.OutputPath, AssemblyName + ".map"),
                    ASMPath            = Path.Combine(Options.OutputPath, AssemblyName + ".new.asm"),
                    LdWorkingDir       = Path.Combine(Options.OutputPath, "DriversCompiler") + "\\",
                    SequencedASMBlocks = SequencedASMBlocks
                };

                if (Directory.Exists(LinkInfo.ISODirPath))
                {
                    Directory.Delete(LinkInfo.ISODirPath, true);
                }
                CopyDirectory(LinkInfo.ISOToolsDirPath, LinkInfo.ISODirPath, true);

                OK = TargetArchitecture.TargetFunctions.LinkISO(TheLibrary, LinkInfo);

                if (OK)
                {
                    DebugDataWriter.ProcessMapFile(LinkInfo.MapPath);
                }
            }

            return(OK ? CompileResult.OK : CompileResult.Fail);
        }
Beispiel #15
0
        /// <summary>
        /// Reads a non-plugged method.
        /// </summary>
        /// <param name="aMethodInfo">The method to read.</param>
        /// <returns>The new IL block for the method.</returns>
        public static ILBlock ReadNonPlugged(Types.MethodInfo aMethodInfo)
        {
            ILBlock result = new ILBlock()
            {
                TheMethodInfo = aMethodInfo
            };

            MethodBody methodBody = aMethodInfo.MethodBody;
            //Method body for something like [DelegateType].Invoke() is null. 
            //  So just return an empty method if that is this case.
            if (methodBody != null)
            {
                foreach (LocalVariableInfo aLocal in methodBody.LocalVariables)
                {
                    aMethodInfo.LocalInfos.Add(new Types.VariableInfo()
                    {
                        UnderlyingType = aLocal.LocalType,
                        Position = aLocal.LocalIndex
                    });
                }

                byte[] ILBytes = methodBody.GetILAsByteArray();
                int ILBytesPos = 0;

                while (ILBytesPos < ILBytes.Length)
                {
                    OpCode currOpCode;
                    ushort currOpCodeID = 0;
                    int currOpBytesSize = 0;

                    if (ILBytes[ILBytesPos] == 0xFE)
                    {
                        currOpCodeID = (ushort)(0xFE00 + (short)ILBytes[ILBytesPos + 1]);
                        currOpBytesSize += 2;
                    }
                    else
                    {
                        currOpCodeID = (ushort)ILBytes[ILBytesPos];
                        currOpBytesSize++;
                    }
                    currOpCode = AllOpCodes[currOpCodeID];

                    int operandSize = 0;
                    switch (currOpCode.OperandType)
                    {
                        #region Operands

                        case OperandType.InlineBrTarget:
                            operandSize = 4;
                            break;
                        case OperandType.InlineField:
                            operandSize = 4;
                            break;
                        case OperandType.InlineI:
                            operandSize = 4;
                            break;
                        case OperandType.InlineI8:
                            operandSize = 8;
                            break;
                        case OperandType.InlineMethod:
                            operandSize = 4;
                            break;
                        case OperandType.InlineNone:
                            //No operands = no op size
                            break;
                        case OperandType.InlineR:
                            operandSize = 8;
                            break;
                        case OperandType.InlineSig:
                            operandSize = 4;
                            break;
                        case OperandType.InlineString:
                            operandSize = 4;
                            break;
                        case OperandType.InlineSwitch:
                            {
                                uint count = Utilities.ReadUInt32(ILBytes, ILBytesPos + currOpBytesSize);
                                currOpBytesSize += 4;
                                operandSize = (int)(count * 4);
                            }
                            break;
                        case OperandType.InlineTok:
                            operandSize = 4;
                            break;
                        case OperandType.InlineType:
                            operandSize = 4;
                            break;
                        case OperandType.InlineVar:
                            operandSize = 2;
                            break;
                        case OperandType.ShortInlineBrTarget:
                            operandSize = 1;
                            break;
                        case OperandType.ShortInlineI:
                            operandSize = 1;
                            break;
                        case OperandType.ShortInlineR:
                            operandSize = 4;
                            break;
                        case OperandType.ShortInlineVar:
                            operandSize = 1;
                            break;
                        default:
                            throw new Exception("Unrecognised operand type!");

                        #endregion
                    }

                    MethodBase methodToCall = null;
                    byte[] valueBytes = new byte[operandSize];
                    if (operandSize > 0)
                    {
                        Array.Copy(ILBytes, ILBytesPos + currOpBytesSize, valueBytes, 0, operandSize);

                        if (currOpCode.Equals(OpCodes.Call) ||
                            currOpCode.Equals(OpCodes.Calli) ||
                            currOpCode.Equals(OpCodes.Callvirt) ||
                            currOpCode.Equals(OpCodes.Ldftn) ||
                            currOpCode.Equals(OpCodes.Newobj))
                        {
                            int MethodMetadataToken = Utilities.ReadInt32(valueBytes, 0);
                            methodToCall = aMethodInfo.UnderlyingInfo.Module.ResolveMethod(MethodMetadataToken);
                        }
                    }

                    result.ILOps.Add(new ILOp()
                    {
                        opCode = currOpCode,
                        Offset = ILBytesPos,
                        BytesSize = currOpBytesSize + operandSize,
                        ValueBytes = valueBytes,
                        MethodToCall = methodToCall
                    });

                    ILBytesPos += currOpBytesSize + operandSize;
                }

                foreach (ExceptionHandlingClause aClause in methodBody.ExceptionHandlingClauses)
                {
                    ExceptionHandledBlock exBlock = result.GetExactExceptionHandledBlock(aClause.TryOffset);
                    if (exBlock == null)
                    {
                        exBlock = new ExceptionHandledBlock();
                        exBlock.Offset = aClause.TryOffset;
                        exBlock.Length = aClause.TryLength;
                        result.ExceptionHandledBlocks.Add(exBlock);
                    }

                    switch (aClause.Flags)
                    {
                        case ExceptionHandlingClauseOptions.Fault:
                        case ExceptionHandlingClauseOptions.Clause:
                            {
                                CatchBlock catchBlock = new CatchBlock()
                                {
                                    Offset = aClause.HandlerOffset,
                                    Length = aClause.HandlerLength,
                                    //Though not used, we may as well set it anyway
                                    FilterType = aClause.CatchType
                                };
                                exBlock.CatchBlocks.Add(catchBlock);
                            }
                            break;
                        case ExceptionHandlingClauseOptions.Finally:
                            {
                                FinallyBlock finallyBlock = new FinallyBlock()
                                {
                                    Offset = aClause.HandlerOffset,
                                    Length = aClause.HandlerLength
                                };
                                exBlock.FinallyBlocks.Add(finallyBlock);
                            }
                            break;
                        default:
                            Logger.LogError("IL0010", "", 0,
                                "Exception handling clause not supported! Type: " + aClause.Flags.ToString());
                            break;
                    }
                }
            }

            return result;
        }