Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 1 | //===-- X86/Printer.cpp - Convert X86 code to human readable rep. ---------===// |
| 2 | // |
| 3 | // This file contains a printer that converts from our internal representation |
| 4 | // of LLVM code to a nice human readable form that is suitable for debuggging. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
| 8 | #include "X86.h" |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 9 | #include "X86InstrInfo.h" |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 10 | #include "llvm/Pass.h" |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 11 | #include "llvm/Function.h" |
| 12 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | dbb61c6 | 2002-11-17 22:53:13 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 233ad71 | 2002-11-21 01:33:44 +0000 | [diff] [blame] | 15 | #include "Support/Statistic.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 16 | |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 17 | namespace { |
| 18 | struct Printer : public FunctionPass { |
| 19 | TargetMachine &TM; |
| 20 | std::ostream &O; |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 21 | |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 22 | Printer(TargetMachine &tm, std::ostream &o) : TM(tm), O(o) {} |
| 23 | |
| 24 | bool runOnFunction(Function &F); |
| 25 | }; |
| 26 | } |
| 27 | |
Chris Lattner | dbb61c6 | 2002-11-17 22:53:13 +0000 | [diff] [blame] | 28 | /// createX86CodePrinterPass - Print out the specified machine code function to |
| 29 | /// the specified stream. This function should work regardless of whether or |
| 30 | /// not the function is in SSA form or not. |
| 31 | /// |
| 32 | Pass *createX86CodePrinterPass(TargetMachine &TM, std::ostream &O) { |
| 33 | return new Printer(TM, O); |
| 34 | } |
| 35 | |
| 36 | |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 37 | /// runOnFunction - This uses the X86InstructionInfo::print method |
| 38 | /// to print assembly for each instruction. |
| 39 | bool Printer::runOnFunction (Function & F) |
| 40 | { |
| 41 | static unsigned bbnumber = 0; |
| 42 | MachineFunction & MF = MachineFunction::get (&F); |
| 43 | const MachineInstrInfo & MII = TM.getInstrInfo (); |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 927dd09 | 2002-11-17 23:20:37 +0000 | [diff] [blame] | 45 | O << "; x86 printing only sorta implemented so far!\n"; |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 46 | |
| 47 | // Print out labels for the function. |
| 48 | O << "\t.globl\t" << F.getName () << "\n"; |
| 49 | O << "\t.type\t" << F.getName () << ", @function\n"; |
| 50 | O << F.getName () << ":\n"; |
| 51 | |
| 52 | // Print out code for the function. |
| 53 | for (MachineFunction::const_iterator bb_i = MF.begin (), bb_e = MF.end (); |
| 54 | bb_i != bb_e; ++bb_i) |
| 55 | { |
| 56 | // Print a label for the basic block. |
| 57 | O << ".BB" << bbnumber++ << ":\n"; |
| 58 | for (MachineBasicBlock::const_iterator i_i = bb_i->begin (), i_e = |
| 59 | bb_i->end (); i_i != i_e; ++i_i) |
| 60 | { |
| 61 | // Print the assembly for the instruction. |
| 62 | O << "\t"; |
Chris Lattner | 927dd09 | 2002-11-17 23:20:37 +0000 | [diff] [blame] | 63 | MII.print(*i_i, O, TM); |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | // We didn't modify anything. |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 71 | static void printOp(std::ostream &O, const MachineOperand &MO, |
| 72 | const MRegisterInfo &RI) { |
| 73 | switch (MO.getType()) { |
| 74 | case MachineOperand::MO_VirtualRegister: |
Misha Brukman | e1f0d81 | 2002-11-20 18:56:41 +0000 | [diff] [blame] | 75 | case MachineOperand::MO_MachineRegister: |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 76 | if (MO.getReg() < MRegisterInfo::FirstVirtualRegister) |
| 77 | O << RI.get(MO.getReg()).Name; |
| 78 | else |
| 79 | O << "%reg" << MO.getReg(); |
| 80 | return; |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 81 | |
| 82 | case MachineOperand::MO_SignExtendedImmed: |
| 83 | case MachineOperand::MO_UnextendedImmed: |
| 84 | O << (int)MO.getImmedValue(); |
| 85 | return; |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 86 | default: |
| 87 | O << "<unknown op ty>"; return; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static inline void toHexDigit(std::ostream &O, unsigned char V) { |
| 92 | if (V >= 10) |
| 93 | O << (char)('A'+V-10); |
| 94 | else |
| 95 | O << (char)('0'+V); |
| 96 | } |
| 97 | |
| 98 | static std::ostream &toHex(std::ostream &O, unsigned char V) { |
| 99 | toHexDigit(O, V >> 4); |
| 100 | toHexDigit(O, V & 0xF); |
| 101 | return O; |
| 102 | } |
| 103 | |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 104 | static std::ostream &emitConstant(std::ostream &O, unsigned Val, unsigned Size){ |
| 105 | // Output the constant in little endian byte order... |
| 106 | for (unsigned i = 0; i != Size; ++i) { |
| 107 | toHex(O, Val) << " "; |
| 108 | Val >>= 8; |
| 109 | } |
| 110 | return O; |
| 111 | } |
| 112 | |
Chris Lattner | dbb61c6 | 2002-11-17 22:53:13 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 644e1ab | 2002-11-21 00:30:01 +0000 | [diff] [blame] | 114 | static bool isReg(const MachineOperand &MO) { |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 115 | return MO.getType() == MachineOperand::MO_VirtualRegister || |
| 116 | MO.getType() == MachineOperand::MO_MachineRegister; |
| 117 | } |
| 118 | |
| 119 | static bool isImmediate(const MachineOperand &MO) { |
| 120 | return MO.getType() == MachineOperand::MO_SignExtendedImmed || |
| 121 | MO.getType() == MachineOperand::MO_UnextendedImmed; |
Chris Lattner | 644e1ab | 2002-11-21 00:30:01 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
Chris Lattner | 233ad71 | 2002-11-21 01:33:44 +0000 | [diff] [blame] | 125 | // getX86RegNum - This function maps LLVM register identifiers to their X86 |
| 126 | // specific numbering, which is used in various places encoding instructions. |
| 127 | // |
| 128 | static unsigned getX86RegNum(unsigned RegNo) { |
| 129 | switch(RegNo) { |
| 130 | case X86::EAX: case X86::AX: case X86::AL: return 0; |
| 131 | case X86::ECX: case X86::CX: case X86::CL: return 1; |
| 132 | case X86::EDX: case X86::DX: case X86::DL: return 2; |
| 133 | case X86::EBX: case X86::BX: case X86::BL: return 3; |
| 134 | case X86::ESP: case X86::SP: case X86::AH: return 4; |
| 135 | case X86::EBP: case X86::BP: case X86::CH: return 5; |
| 136 | case X86::ESI: case X86::SI: case X86::DH: return 6; |
| 137 | case X86::EDI: case X86::DI: case X86::BH: return 7; |
| 138 | default: |
| 139 | assert(RegNo >= MRegisterInfo::FirstVirtualRegister && |
| 140 | "Unknown physical register!"); |
| 141 | DEBUG(std::cerr << "Register allocator hasn't allocated " << RegNo |
| 142 | << " correctly yet!\n"); |
| 143 | return 0; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode, |
| 148 | unsigned RM) { |
| 149 | assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!"); |
| 150 | return RM | (RegOpcode << 3) | (Mod << 6); |
| 151 | } |
| 152 | |
| 153 | static unsigned char regModRMByte(unsigned ModRMReg, unsigned RegOpcodeField) { |
| 154 | return ModRMByte(3, RegOpcodeField, getX86RegNum(ModRMReg)); |
| 155 | } |
| 156 | |
| 157 | |
Chris Lattner | dbb61c6 | 2002-11-17 22:53:13 +0000 | [diff] [blame] | 158 | // print - Print out an x86 instruction in intel syntax |
Chris Lattner | 927dd09 | 2002-11-17 23:20:37 +0000 | [diff] [blame] | 159 | void X86InstrInfo::print(const MachineInstr *MI, std::ostream &O, |
| 160 | const TargetMachine &TM) const { |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 161 | unsigned Opcode = MI->getOpcode(); |
| 162 | const MachineInstrDescriptor &Desc = get(Opcode); |
| 163 | |
Chris Lattner | 233ad71 | 2002-11-21 01:33:44 +0000 | [diff] [blame] | 164 | // Print instruction prefixes if neccesary |
| 165 | |
| 166 | if (Desc.TSFlags & X86II::OpSize) O << "66 "; // Operand size... |
| 167 | if (Desc.TSFlags & X86II::TB) O << "0F "; // Two-byte opcode prefix |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 168 | |
| 169 | switch (Desc.TSFlags & X86II::FormMask) { |
| 170 | case X86II::OtherFrm: |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 171 | O << "\t\t\t"; |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 172 | O << "-"; MI->print(O, TM); |
| 173 | break; |
| 174 | case X86II::RawFrm: |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 175 | toHex(O, getBaseOpcodeFor(Opcode)); |
| 176 | O << "\n\t\t\t\t"; |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 177 | O << getName(MI->getOpCode()) << " "; |
| 178 | |
| 179 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 180 | if (i) O << ", "; |
| 181 | printOp(O, MI->getOperand(i), RI); |
| 182 | } |
| 183 | O << "\n"; |
| 184 | return; |
| 185 | |
| 186 | |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 187 | case X86II::AddRegFrm: { |
| 188 | // There are currently two forms of acceptable AddRegFrm instructions. |
| 189 | // Either the instruction JUST takes a single register (like inc, dec, etc), |
| 190 | // or it takes a register and an immediate of the same size as the register |
| 191 | // (move immediate f.e.). |
| 192 | // |
| 193 | assert(isReg(MI->getOperand(0)) && |
| 194 | (MI->getNumOperands() == 1 || |
| 195 | (MI->getNumOperands() == 2 && isImmediate(MI->getOperand(1)))) && |
| 196 | "Illegal form for AddRegFrm instruction!"); |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 198 | unsigned Reg = MI->getOperand(0).getReg(); |
| 199 | toHex(O, getBaseOpcodeFor(Opcode) + getX86RegNum(Reg)) << " "; |
| 200 | |
| 201 | if (MI->getNumOperands() == 2) { |
| 202 | unsigned Size = 4; |
| 203 | emitConstant(O, MI->getOperand(1).getImmedValue(), Size); |
| 204 | } |
| 205 | |
| 206 | O << "\n\t\t\t\t"; |
| 207 | O << getName(MI->getOpCode()) << " "; |
| 208 | printOp(O, MI->getOperand(0), RI); |
| 209 | if (MI->getNumOperands() == 2) { |
| 210 | O << ", "; |
Chris Lattner | 675dd2c | 2002-11-21 17:09:01 +0000 | [diff] [blame^] | 211 | printOp(O, MI->getOperand(1), RI); |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 212 | } |
| 213 | O << "\n"; |
| 214 | return; |
| 215 | } |
Chris Lattner | 233ad71 | 2002-11-21 01:33:44 +0000 | [diff] [blame] | 216 | case X86II::MRMDestReg: { |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 217 | // There are two acceptable forms of MRMDestReg instructions, those with 3 |
| 218 | // and 2 operands: |
| 219 | // |
| 220 | // 3 Operands: in this form, the first two registers (the destination, and |
| 221 | // the first operand) should be the same, post register allocation. The 3rd |
| 222 | // operand is an additional input. This should be for things like add |
| 223 | // instructions. |
| 224 | // |
| 225 | // 2 Operands: this is for things like mov that do not read a second input |
| 226 | // |
Chris Lattner | 644e1ab | 2002-11-21 00:30:01 +0000 | [diff] [blame] | 227 | assert(isReg(MI->getOperand(0)) && |
| 228 | (MI->getNumOperands() == 2 || |
| 229 | (MI->getNumOperands() == 3 && isReg(MI->getOperand(1)))) && |
| 230 | isReg(MI->getOperand(MI->getNumOperands()-1)) |
Misha Brukman | e1f0d81 | 2002-11-20 18:56:41 +0000 | [diff] [blame] | 231 | && "Bad format for MRMDestReg!"); |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 232 | if (MI->getNumOperands() == 3 && |
| 233 | MI->getOperand(0).getReg() != MI->getOperand(1).getReg()) |
| 234 | O << "**"; |
| 235 | |
Chris Lattner | 233ad71 | 2002-11-21 01:33:44 +0000 | [diff] [blame] | 236 | toHex(O, getBaseOpcodeFor(Opcode)) << " "; |
| 237 | unsigned ModRMReg = MI->getOperand(0).getReg(); |
| 238 | unsigned ExtraReg = MI->getOperand(MI->getNumOperands()-1).getReg(); |
| 239 | toHex(O, regModRMByte(ModRMReg, getX86RegNum(ExtraReg))); |
| 240 | |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 241 | O << "\n\t\t\t\t"; |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 242 | O << getName(MI->getOpCode()) << " "; |
| 243 | printOp(O, MI->getOperand(0), RI); |
| 244 | O << ", "; |
| 245 | printOp(O, MI->getOperand(MI->getNumOperands()-1), RI); |
| 246 | O << "\n"; |
| 247 | return; |
Chris Lattner | 233ad71 | 2002-11-21 01:33:44 +0000 | [diff] [blame] | 248 | } |
| 249 | case X86II::MRMSrcReg: { |
Chris Lattner | 644e1ab | 2002-11-21 00:30:01 +0000 | [diff] [blame] | 250 | // There is a two forms that are acceptable for MRMSrcReg instructions, |
| 251 | // those with 3 and 2 operands: |
| 252 | // |
| 253 | // 3 Operands: in this form, the last register (the second input) is the |
| 254 | // ModR/M input. The first two operands should be the same, post register |
| 255 | // allocation. This is for things like: add r32, r/m32 |
| 256 | // |
| 257 | // 2 Operands: this is for things like mov that do not read a second input |
| 258 | // |
| 259 | assert(isReg(MI->getOperand(0)) && |
| 260 | isReg(MI->getOperand(1)) && |
| 261 | (MI->getNumOperands() == 2 || |
| 262 | (MI->getNumOperands() == 3 && isReg(MI->getOperand(2)))) |
| 263 | && "Bad format for MRMDestReg!"); |
| 264 | if (MI->getNumOperands() == 3 && |
| 265 | MI->getOperand(0).getReg() != MI->getOperand(1).getReg()) |
| 266 | O << "**"; |
| 267 | |
Chris Lattner | 233ad71 | 2002-11-21 01:33:44 +0000 | [diff] [blame] | 268 | toHex(O, getBaseOpcodeFor(Opcode)) << " "; |
| 269 | unsigned ModRMReg = MI->getOperand(MI->getNumOperands()-1).getReg(); |
| 270 | unsigned ExtraReg = MI->getOperand(0).getReg(); |
| 271 | toHex(O, regModRMByte(ModRMReg, getX86RegNum(ExtraReg))); |
| 272 | |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 273 | O << "\n\t\t\t\t"; |
Chris Lattner | 644e1ab | 2002-11-21 00:30:01 +0000 | [diff] [blame] | 274 | O << getName(MI->getOpCode()) << " "; |
| 275 | printOp(O, MI->getOperand(0), RI); |
| 276 | O << ", "; |
| 277 | printOp(O, MI->getOperand(MI->getNumOperands()-1), RI); |
| 278 | O << "\n"; |
| 279 | return; |
Chris Lattner | 233ad71 | 2002-11-21 01:33:44 +0000 | [diff] [blame] | 280 | } |
Chris Lattner | 675dd2c | 2002-11-21 17:09:01 +0000 | [diff] [blame^] | 281 | |
| 282 | case X86II::MRMS0r: case X86II::MRMS1r: |
| 283 | case X86II::MRMS2r: case X86II::MRMS3r: |
| 284 | case X86II::MRMS4r: case X86II::MRMS5r: |
| 285 | case X86II::MRMS6r: case X86II::MRMS7r: { |
| 286 | unsigned ExtraField = (Desc.TSFlags & X86II::FormMask)-X86II::MRMS0r; |
| 287 | |
| 288 | // In this form, the following are valid formats: |
| 289 | // 1. sete r |
| 290 | // 2. shl rdest, rinput <implicit CL or 1> |
| 291 | // 3. sbb rdest, rinput, immediate [rdest = rinput] |
| 292 | // |
| 293 | assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 && |
| 294 | isReg(MI->getOperand(0)) && "Bad MRMSxR format!"); |
| 295 | assert((MI->getNumOperands() < 2 || isReg(MI->getOperand(1))) && |
| 296 | "Bad MRMSxR format!"); |
| 297 | assert((MI->getNumOperands() < 3 || isImmediate(MI->getOperand(2))) && |
| 298 | "Bad MRMSxR format!"); |
| 299 | |
| 300 | if (MI->getNumOperands() > 1 && |
| 301 | MI->getOperand(0).getReg() != MI->getOperand(1).getReg()) |
| 302 | O << "**"; |
| 303 | |
| 304 | toHex(O, getBaseOpcodeFor(Opcode)) << " "; |
| 305 | toHex(O, regModRMByte(MI->getOperand(0).getReg(), ExtraField)); |
| 306 | |
| 307 | if (MI->getNumOperands() == 3) { |
| 308 | unsigned Size = 4; |
| 309 | emitConstant(O, MI->getOperand(1).getImmedValue(), Size); |
| 310 | } |
| 311 | |
| 312 | O << "\n\t\t\t\t"; |
| 313 | O << getName(MI->getOpCode()) << " "; |
| 314 | printOp(O, MI->getOperand(0), RI); |
| 315 | if (MI->getNumOperands() == 3) { |
| 316 | O << ", "; |
| 317 | printOp(O, MI->getOperand(2), RI); |
| 318 | } |
| 319 | O << "\n"; |
| 320 | |
| 321 | return; |
| 322 | } |
| 323 | |
Chris Lattner | 644e1ab | 2002-11-21 00:30:01 +0000 | [diff] [blame] | 324 | case X86II::MRMDestMem: |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 325 | case X86II::MRMSrcMem: |
| 326 | default: |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 327 | O << "\t\t\t-"; MI->print(O, TM); break; |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 328 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 329 | } |