blob: 4ce379c3e4bd3a2ff553f8450e42985e759dfbb1 [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 Patel794fd752007-05-01 21:15:47 +000035const int 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;
135
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000136 if (const GlobalValue *GV = I->getAliasedGlobal())
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000137 Target = Mang->getValueName(GV);
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000138 else
139 assert(0 && "Unsupported aliasee");
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000140
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000141 if (I->hasExternalLinkage())
142 O << "\t.globl\t" << Name << "\n";
143 else if (I->hasWeakLinkage())
144 O << TAI->getWeakRefDirective() << Name << "\n";
145 else if (!I->hasInternalLinkage())
146 assert(0 && "Invalid alias linkage");
147
148 O << TAI->getSetDirective() << Name << ", " << Target << "\n";
149 }
150 }
151
Chris Lattnera80ba712004-08-16 23:15:22 +0000152 delete Mang; Mang = 0;
153 return false;
154}
155
Chris Lattner25045bd2005-11-21 07:51:36 +0000156void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000157 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +0000158 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner77bc2282005-11-21 08:13:27 +0000159 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +0000160}
161
Chris Lattner3b4fd322005-11-21 08:25:09 +0000162/// EmitConstantPool - Print to the current output stream assembly
163/// representations of the constants in the constant pool MCP. This is
164/// used to print out constants which have been "spilled to memory" by
165/// the code generator.
166///
167void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000168 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000169 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000170
171 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
172 // in special sections.
173 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
174 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
175 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
176 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Chengd6594ae2006-09-12 21:00:35 +0000177 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000178 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng2d2cec12006-06-29 00:26:09 +0000179 MachineConstantPoolEntry CPE = CP[i];
Evan Chengd5a99d72006-09-14 07:35:00 +0000180 const Type *Ty = CPE.getType();
Jim Laskey563321a2006-09-06 18:34:40 +0000181 if (TAI->getFourByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000182 TM.getTargetData()->getTypeSize(Ty) == 4)
183 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000184 else if (TAI->getEightByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000185 TM.getTargetData()->getTypeSize(Ty) == 8)
186 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000187 else if (TAI->getSixteenByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000188 TM.getTargetData()->getTypeSize(Ty) == 16)
189 SixteenByteCPs.push_back(std::make_pair(CPE, i));
190 else
191 OtherCPs.push_back(std::make_pair(CPE, i));
192 }
193
194 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskey563321a2006-09-06 18:34:40 +0000195 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
196 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
197 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
198 SixteenByteCPs);
199 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000200}
201
202void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
203 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
204 if (CP.empty()) return;
205
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000206 SwitchToDataSection(Section);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000207 EmitAlignment(Alignment);
208 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Jim Laskey563321a2006-09-06 18:34:40 +0000209 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
210 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Chengd5a99d72006-09-14 07:35:00 +0000211 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
212 if (CP[i].first.isMachineConstantPoolEntry())
Evan Chengd6594ae2006-09-12 21:00:35 +0000213 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Chengd5a99d72006-09-14 07:35:00 +0000214 else
Evan Chengd6594ae2006-09-12 21:00:35 +0000215 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000216 if (i != e-1) {
Evan Chengd5a99d72006-09-14 07:35:00 +0000217 const Type *Ty = CP[i].first.getType();
Evan Cheng2d2cec12006-06-29 00:26:09 +0000218 unsigned EntSize =
Evan Chengd6594ae2006-09-12 21:00:35 +0000219 TM.getTargetData()->getTypeSize(Ty);
Evan Chengd5a99d72006-09-14 07:35:00 +0000220 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattner3029f922006-02-09 04:46:04 +0000221 // Emit inter-object padding for alignment.
Evan Chengd5a99d72006-09-14 07:35:00 +0000222 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattner3029f922006-02-09 04:46:04 +0000223 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000224 }
225}
226
Nate Begeman37efe672006-04-22 18:53:45 +0000227/// EmitJumpTableInfo - Print assembly representations of the jump tables used
228/// by the current function to the current output stream.
229///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000230void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
231 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000232 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
233 if (JT.empty()) return;
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000234 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000235
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000236 // Use JumpTableDirective otherwise honor the entry size from the jump table
237 // info.
Andrew Lenharthbeec30e2006-09-24 19:45:58 +0000238 const char *JTEntryDirective = TAI->getJumpTableDirective();
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000239 bool HadJTEntryDirective = JTEntryDirective != NULL;
240 if (!HadJTEntryDirective) {
241 JTEntryDirective = MJTI->getEntrySize() == 4 ?
242 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
243 }
Nate Begeman37efe672006-04-22 18:53:45 +0000244
Nate Begeman2f1ae882006-07-27 01:13:04 +0000245 // Pick the directive to use to print the jump table entries, and switch to
246 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000247 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000248
249 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
250 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
251 !JumpTableDataSection) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000252 // In PIC mode, we need to emit the jump table to the same section as the
253 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000254 // We should also do if the section name is NULL.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000255 const Function *F = MF.getFunction();
256 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000257 } else {
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000258 SwitchToDataSection(JumpTableDataSection);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000259 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000260
261 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000262
Nate Begeman37efe672006-04-22 18:53:45 +0000263 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000264 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000265
266 // If this jump table was deleted, ignore it.
267 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000268
269 // For PIC codegen, if possible we want to use the SetDirective to reduce
270 // the number of relocations the assembler will generate for the jump table.
271 // Set directives are all printed before the jump table itself.
272 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000273 if (TAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000274 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
275 if (EmittedSets.insert(JTBBs[ii]).second)
276 printSetLabel(i, JTBBs[ii]);
277
Chris Lattner393a8ee2007-01-18 01:12:56 +0000278 // On some targets (e.g. darwin) we want to emit two consequtive labels
279 // before each jump table. The first label is never referenced, but tells
280 // the assembler and linker the extents of the jump table object. The
281 // second label is actually referenced by the code.
282 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
283 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
284
Jim Laskey563321a2006-09-06 18:34:40 +0000285 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
286 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000287
Nate Begeman37efe672006-04-22 18:53:45 +0000288 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000289 O << JTEntryDirective << ' ';
Nate Begeman52a51e382006-08-12 21:29:52 +0000290 // If we have emitted set directives for the jump table entries, print
291 // them rather than the entries themselves. If we're emitting PIC, then
292 // emit the table entries as differences between two text section labels.
293 // If we're emitting non-PIC code, then emit the entries as direct
294 // references to the target basic blocks.
295 if (!EmittedSets.empty()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000296 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
297 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000298 } else if (IsPic) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000299 printBasicBlockLabel(JTBBs[ii], false, false);
Chris Lattner393a8ee2007-01-18 01:12:56 +0000300 // If the arch uses custom Jump Table directives, don't calc relative to
301 // JT
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000302 if (!HadJTEntryDirective)
303 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
304 << getFunctionNumber() << '_' << i;
Nate Begeman52a51e382006-08-12 21:29:52 +0000305 } else {
306 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000307 }
Nate Begeman37efe672006-04-22 18:53:45 +0000308 O << '\n';
309 }
310 }
311}
312
Chris Lattnered138932005-12-13 06:32:10 +0000313/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
314/// special global used by LLVM. If so, emit it and return true, otherwise
315/// do nothing and return false.
316bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey78098112006-03-07 22:00:35 +0000317 // Ignore debug and non-emitted data.
318 if (GV->getSection() == "llvm.metadata") return true;
319
320 if (!GV->hasAppendingLinkage()) return false;
321
322 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000323
Chris Lattnercb05af82006-09-26 03:38:18 +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 }
Chris Lattnered138932005-12-13 06:32:10 +0000329
Chris Lattner5166b822006-01-12 19:17:23 +0000330 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000331 SwitchToDataSection(TAI->getStaticCtorsSection());
Chris Lattnered138932005-12-13 06:32:10 +0000332 EmitAlignment(2, 0);
333 EmitXXStructorList(GV->getInitializer());
334 return true;
335 }
336
Chris Lattner5166b822006-01-12 19:17:23 +0000337 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000338 SwitchToDataSection(TAI->getStaticDtorsSection());
Chris Lattnered138932005-12-13 06:32:10 +0000339 EmitAlignment(2, 0);
340 EmitXXStructorList(GV->getInitializer());
341 return true;
342 }
343
344 return false;
345}
346
Chris Lattnercb05af82006-09-26 03:38:18 +0000347/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
348/// global in the specified llvm.used list as being used with this directive.
349void AsmPrinter::EmitLLVMUsedList(Constant *List) {
350 const char *Directive = TAI->getUsedDirective();
351
352 // Should be an array of 'sbyte*'.
353 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
354 if (InitList == 0) return;
355
356 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
357 O << Directive;
358 EmitConstantValueOnly(InitList->getOperand(i));
359 O << "\n";
360 }
361}
362
Chris Lattnered138932005-12-13 06:32:10 +0000363/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
364/// function pointers, ignoring the init priority.
365void AsmPrinter::EmitXXStructorList(Constant *List) {
366 // Should be an array of '{ int, void ()* }' structs. The first value is the
367 // init priority, which we ignore.
368 if (!isa<ConstantArray>(List)) return;
369 ConstantArray *InitList = cast<ConstantArray>(List);
370 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
371 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
372 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000373
374 if (CS->getOperand(1)->isNullValue())
375 return; // Found a null terminator, exit printing.
376 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000377 EmitGlobalConstant(CS->getOperand(1));
378 }
379}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000380
Jim Laskeya1a19f82006-10-17 13:41:07 +0000381/// getGlobalLinkName - Returns the asm/link name of of the specified
382/// global variable. Should be overridden by each target asm printer to
383/// generate the appropriate value.
Jim Laskey99e41ee2006-10-17 17:17:24 +0000384const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
385 std::string LinkName;
Jim Laskey03742482006-11-20 20:29:06 +0000386
387 if (isa<Function>(GV)) {
388 LinkName += TAI->getFunctionAddrPrefix();
389 LinkName += Mang->getValueName(GV);
390 LinkName += TAI->getFunctionAddrSuffix();
391 } else {
392 LinkName += TAI->getGlobalVarAddrPrefix();
393 LinkName += Mang->getValueName(GV);
394 LinkName += TAI->getGlobalVarAddrSuffix();
395 }
396
Jim Laskey99e41ee2006-10-17 17:17:24 +0000397 return LinkName;
Jim Laskeya1a19f82006-10-17 13:41:07 +0000398}
399
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000400/// EmitExternalGlobal - Emit the external reference to a global variable.
401/// Should be overridden if an indirect reference should be used.
402void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
403 O << getGlobalLinkName(GV);
404}
405
406
407
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000408//===----------------------------------------------------------------------===//
409/// LEB 128 number encoding.
410
411/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
412/// representing an unsigned leb128 value.
413void AsmPrinter::PrintULEB128(unsigned Value) const {
414 do {
415 unsigned Byte = Value & 0x7f;
416 Value >>= 7;
417 if (Value) Byte |= 0x80;
418 O << "0x" << std::hex << Byte << std::dec;
419 if (Value) O << ", ";
420 } while (Value);
421}
422
423/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
424/// value.
425unsigned AsmPrinter::SizeULEB128(unsigned Value) {
426 unsigned Size = 0;
427 do {
428 Value >>= 7;
429 Size += sizeof(int8_t);
430 } while (Value);
431 return Size;
432}
433
434/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
435/// representing a signed leb128 value.
436void AsmPrinter::PrintSLEB128(int Value) const {
437 int Sign = Value >> (8 * sizeof(Value) - 1);
438 bool IsMore;
439
440 do {
441 unsigned Byte = Value & 0x7f;
442 Value >>= 7;
443 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
444 if (IsMore) Byte |= 0x80;
445 O << "0x" << std::hex << Byte << std::dec;
446 if (IsMore) O << ", ";
447 } while (IsMore);
448}
449
450/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
451/// value.
452unsigned AsmPrinter::SizeSLEB128(int Value) {
453 unsigned Size = 0;
454 int Sign = Value >> (8 * sizeof(Value) - 1);
455 bool IsMore;
456
457 do {
458 unsigned Byte = Value & 0x7f;
459 Value >>= 7;
460 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
461 Size += sizeof(int8_t);
462 } while (IsMore);
463 return Size;
464}
465
466//===--------------------------------------------------------------------===//
467// Emission and print routines
468//
469
470/// PrintHex - Print a value as a hexidecimal value.
471///
472void AsmPrinter::PrintHex(int Value) const {
473 O << "0x" << std::hex << Value << std::dec;
474}
475
476/// EOL - Print a newline character to asm stream. If a comment is present
477/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000478void AsmPrinter::EOL() const {
479 O << "\n";
480}
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000481void AsmPrinter::EOL(const std::string &Comment) const {
482 if (AsmVerbose && !Comment.empty()) {
483 O << "\t"
484 << TAI->getCommentString()
485 << " "
486 << Comment;
487 }
488 O << "\n";
489}
490
491/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
492/// unsigned leb128 value.
493void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
494 if (TAI->hasLEB128()) {
495 O << "\t.uleb128\t"
496 << Value;
497 } else {
498 O << TAI->getData8bitsDirective();
499 PrintULEB128(Value);
500 }
501}
502
503/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
504/// signed leb128 value.
505void AsmPrinter::EmitSLEB128Bytes(int Value) const {
506 if (TAI->hasLEB128()) {
507 O << "\t.sleb128\t"
508 << Value;
509 } else {
510 O << TAI->getData8bitsDirective();
511 PrintSLEB128(Value);
512 }
513}
514
515/// EmitInt8 - Emit a byte directive and value.
516///
517void AsmPrinter::EmitInt8(int Value) const {
518 O << TAI->getData8bitsDirective();
519 PrintHex(Value & 0xFF);
520}
521
522/// EmitInt16 - Emit a short directive and value.
523///
524void AsmPrinter::EmitInt16(int Value) const {
525 O << TAI->getData16bitsDirective();
526 PrintHex(Value & 0xFFFF);
527}
528
529/// EmitInt32 - Emit a long directive and value.
530///
531void AsmPrinter::EmitInt32(int Value) const {
532 O << TAI->getData32bitsDirective();
533 PrintHex(Value);
534}
535
536/// EmitInt64 - Emit a long long directive and value.
537///
538void AsmPrinter::EmitInt64(uint64_t Value) const {
539 if (TAI->getData64bitsDirective()) {
540 O << TAI->getData64bitsDirective();
541 PrintHex(Value);
542 } else {
543 if (TM.getTargetData()->isBigEndian()) {
544 EmitInt32(unsigned(Value >> 32)); O << "\n";
545 EmitInt32(unsigned(Value));
546 } else {
547 EmitInt32(unsigned(Value)); O << "\n";
548 EmitInt32(unsigned(Value >> 32));
549 }
550 }
551}
552
553/// toOctal - Convert the low order bits of X into an octal digit.
554///
555static inline char toOctal(int X) {
556 return (X&7)+'0';
557}
558
559/// printStringChar - Print a char, escaped if necessary.
560///
561static void printStringChar(std::ostream &O, unsigned char C) {
562 if (C == '"') {
563 O << "\\\"";
564 } else if (C == '\\') {
565 O << "\\\\";
566 } else if (isprint(C)) {
567 O << C;
568 } else {
569 switch(C) {
570 case '\b': O << "\\b"; break;
571 case '\f': O << "\\f"; break;
572 case '\n': O << "\\n"; break;
573 case '\r': O << "\\r"; break;
574 case '\t': O << "\\t"; break;
575 default:
576 O << '\\';
577 O << toOctal(C >> 6);
578 O << toOctal(C >> 3);
579 O << toOctal(C >> 0);
580 break;
581 }
582 }
583}
584
585/// EmitString - Emit a string with quotes and a null terminator.
586/// Special characters are emitted properly.
587/// \literal (Eg. '\t') \endliteral
588void AsmPrinter::EmitString(const std::string &String) const {
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000589 const char* AscizDirective = TAI->getAscizDirective();
590 if (AscizDirective)
591 O << AscizDirective;
592 else
593 O << TAI->getAsciiDirective();
594 O << "\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000595 for (unsigned i = 0, N = String.size(); i < N; ++i) {
596 unsigned char C = String[i];
597 printStringChar(O, C);
598 }
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000599 if (AscizDirective)
600 O << "\"";
601 else
602 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000603}
604
605
606//===----------------------------------------------------------------------===//
607
Chris Lattner25045bd2005-11-21 07:51:36 +0000608// EmitAlignment - Emit an alignment directive to the specified power of two.
Dale Johannesen19f54692007-04-23 19:58:54 +0000609// Use the maximum of the specified alignment and the alignment from the
610// specified GlobalValue (if any).
Chris Lattner25045bd2005-11-21 07:51:36 +0000611void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000612 if (GV && GV->getAlignment())
613 NumBits = std::max(NumBits, Log2_32(GV->getAlignment()));
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000614 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskey563321a2006-09-06 18:34:40 +0000615 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
616 O << TAI->getAlignDirective() << NumBits << "\n";
Chris Lattnerbfddc202004-08-17 19:14:29 +0000617}
618
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000619
Chris Lattner25045bd2005-11-21 07:51:36 +0000620/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000621///
Chris Lattner25045bd2005-11-21 07:51:36 +0000622void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000623 if (NumZeros) {
Jim Laskey563321a2006-09-06 18:34:40 +0000624 if (TAI->getZeroDirective()) {
625 O << TAI->getZeroDirective() << NumZeros;
626 if (TAI->getZeroDirectiveSuffix())
627 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000628 O << "\n";
629 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000630 for (; NumZeros; --NumZeros)
Jim Laskey563321a2006-09-06 18:34:40 +0000631 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000632 }
633 }
634}
635
Chris Lattnera80ba712004-08-16 23:15:22 +0000636// Print out the specified constant, without a storage class. Only the
637// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000638void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000639 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000640 O << "0";
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000641 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattnera875df32007-01-12 18:15:09 +0000642 O << CI->getZExtValue();
Reid Spencerb83eb642006-10-20 07:07:24 +0000643 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000644 // This is a constant address for a global variable or function. Use the
645 // name of the variable or function as the address value, possibly
646 // decorating it with GlobalVarAddrPrefix/Suffix or
647 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskey563321a2006-09-06 18:34:40 +0000648 if (isa<Function>(GV)) {
649 O << TAI->getFunctionAddrPrefix()
650 << Mang->getValueName(GV)
651 << TAI->getFunctionAddrSuffix();
652 } else {
653 O << TAI->getGlobalVarAddrPrefix()
654 << Mang->getValueName(GV)
655 << TAI->getGlobalVarAddrSuffix();
656 }
Duraid Madina855a5192005-04-02 12:21:51 +0000657 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000658 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000659 unsigned Opcode = CE->getOpcode();
660 switch (Opcode) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000661 case Instruction::GetElementPtr: {
662 // generate a symbolic expression for the byte address
663 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000664 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
665 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
666 idxVec.size())) {
Chris Lattner27e19212005-02-14 21:40:26 +0000667 if (Offset)
668 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000669 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000670 if (Offset > 0)
671 O << ") + " << Offset;
672 else if (Offset < 0)
673 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000674 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000675 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000676 }
677 break;
678 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000679 case Instruction::Trunc:
680 case Instruction::ZExt:
681 case Instruction::SExt:
682 case Instruction::FPTrunc:
683 case Instruction::FPExt:
684 case Instruction::UIToFP:
685 case Instruction::SIToFP:
686 case Instruction::FPToUI:
687 case Instruction::FPToSI:
688 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
689 break;
Chris Lattnercb0a6812006-12-12 05:14:13 +0000690 case Instruction::BitCast:
691 return EmitConstantValueOnly(CE->getOperand(0));
692
Chris Lattnerddc94012006-12-12 05:18:19 +0000693 case Instruction::IntToPtr: {
694 // Handle casts to pointers by changing them into casts to the appropriate
695 // integer type. This promotes constant folding and simplifies this code.
696 Constant *Op = CE->getOperand(0);
697 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
698 return EmitConstantValueOnly(Op);
699 }
700
701
702 case Instruction::PtrToInt: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000703 // Support only foldable casts to/from pointers that can be eliminated by
704 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000705 Constant *Op = CE->getOperand(0);
Chris Lattnerddc94012006-12-12 05:18:19 +0000706 const Type *Ty = CE->getType();
Chris Lattnera80ba712004-08-16 23:15:22 +0000707
Chris Lattnerddc94012006-12-12 05:18:19 +0000708 // We can emit the pointer value into this slot if the slot is an
709 // integer slot greater or equal to the size of the pointer.
Chris Lattner42a75512007-01-15 02:27:26 +0000710 if (Ty->isInteger() &&
Reid Spencera54b7cb2007-01-12 07:05:14 +0000711 TD->getTypeSize(Ty) >= TD->getTypeSize(Op->getType()))
Chris Lattner4a6bd332006-07-29 01:57:19 +0000712 return EmitConstantValueOnly(Op);
Chris Lattner4a6bd332006-07-29 01:57:19 +0000713
714 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000715 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000716 break;
717 }
718 case Instruction::Add:
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000719 case Instruction::Sub:
Chris Lattnera80ba712004-08-16 23:15:22 +0000720 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000721 EmitConstantValueOnly(CE->getOperand(0));
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000722 O << (Opcode==Instruction::Add ? ") + (" : ") - (");
Chris Lattner25045bd2005-11-21 07:51:36 +0000723 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000724 O << ")";
725 break;
726 default:
727 assert(0 && "Unsupported operator!");
728 }
729 } else {
730 assert(0 && "Unknown constant value!");
731 }
732}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000733
Chris Lattner2980cef2005-11-10 18:06:33 +0000734/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000735/// the predicate isString is true.
736///
Chris Lattner2980cef2005-11-10 18:06:33 +0000737static void printAsCString(std::ostream &O, const ConstantArray *CVA,
738 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000739 assert(CVA->isString() && "Array is not string compatible!");
740
741 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000742 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000743 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000744 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000745 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000746 }
747 O << "\"";
748}
749
Jeff Cohenc884db42006-05-02 01:16:28 +0000750/// EmitString - Emit a zero-byte-terminated string constant.
751///
752void AsmPrinter::EmitString(const ConstantArray *CVA) const {
753 unsigned NumElts = CVA->getNumOperands();
Jim Laskey563321a2006-09-06 18:34:40 +0000754 if (TAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000755 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskey563321a2006-09-06 18:34:40 +0000756 O << TAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000757 printAsCString(O, CVA, NumElts-1);
758 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000759 O << TAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000760 printAsCString(O, CVA, NumElts);
761 }
762 O << "\n";
763}
764
Chris Lattner25045bd2005-11-21 07:51:36 +0000765/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000766///
Chris Lattner25045bd2005-11-21 07:51:36 +0000767void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Andersona69571c2006-05-03 01:29:57 +0000768 const TargetData *TD = TM.getTargetData();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000769
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000770 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000771 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000772 return;
773 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
774 if (CVA->isString()) {
Jeff Cohenc884db42006-05-02 01:16:28 +0000775 EmitString(CVA);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000776 } else { // Not a string. Print the values in successive locations
777 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000778 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000779 }
780 return;
781 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
782 // Print the fields in successive locations. Pad to align if needed!
Owen Andersona69571c2006-05-03 01:29:57 +0000783 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000784 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000785 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
786 const Constant* field = CVS->getOperand(i);
787
788 // Check if padding is needed and insert one or more 0s.
Owen Andersona69571c2006-05-03 01:29:57 +0000789 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattnerb0c39a32007-02-10 19:59:22 +0000790 uint64_t padSize = ((i == e-1? cvsLayout->getSizeInBytes()
Chris Lattnerb1919e22007-02-10 19:55:17 +0000791 : cvsLayout->getElementOffset(i+1))
792 - cvsLayout->getElementOffset(i)) - fieldSize;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000793 sizeSoFar += fieldSize + padSize;
794
795 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000796 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000797
798 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000799 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000800 }
Chris Lattnerb0c39a32007-02-10 19:59:22 +0000801 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
Chris Lattner1b7e2352004-08-17 06:36:49 +0000802 "Layout of constant struct may be incorrect!");
803 return;
804 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
805 // FP Constants are printed as integer constants to avoid losing
806 // precision...
807 double Val = CFP->getValue();
808 if (CFP->getType() == Type::DoubleTy) {
Jim Laskey563321a2006-09-06 18:34:40 +0000809 if (TAI->getData64bitsDirective())
810 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
811 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000812 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000813 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
814 << "\t" << TAI->getCommentString()
815 << " double most significant word " << Val << "\n";
816 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
817 << "\t" << TAI->getCommentString()
818 << " double least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000819 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000820 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
821 << "\t" << TAI->getCommentString()
822 << " double least significant word " << Val << "\n";
823 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
824 << "\t" << TAI->getCommentString()
825 << " double most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000826 }
827 return;
828 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000829 O << TAI->getData32bitsDirective() << FloatToBits(Val)
830 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000831 return;
832 }
Reid Spencer47857812006-12-31 05:55:36 +0000833 } else if (CV->getType() == Type::Int64Ty) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000834 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000835 uint64_t Val = CI->getZExtValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000836
Jim Laskey563321a2006-09-06 18:34:40 +0000837 if (TAI->getData64bitsDirective())
838 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000839 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000840 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
841 << "\t" << TAI->getCommentString()
842 << " Double-word most significant word " << Val << "\n";
843 O << TAI->getData32bitsDirective() << unsigned(Val)
844 << "\t" << TAI->getCommentString()
845 << " Double-word least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000846 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000847 O << TAI->getData32bitsDirective() << unsigned(Val)
848 << "\t" << TAI->getCommentString()
849 << " Double-word least significant word " << Val << "\n";
850 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
851 << "\t" << TAI->getCommentString()
852 << " Double-word most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000853 }
854 return;
855 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000856 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
857 const VectorType *PTy = CP->getType();
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000858
859 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
860 EmitGlobalConstant(CP->getOperand(I));
861
862 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000863 }
864
865 const Type *type = CV->getType();
Evan Chengd6594ae2006-09-12 21:00:35 +0000866 printDataDirective(type);
Chris Lattner25045bd2005-11-21 07:51:36 +0000867 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000868 O << "\n";
869}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000870
Evan Chengd6594ae2006-09-12 21:00:35 +0000871void
872AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
873 // Target doesn't support this yet!
874 abort();
875}
876
Chris Lattner3ce9b672006-09-26 23:59:50 +0000877/// PrintSpecial - Print information related to the specified machine instr
878/// that is independent of the operand, and may be independent of the instr
879/// itself. This can be useful for portably encoding the comment character
880/// or other bits of target-specific knowledge into the asmstrings. The
881/// syntax used is ${:comment}. Targets can override this to add support
882/// for their own strange codes.
883void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattnerbae02cf2006-09-27 00:06:07 +0000884 if (!strcmp(Code, "private")) {
885 O << TAI->getPrivateGlobalPrefix();
886 } else if (!strcmp(Code, "comment")) {
Chris Lattner3ce9b672006-09-26 23:59:50 +0000887 O << TAI->getCommentString();
888 } else if (!strcmp(Code, "uid")) {
889 // Assign a unique ID to this machine instruction.
890 static const MachineInstr *LastMI = 0;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +0000891 static const Function *F = 0;
Chris Lattner3ce9b672006-09-26 23:59:50 +0000892 static unsigned Counter = 0U-1;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +0000893
894 // Comparing the address of MI isn't sufficient, because machineinstrs may
895 // be allocated to the same address across functions.
896 const Function *ThisF = MI->getParent()->getParent()->getFunction();
897
Chris Lattner3ce9b672006-09-26 23:59:50 +0000898 // If this is a new machine instruction, bump the counter.
Chris Lattnerb6a24bf2007-02-05 21:23:52 +0000899 if (LastMI != MI || F != ThisF) {
900 ++Counter;
901 LastMI = MI;
Chris Lattner4b092522007-02-06 01:56:31 +0000902 F = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +0000903 }
Chris Lattner3ce9b672006-09-26 23:59:50 +0000904 O << Counter;
905 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +0000906 cerr << "Unknown special formatter '" << Code
907 << "' for machine instr: " << *MI;
Chris Lattner3ce9b672006-09-26 23:59:50 +0000908 exit(1);
909 }
910}
911
912
Chris Lattner0264d1a2006-01-27 02:10:10 +0000913/// printInlineAsm - This method formats and prints the specified machine
914/// instruction that is an inline asm.
915void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000916 unsigned NumOperands = MI->getNumOperands();
917
918 // Count the number of register definitions.
919 unsigned NumDefs = 0;
Chris Lattner67942f52006-09-05 20:02:51 +0000920 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
921 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000922 assert(NumDefs != NumOperands-1 && "No asm string?");
923
924 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +0000925
926 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000927 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +0000928
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000929 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
930 if (AsmStr[0] == 0) {
931 O << "\n"; // Tab already printed, avoid double indenting next instr.
932 return;
933 }
934
Jim Laskey563321a2006-09-06 18:34:40 +0000935 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000936
Bill Wendlingeb9a42c2007-01-16 03:42:04 +0000937 // The variant of the current asmprinter.
938 int AsmPrinterVariant = TAI->getAssemblerDialect();
939
Chris Lattner66099132006-02-01 22:41:11 +0000940 int CurVariant = -1; // The number of the {.|.|.} region we are in.
941 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +0000942
Chris Lattner66099132006-02-01 22:41:11 +0000943 while (*LastEmitted) {
944 switch (*LastEmitted) {
945 default: {
946 // Not a special case, emit the string section literally.
947 const char *LiteralEnd = LastEmitted+1;
948 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +0000949 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +0000950 ++LiteralEnd;
951 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
952 O.write(LastEmitted, LiteralEnd-LastEmitted);
953 LastEmitted = LiteralEnd;
954 break;
955 }
Chris Lattner1c059972006-05-05 21:47:05 +0000956 case '\n':
957 ++LastEmitted; // Consume newline character.
Chris Lattner1f6f4c72007-04-30 17:00:18 +0000958 O << "\n"; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +0000959 break;
Chris Lattner66099132006-02-01 22:41:11 +0000960 case '$': {
961 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000962 bool Done = true;
963
964 // Handle escapes.
965 switch (*LastEmitted) {
966 default: Done = false; break;
967 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +0000968 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
969 O << '$';
970 ++LastEmitted; // Consume second '$' character.
971 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000972 case '(': // $( -> same as GCC's { character.
973 ++LastEmitted; // Consume '(' character.
974 if (CurVariant != -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000975 cerr << "Nested variants found in inline asm string: '"
976 << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000977 exit(1);
978 }
979 CurVariant = 0; // We're in the first variant now.
980 break;
981 case '|':
982 ++LastEmitted; // consume '|' character.
983 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000984 cerr << "Found '|' character outside of variant in inline asm "
985 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000986 exit(1);
987 }
988 ++CurVariant; // We're in the next variant.
989 break;
990 case ')': // $) -> same as GCC's } char.
991 ++LastEmitted; // consume ')' character.
992 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000993 cerr << "Found '}' character outside of variant in inline asm "
994 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000995 exit(1);
996 }
997 CurVariant = -1;
998 break;
Chris Lattner66099132006-02-01 22:41:11 +0000999 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001000 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001001
1002 bool HasCurlyBraces = false;
1003 if (*LastEmitted == '{') { // ${variable}
1004 ++LastEmitted; // Consume '{' character.
1005 HasCurlyBraces = true;
1006 }
1007
1008 const char *IDStart = LastEmitted;
1009 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001010 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001011 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1012 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001013 cerr << "Bad $ operand number in inline asm string: '"
1014 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001015 exit(1);
1016 }
1017 LastEmitted = IDEnd;
1018
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001019 char Modifier[2] = { 0, 0 };
1020
Chris Lattner66099132006-02-01 22:41:11 +00001021 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001022 // If we have curly braces, check for a modifier character. This
1023 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1024 if (*LastEmitted == ':') {
1025 ++LastEmitted; // Consume ':' character.
1026 if (*LastEmitted == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001027 cerr << "Bad ${:} expression in inline asm string: '"
1028 << AsmStr << "'\n";
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001029 exit(1);
1030 }
1031
1032 Modifier[0] = *LastEmitted;
1033 ++LastEmitted; // Consume modifier character.
1034 }
1035
Chris Lattner66099132006-02-01 22:41:11 +00001036 if (*LastEmitted != '}') {
Bill Wendlinge8156192006-12-07 01:30:32 +00001037 cerr << "Bad ${} expression in inline asm string: '"
1038 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001039 exit(1);
1040 }
1041 ++LastEmitted; // Consume '}' character.
1042 }
1043
1044 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001045 cerr << "Invalid $ operand number in inline asm string: '"
1046 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001047 exit(1);
1048 }
1049
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001050 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001051 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001052 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1053 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001054
1055 bool Error = false;
1056
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001057 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001058 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001059 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001060 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1061 OpNo += (OpFlags >> 3) + 1;
1062 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001063
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001064 if (OpNo >= MI->getNumOperands()) {
1065 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001066 } else {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001067 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1068 ++OpNo; // Skip over the ID number.
1069
1070 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1071 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1072 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1073 Modifier[0] ? Modifier : 0);
1074 } else {
1075 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1076 Modifier[0] ? Modifier : 0);
1077 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001078 }
1079 if (Error) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001080 cerr << "Invalid operand found in inline asm: '"
1081 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001082 MI->dump();
1083 exit(1);
1084 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001085 }
Chris Lattner66099132006-02-01 22:41:11 +00001086 break;
1087 }
Chris Lattner66099132006-02-01 22:41:11 +00001088 }
1089 }
Jim Laskey563321a2006-09-06 18:34:40 +00001090 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattner66099132006-02-01 22:41:11 +00001091}
1092
Jim Laskey1ee29252007-01-26 14:34:52 +00001093/// printLabel - This method prints a local label used by debug and
1094/// exception handling tables.
1095void AsmPrinter::printLabel(const MachineInstr *MI) const {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001096 O << "\n"
1097 << TAI->getPrivateGlobalPrefix()
Jim Laskeybacd3042007-02-21 22:48:45 +00001098 << "label"
Jim Laskey1ee29252007-01-26 14:34:52 +00001099 << MI->getOperand(0).getImmedValue()
1100 << ":\n";
1101}
1102
Chris Lattner66099132006-02-01 22:41:11 +00001103/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1104/// instruction, using the specified assembler variant. Targets should
1105/// overried this to format as appropriate.
1106bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001107 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001108 // Target doesn't support this yet!
1109 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001110}
Chris Lattnerdd260332006-02-24 20:21:58 +00001111
1112bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1113 unsigned AsmVariant,
1114 const char *ExtraCode) {
1115 // Target doesn't support this yet!
1116 return true;
1117}
Nate Begeman37efe672006-04-22 18:53:45 +00001118
1119/// printBasicBlockLabel - This method prints the label for the specified
1120/// MachineBasicBlock
Nate Begemancdf38c42006-05-02 05:37:32 +00001121void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
1122 bool printColon,
1123 bool printComment) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001124 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman9d51eeb2006-05-02 17:36:46 +00001125 << MBB->getNumber();
Nate Begemancdf38c42006-05-02 05:37:32 +00001126 if (printColon)
1127 O << ':';
Chris Lattner9c78ecb2006-10-05 21:40:14 +00001128 if (printComment && MBB->getBasicBlock())
Jim Laskey563321a2006-09-06 18:34:40 +00001129 O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
Nate Begeman37efe672006-04-22 18:53:45 +00001130}
Nate Begeman52a51e382006-08-12 21:29:52 +00001131
1132/// printSetLabel - This method prints a set label for the specified
1133/// MachineBasicBlock
1134void AsmPrinter::printSetLabel(unsigned uid,
1135 const MachineBasicBlock *MBB) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001136 if (!TAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001137 return;
1138
Jim Laskey563321a2006-09-06 18:34:40 +00001139 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
1140 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman52a51e382006-08-12 21:29:52 +00001141 printBasicBlockLabel(MBB, false, false);
Jim Laskey563321a2006-09-06 18:34:40 +00001142 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman52a51e382006-08-12 21:29:52 +00001143 << '_' << uid << '\n';
1144}
Evan Chengd6594ae2006-09-12 21:00:35 +00001145
Evan Cheng41349c12006-11-01 09:23:08 +00001146void AsmPrinter::printSetLabel(unsigned uid, unsigned uid2,
1147 const MachineBasicBlock *MBB) const {
1148 if (!TAI->getSetDirective())
1149 return;
1150
1151 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
1152 << getFunctionNumber() << '_' << uid << '_' << uid2
1153 << "_set_" << MBB->getNumber() << ',';
1154 printBasicBlockLabel(MBB, false, false);
1155 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1156 << '_' << uid << '_' << uid2 << '\n';
1157}
1158
Evan Chengd6594ae2006-09-12 21:00:35 +00001159/// printDataDirective - This method prints the asm directive for the
1160/// specified type.
1161void AsmPrinter::printDataDirective(const Type *type) {
1162 const TargetData *TD = TM.getTargetData();
1163 switch (type->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001164 case Type::IntegerTyID: {
1165 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1166 if (BitWidth <= 8)
1167 O << TAI->getData8bitsDirective();
1168 else if (BitWidth <= 16)
1169 O << TAI->getData16bitsDirective();
1170 else if (BitWidth <= 32)
1171 O << TAI->getData32bitsDirective();
1172 else if (BitWidth <= 64) {
1173 assert(TAI->getData64bitsDirective() &&
1174 "Target cannot handle 64-bit constant exprs!");
1175 O << TAI->getData64bitsDirective();
1176 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001177 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001178 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001179 case Type::PointerTyID:
1180 if (TD->getPointerSize() == 8) {
1181 assert(TAI->getData64bitsDirective() &&
1182 "Target cannot handle 64-bit pointer exprs!");
1183 O << TAI->getData64bitsDirective();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001184 } else {
1185 O << TAI->getData32bitsDirective();
Evan Chengd6594ae2006-09-12 21:00:35 +00001186 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001187 break;
1188 case Type::FloatTyID: case Type::DoubleTyID:
1189 assert (0 && "Should have already output floating point constant.");
1190 default:
1191 assert (0 && "Can't handle printing this type of thing");
1192 break;
1193 }
1194}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001195