Пример #1
0
 public override void Render(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     if (!opcodes.TryGetValue(Opcode, out var sOpcode))
     {
         sOpcode = Opcode.ToString();
     }
     writer.WriteOpcode(sOpcode);
     if (op1 == null)
     {
         return;
     }
     writer.Tab();
     Render(op1, writer, options);
     if (op2 == null)
     {
         return;
     }
     writer.WriteChar(',');
     Render(op2, writer, options);
     if (op3 == null)
     {
         return;
     }
     writer.WriteChar(',');
     Render(op3, writer, options);
 }
Пример #2
0
        public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            var sep    = "{";
            int mask   = 1;
            int iStart = -1;

            for (int i = 0; i < 33; ++i, mask <<= 1)
            {
                if ((bitmask & mask) != 0)
                {
                    if (iStart < 0)
                    {
                        // Start a range
                        writer.WriteString(sep);
                        sep    = ",";
                        iStart = i;
                        writer.WriteString(registers[i].Name);
                    }
                }
                else
                {
                    if (0 <= iStart && iStart < i - 1)
                    {
                        writer.WriteChar('-');
                        writer.WriteString(registers[i - 1].Name);
                    }
                    iStart = -1;
                }
            }
            writer.WriteChar('}');
        }
Пример #3
0
        public override void Render(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            string name;

            if (!instrNames.TryGetValue(opcode, out name))
            {
                name = opcode.ToString();
            }
            writer.WriteOpcode(name);

            if (op1 != null)
            {
                writer.Tab();
                op1.Write(writer, options);
                if (op2 != null)
                {
                    writer.WriteChar(',');
                    op2.Write(writer, options);
                    if (op3 != null)
                    {
                        writer.WriteChar(',');
                        op3.Write(writer, options);
                    }
                }
            }
        }
Пример #4
0
        public override void Render(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            var op = string.Format("{0}{1}",
                                   opcode,
                                   setsCR0 ? "." : "");

            writer.WriteOpcode(op);
            if (op1 != null)
            {
                writer.Tab();
                op1.Write(writer, options);
                if (op2 != null)
                {
                    writer.WriteChar(',');
                    op2.Write(writer, options);
                    if (op3 != null)
                    {
                        writer.WriteChar(',');
                        op3.Write(writer, options);
                        if (op4 != null)
                        {
                            writer.WriteChar(',');
                            op4.Write(writer, options);
                            if (op5 != null)
                            {
                                writer.WriteChar(',');
                                op5.Write(writer, options);
                            }
                        }
                    }
                }
            }
        }
Пример #5
0
 public override void Render(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     if (!opcodeNames.TryGetValue(opcode, out string name))
     {
         name = opcode.ToString();
     }
     writer.WriteOpcode(name);
     if (op1 == null)
     {
         return;
     }
     writer.Tab();
     WriteOp(op1, writer);
     if (op2 == null)
     {
         return;
     }
     writer.WriteChar(',');
     WriteOp(op2, writer);
     if (op3 == null)
     {
         return;
     }
     writer.WriteChar(',');
     WriteOp(op3, writer);
     if (op4 == null)
     {
         return;
     }
     writer.WriteChar(',');
     WriteOp(op4, writer);
 }
Пример #6
0
        public override void Render(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            int iOp = WriteOpcode(writer);

            if (ops == null || ops.Length == 0)
            {
                return;
            }
            writer.Tab();
            RenderOperand(ops[iOp++], writer, options);
            for (; iOp < ops.Length; ++iOp)
            {
                var op = ops[iOp];
                writer.WriteChar(',');
                RenderOperand(op, writer, options);
            }
            if (this.shiftCode == Opcode.Invalid)
            {
                return;
            }
            if (shiftCode == Opcode.lsl && (shiftAmount is ImmediateOperand imm && imm.Value.IsIntegerZero))
            {
                return;
            }
            writer.WriteChar(',');
            writer.WriteOpcode(shiftCode.ToString());
            writer.WriteChar(' ');
            RenderOperand(shiftAmount, writer, options);
        }
