Exemple #1
0
 protected Packet( OpCode opCode )
 {
     if( !Enum.IsDefined( typeof( OpCode ), OpCode ) ) {
         throw new ArgumentOutOfRangeException( "opCode" );
     }
     OpCode = opCode;
 }
Exemple #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StobjInstruction"/> class.
 /// </summary>
 /// <param name="opcode">The opcode.</param>
 public StobjInstruction(OpCode opcode)
     : base(opcode)
 {
     switch (opcode) {
         case OpCode.Stind_i1:
             _valueType = new SigType(CilElementType.I1);
             break;
         case OpCode.Stind_i2:
             _valueType = new SigType(CilElementType.I2);
             break;
         case OpCode.Stind_i4:
             _valueType = new SigType(CilElementType.I4);
             break;
         case OpCode.Stind_i8:
             _valueType = new SigType(CilElementType.I8);
             break;
         case OpCode.Stind_r4:
             _valueType = new SigType(CilElementType.R4);
             break;
         case OpCode.Stind_r8:
             _valueType = new SigType(CilElementType.R8);
             break;
         case OpCode.Stind_i:
             _valueType = new SigType(CilElementType.I);
             break;
         case OpCode.Stind_ref: // FIXME: Really object?
             _valueType = new SigType(CilElementType.Object);
             break;
         default:
             throw new NotImplementedException();
     }
 }
Exemple #3
0
        public void Emit_UnknownOpCode_ThrowsNotSupportedException()
        {
            var ilGenerator = new ILGenerator(null);
            var opCode = new OpCode(-1);

            ExceptionAssert.Throws<NotSupportedException>(() => ilGenerator.Emit(opCode));
        }
