blob: 4a54e5914dab8a482129a205569f354da77dda8f [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"
Jim Laskeya0f3d172006-09-07 22:06:40 +000026#include "llvm/Target/TargetAsmInfo.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
Jim Laskey563321a2006-09-06 18:34:40 +000032/// doInitialization
33bool X86SharedAsmPrinter::doInitialization(Module &M) {
Jim Laskeyea348582006-07-27 02:05:13 +000034 if (Subtarget->isTargetDarwin()) {
Evan Chengd5948812006-03-07 02:23:26 +000035 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +000036 DW.BeginModule(&M);
Evan Chengd5948812006-03-07 02:23:26 +000037 }
Evan Cheng3c992d22006-03-07 02:02:57 +000038
Reid Spencer5dc81f62005-01-23 03:52:14 +000039 return AsmPrinter::doInitialization(M);
40}
41
Chris Lattnerac5701c2004-10-04 07:24:48 +000042bool X86SharedAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +000043 // Note: this code is not shared by the Intel printer as it is too different
44 // from how MASM does things. When making changes here don't forget to look
45 // at X86IntelAsmPrinter::doFinalization().
Owen Andersona69571c2006-05-03 01:29:57 +000046 const TargetData *TD = TM.getTargetData();
Chris Lattnerac5701c2004-10-04 07:24:48 +000047
48 // Print out module-level global variables here.
Evan Cheng2338c5c2006-02-07 08:38:37 +000049 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
50 I != E; ++I) {
Chris Lattner9787c642005-11-21 22:48:18 +000051 if (!I->hasInitializer()) continue; // External global require no code
52
Chris Lattnerd1239b72005-12-13 06:32:50 +000053 // Check to see if this is a special global used by LLVM, if so, emit it.
Jim Laskey78098112006-03-07 22:00:35 +000054 if (EmitSpecialLLVMGlobal(I))
Chris Lattnerd1239b72005-12-13 06:32:50 +000055 continue;
Chris Lattnera046e0d2005-12-13 04:53:51 +000056
Chris Lattner9787c642005-11-21 22:48:18 +000057 std::string name = Mang->getValueName(I);
58 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +000059 unsigned Size = TD->getTypeSize(C->getType());
Chris Lattnerff805b52006-02-05 01:45:04 +000060 unsigned Align = getPreferredAlignmentLog(I);
Jeff Cohen00b168892005-07-27 06:12:32 +000061
Evan Cheng2338c5c2006-02-07 08:38:37 +000062 if (C->isNullValue() && /* FIXME: Verify correct */
63 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Evan Cheng17ef92e2006-02-15 01:56:23 +000064 I->hasLinkOnceLinkage() ||
Jim Laskeyea348582006-07-27 02:05:13 +000065 (Subtarget->isTargetDarwin() &&
Evan Cheng932ad512006-05-25 21:59:08 +000066 I->hasExternalLinkage() && !I->hasSection()))) {
Evan Cheng2338c5c2006-02-07 08:38:37 +000067 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Evan Cheng17ef92e2006-02-15 01:56:23 +000068 if (I->hasExternalLinkage()) {
Evan Chenga0ea0532006-02-23 02:43:52 +000069 O << "\t.globl\t" << name << "\n";
Evan Cheng17ef92e2006-02-15 01:56:23 +000070 O << "\t.zerofill __DATA__, __common, " << name << ", "
71 << Size << ", " << Align;
Evan Cheng2338c5c2006-02-07 08:38:37 +000072 } else {
Jim Laskey563321a2006-09-06 18:34:40 +000073 SwitchToDataSection(TAI->getDataSection(), I);
74 if (TAI->getLCOMMDirective() != NULL) {
Evan Cheng17ef92e2006-02-15 01:56:23 +000075 if (I->hasInternalLinkage()) {
Jim Laskey563321a2006-09-06 18:34:40 +000076 O << TAI->getLCOMMDirective() << name << "," << Size;
Jim Laskeyea348582006-07-27 02:05:13 +000077 if (Subtarget->isTargetDarwin())
Jim Laskey563321a2006-09-06 18:34:40 +000078 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng17ef92e2006-02-15 01:56:23 +000079 } else
Jim Laskey563321a2006-09-06 18:34:40 +000080 O << TAI->getCOMMDirective() << name << "," << Size;
Evan Cheng17ef92e2006-02-15 01:56:23 +000081 } else {
Evan Cheng57c07882006-05-26 06:22:34 +000082 if (Subtarget->TargetType != X86Subtarget::isCygwin) {
Evan Cheng932ad512006-05-25 21:59:08 +000083 if (I->hasInternalLinkage())
84 O << "\t.local\t" << name << "\n";
85 }
Jim Laskey563321a2006-09-06 18:34:40 +000086 O << TAI->getCOMMDirective() << name << "," << Size;
87 if (TAI->getCOMMDirectiveTakesAlignment())
88 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng17ef92e2006-02-15 01:56:23 +000089 }
Chris Lattnerb12c9fa2005-07-11 06:25:47 +000090 }
Jim Laskey563321a2006-09-06 18:34:40 +000091 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000092 } else {
93 switch (I->getLinkage()) {
94 case GlobalValue::LinkOnceLinkage:
95 case GlobalValue::WeakLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +000096 if (Subtarget->isTargetDarwin()) {
Evan Cheng5ada3702006-02-07 23:32:58 +000097 O << "\t.globl " << name << "\n"
98 << "\t.weak_definition " << name << "\n";
Evan Chengf8614db2006-06-04 07:24:07 +000099 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
Evan Cheng932ad512006-05-25 21:59:08 +0000100 } else if (Subtarget->TargetType == X86Subtarget::isCygwin) {
101 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\"\n"
102 << "\t.weak " << name << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000103 } else {
Evan Cheng932ad512006-05-25 21:59:08 +0000104 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"
105 << "\t.weak " << name << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000106 }
107 break;
108 case GlobalValue::AppendingLinkage:
109 // FIXME: appending linkage variables should go into a section of
110 // their name or something. For now, just emit them as external.
111 case GlobalValue::ExternalLinkage:
112 // If external or appending, declare as a global symbol
113 O << "\t.globl " << name << "\n";
114 // FALL THROUGH
115 case GlobalValue::InternalLinkage:
Jim Laskey563321a2006-09-06 18:34:40 +0000116 SwitchToDataSection(TAI->getDataSection(), I);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000117 break;
118 default:
119 assert(0 && "Unknown linkage type!");
120 }
Chris Lattner9787c642005-11-21 22:48:18 +0000121
Evan Cheng2338c5c2006-02-07 08:38:37 +0000122 EmitAlignment(Align, I);
Jim Laskey563321a2006-09-06 18:34:40 +0000123 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
Evan Cheng5ada3702006-02-07 23:32:58 +0000124 << "\n";
Jim Laskey563321a2006-09-06 18:34:40 +0000125 if (TAI->hasDotTypeDotSizeDirective())
Evan Cheng5ada3702006-02-07 23:32:58 +0000126 O << "\t.size " << name << ", " << Size << "\n";
127
Evan Cheng2338c5c2006-02-07 08:38:37 +0000128 EmitGlobalConstant(C);
129 O << '\n';
Chris Lattner9787c642005-11-21 22:48:18 +0000130 }
Chris Lattnera35a8e82005-11-21 22:39:40 +0000131 }
132
Jim Laskeyea348582006-07-27 02:05:13 +0000133 if (Subtarget->isTargetDarwin()) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000134 SwitchToDataSection("", 0);
Nate Begeman73213f62005-07-12 01:37:28 +0000135
Nate Begeman72b286b2005-07-08 00:23:26 +0000136 // Output stubs for dynamically-linked functions
137 unsigned j = 1;
138 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000139 i != e; ++i, ++j) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000140 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
141 "self_modifying_code+pure_instructions,5", 0);
Nate Begeman72b286b2005-07-08 00:23:26 +0000142 O << "L" << *i << "$stub:\n";
143 O << "\t.indirect_symbol " << *i << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000144 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000145 }
146
147 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000148
Evan Cheng2338c5c2006-02-07 08:38:37 +0000149 // Output stubs for external and common global variables.
150 if (GVStubs.begin() != GVStubs.end())
Chris Lattner4632d7a2006-05-09 04:59:56 +0000151 SwitchToDataSection(
152 ".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000153 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
154 i != e; ++i) {
155 O << "L" << *i << "$non_lazy_ptr:\n";
156 O << "\t.indirect_symbol " << *i << "\n";
157 O << "\t.long\t0\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000158 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000159
Evan Chengd5948812006-03-07 02:23:26 +0000160 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000161 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000162
163 // Funny Darwin hack: This flag tells the linker that no global symbols
164 // contain code that falls through to other global symbols (e.g. the obvious
165 // implementation of multiple entry points). If this doesn't occur, the
Jim Laskey99db0442006-03-23 18:09:44 +0000166 // linker can safely perform dead code stripping. Since LLVM never
167 // generates code that does this, it is always safe to set.
Evan Chengd5948812006-03-07 02:23:26 +0000168 O << "\t.subsections_via_symbols\n";
169 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000170
Chris Lattnerac5701c2004-10-04 07:24:48 +0000171 AsmPrinter::doFinalization(M);
172 return false; // success
173}
174
Chris Lattnerac5701c2004-10-04 07:24:48 +0000175/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
176/// for a MachineFunction to the given output stream, using the given target
177/// machine description.
178///
Evan Chengc4c62572006-03-13 23:20:37 +0000179FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
Jim Laskey563321a2006-09-06 18:34:40 +0000180 X86TargetMachine &tm) {
Jim Laskey05a059d2006-09-07 12:23:47 +0000181 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
Jim Laskey563321a2006-09-06 18:34:40 +0000182
Jim Laskey05a059d2006-09-07 12:23:47 +0000183 if (Subtarget->isFlavorIntel()) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000184 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
Jim Laskey05a059d2006-09-07 12:23:47 +0000185 } else {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000186 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());
Chris Lattnerac5701c2004-10-04 07:24:48 +0000187 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000188}