blob: 464855508fc4f5fd07169e6d6fd6528195d511be [file] [log] [blame]
Chris Lattner72614082002-10-25 22:55:53 +00001//===-- 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 Gaeke6559bb92002-11-14 22:32:30 +00009#include "X86InstrInfo.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000010#include "llvm/Pass.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000011#include "llvm/Function.h"
12#include "llvm/Target/TargetMachine.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000013#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000014#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner233ad712002-11-21 01:33:44 +000015#include "Support/Statistic.h"
Chris Lattner72614082002-10-25 22:55:53 +000016
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000017namespace {
18 struct Printer : public FunctionPass {
19 TargetMachine &TM;
20 std::ostream &O;
Chris Lattner72614082002-10-25 22:55:53 +000021
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000022 Printer(TargetMachine &tm, std::ostream &o) : TM(tm), O(o) {}
23
24 bool runOnFunction(Function &F);
25 };
26}
27
Chris Lattnerdbb61c62002-11-17 22:53:13 +000028/// 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///
32Pass *createX86CodePrinterPass(TargetMachine &TM, std::ostream &O) {
33 return new Printer(TM, O);
34}
35
36
Brian Gaeke6559bb92002-11-14 22:32:30 +000037/// runOnFunction - This uses the X86InstructionInfo::print method
38/// to print assembly for each instruction.
39bool Printer::runOnFunction (Function & F)
40{
41 static unsigned bbnumber = 0;
42 MachineFunction & MF = MachineFunction::get (&F);
43 const MachineInstrInfo & MII = TM.getInstrInfo ();
Brian Gaeke6559bb92002-11-14 22:32:30 +000044
Chris Lattner927dd092002-11-17 23:20:37 +000045 O << "; x86 printing only sorta implemented so far!\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +000046
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 Lattner927dd092002-11-17 23:20:37 +000063 MII.print(*i_i, O, TM);
Brian Gaeke6559bb92002-11-14 22:32:30 +000064 }
65 }
66
67 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000068 return false;
69}
70
Chris Lattnerf9f60882002-11-18 06:56:51 +000071static void printOp(std::ostream &O, const MachineOperand &MO,
72 const MRegisterInfo &RI) {
73 switch (MO.getType()) {
74 case MachineOperand::MO_VirtualRegister:
Misha Brukmane1f0d812002-11-20 18:56:41 +000075 case MachineOperand::MO_MachineRegister:
Chris Lattnerf9f60882002-11-18 06:56:51 +000076 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
77 O << RI.get(MO.getReg()).Name;
78 else
79 O << "%reg" << MO.getReg();
80 return;
Chris Lattner77875d82002-11-21 02:00:20 +000081
82 case MachineOperand::MO_SignExtendedImmed:
83 case MachineOperand::MO_UnextendedImmed:
84 O << (int)MO.getImmedValue();
85 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +000086 default:
87 O << "<unknown op ty>"; return;
88 }
89}
90
91static 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
98static 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 Lattner77875d82002-11-21 02:00:20 +0000104static 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 Lattnerdbb61c62002-11-17 22:53:13 +0000113
Chris Lattner644e1ab2002-11-21 00:30:01 +0000114static bool isReg(const MachineOperand &MO) {
Chris Lattner77875d82002-11-21 02:00:20 +0000115 return MO.getType() == MachineOperand::MO_VirtualRegister ||
116 MO.getType() == MachineOperand::MO_MachineRegister;
117}
118
119static bool isImmediate(const MachineOperand &MO) {
120 return MO.getType() == MachineOperand::MO_SignExtendedImmed ||
121 MO.getType() == MachineOperand::MO_UnextendedImmed;
Chris Lattner644e1ab2002-11-21 00:30:01 +0000122}
123
124
Chris Lattner233ad712002-11-21 01:33:44 +0000125// getX86RegNum - This function maps LLVM register identifiers to their X86
126// specific numbering, which is used in various places encoding instructions.
127//
128static 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
147inline 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
153static unsigned char regModRMByte(unsigned ModRMReg, unsigned RegOpcodeField) {
154 return ModRMByte(3, RegOpcodeField, getX86RegNum(ModRMReg));
155}
156
157
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000158// print - Print out an x86 instruction in intel syntax
Chris Lattner927dd092002-11-17 23:20:37 +0000159void X86InstrInfo::print(const MachineInstr *MI, std::ostream &O,
160 const TargetMachine &TM) const {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000161 unsigned Opcode = MI->getOpcode();
162 const MachineInstrDescriptor &Desc = get(Opcode);
163
Chris Lattner233ad712002-11-21 01:33:44 +0000164 // 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 Lattnerf9f60882002-11-18 06:56:51 +0000168
169 switch (Desc.TSFlags & X86II::FormMask) {
170 case X86II::OtherFrm:
Chris Lattner77875d82002-11-21 02:00:20 +0000171 O << "\t\t\t";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000172 O << "-"; MI->print(O, TM);
173 break;
174 case X86II::RawFrm:
Chris Lattner77875d82002-11-21 02:00:20 +0000175 toHex(O, getBaseOpcodeFor(Opcode));
176 O << "\n\t\t\t\t";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000177 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 Lattner77875d82002-11-21 02:00:20 +0000187 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 Lattnerf9f60882002-11-18 06:56:51 +0000197
Chris Lattner77875d82002-11-21 02:00:20 +0000198 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 Lattner675dd2c2002-11-21 17:09:01 +0000211 printOp(O, MI->getOperand(1), RI);
Chris Lattner77875d82002-11-21 02:00:20 +0000212 }
213 O << "\n";
214 return;
215 }
Chris Lattner233ad712002-11-21 01:33:44 +0000216 case X86II::MRMDestReg: {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000217 // 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 Lattner644e1ab2002-11-21 00:30:01 +0000227 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 Brukmane1f0d812002-11-20 18:56:41 +0000231 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000232 if (MI->getNumOperands() == 3 &&
233 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
234 O << "**";
235
Chris Lattner233ad712002-11-21 01:33:44 +0000236 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 Lattner77875d82002-11-21 02:00:20 +0000241 O << "\n\t\t\t\t";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000242 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 Lattner233ad712002-11-21 01:33:44 +0000248 }
249 case X86II::MRMSrcReg: {
Chris Lattner644e1ab2002-11-21 00:30:01 +0000250 // 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 Lattner233ad712002-11-21 01:33:44 +0000268 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 Lattner77875d82002-11-21 02:00:20 +0000273 O << "\n\t\t\t\t";
Chris Lattner644e1ab2002-11-21 00:30:01 +0000274 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 Lattner233ad712002-11-21 01:33:44 +0000280 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000281
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 Lattner644e1ab2002-11-21 00:30:01 +0000324 case X86II::MRMDestMem:
Chris Lattnerf9f60882002-11-18 06:56:51 +0000325 case X86II::MRMSrcMem:
326 default:
Chris Lattner77875d82002-11-21 02:00:20 +0000327 O << "\t\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000328 }
Chris Lattner72614082002-10-25 22:55:53 +0000329}