blob: 561a0b106da58855e08e79711666496c4b49281a [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 Lattnera35a8e82005-11-21 22:39:40 +000019#include "X86Subtarget.h"
Chris Lattner72614082002-10-25 22:55:53 +000020#include "X86.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000021#include "llvm/Module.h"
Chris Lattnerc41cc832005-11-21 06:46:22 +000022#include "llvm/Type.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000023#include "llvm/Assembly/Writer.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
Chris Lattner5df14ca2005-11-21 22:19:48 +000043bool X86SharedAsmPrinter::doInitialization(Module &M) {
Chris Lattnera35a8e82005-11-21 22:39:40 +000044 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
45
46 forELF = false;
Chris Lattner8fccc972005-11-21 19:50:31 +000047 forDarwin = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000048
49 switch (Subtarget->TargetType) {
50 case X86Subtarget::isELF:
Chris Lattner5df14ca2005-11-21 22:19:48 +000051 forELF = true;
Chris Lattnera35a8e82005-11-21 22:39:40 +000052 break;
53 case X86Subtarget::isDarwin:
Nate Begeman9d19eb42005-06-30 00:53:20 +000054 AlignmentIsInBytes = false;
Chris Lattner8fccc972005-11-21 19:50:31 +000055 GlobalPrefix = "_";
Nate Begeman9035b992005-07-16 01:59:47 +000056 Data64bitsDirective = 0; // we can't emit a 64-bit unit
57 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
Chris Lattnerc41cc832005-11-21 06:46:22 +000058 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattnerd939f6c2005-11-21 08:32:23 +000059 ConstantPoolSection = "\t.const\n";
Chris Lattner8fccc972005-11-21 19:50:31 +000060 LCOMMDirective = "\t.lcomm\t";
61 COMMDirectiveTakesAlignment = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000062 forDarwin = true;
63 break;
64 case X86Subtarget::isCygwin:
Chris Lattner8fccc972005-11-21 19:50:31 +000065 GlobalPrefix = "_";
66 COMMDirectiveTakesAlignment = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000067 break;
68 case X86Subtarget::isWindows:
Chris Lattner8fccc972005-11-21 19:50:31 +000069 GlobalPrefix = "_";
Chris Lattnera35a8e82005-11-21 22:39:40 +000070 break;
71 default: break;
72 }
Chris Lattner8fccc972005-11-21 19:50:31 +000073
Reid Spencer5dc81f62005-01-23 03:52:14 +000074 return AsmPrinter::doInitialization(M);
75}
76
Chris Lattnerac5701c2004-10-04 07:24:48 +000077bool X86SharedAsmPrinter::doFinalization(Module &M) {
78 const TargetData &TD = TM.getTargetData();
Chris Lattnerac5701c2004-10-04 07:24:48 +000079
80 // Print out module-level global variables here.
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000081 for (Module::const_global_iterator I = M.global_begin(),
Chris Lattnera35a8e82005-11-21 22:39:40 +000082 E = M.global_end(); I != E; ++I) {
Chris Lattner9787c642005-11-21 22:48:18 +000083 if (!I->hasInitializer()) continue; // External global require no code
84
85 O << "\n\n";
86 std::string name = Mang->getValueName(I);
87 Constant *C = I->getInitializer();
88 unsigned Size = TD.getTypeSize(C->getType());
89 unsigned Align = TD.getTypeAlignmentShift(C->getType());
Jeff Cohen00b168892005-07-27 06:12:32 +000090
Chris Lattner9787c642005-11-21 22:48:18 +000091 switch (I->getLinkage()) {
92 default: assert(0 && "Unknown linkage type!");
93 case GlobalValue::LinkOnceLinkage:
94 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
95 if (C->isNullValue()) {
96 O << COMMDirective << name << "," << Size;
97 if (COMMDirectiveTakesAlignment)
98 O << "," << (1 << Align);
99 O << "\t\t# ";
100 WriteAsOperand(O, I, true, true, &M);
101 O << "\n";
102 continue;
103 }
104
105 // Nonnull linkonce -> weak
106 O << "\t.weak " << name << "\n";
107 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
108 SwitchSection("", I);
109 break;
110 case GlobalValue::InternalLinkage:
111 if (C->isNullValue()) {
112 if (LCOMMDirective) {
113 O << LCOMMDirective << name << "," << Size << "," << Align;
114 continue;
115 } else {
116 SwitchSection(".bss", I);
117 O << "\t.local " << name << "\n";
Chris Lattner8fccc972005-11-21 19:50:31 +0000118 O << COMMDirective << name << "," << Size;
119 if (COMMDirectiveTakesAlignment)
120 O << "," << (1 << Align);
121 O << "\t\t# ";
122 WriteAsOperand(O, I, true, true, &M);
123 O << "\n";
124 continue;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000125 }
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000126 }
Chris Lattner9787c642005-11-21 22:48:18 +0000127 SwitchSection(".data", I);
128 break;
129 case GlobalValue::AppendingLinkage:
130 // FIXME: appending linkage variables should go into a section of
131 // their name or something. For now, just emit them as external.
132 case GlobalValue::ExternalLinkage:
133 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
134 // If external or appending, declare as a global symbol
135 O << "\t.globl " << name << "\n";
136 break;
Chris Lattnerac5701c2004-10-04 07:24:48 +0000137 }
Chris Lattner9787c642005-11-21 22:48:18 +0000138
139 EmitAlignment(Align);
140 if (forELF) {
141 O << "\t.type " << name << ",@object\n";
142 O << "\t.size " << name << "," << Size << "\n";
143 }
144 O << name << ":\t\t\t\t# ";
145 WriteAsOperand(O, I, true, true, &M);
146 O << " = ";
147 WriteAsOperand(O, C, false, false, &M);
148 O << "\n";
149 EmitGlobalConstant(C);
Chris Lattnera35a8e82005-11-21 22:39:40 +0000150 }
151
Nate Begeman72b286b2005-07-08 00:23:26 +0000152 if (forDarwin) {
Chris Lattner7b6e53c2005-11-21 07:16:34 +0000153 SwitchSection("", 0);
Nate Begeman73213f62005-07-12 01:37:28 +0000154 // Output stubs for external global variables
155 if (GVStubs.begin() != GVStubs.end())
156 O << "\t.non_lazy_symbol_pointer\n";
157 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
158 i != e; ++i) {
159 O << "L" << *i << "$non_lazy_ptr:\n";
160 O << "\t.indirect_symbol " << *i << "\n";
161 O << "\t.long\t0\n";
162 }
163
Nate Begeman72b286b2005-07-08 00:23:26 +0000164 // Output stubs for dynamically-linked functions
165 unsigned j = 1;
166 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000167 i != e; ++i, ++j) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000168 O << "\t.symbol_stub\n";
169 O << "L" << *i << "$stub:\n";
170 O << "\t.indirect_symbol " << *i << "\n";
171 O << "\tjmp\t*L" << j << "$lz\n";
172 O << "L" << *i << "$stub_binder:\n";
173 O << "\tpushl\t$L" << j << "$lz\n";
174 O << "\tjmp\tdyld_stub_binding_helper\n";
175 O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
176 O << "L" << j << "$lz:\n";
177 O << "\t.indirect_symbol " << *i << "\n";
178 O << "\t.long\tL" << *i << "$stub_binder\n";
179 }
180
181 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000182
Nate Begeman72b286b2005-07-08 00:23:26 +0000183 // Output stubs for link-once variables
184 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
185 O << ".data\n.align 2\n";
186 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
187 e = LinkOnceStubs.end(); i != e; ++i) {
188 O << "L" << *i << "$non_lazy_ptr:\n"
189 << "\t.long\t" << *i << '\n';
190 }
191 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000192
193 AsmPrinter::doFinalization(M);
194 return false; // success
195}
196
Chris Lattnerac5701c2004-10-04 07:24:48 +0000197/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
198/// for a MachineFunction to the given output stream, using the given target
199/// machine description.
200///
201FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
202 switch (AsmWriterFlavor) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000203 default:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000204 assert(0 && "Unknown asm flavor!");
Chris Lattnerac5701c2004-10-04 07:24:48 +0000205 case intel:
206 return new X86IntelAsmPrinter(o, tm);
207 case att:
208 return new X86ATTAsmPrinter(o, tm);
209 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000210}