Пример #7
0
        public override void Render(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            if (repPrefix == 3)
            {
                writer.WriteMnemonic("rep");
                writer.WriteChar(' ');
            }
            else if (repPrefix == 2)
            {
                writer.WriteMnemonic("repne");
                writer.WriteChar(' ');
            }

            string s = Mnemonic.ToString();

            switch (Mnemonic)
            {
            case Mnemonic.cwd:
                if (dataWidth == PrimitiveType.Word32)
                {
                    s = "cdq";
                }
                break;

            case Mnemonic.cbw:
                if (dataWidth == PrimitiveType.Word32)
                {
                    s = "cwde";
                }
                break;

            case Mnemonic.ins:
            case Mnemonic.outs:
            case Mnemonic.movs:
            case Mnemonic.cmps:
            case Mnemonic.stos:
            case Mnemonic.lods:
            case Mnemonic.scas:
                switch (dataWidth.Size)
                {
                case 1: s += 'b'; break;

                case 2: s += 'w'; break;

                case 4: s += 'd'; break;

                case 8: s += 'q'; break;

                default: throw new ArgumentOutOfRangeException();
                }
                break;
            }
            writer.WriteMnemonic(s);

            if (NeedsExplicitMemorySize())
            {
                options |= MachineInstructionWriterOptions.ExplicitOperandSize;
            }
            RenderOperands(writer, options);
        }
Пример #8
0
 public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     writer.WriteChar('{');
     writer.WriteString(BitOffset.ToString());
     writer.WriteChar(':');
     writer.WriteString(BitWidth.ToString());
     writer.WriteChar('}');
 }
Пример #9
0
 public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     writer.WriteChar('(');
     writer.WriteString(Base.Name);
     if (Offset > 0)
     {
         writer.WriteFormat("+{0}", Offset);
     }
     else if (Offset < 0)
     {
         writer.WriteFormat("-{0}", -Offset);
     }
     writer.WriteChar(')');
 }
Пример #10
0
 public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     if (Deferred)
     {
         writer.WriteChar('*');
     }
     if (Base == null)
     {
         writer.WriteChar('$');
         writer.WriteFormat("0x{0:X}", (uint)Offset);
     }
     else
     {
         writer.WriteFormat("{0}(%{1})", Offset, Base);
     }
 }
Пример #11
0
 public override void Render(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     if (code == Opcode.illegal && op1 != null && writer.Platform != null)
     {
         var imm = op1 as M68kImmediateOperand;
         // MacOS uses invalid opcodes to invoke Macintosh Toolbox services.
         // We may have to generalize the Platform API to allow specifying
         // the opcode of the invoking instruction, to disambiguate from
         // "legitimate" TRAP calls.
         var svc = writer.Platform.FindService((int)imm.Constant.ToUInt32(), null);
         if (svc != null)
         {
             writer.WriteString(svc.Name);
             return;
         }
     }
     if (dataWidth != null)
     {
         writer.WriteOpcode(string.Format("{0}{1}", code, DataSizeSuffix(dataWidth)));
     }
     else
     {
         writer.WriteOpcode(code.ToString());
     }
     writer.Tab();
     if (op1 != null)
     {
         WriteOperand(op1, writer, options);
         if (op2 != null)
         {
             writer.WriteChar(',');
             WriteOperand(op2, writer, options);
         }
     }
 }
