blob: 85cc3c28bdfae834f307181a85ffd0bc24eb04e5 [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
Chris Lattner8fccc972005-11-21 19:50:31 +000046 forDarwin = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000047
48 switch (Subtarget->TargetType) {
Chris Lattnera35a8e82005-11-21 22:39:40 +000049 case X86Subtarget::isDarwin:
Nate Begeman9d19eb42005-06-30 00:53:20 +000050 AlignmentIsInBytes = false;
Chris Lattner8fccc972005-11-21 19:50:31 +000051 GlobalPrefix = "_";
Nate Begeman9035b992005-07-16 01:59:47 +000052 Data64bitsDirective = 0; // we can't emit a 64-bit unit
53 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
Chris Lattnerc41cc832005-11-21 06:46:22 +000054 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattnerd939f6c2005-11-21 08:32:23 +000055 ConstantPoolSection = "\t.const\n";
Chris Lattner8fccc972005-11-21 19:50:31 +000056 LCOMMDirective = "\t.lcomm\t";
57 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000058 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000059 forDarwin = true;
60 break;
61 case X86Subtarget::isCygwin:
Chris Lattner8fccc972005-11-21 19:50:31 +000062 GlobalPrefix = "_";
63 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000064 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000065 break;
66 case X86Subtarget::isWindows:
Chris Lattner8fccc972005-11-21 19:50:31 +000067 GlobalPrefix = "_";
Chris Lattnerac2902b2005-11-21 23:06:54 +000068 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000069 break;
70 default: break;
71 }
Chris Lattner8fccc972005-11-21 19:50:31 +000072
Reid Spencer5dc81f62005-01-23 03:52:14 +000073 return AsmPrinter::doInitialization(M);
74}
75
Chris Lattnerac5701c2004-10-04 07:24:48 +000076bool X86SharedAsmPrinter::doFinalization(Module &M) {
77 const TargetData &TD = TM.getTargetData();
Chris Lattnerac5701c2004-10-04 07:24:48 +000078
79 // Print out module-level global variables here.
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000080 for (Module::const_global_iterator I = M.global_begin(),
Chris Lattnera35a8e82005-11-21 22:39:40 +000081 E = M.global_end(); I != E; ++I) {
Chris Lattner9787c642005-11-21 22:48:18 +000082 if (!I->hasInitializer()) continue; // External global require no code
83
84 O << "\n\n";
85 std::string name = Mang->getValueName(I);
86 Constant *C = I->getInitializer();
87 unsigned Size = TD.getTypeSize(C->getType());
88 unsigned Align = TD.getTypeAlignmentShift(C->getType());
Jeff Cohen00b168892005-07-27 06:12:32 +000089
Chris Lattner9787c642005-11-21 22:48:18 +000090 switch (I->getLinkage()) {
91 default: assert(0 && "Unknown linkage type!");
92 case GlobalValue::LinkOnceLinkage:
93 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
94 if (C->isNullValue()) {
95 O << COMMDirective << name << "," << Size;
96 if (COMMDirectiveTakesAlignment)
97 O << "," << (1 << Align);
Chris Lattnerac2902b2005-11-21 23:06:54 +000098 O << "\t\t" << CommentString << " " << I->getName() << "\n";
Chris Lattner9787c642005-11-21 22:48:18 +000099 continue;
100 }
101
102 // Nonnull linkonce -> weak
103 O << "\t.weak " << name << "\n";
104 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
105 SwitchSection("", I);
106 break;
107 case GlobalValue::InternalLinkage:
108 if (C->isNullValue()) {
109 if (LCOMMDirective) {
110 O << LCOMMDirective << name << "," << Size << "," << Align;
111 continue;
112 } else {
113 SwitchSection(".bss", I);
114 O << "\t.local " << name << "\n";
Chris Lattner8fccc972005-11-21 19:50:31 +0000115 O << COMMDirective << name << "," << Size;
116 if (COMMDirectiveTakesAlignment)
117 O << "," << (1 << Align);
118 O << "\t\t# ";
119 WriteAsOperand(O, I, true, true, &M);
120 O << "\n";
121 continue;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000122 }
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000123 }
Chris Lattner9787c642005-11-21 22:48:18 +0000124 SwitchSection(".data", I);
125 break;
126 case GlobalValue::AppendingLinkage:
127 // FIXME: appending linkage variables should go into a section of
128 // their name or something. For now, just emit them as external.
129 case GlobalValue::ExternalLinkage:
130 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
131 // If external or appending, declare as a global symbol
132 O << "\t.globl " << name << "\n";
133 break;
Chris Lattnerac5701c2004-10-04 07:24:48 +0000134 }
Chris Lattner9787c642005-11-21 22:48:18 +0000135
136 EmitAlignment(Align);
Chris Lattnerac2902b2005-11-21 23:06:54 +0000137 if (HasDotTypeDotSizeDirective) {
Chris Lattner9787c642005-11-21 22:48:18 +0000138 O << "\t.type " << name << ",@object\n";
139 O << "\t.size " << name << "," << Size << "\n";
140 }
Chris Lattnerac2902b2005-11-21 23:06:54 +0000141 O << name << ":\t\t\t" << CommentString << ' ' << I->getName() << '\n';
Chris Lattner9787c642005-11-21 22:48:18 +0000142 EmitGlobalConstant(C);
Chris Lattnera35a8e82005-11-21 22:39:40 +0000143 }
144
Nate Begeman72b286b2005-07-08 00:23:26 +0000145 if (forDarwin) {
Chris Lattner7b6e53c2005-11-21 07:16:34 +0000146 SwitchSection("", 0);
Nate Begeman73213f62005-07-12 01:37:28 +0000147 // Output stubs for external global variables
148 if (GVStubs.begin() != GVStubs.end())
149 O << "\t.non_lazy_symbol_pointer\n";
150 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
151 i != e; ++i) {
152 O << "L" << *i << "$non_lazy_ptr:\n";
153 O << "\t.indirect_symbol " << *i << "\n";
154 O << "\t.long\t0\n";
155 }
156
Nate Begeman72b286b2005-07-08 00:23:26 +0000157 // Output stubs for dynamically-linked functions
158 unsigned j = 1;
159 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000160 i != e; ++i, ++j) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000161 O << "\t.symbol_stub\n";
162 O << "L" << *i << "$stub:\n";
163 O << "\t.indirect_symbol " << *i << "\n";
164 O << "\tjmp\t*L" << j << "$lz\n";
165 O << "L" << *i << "$stub_binder:\n";
166 O << "\tpushl\t$L" << j << "$lz\n";
167 O << "\tjmp\tdyld_stub_binding_helper\n";
168 O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
169 O << "L" << j << "$lz:\n";
170 O << "\t.indirect_symbol " << *i << "\n";
171 O << "\t.long\tL" << *i << "$stub_binder\n";
172 }
173
174 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000175
Nate Begeman72b286b2005-07-08 00:23:26 +0000176 // Output stubs for link-once variables
177 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
178 O << ".data\n.align 2\n";
179 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
180 e = LinkOnceStubs.end(); i != e; ++i) {
181 O << "L" << *i << "$non_lazy_ptr:\n"
182 << "\t.long\t" << *i << '\n';
183 }
184 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000185
186 AsmPrinter::doFinalization(M);
187 return false; // success
188}
189
Chris Lattnerac5701c2004-10-04 07:24:48 +0000190/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
191/// for a MachineFunction to the given output stream, using the given target
192/// machine description.
193///
194FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
195 switch (AsmWriterFlavor) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000196 default:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000197 assert(0 && "Unknown asm flavor!");
Chris Lattnerac5701c2004-10-04 07:24:48 +0000198 case intel:
199 return new X86IntelAsmPrinter(o, tm);
200 case att:
201 return new X86ATTAsmPrinter(o, tm);
202 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000203}