Exemple #4
0
 public static void WriteCodes(BinaryWriter bw, OpCode[] ops)
 {
     for (int i = 0; i < ops.Length; i++)
     {
         bw.Write(ops[i].GetCodes());
     }
 }
        //wicky.patch.start
        static OpCodes()
        {
            //Start from first index to ignore nop (onebyte)
            for (int i = 1; i < OneByteOpCode.Length; i++)
            {
                //arglist: FE 00
                if (OneByteOpCode[i].Op2 == 0x00 && OneByteOpCode[i].Code != Code.Arglist)
                {
                    OneByteOpCode[i] = new OpCode(
                        0xff << 0 | (byte)i << 8 | (byte)Code.Unused << 16 | (byte)FlowControl.Next << 24,
                        (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push0 << 24);
                }
            }

            //Start from first index to ignore arglist (twobyte)
            for (int i = 1; i < TwoBytesOpCode.Length; i++)
            {
                //arglist: FE 00
                if (TwoBytesOpCode[i].Op2 == 0x00 && TwoBytesOpCode[i].Code != Code.Arglist)
                {
                    TwoBytesOpCode[i] = new OpCode(
                    0xfe << 0 | (byte)i << 8 | (byte)Code.Unused << 16 | (byte)FlowControl.Next << 24,
                    (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push0 << 24);
                }
            }

        }
Exemple #6
0
        public static void AddCodes(NodeBase caller, BlockBase parent, NodeBase target, OpModule codes, string op, Addr32 dest)
        {
            if (target is TypeOf) target = (target as TypeOf).Target;

            var v = target as Variant;
            if (v != null && parent.GetFunction(v.Name) == null)
            {
                var fpname = (target as Variant).Name;
                var fpt = Types.GetType(parent, fpname);
                if (fpt == null || !fpt.Check())
                    throw caller.Abort("undefined type: {0}", fpname);
                codes.AddCodesV(op, dest, codes.GetTypeObject(fpt));
                return;
            }

            var tt = target.Type;
            var tr = tt as TypeReference;
            var tts = tt.Type as TypeStruct;
            if (tr != null && (tr.IsArray || (tts != null && tts.IsClass)))
            {
                target.AddCodesV(codes, "mov", null);
                var label = new OpCode();
                codes.Add(I386.Test(Reg32.EAX, Reg32.EAX));
                codes.Add(I386.Jcc(Cc.Z, label.Address));
                codes.Add(I386.MovRA(Reg32.EAX, Addr32.NewRO(Reg32.EAX, -16)));
                codes.Add(label);
                codes.AddCodes(op, dest);
            }
            else
                codes.AddCodesV(op, dest, codes.GetTypeObject(tt));
        }
 static bool IsCall(OpCode opCode)
 {
     return opCode == OpCodes.Call ||
            opCode == OpCodes.Callvirt ||
            opCode == OpCodes.Calli ||
            opCode == OpCodes.Ldftn;
 }
 public BinaryOperation(string sOp, Expression x, Expression y)
 {
     operation = sOp;
     opcode = StringToOpCode(sOp);
     operand1 = x;
     operand2 = y;
 }
 public Instruction(byte[] code, int offset, int length, OpCode oc, TokenResolver resolver)
 {
     this.code = code;
     this.offset = offset;
     this.length = length;
     this.oc = oc;
     this.resolver = resolver;
 }
Exemple #10
0
        static void AssertOpCodeSequence(OpCode [] expected, MethodBody body)
        {
            var opcodes = body.Instructions.Select (i => i.OpCode).ToArray ();
            Assert.AreEqual (expected.Length, opcodes.Length);

            for (int i = 0; i < opcodes.Length; i++)
                Assert.AreEqual (expected [i], opcodes [i]);
        }
Exemple #11
0
 public CompiledByte(OpCode op, Value a, Value b)
 {
     ushort[] output = compile(op,a,b);
     for (int i = 0; i < output.Length; i++)
     {
         ;
     }
 }
Exemple #12
0
        public Instruction(OpCode opcode, int index, params Operand[] operands)
        {
            OpCode = opcode;

            Operands = operands;

            Index = index;
        }
Exemple #13
0
		public void Add(Token token, OpCode op, string stringValue, params int[] args)
		{
			List<int> nums = new List<int>(args);
			nums.Insert(0, (int)op);
			this.byteCode.Add(nums.ToArray());
			this.tokens.Add(token);
			this.stringArgs.Add(stringValue);
		}
Exemple #14
0
        private void AddJump(OpCode jumpOp, string jumpLabel)
        {
            // add the jump and a blank space for the destination
            mBytecode.Write(jumpOp, 0);

            // patch the destination later
            mJumpPatches.Add(new JumpPatch(jumpLabel, mBytecode.Position - 4));
        }
Exemple #15
0
        private void AddJumpBack(OpCode jumpOp, string jumpLabel)
        {
            // pop the most recently-added jump with the label
            JumpPatch jump = mJumpBackPatches.Last(j => j.Label == jumpLabel);
            mJumpBackPatches.Remove(jump);

            mBytecode.Write(jumpOp, jump.Location);
        }
 public ScriptElement(OpCode opCode, Byte[] data = null)
 {
     this.opCode = opCode;
     this.data = data;
     if (opCode == OpCode.OP_0)
         this.data = new ScriptVarInt(0).ToBytes();
     else if (opCode == OpCode.OP_1NEGATE || (OpCode.OP_1 <= opCode && opCode <= OpCode.OP_16))
         this.data = new ScriptVarInt((int)opCode - ((int)OpCode.OP_1 - 1)).ToBytes();
 }
Exemple #17
0
		private BranchSet(OpCode brTrue, OpCode brFalse, OpCode brEq, OpCode brNe, OpCode brLt, OpCode brLtUn, OpCode brGt, OpCode brGtUn, OpCode brLe, OpCode brLeUn, OpCode brGe, OpCode brGeUn)
		{
			BrTrue = brTrue; BrFalse = brFalse;
			BrEq = brEq; BrNe = brNe;
			BrLt = brLt; BrLtUn = brLtUn;
			BrGt = brGt; BrGtUn = brGtUn;
			BrLe = brLe; BrLeUn = brLeUn;
			BrGe = brGe; BrGeUn = brGeUn;
		}
        /// <summary>
        ///     Adds a binary operator.
        /// </summary>
        /// <param name="operator">The operator's operation code.</param>
        /// <param name="supportedTypes">The supported types on both sides of the operator.</param>
        /// <param name="func">The logic to perform this operator.</param>
        /// <param name="order">The order the left/right values may appear in.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if <see cref="supportedTypes" /> or <see cref="func" /> is null.</exception>
        public void AddBinaryOperator(OpCode @operator, Type[] supportedTypes,
            Func<EarleValue, EarleValue, EarleValue> func,
            EarleOperatorParamOrder order = EarleOperatorParamOrder.Normal)
        {
            if (supportedTypes == null) throw new ArgumentNullException(nameof(supportedTypes));
            if (func == null) throw new ArgumentNullException(nameof(func));

            AddBinaryOperator(@operator, supportedTypes, supportedTypes, func, order);
        }
Exemple #19
0
		internal void Emit(ILGenerator ilgen, OpCode opcode)
		{
			if (local == null)
			{
				// it's a temporary local that is only allocated on-demand
				local = ilgen.DeclareLocal(type);
			}
			ilgen.Emit(opcode, local);
		}
        /// <summary>
        /// Initializes a new instance of the <see cref="LdobjInstruction"/> class.
        /// </summary>
        /// <param name="opcode">The opcode.</param>
        public LdobjInstruction(OpCode opcode)
            : base(opcode, 1)
        {
            switch (opcode)
            {
                case OpCode.Ldind_i1:
                    typeRef = BuiltInSigType.SByte;
                    break;

                case OpCode.Ldind_i2:
                    typeRef = BuiltInSigType.Int16;
                    break;

                case OpCode.Ldind_i4:
                    typeRef = BuiltInSigType.Int32;
                    break;

                case OpCode.Ldind_i8:
                    typeRef = BuiltInSigType.Int64;
                    break;

                case OpCode.Ldind_u1:
                    typeRef = BuiltInSigType.Byte;
                    break;

                case OpCode.Ldind_u2:
                    typeRef = BuiltInSigType.UInt16;
                    break;

                case OpCode.Ldind_u4:
                    typeRef = BuiltInSigType.UInt32;
                    break;

                case OpCode.Ldind_i:
                    typeRef = BuiltInSigType.IntPtr;
                    break;

                case OpCode.Ldind_r4:
                    typeRef = BuiltInSigType.Single;
                    break;

                case OpCode.Ldind_r8:
                    typeRef = BuiltInSigType.Double;
                    break;

                case OpCode.Ldind_ref: // FIXME: Really object?
                    typeRef = BuiltInSigType.Object;
                    break;

                case OpCode.Ldobj: // FIXME
                    typeRef = null; // BuiltInSigType.Object;
                    break;

                default:
                    throw new NotImplementedException();
            }
        }
Exemple #21
0
 public bool isTwoByte(OpCode code)
 {
     OpCode[] twobytes = { OpCode.ADDR, OpCode.CLEAR, OpCode.COMPR, OpCode.DIVR,
                             OpCode.MULR, OpCode.RMO, OpCode.SUBR, OpCode.TIXR,
                             OpCode.SHIFTL, OpCode.SHIFTR };
     if (twobytes.Contains<OpCode>(code))
         return true;
     else
         return false;
 }
 public int ReadFrom(byte[] buffer, int offset)
 {
     Immediate = (buffer[offset] & 0x40) != 0;
     OpCode = (OpCode)(buffer[offset] & 0x3F);
     FinalPdu = (buffer[offset + 1] & 0x80) != 0;
     TotalAhsLength = buffer[offset + 4];
     DataSegmentLength = Utilities.ToInt32BigEndian(buffer, offset + 4) & 0x00FFFFFF;
     InitiatorTaskTag = Utilities.ToUInt32BigEndian(buffer, offset + 16);
     return 48;
 }
Exemple #23
0
        public Instruction(OpCode opcode, object[] arguments)
        {
            this.opcode = opcode;
            this.arguments = arguments;
            this.size = -1;
            this.position = null;

            if(this.arguments == null)
                this.arguments = EmptyArguments;
        }
Exemple #24
0
        private Instruction GetInstruction(Expression exp, OpCode code)
        {
            Instruction inst;

            if (_indexedInsts.TryGetValue(exp, out inst))
                return inst;
            inst = new Instruction(code);
            _indexedInsts.Add(exp, inst);
            _insts.Add(inst);
            return inst;
        }
        public BinaryFragmentationFrame(OpCode opCode, byte[] data, int offset, int count, bool isFin = false, bool isMasked = true)
        {
            BufferValidator.ValidateBuffer(data, offset, count, "data");

            _opCode = opCode;
            this.Data = data;
            this.Offset = offset;
            this.Count = count;
            this.IsFin = isFin;
            this.IsMasked = isMasked;
        }
Exemple #26
0
        public Statement(string rawLine)
        {
            string[] pieces = Helpers.SplitWrtQuotes(rawLine);

            Op = (OpCode)Enum.Parse(typeof(OpCode), pieces[0]);

            for (int i = 1; i < pieces.Length; ++i)
            {
                Vars.Add(new Variable(pieces[i]));
            }
        }
 public Instruction(OpCode opc, int A, int B, int C, int sBx, uint Bx, int line, int col, string filename)
 {
   OpCode = opc;
   this.A = (byte)A;
   this.B = (byte)B;
   this.C = (byte)C;
   this.sBx = (short)sBx;
   this.Bx = (ushort)Bx;
   this.line = line;
   this.col = col;
   this.filename = filename;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="LdelemInstruction"/> class.
        /// </summary>
        /// <param name="opcode">The opcode.</param>
        public LdelemInstruction(OpCode opcode)
            : base(opcode, 1)
        {
            switch (opcode)
            {
                case OpCode.Ldelem_i1:
                    elementType = BuiltInSigType.SByte;
                    break;

                case OpCode.Ldelem_i2:
                    elementType = BuiltInSigType.Int16;
                    break;

                case OpCode.Ldelem_i4:
                    elementType = BuiltInSigType.Int32;
                    break;

                case OpCode.Ldelem_i8:
                    elementType = BuiltInSigType.Int64;
                    break;

                case OpCode.Ldelem_u1:
                    elementType = BuiltInSigType.Byte;
                    break;

                case OpCode.Ldelem_u2:
                    elementType = BuiltInSigType.UInt16;
                    break;

                case OpCode.Ldelem_u4:
                    elementType = BuiltInSigType.UInt32;
                    break;

                case OpCode.Ldelem_i:
                    elementType = BuiltInSigType.IntPtr;
                    break;

                case OpCode.Ldelem_r4:
                    elementType = BuiltInSigType.Single;
                    break;

                case OpCode.Ldelem_r8:
                    elementType = BuiltInSigType.Double;
                    break;

                case OpCode.Ldelem_ref: // FIXME: Really object?
                    elementType = BuiltInSigType.Object;
                    break;

                default:
                    throw new NotImplementedException();
            }
        }
        /// <summary>
        ///     Adds a binary operator.
        /// </summary>
        /// <param name="operator">The operator's operation code.</param>
        /// <param name="supportedLeftTypes">The supported types on the left side of the operator.</param>
        /// <param name="supportedRightTypes">The supported types on the right side of the operator.</param>
        /// <param name="func">The logic to perform this operator.</param>
        /// <param name="order">The order the left/right values may appear in.</param>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <see cref="supportedLeftTypes" />,
        ///     <see cref="supportedRightTypes" /> or <see cref="func" /> is null.
        /// </exception>
        public void AddBinaryOperator(OpCode @operator, Type[] supportedLeftTypes, Type[] supportedRightTypes,
            Func<EarleValue, EarleValue, EarleValue> func,
            EarleOperatorParamOrder order = EarleOperatorParamOrder.Normal)
        {
            if (supportedLeftTypes == null) throw new ArgumentNullException(nameof(supportedLeftTypes));
            if (supportedRightTypes == null) throw new ArgumentNullException(nameof(supportedRightTypes));
            if (func == null) throw new ArgumentNullException(nameof(func));

            foreach (var left in supportedLeftTypes)
                foreach (var right in supportedRightTypes)
                    AddBinaryOperator(@operator, left, right, func, order);
        }
 public ScriptElement(Byte[] data)
 {
     this.data = data;
     if (0x01 <= data.Length && data.Length <= 0x4b)
         this.opCode = (OpCode)data.Length;
     else if (data.Length <= Byte.MaxValue)
         this.opCode = OpCode.OP_PUSHDATA1;
     else if (data.Length <= UInt16.MaxValue)
         this.opCode = OpCode.OP_PUSHDATA2;
     else
         this.opCode = OpCode.OP_PUSHDATA4;
 }
Exemple #31
0
 internal ILInstruction(int offset, OpCode opCode)
 {
     Offset = offset;
     OpCode = opCode;
 }
Exemple #32
0
 public CodeMatcher InsertBranchAndAdvance(OpCode opcode, int destination)
 {
     InsertBranch(opcode, destination);
     Pos++;
     return(this);
 }
Exemple #33
0
        /// <summary>
        /// Disassembles one instruction
        /// </summary>
        /// <param name="in_address">Memory address for disassembly start</param>
        /// <returns>Disassembled instruction</returns>
        public Z80DisassemblerInstruction Disassemble(ushort in_address)
        {
            OpCode opcode = null;
            Z80DisassemblerInstruction dissassembled_instruction = new Z80DisassemblerInstruction();

            dissassembled_instruction.Address        = in_address;
            dissassembled_instruction.NumericOperand = 0;

            ushort start_address = in_address;

            bool have_displacement = false;
            byte displacement      = 0;

            ushort jump_address      = 0;
            bool   have_jump_address = false;

            have_displacement = false;
            displacement      = 0;

            byte opc = ReadByte(in_address++);

            switch (opc)
            {
            case 0xDD:
            case 0xFD:
                byte next = ReadByte(in_address++);
                if ((next | 0x20) == 0xFD || next == 0xED)
                {
                    dissassembled_instruction.Asm      = "NOP*";
                    dissassembled_instruction.TStates  = 4;
                    dissassembled_instruction.TStates2 = 0;
                    opcode = null;
                }
                else if (next == 0xCB)
                {
                    displacement = ReadByte(in_address++);
                    next         = ReadByte(in_address++);

                    opcode            = (opc == 0xDD) ? dasm_ddcb[next] : dasm_fdcb[next];
                    have_displacement = true;
                }
                else
                {
                    opcode = (opc == 0xDD) ? dasm_dd[next] : dasm_fd[next];
                    if (opcode.Mnemonic == null)                             //mirrored instructions
                    {
                        opcode = dasm_base[next];
                        dissassembled_instruction.TStates  = 4;
                        dissassembled_instruction.TStates2 = 4;
                    }
                }
                break;

            case 0xED:
                next   = ReadByte(in_address++);
                opcode = dasm_ed[next];
                if (opcode.Mnemonic == null)
                {
                    dissassembled_instruction.Asm     = "NOP*";
                    dissassembled_instruction.TStates = 8;
                    opcode = null;
                }
                break;

            case 0xCB:
                next   = ReadByte(in_address++);
                opcode = dasm_cb[next];
                break;

            default:
                opcode = dasm_base[opc];
                break;
            }

            if (opcode != null)
            {
                dissassembled_instruction.OpCode = opcode;

                byte operand_index = 1;
                var  sb            = new StringBuilder();

                foreach (var ch in opcode.Mnemonic)
                {
                    switch (ch)
                    {
                    case '@':
                    {
                        var    lo  = ReadByte(in_address++);
                        var    hi  = ReadByte(in_address++);
                        ushort val = (ushort)(lo + hi * 0x100);

                        if ((opcode.Flags & (OpCodeFlags.RefAddr | OpCodeFlags.Jumps)) != 0)
                        {
                            sb.Append(FormatAddr(val));
                        }
                        else
                        {
                            sb.Append(FormatWord(val));
                        }

                        dissassembled_instruction.WordVal = val;

                        jump_address      = val;
                        have_jump_address = true;

                        dissassembled_instruction.NumericOperand = operand_index;
                        break;
                    }

                    case '$':
                    case '%':
                    {
                        if (!have_displacement)
                        {
                            displacement = ReadByte(in_address++);
                        }

                        var disp = (displacement & 0x80) != 0 ? -(((~displacement) & 0x7f) + 1) : displacement;

                        if (ShowRelativeOffsets)
                        {
                            if (disp > 0)
                            {
                                dissassembled_instruction.Comment = string.Format("+{0}", disp);
                            }
                            else
                            {
                                dissassembled_instruction.Comment = string.Format("{0}", disp);
                            }
                        }

                        if (ch == '$')
                        {
                            if ((sbyte)disp < 0 && sb[sb.Length - 1] == '+')
                            {
                                sb.Length--;
                            }
                            sb.Append(FormatSignedByte((sbyte)disp));
                        }
                        else
                        {
                            jump_address      = (ushort)(in_address + disp);
                            have_jump_address = true;
                            sb.Append(FormatAddr(jump_address));
                        }

                        dissassembled_instruction.NumericOperand = operand_index;

                        break;
                    }

                    case '#':
                    {
                        var lo = ReadByte(in_address++);
                        sb.Append(FormatByte(lo));
                        if (lo >= 0x20 && lo <= 0x7f)
                        {
                            dissassembled_instruction.Comment = string.Format("'{0}'", (char)lo);
                        }
                        dissassembled_instruction.ByteVal = lo;

                        dissassembled_instruction.NumericOperand = operand_index;

                        break;
                    }

                    default:
                        if (ch == ',')
                        {
                            operand_index++;
                        }

                        sb.Append(ch);
                        break;
                    }
                }

                dissassembled_instruction.Asm = sb.ToString();

                dissassembled_instruction.TStates  += opcode.TStates;
                dissassembled_instruction.TStates2 += opcode.TStates2;

                // Return continue address
                if ((opcode.Flags & OpCodeFlags.Continues) != 0)
                {
                    dissassembled_instruction.NextAddress1 = in_address;
                }

                // Return jump target address (if have it)
                if ((opcode.Flags & OpCodeFlags.Jumps) != 0 && have_jump_address)
                {
                    dissassembled_instruction.NextAaddress2 = jump_address;
                }
            }
            else
            {
                dissassembled_instruction.NextAddress1 = in_address;
            }

            if (dissassembled_instruction.TStates == dissassembled_instruction.TStates2)
            {
                dissassembled_instruction.TStates2 = 0;
            }

            dissassembled_instruction.Length = (ushort)(in_address - start_address);

            return(dissassembled_instruction);
        }
Exemple #34
0
 internal static void SET_OPCODE(ref Instruction i, OpCode opcode)
 {
     i = (Instruction)(i & MASK0(SIZE_OP, POS_OP)) | (((uint)opcode << POS_OP) & MASK1(SIZE_OP, POS_OP));
 }
Exemple #35
0
        internal static int CREATE_ABx(OpCode o, int a, int bc)
        {
            int result = (int)(((int)o << POS_OP) | (a << POS_A) | (bc << POS_Bx));

            return((int)(((int)o << POS_OP) | (a << POS_A) | (bc << POS_Bx)));
        }
Exemple #36
0
 public static int testTMode(OpCode m)
 {
     return(luaP_opmodes[(int)m] & (1 << 7));
 }
Exemple #37
0
 public static OpArgMask getCMode(OpCode m)
 {
     return((OpArgMask)((luaP_opmodes[(int)m] >> 2) & 3));
 }
Exemple #38
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UnaryBranchInstruction"/> class.
 /// </summary>
 /// <param name="opcode">The opcode.</param>
 public UnaryBranchInstruction(OpCode opcode)
     : base(opcode)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="RefanyvalInstruction"/> class.
 /// </summary>
 /// <param name="opcode">The opcode.</param>
 public RefanyvalInstruction(OpCode opcode)
     : base(opcode)
 {
 }
Exemple #40
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ArglistInstruction"/> class.
 /// </summary>
 /// <param name="opcode">The opcode.</param>
 public ArglistInstruction(OpCode opcode) : base(opcode)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="BreakInstruction"/> class.
 /// </summary>
 /// <param name="opcode">The opcode.</param>
 public BreakInstruction(OpCode opcode)
     : base(opcode, 0)
 {
 }
 public static DumpInfo GetInfo(this OpCode op, bool simplify = false)
 {
     return(new DumpInfo(op, simplify));
 }
Exemple #43
0
 public CodeMatcher InsertBranch(OpCode opcode, int destination)
 {
     CreateLabelAt(destination, out var label);
     codes.Insert(Pos, new CodeInstruction(opcode, label));
     return(this);
 }
Exemple #44
0
 internal InlineI8Instruction(int offset, OpCode opCode, long value)
     : base(offset, opCode)
 {
     Value = value;
 }
Exemple #45
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LdstrInstruction"/> class.
 /// </summary>
 /// <param name="opCode">The op code.</param>
 public LdstrInstruction(OpCode opCode)
     : base(opCode)
 {
     return;
 }
Exemple #46
0
 protected ZeroParamOp(string aText, OpCode aOpCode) : base(aText)
 {
     mOpCode = aOpCode;
 }
Exemple #47
0
    private bool VerificationHelper(OpCode code,
                                    string name,
                                    StackBehaviour pop,
                                    StackBehaviour push,
                                    OperandType oprandType,
                                    OpCodeType type,
                                    int size,
                                    byte s1,
                                    byte s2,
                                    FlowControl ctrl,
                                    string errorno,
                                    string errordesp)
    {
        bool retVal = true;

        string actualName = code.Name;

        if (actualName != name)
        {
            TestLibrary.TestFramework.LogError(errorno + ".0", "Name returns wrong value for OpCode " + errordesp);
            TestLibrary.TestFramework.LogInformation("WARNING [LOCAL VARIABLE] actualName = " + actualName + ", name = " + name);
            retVal = false;
        }

        StackBehaviour actualPop = code.StackBehaviourPop;

        if (actualPop != pop)
        {
            TestLibrary.TestFramework.LogError(errorno + ".1", "StackBehaviourPop returns wrong value for OpCode " + errordesp);
            TestLibrary.TestFramework.LogInformation("WARNING [LOCAL VARIABLE] actualPop = " + actualPop + ", pop = " + pop);
            retVal = false;
        }

        StackBehaviour actualPush = code.StackBehaviourPush;

        if (actualPush != push)
        {
            TestLibrary.TestFramework.LogError(errorno + ".2", "StackBehaviourPush returns wrong value for OpCode " + errordesp);
            TestLibrary.TestFramework.LogInformation("WARNING [LOCAL VARIABLE] actualPush = " + actualPush + ", push = " + push);
            retVal = false;
        }

        OperandType actualOperandType = code.OperandType;

        if (actualOperandType != oprandType)
        {
            TestLibrary.TestFramework.LogError(errorno + ".3", "OperandType returns wrong value for OpCode " + errordesp);
            TestLibrary.TestFramework.LogInformation("WARNING [LOCAL VARIABLE] actualOperandType = " + actualOperandType + ", oprandType = " + oprandType);
            retVal = false;
        }

        OpCodeType actualOpCodeType = code.OpCodeType;

        if (actualOpCodeType != type)
        {
            TestLibrary.TestFramework.LogError(errorno + ".4", "OpCodeType returns wrong value for OpCode " + errordesp);
            TestLibrary.TestFramework.LogInformation("WARNING [LOCAL VARIABLE] actualOpCodeType = " + actualOpCodeType + ", type = " + type);
            retVal = false;
        }

        int actualSize = code.Size;

        if (actualSize != size)
        {
            TestLibrary.TestFramework.LogError(errorno + ".5", "Size returns wrong value for OpCode " + errordesp);
            TestLibrary.TestFramework.LogInformation("WARNING [LOCAL VARIABLE] actualSize = " + actualSize + ", size = " + size);
            retVal = false;
        }

        short expectedValue = 0;

        if (size == 2)
        {
            expectedValue = (short)(s1 << 8 | s2);
        }
        else
        {
            expectedValue = (short)s2;
        }

        short actualValue = code.Value;

        if (actualValue != expectedValue)
        {
            TestLibrary.TestFramework.LogError(errorno + ".6", "Value returns wrong value for OpCode " + errordesp);
            TestLibrary.TestFramework.LogInformation("WARNING [LOCAL VARIABLE] actualValue = " + actualValue + ", s1 = " + s1 + ", s2 = " + s2 + ", expectedValue = " + expectedValue);
            retVal = false;
        }

        FlowControl actualCtrl = code.FlowControl;

        if (actualCtrl != ctrl)
        {
            TestLibrary.TestFramework.LogError(errorno + ".7", "FlowControl returns wrong value for OpCode " + errordesp);
            TestLibrary.TestFramework.LogInformation("WARNING [LOCAL VARIABLE] actualCtrl = " + actualCtrl + ", ctrl = " + ctrl);
            retVal = false;
        }
        return(retVal);
    }
Exemple #48
0
 record Instruction(OpCode OpCode, ParameterMode[] ParameterModes)
 {
Exemple #49
0
 public static OpMode getOpMode(OpCode m)
 {
     return((OpMode)(luaP_opmodes[(int)m] & 3));
 }
Exemple #50
0
 private Operator(bool isLogical, OpCode opCode, OpCode opCodeUn, OpCode opCodeChk, OpCode opCodeChkUn, bool invertOpResult, BranchInstruction branchOp, string methodName, IMemberInfo[] standardCandidates, SpecificOperatorProvider[] standardTemplates)
 {
     OpCode              = opCode;
     OpCodeUn            = opCodeUn;
     OpCodeChk           = opCodeChk;
     OpCodeChkUn         = opCodeChkUn;
     InvertOpResult      = invertOpResult;
     BranchOp            = branchOp;
     MethodName          = methodName;
     _standardCandidates = standardCandidates ?? new IMemberInfo[0];
     _standardTemplates  = standardTemplates;
     IsLogical           = isLogical;
 }
Exemple #51
0
 internal static int CREATE_ABC(OpCode o, int a, int b, int c)
 {
     return((int)(((int)o << POS_OP) | (a << POS_A) | (b << POS_B) | (c << POS_C)));
 }
Exemple #52
0
        private void ValidateJump(LabelScopeInfo reference)
        {
            // Assume we can do a ret/branch
            _opCode = _canReturn ? OpCodes.Ret : OpCodes.Br;

            // look for a simple jump out
            for (LabelScopeInfo j = reference; j != null; j = j.Parent)
            {
                if (_definitions.Contains(j))
                {
                    // found it, jump is valid!
                    return;
                }
                if (j.Kind == LabelScopeKind.Finally ||
                    j.Kind == LabelScopeKind.Filter)
                {
                    break;
                }
                if (j.Kind == LabelScopeKind.Try ||
                    j.Kind == LabelScopeKind.Catch)
                {
                    _opCode = OpCodes.Leave;
                }
            }

            _acrossBlockJump = true;
            if (_node != null && _node.Type != typeof(void))
            {
                throw Error.NonLocalJumpWithValue(_node.Name);
            }

            if (_definitions.Count > 1)
            {
                throw Error.AmbiguousJump(_node.Name);
            }

            // We didn't find an outward jump. Look for a jump across blocks
            LabelScopeInfo def    = _definitions.First();
            LabelScopeInfo common = Helpers.CommonNode(def, reference, b => b.Parent);

            // Assume we can do a ret/branch
            _opCode = _canReturn ? OpCodes.Ret : OpCodes.Br;

            // Validate that we aren't jumping across a finally
            for (LabelScopeInfo j = reference; j != common; j = j.Parent)
            {
                if (j.Kind == LabelScopeKind.Finally)
                {
                    throw Error.ControlCannotLeaveFinally();
                }
                if (j.Kind == LabelScopeKind.Filter)
                {
                    throw Error.ControlCannotLeaveFilterTest();
                }
                if (j.Kind == LabelScopeKind.Try ||
                    j.Kind == LabelScopeKind.Catch)
                {
                    _opCode = OpCodes.Leave;
                }
            }

            // Valdiate that we aren't jumping into a catch or an expression
            for (LabelScopeInfo j = def; j != common; j = j.Parent)
            {
                if (!j.CanJumpInto)
                {
                    if (j.Kind == LabelScopeKind.Expression)
                    {
                        throw Error.ControlCannotEnterExpression();
                    }
                    else
                    {
                        throw Error.ControlCannotEnterTry();
                    }
                }
            }
        }
Exemple #53
0
 internal static void SET_OPCODE(InstructionPtr i, OpCode opcode)
 {
     SET_OPCODE(ref i.codes[i.pc], opcode);
 }
 public CompilerInstruction(OpCode inOperation, int?inArg = null)
 {
     Operation = inOperation;
     IntArg    = inArg;
 }
 static ILPattern Field(OpCode opcode)
 {
     return(new FieldPattern(ILPattern.OpCode(opcode)));
 }
Exemple #56
0
 /// <summary>Creates a new CodeInstruction with a given opcode and optional operand</summary>
 /// <param name="opcode">The code</param>
 /// <param name="operand">The operand</param>
 ///
 public CodeInstruction(OpCode opcode, object operand = null)
 {
     this.opcode  = opcode;
     this.operand = operand;
 }
Exemple #57
0
 private static void ResolveField(FieldInfo field, List <FieldBuilder> fields, ILGenerator generator, OpCode code)
 {
     foreach (var newField in fields)
     {
         if (newField.DeclaringType.Name == field.DeclaringType.Name && newField.Name == field.Name)
         {
             // We found a replacement field reference
             generator.Emit(code, newField);
             return;
         }
     }
     // Generate the exact field reference
     generator.Emit(code, field);
 }
    private static void SetValueOfRefProperty <T>(PropertyInfo p, object instance, Type targetType, Type funcType, OpCode stCode, T value)
    {
        var getter = p.GetGetMethod();
        var name   = $"TempSetter{getter.Name}";
        var dm     = new DynamicMethod(
            name,
            MethodAttributes.Public | MethodAttributes.Static,
            CallingConventions.Standard,
            typeof(void),
            new[] { targetType, typeof(T) },
            targetType, true);

        var ilg = dm.GetILGenerator();

        ilg.Emit(OpCodes.Ldarg_0);
        ilg.Emit(OpCodes.Call, getter);
        ilg.Emit(OpCodes.Ldarg_1);
        ilg.Emit(stCode);
        ilg.Emit(OpCodes.Ret);

        var del = dm.CreateDelegate(funcType);

        del.DynamicInvoke(instance, value);
    }
Exemple #59
0
 public CodeMatcher SetOpcodeAndAdvance(OpCode opcode)
 {
     Opcode = opcode;
     Pos++;
     return(this);
 }
Exemple #60
0
        ILInstruction Next()
        {
            Int32  offset = m_position;
            OpCode opCode = OpCodes.Nop;
            Int32  token  = 0;

            // read first 1 or 2 bytes as opCode
            Byte code = ReadByte();

            if (code != 0xFE)
            {
                opCode = s_OneByteOpCodes[code];
            }
            else
            {
                code   = ReadByte();
                opCode = s_TwoByteOpCodes[code];
            }

            switch (opCode.OperandType)
            {
            case OperandType.InlineNone:
                return(new InlineNoneInstruction(offset, opCode));

            //The operand is an 8-bit integer branch target.
            case OperandType.ShortInlineBrTarget:
                SByte shortDelta = ReadSByte();
                return(new ShortInlineBrTargetInstruction(offset, opCode, shortDelta));

            //The operand is a 32-bit integer branch target.
            case OperandType.InlineBrTarget:
                Int32 delta = ReadInt32();
                return(new InlineBrTargetInstruction(offset, opCode, delta));

            //The operand is an 8-bit integer: 001F  ldc.i4.s, FE12  unaligned.
            case OperandType.ShortInlineI:
                SByte int8 = ReadSByte();
                return(new ShortInlineIInstruction(offset, opCode, int8));

            //The operand is a 32-bit integer.
            case OperandType.InlineI:
                Int32 int32 = ReadInt32();
                return(new InlineIInstruction(offset, opCode, int32));

            //The operand is a 64-bit integer.
            case OperandType.InlineI8:
                Int64 int64 = ReadInt64();
                return(new InlineI8Instruction(offset, opCode, int64));

            //The operand is a 32-bit IEEE floating point number.
            case OperandType.ShortInlineR:
                Single float32 = ReadSingle();
                return(new ShortInlineRInstruction(offset, opCode, float32));

            //The operand is a 64-bit IEEE floating point number.
            case OperandType.InlineR:
                Double float64 = ReadDouble();
                return(new InlineRInstruction(offset, opCode, float64));

            //The operand is an 8-bit integer containing the ordinal of a local variable or an argument
            case OperandType.ShortInlineVar:
                Byte index8 = ReadByte();
                return(new ShortInlineVarInstruction(offset, opCode, index8));

            //The operand is 16-bit integer containing the ordinal of a local variable or an argument.
            case OperandType.InlineVar:
                UInt16 index16 = ReadUInt16();
                return(new InlineVarInstruction(offset, opCode, index16));

            //The operand is a 32-bit metadata string token.
            case OperandType.InlineString:
                token = ReadInt32();
                return(new InlineStringInstruction(offset, opCode, token, m_resolver));

            //The operand is a 32-bit metadata signature token.
            case OperandType.InlineSig:
                token = ReadInt32();
                return(new InlineSigInstruction(offset, opCode, token, m_resolver));

            //The operand is a 32-bit metadata token.
            case OperandType.InlineMethod:
                token = ReadInt32();
                return(new InlineMethodInstruction(offset, opCode, token, m_resolver));

            //The operand is a 32-bit metadata token.
            case OperandType.InlineField:
                token = ReadInt32();
                return(new InlineFieldInstruction(m_resolver, offset, opCode, token));

            //The operand is a 32-bit metadata token.
            case OperandType.InlineType:
                token = ReadInt32();
                return(new InlineTypeInstruction(offset, opCode, token, m_resolver));

            //The operand is a FieldRef, MethodRef, or TypeRef token.
            case OperandType.InlineTok:
                token = ReadInt32();
                return(new InlineTokInstruction(offset, opCode, token, m_resolver));

            //The operand is the 32-bit integer argument to a switch instruction.
            case OperandType.InlineSwitch:
                Int32   cases  = ReadInt32();
                Int32[] deltas = new Int32[cases];
                for (Int32 i = 0; i < cases; i++)
                {
                    deltas[i] = ReadInt32();
                }
                return(new InlineSwitchInstruction(offset, opCode, deltas));

            default:
                throw new BadImageFormatException("unexpected OperandType " + opCode.OperandType);
            }
        }