blob: 57eb66aefe053c4a55754a51d24700ef856b8553 [file] [log] [blame]
Misha Brukman91b5ca82004-07-26 18:45:48 +00001//===-- X86AsmPrinter.cpp - Convert X86 LLVM code to Intel assembly -------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Misha Brukman538607f2004-03-01 23:53:11 +000010// This file contains a printer that converts from our internal representation
Chris Lattner955f0962004-10-04 07:31:08 +000011// of machine-dependent LLVM code to Intel and AT&T format assembly
12// language. This printer is the output mechanism used by `llc' and `lli
13// -print-machineinstrs' on X86.
Chris Lattner72614082002-10-25 22:55:53 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "X86.h"
Alkis Evlogimenos03090662004-03-09 03:35:34 +000018#include "X86TargetMachine.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000019#include "llvm/Module.h"
20#include "llvm/Assembly/Writer.h"
Chris Lattner055acae2004-08-16 23:16:06 +000021#include "llvm/CodeGen/AsmPrinter.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Alkis Evlogimenos03090662004-03-09 03:35:34 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb12ee502004-08-01 07:43:46 +000024#include "llvm/CodeGen/ValueTypes.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000025#include "llvm/Target/TargetMachine.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000026#include "llvm/Support/Mangler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000027#include "llvm/ADT/Statistic.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/Support/CommandLine.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner955f0962004-10-04 07:31:08 +000031namespace {
32 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
33 enum AsmWriterFlavor { att, intel };
34
35 cl::opt<AsmWriterFlavor>
36 AsmWriterFlavor("x86-asm-syntax",
37 cl::desc("Choose style of code to emit from X86 backend:"),
38 cl::values(
39 clEnumVal(att, " Emit AT&T-style assembly"),
40 clEnumVal(intel, " Emit Intel-style assembly"),
41 clEnumValEnd),
42 cl::init(att));
43
44 struct X86SharedAsmPrinter : public AsmPrinter {
45 X86SharedAsmPrinter(std::ostream &O, TargetMachine &TM)
46 : AsmPrinter(O, TM) { }
47
48 void printConstantPool(MachineConstantPool *MCP);
49 bool doFinalization(Module &M);
50 };
51}
52
Chris Lattnerac5701c2004-10-04 07:24:48 +000053static bool isScale(const MachineOperand &MO) {
54 return MO.isImmediate() &&
55 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
56 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
57}
58
59static bool isMem(const MachineInstr *MI, unsigned Op) {
60 if (MI->getOperand(Op).isFrameIndex()) return true;
61 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
62 return Op+4 <= MI->getNumOperands() &&
63 MI->getOperand(Op ).isRegister() && isScale(MI->getOperand(Op+1)) &&
64 MI->getOperand(Op+2).isRegister() && MI->getOperand(Op+3).isImmediate();
65}
66
67// SwitchSection - Switch to the specified section of the executable if we are
68// not already in it!
69//
70static void SwitchSection(std::ostream &OS, std::string &CurSection,
71 const char *NewSection) {
72 if (CurSection != NewSection) {
73 CurSection = NewSection;
74 if (!CurSection.empty())
75 OS << "\t" << NewSection << "\n";
76 }
77}
78
Chris Lattnerac5701c2004-10-04 07:24:48 +000079/// printConstantPool - Print to the current output stream assembly
80/// representations of the constants in the constant pool MCP. This is
81/// used to print out constants which have been "spilled to memory" by
82/// the code generator.
83///
84void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
85 const std::vector<Constant*> &CP = MCP->getConstants();
86 const TargetData &TD = TM.getTargetData();
87
88 if (CP.empty()) return;
89
90 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
91 O << "\t.section .rodata\n";
92 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
93 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
94 << *CP[i] << "\n";
95 emitGlobalConstant(CP[i]);
96 }
97}
98
99bool X86SharedAsmPrinter::doFinalization(Module &M) {
100 const TargetData &TD = TM.getTargetData();
101 std::string CurSection;
102
103 // Print out module-level global variables here.
104 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
105 if (I->hasInitializer()) { // External global require no code
106 O << "\n\n";
107 std::string name = Mang->getValueName(I);
108 Constant *C = I->getInitializer();
109 unsigned Size = TD.getTypeSize(C->getType());
110 unsigned Align = TD.getTypeAlignmentShift(C->getType());
111
112 if (C->isNullValue() &&
113 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
114 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
115 SwitchSection(O, CurSection, ".data");
116 if (I->hasInternalLinkage())
117 O << "\t.local " << name << "\n";
118
119 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
120 << "," << (1 << Align);
121 O << "\t\t# ";
122 WriteAsOperand(O, I, true, true, &M);
123 O << "\n";
124 } else {
125 switch (I->getLinkage()) {
126 case GlobalValue::LinkOnceLinkage:
127 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
128 // Nonnull linkonce -> weak
129 O << "\t.weak " << name << "\n";
130 SwitchSection(O, CurSection, "");
131 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
132 break;
133 case GlobalValue::AppendingLinkage:
134 // FIXME: appending linkage variables should go into a section of
135 // their name or something. For now, just emit them as external.
136 case GlobalValue::ExternalLinkage:
137 // If external or appending, declare as a global symbol
138 O << "\t.globl " << name << "\n";
139 // FALL THROUGH
140 case GlobalValue::InternalLinkage:
141 if (C->isNullValue())
142 SwitchSection(O, CurSection, ".bss");
143 else
144 SwitchSection(O, CurSection, ".data");
145 break;
146 }
147
148 emitAlignment(Align);
149 O << "\t.type " << name << ",@object\n";
150 O << "\t.size " << name << "," << Size << "\n";
151 O << name << ":\t\t\t\t# ";
152 WriteAsOperand(O, I, true, true, &M);
153 O << " = ";
154 WriteAsOperand(O, C, false, false, &M);
155 O << "\n";
156 emitGlobalConstant(C);
157 }
158 }
159
160 AsmPrinter::doFinalization(M);
161 return false; // success
162}
163
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000164namespace {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000165 struct X86IntelAsmPrinter : public X86SharedAsmPrinter {
166 X86IntelAsmPrinter(std::ostream &O, TargetMachine &TM)
167 : X86SharedAsmPrinter(O, TM) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000168
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000169 virtual const char *getPassName() const {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000170 return "X86 Intel-Style Assembly Printer";
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000171 }
172
Chris Lattner3fa861a2004-08-01 06:02:08 +0000173 /// printInstruction - This method is automatically generated by tablegen
174 /// from the instruction set description. This method returns true if the
175 /// machine instruction was sufficiently described to print it, otherwise it
176 /// returns false.
177 bool printInstruction(const MachineInstr *MI);
178
Chris Lattnerb12ee502004-08-01 07:43:46 +0000179 // This method is used by the tablegen'erated instruction printer.
Chris Lattner055acae2004-08-16 23:16:06 +0000180 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000181 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner25369cf2004-08-01 08:12:41 +0000182 if (MO.getType() == MachineOperand::MO_MachineRegister) {
183 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
184 // Bug Workaround: See note in Printer::doInitialization about %.
185 O << "%" << TM.getRegisterInfo()->get(MO.getReg()).Name;
186 } else {
187 printOp(MO);
188 }
Chris Lattnerb12ee502004-08-01 07:43:46 +0000189 }
190
Chris Lattner055acae2004-08-16 23:16:06 +0000191 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
192 MVT::ValueType VT) {
Chris Lattnere4ead0c2004-08-11 06:59:12 +0000193 printOp(MI->getOperand(OpNo), true); // Don't print "OFFSET".
194 }
195
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000196 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
197 MVT::ValueType VT) {
198 switch (VT) {
199 default: assert(0 && "Unknown arg size!");
200 case MVT::i8: O << "BYTE PTR "; break;
201 case MVT::i16: O << "WORD PTR "; break;
202 case MVT::i32:
203 case MVT::f32: O << "DWORD PTR "; break;
204 case MVT::i64:
205 case MVT::f64: O << "QWORD PTR "; break;
206 case MVT::f80: O << "XWORD PTR "; break;
207 }
208 printMemReference(MI, OpNo);
209 }
210
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000211 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000212 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000213 void printMemReference(const MachineInstr *MI, unsigned Op);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000214 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000215 bool doInitialization(Module &M);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000216 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000217} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000218
Chris Lattner3fa861a2004-08-01 06:02:08 +0000219
220// Include the auto-generated portion of the assembly writer.
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000221#include "X86GenIntelAsmWriter.inc"
Chris Lattner3fa861a2004-08-01 06:02:08 +0000222
223
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000224/// runOnMachineFunction - This uses the printMachineInstruction()
225/// method to print assembly for each instruction.
226///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000227bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner055acae2004-08-16 23:16:06 +0000228 setupMachineFunction(MF);
Chris Lattnere0121322003-08-03 23:37:09 +0000229 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000230
Chris Lattnerb7089442003-01-13 00:35:03 +0000231 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000232 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000233
Brian Gaeke6559bb92002-11-14 22:32:30 +0000234 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000235 O << "\t.text\n";
Chris Lattnerc6393f82004-08-17 19:25:42 +0000236 emitAlignment(4);
Brian Gaeked7908f62003-06-27 00:00:48 +0000237 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner055acae2004-08-16 23:16:06 +0000238 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000239 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000240
241 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000242 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
243 I != E; ++I) {
244 // Print a label for the basic block.
Chris Lattnerc6393f82004-08-17 19:25:42 +0000245 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000246 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000247 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000248 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000249 // Print the assembly for the instruction.
250 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000251 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000252 }
Chris Lattner0285a332002-12-28 20:25:38 +0000253 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000254
255 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000256 return false;
257}
258
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000259void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnerac5701c2004-10-04 07:24:48 +0000260 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000261 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000262 switch (MO.getType()) {
263 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000264 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000265 O << "<" << V->getName() << ">";
266 return;
267 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000268 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000269 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000270 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000271 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000272 O << "%" << RI.get(MO.getReg()).Name;
273 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000274 O << "%reg" << MO.getReg();
275 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000276
277 case MachineOperand::MO_SignExtendedImmed:
278 case MachineOperand::MO_UnextendedImmed:
279 O << (int)MO.getImmedValue();
280 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000281 case MachineOperand::MO_MachineBasicBlock: {
282 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
283 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
284 << "_" << MBBOp->getNumber () << "\t# "
285 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000286 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000287 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000288 case MachineOperand::MO_PCRelativeDisp:
289 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
290 abort ();
291 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000292 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000293 if (!elideOffsetKeyword)
294 O << "OFFSET ";
295 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000296 return;
297 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000298 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000299 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000300 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000301 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000302 }
303}
304
Chris Lattnerac5701c2004-10-04 07:24:48 +0000305void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
Chris Lattner3d3067b2002-11-21 20:44:15 +0000306 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000307
308 if (MI->getOperand(Op).isFrameIndex()) {
309 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
310 if (MI->getOperand(Op+3).getImmedValue())
311 O << " + " << MI->getOperand(Op+3).getImmedValue();
312 O << "]";
313 return;
314 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000315 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000316 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000317 if (MI->getOperand(Op+3).getImmedValue())
318 O << " + " << MI->getOperand(Op+3).getImmedValue();
319 O << "]";
320 return;
321 }
322
Chris Lattner3d3067b2002-11-21 20:44:15 +0000323 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000324 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000325 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000326 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000327
328 O << "[";
329 bool NeedPlus = false;
330 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000331 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000332 NeedPlus = true;
333 }
334
335 if (IndexReg.getReg()) {
336 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000337 if (ScaleVal != 1)
338 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000339 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000340 NeedPlus = true;
341 }
342
Chris Lattner0285a332002-12-28 20:25:38 +0000343 if (DispVal) {
344 if (NeedPlus)
345 if (DispVal > 0)
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000346 O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000347 else {
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000348 O << " - ";
349 DispVal = -DispVal;
Chris Lattner0285a332002-12-28 20:25:38 +0000350 }
351 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000352 }
353 O << "]";
354}
355
John Criswell4ffff9e2004-04-08 20:31:47 +0000356
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000357/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000358/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000359///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000360void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000361 ++EmittedInsts;
Chris Lattner85494292004-08-11 06:09:55 +0000362
Chris Lattner2a998bd2004-08-11 07:02:04 +0000363 // Call the autogenerated instruction printer routines.
Chris Lattner955f0962004-10-04 07:31:08 +0000364 printInstruction(MI);
Chris Lattner72614082002-10-25 22:55:53 +0000365}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000366
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000367bool X86IntelAsmPrinter::doInitialization(Module &M) {
Chris Lattner055acae2004-08-16 23:16:06 +0000368 AsmPrinter::doInitialization(M);
Chris Lattner93c1afa2003-08-11 19:35:26 +0000369 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000370 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000371 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
372 // instruction as a reference to the register named sp, and if you try to
373 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
374 // before being looked up in the symbol table. This creates spurious
375 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
376 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000377 O << "\t.intel_syntax\n";
Chris Lattner055acae2004-08-16 23:16:06 +0000378 return false;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000379}
380
Chris Lattnerac5701c2004-10-04 07:24:48 +0000381
382
383namespace {
384 struct X86ATTAsmPrinter : public X86SharedAsmPrinter {
385 X86ATTAsmPrinter(std::ostream &O, TargetMachine &TM)
386 : X86SharedAsmPrinter(O, TM) { }
387
388 virtual const char *getPassName() const {
389 return "X86 AT&T-Style Assembly Printer";
390 }
391
392 /// printInstruction - This method is automatically generated by tablegen
393 /// from the instruction set description. This method returns true if the
394 /// machine instruction was sufficiently described to print it, otherwise it
395 /// returns false.
396 bool printInstruction(const MachineInstr *MI);
397
398 // This method is used by the tablegen'erated instruction printer.
399 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
400 printOp(MI->getOperand(OpNo));
401 }
402
403 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
404 MVT::ValueType VT) {
405 printOp(MI->getOperand(OpNo), true); // Don't print '$' prefix.
406 }
407
408 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
409 MVT::ValueType VT) {
410 printMemReference(MI, OpNo);
411 }
412
413 void printMachineInstruction(const MachineInstr *MI);
414 void printOp(const MachineOperand &MO, bool isCallOperand = false);
415 void printMemReference(const MachineInstr *MI, unsigned Op);
416 bool runOnMachineFunction(MachineFunction &F);
417 };
418} // end of anonymous namespace
419
420
421// Include the auto-generated portion of the assembly writer.
422#include "X86GenATTAsmWriter.inc"
423
424
425/// runOnMachineFunction - This uses the printMachineInstruction()
426/// method to print assembly for each instruction.
427///
428bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
429 setupMachineFunction(MF);
430 O << "\n\n";
431
432 // Print out constants referenced by the function
433 printConstantPool(MF.getConstantPool());
434
435 // Print out labels for the function.
436 O << "\t.text\n";
437 emitAlignment(4);
438 O << "\t.globl\t" << CurrentFnName << "\n";
439 O << "\t.type\t" << CurrentFnName << ", @function\n";
440 O << CurrentFnName << ":\n";
441
442 // Print out code for the function.
443 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
444 I != E; ++I) {
445 // Print a label for the basic block.
446 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
447 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
448 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
449 II != E; ++II) {
450 // Print the assembly for the instruction.
451 O << "\t";
452 printMachineInstruction(II);
453 }
454 }
455
456 // We didn't modify anything.
457 return false;
458}
459
460void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
461 const MRegisterInfo &RI = *TM.getRegisterInfo();
462 switch (MO.getType()) {
463 case MachineOperand::MO_VirtualRegister:
464 case MachineOperand::MO_MachineRegister:
465 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
466 "Virtual registers should not make it this far!");
467 O << '%';
468 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
469 O << (char)tolower(*Name);
470 return;
471
472 case MachineOperand::MO_SignExtendedImmed:
473 case MachineOperand::MO_UnextendedImmed:
474 O << '$' << (int)MO.getImmedValue();
475 return;
476 case MachineOperand::MO_MachineBasicBlock: {
477 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
478 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
479 << "_" << MBBOp->getNumber () << "\t# "
480 << MBBOp->getBasicBlock ()->getName ();
481 return;
482 }
483 case MachineOperand::MO_PCRelativeDisp:
484 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
485 abort ();
486 return;
487 case MachineOperand::MO_GlobalAddress:
488 if (!isCallOp) O << '$';
489 O << Mang->getValueName(MO.getGlobal());
490 return;
491 case MachineOperand::MO_ExternalSymbol:
492 if (!isCallOp) O << '$';
493 O << MO.getSymbolName();
494 return;
495 default:
496 O << "<unknown operand type>"; return;
Chris Lattnerad200712003-09-09 16:23:36 +0000497 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000498}
499
Chris Lattnerac5701c2004-10-04 07:24:48 +0000500void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
501 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerad200712003-09-09 16:23:36 +0000502
Chris Lattnerac5701c2004-10-04 07:24:48 +0000503 if (MI->getOperand(Op).isFrameIndex()) {
504 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
505 if (MI->getOperand(Op+3).getImmedValue())
506 O << " + " << MI->getOperand(Op+3).getImmedValue();
507 O << "]";
508 return;
509 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
510 O << ".CPI" << CurrentFnName << "_"
511 << MI->getOperand(Op).getConstantPoolIndex();
512 if (MI->getOperand(Op+3).getImmedValue())
513 O << " + " << MI->getOperand(Op+3).getImmedValue();
514 return;
515 }
Chris Lattnerad200712003-09-09 16:23:36 +0000516
Chris Lattnerac5701c2004-10-04 07:24:48 +0000517 const MachineOperand &BaseReg = MI->getOperand(Op);
518 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
519 const MachineOperand &IndexReg = MI->getOperand(Op+2);
520 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattnerad200712003-09-09 16:23:36 +0000521
Chris Lattnerac5701c2004-10-04 07:24:48 +0000522 if (DispVal) O << DispVal;
Chris Lattnerad200712003-09-09 16:23:36 +0000523
Chris Lattnerac5701c2004-10-04 07:24:48 +0000524 O << "(";
525 if (BaseReg.getReg())
526 printOp(BaseReg);
527
528 if (IndexReg.getReg()) {
529 O << ",";
530 printOp(IndexReg);
531 if (ScaleVal != 1)
532 O << "," << ScaleVal;
533 }
534
535 O << ")";
536}
537
538
539/// printMachineInstruction -- Print out a single X86 LLVM instruction
540/// MI in Intel syntax to the current output stream.
541///
542void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
543 ++EmittedInsts;
544 // Call the autogenerated instruction printer routines.
Chris Lattner955f0962004-10-04 07:31:08 +0000545 printInstruction(MI);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000546}
547
548
549/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
550/// for a MachineFunction to the given output stream, using the given target
551/// machine description.
552///
553FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
554 switch (AsmWriterFlavor) {
555 default: assert(0 && "Unknown asm flavor!");
556 case intel:
557 return new X86IntelAsmPrinter(o, tm);
558 case att:
559 return new X86ATTAsmPrinter(o, tm);
560 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000561}