Пример #12
0
        protected override void RenderOperand(MachineOperand op, MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            switch (op)
            {
            case ImmediateOperand _:
                writer.WriteChar('#');
                op.Write(writer, options);
                return;

            case MemoryOperand mop when mop.Base == Registers.pc:
                var addr = this.Address + this.Length;
                if (mop.Offset != null)
                {
                    addr += mop.Offset.ToInt32();
                }
                if ((options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
                {
                    writer.WriteAddress(addr.ToString(), addr);
                    writer.AddAnnotation(op.ToString());
                }
                else
                {
                    op.Write(writer, options);
                    writer.AddAnnotation(addr.ToString());
                }
                return;

            default:
                op.Write(writer, options);
                return;
            }
        }
Пример #13
0
 public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     if (PreDecrement)
     {
         writer.WriteChar('-');
     }
     writer.WriteString(Base.Name);
     if (PostIncrement)
     {
         writer.WriteChar('+');
     }
     else if (Displacement != 0)
     {
         writer.WriteFormat("+{0:X2}", Displacement);
     }
 }
Пример #14
0
        public override void Render(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            if (repPrefix == 3)
            {
                writer.WriteMnemonic("rep");
                writer.WriteChar(' ');
            }
            else if (repPrefix == 2)
            {
                writer.WriteMnemonic("repne");
                writer.WriteChar(' ');
            }

            var s = new StringBuilder(Mnemonic.ToString());

            switch (Mnemonic)
            {
            case Mnemonic.ins:
            case Mnemonic.outs:
            case Mnemonic.movs:
            case Mnemonic.cmps:
            case Mnemonic.stos:
            case Mnemonic.lods:
            case Mnemonic.scas:
                switch (dataWidth.Size)
                {
                case 1: s.Append('b'); break;

                case 2: s.Append('w'); break;

                case 4: s.Append('d'); break;

                case 8: s.Append('q'); break;

                default: throw new ArgumentOutOfRangeException();
                }
                break;
            }
            writer.WriteMnemonic(s.ToString());

            if (NeedsExplicitMemorySize())
            {
                options |= MachineInstructionWriterOptions.ExplicitOperandSize;
            }
            RenderOperands(writer, options);
        }
Пример #15
0
        public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            writer.WriteChar('{');
            var sep = "";

            foreach (var reg in GetRegisters())
            {
                writer.WriteString(sep);
                sep = ",";
                VectorRegisterOperand.WriteName(Width.BitSize, reg, ElementType, Index, writer);
            }
            writer.WriteChar('}');
            if (Index >= 0)
            {
                writer.WriteFormat("[{0}]", Index);
            }
        }
Пример #16
0
 public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     if (Address == null)
     {
         writer.WriteChar('*');
         writer.WriteString(Register.Name);
     }
     else
     {
         writer.WriteAddress("@" + Address, Address);
         if (Register != null)
         {
             writer.WriteChar('(');
             writer.WriteString(Register.Name);
             writer.WriteChar(')');
         }
     }
 }
Пример #17
0
 public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     writer.WriteChar('(');
     if (Base != null)
     {
         if (Increment < 0)
         {
             writer.WriteFormat("{0}:-", -Increment);
         }
         writer.WriteString(Base.Name);
         if (Index != null)
         {
             writer.WriteChar('+');
             writer.WriteString(Index.Name);
         }
         else if (Offset != null)
         {
             int off = Offset.ToInt32();
             int absOff;
             if (off < 0)
             {
                 writer.WriteChar('-');
                 absOff = -off;
             }
             else
             {
                 writer.WriteChar('+');
                 absOff = off;
             }
             writer.WriteString("0x");
             writer.WriteFormat(OffsetFormat(off), absOff);
         }
         if (Increment > 0)
         {
             writer.WriteFormat("+:{0}", Increment);
         }
     }
     else
     {
         var addr = Address.Ptr16(Offset.ToUInt16());
         writer.WriteAddress(addr.ToString(), addr);
     }
     writer.WriteChar(')');
 }
Пример #18
0
 public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     if (Base != null)
     {
         if (Indirect)
         {
             writer.WriteChar('[');
         }
         if (PreIncrement)
         {
             writer.WriteFormat("${0:X2},{1}{2}",
                                Math.Abs(Offset.Value),
                                Offset.Value > 0 ? "+" : "-",
                                Base.Name);
         }
         else if (PostIncrement)
         {
             writer.WriteFormat("${0:X2},{1}{2}",
                                Math.Abs(Offset.Value),
                                Base.Name,
                                Offset.Value > 0 ? "+" : "-");
         }
         else if (Offset != null)
         {
             writer.WriteFormat("${0:X4},{1}", Offset.Value, Base.Name);
         }
         else
         {
             writer.WriteFormat("{0},{1}", Index.Name, Base.Name);
         }
         if (Indirect)
         {
             writer.WriteChar(']');
         }
         return;
     }
     else if (Offset != null)
     {
         // Absolute address
         writer.WriteFormat("${0:X4}", (ushort)Offset.Value);
         return;
     }
     throw new System.NotImplementedException();
 }
Пример #19
0
        public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            writer.WriteChar('[');
            var sep = "";

            if (Base != null)
            {
                writer.WriteString(Base.Name);
                sep = ",";
            }
            if (Offset != 0)
            {
                writer.WriteFormat("{0}{1}", sep, Offset);
            }
            else if (Index != null)
            {
                writer.WriteFormat("{0}{1}", sep, Offset);
            }
            writer.WriteChar(']');
        }
