示例#1
0
        /// <summary>
        /// Obtains a reference to the singleton
        /// </summary>
        /// <returns>The reference</returns>
        public static OpCodesMap GetSingleton()
        {
            if (OpCodesMap._singleton == null)
            {
                OpCodesMap._singleton = new OpCodesMap();
            }

            return(OpCodesMap._singleton);
        }
示例#2
0
        /// <summary>
        /// Returns an array of instructions that describe the code portion of this method.
        /// </summary>
        /// <returns>The array of instructions</returns>
        private ILInstruction[] GetInstructions(byte[] fromILBytes)
        {
            List <ILInstruction> il  = new List <ILInstruction>();
            OpCodesMap           map = OpCodesMap.GetSingleton();

            // for now iterate over the IL bytes and change them to opcodes
            for (int i = 0; i < fromILBytes.Length; i++)
            {
                byte   current = fromILBytes[i];
                OpCode code    = OpCodes.Nop;
                if (current != 0xfe)
                {
                    code = map.GetCode(current);
                }
                else
                {
                    code = map.GetCode(BitConverter.ToInt16(fromILBytes, i));
                    i++;
                }

                ILInstruction instruction = null;
                switch (code.OperandType)
                {
                case OperandType.InlineNone:
                    instruction = new InlineNoneILInstruction(code);
                    break;

                case OperandType.ShortInlineBrTarget:
                    instruction = new ShortInlineBrTargetILInstruction(code, fromILBytes[i++]);
                    break;

                case OperandType.InlineBrTarget:
                    instruction = new InlineBrTargetILInstruction(code, BitConverter.ToInt32(fromILBytes, i + 1));
                    i          += 4;
                    break;

                case OperandType.ShortInlineI:
                    instruction = new ShortInlineIILInstruction(code, fromILBytes[i++]);
                    break;

                case OperandType.ShortInlineR:
                    instruction = new ShortInlineRILInstruction(code, BitConverter.ToSingle(fromILBytes, i + 1));
                    i          += 4;
                    break;

                case OperandType.ShortInlineVar:
                    instruction = new ShortInlineVarILInstruction(code, fromILBytes[i++]);
                    break;

                case OperandType.InlineI:
                    instruction = new InlineIILInstruction(code, BitConverter.ToUInt32(fromILBytes, i + 1));
                    i          += 4;
                    break;

                case OperandType.InlineVar:
                    instruction = new InlineVarILInstruction(code, BitConverter.ToUInt16(fromILBytes, i + 1));
                    i          += 2;
                    break;

                case OperandType.InlineR:
                    instruction = new InlineRILInstruction(code, BitConverter.ToDouble(fromILBytes, i + 1));
                    i          += 8;
                    break;

                case OperandType.InlineI8:
                    instruction = new InlineI8ILInstruction(code, BitConverter.ToUInt64(fromILBytes, i + 1));
                    i          += 8;
                    break;

                case OperandType.InlineString:
                    instruction = new InlineStringILInstruction(this.Assembly, code, BitConverter.ToInt32(fromILBytes, i + 1));
                    i          += 4;
                    break;

                case OperandType.InlineSig:
                    instruction = new InlineSigILInstruction(this.Assembly, code, BitConverter.ToInt32(fromILBytes, i + 1));
                    i          += 4;
                    break;

                case OperandType.InlineField:
                    instruction = new InlineFieldILInstruction(this.Assembly, code, BitConverter.ToUInt32(fromILBytes, i + 1));
                    i          += 4;
                    break;

                case OperandType.InlineMethod:
                    instruction = new InlineMethodILInstruction(this.Assembly, code, BitConverter.ToUInt32(fromILBytes, i + 1));
                    i          += 4;
                    break;

                case OperandType.InlineType:
                    instruction = new InlineTypeILInstruction(this.Assembly, code, BitConverter.ToUInt32(fromILBytes, i + 1));
                    i          += 4;
                    break;

                case OperandType.InlineTok:
                    instruction = new InlineTokenILInstruction(this.Assembly, code, BitConverter.ToUInt32(fromILBytes, i + 1));
                    i          += 4;
                    break;

                case OperandType.InlineSwitch:
                    Int32 cases = BitConverter.ToInt32(fromILBytes, i + 1);
                    i += 4;
                    Int32[] jumpTargets = new Int32[cases];
                    for (Int32 counter = 0; counter < cases; counter++)
                    {
                        jumpTargets[counter] = BitConverter.ToInt32(fromILBytes, i + 1);
                        i += 4;
                    }
                    instruction = new InlineSwitchILInstruction(code, jumpTargets);
                    break;

                default:
                    instruction = new ILInstruction(code);
                    break;
                }
                il.Add(instruction);
            }
            return(il.ToArray());
        }