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"),
48 bool X86SharedAsmPrinter::doInitialization(Module &M) {
49 PrivateGlobalPrefix = ".L";
50 DefaultTextSection = ".text";
51 DefaultDataSection = ".data";
53 switch (Subtarget->TargetType) {
54 case X86Subtarget::isDarwin:
55 AlignmentIsInBytes = false;
57 Data64bitsDirective = 0; // we can't emit a 64-bit unit
58 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
59 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
60 ConstantPoolSection = "\t.const\n";
61 JumpTableSection = "\t.const\n"; // FIXME: depends on PIC mode
62 FourByteConstantSection = "\t.literal4\n";
63 EightByteConstantSection = "\t.literal8\n";
64 LCOMMDirective = "\t.lcomm\t";
65 COMMDirectiveTakesAlignment = false;
66 HasDotTypeDotSizeDirective = false;
67 StaticCtorsSection = ".mod_init_func";
68 StaticDtorsSection = ".mod_term_func";
69 InlineAsmStart = "# InlineAsm Start";
70 InlineAsmEnd = "# InlineAsm End";
72 case X86Subtarget::isCygwin:
74 COMMDirectiveTakesAlignment = false;
75 HasDotTypeDotSizeDirective = false;
76 StaticCtorsSection = "\t.section .ctors,\"aw\"";
77 StaticDtorsSection = "\t.section .dtors,\"aw\"";
79 case X86Subtarget::isWindows:
81 HasDotTypeDotSizeDirective = false;
86 if (Subtarget->TargetType == X86Subtarget::isDarwin) {
87 // Emit initial debug information.
91 return AsmPrinter::doInitialization(M);
94 bool X86SharedAsmPrinter::doFinalization(Module &M) {
95 // Note: this code is not shared by the Intel printer as it is too different
96 // from how MASM does things. When making changes here don't forget to look
97 // at X86IntelAsmPrinter::doFinalization().
98 const TargetData *TD = TM.getTargetData();
100 // Print out module-level global variables here.
101 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
103 if (!I->hasInitializer()) continue; // External global require no code
105 // Check to see if this is a special global used by LLVM, if so, emit it.
106 if (EmitSpecialLLVMGlobal(I))
109 std::string name = Mang->getValueName(I);
110 Constant *C = I->getInitializer();
111 unsigned Size = TD->getTypeSize(C->getType());
112 unsigned Align = getPreferredAlignmentLog(I);
114 if (C->isNullValue() && /* FIXME: Verify correct */
115 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
116 I->hasLinkOnceLinkage() ||
117 (Subtarget->TargetType == X86Subtarget::isDarwin &&
118 I->hasExternalLinkage() && !I->hasSection()))) {
119 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
120 if (I->hasExternalLinkage()) {
121 O << "\t.globl\t" << name << "\n";
122 O << "\t.zerofill __DATA__, __common, " << name << ", "
123 << Size << ", " << Align;
125 SwitchToDataSection(DefaultDataSection, I);
126 if (LCOMMDirective != NULL) {
127 if (I->hasInternalLinkage()) {
128 O << LCOMMDirective << name << "," << Size;
129 if (Subtarget->TargetType == X86Subtarget::isDarwin)
130 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
132 O << COMMDirective << name << "," << Size;
134 if (Subtarget->TargetType != X86Subtarget::isCygwin) {
135 if (I->hasInternalLinkage())
136 O << "\t.local\t" << name << "\n";
138 O << COMMDirective << name << "," << Size;
139 if (COMMDirectiveTakesAlignment)
140 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
143 O << "\t\t" << CommentString << " " << I->getName() << "\n";
145 switch (I->getLinkage()) {
146 case GlobalValue::LinkOnceLinkage:
147 case GlobalValue::WeakLinkage:
148 if (Subtarget->TargetType == X86Subtarget::isDarwin) {
149 O << "\t.globl " << name << "\n"
150 << "\t.weak_definition " << name << "\n";
151 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
152 } else if (Subtarget->TargetType == X86Subtarget::isCygwin) {
153 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\"\n"
154 << "\t.weak " << name << "\n";
156 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"
157 << "\t.weak " << name << "\n";
160 case GlobalValue::AppendingLinkage:
161 // FIXME: appending linkage variables should go into a section of
162 // their name or something. For now, just emit them as external.
163 case GlobalValue::ExternalLinkage:
164 // If external or appending, declare as a global symbol
165 O << "\t.globl " << name << "\n";
167 case GlobalValue::InternalLinkage:
168 SwitchToDataSection(DefaultDataSection, I);
171 assert(0 && "Unknown linkage type!");
174 EmitAlignment(Align, I);
175 O << name << ":\t\t\t\t" << CommentString << " " << I->getName()
177 if (HasDotTypeDotSizeDirective)
178 O << "\t.size " << name << ", " << Size << "\n";
180 EmitGlobalConstant(C);
185 if (Subtarget->TargetType == X86Subtarget::isDarwin) {
186 SwitchToDataSection("", 0);
188 // Output stubs for dynamically-linked functions
190 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
192 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
193 "self_modifying_code+pure_instructions,5", 0);
194 O << "L" << *i << "$stub:\n";
195 O << "\t.indirect_symbol " << *i << "\n";
196 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
201 // Output stubs for external and common global variables.
202 if (GVStubs.begin() != GVStubs.end())
204 ".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
205 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
207 O << "L" << *i << "$non_lazy_ptr:\n";
208 O << "\t.indirect_symbol " << *i << "\n";
212 // Emit initial debug information.
215 // Funny Darwin hack: This flag tells the linker that no global symbols
216 // contain code that falls through to other global symbols (e.g. the obvious
217 // implementation of multiple entry points). If this doesn't occur, the
218 // linker can safely perform dead code stripping. Since LLVM never
219 // generates code that does this, it is always safe to set.
220 O << "\t.subsections_via_symbols\n";
223 AsmPrinter::doFinalization(M);
224 return false; // success
227 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
228 /// for a MachineFunction to the given output stream, using the given target
229 /// machine description.
231 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
232 X86TargetMachine &tm){
233 switch (AsmWriterFlavor) {
235 assert(0 && "Unknown asm flavor!");
237 return new X86IntelAsmPrinter(o, tm);
239 return new X86ATTAsmPrinter(o, tm);