blob: e0bd0de6c86e87ef183b31b309445ae9bbf5f33e [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"
Chris Lattnerb7089442003-01-13 00:35:03 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000024#include "llvm/Support/Mangler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/Support/CommandLine.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000026using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000027using namespace x86;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Jeff Cohen00b168892005-07-27 06:12:32 +000029Statistic<> llvm::x86::EmittedInsts("asm-printer",
Nate Begeman4eb74ba2005-07-01 23:56:38 +000030 "Number of machine instrs printed");
Chris Lattner955f0962004-10-04 07:31:08 +000031
Chris Lattnerb36cbd02005-07-01 22:44:09 +000032enum AsmWriterFlavorTy { att, intel };
33cl::opt<AsmWriterFlavorTy>
34AsmWriterFlavor("x86-asm-syntax",
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000035 cl::desc("Choose style of code to emit from X86 backend:"),
36 cl::values(
37 clEnumVal(att, " Emit AT&T-style assembly"),
38 clEnumVal(intel, " Emit Intel-style assembly"),
39 clEnumValEnd),
40 cl::init(att));
Chris Lattner955f0962004-10-04 07:31:08 +000041
Chris Lattnerb36cbd02005-07-01 22:44:09 +000042/// doInitialization
Reid Spencer5dc81f62005-01-23 03:52:14 +000043bool X86SharedAsmPrinter::doInitialization(Module& M) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +000044 bool leadingUnderscore = false;
45 forCygwin = false;
Reid Spencer5dc81f62005-01-23 03:52:14 +000046 const std::string& TT = M.getTargetTriple();
Nate Begeman9d19eb42005-06-30 00:53:20 +000047 if (TT.length() > 5) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +000048 forCygwin = TT.find("cygwin") != std::string::npos ||
Reid Spencerd632f492005-03-08 17:02:05 +000049 TT.find("mingw") != std::string::npos;
Nate Begeman9d19eb42005-06-30 00:53:20 +000050 forDarwin = TT.find("darwin") != std::string::npos;
51 } else if (TT.empty()) {
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000052#if defined(__CYGWIN__) || defined(__MINGW32__)
Reid Spencer5dc81f62005-01-23 03:52:14 +000053 forCygwin = true;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000054#elif defined(__APPLE__)
Nate Begeman9d19eb42005-06-30 00:53:20 +000055 forDarwin = true;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000056#elif defined(_WIN32)
Chris Lattnerb36cbd02005-07-01 22:44:09 +000057 leadingUnderscore = true;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000058#else
Chris Lattnerb36cbd02005-07-01 22:44:09 +000059 leadingUnderscore = false;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000060#endif
Reid Spencer5dc81f62005-01-23 03:52:14 +000061 }
Jeff Cohen00b168892005-07-27 06:12:32 +000062
Chris Lattnerb36cbd02005-07-01 22:44:09 +000063 if (leadingUnderscore || forCygwin || forDarwin)
Reid Spencer5dc81f62005-01-23 03:52:14 +000064 GlobalPrefix = "_";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000065
Nate Begeman9035b992005-07-16 01:59:47 +000066 if (forDarwin) {
Nate Begeman9d19eb42005-06-30 00:53:20 +000067 AlignmentIsInBytes = false;
Nate Begeman9035b992005-07-16 01:59:47 +000068 Data64bitsDirective = 0; // we can't emit a 64-bit unit
69 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
Chris Lattnerc41cc832005-11-21 06:46:22 +000070 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Nate Begeman9035b992005-07-16 01:59:47 +000071 }
Jeff Cohen00b168892005-07-27 06:12:32 +000072
Reid Spencer5dc81f62005-01-23 03:52:14 +000073 return AsmPrinter::doInitialization(M);
74}
75
Chris Lattnerac5701c2004-10-04 07:24:48 +000076/// printConstantPool - Print to the current output stream assembly
77/// representations of the constants in the constant pool MCP. This is
78/// used to print out constants which have been "spilled to memory" by
79/// the code generator.
80///
81void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
82 const std::vector<Constant*> &CP = MCP->getConstants();
83 const TargetData &TD = TM.getTargetData();
Misha Brukman0e0a7a452005-04-21 23:38:14 +000084
Chris Lattnerac5701c2004-10-04 07:24:48 +000085 if (CP.empty()) return;
86
Chris Lattner7b6e53c2005-11-21 07:16:34 +000087 SwitchSection(forDarwin ? "\t.const\n" : "\t.section .rodata\n", 0);
Chris Lattnerc41cc832005-11-21 06:46:22 +000088
Chris Lattnerac5701c2004-10-04 07:24:48 +000089 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattnerc41cc832005-11-21 06:46:22 +000090 // FIXME: force doubles to be naturally aligned. We should handle this
91 // more correctly in the future.
92 if (CP[i]->getType() == Type::DoubleTy)
93 emitAlignment(3);
Nate Begeman72b286b2005-07-08 00:23:26 +000094 else
Chris Lattnerc41cc832005-11-21 06:46:22 +000095 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
96 O << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_" << i
97 << ":\t\t\t\t\t" << CommentString << *CP[i] << "\n";
Chris Lattnerac5701c2004-10-04 07:24:48 +000098 emitGlobalConstant(CP[i]);
99 }
100}
101
102bool X86SharedAsmPrinter::doFinalization(Module &M) {
103 const TargetData &TD = TM.getTargetData();
Chris Lattnerac5701c2004-10-04 07:24:48 +0000104
105 // Print out module-level global variables here.
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000106 for (Module::const_global_iterator I = M.global_begin(),
107 E = M.global_end(); I != E; ++I)
108 if (I->hasInitializer()) { // External global require no code
109 O << "\n\n";
110 std::string name = Mang->getValueName(I);
111 Constant *C = I->getInitializer();
112 unsigned Size = TD.getTypeSize(C->getType());
113 unsigned Align = TD.getTypeAlignmentShift(C->getType());
Jeff Cohen00b168892005-07-27 06:12:32 +0000114
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000115 if (C->isNullValue() &&
116 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
117 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner224cfc22005-11-21 07:11:11 +0000118 SwitchSection(".data", I);
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000119 if (!forCygwin && !forDarwin && I->hasInternalLinkage())
120 O << "\t.local " << name << "\n";
121 if (forDarwin && I->hasInternalLinkage())
122 O << "\t.lcomm " << name << "," << Size << "," << Align;
Jeff Cohen00b168892005-07-27 06:12:32 +0000123 else
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000124 O << "\t.comm " << name << "," << Size;
125 if (!forCygwin && !forDarwin)
126 O << "," << (1 << Align);
127 O << "\t\t# ";
128 WriteAsOperand(O, I, true, true, &M);
129 O << "\n";
130 } else {
131 switch (I->getLinkage()) {
132 default: assert(0 && "Unknown linkage type!");
133 case GlobalValue::LinkOnceLinkage:
134 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
135 // Nonnull linkonce -> weak
136 O << "\t.weak " << name << "\n";
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000137 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
Chris Lattner224cfc22005-11-21 07:11:11 +0000138 SwitchSection("", I);
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000139 break;
140 case GlobalValue::AppendingLinkage:
141 // FIXME: appending linkage variables should go into a section of
142 // their name or something. For now, just emit them as external.
143 case GlobalValue::ExternalLinkage:
144 // If external or appending, declare as a global symbol
145 O << "\t.globl " << name << "\n";
146 // FALL THROUGH
147 case GlobalValue::InternalLinkage:
Chris Lattner224cfc22005-11-21 07:11:11 +0000148 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000149 break;
150 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000151
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000152 emitAlignment(Align);
153 if (!forCygwin && !forDarwin) {
154 O << "\t.type " << name << ",@object\n";
155 O << "\t.size " << name << "," << Size << "\n";
156 }
157 O << name << ":\t\t\t\t# ";
158 WriteAsOperand(O, I, true, true, &M);
159 O << " = ";
160 WriteAsOperand(O, C, false, false, &M);
161 O << "\n";
162 emitGlobalConstant(C);
163 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000164 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000165
Nate Begeman72b286b2005-07-08 00:23:26 +0000166 if (forDarwin) {
Chris Lattner7b6e53c2005-11-21 07:16:34 +0000167 SwitchSection("", 0);
Nate Begeman73213f62005-07-12 01:37:28 +0000168 // Output stubs for external global variables
169 if (GVStubs.begin() != GVStubs.end())
170 O << "\t.non_lazy_symbol_pointer\n";
171 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
172 i != e; ++i) {
173 O << "L" << *i << "$non_lazy_ptr:\n";
174 O << "\t.indirect_symbol " << *i << "\n";
175 O << "\t.long\t0\n";
176 }
177
Nate Begeman72b286b2005-07-08 00:23:26 +0000178 // Output stubs for dynamically-linked functions
179 unsigned j = 1;
180 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000181 i != e; ++i, ++j) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000182 O << "\t.symbol_stub\n";
183 O << "L" << *i << "$stub:\n";
184 O << "\t.indirect_symbol " << *i << "\n";
185 O << "\tjmp\t*L" << j << "$lz\n";
186 O << "L" << *i << "$stub_binder:\n";
187 O << "\tpushl\t$L" << j << "$lz\n";
188 O << "\tjmp\tdyld_stub_binding_helper\n";
189 O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
190 O << "L" << j << "$lz:\n";
191 O << "\t.indirect_symbol " << *i << "\n";
192 O << "\t.long\tL" << *i << "$stub_binder\n";
193 }
194
195 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000196
Nate Begeman72b286b2005-07-08 00:23:26 +0000197 // Output stubs for link-once variables
198 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
199 O << ".data\n.align 2\n";
200 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
201 e = LinkOnceStubs.end(); i != e; ++i) {
202 O << "L" << *i << "$non_lazy_ptr:\n"
203 << "\t.long\t" << *i << '\n';
204 }
205 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000206
207 AsmPrinter::doFinalization(M);
208 return false; // success
209}
210
Chris Lattnerac5701c2004-10-04 07:24:48 +0000211/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
212/// for a MachineFunction to the given output stream, using the given target
213/// machine description.
214///
215FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
216 switch (AsmWriterFlavor) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000217 default:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000218 assert(0 && "Unknown asm flavor!");
Chris Lattnerac5701c2004-10-04 07:24:48 +0000219 case intel:
220 return new X86IntelAsmPrinter(o, tm);
221 case att:
222 return new X86ATTAsmPrinter(o, tm);
223 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000224}