blob: 3b2dcb31d29e95f33b5cb012ae3486ec064b010f [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 Henriksen5180e852008-01-07 01:30:38 +000019#include "llvm/CodeGen/Collector.h"
20#include "llvm/CodeGen/CollectorMetadata.h"
Gordon Henriksendbe06d32008-08-17 12:08:44 +000021#include "llvm/CodeGen/Collectors.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000023#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000025#include "llvm/Support/Mangler.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000026#include "llvm/Support/MathExtras.h"
Bill Wendling5c3966a2006-11-29 00:39:47 +000027#include "llvm/Support/Streams.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000028#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000029#include "llvm/Target/TargetData.h"
Chris Lattner90438232006-10-06 22:50:56 +000030#include "llvm/Target/TargetLowering.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000031#include "llvm/Target/TargetMachine.h"
Evan Chengc963f6c2008-07-01 23:18:29 +000032#include "llvm/Target/TargetOptions.h"
Evan Cheng0e7b00d2008-03-15 00:03:38 +000033#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng797d56f2007-11-09 01:32:10 +000034#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner17f71652008-08-17 07:19:36 +000035#include "llvm/ADT/SmallString.h"
Chris Lattneraa23fa92006-02-01 22:41:11 +000036#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000037using namespace llvm;
38
Devang Patel8c78a0b2007-05-03 01:11:54 +000039char AsmPrinter::ID = 0;
Jim Laskey261779b2006-09-07 22:06:40 +000040AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
41 const TargetAsmInfo *T)
Evan Cheng0e7b00d2008-03-15 00:03:38 +000042 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o),
43 TM(tm), TAI(T), TRI(tm.getRegisterInfo()),
Evan Chengc7990652008-02-28 00:43:03 +000044 IsInTextSection(false)
Jim Laskeya6211dc2006-09-06 18:34:40 +000045{}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000046
Gordon Henriksendbe06d32008-08-17 12:08:44 +000047AsmPrinter::~AsmPrinter() {
48 for (gcp_iterator I = GCMetadataPrinters.begin(),
49 E = GCMetadataPrinters.end(); I != E; ++I)
50 delete I->second;
51}
52
Chris Lattnerdc822412006-10-05 02:42:47 +000053std::string AsmPrinter::getSectionForFunction(const Function &F) const {
54 return TAI->getTextSection();
55}
56
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000057
Chris Lattner8488ba22006-05-09 04:59:56 +000058/// SwitchToTextSection - Switch to the specified text section of the executable
59/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000060///
Chris Lattner8488ba22006-05-09 04:59:56 +000061void AsmPrinter::SwitchToTextSection(const char *NewSection,
62 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000063 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000064 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000065 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000066 else
67 NS = NewSection;
68
69 // If we're already in this section, we're done.
70 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000071
Chris Lattner4ebc6a22006-05-09 05:33:48 +000072 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000073 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman6896901e2008-06-30 22:03:41 +000074 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Jeff Cohen470f4312006-05-02 03:58:45 +000075
Chris Lattner4ebc6a22006-05-09 05:33:48 +000076 CurrentSection = NS;
77
78 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000079 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Evan Chengc7990652008-02-28 00:43:03 +000080
81 IsInTextSection = true;
Chris Lattner2ea5c992005-11-21 07:06:27 +000082}
83
Nate Begeman78756502006-07-27 01:13:04 +000084/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000085/// if we are not already in it!
86///
87void AsmPrinter::SwitchToDataSection(const char *NewSection,
88 const GlobalValue *GV) {
89 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000090 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000091 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner6341df82006-05-09 05:23:12 +000092 else
93 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +000094
Chris Lattner6341df82006-05-09 05:23:12 +000095 // If we're already in this section, we're done.
96 if (CurrentSection == NS) return;
97
Chris Lattner4ebc6a22006-05-09 05:33:48 +000098 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000099 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman6896901e2008-06-30 22:03:41 +0000100 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000101
102 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +0000103
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000104 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000105 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Evan Chengc7990652008-02-28 00:43:03 +0000106
107 IsInTextSection = false;
Chris Lattner8488ba22006-05-09 04:59:56 +0000108}
109
110
Gordon Henriksen5180e852008-01-07 01:30:38 +0000111void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
112 MachineFunctionPass::getAnalysisUsage(AU);
113 AU.addRequired<CollectorModuleMetadata>();
114}
115
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000116bool AsmPrinter::doInitialization(Module &M) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000117 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +0000118
Gordon Henriksen5180e852008-01-07 01:30:38 +0000119 CollectorModuleMetadata *CMM = getAnalysisToUpdate<CollectorModuleMetadata>();
120 assert(CMM && "AsmPrinter didn't require CollectorModuleMetadata?");
121 for (CollectorModuleMetadata::iterator I = CMM->begin(),
122 E = CMM->end(); I != E; ++I)
Gordon Henriksendbe06d32008-08-17 12:08:44 +0000123 if (GCMetadataPrinter *GCP = GetOrCreateGCPrinter(*I))
124 GCP->beginAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000125
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000126 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000127 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000128 << M.getModuleInlineAsm()
Dan Gohman6896901e2008-06-30 22:03:41 +0000129 << '\n' << TAI->getCommentString()
Jim Laskeya6211dc2006-09-06 18:34:40 +0000130 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000131
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000132 SwitchToDataSection(""); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000133
Evan Cheng2cb90682008-02-04 23:06:48 +0000134 MMI = getAnalysisToUpdate<MachineModuleInfo>();
135 if (MMI) MMI->AnalyzeModule(M);
Jim Laskey0bbdc552006-01-26 20:21:46 +0000136
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000137 return false;
138}
139
140bool AsmPrinter::doFinalization(Module &M) {
Rafael Espindolad7998d02006-12-18 03:37:18 +0000141 if (TAI->getWeakRefDirective()) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000142 if (!ExtWeakSymbols.empty())
Rafael Espindolad7998d02006-12-18 03:37:18 +0000143 SwitchToDataSection("");
144
145 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
146 e = ExtWeakSymbols.end(); i != e; ++i) {
147 const GlobalValue *GV = *i;
148 std::string Name = Mang->getValueName(GV);
Dan Gohman6896901e2008-06-30 22:03:41 +0000149 O << TAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolad7998d02006-12-18 03:37:18 +0000150 }
151 }
152
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000153 if (TAI->getSetDirective()) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000154 if (!M.alias_empty())
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000155 SwitchToTextSection(TAI->getTextSection());
156
Dan Gohman6896901e2008-06-30 22:03:41 +0000157 O << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000158 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
159 I!=E; ++I) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000160 std::string Name = Mang->getValueName(I);
161 std::string Target;
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000162
163 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
164 Target = Mang->getValueName(GV);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000165
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000166 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000167 O << "\t.globl\t" << Name << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000168 else if (I->hasWeakLinkage())
Dan Gohman6896901e2008-06-30 22:03:41 +0000169 O << TAI->getWeakRefDirective() << Name << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000170 else if (!I->hasInternalLinkage())
171 assert(0 && "Invalid alias linkage");
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000172
173 if (I->hasHiddenVisibility()) {
174 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000175 O << Directive << Name << '\n';
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000176 } else if (I->hasProtectedVisibility()) {
177 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000178 O << Directive << Name << '\n';
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000179 }
180
Dan Gohman6896901e2008-06-30 22:03:41 +0000181 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000182
183 // If the aliasee has external weak linkage it can be referenced only by
184 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
185 // weak reference in such case.
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000186 if (GV->hasExternalWeakLinkage()) {
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000187 if (TAI->getWeakRefDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000188 O << TAI->getWeakRefDirective() << Target << '\n';
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000189 else
Dan Gohman6896901e2008-06-30 22:03:41 +0000190 O << "\t.globl\t" << Target << '\n';
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000191 }
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000192 }
193 }
194
Gordon Henriksen5180e852008-01-07 01:30:38 +0000195 CollectorModuleMetadata *CMM = getAnalysisToUpdate<CollectorModuleMetadata>();
196 assert(CMM && "AsmPrinter didn't require CollectorModuleMetadata?");
197 for (CollectorModuleMetadata::iterator I = CMM->end(),
198 E = CMM->begin(); I != E; )
Gordon Henriksendbe06d32008-08-17 12:08:44 +0000199 if (GCMetadataPrinter *GCP = GetOrCreateGCPrinter(*--I))
200 GCP->finishAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000201
Dan Gohmanbcde1722008-05-05 00:28:39 +0000202 // If we don't have any trampolines, then we don't require stack memory
203 // to be executable. Some targets have a directive to declare this.
204 Function* InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
205 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
206 if (TAI->getNonexecutableStackDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000207 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmanbcde1722008-05-05 00:28:39 +0000208
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000209 delete Mang; Mang = 0;
210 return false;
211}
212
Bill Wendlinge8c885f2007-09-18 09:10:16 +0000213std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
Bill Wendling067f1d82007-09-18 01:47:22 +0000214 assert(MF && "No machine function?");
Dale Johannesen49155772008-04-02 20:10:52 +0000215 std::string Name = MF->getFunction()->getName();
216 if (Name.empty())
217 Name = Mang->getValueName(MF->getFunction());
218 return Mang->makeNameProper(Name + ".eh", TAI->getGlobalPrefix());
Bill Wendling067f1d82007-09-18 01:47:22 +0000219}
220
Chris Lattnerbb644e32005-11-21 07:51:36 +0000221void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000222 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000223 CurrentFnName = Mang->getValueName(MF.getFunction());
Evan Chengcdf36092007-10-14 05:57:21 +0000224 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000225}
226
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000227/// EmitConstantPool - Print to the current output stream assembly
228/// representations of the constants in the constant pool MCP. This is
229/// used to print out constants which have been "spilled to memory" by
230/// the code generator.
231///
232void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000233 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000234 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000235
236 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
237 // in special sections.
238 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
239 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
240 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
241 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000242 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000243 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000244 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng2ad050f2006-09-14 07:35:00 +0000245 const Type *Ty = CPE.getType();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000246 if (TAI->getFourByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000247 TM.getTargetData()->getABITypeSize(Ty) == 4)
Evan Chengec1d60b2006-06-29 00:26:09 +0000248 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000249 else if (TAI->getEightByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000250 TM.getTargetData()->getABITypeSize(Ty) == 8)
Evan Chengec1d60b2006-06-29 00:26:09 +0000251 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000252 else if (TAI->getSixteenByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000253 TM.getTargetData()->getABITypeSize(Ty) == 16)
Evan Chengec1d60b2006-06-29 00:26:09 +0000254 SixteenByteCPs.push_back(std::make_pair(CPE, i));
255 else
256 OtherCPs.push_back(std::make_pair(CPE, i));
257 }
258
259 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000260 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
261 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
262 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
263 SixteenByteCPs);
264 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Chengec1d60b2006-06-29 00:26:09 +0000265}
266
267void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
268 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
269 if (CP.empty()) return;
270
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000271 SwitchToDataSection(Section);
Evan Chengec1d60b2006-06-29 00:26:09 +0000272 EmitAlignment(Alignment);
273 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengcdf36092007-10-14 05:57:21 +0000274 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohman6896901e2008-06-30 22:03:41 +0000275 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng2ad050f2006-09-14 07:35:00 +0000276 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
277 if (CP[i].first.isMachineConstantPoolEntry())
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000278 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000279 else
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000280 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000281 if (i != e-1) {
Evan Cheng2ad050f2006-09-14 07:35:00 +0000282 const Type *Ty = CP[i].first.getType();
Evan Chengec1d60b2006-06-29 00:26:09 +0000283 unsigned EntSize =
Duncan Sands283207a2007-11-05 00:04:43 +0000284 TM.getTargetData()->getABITypeSize(Ty);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000285 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000286 // Emit inter-object padding for alignment.
Evan Cheng2ad050f2006-09-14 07:35:00 +0000287 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000288 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000289 }
290}
291
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000292/// EmitJumpTableInfo - Print assembly representations of the jump tables used
293/// by the current function to the current output stream.
294///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000295void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
296 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000297 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
298 if (JT.empty()) return;
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000299
Jim Laskey70323a82006-12-14 19:17:33 +0000300 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begemanefc312a2006-07-27 16:46:58 +0000301
Nate Begeman78756502006-07-27 01:13:04 +0000302 // Pick the directive to use to print the jump table entries, and switch to
303 // the appropriate section.
Jim Laskey70323a82006-12-14 19:17:33 +0000304 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000305
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000306 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
307 const Function *F = MF.getFunction();
308 unsigned SectionFlags = TAI->SectionFlagsForGlobal(F);
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000309 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000310 !JumpTableDataSection ||
311 SectionFlags & SectionFlags::Linkonce) {
Jim Laskey70323a82006-12-14 19:17:33 +0000312 // In PIC mode, we need to emit the jump table to the same section as the
313 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000314 // We should also do if the section name is NULL or function is declared in
315 // discardable section.
Jim Laskey70323a82006-12-14 19:17:33 +0000316 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman78756502006-07-27 01:13:04 +0000317 } else {
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000318 SwitchToDataSection(JumpTableDataSection);
Nate Begeman78756502006-07-27 01:13:04 +0000319 }
Jim Laskey70323a82006-12-14 19:17:33 +0000320
321 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000322
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000323 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000324 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner28bfe382006-10-28 18:10:06 +0000325
326 // If this jump table was deleted, ignore it.
327 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-08-12 21:29:52 +0000328
329 // For PIC codegen, if possible we want to use the SetDirective to reduce
330 // the number of relocations the assembler will generate for the jump table.
331 // Set directives are all printed before the jump table itself.
Evan Cheng797d56f2007-11-09 01:32:10 +0000332 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Jim Laskey70323a82006-12-14 19:17:33 +0000333 if (TAI->getSetDirective() && IsPic)
Nate Begeman984c1a42006-08-12 21:29:52 +0000334 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng797d56f2007-11-09 01:32:10 +0000335 if (EmittedSets.insert(JTBBs[ii]))
336 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman984c1a42006-08-12 21:29:52 +0000337
Chris Lattner0ee2d462007-01-18 01:12:56 +0000338 // On some targets (e.g. darwin) we want to emit two consequtive labels
339 // before each jump table. The first label is never referenced, but tells
340 // the assembler and linker the extents of the jump table object. The
341 // second label is actually referenced by the code.
342 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
Evan Chengcdf36092007-10-14 05:57:21 +0000343 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Chris Lattner0ee2d462007-01-18 01:12:56 +0000344
Evan Chengcdf36092007-10-14 05:57:21 +0000345 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
346 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000347
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000348 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000349 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000350 O << '\n';
351 }
352 }
353}
354
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000355void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
356 const MachineBasicBlock *MBB,
357 unsigned uid) const {
358 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
359
360 // Use JumpTableDirective otherwise honor the entry size from the jump table
361 // info.
362 const char *JTEntryDirective = TAI->getJumpTableDirective();
363 bool HadJTEntryDirective = JTEntryDirective != NULL;
364 if (!HadJTEntryDirective) {
365 JTEntryDirective = MJTI->getEntrySize() == 4 ?
366 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
367 }
368
369 O << JTEntryDirective << ' ';
370
371 // If we have emitted set directives for the jump table entries, print
372 // them rather than the entries themselves. If we're emitting PIC, then
373 // emit the table entries as differences between two text section labels.
374 // If we're emitting non-PIC code, then emit the entries as direct
375 // references to the target basic blocks.
376 if (IsPic) {
377 if (TAI->getSetDirective()) {
378 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
379 << '_' << uid << "_set_" << MBB->getNumber();
380 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000381 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000382 // If the arch uses custom Jump Table directives, don't calc relative to
383 // JT
384 if (!HadJTEntryDirective)
385 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
386 << getFunctionNumber() << '_' << uid;
387 }
388 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000389 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000390 }
391}
392
393
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000394/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
395/// special global used by LLVM. If so, emit it and return true, otherwise
396/// do nothing and return false.
397bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Andrew Lenharthbeb80a92007-08-22 19:33:11 +0000398 if (GV->getName() == "llvm.used") {
399 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
400 EmitLLVMUsedList(GV->getInitializer());
401 return true;
402 }
403
Jim Laskey313570f2006-03-07 22:00:35 +0000404 // Ignore debug and non-emitted data.
405 if (GV->getSection() == "llvm.metadata") return true;
406
407 if (!GV->hasAppendingLinkage()) return false;
408
409 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000410
Evan Cheng53ce7de2007-06-04 20:39:18 +0000411 const TargetData *TD = TM.getTargetData();
412 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner3760e902006-01-12 19:17:23 +0000413 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000414 SwitchToDataSection(TAI->getStaticCtorsSection());
Evan Cheng53ce7de2007-06-04 20:39:18 +0000415 EmitAlignment(Align, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000416 EmitXXStructorList(GV->getInitializer());
417 return true;
418 }
419
Chris Lattner3760e902006-01-12 19:17:23 +0000420 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000421 SwitchToDataSection(TAI->getStaticDtorsSection());
Evan Cheng53ce7de2007-06-04 20:39:18 +0000422 EmitAlignment(Align, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000423 EmitXXStructorList(GV->getInitializer());
424 return true;
425 }
426
427 return false;
428}
429
Chris Lattner66af3902006-09-26 03:38:18 +0000430/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
431/// global in the specified llvm.used list as being used with this directive.
432void AsmPrinter::EmitLLVMUsedList(Constant *List) {
433 const char *Directive = TAI->getUsedDirective();
434
435 // Should be an array of 'sbyte*'.
436 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
437 if (InitList == 0) return;
438
439 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
440 O << Directive;
441 EmitConstantValueOnly(InitList->getOperand(i));
Dan Gohman6896901e2008-06-30 22:03:41 +0000442 O << '\n';
Chris Lattner66af3902006-09-26 03:38:18 +0000443 }
444}
445
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000446/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
447/// function pointers, ignoring the init priority.
448void AsmPrinter::EmitXXStructorList(Constant *List) {
449 // Should be an array of '{ int, void ()* }' structs. The first value is the
450 // init priority, which we ignore.
451 if (!isa<ConstantArray>(List)) return;
452 ConstantArray *InitList = cast<ConstantArray>(List);
453 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
454 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
455 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000456
457 if (CS->getOperand(1)->isNullValue())
458 return; // Found a null terminator, exit printing.
459 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000460 EmitGlobalConstant(CS->getOperand(1));
461 }
462}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000463
Jim Laskey71262542006-10-17 13:41:07 +0000464/// getGlobalLinkName - Returns the asm/link name of of the specified
465/// global variable. Should be overridden by each target asm printer to
466/// generate the appropriate value.
Jim Laskeyd24b9132006-10-17 17:17:24 +0000467const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
468 std::string LinkName;
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000469
470 if (isa<Function>(GV)) {
471 LinkName += TAI->getFunctionAddrPrefix();
472 LinkName += Mang->getValueName(GV);
473 LinkName += TAI->getFunctionAddrSuffix();
474 } else {
475 LinkName += TAI->getGlobalVarAddrPrefix();
476 LinkName += Mang->getValueName(GV);
477 LinkName += TAI->getGlobalVarAddrSuffix();
478 }
479
Jim Laskeyd24b9132006-10-17 17:17:24 +0000480 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000481}
482
Jim Laskey18fc0972007-02-21 22:47:38 +0000483/// EmitExternalGlobal - Emit the external reference to a global variable.
484/// Should be overridden if an indirect reference should be used.
485void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
486 O << getGlobalLinkName(GV);
487}
488
489
490
Jim Laskey1c055e82007-01-25 15:12:02 +0000491//===----------------------------------------------------------------------===//
492/// LEB 128 number encoding.
493
494/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
495/// representing an unsigned leb128 value.
496void AsmPrinter::PrintULEB128(unsigned Value) const {
497 do {
498 unsigned Byte = Value & 0x7f;
499 Value >>= 7;
500 if (Value) Byte |= 0x80;
501 O << "0x" << std::hex << Byte << std::dec;
502 if (Value) O << ", ";
503 } while (Value);
504}
505
Jim Laskey1c055e82007-01-25 15:12:02 +0000506/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
507/// representing a signed leb128 value.
508void AsmPrinter::PrintSLEB128(int Value) const {
509 int Sign = Value >> (8 * sizeof(Value) - 1);
510 bool IsMore;
Anton Korobeynikovbd890b12008-08-16 12:57:46 +0000511
Jim Laskey1c055e82007-01-25 15:12:02 +0000512 do {
513 unsigned Byte = Value & 0x7f;
514 Value >>= 7;
515 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
516 if (IsMore) Byte |= 0x80;
517 O << "0x" << std::hex << Byte << std::dec;
518 if (IsMore) O << ", ";
519 } while (IsMore);
520}
521
Jim Laskey1c055e82007-01-25 15:12:02 +0000522//===--------------------------------------------------------------------===//
523// Emission and print routines
524//
525
526/// PrintHex - Print a value as a hexidecimal value.
527///
528void AsmPrinter::PrintHex(int Value) const {
529 O << "0x" << std::hex << Value << std::dec;
530}
531
532/// EOL - Print a newline character to asm stream. If a comment is present
533/// then it will be printed first. Comments should not contain '\n'.
Jim Laskey18fc0972007-02-21 22:47:38 +0000534void AsmPrinter::EOL() const {
Dan Gohman6896901e2008-06-30 22:03:41 +0000535 O << '\n';
Jim Laskey18fc0972007-02-21 22:47:38 +0000536}
Owen Anderson1d952532008-07-01 21:16:27 +0000537
Jim Laskey1c055e82007-01-25 15:12:02 +0000538void AsmPrinter::EOL(const std::string &Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000539 if (VerboseAsm && !Comment.empty()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000540 O << '\t'
Jim Laskey1c055e82007-01-25 15:12:02 +0000541 << TAI->getCommentString()
Dan Gohman6896901e2008-06-30 22:03:41 +0000542 << ' '
Jim Laskey1c055e82007-01-25 15:12:02 +0000543 << Comment;
544 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000545 O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000546}
547
Owen Anderson1d952532008-07-01 21:16:27 +0000548void AsmPrinter::EOL(const char* Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000549 if (VerboseAsm && *Comment) {
Owen Anderson1d952532008-07-01 21:16:27 +0000550 O << '\t'
551 << TAI->getCommentString()
552 << ' '
553 << Comment;
554 }
555 O << '\n';
556}
557
Jim Laskey1c055e82007-01-25 15:12:02 +0000558/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
559/// unsigned leb128 value.
560void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
561 if (TAI->hasLEB128()) {
562 O << "\t.uleb128\t"
563 << Value;
564 } else {
565 O << TAI->getData8bitsDirective();
566 PrintULEB128(Value);
567 }
568}
569
570/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
571/// signed leb128 value.
572void AsmPrinter::EmitSLEB128Bytes(int Value) const {
573 if (TAI->hasLEB128()) {
574 O << "\t.sleb128\t"
575 << Value;
576 } else {
577 O << TAI->getData8bitsDirective();
578 PrintSLEB128(Value);
579 }
580}
581
582/// EmitInt8 - Emit a byte directive and value.
583///
584void AsmPrinter::EmitInt8(int Value) const {
585 O << TAI->getData8bitsDirective();
586 PrintHex(Value & 0xFF);
587}
588
589/// EmitInt16 - Emit a short directive and value.
590///
591void AsmPrinter::EmitInt16(int Value) const {
592 O << TAI->getData16bitsDirective();
593 PrintHex(Value & 0xFFFF);
594}
595
596/// EmitInt32 - Emit a long directive and value.
597///
598void AsmPrinter::EmitInt32(int Value) const {
599 O << TAI->getData32bitsDirective();
600 PrintHex(Value);
601}
602
603/// EmitInt64 - Emit a long long directive and value.
604///
605void AsmPrinter::EmitInt64(uint64_t Value) const {
606 if (TAI->getData64bitsDirective()) {
607 O << TAI->getData64bitsDirective();
608 PrintHex(Value);
609 } else {
610 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000611 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000612 EmitInt32(unsigned(Value));
613 } else {
Dan Gohman6896901e2008-06-30 22:03:41 +0000614 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000615 EmitInt32(unsigned(Value >> 32));
616 }
617 }
618}
619
620/// toOctal - Convert the low order bits of X into an octal digit.
621///
622static inline char toOctal(int X) {
623 return (X&7)+'0';
624}
625
626/// printStringChar - Print a char, escaped if necessary.
627///
628static void printStringChar(std::ostream &O, unsigned char C) {
629 if (C == '"') {
630 O << "\\\"";
631 } else if (C == '\\') {
632 O << "\\\\";
633 } else if (isprint(C)) {
634 O << C;
635 } else {
636 switch(C) {
637 case '\b': O << "\\b"; break;
638 case '\f': O << "\\f"; break;
639 case '\n': O << "\\n"; break;
640 case '\r': O << "\\r"; break;
641 case '\t': O << "\\t"; break;
642 default:
643 O << '\\';
644 O << toOctal(C >> 6);
645 O << toOctal(C >> 3);
646 O << toOctal(C >> 0);
647 break;
648 }
649 }
650}
651
652/// EmitString - Emit a string with quotes and a null terminator.
653/// Special characters are emitted properly.
654/// \literal (Eg. '\t') \endliteral
655void AsmPrinter::EmitString(const std::string &String) const {
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000656 const char* AscizDirective = TAI->getAscizDirective();
657 if (AscizDirective)
658 O << AscizDirective;
659 else
660 O << TAI->getAsciiDirective();
Dan Gohman6896901e2008-06-30 22:03:41 +0000661 O << '\"';
Jim Laskey1c055e82007-01-25 15:12:02 +0000662 for (unsigned i = 0, N = String.size(); i < N; ++i) {
663 unsigned char C = String[i];
664 printStringChar(O, C);
665 }
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000666 if (AscizDirective)
Dan Gohman6896901e2008-06-30 22:03:41 +0000667 O << '\"';
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000668 else
669 O << "\\0\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000670}
671
672
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000673/// EmitFile - Emit a .file directive.
674void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
675 O << "\t.file\t" << Number << " \"";
676 for (unsigned i = 0, N = Name.size(); i < N; ++i) {
677 unsigned char C = Name[i];
678 printStringChar(O, C);
679 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000680 O << '\"';
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000681}
682
683
Jim Laskey1c055e82007-01-25 15:12:02 +0000684//===----------------------------------------------------------------------===//
685
Chris Lattner3e3ff302007-05-31 18:57:45 +0000686// EmitAlignment - Emit an alignment directive to the specified power of
687// two boundary. For example, if you pass in 3 here, you will get an 8
688// byte alignment. If a global value is specified, and if that global has
689// an explicit alignment requested, it will unconditionally override the
690// alignment request. However, if ForcedAlignBits is specified, this value
691// has final say: the ultimate alignment will be the max of ForcedAlignBits
692// and the alignment computed with NumBits and the global.
693//
694// The algorithm is:
695// Align = NumBits;
696// if (GV && GV->hasalignment) Align = GV->getalignment();
697// Align = std::max(Align, ForcedAlignBits);
698//
699void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng26edb592008-02-29 19:36:59 +0000700 unsigned ForcedAlignBits,
701 bool UseFillExpr) const {
Dale Johannesen8653d292007-04-23 23:33:31 +0000702 if (GV && GV->getAlignment())
Chris Lattner3e3ff302007-05-31 18:57:45 +0000703 NumBits = Log2_32(GV->getAlignment());
704 NumBits = std::max(NumBits, ForcedAlignBits);
705
Chris Lattner747960d2005-11-10 18:09:27 +0000706 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000707 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng86eb3fd2007-07-25 23:35:07 +0000708 O << TAI->getAlignDirective() << NumBits;
Evan Chengc7990652008-02-28 00:43:03 +0000709
710 unsigned FillValue = TAI->getTextAlignFillValue();
Evan Cheng26edb592008-02-29 19:36:59 +0000711 UseFillExpr &= IsInTextSection && FillValue;
Evan Cheng86eb3fd2007-07-25 23:35:07 +0000712 if (UseFillExpr) O << ",0x" << std::hex << FillValue << std::dec;
Dan Gohman6896901e2008-06-30 22:03:41 +0000713 O << '\n';
Chris Lattner1d35c162004-08-17 19:14:29 +0000714}
715
Jim Laskey18fc0972007-02-21 22:47:38 +0000716
Chris Lattnerbb644e32005-11-21 07:51:36 +0000717/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000718///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000719void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000720 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000721 if (TAI->getZeroDirective()) {
722 O << TAI->getZeroDirective() << NumZeros;
723 if (TAI->getZeroDirectiveSuffix())
724 O << TAI->getZeroDirectiveSuffix();
Dan Gohman6896901e2008-06-30 22:03:41 +0000725 O << '\n';
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000726 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000727 for (; NumZeros; --NumZeros)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000728 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000729 }
730 }
731}
732
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000733// Print out the specified constant, without a storage class. Only the
734// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000735void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000736 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman6896901e2008-06-30 22:03:41 +0000737 O << '0';
Zhou Sheng75b871f2007-01-11 12:24:14 +0000738 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel499c1192008-06-03 06:18:19 +0000739 O << CI->getZExtValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000740 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000741 // This is a constant address for a global variable or function. Use the
742 // name of the variable or function as the address value, possibly
743 // decorating it with GlobalVarAddrPrefix/Suffix or
744 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000745 if (isa<Function>(GV)) {
746 O << TAI->getFunctionAddrPrefix()
747 << Mang->getValueName(GV)
748 << TAI->getFunctionAddrSuffix();
749 } else {
750 O << TAI->getGlobalVarAddrPrefix()
751 << Mang->getValueName(GV)
752 << TAI->getGlobalVarAddrSuffix();
753 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000754 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000755 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000756 unsigned Opcode = CE->getOpcode();
757 switch (Opcode) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000758 case Instruction::GetElementPtr: {
759 // generate a symbolic expression for the byte address
760 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner83dfca82007-02-10 20:31:59 +0000761 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
762 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
763 idxVec.size())) {
Chris Lattner145569b2005-02-14 21:40:26 +0000764 if (Offset)
Dan Gohman6896901e2008-06-30 22:03:41 +0000765 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000766 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000767 if (Offset > 0)
768 O << ") + " << Offset;
769 else if (Offset < 0)
770 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000771 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000772 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000773 }
774 break;
775 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000776 case Instruction::Trunc:
777 case Instruction::ZExt:
778 case Instruction::SExt:
779 case Instruction::FPTrunc:
780 case Instruction::FPExt:
781 case Instruction::UIToFP:
782 case Instruction::SIToFP:
783 case Instruction::FPToUI:
784 case Instruction::FPToSI:
785 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
786 break;
Chris Lattnerb9e41f52006-12-12 05:14:13 +0000787 case Instruction::BitCast:
788 return EmitConstantValueOnly(CE->getOperand(0));
789
Chris Lattner0c537da2006-12-12 05:18:19 +0000790 case Instruction::IntToPtr: {
791 // Handle casts to pointers by changing them into casts to the appropriate
792 // integer type. This promotes constant folding and simplifies this code.
793 Constant *Op = CE->getOperand(0);
794 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
795 return EmitConstantValueOnly(Op);
796 }
797
798
799 case Instruction::PtrToInt: {
Chris Lattner85492422006-07-29 01:57:19 +0000800 // Support only foldable casts to/from pointers that can be eliminated by
801 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000802 Constant *Op = CE->getOperand(0);
Chris Lattner0c537da2006-12-12 05:18:19 +0000803 const Type *Ty = CE->getType();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000804
Chris Lattner0c537da2006-12-12 05:18:19 +0000805 // We can emit the pointer value into this slot if the slot is an
806 // integer slot greater or equal to the size of the pointer.
Nick Lewycky42a19b62008-08-08 06:34:07 +0000807 if (TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
Chris Lattner85492422006-07-29 01:57:19 +0000808 return EmitConstantValueOnly(Op);
Nick Lewycky42a19b62008-08-08 06:34:07 +0000809
810 O << "((";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000811 EmitConstantValueOnly(Op);
Nick Lewycky42a19b62008-08-08 06:34:07 +0000812 APInt ptrMask = APInt::getAllOnesValue(TD->getABITypeSizeInBits(Ty));
Chris Lattner17f71652008-08-17 07:19:36 +0000813
814 SmallString<40> S;
815 ptrMask.toStringUnsigned(S);
816 O << ") & " << S.c_str() << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000817 break;
818 }
819 case Instruction::Add:
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000820 case Instruction::Sub:
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000821 case Instruction::And:
822 case Instruction::Or:
823 case Instruction::Xor:
Dan Gohman6896901e2008-06-30 22:03:41 +0000824 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000825 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman6896901e2008-06-30 22:03:41 +0000826 O << ')';
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000827 switch (Opcode) {
828 case Instruction::Add:
829 O << " + ";
830 break;
831 case Instruction::Sub:
832 O << " - ";
833 break;
834 case Instruction::And:
835 O << " & ";
836 break;
837 case Instruction::Or:
838 O << " | ";
839 break;
840 case Instruction::Xor:
841 O << " ^ ";
842 break;
843 default:
844 break;
845 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000846 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000847 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman6896901e2008-06-30 22:03:41 +0000848 O << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000849 break;
850 default:
851 assert(0 && "Unsupported operator!");
852 }
853 } else {
854 assert(0 && "Unknown constant value!");
855 }
856}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000857
Chris Lattner55a6d902005-11-10 18:06:33 +0000858/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000859/// the predicate isString is true.
860///
Chris Lattner55a6d902005-11-10 18:06:33 +0000861static void printAsCString(std::ostream &O, const ConstantArray *CVA,
862 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000863 assert(CVA->isString() && "Array is not string compatible!");
864
Dan Gohman6896901e2008-06-30 22:03:41 +0000865 O << '\"';
Chris Lattner55a6d902005-11-10 18:06:33 +0000866 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000867 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000868 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskey1c055e82007-01-25 15:12:02 +0000869 printStringChar(O, C);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000870 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000871 O << '\"';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000872}
873
Jeff Cohen24a62a92006-05-02 01:16:28 +0000874/// EmitString - Emit a zero-byte-terminated string constant.
875///
876void AsmPrinter::EmitString(const ConstantArray *CVA) const {
877 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000878 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000879 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000880 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000881 printAsCString(O, CVA, NumElts-1);
882 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000883 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000884 printAsCString(O, CVA, NumElts);
885 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000886 O << '\n';
Jeff Cohen24a62a92006-05-02 01:16:28 +0000887}
888
Chris Lattnerbb644e32005-11-21 07:51:36 +0000889/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000890void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000891 const TargetData *TD = TM.getTargetData();
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000892 unsigned Size = TD->getABITypeSize(CV->getType());
Chris Lattner8452a1f2004-08-17 06:36:49 +0000893
Chris Lattner61753bf2004-10-16 18:19:26 +0000894 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Duncan Sands283207a2007-11-05 00:04:43 +0000895 EmitZeros(Size);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000896 return;
897 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
898 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000899 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000900 } else { // Not a string. Print the values in successive locations
Duncan Sands283207a2007-11-05 00:04:43 +0000901 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000902 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000903 }
904 return;
905 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
906 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000907 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000908 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000909 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
910 const Constant* field = CVS->getOperand(i);
911
912 // Check if padding is needed and insert one or more 0s.
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000913 uint64_t fieldSize = TD->getABITypeSize(field->getType());
Duncan Sandsf7ae8bd2007-11-05 18:03:02 +0000914 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
Chris Lattnerc473d8e2007-02-10 19:55:17 +0000915 - cvsLayout->getElementOffset(i)) - fieldSize;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000916 sizeSoFar += fieldSize + padSize;
917
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000918 // Now print the actual field value.
919 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000920
Duncan Sandsf7ae8bd2007-11-05 18:03:02 +0000921 // Insert padding - this may include padding to increase the size of the
922 // current field up to the ABI size (if the struct is not packed) as well
923 // as padding to ensure that the next field starts at the right offset.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000924 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000925 }
Chris Lattnerb84892d2007-02-10 19:59:22 +0000926 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
Chris Lattner8452a1f2004-08-17 06:36:49 +0000927 "Layout of constant struct may be incorrect!");
928 return;
929 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
930 // FP Constants are printed as integer constants to avoid losing
931 // precision...
Chris Lattner8452a1f2004-08-17 06:36:49 +0000932 if (CFP->getType() == Type::DoubleTy) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000933 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
Dale Johannesen028084e2007-09-12 03:30:33 +0000934 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000935 if (TAI->getData64bitsDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000936 O << TAI->getData64bitsDirective() << i << '\t'
937 << TAI->getCommentString() << " double value: " << Val << '\n';
Owen Anderson20a631f2006-05-03 01:29:57 +0000938 else if (TD->isBigEndian()) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000939 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +0000940 << '\t' << TAI->getCommentString()
941 << " double most significant word " << Val << '\n';
Dale Johannesen245dceb2007-09-11 18:32:33 +0000942 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman6896901e2008-06-30 22:03:41 +0000943 << '\t' << TAI->getCommentString()
944 << " double least significant word " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000945 } else {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000946 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman6896901e2008-06-30 22:03:41 +0000947 << '\t' << TAI->getCommentString()
948 << " double least significant word " << Val << '\n';
Dale Johannesen245dceb2007-09-11 18:32:33 +0000949 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +0000950 << '\t' << TAI->getCommentString()
951 << " double most significant word " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000952 }
953 return;
Dale Johannesen028084e2007-09-12 03:30:33 +0000954 } else if (CFP->getType() == Type::FloatTy) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000955 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
956 O << TAI->getData32bitsDirective()
Dale Johannesen028084e2007-09-12 03:30:33 +0000957 << CFP->getValueAPF().convertToAPInt().getZExtValue()
Dan Gohman6896901e2008-06-30 22:03:41 +0000958 << '\t' << TAI->getCommentString() << " float " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000959 return;
Dale Johannesen028084e2007-09-12 03:30:33 +0000960 } else if (CFP->getType() == Type::X86_FP80Ty) {
961 // all long double variants are printed as hex
Dale Johannesen34aa41c2007-09-26 23:20:33 +0000962 // api needed to prevent premature destruction
963 APInt api = CFP->getValueAPF().convertToAPInt();
964 const uint64_t *p = api.getRawData();
Chris Lattnerf1a6c9f2008-01-27 06:09:28 +0000965 APFloat DoubleVal = CFP->getValueAPF();
966 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven);
Dale Johannesen028084e2007-09-12 03:30:33 +0000967 if (TD->isBigEndian()) {
968 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
Dan Gohman6896901e2008-06-30 22:03:41 +0000969 << '\t' << TAI->getCommentString()
Chris Lattnerf1a6c9f2008-01-27 06:09:28 +0000970 << " long double most significant halfword of ~"
Dan Gohman6896901e2008-06-30 22:03:41 +0000971 << DoubleVal.convertToDouble() << '\n';
Dale Johannesen028084e2007-09-12 03:30:33 +0000972 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +0000973 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000974 << " long double next halfword\n";
975 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
Dan Gohman6896901e2008-06-30 22:03:41 +0000976 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000977 << " long double next halfword\n";
978 O << TAI->getData16bitsDirective() << uint16_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +0000979 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000980 << " long double next halfword\n";
981 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +0000982 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000983 << " long double least significant halfword\n";
984 } else {
985 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +0000986 << '\t' << TAI->getCommentString()
Chris Lattnerf1a6c9f2008-01-27 06:09:28 +0000987 << " long double least significant halfword of ~"
Dan Gohman6896901e2008-06-30 22:03:41 +0000988 << DoubleVal.convertToDouble() << '\n';
Dale Johannesen028084e2007-09-12 03:30:33 +0000989 O << TAI->getData16bitsDirective() << uint16_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +0000990 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000991 << " long double next halfword\n";
992 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
Dan Gohman6896901e2008-06-30 22:03:41 +0000993 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000994 << " long double next halfword\n";
995 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +0000996 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000997 << " long double next halfword\n";
998 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
Dan Gohman6896901e2008-06-30 22:03:41 +0000999 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +00001000 << " long double most significant halfword\n";
1001 }
Duncan Sands283207a2007-11-05 00:04:43 +00001002 EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty));
Dale Johannesen028084e2007-09-12 03:30:33 +00001003 return;
Dale Johannesen6472eb62007-10-11 23:32:15 +00001004 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1005 // all long double variants are printed as hex
1006 // api needed to prevent premature destruction
1007 APInt api = CFP->getValueAPF().convertToAPInt();
1008 const uint64_t *p = api.getRawData();
1009 if (TD->isBigEndian()) {
1010 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001011 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001012 << " long double most significant word\n";
1013 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +00001014 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001015 << " long double next word\n";
1016 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001017 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001018 << " long double next word\n";
1019 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +00001020 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001021 << " long double least significant word\n";
1022 } else {
1023 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +00001024 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001025 << " long double least significant word\n";
1026 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001027 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001028 << " long double next word\n";
1029 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +00001030 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001031 << " long double next word\n";
1032 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001033 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001034 << " long double most significant word\n";
1035 }
1036 return;
Dale Johannesen028084e2007-09-12 03:30:33 +00001037 } else assert(0 && "Floating point constant type not handled");
Reid Spencere63b6512006-12-31 05:55:36 +00001038 } else if (CV->getType() == Type::Int64Ty) {
Chris Lattner8452a1f2004-08-17 06:36:49 +00001039 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00001040 uint64_t Val = CI->getZExtValue();
Misha Brukman835702a2005-04-21 22:36:52 +00001041
Jim Laskeya6211dc2006-09-06 18:34:40 +00001042 if (TAI->getData64bitsDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +00001043 O << TAI->getData64bitsDirective() << Val << '\n';
Owen Anderson20a631f2006-05-03 01:29:57 +00001044 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001045 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001046 << '\t' << TAI->getCommentString()
1047 << " Double-word most significant word " << Val << '\n';
Jim Laskeya6211dc2006-09-06 18:34:40 +00001048 O << TAI->getData32bitsDirective() << unsigned(Val)
Dan Gohman6896901e2008-06-30 22:03:41 +00001049 << '\t' << TAI->getCommentString()
1050 << " Double-word least significant word " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001051 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001052 O << TAI->getData32bitsDirective() << unsigned(Val)
Dan Gohman6896901e2008-06-30 22:03:41 +00001053 << '\t' << TAI->getCommentString()
1054 << " Double-word least significant word " << Val << '\n';
Jim Laskeya6211dc2006-09-06 18:34:40 +00001055 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001056 << '\t' << TAI->getCommentString()
1057 << " Double-word most significant word " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001058 }
1059 return;
1060 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001061 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1062 const VectorType *PTy = CP->getType();
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001063
1064 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
Duncan Sandsfc3c4892008-06-04 08:21:45 +00001065 EmitGlobalConstant(CP->getOperand(I));
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001066
1067 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +00001068 }
1069
1070 const Type *type = CV->getType();
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001071 printDataDirective(type);
Chris Lattnerbb644e32005-11-21 07:51:36 +00001072 EmitConstantValueOnly(CV);
Scott Michelc0e9ff62008-06-03 15:39:51 +00001073 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner17f71652008-08-17 07:19:36 +00001074 SmallString<40> S;
1075 CI->getValue().toStringUnsigned(S, 16);
1076 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
Scott Michelc0e9ff62008-06-03 15:39:51 +00001077 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001078 O << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001079}
Chris Lattner061d9e22006-01-27 02:10:10 +00001080
Chris Lattner17f71652008-08-17 07:19:36 +00001081void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001082 // Target doesn't support this yet!
1083 abort();
1084}
1085
Chris Lattnera32814b2006-09-26 23:59:50 +00001086/// PrintSpecial - Print information related to the specified machine instr
1087/// that is independent of the operand, and may be independent of the instr
1088/// itself. This can be useful for portably encoding the comment character
1089/// or other bits of target-specific knowledge into the asmstrings. The
1090/// syntax used is ${:comment}. Targets can override this to add support
1091/// for their own strange codes.
1092void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattner30b47082006-09-27 00:06:07 +00001093 if (!strcmp(Code, "private")) {
1094 O << TAI->getPrivateGlobalPrefix();
1095 } else if (!strcmp(Code, "comment")) {
Chris Lattnera32814b2006-09-26 23:59:50 +00001096 O << TAI->getCommentString();
1097 } else if (!strcmp(Code, "uid")) {
1098 // Assign a unique ID to this machine instruction.
1099 static const MachineInstr *LastMI = 0;
Chris Lattnera9428c42007-02-05 21:23:52 +00001100 static const Function *F = 0;
Chris Lattnera32814b2006-09-26 23:59:50 +00001101 static unsigned Counter = 0U-1;
Chris Lattnera9428c42007-02-05 21:23:52 +00001102
1103 // Comparing the address of MI isn't sufficient, because machineinstrs may
1104 // be allocated to the same address across functions.
1105 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1106
Chris Lattnera32814b2006-09-26 23:59:50 +00001107 // If this is a new machine instruction, bump the counter.
Chris Lattnera9428c42007-02-05 21:23:52 +00001108 if (LastMI != MI || F != ThisF) {
1109 ++Counter;
1110 LastMI = MI;
Chris Lattner89e66e02007-02-06 01:56:31 +00001111 F = ThisF;
Chris Lattnera9428c42007-02-05 21:23:52 +00001112 }
Chris Lattnera32814b2006-09-26 23:59:50 +00001113 O << Counter;
1114 } else {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001115 cerr << "Unknown special formatter '" << Code
1116 << "' for machine instr: " << *MI;
Chris Lattnera32814b2006-09-26 23:59:50 +00001117 exit(1);
1118 }
1119}
1120
1121
Chris Lattner061d9e22006-01-27 02:10:10 +00001122/// printInlineAsm - This method formats and prints the specified machine
1123/// instruction that is an inline asm.
1124void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +00001125 unsigned NumOperands = MI->getNumOperands();
1126
1127 // Count the number of register definitions.
1128 unsigned NumDefs = 0;
Dan Gohman9da02f52007-09-14 20:33:02 +00001129 for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef();
Chris Lattner45456d42006-09-05 20:02:51 +00001130 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +00001131 assert(NumDefs != NumOperands-1 && "No asm string?");
1132
1133 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001134
1135 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +00001136 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +00001137
Dale Johannesen2b3bc302008-01-29 02:21:21 +00001138 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1139 // These are useful to see where empty asm's wound up.
Chris Lattner3390cbe2006-07-28 00:17:20 +00001140 if (AsmStr[0] == 0) {
Dan Gohman6896901e2008-06-30 22:03:41 +00001141 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattner3390cbe2006-07-28 00:17:20 +00001142 return;
1143 }
1144
Jim Laskeya6211dc2006-09-06 18:34:40 +00001145 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +00001146
Bill Wendlinge21237e2007-01-16 03:42:04 +00001147 // The variant of the current asmprinter.
1148 int AsmPrinterVariant = TAI->getAssemblerDialect();
1149
Chris Lattneraa23fa92006-02-01 22:41:11 +00001150 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1151 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +00001152
Chris Lattneraa23fa92006-02-01 22:41:11 +00001153 while (*LastEmitted) {
1154 switch (*LastEmitted) {
1155 default: {
1156 // Not a special case, emit the string section literally.
1157 const char *LiteralEnd = LastEmitted+1;
1158 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +00001159 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +00001160 ++LiteralEnd;
1161 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1162 O.write(LastEmitted, LiteralEnd-LastEmitted);
1163 LastEmitted = LiteralEnd;
1164 break;
1165 }
Chris Lattnera633c3132006-05-05 21:47:05 +00001166 case '\n':
1167 ++LastEmitted; // Consume newline character.
Dan Gohman6896901e2008-06-30 22:03:41 +00001168 O << '\n'; // Indent code with newline.
Chris Lattnera633c3132006-05-05 21:47:05 +00001169 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001170 case '$': {
1171 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001172 bool Done = true;
1173
1174 // Handle escapes.
1175 switch (*LastEmitted) {
1176 default: Done = false; break;
1177 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +00001178 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1179 O << '$';
1180 ++LastEmitted; // Consume second '$' character.
1181 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001182 case '(': // $( -> same as GCC's { character.
1183 ++LastEmitted; // Consume '(' character.
1184 if (CurVariant != -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001185 cerr << "Nested variants found in inline asm string: '"
1186 << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001187 exit(1);
1188 }
1189 CurVariant = 0; // We're in the first variant now.
1190 break;
1191 case '|':
1192 ++LastEmitted; // consume '|' character.
1193 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001194 cerr << "Found '|' character outside of variant in inline asm "
1195 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001196 exit(1);
1197 }
1198 ++CurVariant; // We're in the next variant.
1199 break;
1200 case ')': // $) -> same as GCC's } char.
1201 ++LastEmitted; // consume ')' character.
1202 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001203 cerr << "Found '}' character outside of variant in inline asm "
1204 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001205 exit(1);
1206 }
1207 CurVariant = -1;
1208 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001209 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001210 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001211
1212 bool HasCurlyBraces = false;
1213 if (*LastEmitted == '{') { // ${variable}
1214 ++LastEmitted; // Consume '{' character.
1215 HasCurlyBraces = true;
1216 }
1217
1218 const char *IDStart = LastEmitted;
1219 char *IDEnd;
Chris Lattnerd39e3882007-01-23 00:36:17 +00001220 errno = 0;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001221 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1222 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001223 cerr << "Bad $ operand number in inline asm string: '"
1224 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001225 exit(1);
1226 }
1227 LastEmitted = IDEnd;
1228
Chris Lattner34f74c12006-02-06 22:17:23 +00001229 char Modifier[2] = { 0, 0 };
1230
Chris Lattneraa23fa92006-02-01 22:41:11 +00001231 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +00001232 // If we have curly braces, check for a modifier character. This
1233 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1234 if (*LastEmitted == ':') {
1235 ++LastEmitted; // Consume ':' character.
1236 if (*LastEmitted == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001237 cerr << "Bad ${:} expression in inline asm string: '"
1238 << AsmStr << "'\n";
Chris Lattner34f74c12006-02-06 22:17:23 +00001239 exit(1);
1240 }
1241
1242 Modifier[0] = *LastEmitted;
1243 ++LastEmitted; // Consume modifier character.
1244 }
1245
Chris Lattneraa23fa92006-02-01 22:41:11 +00001246 if (*LastEmitted != '}') {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001247 cerr << "Bad ${} expression in inline asm string: '"
1248 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001249 exit(1);
1250 }
1251 ++LastEmitted; // Consume '}' character.
1252 }
1253
1254 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001255 cerr << "Invalid $ operand number in inline asm string: '"
1256 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001257 exit(1);
1258 }
1259
Chris Lattner571d9642006-02-23 19:21:04 +00001260 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +00001261 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +00001262 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1263 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001264
1265 bool Error = false;
1266
Chris Lattner571d9642006-02-23 19:21:04 +00001267 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +00001268 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001269 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner81798412007-12-30 20:50:28 +00001270 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner5af3fde2006-02-24 19:50:58 +00001271 OpNo += (OpFlags >> 3) + 1;
1272 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001273
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001274 if (OpNo >= MI->getNumOperands()) {
1275 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +00001276 } else {
Chris Lattner81798412007-12-30 20:50:28 +00001277 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001278 ++OpNo; // Skip over the ID number.
1279
Dale Johannesen4646aa32007-11-05 21:20:28 +00001280 if (Modifier[0]=='l') // labels are target independent
Chris Lattnera5bb3702007-12-30 23:10:15 +00001281 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Chengc7990652008-02-28 00:43:03 +00001282 false, false, false);
Dale Johannesen4646aa32007-11-05 21:20:28 +00001283 else {
1284 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1285 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1286 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1287 Modifier[0] ? Modifier : 0);
1288 } else {
1289 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1290 Modifier[0] ? Modifier : 0);
1291 }
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001292 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001293 }
1294 if (Error) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001295 cerr << "Invalid operand found in inline asm: '"
1296 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001297 MI->dump();
1298 exit(1);
1299 }
Chris Lattner571d9642006-02-23 19:21:04 +00001300 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001301 break;
1302 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001303 }
1304 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001305 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattneraa23fa92006-02-01 22:41:11 +00001306}
1307
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001308/// printImplicitDef - This method prints the specified machine instruction
1309/// that is an implicit def.
1310void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Dan Gohman6896901e2008-06-30 22:03:41 +00001311 O << '\t' << TAI->getCommentString() << " implicit-def: "
1312 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001313}
1314
Jim Laskeyf9e54452007-01-26 14:34:52 +00001315/// printLabel - This method prints a local label used by debug and
1316/// exception handling tables.
1317void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohmanb58aff42008-07-01 00:16:26 +00001318 printLabel(MI->getOperand(0).getImm());
Jim Laskeyf9e54452007-01-26 14:34:52 +00001319}
1320
Evan Chengd6e44ab2008-02-01 09:10:45 +00001321void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng32e53472008-02-02 08:39:46 +00001322 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chengd6e44ab2008-02-01 09:10:45 +00001323}
1324
Evan Chengefd142a2008-02-02 04:07:54 +00001325/// printDeclare - This method prints a local variable declaration used by
1326/// debug tables.
Evan Cheng2cb90682008-02-04 23:06:48 +00001327/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1328/// entry into dwarf table.
Evan Chengefd142a2008-02-02 04:07:54 +00001329void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Evan Cheng2cb90682008-02-04 23:06:48 +00001330 int FI = MI->getOperand(0).getIndex();
1331 GlobalValue *GV = MI->getOperand(1).getGlobal();
1332 MMI->RecordVariable(GV, FI);
Evan Chengefd142a2008-02-02 04:07:54 +00001333}
1334
Chris Lattneraa23fa92006-02-01 22:41:11 +00001335/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1336/// instruction, using the specified assembler variant. Targets should
1337/// overried this to format as appropriate.
1338bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +00001339 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +00001340 // Target doesn't support this yet!
1341 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +00001342}
Chris Lattner1d08c652006-02-24 20:21:58 +00001343
1344bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1345 unsigned AsmVariant,
1346 const char *ExtraCode) {
1347 // Target doesn't support this yet!
1348 return true;
1349}
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001350
1351/// printBasicBlockLabel - This method prints the label for the specified
1352/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +00001353void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Chengc7990652008-02-28 00:43:03 +00001354 bool printAlign,
Nate Begemanb9d4f832006-05-02 05:37:32 +00001355 bool printColon,
1356 bool printComment) const {
Evan Chengc7990652008-02-28 00:43:03 +00001357 if (printAlign) {
1358 unsigned Align = MBB->getAlignment();
1359 if (Align)
1360 EmitAlignment(Log2_32(Align));
1361 }
1362
Dan Gohman6896901e2008-06-30 22:03:41 +00001363 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Chengcdf36092007-10-14 05:57:21 +00001364 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +00001365 if (printColon)
1366 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +00001367 if (printComment && MBB->getBasicBlock())
Dan Gohman33d0ea22007-07-30 15:06:25 +00001368 O << '\t' << TAI->getCommentString() << ' '
Evan Cheng53495222008-07-08 00:55:58 +00001369 << MBB->getBasicBlock()->getNameStart();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001370}
Nate Begeman984c1a42006-08-12 21:29:52 +00001371
Evan Cheng797d56f2007-11-09 01:32:10 +00001372/// printPICJumpTableSetLabel - This method prints a set label for the
1373/// specified MachineBasicBlock for a jumptable entry.
1374void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1375 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001376 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +00001377 return;
1378
Jim Laskeya6211dc2006-09-06 18:34:40 +00001379 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001380 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001381 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001382 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1383 << '_' << uid << '\n';
Nate Begeman984c1a42006-08-12 21:29:52 +00001384}
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001385
Evan Cheng797d56f2007-11-09 01:32:10 +00001386void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1387 const MachineBasicBlock *MBB) const {
Evan Cheng91f120f2006-11-01 09:23:08 +00001388 if (!TAI->getSetDirective())
1389 return;
1390
1391 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001392 << getFunctionNumber() << '_' << uid << '_' << uid2
1393 << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001394 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001395 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1396 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng91f120f2006-11-01 09:23:08 +00001397}
1398
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001399/// printDataDirective - This method prints the asm directive for the
1400/// specified type.
1401void AsmPrinter::printDataDirective(const Type *type) {
1402 const TargetData *TD = TM.getTargetData();
1403 switch (type->getTypeID()) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001404 case Type::IntegerTyID: {
1405 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1406 if (BitWidth <= 8)
1407 O << TAI->getData8bitsDirective();
1408 else if (BitWidth <= 16)
1409 O << TAI->getData16bitsDirective();
1410 else if (BitWidth <= 32)
1411 O << TAI->getData32bitsDirective();
1412 else if (BitWidth <= 64) {
1413 assert(TAI->getData64bitsDirective() &&
1414 "Target cannot handle 64-bit constant exprs!");
1415 O << TAI->getData64bitsDirective();
1416 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001417 break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001418 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001419 case Type::PointerTyID:
1420 if (TD->getPointerSize() == 8) {
1421 assert(TAI->getData64bitsDirective() &&
1422 "Target cannot handle 64-bit pointer exprs!");
1423 O << TAI->getData64bitsDirective();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001424 } else {
1425 O << TAI->getData32bitsDirective();
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001426 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001427 break;
1428 case Type::FloatTyID: case Type::DoubleTyID:
Dale Johannesen6bf69ed2007-09-28 18:06:58 +00001429 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001430 assert (0 && "Should have already output floating point constant.");
1431 default:
1432 assert (0 && "Can't handle printing this type of thing");
1433 break;
1434 }
1435}
Dale Johannesen915e1542007-02-16 01:54:53 +00001436
Evan Cheng0a1e6722008-07-08 16:40:43 +00001437void AsmPrinter::printSuffixedName(const char *Name, const char *Suffix,
1438 const char *Prefix) {
Dale Johannesen5bf742f2008-05-19 21:38:18 +00001439 if (Name[0]=='\"')
Evan Cheng0a1e6722008-07-08 16:40:43 +00001440 O << '\"';
1441 O << TAI->getPrivateGlobalPrefix();
1442 if (Prefix) O << Prefix;
1443 if (Name[0]=='\"')
1444 O << '\"';
1445 if (Name[0]=='\"')
1446 O << Name[1];
Dale Johannesen5bf742f2008-05-19 21:38:18 +00001447 else
Evan Cheng0a1e6722008-07-08 16:40:43 +00001448 O << Name;
1449 O << Suffix;
1450 if (Name[0]=='\"')
1451 O << '\"';
Dale Johannesen5bf742f2008-05-19 21:38:18 +00001452}
Evan Cheng53495222008-07-08 00:55:58 +00001453
Evan Cheng0a1e6722008-07-08 16:40:43 +00001454void AsmPrinter::printSuffixedName(const std::string &Name, const char* Suffix) {
Evan Cheng53495222008-07-08 00:55:58 +00001455 printSuffixedName(Name.c_str(), Suffix);
1456}
Anton Korobeynikoved473292008-08-08 18:25:07 +00001457
1458void AsmPrinter::printVisibility(const std::string& Name,
1459 unsigned Visibility) const {
1460 if (Visibility == GlobalValue::HiddenVisibility) {
1461 if (const char *Directive = TAI->getHiddenDirective())
1462 O << Directive << Name << '\n';
1463 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1464 if (const char *Directive = TAI->getProtectedDirective())
1465 O << Directive << Name << '\n';
1466 }
1467}
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001468
1469GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(Collector *C) {
1470 if (!C->usesMetadata())
1471 return 0;
1472
1473 gcp_iterator GCPI = GCMetadataPrinters.find(C);
1474 if (GCPI != GCMetadataPrinters.end())
1475 return GCPI->second;
1476
1477 const char *Name = C->getName().c_str();
1478
1479 for (GCMetadataPrinterRegistry::iterator
1480 I = GCMetadataPrinterRegistry::begin(),
1481 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1482 if (strcmp(Name, I->getName()) == 0) {
1483 GCMetadataPrinter *GCP = I->instantiate();
1484 GCP->Coll = C;
1485 GCMetadataPrinters.insert(std::make_pair(C, GCP));
1486 return GCP;
1487 }
1488
1489 cerr << "no GCMetadataPrinter registered for collector: " << Name << "\n";
1490 abort();
1491}