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/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) {
45 bool forWin32 = 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__)
62 AlignmentIsInBytes = false;
64 Data64bitsDirective = 0; // we can't emit a 64-bit unit
65 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
66 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
67 ConstantPoolSection = "\t.const\n";
68 LCOMMDirective = "\t.lcomm\t";
69 COMMDirectiveTakesAlignment = false;
70 } else if (forCygwin) {
72 COMMDirectiveTakesAlignment = false;
73 } else if (forWin32) {
77 return AsmPrinter::doInitialization(M);
80 bool X86SharedAsmPrinter::doFinalization(Module &M) {
81 const TargetData &TD = TM.getTargetData();
83 // Print out module-level global variables here.
84 for (Module::const_global_iterator I = M.global_begin(),
85 E = M.global_end(); I != E; ++I)
86 if (I->hasInitializer()) { // External global require no code
88 std::string name = Mang->getValueName(I);
89 Constant *C = I->getInitializer();
90 unsigned Size = TD.getTypeSize(C->getType());
91 unsigned Align = TD.getTypeAlignmentShift(C->getType());
93 switch (I->getLinkage()) {
94 default: assert(0 && "Unknown linkage type!");
95 case GlobalValue::LinkOnceLinkage:
96 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
97 if (C->isNullValue()) {
98 O << COMMDirective << name << "," << Size;
99 if (COMMDirectiveTakesAlignment)
100 O << "," << (1 << Align);
102 WriteAsOperand(O, I, true, true, &M);
107 // Nonnull linkonce -> weak
108 O << "\t.weak " << name << "\n";
109 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
110 SwitchSection("", I);
112 case GlobalValue::InternalLinkage:
113 if (C->isNullValue()) {
114 if (LCOMMDirective) {
115 O << LCOMMDirective << name << "," << Size << "," << Align;
118 SwitchSection(".bss", I);
119 O << "\t.local " << name << "\n";
120 O << COMMDirective << name << "," << Size;
121 if (COMMDirectiveTakesAlignment)
122 O << "," << (1 << Align);
124 WriteAsOperand(O, I, true, true, &M);
129 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
131 case GlobalValue::AppendingLinkage:
132 // FIXME: appending linkage variables should go into a section of
133 // their name or something. For now, just emit them as external.
134 case GlobalValue::ExternalLinkage:
135 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
136 // If external or appending, declare as a global symbol
137 O << "\t.globl " << name << "\n";
141 EmitAlignment(Align);
142 if (!forCygwin && !forDarwin) {
143 O << "\t.type " << name << ",@object\n";
144 O << "\t.size " << name << "," << Size << "\n";
146 O << name << ":\t\t\t\t# ";
147 WriteAsOperand(O, I, true, true, &M);
149 WriteAsOperand(O, C, false, false, &M);
151 EmitGlobalConstant(C);
155 SwitchSection("", 0);
156 // Output stubs for external global variables
157 if (GVStubs.begin() != GVStubs.end())
158 O << "\t.non_lazy_symbol_pointer\n";
159 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
161 O << "L" << *i << "$non_lazy_ptr:\n";
162 O << "\t.indirect_symbol " << *i << "\n";
166 // Output stubs for dynamically-linked functions
168 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
170 O << "\t.symbol_stub\n";
171 O << "L" << *i << "$stub:\n";
172 O << "\t.indirect_symbol " << *i << "\n";
173 O << "\tjmp\t*L" << j << "$lz\n";
174 O << "L" << *i << "$stub_binder:\n";
175 O << "\tpushl\t$L" << j << "$lz\n";
176 O << "\tjmp\tdyld_stub_binding_helper\n";
177 O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
178 O << "L" << j << "$lz:\n";
179 O << "\t.indirect_symbol " << *i << "\n";
180 O << "\t.long\tL" << *i << "$stub_binder\n";
185 // Output stubs for link-once variables
186 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
187 O << ".data\n.align 2\n";
188 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
189 e = LinkOnceStubs.end(); i != e; ++i) {
190 O << "L" << *i << "$non_lazy_ptr:\n"
191 << "\t.long\t" << *i << '\n';
195 AsmPrinter::doFinalization(M);
196 return false; // success
199 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
200 /// for a MachineFunction to the given output stream, using the given target
201 /// machine description.
203 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
204 switch (AsmWriterFlavor) {
206 assert(0 && "Unknown asm flavor!");
208 return new X86IntelAsmPrinter(o, tm);
210 return new X86ATTAsmPrinter(o, tm);