blob: 5b9742bd4406f802b5ad779fdf1053dfaf026f13 [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 Lattner72614082002-10-25 22:55:53 +000019#include "X86.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000020#include "llvm/Module.h"
Chris Lattnerc41cc832005-11-21 06:46:22 +000021#include "llvm/Type.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000022#include "llvm/Assembly/Writer.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000023#include "llvm/Support/Mangler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000025using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000026using namespace x86;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Jeff Cohen00b168892005-07-27 06:12:32 +000028Statistic<> llvm::x86::EmittedInsts("asm-printer",
Nate Begeman4eb74ba2005-07-01 23:56:38 +000029 "Number of machine instrs printed");
Chris Lattner955f0962004-10-04 07:31:08 +000030
Chris Lattnerb36cbd02005-07-01 22:44:09 +000031enum AsmWriterFlavorTy { att, intel };
32cl::opt<AsmWriterFlavorTy>
33AsmWriterFlavor("x86-asm-syntax",
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000034 cl::desc("Choose style of code to emit from X86 backend:"),
35 cl::values(
36 clEnumVal(att, " Emit AT&T-style assembly"),
37 clEnumVal(intel, " Emit Intel-style assembly"),
38 clEnumValEnd),
39 cl::init(att));
Chris Lattner955f0962004-10-04 07:31:08 +000040
Chris Lattnerb36cbd02005-07-01 22:44:09 +000041/// doInitialization
Chris Lattner5df14ca2005-11-21 22:19:48 +000042bool X86SharedAsmPrinter::doInitialization(Module &M) {
43 bool forCygwin = false;
Chris Lattner8fccc972005-11-21 19:50:31 +000044 forDarwin = false;
Chris Lattner5df14ca2005-11-21 22:19:48 +000045 forELF = false;
Chris Lattner8fccc972005-11-21 19:50:31 +000046 bool forWin32 = false;
Reid Spencer5dc81f62005-01-23 03:52:14 +000047 const std::string& TT = M.getTargetTriple();
Nate Begeman9d19eb42005-06-30 00:53:20 +000048 if (TT.length() > 5) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +000049 forCygwin = TT.find("cygwin") != std::string::npos ||
Reid Spencerd632f492005-03-08 17:02:05 +000050 TT.find("mingw") != std::string::npos;
Nate Begeman9d19eb42005-06-30 00:53:20 +000051 forDarwin = TT.find("darwin") != std::string::npos;
Chris Lattner5df14ca2005-11-21 22:19:48 +000052 if (!forDarwin && !forCygwin)
53 forELF = true;
Nate Begeman9d19eb42005-06-30 00:53:20 +000054 } else if (TT.empty()) {
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000055#if defined(__CYGWIN__) || defined(__MINGW32__)
Reid Spencer5dc81f62005-01-23 03:52:14 +000056 forCygwin = true;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000057#elif defined(__APPLE__)
Nate Begeman9d19eb42005-06-30 00:53:20 +000058 forDarwin = true;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000059#elif defined(_WIN32)
Chris Lattner8fccc972005-11-21 19:50:31 +000060 forWin32 = true;
Chris Lattner5df14ca2005-11-21 22:19:48 +000061#else
62 forELF = true;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000063#endif
Reid Spencer5dc81f62005-01-23 03:52:14 +000064 }
Jeff Cohen00b168892005-07-27 06:12:32 +000065
Nate Begeman9035b992005-07-16 01:59:47 +000066 if (forDarwin) {
Nate Begeman9d19eb42005-06-30 00:53:20 +000067 AlignmentIsInBytes = false;
Chris Lattner8fccc972005-11-21 19:50:31 +000068 GlobalPrefix = "_";
Nate Begeman9035b992005-07-16 01:59:47 +000069 Data64bitsDirective = 0; // we can't emit a 64-bit unit
70 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
Chris Lattnerc41cc832005-11-21 06:46:22 +000071 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattnerd939f6c2005-11-21 08:32:23 +000072 ConstantPoolSection = "\t.const\n";
Chris Lattner8fccc972005-11-21 19:50:31 +000073 LCOMMDirective = "\t.lcomm\t";
74 COMMDirectiveTakesAlignment = false;
75 } else if (forCygwin) {
76 GlobalPrefix = "_";
77 COMMDirectiveTakesAlignment = false;
78 } else if (forWin32) {
79 GlobalPrefix = "_";
80 }
81
Reid Spencer5dc81f62005-01-23 03:52:14 +000082 return AsmPrinter::doInitialization(M);
83}
84
Chris Lattnerac5701c2004-10-04 07:24:48 +000085bool X86SharedAsmPrinter::doFinalization(Module &M) {
86 const TargetData &TD = TM.getTargetData();
Chris Lattnerac5701c2004-10-04 07:24:48 +000087
88 // Print out module-level global variables here.
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000089 for (Module::const_global_iterator I = M.global_begin(),
90 E = M.global_end(); I != E; ++I)
91 if (I->hasInitializer()) { // External global require no code
92 O << "\n\n";
93 std::string name = Mang->getValueName(I);
94 Constant *C = I->getInitializer();
95 unsigned Size = TD.getTypeSize(C->getType());
96 unsigned Align = TD.getTypeAlignmentShift(C->getType());
Jeff Cohen00b168892005-07-27 06:12:32 +000097
Chris Lattner8fccc972005-11-21 19:50:31 +000098 switch (I->getLinkage()) {
99 default: assert(0 && "Unknown linkage type!");
100 case GlobalValue::LinkOnceLinkage:
101 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
102 if (C->isNullValue()) {
103 O << COMMDirective << name << "," << Size;
104 if (COMMDirectiveTakesAlignment)
105 O << "," << (1 << Align);
106 O << "\t\t# ";
107 WriteAsOperand(O, I, true, true, &M);
108 O << "\n";
109 continue;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000110 }
Chris Lattner8fccc972005-11-21 19:50:31 +0000111
112 // Nonnull linkonce -> weak
113 O << "\t.weak " << name << "\n";
114 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
115 SwitchSection("", I);
116 break;
117 case GlobalValue::InternalLinkage:
118 if (C->isNullValue()) {
119 if (LCOMMDirective) {
120 O << LCOMMDirective << name << "," << Size << "," << Align;
121 continue;
122 } else {
123 SwitchSection(".bss", I);
124 O << "\t.local " << name << "\n";
125 O << COMMDirective << name << "," << Size;
126 if (COMMDirectiveTakesAlignment)
127 O << "," << (1 << Align);
128 O << "\t\t# ";
129 WriteAsOperand(O, I, true, true, &M);
130 O << "\n";
131 continue;
132 }
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000133 }
Chris Lattner8fccc972005-11-21 19:50:31 +0000134 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
135 break;
136 case GlobalValue::AppendingLinkage:
137 // FIXME: appending linkage variables should go into a section of
138 // their name or something. For now, just emit them as external.
139 case GlobalValue::ExternalLinkage:
140 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
141 // If external or appending, declare as a global symbol
142 O << "\t.globl " << name << "\n";
143 break;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000144 }
Chris Lattner8fccc972005-11-21 19:50:31 +0000145
146 EmitAlignment(Align);
Chris Lattner5df14ca2005-11-21 22:19:48 +0000147 if (forELF) {
Chris Lattner8fccc972005-11-21 19:50:31 +0000148 O << "\t.type " << name << ",@object\n";
149 O << "\t.size " << name << "," << Size << "\n";
150 }
151 O << name << ":\t\t\t\t# ";
152 WriteAsOperand(O, I, true, true, &M);
153 O << " = ";
154 WriteAsOperand(O, C, false, false, &M);
155 O << "\n";
156 EmitGlobalConstant(C);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000157 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000158
Nate Begeman72b286b2005-07-08 00:23:26 +0000159 if (forDarwin) {
Chris Lattner7b6e53c2005-11-21 07:16:34 +0000160 SwitchSection("", 0);
Nate Begeman73213f62005-07-12 01:37:28 +0000161 // Output stubs for external global variables
162 if (GVStubs.begin() != GVStubs.end())
163 O << "\t.non_lazy_symbol_pointer\n";
164 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
165 i != e; ++i) {
166 O << "L" << *i << "$non_lazy_ptr:\n";
167 O << "\t.indirect_symbol " << *i << "\n";
168 O << "\t.long\t0\n";
169 }
170
Nate Begeman72b286b2005-07-08 00:23:26 +0000171 // Output stubs for dynamically-linked functions
172 unsigned j = 1;
173 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000174 i != e; ++i, ++j) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000175 O << "\t.symbol_stub\n";
176 O << "L" << *i << "$stub:\n";
177 O << "\t.indirect_symbol " << *i << "\n";
178 O << "\tjmp\t*L" << j << "$lz\n";
179 O << "L" << *i << "$stub_binder:\n";
180 O << "\tpushl\t$L" << j << "$lz\n";
181 O << "\tjmp\tdyld_stub_binding_helper\n";
182 O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
183 O << "L" << j << "$lz:\n";
184 O << "\t.indirect_symbol " << *i << "\n";
185 O << "\t.long\tL" << *i << "$stub_binder\n";
186 }
187
188 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000189
Nate Begeman72b286b2005-07-08 00:23:26 +0000190 // Output stubs for link-once variables
191 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
192 O << ".data\n.align 2\n";
193 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
194 e = LinkOnceStubs.end(); i != e; ++i) {
195 O << "L" << *i << "$non_lazy_ptr:\n"
196 << "\t.long\t" << *i << '\n';
197 }
198 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000199
200 AsmPrinter::doFinalization(M);
201 return false; // success
202}
203
Chris Lattnerac5701c2004-10-04 07:24:48 +0000204/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
205/// for a MachineFunction to the given output stream, using the given target
206/// machine description.
207///
208FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
209 switch (AsmWriterFlavor) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000210 default:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000211 assert(0 && "Unknown asm flavor!");
Chris Lattnerac5701c2004-10-04 07:24:48 +0000212 case intel:
213 return new X86IntelAsmPrinter(o, tm);
214 case att:
215 return new X86ATTAsmPrinter(o, tm);
216 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000217}