blob: b0d27888fc6c38a4995b65860a4effd5d79ae50b [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;
Misha Brukmand2691fd2004-11-14 21:03:49 +0000147 case GlobalValue::GhostLinkage:
148 std::cerr << "GhostLinkage cannot appear in X86AsmPrinter!\n";
149 abort();
Chris Lattnerac5701c2004-10-04 07:24:48 +0000150 }
151
152 emitAlignment(Align);
153 O << "\t.type " << name << ",@object\n";
154 O << "\t.size " << name << "," << Size << "\n";
155 O << name << ":\t\t\t\t# ";
156 WriteAsOperand(O, I, true, true, &M);
157 O << " = ";
158 WriteAsOperand(O, C, false, false, &M);
159 O << "\n";
160 emitGlobalConstant(C);
161 }
162 }
163
164 AsmPrinter::doFinalization(M);
165 return false; // success
166}
167
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000168namespace {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000169 struct X86IntelAsmPrinter : public X86SharedAsmPrinter {
170 X86IntelAsmPrinter(std::ostream &O, TargetMachine &TM)
171 : X86SharedAsmPrinter(O, TM) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000172
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000173 virtual const char *getPassName() const {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000174 return "X86 Intel-Style Assembly Printer";
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000175 }
176
Chris Lattner3fa861a2004-08-01 06:02:08 +0000177 /// printInstruction - This method is automatically generated by tablegen
178 /// from the instruction set description. This method returns true if the
179 /// machine instruction was sufficiently described to print it, otherwise it
180 /// returns false.
181 bool printInstruction(const MachineInstr *MI);
182
Chris Lattnerb12ee502004-08-01 07:43:46 +0000183 // This method is used by the tablegen'erated instruction printer.
Chris Lattner055acae2004-08-16 23:16:06 +0000184 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000185 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner25369cf2004-08-01 08:12:41 +0000186 if (MO.getType() == MachineOperand::MO_MachineRegister) {
187 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
188 // Bug Workaround: See note in Printer::doInitialization about %.
189 O << "%" << TM.getRegisterInfo()->get(MO.getReg()).Name;
190 } else {
191 printOp(MO);
192 }
Chris Lattnerb12ee502004-08-01 07:43:46 +0000193 }
194
Chris Lattner055acae2004-08-16 23:16:06 +0000195 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
196 MVT::ValueType VT) {
Chris Lattnere4ead0c2004-08-11 06:59:12 +0000197 printOp(MI->getOperand(OpNo), true); // Don't print "OFFSET".
198 }
199
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000200 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
201 MVT::ValueType VT) {
202 switch (VT) {
203 default: assert(0 && "Unknown arg size!");
204 case MVT::i8: O << "BYTE PTR "; break;
205 case MVT::i16: O << "WORD PTR "; break;
206 case MVT::i32:
207 case MVT::f32: O << "DWORD PTR "; break;
208 case MVT::i64:
209 case MVT::f64: O << "QWORD PTR "; break;
210 case MVT::f80: O << "XWORD PTR "; break;
211 }
212 printMemReference(MI, OpNo);
213 }
214
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000215 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000216 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000217 void printMemReference(const MachineInstr *MI, unsigned Op);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000218 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000219 bool doInitialization(Module &M);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000220 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000221} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000222
Chris Lattner3fa861a2004-08-01 06:02:08 +0000223
224// Include the auto-generated portion of the assembly writer.
Chris Lattner84b85c82004-12-16 17:33:24 +0000225#include "X86GenAsmWriter1.inc"
Chris Lattner3fa861a2004-08-01 06:02:08 +0000226
227
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000228/// runOnMachineFunction - This uses the printMachineInstruction()
229/// method to print assembly for each instruction.
230///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000231bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner055acae2004-08-16 23:16:06 +0000232 setupMachineFunction(MF);
Chris Lattnere0121322003-08-03 23:37:09 +0000233 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000234
Chris Lattnerb7089442003-01-13 00:35:03 +0000235 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000236 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000237
Brian Gaeke6559bb92002-11-14 22:32:30 +0000238 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000239 O << "\t.text\n";
Chris Lattnerc6393f82004-08-17 19:25:42 +0000240 emitAlignment(4);
Brian Gaeked7908f62003-06-27 00:00:48 +0000241 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner055acae2004-08-16 23:16:06 +0000242 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000243 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000244
245 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000246 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
247 I != E; ++I) {
Chris Lattner4b2c09f2004-11-13 23:27:11 +0000248 // Print a label for the basic block if there are any predecessors.
249 if (I->pred_begin() != I->pred_end())
250 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
251 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000252 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000253 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000254 // Print the assembly for the instruction.
255 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000256 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000257 }
Chris Lattner0285a332002-12-28 20:25:38 +0000258 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000259
260 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000261 return false;
262}
263
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000264void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnerac5701c2004-10-04 07:24:48 +0000265 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000266 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000267 switch (MO.getType()) {
268 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000269 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000270 O << "<" << V->getName() << ">";
271 return;
272 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000273 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000274 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000275 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000276 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000277 O << "%" << RI.get(MO.getReg()).Name;
278 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000279 O << "%reg" << MO.getReg();
280 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000281
282 case MachineOperand::MO_SignExtendedImmed:
283 case MachineOperand::MO_UnextendedImmed:
284 O << (int)MO.getImmedValue();
285 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000286 case MachineOperand::MO_MachineBasicBlock: {
287 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
288 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
289 << "_" << MBBOp->getNumber () << "\t# "
290 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000291 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000292 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000293 case MachineOperand::MO_PCRelativeDisp:
294 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
295 abort ();
296 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000297 case MachineOperand::MO_GlobalAddress: {
Brian Gaeke002a50a2003-07-31 17:38:52 +0000298 if (!elideOffsetKeyword)
299 O << "OFFSET ";
300 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerd416f082004-10-15 04:44:53 +0000301 int Offset = MO.getOffset();
302 if (Offset > 0)
303 O << " + " << Offset;
304 else if (Offset < 0)
305 O << " - " << -Offset;
Chris Lattnerb7089442003-01-13 00:35:03 +0000306 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000307 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000308 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000309 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000310 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000311 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000312 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000313 }
314}
315
Chris Lattnerac5701c2004-10-04 07:24:48 +0000316void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
Chris Lattner3d3067b2002-11-21 20:44:15 +0000317 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000318
Chris Lattner3d3067b2002-11-21 20:44:15 +0000319 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000320 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000321 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerd416f082004-10-15 04:44:53 +0000322 const MachineOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000323
Chris Lattner0e0ed852004-10-17 07:16:32 +0000324 if (BaseReg.isFrameIndex()) {
325 O << "[frame slot #" << BaseReg.getFrameIndex();
326 if (DispSpec.getImmedValue())
327 O << " + " << DispSpec.getImmedValue();
328 O << "]";
329 return;
330 } else if (BaseReg.isConstantPoolIndex()) {
331 O << "[.CPI" << CurrentFnName << "_"
332 << BaseReg.getConstantPoolIndex();
333
334 if (IndexReg.getReg()) {
335 O << " + ";
336 if (ScaleVal != 1)
337 O << ScaleVal << "*";
338 printOp(IndexReg);
339 }
340
341 if (DispSpec.getImmedValue())
342 O << " + " << DispSpec.getImmedValue();
343 O << "]";
344 return;
345 }
346
Chris Lattner3d3067b2002-11-21 20:44:15 +0000347 O << "[";
348 bool NeedPlus = false;
349 if (BaseReg.getReg()) {
Chris Lattnerd416f082004-10-15 04:44:53 +0000350 printOp(BaseReg, true);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000351 NeedPlus = true;
352 }
353
354 if (IndexReg.getReg()) {
355 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000356 if (ScaleVal != 1)
357 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000358 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000359 NeedPlus = true;
360 }
361
Chris Lattnerd416f082004-10-15 04:44:53 +0000362 if (DispSpec.isGlobalAddress()) {
Chris Lattner0285a332002-12-28 20:25:38 +0000363 if (NeedPlus)
Chris Lattnerd416f082004-10-15 04:44:53 +0000364 O << " + ";
365 printOp(DispSpec, true);
366 } else {
367 int DispVal = DispSpec.getImmedValue();
Chris Lattnere11a9a92005-01-12 04:07:11 +0000368 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Chris Lattnerd416f082004-10-15 04:44:53 +0000369 if (NeedPlus)
370 if (DispVal > 0)
371 O << " + ";
372 else {
373 O << " - ";
374 DispVal = -DispVal;
375 }
376 O << DispVal;
377 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000378 }
379 O << "]";
380}
381
John Criswell4ffff9e2004-04-08 20:31:47 +0000382
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000383/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000384/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000385///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000386void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000387 ++EmittedInsts;
Chris Lattner85494292004-08-11 06:09:55 +0000388
Chris Lattner2a998bd2004-08-11 07:02:04 +0000389 // Call the autogenerated instruction printer routines.
Chris Lattner955f0962004-10-04 07:31:08 +0000390 printInstruction(MI);
Chris Lattner72614082002-10-25 22:55:53 +0000391}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000392
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000393bool X86IntelAsmPrinter::doInitialization(Module &M) {
Chris Lattner055acae2004-08-16 23:16:06 +0000394 AsmPrinter::doInitialization(M);
Chris Lattner93c1afa2003-08-11 19:35:26 +0000395 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000396 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000397 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
398 // instruction as a reference to the register named sp, and if you try to
399 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
400 // before being looked up in the symbol table. This creates spurious
401 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
402 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000403 O << "\t.intel_syntax\n";
Chris Lattner055acae2004-08-16 23:16:06 +0000404 return false;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000405}
406
Chris Lattnerac5701c2004-10-04 07:24:48 +0000407
408
409namespace {
410 struct X86ATTAsmPrinter : public X86SharedAsmPrinter {
411 X86ATTAsmPrinter(std::ostream &O, TargetMachine &TM)
412 : X86SharedAsmPrinter(O, TM) { }
413
414 virtual const char *getPassName() const {
415 return "X86 AT&T-Style Assembly Printer";
416 }
417
418 /// printInstruction - This method is automatically generated by tablegen
419 /// from the instruction set description. This method returns true if the
420 /// machine instruction was sufficiently described to print it, otherwise it
421 /// returns false.
422 bool printInstruction(const MachineInstr *MI);
423
424 // This method is used by the tablegen'erated instruction printer.
425 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
426 printOp(MI->getOperand(OpNo));
427 }
428
429 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
430 MVT::ValueType VT) {
431 printOp(MI->getOperand(OpNo), true); // Don't print '$' prefix.
432 }
433
434 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
435 MVT::ValueType VT) {
436 printMemReference(MI, OpNo);
437 }
438
439 void printMachineInstruction(const MachineInstr *MI);
440 void printOp(const MachineOperand &MO, bool isCallOperand = false);
441 void printMemReference(const MachineInstr *MI, unsigned Op);
442 bool runOnMachineFunction(MachineFunction &F);
443 };
444} // end of anonymous namespace
445
446
447// Include the auto-generated portion of the assembly writer.
Chris Lattner84b85c82004-12-16 17:33:24 +0000448#include "X86GenAsmWriter.inc"
Chris Lattnerac5701c2004-10-04 07:24:48 +0000449
450
451/// runOnMachineFunction - This uses the printMachineInstruction()
452/// method to print assembly for each instruction.
453///
454bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
455 setupMachineFunction(MF);
456 O << "\n\n";
457
458 // Print out constants referenced by the function
459 printConstantPool(MF.getConstantPool());
460
461 // Print out labels for the function.
462 O << "\t.text\n";
463 emitAlignment(4);
464 O << "\t.globl\t" << CurrentFnName << "\n";
465 O << "\t.type\t" << CurrentFnName << ", @function\n";
466 O << CurrentFnName << ":\n";
467
468 // Print out code for the function.
469 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
470 I != E; ++I) {
471 // Print a label for the basic block.
Chris Lattner4b2c09f2004-11-13 23:27:11 +0000472 if (I->pred_begin() != I->pred_end())
473 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
474 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Chris Lattnerac5701c2004-10-04 07:24:48 +0000475 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
476 II != E; ++II) {
477 // Print the assembly for the instruction.
478 O << "\t";
479 printMachineInstruction(II);
480 }
481 }
482
483 // We didn't modify anything.
484 return false;
485}
486
487void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
488 const MRegisterInfo &RI = *TM.getRegisterInfo();
489 switch (MO.getType()) {
490 case MachineOperand::MO_VirtualRegister:
491 case MachineOperand::MO_MachineRegister:
492 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
493 "Virtual registers should not make it this far!");
494 O << '%';
495 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
496 O << (char)tolower(*Name);
497 return;
498
499 case MachineOperand::MO_SignExtendedImmed:
500 case MachineOperand::MO_UnextendedImmed:
501 O << '$' << (int)MO.getImmedValue();
502 return;
503 case MachineOperand::MO_MachineBasicBlock: {
504 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
505 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
506 << "_" << MBBOp->getNumber () << "\t# "
507 << MBBOp->getBasicBlock ()->getName ();
508 return;
509 }
510 case MachineOperand::MO_PCRelativeDisp:
511 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
512 abort ();
513 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000514 case MachineOperand::MO_GlobalAddress: {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000515 if (!isCallOp) O << '$';
516 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerd416f082004-10-15 04:44:53 +0000517 int Offset = MO.getOffset();
518 if (Offset > 0)
519 O << "+" << Offset;
520 else if (Offset < 0)
521 O << Offset;
Chris Lattnerac5701c2004-10-04 07:24:48 +0000522 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000523 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000524 case MachineOperand::MO_ExternalSymbol:
525 if (!isCallOp) O << '$';
526 O << MO.getSymbolName();
527 return;
528 default:
529 O << "<unknown operand type>"; return;
Chris Lattnerad200712003-09-09 16:23:36 +0000530 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000531}
532
Chris Lattnerac5701c2004-10-04 07:24:48 +0000533void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
534 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerad200712003-09-09 16:23:36 +0000535
Chris Lattnerac5701c2004-10-04 07:24:48 +0000536 const MachineOperand &BaseReg = MI->getOperand(Op);
537 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
538 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerd416f082004-10-15 04:44:53 +0000539 const MachineOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattnerad200712003-09-09 16:23:36 +0000540
Chris Lattner0e0ed852004-10-17 07:16:32 +0000541 if (BaseReg.isFrameIndex()) {
542 O << "[frame slot #" << BaseReg.getFrameIndex();
543 if (DispSpec.getImmedValue())
544 O << " + " << DispSpec.getImmedValue();
545 O << "]";
546 return;
547 } else if (BaseReg.isConstantPoolIndex()) {
548 O << ".CPI" << CurrentFnName << "_"
549 << BaseReg.getConstantPoolIndex();
550 if (DispSpec.getImmedValue())
551 O << "+" << DispSpec.getImmedValue();
552 if (IndexReg.getReg()) {
553 O << "(,";
554 printOp(IndexReg);
555 if (ScaleVal != 1)
556 O << "," << ScaleVal;
557 O << ")";
558 }
559 return;
560 }
561
Chris Lattnerd416f082004-10-15 04:44:53 +0000562 if (DispSpec.isGlobalAddress()) {
563 printOp(DispSpec, true);
564 } else {
565 int DispVal = DispSpec.getImmedValue();
Chris Lattner9f2cb3d2005-01-12 04:05:19 +0000566 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
Chris Lattnerd416f082004-10-15 04:44:53 +0000567 O << DispVal;
Chris Lattnerac5701c2004-10-04 07:24:48 +0000568 }
569
Chris Lattnerd416f082004-10-15 04:44:53 +0000570 if (IndexReg.getReg() || BaseReg.getReg()) {
571 O << "(";
572 if (BaseReg.getReg())
573 printOp(BaseReg);
574
575 if (IndexReg.getReg()) {
576 O << ",";
577 printOp(IndexReg);
578 if (ScaleVal != 1)
579 O << "," << ScaleVal;
580 }
581
582 O << ")";
583 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000584}
585
586
587/// printMachineInstruction -- Print out a single X86 LLVM instruction
588/// MI in Intel syntax to the current output stream.
589///
590void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
591 ++EmittedInsts;
592 // Call the autogenerated instruction printer routines.
Chris Lattner955f0962004-10-04 07:31:08 +0000593 printInstruction(MI);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000594}
595
596
597/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
598/// for a MachineFunction to the given output stream, using the given target
599/// machine description.
600///
601FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
602 switch (AsmWriterFlavor) {
603 default: assert(0 && "Unknown asm flavor!");
604 case intel:
605 return new X86IntelAsmPrinter(o, tm);
606 case att:
607 return new X86ATTAsmPrinter(o, tm);
608 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000609}