Пример #20
0
 public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     if (Base == null)
     {
         writer.WriteFormat("&{0:X4}", (ushort)this.Offset);
         return;
     }
     if (Offset > 0)
     {
         if (Base == Registers.pc && (options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
         {
             var addr = writer.Address + 4 + Offset;
             writer.WriteAddress(addr.ToString(), addr);
         }
         else
         {
             writer.WriteFormat("{0:X4}({1})", Offset, Base.Name);
         }
     }
     else if (Offset < 0)
     {
         if (Base == Registers.pc && (options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
         {
             var addr = writer.Address + 4 + Offset;
             writer.WriteAddress(addr.ToString(), addr);
         }
         else
         {
             writer.WriteFormat("-{0:X4}({1})", -Offset, Base.Name);
         }
     }
     else
     {
         writer.WriteChar('@');
         writer.WriteString(Base.Name);
         if (PostIncrement)
         {
             writer.WriteChar('+');
         }
     }
 }
Пример #21
0
        private void Render(MachineOperand op, MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            switch (op)
            {
            case ImmediateOperand immOp:
                writer.WriteChar('#');
                immOp.Write(writer, options);
                return;

            case MemoryOperand memOp:
                if (memOp.mode == AddressingMode.PcRelativeDisplacement)
                {
                    uint uAddr = this.Address.ToUInt32();
                    if (memOp.Width.Size == 4)
                    {
                        uAddr &= ~3u;
                    }
                    uAddr += (uint)(memOp.disp + 4);
                    var addr = Core.Address.Ptr32(uAddr);
                    if ((options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
                    {
                        writer.WriteChar('(');
                        writer.WriteAddress(addr.ToString(), addr);
                        writer.WriteChar(')');
                        writer.AddAnnotation(op.ToString());
                    }
                    else
                    {
                        op.Write(writer, options);
                        writer.AddAnnotation(addr.ToString());
                    }
                    return;
                }
                goto default;

            default:
                op.Write(writer, options);
                break;
            }
        }
Пример #22
0
 public override void Render(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     writer.WriteOpcode(this.Opcode.ToString());
     if (op1 == null)
     {
         return;
     }
     writer.Tab();
     op1.Write(writer, options);
     if (op2 == null)
     {
         return;
     }
     writer.WriteChar(',');
     op2.Write(writer, options);
     if (op3 == null)
     {
         return;
     }
     writer.WriteChar(',');
     op3.Write(writer, options);
 }
Пример #23
0
 public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     if (base.Width.BitSize == 8)
     {
         writer.WriteChar('B');
     }
     else if (base.Width.BitSize == 16)
     {
         writer.WriteChar('W');
     }
     writer.WriteChar('[');
     if (PreDecrement)
     {
         writer.WriteString("--");
     }
     writer.WriteString(Base.Name);
     if (Index != null)
     {
         writer.WriteString(" + ");
         writer.WriteString(Index.Name);
     }
     else if (Offset > 0)
     {
         writer.WriteString($" + 0x{Offset:X4}");
     }
     else if (Offset < 0)
     {
         writer.WriteString($" - 0x{-Offset:X4}");
     }
     if (PostIncrement)
     {
         writer.WriteString("++");
     }
     else if (PostDecrement)
     {
         writer.WriteString("--");
     }
     writer.WriteChar(']');
 }
Пример #24
0
        public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            int offset = Offset;

            if (offset < 0)
            {
                offset = -offset;
                writer.WriteChar('-');
            }
            writer.WriteFormat("{0:X}(", offset);
            writer.WriteString(this.Base.Name);
            writer.WriteString(")");
        }
Пример #25
0
        public override void Render(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            writer.WriteOpcode(this.Opcode.ToString());
            writer.Tab();
            bool sep = false;

            foreach (var op in Operands)
            {
                if (sep)
                {
                    writer.WriteChar(',');
                }
                sep = true;
                if (op is ImmediateOperand)
                {
                    writer.WriteChar('#');
                    op.Write(writer, options);
                }
                else if (op is MemoryOperand && ((MemoryOperand)op).Base == Registers.pc)
                {
                    var addr = this.Address + (this.Length + ((MemoryOperand)op).Offset.ToInt32());
                    if ((options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
                    {
                        writer.WriteAddress(addr.ToString(), addr);
                        writer.AddAnnotation(op.ToString());
                    }
                    else
                    {
                        op.Write(writer, options);
                        writer.AddAnnotation(addr.ToString());
                    }
                }
                else
                {
                    op.Write(writer, options);
                }
            }
        }
Пример #26
0
 /// <summary>
 /// Utility function to render the operands, separated by commas.
 /// </summary>
 /// <param name="writer"></param>
 /// <param name="options"></param>
 protected void RenderOperands(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     if (Operands.Length == 0)
     {
         return;
     }
     writer.Tab();
     RenderOperand(Operands[0], writer, options);
     for (int i = 1; i < Operands.Length; ++i)
     {
         writer.WriteChar(',');
         RenderOperand(Operands[i], writer, options);
     }
 }
Пример #27
0
        public override void Render(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            writer.WriteOpcode(
                string.Format("{0}{1}",
                              Opcode.ToString(),
                              Annul ? ",a" : ""));

            if (Op1 != null)
            {
                writer.Tab();
                Write(Op1, writer, options);
                if (Op2 != null)
                {
                    writer.WriteChar(',');
                    Write(Op2, writer, options);
                    if (Op3 != null)
                    {
                        writer.WriteChar(',');
                        Write(Op3, writer, options);
                    }
                }
            }
        }
Пример #28
0
        private void Render(MachineOperand op, MachineInstructionWriter writer, MachineInstructionWriterOptions options)
        {
            var immOp = op as ImmediateOperand;

            if (immOp != null)
            {
                writer.WriteChar('#');
                immOp.Write(writer, options);
                return;
            }
            var memOp = op as MemoryOperand;

            if (memOp != null && memOp.mode == AddressingMode.PcRelativeDisplacement)
            {
                uint uAddr = this.Address.ToUInt32();
                if (memOp.Width.Size == 4)
                {
                    uAddr &= ~3u;
                }
                uAddr += (uint)(memOp.disp + 4);
                var addr = Core.Address.Ptr32(uAddr);
                if ((options & MachineInstructionWriterOptions.ResolvePcRelativeAddress) != 0)
                {
                    writer.WriteChar('(');
                    writer.WriteAddress(addr.ToString(), addr);
                    writer.WriteChar(')');
                    writer.AddAnnotation(op.ToString());
                }
                else
                {
                    op.Write(writer, options);
                    writer.AddAnnotation(addr.ToString());
                }
                return;
            }
            op.Write(writer, options);
        }
Пример #29
0
 public override void Render(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     WriteMnemonic(writer);
     if (Operands.Length == 0)
     {
         return;
     }
     writer.Tab();
     Operands[0].Write(writer, options);
     for (int i = 1; i < Operands.Length; ++i)
     {
         writer.WriteChar(',');
         Operands[i].Write(writer, options);
     }
 }
Пример #30
0
 public override void Write(MachineInstructionWriter writer, MachineInstructionWriterOptions options)
 {
     writer.WriteChar('[');
     writer.WriteString(Base.Name);
     if (Offset != null && !Offset.IsIntegerZero)
     {
         if (PostIndex)
         {
             writer.WriteChar(']');
         }
         writer.WriteChar(',');
         var off = Offset.ToInt32();
         if (off < 0)
         {
             writer.WriteFormat("#-&{0:X}", -off);
         }
         else
         {
             writer.WriteFormat("#&{0:X}", off);
         }
     }
     else if (Index != null)
     {
         writer.WriteChar(',');
         writer.WriteString(Index.Name);
         if (IndexExtend != Mnemonic.Invalid && (IndexExtend != Mnemonic.lsl || IndexShift != 0))
         {
             writer.WriteChar(',');
             writer.WriteOpcode(IndexExtend.ToString());
             if (IndexShift != 0)
             {
                 writer.WriteString($" #{IndexShift}");
             }
         }
     }
     if (!PostIndex)
     {
         writer.WriteChar(']');
     }
     if (PreIndex)
     {
         writer.WriteChar('!');
     }
 }