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) {
43 bool forCygwin = false;
46 bool forWin32 = false;
47 const std::string& TT = M.getTargetTriple();
48 if (TT.length() > 5) {
49 forCygwin = TT.find("cygwin") != std::string::npos ||
50 TT.find("mingw") != std::string::npos;
51 forDarwin = TT.find("darwin") != std::string::npos;
52 if (!forDarwin && !forCygwin)
54 } else if (TT.empty()) {
55 #if defined(__CYGWIN__) || defined(__MINGW32__)
57 #elif defined(__APPLE__)
67 AlignmentIsInBytes = false;
69 Data64bitsDirective = 0; // we can't emit a 64-bit unit
70 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
71 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
72 ConstantPoolSection = "\t.const\n";
73 LCOMMDirective = "\t.lcomm\t";
74 COMMDirectiveTakesAlignment = false;
75 } else if (forCygwin) {
77 COMMDirectiveTakesAlignment = false;
78 } else if (forWin32) {
82 return AsmPrinter::doInitialization(M);
85 bool X86SharedAsmPrinter::doFinalization(Module &M) {
86 const TargetData &TD = TM.getTargetData();
88 // Print out module-level global variables here.
89 for (Module::const_global_iterator I = M.global_begin(),
90 E = M.global_end(); I != E; ++I)
91 if (I->hasInitializer()) { // External global require no code
93 std::string name = Mang->getValueName(I);
94 Constant *C = I->getInitializer();
95 unsigned Size = TD.getTypeSize(C->getType());
96 unsigned Align = TD.getTypeAlignmentShift(C->getType());
98 switch (I->getLinkage()) {
99 default: assert(0 && "Unknown linkage type!");
100 case GlobalValue::LinkOnceLinkage:
101 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
102 if (C->isNullValue()) {
103 O << COMMDirective << name << "," << Size;
104 if (COMMDirectiveTakesAlignment)
105 O << "," << (1 << Align);
107 WriteAsOperand(O, I, true, true, &M);
112 // Nonnull linkonce -> weak
113 O << "\t.weak " << name << "\n";
114 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
115 SwitchSection("", I);
117 case GlobalValue::InternalLinkage:
118 if (C->isNullValue()) {
119 if (LCOMMDirective) {
120 O << LCOMMDirective << name << "," << Size << "," << Align;
123 SwitchSection(".bss", I);
124 O << "\t.local " << name << "\n";
125 O << COMMDirective << name << "," << Size;
126 if (COMMDirectiveTakesAlignment)
127 O << "," << (1 << Align);
129 WriteAsOperand(O, I, true, true, &M);
134 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
136 case GlobalValue::AppendingLinkage:
137 // FIXME: appending linkage variables should go into a section of
138 // their name or something. For now, just emit them as external.
139 case GlobalValue::ExternalLinkage:
140 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
141 // If external or appending, declare as a global symbol
142 O << "\t.globl " << name << "\n";
146 EmitAlignment(Align);
148 O << "\t.type " << name << ",@object\n";
149 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 SwitchSection("", 0);
161 // Output stubs for external global variables
162 if (GVStubs.begin() != GVStubs.end())
163 O << "\t.non_lazy_symbol_pointer\n";
164 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
166 O << "L" << *i << "$non_lazy_ptr:\n";
167 O << "\t.indirect_symbol " << *i << "\n";
171 // Output stubs for dynamically-linked functions
173 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
175 O << "\t.symbol_stub\n";
176 O << "L" << *i << "$stub:\n";
177 O << "\t.indirect_symbol " << *i << "\n";
178 O << "\tjmp\t*L" << j << "$lz\n";
179 O << "L" << *i << "$stub_binder:\n";
180 O << "\tpushl\t$L" << j << "$lz\n";
181 O << "\tjmp\tdyld_stub_binding_helper\n";
182 O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
183 O << "L" << j << "$lz:\n";
184 O << "\t.indirect_symbol " << *i << "\n";
185 O << "\t.long\tL" << *i << "$stub_binder\n";
190 // Output stubs for link-once variables
191 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
192 O << ".data\n.align 2\n";
193 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
194 e = LinkOnceStubs.end(); i != e; ++i) {
195 O << "L" << *i << "$non_lazy_ptr:\n"
196 << "\t.long\t" << *i << '\n';
200 AsmPrinter::doFinalization(M);
201 return false; // success
204 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
205 /// for a MachineFunction to the given output stream, using the given target
206 /// machine description.
208 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
209 switch (AsmWriterFlavor) {
211 assert(0 && "Unknown asm flavor!");
213 return new X86IntelAsmPrinter(o, tm);
215 return new X86ATTAsmPrinter(o, tm);