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/Assembly/Writer.h"
22 #include "llvm/CodeGen/MachineConstantPool.h"
23 #include "llvm/Support/Mangler.h"
24 #include "llvm/Support/CommandLine.h"
28 Statistic<> llvm::x86::EmittedInsts("asm-printer",
29 "Number of machine instrs printed");
31 enum AsmWriterFlavorTy { att, intel };
32 cl::opt<AsmWriterFlavorTy>
33 AsmWriterFlavor("x86-asm-syntax",
34 cl::desc("Choose style of code to emit from X86 backend:"),
36 clEnumVal(att, " Emit AT&T-style assembly"),
37 clEnumVal(intel, " Emit Intel-style assembly"),
42 bool X86SharedAsmPrinter::doInitialization(Module& M) {
43 bool leadingUnderscore = false;
45 const std::string& TT = M.getTargetTriple();
46 if (TT.length() > 5) {
47 forCygwin = TT.find("cygwin") != std::string::npos ||
48 TT.find("mingw") != std::string::npos;
49 forDarwin = TT.find("darwin") != std::string::npos;
50 } else if (TT.empty()) {
51 #if defined(__CYGWIN__) || defined(__MINGW32__)
53 #elif defined(__APPLE__)
56 leadingUnderscore = true;
58 leadingUnderscore = false;
62 if (leadingUnderscore || forCygwin || forDarwin)
66 AlignmentIsInBytes = false;
68 return AsmPrinter::doInitialization(M);
71 /// printConstantPool - Print to the current output stream assembly
72 /// representations of the constants in the constant pool MCP. This is
73 /// used to print out constants which have been "spilled to memory" by
74 /// the code generator.
76 void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
77 const std::vector<Constant*> &CP = MCP->getConstants();
78 const TargetData &TD = TM.getTargetData();
80 if (CP.empty()) return;
82 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
86 O << "\t.section .rodata\n";
87 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
88 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
90 emitGlobalConstant(CP[i]);
94 bool X86SharedAsmPrinter::doFinalization(Module &M) {
95 const TargetData &TD = TM.getTargetData();
96 std::string CurSection;
98 // Print out module-level global variables here.
99 for (Module::const_global_iterator I = M.global_begin(),
100 E = M.global_end(); I != E; ++I)
101 if (I->hasInitializer()) { // External global require no code
103 std::string name = Mang->getValueName(I);
104 Constant *C = I->getInitializer();
105 unsigned Size = TD.getTypeSize(C->getType());
106 unsigned Align = TD.getTypeAlignmentShift(C->getType());
108 if (C->isNullValue() &&
109 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
110 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
111 SwitchSection(O, CurSection, ".data");
112 if (!forCygwin && !forDarwin && I->hasInternalLinkage())
113 O << "\t.local " << name << "\n";
114 if (forDarwin && I->hasInternalLinkage())
115 O << "\t.lcomm " << name << "," << Size << "," << Align;
117 O << "\t.comm " << name << "," << Size;
118 if (!forCygwin && !forDarwin)
119 O << "," << (1 << Align);
121 WriteAsOperand(O, I, true, true, &M);
124 switch (I->getLinkage()) {
125 default: assert(0 && "Unknown linkage type!");
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 if (!forCygwin && !forDarwin) {
150 O << "\t.type " << name << ",@object\n";
151 O << "\t.size " << name << "," << Size << "\n";
153 O << name << ":\t\t\t\t# ";
154 WriteAsOperand(O, I, true, true, &M);
156 WriteAsOperand(O, C, false, false, &M);
158 emitGlobalConstant(C);
163 // Output stubs for external global variables
164 if (GVStubs.begin() != GVStubs.end())
165 O << "\t.non_lazy_symbol_pointer\n";
166 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
168 O << "L" << *i << "$non_lazy_ptr:\n";
169 O << "\t.indirect_symbol " << *i << "\n";
173 // Output stubs for dynamically-linked functions
175 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
177 O << "\t.symbol_stub\n";
178 O << "L" << *i << "$stub:\n";
179 O << "\t.indirect_symbol " << *i << "\n";
180 O << "\tjmp\t*L" << j << "$lz\n";
181 O << "L" << *i << "$stub_binder:\n";
182 O << "\tpushl\t$L" << j << "$lz\n";
183 O << "\tjmp\tdyld_stub_binding_helper\n";
184 O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
185 O << "L" << j << "$lz:\n";
186 O << "\t.indirect_symbol " << *i << "\n";
187 O << "\t.long\tL" << *i << "$stub_binder\n";
192 // Output stubs for link-once variables
193 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
194 O << ".data\n.align 2\n";
195 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
196 e = LinkOnceStubs.end(); i != e; ++i) {
197 O << "L" << *i << "$non_lazy_ptr:\n"
198 << "\t.long\t" << *i << '\n';
202 AsmPrinter::doFinalization(M);
203 return false; // success
206 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
207 /// for a MachineFunction to the given output stream, using the given target
208 /// machine description.
210 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
211 switch (AsmWriterFlavor) {
213 assert(0 && "Unknown asm flavor!");
215 return new X86IntelAsmPrinter(o, tm);
217 return new X86ATTAsmPrinter(o, tm);