1 //===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 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 the shared super class printer that converts from our internal
11 // representation of machine-dependent LLVM code to Intel and AT&T format
13 // This printer is the output mechanism used by `llc'.
15 //===----------------------------------------------------------------------===//
17 #include "X86ATTAsmPrinter.h"
18 #include "X86IntelAsmPrinter.h"
20 #include "llvm/Module.h"
21 #include "llvm/Type.h"
22 #include "llvm/Assembly/Writer.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/Support/Mangler.h"
25 #include "llvm/Support/CommandLine.h"
29 Statistic<> llvm::x86::EmittedInsts("asm-printer",
30 "Number of machine instrs printed");
32 enum AsmWriterFlavorTy { att, intel };
33 cl::opt<AsmWriterFlavorTy>
34 AsmWriterFlavor("x86-asm-syntax",
35 cl::desc("Choose style of code to emit from X86 backend:"),
37 clEnumVal(att, " Emit AT&T-style assembly"),
38 clEnumVal(intel, " Emit Intel-style assembly"),
43 bool X86SharedAsmPrinter::doInitialization(Module& M) {
44 bool leadingUnderscore = false;
46 const std::string& TT = M.getTargetTriple();
47 if (TT.length() > 5) {
48 forCygwin = TT.find("cygwin") != std::string::npos ||
49 TT.find("mingw") != std::string::npos;
50 forDarwin = TT.find("darwin") != std::string::npos;
51 } else if (TT.empty()) {
52 #if defined(__CYGWIN__) || defined(__MINGW32__)
54 #elif defined(__APPLE__)
57 leadingUnderscore = true;
59 leadingUnderscore = false;
63 if (leadingUnderscore || forCygwin || forDarwin)
67 AlignmentIsInBytes = false;
68 Data64bitsDirective = 0; // we can't emit a 64-bit unit
69 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
70 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
73 return AsmPrinter::doInitialization(M);
76 /// printConstantPool - Print to the current output stream assembly
77 /// representations of the constants in the constant pool MCP. This is
78 /// used to print out constants which have been "spilled to memory" by
79 /// the code generator.
81 void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
82 const std::vector<Constant*> &CP = MCP->getConstants();
83 const TargetData &TD = TM.getTargetData();
85 if (CP.empty()) return;
87 SwitchSection(forDarwin ? "\t.const\n" : "\t.section .rodata\n", 0);
89 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
90 // FIXME: force doubles to be naturally aligned. We should handle this
91 // more correctly in the future.
92 if (CP[i]->getType() == Type::DoubleTy)
95 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
96 O << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_" << i
97 << ":\t\t\t\t\t" << CommentString << *CP[i] << "\n";
98 emitGlobalConstant(CP[i]);
102 bool X86SharedAsmPrinter::doFinalization(Module &M) {
103 const TargetData &TD = TM.getTargetData();
105 // Print out module-level global variables here.
106 for (Module::const_global_iterator I = M.global_begin(),
107 E = M.global_end(); I != E; ++I)
108 if (I->hasInitializer()) { // External global require no code
110 std::string name = Mang->getValueName(I);
111 Constant *C = I->getInitializer();
112 unsigned Size = TD.getTypeSize(C->getType());
113 unsigned Align = TD.getTypeAlignmentShift(C->getType());
115 if (C->isNullValue() &&
116 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
117 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
118 SwitchSection(".data", I);
119 if (!forCygwin && !forDarwin && I->hasInternalLinkage())
120 O << "\t.local " << name << "\n";
121 if (forDarwin && I->hasInternalLinkage())
122 O << "\t.lcomm " << name << "," << Size << "," << Align;
124 O << "\t.comm " << name << "," << Size;
125 if (!forCygwin && !forDarwin)
126 O << "," << (1 << Align);
128 WriteAsOperand(O, I, true, true, &M);
131 switch (I->getLinkage()) {
132 default: assert(0 && "Unknown linkage type!");
133 case GlobalValue::LinkOnceLinkage:
134 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
135 // Nonnull linkonce -> weak
136 O << "\t.weak " << name << "\n";
137 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
138 SwitchSection("", I);
140 case GlobalValue::AppendingLinkage:
141 // FIXME: appending linkage variables should go into a section of
142 // their name or something. For now, just emit them as external.
143 case GlobalValue::ExternalLinkage:
144 // If external or appending, declare as a global symbol
145 O << "\t.globl " << name << "\n";
147 case GlobalValue::InternalLinkage:
148 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
152 emitAlignment(Align);
153 if (!forCygwin && !forDarwin) {
154 O << "\t.type " << name << ",@object\n";
155 O << "\t.size " << name << "," << Size << "\n";
157 O << name << ":\t\t\t\t# ";
158 WriteAsOperand(O, I, true, true, &M);
160 WriteAsOperand(O, C, false, false, &M);
162 emitGlobalConstant(C);
167 SwitchSection("", 0);
168 // Output stubs for external global variables
169 if (GVStubs.begin() != GVStubs.end())
170 O << "\t.non_lazy_symbol_pointer\n";
171 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
173 O << "L" << *i << "$non_lazy_ptr:\n";
174 O << "\t.indirect_symbol " << *i << "\n";
178 // Output stubs for dynamically-linked functions
180 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
182 O << "\t.symbol_stub\n";
183 O << "L" << *i << "$stub:\n";
184 O << "\t.indirect_symbol " << *i << "\n";
185 O << "\tjmp\t*L" << j << "$lz\n";
186 O << "L" << *i << "$stub_binder:\n";
187 O << "\tpushl\t$L" << j << "$lz\n";
188 O << "\tjmp\tdyld_stub_binding_helper\n";
189 O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
190 O << "L" << j << "$lz:\n";
191 O << "\t.indirect_symbol " << *i << "\n";
192 O << "\t.long\tL" << *i << "$stub_binder\n";
197 // Output stubs for link-once variables
198 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
199 O << ".data\n.align 2\n";
200 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
201 e = LinkOnceStubs.end(); i != e; ++i) {
202 O << "L" << *i << "$non_lazy_ptr:\n"
203 << "\t.long\t" << *i << '\n';
207 AsmPrinter::doFinalization(M);
208 return false; // success
211 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
212 /// for a MachineFunction to the given output stream, using the given target
213 /// machine description.
215 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
216 switch (AsmWriterFlavor) {
218 assert(0 && "Unknown asm flavor!");
220 return new X86IntelAsmPrinter(o, tm);
222 return new X86ATTAsmPrinter(o, tm);