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 "X86AsmPrinter.h"
18 #include "X86ATTAsmPrinter.h"
19 #include "X86IntelAsmPrinter.h"
20 #include "X86Subtarget.h"
21 #include "llvm/Constants.h"
22 #include "llvm/Module.h"
23 #include "llvm/Type.h"
24 #include "llvm/Assembly/Writer.h"
25 #include "llvm/Support/Mangler.h"
26 #include "llvm/Support/CommandLine.h"
29 Statistic<> llvm::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 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
47 PrivateGlobalPrefix = ".L";
49 switch (Subtarget->TargetType) {
50 case X86Subtarget::isDarwin:
51 AlignmentIsInBytes = false;
53 Data64bitsDirective = 0; // we can't emit a 64-bit unit
54 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
55 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
56 ConstantPoolSection = "\t.const\n";
57 LCOMMDirective = "\t.lcomm\t";
58 COMMDirectiveTakesAlignment = false;
59 HasDotTypeDotSizeDirective = false;
61 StaticCtorsSection = ".mod_init_func";
62 StaticDtorsSection = ".mod_term_func";
63 InlineAsmStart = InlineAsmEnd = ""; // Don't use #APP/#NO_APP
65 case X86Subtarget::isCygwin:
67 COMMDirectiveTakesAlignment = false;
68 HasDotTypeDotSizeDirective = false;
70 case X86Subtarget::isWindows:
72 HasDotTypeDotSizeDirective = false;
78 // Emit initial debug information.
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(), E = M.global_end();
91 if (!I->hasInitializer()) continue; // External global require no code
93 // Check to see if this is a special global used by LLVM, if so, emit it.
94 if (EmitSpecialLLVMGlobal(I))
97 std::string name = Mang->getValueName(I);
98 Constant *C = I->getInitializer();
99 unsigned Size = TD.getTypeSize(C->getType());
100 unsigned Align = getPreferredAlignmentLog(I);
102 if (C->isNullValue() && /* FIXME: Verify correct */
103 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
104 I->hasLinkOnceLinkage() ||
105 (forDarwin && I->hasExternalLinkage() && !I->hasSection()))) {
106 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
107 if (I->hasExternalLinkage()) {
108 O << "\t.globl\t" << name << "\n";
109 O << "\t.zerofill __DATA__, __common, " << name << ", "
110 << Size << ", " << Align;
112 SwitchSection(".data", I);
113 if (LCOMMDirective != NULL) {
114 if (I->hasInternalLinkage()) {
115 O << LCOMMDirective << name << "," << Size;
117 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
119 O << COMMDirective << name << "," << Size;
121 if (I->hasInternalLinkage())
122 O << "\t.local\t" << name << "\n";
123 O << COMMDirective << name << "," << Size;
124 if (COMMDirectiveTakesAlignment)
125 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
128 O << "\t\t" << CommentString << " " << I->getName() << "\n";
130 switch (I->getLinkage()) {
131 case GlobalValue::LinkOnceLinkage:
132 case GlobalValue::WeakLinkage:
134 O << "\t.globl " << name << "\n"
135 << "\t.weak_definition " << name << "\n";
136 SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
138 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
139 O << "\t.weak " << name << "\n";
142 case GlobalValue::AppendingLinkage:
143 // FIXME: appending linkage variables should go into a section of
144 // their name or something. For now, just emit them as external.
145 case GlobalValue::ExternalLinkage:
146 // If external or appending, declare as a global symbol
147 O << "\t.globl " << name << "\n";
149 case GlobalValue::InternalLinkage:
150 SwitchSection(".data", I);
153 assert(0 && "Unknown linkage type!");
156 EmitAlignment(Align, I);
157 O << name << ":\t\t\t\t" << CommentString << " " << I->getName()
159 if (HasDotTypeDotSizeDirective)
160 O << "\t.size " << name << ", " << Size << "\n";
162 EmitGlobalConstant(C);
168 SwitchSection("", 0);
170 // Output stubs for dynamically-linked functions
172 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
174 SwitchSection(".section __IMPORT,__jump_table,symbol_stubs,"
175 "self_modifying_code+pure_instructions,5", 0);
176 O << "L" << *i << "$stub:\n";
177 O << "\t.indirect_symbol " << *i << "\n";
178 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
183 // Output stubs for external and common global variables.
184 if (GVStubs.begin() != GVStubs.end())
185 SwitchSection(".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
186 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
188 O << "L" << *i << "$non_lazy_ptr:\n";
189 O << "\t.indirect_symbol " << *i << "\n";
193 // Emit initial debug information.
196 // Funny Darwin hack: This flag tells the linker that no global symbols
197 // contain code that falls through to other global symbols (e.g. the obvious
198 // implementation of multiple entry points). If this doesn't occur, the
199 // linker can safely perform dead code stripping. Since LLVM never generates
200 // code that does this, it is always safe to set.
201 O << "\t.subsections_via_symbols\n";
204 AsmPrinter::doFinalization(M);
205 return false; // success
208 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
209 /// for a MachineFunction to the given output stream, using the given target
210 /// machine description.
212 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
213 X86TargetMachine &tm){
214 switch (AsmWriterFlavor) {
216 assert(0 && "Unknown asm flavor!");
218 return new X86IntelAsmPrinter(o, tm);
220 return new X86ATTAsmPrinter(o, tm);