blob: e80afd40eeda1d1145d05af1836a09c90aeb1ebf [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng932f0222006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng246ae0d2006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman8cfa57b2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman37efe672006-04-22 18:53:45 +000020#include "llvm/CodeGen/MachineJumpTableInfo.h"
Jim Laskeyf1cdea12007-01-25 15:12:02 +000021#include "llvm/Support/CommandLine.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000022#include "llvm/Support/Mangler.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000023#include "llvm/Support/MathExtras.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000024#include "llvm/Support/Streams.h"
Jim Laskey563321a2006-09-06 18:34:40 +000025#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000026#include "llvm/Target/TargetData.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000027#include "llvm/Target/TargetLowering.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000028#include "llvm/Target/TargetMachine.h"
Chris Lattner66099132006-02-01 22:41:11 +000029#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000030using namespace llvm;
31
Jim Laskeyf1cdea12007-01-25 15:12:02 +000032static cl::opt<bool>
33AsmVerbose("asm-verbose", cl::Hidden, cl::desc("Add comments to directives."));
34
Devang Patel19974732007-05-03 01:11:54 +000035char AsmPrinter::ID = 0;
Jim Laskeya0f3d172006-09-07 22:06:40 +000036AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
37 const TargetAsmInfo *T)
Devang Patel794fd752007-05-01 21:15:47 +000038 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o), TM(tm), TAI(T)
Jim Laskey563321a2006-09-06 18:34:40 +000039{}
Chris Lattnered138932005-12-13 06:32:10 +000040
Chris Lattner52f06702006-10-05 02:42:47 +000041std::string AsmPrinter::getSectionForFunction(const Function &F) const {
42 return TAI->getTextSection();
43}
44
Chris Lattnered138932005-12-13 06:32:10 +000045
Chris Lattner4632d7a2006-05-09 04:59:56 +000046/// SwitchToTextSection - Switch to the specified text section of the executable
47/// if we are not already in it!
Chris Lattnerac28fbd2005-11-21 07:06:27 +000048///
Chris Lattner4632d7a2006-05-09 04:59:56 +000049void AsmPrinter::SwitchToTextSection(const char *NewSection,
50 const GlobalValue *GV) {
Chris Lattnerac28fbd2005-11-21 07:06:27 +000051 std::string NS;
Chris Lattnera7090ae2006-05-09 05:24:50 +000052 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000053 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnera7090ae2006-05-09 05:24:50 +000054 else
55 NS = NewSection;
56
57 // If we're already in this section, we're done.
58 if (CurrentSection == NS) return;
Jeff Cohen51b776d2006-05-02 03:58:45 +000059
Chris Lattner7faec9b2006-05-09 05:33:48 +000060 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000061 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
62 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen51b776d2006-05-02 03:58:45 +000063
Chris Lattner7faec9b2006-05-09 05:33:48 +000064 CurrentSection = NS;
65
66 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000067 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattnerac28fbd2005-11-21 07:06:27 +000068}
69
Nate Begeman2f1ae882006-07-27 01:13:04 +000070/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner4632d7a2006-05-09 04:59:56 +000071/// if we are not already in it!
72///
73void AsmPrinter::SwitchToDataSection(const char *NewSection,
74 const GlobalValue *GV) {
75 std::string NS;
Chris Lattnerb81cb612006-05-09 05:23:12 +000076 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000077 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnerb81cb612006-05-09 05:23:12 +000078 else
79 NS = NewSection;
Chris Lattner4632d7a2006-05-09 04:59:56 +000080
Chris Lattnerb81cb612006-05-09 05:23:12 +000081 // If we're already in this section, we're done.
82 if (CurrentSection == NS) return;
83
Chris Lattner7faec9b2006-05-09 05:33:48 +000084 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000085 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
86 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner7faec9b2006-05-09 05:33:48 +000087
88 CurrentSection = NS;
Chris Lattner4632d7a2006-05-09 04:59:56 +000089
Chris Lattner7faec9b2006-05-09 05:33:48 +000090 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000091 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner4632d7a2006-05-09 04:59:56 +000092}
93
94
Chris Lattnera80ba712004-08-16 23:15:22 +000095bool AsmPrinter::doInitialization(Module &M) {
Jim Laskey563321a2006-09-06 18:34:40 +000096 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattner2c1b1592006-01-23 23:47:53 +000097
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000098 if (!M.getModuleInlineAsm().empty())
Jim Laskey563321a2006-09-06 18:34:40 +000099 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000100 << M.getModuleInlineAsm()
Jim Laskey563321a2006-09-06 18:34:40 +0000101 << "\n" << TAI->getCommentString()
102 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000103
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000104 SwitchToDataSection(""); // Reset back to no section.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000105
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000106 if (MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>()) {
107 MMI->AnalyzeModule(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000108 }
109
Chris Lattnera80ba712004-08-16 23:15:22 +0000110 return false;
111}
112
113bool AsmPrinter::doFinalization(Module &M) {
Rafael Espindola15404d02006-12-18 03:37:18 +0000114 if (TAI->getWeakRefDirective()) {
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000115 if (!ExtWeakSymbols.empty())
Rafael Espindola15404d02006-12-18 03:37:18 +0000116 SwitchToDataSection("");
117
118 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
119 e = ExtWeakSymbols.end(); i != e; ++i) {
120 const GlobalValue *GV = *i;
121 std::string Name = Mang->getValueName(GV);
122 O << TAI->getWeakRefDirective() << Name << "\n";
123 }
124 }
125
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000126 if (TAI->getSetDirective()) {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000127 if (!M.alias_empty())
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000128 SwitchToTextSection(TAI->getTextSection());
129
130 O << "\n";
131 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
132 I!=E; ++I) {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000133 std::string Name = Mang->getValueName(I);
134 std::string Target;
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000135
136 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
137 Target = Mang->getValueName(GV);
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000138
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000139 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000140 O << "\t.globl\t" << Name << "\n";
141 else if (I->hasWeakLinkage())
142 O << TAI->getWeakRefDirective() << Name << "\n";
143 else if (!I->hasInternalLinkage())
144 assert(0 && "Invalid alias linkage");
145
146 O << TAI->getSetDirective() << Name << ", " << Target << "\n";
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000147
148 // If the aliasee has external weak linkage it can be referenced only by
149 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
150 // weak reference in such case.
151 if (GV->hasExternalWeakLinkage())
152 if (TAI->getWeakRefDirective())
153 O << TAI->getWeakRefDirective() << Target << "\n";
154 else
155 O << "\t.globl\t" << Target << "\n";
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000156 }
157 }
158
Chris Lattnera80ba712004-08-16 23:15:22 +0000159 delete Mang; Mang = 0;
160 return false;
161}
162
Chris Lattner25045bd2005-11-21 07:51:36 +0000163void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000164 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +0000165 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner77bc2282005-11-21 08:13:27 +0000166 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +0000167}
168
Chris Lattner3b4fd322005-11-21 08:25:09 +0000169/// EmitConstantPool - Print to the current output stream assembly
170/// representations of the constants in the constant pool MCP. This is
171/// used to print out constants which have been "spilled to memory" by
172/// the code generator.
173///
174void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000175 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000176 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000177
178 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
179 // in special sections.
180 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
181 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
182 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
183 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Chengd6594ae2006-09-12 21:00:35 +0000184 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000185 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng2d2cec12006-06-29 00:26:09 +0000186 MachineConstantPoolEntry CPE = CP[i];
Evan Chengd5a99d72006-09-14 07:35:00 +0000187 const Type *Ty = CPE.getType();
Jim Laskey563321a2006-09-06 18:34:40 +0000188 if (TAI->getFourByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000189 TM.getTargetData()->getTypeSize(Ty) == 4)
190 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000191 else if (TAI->getEightByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000192 TM.getTargetData()->getTypeSize(Ty) == 8)
193 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000194 else if (TAI->getSixteenByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000195 TM.getTargetData()->getTypeSize(Ty) == 16)
196 SixteenByteCPs.push_back(std::make_pair(CPE, i));
197 else
198 OtherCPs.push_back(std::make_pair(CPE, i));
199 }
200
201 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskey563321a2006-09-06 18:34:40 +0000202 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
203 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
204 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
205 SixteenByteCPs);
206 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000207}
208
209void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
210 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
211 if (CP.empty()) return;
212
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000213 SwitchToDataSection(Section);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000214 EmitAlignment(Alignment);
215 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Jim Laskey563321a2006-09-06 18:34:40 +0000216 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
217 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Chengd5a99d72006-09-14 07:35:00 +0000218 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
219 if (CP[i].first.isMachineConstantPoolEntry())
Evan Chengd6594ae2006-09-12 21:00:35 +0000220 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Chengd5a99d72006-09-14 07:35:00 +0000221 else
Evan Chengd6594ae2006-09-12 21:00:35 +0000222 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000223 if (i != e-1) {
Evan Chengd5a99d72006-09-14 07:35:00 +0000224 const Type *Ty = CP[i].first.getType();
Evan Cheng2d2cec12006-06-29 00:26:09 +0000225 unsigned EntSize =
Evan Chengd6594ae2006-09-12 21:00:35 +0000226 TM.getTargetData()->getTypeSize(Ty);
Evan Chengd5a99d72006-09-14 07:35:00 +0000227 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattner3029f922006-02-09 04:46:04 +0000228 // Emit inter-object padding for alignment.
Evan Chengd5a99d72006-09-14 07:35:00 +0000229 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattner3029f922006-02-09 04:46:04 +0000230 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000231 }
232}
233
Nate Begeman37efe672006-04-22 18:53:45 +0000234/// EmitJumpTableInfo - Print assembly representations of the jump tables used
235/// by the current function to the current output stream.
236///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000237void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
238 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000239 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
240 if (JT.empty()) return;
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000241 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000242
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000243 // Use JumpTableDirective otherwise honor the entry size from the jump table
244 // info.
Andrew Lenharthbeec30e2006-09-24 19:45:58 +0000245 const char *JTEntryDirective = TAI->getJumpTableDirective();
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000246 bool HadJTEntryDirective = JTEntryDirective != NULL;
247 if (!HadJTEntryDirective) {
248 JTEntryDirective = MJTI->getEntrySize() == 4 ?
249 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
250 }
Nate Begeman37efe672006-04-22 18:53:45 +0000251
Nate Begeman2f1ae882006-07-27 01:13:04 +0000252 // Pick the directive to use to print the jump table entries, and switch to
253 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000254 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000255
256 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
257 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
258 !JumpTableDataSection) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000259 // In PIC mode, we need to emit the jump table to the same section as the
260 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000261 // We should also do if the section name is NULL.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000262 const Function *F = MF.getFunction();
263 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000264 } else {
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000265 SwitchToDataSection(JumpTableDataSection);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000266 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000267
268 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000269
Nate Begeman37efe672006-04-22 18:53:45 +0000270 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000271 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000272
273 // If this jump table was deleted, ignore it.
274 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000275
276 // For PIC codegen, if possible we want to use the SetDirective to reduce
277 // the number of relocations the assembler will generate for the jump table.
278 // Set directives are all printed before the jump table itself.
279 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000280 if (TAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000281 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
282 if (EmittedSets.insert(JTBBs[ii]).second)
283 printSetLabel(i, JTBBs[ii]);
284
Chris Lattner393a8ee2007-01-18 01:12:56 +0000285 // On some targets (e.g. darwin) we want to emit two consequtive labels
286 // before each jump table. The first label is never referenced, but tells
287 // the assembler and linker the extents of the jump table object. The
288 // second label is actually referenced by the code.
289 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
290 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
291
Jim Laskey563321a2006-09-06 18:34:40 +0000292 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
293 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000294
Nate Begeman37efe672006-04-22 18:53:45 +0000295 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000296 O << JTEntryDirective << ' ';
Nate Begeman52a51e382006-08-12 21:29:52 +0000297 // If we have emitted set directives for the jump table entries, print
298 // them rather than the entries themselves. If we're emitting PIC, then
299 // emit the table entries as differences between two text section labels.
300 // If we're emitting non-PIC code, then emit the entries as direct
301 // references to the target basic blocks.
302 if (!EmittedSets.empty()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000303 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
304 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000305 } else if (IsPic) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000306 printBasicBlockLabel(JTBBs[ii], false, false);
Chris Lattner393a8ee2007-01-18 01:12:56 +0000307 // If the arch uses custom Jump Table directives, don't calc relative to
308 // JT
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000309 if (!HadJTEntryDirective)
310 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
311 << getFunctionNumber() << '_' << i;
Nate Begeman52a51e382006-08-12 21:29:52 +0000312 } else {
313 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000314 }
Nate Begeman37efe672006-04-22 18:53:45 +0000315 O << '\n';
316 }
317 }
318}
319
Chris Lattnered138932005-12-13 06:32:10 +0000320/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
321/// special global used by LLVM. If so, emit it and return true, otherwise
322/// do nothing and return false.
323bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000324 if (GV->getName() == "llvm.used") {
325 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
326 EmitLLVMUsedList(GV->getInitializer());
327 return true;
328 }
329
Jim Laskey78098112006-03-07 22:00:35 +0000330 // Ignore debug and non-emitted data.
331 if (GV->getSection() == "llvm.metadata") return true;
332
333 if (!GV->hasAppendingLinkage()) return false;
334
335 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000336
Evan Cheng916d07c2007-06-04 20:39:18 +0000337 const TargetData *TD = TM.getTargetData();
338 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner5166b822006-01-12 19:17:23 +0000339 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000340 SwitchToDataSection(TAI->getStaticCtorsSection());
Evan Cheng916d07c2007-06-04 20:39:18 +0000341 EmitAlignment(Align, 0);
Chris Lattnered138932005-12-13 06:32:10 +0000342 EmitXXStructorList(GV->getInitializer());
343 return true;
344 }
345
Chris Lattner5166b822006-01-12 19:17:23 +0000346 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000347 SwitchToDataSection(TAI->getStaticDtorsSection());
Evan Cheng916d07c2007-06-04 20:39:18 +0000348 EmitAlignment(Align, 0);
Chris Lattnered138932005-12-13 06:32:10 +0000349 EmitXXStructorList(GV->getInitializer());
350 return true;
351 }
352
353 return false;
354}
355
Chris Lattnercb05af82006-09-26 03:38:18 +0000356/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
357/// global in the specified llvm.used list as being used with this directive.
358void AsmPrinter::EmitLLVMUsedList(Constant *List) {
359 const char *Directive = TAI->getUsedDirective();
360
361 // Should be an array of 'sbyte*'.
362 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
363 if (InitList == 0) return;
364
365 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
366 O << Directive;
367 EmitConstantValueOnly(InitList->getOperand(i));
368 O << "\n";
369 }
370}
371
Chris Lattnered138932005-12-13 06:32:10 +0000372/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
373/// function pointers, ignoring the init priority.
374void AsmPrinter::EmitXXStructorList(Constant *List) {
375 // Should be an array of '{ int, void ()* }' structs. The first value is the
376 // init priority, which we ignore.
377 if (!isa<ConstantArray>(List)) return;
378 ConstantArray *InitList = cast<ConstantArray>(List);
379 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
380 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
381 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000382
383 if (CS->getOperand(1)->isNullValue())
384 return; // Found a null terminator, exit printing.
385 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000386 EmitGlobalConstant(CS->getOperand(1));
387 }
388}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000389
Jim Laskeya1a19f82006-10-17 13:41:07 +0000390/// getGlobalLinkName - Returns the asm/link name of of the specified
391/// global variable. Should be overridden by each target asm printer to
392/// generate the appropriate value.
Jim Laskey99e41ee2006-10-17 17:17:24 +0000393const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
394 std::string LinkName;
Jim Laskey03742482006-11-20 20:29:06 +0000395
396 if (isa<Function>(GV)) {
397 LinkName += TAI->getFunctionAddrPrefix();
398 LinkName += Mang->getValueName(GV);
399 LinkName += TAI->getFunctionAddrSuffix();
400 } else {
401 LinkName += TAI->getGlobalVarAddrPrefix();
402 LinkName += Mang->getValueName(GV);
403 LinkName += TAI->getGlobalVarAddrSuffix();
404 }
405
Jim Laskey99e41ee2006-10-17 17:17:24 +0000406 return LinkName;
Jim Laskeya1a19f82006-10-17 13:41:07 +0000407}
408
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000409/// EmitExternalGlobal - Emit the external reference to a global variable.
410/// Should be overridden if an indirect reference should be used.
411void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
412 O << getGlobalLinkName(GV);
413}
414
415
416
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000417//===----------------------------------------------------------------------===//
418/// LEB 128 number encoding.
419
420/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
421/// representing an unsigned leb128 value.
422void AsmPrinter::PrintULEB128(unsigned Value) const {
423 do {
424 unsigned Byte = Value & 0x7f;
425 Value >>= 7;
426 if (Value) Byte |= 0x80;
427 O << "0x" << std::hex << Byte << std::dec;
428 if (Value) O << ", ";
429 } while (Value);
430}
431
432/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
433/// value.
434unsigned AsmPrinter::SizeULEB128(unsigned Value) {
435 unsigned Size = 0;
436 do {
437 Value >>= 7;
438 Size += sizeof(int8_t);
439 } while (Value);
440 return Size;
441}
442
443/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
444/// representing a signed leb128 value.
445void AsmPrinter::PrintSLEB128(int Value) const {
446 int Sign = Value >> (8 * sizeof(Value) - 1);
447 bool IsMore;
448
449 do {
450 unsigned Byte = Value & 0x7f;
451 Value >>= 7;
452 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
453 if (IsMore) Byte |= 0x80;
454 O << "0x" << std::hex << Byte << std::dec;
455 if (IsMore) O << ", ";
456 } while (IsMore);
457}
458
459/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
460/// value.
461unsigned AsmPrinter::SizeSLEB128(int Value) {
462 unsigned Size = 0;
463 int Sign = Value >> (8 * sizeof(Value) - 1);
464 bool IsMore;
465
466 do {
467 unsigned Byte = Value & 0x7f;
468 Value >>= 7;
469 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
470 Size += sizeof(int8_t);
471 } while (IsMore);
472 return Size;
473}
474
475//===--------------------------------------------------------------------===//
476// Emission and print routines
477//
478
479/// PrintHex - Print a value as a hexidecimal value.
480///
481void AsmPrinter::PrintHex(int Value) const {
482 O << "0x" << std::hex << Value << std::dec;
483}
484
485/// EOL - Print a newline character to asm stream. If a comment is present
486/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000487void AsmPrinter::EOL() const {
488 O << "\n";
489}
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000490void AsmPrinter::EOL(const std::string &Comment) const {
491 if (AsmVerbose && !Comment.empty()) {
492 O << "\t"
493 << TAI->getCommentString()
494 << " "
495 << Comment;
496 }
497 O << "\n";
498}
499
500/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
501/// unsigned leb128 value.
502void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
503 if (TAI->hasLEB128()) {
504 O << "\t.uleb128\t"
505 << Value;
506 } else {
507 O << TAI->getData8bitsDirective();
508 PrintULEB128(Value);
509 }
510}
511
512/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
513/// signed leb128 value.
514void AsmPrinter::EmitSLEB128Bytes(int Value) const {
515 if (TAI->hasLEB128()) {
516 O << "\t.sleb128\t"
517 << Value;
518 } else {
519 O << TAI->getData8bitsDirective();
520 PrintSLEB128(Value);
521 }
522}
523
524/// EmitInt8 - Emit a byte directive and value.
525///
526void AsmPrinter::EmitInt8(int Value) const {
527 O << TAI->getData8bitsDirective();
528 PrintHex(Value & 0xFF);
529}
530
531/// EmitInt16 - Emit a short directive and value.
532///
533void AsmPrinter::EmitInt16(int Value) const {
534 O << TAI->getData16bitsDirective();
535 PrintHex(Value & 0xFFFF);
536}
537
538/// EmitInt32 - Emit a long directive and value.
539///
540void AsmPrinter::EmitInt32(int Value) const {
541 O << TAI->getData32bitsDirective();
542 PrintHex(Value);
543}
544
545/// EmitInt64 - Emit a long long directive and value.
546///
547void AsmPrinter::EmitInt64(uint64_t Value) const {
548 if (TAI->getData64bitsDirective()) {
549 O << TAI->getData64bitsDirective();
550 PrintHex(Value);
551 } else {
552 if (TM.getTargetData()->isBigEndian()) {
553 EmitInt32(unsigned(Value >> 32)); O << "\n";
554 EmitInt32(unsigned(Value));
555 } else {
556 EmitInt32(unsigned(Value)); O << "\n";
557 EmitInt32(unsigned(Value >> 32));
558 }
559 }
560}
561
562/// toOctal - Convert the low order bits of X into an octal digit.
563///
564static inline char toOctal(int X) {
565 return (X&7)+'0';
566}
567
568/// printStringChar - Print a char, escaped if necessary.
569///
570static void printStringChar(std::ostream &O, unsigned char C) {
571 if (C == '"') {
572 O << "\\\"";
573 } else if (C == '\\') {
574 O << "\\\\";
575 } else if (isprint(C)) {
576 O << C;
577 } else {
578 switch(C) {
579 case '\b': O << "\\b"; break;
580 case '\f': O << "\\f"; break;
581 case '\n': O << "\\n"; break;
582 case '\r': O << "\\r"; break;
583 case '\t': O << "\\t"; break;
584 default:
585 O << '\\';
586 O << toOctal(C >> 6);
587 O << toOctal(C >> 3);
588 O << toOctal(C >> 0);
589 break;
590 }
591 }
592}
593
594/// EmitString - Emit a string with quotes and a null terminator.
595/// Special characters are emitted properly.
596/// \literal (Eg. '\t') \endliteral
597void AsmPrinter::EmitString(const std::string &String) const {
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000598 const char* AscizDirective = TAI->getAscizDirective();
599 if (AscizDirective)
600 O << AscizDirective;
601 else
602 O << TAI->getAsciiDirective();
603 O << "\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000604 for (unsigned i = 0, N = String.size(); i < N; ++i) {
605 unsigned char C = String[i];
606 printStringChar(O, C);
607 }
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000608 if (AscizDirective)
609 O << "\"";
610 else
611 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000612}
613
614
615//===----------------------------------------------------------------------===//
616
Chris Lattner3a420532007-05-31 18:57:45 +0000617// EmitAlignment - Emit an alignment directive to the specified power of
618// two boundary. For example, if you pass in 3 here, you will get an 8
619// byte alignment. If a global value is specified, and if that global has
620// an explicit alignment requested, it will unconditionally override the
621// alignment request. However, if ForcedAlignBits is specified, this value
622// has final say: the ultimate alignment will be the max of ForcedAlignBits
623// and the alignment computed with NumBits and the global.
624//
625// The algorithm is:
626// Align = NumBits;
627// if (GV && GV->hasalignment) Align = GV->getalignment();
628// Align = std::max(Align, ForcedAlignBits);
629//
630void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng73a259a2007-07-25 23:35:07 +0000631 unsigned ForcedAlignBits, bool UseFillExpr,
632 unsigned FillValue) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000633 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000634 NumBits = Log2_32(GV->getAlignment());
635 NumBits = std::max(NumBits, ForcedAlignBits);
636
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000637 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskey563321a2006-09-06 18:34:40 +0000638 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng73a259a2007-07-25 23:35:07 +0000639 O << TAI->getAlignDirective() << NumBits;
640 if (UseFillExpr) O << ",0x" << std::hex << FillValue << std::dec;
641 O << "\n";
Chris Lattnerbfddc202004-08-17 19:14:29 +0000642}
643
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000644
Chris Lattner25045bd2005-11-21 07:51:36 +0000645/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000646///
Chris Lattner25045bd2005-11-21 07:51:36 +0000647void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000648 if (NumZeros) {
Jim Laskey563321a2006-09-06 18:34:40 +0000649 if (TAI->getZeroDirective()) {
650 O << TAI->getZeroDirective() << NumZeros;
651 if (TAI->getZeroDirectiveSuffix())
652 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000653 O << "\n";
654 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000655 for (; NumZeros; --NumZeros)
Jim Laskey563321a2006-09-06 18:34:40 +0000656 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000657 }
658 }
659}
660
Chris Lattnera80ba712004-08-16 23:15:22 +0000661// Print out the specified constant, without a storage class. Only the
662// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000663void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000664 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000665 O << "0";
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000666 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattnera875df32007-01-12 18:15:09 +0000667 O << CI->getZExtValue();
Reid Spencerb83eb642006-10-20 07:07:24 +0000668 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000669 // This is a constant address for a global variable or function. Use the
670 // name of the variable or function as the address value, possibly
671 // decorating it with GlobalVarAddrPrefix/Suffix or
672 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskey563321a2006-09-06 18:34:40 +0000673 if (isa<Function>(GV)) {
674 O << TAI->getFunctionAddrPrefix()
675 << Mang->getValueName(GV)
676 << TAI->getFunctionAddrSuffix();
677 } else {
678 O << TAI->getGlobalVarAddrPrefix()
679 << Mang->getValueName(GV)
680 << TAI->getGlobalVarAddrSuffix();
681 }
Duraid Madina855a5192005-04-02 12:21:51 +0000682 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000683 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000684 unsigned Opcode = CE->getOpcode();
685 switch (Opcode) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000686 case Instruction::GetElementPtr: {
687 // generate a symbolic expression for the byte address
688 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000689 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
690 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
691 idxVec.size())) {
Chris Lattner27e19212005-02-14 21:40:26 +0000692 if (Offset)
693 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000694 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000695 if (Offset > 0)
696 O << ") + " << Offset;
697 else if (Offset < 0)
698 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000699 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000700 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000701 }
702 break;
703 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000704 case Instruction::Trunc:
705 case Instruction::ZExt:
706 case Instruction::SExt:
707 case Instruction::FPTrunc:
708 case Instruction::FPExt:
709 case Instruction::UIToFP:
710 case Instruction::SIToFP:
711 case Instruction::FPToUI:
712 case Instruction::FPToSI:
713 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
714 break;
Chris Lattnercb0a6812006-12-12 05:14:13 +0000715 case Instruction::BitCast:
716 return EmitConstantValueOnly(CE->getOperand(0));
717
Chris Lattnerddc94012006-12-12 05:18:19 +0000718 case Instruction::IntToPtr: {
719 // Handle casts to pointers by changing them into casts to the appropriate
720 // integer type. This promotes constant folding and simplifies this code.
721 Constant *Op = CE->getOperand(0);
722 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
723 return EmitConstantValueOnly(Op);
724 }
725
726
727 case Instruction::PtrToInt: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000728 // Support only foldable casts to/from pointers that can be eliminated by
729 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000730 Constant *Op = CE->getOperand(0);
Chris Lattnerddc94012006-12-12 05:18:19 +0000731 const Type *Ty = CE->getType();
Chris Lattnera80ba712004-08-16 23:15:22 +0000732
Chris Lattnerddc94012006-12-12 05:18:19 +0000733 // We can emit the pointer value into this slot if the slot is an
734 // integer slot greater or equal to the size of the pointer.
Chris Lattner42a75512007-01-15 02:27:26 +0000735 if (Ty->isInteger() &&
Reid Spencera54b7cb2007-01-12 07:05:14 +0000736 TD->getTypeSize(Ty) >= TD->getTypeSize(Op->getType()))
Chris Lattner4a6bd332006-07-29 01:57:19 +0000737 return EmitConstantValueOnly(Op);
Chris Lattner4a6bd332006-07-29 01:57:19 +0000738
739 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000740 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000741 break;
742 }
743 case Instruction::Add:
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000744 case Instruction::Sub:
Chris Lattnera80ba712004-08-16 23:15:22 +0000745 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000746 EmitConstantValueOnly(CE->getOperand(0));
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000747 O << (Opcode==Instruction::Add ? ") + (" : ") - (");
Chris Lattner25045bd2005-11-21 07:51:36 +0000748 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000749 O << ")";
750 break;
751 default:
752 assert(0 && "Unsupported operator!");
753 }
754 } else {
755 assert(0 && "Unknown constant value!");
756 }
757}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000758
Chris Lattner2980cef2005-11-10 18:06:33 +0000759/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000760/// the predicate isString is true.
761///
Chris Lattner2980cef2005-11-10 18:06:33 +0000762static void printAsCString(std::ostream &O, const ConstantArray *CVA,
763 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000764 assert(CVA->isString() && "Array is not string compatible!");
765
766 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000767 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000768 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000769 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000770 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000771 }
772 O << "\"";
773}
774
Jeff Cohenc884db42006-05-02 01:16:28 +0000775/// EmitString - Emit a zero-byte-terminated string constant.
776///
777void AsmPrinter::EmitString(const ConstantArray *CVA) const {
778 unsigned NumElts = CVA->getNumOperands();
Jim Laskey563321a2006-09-06 18:34:40 +0000779 if (TAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000780 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskey563321a2006-09-06 18:34:40 +0000781 O << TAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000782 printAsCString(O, CVA, NumElts-1);
783 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000784 O << TAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000785 printAsCString(O, CVA, NumElts);
786 }
787 O << "\n";
788}
789
Chris Lattner25045bd2005-11-21 07:51:36 +0000790/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000791///
Chris Lattner25045bd2005-11-21 07:51:36 +0000792void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Andersona69571c2006-05-03 01:29:57 +0000793 const TargetData *TD = TM.getTargetData();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000794
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000795 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000796 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000797 return;
798 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
799 if (CVA->isString()) {
Jeff Cohenc884db42006-05-02 01:16:28 +0000800 EmitString(CVA);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000801 } else { // Not a string. Print the values in successive locations
802 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000803 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000804 }
805 return;
806 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
807 // Print the fields in successive locations. Pad to align if needed!
Owen Andersona69571c2006-05-03 01:29:57 +0000808 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000809 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000810 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
811 const Constant* field = CVS->getOperand(i);
812
813 // Check if padding is needed and insert one or more 0s.
Owen Andersona69571c2006-05-03 01:29:57 +0000814 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattnerb0c39a32007-02-10 19:59:22 +0000815 uint64_t padSize = ((i == e-1? cvsLayout->getSizeInBytes()
Chris Lattnerb1919e22007-02-10 19:55:17 +0000816 : cvsLayout->getElementOffset(i+1))
817 - cvsLayout->getElementOffset(i)) - fieldSize;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000818 sizeSoFar += fieldSize + padSize;
819
820 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000821 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000822
823 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000824 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000825 }
Chris Lattnerb0c39a32007-02-10 19:59:22 +0000826 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
Chris Lattner1b7e2352004-08-17 06:36:49 +0000827 "Layout of constant struct may be incorrect!");
828 return;
829 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
830 // FP Constants are printed as integer constants to avoid losing
831 // precision...
Chris Lattner1b7e2352004-08-17 06:36:49 +0000832 if (CFP->getType() == Type::DoubleTy) {
Dale Johannesen43421b32007-09-06 18:13:44 +0000833 double Val = CFP->getValueAPF().convertToDouble();
Jim Laskey563321a2006-09-06 18:34:40 +0000834 if (TAI->getData64bitsDirective())
835 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
836 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000837 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000838 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
839 << "\t" << TAI->getCommentString()
840 << " double most significant word " << Val << "\n";
841 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
842 << "\t" << TAI->getCommentString()
843 << " double least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000844 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000845 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
846 << "\t" << TAI->getCommentString()
847 << " double least significant word " << Val << "\n";
848 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
849 << "\t" << TAI->getCommentString()
850 << " double most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000851 }
852 return;
853 } else {
Dale Johannesen43421b32007-09-06 18:13:44 +0000854 float Val = CFP->getValueAPF().convertToFloat();
Jim Laskey563321a2006-09-06 18:34:40 +0000855 O << TAI->getData32bitsDirective() << FloatToBits(Val)
856 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000857 return;
858 }
Reid Spencer47857812006-12-31 05:55:36 +0000859 } else if (CV->getType() == Type::Int64Ty) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000860 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000861 uint64_t Val = CI->getZExtValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000862
Jim Laskey563321a2006-09-06 18:34:40 +0000863 if (TAI->getData64bitsDirective())
864 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000865 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000866 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
867 << "\t" << TAI->getCommentString()
868 << " Double-word most significant word " << Val << "\n";
869 O << TAI->getData32bitsDirective() << unsigned(Val)
870 << "\t" << TAI->getCommentString()
871 << " Double-word least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000872 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000873 O << TAI->getData32bitsDirective() << unsigned(Val)
874 << "\t" << TAI->getCommentString()
875 << " Double-word least significant word " << Val << "\n";
876 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
877 << "\t" << TAI->getCommentString()
878 << " Double-word most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000879 }
880 return;
881 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000882 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
883 const VectorType *PTy = CP->getType();
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000884
885 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
886 EmitGlobalConstant(CP->getOperand(I));
887
888 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000889 }
890
891 const Type *type = CV->getType();
Evan Chengd6594ae2006-09-12 21:00:35 +0000892 printDataDirective(type);
Chris Lattner25045bd2005-11-21 07:51:36 +0000893 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000894 O << "\n";
895}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000896
Evan Chengd6594ae2006-09-12 21:00:35 +0000897void
898AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
899 // Target doesn't support this yet!
900 abort();
901}
902
Chris Lattner3ce9b672006-09-26 23:59:50 +0000903/// PrintSpecial - Print information related to the specified machine instr
904/// that is independent of the operand, and may be independent of the instr
905/// itself. This can be useful for portably encoding the comment character
906/// or other bits of target-specific knowledge into the asmstrings. The
907/// syntax used is ${:comment}. Targets can override this to add support
908/// for their own strange codes.
909void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattnerbae02cf2006-09-27 00:06:07 +0000910 if (!strcmp(Code, "private")) {
911 O << TAI->getPrivateGlobalPrefix();
912 } else if (!strcmp(Code, "comment")) {
Chris Lattner3ce9b672006-09-26 23:59:50 +0000913 O << TAI->getCommentString();
914 } else if (!strcmp(Code, "uid")) {
915 // Assign a unique ID to this machine instruction.
916 static const MachineInstr *LastMI = 0;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +0000917 static const Function *F = 0;
Chris Lattner3ce9b672006-09-26 23:59:50 +0000918 static unsigned Counter = 0U-1;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +0000919
920 // Comparing the address of MI isn't sufficient, because machineinstrs may
921 // be allocated to the same address across functions.
922 const Function *ThisF = MI->getParent()->getParent()->getFunction();
923
Chris Lattner3ce9b672006-09-26 23:59:50 +0000924 // If this is a new machine instruction, bump the counter.
Chris Lattnerb6a24bf2007-02-05 21:23:52 +0000925 if (LastMI != MI || F != ThisF) {
926 ++Counter;
927 LastMI = MI;
Chris Lattner4b092522007-02-06 01:56:31 +0000928 F = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +0000929 }
Chris Lattner3ce9b672006-09-26 23:59:50 +0000930 O << Counter;
931 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +0000932 cerr << "Unknown special formatter '" << Code
933 << "' for machine instr: " << *MI;
Chris Lattner3ce9b672006-09-26 23:59:50 +0000934 exit(1);
935 }
936}
937
938
Chris Lattner0264d1a2006-01-27 02:10:10 +0000939/// printInlineAsm - This method formats and prints the specified machine
940/// instruction that is an inline asm.
941void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000942 unsigned NumOperands = MI->getNumOperands();
943
944 // Count the number of register definitions.
945 unsigned NumDefs = 0;
Chris Lattner67942f52006-09-05 20:02:51 +0000946 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
947 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000948 assert(NumDefs != NumOperands-1 && "No asm string?");
949
950 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +0000951
952 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000953 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +0000954
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000955 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
956 if (AsmStr[0] == 0) {
957 O << "\n"; // Tab already printed, avoid double indenting next instr.
958 return;
959 }
960
Jim Laskey563321a2006-09-06 18:34:40 +0000961 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000962
Bill Wendlingeb9a42c2007-01-16 03:42:04 +0000963 // The variant of the current asmprinter.
964 int AsmPrinterVariant = TAI->getAssemblerDialect();
965
Chris Lattner66099132006-02-01 22:41:11 +0000966 int CurVariant = -1; // The number of the {.|.|.} region we are in.
967 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +0000968
Chris Lattner66099132006-02-01 22:41:11 +0000969 while (*LastEmitted) {
970 switch (*LastEmitted) {
971 default: {
972 // Not a special case, emit the string section literally.
973 const char *LiteralEnd = LastEmitted+1;
974 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +0000975 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +0000976 ++LiteralEnd;
977 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
978 O.write(LastEmitted, LiteralEnd-LastEmitted);
979 LastEmitted = LiteralEnd;
980 break;
981 }
Chris Lattner1c059972006-05-05 21:47:05 +0000982 case '\n':
983 ++LastEmitted; // Consume newline character.
Chris Lattner1f6f4c72007-04-30 17:00:18 +0000984 O << "\n"; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +0000985 break;
Chris Lattner66099132006-02-01 22:41:11 +0000986 case '$': {
987 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000988 bool Done = true;
989
990 // Handle escapes.
991 switch (*LastEmitted) {
992 default: Done = false; break;
993 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +0000994 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
995 O << '$';
996 ++LastEmitted; // Consume second '$' character.
997 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000998 case '(': // $( -> same as GCC's { character.
999 ++LastEmitted; // Consume '(' character.
1000 if (CurVariant != -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001001 cerr << "Nested variants found in inline asm string: '"
1002 << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001003 exit(1);
1004 }
1005 CurVariant = 0; // We're in the first variant now.
1006 break;
1007 case '|':
1008 ++LastEmitted; // consume '|' character.
1009 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001010 cerr << "Found '|' character outside of variant in inline asm "
1011 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001012 exit(1);
1013 }
1014 ++CurVariant; // We're in the next variant.
1015 break;
1016 case ')': // $) -> same as GCC's } char.
1017 ++LastEmitted; // consume ')' character.
1018 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001019 cerr << "Found '}' character outside of variant in inline asm "
1020 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001021 exit(1);
1022 }
1023 CurVariant = -1;
1024 break;
Chris Lattner66099132006-02-01 22:41:11 +00001025 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001026 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001027
1028 bool HasCurlyBraces = false;
1029 if (*LastEmitted == '{') { // ${variable}
1030 ++LastEmitted; // Consume '{' character.
1031 HasCurlyBraces = true;
1032 }
1033
1034 const char *IDStart = LastEmitted;
1035 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001036 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001037 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1038 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001039 cerr << "Bad $ operand number in inline asm string: '"
1040 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001041 exit(1);
1042 }
1043 LastEmitted = IDEnd;
1044
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001045 char Modifier[2] = { 0, 0 };
1046
Chris Lattner66099132006-02-01 22:41:11 +00001047 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001048 // If we have curly braces, check for a modifier character. This
1049 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1050 if (*LastEmitted == ':') {
1051 ++LastEmitted; // Consume ':' character.
1052 if (*LastEmitted == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001053 cerr << "Bad ${:} expression in inline asm string: '"
1054 << AsmStr << "'\n";
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001055 exit(1);
1056 }
1057
1058 Modifier[0] = *LastEmitted;
1059 ++LastEmitted; // Consume modifier character.
1060 }
1061
Chris Lattner66099132006-02-01 22:41:11 +00001062 if (*LastEmitted != '}') {
Bill Wendlinge8156192006-12-07 01:30:32 +00001063 cerr << "Bad ${} expression in inline asm string: '"
1064 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001065 exit(1);
1066 }
1067 ++LastEmitted; // Consume '}' character.
1068 }
1069
1070 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001071 cerr << "Invalid $ operand number in inline asm string: '"
1072 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001073 exit(1);
1074 }
1075
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001076 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001077 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001078 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1079 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001080
1081 bool Error = false;
1082
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001083 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001084 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001085 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001086 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1087 OpNo += (OpFlags >> 3) + 1;
1088 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001089
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001090 if (OpNo >= MI->getNumOperands()) {
1091 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001092 } else {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001093 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1094 ++OpNo; // Skip over the ID number.
1095
1096 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1097 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1098 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1099 Modifier[0] ? Modifier : 0);
1100 } else {
1101 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1102 Modifier[0] ? Modifier : 0);
1103 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001104 }
1105 if (Error) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001106 cerr << "Invalid operand found in inline asm: '"
1107 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001108 MI->dump();
1109 exit(1);
1110 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001111 }
Chris Lattner66099132006-02-01 22:41:11 +00001112 break;
1113 }
Chris Lattner66099132006-02-01 22:41:11 +00001114 }
1115 }
Jim Laskey563321a2006-09-06 18:34:40 +00001116 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattner66099132006-02-01 22:41:11 +00001117}
1118
Jim Laskey1ee29252007-01-26 14:34:52 +00001119/// printLabel - This method prints a local label used by debug and
1120/// exception handling tables.
1121void AsmPrinter::printLabel(const MachineInstr *MI) const {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001122 O << "\n"
1123 << TAI->getPrivateGlobalPrefix()
Jim Laskeybacd3042007-02-21 22:48:45 +00001124 << "label"
Jim Laskey1ee29252007-01-26 14:34:52 +00001125 << MI->getOperand(0).getImmedValue()
1126 << ":\n";
1127}
1128
Chris Lattner66099132006-02-01 22:41:11 +00001129/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1130/// instruction, using the specified assembler variant. Targets should
1131/// overried this to format as appropriate.
1132bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001133 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001134 // Target doesn't support this yet!
1135 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001136}
Chris Lattnerdd260332006-02-24 20:21:58 +00001137
1138bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1139 unsigned AsmVariant,
1140 const char *ExtraCode) {
1141 // Target doesn't support this yet!
1142 return true;
1143}
Nate Begeman37efe672006-04-22 18:53:45 +00001144
1145/// printBasicBlockLabel - This method prints the label for the specified
1146/// MachineBasicBlock
Nate Begemancdf38c42006-05-02 05:37:32 +00001147void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
1148 bool printColon,
1149 bool printComment) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001150 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman9d51eeb2006-05-02 17:36:46 +00001151 << MBB->getNumber();
Nate Begemancdf38c42006-05-02 05:37:32 +00001152 if (printColon)
1153 O << ':';
Chris Lattner9c78ecb2006-10-05 21:40:14 +00001154 if (printComment && MBB->getBasicBlock())
Dan Gohman286d5692007-07-30 15:06:25 +00001155 O << '\t' << TAI->getCommentString() << ' '
1156 << MBB->getBasicBlock()->getName();
Nate Begeman37efe672006-04-22 18:53:45 +00001157}
Nate Begeman52a51e382006-08-12 21:29:52 +00001158
1159/// printSetLabel - This method prints a set label for the specified
1160/// MachineBasicBlock
1161void AsmPrinter::printSetLabel(unsigned uid,
1162 const MachineBasicBlock *MBB) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001163 if (!TAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001164 return;
1165
Jim Laskey563321a2006-09-06 18:34:40 +00001166 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
1167 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman52a51e382006-08-12 21:29:52 +00001168 printBasicBlockLabel(MBB, false, false);
Jim Laskey563321a2006-09-06 18:34:40 +00001169 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman52a51e382006-08-12 21:29:52 +00001170 << '_' << uid << '\n';
1171}
Evan Chengd6594ae2006-09-12 21:00:35 +00001172
Evan Cheng41349c12006-11-01 09:23:08 +00001173void AsmPrinter::printSetLabel(unsigned uid, unsigned uid2,
1174 const MachineBasicBlock *MBB) const {
1175 if (!TAI->getSetDirective())
1176 return;
1177
1178 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
1179 << getFunctionNumber() << '_' << uid << '_' << uid2
1180 << "_set_" << MBB->getNumber() << ',';
1181 printBasicBlockLabel(MBB, false, false);
1182 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1183 << '_' << uid << '_' << uid2 << '\n';
1184}
1185
Evan Chengd6594ae2006-09-12 21:00:35 +00001186/// printDataDirective - This method prints the asm directive for the
1187/// specified type.
1188void AsmPrinter::printDataDirective(const Type *type) {
1189 const TargetData *TD = TM.getTargetData();
1190 switch (type->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001191 case Type::IntegerTyID: {
1192 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1193 if (BitWidth <= 8)
1194 O << TAI->getData8bitsDirective();
1195 else if (BitWidth <= 16)
1196 O << TAI->getData16bitsDirective();
1197 else if (BitWidth <= 32)
1198 O << TAI->getData32bitsDirective();
1199 else if (BitWidth <= 64) {
1200 assert(TAI->getData64bitsDirective() &&
1201 "Target cannot handle 64-bit constant exprs!");
1202 O << TAI->getData64bitsDirective();
1203 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001204 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001205 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001206 case Type::PointerTyID:
1207 if (TD->getPointerSize() == 8) {
1208 assert(TAI->getData64bitsDirective() &&
1209 "Target cannot handle 64-bit pointer exprs!");
1210 O << TAI->getData64bitsDirective();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001211 } else {
1212 O << TAI->getData32bitsDirective();
Evan Chengd6594ae2006-09-12 21:00:35 +00001213 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001214 break;
1215 case Type::FloatTyID: case Type::DoubleTyID:
1216 assert (0 && "Should have already output floating point constant.");
1217 default:
1218 assert (0 && "Can't handle printing this type of thing");
1219 break;
1220 }
1221}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001222