blob: e5d483150781a758faf91abf9adee06d52956aaf [file] [log] [blame]
Misha Brukmancf7d3af2004-07-26 18:45:48 +00001//===-- X86AsmPrinter.cpp - Convert X86 LLVM code to Intel assembly -------===//
John Criswell482202a2003-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 Lattnerd92fb002002-10-25 22:55:53 +00009//
Misha Brukmana6025e62004-03-01 23:53:11 +000010// This file contains a printer that converts from our internal representation
Chris Lattnerd1ab3782004-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 Lattnerd92fb002002-10-25 22:55:53 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "X86.h"
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000018#include "X86TargetMachine.h"
Chris Lattner1a2e6f72003-08-11 20:06:16 +000019#include "llvm/Module.h"
Chris Lattner7b9020a2005-03-17 15:38:16 +000020#include "llvm/Type.h"
Chris Lattner1a2e6f72003-08-11 20:06:16 +000021#include "llvm/Assembly/Writer.h"
Chris Lattner9cf46452004-08-16 23:16:06 +000022#include "llvm/CodeGen/AsmPrinter.h"
Chris Lattner956e8372003-01-13 00:35:03 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerd5540022004-08-01 07:43:46 +000025#include "llvm/CodeGen/ValueTypes.h"
Chris Lattner1a2e6f72003-08-11 20:06:16 +000026#include "llvm/Target/TargetMachine.h"
Brian Gaeke46f8b712003-07-24 20:20:44 +000027#include "llvm/Support/Mangler.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000028#include "llvm/ADT/Statistic.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000029#include "llvm/Support/CommandLine.h"
Chris Lattner9f75a552004-02-14 06:00:36 +000030using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000031
Chris Lattnerd1ab3782004-10-04 07:31:08 +000032namespace {
33 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
34 enum AsmWriterFlavor { att, intel };
35
36 cl::opt<AsmWriterFlavor>
37 AsmWriterFlavor("x86-asm-syntax",
38 cl::desc("Choose style of code to emit from X86 backend:"),
39 cl::values(
40 clEnumVal(att, " Emit AT&T-style assembly"),
41 clEnumVal(intel, " Emit Intel-style assembly"),
42 clEnumValEnd),
43 cl::init(att));
44
45 struct X86SharedAsmPrinter : public AsmPrinter {
46 X86SharedAsmPrinter(std::ostream &O, TargetMachine &TM)
Reid Spencer30226da2005-01-23 03:52:14 +000047 : AsmPrinter(O, TM), forCygwin(false) { }
Chris Lattnerd1ab3782004-10-04 07:31:08 +000048
Reid Spencer30226da2005-01-23 03:52:14 +000049 bool doInitialization(Module &M);
Chris Lattnerd1ab3782004-10-04 07:31:08 +000050 void printConstantPool(MachineConstantPool *MCP);
51 bool doFinalization(Module &M);
Reid Spencer30226da2005-01-23 03:52:14 +000052 bool forCygwin;
Chris Lattnerd1ab3782004-10-04 07:31:08 +000053 };
54}
55
Chris Lattner68ab0be2004-10-04 07:24:48 +000056static bool isScale(const MachineOperand &MO) {
57 return MO.isImmediate() &&
58 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
59 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
60}
61
62static bool isMem(const MachineInstr *MI, unsigned Op) {
63 if (MI->getOperand(Op).isFrameIndex()) return true;
64 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
65 return Op+4 <= MI->getNumOperands() &&
66 MI->getOperand(Op ).isRegister() && isScale(MI->getOperand(Op+1)) &&
Chris Lattner19025d52004-10-15 04:44:53 +000067 MI->getOperand(Op+2).isRegister() && (MI->getOperand(Op+3).isImmediate() ||
68 MI->getOperand(Op+3).isGlobalAddress());
Chris Lattner68ab0be2004-10-04 07:24:48 +000069}
70
71// SwitchSection - Switch to the specified section of the executable if we are
72// not already in it!
73//
74static void SwitchSection(std::ostream &OS, std::string &CurSection,
75 const char *NewSection) {
76 if (CurSection != NewSection) {
77 CurSection = NewSection;
78 if (!CurSection.empty())
79 OS << "\t" << NewSection << "\n";
80 }
81}
82
Reid Spencer30226da2005-01-23 03:52:14 +000083/// doInitialization - determine
84bool X86SharedAsmPrinter::doInitialization(Module& M) {
85 forCygwin = false;
86 const std::string& TT = M.getTargetTriple();
87 if (TT.length() > 5)
Reid Spencer00658b82005-03-08 17:02:05 +000088 forCygwin = TT.find("cygwin") != std::string::npos ||
89 TT.find("mingw") != std::string::npos;
Reid Spencer30226da2005-01-23 03:52:14 +000090 else if (TT.empty()) {
Reid Spencer00658b82005-03-08 17:02:05 +000091#if defined(__CYGWIN__) || defined(__MINGW32__)
Reid Spencer30226da2005-01-23 03:52:14 +000092 forCygwin = true;
93#else
94 forCygwin = false;
95#endif
96 }
97 if (forCygwin)
98 GlobalPrefix = "_";
99 return AsmPrinter::doInitialization(M);
100}
101
Chris Lattner68ab0be2004-10-04 07:24:48 +0000102/// printConstantPool - Print to the current output stream assembly
103/// representations of the constants in the constant pool MCP. This is
104/// used to print out constants which have been "spilled to memory" by
105/// the code generator.
106///
107void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
108 const std::vector<Constant*> &CP = MCP->getConstants();
109 const TargetData &TD = TM.getTargetData();
110
111 if (CP.empty()) return;
112
113 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
114 O << "\t.section .rodata\n";
115 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
116 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
117 << *CP[i] << "\n";
118 emitGlobalConstant(CP[i]);
119 }
120}
121
122bool X86SharedAsmPrinter::doFinalization(Module &M) {
123 const TargetData &TD = TM.getTargetData();
124 std::string CurSection;
125
126 // Print out module-level global variables here.
Chris Lattner531f9e92005-03-15 04:54:21 +0000127 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Chris Lattner68ab0be2004-10-04 07:24:48 +0000128 if (I->hasInitializer()) { // External global require no code
129 O << "\n\n";
130 std::string name = Mang->getValueName(I);
131 Constant *C = I->getInitializer();
132 unsigned Size = TD.getTypeSize(C->getType());
133 unsigned Align = TD.getTypeAlignmentShift(C->getType());
134
135 if (C->isNullValue() &&
136 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
137 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
138 SwitchSection(O, CurSection, ".data");
Reid Spencer30226da2005-01-23 03:52:14 +0000139 if (!forCygwin && I->hasInternalLinkage())
Chris Lattner68ab0be2004-10-04 07:24:48 +0000140 O << "\t.local " << name << "\n";
141
Reid Spencer30226da2005-01-23 03:52:14 +0000142 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType());
143 if (!forCygwin)
144 O << "," << (1 << Align);
Chris Lattner68ab0be2004-10-04 07:24:48 +0000145 O << "\t\t# ";
146 WriteAsOperand(O, I, true, true, &M);
147 O << "\n";
148 } else {
149 switch (I->getLinkage()) {
150 case GlobalValue::LinkOnceLinkage:
151 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
152 // Nonnull linkonce -> weak
153 O << "\t.weak " << name << "\n";
154 SwitchSection(O, CurSection, "");
155 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
156 break;
157 case GlobalValue::AppendingLinkage:
158 // FIXME: appending linkage variables should go into a section of
159 // their name or something. For now, just emit them as external.
160 case GlobalValue::ExternalLinkage:
161 // If external or appending, declare as a global symbol
162 O << "\t.globl " << name << "\n";
163 // FALL THROUGH
164 case GlobalValue::InternalLinkage:
165 if (C->isNullValue())
166 SwitchSection(O, CurSection, ".bss");
167 else
168 SwitchSection(O, CurSection, ".data");
169 break;
Misha Brukman7f245d42004-11-14 21:03:49 +0000170 case GlobalValue::GhostLinkage:
171 std::cerr << "GhostLinkage cannot appear in X86AsmPrinter!\n";
172 abort();
Chris Lattner68ab0be2004-10-04 07:24:48 +0000173 }
174
175 emitAlignment(Align);
Reid Spencer30226da2005-01-23 03:52:14 +0000176 if (!forCygwin) {
177 O << "\t.type " << name << ",@object\n";
178 O << "\t.size " << name << "," << Size << "\n";
179 }
Chris Lattner68ab0be2004-10-04 07:24:48 +0000180 O << name << ":\t\t\t\t# ";
181 WriteAsOperand(O, I, true, true, &M);
182 O << " = ";
183 WriteAsOperand(O, C, false, false, &M);
184 O << "\n";
185 emitGlobalConstant(C);
186 }
187 }
188
189 AsmPrinter::doFinalization(M);
190 return false; // success
191}
192
Chris Lattner02a3d832002-10-29 22:37:54 +0000193namespace {
Chris Lattner68ab0be2004-10-04 07:24:48 +0000194 struct X86IntelAsmPrinter : public X86SharedAsmPrinter {
195 X86IntelAsmPrinter(std::ostream &O, TargetMachine &TM)
196 : X86SharedAsmPrinter(O, TM) { }
Brian Gaekec3998cb2003-07-23 18:37:06 +0000197
Chris Lattnerd06650a2002-12-15 21:13:40 +0000198 virtual const char *getPassName() const {
Chris Lattner68ab0be2004-10-04 07:24:48 +0000199 return "X86 Intel-Style Assembly Printer";
Chris Lattnerd06650a2002-12-15 21:13:40 +0000200 }
201
Chris Lattner9520d202004-08-01 06:02:08 +0000202 /// printInstruction - This method is automatically generated by tablegen
203 /// from the instruction set description. This method returns true if the
204 /// machine instruction was sufficiently described to print it, otherwise it
205 /// returns false.
206 bool printInstruction(const MachineInstr *MI);
207
Chris Lattnerd5540022004-08-01 07:43:46 +0000208 // This method is used by the tablegen'erated instruction printer.
Chris Lattner9cf46452004-08-16 23:16:06 +0000209 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
Chris Lattner09ee05b2004-08-11 02:25:00 +0000210 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner06cf67e2004-08-01 08:12:41 +0000211 if (MO.getType() == MachineOperand::MO_MachineRegister) {
212 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
213 // Bug Workaround: See note in Printer::doInitialization about %.
214 O << "%" << TM.getRegisterInfo()->get(MO.getReg()).Name;
215 } else {
216 printOp(MO);
217 }
Chris Lattnerd5540022004-08-01 07:43:46 +0000218 }
219
Chris Lattner9cf46452004-08-16 23:16:06 +0000220 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
221 MVT::ValueType VT) {
Chris Lattnera0bafce2004-08-11 06:59:12 +0000222 printOp(MI->getOperand(OpNo), true); // Don't print "OFFSET".
223 }
224
Chris Lattner09ee05b2004-08-11 02:25:00 +0000225 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
226 MVT::ValueType VT) {
227 switch (VT) {
228 default: assert(0 && "Unknown arg size!");
229 case MVT::i8: O << "BYTE PTR "; break;
230 case MVT::i16: O << "WORD PTR "; break;
231 case MVT::i32:
232 case MVT::f32: O << "DWORD PTR "; break;
233 case MVT::i64:
234 case MVT::f64: O << "QWORD PTR "; break;
235 case MVT::f80: O << "XWORD PTR "; break;
236 }
237 printMemReference(MI, OpNo);
238 }
239
Brian Gaeke46f8b712003-07-24 20:20:44 +0000240 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmanc968b872004-06-29 19:28:53 +0000241 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeke46f8b712003-07-24 20:20:44 +0000242 void printMemReference(const MachineInstr *MI, unsigned Op);
Brian Gaeke25e766a2003-06-25 18:01:07 +0000243 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000244 bool doInitialization(Module &M);
Chris Lattner02a3d832002-10-29 22:37:54 +0000245 };
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000246} // end of anonymous namespace
Chris Lattner02a3d832002-10-29 22:37:54 +0000247
Chris Lattner9520d202004-08-01 06:02:08 +0000248
249// Include the auto-generated portion of the assembly writer.
Chris Lattner20d74fd2004-12-16 17:33:24 +0000250#include "X86GenAsmWriter1.inc"
Chris Lattner9520d202004-08-01 06:02:08 +0000251
252
Brian Gaekec3998cb2003-07-23 18:37:06 +0000253/// runOnMachineFunction - This uses the printMachineInstruction()
254/// method to print assembly for each instruction.
255///
Chris Lattner56832602004-10-03 20:36:57 +0000256bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner9cf46452004-08-16 23:16:06 +0000257 setupMachineFunction(MF);
Chris Lattner5dcb6542003-08-03 23:37:09 +0000258 O << "\n\n";
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000259
Chris Lattner956e8372003-01-13 00:35:03 +0000260 // Print out constants referenced by the function
Brian Gaeke25e766a2003-06-25 18:01:07 +0000261 printConstantPool(MF.getConstantPool());
Chris Lattner956e8372003-01-13 00:35:03 +0000262
Brian Gaekee7454352002-11-14 22:32:30 +0000263 // Print out labels for the function.
Chris Lattner956e8372003-01-13 00:35:03 +0000264 O << "\t.text\n";
Chris Lattner866b58d2004-08-17 19:25:42 +0000265 emitAlignment(4);
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000266 O << "\t.globl\t" << CurrentFnName << "\n";
Reid Spencer30226da2005-01-23 03:52:14 +0000267 if (!forCygwin)
268 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000269 O << CurrentFnName << ":\n";
Brian Gaekee7454352002-11-14 22:32:30 +0000270
271 // Print out code for the function.
Chris Lattner1520c5a2002-12-28 20:25:38 +0000272 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
273 I != E; ++I) {
Chris Lattner56c4c992004-11-13 23:27:11 +0000274 // Print a label for the basic block if there are any predecessors.
275 if (I->pred_begin() != I->pred_end())
276 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
277 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Chris Lattner1520c5a2002-12-28 20:25:38 +0000278 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmanc968b872004-06-29 19:28:53 +0000279 II != E; ++II) {
Chris Lattner1520c5a2002-12-28 20:25:38 +0000280 // Print the assembly for the instruction.
281 O << "\t";
Alkis Evlogimenos80da8652004-02-12 02:27:10 +0000282 printMachineInstruction(II);
Brian Gaekee7454352002-11-14 22:32:30 +0000283 }
Chris Lattner1520c5a2002-12-28 20:25:38 +0000284 }
Brian Gaekee7454352002-11-14 22:32:30 +0000285
286 // We didn't modify anything.
Chris Lattner02a3d832002-10-29 22:37:54 +0000287 return false;
288}
289
Chris Lattner56832602004-10-03 20:36:57 +0000290void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattner68ab0be2004-10-04 07:24:48 +0000291 bool elideOffsetKeyword /* = false */) {
Brian Gaekea92dce42003-07-23 20:25:08 +0000292 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner5812f062002-11-18 06:56:51 +0000293 switch (MO.getType()) {
294 case MachineOperand::MO_VirtualRegister:
Chris Lattnerfb8032d2002-12-04 17:32:52 +0000295 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattner6425a502002-12-04 06:45:19 +0000296 O << "<" << V->getName() << ">";
297 return;
298 }
Chris Lattner956e8372003-01-13 00:35:03 +0000299 // FALLTHROUGH
Misha Brukman6e5d4932002-11-20 18:56:41 +0000300 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenosbbf53932004-02-15 21:37:17 +0000301 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke12b32532003-08-11 19:05:46 +0000302 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke1600c4a2003-08-13 18:15:15 +0000303 O << "%" << RI.get(MO.getReg()).Name;
304 else
Chris Lattner5812f062002-11-18 06:56:51 +0000305 O << "%reg" << MO.getReg();
306 return;
Chris Lattner177e9282002-11-21 02:00:20 +0000307
308 case MachineOperand::MO_SignExtendedImmed:
309 case MachineOperand::MO_UnextendedImmed:
310 O << (int)MO.getImmedValue();
311 return;
Brian Gaeke2b3a81c2004-05-14 06:54:57 +0000312 case MachineOperand::MO_MachineBasicBlock: {
313 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
314 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
315 << "_" << MBBOp->getNumber () << "\t# "
316 << MBBOp->getBasicBlock ()->getName ();
Chris Lattner08cd1ed2002-12-01 23:25:59 +0000317 return;
Chris Lattner230cffb2003-09-09 16:23:36 +0000318 }
Brian Gaeke2b3a81c2004-05-14 06:54:57 +0000319 case MachineOperand::MO_PCRelativeDisp:
320 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
321 abort ();
322 return;
Chris Lattner19025d52004-10-15 04:44:53 +0000323 case MachineOperand::MO_GlobalAddress: {
Brian Gaekea2d9f402003-07-31 17:38:52 +0000324 if (!elideOffsetKeyword)
325 O << "OFFSET ";
326 O << Mang->getValueName(MO.getGlobal());
Chris Lattner19025d52004-10-15 04:44:53 +0000327 int Offset = MO.getOffset();
328 if (Offset > 0)
329 O << " + " << Offset;
330 else if (Offset < 0)
331 O << " - " << -Offset;
Chris Lattner956e8372003-01-13 00:35:03 +0000332 return;
Chris Lattner19025d52004-10-15 04:44:53 +0000333 }
Chris Lattner956e8372003-01-13 00:35:03 +0000334 case MachineOperand::MO_ExternalSymbol:
Reid Spencer30226da2005-01-23 03:52:14 +0000335 O << GlobalPrefix << MO.getSymbolName();
Chris Lattner956e8372003-01-13 00:35:03 +0000336 return;
Chris Lattner5812f062002-11-18 06:56:51 +0000337 default:
Brian Gaeke25e766a2003-06-25 18:01:07 +0000338 O << "<unknown operand type>"; return;
Chris Lattner5812f062002-11-18 06:56:51 +0000339 }
340}
341
Chris Lattner68ab0be2004-10-04 07:24:48 +0000342void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
Chris Lattner61fafd352002-11-21 20:44:15 +0000343 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner956e8372003-01-13 00:35:03 +0000344
Chris Lattner61fafd352002-11-21 20:44:15 +0000345 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner1520c5a2002-12-28 20:25:38 +0000346 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner61fafd352002-11-21 20:44:15 +0000347 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner19025d52004-10-15 04:44:53 +0000348 const MachineOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattner61fafd352002-11-21 20:44:15 +0000349
Chris Lattneraf19d392004-10-17 07:16:32 +0000350 if (BaseReg.isFrameIndex()) {
351 O << "[frame slot #" << BaseReg.getFrameIndex();
352 if (DispSpec.getImmedValue())
353 O << " + " << DispSpec.getImmedValue();
354 O << "]";
355 return;
356 } else if (BaseReg.isConstantPoolIndex()) {
357 O << "[.CPI" << CurrentFnName << "_"
358 << BaseReg.getConstantPoolIndex();
359
360 if (IndexReg.getReg()) {
361 O << " + ";
362 if (ScaleVal != 1)
363 O << ScaleVal << "*";
364 printOp(IndexReg);
365 }
366
367 if (DispSpec.getImmedValue())
368 O << " + " << DispSpec.getImmedValue();
369 O << "]";
370 return;
371 }
372
Chris Lattner61fafd352002-11-21 20:44:15 +0000373 O << "[";
374 bool NeedPlus = false;
375 if (BaseReg.getReg()) {
Chris Lattner19025d52004-10-15 04:44:53 +0000376 printOp(BaseReg, true);
Chris Lattner61fafd352002-11-21 20:44:15 +0000377 NeedPlus = true;
378 }
379
380 if (IndexReg.getReg()) {
381 if (NeedPlus) O << " + ";
Chris Lattner1520c5a2002-12-28 20:25:38 +0000382 if (ScaleVal != 1)
383 O << ScaleVal << "*";
Brian Gaekea92dce42003-07-23 20:25:08 +0000384 printOp(IndexReg);
Chris Lattner61fafd352002-11-21 20:44:15 +0000385 NeedPlus = true;
386 }
387
Chris Lattner19025d52004-10-15 04:44:53 +0000388 if (DispSpec.isGlobalAddress()) {
Chris Lattner1520c5a2002-12-28 20:25:38 +0000389 if (NeedPlus)
Chris Lattner19025d52004-10-15 04:44:53 +0000390 O << " + ";
391 printOp(DispSpec, true);
392 } else {
393 int DispVal = DispSpec.getImmedValue();
Chris Lattnerbb4c14f2005-01-12 04:07:11 +0000394 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Chris Lattner19025d52004-10-15 04:44:53 +0000395 if (NeedPlus)
396 if (DispVal > 0)
397 O << " + ";
398 else {
399 O << " - ";
400 DispVal = -DispVal;
401 }
402 O << DispVal;
403 }
Chris Lattner61fafd352002-11-21 20:44:15 +0000404 }
405 O << "]";
406}
407
John Criswell10db0622004-04-08 20:31:47 +0000408
Brian Gaekec3998cb2003-07-23 18:37:06 +0000409/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekea92dce42003-07-23 20:25:08 +0000410/// MI in Intel syntax to the current output stream.
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000411///
Chris Lattner56832602004-10-03 20:36:57 +0000412void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Chris Lattner9520d202004-08-01 06:02:08 +0000413 ++EmittedInsts;
Chris Lattnerb9756362004-08-11 06:09:55 +0000414
Chris Lattnerd1bee6e2004-08-11 07:02:04 +0000415 // Call the autogenerated instruction printer routines.
Chris Lattnerd1ab3782004-10-04 07:31:08 +0000416 printInstruction(MI);
Chris Lattnerd92fb002002-10-25 22:55:53 +0000417}
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000418
Chris Lattner56832602004-10-03 20:36:57 +0000419bool X86IntelAsmPrinter::doInitialization(Module &M) {
Chris Lattner9cf46452004-08-16 23:16:06 +0000420 AsmPrinter::doInitialization(M);
Chris Lattner9a59f582003-08-11 19:35:26 +0000421 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke12b32532003-08-11 19:05:46 +0000422 //
Chris Lattner9a59f582003-08-11 19:35:26 +0000423 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
424 // instruction as a reference to the register named sp, and if you try to
425 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
426 // before being looked up in the symbol table. This creates spurious
427 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
428 // mode, and decorate all register names with percent signs.
Chris Lattner262c8322003-08-11 20:04:57 +0000429 O << "\t.intel_syntax\n";
Chris Lattner9cf46452004-08-16 23:16:06 +0000430 return false;
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000431}
432
Chris Lattner68ab0be2004-10-04 07:24:48 +0000433
434
435namespace {
436 struct X86ATTAsmPrinter : public X86SharedAsmPrinter {
437 X86ATTAsmPrinter(std::ostream &O, TargetMachine &TM)
438 : X86SharedAsmPrinter(O, TM) { }
439
440 virtual const char *getPassName() const {
441 return "X86 AT&T-Style Assembly Printer";
442 }
443
444 /// printInstruction - This method is automatically generated by tablegen
445 /// from the instruction set description. This method returns true if the
446 /// machine instruction was sufficiently described to print it, otherwise it
447 /// returns false.
448 bool printInstruction(const MachineInstr *MI);
449
450 // This method is used by the tablegen'erated instruction printer.
451 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
452 printOp(MI->getOperand(OpNo));
453 }
454
455 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
456 MVT::ValueType VT) {
457 printOp(MI->getOperand(OpNo), true); // Don't print '$' prefix.
458 }
459
460 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
461 MVT::ValueType VT) {
462 printMemReference(MI, OpNo);
463 }
464
465 void printMachineInstruction(const MachineInstr *MI);
466 void printOp(const MachineOperand &MO, bool isCallOperand = false);
467 void printMemReference(const MachineInstr *MI, unsigned Op);
468 bool runOnMachineFunction(MachineFunction &F);
469 };
470} // end of anonymous namespace
471
472
473// Include the auto-generated portion of the assembly writer.
Chris Lattner20d74fd2004-12-16 17:33:24 +0000474#include "X86GenAsmWriter.inc"
Chris Lattner68ab0be2004-10-04 07:24:48 +0000475
476
477/// runOnMachineFunction - This uses the printMachineInstruction()
478/// method to print assembly for each instruction.
479///
480bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
481 setupMachineFunction(MF);
482 O << "\n\n";
483
484 // Print out constants referenced by the function
485 printConstantPool(MF.getConstantPool());
486
487 // Print out labels for the function.
488 O << "\t.text\n";
489 emitAlignment(4);
490 O << "\t.globl\t" << CurrentFnName << "\n";
Reid Spencer30226da2005-01-23 03:52:14 +0000491 if (!forCygwin)
492 O << "\t.type\t" << CurrentFnName << ", @function\n";
Chris Lattner68ab0be2004-10-04 07:24:48 +0000493 O << CurrentFnName << ":\n";
494
495 // Print out code for the function.
496 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
497 I != E; ++I) {
498 // Print a label for the basic block.
Chris Lattner56c4c992004-11-13 23:27:11 +0000499 if (I->pred_begin() != I->pred_end())
500 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
501 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Chris Lattner68ab0be2004-10-04 07:24:48 +0000502 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
503 II != E; ++II) {
504 // Print the assembly for the instruction.
505 O << "\t";
506 printMachineInstruction(II);
507 }
508 }
509
510 // We didn't modify anything.
511 return false;
512}
513
514void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
515 const MRegisterInfo &RI = *TM.getRegisterInfo();
516 switch (MO.getType()) {
517 case MachineOperand::MO_VirtualRegister:
518 case MachineOperand::MO_MachineRegister:
519 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
520 "Virtual registers should not make it this far!");
521 O << '%';
522 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
523 O << (char)tolower(*Name);
524 return;
525
526 case MachineOperand::MO_SignExtendedImmed:
527 case MachineOperand::MO_UnextendedImmed:
528 O << '$' << (int)MO.getImmedValue();
529 return;
530 case MachineOperand::MO_MachineBasicBlock: {
531 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
532 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
533 << "_" << MBBOp->getNumber () << "\t# "
534 << MBBOp->getBasicBlock ()->getName ();
535 return;
536 }
537 case MachineOperand::MO_PCRelativeDisp:
538 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
539 abort ();
540 return;
Chris Lattner19025d52004-10-15 04:44:53 +0000541 case MachineOperand::MO_GlobalAddress: {
Chris Lattner68ab0be2004-10-04 07:24:48 +0000542 if (!isCallOp) O << '$';
543 O << Mang->getValueName(MO.getGlobal());
Chris Lattner19025d52004-10-15 04:44:53 +0000544 int Offset = MO.getOffset();
545 if (Offset > 0)
546 O << "+" << Offset;
547 else if (Offset < 0)
548 O << Offset;
Chris Lattner68ab0be2004-10-04 07:24:48 +0000549 return;
Chris Lattner19025d52004-10-15 04:44:53 +0000550 }
Chris Lattner68ab0be2004-10-04 07:24:48 +0000551 case MachineOperand::MO_ExternalSymbol:
552 if (!isCallOp) O << '$';
Reid Spencer30226da2005-01-23 03:52:14 +0000553 O << GlobalPrefix << MO.getSymbolName();
Chris Lattner68ab0be2004-10-04 07:24:48 +0000554 return;
555 default:
556 O << "<unknown operand type>"; return;
Chris Lattner230cffb2003-09-09 16:23:36 +0000557 }
Brian Gaekeb99d6842003-07-11 21:57:01 +0000558}
559
Chris Lattner68ab0be2004-10-04 07:24:48 +0000560void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
561 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner230cffb2003-09-09 16:23:36 +0000562
Chris Lattner68ab0be2004-10-04 07:24:48 +0000563 const MachineOperand &BaseReg = MI->getOperand(Op);
564 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
565 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner19025d52004-10-15 04:44:53 +0000566 const MachineOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattner230cffb2003-09-09 16:23:36 +0000567
Chris Lattneraf19d392004-10-17 07:16:32 +0000568 if (BaseReg.isFrameIndex()) {
569 O << "[frame slot #" << BaseReg.getFrameIndex();
570 if (DispSpec.getImmedValue())
571 O << " + " << DispSpec.getImmedValue();
572 O << "]";
573 return;
574 } else if (BaseReg.isConstantPoolIndex()) {
575 O << ".CPI" << CurrentFnName << "_"
576 << BaseReg.getConstantPoolIndex();
577 if (DispSpec.getImmedValue())
578 O << "+" << DispSpec.getImmedValue();
579 if (IndexReg.getReg()) {
580 O << "(,";
581 printOp(IndexReg);
582 if (ScaleVal != 1)
583 O << "," << ScaleVal;
584 O << ")";
585 }
586 return;
587 }
588
Chris Lattner19025d52004-10-15 04:44:53 +0000589 if (DispSpec.isGlobalAddress()) {
590 printOp(DispSpec, true);
591 } else {
592 int DispVal = DispSpec.getImmedValue();
Chris Lattnerb372fab2005-01-12 04:05:19 +0000593 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
Chris Lattner19025d52004-10-15 04:44:53 +0000594 O << DispVal;
Chris Lattner68ab0be2004-10-04 07:24:48 +0000595 }
596
Chris Lattner19025d52004-10-15 04:44:53 +0000597 if (IndexReg.getReg() || BaseReg.getReg()) {
598 O << "(";
599 if (BaseReg.getReg())
600 printOp(BaseReg);
601
602 if (IndexReg.getReg()) {
603 O << ",";
604 printOp(IndexReg);
605 if (ScaleVal != 1)
606 O << "," << ScaleVal;
607 }
608
609 O << ")";
610 }
Chris Lattner68ab0be2004-10-04 07:24:48 +0000611}
612
613
614/// printMachineInstruction -- Print out a single X86 LLVM instruction
615/// MI in Intel syntax to the current output stream.
616///
617void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
618 ++EmittedInsts;
619 // Call the autogenerated instruction printer routines.
Chris Lattnerd1ab3782004-10-04 07:31:08 +0000620 printInstruction(MI);
Chris Lattner68ab0be2004-10-04 07:24:48 +0000621}
622
623
624/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
625/// for a MachineFunction to the given output stream, using the given target
626/// machine description.
627///
628FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
629 switch (AsmWriterFlavor) {
Reid Spencer30226da2005-01-23 03:52:14 +0000630 default:
631 assert(0 && "Unknown asm flavor!");
Chris Lattner68ab0be2004-10-04 07:24:48 +0000632 case intel:
633 return new X86IntelAsmPrinter(o, tm);
634 case att:
635 return new X86ATTAsmPrinter(o, tm);
636 }
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000637}