blob: 13c136f3481ddf938c0700de25f6ad7a6f2c6b48 [file] [log] [blame]
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001//===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 assembly -----------===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Chris Lattnerb36cbd02005-07-01 22:44:09 +000010// 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
12// assembly language.
13// This printer is the output mechanism used by `llc'.
Chris Lattner72614082002-10-25 22:55:53 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattnerb36cbd02005-07-01 22:44:09 +000017#include "X86ATTAsmPrinter.h"
18#include "X86IntelAsmPrinter.h"
Chris Lattnera35a8e82005-11-21 22:39:40 +000019#include "X86Subtarget.h"
Chris Lattner72614082002-10-25 22:55:53 +000020#include "X86.h"
Chris Lattnera046e0d2005-12-13 04:53:51 +000021#include "llvm/Constants.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000022#include "llvm/Module.h"
Chris Lattnerc41cc832005-11-21 06:46:22 +000023#include "llvm/Type.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000024#include "llvm/Assembly/Writer.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000025#include "llvm/Support/Mangler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000027using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000028using namespace x86;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Jeff Cohen00b168892005-07-27 06:12:32 +000030Statistic<> llvm::x86::EmittedInsts("asm-printer",
Nate Begeman4eb74ba2005-07-01 23:56:38 +000031 "Number of machine instrs printed");
Chris Lattner955f0962004-10-04 07:31:08 +000032
Chris Lattnerb36cbd02005-07-01 22:44:09 +000033enum AsmWriterFlavorTy { att, intel };
34cl::opt<AsmWriterFlavorTy>
35AsmWriterFlavor("x86-asm-syntax",
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000036 cl::desc("Choose style of code to emit from X86 backend:"),
37 cl::values(
38 clEnumVal(att, " Emit AT&T-style assembly"),
39 clEnumVal(intel, " Emit Intel-style assembly"),
40 clEnumValEnd),
41 cl::init(att));
Chris Lattner955f0962004-10-04 07:31:08 +000042
Chris Lattnerb36cbd02005-07-01 22:44:09 +000043/// doInitialization
Chris Lattner5df14ca2005-11-21 22:19:48 +000044bool X86SharedAsmPrinter::doInitialization(Module &M) {
Chris Lattnera35a8e82005-11-21 22:39:40 +000045 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
46
Chris Lattner8fccc972005-11-21 19:50:31 +000047 forDarwin = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000048
49 switch (Subtarget->TargetType) {
Chris Lattnera35a8e82005-11-21 22:39:40 +000050 case X86Subtarget::isDarwin:
Nate Begeman9d19eb42005-06-30 00:53:20 +000051 AlignmentIsInBytes = false;
Chris Lattner8fccc972005-11-21 19:50:31 +000052 GlobalPrefix = "_";
Nate Begeman9035b992005-07-16 01:59:47 +000053 Data64bitsDirective = 0; // we can't emit a 64-bit unit
54 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
Chris Lattnerc41cc832005-11-21 06:46:22 +000055 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattnerd939f6c2005-11-21 08:32:23 +000056 ConstantPoolSection = "\t.const\n";
Chris Lattner8fccc972005-11-21 19:50:31 +000057 LCOMMDirective = "\t.lcomm\t";
58 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000059 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000060 forDarwin = true;
61 break;
62 case X86Subtarget::isCygwin:
Chris Lattner8fccc972005-11-21 19:50:31 +000063 GlobalPrefix = "_";
64 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000065 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000066 break;
67 case X86Subtarget::isWindows:
Chris Lattner8fccc972005-11-21 19:50:31 +000068 GlobalPrefix = "_";
Chris Lattnerac2902b2005-11-21 23:06:54 +000069 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000070 break;
71 default: break;
72 }
Chris Lattner8fccc972005-11-21 19:50:31 +000073
Reid Spencer5dc81f62005-01-23 03:52:14 +000074 return AsmPrinter::doInitialization(M);
75}
76
Chris Lattnera046e0d2005-12-13 04:53:51 +000077/// EmitXXStructorList - Emit the ctor or dtor list. On darwin, this just
78/// prints out the function pointers.
79void X86SharedAsmPrinter::EmitXXStructorList(Constant *List) {
80 // Should be an array of '{ int, void ()* }' structs. The first value is the
81 // init priority, which we ignore.
82 if (!isa<ConstantArray>(List)) return;
83 ConstantArray *InitList = cast<ConstantArray>(List);
84 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
85 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
86 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
87 // Emit the function pointer.
88 EmitGlobalConstant(CS->getOperand(1));
89 }
90}
91
Chris Lattnerac5701c2004-10-04 07:24:48 +000092bool X86SharedAsmPrinter::doFinalization(Module &M) {
93 const TargetData &TD = TM.getTargetData();
Chris Lattnerac5701c2004-10-04 07:24:48 +000094
95 // Print out module-level global variables here.
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000096 for (Module::const_global_iterator I = M.global_begin(),
Chris Lattnera35a8e82005-11-21 22:39:40 +000097 E = M.global_end(); I != E; ++I) {
Chris Lattner9787c642005-11-21 22:48:18 +000098 if (!I->hasInitializer()) continue; // External global require no code
99
Chris Lattnera046e0d2005-12-13 04:53:51 +0000100 // Check to see if this is a special global used by LLVM.
101 if (I->hasAppendingLinkage()) {
102 if (I->getName() == "llvm.used")
103 continue; // No need to emit this at all.
104 if (I->getName() == "llvm.global_ctors") {
105 if (forDarwin)
106 SwitchSection(".mod_init_func", 0);
107 else
108 SwitchSection(".ctors,\"aw\",@progbits", 0);
109 EmitAlignment(2, 0);
110 EmitXXStructorList(I->getInitializer());
111 continue;
112 } else if (I->getName() == "llvm.global_dtors") {
113 if (forDarwin)
114 SwitchSection(".mod_term_func", 0);
115 else
116 SwitchSection(".dtors,\"aw\",@progbits", 0);
117 EmitAlignment(2, 0);
118 EmitXXStructorList(I->getInitializer());
119 continue;
120 }
121 }
122
Chris Lattner9787c642005-11-21 22:48:18 +0000123 O << "\n\n";
124 std::string name = Mang->getValueName(I);
125 Constant *C = I->getInitializer();
126 unsigned Size = TD.getTypeSize(C->getType());
127 unsigned Align = TD.getTypeAlignmentShift(C->getType());
Jeff Cohen00b168892005-07-27 06:12:32 +0000128
Chris Lattner9787c642005-11-21 22:48:18 +0000129 switch (I->getLinkage()) {
130 default: assert(0 && "Unknown linkage type!");
131 case GlobalValue::LinkOnceLinkage:
132 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
133 if (C->isNullValue()) {
134 O << COMMDirective << name << "," << Size;
135 if (COMMDirectiveTakesAlignment)
136 O << "," << (1 << Align);
Chris Lattnerac2902b2005-11-21 23:06:54 +0000137 O << "\t\t" << CommentString << " " << I->getName() << "\n";
Chris Lattner9787c642005-11-21 22:48:18 +0000138 continue;
139 }
140
141 // Nonnull linkonce -> weak
142 O << "\t.weak " << name << "\n";
143 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
144 SwitchSection("", I);
145 break;
146 case GlobalValue::InternalLinkage:
147 if (C->isNullValue()) {
148 if (LCOMMDirective) {
149 O << LCOMMDirective << name << "," << Size << "," << Align;
150 continue;
151 } else {
152 SwitchSection(".bss", I);
153 O << "\t.local " << name << "\n";
Chris Lattner8fccc972005-11-21 19:50:31 +0000154 O << COMMDirective << name << "," << Size;
155 if (COMMDirectiveTakesAlignment)
156 O << "," << (1 << Align);
157 O << "\t\t# ";
158 WriteAsOperand(O, I, true, true, &M);
159 O << "\n";
160 continue;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000161 }
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000162 }
Chris Lattner9787c642005-11-21 22:48:18 +0000163 SwitchSection(".data", I);
164 break;
165 case GlobalValue::AppendingLinkage:
166 // FIXME: appending linkage variables should go into a section of
167 // their name or something. For now, just emit them as external.
168 case GlobalValue::ExternalLinkage:
169 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
170 // If external or appending, declare as a global symbol
171 O << "\t.globl " << name << "\n";
172 break;
Chris Lattnerac5701c2004-10-04 07:24:48 +0000173 }
Chris Lattner9787c642005-11-21 22:48:18 +0000174
175 EmitAlignment(Align);
Chris Lattnerac2902b2005-11-21 23:06:54 +0000176 if (HasDotTypeDotSizeDirective) {
Chris Lattner9787c642005-11-21 22:48:18 +0000177 O << "\t.type " << name << ",@object\n";
178 O << "\t.size " << name << "," << Size << "\n";
179 }
Chris Lattnerac2902b2005-11-21 23:06:54 +0000180 O << name << ":\t\t\t" << CommentString << ' ' << I->getName() << '\n';
Chris Lattner9787c642005-11-21 22:48:18 +0000181 EmitGlobalConstant(C);
Chris Lattnera35a8e82005-11-21 22:39:40 +0000182 }
183
Nate Begeman72b286b2005-07-08 00:23:26 +0000184 if (forDarwin) {
Chris Lattner7b6e53c2005-11-21 07:16:34 +0000185 SwitchSection("", 0);
Nate Begeman73213f62005-07-12 01:37:28 +0000186 // Output stubs for external global variables
187 if (GVStubs.begin() != GVStubs.end())
188 O << "\t.non_lazy_symbol_pointer\n";
189 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
190 i != e; ++i) {
191 O << "L" << *i << "$non_lazy_ptr:\n";
192 O << "\t.indirect_symbol " << *i << "\n";
193 O << "\t.long\t0\n";
194 }
195
Nate Begeman72b286b2005-07-08 00:23:26 +0000196 // Output stubs for dynamically-linked functions
197 unsigned j = 1;
198 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000199 i != e; ++i, ++j) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000200 O << "\t.symbol_stub\n";
201 O << "L" << *i << "$stub:\n";
202 O << "\t.indirect_symbol " << *i << "\n";
203 O << "\tjmp\t*L" << j << "$lz\n";
204 O << "L" << *i << "$stub_binder:\n";
205 O << "\tpushl\t$L" << j << "$lz\n";
206 O << "\tjmp\tdyld_stub_binding_helper\n";
207 O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
208 O << "L" << j << "$lz:\n";
209 O << "\t.indirect_symbol " << *i << "\n";
210 O << "\t.long\tL" << *i << "$stub_binder\n";
211 }
212
213 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000214
Nate Begeman72b286b2005-07-08 00:23:26 +0000215 // Output stubs for link-once variables
216 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
217 O << ".data\n.align 2\n";
218 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
219 e = LinkOnceStubs.end(); i != e; ++i) {
220 O << "L" << *i << "$non_lazy_ptr:\n"
221 << "\t.long\t" << *i << '\n';
222 }
223 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000224
225 AsmPrinter::doFinalization(M);
226 return false; // success
227}
228
Chris Lattnerac5701c2004-10-04 07:24:48 +0000229/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
230/// for a MachineFunction to the given output stream, using the given target
231/// machine description.
232///
233FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
234 switch (AsmWriterFlavor) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000235 default:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000236 assert(0 && "Unknown asm flavor!");
Chris Lattnerac5701c2004-10-04 07:24:48 +0000237 case intel:
238 return new X86IntelAsmPrinter(o, tm);
239 case att:
240 return new X86ATTAsmPrinter(o, tm);
241 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000242}