blob: 24e92ed30532e9df65888727a2a34f77cc66954c [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)
Reid Spencerd632f492005-03-08 17:02:05 +000087 forCygwin = TT.find("cygwin") != std::string::npos ||
88 TT.find("mingw") != std::string::npos;
Reid Spencer5dc81f62005-01-23 03:52:14 +000089 else if (TT.empty()) {
Reid Spencerd632f492005-03-08 17:02:05 +000090#if defined(__CYGWIN__) || defined(__MINGW32__)
Reid Spencer5dc81f62005-01-23 03:52:14 +000091 forCygwin = true;
92#else
93 forCygwin = false;
94#endif
95 }
96 if (forCygwin)
97 GlobalPrefix = "_";
98 return AsmPrinter::doInitialization(M);
99}
100
Chris Lattnerac5701c2004-10-04 07:24:48 +0000101/// printConstantPool - Print to the current output stream assembly
102/// representations of the constants in the constant pool MCP. This is
103/// used to print out constants which have been "spilled to memory" by
104/// the code generator.
105///
106void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
107 const std::vector<Constant*> &CP = MCP->getConstants();
108 const TargetData &TD = TM.getTargetData();
109
110 if (CP.empty()) return;
111
112 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
113 O << "\t.section .rodata\n";
114 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
115 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
116 << *CP[i] << "\n";
117 emitGlobalConstant(CP[i]);
118 }
119}
120
121bool X86SharedAsmPrinter::doFinalization(Module &M) {
122 const TargetData &TD = TM.getTargetData();
123 std::string CurSection;
124
125 // Print out module-level global variables here.
Chris Lattnere4d5c442005-03-15 04:54:21 +0000126 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Chris Lattnerac5701c2004-10-04 07:24:48 +0000127 if (I->hasInitializer()) { // External global require no code
128 O << "\n\n";
129 std::string name = Mang->getValueName(I);
130 Constant *C = I->getInitializer();
131 unsigned Size = TD.getTypeSize(C->getType());
132 unsigned Align = TD.getTypeAlignmentShift(C->getType());
133
134 if (C->isNullValue() &&
135 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
136 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
137 SwitchSection(O, CurSection, ".data");
Reid Spencer5dc81f62005-01-23 03:52:14 +0000138 if (!forCygwin && I->hasInternalLinkage())
Chris Lattnerac5701c2004-10-04 07:24:48 +0000139 O << "\t.local " << name << "\n";
140
Reid Spencer5dc81f62005-01-23 03:52:14 +0000141 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType());
142 if (!forCygwin)
143 O << "," << (1 << Align);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000144 O << "\t\t# ";
145 WriteAsOperand(O, I, true, true, &M);
146 O << "\n";
147 } else {
148 switch (I->getLinkage()) {
149 case GlobalValue::LinkOnceLinkage:
150 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
151 // Nonnull linkonce -> weak
152 O << "\t.weak " << name << "\n";
153 SwitchSection(O, CurSection, "");
154 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
155 break;
156 case GlobalValue::AppendingLinkage:
157 // FIXME: appending linkage variables should go into a section of
158 // their name or something. For now, just emit them as external.
159 case GlobalValue::ExternalLinkage:
160 // If external or appending, declare as a global symbol
161 O << "\t.globl " << name << "\n";
162 // FALL THROUGH
163 case GlobalValue::InternalLinkage:
164 if (C->isNullValue())
165 SwitchSection(O, CurSection, ".bss");
166 else
167 SwitchSection(O, CurSection, ".data");
168 break;
Misha Brukmand2691fd2004-11-14 21:03:49 +0000169 case GlobalValue::GhostLinkage:
170 std::cerr << "GhostLinkage cannot appear in X86AsmPrinter!\n";
171 abort();
Chris Lattnerac5701c2004-10-04 07:24:48 +0000172 }
173
174 emitAlignment(Align);
Reid Spencer5dc81f62005-01-23 03:52:14 +0000175 if (!forCygwin) {
176 O << "\t.type " << name << ",@object\n";
177 O << "\t.size " << name << "," << Size << "\n";
178 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000179 O << name << ":\t\t\t\t# ";
180 WriteAsOperand(O, I, true, true, &M);
181 O << " = ";
182 WriteAsOperand(O, C, false, false, &M);
183 O << "\n";
184 emitGlobalConstant(C);
185 }
186 }
187
188 AsmPrinter::doFinalization(M);
189 return false; // success
190}
191
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000192namespace {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000193 struct X86IntelAsmPrinter : public X86SharedAsmPrinter {
194 X86IntelAsmPrinter(std::ostream &O, TargetMachine &TM)
195 : X86SharedAsmPrinter(O, TM) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000196
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000197 virtual const char *getPassName() const {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000198 return "X86 Intel-Style Assembly Printer";
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000199 }
200
Chris Lattner3fa861a2004-08-01 06:02:08 +0000201 /// printInstruction - This method is automatically generated by tablegen
202 /// from the instruction set description. This method returns true if the
203 /// machine instruction was sufficiently described to print it, otherwise it
204 /// returns false.
205 bool printInstruction(const MachineInstr *MI);
206
Chris Lattnerb12ee502004-08-01 07:43:46 +0000207 // This method is used by the tablegen'erated instruction printer.
Chris Lattner055acae2004-08-16 23:16:06 +0000208 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000209 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner25369cf2004-08-01 08:12:41 +0000210 if (MO.getType() == MachineOperand::MO_MachineRegister) {
211 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
212 // Bug Workaround: See note in Printer::doInitialization about %.
213 O << "%" << TM.getRegisterInfo()->get(MO.getReg()).Name;
214 } else {
215 printOp(MO);
216 }
Chris Lattnerb12ee502004-08-01 07:43:46 +0000217 }
218
Chris Lattner055acae2004-08-16 23:16:06 +0000219 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
220 MVT::ValueType VT) {
Chris Lattnere4ead0c2004-08-11 06:59:12 +0000221 printOp(MI->getOperand(OpNo), true); // Don't print "OFFSET".
222 }
223
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000224 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
225 MVT::ValueType VT) {
226 switch (VT) {
227 default: assert(0 && "Unknown arg size!");
228 case MVT::i8: O << "BYTE PTR "; break;
229 case MVT::i16: O << "WORD PTR "; break;
230 case MVT::i32:
231 case MVT::f32: O << "DWORD PTR "; break;
232 case MVT::i64:
233 case MVT::f64: O << "QWORD PTR "; break;
234 case MVT::f80: O << "XWORD PTR "; break;
235 }
236 printMemReference(MI, OpNo);
237 }
238
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000239 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000240 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000241 void printMemReference(const MachineInstr *MI, unsigned Op);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000242 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000243 bool doInitialization(Module &M);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000244 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000245} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000246
Chris Lattner3fa861a2004-08-01 06:02:08 +0000247
248// Include the auto-generated portion of the assembly writer.
Chris Lattner84b85c82004-12-16 17:33:24 +0000249#include "X86GenAsmWriter1.inc"
Chris Lattner3fa861a2004-08-01 06:02:08 +0000250
251
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000252/// runOnMachineFunction - This uses the printMachineInstruction()
253/// method to print assembly for each instruction.
254///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000255bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner055acae2004-08-16 23:16:06 +0000256 setupMachineFunction(MF);
Chris Lattnere0121322003-08-03 23:37:09 +0000257 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000258
Chris Lattnerb7089442003-01-13 00:35:03 +0000259 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000260 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000261
Brian Gaeke6559bb92002-11-14 22:32:30 +0000262 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000263 O << "\t.text\n";
Chris Lattnerc6393f82004-08-17 19:25:42 +0000264 emitAlignment(4);
Brian Gaeked7908f62003-06-27 00:00:48 +0000265 O << "\t.globl\t" << CurrentFnName << "\n";
Reid Spencer5dc81f62005-01-23 03:52:14 +0000266 if (!forCygwin)
267 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000268 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000269
270 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000271 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
272 I != E; ++I) {
Chris Lattner4b2c09f2004-11-13 23:27:11 +0000273 // Print a label for the basic block if there are any predecessors.
274 if (I->pred_begin() != I->pred_end())
275 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
276 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000277 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000278 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000279 // Print the assembly for the instruction.
280 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000281 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000282 }
Chris Lattner0285a332002-12-28 20:25:38 +0000283 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000284
285 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000286 return false;
287}
288
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000289void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnerac5701c2004-10-04 07:24:48 +0000290 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000291 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000292 switch (MO.getType()) {
293 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000294 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000295 O << "<" << V->getName() << ">";
296 return;
297 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000298 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000299 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000300 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000301 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000302 O << "%" << RI.get(MO.getReg()).Name;
303 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000304 O << "%reg" << MO.getReg();
305 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000306
307 case MachineOperand::MO_SignExtendedImmed:
308 case MachineOperand::MO_UnextendedImmed:
309 O << (int)MO.getImmedValue();
310 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000311 case MachineOperand::MO_MachineBasicBlock: {
312 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
313 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
314 << "_" << MBBOp->getNumber () << "\t# "
315 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000316 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000317 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000318 case MachineOperand::MO_PCRelativeDisp:
319 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
320 abort ();
321 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000322 case MachineOperand::MO_GlobalAddress: {
Brian Gaeke002a50a2003-07-31 17:38:52 +0000323 if (!elideOffsetKeyword)
324 O << "OFFSET ";
325 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerd416f082004-10-15 04:44:53 +0000326 int Offset = MO.getOffset();
327 if (Offset > 0)
328 O << " + " << Offset;
329 else if (Offset < 0)
330 O << " - " << -Offset;
Chris Lattnerb7089442003-01-13 00:35:03 +0000331 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000332 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000333 case MachineOperand::MO_ExternalSymbol:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000334 O << GlobalPrefix << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000335 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000336 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000337 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000338 }
339}
340
Chris Lattnerac5701c2004-10-04 07:24:48 +0000341void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
Chris Lattner3d3067b2002-11-21 20:44:15 +0000342 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000343
Chris Lattner3d3067b2002-11-21 20:44:15 +0000344 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000345 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000346 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerd416f082004-10-15 04:44:53 +0000347 const MachineOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000348
Chris Lattner0e0ed852004-10-17 07:16:32 +0000349 if (BaseReg.isFrameIndex()) {
350 O << "[frame slot #" << BaseReg.getFrameIndex();
351 if (DispSpec.getImmedValue())
352 O << " + " << DispSpec.getImmedValue();
353 O << "]";
354 return;
355 } else if (BaseReg.isConstantPoolIndex()) {
356 O << "[.CPI" << CurrentFnName << "_"
357 << BaseReg.getConstantPoolIndex();
358
359 if (IndexReg.getReg()) {
360 O << " + ";
361 if (ScaleVal != 1)
362 O << ScaleVal << "*";
363 printOp(IndexReg);
364 }
365
366 if (DispSpec.getImmedValue())
367 O << " + " << DispSpec.getImmedValue();
368 O << "]";
369 return;
370 }
371
Chris Lattner3d3067b2002-11-21 20:44:15 +0000372 O << "[";
373 bool NeedPlus = false;
374 if (BaseReg.getReg()) {
Chris Lattnerd416f082004-10-15 04:44:53 +0000375 printOp(BaseReg, true);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000376 NeedPlus = true;
377 }
378
379 if (IndexReg.getReg()) {
380 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000381 if (ScaleVal != 1)
382 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000383 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000384 NeedPlus = true;
385 }
386
Chris Lattnerd416f082004-10-15 04:44:53 +0000387 if (DispSpec.isGlobalAddress()) {
Chris Lattner0285a332002-12-28 20:25:38 +0000388 if (NeedPlus)
Chris Lattnerd416f082004-10-15 04:44:53 +0000389 O << " + ";
390 printOp(DispSpec, true);
391 } else {
392 int DispVal = DispSpec.getImmedValue();
Chris Lattnere11a9a92005-01-12 04:07:11 +0000393 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Chris Lattnerd416f082004-10-15 04:44:53 +0000394 if (NeedPlus)
395 if (DispVal > 0)
396 O << " + ";
397 else {
398 O << " - ";
399 DispVal = -DispVal;
400 }
401 O << DispVal;
402 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000403 }
404 O << "]";
405}
406
John Criswell4ffff9e2004-04-08 20:31:47 +0000407
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000408/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000409/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000410///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000411void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000412 ++EmittedInsts;
Chris Lattner85494292004-08-11 06:09:55 +0000413
Chris Lattner2a998bd2004-08-11 07:02:04 +0000414 // Call the autogenerated instruction printer routines.
Chris Lattner955f0962004-10-04 07:31:08 +0000415 printInstruction(MI);
Chris Lattner72614082002-10-25 22:55:53 +0000416}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000417
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000418bool X86IntelAsmPrinter::doInitialization(Module &M) {
Chris Lattner055acae2004-08-16 23:16:06 +0000419 AsmPrinter::doInitialization(M);
Chris Lattner93c1afa2003-08-11 19:35:26 +0000420 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000421 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000422 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
423 // instruction as a reference to the register named sp, and if you try to
424 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
425 // before being looked up in the symbol table. This creates spurious
426 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
427 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000428 O << "\t.intel_syntax\n";
Chris Lattner055acae2004-08-16 23:16:06 +0000429 return false;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000430}
431
Chris Lattnerac5701c2004-10-04 07:24:48 +0000432
433
434namespace {
435 struct X86ATTAsmPrinter : public X86SharedAsmPrinter {
436 X86ATTAsmPrinter(std::ostream &O, TargetMachine &TM)
437 : X86SharedAsmPrinter(O, TM) { }
438
439 virtual const char *getPassName() const {
440 return "X86 AT&T-Style Assembly Printer";
441 }
442
443 /// printInstruction - This method is automatically generated by tablegen
444 /// from the instruction set description. This method returns true if the
445 /// machine instruction was sufficiently described to print it, otherwise it
446 /// returns false.
447 bool printInstruction(const MachineInstr *MI);
448
449 // This method is used by the tablegen'erated instruction printer.
450 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
451 printOp(MI->getOperand(OpNo));
452 }
453
454 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
455 MVT::ValueType VT) {
456 printOp(MI->getOperand(OpNo), true); // Don't print '$' prefix.
457 }
458
459 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
460 MVT::ValueType VT) {
461 printMemReference(MI, OpNo);
462 }
463
464 void printMachineInstruction(const MachineInstr *MI);
465 void printOp(const MachineOperand &MO, bool isCallOperand = false);
466 void printMemReference(const MachineInstr *MI, unsigned Op);
467 bool runOnMachineFunction(MachineFunction &F);
468 };
469} // end of anonymous namespace
470
471
472// Include the auto-generated portion of the assembly writer.
Chris Lattner84b85c82004-12-16 17:33:24 +0000473#include "X86GenAsmWriter.inc"
Chris Lattnerac5701c2004-10-04 07:24:48 +0000474
475
476/// runOnMachineFunction - This uses the printMachineInstruction()
477/// method to print assembly for each instruction.
478///
479bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
480 setupMachineFunction(MF);
481 O << "\n\n";
482
483 // Print out constants referenced by the function
484 printConstantPool(MF.getConstantPool());
485
486 // Print out labels for the function.
487 O << "\t.text\n";
488 emitAlignment(4);
489 O << "\t.globl\t" << CurrentFnName << "\n";
Reid Spencer5dc81f62005-01-23 03:52:14 +0000490 if (!forCygwin)
491 O << "\t.type\t" << CurrentFnName << ", @function\n";
Chris Lattnerac5701c2004-10-04 07:24:48 +0000492 O << CurrentFnName << ":\n";
493
494 // Print out code for the function.
495 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
496 I != E; ++I) {
497 // Print a label for the basic block.
Chris Lattner4b2c09f2004-11-13 23:27:11 +0000498 if (I->pred_begin() != I->pred_end())
499 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
500 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Chris Lattnerac5701c2004-10-04 07:24:48 +0000501 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
502 II != E; ++II) {
503 // Print the assembly for the instruction.
504 O << "\t";
505 printMachineInstruction(II);
506 }
507 }
508
509 // We didn't modify anything.
510 return false;
511}
512
513void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
514 const MRegisterInfo &RI = *TM.getRegisterInfo();
515 switch (MO.getType()) {
516 case MachineOperand::MO_VirtualRegister:
517 case MachineOperand::MO_MachineRegister:
518 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
519 "Virtual registers should not make it this far!");
520 O << '%';
521 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
522 O << (char)tolower(*Name);
523 return;
524
525 case MachineOperand::MO_SignExtendedImmed:
526 case MachineOperand::MO_UnextendedImmed:
527 O << '$' << (int)MO.getImmedValue();
528 return;
529 case MachineOperand::MO_MachineBasicBlock: {
530 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
531 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
532 << "_" << MBBOp->getNumber () << "\t# "
533 << MBBOp->getBasicBlock ()->getName ();
534 return;
535 }
536 case MachineOperand::MO_PCRelativeDisp:
537 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
538 abort ();
539 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000540 case MachineOperand::MO_GlobalAddress: {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000541 if (!isCallOp) O << '$';
542 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerd416f082004-10-15 04:44:53 +0000543 int Offset = MO.getOffset();
544 if (Offset > 0)
545 O << "+" << Offset;
546 else if (Offset < 0)
547 O << Offset;
Chris Lattnerac5701c2004-10-04 07:24:48 +0000548 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000549 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000550 case MachineOperand::MO_ExternalSymbol:
551 if (!isCallOp) O << '$';
Reid Spencer5dc81f62005-01-23 03:52:14 +0000552 O << GlobalPrefix << MO.getSymbolName();
Chris Lattnerac5701c2004-10-04 07:24:48 +0000553 return;
554 default:
555 O << "<unknown operand type>"; return;
Chris Lattnerad200712003-09-09 16:23:36 +0000556 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000557}
558
Chris Lattnerac5701c2004-10-04 07:24:48 +0000559void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
560 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerad200712003-09-09 16:23:36 +0000561
Chris Lattnerac5701c2004-10-04 07:24:48 +0000562 const MachineOperand &BaseReg = MI->getOperand(Op);
563 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
564 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerd416f082004-10-15 04:44:53 +0000565 const MachineOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattnerad200712003-09-09 16:23:36 +0000566
Chris Lattner0e0ed852004-10-17 07:16:32 +0000567 if (BaseReg.isFrameIndex()) {
568 O << "[frame slot #" << BaseReg.getFrameIndex();
569 if (DispSpec.getImmedValue())
570 O << " + " << DispSpec.getImmedValue();
571 O << "]";
572 return;
573 } else if (BaseReg.isConstantPoolIndex()) {
574 O << ".CPI" << CurrentFnName << "_"
575 << BaseReg.getConstantPoolIndex();
576 if (DispSpec.getImmedValue())
577 O << "+" << DispSpec.getImmedValue();
578 if (IndexReg.getReg()) {
579 O << "(,";
580 printOp(IndexReg);
581 if (ScaleVal != 1)
582 O << "," << ScaleVal;
583 O << ")";
584 }
585 return;
586 }
587
Chris Lattnerd416f082004-10-15 04:44:53 +0000588 if (DispSpec.isGlobalAddress()) {
589 printOp(DispSpec, true);
590 } else {
591 int DispVal = DispSpec.getImmedValue();
Chris Lattner9f2cb3d2005-01-12 04:05:19 +0000592 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
Chris Lattnerd416f082004-10-15 04:44:53 +0000593 O << DispVal;
Chris Lattnerac5701c2004-10-04 07:24:48 +0000594 }
595
Chris Lattnerd416f082004-10-15 04:44:53 +0000596 if (IndexReg.getReg() || BaseReg.getReg()) {
597 O << "(";
598 if (BaseReg.getReg())
599 printOp(BaseReg);
600
601 if (IndexReg.getReg()) {
602 O << ",";
603 printOp(IndexReg);
604 if (ScaleVal != 1)
605 O << "," << ScaleVal;
606 }
607
608 O << ")";
609 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000610}
611
612
613/// printMachineInstruction -- Print out a single X86 LLVM instruction
614/// MI in Intel syntax to the current output stream.
615///
616void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
617 ++EmittedInsts;
618 // Call the autogenerated instruction printer routines.
Chris Lattner955f0962004-10-04 07:31:08 +0000619 printInstruction(MI);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000620}
621
622
623/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
624/// for a MachineFunction to the given output stream, using the given target
625/// machine description.
626///
627FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
628 switch (AsmWriterFlavor) {
Reid Spencer5dc81f62005-01-23 03:52:14 +0000629 default:
630 assert(0 && "Unknown asm flavor!");
Chris Lattnerac5701c2004-10-04 07:24:48 +0000631 case intel:
632 return new X86IntelAsmPrinter(o, tm);
633 case att:
634 return new X86ATTAsmPrinter(o, tm);
635 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000636}