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>();
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;
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(), E = M.global_end();
86 if (!I->hasInitializer()) continue; // External global require no code
88 // Check to see if this is a special global used by LLVM, if so, emit it.
89 if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I))
92 std::string name = Mang->getValueName(I);
93 Constant *C = I->getInitializer();
94 unsigned Size = TD.getTypeSize(C->getType());
95 unsigned Align = getPreferredAlignmentLog(I);
97 if (C->isNullValue() && /* FIXME: Verify correct */
98 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
99 I->hasLinkOnceLinkage())) {
100 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
101 SwitchSection(".data", I);
102 if (LCOMMDirective != NULL) {
103 if (I->hasInternalLinkage()) {
104 O << LCOMMDirective << name << "," << Size;
106 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
108 O << COMMDirective << name << "," << Size;
110 if (I->hasInternalLinkage())
111 O <<"\t.local\t" << name << "\n";
112 O << COMMDirective << name << "," << Size;
113 if (COMMDirectiveTakesAlignment)
114 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
116 O << "\t\t" << CommentString << " " << I->getName() << "\n";
118 switch (I->getLinkage()) {
119 case GlobalValue::LinkOnceLinkage:
120 case GlobalValue::WeakLinkage:
122 O << "\t.globl " << name << "\n"
123 << "\t.weak_definition " << name << "\n";
124 SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
126 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
127 O << "\t.weak " << name << "\n";
130 case GlobalValue::AppendingLinkage:
131 // FIXME: appending linkage variables should go into a section of
132 // their name or something. For now, just emit them as external.
133 case GlobalValue::ExternalLinkage:
134 // If external or appending, declare as a global symbol
135 O << "\t.globl " << name << "\n";
137 case GlobalValue::InternalLinkage:
138 SwitchSection(".data", I);
141 assert(0 && "Unknown linkage type!");
144 EmitAlignment(Align, I);
145 O << name << ":\t\t\t\t" << CommentString << " " << I->getName()
147 if (HasDotTypeDotSizeDirective)
148 O << "\t.size " << name << ", " << Size << "\n";
150 EmitGlobalConstant(C);
156 SwitchSection("", 0);
158 // Output stubs for dynamically-linked functions
160 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
162 SwitchSection(".section __IMPORT,__jump_table,symbol_stubs,"
163 "self_modifying_code+pure_instructions,5", 0);
164 O << "L" << *i << "$stub:\n";
165 O << "\t.indirect_symbol " << *i << "\n";
166 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
171 // Output stubs for external and common global variables.
172 if (GVStubs.begin() != GVStubs.end())
173 SwitchSection(".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
174 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
176 O << "L" << *i << "$non_lazy_ptr:\n";
177 O << "\t.indirect_symbol " << *i << "\n";
182 AsmPrinter::doFinalization(M);
183 return false; // success
186 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
187 /// for a MachineFunction to the given output stream, using the given target
188 /// machine description.
190 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
191 switch (AsmWriterFlavor) {
193 assert(0 && "Unknown asm flavor!");
195 return new X86IntelAsmPrinter(o, tm);
197 return new X86ATTAsmPrinter(o, tm);