blob: 17752b9652119870ac4d81e4105a77b1c76f5036 [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"
21#include "llvm/Assembly/Writer.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000022#include "llvm/CodeGen/MachineConstantPool.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
Nate Begeman4eb74ba2005-07-01 23:56:38 +000028Statistic<> llvm::x86::EmittedInsts("asm-printer",
29 "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 bool leadingUnderscore = false;
44 forCygwin = false;
Reid Spencer5dc81f62005-01-23 03:52:14 +000045 const std::string& TT = M.getTargetTriple();
Nate Begeman9d19eb42005-06-30 00:53:20 +000046 if (TT.length() > 5) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +000047 forCygwin = TT.find("cygwin") != std::string::npos ||
Reid Spencerd632f492005-03-08 17:02:05 +000048 TT.find("mingw") != std::string::npos;
Nate Begeman9d19eb42005-06-30 00:53:20 +000049 forDarwin = TT.find("darwin") != std::string::npos;
50 } else if (TT.empty()) {
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000051#if defined(__CYGWIN__) || defined(__MINGW32__)
Reid Spencer5dc81f62005-01-23 03:52:14 +000052 forCygwin = true;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000053#elif defined(__APPLE__)
Nate Begeman9d19eb42005-06-30 00:53:20 +000054 forDarwin = true;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000055#elif defined(_WIN32)
Chris Lattnerb36cbd02005-07-01 22:44:09 +000056 leadingUnderscore = true;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000057#else
Chris Lattnerb36cbd02005-07-01 22:44:09 +000058 leadingUnderscore = false;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000059#endif
Reid Spencer5dc81f62005-01-23 03:52:14 +000060 }
Nate Begeman73213f62005-07-12 01:37:28 +000061
Chris Lattnerb36cbd02005-07-01 22:44:09 +000062 if (leadingUnderscore || forCygwin || forDarwin)
Reid Spencer5dc81f62005-01-23 03:52:14 +000063 GlobalPrefix = "_";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000064
Nate Begeman9d19eb42005-06-30 00:53:20 +000065 if (forDarwin)
66 AlignmentIsInBytes = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000067
Reid Spencer5dc81f62005-01-23 03:52:14 +000068 return AsmPrinter::doInitialization(M);
69}
70
Chris Lattnerac5701c2004-10-04 07:24:48 +000071/// printConstantPool - Print to the current output stream assembly
72/// representations of the constants in the constant pool MCP. This is
73/// used to print out constants which have been "spilled to memory" by
74/// the code generator.
75///
76void X86SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
77 const std::vector<Constant*> &CP = MCP->getConstants();
78 const TargetData &TD = TM.getTargetData();
Misha Brukman0e0a7a452005-04-21 23:38:14 +000079
Chris Lattnerac5701c2004-10-04 07:24:48 +000080 if (CP.empty()) return;
81
82 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Nate Begeman72b286b2005-07-08 00:23:26 +000083 if (forDarwin)
84 O << "\t.data\n";
85 else
86 O << "\t.section .rodata\n";
Chris Lattnerac5701c2004-10-04 07:24:48 +000087 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
88 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
89 << *CP[i] << "\n";
90 emitGlobalConstant(CP[i]);
91 }
92}
93
94bool X86SharedAsmPrinter::doFinalization(Module &M) {
95 const TargetData &TD = TM.getTargetData();
96 std::string CurSection;
97
98 // Print out module-level global variables here.
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000099 for (Module::const_global_iterator I = M.global_begin(),
100 E = M.global_end(); I != E; ++I)
101 if (I->hasInitializer()) { // External global require no code
102 O << "\n\n";
103 std::string name = Mang->getValueName(I);
104 Constant *C = I->getInitializer();
105 unsigned Size = TD.getTypeSize(C->getType());
106 unsigned Align = TD.getTypeAlignmentShift(C->getType());
107
108 if (C->isNullValue() &&
109 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
110 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
111 SwitchSection(O, CurSection, ".data");
112 if (!forCygwin && !forDarwin && I->hasInternalLinkage())
113 O << "\t.local " << name << "\n";
114 if (forDarwin && I->hasInternalLinkage())
115 O << "\t.lcomm " << name << "," << Size << "," << Align;
116 else
117 O << "\t.comm " << name << "," << Size;
118 if (!forCygwin && !forDarwin)
119 O << "," << (1 << Align);
120 O << "\t\t# ";
121 WriteAsOperand(O, I, true, true, &M);
122 O << "\n";
123 } else {
124 switch (I->getLinkage()) {
125 default: assert(0 && "Unknown linkage type!");
126 case GlobalValue::LinkOnceLinkage:
127 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
128 // Nonnull linkonce -> weak
129 O << "\t.weak " << name << "\n";
130 SwitchSection(O, CurSection, "");
131 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
132 break;
133 case GlobalValue::AppendingLinkage:
134 // FIXME: appending linkage variables should go into a section of
135 // their name or something. For now, just emit them as external.
136 case GlobalValue::ExternalLinkage:
137 // If external or appending, declare as a global symbol
138 O << "\t.globl " << name << "\n";
139 // FALL THROUGH
140 case GlobalValue::InternalLinkage:
141 if (C->isNullValue())
142 SwitchSection(O, CurSection, ".bss");
143 else
144 SwitchSection(O, CurSection, ".data");
145 break;
146 }
147
148 emitAlignment(Align);
149 if (!forCygwin && !forDarwin) {
150 O << "\t.type " << name << ",@object\n";
151 O << "\t.size " << name << "," << Size << "\n";
152 }
153 O << name << ":\t\t\t\t# ";
154 WriteAsOperand(O, I, true, true, &M);
155 O << " = ";
156 WriteAsOperand(O, C, false, false, &M);
157 O << "\n";
158 emitGlobalConstant(C);
159 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000160 }
Nate Begeman72b286b2005-07-08 00:23:26 +0000161
162 if (forDarwin) {
Nate Begeman73213f62005-07-12 01:37:28 +0000163 // Output stubs for external global variables
164 if (GVStubs.begin() != GVStubs.end())
165 O << "\t.non_lazy_symbol_pointer\n";
166 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
167 i != e; ++i) {
168 O << "L" << *i << "$non_lazy_ptr:\n";
169 O << "\t.indirect_symbol " << *i << "\n";
170 O << "\t.long\t0\n";
171 }
172
Nate Begeman72b286b2005-07-08 00:23:26 +0000173 // Output stubs for dynamically-linked functions
174 unsigned j = 1;
175 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000176 i != e; ++i, ++j) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000177 O << "\t.symbol_stub\n";
178 O << "L" << *i << "$stub:\n";
179 O << "\t.indirect_symbol " << *i << "\n";
180 O << "\tjmp\t*L" << j << "$lz\n";
181 O << "L" << *i << "$stub_binder:\n";
182 O << "\tpushl\t$L" << j << "$lz\n";
183 O << "\tjmp\tdyld_stub_binding_helper\n";
184 O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
185 O << "L" << j << "$lz:\n";
186 O << "\t.indirect_symbol " << *i << "\n";
187 O << "\t.long\tL" << *i << "$stub_binder\n";
188 }
189
190 O << "\n";
191
Nate Begeman72b286b2005-07-08 00:23:26 +0000192 // Output stubs for link-once variables
193 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
194 O << ".data\n.align 2\n";
195 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
196 e = LinkOnceStubs.end(); i != e; ++i) {
197 O << "L" << *i << "$non_lazy_ptr:\n"
198 << "\t.long\t" << *i << '\n';
199 }
200 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000201
202 AsmPrinter::doFinalization(M);
203 return false; // success
204}
205
Chris Lattnerac5701c2004-10-04 07:24:48 +0000206/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
207/// for a MachineFunction to the given output stream, using the given target
208/// machine description.
209///
210FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
211 switch (AsmWriterFlavor) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000212 default:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000213 assert(0 && "Unknown asm flavor!");
Chris Lattnerac5701c2004-10-04 07:24:48 +0000214 case intel:
215 return new X86IntelAsmPrinter(o, tm);
216 case att:
217 return new X86ATTAsmPrinter(o, tm);
218 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000219}