blob: bcc0a0cb5d47637a07808e37d0b59af77f89751b [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)
Nate Begeman9d19eb42005-06-30 00:53:20 +000047 : AsmPrinter(O, TM), forCygwin(false), forDarwin(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;
Nate Begeman9d19eb42005-06-30 00:53:20 +000053 bool forDarwin;
Chris Lattner955f0962004-10-04 07:31:08 +000054 };
55}
56
Chris Lattnerac5701c2004-10-04 07:24:48 +000057static bool isScale(const MachineOperand &MO) {
58 return MO.isImmediate() &&
59 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
60 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
61}
62
63static bool isMem(const MachineInstr *MI, unsigned Op) {
64 if (MI->getOperand(Op).isFrameIndex()) return true;
65 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
66 return Op+4 <= MI->getNumOperands() &&
67 MI->getOperand(Op ).isRegister() && isScale(MI->getOperand(Op+1)) &&
Chris Lattnerd416f082004-10-15 04:44:53 +000068 MI->getOperand(Op+2).isRegister() && (MI->getOperand(Op+3).isImmediate() ||
69 MI->getOperand(Op+3).isGlobalAddress());
Chris Lattnerac5701c2004-10-04 07:24:48 +000070}
71
72// SwitchSection - Switch to the specified section of the executable if we are
73// not already in it!
74//
75static void SwitchSection(std::ostream &OS, std::string &CurSection,
76 const char *NewSection) {
77 if (CurSection != NewSection) {
78 CurSection = NewSection;
79 if (!CurSection.empty())
80 OS << "\t" << NewSection << "\n";
81 }
82}
83
Reid Spencer5dc81f62005-01-23 03:52:14 +000084/// doInitialization - determine
85bool X86SharedAsmPrinter::doInitialization(Module& M) {
Reid Spencer5dc81f62005-01-23 03:52:14 +000086 const std::string& TT = M.getTargetTriple();
Nate Begeman9d19eb42005-06-30 00:53:20 +000087 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;
Nate Begeman9d19eb42005-06-30 00:53:20 +000090 forDarwin = TT.find("darwin") != std::string::npos;
91 } else if (TT.empty()) {
Reid Spencerd632f492005-03-08 17:02:05 +000092#if defined(__CYGWIN__) || defined(__MINGW32__)
Reid Spencer5dc81f62005-01-23 03:52:14 +000093 forCygwin = true;
Nate Begeman9d19eb42005-06-30 00:53:20 +000094#elif defined(__MACOSX__)
95 forDarwin = true;
Reid Spencer5dc81f62005-01-23 03:52:14 +000096#endif
97 }
Nate Begeman9d19eb42005-06-30 00:53:20 +000098 if (forCygwin || forDarwin)
Reid Spencer5dc81f62005-01-23 03:52:14 +000099 GlobalPrefix = "_";
Nate Begeman9d19eb42005-06-30 00:53:20 +0000100 if (forDarwin)
101 AlignmentIsInBytes = false;
Reid Spencer5dc81f62005-01-23 03:52:14 +0000102 return AsmPrinter::doInitialization(M);
103}
104
Chris Lattnerac5701c2004-10-04 07:24:48 +0000105/// printConstantPool - Print to the current output stream assembly
106/// representations of the constants in the constant pool MCP. This is
107/// used to print out constants which have been "spilled to memory" by
108/// the code generator.
109///
110void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
111 const std::vector<Constant*> &CP = MCP->getConstants();
112 const TargetData &TD = TM.getTargetData();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000113
Chris Lattnerac5701c2004-10-04 07:24:48 +0000114 if (CP.empty()) return;
115
116 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
117 O << "\t.section .rodata\n";
118 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
119 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
120 << *CP[i] << "\n";
121 emitGlobalConstant(CP[i]);
122 }
123}
124
125bool X86SharedAsmPrinter::doFinalization(Module &M) {
126 const TargetData &TD = TM.getTargetData();
127 std::string CurSection;
128
129 // Print out module-level global variables here.
Chris Lattnere4d5c442005-03-15 04:54:21 +0000130 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Chris Lattnerac5701c2004-10-04 07:24:48 +0000131 if (I->hasInitializer()) { // External global require no code
132 O << "\n\n";
133 std::string name = Mang->getValueName(I);
134 Constant *C = I->getInitializer();
135 unsigned Size = TD.getTypeSize(C->getType());
136 unsigned Align = TD.getTypeAlignmentShift(C->getType());
137
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000138 if (C->isNullValue() &&
Chris Lattnerac5701c2004-10-04 07:24:48 +0000139 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
140 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
141 SwitchSection(O, CurSection, ".data");
Reid Spencer5dc81f62005-01-23 03:52:14 +0000142 if (!forCygwin && I->hasInternalLinkage())
Chris Lattnerac5701c2004-10-04 07:24:48 +0000143 O << "\t.local " << name << "\n";
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000144
Reid Spencer5dc81f62005-01-23 03:52:14 +0000145 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType());
146 if (!forCygwin)
147 O << "," << (1 << Align);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000148 O << "\t\t# ";
149 WriteAsOperand(O, I, true, true, &M);
150 O << "\n";
151 } else {
152 switch (I->getLinkage()) {
153 case GlobalValue::LinkOnceLinkage:
154 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
155 // Nonnull linkonce -> weak
156 O << "\t.weak " << name << "\n";
157 SwitchSection(O, CurSection, "");
158 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
159 break;
160 case GlobalValue::AppendingLinkage:
161 // FIXME: appending linkage variables should go into a section of
162 // their name or something. For now, just emit them as external.
163 case GlobalValue::ExternalLinkage:
164 // If external or appending, declare as a global symbol
165 O << "\t.globl " << name << "\n";
166 // FALL THROUGH
167 case GlobalValue::InternalLinkage:
168 if (C->isNullValue())
169 SwitchSection(O, CurSection, ".bss");
170 else
171 SwitchSection(O, CurSection, ".data");
172 break;
Misha Brukmand2691fd2004-11-14 21:03:49 +0000173 case GlobalValue::GhostLinkage:
174 std::cerr << "GhostLinkage cannot appear in X86AsmPrinter!\n";
175 abort();
Chris Lattnerac5701c2004-10-04 07:24:48 +0000176 }
177
178 emitAlignment(Align);
Nate Begeman9d19eb42005-06-30 00:53:20 +0000179 if (!forCygwin && !forDarwin) {
John Criswell57c24502005-06-20 19:59:22 +0000180 O << "\t.type " << name << ",@object\n";
181 O << "\t.size " << name << "," << Size << "\n";
Reid Spencer5dc81f62005-01-23 03:52:14 +0000182 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000183 O << name << ":\t\t\t\t# ";
184 WriteAsOperand(O, I, true, true, &M);
185 O << " = ";
186 WriteAsOperand(O, C, false, false, &M);
187 O << "\n";
188 emitGlobalConstant(C);
189 }
190 }
191
192 AsmPrinter::doFinalization(M);
193 return false; // success
194}
195
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000196namespace {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000197 struct X86IntelAsmPrinter : public X86SharedAsmPrinter {
198 X86IntelAsmPrinter(std::ostream &O, TargetMachine &TM)
199 : X86SharedAsmPrinter(O, TM) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000200
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000201 virtual const char *getPassName() const {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000202 return "X86 Intel-Style Assembly Printer";
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000203 }
204
Chris Lattner3fa861a2004-08-01 06:02:08 +0000205 /// printInstruction - This method is automatically generated by tablegen
206 /// from the instruction set description. This method returns true if the
207 /// machine instruction was sufficiently described to print it, otherwise it
208 /// returns false.
209 bool printInstruction(const MachineInstr *MI);
210
Chris Lattnerb12ee502004-08-01 07:43:46 +0000211 // This method is used by the tablegen'erated instruction printer.
Chris Lattner055acae2004-08-16 23:16:06 +0000212 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000213 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner25369cf2004-08-01 08:12:41 +0000214 if (MO.getType() == MachineOperand::MO_MachineRegister) {
215 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
216 // Bug Workaround: See note in Printer::doInitialization about %.
217 O << "%" << TM.getRegisterInfo()->get(MO.getReg()).Name;
218 } else {
219 printOp(MO);
220 }
Chris Lattnerb12ee502004-08-01 07:43:46 +0000221 }
222
Chris Lattner055acae2004-08-16 23:16:06 +0000223 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
224 MVT::ValueType VT) {
Chris Lattnere4ead0c2004-08-11 06:59:12 +0000225 printOp(MI->getOperand(OpNo), true); // Don't print "OFFSET".
226 }
227
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000228 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
229 MVT::ValueType VT) {
230 switch (VT) {
231 default: assert(0 && "Unknown arg size!");
232 case MVT::i8: O << "BYTE PTR "; break;
233 case MVT::i16: O << "WORD PTR "; break;
234 case MVT::i32:
235 case MVT::f32: O << "DWORD PTR "; break;
236 case MVT::i64:
237 case MVT::f64: O << "QWORD PTR "; break;
238 case MVT::f80: O << "XWORD PTR "; break;
239 }
240 printMemReference(MI, OpNo);
241 }
242
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000243 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000244 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000245 void printMemReference(const MachineInstr *MI, unsigned Op);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000246 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000247 bool doInitialization(Module &M);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000248 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000249} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000250
Chris Lattner3fa861a2004-08-01 06:02:08 +0000251
252// Include the auto-generated portion of the assembly writer.
Chris Lattner84b85c82004-12-16 17:33:24 +0000253#include "X86GenAsmWriter1.inc"
Chris Lattner3fa861a2004-08-01 06:02:08 +0000254
255
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000256/// runOnMachineFunction - This uses the printMachineInstruction()
257/// method to print assembly for each instruction.
258///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000259bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner055acae2004-08-16 23:16:06 +0000260 setupMachineFunction(MF);
Chris Lattnere0121322003-08-03 23:37:09 +0000261 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000262
Chris Lattnerb7089442003-01-13 00:35:03 +0000263 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000264 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000265
Brian Gaeke6559bb92002-11-14 22:32:30 +0000266 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000267 O << "\t.text\n";
Chris Lattnerc6393f82004-08-17 19:25:42 +0000268 emitAlignment(4);
Brian Gaeked7908f62003-06-27 00:00:48 +0000269 O << "\t.globl\t" << CurrentFnName << "\n";
Nate Begeman9d19eb42005-06-30 00:53:20 +0000270 if (!forCygwin && !forDarwin)
Reid Spencer5dc81f62005-01-23 03:52:14 +0000271 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000272 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000273
274 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000275 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
276 I != E; ++I) {
Chris Lattner4b2c09f2004-11-13 23:27:11 +0000277 // Print a label for the basic block if there are any predecessors.
278 if (I->pred_begin() != I->pred_end())
279 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
280 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000281 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000282 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000283 // Print the assembly for the instruction.
284 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000285 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000286 }
Chris Lattner0285a332002-12-28 20:25:38 +0000287 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000288
289 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000290 return false;
291}
292
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000293void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnerac5701c2004-10-04 07:24:48 +0000294 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000295 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000296 switch (MO.getType()) {
297 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000298 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000299 O << "<" << V->getName() << ">";
300 return;
301 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000302 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000303 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000304 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000305 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000306 O << "%" << RI.get(MO.getReg()).Name;
307 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000308 O << "%reg" << MO.getReg();
309 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000310
311 case MachineOperand::MO_SignExtendedImmed:
312 case MachineOperand::MO_UnextendedImmed:
313 O << (int)MO.getImmedValue();
314 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000315 case MachineOperand::MO_MachineBasicBlock: {
316 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
317 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Nate Begeman9d19eb42005-06-30 00:53:20 +0000318 << "_" << MBBOp->getNumber () << '\t' << CommentString
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000319 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000320 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000321 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000322 case MachineOperand::MO_PCRelativeDisp:
323 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
324 abort ();
325 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000326 case MachineOperand::MO_GlobalAddress: {
Brian Gaeke002a50a2003-07-31 17:38:52 +0000327 if (!elideOffsetKeyword)
328 O << "OFFSET ";
329 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerd416f082004-10-15 04:44:53 +0000330 int Offset = MO.getOffset();
331 if (Offset > 0)
332 O << " + " << Offset;
333 else if (Offset < 0)
334 O << " - " << -Offset;
Chris Lattnerb7089442003-01-13 00:35:03 +0000335 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000336 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000337 case MachineOperand::MO_ExternalSymbol:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000338 O << GlobalPrefix << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000339 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000340 default:
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000341 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000342 }
343}
344
Chris Lattnerac5701c2004-10-04 07:24:48 +0000345void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
Chris Lattner3d3067b2002-11-21 20:44:15 +0000346 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000347
Chris Lattner3d3067b2002-11-21 20:44:15 +0000348 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000349 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000350 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerd416f082004-10-15 04:44:53 +0000351 const MachineOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000352
Chris Lattner0e0ed852004-10-17 07:16:32 +0000353 if (BaseReg.isFrameIndex()) {
354 O << "[frame slot #" << BaseReg.getFrameIndex();
355 if (DispSpec.getImmedValue())
356 O << " + " << DispSpec.getImmedValue();
357 O << "]";
358 return;
359 } else if (BaseReg.isConstantPoolIndex()) {
360 O << "[.CPI" << CurrentFnName << "_"
361 << BaseReg.getConstantPoolIndex();
362
363 if (IndexReg.getReg()) {
364 O << " + ";
365 if (ScaleVal != 1)
366 O << ScaleVal << "*";
367 printOp(IndexReg);
368 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000369
Chris Lattner0e0ed852004-10-17 07:16:32 +0000370 if (DispSpec.getImmedValue())
371 O << " + " << DispSpec.getImmedValue();
372 O << "]";
373 return;
374 }
375
Chris Lattner3d3067b2002-11-21 20:44:15 +0000376 O << "[";
377 bool NeedPlus = false;
378 if (BaseReg.getReg()) {
Chris Lattnerd416f082004-10-15 04:44:53 +0000379 printOp(BaseReg, true);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000380 NeedPlus = true;
381 }
382
383 if (IndexReg.getReg()) {
384 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000385 if (ScaleVal != 1)
386 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000387 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000388 NeedPlus = true;
389 }
390
Chris Lattnerd416f082004-10-15 04:44:53 +0000391 if (DispSpec.isGlobalAddress()) {
Chris Lattner0285a332002-12-28 20:25:38 +0000392 if (NeedPlus)
Chris Lattnerd416f082004-10-15 04:44:53 +0000393 O << " + ";
394 printOp(DispSpec, true);
395 } else {
396 int DispVal = DispSpec.getImmedValue();
Chris Lattnere11a9a92005-01-12 04:07:11 +0000397 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Chris Lattnerd416f082004-10-15 04:44:53 +0000398 if (NeedPlus)
399 if (DispVal > 0)
400 O << " + ";
401 else {
402 O << " - ";
403 DispVal = -DispVal;
404 }
405 O << DispVal;
406 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000407 }
408 O << "]";
409}
410
John Criswell4ffff9e2004-04-08 20:31:47 +0000411
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000412/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000413/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000414///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000415void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000416 ++EmittedInsts;
Chris Lattner85494292004-08-11 06:09:55 +0000417
Chris Lattner2a998bd2004-08-11 07:02:04 +0000418 // Call the autogenerated instruction printer routines.
Chris Lattner955f0962004-10-04 07:31:08 +0000419 printInstruction(MI);
Chris Lattner72614082002-10-25 22:55:53 +0000420}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000421
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000422bool X86IntelAsmPrinter::doInitialization(Module &M) {
Chris Lattner055acae2004-08-16 23:16:06 +0000423 AsmPrinter::doInitialization(M);
Chris Lattner93c1afa2003-08-11 19:35:26 +0000424 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000425 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000426 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
427 // instruction as a reference to the register named sp, and if you try to
428 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
429 // before being looked up in the symbol table. This creates spurious
430 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
431 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000432 O << "\t.intel_syntax\n";
Chris Lattner055acae2004-08-16 23:16:06 +0000433 return false;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000434}
435
Chris Lattnerac5701c2004-10-04 07:24:48 +0000436
437
438namespace {
439 struct X86ATTAsmPrinter : public X86SharedAsmPrinter {
440 X86ATTAsmPrinter(std::ostream &O, TargetMachine &TM)
441 : X86SharedAsmPrinter(O, TM) { }
442
443 virtual const char *getPassName() const {
444 return "X86 AT&T-Style Assembly Printer";
445 }
446
447 /// printInstruction - This method is automatically generated by tablegen
448 /// from the instruction set description. This method returns true if the
449 /// machine instruction was sufficiently described to print it, otherwise it
450 /// returns false.
451 bool printInstruction(const MachineInstr *MI);
452
453 // This method is used by the tablegen'erated instruction printer.
454 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
455 printOp(MI->getOperand(OpNo));
456 }
457
458 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
459 MVT::ValueType VT) {
460 printOp(MI->getOperand(OpNo), true); // Don't print '$' prefix.
461 }
462
463 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
464 MVT::ValueType VT) {
465 printMemReference(MI, OpNo);
466 }
467
468 void printMachineInstruction(const MachineInstr *MI);
469 void printOp(const MachineOperand &MO, bool isCallOperand = false);
470 void printMemReference(const MachineInstr *MI, unsigned Op);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000471 bool runOnMachineFunction(MachineFunction &F);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000472 };
473} // end of anonymous namespace
474
475
476// Include the auto-generated portion of the assembly writer.
Chris Lattner84b85c82004-12-16 17:33:24 +0000477#include "X86GenAsmWriter.inc"
Chris Lattnerac5701c2004-10-04 07:24:48 +0000478
479
480/// runOnMachineFunction - This uses the printMachineInstruction()
481/// method to print assembly for each instruction.
482///
483bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
484 setupMachineFunction(MF);
485 O << "\n\n";
486
487 // Print out constants referenced by the function
488 printConstantPool(MF.getConstantPool());
489
490 // Print out labels for the function.
491 O << "\t.text\n";
492 emitAlignment(4);
493 O << "\t.globl\t" << CurrentFnName << "\n";
Nate Begeman9d19eb42005-06-30 00:53:20 +0000494 if (!forCygwin && !forDarwin)
Reid Spencer5dc81f62005-01-23 03:52:14 +0000495 O << "\t.type\t" << CurrentFnName << ", @function\n";
Chris Lattnerac5701c2004-10-04 07:24:48 +0000496 O << CurrentFnName << ":\n";
497
498 // Print out code for the function.
499 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
500 I != E; ++I) {
501 // Print a label for the basic block.
Chris Lattner4b2c09f2004-11-13 23:27:11 +0000502 if (I->pred_begin() != I->pred_end())
503 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
504 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Chris Lattnerac5701c2004-10-04 07:24:48 +0000505 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
506 II != E; ++II) {
507 // Print the assembly for the instruction.
508 O << "\t";
509 printMachineInstruction(II);
510 }
511 }
512
513 // We didn't modify anything.
514 return false;
515}
516
517void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
518 const MRegisterInfo &RI = *TM.getRegisterInfo();
519 switch (MO.getType()) {
520 case MachineOperand::MO_VirtualRegister:
521 case MachineOperand::MO_MachineRegister:
522 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
523 "Virtual registers should not make it this far!");
524 O << '%';
525 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
526 O << (char)tolower(*Name);
527 return;
528
529 case MachineOperand::MO_SignExtendedImmed:
530 case MachineOperand::MO_UnextendedImmed:
531 O << '$' << (int)MO.getImmedValue();
532 return;
533 case MachineOperand::MO_MachineBasicBlock: {
534 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
535 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Nate Begeman9d19eb42005-06-30 00:53:20 +0000536 << "_" << MBBOp->getNumber () << '\t' << CommentString
Chris Lattnerac5701c2004-10-04 07:24:48 +0000537 << MBBOp->getBasicBlock ()->getName ();
538 return;
539 }
540 case MachineOperand::MO_PCRelativeDisp:
541 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
542 abort ();
543 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000544 case MachineOperand::MO_GlobalAddress: {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000545 if (!isCallOp) O << '$';
546 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerd416f082004-10-15 04:44:53 +0000547 int Offset = MO.getOffset();
548 if (Offset > 0)
549 O << "+" << Offset;
550 else if (Offset < 0)
551 O << Offset;
Chris Lattnerac5701c2004-10-04 07:24:48 +0000552 return;
Chris Lattnerd416f082004-10-15 04:44:53 +0000553 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000554 case MachineOperand::MO_ExternalSymbol:
555 if (!isCallOp) O << '$';
Reid Spencer5dc81f62005-01-23 03:52:14 +0000556 O << GlobalPrefix << MO.getSymbolName();
Chris Lattnerac5701c2004-10-04 07:24:48 +0000557 return;
558 default:
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000559 O << "<unknown operand type>"; return;
Chris Lattnerad200712003-09-09 16:23:36 +0000560 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000561}
562
Chris Lattnerac5701c2004-10-04 07:24:48 +0000563void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
564 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerad200712003-09-09 16:23:36 +0000565
Chris Lattnerac5701c2004-10-04 07:24:48 +0000566 const MachineOperand &BaseReg = MI->getOperand(Op);
567 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
568 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerd416f082004-10-15 04:44:53 +0000569 const MachineOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattnerad200712003-09-09 16:23:36 +0000570
Chris Lattner0e0ed852004-10-17 07:16:32 +0000571 if (BaseReg.isFrameIndex()) {
572 O << "[frame slot #" << BaseReg.getFrameIndex();
573 if (DispSpec.getImmedValue())
574 O << " + " << DispSpec.getImmedValue();
575 O << "]";
576 return;
577 } else if (BaseReg.isConstantPoolIndex()) {
578 O << ".CPI" << CurrentFnName << "_"
579 << BaseReg.getConstantPoolIndex();
580 if (DispSpec.getImmedValue())
581 O << "+" << DispSpec.getImmedValue();
582 if (IndexReg.getReg()) {
583 O << "(,";
584 printOp(IndexReg);
585 if (ScaleVal != 1)
586 O << "," << ScaleVal;
587 O << ")";
588 }
589 return;
590 }
591
Chris Lattnerd416f082004-10-15 04:44:53 +0000592 if (DispSpec.isGlobalAddress()) {
593 printOp(DispSpec, true);
594 } else {
595 int DispVal = DispSpec.getImmedValue();
Chris Lattner9f2cb3d2005-01-12 04:05:19 +0000596 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
Chris Lattnerd416f082004-10-15 04:44:53 +0000597 O << DispVal;
Chris Lattnerac5701c2004-10-04 07:24:48 +0000598 }
599
Chris Lattnerd416f082004-10-15 04:44:53 +0000600 if (IndexReg.getReg() || BaseReg.getReg()) {
601 O << "(";
602 if (BaseReg.getReg())
603 printOp(BaseReg);
604
605 if (IndexReg.getReg()) {
606 O << ",";
607 printOp(IndexReg);
608 if (ScaleVal != 1)
609 O << "," << ScaleVal;
610 }
611
612 O << ")";
613 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000614}
615
616
617/// printMachineInstruction -- Print out a single X86 LLVM instruction
618/// MI in Intel syntax to the current output stream.
619///
620void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
621 ++EmittedInsts;
622 // Call the autogenerated instruction printer routines.
Chris Lattner955f0962004-10-04 07:31:08 +0000623 printInstruction(MI);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000624}
625
626
627/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
628/// for a MachineFunction to the given output stream, using the given target
629/// machine description.
630///
631FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
632 switch (AsmWriterFlavor) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000633 default:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000634 assert(0 && "Unknown asm flavor!");
Chris Lattnerac5701c2004-10-04 07:24:48 +0000635 case intel:
636 return new X86IntelAsmPrinter(o, tm);
637 case att:
638 return new X86ATTAsmPrinter(o, tm);
639 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000640}