blob: 36f4249e223a58da644036ddbbfe33412a0d9e4d [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
Reid Spencer5dc81f62005-01-23 03:52:14 +000042bool X86SharedAsmPrinter::doInitialization(Module& M) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +000043 forCygwin = false;
Chris Lattner8fccc972005-11-21 19:50:31 +000044 forDarwin = false;
45 bool forWin32 = 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 Lattner8fccc972005-11-21 19:50:31 +000057 forWin32 = true;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000058#endif
Reid Spencer5dc81f62005-01-23 03:52:14 +000059 }
Jeff Cohen00b168892005-07-27 06:12:32 +000060
Nate Begeman9035b992005-07-16 01:59:47 +000061 if (forDarwin) {
Nate Begeman9d19eb42005-06-30 00:53:20 +000062 AlignmentIsInBytes = false;
Chris Lattner8fccc972005-11-21 19:50:31 +000063 GlobalPrefix = "_";
Nate Begeman9035b992005-07-16 01:59:47 +000064 Data64bitsDirective = 0; // we can't emit a 64-bit unit
65 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
Chris Lattnerc41cc832005-11-21 06:46:22 +000066 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattnerd939f6c2005-11-21 08:32:23 +000067 ConstantPoolSection = "\t.const\n";
Chris Lattner8fccc972005-11-21 19:50:31 +000068 LCOMMDirective = "\t.lcomm\t";
69 COMMDirectiveTakesAlignment = false;
70 } else if (forCygwin) {
71 GlobalPrefix = "_";
72 COMMDirectiveTakesAlignment = false;
73 } else if (forWin32) {
74 GlobalPrefix = "_";
75 }
76
Reid Spencer5dc81f62005-01-23 03:52:14 +000077 return AsmPrinter::doInitialization(M);
78}
79
Chris Lattnerac5701c2004-10-04 07:24:48 +000080bool X86SharedAsmPrinter::doFinalization(Module &M) {
81 const TargetData &TD = TM.getTargetData();
Chris Lattnerac5701c2004-10-04 07:24:48 +000082
83 // Print out module-level global variables here.
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000084 for (Module::const_global_iterator I = M.global_begin(),
85 E = M.global_end(); I != E; ++I)
86 if (I->hasInitializer()) { // External global require no code
87 O << "\n\n";
88 std::string name = Mang->getValueName(I);
89 Constant *C = I->getInitializer();
90 unsigned Size = TD.getTypeSize(C->getType());
91 unsigned Align = TD.getTypeAlignmentShift(C->getType());
Jeff Cohen00b168892005-07-27 06:12:32 +000092
Chris Lattner8fccc972005-11-21 19:50:31 +000093 switch (I->getLinkage()) {
94 default: assert(0 && "Unknown linkage type!");
95 case GlobalValue::LinkOnceLinkage:
96 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
97 if (C->isNullValue()) {
98 O << COMMDirective << name << "," << Size;
99 if (COMMDirectiveTakesAlignment)
100 O << "," << (1 << Align);
101 O << "\t\t# ";
102 WriteAsOperand(O, I, true, true, &M);
103 O << "\n";
104 continue;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000105 }
Chris Lattner8fccc972005-11-21 19:50:31 +0000106
107 // Nonnull linkonce -> weak
108 O << "\t.weak " << name << "\n";
109 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
110 SwitchSection("", I);
111 break;
112 case GlobalValue::InternalLinkage:
113 if (C->isNullValue()) {
114 if (LCOMMDirective) {
115 O << LCOMMDirective << name << "," << Size << "," << Align;
116 continue;
117 } else {
118 SwitchSection(".bss", I);
119 O << "\t.local " << name << "\n";
120 O << COMMDirective << name << "," << Size;
121 if (COMMDirectiveTakesAlignment)
122 O << "," << (1 << Align);
123 O << "\t\t# ";
124 WriteAsOperand(O, I, true, true, &M);
125 O << "\n";
126 continue;
127 }
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000128 }
Chris Lattner8fccc972005-11-21 19:50:31 +0000129 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
130 break;
131 case GlobalValue::AppendingLinkage:
132 // FIXME: appending linkage variables should go into a section of
133 // their name or something. For now, just emit them as external.
134 case GlobalValue::ExternalLinkage:
135 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
136 // If external or appending, declare as a global symbol
137 O << "\t.globl " << name << "\n";
138 break;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000139 }
Chris Lattner8fccc972005-11-21 19:50:31 +0000140
141 EmitAlignment(Align);
142 if (!forCygwin && !forDarwin) {
143 O << "\t.type " << name << ",@object\n";
144 O << "\t.size " << name << "," << Size << "\n";
145 }
146 O << name << ":\t\t\t\t# ";
147 WriteAsOperand(O, I, true, true, &M);
148 O << " = ";
149 WriteAsOperand(O, C, false, false, &M);
150 O << "\n";
151 EmitGlobalConstant(C);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000152 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000153
Nate Begeman72b286b2005-07-08 00:23:26 +0000154 if (forDarwin) {
Chris Lattner7b6e53c2005-11-21 07:16:34 +0000155 SwitchSection("", 0);
Nate Begeman73213f62005-07-12 01:37:28 +0000156 // Output stubs for external global variables
157 if (GVStubs.begin() != GVStubs.end())
158 O << "\t.non_lazy_symbol_pointer\n";
159 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
160 i != e; ++i) {
161 O << "L" << *i << "$non_lazy_ptr:\n";
162 O << "\t.indirect_symbol " << *i << "\n";
163 O << "\t.long\t0\n";
164 }
165
Nate Begeman72b286b2005-07-08 00:23:26 +0000166 // Output stubs for dynamically-linked functions
167 unsigned j = 1;
168 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000169 i != e; ++i, ++j) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000170 O << "\t.symbol_stub\n";
171 O << "L" << *i << "$stub:\n";
172 O << "\t.indirect_symbol " << *i << "\n";
173 O << "\tjmp\t*L" << j << "$lz\n";
174 O << "L" << *i << "$stub_binder:\n";
175 O << "\tpushl\t$L" << j << "$lz\n";
176 O << "\tjmp\tdyld_stub_binding_helper\n";
177 O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
178 O << "L" << j << "$lz:\n";
179 O << "\t.indirect_symbol " << *i << "\n";
180 O << "\t.long\tL" << *i << "$stub_binder\n";
181 }
182
183 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000184
Nate Begeman72b286b2005-07-08 00:23:26 +0000185 // Output stubs for link-once variables
186 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
187 O << ".data\n.align 2\n";
188 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
189 e = LinkOnceStubs.end(); i != e; ++i) {
190 O << "L" << *i << "$non_lazy_ptr:\n"
191 << "\t.long\t" << *i << '\n';
192 }
193 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000194
195 AsmPrinter::doFinalization(M);
196 return false; // success
197}
198
Chris Lattnerac5701c2004-10-04 07:24:48 +0000199/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
200/// for a MachineFunction to the given output stream, using the given target
201/// machine description.
202///
203FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
204 switch (AsmWriterFlavor) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000205 default:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000206 assert(0 && "Unknown asm flavor!");
Chris Lattnerac5701c2004-10-04 07:24:48 +0000207 case intel:
208 return new X86IntelAsmPrinter(o, tm);
209 case att:
210 return new X86ATTAsmPrinter(o, tm);
211 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000212}