blob: dc88da954ab60af0c0909cdbebbc9efe974a1560 [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)
Evan Cheng0e7b00d2008-03-15 00:03:38 +000040 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o),
41 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
108
Gordon Henriksen5180e852008-01-07 01:30:38 +0000109void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
110 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksend930f912008-08-17 18:44:35 +0000111 AU.addRequired<GCModuleInfo>();
Gordon Henriksen5180e852008-01-07 01:30:38 +0000112}
113
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000114bool AsmPrinter::doInitialization(Module &M) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000115 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +0000116
Gordon Henriksend930f912008-08-17 18:44:35 +0000117 GCModuleInfo *MI = getAnalysisToUpdate<GCModuleInfo>();
118 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
119 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
120 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
121 MP->beginAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000122
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000123 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000124 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000125 << M.getModuleInlineAsm()
Dan Gohman6896901e2008-06-30 22:03:41 +0000126 << '\n' << TAI->getCommentString()
Jim Laskeya6211dc2006-09-06 18:34:40 +0000127 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000128
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000129 SwitchToDataSection(""); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000130
Evan Cheng2cb90682008-02-04 23:06:48 +0000131 MMI = getAnalysisToUpdate<MachineModuleInfo>();
132 if (MMI) MMI->AnalyzeModule(M);
Jim Laskey0bbdc552006-01-26 20:21:46 +0000133
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000134 return false;
135}
136
137bool AsmPrinter::doFinalization(Module &M) {
Rafael Espindolad7998d02006-12-18 03:37:18 +0000138 if (TAI->getWeakRefDirective()) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000139 if (!ExtWeakSymbols.empty())
Rafael Espindolad7998d02006-12-18 03:37:18 +0000140 SwitchToDataSection("");
141
142 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
143 e = ExtWeakSymbols.end(); i != e; ++i) {
144 const GlobalValue *GV = *i;
145 std::string Name = Mang->getValueName(GV);
Dan Gohman6896901e2008-06-30 22:03:41 +0000146 O << TAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolad7998d02006-12-18 03:37:18 +0000147 }
148 }
149
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000150 if (TAI->getSetDirective()) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000151 if (!M.alias_empty())
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000152 SwitchToTextSection(TAI->getTextSection());
153
Dan Gohman6896901e2008-06-30 22:03:41 +0000154 O << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000155 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
156 I!=E; ++I) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000157 std::string Name = Mang->getValueName(I);
158 std::string Target;
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000159
160 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
161 Target = Mang->getValueName(GV);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000162
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000163 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000164 O << "\t.globl\t" << Name << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000165 else if (I->hasWeakLinkage())
Dan Gohman6896901e2008-06-30 22:03:41 +0000166 O << TAI->getWeakRefDirective() << Name << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000167 else if (!I->hasInternalLinkage())
168 assert(0 && "Invalid alias linkage");
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000169
170 if (I->hasHiddenVisibility()) {
171 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000172 O << Directive << Name << '\n';
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000173 } else if (I->hasProtectedVisibility()) {
174 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000175 O << Directive << Name << '\n';
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000176 }
177
Dan Gohman6896901e2008-06-30 22:03:41 +0000178 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000179
180 // If the aliasee has external weak linkage it can be referenced only by
181 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
182 // weak reference in such case.
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000183 if (GV->hasExternalWeakLinkage()) {
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000184 if (TAI->getWeakRefDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000185 O << TAI->getWeakRefDirective() << Target << '\n';
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000186 else
Dan Gohman6896901e2008-06-30 22:03:41 +0000187 O << "\t.globl\t" << Target << '\n';
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000188 }
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000189 }
190 }
191
Gordon Henriksend930f912008-08-17 18:44:35 +0000192 GCModuleInfo *MI = getAnalysisToUpdate<GCModuleInfo>();
193 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
194 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
195 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
196 MP->finishAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000197
Dan Gohmanbcde1722008-05-05 00:28:39 +0000198 // If we don't have any trampolines, then we don't require stack memory
199 // to be executable. Some targets have a directive to declare this.
200 Function* InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
201 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
202 if (TAI->getNonexecutableStackDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000203 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmanbcde1722008-05-05 00:28:39 +0000204
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000205 delete Mang; Mang = 0;
206 return false;
207}
208
Bill Wendlinge8c885f2007-09-18 09:10:16 +0000209std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
Bill Wendling067f1d82007-09-18 01:47:22 +0000210 assert(MF && "No machine function?");
Dale Johannesen49155772008-04-02 20:10:52 +0000211 std::string Name = MF->getFunction()->getName();
212 if (Name.empty())
213 Name = Mang->getValueName(MF->getFunction());
214 return Mang->makeNameProper(Name + ".eh", TAI->getGlobalPrefix());
Bill Wendling067f1d82007-09-18 01:47:22 +0000215}
216
Chris Lattnerbb644e32005-11-21 07:51:36 +0000217void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000218 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000219 CurrentFnName = Mang->getValueName(MF.getFunction());
Evan Chengcdf36092007-10-14 05:57:21 +0000220 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000221}
222
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000223/// EmitConstantPool - Print to the current output stream assembly
224/// representations of the constants in the constant pool MCP. This is
225/// used to print out constants which have been "spilled to memory" by
226/// the code generator.
227///
228void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000229 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000230 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000231
232 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
233 // in special sections.
234 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
235 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
236 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
237 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000238 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000239 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000240 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng2ad050f2006-09-14 07:35:00 +0000241 const Type *Ty = CPE.getType();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000242 if (TAI->getFourByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000243 TM.getTargetData()->getABITypeSize(Ty) == 4)
Evan Chengec1d60b2006-06-29 00:26:09 +0000244 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000245 else if (TAI->getEightByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000246 TM.getTargetData()->getABITypeSize(Ty) == 8)
Evan Chengec1d60b2006-06-29 00:26:09 +0000247 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000248 else if (TAI->getSixteenByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000249 TM.getTargetData()->getABITypeSize(Ty) == 16)
Evan Chengec1d60b2006-06-29 00:26:09 +0000250 SixteenByteCPs.push_back(std::make_pair(CPE, i));
251 else
252 OtherCPs.push_back(std::make_pair(CPE, i));
253 }
254
255 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000256 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
257 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
258 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
259 SixteenByteCPs);
260 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Chengec1d60b2006-06-29 00:26:09 +0000261}
262
263void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
264 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
265 if (CP.empty()) return;
266
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000267 SwitchToDataSection(Section);
Evan Chengec1d60b2006-06-29 00:26:09 +0000268 EmitAlignment(Alignment);
269 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengcdf36092007-10-14 05:57:21 +0000270 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Owen Anderson93719642008-08-21 00:14:44 +0000271 << CP[i].second << ":\t\t\t\t\t";
272 // O << TAI->getCommentString() << ' ' <<
273 // WriteTypeSymbolic(O, CP[i].first.getType(), 0);
Chris Lattner5d2a9a42008-08-19 04:44:30 +0000274 O << '\n';
Evan Cheng2ad050f2006-09-14 07:35:00 +0000275 if (CP[i].first.isMachineConstantPoolEntry())
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000276 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000277 else
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000278 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000279 if (i != e-1) {
Evan Cheng2ad050f2006-09-14 07:35:00 +0000280 const Type *Ty = CP[i].first.getType();
Evan Chengec1d60b2006-06-29 00:26:09 +0000281 unsigned EntSize =
Duncan Sands283207a2007-11-05 00:04:43 +0000282 TM.getTargetData()->getABITypeSize(Ty);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000283 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000284 // Emit inter-object padding for alignment.
Evan Cheng2ad050f2006-09-14 07:35:00 +0000285 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000286 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000287 }
288}
289
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000290/// EmitJumpTableInfo - Print assembly representations of the jump tables used
291/// by the current function to the current output stream.
292///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000293void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
294 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000295 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
296 if (JT.empty()) return;
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000297
Jim Laskey70323a82006-12-14 19:17:33 +0000298 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begemanefc312a2006-07-27 16:46:58 +0000299
Nate Begeman78756502006-07-27 01:13:04 +0000300 // Pick the directive to use to print the jump table entries, and switch to
301 // the appropriate section.
Jim Laskey70323a82006-12-14 19:17:33 +0000302 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000303
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000304 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
305 const Function *F = MF.getFunction();
306 unsigned SectionFlags = TAI->SectionFlagsForGlobal(F);
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000307 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000308 !JumpTableDataSection ||
309 SectionFlags & SectionFlags::Linkonce) {
Jim Laskey70323a82006-12-14 19:17:33 +0000310 // In PIC mode, we need to emit the jump table to the same section as the
311 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000312 // We should also do if the section name is NULL or function is declared in
313 // discardable section.
Jim Laskey70323a82006-12-14 19:17:33 +0000314 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman78756502006-07-27 01:13:04 +0000315 } else {
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000316 SwitchToDataSection(JumpTableDataSection);
Nate Begeman78756502006-07-27 01:13:04 +0000317 }
Jim Laskey70323a82006-12-14 19:17:33 +0000318
319 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000320
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000321 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000322 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner28bfe382006-10-28 18:10:06 +0000323
324 // If this jump table was deleted, ignore it.
325 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-08-12 21:29:52 +0000326
327 // For PIC codegen, if possible we want to use the SetDirective to reduce
328 // the number of relocations the assembler will generate for the jump table.
329 // Set directives are all printed before the jump table itself.
Evan Cheng797d56f2007-11-09 01:32:10 +0000330 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Jim Laskey70323a82006-12-14 19:17:33 +0000331 if (TAI->getSetDirective() && IsPic)
Nate Begeman984c1a42006-08-12 21:29:52 +0000332 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng797d56f2007-11-09 01:32:10 +0000333 if (EmittedSets.insert(JTBBs[ii]))
334 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman984c1a42006-08-12 21:29:52 +0000335
Chris Lattner0ee2d462007-01-18 01:12:56 +0000336 // On some targets (e.g. darwin) we want to emit two consequtive labels
337 // before each jump table. The first label is never referenced, but tells
338 // the assembler and linker the extents of the jump table object. The
339 // second label is actually referenced by the code.
340 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
Evan Chengcdf36092007-10-14 05:57:21 +0000341 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Chris Lattner0ee2d462007-01-18 01:12:56 +0000342
Evan Chengcdf36092007-10-14 05:57:21 +0000343 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
344 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000345
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000346 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000347 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000348 O << '\n';
349 }
350 }
351}
352
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000353void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
354 const MachineBasicBlock *MBB,
355 unsigned uid) const {
356 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
357
358 // Use JumpTableDirective otherwise honor the entry size from the jump table
359 // info.
360 const char *JTEntryDirective = TAI->getJumpTableDirective();
361 bool HadJTEntryDirective = JTEntryDirective != NULL;
362 if (!HadJTEntryDirective) {
363 JTEntryDirective = MJTI->getEntrySize() == 4 ?
364 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
365 }
366
367 O << JTEntryDirective << ' ';
368
369 // If we have emitted set directives for the jump table entries, print
370 // them rather than the entries themselves. If we're emitting PIC, then
371 // emit the table entries as differences between two text section labels.
372 // If we're emitting non-PIC code, then emit the entries as direct
373 // references to the target basic blocks.
374 if (IsPic) {
375 if (TAI->getSetDirective()) {
376 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
377 << '_' << uid << "_set_" << MBB->getNumber();
378 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000379 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000380 // If the arch uses custom Jump Table directives, don't calc relative to
381 // JT
382 if (!HadJTEntryDirective)
383 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
384 << getFunctionNumber() << '_' << uid;
385 }
386 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000387 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000388 }
389}
390
391
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000392/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
393/// special global used by LLVM. If so, emit it and return true, otherwise
394/// do nothing and return false.
395bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Andrew Lenharthbeb80a92007-08-22 19:33:11 +0000396 if (GV->getName() == "llvm.used") {
397 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
398 EmitLLVMUsedList(GV->getInitializer());
399 return true;
400 }
401
Jim Laskey313570f2006-03-07 22:00:35 +0000402 // Ignore debug and non-emitted data.
403 if (GV->getSection() == "llvm.metadata") return true;
404
405 if (!GV->hasAppendingLinkage()) return false;
406
407 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000408
Evan Cheng53ce7de2007-06-04 20:39:18 +0000409 const TargetData *TD = TM.getTargetData();
410 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner3760e902006-01-12 19:17:23 +0000411 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000412 SwitchToDataSection(TAI->getStaticCtorsSection());
Evan Cheng53ce7de2007-06-04 20:39:18 +0000413 EmitAlignment(Align, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000414 EmitXXStructorList(GV->getInitializer());
415 return true;
416 }
417
Chris Lattner3760e902006-01-12 19:17:23 +0000418 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000419 SwitchToDataSection(TAI->getStaticDtorsSection());
Evan Cheng53ce7de2007-06-04 20:39:18 +0000420 EmitAlignment(Align, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000421 EmitXXStructorList(GV->getInitializer());
422 return true;
423 }
424
425 return false;
426}
427
Chris Lattner66af3902006-09-26 03:38:18 +0000428/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
429/// global in the specified llvm.used list as being used with this directive.
430void AsmPrinter::EmitLLVMUsedList(Constant *List) {
431 const char *Directive = TAI->getUsedDirective();
432
433 // Should be an array of 'sbyte*'.
434 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
435 if (InitList == 0) return;
436
437 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
438 O << Directive;
439 EmitConstantValueOnly(InitList->getOperand(i));
Dan Gohman6896901e2008-06-30 22:03:41 +0000440 O << '\n';
Chris Lattner66af3902006-09-26 03:38:18 +0000441 }
442}
443
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000444/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
445/// function pointers, ignoring the init priority.
446void AsmPrinter::EmitXXStructorList(Constant *List) {
447 // Should be an array of '{ int, void ()* }' structs. The first value is the
448 // init priority, which we ignore.
449 if (!isa<ConstantArray>(List)) return;
450 ConstantArray *InitList = cast<ConstantArray>(List);
451 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
452 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
453 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000454
455 if (CS->getOperand(1)->isNullValue())
456 return; // Found a null terminator, exit printing.
457 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000458 EmitGlobalConstant(CS->getOperand(1));
459 }
460}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000461
Jim Laskey71262542006-10-17 13:41:07 +0000462/// getGlobalLinkName - Returns the asm/link name of of the specified
463/// global variable. Should be overridden by each target asm printer to
464/// generate the appropriate value.
Jim Laskeyd24b9132006-10-17 17:17:24 +0000465const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
466 std::string LinkName;
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000467
468 if (isa<Function>(GV)) {
469 LinkName += TAI->getFunctionAddrPrefix();
470 LinkName += Mang->getValueName(GV);
471 LinkName += TAI->getFunctionAddrSuffix();
472 } else {
473 LinkName += TAI->getGlobalVarAddrPrefix();
474 LinkName += Mang->getValueName(GV);
475 LinkName += TAI->getGlobalVarAddrSuffix();
476 }
477
Jim Laskeyd24b9132006-10-17 17:17:24 +0000478 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000479}
480
Jim Laskey18fc0972007-02-21 22:47:38 +0000481/// EmitExternalGlobal - Emit the external reference to a global variable.
482/// Should be overridden if an indirect reference should be used.
483void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
484 O << getGlobalLinkName(GV);
485}
486
487
488
Jim Laskey1c055e82007-01-25 15:12:02 +0000489//===----------------------------------------------------------------------===//
490/// LEB 128 number encoding.
491
492/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
493/// representing an unsigned leb128 value.
494void AsmPrinter::PrintULEB128(unsigned Value) const {
495 do {
496 unsigned Byte = Value & 0x7f;
497 Value >>= 7;
498 if (Value) Byte |= 0x80;
Owen Anderson93719642008-08-21 00:14:44 +0000499 O << "0x" << utohexstr(Byte);
Jim Laskey1c055e82007-01-25 15:12:02 +0000500 if (Value) O << ", ";
501 } while (Value);
502}
503
Jim Laskey1c055e82007-01-25 15:12:02 +0000504/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
505/// representing a signed leb128 value.
506void AsmPrinter::PrintSLEB128(int Value) const {
507 int Sign = Value >> (8 * sizeof(Value) - 1);
508 bool IsMore;
Anton Korobeynikovbd890b12008-08-16 12:57:46 +0000509
Jim Laskey1c055e82007-01-25 15:12:02 +0000510 do {
511 unsigned Byte = Value & 0x7f;
512 Value >>= 7;
513 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
514 if (IsMore) Byte |= 0x80;
Owen Anderson93719642008-08-21 00:14:44 +0000515 O << "0x" << utohexstr(Byte);
Jim Laskey1c055e82007-01-25 15:12:02 +0000516 if (IsMore) O << ", ";
517 } while (IsMore);
518}
519
Jim Laskey1c055e82007-01-25 15:12:02 +0000520//===--------------------------------------------------------------------===//
521// Emission and print routines
522//
523
524/// PrintHex - Print a value as a hexidecimal value.
525///
526void AsmPrinter::PrintHex(int Value) const {
Owen Anderson93719642008-08-21 00:14:44 +0000527 O << "0x" << utohexstr(static_cast<unsigned>(Value));
Jim Laskey1c055e82007-01-25 15:12:02 +0000528}
529
530/// EOL - Print a newline character to asm stream. If a comment is present
531/// then it will be printed first. Comments should not contain '\n'.
Jim Laskey18fc0972007-02-21 22:47:38 +0000532void AsmPrinter::EOL() const {
Dan Gohman6896901e2008-06-30 22:03:41 +0000533 O << '\n';
Jim Laskey18fc0972007-02-21 22:47:38 +0000534}
Owen Anderson1d952532008-07-01 21:16:27 +0000535
Jim Laskey1c055e82007-01-25 15:12:02 +0000536void AsmPrinter::EOL(const std::string &Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000537 if (VerboseAsm && !Comment.empty()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000538 O << '\t'
Jim Laskey1c055e82007-01-25 15:12:02 +0000539 << TAI->getCommentString()
Dan Gohman6896901e2008-06-30 22:03:41 +0000540 << ' '
Jim Laskey1c055e82007-01-25 15:12:02 +0000541 << Comment;
542 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000543 O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000544}
545
Owen Anderson1d952532008-07-01 21:16:27 +0000546void AsmPrinter::EOL(const char* Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000547 if (VerboseAsm && *Comment) {
Owen Anderson1d952532008-07-01 21:16:27 +0000548 O << '\t'
549 << TAI->getCommentString()
550 << ' '
551 << Comment;
552 }
553 O << '\n';
554}
555
Jim Laskey1c055e82007-01-25 15:12:02 +0000556/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
557/// unsigned leb128 value.
558void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
559 if (TAI->hasLEB128()) {
560 O << "\t.uleb128\t"
561 << Value;
562 } else {
563 O << TAI->getData8bitsDirective();
564 PrintULEB128(Value);
565 }
566}
567
568/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
569/// signed leb128 value.
570void AsmPrinter::EmitSLEB128Bytes(int Value) const {
571 if (TAI->hasLEB128()) {
572 O << "\t.sleb128\t"
573 << Value;
574 } else {
575 O << TAI->getData8bitsDirective();
576 PrintSLEB128(Value);
577 }
578}
579
580/// EmitInt8 - Emit a byte directive and value.
581///
582void AsmPrinter::EmitInt8(int Value) const {
583 O << TAI->getData8bitsDirective();
584 PrintHex(Value & 0xFF);
585}
586
587/// EmitInt16 - Emit a short directive and value.
588///
589void AsmPrinter::EmitInt16(int Value) const {
590 O << TAI->getData16bitsDirective();
591 PrintHex(Value & 0xFFFF);
592}
593
594/// EmitInt32 - Emit a long directive and value.
595///
596void AsmPrinter::EmitInt32(int Value) const {
597 O << TAI->getData32bitsDirective();
598 PrintHex(Value);
599}
600
601/// EmitInt64 - Emit a long long directive and value.
602///
603void AsmPrinter::EmitInt64(uint64_t Value) const {
604 if (TAI->getData64bitsDirective()) {
605 O << TAI->getData64bitsDirective();
606 PrintHex(Value);
607 } else {
608 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000609 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000610 EmitInt32(unsigned(Value));
611 } else {
Dan Gohman6896901e2008-06-30 22:03:41 +0000612 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000613 EmitInt32(unsigned(Value >> 32));
614 }
615 }
616}
617
618/// toOctal - Convert the low order bits of X into an octal digit.
619///
620static inline char toOctal(int X) {
621 return (X&7)+'0';
622}
623
624/// printStringChar - Print a char, escaped if necessary.
625///
Owen Anderson93719642008-08-21 00:14:44 +0000626static void printStringChar(raw_ostream &O, char C) {
Jim Laskey1c055e82007-01-25 15:12:02 +0000627 if (C == '"') {
628 O << "\\\"";
629 } else if (C == '\\') {
630 O << "\\\\";
631 } else if (isprint(C)) {
632 O << C;
633 } else {
634 switch(C) {
635 case '\b': O << "\\b"; break;
636 case '\f': O << "\\f"; break;
637 case '\n': O << "\\n"; break;
638 case '\r': O << "\\r"; break;
639 case '\t': O << "\\t"; break;
640 default:
641 O << '\\';
642 O << toOctal(C >> 6);
643 O << toOctal(C >> 3);
644 O << toOctal(C >> 0);
645 break;
646 }
647 }
648}
649
650/// EmitString - Emit a string with quotes and a null terminator.
651/// Special characters are emitted properly.
652/// \literal (Eg. '\t') \endliteral
653void AsmPrinter::EmitString(const std::string &String) const {
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000654 const char* AscizDirective = TAI->getAscizDirective();
655 if (AscizDirective)
656 O << AscizDirective;
657 else
658 O << TAI->getAsciiDirective();
Dan Gohman6896901e2008-06-30 22:03:41 +0000659 O << '\"';
Jim Laskey1c055e82007-01-25 15:12:02 +0000660 for (unsigned i = 0, N = String.size(); i < N; ++i) {
661 unsigned char C = String[i];
662 printStringChar(O, C);
663 }
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000664 if (AscizDirective)
Dan Gohman6896901e2008-06-30 22:03:41 +0000665 O << '\"';
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000666 else
667 O << "\\0\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000668}
669
670
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000671/// EmitFile - Emit a .file directive.
672void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
673 O << "\t.file\t" << Number << " \"";
674 for (unsigned i = 0, N = Name.size(); i < N; ++i) {
675 unsigned char C = Name[i];
676 printStringChar(O, C);
677 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000678 O << '\"';
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000679}
680
681
Jim Laskey1c055e82007-01-25 15:12:02 +0000682//===----------------------------------------------------------------------===//
683
Chris Lattner3e3ff302007-05-31 18:57:45 +0000684// EmitAlignment - Emit an alignment directive to the specified power of
685// two boundary. For example, if you pass in 3 here, you will get an 8
686// byte alignment. If a global value is specified, and if that global has
687// an explicit alignment requested, it will unconditionally override the
688// alignment request. However, if ForcedAlignBits is specified, this value
689// has final say: the ultimate alignment will be the max of ForcedAlignBits
690// and the alignment computed with NumBits and the global.
691//
692// The algorithm is:
693// Align = NumBits;
694// if (GV && GV->hasalignment) Align = GV->getalignment();
695// Align = std::max(Align, ForcedAlignBits);
696//
697void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng26edb592008-02-29 19:36:59 +0000698 unsigned ForcedAlignBits,
699 bool UseFillExpr) const {
Dale Johannesen8653d292007-04-23 23:33:31 +0000700 if (GV && GV->getAlignment())
Chris Lattner3e3ff302007-05-31 18:57:45 +0000701 NumBits = Log2_32(GV->getAlignment());
702 NumBits = std::max(NumBits, ForcedAlignBits);
703
Chris Lattner747960d2005-11-10 18:09:27 +0000704 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000705 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng86eb3fd2007-07-25 23:35:07 +0000706 O << TAI->getAlignDirective() << NumBits;
Evan Chengc7990652008-02-28 00:43:03 +0000707
708 unsigned FillValue = TAI->getTextAlignFillValue();
Evan Cheng26edb592008-02-29 19:36:59 +0000709 UseFillExpr &= IsInTextSection && FillValue;
Owen Anderson93719642008-08-21 00:14:44 +0000710 if (UseFillExpr) O << ",0x" << utohexstr(FillValue);
Dan Gohman6896901e2008-06-30 22:03:41 +0000711 O << '\n';
Chris Lattner1d35c162004-08-17 19:14:29 +0000712}
713
Jim Laskey18fc0972007-02-21 22:47:38 +0000714
Chris Lattnerbb644e32005-11-21 07:51:36 +0000715/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000716///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000717void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000718 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000719 if (TAI->getZeroDirective()) {
720 O << TAI->getZeroDirective() << NumZeros;
721 if (TAI->getZeroDirectiveSuffix())
722 O << TAI->getZeroDirectiveSuffix();
Dan Gohman6896901e2008-06-30 22:03:41 +0000723 O << '\n';
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000724 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000725 for (; NumZeros; --NumZeros)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000726 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000727 }
728 }
729}
730
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000731// Print out the specified constant, without a storage class. Only the
732// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000733void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000734 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman6896901e2008-06-30 22:03:41 +0000735 O << '0';
Zhou Sheng75b871f2007-01-11 12:24:14 +0000736 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel499c1192008-06-03 06:18:19 +0000737 O << CI->getZExtValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000738 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000739 // This is a constant address for a global variable or function. Use the
740 // name of the variable or function as the address value, possibly
741 // decorating it with GlobalVarAddrPrefix/Suffix or
742 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000743 if (isa<Function>(GV)) {
744 O << TAI->getFunctionAddrPrefix()
745 << Mang->getValueName(GV)
746 << TAI->getFunctionAddrSuffix();
747 } else {
748 O << TAI->getGlobalVarAddrPrefix()
749 << Mang->getValueName(GV)
750 << TAI->getGlobalVarAddrSuffix();
751 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000752 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000753 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000754 unsigned Opcode = CE->getOpcode();
755 switch (Opcode) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000756 case Instruction::GetElementPtr: {
757 // generate a symbolic expression for the byte address
758 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner83dfca82007-02-10 20:31:59 +0000759 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
760 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
761 idxVec.size())) {
Chris Lattner145569b2005-02-14 21:40:26 +0000762 if (Offset)
Dan Gohman6896901e2008-06-30 22:03:41 +0000763 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000764 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000765 if (Offset > 0)
766 O << ") + " << Offset;
767 else if (Offset < 0)
768 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000769 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000770 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000771 }
772 break;
773 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000774 case Instruction::Trunc:
775 case Instruction::ZExt:
776 case Instruction::SExt:
777 case Instruction::FPTrunc:
778 case Instruction::FPExt:
779 case Instruction::UIToFP:
780 case Instruction::SIToFP:
781 case Instruction::FPToUI:
782 case Instruction::FPToSI:
783 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
784 break;
Chris Lattnerb9e41f52006-12-12 05:14:13 +0000785 case Instruction::BitCast:
786 return EmitConstantValueOnly(CE->getOperand(0));
787
Chris Lattner0c537da2006-12-12 05:18:19 +0000788 case Instruction::IntToPtr: {
789 // Handle casts to pointers by changing them into casts to the appropriate
790 // integer type. This promotes constant folding and simplifies this code.
791 Constant *Op = CE->getOperand(0);
792 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
793 return EmitConstantValueOnly(Op);
794 }
795
796
797 case Instruction::PtrToInt: {
Chris Lattner85492422006-07-29 01:57:19 +0000798 // Support only foldable casts to/from pointers that can be eliminated by
799 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000800 Constant *Op = CE->getOperand(0);
Chris Lattner0c537da2006-12-12 05:18:19 +0000801 const Type *Ty = CE->getType();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000802
Chris Lattner0c537da2006-12-12 05:18:19 +0000803 // We can emit the pointer value into this slot if the slot is an
804 // integer slot greater or equal to the size of the pointer.
Nick Lewycky42a19b62008-08-08 06:34:07 +0000805 if (TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
Chris Lattner85492422006-07-29 01:57:19 +0000806 return EmitConstantValueOnly(Op);
Nick Lewycky42a19b62008-08-08 06:34:07 +0000807
808 O << "((";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000809 EmitConstantValueOnly(Op);
Nick Lewycky42a19b62008-08-08 06:34:07 +0000810 APInt ptrMask = APInt::getAllOnesValue(TD->getABITypeSizeInBits(Ty));
Chris Lattner17f71652008-08-17 07:19:36 +0000811
812 SmallString<40> S;
813 ptrMask.toStringUnsigned(S);
814 O << ") & " << S.c_str() << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000815 break;
816 }
817 case Instruction::Add:
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000818 case Instruction::Sub:
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000819 case Instruction::And:
820 case Instruction::Or:
821 case Instruction::Xor:
Dan Gohman6896901e2008-06-30 22:03:41 +0000822 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000823 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman6896901e2008-06-30 22:03:41 +0000824 O << ')';
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000825 switch (Opcode) {
826 case Instruction::Add:
827 O << " + ";
828 break;
829 case Instruction::Sub:
830 O << " - ";
831 break;
832 case Instruction::And:
833 O << " & ";
834 break;
835 case Instruction::Or:
836 O << " | ";
837 break;
838 case Instruction::Xor:
839 O << " ^ ";
840 break;
841 default:
842 break;
843 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000844 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000845 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman6896901e2008-06-30 22:03:41 +0000846 O << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000847 break;
848 default:
849 assert(0 && "Unsupported operator!");
850 }
851 } else {
852 assert(0 && "Unknown constant value!");
853 }
854}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000855
Chris Lattner55a6d902005-11-10 18:06:33 +0000856/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000857/// the predicate isString is true.
858///
Owen Anderson93719642008-08-21 00:14:44 +0000859static void printAsCString(raw_ostream &O, const ConstantArray *CVA,
Chris Lattner55a6d902005-11-10 18:06:33 +0000860 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000861 assert(CVA->isString() && "Array is not string compatible!");
862
Dan Gohman6896901e2008-06-30 22:03:41 +0000863 O << '\"';
Chris Lattner55a6d902005-11-10 18:06:33 +0000864 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000865 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000866 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskey1c055e82007-01-25 15:12:02 +0000867 printStringChar(O, C);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000868 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000869 O << '\"';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000870}
871
Jeff Cohen24a62a92006-05-02 01:16:28 +0000872/// EmitString - Emit a zero-byte-terminated string constant.
873///
874void AsmPrinter::EmitString(const ConstantArray *CVA) const {
875 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000876 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000877 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000878 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000879 printAsCString(O, CVA, NumElts-1);
880 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000881 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000882 printAsCString(O, CVA, NumElts);
883 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000884 O << '\n';
Jeff Cohen24a62a92006-05-02 01:16:28 +0000885}
886
Chris Lattnerbb644e32005-11-21 07:51:36 +0000887/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000888void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000889 const TargetData *TD = TM.getTargetData();
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000890 unsigned Size = TD->getABITypeSize(CV->getType());
Chris Lattner8452a1f2004-08-17 06:36:49 +0000891
Chris Lattner61753bf2004-10-16 18:19:26 +0000892 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Duncan Sands283207a2007-11-05 00:04:43 +0000893 EmitZeros(Size);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000894 return;
895 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
896 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000897 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000898 } else { // Not a string. Print the values in successive locations
Duncan Sands283207a2007-11-05 00:04:43 +0000899 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000900 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000901 }
902 return;
903 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
904 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000905 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000906 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000907 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
908 const Constant* field = CVS->getOperand(i);
909
910 // Check if padding is needed and insert one or more 0s.
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000911 uint64_t fieldSize = TD->getABITypeSize(field->getType());
Duncan Sandsf7ae8bd2007-11-05 18:03:02 +0000912 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
Chris Lattnerc473d8e2007-02-10 19:55:17 +0000913 - cvsLayout->getElementOffset(i)) - fieldSize;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000914 sizeSoFar += fieldSize + padSize;
915
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000916 // Now print the actual field value.
917 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000918
Duncan Sandsf7ae8bd2007-11-05 18:03:02 +0000919 // Insert padding - this may include padding to increase the size of the
920 // current field up to the ABI size (if the struct is not packed) as well
921 // as padding to ensure that the next field starts at the right offset.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000922 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000923 }
Chris Lattnerb84892d2007-02-10 19:59:22 +0000924 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
Chris Lattner8452a1f2004-08-17 06:36:49 +0000925 "Layout of constant struct may be incorrect!");
926 return;
927 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
928 // FP Constants are printed as integer constants to avoid losing
929 // precision...
Chris Lattner8452a1f2004-08-17 06:36:49 +0000930 if (CFP->getType() == Type::DoubleTy) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000931 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
Dale Johannesen028084e2007-09-12 03:30:33 +0000932 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000933 if (TAI->getData64bitsDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000934 O << TAI->getData64bitsDirective() << i << '\t'
935 << TAI->getCommentString() << " double value: " << Val << '\n';
Owen Anderson20a631f2006-05-03 01:29:57 +0000936 else if (TD->isBigEndian()) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000937 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +0000938 << '\t' << TAI->getCommentString()
939 << " double most significant word " << Val << '\n';
Dale Johannesen245dceb2007-09-11 18:32:33 +0000940 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman6896901e2008-06-30 22:03:41 +0000941 << '\t' << TAI->getCommentString()
942 << " double least significant word " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000943 } else {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000944 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman6896901e2008-06-30 22:03:41 +0000945 << '\t' << TAI->getCommentString()
946 << " double least significant word " << Val << '\n';
Dale Johannesen245dceb2007-09-11 18:32:33 +0000947 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +0000948 << '\t' << TAI->getCommentString()
949 << " double most significant word " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000950 }
951 return;
Dale Johannesen028084e2007-09-12 03:30:33 +0000952 } else if (CFP->getType() == Type::FloatTy) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000953 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
954 O << TAI->getData32bitsDirective()
Dale Johannesen028084e2007-09-12 03:30:33 +0000955 << CFP->getValueAPF().convertToAPInt().getZExtValue()
Dan Gohman6896901e2008-06-30 22:03:41 +0000956 << '\t' << TAI->getCommentString() << " float " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000957 return;
Dale Johannesen028084e2007-09-12 03:30:33 +0000958 } else if (CFP->getType() == Type::X86_FP80Ty) {
959 // all long double variants are printed as hex
Dale Johannesen34aa41c2007-09-26 23:20:33 +0000960 // api needed to prevent premature destruction
961 APInt api = CFP->getValueAPF().convertToAPInt();
962 const uint64_t *p = api.getRawData();
Chris Lattnerf1a6c9f2008-01-27 06:09:28 +0000963 APFloat DoubleVal = CFP->getValueAPF();
964 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven);
Dale Johannesen028084e2007-09-12 03:30:33 +0000965 if (TD->isBigEndian()) {
966 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
Dan Gohman6896901e2008-06-30 22:03:41 +0000967 << '\t' << TAI->getCommentString()
Chris Lattnerf1a6c9f2008-01-27 06:09:28 +0000968 << " long double most significant halfword of ~"
Dan Gohman6896901e2008-06-30 22:03:41 +0000969 << DoubleVal.convertToDouble() << '\n';
Dale Johannesen028084e2007-09-12 03:30:33 +0000970 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +0000971 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000972 << " long double next halfword\n";
973 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
Dan Gohman6896901e2008-06-30 22:03:41 +0000974 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000975 << " long double next halfword\n";
976 O << TAI->getData16bitsDirective() << uint16_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +0000977 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000978 << " long double next halfword\n";
979 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +0000980 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000981 << " long double least significant halfword\n";
982 } else {
983 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +0000984 << '\t' << TAI->getCommentString()
Chris Lattnerf1a6c9f2008-01-27 06:09:28 +0000985 << " long double least significant halfword of ~"
Dan Gohman6896901e2008-06-30 22:03:41 +0000986 << DoubleVal.convertToDouble() << '\n';
Dale Johannesen028084e2007-09-12 03:30:33 +0000987 O << TAI->getData16bitsDirective() << uint16_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +0000988 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000989 << " long double next halfword\n";
990 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
Dan Gohman6896901e2008-06-30 22:03:41 +0000991 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000992 << " long double next halfword\n";
993 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +0000994 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000995 << " long double next halfword\n";
996 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
Dan Gohman6896901e2008-06-30 22:03:41 +0000997 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000998 << " long double most significant halfword\n";
999 }
Duncan Sands283207a2007-11-05 00:04:43 +00001000 EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty));
Dale Johannesen028084e2007-09-12 03:30:33 +00001001 return;
Dale Johannesen6472eb62007-10-11 23:32:15 +00001002 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1003 // all long double variants are printed as hex
1004 // api needed to prevent premature destruction
1005 APInt api = CFP->getValueAPF().convertToAPInt();
1006 const uint64_t *p = api.getRawData();
1007 if (TD->isBigEndian()) {
1008 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001009 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001010 << " long double most significant word\n";
1011 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +00001012 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001013 << " long double next word\n";
1014 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001015 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001016 << " long double next word\n";
1017 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +00001018 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001019 << " long double least significant word\n";
1020 } else {
1021 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +00001022 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001023 << " long double least significant word\n";
1024 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001025 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001026 << " long double next word\n";
1027 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +00001028 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001029 << " long double next word\n";
1030 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001031 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001032 << " long double most significant word\n";
1033 }
1034 return;
Dale Johannesen028084e2007-09-12 03:30:33 +00001035 } else assert(0 && "Floating point constant type not handled");
Reid Spencere63b6512006-12-31 05:55:36 +00001036 } else if (CV->getType() == Type::Int64Ty) {
Chris Lattner8452a1f2004-08-17 06:36:49 +00001037 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00001038 uint64_t Val = CI->getZExtValue();
Misha Brukman835702a2005-04-21 22:36:52 +00001039
Jim Laskeya6211dc2006-09-06 18:34:40 +00001040 if (TAI->getData64bitsDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +00001041 O << TAI->getData64bitsDirective() << Val << '\n';
Owen Anderson20a631f2006-05-03 01:29:57 +00001042 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001043 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001044 << '\t' << TAI->getCommentString()
1045 << " Double-word most significant word " << Val << '\n';
Jim Laskeya6211dc2006-09-06 18:34:40 +00001046 O << TAI->getData32bitsDirective() << unsigned(Val)
Dan Gohman6896901e2008-06-30 22:03:41 +00001047 << '\t' << TAI->getCommentString()
1048 << " Double-word least significant word " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001049 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001050 O << TAI->getData32bitsDirective() << unsigned(Val)
Dan Gohman6896901e2008-06-30 22:03:41 +00001051 << '\t' << TAI->getCommentString()
1052 << " Double-word least significant word " << Val << '\n';
Jim Laskeya6211dc2006-09-06 18:34:40 +00001053 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001054 << '\t' << TAI->getCommentString()
1055 << " Double-word most significant word " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001056 }
1057 return;
1058 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001059 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1060 const VectorType *PTy = CP->getType();
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001061
1062 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
Duncan Sandsfc3c4892008-06-04 08:21:45 +00001063 EmitGlobalConstant(CP->getOperand(I));
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001064
1065 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +00001066 }
1067
1068 const Type *type = CV->getType();
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001069 printDataDirective(type);
Chris Lattnerbb644e32005-11-21 07:51:36 +00001070 EmitConstantValueOnly(CV);
Scott Michelc0e9ff62008-06-03 15:39:51 +00001071 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner17f71652008-08-17 07:19:36 +00001072 SmallString<40> S;
1073 CI->getValue().toStringUnsigned(S, 16);
1074 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
Scott Michelc0e9ff62008-06-03 15:39:51 +00001075 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001076 O << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001077}
Chris Lattner061d9e22006-01-27 02:10:10 +00001078
Chris Lattner17f71652008-08-17 07:19:36 +00001079void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001080 // Target doesn't support this yet!
1081 abort();
1082}
1083
Chris Lattnera32814b2006-09-26 23:59:50 +00001084/// PrintSpecial - Print information related to the specified machine instr
1085/// that is independent of the operand, and may be independent of the instr
1086/// itself. This can be useful for portably encoding the comment character
1087/// or other bits of target-specific knowledge into the asmstrings. The
1088/// syntax used is ${:comment}. Targets can override this to add support
1089/// for their own strange codes.
1090void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattner30b47082006-09-27 00:06:07 +00001091 if (!strcmp(Code, "private")) {
1092 O << TAI->getPrivateGlobalPrefix();
1093 } else if (!strcmp(Code, "comment")) {
Chris Lattnera32814b2006-09-26 23:59:50 +00001094 O << TAI->getCommentString();
1095 } else if (!strcmp(Code, "uid")) {
1096 // Assign a unique ID to this machine instruction.
1097 static const MachineInstr *LastMI = 0;
Chris Lattnera9428c42007-02-05 21:23:52 +00001098 static const Function *F = 0;
Chris Lattnera32814b2006-09-26 23:59:50 +00001099 static unsigned Counter = 0U-1;
Chris Lattnera9428c42007-02-05 21:23:52 +00001100
1101 // Comparing the address of MI isn't sufficient, because machineinstrs may
1102 // be allocated to the same address across functions.
1103 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1104
Chris Lattnera32814b2006-09-26 23:59:50 +00001105 // If this is a new machine instruction, bump the counter.
Chris Lattnera9428c42007-02-05 21:23:52 +00001106 if (LastMI != MI || F != ThisF) {
1107 ++Counter;
1108 LastMI = MI;
Chris Lattner89e66e02007-02-06 01:56:31 +00001109 F = ThisF;
Chris Lattnera9428c42007-02-05 21:23:52 +00001110 }
Chris Lattnera32814b2006-09-26 23:59:50 +00001111 O << Counter;
1112 } else {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001113 cerr << "Unknown special formatter '" << Code
1114 << "' for machine instr: " << *MI;
Chris Lattnera32814b2006-09-26 23:59:50 +00001115 exit(1);
1116 }
1117}
1118
1119
Chris Lattner061d9e22006-01-27 02:10:10 +00001120/// printInlineAsm - This method formats and prints the specified machine
1121/// instruction that is an inline asm.
1122void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +00001123 unsigned NumOperands = MI->getNumOperands();
1124
1125 // Count the number of register definitions.
1126 unsigned NumDefs = 0;
Dan Gohman9da02f52007-09-14 20:33:02 +00001127 for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef();
Chris Lattner45456d42006-09-05 20:02:51 +00001128 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +00001129 assert(NumDefs != NumOperands-1 && "No asm string?");
1130
1131 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001132
1133 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +00001134 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +00001135
Dale Johannesen2b3bc302008-01-29 02:21:21 +00001136 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1137 // These are useful to see where empty asm's wound up.
Chris Lattner3390cbe2006-07-28 00:17:20 +00001138 if (AsmStr[0] == 0) {
Dan Gohman6896901e2008-06-30 22:03:41 +00001139 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattner3390cbe2006-07-28 00:17:20 +00001140 return;
1141 }
1142
Jim Laskeya6211dc2006-09-06 18:34:40 +00001143 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +00001144
Bill Wendlinge21237e2007-01-16 03:42:04 +00001145 // The variant of the current asmprinter.
1146 int AsmPrinterVariant = TAI->getAssemblerDialect();
1147
Chris Lattneraa23fa92006-02-01 22:41:11 +00001148 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1149 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +00001150
Chris Lattneraa23fa92006-02-01 22:41:11 +00001151 while (*LastEmitted) {
1152 switch (*LastEmitted) {
1153 default: {
1154 // Not a special case, emit the string section literally.
1155 const char *LiteralEnd = LastEmitted+1;
1156 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +00001157 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +00001158 ++LiteralEnd;
1159 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1160 O.write(LastEmitted, LiteralEnd-LastEmitted);
1161 LastEmitted = LiteralEnd;
1162 break;
1163 }
Chris Lattnera633c3132006-05-05 21:47:05 +00001164 case '\n':
1165 ++LastEmitted; // Consume newline character.
Dan Gohman6896901e2008-06-30 22:03:41 +00001166 O << '\n'; // Indent code with newline.
Chris Lattnera633c3132006-05-05 21:47:05 +00001167 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001168 case '$': {
1169 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001170 bool Done = true;
1171
1172 // Handle escapes.
1173 switch (*LastEmitted) {
1174 default: Done = false; break;
1175 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +00001176 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1177 O << '$';
1178 ++LastEmitted; // Consume second '$' character.
1179 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001180 case '(': // $( -> same as GCC's { character.
1181 ++LastEmitted; // Consume '(' character.
1182 if (CurVariant != -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001183 cerr << "Nested variants found in inline asm string: '"
1184 << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001185 exit(1);
1186 }
1187 CurVariant = 0; // We're in the first variant now.
1188 break;
1189 case '|':
1190 ++LastEmitted; // consume '|' character.
1191 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001192 cerr << "Found '|' character outside of variant in inline asm "
1193 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001194 exit(1);
1195 }
1196 ++CurVariant; // We're in the next variant.
1197 break;
1198 case ')': // $) -> same as GCC's } char.
1199 ++LastEmitted; // consume ')' character.
1200 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001201 cerr << "Found '}' character outside of variant in inline asm "
1202 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001203 exit(1);
1204 }
1205 CurVariant = -1;
1206 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001207 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001208 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001209
1210 bool HasCurlyBraces = false;
1211 if (*LastEmitted == '{') { // ${variable}
1212 ++LastEmitted; // Consume '{' character.
1213 HasCurlyBraces = true;
1214 }
1215
1216 const char *IDStart = LastEmitted;
1217 char *IDEnd;
Chris Lattnerd39e3882007-01-23 00:36:17 +00001218 errno = 0;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001219 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1220 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001221 cerr << "Bad $ operand number in inline asm string: '"
1222 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001223 exit(1);
1224 }
1225 LastEmitted = IDEnd;
1226
Chris Lattner34f74c12006-02-06 22:17:23 +00001227 char Modifier[2] = { 0, 0 };
1228
Chris Lattneraa23fa92006-02-01 22:41:11 +00001229 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +00001230 // If we have curly braces, check for a modifier character. This
1231 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1232 if (*LastEmitted == ':') {
1233 ++LastEmitted; // Consume ':' character.
1234 if (*LastEmitted == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001235 cerr << "Bad ${:} expression in inline asm string: '"
1236 << AsmStr << "'\n";
Chris Lattner34f74c12006-02-06 22:17:23 +00001237 exit(1);
1238 }
1239
1240 Modifier[0] = *LastEmitted;
1241 ++LastEmitted; // Consume modifier character.
1242 }
1243
Chris Lattneraa23fa92006-02-01 22:41:11 +00001244 if (*LastEmitted != '}') {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001245 cerr << "Bad ${} expression in inline asm string: '"
1246 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001247 exit(1);
1248 }
1249 ++LastEmitted; // Consume '}' character.
1250 }
1251
1252 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001253 cerr << "Invalid $ operand number in inline asm string: '"
1254 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001255 exit(1);
1256 }
1257
Chris Lattner571d9642006-02-23 19:21:04 +00001258 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +00001259 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +00001260 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1261 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001262
1263 bool Error = false;
1264
Chris Lattner571d9642006-02-23 19:21:04 +00001265 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +00001266 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001267 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner81798412007-12-30 20:50:28 +00001268 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner5af3fde2006-02-24 19:50:58 +00001269 OpNo += (OpFlags >> 3) + 1;
1270 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001271
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001272 if (OpNo >= MI->getNumOperands()) {
1273 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +00001274 } else {
Chris Lattner81798412007-12-30 20:50:28 +00001275 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001276 ++OpNo; // Skip over the ID number.
1277
Dale Johannesen4646aa32007-11-05 21:20:28 +00001278 if (Modifier[0]=='l') // labels are target independent
Chris Lattnera5bb3702007-12-30 23:10:15 +00001279 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Chengc7990652008-02-28 00:43:03 +00001280 false, false, false);
Dale Johannesen4646aa32007-11-05 21:20:28 +00001281 else {
1282 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1283 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1284 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1285 Modifier[0] ? Modifier : 0);
1286 } else {
1287 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1288 Modifier[0] ? Modifier : 0);
1289 }
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001290 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001291 }
1292 if (Error) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001293 cerr << "Invalid operand found in inline asm: '"
1294 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001295 MI->dump();
1296 exit(1);
1297 }
Chris Lattner571d9642006-02-23 19:21:04 +00001298 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001299 break;
1300 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001301 }
1302 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001303 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattneraa23fa92006-02-01 22:41:11 +00001304}
1305
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001306/// printImplicitDef - This method prints the specified machine instruction
1307/// that is an implicit def.
1308void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Dan Gohman6896901e2008-06-30 22:03:41 +00001309 O << '\t' << TAI->getCommentString() << " implicit-def: "
1310 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001311}
1312
Jim Laskeyf9e54452007-01-26 14:34:52 +00001313/// printLabel - This method prints a local label used by debug and
1314/// exception handling tables.
1315void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohmanb58aff42008-07-01 00:16:26 +00001316 printLabel(MI->getOperand(0).getImm());
Jim Laskeyf9e54452007-01-26 14:34:52 +00001317}
1318
Evan Chengd6e44ab2008-02-01 09:10:45 +00001319void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng32e53472008-02-02 08:39:46 +00001320 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chengd6e44ab2008-02-01 09:10:45 +00001321}
1322
Evan Chengefd142a2008-02-02 04:07:54 +00001323/// printDeclare - This method prints a local variable declaration used by
1324/// debug tables.
Evan Cheng2cb90682008-02-04 23:06:48 +00001325/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1326/// entry into dwarf table.
Evan Chengefd142a2008-02-02 04:07:54 +00001327void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Evan Cheng2cb90682008-02-04 23:06:48 +00001328 int FI = MI->getOperand(0).getIndex();
1329 GlobalValue *GV = MI->getOperand(1).getGlobal();
1330 MMI->RecordVariable(GV, FI);
Evan Chengefd142a2008-02-02 04:07:54 +00001331}
1332
Chris Lattneraa23fa92006-02-01 22:41:11 +00001333/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1334/// instruction, using the specified assembler variant. Targets should
1335/// overried this to format as appropriate.
1336bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +00001337 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +00001338 // Target doesn't support this yet!
1339 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +00001340}
Chris Lattner1d08c652006-02-24 20:21:58 +00001341
1342bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1343 unsigned AsmVariant,
1344 const char *ExtraCode) {
1345 // Target doesn't support this yet!
1346 return true;
1347}
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001348
1349/// printBasicBlockLabel - This method prints the label for the specified
1350/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +00001351void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Chengc7990652008-02-28 00:43:03 +00001352 bool printAlign,
Nate Begemanb9d4f832006-05-02 05:37:32 +00001353 bool printColon,
1354 bool printComment) const {
Evan Chengc7990652008-02-28 00:43:03 +00001355 if (printAlign) {
1356 unsigned Align = MBB->getAlignment();
1357 if (Align)
1358 EmitAlignment(Log2_32(Align));
1359 }
1360
Dan Gohman6896901e2008-06-30 22:03:41 +00001361 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Chengcdf36092007-10-14 05:57:21 +00001362 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +00001363 if (printColon)
1364 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +00001365 if (printComment && MBB->getBasicBlock())
Dan Gohman33d0ea22007-07-30 15:06:25 +00001366 O << '\t' << TAI->getCommentString() << ' '
Evan Cheng53495222008-07-08 00:55:58 +00001367 << MBB->getBasicBlock()->getNameStart();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001368}
Nate Begeman984c1a42006-08-12 21:29:52 +00001369
Evan Cheng797d56f2007-11-09 01:32:10 +00001370/// printPICJumpTableSetLabel - This method prints a set label for the
1371/// specified MachineBasicBlock for a jumptable entry.
1372void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1373 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001374 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +00001375 return;
1376
Jim Laskeya6211dc2006-09-06 18:34:40 +00001377 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001378 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001379 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001380 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1381 << '_' << uid << '\n';
Nate Begeman984c1a42006-08-12 21:29:52 +00001382}
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001383
Evan Cheng797d56f2007-11-09 01:32:10 +00001384void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1385 const MachineBasicBlock *MBB) const {
Evan Cheng91f120f2006-11-01 09:23:08 +00001386 if (!TAI->getSetDirective())
1387 return;
1388
1389 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001390 << getFunctionNumber() << '_' << uid << '_' << uid2
1391 << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001392 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001393 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1394 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng91f120f2006-11-01 09:23:08 +00001395}
1396
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001397/// printDataDirective - This method prints the asm directive for the
1398/// specified type.
1399void AsmPrinter::printDataDirective(const Type *type) {
1400 const TargetData *TD = TM.getTargetData();
1401 switch (type->getTypeID()) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001402 case Type::IntegerTyID: {
1403 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1404 if (BitWidth <= 8)
1405 O << TAI->getData8bitsDirective();
1406 else if (BitWidth <= 16)
1407 O << TAI->getData16bitsDirective();
1408 else if (BitWidth <= 32)
1409 O << TAI->getData32bitsDirective();
1410 else if (BitWidth <= 64) {
1411 assert(TAI->getData64bitsDirective() &&
1412 "Target cannot handle 64-bit constant exprs!");
1413 O << TAI->getData64bitsDirective();
1414 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001415 break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001416 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001417 case Type::PointerTyID:
1418 if (TD->getPointerSize() == 8) {
1419 assert(TAI->getData64bitsDirective() &&
1420 "Target cannot handle 64-bit pointer exprs!");
1421 O << TAI->getData64bitsDirective();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001422 } else {
1423 O << TAI->getData32bitsDirective();
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001424 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001425 break;
1426 case Type::FloatTyID: case Type::DoubleTyID:
Dale Johannesen6bf69ed2007-09-28 18:06:58 +00001427 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001428 assert (0 && "Should have already output floating point constant.");
1429 default:
1430 assert (0 && "Can't handle printing this type of thing");
1431 break;
1432 }
1433}
Dale Johannesen915e1542007-02-16 01:54:53 +00001434
Evan Cheng0a1e6722008-07-08 16:40:43 +00001435void AsmPrinter::printSuffixedName(const char *Name, const char *Suffix,
1436 const char *Prefix) {
Dale Johannesen5bf742f2008-05-19 21:38:18 +00001437 if (Name[0]=='\"')
Evan Cheng0a1e6722008-07-08 16:40:43 +00001438 O << '\"';
1439 O << TAI->getPrivateGlobalPrefix();
1440 if (Prefix) O << Prefix;
1441 if (Name[0]=='\"')
1442 O << '\"';
1443 if (Name[0]=='\"')
1444 O << Name[1];
Dale Johannesen5bf742f2008-05-19 21:38:18 +00001445 else
Evan Cheng0a1e6722008-07-08 16:40:43 +00001446 O << Name;
1447 O << Suffix;
1448 if (Name[0]=='\"')
1449 O << '\"';
Dale Johannesen5bf742f2008-05-19 21:38:18 +00001450}
Evan Cheng53495222008-07-08 00:55:58 +00001451
Evan Cheng0a1e6722008-07-08 16:40:43 +00001452void AsmPrinter::printSuffixedName(const std::string &Name, const char* Suffix) {
Evan Cheng53495222008-07-08 00:55:58 +00001453 printSuffixedName(Name.c_str(), Suffix);
1454}
Anton Korobeynikoved473292008-08-08 18:25:07 +00001455
1456void AsmPrinter::printVisibility(const std::string& Name,
1457 unsigned Visibility) const {
1458 if (Visibility == GlobalValue::HiddenVisibility) {
1459 if (const char *Directive = TAI->getHiddenDirective())
1460 O << Directive << Name << '\n';
1461 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1462 if (const char *Directive = TAI->getProtectedDirective())
1463 O << Directive << Name << '\n';
1464 }
1465}
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001466
Gordon Henriksend930f912008-08-17 18:44:35 +00001467GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1468 if (!S->usesMetadata())
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001469 return 0;
1470
Gordon Henriksend930f912008-08-17 18:44:35 +00001471 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001472 if (GCPI != GCMetadataPrinters.end())
1473 return GCPI->second;
1474
Gordon Henriksend930f912008-08-17 18:44:35 +00001475 const char *Name = S->getName().c_str();
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001476
1477 for (GCMetadataPrinterRegistry::iterator
1478 I = GCMetadataPrinterRegistry::begin(),
1479 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1480 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001481 GCMetadataPrinter *GMP = I->instantiate();
1482 GMP->S = S;
1483 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1484 return GMP;
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001485 }
1486
Gordon Henriksend930f912008-08-17 18:44:35 +00001487 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001488 abort();
1489}