blob: ce6d26e7d025a6b3481547c299253e2f49874fa4 [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)
Reid Spencer5dc81f62005-01-23 03:52:14 +000046 : AsmPrinter(O, TM), forCygwin(false) { }
Chris Lattner955f0962004-10-04 07:31:08 +000047
Reid Spencer5dc81f62005-01-23 03:52:14 +000048 bool doInitialization(Module &M);
Chris Lattner955f0962004-10-04 07:31:08 +000049 void printConstantPool(MachineConstantPool *MCP);
50 bool doFinalization(Module &M);
Reid Spencer5dc81f62005-01-23 03:52:14 +000051 bool forCygwin;
Chris Lattner955f0962004-10-04 07:31:08 +000052 };
53}
54
Chris Lattnerac5701c2004-10-04 07:24:48 +000055static bool isScale(const MachineOperand &MO) {
56 return MO.isImmediate() &&
57 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
58 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
59}
60
61static bool isMem(const MachineInstr *MI, unsigned Op) {
62 if (MI->getOperand(Op).isFrameIndex()) return true;
63 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
64 return Op+4 <= MI->getNumOperands() &&
65 MI->getOperand(Op ).isRegister() && isScale(MI->getOperand(Op+1)) &&
Chris Lattnerd416f082004-10-15 04:44:53 +000066 MI->getOperand(Op+2).isRegister() && (MI->getOperand(Op+3).isImmediate() ||
67 MI->getOperand(Op+3).isGlobalAddress());
Chris Lattnerac5701c2004-10-04 07:24:48 +000068}
69
70// SwitchSection - Switch to the specified section of the executable if we are
71// not already in it!
72//
73static void SwitchSection(std::ostream &OS, std::string &CurSection,
74 const char *NewSection) {
75 if (CurSection != NewSection) {
76 CurSection = NewSection;
77 if (!CurSection.empty())
78 OS << "\t" << NewSection << "\n";
79 }
80}
81
Reid Spencer5dc81f62005-01-23 03:52:14 +000082/// doInitialization - determine
83bool X86SharedAsmPrinter::doInitialization(Module& M) {
84 forCygwin = false;
85 const std::string& TT = M.getTargetTriple();
86 if (TT.length() > 5)
87 forCygwin = TT.find("cygwin") != std::string::npos;
88 else if (TT.empty()) {
89#ifdef __CYGWIN__
90 forCygwin = true;
91#else
92 forCygwin = false;
93#endif
94 }
95 if (forCygwin)
96 GlobalPrefix = "_";
97 return AsmPrinter::doInitialization(M);
98}
99
Chris Lattnerac5701c2004-10-04 07:24:48 +0000100/// printConstantPool - Print to the current output stream assembly
101/// representations of the constants in the constant pool MCP. This is
102/// used to print out constants which have been "spilled to memory" by
103/// the code generator.
104///
105void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
106 const std::vector<Constant*> &CP = MCP->getConstants();
107 const TargetData &TD = TM.getTargetData();
108
109 if (CP.empty()) return;
110
111 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
112 O << "\t.section .rodata\n";
113 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
114 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
115 << *CP[i] << "\n";
116 emitGlobalConstant(CP[i]);
117 }
118}
119
120bool X86SharedAsmPrinter::doFinalization(Module &M) {
121 const TargetData &TD = TM.getTargetData();
122 std::string CurSection;
123
124 // Print out module-level global variables here.
125 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
126 if (I->hasInitializer()) { // External global require no code
127 O << "\n\n";
128 std::string name = Mang->getValueName(I);
129 Constant *C = I->getInitializer();
130 unsigned Size = TD.getTypeSize(C->getType());
131 unsigned Align = TD.getTypeAlignmentShift(C->getType());
132
133 if (C->isNullValue() &&
134 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
135 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
136 SwitchSection(O, CurSection, ".data");
Reid Spencer5dc81f62005-01-23 03:52:14 +0000137 if (!forCygwin && I->hasInternalLinkage())
Chris Lattnerac5701c2004-10-04 07:24:48 +0000138 O << "\t.local " << name << "\n";
139
Reid Spencer5dc81f62005-01-23 03:52:14 +0000140 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType());
141 if (!forCygwin)
142 O << "," << (1 << Align);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000143 O << "\t\t# ";
144 WriteAsOperand(O, I, true, true, &M);
145 O << "\n";
146 } else {
147 switch (I->getLinkage()) {
148 case GlobalValue::LinkOnceLinkage:
149 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
150 // Nonnull linkonce -> weak
151 O << "\t.weak " << name << "\n";
152 SwitchSection(O, CurSection, "");
153 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
154 break;
155 case GlobalValue::AppendingLinkage:
156 // FIXME: appending linkage variables should go into a section of
157 // their name or something. For now, just emit them as external.
158 case GlobalValue::ExternalLinkage:
159 // If external or appending, declare as a global symbol
160 O << "\t.globl " << name << "\n";
161 // FALL THROUGH
162 case GlobalValue::InternalLinkage:
163 if (C->isNullValue())
164 SwitchSection(O, CurSection, ".bss");
165 else
166 SwitchSection(O, CurSection, ".data");
167 break;
Misha Brukmand2691fd2004-11-14 21:03:49 +0000168 case GlobalValue::GhostLinkage:
169 std::cerr << "GhostLinkage cannot appear in X86AsmPrinter!\n";
170 abort();
Chris Lattnerac5701c2004-10-04 07:24:48 +0000171 }
172
173 emitAlignment(Align);
Reid Spencer5dc81f62005-01-23 03:52:14 +0000174 if (!forCygwin) {
175 O << "\t.type " << name << ",@object\n";
176 O << "\t.size " << name << "," << Size << "\n";
177 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000178 O << name << ":\t\t\t\t# ";
179 WriteAsOperand(O, I, true, true, &M);
180 O << " = ";
181 WriteAsOperand(O, C, false, false, &M);
182 O << "\n";
183 emitGlobalConstant(C);
184 }
185 }
186
187 AsmPrinter::doFinalization(M);
188 return false; // success
189}
190
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000191namespace {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000192 struct X86IntelAsmPrinter : public X86SharedAsmPrinter {
193 X86IntelAsmPrinter(std::ostream &O, TargetMachine &TM)
194 : X86SharedAsmPrinter(O, TM) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000195
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000196 virtual const char *getPassName() const {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000197 return "X86 Intel-Style Assembly Printer";
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000198 }
199
Chris Lattner3fa861a2004-08-01 06:02:08 +0000200 /// printInstruction - This method is automatically generated by tablegen
201 /// from the instruction set description. This method returns true if the
202 /// machine instruction was sufficiently described to print it, otherwise it
203 /// returns false.
204 bool printInstruction(const MachineInstr *MI);
205
Chris Lattnerb12ee502004-08-01 07:43:46 +0000206 // This method is used by the tablegen'erated instruction printer.
Chris Lattner055acae2004-08-16 23:16:06 +0000207 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000208 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner25369cf2004-08-01 08:12:41 +0000209 if (MO.getType() == MachineOperand::MO_MachineRegister) {
210 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
211 // Bug Workaround: See note in Printer::doInitialization about %.
212 O << "%" << TM.getRegisterInfo()->get(MO.getReg()).Name;
213 } else {
214 printOp(MO);
215 }
Chris Lattnerb12ee502004-08-01 07:43:46 +0000216 }
217
Chris Lattner055acae2004-08-16 23:16:06 +0000218 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
219 MVT::ValueType VT) {
Chris Lattnere4ead0c2004-08-11 06:59:12 +0000220 printOp(MI->getOperand(OpNo), true); // Don't print "OFFSET".
221 }
222
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000223 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
224 MVT::ValueType VT) {
225 switch (VT) {
226 default: assert(0 && "Unknown arg size!");
227 case MVT::i8: O << "BYTE PTR "; break;
228 case MVT::i16: O << "WORD PTR "; break;
229 case MVT::i32:
230 case MVT::f32: O << "DWORD PTR "; break;
231 case MVT::i64:
232 case MVT::f64: O << "QWORD PTR "; break;
233 case MVT::f80: O << "XWORD PTR "; break;
234 }
235 printMemReference(MI, OpNo);
236 }
237
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000238 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000239 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000240 void printMemReference(const MachineInstr *MI, unsigned Op);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000241 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000242 bool doInitialization(Module &M);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000243 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000244} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000245
Chris Lattner3fa861a2004-08-01 06:02:08 +0000246
247// Include the auto-generated portion of the assembly writer.
Chris Lattner84b85c82004-12-16 17:33:24 +0000248#include "X86GenAsmWriter1.inc"
Chris Lattner3fa861a2004-08-01 06:02:08 +0000249
250
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000251/// runOnMachineFunction - This uses the printMachineInstruction()
252/// method to print assembly for each instruction.
253///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000254bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner055acae2004-08-16 23:16:06 +0000255 setupMachineFunction(MF);
Chris Lattnere0121322003-08-03 23:37:09 +0000256 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000257
Chris Lattnerb7089442003-01-13 00:35:03 +0000258 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000259 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000260
Brian Gaeke6559bb92002-11-14 22:32:30 +0000261 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000262 O << "\t.text\n";
Chris Lattnerc6393f82004-08-17 19:25:42 +0000263 emitAlignment(4);
Brian Gaeked7908f62003-06-27 00:00:48 +0000264 O << "\t.globl\t" << CurrentFnName << "\n";
Reid Spencer5dc81f62005-01-23 03:52:14 +0000265 if (!forCygwin)
266 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000267 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000268
269 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000270 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
271 I != E; ++I) {
Chris Lattner4b2c09f2004-11-13 23:27:11 +0000272 // Print a label for the basic block if there are any predecessors.
273 if (I->pred_begin() != I->pred_end())
274 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
275 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000276 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000277 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000278 // Print the assembly for the instruction.
279 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000280 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000281 }
Chris Lattner0285a332002-12-28 20:25:38 +0000282 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000283
284 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000285 return false;
286}
287
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000288void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnerac5701c2004-10-04 07:24:48 +0000289 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000290 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000291 switch (MO.getType()) {
292 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000293 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000294 O << "<" << V->getName() << ">";
295 return;
296 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000297 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000298 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000299 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000300 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000301 O << "%" << RI.get(MO.getReg()).Name;
302 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000303 O << "%reg" << MO.getReg();
304 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000305
306 case MachineOperand::MO_SignExtendedImmed:
307 case MachineOperand::MO_UnextendedImmed:
308 O << (int)MO.getImmedValue();
309 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000310 case MachineOperand::MO_MachineBasicBlock: {
311 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
312 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
313 << "_" << MBBOp->getNumber () << "\t# "
314 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000315 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000316 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000317 case MachineOperand::MO_PCRelativeDisp:
318 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
319 abort ();
320 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000321 case MachineOperand::MO_GlobalAddress: {
Brian Gaeke002a50a2003-07-31 17:38:52 +0000322 if (!elideOffsetKeyword)
323 O << "OFFSET ";
324 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerd416f082004-10-15 04:44:53 +0000325 int Offset = MO.getOffset();
326 if (Offset > 0)
327 O << " + " << Offset;
328 else if (Offset < 0)
329 O << " - " << -Offset;
Chris Lattnerb7089442003-01-13 00:35:03 +0000330 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000331 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000332 case MachineOperand::MO_ExternalSymbol:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000333 O << GlobalPrefix << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000334 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000335 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000336 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000337 }
338}
339
Chris Lattnerac5701c2004-10-04 07:24:48 +0000340void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
Chris Lattner3d3067b2002-11-21 20:44:15 +0000341 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000342
Chris Lattner3d3067b2002-11-21 20:44:15 +0000343 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000344 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000345 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerd416f082004-10-15 04:44:53 +0000346 const MachineOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000347
Chris Lattner0e0ed852004-10-17 07:16:32 +0000348 if (BaseReg.isFrameIndex()) {
349 O << "[frame slot #" << BaseReg.getFrameIndex();
350 if (DispSpec.getImmedValue())
351 O << " + " << DispSpec.getImmedValue();
352 O << "]";
353 return;
354 } else if (BaseReg.isConstantPoolIndex()) {
355 O << "[.CPI" << CurrentFnName << "_"
356 << BaseReg.getConstantPoolIndex();
357
358 if (IndexReg.getReg()) {
359 O << " + ";
360 if (ScaleVal != 1)
361 O << ScaleVal << "*";
362 printOp(IndexReg);
363 }
364
365 if (DispSpec.getImmedValue())
366 O << " + " << DispSpec.getImmedValue();
367 O << "]";
368 return;
369 }
370
Chris Lattner3d3067b2002-11-21 20:44:15 +0000371 O << "[";
372 bool NeedPlus = false;
373 if (BaseReg.getReg()) {
Chris Lattnerd416f082004-10-15 04:44:53 +0000374 printOp(BaseReg, true);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000375 NeedPlus = true;
376 }
377
378 if (IndexReg.getReg()) {
379 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000380 if (ScaleVal != 1)
381 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000382 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000383 NeedPlus = true;
384 }
385
Chris Lattnerd416f082004-10-15 04:44:53 +0000386 if (DispSpec.isGlobalAddress()) {
Chris Lattner0285a332002-12-28 20:25:38 +0000387 if (NeedPlus)
Chris Lattnerd416f082004-10-15 04:44:53 +0000388 O << " + ";
389 printOp(DispSpec, true);
390 } else {
391 int DispVal = DispSpec.getImmedValue();
Chris Lattnere11a9a92005-01-12 04:07:11 +0000392 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Chris Lattnerd416f082004-10-15 04:44:53 +0000393 if (NeedPlus)
394 if (DispVal > 0)
395 O << " + ";
396 else {
397 O << " - ";
398 DispVal = -DispVal;
399 }
400 O << DispVal;
401 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000402 }
403 O << "]";
404}
405
John Criswell4ffff9e2004-04-08 20:31:47 +0000406
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000407/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000408/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000409///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000410void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000411 ++EmittedInsts;
Chris Lattner85494292004-08-11 06:09:55 +0000412
Chris Lattner2a998bd2004-08-11 07:02:04 +0000413 // Call the autogenerated instruction printer routines.
Chris Lattner955f0962004-10-04 07:31:08 +0000414 printInstruction(MI);
Chris Lattner72614082002-10-25 22:55:53 +0000415}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000416
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000417bool X86IntelAsmPrinter::doInitialization(Module &M) {
Chris Lattner055acae2004-08-16 23:16:06 +0000418 AsmPrinter::doInitialization(M);
Chris Lattner93c1afa2003-08-11 19:35:26 +0000419 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000420 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000421 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
422 // instruction as a reference to the register named sp, and if you try to
423 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
424 // before being looked up in the symbol table. This creates spurious
425 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
426 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000427 O << "\t.intel_syntax\n";
Chris Lattner055acae2004-08-16 23:16:06 +0000428 return false;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000429}
430
Chris Lattnerac5701c2004-10-04 07:24:48 +0000431
432
433namespace {
434 struct X86ATTAsmPrinter : public X86SharedAsmPrinter {
435 X86ATTAsmPrinter(std::ostream &O, TargetMachine &TM)
436 : X86SharedAsmPrinter(O, TM) { }
437
438 virtual const char *getPassName() const {
439 return "X86 AT&T-Style Assembly Printer";
440 }
441
442 /// printInstruction - This method is automatically generated by tablegen
443 /// from the instruction set description. This method returns true if the
444 /// machine instruction was sufficiently described to print it, otherwise it
445 /// returns false.
446 bool printInstruction(const MachineInstr *MI);
447
448 // This method is used by the tablegen'erated instruction printer.
449 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
450 printOp(MI->getOperand(OpNo));
451 }
452
453 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
454 MVT::ValueType VT) {
455 printOp(MI->getOperand(OpNo), true); // Don't print '$' prefix.
456 }
457
458 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
459 MVT::ValueType VT) {
460 printMemReference(MI, OpNo);
461 }
462
463 void printMachineInstruction(const MachineInstr *MI);
464 void printOp(const MachineOperand &MO, bool isCallOperand = false);
465 void printMemReference(const MachineInstr *MI, unsigned Op);
466 bool runOnMachineFunction(MachineFunction &F);
467 };
468} // end of anonymous namespace
469
470
471// Include the auto-generated portion of the assembly writer.
Chris Lattner84b85c82004-12-16 17:33:24 +0000472#include "X86GenAsmWriter.inc"
Chris Lattnerac5701c2004-10-04 07:24:48 +0000473
474
475/// runOnMachineFunction - This uses the printMachineInstruction()
476/// method to print assembly for each instruction.
477///
478bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
479 setupMachineFunction(MF);
480 O << "\n\n";
481
482 // Print out constants referenced by the function
483 printConstantPool(MF.getConstantPool());
484
485 // Print out labels for the function.
486 O << "\t.text\n";
487 emitAlignment(4);
488 O << "\t.globl\t" << CurrentFnName << "\n";
Reid Spencer5dc81f62005-01-23 03:52:14 +0000489 if (!forCygwin)
490 O << "\t.type\t" << CurrentFnName << ", @function\n";
Chris Lattnerac5701c2004-10-04 07:24:48 +0000491 O << CurrentFnName << ":\n";
492
493 // Print out code for the function.
494 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
495 I != E; ++I) {
496 // Print a label for the basic block.
Chris Lattner4b2c09f2004-11-13 23:27:11 +0000497 if (I->pred_begin() != I->pred_end())
498 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
499 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Chris Lattnerac5701c2004-10-04 07:24:48 +0000500 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
501 II != E; ++II) {
502 // Print the assembly for the instruction.
503 O << "\t";
504 printMachineInstruction(II);
505 }
506 }
507
508 // We didn't modify anything.
509 return false;
510}
511
512void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
513 const MRegisterInfo &RI = *TM.getRegisterInfo();
514 switch (MO.getType()) {
515 case MachineOperand::MO_VirtualRegister:
516 case MachineOperand::MO_MachineRegister:
517 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
518 "Virtual registers should not make it this far!");
519 O << '%';
520 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
521 O << (char)tolower(*Name);
522 return;
523
524 case MachineOperand::MO_SignExtendedImmed:
525 case MachineOperand::MO_UnextendedImmed:
526 O << '$' << (int)MO.getImmedValue();
527 return;
528 case MachineOperand::MO_MachineBasicBlock: {
529 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
530 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
531 << "_" << MBBOp->getNumber () << "\t# "
532 << MBBOp->getBasicBlock ()->getName ();
533 return;
534 }
535 case MachineOperand::MO_PCRelativeDisp:
536 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
537 abort ();
538 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000539 case MachineOperand::MO_GlobalAddress: {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000540 if (!isCallOp) O << '$';
541 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerd416f082004-10-15 04:44:53 +0000542 int Offset = MO.getOffset();
543 if (Offset > 0)
544 O << "+" << Offset;
545 else if (Offset < 0)
546 O << Offset;
Chris Lattnerac5701c2004-10-04 07:24:48 +0000547 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000548 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000549 case MachineOperand::MO_ExternalSymbol:
550 if (!isCallOp) O << '$';
Reid Spencer5dc81f62005-01-23 03:52:14 +0000551 O << GlobalPrefix << MO.getSymbolName();
Chris Lattnerac5701c2004-10-04 07:24:48 +0000552 return;
553 default:
554 O << "<unknown operand type>"; return;
Chris Lattnerad200712003-09-09 16:23:36 +0000555 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000556}
557
Chris Lattnerac5701c2004-10-04 07:24:48 +0000558void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
559 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerad200712003-09-09 16:23:36 +0000560
Chris Lattnerac5701c2004-10-04 07:24:48 +0000561 const MachineOperand &BaseReg = MI->getOperand(Op);
562 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
563 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerd416f082004-10-15 04:44:53 +0000564 const MachineOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattnerad200712003-09-09 16:23:36 +0000565
Chris Lattner0e0ed852004-10-17 07:16:32 +0000566 if (BaseReg.isFrameIndex()) {
567 O << "[frame slot #" << BaseReg.getFrameIndex();
568 if (DispSpec.getImmedValue())
569 O << " + " << DispSpec.getImmedValue();
570 O << "]";
571 return;
572 } else if (BaseReg.isConstantPoolIndex()) {
573 O << ".CPI" << CurrentFnName << "_"
574 << BaseReg.getConstantPoolIndex();
575 if (DispSpec.getImmedValue())
576 O << "+" << DispSpec.getImmedValue();
577 if (IndexReg.getReg()) {
578 O << "(,";
579 printOp(IndexReg);
580 if (ScaleVal != 1)
581 O << "," << ScaleVal;
582 O << ")";
583 }
584 return;
585 }
586
Chris Lattnerd416f082004-10-15 04:44:53 +0000587 if (DispSpec.isGlobalAddress()) {
588 printOp(DispSpec, true);
589 } else {
590 int DispVal = DispSpec.getImmedValue();
Chris Lattner9f2cb3d2005-01-12 04:05:19 +0000591 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
Chris Lattnerd416f082004-10-15 04:44:53 +0000592 O << DispVal;
Chris Lattnerac5701c2004-10-04 07:24:48 +0000593 }
594
Chris Lattnerd416f082004-10-15 04:44:53 +0000595 if (IndexReg.getReg() || BaseReg.getReg()) {
596 O << "(";
597 if (BaseReg.getReg())
598 printOp(BaseReg);
599
600 if (IndexReg.getReg()) {
601 O << ",";
602 printOp(IndexReg);
603 if (ScaleVal != 1)
604 O << "," << ScaleVal;
605 }
606
607 O << ")";
608 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000609}
610
611
612/// printMachineInstruction -- Print out a single X86 LLVM instruction
613/// MI in Intel syntax to the current output stream.
614///
615void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
616 ++EmittedInsts;
617 // Call the autogenerated instruction printer routines.
Chris Lattner955f0962004-10-04 07:31:08 +0000618 printInstruction(MI);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000619}
620
621
622/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
623/// for a MachineFunction to the given output stream, using the given target
624/// machine description.
625///
626FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
627 switch (AsmWriterFlavor) {
Reid Spencer5dc81f62005-01-23 03:52:14 +0000628 default:
629 assert(0 && "Unknown asm flavor!");
Chris Lattnerac5701c2004-10-04 07:24:48 +0000630 case intel:
631 return new X86IntelAsmPrinter(o, tm);
632 case att:
633 return new X86ATTAsmPrinter(o, tm);
634 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000635}