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"
19 #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"
30 Statistic<> llvm::x86::EmittedInsts("asm-printer",
31 "Number of machine instrs printed");
33 enum AsmWriterFlavorTy { att, intel };
34 cl::opt<AsmWriterFlavorTy>
35 AsmWriterFlavor("x86-asm-syntax",
36 cl::desc("Choose style of code to emit from X86 backend:"),
38 clEnumVal(att, " Emit AT&T-style assembly"),
39 clEnumVal(intel, " Emit Intel-style assembly"),
44 bool X86SharedAsmPrinter::doInitialization(Module &M) {
45 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
48 PrivateGlobalPrefix = ".L";
50 switch (Subtarget->TargetType) {
51 case X86Subtarget::isDarwin:
52 AlignmentIsInBytes = false;
54 Data64bitsDirective = 0; // we can't emit a 64-bit unit
55 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
56 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
57 ConstantPoolSection = "\t.const\n";
58 LCOMMDirective = "\t.lcomm\t";
59 COMMDirectiveTakesAlignment = false;
60 HasDotTypeDotSizeDirective = false;
62 StaticCtorsSection = ".mod_init_func";
63 StaticDtorsSection = ".mod_term_func";
64 InlineAsmStart = InlineAsmEnd = ""; // Don't use #APP/#NO_APP
66 case X86Subtarget::isCygwin:
68 COMMDirectiveTakesAlignment = false;
69 HasDotTypeDotSizeDirective = false;
71 case X86Subtarget::isWindows:
73 HasDotTypeDotSizeDirective = false;
78 // Emit initial debug information.
81 return AsmPrinter::doInitialization(M);
84 bool X86SharedAsmPrinter::doFinalization(Module &M) {
85 const TargetData &TD = TM.getTargetData();
87 // Print out module-level global variables here.
88 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
90 if (!I->hasInitializer()) continue; // External global require no code
92 // Check to see if this is a special global used by LLVM, if so, emit it.
93 if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I))
96 std::string name = Mang->getValueName(I);
97 Constant *C = I->getInitializer();
98 unsigned Size = TD.getTypeSize(C->getType());
99 unsigned Align = getPreferredAlignmentLog(I);
101 if (C->isNullValue() && /* FIXME: Verify correct */
102 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
103 I->hasLinkOnceLinkage() ||
104 (forDarwin && I->hasExternalLinkage() && !I->hasSection()))) {
105 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
106 if (I->hasExternalLinkage()) {
107 O << "\t.globl\t" << name << "\n";
108 O << "\t.zerofill __DATA__, __common, " << name << ", "
109 << Size << ", " << Align;
111 SwitchSection(".data", I);
112 if (LCOMMDirective != NULL) {
113 if (I->hasInternalLinkage()) {
114 O << LCOMMDirective << name << "," << Size;
116 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
118 O << COMMDirective << name << "," << Size;
120 if (I->hasInternalLinkage())
121 O << "\t.local\t" << name << "\n";
122 O << COMMDirective << name << "," << Size;
123 if (COMMDirectiveTakesAlignment)
124 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
127 O << "\t\t" << CommentString << " " << I->getName() << "\n";
129 switch (I->getLinkage()) {
130 case GlobalValue::LinkOnceLinkage:
131 case GlobalValue::WeakLinkage:
133 O << "\t.globl " << name << "\n"
134 << "\t.weak_definition " << name << "\n";
135 SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
137 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
138 O << "\t.weak " << name << "\n";
141 case GlobalValue::AppendingLinkage:
142 // FIXME: appending linkage variables should go into a section of
143 // their name or something. For now, just emit them as external.
144 case GlobalValue::ExternalLinkage:
145 // If external or appending, declare as a global symbol
146 O << "\t.globl " << name << "\n";
148 case GlobalValue::InternalLinkage:
149 SwitchSection(".data", I);
152 assert(0 && "Unknown linkage type!");
155 EmitAlignment(Align, I);
156 O << name << ":\t\t\t\t" << CommentString << " " << I->getName()
158 if (HasDotTypeDotSizeDirective)
159 O << "\t.size " << name << ", " << Size << "\n";
161 EmitGlobalConstant(C);
167 SwitchSection("", 0);
169 // Output stubs for dynamically-linked functions
171 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
173 SwitchSection(".section __IMPORT,__jump_table,symbol_stubs,"
174 "self_modifying_code+pure_instructions,5", 0);
175 O << "L" << *i << "$stub:\n";
176 O << "\t.indirect_symbol " << *i << "\n";
177 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
182 // Output stubs for external and common global variables.
183 if (GVStubs.begin() != GVStubs.end())
184 SwitchSection(".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
185 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
187 O << "L" << *i << "$non_lazy_ptr:\n";
188 O << "\t.indirect_symbol " << *i << "\n";
193 // Emit initial debug information.
196 AsmPrinter::doFinalization(M);
197 return false; // success
200 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
201 /// for a MachineFunction to the given output stream, using the given target
202 /// machine description.
204 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
205 switch (AsmWriterFlavor) {
207 assert(0 && "Unknown asm flavor!");
209 return new X86IntelAsmPrinter(o, tm);
211 return new X86ATTAsmPrinter(o, tm);