|
| 1 | +/* |
| 2 | + * Copyright (C) 1999-2007 John Källén. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2, or (at your option) |
| 7 | + * any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; see the file COPYING. If not, write to |
| 16 | + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 | + */ |
| 18 | + |
| 19 | +using Decompiler.Core; |
| 20 | +using Decompiler.Core.Types; |
| 21 | +using System; |
| 22 | +using System.Diagnostics; |
| 23 | +using System.IO; |
| 24 | + |
| 25 | +namespace Decompiler.Arch.Intel.Assembler |
| 26 | +{ |
| 27 | + /// <summary> |
| 28 | + /// Emits bytes into a bytestream belonging to a segment/section. |
| 29 | + /// </summary> |
| 30 | + public class Emitter |
| 31 | + { |
| 32 | + private IntelRegister segOverride; |
| 33 | + private PrimitiveType dataWidthSeg; // Default data width for this segment. |
| 34 | + private PrimitiveType addrWidthSeg; // Default address width for this segment. |
| 35 | + private PrimitiveType addrWidth; // width of the address of this opcode. |
| 36 | + |
| 37 | + private MemoryStream stmOut = new MemoryStream(); |
| 38 | + |
| 39 | + public Emitter() |
| 40 | + { |
| 41 | + segOverride = Registers.None; |
| 42 | + } |
| 43 | + |
| 44 | + public PrimitiveType AddressWidth |
| 45 | + { |
| 46 | + get { return addrWidth; } |
| 47 | + set { addrWidth = value; } |
| 48 | + } |
| 49 | + |
| 50 | + public byte [] Bytes |
| 51 | + { |
| 52 | + get { return stmOut.ToArray(); } |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + public void EmitByte(int b) |
| 58 | + { |
| 59 | + stmOut.WriteByte((byte) b); |
| 60 | + } |
| 61 | + |
| 62 | + public void EmitBytes(int b, int count) |
| 63 | + { |
| 64 | + byte by = (byte) b; |
| 65 | + for (int i = 0; i < count; ++i) |
| 66 | + { |
| 67 | + stmOut.WriteByte(by); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public void EmitDword(uint l) |
| 72 | + { |
| 73 | + stmOut.WriteByte((byte)(l)); |
| 74 | + l >>= 8; |
| 75 | + stmOut.WriteByte((byte)(l)); |
| 76 | + l >>= 8; |
| 77 | + stmOut.WriteByte((byte)(l)); |
| 78 | + l >>= 8; |
| 79 | + stmOut.WriteByte((byte)(l)); |
| 80 | + } |
| 81 | + |
| 82 | + public void EmitImmediate(Value v, PrimitiveType w) |
| 83 | + { |
| 84 | + switch (w.Size) |
| 85 | + { |
| 86 | + case 1: EmitByte(v.Byte); return; |
| 87 | + case 2: EmitWord(v.Word); return; |
| 88 | + case 4: EmitDword(v.Unsigned); return; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public void EmitInteger(PrimitiveType vt, int v) |
| 93 | + { |
| 94 | + switch (vt.Size) |
| 95 | + { |
| 96 | + case 1: EmitByte(v); return; |
| 97 | + case 2: EmitWord(v); return; |
| 98 | + case 4: EmitDword((uint) v); return; |
| 99 | + default: throw new ArgumentException(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public void EmitOpcode(int b, PrimitiveType dataWidth) |
| 104 | + { |
| 105 | + if (SegmentOverride != Registers.None) |
| 106 | + { |
| 107 | + byte bOv; |
| 108 | + if (SegmentOverride == Registers.es) bOv = 0x26; else |
| 109 | + if (SegmentOverride == Registers.cs) bOv = 0x2E; else |
| 110 | + if (SegmentOverride == Registers.ss) bOv = 0x36; else |
| 111 | + if (SegmentOverride == Registers.ds) bOv = 0x3E; else |
| 112 | + if (SegmentOverride == Registers.fs) bOv = 0x64; else |
| 113 | + if (SegmentOverride == Registers.gs) bOv = 0x65; else |
| 114 | + throw new ArgumentOutOfRangeException("Invalid segment register: " + SegmentOverride); |
| 115 | + EmitByte(bOv); |
| 116 | + SegmentOverride = Registers.None; |
| 117 | + } |
| 118 | + if (IsDataWidthOverridden(dataWidth)) |
| 119 | + { |
| 120 | + EmitByte(0x66); |
| 121 | + } |
| 122 | + if (AddressWidth != null && AddressWidth != SegmentAddressWidth) |
| 123 | + { |
| 124 | + EmitByte(0x67); |
| 125 | + } |
| 126 | + EmitByte(b); |
| 127 | + } |
| 128 | + |
| 129 | + public void EmitString(string pstr) |
| 130 | + { |
| 131 | + for (int i = 0; i != pstr.Length; ++i) |
| 132 | + { |
| 133 | + EmitByte(pstr[i]); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public void EmitWord(int s) |
| 138 | + { |
| 139 | + stmOut.WriteByte((byte)(s & 0xFF)); |
| 140 | + stmOut.WriteByte((byte)(s >> 8)); |
| 141 | + } |
| 142 | + |
| 143 | + private bool IsDataWidthOverridden(PrimitiveType dataWidth) |
| 144 | + { |
| 145 | + return dataWidth != null && |
| 146 | + dataWidth.Domain != Domain.Real && |
| 147 | + dataWidth.Size != 1 && |
| 148 | + dataWidth != SegmentDataWidth; |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + public int Length |
| 153 | + { |
| 154 | + get { return (int) stmOut.Length; } |
| 155 | + } |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// Patches a value by fetching it from the stream and adding an offset. |
| 159 | + /// </summary> |
| 160 | + /// <param name="offsetPatch"></param> |
| 161 | + /// <param name="offsetRef"></param> |
| 162 | + /// <param name="width"></param> |
| 163 | + public void Patch(int offsetPatch, int offsetRef, PrimitiveType width) |
| 164 | + { |
| 165 | + Debug.Assert(offsetPatch < stmOut.Length); |
| 166 | + long posOrig = stmOut.Position; |
| 167 | + stmOut.Position = offsetPatch; |
| 168 | + int patchVal; |
| 169 | + switch (width.Size) |
| 170 | + { |
| 171 | + default: |
| 172 | + throw new ApplicationException("unexpected"); |
| 173 | + case 1: |
| 174 | + patchVal = stmOut.ReadByte() + offsetRef; |
| 175 | + stmOut.Position = offsetPatch; |
| 176 | + stmOut.WriteByte((byte) patchVal); |
| 177 | + break; |
| 178 | + case 2: |
| 179 | + patchVal = stmOut.ReadByte(); |
| 180 | + patchVal |= stmOut.ReadByte() << 8; |
| 181 | + patchVal += offsetRef; |
| 182 | + stmOut.Position = offsetPatch; |
| 183 | + stmOut.WriteByte((byte) patchVal); |
| 184 | + stmOut.WriteByte((byte) (patchVal >> 8)); |
| 185 | + break; |
| 186 | + case 4: |
| 187 | + patchVal = stmOut.ReadByte(); |
| 188 | + patchVal |= stmOut.ReadByte() << 8; |
| 189 | + patchVal |= stmOut.ReadByte() << 16; |
| 190 | + patchVal |= stmOut.ReadByte() << 24; |
| 191 | + patchVal += offsetRef; |
| 192 | + stmOut.Position = offsetPatch; |
| 193 | + stmOut.WriteByte((byte) patchVal); |
| 194 | + stmOut.WriteByte((byte) (patchVal >>= 8)); |
| 195 | + stmOut.WriteByte((byte) (patchVal >>= 8)); |
| 196 | + stmOut.WriteByte((byte) (patchVal >>= 8)); |
| 197 | + break; |
| 198 | + } |
| 199 | + stmOut.Position = posOrig; |
| 200 | + } |
| 201 | + |
| 202 | + |
| 203 | + public int Position |
| 204 | + { |
| 205 | + get { return (int) stmOut.Position; } |
| 206 | + } |
| 207 | + |
| 208 | + public PrimitiveType SegmentAddressWidth |
| 209 | + { |
| 210 | + get { return addrWidthSeg; } |
| 211 | + set { addrWidthSeg = value; } |
| 212 | + } |
| 213 | + |
| 214 | + public PrimitiveType SegmentDataWidth |
| 215 | + { |
| 216 | + get { return dataWidthSeg; } |
| 217 | + set { dataWidthSeg = value; } |
| 218 | + } |
| 219 | + |
| 220 | + public IntelRegister SegmentOverride |
| 221 | + { |
| 222 | + get { return segOverride; } |
| 223 | + set { segOverride = value; } |
| 224 | + } |
| 225 | + } |
| 226 | +} |
0 commit comments