blob: 6e9d935e0c009b2222d89480a08123af30fd469d [file] [log] [blame]
Chris Lattner6a8e0f52004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner6a8e0f52004-08-16 23:15:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng993e9cf2006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng38d5e762006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman41b1cdc2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000018#include "llvm/Module.h"
Gordon Henriksend930f912008-08-17 18:44:35 +000019#include "llvm/CodeGen/GCMetadataPrinter.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000021#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000023#include "llvm/Support/Mangler.h"
Owen Anderson93719642008-08-21 00:14:44 +000024#include "llvm/Support/raw_ostream.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000025#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000026#include "llvm/Target/TargetData.h"
Chris Lattner90438232006-10-06 22:50:56 +000027#include "llvm/Target/TargetLowering.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000028#include "llvm/Target/TargetMachine.h"
Evan Chengc963f6c2008-07-01 23:18:29 +000029#include "llvm/Target/TargetOptions.h"
Evan Cheng0e7b00d2008-03-15 00:03:38 +000030#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng797d56f2007-11-09 01:32:10 +000031#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner17f71652008-08-17 07:19:36 +000032#include "llvm/ADT/SmallString.h"
Owen Anderson93719642008-08-21 00:14:44 +000033#include "llvm/ADT/StringExtras.h"
Chris Lattneraa23fa92006-02-01 22:41:11 +000034#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000035using namespace llvm;
36
Devang Patel8c78a0b2007-05-03 01:11:54 +000037char AsmPrinter::ID = 0;
Owen Anderson93719642008-08-21 00:14:44 +000038AsmPrinter::AsmPrinter(raw_ostream &o, TargetMachine &tm,
Jim Laskey261779b2006-09-07 22:06:40 +000039 const TargetAsmInfo *T)
Dan Gohmana79db302008-09-04 17:05:41 +000040 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Evan Cheng0e7b00d2008-03-15 00:03:38 +000041 TM(tm), TAI(T), TRI(tm.getRegisterInfo()),
Evan Chengc7990652008-02-28 00:43:03 +000042 IsInTextSection(false)
Jim Laskeya6211dc2006-09-06 18:34:40 +000043{}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000044
Gordon Henriksendbe06d32008-08-17 12:08:44 +000045AsmPrinter::~AsmPrinter() {
46 for (gcp_iterator I = GCMetadataPrinters.begin(),
47 E = GCMetadataPrinters.end(); I != E; ++I)
48 delete I->second;
49}
50
Chris Lattnerdc822412006-10-05 02:42:47 +000051std::string AsmPrinter::getSectionForFunction(const Function &F) const {
52 return TAI->getTextSection();
53}
54
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000055
Chris Lattner8488ba22006-05-09 04:59:56 +000056/// SwitchToTextSection - Switch to the specified text section of the executable
57/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000058///
Chris Lattner8488ba22006-05-09 04:59:56 +000059void AsmPrinter::SwitchToTextSection(const char *NewSection,
60 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000061 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000062 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000063 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000064 else
65 NS = NewSection;
66
67 // If we're already in this section, we're done.
68 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000069
Chris Lattner4ebc6a22006-05-09 05:33:48 +000070 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000071 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman6896901e2008-06-30 22:03:41 +000072 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Jeff Cohen470f4312006-05-02 03:58:45 +000073
Chris Lattner4ebc6a22006-05-09 05:33:48 +000074 CurrentSection = NS;
75
76 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000077 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Evan Chengc7990652008-02-28 00:43:03 +000078
79 IsInTextSection = true;
Chris Lattner2ea5c992005-11-21 07:06:27 +000080}
81
Nate Begeman78756502006-07-27 01:13:04 +000082/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000083/// if we are not already in it!
84///
85void AsmPrinter::SwitchToDataSection(const char *NewSection,
86 const GlobalValue *GV) {
87 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000088 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000089 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner6341df82006-05-09 05:23:12 +000090 else
91 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +000092
Chris Lattner6341df82006-05-09 05:23:12 +000093 // If we're already in this section, we're done.
94 if (CurrentSection == NS) return;
95
Chris Lattner4ebc6a22006-05-09 05:33:48 +000096 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000097 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman6896901e2008-06-30 22:03:41 +000098 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Chris Lattner4ebc6a22006-05-09 05:33:48 +000099
100 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +0000101
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000102 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000103 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Evan Chengc7990652008-02-28 00:43:03 +0000104
105 IsInTextSection = false;
Chris Lattner8488ba22006-05-09 04:59:56 +0000106}
107
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +0000108/// SwitchToSection - Switch to the specified section of the executable if we
109/// are not already in it!
110void AsmPrinter::SwitchToSection(const Section* NS) {
111 const std::string& NewSection = NS->getName();
112
113 // If we're already in this section, we're done.
114 if (CurrentSection == NewSection) return;
115
116 // Close the current section, if applicable.
117 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
118 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
119
120 // FIXME: Make CurrentSection a Section* in the future
121 CurrentSection = NewSection;
122
123 if (!CurrentSection.empty())
124 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
125
126 IsInTextSection = (NS->getFlags() & SectionFlags::Code);
127}
Chris Lattner8488ba22006-05-09 04:59:56 +0000128
Gordon Henriksen5180e852008-01-07 01:30:38 +0000129void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
130 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksend930f912008-08-17 18:44:35 +0000131 AU.addRequired<GCModuleInfo>();
Gordon Henriksen5180e852008-01-07 01:30:38 +0000132}
133
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000134bool AsmPrinter::doInitialization(Module &M) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000135 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +0000136
Gordon Henriksend930f912008-08-17 18:44:35 +0000137 GCModuleInfo *MI = getAnalysisToUpdate<GCModuleInfo>();
138 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
139 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
140 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
141 MP->beginAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000142
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000143 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000144 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000145 << M.getModuleInlineAsm()
Dan Gohman6896901e2008-06-30 22:03:41 +0000146 << '\n' << TAI->getCommentString()
Jim Laskeya6211dc2006-09-06 18:34:40 +0000147 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000148
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000149 SwitchToDataSection(""); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000150
Evan Cheng2cb90682008-02-04 23:06:48 +0000151 MMI = getAnalysisToUpdate<MachineModuleInfo>();
152 if (MMI) MMI->AnalyzeModule(M);
Jim Laskey0bbdc552006-01-26 20:21:46 +0000153
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000154 return false;
155}
156
157bool AsmPrinter::doFinalization(Module &M) {
Rafael Espindolad7998d02006-12-18 03:37:18 +0000158 if (TAI->getWeakRefDirective()) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000159 if (!ExtWeakSymbols.empty())
Rafael Espindolad7998d02006-12-18 03:37:18 +0000160 SwitchToDataSection("");
161
162 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
163 e = ExtWeakSymbols.end(); i != e; ++i) {
164 const GlobalValue *GV = *i;
165 std::string Name = Mang->getValueName(GV);
Dan Gohman6896901e2008-06-30 22:03:41 +0000166 O << TAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolad7998d02006-12-18 03:37:18 +0000167 }
168 }
169
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000170 if (TAI->getSetDirective()) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000171 if (!M.alias_empty())
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000172 SwitchToTextSection(TAI->getTextSection());
173
Dan Gohman6896901e2008-06-30 22:03:41 +0000174 O << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000175 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
176 I!=E; ++I) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000177 std::string Name = Mang->getValueName(I);
178 std::string Target;
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000179
180 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
181 Target = Mang->getValueName(GV);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000182
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000183 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000184 O << "\t.globl\t" << Name << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000185 else if (I->hasWeakLinkage())
Dan Gohman6896901e2008-06-30 22:03:41 +0000186 O << TAI->getWeakRefDirective() << Name << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000187 else if (!I->hasInternalLinkage())
188 assert(0 && "Invalid alias linkage");
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000189
190 if (I->hasHiddenVisibility()) {
191 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000192 O << Directive << Name << '\n';
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000193 } else if (I->hasProtectedVisibility()) {
194 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000195 O << Directive << Name << '\n';
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000196 }
197
Dan Gohman6896901e2008-06-30 22:03:41 +0000198 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000199
200 // If the aliasee has external weak linkage it can be referenced only by
201 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
202 // weak reference in such case.
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000203 if (GV->hasExternalWeakLinkage()) {
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000204 if (TAI->getWeakRefDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000205 O << TAI->getWeakRefDirective() << Target << '\n';
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000206 else
Dan Gohman6896901e2008-06-30 22:03:41 +0000207 O << "\t.globl\t" << Target << '\n';
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000208 }
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000209 }
210 }
211
Gordon Henriksend930f912008-08-17 18:44:35 +0000212 GCModuleInfo *MI = getAnalysisToUpdate<GCModuleInfo>();
213 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
214 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
215 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
216 MP->finishAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000217
Dan Gohmanbcde1722008-05-05 00:28:39 +0000218 // If we don't have any trampolines, then we don't require stack memory
219 // to be executable. Some targets have a directive to declare this.
220 Function* InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
221 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
222 if (TAI->getNonexecutableStackDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000223 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmanbcde1722008-05-05 00:28:39 +0000224
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000225 delete Mang; Mang = 0;
226 return false;
227}
228
Bill Wendlinge8c885f2007-09-18 09:10:16 +0000229std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
Bill Wendling067f1d82007-09-18 01:47:22 +0000230 assert(MF && "No machine function?");
Dale Johannesen49155772008-04-02 20:10:52 +0000231 std::string Name = MF->getFunction()->getName();
232 if (Name.empty())
233 Name = Mang->getValueName(MF->getFunction());
234 return Mang->makeNameProper(Name + ".eh", TAI->getGlobalPrefix());
Bill Wendling067f1d82007-09-18 01:47:22 +0000235}
236
Chris Lattnerbb644e32005-11-21 07:51:36 +0000237void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000238 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000239 CurrentFnName = Mang->getValueName(MF.getFunction());
Evan Chengcdf36092007-10-14 05:57:21 +0000240 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000241}
242
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000243/// EmitConstantPool - Print to the current output stream assembly
244/// representations of the constants in the constant pool MCP. This is
245/// used to print out constants which have been "spilled to memory" by
246/// the code generator.
247///
248void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000249 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000250 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000251
252 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
253 // in special sections.
254 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
255 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
256 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
257 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000258 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000259 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000260 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng2ad050f2006-09-14 07:35:00 +0000261 const Type *Ty = CPE.getType();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000262 if (TAI->getFourByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000263 TM.getTargetData()->getABITypeSize(Ty) == 4)
Evan Chengec1d60b2006-06-29 00:26:09 +0000264 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000265 else if (TAI->getEightByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000266 TM.getTargetData()->getABITypeSize(Ty) == 8)
Evan Chengec1d60b2006-06-29 00:26:09 +0000267 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000268 else if (TAI->getSixteenByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000269 TM.getTargetData()->getABITypeSize(Ty) == 16)
Evan Chengec1d60b2006-06-29 00:26:09 +0000270 SixteenByteCPs.push_back(std::make_pair(CPE, i));
271 else
272 OtherCPs.push_back(std::make_pair(CPE, i));
273 }
274
275 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000276 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
277 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
278 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
279 SixteenByteCPs);
280 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Chengec1d60b2006-06-29 00:26:09 +0000281}
282
283void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
284 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
285 if (CP.empty()) return;
286
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000287 SwitchToDataSection(Section);
Evan Chengec1d60b2006-06-29 00:26:09 +0000288 EmitAlignment(Alignment);
289 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengcdf36092007-10-14 05:57:21 +0000290 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Owen Anderson93719642008-08-21 00:14:44 +0000291 << CP[i].second << ":\t\t\t\t\t";
292 // O << TAI->getCommentString() << ' ' <<
293 // WriteTypeSymbolic(O, CP[i].first.getType(), 0);
Chris Lattner5d2a9a42008-08-19 04:44:30 +0000294 O << '\n';
Evan Cheng2ad050f2006-09-14 07:35:00 +0000295 if (CP[i].first.isMachineConstantPoolEntry())
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000296 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000297 else
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000298 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000299 if (i != e-1) {
Evan Cheng2ad050f2006-09-14 07:35:00 +0000300 const Type *Ty = CP[i].first.getType();
Evan Chengec1d60b2006-06-29 00:26:09 +0000301 unsigned EntSize =
Duncan Sands283207a2007-11-05 00:04:43 +0000302 TM.getTargetData()->getABITypeSize(Ty);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000303 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000304 // Emit inter-object padding for alignment.
Evan Cheng2ad050f2006-09-14 07:35:00 +0000305 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000306 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000307 }
308}
309
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000310/// EmitJumpTableInfo - Print assembly representations of the jump tables used
311/// by the current function to the current output stream.
312///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000313void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
314 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000315 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
316 if (JT.empty()) return;
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000317
Jim Laskey70323a82006-12-14 19:17:33 +0000318 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begemanefc312a2006-07-27 16:46:58 +0000319
Nate Begeman78756502006-07-27 01:13:04 +0000320 // Pick the directive to use to print the jump table entries, and switch to
321 // the appropriate section.
Jim Laskey70323a82006-12-14 19:17:33 +0000322 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000323
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000324 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
325 const Function *F = MF.getFunction();
326 unsigned SectionFlags = TAI->SectionFlagsForGlobal(F);
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000327 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000328 !JumpTableDataSection ||
329 SectionFlags & SectionFlags::Linkonce) {
Jim Laskey70323a82006-12-14 19:17:33 +0000330 // In PIC mode, we need to emit the jump table to the same section as the
331 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000332 // We should also do if the section name is NULL or function is declared in
333 // discardable section.
Jim Laskey70323a82006-12-14 19:17:33 +0000334 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman78756502006-07-27 01:13:04 +0000335 } else {
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000336 SwitchToDataSection(JumpTableDataSection);
Nate Begeman78756502006-07-27 01:13:04 +0000337 }
Jim Laskey70323a82006-12-14 19:17:33 +0000338
339 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000340
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000341 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000342 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner28bfe382006-10-28 18:10:06 +0000343
344 // If this jump table was deleted, ignore it.
345 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-08-12 21:29:52 +0000346
347 // For PIC codegen, if possible we want to use the SetDirective to reduce
348 // the number of relocations the assembler will generate for the jump table.
349 // Set directives are all printed before the jump table itself.
Evan Cheng797d56f2007-11-09 01:32:10 +0000350 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Jim Laskey70323a82006-12-14 19:17:33 +0000351 if (TAI->getSetDirective() && IsPic)
Nate Begeman984c1a42006-08-12 21:29:52 +0000352 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng797d56f2007-11-09 01:32:10 +0000353 if (EmittedSets.insert(JTBBs[ii]))
354 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman984c1a42006-08-12 21:29:52 +0000355
Chris Lattner0ee2d462007-01-18 01:12:56 +0000356 // On some targets (e.g. darwin) we want to emit two consequtive labels
357 // before each jump table. The first label is never referenced, but tells
358 // the assembler and linker the extents of the jump table object. The
359 // second label is actually referenced by the code.
360 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
Evan Chengcdf36092007-10-14 05:57:21 +0000361 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Chris Lattner0ee2d462007-01-18 01:12:56 +0000362
Evan Chengcdf36092007-10-14 05:57:21 +0000363 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
364 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000365
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000366 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000367 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000368 O << '\n';
369 }
370 }
371}
372
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000373void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
374 const MachineBasicBlock *MBB,
375 unsigned uid) const {
376 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
377
378 // Use JumpTableDirective otherwise honor the entry size from the jump table
379 // info.
380 const char *JTEntryDirective = TAI->getJumpTableDirective();
381 bool HadJTEntryDirective = JTEntryDirective != NULL;
382 if (!HadJTEntryDirective) {
383 JTEntryDirective = MJTI->getEntrySize() == 4 ?
384 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
385 }
386
387 O << JTEntryDirective << ' ';
388
389 // If we have emitted set directives for the jump table entries, print
390 // them rather than the entries themselves. If we're emitting PIC, then
391 // emit the table entries as differences between two text section labels.
392 // If we're emitting non-PIC code, then emit the entries as direct
393 // references to the target basic blocks.
394 if (IsPic) {
395 if (TAI->getSetDirective()) {
396 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
397 << '_' << uid << "_set_" << MBB->getNumber();
398 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000399 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000400 // If the arch uses custom Jump Table directives, don't calc relative to
401 // JT
402 if (!HadJTEntryDirective)
403 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
404 << getFunctionNumber() << '_' << uid;
405 }
406 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000407 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000408 }
409}
410
411
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000412/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
413/// special global used by LLVM. If so, emit it and return true, otherwise
414/// do nothing and return false.
415bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Andrew Lenharthbeb80a92007-08-22 19:33:11 +0000416 if (GV->getName() == "llvm.used") {
417 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
418 EmitLLVMUsedList(GV->getInitializer());
419 return true;
420 }
421
Jim Laskey313570f2006-03-07 22:00:35 +0000422 // Ignore debug and non-emitted data.
423 if (GV->getSection() == "llvm.metadata") return true;
424
425 if (!GV->hasAppendingLinkage()) return false;
426
427 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000428
Evan Cheng53ce7de2007-06-04 20:39:18 +0000429 const TargetData *TD = TM.getTargetData();
430 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner3760e902006-01-12 19:17:23 +0000431 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000432 SwitchToDataSection(TAI->getStaticCtorsSection());
Evan Cheng53ce7de2007-06-04 20:39:18 +0000433 EmitAlignment(Align, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000434 EmitXXStructorList(GV->getInitializer());
435 return true;
436 }
437
Chris Lattner3760e902006-01-12 19:17:23 +0000438 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000439 SwitchToDataSection(TAI->getStaticDtorsSection());
Evan Cheng53ce7de2007-06-04 20:39:18 +0000440 EmitAlignment(Align, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000441 EmitXXStructorList(GV->getInitializer());
442 return true;
443 }
444
445 return false;
446}
447
Dale Johannesen5c1ff112008-09-03 20:34:58 +0000448/// findGlobalValue - if CV is an expression equivalent to a single
449/// global value, return that value.
450const GlobalValue * AsmPrinter::findGlobalValue(const Constant *CV) {
451 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
452 return GV;
453 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
454 const TargetData *TD = TM.getTargetData();
455 unsigned Opcode = CE->getOpcode();
456 switch (Opcode) {
457 case Instruction::GetElementPtr: {
458 const Constant *ptrVal = CE->getOperand(0);
459 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
460 if (TD->getIndexedOffset(ptrVal->getType(), &idxVec[0], idxVec.size()))
461 return 0;
462 return findGlobalValue(ptrVal);
463 }
464 case Instruction::BitCast:
465 return findGlobalValue(CE->getOperand(0));
466 default:
467 return 0;
468 }
469 }
470 return 0;
471}
472
Chris Lattner66af3902006-09-26 03:38:18 +0000473/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
Dale Johannesenabb1e772008-09-09 22:29:13 +0000474/// global in the specified llvm.used list for which emitUsedDirectiveFor
475/// is true, as being used with this directive.
476
Chris Lattner66af3902006-09-26 03:38:18 +0000477void AsmPrinter::EmitLLVMUsedList(Constant *List) {
478 const char *Directive = TAI->getUsedDirective();
479
480 // Should be an array of 'sbyte*'.
481 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
482 if (InitList == 0) return;
483
484 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Dale Johannesen5c1ff112008-09-03 20:34:58 +0000485 const GlobalValue *GV = findGlobalValue(InitList->getOperand(i));
Dale Johannesenabb1e772008-09-09 22:29:13 +0000486 if (TAI->emitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen5c1ff112008-09-03 20:34:58 +0000487 O << Directive;
488 EmitConstantValueOnly(InitList->getOperand(i));
489 O << '\n';
490 }
Chris Lattner66af3902006-09-26 03:38:18 +0000491 }
492}
493
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000494/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
495/// function pointers, ignoring the init priority.
496void AsmPrinter::EmitXXStructorList(Constant *List) {
497 // Should be an array of '{ int, void ()* }' structs. The first value is the
498 // init priority, which we ignore.
499 if (!isa<ConstantArray>(List)) return;
500 ConstantArray *InitList = cast<ConstantArray>(List);
501 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
502 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
503 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000504
505 if (CS->getOperand(1)->isNullValue())
506 return; // Found a null terminator, exit printing.
507 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000508 EmitGlobalConstant(CS->getOperand(1));
509 }
510}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000511
Jim Laskey71262542006-10-17 13:41:07 +0000512/// getGlobalLinkName - Returns the asm/link name of of the specified
513/// global variable. Should be overridden by each target asm printer to
514/// generate the appropriate value.
Jim Laskeyd24b9132006-10-17 17:17:24 +0000515const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
516 std::string LinkName;
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000517
518 if (isa<Function>(GV)) {
519 LinkName += TAI->getFunctionAddrPrefix();
520 LinkName += Mang->getValueName(GV);
521 LinkName += TAI->getFunctionAddrSuffix();
522 } else {
523 LinkName += TAI->getGlobalVarAddrPrefix();
524 LinkName += Mang->getValueName(GV);
525 LinkName += TAI->getGlobalVarAddrSuffix();
526 }
527
Jim Laskeyd24b9132006-10-17 17:17:24 +0000528 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000529}
530
Jim Laskey18fc0972007-02-21 22:47:38 +0000531/// EmitExternalGlobal - Emit the external reference to a global variable.
532/// Should be overridden if an indirect reference should be used.
533void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
534 O << getGlobalLinkName(GV);
535}
536
537
538
Jim Laskey1c055e82007-01-25 15:12:02 +0000539//===----------------------------------------------------------------------===//
540/// LEB 128 number encoding.
541
542/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
543/// representing an unsigned leb128 value.
544void AsmPrinter::PrintULEB128(unsigned Value) const {
545 do {
546 unsigned Byte = Value & 0x7f;
547 Value >>= 7;
548 if (Value) Byte |= 0x80;
Owen Anderson93719642008-08-21 00:14:44 +0000549 O << "0x" << utohexstr(Byte);
Jim Laskey1c055e82007-01-25 15:12:02 +0000550 if (Value) O << ", ";
551 } while (Value);
552}
553
Jim Laskey1c055e82007-01-25 15:12:02 +0000554/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
555/// representing a signed leb128 value.
556void AsmPrinter::PrintSLEB128(int Value) const {
557 int Sign = Value >> (8 * sizeof(Value) - 1);
558 bool IsMore;
Anton Korobeynikovbd890b12008-08-16 12:57:46 +0000559
Jim Laskey1c055e82007-01-25 15:12:02 +0000560 do {
561 unsigned Byte = Value & 0x7f;
562 Value >>= 7;
563 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
564 if (IsMore) Byte |= 0x80;
Owen Anderson93719642008-08-21 00:14:44 +0000565 O << "0x" << utohexstr(Byte);
Jim Laskey1c055e82007-01-25 15:12:02 +0000566 if (IsMore) O << ", ";
567 } while (IsMore);
568}
569
Jim Laskey1c055e82007-01-25 15:12:02 +0000570//===--------------------------------------------------------------------===//
571// Emission and print routines
572//
573
574/// PrintHex - Print a value as a hexidecimal value.
575///
576void AsmPrinter::PrintHex(int Value) const {
Owen Anderson93719642008-08-21 00:14:44 +0000577 O << "0x" << utohexstr(static_cast<unsigned>(Value));
Jim Laskey1c055e82007-01-25 15:12:02 +0000578}
579
580/// EOL - Print a newline character to asm stream. If a comment is present
581/// then it will be printed first. Comments should not contain '\n'.
Jim Laskey18fc0972007-02-21 22:47:38 +0000582void AsmPrinter::EOL() const {
Dan Gohman6896901e2008-06-30 22:03:41 +0000583 O << '\n';
Jim Laskey18fc0972007-02-21 22:47:38 +0000584}
Owen Anderson1d952532008-07-01 21:16:27 +0000585
Jim Laskey1c055e82007-01-25 15:12:02 +0000586void AsmPrinter::EOL(const std::string &Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000587 if (VerboseAsm && !Comment.empty()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000588 O << '\t'
Jim Laskey1c055e82007-01-25 15:12:02 +0000589 << TAI->getCommentString()
Dan Gohman6896901e2008-06-30 22:03:41 +0000590 << ' '
Jim Laskey1c055e82007-01-25 15:12:02 +0000591 << Comment;
592 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000593 O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000594}
595
Owen Anderson1d952532008-07-01 21:16:27 +0000596void AsmPrinter::EOL(const char* Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000597 if (VerboseAsm && *Comment) {
Owen Anderson1d952532008-07-01 21:16:27 +0000598 O << '\t'
599 << TAI->getCommentString()
600 << ' '
601 << Comment;
602 }
603 O << '\n';
604}
605
Jim Laskey1c055e82007-01-25 15:12:02 +0000606/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
607/// unsigned leb128 value.
608void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
609 if (TAI->hasLEB128()) {
610 O << "\t.uleb128\t"
611 << Value;
612 } else {
613 O << TAI->getData8bitsDirective();
614 PrintULEB128(Value);
615 }
616}
617
618/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
619/// signed leb128 value.
620void AsmPrinter::EmitSLEB128Bytes(int Value) const {
621 if (TAI->hasLEB128()) {
622 O << "\t.sleb128\t"
623 << Value;
624 } else {
625 O << TAI->getData8bitsDirective();
626 PrintSLEB128(Value);
627 }
628}
629
630/// EmitInt8 - Emit a byte directive and value.
631///
632void AsmPrinter::EmitInt8(int Value) const {
633 O << TAI->getData8bitsDirective();
634 PrintHex(Value & 0xFF);
635}
636
637/// EmitInt16 - Emit a short directive and value.
638///
639void AsmPrinter::EmitInt16(int Value) const {
640 O << TAI->getData16bitsDirective();
641 PrintHex(Value & 0xFFFF);
642}
643
644/// EmitInt32 - Emit a long directive and value.
645///
646void AsmPrinter::EmitInt32(int Value) const {
647 O << TAI->getData32bitsDirective();
648 PrintHex(Value);
649}
650
651/// EmitInt64 - Emit a long long directive and value.
652///
653void AsmPrinter::EmitInt64(uint64_t Value) const {
654 if (TAI->getData64bitsDirective()) {
655 O << TAI->getData64bitsDirective();
656 PrintHex(Value);
657 } else {
658 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000659 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000660 EmitInt32(unsigned(Value));
661 } else {
Dan Gohman6896901e2008-06-30 22:03:41 +0000662 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000663 EmitInt32(unsigned(Value >> 32));
664 }
665 }
666}
667
668/// toOctal - Convert the low order bits of X into an octal digit.
669///
670static inline char toOctal(int X) {
671 return (X&7)+'0';
672}
673
674/// printStringChar - Print a char, escaped if necessary.
675///
Owen Anderson93719642008-08-21 00:14:44 +0000676static void printStringChar(raw_ostream &O, char C) {
Jim Laskey1c055e82007-01-25 15:12:02 +0000677 if (C == '"') {
678 O << "\\\"";
679 } else if (C == '\\') {
680 O << "\\\\";
681 } else if (isprint(C)) {
682 O << C;
683 } else {
684 switch(C) {
685 case '\b': O << "\\b"; break;
686 case '\f': O << "\\f"; break;
687 case '\n': O << "\\n"; break;
688 case '\r': O << "\\r"; break;
689 case '\t': O << "\\t"; break;
690 default:
691 O << '\\';
692 O << toOctal(C >> 6);
693 O << toOctal(C >> 3);
694 O << toOctal(C >> 0);
695 break;
696 }
697 }
698}
699
700/// EmitString - Emit a string with quotes and a null terminator.
701/// Special characters are emitted properly.
702/// \literal (Eg. '\t') \endliteral
703void AsmPrinter::EmitString(const std::string &String) const {
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000704 const char* AscizDirective = TAI->getAscizDirective();
705 if (AscizDirective)
706 O << AscizDirective;
707 else
708 O << TAI->getAsciiDirective();
Dan Gohman6896901e2008-06-30 22:03:41 +0000709 O << '\"';
Jim Laskey1c055e82007-01-25 15:12:02 +0000710 for (unsigned i = 0, N = String.size(); i < N; ++i) {
711 unsigned char C = String[i];
712 printStringChar(O, C);
713 }
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000714 if (AscizDirective)
Dan Gohman6896901e2008-06-30 22:03:41 +0000715 O << '\"';
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000716 else
717 O << "\\0\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000718}
719
720
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000721/// EmitFile - Emit a .file directive.
722void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
723 O << "\t.file\t" << Number << " \"";
724 for (unsigned i = 0, N = Name.size(); i < N; ++i) {
725 unsigned char C = Name[i];
726 printStringChar(O, C);
727 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000728 O << '\"';
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000729}
730
731
Jim Laskey1c055e82007-01-25 15:12:02 +0000732//===----------------------------------------------------------------------===//
733
Chris Lattner3e3ff302007-05-31 18:57:45 +0000734// EmitAlignment - Emit an alignment directive to the specified power of
735// two boundary. For example, if you pass in 3 here, you will get an 8
736// byte alignment. If a global value is specified, and if that global has
737// an explicit alignment requested, it will unconditionally override the
738// alignment request. However, if ForcedAlignBits is specified, this value
739// has final say: the ultimate alignment will be the max of ForcedAlignBits
740// and the alignment computed with NumBits and the global.
741//
742// The algorithm is:
743// Align = NumBits;
744// if (GV && GV->hasalignment) Align = GV->getalignment();
745// Align = std::max(Align, ForcedAlignBits);
746//
747void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng26edb592008-02-29 19:36:59 +0000748 unsigned ForcedAlignBits,
749 bool UseFillExpr) const {
Dale Johannesen8653d292007-04-23 23:33:31 +0000750 if (GV && GV->getAlignment())
Chris Lattner3e3ff302007-05-31 18:57:45 +0000751 NumBits = Log2_32(GV->getAlignment());
752 NumBits = std::max(NumBits, ForcedAlignBits);
753
Chris Lattner747960d2005-11-10 18:09:27 +0000754 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000755 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng86eb3fd2007-07-25 23:35:07 +0000756 O << TAI->getAlignDirective() << NumBits;
Evan Chengc7990652008-02-28 00:43:03 +0000757
758 unsigned FillValue = TAI->getTextAlignFillValue();
Evan Cheng26edb592008-02-29 19:36:59 +0000759 UseFillExpr &= IsInTextSection && FillValue;
Owen Anderson93719642008-08-21 00:14:44 +0000760 if (UseFillExpr) O << ",0x" << utohexstr(FillValue);
Dan Gohman6896901e2008-06-30 22:03:41 +0000761 O << '\n';
Chris Lattner1d35c162004-08-17 19:14:29 +0000762}
763
Jim Laskey18fc0972007-02-21 22:47:38 +0000764
Chris Lattnerbb644e32005-11-21 07:51:36 +0000765/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000766///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000767void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000768 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000769 if (TAI->getZeroDirective()) {
770 O << TAI->getZeroDirective() << NumZeros;
771 if (TAI->getZeroDirectiveSuffix())
772 O << TAI->getZeroDirectiveSuffix();
Dan Gohman6896901e2008-06-30 22:03:41 +0000773 O << '\n';
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000774 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000775 for (; NumZeros; --NumZeros)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000776 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000777 }
778 }
779}
780
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000781// Print out the specified constant, without a storage class. Only the
782// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000783void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000784 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman6896901e2008-06-30 22:03:41 +0000785 O << '0';
Zhou Sheng75b871f2007-01-11 12:24:14 +0000786 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel499c1192008-06-03 06:18:19 +0000787 O << CI->getZExtValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000788 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000789 // This is a constant address for a global variable or function. Use the
790 // name of the variable or function as the address value, possibly
791 // decorating it with GlobalVarAddrPrefix/Suffix or
792 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000793 if (isa<Function>(GV)) {
794 O << TAI->getFunctionAddrPrefix()
795 << Mang->getValueName(GV)
796 << TAI->getFunctionAddrSuffix();
797 } else {
798 O << TAI->getGlobalVarAddrPrefix()
799 << Mang->getValueName(GV)
800 << TAI->getGlobalVarAddrSuffix();
801 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000802 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000803 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000804 unsigned Opcode = CE->getOpcode();
805 switch (Opcode) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000806 case Instruction::GetElementPtr: {
807 // generate a symbolic expression for the byte address
808 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner83dfca82007-02-10 20:31:59 +0000809 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
810 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
811 idxVec.size())) {
Chris Lattner145569b2005-02-14 21:40:26 +0000812 if (Offset)
Dan Gohman6896901e2008-06-30 22:03:41 +0000813 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000814 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000815 if (Offset > 0)
816 O << ") + " << Offset;
817 else if (Offset < 0)
818 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000819 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000820 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000821 }
822 break;
823 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000824 case Instruction::Trunc:
825 case Instruction::ZExt:
826 case Instruction::SExt:
827 case Instruction::FPTrunc:
828 case Instruction::FPExt:
829 case Instruction::UIToFP:
830 case Instruction::SIToFP:
831 case Instruction::FPToUI:
832 case Instruction::FPToSI:
833 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
834 break;
Chris Lattnerb9e41f52006-12-12 05:14:13 +0000835 case Instruction::BitCast:
836 return EmitConstantValueOnly(CE->getOperand(0));
837
Chris Lattner0c537da2006-12-12 05:18:19 +0000838 case Instruction::IntToPtr: {
839 // Handle casts to pointers by changing them into casts to the appropriate
840 // integer type. This promotes constant folding and simplifies this code.
841 Constant *Op = CE->getOperand(0);
842 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
843 return EmitConstantValueOnly(Op);
844 }
845
846
847 case Instruction::PtrToInt: {
Chris Lattner85492422006-07-29 01:57:19 +0000848 // Support only foldable casts to/from pointers that can be eliminated by
849 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000850 Constant *Op = CE->getOperand(0);
Chris Lattner0c537da2006-12-12 05:18:19 +0000851 const Type *Ty = CE->getType();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000852
Chris Lattner0c537da2006-12-12 05:18:19 +0000853 // We can emit the pointer value into this slot if the slot is an
854 // integer slot greater or equal to the size of the pointer.
Nick Lewycky42a19b62008-08-08 06:34:07 +0000855 if (TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
Chris Lattner85492422006-07-29 01:57:19 +0000856 return EmitConstantValueOnly(Op);
Nick Lewycky42a19b62008-08-08 06:34:07 +0000857
858 O << "((";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000859 EmitConstantValueOnly(Op);
Nick Lewycky42a19b62008-08-08 06:34:07 +0000860 APInt ptrMask = APInt::getAllOnesValue(TD->getABITypeSizeInBits(Ty));
Chris Lattner17f71652008-08-17 07:19:36 +0000861
862 SmallString<40> S;
863 ptrMask.toStringUnsigned(S);
864 O << ") & " << S.c_str() << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000865 break;
866 }
867 case Instruction::Add:
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000868 case Instruction::Sub:
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000869 case Instruction::And:
870 case Instruction::Or:
871 case Instruction::Xor:
Dan Gohman6896901e2008-06-30 22:03:41 +0000872 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000873 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman6896901e2008-06-30 22:03:41 +0000874 O << ')';
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000875 switch (Opcode) {
876 case Instruction::Add:
877 O << " + ";
878 break;
879 case Instruction::Sub:
880 O << " - ";
881 break;
882 case Instruction::And:
883 O << " & ";
884 break;
885 case Instruction::Or:
886 O << " | ";
887 break;
888 case Instruction::Xor:
889 O << " ^ ";
890 break;
891 default:
892 break;
893 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000894 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000895 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman6896901e2008-06-30 22:03:41 +0000896 O << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000897 break;
898 default:
899 assert(0 && "Unsupported operator!");
900 }
901 } else {
902 assert(0 && "Unknown constant value!");
903 }
904}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000905
Chris Lattner55a6d902005-11-10 18:06:33 +0000906/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000907/// the predicate isString is true.
908///
Owen Anderson93719642008-08-21 00:14:44 +0000909static void printAsCString(raw_ostream &O, const ConstantArray *CVA,
Chris Lattner55a6d902005-11-10 18:06:33 +0000910 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000911 assert(CVA->isString() && "Array is not string compatible!");
912
Dan Gohman6896901e2008-06-30 22:03:41 +0000913 O << '\"';
Chris Lattner55a6d902005-11-10 18:06:33 +0000914 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000915 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000916 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskey1c055e82007-01-25 15:12:02 +0000917 printStringChar(O, C);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000918 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000919 O << '\"';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000920}
921
Jeff Cohen24a62a92006-05-02 01:16:28 +0000922/// EmitString - Emit a zero-byte-terminated string constant.
923///
924void AsmPrinter::EmitString(const ConstantArray *CVA) const {
925 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000926 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000927 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000928 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000929 printAsCString(O, CVA, NumElts-1);
930 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000931 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000932 printAsCString(O, CVA, NumElts);
933 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000934 O << '\n';
Jeff Cohen24a62a92006-05-02 01:16:28 +0000935}
936
Chris Lattnerbb644e32005-11-21 07:51:36 +0000937/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000938void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000939 const TargetData *TD = TM.getTargetData();
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000940 unsigned Size = TD->getABITypeSize(CV->getType());
Chris Lattner8452a1f2004-08-17 06:36:49 +0000941
Chris Lattner61753bf2004-10-16 18:19:26 +0000942 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Duncan Sands283207a2007-11-05 00:04:43 +0000943 EmitZeros(Size);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000944 return;
945 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
946 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000947 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000948 } else { // Not a string. Print the values in successive locations
Duncan Sands283207a2007-11-05 00:04:43 +0000949 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000950 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000951 }
952 return;
953 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
954 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000955 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000956 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000957 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
958 const Constant* field = CVS->getOperand(i);
959
960 // Check if padding is needed and insert one or more 0s.
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000961 uint64_t fieldSize = TD->getABITypeSize(field->getType());
Duncan Sandsf7ae8bd2007-11-05 18:03:02 +0000962 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
Chris Lattnerc473d8e2007-02-10 19:55:17 +0000963 - cvsLayout->getElementOffset(i)) - fieldSize;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000964 sizeSoFar += fieldSize + padSize;
965
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000966 // Now print the actual field value.
967 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000968
Duncan Sandsf7ae8bd2007-11-05 18:03:02 +0000969 // Insert padding - this may include padding to increase the size of the
970 // current field up to the ABI size (if the struct is not packed) as well
971 // as padding to ensure that the next field starts at the right offset.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000972 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000973 }
Chris Lattnerb84892d2007-02-10 19:59:22 +0000974 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
Chris Lattner8452a1f2004-08-17 06:36:49 +0000975 "Layout of constant struct may be incorrect!");
976 return;
977 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
978 // FP Constants are printed as integer constants to avoid losing
979 // precision...
Chris Lattner8452a1f2004-08-17 06:36:49 +0000980 if (CFP->getType() == Type::DoubleTy) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000981 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
Dale Johannesen028084e2007-09-12 03:30:33 +0000982 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000983 if (TAI->getData64bitsDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000984 O << TAI->getData64bitsDirective() << i << '\t'
985 << TAI->getCommentString() << " double value: " << Val << '\n';
Owen Anderson20a631f2006-05-03 01:29:57 +0000986 else if (TD->isBigEndian()) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000987 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +0000988 << '\t' << TAI->getCommentString()
989 << " double most significant word " << Val << '\n';
Dale Johannesen245dceb2007-09-11 18:32:33 +0000990 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman6896901e2008-06-30 22:03:41 +0000991 << '\t' << TAI->getCommentString()
992 << " double least significant word " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000993 } else {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000994 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman6896901e2008-06-30 22:03:41 +0000995 << '\t' << TAI->getCommentString()
996 << " double least significant word " << Val << '\n';
Dale Johannesen245dceb2007-09-11 18:32:33 +0000997 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +0000998 << '\t' << TAI->getCommentString()
999 << " double most significant word " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001000 }
1001 return;
Dale Johannesen028084e2007-09-12 03:30:33 +00001002 } else if (CFP->getType() == Type::FloatTy) {
Dale Johannesen245dceb2007-09-11 18:32:33 +00001003 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
1004 O << TAI->getData32bitsDirective()
Dale Johannesen028084e2007-09-12 03:30:33 +00001005 << CFP->getValueAPF().convertToAPInt().getZExtValue()
Dan Gohman6896901e2008-06-30 22:03:41 +00001006 << '\t' << TAI->getCommentString() << " float " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001007 return;
Dale Johannesen028084e2007-09-12 03:30:33 +00001008 } else if (CFP->getType() == Type::X86_FP80Ty) {
1009 // all long double variants are printed as hex
Dale Johannesen34aa41c2007-09-26 23:20:33 +00001010 // api needed to prevent premature destruction
1011 APInt api = CFP->getValueAPF().convertToAPInt();
1012 const uint64_t *p = api.getRawData();
Chris Lattnerf1a6c9f2008-01-27 06:09:28 +00001013 APFloat DoubleVal = CFP->getValueAPF();
1014 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven);
Dale Johannesen028084e2007-09-12 03:30:33 +00001015 if (TD->isBigEndian()) {
1016 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
Dan Gohman6896901e2008-06-30 22:03:41 +00001017 << '\t' << TAI->getCommentString()
Chris Lattnerf1a6c9f2008-01-27 06:09:28 +00001018 << " long double most significant halfword of ~"
Dan Gohman6896901e2008-06-30 22:03:41 +00001019 << DoubleVal.convertToDouble() << '\n';
Dale Johannesen028084e2007-09-12 03:30:33 +00001020 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001021 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +00001022 << " long double next halfword\n";
1023 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
Dan Gohman6896901e2008-06-30 22:03:41 +00001024 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +00001025 << " long double next halfword\n";
1026 O << TAI->getData16bitsDirective() << uint16_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +00001027 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +00001028 << " long double next halfword\n";
1029 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +00001030 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +00001031 << " long double least significant halfword\n";
1032 } else {
1033 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +00001034 << '\t' << TAI->getCommentString()
Chris Lattnerf1a6c9f2008-01-27 06:09:28 +00001035 << " long double least significant halfword of ~"
Dan Gohman6896901e2008-06-30 22:03:41 +00001036 << DoubleVal.convertToDouble() << '\n';
Dale Johannesen028084e2007-09-12 03:30:33 +00001037 O << TAI->getData16bitsDirective() << uint16_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +00001038 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +00001039 << " long double next halfword\n";
1040 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
Dan Gohman6896901e2008-06-30 22:03:41 +00001041 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +00001042 << " long double next halfword\n";
1043 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001044 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +00001045 << " long double next halfword\n";
1046 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
Dan Gohman6896901e2008-06-30 22:03:41 +00001047 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +00001048 << " long double most significant halfword\n";
1049 }
Duncan Sands283207a2007-11-05 00:04:43 +00001050 EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty));
Dale Johannesen028084e2007-09-12 03:30:33 +00001051 return;
Dale Johannesen6472eb62007-10-11 23:32:15 +00001052 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1053 // all long double variants are printed as hex
1054 // api needed to prevent premature destruction
1055 APInt api = CFP->getValueAPF().convertToAPInt();
1056 const uint64_t *p = api.getRawData();
1057 if (TD->isBigEndian()) {
1058 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001059 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001060 << " long double most significant word\n";
1061 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +00001062 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001063 << " long double next word\n";
1064 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001065 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001066 << " long double next word\n";
1067 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +00001068 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001069 << " long double least significant word\n";
1070 } else {
1071 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +00001072 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001073 << " long double least significant word\n";
1074 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001075 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001076 << " long double next word\n";
1077 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +00001078 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001079 << " long double next word\n";
1080 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001081 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001082 << " long double most significant word\n";
1083 }
1084 return;
Dale Johannesen028084e2007-09-12 03:30:33 +00001085 } else assert(0 && "Floating point constant type not handled");
Dan Gohmanf9b20542008-09-08 16:40:13 +00001086 } else if (CV->getType()->isInteger() &&
1087 cast<IntegerType>(CV->getType())->getBitWidth() >= 64) {
Chris Lattner8452a1f2004-08-17 06:36:49 +00001088 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Dan Gohmanf9b20542008-09-08 16:40:13 +00001089 unsigned BitWidth = CI->getBitWidth();
1090 assert(isPowerOf2_32(BitWidth) &&
1091 "Non-power-of-2-sized integers not handled!");
Misha Brukman835702a2005-04-21 22:36:52 +00001092
Dan Gohmanf9b20542008-09-08 16:40:13 +00001093 // We don't expect assemblers to support integer data directives
1094 // for more than 64 bits, so we emit the data in at most 64-bit
1095 // quantities at a time.
1096 const uint64_t *RawData = CI->getValue().getRawData();
1097 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1098 uint64_t Val;
1099 if (TD->isBigEndian())
1100 Val = RawData[e - i - 1];
1101 else
1102 Val = RawData[i];
1103
1104 if (TAI->getData64bitsDirective())
1105 O << TAI->getData64bitsDirective() << Val << '\n';
1106 else if (TD->isBigEndian()) {
1107 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
1108 << '\t' << TAI->getCommentString()
1109 << " Double-word most significant word " << Val << '\n';
1110 O << TAI->getData32bitsDirective() << unsigned(Val)
1111 << '\t' << TAI->getCommentString()
1112 << " Double-word least significant word " << Val << '\n';
1113 } else {
1114 O << TAI->getData32bitsDirective() << unsigned(Val)
1115 << '\t' << TAI->getCommentString()
1116 << " Double-word least significant word " << Val << '\n';
1117 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
1118 << '\t' << TAI->getCommentString()
1119 << " Double-word most significant word " << Val << '\n';
1120 }
Chris Lattner8452a1f2004-08-17 06:36:49 +00001121 }
1122 return;
1123 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001124 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1125 const VectorType *PTy = CP->getType();
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001126
1127 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
Duncan Sandsfc3c4892008-06-04 08:21:45 +00001128 EmitGlobalConstant(CP->getOperand(I));
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001129
1130 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +00001131 }
1132
1133 const Type *type = CV->getType();
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001134 printDataDirective(type);
Chris Lattnerbb644e32005-11-21 07:51:36 +00001135 EmitConstantValueOnly(CV);
Scott Michelc0e9ff62008-06-03 15:39:51 +00001136 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner17f71652008-08-17 07:19:36 +00001137 SmallString<40> S;
1138 CI->getValue().toStringUnsigned(S, 16);
1139 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
Scott Michelc0e9ff62008-06-03 15:39:51 +00001140 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001141 O << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001142}
Chris Lattner061d9e22006-01-27 02:10:10 +00001143
Chris Lattner17f71652008-08-17 07:19:36 +00001144void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001145 // Target doesn't support this yet!
1146 abort();
1147}
1148
Chris Lattnera32814b2006-09-26 23:59:50 +00001149/// PrintSpecial - Print information related to the specified machine instr
1150/// that is independent of the operand, and may be independent of the instr
1151/// itself. This can be useful for portably encoding the comment character
1152/// or other bits of target-specific knowledge into the asmstrings. The
1153/// syntax used is ${:comment}. Targets can override this to add support
1154/// for their own strange codes.
1155void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattner30b47082006-09-27 00:06:07 +00001156 if (!strcmp(Code, "private")) {
1157 O << TAI->getPrivateGlobalPrefix();
1158 } else if (!strcmp(Code, "comment")) {
Chris Lattnera32814b2006-09-26 23:59:50 +00001159 O << TAI->getCommentString();
1160 } else if (!strcmp(Code, "uid")) {
1161 // Assign a unique ID to this machine instruction.
1162 static const MachineInstr *LastMI = 0;
Chris Lattnera9428c42007-02-05 21:23:52 +00001163 static const Function *F = 0;
Chris Lattnera32814b2006-09-26 23:59:50 +00001164 static unsigned Counter = 0U-1;
Chris Lattnera9428c42007-02-05 21:23:52 +00001165
1166 // Comparing the address of MI isn't sufficient, because machineinstrs may
1167 // be allocated to the same address across functions.
1168 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1169
Chris Lattnera32814b2006-09-26 23:59:50 +00001170 // If this is a new machine instruction, bump the counter.
Chris Lattnera9428c42007-02-05 21:23:52 +00001171 if (LastMI != MI || F != ThisF) {
1172 ++Counter;
1173 LastMI = MI;
Chris Lattner89e66e02007-02-06 01:56:31 +00001174 F = ThisF;
Chris Lattnera9428c42007-02-05 21:23:52 +00001175 }
Chris Lattnera32814b2006-09-26 23:59:50 +00001176 O << Counter;
1177 } else {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001178 cerr << "Unknown special formatter '" << Code
1179 << "' for machine instr: " << *MI;
Chris Lattnera32814b2006-09-26 23:59:50 +00001180 exit(1);
1181 }
1182}
1183
1184
Chris Lattner061d9e22006-01-27 02:10:10 +00001185/// printInlineAsm - This method formats and prints the specified machine
1186/// instruction that is an inline asm.
1187void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +00001188 unsigned NumOperands = MI->getNumOperands();
1189
1190 // Count the number of register definitions.
1191 unsigned NumDefs = 0;
Dan Gohman9da02f52007-09-14 20:33:02 +00001192 for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef();
Chris Lattner45456d42006-09-05 20:02:51 +00001193 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +00001194 assert(NumDefs != NumOperands-1 && "No asm string?");
1195
1196 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001197
1198 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +00001199 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +00001200
Dale Johannesen2b3bc302008-01-29 02:21:21 +00001201 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1202 // These are useful to see where empty asm's wound up.
Chris Lattner3390cbe2006-07-28 00:17:20 +00001203 if (AsmStr[0] == 0) {
Dan Gohman6896901e2008-06-30 22:03:41 +00001204 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattner3390cbe2006-07-28 00:17:20 +00001205 return;
1206 }
1207
Jim Laskeya6211dc2006-09-06 18:34:40 +00001208 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +00001209
Bill Wendlinge21237e2007-01-16 03:42:04 +00001210 // The variant of the current asmprinter.
1211 int AsmPrinterVariant = TAI->getAssemblerDialect();
1212
Chris Lattneraa23fa92006-02-01 22:41:11 +00001213 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1214 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +00001215
Chris Lattneraa23fa92006-02-01 22:41:11 +00001216 while (*LastEmitted) {
1217 switch (*LastEmitted) {
1218 default: {
1219 // Not a special case, emit the string section literally.
1220 const char *LiteralEnd = LastEmitted+1;
1221 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +00001222 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +00001223 ++LiteralEnd;
1224 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1225 O.write(LastEmitted, LiteralEnd-LastEmitted);
1226 LastEmitted = LiteralEnd;
1227 break;
1228 }
Chris Lattnera633c3132006-05-05 21:47:05 +00001229 case '\n':
1230 ++LastEmitted; // Consume newline character.
Dan Gohman6896901e2008-06-30 22:03:41 +00001231 O << '\n'; // Indent code with newline.
Chris Lattnera633c3132006-05-05 21:47:05 +00001232 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001233 case '$': {
1234 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001235 bool Done = true;
1236
1237 // Handle escapes.
1238 switch (*LastEmitted) {
1239 default: Done = false; break;
1240 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +00001241 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1242 O << '$';
1243 ++LastEmitted; // Consume second '$' character.
1244 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001245 case '(': // $( -> same as GCC's { character.
1246 ++LastEmitted; // Consume '(' character.
1247 if (CurVariant != -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001248 cerr << "Nested variants found in inline asm string: '"
1249 << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001250 exit(1);
1251 }
1252 CurVariant = 0; // We're in the first variant now.
1253 break;
1254 case '|':
1255 ++LastEmitted; // consume '|' character.
1256 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001257 cerr << "Found '|' character outside of variant in inline asm "
1258 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001259 exit(1);
1260 }
1261 ++CurVariant; // We're in the next variant.
1262 break;
1263 case ')': // $) -> same as GCC's } char.
1264 ++LastEmitted; // consume ')' character.
1265 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001266 cerr << "Found '}' character outside of variant in inline asm "
1267 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001268 exit(1);
1269 }
1270 CurVariant = -1;
1271 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001272 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001273 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001274
1275 bool HasCurlyBraces = false;
1276 if (*LastEmitted == '{') { // ${variable}
1277 ++LastEmitted; // Consume '{' character.
1278 HasCurlyBraces = true;
1279 }
1280
1281 const char *IDStart = LastEmitted;
1282 char *IDEnd;
Chris Lattnerd39e3882007-01-23 00:36:17 +00001283 errno = 0;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001284 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1285 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001286 cerr << "Bad $ operand number in inline asm string: '"
1287 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001288 exit(1);
1289 }
1290 LastEmitted = IDEnd;
1291
Chris Lattner34f74c12006-02-06 22:17:23 +00001292 char Modifier[2] = { 0, 0 };
1293
Chris Lattneraa23fa92006-02-01 22:41:11 +00001294 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +00001295 // If we have curly braces, check for a modifier character. This
1296 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1297 if (*LastEmitted == ':') {
1298 ++LastEmitted; // Consume ':' character.
1299 if (*LastEmitted == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001300 cerr << "Bad ${:} expression in inline asm string: '"
1301 << AsmStr << "'\n";
Chris Lattner34f74c12006-02-06 22:17:23 +00001302 exit(1);
1303 }
1304
1305 Modifier[0] = *LastEmitted;
1306 ++LastEmitted; // Consume modifier character.
1307 }
1308
Chris Lattneraa23fa92006-02-01 22:41:11 +00001309 if (*LastEmitted != '}') {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001310 cerr << "Bad ${} expression in inline asm string: '"
1311 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001312 exit(1);
1313 }
1314 ++LastEmitted; // Consume '}' character.
1315 }
1316
1317 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001318 cerr << "Invalid $ operand number in inline asm string: '"
1319 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001320 exit(1);
1321 }
1322
Chris Lattner571d9642006-02-23 19:21:04 +00001323 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +00001324 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +00001325 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1326 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001327
1328 bool Error = false;
1329
Chris Lattner571d9642006-02-23 19:21:04 +00001330 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +00001331 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001332 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner81798412007-12-30 20:50:28 +00001333 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner5af3fde2006-02-24 19:50:58 +00001334 OpNo += (OpFlags >> 3) + 1;
1335 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001336
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001337 if (OpNo >= MI->getNumOperands()) {
1338 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +00001339 } else {
Chris Lattner81798412007-12-30 20:50:28 +00001340 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001341 ++OpNo; // Skip over the ID number.
1342
Dale Johannesen4646aa32007-11-05 21:20:28 +00001343 if (Modifier[0]=='l') // labels are target independent
Chris Lattnera5bb3702007-12-30 23:10:15 +00001344 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Chengc7990652008-02-28 00:43:03 +00001345 false, false, false);
Dale Johannesen4646aa32007-11-05 21:20:28 +00001346 else {
1347 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesenc36660d2008-09-24 01:07:17 +00001348 if ((OpFlags & 7) == 4) {
Dale Johannesen4646aa32007-11-05 21:20:28 +00001349 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1350 Modifier[0] ? Modifier : 0);
1351 } else {
1352 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1353 Modifier[0] ? Modifier : 0);
1354 }
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001355 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001356 }
1357 if (Error) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001358 cerr << "Invalid operand found in inline asm: '"
1359 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001360 MI->dump();
1361 exit(1);
1362 }
Chris Lattner571d9642006-02-23 19:21:04 +00001363 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001364 break;
1365 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001366 }
1367 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001368 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattneraa23fa92006-02-01 22:41:11 +00001369}
1370
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001371/// printImplicitDef - This method prints the specified machine instruction
1372/// that is an implicit def.
1373void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Dan Gohman6896901e2008-06-30 22:03:41 +00001374 O << '\t' << TAI->getCommentString() << " implicit-def: "
1375 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001376}
1377
Jim Laskeyf9e54452007-01-26 14:34:52 +00001378/// printLabel - This method prints a local label used by debug and
1379/// exception handling tables.
1380void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohmanb58aff42008-07-01 00:16:26 +00001381 printLabel(MI->getOperand(0).getImm());
Jim Laskeyf9e54452007-01-26 14:34:52 +00001382}
1383
Evan Chengd6e44ab2008-02-01 09:10:45 +00001384void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng32e53472008-02-02 08:39:46 +00001385 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chengd6e44ab2008-02-01 09:10:45 +00001386}
1387
Evan Chengefd142a2008-02-02 04:07:54 +00001388/// printDeclare - This method prints a local variable declaration used by
1389/// debug tables.
Evan Cheng2cb90682008-02-04 23:06:48 +00001390/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1391/// entry into dwarf table.
Evan Chengefd142a2008-02-02 04:07:54 +00001392void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Evan Cheng2cb90682008-02-04 23:06:48 +00001393 int FI = MI->getOperand(0).getIndex();
1394 GlobalValue *GV = MI->getOperand(1).getGlobal();
1395 MMI->RecordVariable(GV, FI);
Evan Chengefd142a2008-02-02 04:07:54 +00001396}
1397
Chris Lattneraa23fa92006-02-01 22:41:11 +00001398/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1399/// instruction, using the specified assembler variant. Targets should
1400/// overried this to format as appropriate.
1401bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +00001402 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +00001403 // Target doesn't support this yet!
1404 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +00001405}
Chris Lattner1d08c652006-02-24 20:21:58 +00001406
1407bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1408 unsigned AsmVariant,
1409 const char *ExtraCode) {
1410 // Target doesn't support this yet!
1411 return true;
1412}
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001413
1414/// printBasicBlockLabel - This method prints the label for the specified
1415/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +00001416void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Chengc7990652008-02-28 00:43:03 +00001417 bool printAlign,
Nate Begemanb9d4f832006-05-02 05:37:32 +00001418 bool printColon,
1419 bool printComment) const {
Evan Chengc7990652008-02-28 00:43:03 +00001420 if (printAlign) {
1421 unsigned Align = MBB->getAlignment();
1422 if (Align)
1423 EmitAlignment(Log2_32(Align));
1424 }
1425
Dan Gohman6896901e2008-06-30 22:03:41 +00001426 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Chengcdf36092007-10-14 05:57:21 +00001427 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +00001428 if (printColon)
1429 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +00001430 if (printComment && MBB->getBasicBlock())
Dan Gohman33d0ea22007-07-30 15:06:25 +00001431 O << '\t' << TAI->getCommentString() << ' '
Evan Cheng53495222008-07-08 00:55:58 +00001432 << MBB->getBasicBlock()->getNameStart();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001433}
Nate Begeman984c1a42006-08-12 21:29:52 +00001434
Evan Cheng797d56f2007-11-09 01:32:10 +00001435/// printPICJumpTableSetLabel - This method prints a set label for the
1436/// specified MachineBasicBlock for a jumptable entry.
1437void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1438 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001439 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +00001440 return;
1441
Jim Laskeya6211dc2006-09-06 18:34:40 +00001442 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001443 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001444 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001445 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1446 << '_' << uid << '\n';
Nate Begeman984c1a42006-08-12 21:29:52 +00001447}
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001448
Evan Cheng797d56f2007-11-09 01:32:10 +00001449void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1450 const MachineBasicBlock *MBB) const {
Evan Cheng91f120f2006-11-01 09:23:08 +00001451 if (!TAI->getSetDirective())
1452 return;
1453
1454 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001455 << getFunctionNumber() << '_' << uid << '_' << uid2
1456 << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001457 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001458 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1459 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng91f120f2006-11-01 09:23:08 +00001460}
1461
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001462/// printDataDirective - This method prints the asm directive for the
1463/// specified type.
1464void AsmPrinter::printDataDirective(const Type *type) {
1465 const TargetData *TD = TM.getTargetData();
1466 switch (type->getTypeID()) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001467 case Type::IntegerTyID: {
1468 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1469 if (BitWidth <= 8)
1470 O << TAI->getData8bitsDirective();
1471 else if (BitWidth <= 16)
1472 O << TAI->getData16bitsDirective();
1473 else if (BitWidth <= 32)
1474 O << TAI->getData32bitsDirective();
1475 else if (BitWidth <= 64) {
1476 assert(TAI->getData64bitsDirective() &&
1477 "Target cannot handle 64-bit constant exprs!");
1478 O << TAI->getData64bitsDirective();
Dan Gohmanf9b20542008-09-08 16:40:13 +00001479 } else {
1480 assert(0 && "Target cannot handle given data directive width!");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001481 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001482 break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001483 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001484 case Type::PointerTyID:
1485 if (TD->getPointerSize() == 8) {
1486 assert(TAI->getData64bitsDirective() &&
1487 "Target cannot handle 64-bit pointer exprs!");
1488 O << TAI->getData64bitsDirective();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001489 } else {
1490 O << TAI->getData32bitsDirective();
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001491 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001492 break;
1493 case Type::FloatTyID: case Type::DoubleTyID:
Dale Johannesen6bf69ed2007-09-28 18:06:58 +00001494 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001495 assert (0 && "Should have already output floating point constant.");
1496 default:
1497 assert (0 && "Can't handle printing this type of thing");
1498 break;
1499 }
1500}
Dale Johannesen915e1542007-02-16 01:54:53 +00001501
Evan Cheng0a1e6722008-07-08 16:40:43 +00001502void AsmPrinter::printSuffixedName(const char *Name, const char *Suffix,
1503 const char *Prefix) {
Dale Johannesen5bf742f2008-05-19 21:38:18 +00001504 if (Name[0]=='\"')
Evan Cheng0a1e6722008-07-08 16:40:43 +00001505 O << '\"';
1506 O << TAI->getPrivateGlobalPrefix();
1507 if (Prefix) O << Prefix;
1508 if (Name[0]=='\"')
1509 O << '\"';
1510 if (Name[0]=='\"')
1511 O << Name[1];
Dale Johannesen5bf742f2008-05-19 21:38:18 +00001512 else
Evan Cheng0a1e6722008-07-08 16:40:43 +00001513 O << Name;
1514 O << Suffix;
1515 if (Name[0]=='\"')
1516 O << '\"';
Dale Johannesen5bf742f2008-05-19 21:38:18 +00001517}
Evan Cheng53495222008-07-08 00:55:58 +00001518
Evan Cheng0a1e6722008-07-08 16:40:43 +00001519void AsmPrinter::printSuffixedName(const std::string &Name, const char* Suffix) {
Evan Cheng53495222008-07-08 00:55:58 +00001520 printSuffixedName(Name.c_str(), Suffix);
1521}
Anton Korobeynikoved473292008-08-08 18:25:07 +00001522
1523void AsmPrinter::printVisibility(const std::string& Name,
1524 unsigned Visibility) const {
1525 if (Visibility == GlobalValue::HiddenVisibility) {
1526 if (const char *Directive = TAI->getHiddenDirective())
1527 O << Directive << Name << '\n';
1528 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1529 if (const char *Directive = TAI->getProtectedDirective())
1530 O << Directive << Name << '\n';
1531 }
1532}
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001533
Gordon Henriksend930f912008-08-17 18:44:35 +00001534GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1535 if (!S->usesMetadata())
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001536 return 0;
1537
Gordon Henriksend930f912008-08-17 18:44:35 +00001538 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001539 if (GCPI != GCMetadataPrinters.end())
1540 return GCPI->second;
1541
Gordon Henriksend930f912008-08-17 18:44:35 +00001542 const char *Name = S->getName().c_str();
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001543
1544 for (GCMetadataPrinterRegistry::iterator
1545 I = GCMetadataPrinterRegistry::begin(),
1546 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1547 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001548 GCMetadataPrinter *GMP = I->instantiate();
1549 GMP->S = S;
1550 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1551 return GMP;
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001552 }
1553
Gordon Henriksend930f912008-08-17 18:44:35 +00001554 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001555 abort();
1556}