blob: bf57a653ad778ddd9aa075b36edf9296cff909b2 [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"
Bill Wendling5c3966a2006-11-29 00:39:47 +000025#include "llvm/Support/Streams.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000026#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000027#include "llvm/Target/TargetData.h"
Chris Lattner90438232006-10-06 22:50:56 +000028#include "llvm/Target/TargetLowering.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000029#include "llvm/Target/TargetMachine.h"
Evan Chengc963f6c2008-07-01 23:18:29 +000030#include "llvm/Target/TargetOptions.h"
Evan Cheng0e7b00d2008-03-15 00:03:38 +000031#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng797d56f2007-11-09 01:32:10 +000032#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner17f71652008-08-17 07:19:36 +000033#include "llvm/ADT/SmallString.h"
Owen Anderson93719642008-08-21 00:14:44 +000034#include "llvm/ADT/StringExtras.h"
Chris Lattneraa23fa92006-02-01 22:41:11 +000035#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000036using namespace llvm;
37
Devang Patel8c78a0b2007-05-03 01:11:54 +000038char AsmPrinter::ID = 0;
Owen Anderson93719642008-08-21 00:14:44 +000039AsmPrinter::AsmPrinter(raw_ostream &o, TargetMachine &tm,
Jim Laskey261779b2006-09-07 22:06:40 +000040 const TargetAsmInfo *T)
Evan Cheng0e7b00d2008-03-15 00:03:38 +000041 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o),
42 TM(tm), TAI(T), TRI(tm.getRegisterInfo()),
Evan Chengc7990652008-02-28 00:43:03 +000043 IsInTextSection(false)
Jim Laskeya6211dc2006-09-06 18:34:40 +000044{}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000045
Gordon Henriksendbe06d32008-08-17 12:08:44 +000046AsmPrinter::~AsmPrinter() {
47 for (gcp_iterator I = GCMetadataPrinters.begin(),
48 E = GCMetadataPrinters.end(); I != E; ++I)
49 delete I->second;
50}
51
Chris Lattnerdc822412006-10-05 02:42:47 +000052std::string AsmPrinter::getSectionForFunction(const Function &F) const {
53 return TAI->getTextSection();
54}
55
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000056
Chris Lattner8488ba22006-05-09 04:59:56 +000057/// SwitchToTextSection - Switch to the specified text section of the executable
58/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000059///
Chris Lattner8488ba22006-05-09 04:59:56 +000060void AsmPrinter::SwitchToTextSection(const char *NewSection,
61 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000062 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000063 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000064 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000065 else
66 NS = NewSection;
67
68 // If we're already in this section, we're done.
69 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000070
Chris Lattner4ebc6a22006-05-09 05:33:48 +000071 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000072 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman6896901e2008-06-30 22:03:41 +000073 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Jeff Cohen470f4312006-05-02 03:58:45 +000074
Chris Lattner4ebc6a22006-05-09 05:33:48 +000075 CurrentSection = NS;
76
77 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000078 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Evan Chengc7990652008-02-28 00:43:03 +000079
80 IsInTextSection = true;
Chris Lattner2ea5c992005-11-21 07:06:27 +000081}
82
Nate Begeman78756502006-07-27 01:13:04 +000083/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000084/// if we are not already in it!
85///
86void AsmPrinter::SwitchToDataSection(const char *NewSection,
87 const GlobalValue *GV) {
88 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000089 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000090 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner6341df82006-05-09 05:23:12 +000091 else
92 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +000093
Chris Lattner6341df82006-05-09 05:23:12 +000094 // If we're already in this section, we're done.
95 if (CurrentSection == NS) return;
96
Chris Lattner4ebc6a22006-05-09 05:33:48 +000097 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000098 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman6896901e2008-06-30 22:03:41 +000099 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000100
101 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +0000102
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000103 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000104 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Evan Chengc7990652008-02-28 00:43:03 +0000105
106 IsInTextSection = false;
Chris Lattner8488ba22006-05-09 04:59:56 +0000107}
108
109
Gordon Henriksen5180e852008-01-07 01:30:38 +0000110void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
111 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksend930f912008-08-17 18:44:35 +0000112 AU.addRequired<GCModuleInfo>();
Gordon Henriksen5180e852008-01-07 01:30:38 +0000113}
114
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000115bool AsmPrinter::doInitialization(Module &M) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000116 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +0000117
Gordon Henriksend930f912008-08-17 18:44:35 +0000118 GCModuleInfo *MI = getAnalysisToUpdate<GCModuleInfo>();
119 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
120 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
121 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
122 MP->beginAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000123
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000124 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000125 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000126 << M.getModuleInlineAsm()
Dan Gohman6896901e2008-06-30 22:03:41 +0000127 << '\n' << TAI->getCommentString()
Jim Laskeya6211dc2006-09-06 18:34:40 +0000128 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000129
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000130 SwitchToDataSection(""); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000131
Evan Cheng2cb90682008-02-04 23:06:48 +0000132 MMI = getAnalysisToUpdate<MachineModuleInfo>();
133 if (MMI) MMI->AnalyzeModule(M);
Jim Laskey0bbdc552006-01-26 20:21:46 +0000134
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000135 return false;
136}
137
138bool AsmPrinter::doFinalization(Module &M) {
Rafael Espindolad7998d02006-12-18 03:37:18 +0000139 if (TAI->getWeakRefDirective()) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000140 if (!ExtWeakSymbols.empty())
Rafael Espindolad7998d02006-12-18 03:37:18 +0000141 SwitchToDataSection("");
142
143 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
144 e = ExtWeakSymbols.end(); i != e; ++i) {
145 const GlobalValue *GV = *i;
146 std::string Name = Mang->getValueName(GV);
Dan Gohman6896901e2008-06-30 22:03:41 +0000147 O << TAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolad7998d02006-12-18 03:37:18 +0000148 }
149 }
150
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000151 if (TAI->getSetDirective()) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000152 if (!M.alias_empty())
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000153 SwitchToTextSection(TAI->getTextSection());
154
Dan Gohman6896901e2008-06-30 22:03:41 +0000155 O << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000156 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
157 I!=E; ++I) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000158 std::string Name = Mang->getValueName(I);
159 std::string Target;
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000160
161 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
162 Target = Mang->getValueName(GV);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000163
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000164 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000165 O << "\t.globl\t" << Name << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000166 else if (I->hasWeakLinkage())
Dan Gohman6896901e2008-06-30 22:03:41 +0000167 O << TAI->getWeakRefDirective() << Name << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000168 else if (!I->hasInternalLinkage())
169 assert(0 && "Invalid alias linkage");
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000170
171 if (I->hasHiddenVisibility()) {
172 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000173 O << Directive << Name << '\n';
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000174 } else if (I->hasProtectedVisibility()) {
175 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000176 O << Directive << Name << '\n';
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000177 }
178
Dan Gohman6896901e2008-06-30 22:03:41 +0000179 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000180
181 // If the aliasee has external weak linkage it can be referenced only by
182 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
183 // weak reference in such case.
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000184 if (GV->hasExternalWeakLinkage()) {
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000185 if (TAI->getWeakRefDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000186 O << TAI->getWeakRefDirective() << Target << '\n';
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000187 else
Dan Gohman6896901e2008-06-30 22:03:41 +0000188 O << "\t.globl\t" << Target << '\n';
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000189 }
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000190 }
191 }
192
Gordon Henriksend930f912008-08-17 18:44:35 +0000193 GCModuleInfo *MI = getAnalysisToUpdate<GCModuleInfo>();
194 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
195 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
196 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
197 MP->finishAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000198
Dan Gohmanbcde1722008-05-05 00:28:39 +0000199 // If we don't have any trampolines, then we don't require stack memory
200 // to be executable. Some targets have a directive to declare this.
201 Function* InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
202 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
203 if (TAI->getNonexecutableStackDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000204 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmanbcde1722008-05-05 00:28:39 +0000205
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000206 delete Mang; Mang = 0;
207 return false;
208}
209
Bill Wendlinge8c885f2007-09-18 09:10:16 +0000210std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
Bill Wendling067f1d82007-09-18 01:47:22 +0000211 assert(MF && "No machine function?");
Dale Johannesen49155772008-04-02 20:10:52 +0000212 std::string Name = MF->getFunction()->getName();
213 if (Name.empty())
214 Name = Mang->getValueName(MF->getFunction());
215 return Mang->makeNameProper(Name + ".eh", TAI->getGlobalPrefix());
Bill Wendling067f1d82007-09-18 01:47:22 +0000216}
217
Chris Lattnerbb644e32005-11-21 07:51:36 +0000218void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000219 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000220 CurrentFnName = Mang->getValueName(MF.getFunction());
Evan Chengcdf36092007-10-14 05:57:21 +0000221 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000222}
223
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000224/// EmitConstantPool - Print to the current output stream assembly
225/// representations of the constants in the constant pool MCP. This is
226/// used to print out constants which have been "spilled to memory" by
227/// the code generator.
228///
229void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000230 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000231 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000232
233 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
234 // in special sections.
235 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
236 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
237 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
238 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000239 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000240 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000241 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng2ad050f2006-09-14 07:35:00 +0000242 const Type *Ty = CPE.getType();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000243 if (TAI->getFourByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000244 TM.getTargetData()->getABITypeSize(Ty) == 4)
Evan Chengec1d60b2006-06-29 00:26:09 +0000245 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000246 else if (TAI->getEightByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000247 TM.getTargetData()->getABITypeSize(Ty) == 8)
Evan Chengec1d60b2006-06-29 00:26:09 +0000248 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000249 else if (TAI->getSixteenByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000250 TM.getTargetData()->getABITypeSize(Ty) == 16)
Evan Chengec1d60b2006-06-29 00:26:09 +0000251 SixteenByteCPs.push_back(std::make_pair(CPE, i));
252 else
253 OtherCPs.push_back(std::make_pair(CPE, i));
254 }
255
256 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000257 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
258 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
259 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
260 SixteenByteCPs);
261 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Chengec1d60b2006-06-29 00:26:09 +0000262}
263
264void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
265 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
266 if (CP.empty()) return;
267
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000268 SwitchToDataSection(Section);
Evan Chengec1d60b2006-06-29 00:26:09 +0000269 EmitAlignment(Alignment);
270 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengcdf36092007-10-14 05:57:21 +0000271 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Owen Anderson93719642008-08-21 00:14:44 +0000272 << CP[i].second << ":\t\t\t\t\t";
273 // O << TAI->getCommentString() << ' ' <<
274 // WriteTypeSymbolic(O, CP[i].first.getType(), 0);
Chris Lattner5d2a9a42008-08-19 04:44:30 +0000275 O << '\n';
Evan Cheng2ad050f2006-09-14 07:35:00 +0000276 if (CP[i].first.isMachineConstantPoolEntry())
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000277 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000278 else
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000279 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000280 if (i != e-1) {
Evan Cheng2ad050f2006-09-14 07:35:00 +0000281 const Type *Ty = CP[i].first.getType();
Evan Chengec1d60b2006-06-29 00:26:09 +0000282 unsigned EntSize =
Duncan Sands283207a2007-11-05 00:04:43 +0000283 TM.getTargetData()->getABITypeSize(Ty);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000284 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000285 // Emit inter-object padding for alignment.
Evan Cheng2ad050f2006-09-14 07:35:00 +0000286 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000287 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000288 }
289}
290
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000291/// EmitJumpTableInfo - Print assembly representations of the jump tables used
292/// by the current function to the current output stream.
293///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000294void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
295 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000296 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
297 if (JT.empty()) return;
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000298
Jim Laskey70323a82006-12-14 19:17:33 +0000299 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begemanefc312a2006-07-27 16:46:58 +0000300
Nate Begeman78756502006-07-27 01:13:04 +0000301 // Pick the directive to use to print the jump table entries, and switch to
302 // the appropriate section.
Jim Laskey70323a82006-12-14 19:17:33 +0000303 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000304
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000305 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
306 const Function *F = MF.getFunction();
307 unsigned SectionFlags = TAI->SectionFlagsForGlobal(F);
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000308 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000309 !JumpTableDataSection ||
310 SectionFlags & SectionFlags::Linkonce) {
Jim Laskey70323a82006-12-14 19:17:33 +0000311 // In PIC mode, we need to emit the jump table to the same section as the
312 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000313 // We should also do if the section name is NULL or function is declared in
314 // discardable section.
Jim Laskey70323a82006-12-14 19:17:33 +0000315 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman78756502006-07-27 01:13:04 +0000316 } else {
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000317 SwitchToDataSection(JumpTableDataSection);
Nate Begeman78756502006-07-27 01:13:04 +0000318 }
Jim Laskey70323a82006-12-14 19:17:33 +0000319
320 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000321
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000322 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000323 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner28bfe382006-10-28 18:10:06 +0000324
325 // If this jump table was deleted, ignore it.
326 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-08-12 21:29:52 +0000327
328 // For PIC codegen, if possible we want to use the SetDirective to reduce
329 // the number of relocations the assembler will generate for the jump table.
330 // Set directives are all printed before the jump table itself.
Evan Cheng797d56f2007-11-09 01:32:10 +0000331 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Jim Laskey70323a82006-12-14 19:17:33 +0000332 if (TAI->getSetDirective() && IsPic)
Nate Begeman984c1a42006-08-12 21:29:52 +0000333 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng797d56f2007-11-09 01:32:10 +0000334 if (EmittedSets.insert(JTBBs[ii]))
335 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman984c1a42006-08-12 21:29:52 +0000336
Chris Lattner0ee2d462007-01-18 01:12:56 +0000337 // On some targets (e.g. darwin) we want to emit two consequtive labels
338 // before each jump table. The first label is never referenced, but tells
339 // the assembler and linker the extents of the jump table object. The
340 // second label is actually referenced by the code.
341 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
Evan Chengcdf36092007-10-14 05:57:21 +0000342 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Chris Lattner0ee2d462007-01-18 01:12:56 +0000343
Evan Chengcdf36092007-10-14 05:57:21 +0000344 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
345 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000346
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000347 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000348 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000349 O << '\n';
350 }
351 }
352}
353
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000354void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
355 const MachineBasicBlock *MBB,
356 unsigned uid) const {
357 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
358
359 // Use JumpTableDirective otherwise honor the entry size from the jump table
360 // info.
361 const char *JTEntryDirective = TAI->getJumpTableDirective();
362 bool HadJTEntryDirective = JTEntryDirective != NULL;
363 if (!HadJTEntryDirective) {
364 JTEntryDirective = MJTI->getEntrySize() == 4 ?
365 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
366 }
367
368 O << JTEntryDirective << ' ';
369
370 // If we have emitted set directives for the jump table entries, print
371 // them rather than the entries themselves. If we're emitting PIC, then
372 // emit the table entries as differences between two text section labels.
373 // If we're emitting non-PIC code, then emit the entries as direct
374 // references to the target basic blocks.
375 if (IsPic) {
376 if (TAI->getSetDirective()) {
377 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
378 << '_' << uid << "_set_" << MBB->getNumber();
379 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000380 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000381 // If the arch uses custom Jump Table directives, don't calc relative to
382 // JT
383 if (!HadJTEntryDirective)
384 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
385 << getFunctionNumber() << '_' << uid;
386 }
387 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000388 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000389 }
390}
391
392
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000393/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
394/// special global used by LLVM. If so, emit it and return true, otherwise
395/// do nothing and return false.
396bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Andrew Lenharthbeb80a92007-08-22 19:33:11 +0000397 if (GV->getName() == "llvm.used") {
398 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
399 EmitLLVMUsedList(GV->getInitializer());
400 return true;
401 }
402
Jim Laskey313570f2006-03-07 22:00:35 +0000403 // Ignore debug and non-emitted data.
404 if (GV->getSection() == "llvm.metadata") return true;
405
406 if (!GV->hasAppendingLinkage()) return false;
407
408 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000409
Evan Cheng53ce7de2007-06-04 20:39:18 +0000410 const TargetData *TD = TM.getTargetData();
411 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner3760e902006-01-12 19:17:23 +0000412 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000413 SwitchToDataSection(TAI->getStaticCtorsSection());
Evan Cheng53ce7de2007-06-04 20:39:18 +0000414 EmitAlignment(Align, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000415 EmitXXStructorList(GV->getInitializer());
416 return true;
417 }
418
Chris Lattner3760e902006-01-12 19:17:23 +0000419 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000420 SwitchToDataSection(TAI->getStaticDtorsSection());
Evan Cheng53ce7de2007-06-04 20:39:18 +0000421 EmitAlignment(Align, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000422 EmitXXStructorList(GV->getInitializer());
423 return true;
424 }
425
426 return false;
427}
428
Chris Lattner66af3902006-09-26 03:38:18 +0000429/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
430/// global in the specified llvm.used list as being used with this directive.
431void AsmPrinter::EmitLLVMUsedList(Constant *List) {
432 const char *Directive = TAI->getUsedDirective();
433
434 // Should be an array of 'sbyte*'.
435 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
436 if (InitList == 0) return;
437
438 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
439 O << Directive;
440 EmitConstantValueOnly(InitList->getOperand(i));
Dan Gohman6896901e2008-06-30 22:03:41 +0000441 O << '\n';
Chris Lattner66af3902006-09-26 03:38:18 +0000442 }
443}
444
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000445/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
446/// function pointers, ignoring the init priority.
447void AsmPrinter::EmitXXStructorList(Constant *List) {
448 // Should be an array of '{ int, void ()* }' structs. The first value is the
449 // init priority, which we ignore.
450 if (!isa<ConstantArray>(List)) return;
451 ConstantArray *InitList = cast<ConstantArray>(List);
452 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
453 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
454 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000455
456 if (CS->getOperand(1)->isNullValue())
457 return; // Found a null terminator, exit printing.
458 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000459 EmitGlobalConstant(CS->getOperand(1));
460 }
461}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000462
Jim Laskey71262542006-10-17 13:41:07 +0000463/// getGlobalLinkName - Returns the asm/link name of of the specified
464/// global variable. Should be overridden by each target asm printer to
465/// generate the appropriate value.
Jim Laskeyd24b9132006-10-17 17:17:24 +0000466const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
467 std::string LinkName;
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000468
469 if (isa<Function>(GV)) {
470 LinkName += TAI->getFunctionAddrPrefix();
471 LinkName += Mang->getValueName(GV);
472 LinkName += TAI->getFunctionAddrSuffix();
473 } else {
474 LinkName += TAI->getGlobalVarAddrPrefix();
475 LinkName += Mang->getValueName(GV);
476 LinkName += TAI->getGlobalVarAddrSuffix();
477 }
478
Jim Laskeyd24b9132006-10-17 17:17:24 +0000479 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000480}
481
Jim Laskey18fc0972007-02-21 22:47:38 +0000482/// EmitExternalGlobal - Emit the external reference to a global variable.
483/// Should be overridden if an indirect reference should be used.
484void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
485 O << getGlobalLinkName(GV);
486}
487
488
489
Jim Laskey1c055e82007-01-25 15:12:02 +0000490//===----------------------------------------------------------------------===//
491/// LEB 128 number encoding.
492
493/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
494/// representing an unsigned leb128 value.
495void AsmPrinter::PrintULEB128(unsigned Value) const {
496 do {
497 unsigned Byte = Value & 0x7f;
498 Value >>= 7;
499 if (Value) Byte |= 0x80;
Owen Anderson93719642008-08-21 00:14:44 +0000500 O << "0x" << utohexstr(Byte);
Jim Laskey1c055e82007-01-25 15:12:02 +0000501 if (Value) O << ", ";
502 } while (Value);
503}
504
Jim Laskey1c055e82007-01-25 15:12:02 +0000505/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
506/// representing a signed leb128 value.
507void AsmPrinter::PrintSLEB128(int Value) const {
508 int Sign = Value >> (8 * sizeof(Value) - 1);
509 bool IsMore;
Anton Korobeynikovbd890b12008-08-16 12:57:46 +0000510
Jim Laskey1c055e82007-01-25 15:12:02 +0000511 do {
512 unsigned Byte = Value & 0x7f;
513 Value >>= 7;
514 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
515 if (IsMore) Byte |= 0x80;
Owen Anderson93719642008-08-21 00:14:44 +0000516 O << "0x" << utohexstr(Byte);
Jim Laskey1c055e82007-01-25 15:12:02 +0000517 if (IsMore) O << ", ";
518 } while (IsMore);
519}
520
Jim Laskey1c055e82007-01-25 15:12:02 +0000521//===--------------------------------------------------------------------===//
522// Emission and print routines
523//
524
525/// PrintHex - Print a value as a hexidecimal value.
526///
527void AsmPrinter::PrintHex(int Value) const {
Owen Anderson93719642008-08-21 00:14:44 +0000528 O << "0x" << utohexstr(static_cast<unsigned>(Value));
Jim Laskey1c055e82007-01-25 15:12:02 +0000529}
530
531/// EOL - Print a newline character to asm stream. If a comment is present
532/// then it will be printed first. Comments should not contain '\n'.
Jim Laskey18fc0972007-02-21 22:47:38 +0000533void AsmPrinter::EOL() const {
Dan Gohman6896901e2008-06-30 22:03:41 +0000534 O << '\n';
Jim Laskey18fc0972007-02-21 22:47:38 +0000535}
Owen Anderson1d952532008-07-01 21:16:27 +0000536
Jim Laskey1c055e82007-01-25 15:12:02 +0000537void AsmPrinter::EOL(const std::string &Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000538 if (VerboseAsm && !Comment.empty()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000539 O << '\t'
Jim Laskey1c055e82007-01-25 15:12:02 +0000540 << TAI->getCommentString()
Dan Gohman6896901e2008-06-30 22:03:41 +0000541 << ' '
Jim Laskey1c055e82007-01-25 15:12:02 +0000542 << Comment;
543 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000544 O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000545}
546
Owen Anderson1d952532008-07-01 21:16:27 +0000547void AsmPrinter::EOL(const char* Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000548 if (VerboseAsm && *Comment) {
Owen Anderson1d952532008-07-01 21:16:27 +0000549 O << '\t'
550 << TAI->getCommentString()
551 << ' '
552 << Comment;
553 }
554 O << '\n';
555}
556
Jim Laskey1c055e82007-01-25 15:12:02 +0000557/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
558/// unsigned leb128 value.
559void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
560 if (TAI->hasLEB128()) {
561 O << "\t.uleb128\t"
562 << Value;
563 } else {
564 O << TAI->getData8bitsDirective();
565 PrintULEB128(Value);
566 }
567}
568
569/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
570/// signed leb128 value.
571void AsmPrinter::EmitSLEB128Bytes(int Value) const {
572 if (TAI->hasLEB128()) {
573 O << "\t.sleb128\t"
574 << Value;
575 } else {
576 O << TAI->getData8bitsDirective();
577 PrintSLEB128(Value);
578 }
579}
580
581/// EmitInt8 - Emit a byte directive and value.
582///
583void AsmPrinter::EmitInt8(int Value) const {
584 O << TAI->getData8bitsDirective();
585 PrintHex(Value & 0xFF);
586}
587
588/// EmitInt16 - Emit a short directive and value.
589///
590void AsmPrinter::EmitInt16(int Value) const {
591 O << TAI->getData16bitsDirective();
592 PrintHex(Value & 0xFFFF);
593}
594
595/// EmitInt32 - Emit a long directive and value.
596///
597void AsmPrinter::EmitInt32(int Value) const {
598 O << TAI->getData32bitsDirective();
599 PrintHex(Value);
600}
601
602/// EmitInt64 - Emit a long long directive and value.
603///
604void AsmPrinter::EmitInt64(uint64_t Value) const {
605 if (TAI->getData64bitsDirective()) {
606 O << TAI->getData64bitsDirective();
607 PrintHex(Value);
608 } else {
609 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000610 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000611 EmitInt32(unsigned(Value));
612 } else {
Dan Gohman6896901e2008-06-30 22:03:41 +0000613 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000614 EmitInt32(unsigned(Value >> 32));
615 }
616 }
617}
618
619/// toOctal - Convert the low order bits of X into an octal digit.
620///
621static inline char toOctal(int X) {
622 return (X&7)+'0';
623}
624
625/// printStringChar - Print a char, escaped if necessary.
626///
Owen Anderson93719642008-08-21 00:14:44 +0000627static void printStringChar(raw_ostream &O, char C) {
Jim Laskey1c055e82007-01-25 15:12:02 +0000628 if (C == '"') {
629 O << "\\\"";
630 } else if (C == '\\') {
631 O << "\\\\";
632 } else if (isprint(C)) {
633 O << C;
634 } else {
635 switch(C) {
636 case '\b': O << "\\b"; break;
637 case '\f': O << "\\f"; break;
638 case '\n': O << "\\n"; break;
639 case '\r': O << "\\r"; break;
640 case '\t': O << "\\t"; break;
641 default:
642 O << '\\';
643 O << toOctal(C >> 6);
644 O << toOctal(C >> 3);
645 O << toOctal(C >> 0);
646 break;
647 }
648 }
649}
650
651/// EmitString - Emit a string with quotes and a null terminator.
652/// Special characters are emitted properly.
653/// \literal (Eg. '\t') \endliteral
654void AsmPrinter::EmitString(const std::string &String) const {
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000655 const char* AscizDirective = TAI->getAscizDirective();
656 if (AscizDirective)
657 O << AscizDirective;
658 else
659 O << TAI->getAsciiDirective();
Dan Gohman6896901e2008-06-30 22:03:41 +0000660 O << '\"';
Jim Laskey1c055e82007-01-25 15:12:02 +0000661 for (unsigned i = 0, N = String.size(); i < N; ++i) {
662 unsigned char C = String[i];
663 printStringChar(O, C);
664 }
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000665 if (AscizDirective)
Dan Gohman6896901e2008-06-30 22:03:41 +0000666 O << '\"';
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000667 else
668 O << "\\0\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000669}
670
671
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000672/// EmitFile - Emit a .file directive.
673void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
674 O << "\t.file\t" << Number << " \"";
675 for (unsigned i = 0, N = Name.size(); i < N; ++i) {
676 unsigned char C = Name[i];
677 printStringChar(O, C);
678 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000679 O << '\"';
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000680}
681
682
Jim Laskey1c055e82007-01-25 15:12:02 +0000683//===----------------------------------------------------------------------===//
684
Chris Lattner3e3ff302007-05-31 18:57:45 +0000685// EmitAlignment - Emit an alignment directive to the specified power of
686// two boundary. For example, if you pass in 3 here, you will get an 8
687// byte alignment. If a global value is specified, and if that global has
688// an explicit alignment requested, it will unconditionally override the
689// alignment request. However, if ForcedAlignBits is specified, this value
690// has final say: the ultimate alignment will be the max of ForcedAlignBits
691// and the alignment computed with NumBits and the global.
692//
693// The algorithm is:
694// Align = NumBits;
695// if (GV && GV->hasalignment) Align = GV->getalignment();
696// Align = std::max(Align, ForcedAlignBits);
697//
698void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng26edb592008-02-29 19:36:59 +0000699 unsigned ForcedAlignBits,
700 bool UseFillExpr) const {
Dale Johannesen8653d292007-04-23 23:33:31 +0000701 if (GV && GV->getAlignment())
Chris Lattner3e3ff302007-05-31 18:57:45 +0000702 NumBits = Log2_32(GV->getAlignment());
703 NumBits = std::max(NumBits, ForcedAlignBits);
704
Chris Lattner747960d2005-11-10 18:09:27 +0000705 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000706 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng86eb3fd2007-07-25 23:35:07 +0000707 O << TAI->getAlignDirective() << NumBits;
Evan Chengc7990652008-02-28 00:43:03 +0000708
709 unsigned FillValue = TAI->getTextAlignFillValue();
Evan Cheng26edb592008-02-29 19:36:59 +0000710 UseFillExpr &= IsInTextSection && FillValue;
Owen Anderson93719642008-08-21 00:14:44 +0000711 if (UseFillExpr) O << ",0x" << utohexstr(FillValue);
Dan Gohman6896901e2008-06-30 22:03:41 +0000712 O << '\n';
Chris Lattner1d35c162004-08-17 19:14:29 +0000713}
714
Jim Laskey18fc0972007-02-21 22:47:38 +0000715
Chris Lattnerbb644e32005-11-21 07:51:36 +0000716/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000717///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000718void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000719 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000720 if (TAI->getZeroDirective()) {
721 O << TAI->getZeroDirective() << NumZeros;
722 if (TAI->getZeroDirectiveSuffix())
723 O << TAI->getZeroDirectiveSuffix();
Dan Gohman6896901e2008-06-30 22:03:41 +0000724 O << '\n';
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000725 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000726 for (; NumZeros; --NumZeros)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000727 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000728 }
729 }
730}
731
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000732// Print out the specified constant, without a storage class. Only the
733// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000734void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000735 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman6896901e2008-06-30 22:03:41 +0000736 O << '0';
Zhou Sheng75b871f2007-01-11 12:24:14 +0000737 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel499c1192008-06-03 06:18:19 +0000738 O << CI->getZExtValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000739 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000740 // This is a constant address for a global variable or function. Use the
741 // name of the variable or function as the address value, possibly
742 // decorating it with GlobalVarAddrPrefix/Suffix or
743 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000744 if (isa<Function>(GV)) {
745 O << TAI->getFunctionAddrPrefix()
746 << Mang->getValueName(GV)
747 << TAI->getFunctionAddrSuffix();
748 } else {
749 O << TAI->getGlobalVarAddrPrefix()
750 << Mang->getValueName(GV)
751 << TAI->getGlobalVarAddrSuffix();
752 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000753 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000754 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000755 unsigned Opcode = CE->getOpcode();
756 switch (Opcode) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000757 case Instruction::GetElementPtr: {
758 // generate a symbolic expression for the byte address
759 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner83dfca82007-02-10 20:31:59 +0000760 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
761 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
762 idxVec.size())) {
Chris Lattner145569b2005-02-14 21:40:26 +0000763 if (Offset)
Dan Gohman6896901e2008-06-30 22:03:41 +0000764 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000765 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000766 if (Offset > 0)
767 O << ") + " << Offset;
768 else if (Offset < 0)
769 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000770 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000771 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000772 }
773 break;
774 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000775 case Instruction::Trunc:
776 case Instruction::ZExt:
777 case Instruction::SExt:
778 case Instruction::FPTrunc:
779 case Instruction::FPExt:
780 case Instruction::UIToFP:
781 case Instruction::SIToFP:
782 case Instruction::FPToUI:
783 case Instruction::FPToSI:
784 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
785 break;
Chris Lattnerb9e41f52006-12-12 05:14:13 +0000786 case Instruction::BitCast:
787 return EmitConstantValueOnly(CE->getOperand(0));
788
Chris Lattner0c537da2006-12-12 05:18:19 +0000789 case Instruction::IntToPtr: {
790 // Handle casts to pointers by changing them into casts to the appropriate
791 // integer type. This promotes constant folding and simplifies this code.
792 Constant *Op = CE->getOperand(0);
793 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
794 return EmitConstantValueOnly(Op);
795 }
796
797
798 case Instruction::PtrToInt: {
Chris Lattner85492422006-07-29 01:57:19 +0000799 // Support only foldable casts to/from pointers that can be eliminated by
800 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000801 Constant *Op = CE->getOperand(0);
Chris Lattner0c537da2006-12-12 05:18:19 +0000802 const Type *Ty = CE->getType();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000803
Chris Lattner0c537da2006-12-12 05:18:19 +0000804 // We can emit the pointer value into this slot if the slot is an
805 // integer slot greater or equal to the size of the pointer.
Nick Lewycky42a19b62008-08-08 06:34:07 +0000806 if (TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
Chris Lattner85492422006-07-29 01:57:19 +0000807 return EmitConstantValueOnly(Op);
Nick Lewycky42a19b62008-08-08 06:34:07 +0000808
809 O << "((";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000810 EmitConstantValueOnly(Op);
Nick Lewycky42a19b62008-08-08 06:34:07 +0000811 APInt ptrMask = APInt::getAllOnesValue(TD->getABITypeSizeInBits(Ty));
Chris Lattner17f71652008-08-17 07:19:36 +0000812
813 SmallString<40> S;
814 ptrMask.toStringUnsigned(S);
815 O << ") & " << S.c_str() << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000816 break;
817 }
818 case Instruction::Add:
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000819 case Instruction::Sub:
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000820 case Instruction::And:
821 case Instruction::Or:
822 case Instruction::Xor:
Dan Gohman6896901e2008-06-30 22:03:41 +0000823 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000824 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman6896901e2008-06-30 22:03:41 +0000825 O << ')';
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000826 switch (Opcode) {
827 case Instruction::Add:
828 O << " + ";
829 break;
830 case Instruction::Sub:
831 O << " - ";
832 break;
833 case Instruction::And:
834 O << " & ";
835 break;
836 case Instruction::Or:
837 O << " | ";
838 break;
839 case Instruction::Xor:
840 O << " ^ ";
841 break;
842 default:
843 break;
844 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000845 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000846 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman6896901e2008-06-30 22:03:41 +0000847 O << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000848 break;
849 default:
850 assert(0 && "Unsupported operator!");
851 }
852 } else {
853 assert(0 && "Unknown constant value!");
854 }
855}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000856
Chris Lattner55a6d902005-11-10 18:06:33 +0000857/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000858/// the predicate isString is true.
859///
Owen Anderson93719642008-08-21 00:14:44 +0000860static void printAsCString(raw_ostream &O, const ConstantArray *CVA,
Chris Lattner55a6d902005-11-10 18:06:33 +0000861 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000862 assert(CVA->isString() && "Array is not string compatible!");
863
Dan Gohman6896901e2008-06-30 22:03:41 +0000864 O << '\"';
Chris Lattner55a6d902005-11-10 18:06:33 +0000865 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000866 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000867 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskey1c055e82007-01-25 15:12:02 +0000868 printStringChar(O, C);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000869 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000870 O << '\"';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000871}
872
Jeff Cohen24a62a92006-05-02 01:16:28 +0000873/// EmitString - Emit a zero-byte-terminated string constant.
874///
875void AsmPrinter::EmitString(const ConstantArray *CVA) const {
876 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000877 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000878 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000879 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000880 printAsCString(O, CVA, NumElts-1);
881 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000882 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000883 printAsCString(O, CVA, NumElts);
884 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000885 O << '\n';
Jeff Cohen24a62a92006-05-02 01:16:28 +0000886}
887
Chris Lattnerbb644e32005-11-21 07:51:36 +0000888/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000889void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000890 const TargetData *TD = TM.getTargetData();
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000891 unsigned Size = TD->getABITypeSize(CV->getType());
Chris Lattner8452a1f2004-08-17 06:36:49 +0000892
Chris Lattner61753bf2004-10-16 18:19:26 +0000893 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Duncan Sands283207a2007-11-05 00:04:43 +0000894 EmitZeros(Size);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000895 return;
896 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
897 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000898 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000899 } else { // Not a string. Print the values in successive locations
Duncan Sands283207a2007-11-05 00:04:43 +0000900 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000901 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000902 }
903 return;
904 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
905 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000906 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000907 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000908 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
909 const Constant* field = CVS->getOperand(i);
910
911 // Check if padding is needed and insert one or more 0s.
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000912 uint64_t fieldSize = TD->getABITypeSize(field->getType());
Duncan Sandsf7ae8bd2007-11-05 18:03:02 +0000913 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
Chris Lattnerc473d8e2007-02-10 19:55:17 +0000914 - cvsLayout->getElementOffset(i)) - fieldSize;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000915 sizeSoFar += fieldSize + padSize;
916
Duncan Sandsfc3c4892008-06-04 08:21:45 +0000917 // Now print the actual field value.
918 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000919
Duncan Sandsf7ae8bd2007-11-05 18:03:02 +0000920 // Insert padding - this may include padding to increase the size of the
921 // current field up to the ABI size (if the struct is not packed) as well
922 // as padding to ensure that the next field starts at the right offset.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000923 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000924 }
Chris Lattnerb84892d2007-02-10 19:59:22 +0000925 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
Chris Lattner8452a1f2004-08-17 06:36:49 +0000926 "Layout of constant struct may be incorrect!");
927 return;
928 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
929 // FP Constants are printed as integer constants to avoid losing
930 // precision...
Chris Lattner8452a1f2004-08-17 06:36:49 +0000931 if (CFP->getType() == Type::DoubleTy) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000932 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
Dale Johannesen028084e2007-09-12 03:30:33 +0000933 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000934 if (TAI->getData64bitsDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000935 O << TAI->getData64bitsDirective() << i << '\t'
936 << TAI->getCommentString() << " double value: " << Val << '\n';
Owen Anderson20a631f2006-05-03 01:29:57 +0000937 else if (TD->isBigEndian()) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000938 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +0000939 << '\t' << TAI->getCommentString()
940 << " double most significant word " << Val << '\n';
Dale Johannesen245dceb2007-09-11 18:32:33 +0000941 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman6896901e2008-06-30 22:03:41 +0000942 << '\t' << TAI->getCommentString()
943 << " double least significant word " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000944 } else {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000945 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman6896901e2008-06-30 22:03:41 +0000946 << '\t' << TAI->getCommentString()
947 << " double least significant word " << Val << '\n';
Dale Johannesen245dceb2007-09-11 18:32:33 +0000948 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +0000949 << '\t' << TAI->getCommentString()
950 << " double most significant word " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000951 }
952 return;
Dale Johannesen028084e2007-09-12 03:30:33 +0000953 } else if (CFP->getType() == Type::FloatTy) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000954 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
955 O << TAI->getData32bitsDirective()
Dale Johannesen028084e2007-09-12 03:30:33 +0000956 << CFP->getValueAPF().convertToAPInt().getZExtValue()
Dan Gohman6896901e2008-06-30 22:03:41 +0000957 << '\t' << TAI->getCommentString() << " float " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000958 return;
Dale Johannesen028084e2007-09-12 03:30:33 +0000959 } else if (CFP->getType() == Type::X86_FP80Ty) {
960 // all long double variants are printed as hex
Dale Johannesen34aa41c2007-09-26 23:20:33 +0000961 // api needed to prevent premature destruction
962 APInt api = CFP->getValueAPF().convertToAPInt();
963 const uint64_t *p = api.getRawData();
Chris Lattnerf1a6c9f2008-01-27 06:09:28 +0000964 APFloat DoubleVal = CFP->getValueAPF();
965 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven);
Dale Johannesen028084e2007-09-12 03:30:33 +0000966 if (TD->isBigEndian()) {
967 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
Dan Gohman6896901e2008-06-30 22:03:41 +0000968 << '\t' << TAI->getCommentString()
Chris Lattnerf1a6c9f2008-01-27 06:09:28 +0000969 << " long double most significant halfword of ~"
Dan Gohman6896901e2008-06-30 22:03:41 +0000970 << DoubleVal.convertToDouble() << '\n';
Dale Johannesen028084e2007-09-12 03:30:33 +0000971 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +0000972 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000973 << " long double next halfword\n";
974 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
Dan Gohman6896901e2008-06-30 22:03:41 +0000975 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000976 << " long double next halfword\n";
977 O << TAI->getData16bitsDirective() << uint16_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +0000978 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000979 << " long double next halfword\n";
980 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +0000981 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000982 << " long double least significant halfword\n";
983 } else {
984 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +0000985 << '\t' << TAI->getCommentString()
Chris Lattnerf1a6c9f2008-01-27 06:09:28 +0000986 << " long double least significant halfword of ~"
Dan Gohman6896901e2008-06-30 22:03:41 +0000987 << DoubleVal.convertToDouble() << '\n';
Dale Johannesen028084e2007-09-12 03:30:33 +0000988 O << TAI->getData16bitsDirective() << uint16_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +0000989 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000990 << " long double next halfword\n";
991 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
Dan Gohman6896901e2008-06-30 22:03:41 +0000992 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000993 << " long double next halfword\n";
994 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +0000995 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000996 << " long double next halfword\n";
997 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
Dan Gohman6896901e2008-06-30 22:03:41 +0000998 << '\t' << TAI->getCommentString()
Dale Johannesen028084e2007-09-12 03:30:33 +0000999 << " long double most significant halfword\n";
1000 }
Duncan Sands283207a2007-11-05 00:04:43 +00001001 EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty));
Dale Johannesen028084e2007-09-12 03:30:33 +00001002 return;
Dale Johannesen6472eb62007-10-11 23:32:15 +00001003 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1004 // all long double variants are printed as hex
1005 // api needed to prevent premature destruction
1006 APInt api = CFP->getValueAPF().convertToAPInt();
1007 const uint64_t *p = api.getRawData();
1008 if (TD->isBigEndian()) {
1009 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001010 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001011 << " long double most significant word\n";
1012 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +00001013 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001014 << " long double next word\n";
1015 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001016 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001017 << " long double next word\n";
1018 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +00001019 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001020 << " long double least significant word\n";
1021 } else {
1022 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman6896901e2008-06-30 22:03:41 +00001023 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001024 << " long double least significant word\n";
1025 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001026 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001027 << " long double next word\n";
1028 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman6896901e2008-06-30 22:03:41 +00001029 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001030 << " long double next word\n";
1031 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001032 << '\t' << TAI->getCommentString()
Dale Johannesen6472eb62007-10-11 23:32:15 +00001033 << " long double most significant word\n";
1034 }
1035 return;
Dale Johannesen028084e2007-09-12 03:30:33 +00001036 } else assert(0 && "Floating point constant type not handled");
Reid Spencere63b6512006-12-31 05:55:36 +00001037 } else if (CV->getType() == Type::Int64Ty) {
Chris Lattner8452a1f2004-08-17 06:36:49 +00001038 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00001039 uint64_t Val = CI->getZExtValue();
Misha Brukman835702a2005-04-21 22:36:52 +00001040
Jim Laskeya6211dc2006-09-06 18:34:40 +00001041 if (TAI->getData64bitsDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +00001042 O << TAI->getData64bitsDirective() << Val << '\n';
Owen Anderson20a631f2006-05-03 01:29:57 +00001043 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001044 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001045 << '\t' << TAI->getCommentString()
1046 << " Double-word most significant word " << Val << '\n';
Jim Laskeya6211dc2006-09-06 18:34:40 +00001047 O << TAI->getData32bitsDirective() << unsigned(Val)
Dan Gohman6896901e2008-06-30 22:03:41 +00001048 << '\t' << TAI->getCommentString()
1049 << " Double-word least significant word " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001050 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001051 O << TAI->getData32bitsDirective() << unsigned(Val)
Dan Gohman6896901e2008-06-30 22:03:41 +00001052 << '\t' << TAI->getCommentString()
1053 << " Double-word least significant word " << Val << '\n';
Jim Laskeya6211dc2006-09-06 18:34:40 +00001054 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
Dan Gohman6896901e2008-06-30 22:03:41 +00001055 << '\t' << TAI->getCommentString()
1056 << " Double-word most significant word " << Val << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001057 }
1058 return;
1059 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001060 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1061 const VectorType *PTy = CP->getType();
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001062
1063 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
Duncan Sandsfc3c4892008-06-04 08:21:45 +00001064 EmitGlobalConstant(CP->getOperand(I));
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001065
1066 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +00001067 }
1068
1069 const Type *type = CV->getType();
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001070 printDataDirective(type);
Chris Lattnerbb644e32005-11-21 07:51:36 +00001071 EmitConstantValueOnly(CV);
Scott Michelc0e9ff62008-06-03 15:39:51 +00001072 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner17f71652008-08-17 07:19:36 +00001073 SmallString<40> S;
1074 CI->getValue().toStringUnsigned(S, 16);
1075 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
Scott Michelc0e9ff62008-06-03 15:39:51 +00001076 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001077 O << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001078}
Chris Lattner061d9e22006-01-27 02:10:10 +00001079
Chris Lattner17f71652008-08-17 07:19:36 +00001080void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001081 // Target doesn't support this yet!
1082 abort();
1083}
1084
Chris Lattnera32814b2006-09-26 23:59:50 +00001085/// PrintSpecial - Print information related to the specified machine instr
1086/// that is independent of the operand, and may be independent of the instr
1087/// itself. This can be useful for portably encoding the comment character
1088/// or other bits of target-specific knowledge into the asmstrings. The
1089/// syntax used is ${:comment}. Targets can override this to add support
1090/// for their own strange codes.
1091void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattner30b47082006-09-27 00:06:07 +00001092 if (!strcmp(Code, "private")) {
1093 O << TAI->getPrivateGlobalPrefix();
1094 } else if (!strcmp(Code, "comment")) {
Chris Lattnera32814b2006-09-26 23:59:50 +00001095 O << TAI->getCommentString();
1096 } else if (!strcmp(Code, "uid")) {
1097 // Assign a unique ID to this machine instruction.
1098 static const MachineInstr *LastMI = 0;
Chris Lattnera9428c42007-02-05 21:23:52 +00001099 static const Function *F = 0;
Chris Lattnera32814b2006-09-26 23:59:50 +00001100 static unsigned Counter = 0U-1;
Chris Lattnera9428c42007-02-05 21:23:52 +00001101
1102 // Comparing the address of MI isn't sufficient, because machineinstrs may
1103 // be allocated to the same address across functions.
1104 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1105
Chris Lattnera32814b2006-09-26 23:59:50 +00001106 // If this is a new machine instruction, bump the counter.
Chris Lattnera9428c42007-02-05 21:23:52 +00001107 if (LastMI != MI || F != ThisF) {
1108 ++Counter;
1109 LastMI = MI;
Chris Lattner89e66e02007-02-06 01:56:31 +00001110 F = ThisF;
Chris Lattnera9428c42007-02-05 21:23:52 +00001111 }
Chris Lattnera32814b2006-09-26 23:59:50 +00001112 O << Counter;
1113 } else {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001114 cerr << "Unknown special formatter '" << Code
1115 << "' for machine instr: " << *MI;
Chris Lattnera32814b2006-09-26 23:59:50 +00001116 exit(1);
1117 }
1118}
1119
1120
Chris Lattner061d9e22006-01-27 02:10:10 +00001121/// printInlineAsm - This method formats and prints the specified machine
1122/// instruction that is an inline asm.
1123void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +00001124 unsigned NumOperands = MI->getNumOperands();
1125
1126 // Count the number of register definitions.
1127 unsigned NumDefs = 0;
Dan Gohman9da02f52007-09-14 20:33:02 +00001128 for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef();
Chris Lattner45456d42006-09-05 20:02:51 +00001129 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +00001130 assert(NumDefs != NumOperands-1 && "No asm string?");
1131
1132 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001133
1134 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +00001135 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +00001136
Dale Johannesen2b3bc302008-01-29 02:21:21 +00001137 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1138 // These are useful to see where empty asm's wound up.
Chris Lattner3390cbe2006-07-28 00:17:20 +00001139 if (AsmStr[0] == 0) {
Dan Gohman6896901e2008-06-30 22:03:41 +00001140 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattner3390cbe2006-07-28 00:17:20 +00001141 return;
1142 }
1143
Jim Laskeya6211dc2006-09-06 18:34:40 +00001144 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +00001145
Bill Wendlinge21237e2007-01-16 03:42:04 +00001146 // The variant of the current asmprinter.
1147 int AsmPrinterVariant = TAI->getAssemblerDialect();
1148
Chris Lattneraa23fa92006-02-01 22:41:11 +00001149 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1150 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +00001151
Chris Lattneraa23fa92006-02-01 22:41:11 +00001152 while (*LastEmitted) {
1153 switch (*LastEmitted) {
1154 default: {
1155 // Not a special case, emit the string section literally.
1156 const char *LiteralEnd = LastEmitted+1;
1157 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +00001158 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +00001159 ++LiteralEnd;
1160 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1161 O.write(LastEmitted, LiteralEnd-LastEmitted);
1162 LastEmitted = LiteralEnd;
1163 break;
1164 }
Chris Lattnera633c3132006-05-05 21:47:05 +00001165 case '\n':
1166 ++LastEmitted; // Consume newline character.
Dan Gohman6896901e2008-06-30 22:03:41 +00001167 O << '\n'; // Indent code with newline.
Chris Lattnera633c3132006-05-05 21:47:05 +00001168 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001169 case '$': {
1170 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001171 bool Done = true;
1172
1173 // Handle escapes.
1174 switch (*LastEmitted) {
1175 default: Done = false; break;
1176 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +00001177 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1178 O << '$';
1179 ++LastEmitted; // Consume second '$' character.
1180 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001181 case '(': // $( -> same as GCC's { character.
1182 ++LastEmitted; // Consume '(' character.
1183 if (CurVariant != -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001184 cerr << "Nested variants found in inline asm string: '"
1185 << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001186 exit(1);
1187 }
1188 CurVariant = 0; // We're in the first variant now.
1189 break;
1190 case '|':
1191 ++LastEmitted; // consume '|' character.
1192 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001193 cerr << "Found '|' character outside of variant in inline asm "
1194 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001195 exit(1);
1196 }
1197 ++CurVariant; // We're in the next variant.
1198 break;
1199 case ')': // $) -> same as GCC's } char.
1200 ++LastEmitted; // consume ')' character.
1201 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001202 cerr << "Found '}' character outside of variant in inline asm "
1203 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001204 exit(1);
1205 }
1206 CurVariant = -1;
1207 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001208 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001209 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001210
1211 bool HasCurlyBraces = false;
1212 if (*LastEmitted == '{') { // ${variable}
1213 ++LastEmitted; // Consume '{' character.
1214 HasCurlyBraces = true;
1215 }
1216
1217 const char *IDStart = LastEmitted;
1218 char *IDEnd;
Chris Lattnerd39e3882007-01-23 00:36:17 +00001219 errno = 0;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001220 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1221 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001222 cerr << "Bad $ operand number in inline asm string: '"
1223 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001224 exit(1);
1225 }
1226 LastEmitted = IDEnd;
1227
Chris Lattner34f74c12006-02-06 22:17:23 +00001228 char Modifier[2] = { 0, 0 };
1229
Chris Lattneraa23fa92006-02-01 22:41:11 +00001230 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +00001231 // If we have curly braces, check for a modifier character. This
1232 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1233 if (*LastEmitted == ':') {
1234 ++LastEmitted; // Consume ':' character.
1235 if (*LastEmitted == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001236 cerr << "Bad ${:} expression in inline asm string: '"
1237 << AsmStr << "'\n";
Chris Lattner34f74c12006-02-06 22:17:23 +00001238 exit(1);
1239 }
1240
1241 Modifier[0] = *LastEmitted;
1242 ++LastEmitted; // Consume modifier character.
1243 }
1244
Chris Lattneraa23fa92006-02-01 22:41:11 +00001245 if (*LastEmitted != '}') {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001246 cerr << "Bad ${} expression in inline asm string: '"
1247 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001248 exit(1);
1249 }
1250 ++LastEmitted; // Consume '}' character.
1251 }
1252
1253 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001254 cerr << "Invalid $ operand number in inline asm string: '"
1255 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001256 exit(1);
1257 }
1258
Chris Lattner571d9642006-02-23 19:21:04 +00001259 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +00001260 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +00001261 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1262 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001263
1264 bool Error = false;
1265
Chris Lattner571d9642006-02-23 19:21:04 +00001266 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +00001267 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001268 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner81798412007-12-30 20:50:28 +00001269 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner5af3fde2006-02-24 19:50:58 +00001270 OpNo += (OpFlags >> 3) + 1;
1271 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001272
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001273 if (OpNo >= MI->getNumOperands()) {
1274 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +00001275 } else {
Chris Lattner81798412007-12-30 20:50:28 +00001276 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001277 ++OpNo; // Skip over the ID number.
1278
Dale Johannesen4646aa32007-11-05 21:20:28 +00001279 if (Modifier[0]=='l') // labels are target independent
Chris Lattnera5bb3702007-12-30 23:10:15 +00001280 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Chengc7990652008-02-28 00:43:03 +00001281 false, false, false);
Dale Johannesen4646aa32007-11-05 21:20:28 +00001282 else {
1283 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1284 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1285 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1286 Modifier[0] ? Modifier : 0);
1287 } else {
1288 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1289 Modifier[0] ? Modifier : 0);
1290 }
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001291 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001292 }
1293 if (Error) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001294 cerr << "Invalid operand found in inline asm: '"
1295 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001296 MI->dump();
1297 exit(1);
1298 }
Chris Lattner571d9642006-02-23 19:21:04 +00001299 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001300 break;
1301 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001302 }
1303 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001304 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattneraa23fa92006-02-01 22:41:11 +00001305}
1306
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001307/// printImplicitDef - This method prints the specified machine instruction
1308/// that is an implicit def.
1309void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Dan Gohman6896901e2008-06-30 22:03:41 +00001310 O << '\t' << TAI->getCommentString() << " implicit-def: "
1311 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001312}
1313
Jim Laskeyf9e54452007-01-26 14:34:52 +00001314/// printLabel - This method prints a local label used by debug and
1315/// exception handling tables.
1316void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohmanb58aff42008-07-01 00:16:26 +00001317 printLabel(MI->getOperand(0).getImm());
Jim Laskeyf9e54452007-01-26 14:34:52 +00001318}
1319
Evan Chengd6e44ab2008-02-01 09:10:45 +00001320void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng32e53472008-02-02 08:39:46 +00001321 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chengd6e44ab2008-02-01 09:10:45 +00001322}
1323
Evan Chengefd142a2008-02-02 04:07:54 +00001324/// printDeclare - This method prints a local variable declaration used by
1325/// debug tables.
Evan Cheng2cb90682008-02-04 23:06:48 +00001326/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1327/// entry into dwarf table.
Evan Chengefd142a2008-02-02 04:07:54 +00001328void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Evan Cheng2cb90682008-02-04 23:06:48 +00001329 int FI = MI->getOperand(0).getIndex();
1330 GlobalValue *GV = MI->getOperand(1).getGlobal();
1331 MMI->RecordVariable(GV, FI);
Evan Chengefd142a2008-02-02 04:07:54 +00001332}
1333
Chris Lattneraa23fa92006-02-01 22:41:11 +00001334/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1335/// instruction, using the specified assembler variant. Targets should
1336/// overried this to format as appropriate.
1337bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +00001338 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +00001339 // Target doesn't support this yet!
1340 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +00001341}
Chris Lattner1d08c652006-02-24 20:21:58 +00001342
1343bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1344 unsigned AsmVariant,
1345 const char *ExtraCode) {
1346 // Target doesn't support this yet!
1347 return true;
1348}
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001349
1350/// printBasicBlockLabel - This method prints the label for the specified
1351/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +00001352void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Chengc7990652008-02-28 00:43:03 +00001353 bool printAlign,
Nate Begemanb9d4f832006-05-02 05:37:32 +00001354 bool printColon,
1355 bool printComment) const {
Evan Chengc7990652008-02-28 00:43:03 +00001356 if (printAlign) {
1357 unsigned Align = MBB->getAlignment();
1358 if (Align)
1359 EmitAlignment(Log2_32(Align));
1360 }
1361
Dan Gohman6896901e2008-06-30 22:03:41 +00001362 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Chengcdf36092007-10-14 05:57:21 +00001363 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +00001364 if (printColon)
1365 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +00001366 if (printComment && MBB->getBasicBlock())
Dan Gohman33d0ea22007-07-30 15:06:25 +00001367 O << '\t' << TAI->getCommentString() << ' '
Evan Cheng53495222008-07-08 00:55:58 +00001368 << MBB->getBasicBlock()->getNameStart();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001369}
Nate Begeman984c1a42006-08-12 21:29:52 +00001370
Evan Cheng797d56f2007-11-09 01:32:10 +00001371/// printPICJumpTableSetLabel - This method prints a set label for the
1372/// specified MachineBasicBlock for a jumptable entry.
1373void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1374 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001375 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +00001376 return;
1377
Jim Laskeya6211dc2006-09-06 18:34:40 +00001378 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001379 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001380 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001381 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1382 << '_' << uid << '\n';
Nate Begeman984c1a42006-08-12 21:29:52 +00001383}
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001384
Evan Cheng797d56f2007-11-09 01:32:10 +00001385void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1386 const MachineBasicBlock *MBB) const {
Evan Cheng91f120f2006-11-01 09:23:08 +00001387 if (!TAI->getSetDirective())
1388 return;
1389
1390 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001391 << getFunctionNumber() << '_' << uid << '_' << uid2
1392 << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001393 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001394 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1395 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng91f120f2006-11-01 09:23:08 +00001396}
1397
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001398/// printDataDirective - This method prints the asm directive for the
1399/// specified type.
1400void AsmPrinter::printDataDirective(const Type *type) {
1401 const TargetData *TD = TM.getTargetData();
1402 switch (type->getTypeID()) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001403 case Type::IntegerTyID: {
1404 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1405 if (BitWidth <= 8)
1406 O << TAI->getData8bitsDirective();
1407 else if (BitWidth <= 16)
1408 O << TAI->getData16bitsDirective();
1409 else if (BitWidth <= 32)
1410 O << TAI->getData32bitsDirective();
1411 else if (BitWidth <= 64) {
1412 assert(TAI->getData64bitsDirective() &&
1413 "Target cannot handle 64-bit constant exprs!");
1414 O << TAI->getData64bitsDirective();
1415 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001416 break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001417 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001418 case Type::PointerTyID:
1419 if (TD->getPointerSize() == 8) {
1420 assert(TAI->getData64bitsDirective() &&
1421 "Target cannot handle 64-bit pointer exprs!");
1422 O << TAI->getData64bitsDirective();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001423 } else {
1424 O << TAI->getData32bitsDirective();
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001425 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001426 break;
1427 case Type::FloatTyID: case Type::DoubleTyID:
Dale Johannesen6bf69ed2007-09-28 18:06:58 +00001428 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001429 assert (0 && "Should have already output floating point constant.");
1430 default:
1431 assert (0 && "Can't handle printing this type of thing");
1432 break;
1433 }
1434}
Dale Johannesen915e1542007-02-16 01:54:53 +00001435
Evan Cheng0a1e6722008-07-08 16:40:43 +00001436void AsmPrinter::printSuffixedName(const char *Name, const char *Suffix,
1437 const char *Prefix) {
Dale Johannesen5bf742f2008-05-19 21:38:18 +00001438 if (Name[0]=='\"')
Evan Cheng0a1e6722008-07-08 16:40:43 +00001439 O << '\"';
1440 O << TAI->getPrivateGlobalPrefix();
1441 if (Prefix) O << Prefix;
1442 if (Name[0]=='\"')
1443 O << '\"';
1444 if (Name[0]=='\"')
1445 O << Name[1];
Dale Johannesen5bf742f2008-05-19 21:38:18 +00001446 else
Evan Cheng0a1e6722008-07-08 16:40:43 +00001447 O << Name;
1448 O << Suffix;
1449 if (Name[0]=='\"')
1450 O << '\"';
Dale Johannesen5bf742f2008-05-19 21:38:18 +00001451}
Evan Cheng53495222008-07-08 00:55:58 +00001452
Evan Cheng0a1e6722008-07-08 16:40:43 +00001453void AsmPrinter::printSuffixedName(const std::string &Name, const char* Suffix) {
Evan Cheng53495222008-07-08 00:55:58 +00001454 printSuffixedName(Name.c_str(), Suffix);
1455}
Anton Korobeynikoved473292008-08-08 18:25:07 +00001456
1457void AsmPrinter::printVisibility(const std::string& Name,
1458 unsigned Visibility) const {
1459 if (Visibility == GlobalValue::HiddenVisibility) {
1460 if (const char *Directive = TAI->getHiddenDirective())
1461 O << Directive << Name << '\n';
1462 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1463 if (const char *Directive = TAI->getProtectedDirective())
1464 O << Directive << Name << '\n';
1465 }
1466}
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001467
Gordon Henriksend930f912008-08-17 18:44:35 +00001468GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1469 if (!S->usesMetadata())
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001470 return 0;
1471
Gordon Henriksend930f912008-08-17 18:44:35 +00001472 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001473 if (GCPI != GCMetadataPrinters.end())
1474 return GCPI->second;
1475
Gordon Henriksend930f912008-08-17 18:44:35 +00001476 const char *Name = S->getName().c_str();
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001477
1478 for (GCMetadataPrinterRegistry::iterator
1479 I = GCMetadataPrinterRegistry::begin(),
1480 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1481 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001482 GCMetadataPrinter *GMP = I->instantiate();
1483 GMP->S = S;
1484 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1485 return GMP;
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001486 }
1487
Gordon Henriksend930f912008-08-17 18:44:35 +00001488 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001489 abort();
1490}