blob: 990951ea7b8126fdb3dcffcd1cbe2319a428d226 [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
Evan Chengc4c62572006-03-13 23:20:37 +000017#include "X86AsmPrinter.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000018#include "X86ATTAsmPrinter.h"
19#include "X86IntelAsmPrinter.h"
Chris Lattnera35a8e82005-11-21 22:39:40 +000020#include "X86Subtarget.h"
Chris Lattnera046e0d2005-12-13 04:53:51 +000021#include "llvm/Constants.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000022#include "llvm/Module.h"
Chris Lattnerc41cc832005-11-21 06:46:22 +000023#include "llvm/Type.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000024#include "llvm/Assembly/Writer.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000025#include "llvm/Support/Mangler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Evan Chengc4c62572006-03-13 23:20:37 +000029Statistic<> llvm::EmittedInsts("asm-printer",
30 "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 Lattnere650a6b2006-02-23 05:25:02 +000047 PrivateGlobalPrefix = ".L";
Chris Lattnera35a8e82005-11-21 22:39:40 +000048
49 switch (Subtarget->TargetType) {
Chris Lattnera35a8e82005-11-21 22:39:40 +000050 case X86Subtarget::isDarwin:
Nate Begeman9d19eb42005-06-30 00:53:20 +000051 AlignmentIsInBytes = false;
Chris Lattner8fccc972005-11-21 19:50:31 +000052 GlobalPrefix = "_";
Nate Begeman9035b992005-07-16 01:59:47 +000053 Data64bitsDirective = 0; // we can't emit a 64-bit unit
54 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
Chris Lattnerc41cc832005-11-21 06:46:22 +000055 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Chris Lattnerd939f6c2005-11-21 08:32:23 +000056 ConstantPoolSection = "\t.const\n";
Chris Lattner8fccc972005-11-21 19:50:31 +000057 LCOMMDirective = "\t.lcomm\t";
58 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000059 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000060 forDarwin = true;
Chris Lattnerd1239b72005-12-13 06:32:50 +000061 StaticCtorsSection = ".mod_init_func";
62 StaticDtorsSection = ".mod_term_func";
Chris Lattner418caa12006-02-08 23:42:22 +000063 InlineAsmStart = InlineAsmEnd = ""; // Don't use #APP/#NO_APP
Chris Lattnera35a8e82005-11-21 22:39:40 +000064 break;
65 case X86Subtarget::isCygwin:
Chris Lattner8fccc972005-11-21 19:50:31 +000066 GlobalPrefix = "_";
67 COMMDirectiveTakesAlignment = false;
Chris Lattnerac2902b2005-11-21 23:06:54 +000068 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000069 break;
70 case X86Subtarget::isWindows:
Chris Lattner8fccc972005-11-21 19:50:31 +000071 GlobalPrefix = "_";
Chris Lattnerac2902b2005-11-21 23:06:54 +000072 HasDotTypeDotSizeDirective = false;
Chris Lattnera35a8e82005-11-21 22:39:40 +000073 break;
74 default: break;
75 }
Chris Lattner8fccc972005-11-21 19:50:31 +000076
Evan Chengd5948812006-03-07 02:23:26 +000077 if (forDarwin) {
78 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +000079 DW.BeginModule(&M);
Evan Chengd5948812006-03-07 02:23:26 +000080 }
Evan Cheng3c992d22006-03-07 02:02:57 +000081
Reid Spencer5dc81f62005-01-23 03:52:14 +000082 return AsmPrinter::doInitialization(M);
83}
84
Chris Lattnerac5701c2004-10-04 07:24:48 +000085bool X86SharedAsmPrinter::doFinalization(Module &M) {
86 const TargetData &TD = TM.getTargetData();
Chris Lattnerac5701c2004-10-04 07:24:48 +000087
88 // Print out module-level global variables here.
Evan Cheng2338c5c2006-02-07 08:38:37 +000089 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
90 I != E; ++I) {
Chris Lattner9787c642005-11-21 22:48:18 +000091 if (!I->hasInitializer()) continue; // External global require no code
92
Chris Lattnerd1239b72005-12-13 06:32:50 +000093 // Check to see if this is a special global used by LLVM, if so, emit it.
Jim Laskey78098112006-03-07 22:00:35 +000094 if (EmitSpecialLLVMGlobal(I))
Chris Lattnerd1239b72005-12-13 06:32:50 +000095 continue;
Chris Lattnera046e0d2005-12-13 04:53:51 +000096
Chris Lattner9787c642005-11-21 22:48:18 +000097 std::string name = Mang->getValueName(I);
98 Constant *C = I->getInitializer();
99 unsigned Size = TD.getTypeSize(C->getType());
Chris Lattnerff805b52006-02-05 01:45:04 +0000100 unsigned Align = getPreferredAlignmentLog(I);
Jeff Cohen00b168892005-07-27 06:12:32 +0000101
Evan Cheng2338c5c2006-02-07 08:38:37 +0000102 if (C->isNullValue() && /* FIXME: Verify correct */
103 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Evan Cheng17ef92e2006-02-15 01:56:23 +0000104 I->hasLinkOnceLinkage() ||
105 (forDarwin && I->hasExternalLinkage() && !I->hasSection()))) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000106 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Evan Cheng17ef92e2006-02-15 01:56:23 +0000107 if (I->hasExternalLinkage()) {
Evan Chenga0ea0532006-02-23 02:43:52 +0000108 O << "\t.globl\t" << name << "\n";
Evan Cheng17ef92e2006-02-15 01:56:23 +0000109 O << "\t.zerofill __DATA__, __common, " << name << ", "
110 << Size << ", " << Align;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000111 } else {
Evan Cheng17ef92e2006-02-15 01:56:23 +0000112 SwitchSection(".data", I);
113 if (LCOMMDirective != NULL) {
114 if (I->hasInternalLinkage()) {
115 O << LCOMMDirective << name << "," << Size;
116 if (forDarwin)
117 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
118 } else
119 O << COMMDirective << name << "," << Size;
120 } else {
121 if (I->hasInternalLinkage())
122 O << "\t.local\t" << name << "\n";
123 O << COMMDirective << name << "," << Size;
124 if (COMMDirectiveTakesAlignment)
125 O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
126 }
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000127 }
Evan Chenga0ea0532006-02-23 02:43:52 +0000128 O << "\t\t" << CommentString << " " << I->getName() << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000129 } else {
130 switch (I->getLinkage()) {
131 case GlobalValue::LinkOnceLinkage:
132 case GlobalValue::WeakLinkage:
133 if (forDarwin) {
Evan Cheng5ada3702006-02-07 23:32:58 +0000134 O << "\t.globl " << name << "\n"
135 << "\t.weak_definition " << name << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000136 SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
137 } else {
138 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
139 O << "\t.weak " << name << "\n";
140 }
141 break;
142 case GlobalValue::AppendingLinkage:
143 // FIXME: appending linkage variables should go into a section of
144 // their name or something. For now, just emit them as external.
145 case GlobalValue::ExternalLinkage:
146 // If external or appending, declare as a global symbol
147 O << "\t.globl " << name << "\n";
148 // FALL THROUGH
149 case GlobalValue::InternalLinkage:
150 SwitchSection(".data", I);
151 break;
152 default:
153 assert(0 && "Unknown linkage type!");
154 }
Chris Lattner9787c642005-11-21 22:48:18 +0000155
Evan Cheng2338c5c2006-02-07 08:38:37 +0000156 EmitAlignment(Align, I);
Evan Cheng5ada3702006-02-07 23:32:58 +0000157 O << name << ":\t\t\t\t" << CommentString << " " << I->getName()
158 << "\n";
159 if (HasDotTypeDotSizeDirective)
160 O << "\t.size " << name << ", " << Size << "\n";
161
Evan Cheng2338c5c2006-02-07 08:38:37 +0000162 EmitGlobalConstant(C);
163 O << '\n';
Chris Lattner9787c642005-11-21 22:48:18 +0000164 }
Chris Lattnera35a8e82005-11-21 22:39:40 +0000165 }
166
Nate Begeman72b286b2005-07-08 00:23:26 +0000167 if (forDarwin) {
Chris Lattner7b6e53c2005-11-21 07:16:34 +0000168 SwitchSection("", 0);
Nate Begeman73213f62005-07-12 01:37:28 +0000169
Nate Begeman72b286b2005-07-08 00:23:26 +0000170 // Output stubs for dynamically-linked functions
171 unsigned j = 1;
172 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000173 i != e; ++i, ++j) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000174 SwitchSection(".section __IMPORT,__jump_table,symbol_stubs,"
175 "self_modifying_code+pure_instructions,5", 0);
Nate Begeman72b286b2005-07-08 00:23:26 +0000176 O << "L" << *i << "$stub:\n";
177 O << "\t.indirect_symbol " << *i << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000178 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000179 }
180
181 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000182
Evan Cheng2338c5c2006-02-07 08:38:37 +0000183 // Output stubs for external and common global variables.
184 if (GVStubs.begin() != GVStubs.end())
185 SwitchSection(".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
186 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
187 i != e; ++i) {
188 O << "L" << *i << "$non_lazy_ptr:\n";
189 O << "\t.indirect_symbol " << *i << "\n";
190 O << "\t.long\t0\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000191 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000192
Evan Chengd5948812006-03-07 02:23:26 +0000193 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000194 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000195
196 // Funny Darwin hack: This flag tells the linker that no global symbols
197 // contain code that falls through to other global symbols (e.g. the obvious
198 // implementation of multiple entry points). If this doesn't occur, the
Jim Laskey99db0442006-03-23 18:09:44 +0000199 // linker can safely perform dead code stripping. Since LLVM never
200 // generates code that does this, it is always safe to set.
Evan Chengd5948812006-03-07 02:23:26 +0000201 O << "\t.subsections_via_symbols\n";
202 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000203
Chris Lattnerac5701c2004-10-04 07:24:48 +0000204 AsmPrinter::doFinalization(M);
205 return false; // success
206}
207
Chris Lattnerac5701c2004-10-04 07:24:48 +0000208/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
209/// for a MachineFunction to the given output stream, using the given target
210/// machine description.
211///
Evan Chengc4c62572006-03-13 23:20:37 +0000212FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
213 X86TargetMachine &tm){
Chris Lattnerac5701c2004-10-04 07:24:48 +0000214 switch (AsmWriterFlavor) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000215 default:
Reid Spencer5dc81f62005-01-23 03:52:14 +0000216 assert(0 && "Unknown asm flavor!");
Chris Lattnerac5701c2004-10-04 07:24:48 +0000217 case intel:
218 return new X86IntelAsmPrinter(o, tm);
219 case att:
220 return new X86ATTAsmPrinter(o, tm);
221 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000222}