blob: 0e8407160d5ccd91a0d5b517356aa5726650fbf2 [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
11// of machine-dependent LLVM code to Intel-format assembly language. This
12// printer is the output mechanism used by `llc' and `lli -print-machineinstrs'
13// on X86.
Chris Lattner72614082002-10-25 22:55:53 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "X86.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000018#include "X86InstrInfo.h"
Alkis Evlogimenos03090662004-03-09 03:35:34 +000019#include "X86TargetMachine.h"
Chris Lattnere0121322003-08-03 23:37:09 +000020#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000022#include "llvm/Module.h"
23#include "llvm/Assembly/Writer.h"
Chris Lattner055acae2004-08-16 23:16:06 +000024#include "llvm/CodeGen/AsmPrinter.h"
Alkis Evlogimenos03090662004-03-09 03:35:34 +000025#include "llvm/CodeGen/MachineCodeEmitter.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Alkis Evlogimenos03090662004-03-09 03:35:34 +000027#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000028#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerb12ee502004-08-01 07:43:46 +000029#include "llvm/CodeGen/ValueTypes.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000030#include "llvm/Target/TargetMachine.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000031#include "llvm/Support/Mangler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/ADT/Statistic.h"
33#include "llvm/ADT/StringExtras.h"
34#include "llvm/Support/CommandLine.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000035using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattnerac5701c2004-10-04 07:24:48 +000037static bool isScale(const MachineOperand &MO) {
38 return MO.isImmediate() &&
39 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
40 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
41}
42
43static bool isMem(const MachineInstr *MI, unsigned Op) {
44 if (MI->getOperand(Op).isFrameIndex()) return true;
45 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
46 return Op+4 <= MI->getNumOperands() &&
47 MI->getOperand(Op ).isRegister() && isScale(MI->getOperand(Op+1)) &&
48 MI->getOperand(Op+2).isRegister() && MI->getOperand(Op+3).isImmediate();
49}
50
51// SwitchSection - Switch to the specified section of the executable if we are
52// not already in it!
53//
54static void SwitchSection(std::ostream &OS, std::string &CurSection,
55 const char *NewSection) {
56 if (CurSection != NewSection) {
57 CurSection = NewSection;
58 if (!CurSection.empty())
59 OS << "\t" << NewSection << "\n";
60 }
61}
62
63namespace {
64 struct X86SharedAsmPrinter : public AsmPrinter {
65 X86SharedAsmPrinter(std::ostream &O, TargetMachine &TM)
66 : AsmPrinter(O, TM) { }
67
68 void printConstantPool(MachineConstantPool *MCP);
69 bool doFinalization(Module &M);
70 };
71}
72
73/// printConstantPool - Print to the current output stream assembly
74/// representations of the constants in the constant pool MCP. This is
75/// used to print out constants which have been "spilled to memory" by
76/// the code generator.
77///
78void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
79 const std::vector<Constant*> &CP = MCP->getConstants();
80 const TargetData &TD = TM.getTargetData();
81
82 if (CP.empty()) return;
83
84 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
85 O << "\t.section .rodata\n";
86 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
87 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
88 << *CP[i] << "\n";
89 emitGlobalConstant(CP[i]);
90 }
91}
92
93bool X86SharedAsmPrinter::doFinalization(Module &M) {
94 const TargetData &TD = TM.getTargetData();
95 std::string CurSection;
96
97 // Print out module-level global variables here.
98 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
99 if (I->hasInitializer()) { // External global require no code
100 O << "\n\n";
101 std::string name = Mang->getValueName(I);
102 Constant *C = I->getInitializer();
103 unsigned Size = TD.getTypeSize(C->getType());
104 unsigned Align = TD.getTypeAlignmentShift(C->getType());
105
106 if (C->isNullValue() &&
107 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
108 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
109 SwitchSection(O, CurSection, ".data");
110 if (I->hasInternalLinkage())
111 O << "\t.local " << name << "\n";
112
113 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
114 << "," << (1 << Align);
115 O << "\t\t# ";
116 WriteAsOperand(O, I, true, true, &M);
117 O << "\n";
118 } else {
119 switch (I->getLinkage()) {
120 case GlobalValue::LinkOnceLinkage:
121 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
122 // Nonnull linkonce -> weak
123 O << "\t.weak " << name << "\n";
124 SwitchSection(O, CurSection, "");
125 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
126 break;
127 case GlobalValue::AppendingLinkage:
128 // FIXME: appending linkage variables should go into a section of
129 // their name or something. For now, just emit them as external.
130 case GlobalValue::ExternalLinkage:
131 // If external or appending, declare as a global symbol
132 O << "\t.globl " << name << "\n";
133 // FALL THROUGH
134 case GlobalValue::InternalLinkage:
135 if (C->isNullValue())
136 SwitchSection(O, CurSection, ".bss");
137 else
138 SwitchSection(O, CurSection, ".data");
139 break;
140 }
141
142 emitAlignment(Align);
143 O << "\t.type " << name << ",@object\n";
144 O << "\t.size " << name << "," << Size << "\n";
145 O << name << ":\t\t\t\t# ";
146 WriteAsOperand(O, I, true, true, &M);
147 O << " = ";
148 WriteAsOperand(O, C, false, false, &M);
149 O << "\n";
150 emitGlobalConstant(C);
151 }
152 }
153
154 AsmPrinter::doFinalization(M);
155 return false; // success
156}
157
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000158namespace {
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000159 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000160 enum AsmWriterFlavor { att, intel };
161
162 cl::opt<AsmWriterFlavor>
163 AsmWriterFlavor("x86-asm-syntax",
164 cl::desc("Choose style of code to emit from X86 backend:"),
165 cl::values(
Chris Lattnerac5701c2004-10-04 07:24:48 +0000166 clEnumVal(att, " Emit AT&T-style assembly"),
167 clEnumVal(intel, " Emit Intel-style assembly"),
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000168 clEnumValEnd),
Chris Lattnerac5701c2004-10-04 07:24:48 +0000169 cl::init(att));
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000170
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000171 struct GasBugWorkaroundEmitter : public MachineCodeEmitter {
Misha Brukman8606aea2004-07-26 18:48:58 +0000172 GasBugWorkaroundEmitter(std::ostream& o)
173 : O(o), OldFlags(O.flags()), firstByte(true) {
174 O << std::hex;
175 }
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000176
Misha Brukman8606aea2004-07-26 18:48:58 +0000177 ~GasBugWorkaroundEmitter() {
178 O.flags(OldFlags);
Misha Brukman8606aea2004-07-26 18:48:58 +0000179 }
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000180
Misha Brukman8606aea2004-07-26 18:48:58 +0000181 virtual void emitByte(unsigned char B) {
182 if (!firstByte) O << "\n\t";
183 firstByte = false;
184 O << ".byte 0x" << (unsigned) B;
185 }
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000186
Misha Brukman8606aea2004-07-26 18:48:58 +0000187 // These should never be called
188 virtual void emitWord(unsigned W) { assert(0); }
189 virtual uint64_t getGlobalValueAddress(GlobalValue *V) { abort(); }
190 virtual uint64_t getGlobalValueAddress(const std::string &Name) { abort(); }
191 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) { abort(); }
192 virtual uint64_t getCurrentPCValue() { abort(); }
193 virtual uint64_t forceCompilationOf(Function *F) { abort(); }
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000194
195 private:
Misha Brukman8606aea2004-07-26 18:48:58 +0000196 std::ostream& O;
197 std::ios::fmtflags OldFlags;
198 bool firstByte;
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000199 };
200
Chris Lattnerac5701c2004-10-04 07:24:48 +0000201 struct X86IntelAsmPrinter : public X86SharedAsmPrinter {
202 X86IntelAsmPrinter(std::ostream &O, TargetMachine &TM)
203 : X86SharedAsmPrinter(O, TM) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000204
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000205 virtual const char *getPassName() const {
Chris Lattnerac5701c2004-10-04 07:24:48 +0000206 return "X86 Intel-Style Assembly Printer";
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000207 }
208
Chris Lattner3fa861a2004-08-01 06:02:08 +0000209 /// printInstruction - This method is automatically generated by tablegen
210 /// from the instruction set description. This method returns true if the
211 /// machine instruction was sufficiently described to print it, otherwise it
212 /// returns false.
213 bool printInstruction(const MachineInstr *MI);
214
Chris Lattnerb12ee502004-08-01 07:43:46 +0000215 // This method is used by the tablegen'erated instruction printer.
Chris Lattner055acae2004-08-16 23:16:06 +0000216 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000217 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner25369cf2004-08-01 08:12:41 +0000218 if (MO.getType() == MachineOperand::MO_MachineRegister) {
219 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
220 // Bug Workaround: See note in Printer::doInitialization about %.
221 O << "%" << TM.getRegisterInfo()->get(MO.getReg()).Name;
222 } else {
223 printOp(MO);
224 }
Chris Lattnerb12ee502004-08-01 07:43:46 +0000225 }
226
Chris Lattner055acae2004-08-16 23:16:06 +0000227 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
228 MVT::ValueType VT) {
Chris Lattnere4ead0c2004-08-11 06:59:12 +0000229 printOp(MI->getOperand(OpNo), true); // Don't print "OFFSET".
230 }
231
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000232 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
233 MVT::ValueType VT) {
234 switch (VT) {
235 default: assert(0 && "Unknown arg size!");
236 case MVT::i8: O << "BYTE PTR "; break;
237 case MVT::i16: O << "WORD PTR "; break;
238 case MVT::i32:
239 case MVT::f32: O << "DWORD PTR "; break;
240 case MVT::i64:
241 case MVT::f64: O << "QWORD PTR "; break;
242 case MVT::f80: O << "XWORD PTR "; break;
243 }
244 printMemReference(MI, OpNo);
245 }
246
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000247 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000248 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000249 void printMemReference(const MachineInstr *MI, unsigned Op);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000250 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000251 bool doInitialization(Module &M);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000252 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000253} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000254
Chris Lattner3fa861a2004-08-01 06:02:08 +0000255
256// Include the auto-generated portion of the assembly writer.
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000257#include "X86GenIntelAsmWriter.inc"
Chris Lattner3fa861a2004-08-01 06:02:08 +0000258
259
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000260/// runOnMachineFunction - This uses the printMachineInstruction()
261/// method to print assembly for each instruction.
262///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000263bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner055acae2004-08-16 23:16:06 +0000264 setupMachineFunction(MF);
Chris Lattnere0121322003-08-03 23:37:09 +0000265 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000266
Chris Lattnerb7089442003-01-13 00:35:03 +0000267 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000268 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000269
Brian Gaeke6559bb92002-11-14 22:32:30 +0000270 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000271 O << "\t.text\n";
Chris Lattnerc6393f82004-08-17 19:25:42 +0000272 emitAlignment(4);
Brian Gaeked7908f62003-06-27 00:00:48 +0000273 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner055acae2004-08-16 23:16:06 +0000274 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000275 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000276
277 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000278 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
279 I != E; ++I) {
280 // Print a label for the basic block.
Chris Lattnerc6393f82004-08-17 19:25:42 +0000281 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000282 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000283 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000284 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000285 // Print the assembly for the instruction.
286 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000287 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000288 }
Chris Lattner0285a332002-12-28 20:25:38 +0000289 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000290
291 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000292 return false;
293}
294
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000295void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnerac5701c2004-10-04 07:24:48 +0000296 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000297 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000298 switch (MO.getType()) {
299 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000300 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000301 O << "<" << V->getName() << ">";
302 return;
303 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000304 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000305 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000306 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000307 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000308 O << "%" << RI.get(MO.getReg()).Name;
309 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000310 O << "%reg" << MO.getReg();
311 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000312
313 case MachineOperand::MO_SignExtendedImmed:
314 case MachineOperand::MO_UnextendedImmed:
315 O << (int)MO.getImmedValue();
316 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000317 case MachineOperand::MO_MachineBasicBlock: {
318 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
319 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
320 << "_" << MBBOp->getNumber () << "\t# "
321 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000322 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000323 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000324 case MachineOperand::MO_PCRelativeDisp:
325 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
326 abort ();
327 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000328 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000329 if (!elideOffsetKeyword)
330 O << "OFFSET ";
331 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000332 return;
333 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000334 O << 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
344 if (MI->getOperand(Op).isFrameIndex()) {
345 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
346 if (MI->getOperand(Op+3).getImmedValue())
347 O << " + " << MI->getOperand(Op+3).getImmedValue();
348 O << "]";
349 return;
350 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000351 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000352 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000353 if (MI->getOperand(Op+3).getImmedValue())
354 O << " + " << MI->getOperand(Op+3).getImmedValue();
355 O << "]";
356 return;
357 }
358
Chris Lattner3d3067b2002-11-21 20:44:15 +0000359 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000360 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000361 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000362 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000363
364 O << "[";
365 bool NeedPlus = false;
366 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000367 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000368 NeedPlus = true;
369 }
370
371 if (IndexReg.getReg()) {
372 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000373 if (ScaleVal != 1)
374 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000375 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000376 NeedPlus = true;
377 }
378
Chris Lattner0285a332002-12-28 20:25:38 +0000379 if (DispVal) {
380 if (NeedPlus)
381 if (DispVal > 0)
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000382 O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000383 else {
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000384 O << " - ";
385 DispVal = -DispVal;
Chris Lattner0285a332002-12-28 20:25:38 +0000386 }
387 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000388 }
389 O << "]";
390}
391
John Criswell4ffff9e2004-04-08 20:31:47 +0000392
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000393/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000394/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000395///
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000396void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000397 ++EmittedInsts;
Chris Lattner85494292004-08-11 06:09:55 +0000398
399 // gas bugs:
400 //
401 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]" is misassembled
402 // by gas in intel_syntax mode as its 32-bit equivalent "fstp DWORD PTR
403 // [...]". Workaround: Output the raw opcode bytes instead of the instruction.
404 //
405 // The 80-bit FP load instruction "fld XWORD PTR [...]" is misassembled by gas
406 // in intel_syntax mode as its 32-bit equivalent "fld DWORD PTR
407 // [...]". Workaround: Output the raw opcode bytes instead of the instruction.
408 //
409 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an invalid opcode,
410 // saying "64 bit operations are only supported in 64 bit modes." libopcodes
411 // disassembles it as "fild DWORD PTR [...]", which is wrong. Workaround:
412 // Output the raw opcode bytes instead of the instruction.
413 //
414 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an invalid opcode,
415 // saying "64 bit operations are only supported in 64 bit modes." libopcodes
416 // disassembles it as "fistpll DWORD PTR [...]", which is wrong. Workaround:
417 // Output the raw opcode bytes instead of the instruction.
418 switch (MI->getOpcode()) {
419 case X86::FSTP80m:
420 case X86::FLD80m:
421 case X86::FILD64m:
422 case X86::FISTP64m:
423 GasBugWorkaroundEmitter gwe(O);
424 X86::emitInstruction(gwe, (X86InstrInfo&)*TM.getInstrInfo(), *MI);
425 O << "\t# ";
426 }
427
Chris Lattner2a998bd2004-08-11 07:02:04 +0000428 // Call the autogenerated instruction printer routines.
429 bool Handled = printInstruction(MI);
430 if (!Handled) {
431 MI->dump();
432 assert(0 && "Do not know how to print this instruction!");
433 abort();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000434 }
Chris Lattner72614082002-10-25 22:55:53 +0000435}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000436
Chris Lattner9a3e49a2004-10-03 20:36:57 +0000437bool X86IntelAsmPrinter::doInitialization(Module &M) {
Chris Lattner055acae2004-08-16 23:16:06 +0000438 AsmPrinter::doInitialization(M);
Chris Lattner93c1afa2003-08-11 19:35:26 +0000439 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000440 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000441 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
442 // instruction as a reference to the register named sp, and if you try to
443 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
444 // before being looked up in the symbol table. This creates spurious
445 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
446 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000447 O << "\t.intel_syntax\n";
Chris Lattner055acae2004-08-16 23:16:06 +0000448 return false;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000449}
450
Chris Lattnerac5701c2004-10-04 07:24:48 +0000451
452
453namespace {
454 struct X86ATTAsmPrinter : public X86SharedAsmPrinter {
455 X86ATTAsmPrinter(std::ostream &O, TargetMachine &TM)
456 : X86SharedAsmPrinter(O, TM) { }
457
458 virtual const char *getPassName() const {
459 return "X86 AT&T-Style Assembly Printer";
460 }
461
462 /// printInstruction - This method is automatically generated by tablegen
463 /// from the instruction set description. This method returns true if the
464 /// machine instruction was sufficiently described to print it, otherwise it
465 /// returns false.
466 bool printInstruction(const MachineInstr *MI);
467
468 // This method is used by the tablegen'erated instruction printer.
469 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
470 printOp(MI->getOperand(OpNo));
471 }
472
473 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
474 MVT::ValueType VT) {
475 printOp(MI->getOperand(OpNo), true); // Don't print '$' prefix.
476 }
477
478 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
479 MVT::ValueType VT) {
480 printMemReference(MI, OpNo);
481 }
482
483 void printMachineInstruction(const MachineInstr *MI);
484 void printOp(const MachineOperand &MO, bool isCallOperand = false);
485 void printMemReference(const MachineInstr *MI, unsigned Op);
486 bool runOnMachineFunction(MachineFunction &F);
487 };
488} // end of anonymous namespace
489
490
491// Include the auto-generated portion of the assembly writer.
492#include "X86GenATTAsmWriter.inc"
493
494
495/// runOnMachineFunction - This uses the printMachineInstruction()
496/// method to print assembly for each instruction.
497///
498bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
499 setupMachineFunction(MF);
500 O << "\n\n";
501
502 // Print out constants referenced by the function
503 printConstantPool(MF.getConstantPool());
504
505 // Print out labels for the function.
506 O << "\t.text\n";
507 emitAlignment(4);
508 O << "\t.globl\t" << CurrentFnName << "\n";
509 O << "\t.type\t" << CurrentFnName << ", @function\n";
510 O << CurrentFnName << ":\n";
511
512 // Print out code for the function.
513 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
514 I != E; ++I) {
515 // Print a label for the basic block.
516 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
517 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
518 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
519 II != E; ++II) {
520 // Print the assembly for the instruction.
521 O << "\t";
522 printMachineInstruction(II);
523 }
524 }
525
526 // We didn't modify anything.
527 return false;
528}
529
530void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
531 const MRegisterInfo &RI = *TM.getRegisterInfo();
532 switch (MO.getType()) {
533 case MachineOperand::MO_VirtualRegister:
534 case MachineOperand::MO_MachineRegister:
535 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
536 "Virtual registers should not make it this far!");
537 O << '%';
538 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
539 O << (char)tolower(*Name);
540 return;
541
542 case MachineOperand::MO_SignExtendedImmed:
543 case MachineOperand::MO_UnextendedImmed:
544 O << '$' << (int)MO.getImmedValue();
545 return;
546 case MachineOperand::MO_MachineBasicBlock: {
547 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
548 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
549 << "_" << MBBOp->getNumber () << "\t# "
550 << MBBOp->getBasicBlock ()->getName ();
551 return;
552 }
553 case MachineOperand::MO_PCRelativeDisp:
554 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
555 abort ();
556 return;
557 case MachineOperand::MO_GlobalAddress:
558 if (!isCallOp) O << '$';
559 O << Mang->getValueName(MO.getGlobal());
560 return;
561 case MachineOperand::MO_ExternalSymbol:
562 if (!isCallOp) O << '$';
563 O << MO.getSymbolName();
564 return;
565 default:
566 O << "<unknown operand type>"; return;
Chris Lattnerad200712003-09-09 16:23:36 +0000567 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000568}
569
Chris Lattnerac5701c2004-10-04 07:24:48 +0000570void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
571 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerad200712003-09-09 16:23:36 +0000572
Chris Lattnerac5701c2004-10-04 07:24:48 +0000573 if (MI->getOperand(Op).isFrameIndex()) {
574 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
575 if (MI->getOperand(Op+3).getImmedValue())
576 O << " + " << MI->getOperand(Op+3).getImmedValue();
577 O << "]";
578 return;
579 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
580 O << ".CPI" << CurrentFnName << "_"
581 << MI->getOperand(Op).getConstantPoolIndex();
582 if (MI->getOperand(Op+3).getImmedValue())
583 O << " + " << MI->getOperand(Op+3).getImmedValue();
584 return;
585 }
Chris Lattnerad200712003-09-09 16:23:36 +0000586
Chris Lattnerac5701c2004-10-04 07:24:48 +0000587 const MachineOperand &BaseReg = MI->getOperand(Op);
588 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
589 const MachineOperand &IndexReg = MI->getOperand(Op+2);
590 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattnerad200712003-09-09 16:23:36 +0000591
Chris Lattnerac5701c2004-10-04 07:24:48 +0000592 if (DispVal) O << DispVal;
Chris Lattnerad200712003-09-09 16:23:36 +0000593
Chris Lattnerac5701c2004-10-04 07:24:48 +0000594 O << "(";
595 if (BaseReg.getReg())
596 printOp(BaseReg);
597
598 if (IndexReg.getReg()) {
599 O << ",";
600 printOp(IndexReg);
601 if (ScaleVal != 1)
602 O << "," << ScaleVal;
603 }
604
605 O << ")";
606}
607
608
609/// printMachineInstruction -- Print out a single X86 LLVM instruction
610/// MI in Intel syntax to the current output stream.
611///
612void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
613 ++EmittedInsts;
614 // Call the autogenerated instruction printer routines.
615 if (!printInstruction(MI)) {
616 MI->dump();
617 assert(0 && "Do not know how to print this instruction!");
618 abort();
619 }
620}
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) {
629 default: assert(0 && "Unknown asm flavor!");
630 case intel:
631 return new X86IntelAsmPrinter(o, tm);
632 case att:
633 return new X86ATTAsmPrinter(o, tm);
634 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000635}