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/Module.h"
22 #include "llvm/Type.h"
23 #include "llvm/Assembly/Writer.h"
24 #include "llvm/Support/Mangler.h"
25 #include "llvm/Support/CommandLine.h"
29 Statistic<> llvm::x86::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>();
48 switch (Subtarget->TargetType) {
49 case X86Subtarget::isDarwin:
50 AlignmentIsInBytes = false;
52 Data64bitsDirective = 0; // we can't emit a 64-bit unit
53 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
54 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
55 ConstantPoolSection = "\t.const\n";
56 LCOMMDirective = "\t.lcomm\t";
57 COMMDirectiveTakesAlignment = false;
58 HasDotTypeDotSizeDirective = false;
61 case X86Subtarget::isCygwin:
63 COMMDirectiveTakesAlignment = false;
64 HasDotTypeDotSizeDirective = false;
66 case X86Subtarget::isWindows:
68 HasDotTypeDotSizeDirective = false;
73 return AsmPrinter::doInitialization(M);
76 bool X86SharedAsmPrinter::doFinalization(Module &M) {
77 const TargetData &TD = TM.getTargetData();
79 // Print out module-level global variables here.
80 for (Module::const_global_iterator I = M.global_begin(),
81 E = M.global_end(); I != E; ++I) {
82 if (!I->hasInitializer()) continue; // External global require no code
85 std::string name = Mang->getValueName(I);
86 Constant *C = I->getInitializer();
87 unsigned Size = TD.getTypeSize(C->getType());
88 unsigned Align = TD.getTypeAlignmentShift(C->getType());
90 switch (I->getLinkage()) {
91 default: assert(0 && "Unknown linkage type!");
92 case GlobalValue::LinkOnceLinkage:
93 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
94 if (C->isNullValue()) {
95 O << COMMDirective << name << "," << Size;
96 if (COMMDirectiveTakesAlignment)
97 O << "," << (1 << Align);
98 O << "\t\t" << CommentString << " " << I->getName() << "\n";
102 // Nonnull linkonce -> weak
103 O << "\t.weak " << name << "\n";
104 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
105 SwitchSection("", I);
107 case GlobalValue::InternalLinkage:
108 if (C->isNullValue()) {
109 if (LCOMMDirective) {
110 O << LCOMMDirective << name << "," << Size << "," << Align;
113 SwitchSection(".bss", I);
114 O << "\t.local " << name << "\n";
115 O << COMMDirective << name << "," << Size;
116 if (COMMDirectiveTakesAlignment)
117 O << "," << (1 << Align);
119 WriteAsOperand(O, I, true, true, &M);
124 SwitchSection(".data", I);
126 case GlobalValue::AppendingLinkage:
127 // FIXME: appending linkage variables should go into a section of
128 // their name or something. For now, just emit them as external.
129 case GlobalValue::ExternalLinkage:
130 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
131 // If external or appending, declare as a global symbol
132 O << "\t.globl " << name << "\n";
136 EmitAlignment(Align);
137 if (HasDotTypeDotSizeDirective) {
138 O << "\t.type " << name << ",@object\n";
139 O << "\t.size " << name << "," << Size << "\n";
141 O << name << ":\t\t\t" << CommentString << ' ' << I->getName() << '\n';
142 EmitGlobalConstant(C);
146 SwitchSection("", 0);
147 // Output stubs for external global variables
148 if (GVStubs.begin() != GVStubs.end())
149 O << "\t.non_lazy_symbol_pointer\n";
150 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
152 O << "L" << *i << "$non_lazy_ptr:\n";
153 O << "\t.indirect_symbol " << *i << "\n";
157 // Output stubs for dynamically-linked functions
159 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
161 O << "\t.symbol_stub\n";
162 O << "L" << *i << "$stub:\n";
163 O << "\t.indirect_symbol " << *i << "\n";
164 O << "\tjmp\t*L" << j << "$lz\n";
165 O << "L" << *i << "$stub_binder:\n";
166 O << "\tpushl\t$L" << j << "$lz\n";
167 O << "\tjmp\tdyld_stub_binding_helper\n";
168 O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
169 O << "L" << j << "$lz:\n";
170 O << "\t.indirect_symbol " << *i << "\n";
171 O << "\t.long\tL" << *i << "$stub_binder\n";
176 // Output stubs for link-once variables
177 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
178 O << ".data\n.align 2\n";
179 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
180 e = LinkOnceStubs.end(); i != e; ++i) {
181 O << "L" << *i << "$non_lazy_ptr:\n"
182 << "\t.long\t" << *i << '\n';
186 AsmPrinter::doFinalization(M);
187 return false; // success
190 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
191 /// for a MachineFunction to the given output stream, using the given target
192 /// machine description.
194 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
195 switch (AsmWriterFlavor) {
197 assert(0 && "Unknown asm flavor!");
199 return new X86IntelAsmPrinter(o, tm);
201 return new X86ATTAsmPrinter(o, tm);