blob: 8cb99b06ddf898791cd5e5472d8243ff3ba94c38 [file] [log] [blame]
Misha Brukman91b5ca82004-07-26 18:45:48 +00001//===-- X86AsmPrinter.cpp - Convert X86 LLVM code to Intel assembly -------===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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"
Chris Lattner5b3a4552005-03-17 15:38:16 +000020#include "llvm/Type.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000021#include "llvm/Assembly/Writer.h"
Chris Lattner055acae2004-08-16 23:16:06 +000022#include "llvm/CodeGen/AsmPrinter.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Alkis Evlogimenos03090662004-03-09 03:35:34 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb12ee502004-08-01 07:43:46 +000025#include "llvm/CodeGen/ValueTypes.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000026#include "llvm/Target/TargetMachine.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000027#include "llvm/Support/Mangler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/ADT/Statistic.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000029#include "llvm/Support/CommandLine.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000030using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Chris Lattner955f0962004-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 Spencer5dc81f62005-01-23 03:52:14 +000047 : AsmPrinter(O, TM), forCygwin(false) { }
Chris Lattner955f0962004-10-04 07:31:08 +000048
Reid Spencer5dc81f62005-01-23 03:52:14 +000049 bool doInitialization(Module &M);
Chris Lattner955f0962004-10-04 07:31:08 +000050 void printConstantPool(MachineConstantPool *MCP);
51 bool doFinalization(Module &M);
Reid Spencer5dc81f62005-01-23 03:52:14 +000052 bool forCygwin;
Chris Lattner955f0962004-10-04 07:31:08 +000053 };
54}
55
Chris Lattnerac5701c2004-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 Lattnerd416f082004-10-15 04:44:53 +000067 MI->getOperand(Op+2).isRegister() && (MI->getOperand(Op+3).isImmediate() ||
68 MI->getOperand(Op+3).isGlobalAddress());
Chris Lattnerac5701c2004-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 Spencer5dc81f62005-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)
Misha Brukman0e0a7a452005-04-21 23:38:14 +000088 forCygwin = TT.find("cygwin") != std::string::npos ||
Reid Spencerd632f492005-03-08 17:02:05 +000089 TT.find("mingw") != std::string::npos;
Reid Spencer5dc81f62005-01-23 03:52:14 +000090 else if (TT.empty()) {
Reid Spencerd632f492005-03-08 17:02:05 +000091#if defined(__CYGWIN__) || defined(__MINGW32__)
Reid Spencer5dc81f62005-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 Lattnerac5701c2004-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();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000110
Chris Lattnerac5701c2004-10-04 07:24:48 +0000111 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 Lattnere4d5c442005-03-15 04:54:21 +0000127 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Chris Lattnerac5701c2004-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
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000135 if (C->isNullValue() &&
Chris Lattnerac5701c2004-10-04 07:24:48 +0000136 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
137 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
138 SwitchSection(O, CurSection, ".data");
Reid Spencer5dc81f62005-01-23 03:52:14 +0000139 if (!forCygwin && I->hasInternalLinkage())
Chris Lattnerac5701c2004-10-04 07:24:48 +0000140 O << "\t.local " << name << "\n";
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000141
Reid Spencer5dc81f62005-01-23 03:52:14 +0000142 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType());
143 if (!forCygwin)
144 O << "," << (1 << Align);
Chris Lattnerac5701c2004-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 Brukmand2691fd2004-11-14 21:03:49 +0000170 case GlobalValue::GhostLinkage:
171 std::cerr << "GhostLinkage cannot appear in X86AsmPrinter!\n";
172 abort();
Chris Lattnerac5701c2004-10-04 07:24:48 +0000173 }
174
175 emitAlignment(Align);
John Criswell57c24502005-06-20 19:59:22 +0000176 if (!forCygwin) {
177 O << "\t.type " << name << ",@object\n";
178 O << "\t.size " << name << "," << Size << "\n";
Reid Spencer5dc81f62005-01-23 03:52:14 +0000179 }
Chris Lattnerac5701c2004-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 Lattnerb4f68ed2002-10-29 22:37:54 +0000193namespace {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000194 struct X86IntelAsmPrinter : public X86SharedAsmPrinter {
195 X86IntelAsmPrinter(std::ostream &O, TargetMachine &TM)
196 : X86SharedAsmPrinter(O, TM) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000197
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000198 virtual const char *getPassName() const {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000199 return "X86 Intel-Style Assembly Printer";
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000200 }
201
Chris Lattner3fa861a2004-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 Lattnerb12ee502004-08-01 07:43:46 +0000208 // This method is used by the tablegen'erated instruction printer.
Chris Lattner055acae2004-08-16 23:16:06 +0000209 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000210 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner25369cf2004-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 Lattnerb12ee502004-08-01 07:43:46 +0000218 }
219
Chris Lattner055acae2004-08-16 23:16:06 +0000220 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
221 MVT::ValueType VT) {
Chris Lattnere4ead0c2004-08-11 06:59:12 +0000222 printOp(MI->getOperand(OpNo), true); // Don't print "OFFSET".
223 }
224
Chris Lattner66fa1dc2004-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 Gaeked9fb37a2003-07-24 20:20:44 +0000240 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000241 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000242 void printMemReference(const MachineInstr *MI, unsigned Op);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000243 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000244 bool doInitialization(Module &M);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000245 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000246} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000247
Chris Lattner3fa861a2004-08-01 06:02:08 +0000248
249// Include the auto-generated portion of the assembly writer.
Chris Lattner84b85c82004-12-16 17:33:24 +0000250#include "X86GenAsmWriter1.inc"
Chris Lattner3fa861a2004-08-01 06:02:08 +0000251
252
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000253/// runOnMachineFunction - This uses the printMachineInstruction()
254/// method to print assembly for each instruction.
255///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000256bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner055acae2004-08-16 23:16:06 +0000257 setupMachineFunction(MF);
Chris Lattnere0121322003-08-03 23:37:09 +0000258 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000259
Chris Lattnerb7089442003-01-13 00:35:03 +0000260 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000261 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000262
Brian Gaeke6559bb92002-11-14 22:32:30 +0000263 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000264 O << "\t.text\n";
Chris Lattnerc6393f82004-08-17 19:25:42 +0000265 emitAlignment(4);
Brian Gaeked7908f62003-06-27 00:00:48 +0000266 O << "\t.globl\t" << CurrentFnName << "\n";
Reid Spencer5dc81f62005-01-23 03:52:14 +0000267 if (!forCygwin)
268 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000269 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000270
271 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000272 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
273 I != E; ++I) {
Chris Lattner4b2c09f2004-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 Lattner0285a332002-12-28 20:25:38 +0000278 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000279 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000280 // Print the assembly for the instruction.
281 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000282 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000283 }
Chris Lattner0285a332002-12-28 20:25:38 +0000284 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000285
286 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000287 return false;
288}
289
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000290void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnerac5701c2004-10-04 07:24:48 +0000291 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000292 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000293 switch (MO.getType()) {
294 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000295 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000296 O << "<" << V->getName() << ">";
297 return;
298 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000299 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000300 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000301 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000302 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000303 O << "%" << RI.get(MO.getReg()).Name;
304 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000305 O << "%reg" << MO.getReg();
306 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000307
308 case MachineOperand::MO_SignExtendedImmed:
309 case MachineOperand::MO_UnextendedImmed:
310 O << (int)MO.getImmedValue();
311 return;
Brian Gaeke3fb5d1a2004-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 Lattnerf8bafe82002-12-01 23:25:59 +0000317 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000318 }
Brian Gaeke3fb5d1a2004-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 Lattnerd416f082004-10-15 04:44:53 +0000323 case MachineOperand::MO_GlobalAddress: {
Brian Gaeke002a50a2003-07-31 17:38:52 +0000324 if (!elideOffsetKeyword)
325 O << "OFFSET ";
326 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerd416f082004-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 Lattnerb7089442003-01-13 00:35:03 +0000332 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000333 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000334 case MachineOperand::MO_ExternalSymbol:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000335 O << GlobalPrefix << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000336 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000337 default:
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000338 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000339 }
340}
341
Chris Lattnerac5701c2004-10-04 07:24:48 +0000342void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
Chris Lattner3d3067b2002-11-21 20:44:15 +0000343 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000344
Chris Lattner3d3067b2002-11-21 20:44:15 +0000345 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000346 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000347 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerd416f082004-10-15 04:44:53 +0000348 const MachineOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000349
Chris Lattner0e0ed852004-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 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000366
Chris Lattner0e0ed852004-10-17 07:16:32 +0000367 if (DispSpec.getImmedValue())
368 O << " + " << DispSpec.getImmedValue();
369 O << "]";
370 return;
371 }
372
Chris Lattner3d3067b2002-11-21 20:44:15 +0000373 O << "[";
374 bool NeedPlus = false;
375 if (BaseReg.getReg()) {
Chris Lattnerd416f082004-10-15 04:44:53 +0000376 printOp(BaseReg, true);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000377 NeedPlus = true;
378 }
379
380 if (IndexReg.getReg()) {
381 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000382 if (ScaleVal != 1)
383 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000384 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000385 NeedPlus = true;
386 }
387
Chris Lattnerd416f082004-10-15 04:44:53 +0000388 if (DispSpec.isGlobalAddress()) {
Chris Lattner0285a332002-12-28 20:25:38 +0000389 if (NeedPlus)
Chris Lattnerd416f082004-10-15 04:44:53 +0000390 O << " + ";
391 printOp(DispSpec, true);
392 } else {
393 int DispVal = DispSpec.getImmedValue();
Chris Lattnere11a9a92005-01-12 04:07:11 +0000394 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Chris Lattnerd416f082004-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 Lattner3d3067b2002-11-21 20:44:15 +0000404 }
405 O << "]";
406}
407
John Criswell4ffff9e2004-04-08 20:31:47 +0000408
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000409/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000410/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000411///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000412void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000413 ++EmittedInsts;
Chris Lattner85494292004-08-11 06:09:55 +0000414
Chris Lattner2a998bd2004-08-11 07:02:04 +0000415 // Call the autogenerated instruction printer routines.
Chris Lattner955f0962004-10-04 07:31:08 +0000416 printInstruction(MI);
Chris Lattner72614082002-10-25 22:55:53 +0000417}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000418
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000419bool X86IntelAsmPrinter::doInitialization(Module &M) {
Chris Lattner055acae2004-08-16 23:16:06 +0000420 AsmPrinter::doInitialization(M);
Chris Lattner93c1afa2003-08-11 19:35:26 +0000421 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000422 //
Chris Lattner93c1afa2003-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 Lattner67488a92003-08-11 20:04:57 +0000429 O << "\t.intel_syntax\n";
Chris Lattner055acae2004-08-16 23:16:06 +0000430 return false;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000431}
432
Chris Lattnerac5701c2004-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);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000468 bool runOnMachineFunction(MachineFunction &F);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000469 };
470} // end of anonymous namespace
471
472
473// Include the auto-generated portion of the assembly writer.
Chris Lattner84b85c82004-12-16 17:33:24 +0000474#include "X86GenAsmWriter.inc"
Chris Lattnerac5701c2004-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 Spencer5dc81f62005-01-23 03:52:14 +0000491 if (!forCygwin)
492 O << "\t.type\t" << CurrentFnName << ", @function\n";
Chris Lattnerac5701c2004-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 Lattner4b2c09f2004-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 Lattnerac5701c2004-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 Lattnerd416f082004-10-15 04:44:53 +0000541 case MachineOperand::MO_GlobalAddress: {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000542 if (!isCallOp) O << '$';
543 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerd416f082004-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 Lattnerac5701c2004-10-04 07:24:48 +0000549 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000550 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000551 case MachineOperand::MO_ExternalSymbol:
552 if (!isCallOp) O << '$';
Reid Spencer5dc81f62005-01-23 03:52:14 +0000553 O << GlobalPrefix << MO.getSymbolName();
Chris Lattnerac5701c2004-10-04 07:24:48 +0000554 return;
555 default:
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000556 O << "<unknown operand type>"; return;
Chris Lattnerad200712003-09-09 16:23:36 +0000557 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000558}
559
Chris Lattnerac5701c2004-10-04 07:24:48 +0000560void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
561 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerad200712003-09-09 16:23:36 +0000562
Chris Lattnerac5701c2004-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 Lattnerd416f082004-10-15 04:44:53 +0000566 const MachineOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattnerad200712003-09-09 16:23:36 +0000567
Chris Lattner0e0ed852004-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 Lattnerd416f082004-10-15 04:44:53 +0000589 if (DispSpec.isGlobalAddress()) {
590 printOp(DispSpec, true);
591 } else {
592 int DispVal = DispSpec.getImmedValue();
Chris Lattner9f2cb3d2005-01-12 04:05:19 +0000593 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
Chris Lattnerd416f082004-10-15 04:44:53 +0000594 O << DispVal;
Chris Lattnerac5701c2004-10-04 07:24:48 +0000595 }
596
Chris Lattnerd416f082004-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 Lattnerac5701c2004-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 Lattner955f0962004-10-04 07:31:08 +0000620 printInstruction(MI);
Chris Lattnerac5701c2004-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) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000630 default:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000631 assert(0 && "Unknown asm flavor!");
Chris Lattnerac5701c2004-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 Gaeke9e474c42003-06-19 19:32:32 +0000637}