blob: 1c73ae7d96f3506d4b50b58ac5aaad603b562b49 [file] [log] [blame]
Chris Lattnerb9740462005-07-01 22:44:09 +00001//===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 assembly -----------===//
Misha Brukmanc88330a2005-04-21 23:38:14 +00002//
John Criswell482202a2003-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 Brukmanc88330a2005-04-21 23:38:14 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd92fb002002-10-25 22:55:53 +00009//
Chris Lattnerb9740462005-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 Lattnerd92fb002002-10-25 22:55:53 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattnerb9740462005-07-01 22:44:09 +000017#include "X86ATTAsmPrinter.h"
18#include "X86IntelAsmPrinter.h"
Chris Lattnerd92fb002002-10-25 22:55:53 +000019#include "X86.h"
Chris Lattner1a2e6f72003-08-11 20:06:16 +000020#include "llvm/Module.h"
Chris Lattner618981f2005-11-21 06:46:22 +000021#include "llvm/Type.h"
Chris Lattner1a2e6f72003-08-11 20:06:16 +000022#include "llvm/Assembly/Writer.h"
Brian Gaeke46f8b712003-07-24 20:20:44 +000023#include "llvm/Support/Mangler.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Chris Lattner9f75a552004-02-14 06:00:36 +000025using namespace llvm;
Chris Lattnerb9740462005-07-01 22:44:09 +000026using namespace x86;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000028Statistic<> llvm::x86::EmittedInsts("asm-printer",
Nate Begeman9a1dc722005-07-01 23:56:38 +000029 "Number of machine instrs printed");
Chris Lattnerd1ab3782004-10-04 07:31:08 +000030
Chris Lattnerb9740462005-07-01 22:44:09 +000031enum AsmWriterFlavorTy { att, intel };
32cl::opt<AsmWriterFlavorTy>
33AsmWriterFlavor("x86-asm-syntax",
Chris Lattner0d2f0432005-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 Lattnerd1ab3782004-10-04 07:31:08 +000040
Chris Lattnerb9740462005-07-01 22:44:09 +000041/// doInitialization
Reid Spencer30226da2005-01-23 03:52:14 +000042bool X86SharedAsmPrinter::doInitialization(Module& M) {
Chris Lattnerb9740462005-07-01 22:44:09 +000043 bool leadingUnderscore = false;
44 forCygwin = false;
Reid Spencer30226da2005-01-23 03:52:14 +000045 const std::string& TT = M.getTargetTriple();
Nate Begeman718387e2005-06-30 00:53:20 +000046 if (TT.length() > 5) {
Misha Brukmanc88330a2005-04-21 23:38:14 +000047 forCygwin = TT.find("cygwin") != std::string::npos ||
Reid Spencer00658b82005-03-08 17:02:05 +000048 TT.find("mingw") != std::string::npos;
Nate Begeman718387e2005-06-30 00:53:20 +000049 forDarwin = TT.find("darwin") != std::string::npos;
50 } else if (TT.empty()) {
Chris Lattner0d2f0432005-07-11 06:25:47 +000051#if defined(__CYGWIN__) || defined(__MINGW32__)
Reid Spencer30226da2005-01-23 03:52:14 +000052 forCygwin = true;
Chris Lattner0d2f0432005-07-11 06:25:47 +000053#elif defined(__APPLE__)
Nate Begeman718387e2005-06-30 00:53:20 +000054 forDarwin = true;
Chris Lattner0d2f0432005-07-11 06:25:47 +000055#elif defined(_WIN32)
Chris Lattnerb9740462005-07-01 22:44:09 +000056 leadingUnderscore = true;
Chris Lattner0d2f0432005-07-11 06:25:47 +000057#else
Chris Lattnerb9740462005-07-01 22:44:09 +000058 leadingUnderscore = false;
Chris Lattner0d2f0432005-07-11 06:25:47 +000059#endif
Reid Spencer30226da2005-01-23 03:52:14 +000060 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000061
Chris Lattnerb9740462005-07-01 22:44:09 +000062 if (leadingUnderscore || forCygwin || forDarwin)
Reid Spencer30226da2005-01-23 03:52:14 +000063 GlobalPrefix = "_";
Chris Lattnerb9740462005-07-01 22:44:09 +000064
Nate Begeman57b9ed52005-07-16 01:59:47 +000065 if (forDarwin) {
Nate Begeman718387e2005-06-30 00:53:20 +000066 AlignmentIsInBytes = false;
Nate Begeman57b9ed52005-07-16 01:59:47 +000067 Data64bitsDirective = 0; // we can't emit a 64-bit unit
68 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
Chris Lattner618981f2005-11-21 06:46:22 +000069 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattner8a5f3c12005-11-21 08:32:23 +000070 ConstantPoolSection = "\t.const\n";
Nate Begeman57b9ed52005-07-16 01:59:47 +000071 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000072
Reid Spencer30226da2005-01-23 03:52:14 +000073 return AsmPrinter::doInitialization(M);
74}
75
Chris Lattner68ab0be2004-10-04 07:24:48 +000076bool X86SharedAsmPrinter::doFinalization(Module &M) {
77 const TargetData &TD = TM.getTargetData();
Chris Lattner68ab0be2004-10-04 07:24:48 +000078
79 // Print out module-level global variables here.
Chris Lattner0d2f0432005-07-11 06:25:47 +000080 for (Module::const_global_iterator I = M.global_begin(),
81 E = M.global_end(); I != E; ++I)
82 if (I->hasInitializer()) { // External global require no code
83 O << "\n\n";
84 std::string name = Mang->getValueName(I);
85 Constant *C = I->getInitializer();
86 unsigned Size = TD.getTypeSize(C->getType());
87 unsigned Align = TD.getTypeAlignmentShift(C->getType());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000088
Chris Lattner0d2f0432005-07-11 06:25:47 +000089 if (C->isNullValue() &&
90 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
91 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner024e32e2005-11-21 07:11:11 +000092 SwitchSection(".data", I);
Chris Lattner0d2f0432005-07-11 06:25:47 +000093 if (!forCygwin && !forDarwin && I->hasInternalLinkage())
94 O << "\t.local " << name << "\n";
95 if (forDarwin && I->hasInternalLinkage())
96 O << "\t.lcomm " << name << "," << Size << "," << Align;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000097 else
Chris Lattner0d2f0432005-07-11 06:25:47 +000098 O << "\t.comm " << name << "," << Size;
99 if (!forCygwin && !forDarwin)
100 O << "," << (1 << Align);
101 O << "\t\t# ";
102 WriteAsOperand(O, I, true, true, &M);
103 O << "\n";
104 } else {
105 switch (I->getLinkage()) {
106 default: assert(0 && "Unknown linkage type!");
107 case GlobalValue::LinkOnceLinkage:
108 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
109 // Nonnull linkonce -> weak
110 O << "\t.weak " << name << "\n";
Chris Lattner0d2f0432005-07-11 06:25:47 +0000111 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
Chris Lattner024e32e2005-11-21 07:11:11 +0000112 SwitchSection("", I);
Chris Lattner0d2f0432005-07-11 06:25:47 +0000113 break;
114 case GlobalValue::AppendingLinkage:
115 // FIXME: appending linkage variables should go into a section of
116 // their name or something. For now, just emit them as external.
117 case GlobalValue::ExternalLinkage:
118 // If external or appending, declare as a global symbol
119 O << "\t.globl " << name << "\n";
120 // FALL THROUGH
121 case GlobalValue::InternalLinkage:
Chris Lattner024e32e2005-11-21 07:11:11 +0000122 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
Chris Lattner0d2f0432005-07-11 06:25:47 +0000123 break;
124 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000125
Chris Lattner99946fb2005-11-21 07:51:23 +0000126 EmitAlignment(Align);
Chris Lattner0d2f0432005-07-11 06:25:47 +0000127 if (!forCygwin && !forDarwin) {
128 O << "\t.type " << name << ",@object\n";
129 O << "\t.size " << name << "," << Size << "\n";
130 }
131 O << name << ":\t\t\t\t# ";
132 WriteAsOperand(O, I, true, true, &M);
133 O << " = ";
134 WriteAsOperand(O, C, false, false, &M);
135 O << "\n";
Chris Lattner99946fb2005-11-21 07:51:23 +0000136 EmitGlobalConstant(C);
Chris Lattner0d2f0432005-07-11 06:25:47 +0000137 }
Chris Lattner68ab0be2004-10-04 07:24:48 +0000138 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000139
Nate Begemanb62a4c82005-07-08 00:23:26 +0000140 if (forDarwin) {
Chris Lattner050bf2f2005-11-21 07:16:34 +0000141 SwitchSection("", 0);
Nate Begeman83b492b2005-07-12 01:37:28 +0000142 // Output stubs for external global variables
143 if (GVStubs.begin() != GVStubs.end())
144 O << "\t.non_lazy_symbol_pointer\n";
145 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
146 i != e; ++i) {
147 O << "L" << *i << "$non_lazy_ptr:\n";
148 O << "\t.indirect_symbol " << *i << "\n";
149 O << "\t.long\t0\n";
150 }
151
Nate Begemanb62a4c82005-07-08 00:23:26 +0000152 // Output stubs for dynamically-linked functions
153 unsigned j = 1;
154 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattner0d2f0432005-07-11 06:25:47 +0000155 i != e; ++i, ++j) {
Nate Begemanb62a4c82005-07-08 00:23:26 +0000156 O << "\t.symbol_stub\n";
157 O << "L" << *i << "$stub:\n";
158 O << "\t.indirect_symbol " << *i << "\n";
159 O << "\tjmp\t*L" << j << "$lz\n";
160 O << "L" << *i << "$stub_binder:\n";
161 O << "\tpushl\t$L" << j << "$lz\n";
162 O << "\tjmp\tdyld_stub_binding_helper\n";
163 O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
164 O << "L" << j << "$lz:\n";
165 O << "\t.indirect_symbol " << *i << "\n";
166 O << "\t.long\tL" << *i << "$stub_binder\n";
167 }
168
169 O << "\n";
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000170
Nate Begemanb62a4c82005-07-08 00:23:26 +0000171 // Output stubs for link-once variables
172 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
173 O << ".data\n.align 2\n";
174 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
175 e = LinkOnceStubs.end(); i != e; ++i) {
176 O << "L" << *i << "$non_lazy_ptr:\n"
177 << "\t.long\t" << *i << '\n';
178 }
179 }
Chris Lattner68ab0be2004-10-04 07:24:48 +0000180
181 AsmPrinter::doFinalization(M);
182 return false; // success
183}
184
Chris Lattner68ab0be2004-10-04 07:24:48 +0000185/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
186/// for a MachineFunction to the given output stream, using the given target
187/// machine description.
188///
189FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
190 switch (AsmWriterFlavor) {
Misha Brukmanc88330a2005-04-21 23:38:14 +0000191 default:
Reid Spencer30226da2005-01-23 03:52:14 +0000192 assert(0 && "Unknown asm flavor!");
Chris Lattner68ab0be2004-10-04 07:24:48 +0000193 case intel:
194 return new X86IntelAsmPrinter(o, tm);
195 case att:
196 return new X86ATTAsmPrinter(o, tm);
197 }
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000198}