blob: a57c2fe6799f9ae9aec2aeea17b40a1ba9949bbd [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)) &&
Chris Lattnerd416f082004-10-15 04:44:53 +000064 MI->getOperand(Op+2).isRegister() && (MI->getOperand(Op+3).isImmediate() ||
65 MI->getOperand(Op+3).isGlobalAddress());
Chris Lattnerac5701c2004-10-04 07:24:48 +000066}
67
68// SwitchSection - Switch to the specified section of the executable if we are
69// not already in it!
70//
71static void SwitchSection(std::ostream &OS, std::string &CurSection,
72 const char *NewSection) {
73 if (CurSection != NewSection) {
74 CurSection = NewSection;
75 if (!CurSection.empty())
76 OS << "\t" << NewSection << "\n";
77 }
78}
79
Chris Lattnerac5701c2004-10-04 07:24:48 +000080/// printConstantPool - Print to the current output stream assembly
81/// representations of the constants in the constant pool MCP. This is
82/// used to print out constants which have been "spilled to memory" by
83/// the code generator.
84///
85void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
86 const std::vector<Constant*> &CP = MCP->getConstants();
87 const TargetData &TD = TM.getTargetData();
88
89 if (CP.empty()) return;
90
91 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
92 O << "\t.section .rodata\n";
93 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
94 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
95 << *CP[i] << "\n";
96 emitGlobalConstant(CP[i]);
97 }
98}
99
100bool X86SharedAsmPrinter::doFinalization(Module &M) {
101 const TargetData &TD = TM.getTargetData();
102 std::string CurSection;
103
104 // Print out module-level global variables here.
105 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
106 if (I->hasInitializer()) { // External global require no code
107 O << "\n\n";
108 std::string name = Mang->getValueName(I);
109 Constant *C = I->getInitializer();
110 unsigned Size = TD.getTypeSize(C->getType());
111 unsigned Align = TD.getTypeAlignmentShift(C->getType());
112
113 if (C->isNullValue() &&
114 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
115 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
116 SwitchSection(O, CurSection, ".data");
117 if (I->hasInternalLinkage())
118 O << "\t.local " << name << "\n";
119
120 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
121 << "," << (1 << Align);
122 O << "\t\t# ";
123 WriteAsOperand(O, I, true, true, &M);
124 O << "\n";
125 } else {
126 switch (I->getLinkage()) {
127 case GlobalValue::LinkOnceLinkage:
128 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
129 // Nonnull linkonce -> weak
130 O << "\t.weak " << name << "\n";
131 SwitchSection(O, CurSection, "");
132 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
133 break;
134 case GlobalValue::AppendingLinkage:
135 // FIXME: appending linkage variables should go into a section of
136 // their name or something. For now, just emit them as external.
137 case GlobalValue::ExternalLinkage:
138 // If external or appending, declare as a global symbol
139 O << "\t.globl " << name << "\n";
140 // FALL THROUGH
141 case GlobalValue::InternalLinkage:
142 if (C->isNullValue())
143 SwitchSection(O, CurSection, ".bss");
144 else
145 SwitchSection(O, CurSection, ".data");
146 break;
147 }
148
149 emitAlignment(Align);
150 O << "\t.type " << name << ",@object\n";
151 O << "\t.size " << name << "," << Size << "\n";
152 O << name << ":\t\t\t\t# ";
153 WriteAsOperand(O, I, true, true, &M);
154 O << " = ";
155 WriteAsOperand(O, C, false, false, &M);
156 O << "\n";
157 emitGlobalConstant(C);
158 }
159 }
160
161 AsmPrinter::doFinalization(M);
162 return false; // success
163}
164
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000165namespace {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000166 struct X86IntelAsmPrinter : public X86SharedAsmPrinter {
167 X86IntelAsmPrinter(std::ostream &O, TargetMachine &TM)
168 : X86SharedAsmPrinter(O, TM) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000169
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000170 virtual const char *getPassName() const {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000171 return "X86 Intel-Style Assembly Printer";
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000172 }
173
Chris Lattner3fa861a2004-08-01 06:02:08 +0000174 /// printInstruction - This method is automatically generated by tablegen
175 /// from the instruction set description. This method returns true if the
176 /// machine instruction was sufficiently described to print it, otherwise it
177 /// returns false.
178 bool printInstruction(const MachineInstr *MI);
179
Chris Lattnerb12ee502004-08-01 07:43:46 +0000180 // This method is used by the tablegen'erated instruction printer.
Chris Lattner055acae2004-08-16 23:16:06 +0000181 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000182 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner25369cf2004-08-01 08:12:41 +0000183 if (MO.getType() == MachineOperand::MO_MachineRegister) {
184 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
185 // Bug Workaround: See note in Printer::doInitialization about %.
186 O << "%" << TM.getRegisterInfo()->get(MO.getReg()).Name;
187 } else {
188 printOp(MO);
189 }
Chris Lattnerb12ee502004-08-01 07:43:46 +0000190 }
191
Chris Lattner055acae2004-08-16 23:16:06 +0000192 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
193 MVT::ValueType VT) {
Chris Lattnere4ead0c2004-08-11 06:59:12 +0000194 printOp(MI->getOperand(OpNo), true); // Don't print "OFFSET".
195 }
196
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000197 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
198 MVT::ValueType VT) {
199 switch (VT) {
200 default: assert(0 && "Unknown arg size!");
201 case MVT::i8: O << "BYTE PTR "; break;
202 case MVT::i16: O << "WORD PTR "; break;
203 case MVT::i32:
204 case MVT::f32: O << "DWORD PTR "; break;
205 case MVT::i64:
206 case MVT::f64: O << "QWORD PTR "; break;
207 case MVT::f80: O << "XWORD PTR "; break;
208 }
209 printMemReference(MI, OpNo);
210 }
211
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000212 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000213 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000214 void printMemReference(const MachineInstr *MI, unsigned Op);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000215 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000216 bool doInitialization(Module &M);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000217 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000218} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000219
Chris Lattner3fa861a2004-08-01 06:02:08 +0000220
221// Include the auto-generated portion of the assembly writer.
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000222#include "X86GenIntelAsmWriter.inc"
Chris Lattner3fa861a2004-08-01 06:02:08 +0000223
224
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000225/// runOnMachineFunction - This uses the printMachineInstruction()
226/// method to print assembly for each instruction.
227///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000228bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner055acae2004-08-16 23:16:06 +0000229 setupMachineFunction(MF);
Chris Lattnere0121322003-08-03 23:37:09 +0000230 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000231
Chris Lattnerb7089442003-01-13 00:35:03 +0000232 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000233 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000234
Brian Gaeke6559bb92002-11-14 22:32:30 +0000235 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000236 O << "\t.text\n";
Chris Lattnerc6393f82004-08-17 19:25:42 +0000237 emitAlignment(4);
Brian Gaeked7908f62003-06-27 00:00:48 +0000238 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner055acae2004-08-16 23:16:06 +0000239 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000240 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000241
242 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000243 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
244 I != E; ++I) {
Chris Lattner4b2c09f2004-11-13 23:27:11 +0000245 // Print a label for the basic block if there are any predecessors.
246 if (I->pred_begin() != I->pred_end())
247 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
248 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000249 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000250 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000251 // Print the assembly for the instruction.
252 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000253 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000254 }
Chris Lattner0285a332002-12-28 20:25:38 +0000255 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000256
257 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000258 return false;
259}
260
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000261void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnerac5701c2004-10-04 07:24:48 +0000262 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000263 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000264 switch (MO.getType()) {
265 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000266 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000267 O << "<" << V->getName() << ">";
268 return;
269 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000270 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000271 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000272 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000273 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000274 O << "%" << RI.get(MO.getReg()).Name;
275 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000276 O << "%reg" << MO.getReg();
277 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000278
279 case MachineOperand::MO_SignExtendedImmed:
280 case MachineOperand::MO_UnextendedImmed:
281 O << (int)MO.getImmedValue();
282 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000283 case MachineOperand::MO_MachineBasicBlock: {
284 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
285 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
286 << "_" << MBBOp->getNumber () << "\t# "
287 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000288 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000289 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000290 case MachineOperand::MO_PCRelativeDisp:
291 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
292 abort ();
293 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000294 case MachineOperand::MO_GlobalAddress: {
Brian Gaeke002a50a2003-07-31 17:38:52 +0000295 if (!elideOffsetKeyword)
296 O << "OFFSET ";
297 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerd416f082004-10-15 04:44:53 +0000298 int Offset = MO.getOffset();
299 if (Offset > 0)
300 O << " + " << Offset;
301 else if (Offset < 0)
302 O << " - " << -Offset;
Chris Lattnerb7089442003-01-13 00:35:03 +0000303 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000304 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000305 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000306 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000307 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000308 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000309 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000310 }
311}
312
Chris Lattnerac5701c2004-10-04 07:24:48 +0000313void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
Chris Lattner3d3067b2002-11-21 20:44:15 +0000314 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000315
Chris Lattner3d3067b2002-11-21 20:44:15 +0000316 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000317 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000318 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerd416f082004-10-15 04:44:53 +0000319 const MachineOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000320
Chris Lattner0e0ed852004-10-17 07:16:32 +0000321 if (BaseReg.isFrameIndex()) {
322 O << "[frame slot #" << BaseReg.getFrameIndex();
323 if (DispSpec.getImmedValue())
324 O << " + " << DispSpec.getImmedValue();
325 O << "]";
326 return;
327 } else if (BaseReg.isConstantPoolIndex()) {
328 O << "[.CPI" << CurrentFnName << "_"
329 << BaseReg.getConstantPoolIndex();
330
331 if (IndexReg.getReg()) {
332 O << " + ";
333 if (ScaleVal != 1)
334 O << ScaleVal << "*";
335 printOp(IndexReg);
336 }
337
338 if (DispSpec.getImmedValue())
339 O << " + " << DispSpec.getImmedValue();
340 O << "]";
341 return;
342 }
343
Chris Lattner3d3067b2002-11-21 20:44:15 +0000344 O << "[";
345 bool NeedPlus = false;
346 if (BaseReg.getReg()) {
Chris Lattnerd416f082004-10-15 04:44:53 +0000347 printOp(BaseReg, true);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000348 NeedPlus = true;
349 }
350
351 if (IndexReg.getReg()) {
352 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000353 if (ScaleVal != 1)
354 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000355 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000356 NeedPlus = true;
357 }
358
Chris Lattnerd416f082004-10-15 04:44:53 +0000359 if (DispSpec.isGlobalAddress()) {
Chris Lattner0285a332002-12-28 20:25:38 +0000360 if (NeedPlus)
Chris Lattnerd416f082004-10-15 04:44:53 +0000361 O << " + ";
362 printOp(DispSpec, true);
363 } else {
364 int DispVal = DispSpec.getImmedValue();
365 if (DispVal) {
366 if (NeedPlus)
367 if (DispVal > 0)
368 O << " + ";
369 else {
370 O << " - ";
371 DispVal = -DispVal;
372 }
373 O << DispVal;
374 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000375 }
376 O << "]";
377}
378
John Criswell4ffff9e2004-04-08 20:31:47 +0000379
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000380/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000381/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000382///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000383void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000384 ++EmittedInsts;
Chris Lattner85494292004-08-11 06:09:55 +0000385
Chris Lattner2a998bd2004-08-11 07:02:04 +0000386 // Call the autogenerated instruction printer routines.
Chris Lattner955f0962004-10-04 07:31:08 +0000387 printInstruction(MI);
Chris Lattner72614082002-10-25 22:55:53 +0000388}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000389
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000390bool X86IntelAsmPrinter::doInitialization(Module &M) {
Chris Lattner055acae2004-08-16 23:16:06 +0000391 AsmPrinter::doInitialization(M);
Chris Lattner93c1afa2003-08-11 19:35:26 +0000392 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000393 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000394 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
395 // instruction as a reference to the register named sp, and if you try to
396 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
397 // before being looked up in the symbol table. This creates spurious
398 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
399 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000400 O << "\t.intel_syntax\n";
Chris Lattner055acae2004-08-16 23:16:06 +0000401 return false;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000402}
403
Chris Lattnerac5701c2004-10-04 07:24:48 +0000404
405
406namespace {
407 struct X86ATTAsmPrinter : public X86SharedAsmPrinter {
408 X86ATTAsmPrinter(std::ostream &O, TargetMachine &TM)
409 : X86SharedAsmPrinter(O, TM) { }
410
411 virtual const char *getPassName() const {
412 return "X86 AT&T-Style Assembly Printer";
413 }
414
415 /// printInstruction - This method is automatically generated by tablegen
416 /// from the instruction set description. This method returns true if the
417 /// machine instruction was sufficiently described to print it, otherwise it
418 /// returns false.
419 bool printInstruction(const MachineInstr *MI);
420
421 // This method is used by the tablegen'erated instruction printer.
422 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
423 printOp(MI->getOperand(OpNo));
424 }
425
426 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
427 MVT::ValueType VT) {
428 printOp(MI->getOperand(OpNo), true); // Don't print '$' prefix.
429 }
430
431 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
432 MVT::ValueType VT) {
433 printMemReference(MI, OpNo);
434 }
435
436 void printMachineInstruction(const MachineInstr *MI);
437 void printOp(const MachineOperand &MO, bool isCallOperand = false);
438 void printMemReference(const MachineInstr *MI, unsigned Op);
439 bool runOnMachineFunction(MachineFunction &F);
440 };
441} // end of anonymous namespace
442
443
444// Include the auto-generated portion of the assembly writer.
445#include "X86GenATTAsmWriter.inc"
446
447
448/// runOnMachineFunction - This uses the printMachineInstruction()
449/// method to print assembly for each instruction.
450///
451bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
452 setupMachineFunction(MF);
453 O << "\n\n";
454
455 // Print out constants referenced by the function
456 printConstantPool(MF.getConstantPool());
457
458 // Print out labels for the function.
459 O << "\t.text\n";
460 emitAlignment(4);
461 O << "\t.globl\t" << CurrentFnName << "\n";
462 O << "\t.type\t" << CurrentFnName << ", @function\n";
463 O << CurrentFnName << ":\n";
464
465 // Print out code for the function.
466 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
467 I != E; ++I) {
468 // Print a label for the basic block.
Chris Lattner4b2c09f2004-11-13 23:27:11 +0000469 if (I->pred_begin() != I->pred_end())
470 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
471 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Chris Lattnerac5701c2004-10-04 07:24:48 +0000472 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
473 II != E; ++II) {
474 // Print the assembly for the instruction.
475 O << "\t";
476 printMachineInstruction(II);
477 }
478 }
479
480 // We didn't modify anything.
481 return false;
482}
483
484void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
485 const MRegisterInfo &RI = *TM.getRegisterInfo();
486 switch (MO.getType()) {
487 case MachineOperand::MO_VirtualRegister:
488 case MachineOperand::MO_MachineRegister:
489 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
490 "Virtual registers should not make it this far!");
491 O << '%';
492 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
493 O << (char)tolower(*Name);
494 return;
495
496 case MachineOperand::MO_SignExtendedImmed:
497 case MachineOperand::MO_UnextendedImmed:
498 O << '$' << (int)MO.getImmedValue();
499 return;
500 case MachineOperand::MO_MachineBasicBlock: {
501 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
502 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
503 << "_" << MBBOp->getNumber () << "\t# "
504 << MBBOp->getBasicBlock ()->getName ();
505 return;
506 }
507 case MachineOperand::MO_PCRelativeDisp:
508 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
509 abort ();
510 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000511 case MachineOperand::MO_GlobalAddress: {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000512 if (!isCallOp) O << '$';
513 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerd416f082004-10-15 04:44:53 +0000514 int Offset = MO.getOffset();
515 if (Offset > 0)
516 O << "+" << Offset;
517 else if (Offset < 0)
518 O << Offset;
Chris Lattnerac5701c2004-10-04 07:24:48 +0000519 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000520 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000521 case MachineOperand::MO_ExternalSymbol:
522 if (!isCallOp) O << '$';
523 O << MO.getSymbolName();
524 return;
525 default:
526 O << "<unknown operand type>"; return;
Chris Lattnerad200712003-09-09 16:23:36 +0000527 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000528}
529
Chris Lattnerac5701c2004-10-04 07:24:48 +0000530void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
531 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerad200712003-09-09 16:23:36 +0000532
Chris Lattnerac5701c2004-10-04 07:24:48 +0000533 const MachineOperand &BaseReg = MI->getOperand(Op);
534 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
535 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerd416f082004-10-15 04:44:53 +0000536 const MachineOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattnerad200712003-09-09 16:23:36 +0000537
Chris Lattner0e0ed852004-10-17 07:16:32 +0000538 if (BaseReg.isFrameIndex()) {
539 O << "[frame slot #" << BaseReg.getFrameIndex();
540 if (DispSpec.getImmedValue())
541 O << " + " << DispSpec.getImmedValue();
542 O << "]";
543 return;
544 } else if (BaseReg.isConstantPoolIndex()) {
545 O << ".CPI" << CurrentFnName << "_"
546 << BaseReg.getConstantPoolIndex();
547 if (DispSpec.getImmedValue())
548 O << "+" << DispSpec.getImmedValue();
549 if (IndexReg.getReg()) {
550 O << "(,";
551 printOp(IndexReg);
552 if (ScaleVal != 1)
553 O << "," << ScaleVal;
554 O << ")";
555 }
556 return;
557 }
558
Chris Lattnerd416f082004-10-15 04:44:53 +0000559 if (DispSpec.isGlobalAddress()) {
560 printOp(DispSpec, true);
561 } else {
562 int DispVal = DispSpec.getImmedValue();
563 if (DispVal)
564 O << DispVal;
Chris Lattnerac5701c2004-10-04 07:24:48 +0000565 }
566
Chris Lattnerd416f082004-10-15 04:44:53 +0000567 if (IndexReg.getReg() || BaseReg.getReg()) {
568 O << "(";
569 if (BaseReg.getReg())
570 printOp(BaseReg);
571
572 if (IndexReg.getReg()) {
573 O << ",";
574 printOp(IndexReg);
575 if (ScaleVal != 1)
576 O << "," << ScaleVal;
577 }
578
579 O << ")";
580 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000581}
582
583
584/// printMachineInstruction -- Print out a single X86 LLVM instruction
585/// MI in Intel syntax to the current output stream.
586///
587void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
588 ++EmittedInsts;
589 // Call the autogenerated instruction printer routines.
Chris Lattner955f0962004-10-04 07:31:08 +0000590 printInstruction(MI);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000591}
592
593
594/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
595/// for a MachineFunction to the given output stream, using the given target
596/// machine description.
597///
598FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
599 switch (AsmWriterFlavor) {
600 default: assert(0 && "Unknown asm flavor!");
601 case intel:
602 return new X86IntelAsmPrinter(o, tm);
603 case att:
604 return new X86ATTAsmPrinter(o, tm);
605 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000606}