1 //===-- X86AsmPrinter.cpp - Convert X86 LLVM code to Intel assembly -------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // 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.
15 //===----------------------------------------------------------------------===//
18 #include "X86TargetMachine.h"
19 #include "llvm/Module.h"
20 #include "llvm/Assembly/Writer.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/MachineConstantPool.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/ValueTypes.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Support/Mangler.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/Support/CommandLine.h"
32 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
33 enum AsmWriterFlavor { att, intel };
35 cl::opt<AsmWriterFlavor>
36 AsmWriterFlavor("x86-asm-syntax",
37 cl::desc("Choose style of code to emit from X86 backend:"),
39 clEnumVal(att, " Emit AT&T-style assembly"),
40 clEnumVal(intel, " Emit Intel-style assembly"),
44 struct X86SharedAsmPrinter : public AsmPrinter {
45 X86SharedAsmPrinter(std::ostream &O, TargetMachine &TM)
46 : AsmPrinter(O, TM) { }
48 void printConstantPool(MachineConstantPool *MCP);
49 bool doFinalization(Module &M);
53 static bool isScale(const MachineOperand &MO) {
54 return MO.isImmediate() &&
55 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
56 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
59 static bool isMem(const MachineInstr *MI, unsigned Op) {
60 if (MI->getOperand(Op).isFrameIndex()) return true;
61 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
62 return Op+4 <= MI->getNumOperands() &&
63 MI->getOperand(Op ).isRegister() && isScale(MI->getOperand(Op+1)) &&
64 MI->getOperand(Op+2).isRegister() && MI->getOperand(Op+3).isImmediate();
67 // SwitchSection - Switch to the specified section of the executable if we are
70 static void SwitchSection(std::ostream &OS, std::string &CurSection,
71 const char *NewSection) {
72 if (CurSection != NewSection) {
73 CurSection = NewSection;
74 if (!CurSection.empty())
75 OS << "\t" << NewSection << "\n";
79 /// printConstantPool - Print to the current output stream assembly
80 /// representations of the constants in the constant pool MCP. This is
81 /// used to print out constants which have been "spilled to memory" by
82 /// the code generator.
84 void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
85 const std::vector<Constant*> &CP = MCP->getConstants();
86 const TargetData &TD = TM.getTargetData();
88 if (CP.empty()) return;
90 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
91 O << "\t.section .rodata\n";
92 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
93 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
95 emitGlobalConstant(CP[i]);
99 bool X86SharedAsmPrinter::doFinalization(Module &M) {
100 const TargetData &TD = TM.getTargetData();
101 std::string CurSection;
103 // Print out module-level global variables here.
104 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
105 if (I->hasInitializer()) { // External global require no code
107 std::string name = Mang->getValueName(I);
108 Constant *C = I->getInitializer();
109 unsigned Size = TD.getTypeSize(C->getType());
110 unsigned Align = TD.getTypeAlignmentShift(C->getType());
112 if (C->isNullValue() &&
113 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
114 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
115 SwitchSection(O, CurSection, ".data");
116 if (I->hasInternalLinkage())
117 O << "\t.local " << name << "\n";
119 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
120 << "," << (1 << Align);
122 WriteAsOperand(O, I, true, true, &M);
125 switch (I->getLinkage()) {
126 case GlobalValue::LinkOnceLinkage:
127 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
128 // Nonnull linkonce -> weak
129 O << "\t.weak " << name << "\n";
130 SwitchSection(O, CurSection, "");
131 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
133 case GlobalValue::AppendingLinkage:
134 // FIXME: appending linkage variables should go into a section of
135 // their name or something. For now, just emit them as external.
136 case GlobalValue::ExternalLinkage:
137 // If external or appending, declare as a global symbol
138 O << "\t.globl " << name << "\n";
140 case GlobalValue::InternalLinkage:
141 if (C->isNullValue())
142 SwitchSection(O, CurSection, ".bss");
144 SwitchSection(O, CurSection, ".data");
148 emitAlignment(Align);
149 O << "\t.type " << name << ",@object\n";
150 O << "\t.size " << name << "," << Size << "\n";
151 O << name << ":\t\t\t\t# ";
152 WriteAsOperand(O, I, true, true, &M);
154 WriteAsOperand(O, C, false, false, &M);
156 emitGlobalConstant(C);
160 AsmPrinter::doFinalization(M);
161 return false; // success
165 struct X86IntelAsmPrinter : public X86SharedAsmPrinter {
166 X86IntelAsmPrinter(std::ostream &O, TargetMachine &TM)
167 : X86SharedAsmPrinter(O, TM) { }
169 virtual const char *getPassName() const {
170 return "X86 Intel-Style Assembly Printer";
173 /// printInstruction - This method is automatically generated by tablegen
174 /// from the instruction set description. This method returns true if the
175 /// machine instruction was sufficiently described to print it, otherwise it
177 bool printInstruction(const MachineInstr *MI);
179 // This method is used by the tablegen'erated instruction printer.
180 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
181 const MachineOperand &MO = MI->getOperand(OpNo);
182 if (MO.getType() == MachineOperand::MO_MachineRegister) {
183 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
184 // Bug Workaround: See note in Printer::doInitialization about %.
185 O << "%" << TM.getRegisterInfo()->get(MO.getReg()).Name;
191 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
193 printOp(MI->getOperand(OpNo), true); // Don't print "OFFSET".
196 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
199 default: assert(0 && "Unknown arg size!");
200 case MVT::i8: O << "BYTE PTR "; break;
201 case MVT::i16: O << "WORD PTR "; break;
203 case MVT::f32: O << "DWORD PTR "; break;
205 case MVT::f64: O << "QWORD PTR "; break;
206 case MVT::f80: O << "XWORD PTR "; break;
208 printMemReference(MI, OpNo);
211 void printMachineInstruction(const MachineInstr *MI);
212 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
213 void printMemReference(const MachineInstr *MI, unsigned Op);
214 bool runOnMachineFunction(MachineFunction &F);
215 bool doInitialization(Module &M);
217 } // end of anonymous namespace
220 // Include the auto-generated portion of the assembly writer.
221 #include "X86GenIntelAsmWriter.inc"
224 /// runOnMachineFunction - This uses the printMachineInstruction()
225 /// method to print assembly for each instruction.
227 bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
228 setupMachineFunction(MF);
231 // Print out constants referenced by the function
232 printConstantPool(MF.getConstantPool());
234 // Print out labels for the function.
237 O << "\t.globl\t" << CurrentFnName << "\n";
238 O << "\t.type\t" << CurrentFnName << ", @function\n";
239 O << CurrentFnName << ":\n";
241 // Print out code for the function.
242 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
244 // Print a label for the basic block.
245 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
246 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
247 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
249 // Print the assembly for the instruction.
251 printMachineInstruction(II);
255 // We didn't modify anything.
259 void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
260 bool elideOffsetKeyword /* = false */) {
261 const MRegisterInfo &RI = *TM.getRegisterInfo();
262 switch (MO.getType()) {
263 case MachineOperand::MO_VirtualRegister:
264 if (Value *V = MO.getVRegValueOrNull()) {
265 O << "<" << V->getName() << ">";
269 case MachineOperand::MO_MachineRegister:
270 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
271 // Bug Workaround: See note in Printer::doInitialization about %.
272 O << "%" << RI.get(MO.getReg()).Name;
274 O << "%reg" << MO.getReg();
277 case MachineOperand::MO_SignExtendedImmed:
278 case MachineOperand::MO_UnextendedImmed:
279 O << (int)MO.getImmedValue();
281 case MachineOperand::MO_MachineBasicBlock: {
282 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
283 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
284 << "_" << MBBOp->getNumber () << "\t# "
285 << MBBOp->getBasicBlock ()->getName ();
288 case MachineOperand::MO_PCRelativeDisp:
289 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
292 case MachineOperand::MO_GlobalAddress:
293 if (!elideOffsetKeyword)
295 O << Mang->getValueName(MO.getGlobal());
297 case MachineOperand::MO_ExternalSymbol:
298 O << MO.getSymbolName();
301 O << "<unknown operand type>"; return;
305 void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
306 assert(isMem(MI, Op) && "Invalid memory reference!");
308 if (MI->getOperand(Op).isFrameIndex()) {
309 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
310 if (MI->getOperand(Op+3).getImmedValue())
311 O << " + " << MI->getOperand(Op+3).getImmedValue();
314 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
315 O << "[.CPI" << CurrentFnName << "_"
316 << MI->getOperand(Op).getConstantPoolIndex();
317 if (MI->getOperand(Op+3).getImmedValue())
318 O << " + " << MI->getOperand(Op+3).getImmedValue();
323 const MachineOperand &BaseReg = MI->getOperand(Op);
324 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
325 const MachineOperand &IndexReg = MI->getOperand(Op+2);
326 int DispVal = MI->getOperand(Op+3).getImmedValue();
329 bool NeedPlus = false;
330 if (BaseReg.getReg()) {
335 if (IndexReg.getReg()) {
336 if (NeedPlus) O << " + ";
338 O << ScaleVal << "*";
357 /// printMachineInstruction -- Print out a single X86 LLVM instruction
358 /// MI in Intel syntax to the current output stream.
360 void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
363 // Call the autogenerated instruction printer routines.
364 printInstruction(MI);
367 bool X86IntelAsmPrinter::doInitialization(Module &M) {
368 AsmPrinter::doInitialization(M);
369 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
371 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
372 // instruction as a reference to the register named sp, and if you try to
373 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
374 // before being looked up in the symbol table. This creates spurious
375 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
376 // mode, and decorate all register names with percent signs.
377 O << "\t.intel_syntax\n";
384 struct X86ATTAsmPrinter : public X86SharedAsmPrinter {
385 X86ATTAsmPrinter(std::ostream &O, TargetMachine &TM)
386 : X86SharedAsmPrinter(O, TM) { }
388 virtual const char *getPassName() const {
389 return "X86 AT&T-Style Assembly Printer";
392 /// printInstruction - This method is automatically generated by tablegen
393 /// from the instruction set description. This method returns true if the
394 /// machine instruction was sufficiently described to print it, otherwise it
396 bool printInstruction(const MachineInstr *MI);
398 // This method is used by the tablegen'erated instruction printer.
399 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
400 printOp(MI->getOperand(OpNo));
403 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
405 printOp(MI->getOperand(OpNo), true); // Don't print '$' prefix.
408 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
410 printMemReference(MI, OpNo);
413 void printMachineInstruction(const MachineInstr *MI);
414 void printOp(const MachineOperand &MO, bool isCallOperand = false);
415 void printMemReference(const MachineInstr *MI, unsigned Op);
416 bool runOnMachineFunction(MachineFunction &F);
418 } // end of anonymous namespace
421 // Include the auto-generated portion of the assembly writer.
422 #include "X86GenATTAsmWriter.inc"
425 /// runOnMachineFunction - This uses the printMachineInstruction()
426 /// method to print assembly for each instruction.
428 bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
429 setupMachineFunction(MF);
432 // Print out constants referenced by the function
433 printConstantPool(MF.getConstantPool());
435 // Print out labels for the function.
438 O << "\t.globl\t" << CurrentFnName << "\n";
439 O << "\t.type\t" << CurrentFnName << ", @function\n";
440 O << CurrentFnName << ":\n";
442 // Print out code for the function.
443 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
445 // Print a label for the basic block.
446 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
447 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
448 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
450 // Print the assembly for the instruction.
452 printMachineInstruction(II);
456 // We didn't modify anything.
460 void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
461 const MRegisterInfo &RI = *TM.getRegisterInfo();
462 switch (MO.getType()) {
463 case MachineOperand::MO_VirtualRegister:
464 case MachineOperand::MO_MachineRegister:
465 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
466 "Virtual registers should not make it this far!");
468 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
469 O << (char)tolower(*Name);
472 case MachineOperand::MO_SignExtendedImmed:
473 case MachineOperand::MO_UnextendedImmed:
474 O << '$' << (int)MO.getImmedValue();
476 case MachineOperand::MO_MachineBasicBlock: {
477 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
478 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
479 << "_" << MBBOp->getNumber () << "\t# "
480 << MBBOp->getBasicBlock ()->getName ();
483 case MachineOperand::MO_PCRelativeDisp:
484 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
487 case MachineOperand::MO_GlobalAddress:
488 if (!isCallOp) O << '$';
489 O << Mang->getValueName(MO.getGlobal());
491 case MachineOperand::MO_ExternalSymbol:
492 if (!isCallOp) O << '$';
493 O << MO.getSymbolName();
496 O << "<unknown operand type>"; return;
500 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
501 assert(isMem(MI, Op) && "Invalid memory reference!");
503 if (MI->getOperand(Op).isFrameIndex()) {
504 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
505 if (MI->getOperand(Op+3).getImmedValue())
506 O << " + " << MI->getOperand(Op+3).getImmedValue();
509 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
510 O << ".CPI" << CurrentFnName << "_"
511 << MI->getOperand(Op).getConstantPoolIndex();
512 if (MI->getOperand(Op+3).getImmedValue())
513 O << " + " << MI->getOperand(Op+3).getImmedValue();
517 const MachineOperand &BaseReg = MI->getOperand(Op);
518 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
519 const MachineOperand &IndexReg = MI->getOperand(Op+2);
520 int DispVal = MI->getOperand(Op+3).getImmedValue();
522 if (DispVal) O << DispVal;
525 if (BaseReg.getReg())
528 if (IndexReg.getReg()) {
532 O << "," << ScaleVal;
539 /// printMachineInstruction -- Print out a single X86 LLVM instruction
540 /// MI in Intel syntax to the current output stream.
542 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
544 // Call the autogenerated instruction printer routines.
545 printInstruction(MI);
549 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
550 /// for a MachineFunction to the given output stream, using the given target
551 /// machine description.
553 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
554 switch (AsmWriterFlavor) {
555 default: assert(0 && "Unknown asm flavor!");
557 return new X86IntelAsmPrinter(o, tm);
559 return new X86ATTAsmPrinter(o, tm);