blob: 586472c38ef9b17f2021b7c36a7bc797a43eef7e [file] [log] [blame]
Chris Lattner6a8e0f52004-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 Cheng993e9cf2006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng38d5e762006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman41b1cdc2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000020#include "llvm/CodeGen/MachineJumpTableInfo.h"
Jim Laskey1c055e82007-01-25 15:12:02 +000021#include "llvm/Support/CommandLine.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000022#include "llvm/Support/Mangler.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000023#include "llvm/Support/MathExtras.h"
Bill Wendling5c3966a2006-11-29 00:39:47 +000024#include "llvm/Support/Streams.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000025#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000026#include "llvm/Target/TargetData.h"
Chris Lattner90438232006-10-06 22:50:56 +000027#include "llvm/Target/TargetLowering.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000028#include "llvm/Target/TargetMachine.h"
Chris Lattneraa23fa92006-02-01 22:41:11 +000029#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000030using namespace llvm;
31
Jim Laskey1c055e82007-01-25 15:12:02 +000032static cl::opt<bool>
33AsmVerbose("asm-verbose", cl::Hidden, cl::desc("Add comments to directives."));
34
Devang Patel8c78a0b2007-05-03 01:11:54 +000035char AsmPrinter::ID = 0;
Jim Laskey261779b2006-09-07 22:06:40 +000036AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
37 const TargetAsmInfo *T)
Devang Patel09f162c2007-05-01 21:15:47 +000038 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o), TM(tm), TAI(T)
Jim Laskeya6211dc2006-09-06 18:34:40 +000039{}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000040
Chris Lattnerdc822412006-10-05 02:42:47 +000041std::string AsmPrinter::getSectionForFunction(const Function &F) const {
42 return TAI->getTextSection();
43}
44
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000045
Chris Lattner8488ba22006-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 Lattner2ea5c992005-11-21 07:06:27 +000048///
Chris Lattner8488ba22006-05-09 04:59:56 +000049void AsmPrinter::SwitchToTextSection(const char *NewSection,
50 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000051 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000052 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000053 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner8c2bfc02006-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 Cohen470f4312006-05-02 03:58:45 +000059
Chris Lattner4ebc6a22006-05-09 05:33:48 +000060 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000061 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
62 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000063
Chris Lattner4ebc6a22006-05-09 05:33:48 +000064 CurrentSection = NS;
65
66 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000067 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000068}
69
Nate Begeman78756502006-07-27 01:13:04 +000070/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-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 Lattner6341df82006-05-09 05:23:12 +000076 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000077 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner6341df82006-05-09 05:23:12 +000078 else
79 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +000080
Chris Lattner6341df82006-05-09 05:23:12 +000081 // If we're already in this section, we're done.
82 if (CurrentSection == NS) return;
83
Chris Lattner4ebc6a22006-05-09 05:33:48 +000084 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000085 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
86 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner4ebc6a22006-05-09 05:33:48 +000087
88 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +000089
Chris Lattner4ebc6a22006-05-09 05:33:48 +000090 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000091 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +000092}
93
94
Chris Lattner6a8e0f52004-08-16 23:15:22 +000095bool AsmPrinter::doInitialization(Module &M) {
Jim Laskeya6211dc2006-09-06 18:34:40 +000096 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +000097
Chris Lattner00fcdfe2006-01-24 04:16:34 +000098 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000099 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000100 << M.getModuleInlineAsm()
Jim Laskeya6211dc2006-09-06 18:34:40 +0000101 << "\n" << TAI->getCommentString()
102 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000103
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000104 SwitchToDataSection(""); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000105
Jim Laskeyc56315c2007-01-26 21:22:28 +0000106 if (MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>()) {
107 MMI->AnalyzeModule(M);
Jim Laskey0bbdc552006-01-26 20:21:46 +0000108 }
109
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000110 return false;
111}
112
113bool AsmPrinter::doFinalization(Module &M) {
Rafael Espindolad7998d02006-12-18 03:37:18 +0000114 if (TAI->getWeakRefDirective()) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000115 if (!ExtWeakSymbols.empty())
Rafael Espindolad7998d02006-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 Korobeynikova97b6942007-04-25 14:27:10 +0000126 if (TAI->getSetDirective()) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000127 if (!M.alias_empty())
Anton Korobeynikova97b6942007-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 Korobeynikovb18f8f82007-04-28 13:45:00 +0000133 std::string Name = Mang->getValueName(I);
134 std::string Target;
135
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000136 if (const GlobalValue *GV = I->getAliasedGlobal())
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000137 Target = Mang->getValueName(GV);
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +0000138 else
139 assert(0 && "Unsupported aliasee");
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000140
Anton Korobeynikova97b6942007-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 Lattner6a8e0f52004-08-16 23:15:22 +0000152 delete Mang; Mang = 0;
153 return false;
154}
155
Chris Lattnerbb644e32005-11-21 07:51:36 +0000156void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000157 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000158 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000159 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000160}
161
Chris Lattnerf2991ce2005-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 Lattnerba972642006-02-09 04:22:52 +0000168 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000169 if (CP.empty()) return;
Evan Chengec1d60b2006-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 Cheng45fe3bc2006-09-12 21:00:35 +0000177 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000178 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000179 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng2ad050f2006-09-14 07:35:00 +0000180 const Type *Ty = CPE.getType();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000181 if (TAI->getFourByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000182 TM.getTargetData()->getTypeSize(Ty) == 4)
183 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000184 else if (TAI->getEightByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000185 TM.getTargetData()->getTypeSize(Ty) == 8)
186 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000187 else if (TAI->getSixteenByteConstantSection() &&
Evan Chengec1d60b2006-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 Laskeya6211dc2006-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 Chengec1d60b2006-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 Korobeynikovaa4c0f92006-10-31 08:31:24 +0000206 SwitchToDataSection(Section);
Evan Chengec1d60b2006-06-29 00:26:09 +0000207 EmitAlignment(Alignment);
208 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000209 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
210 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Cheng2ad050f2006-09-14 07:35:00 +0000211 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
212 if (CP[i].first.isMachineConstantPoolEntry())
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000213 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000214 else
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000215 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000216 if (i != e-1) {
Evan Cheng2ad050f2006-09-14 07:35:00 +0000217 const Type *Ty = CP[i].first.getType();
Evan Chengec1d60b2006-06-29 00:26:09 +0000218 unsigned EntSize =
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000219 TM.getTargetData()->getTypeSize(Ty);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000220 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000221 // Emit inter-object padding for alignment.
Evan Cheng2ad050f2006-09-14 07:35:00 +0000222 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000223 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000224 }
225}
226
Nate Begeman4ca2ea52006-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 Lattnera6a570e2006-10-05 03:01:21 +0000230void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
231 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000232 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
233 if (JT.empty()) return;
Jim Laskey70323a82006-12-14 19:17:33 +0000234 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begemanefc312a2006-07-27 16:46:58 +0000235
Jim Laskey70323a82006-12-14 19:17:33 +0000236 // Use JumpTableDirective otherwise honor the entry size from the jump table
237 // info.
Andrew Lenharth783a4a92006-09-24 19:45:58 +0000238 const char *JTEntryDirective = TAI->getJumpTableDirective();
Jim Laskey70323a82006-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 Begeman4ca2ea52006-04-22 18:53:45 +0000244
Nate Begeman78756502006-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 Laskey70323a82006-12-14 19:17:33 +0000247 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000248
249 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
250 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
251 !JumpTableDataSection) {
Jim Laskey70323a82006-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 Korobeynikov44ef9342006-12-19 21:04:20 +0000254 // We should also do if the section name is NULL.
Jim Laskey70323a82006-12-14 19:17:33 +0000255 const Function *F = MF.getFunction();
256 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman78756502006-07-27 01:13:04 +0000257 } else {
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000258 SwitchToDataSection(JumpTableDataSection);
Nate Begeman78756502006-07-27 01:13:04 +0000259 }
Jim Laskey70323a82006-12-14 19:17:33 +0000260
261 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000262
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000263 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000264 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner28bfe382006-10-28 18:10:06 +0000265
266 // If this jump table was deleted, ignore it.
267 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-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 Laskey70323a82006-12-14 19:17:33 +0000273 if (TAI->getSetDirective() && IsPic)
Nate Begeman984c1a42006-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 Lattner0ee2d462007-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 Laskeya6211dc2006-09-06 18:34:40 +0000285 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
286 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000287
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000288 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begemanefc312a2006-07-27 16:46:58 +0000289 O << JTEntryDirective << ' ';
Nate Begeman984c1a42006-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 Laskeya6211dc2006-09-06 18:34:40 +0000296 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
297 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Jim Laskey70323a82006-12-14 19:17:33 +0000298 } else if (IsPic) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000299 printBasicBlockLabel(JTBBs[ii], false, false);
Chris Lattner0ee2d462007-01-18 01:12:56 +0000300 // If the arch uses custom Jump Table directives, don't calc relative to
301 // JT
Jim Laskey70323a82006-12-14 19:17:33 +0000302 if (!HadJTEntryDirective)
303 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
304 << getFunctionNumber() << '_' << i;
Nate Begeman984c1a42006-08-12 21:29:52 +0000305 } else {
306 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman78756502006-07-27 01:13:04 +0000307 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000308 O << '\n';
309 }
310 }
311}
312
Chris Lattnerf0e9aef2005-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 Laskey313570f2006-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 Lattnerf0e9aef2005-12-13 06:32:10 +0000323
Chris Lattner66af3902006-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 Lattnerf0e9aef2005-12-13 06:32:10 +0000329
Evan Cheng53ce7de2007-06-04 20:39:18 +0000330 const TargetData *TD = TM.getTargetData();
331 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner3760e902006-01-12 19:17:23 +0000332 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000333 SwitchToDataSection(TAI->getStaticCtorsSection());
Evan Cheng53ce7de2007-06-04 20:39:18 +0000334 EmitAlignment(Align, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000335 EmitXXStructorList(GV->getInitializer());
336 return true;
337 }
338
Chris Lattner3760e902006-01-12 19:17:23 +0000339 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000340 SwitchToDataSection(TAI->getStaticDtorsSection());
Evan Cheng53ce7de2007-06-04 20:39:18 +0000341 EmitAlignment(Align, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000342 EmitXXStructorList(GV->getInitializer());
343 return true;
344 }
345
346 return false;
347}
348
Chris Lattner66af3902006-09-26 03:38:18 +0000349/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
350/// global in the specified llvm.used list as being used with this directive.
351void AsmPrinter::EmitLLVMUsedList(Constant *List) {
352 const char *Directive = TAI->getUsedDirective();
353
354 // Should be an array of 'sbyte*'.
355 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
356 if (InitList == 0) return;
357
358 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
359 O << Directive;
360 EmitConstantValueOnly(InitList->getOperand(i));
361 O << "\n";
362 }
363}
364
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000365/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
366/// function pointers, ignoring the init priority.
367void AsmPrinter::EmitXXStructorList(Constant *List) {
368 // Should be an array of '{ int, void ()* }' structs. The first value is the
369 // init priority, which we ignore.
370 if (!isa<ConstantArray>(List)) return;
371 ConstantArray *InitList = cast<ConstantArray>(List);
372 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
373 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
374 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000375
376 if (CS->getOperand(1)->isNullValue())
377 return; // Found a null terminator, exit printing.
378 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000379 EmitGlobalConstant(CS->getOperand(1));
380 }
381}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000382
Jim Laskey71262542006-10-17 13:41:07 +0000383/// getGlobalLinkName - Returns the asm/link name of of the specified
384/// global variable. Should be overridden by each target asm printer to
385/// generate the appropriate value.
Jim Laskeyd24b9132006-10-17 17:17:24 +0000386const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
387 std::string LinkName;
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000388
389 if (isa<Function>(GV)) {
390 LinkName += TAI->getFunctionAddrPrefix();
391 LinkName += Mang->getValueName(GV);
392 LinkName += TAI->getFunctionAddrSuffix();
393 } else {
394 LinkName += TAI->getGlobalVarAddrPrefix();
395 LinkName += Mang->getValueName(GV);
396 LinkName += TAI->getGlobalVarAddrSuffix();
397 }
398
Jim Laskeyd24b9132006-10-17 17:17:24 +0000399 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000400}
401
Jim Laskey18fc0972007-02-21 22:47:38 +0000402/// EmitExternalGlobal - Emit the external reference to a global variable.
403/// Should be overridden if an indirect reference should be used.
404void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
405 O << getGlobalLinkName(GV);
406}
407
408
409
Jim Laskey1c055e82007-01-25 15:12:02 +0000410//===----------------------------------------------------------------------===//
411/// LEB 128 number encoding.
412
413/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
414/// representing an unsigned leb128 value.
415void AsmPrinter::PrintULEB128(unsigned Value) const {
416 do {
417 unsigned Byte = Value & 0x7f;
418 Value >>= 7;
419 if (Value) Byte |= 0x80;
420 O << "0x" << std::hex << Byte << std::dec;
421 if (Value) O << ", ";
422 } while (Value);
423}
424
425/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
426/// value.
427unsigned AsmPrinter::SizeULEB128(unsigned Value) {
428 unsigned Size = 0;
429 do {
430 Value >>= 7;
431 Size += sizeof(int8_t);
432 } while (Value);
433 return Size;
434}
435
436/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
437/// representing a signed leb128 value.
438void AsmPrinter::PrintSLEB128(int Value) const {
439 int Sign = Value >> (8 * sizeof(Value) - 1);
440 bool IsMore;
441
442 do {
443 unsigned Byte = Value & 0x7f;
444 Value >>= 7;
445 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
446 if (IsMore) Byte |= 0x80;
447 O << "0x" << std::hex << Byte << std::dec;
448 if (IsMore) O << ", ";
449 } while (IsMore);
450}
451
452/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
453/// value.
454unsigned AsmPrinter::SizeSLEB128(int Value) {
455 unsigned Size = 0;
456 int Sign = Value >> (8 * sizeof(Value) - 1);
457 bool IsMore;
458
459 do {
460 unsigned Byte = Value & 0x7f;
461 Value >>= 7;
462 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
463 Size += sizeof(int8_t);
464 } while (IsMore);
465 return Size;
466}
467
468//===--------------------------------------------------------------------===//
469// Emission and print routines
470//
471
472/// PrintHex - Print a value as a hexidecimal value.
473///
474void AsmPrinter::PrintHex(int Value) const {
475 O << "0x" << std::hex << Value << std::dec;
476}
477
478/// EOL - Print a newline character to asm stream. If a comment is present
479/// then it will be printed first. Comments should not contain '\n'.
Jim Laskey18fc0972007-02-21 22:47:38 +0000480void AsmPrinter::EOL() const {
481 O << "\n";
482}
Jim Laskey1c055e82007-01-25 15:12:02 +0000483void AsmPrinter::EOL(const std::string &Comment) const {
484 if (AsmVerbose && !Comment.empty()) {
485 O << "\t"
486 << TAI->getCommentString()
487 << " "
488 << Comment;
489 }
490 O << "\n";
491}
492
493/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
494/// unsigned leb128 value.
495void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
496 if (TAI->hasLEB128()) {
497 O << "\t.uleb128\t"
498 << Value;
499 } else {
500 O << TAI->getData8bitsDirective();
501 PrintULEB128(Value);
502 }
503}
504
505/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
506/// signed leb128 value.
507void AsmPrinter::EmitSLEB128Bytes(int Value) const {
508 if (TAI->hasLEB128()) {
509 O << "\t.sleb128\t"
510 << Value;
511 } else {
512 O << TAI->getData8bitsDirective();
513 PrintSLEB128(Value);
514 }
515}
516
517/// EmitInt8 - Emit a byte directive and value.
518///
519void AsmPrinter::EmitInt8(int Value) const {
520 O << TAI->getData8bitsDirective();
521 PrintHex(Value & 0xFF);
522}
523
524/// EmitInt16 - Emit a short directive and value.
525///
526void AsmPrinter::EmitInt16(int Value) const {
527 O << TAI->getData16bitsDirective();
528 PrintHex(Value & 0xFFFF);
529}
530
531/// EmitInt32 - Emit a long directive and value.
532///
533void AsmPrinter::EmitInt32(int Value) const {
534 O << TAI->getData32bitsDirective();
535 PrintHex(Value);
536}
537
538/// EmitInt64 - Emit a long long directive and value.
539///
540void AsmPrinter::EmitInt64(uint64_t Value) const {
541 if (TAI->getData64bitsDirective()) {
542 O << TAI->getData64bitsDirective();
543 PrintHex(Value);
544 } else {
545 if (TM.getTargetData()->isBigEndian()) {
546 EmitInt32(unsigned(Value >> 32)); O << "\n";
547 EmitInt32(unsigned(Value));
548 } else {
549 EmitInt32(unsigned(Value)); O << "\n";
550 EmitInt32(unsigned(Value >> 32));
551 }
552 }
553}
554
555/// toOctal - Convert the low order bits of X into an octal digit.
556///
557static inline char toOctal(int X) {
558 return (X&7)+'0';
559}
560
561/// printStringChar - Print a char, escaped if necessary.
562///
563static void printStringChar(std::ostream &O, unsigned char C) {
564 if (C == '"') {
565 O << "\\\"";
566 } else if (C == '\\') {
567 O << "\\\\";
568 } else if (isprint(C)) {
569 O << C;
570 } else {
571 switch(C) {
572 case '\b': O << "\\b"; break;
573 case '\f': O << "\\f"; break;
574 case '\n': O << "\\n"; break;
575 case '\r': O << "\\r"; break;
576 case '\t': O << "\\t"; break;
577 default:
578 O << '\\';
579 O << toOctal(C >> 6);
580 O << toOctal(C >> 3);
581 O << toOctal(C >> 0);
582 break;
583 }
584 }
585}
586
587/// EmitString - Emit a string with quotes and a null terminator.
588/// Special characters are emitted properly.
589/// \literal (Eg. '\t') \endliteral
590void AsmPrinter::EmitString(const std::string &String) const {
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000591 const char* AscizDirective = TAI->getAscizDirective();
592 if (AscizDirective)
593 O << AscizDirective;
594 else
595 O << TAI->getAsciiDirective();
596 O << "\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000597 for (unsigned i = 0, N = String.size(); i < N; ++i) {
598 unsigned char C = String[i];
599 printStringChar(O, C);
600 }
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000601 if (AscizDirective)
602 O << "\"";
603 else
604 O << "\\0\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000605}
606
607
608//===----------------------------------------------------------------------===//
609
Chris Lattner3e3ff302007-05-31 18:57:45 +0000610// EmitAlignment - Emit an alignment directive to the specified power of
611// two boundary. For example, if you pass in 3 here, you will get an 8
612// byte alignment. If a global value is specified, and if that global has
613// an explicit alignment requested, it will unconditionally override the
614// alignment request. However, if ForcedAlignBits is specified, this value
615// has final say: the ultimate alignment will be the max of ForcedAlignBits
616// and the alignment computed with NumBits and the global.
617//
618// The algorithm is:
619// Align = NumBits;
620// if (GV && GV->hasalignment) Align = GV->getalignment();
621// Align = std::max(Align, ForcedAlignBits);
622//
623void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
624 unsigned ForcedAlignBits) const {
Dale Johannesen8653d292007-04-23 23:33:31 +0000625 if (GV && GV->getAlignment())
Chris Lattner3e3ff302007-05-31 18:57:45 +0000626 NumBits = Log2_32(GV->getAlignment());
627 NumBits = std::max(NumBits, ForcedAlignBits);
628
Chris Lattner747960d2005-11-10 18:09:27 +0000629 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000630 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
631 O << TAI->getAlignDirective() << NumBits << "\n";
Chris Lattner1d35c162004-08-17 19:14:29 +0000632}
633
Jim Laskey18fc0972007-02-21 22:47:38 +0000634
Chris Lattnerbb644e32005-11-21 07:51:36 +0000635/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000636///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000637void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000638 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000639 if (TAI->getZeroDirective()) {
640 O << TAI->getZeroDirective() << NumZeros;
641 if (TAI->getZeroDirectiveSuffix())
642 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000643 O << "\n";
644 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000645 for (; NumZeros; --NumZeros)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000646 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000647 }
648 }
649}
650
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000651// Print out the specified constant, without a storage class. Only the
652// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000653void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000654 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000655 O << "0";
Zhou Sheng75b871f2007-01-11 12:24:14 +0000656 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner7f6fac42007-01-12 18:15:09 +0000657 O << CI->getZExtValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000658 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000659 // This is a constant address for a global variable or function. Use the
660 // name of the variable or function as the address value, possibly
661 // decorating it with GlobalVarAddrPrefix/Suffix or
662 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000663 if (isa<Function>(GV)) {
664 O << TAI->getFunctionAddrPrefix()
665 << Mang->getValueName(GV)
666 << TAI->getFunctionAddrSuffix();
667 } else {
668 O << TAI->getGlobalVarAddrPrefix()
669 << Mang->getValueName(GV)
670 << TAI->getGlobalVarAddrSuffix();
671 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000672 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000673 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000674 unsigned Opcode = CE->getOpcode();
675 switch (Opcode) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000676 case Instruction::GetElementPtr: {
677 // generate a symbolic expression for the byte address
678 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner83dfca82007-02-10 20:31:59 +0000679 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
680 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
681 idxVec.size())) {
Chris Lattner145569b2005-02-14 21:40:26 +0000682 if (Offset)
683 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000684 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000685 if (Offset > 0)
686 O << ") + " << Offset;
687 else if (Offset < 0)
688 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000689 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000690 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000691 }
692 break;
693 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000694 case Instruction::Trunc:
695 case Instruction::ZExt:
696 case Instruction::SExt:
697 case Instruction::FPTrunc:
698 case Instruction::FPExt:
699 case Instruction::UIToFP:
700 case Instruction::SIToFP:
701 case Instruction::FPToUI:
702 case Instruction::FPToSI:
703 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
704 break;
Chris Lattnerb9e41f52006-12-12 05:14:13 +0000705 case Instruction::BitCast:
706 return EmitConstantValueOnly(CE->getOperand(0));
707
Chris Lattner0c537da2006-12-12 05:18:19 +0000708 case Instruction::IntToPtr: {
709 // Handle casts to pointers by changing them into casts to the appropriate
710 // integer type. This promotes constant folding and simplifies this code.
711 Constant *Op = CE->getOperand(0);
712 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
713 return EmitConstantValueOnly(Op);
714 }
715
716
717 case Instruction::PtrToInt: {
Chris Lattner85492422006-07-29 01:57:19 +0000718 // Support only foldable casts to/from pointers that can be eliminated by
719 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000720 Constant *Op = CE->getOperand(0);
Chris Lattner0c537da2006-12-12 05:18:19 +0000721 const Type *Ty = CE->getType();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000722
Chris Lattner0c537da2006-12-12 05:18:19 +0000723 // We can emit the pointer value into this slot if the slot is an
724 // integer slot greater or equal to the size of the pointer.
Chris Lattner03c49532007-01-15 02:27:26 +0000725 if (Ty->isInteger() &&
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000726 TD->getTypeSize(Ty) >= TD->getTypeSize(Op->getType()))
Chris Lattner85492422006-07-29 01:57:19 +0000727 return EmitConstantValueOnly(Op);
Chris Lattner85492422006-07-29 01:57:19 +0000728
729 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000730 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000731 break;
732 }
733 case Instruction::Add:
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000734 case Instruction::Sub:
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000735 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000736 EmitConstantValueOnly(CE->getOperand(0));
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000737 O << (Opcode==Instruction::Add ? ") + (" : ") - (");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000738 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000739 O << ")";
740 break;
741 default:
742 assert(0 && "Unsupported operator!");
743 }
744 } else {
745 assert(0 && "Unknown constant value!");
746 }
747}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000748
Chris Lattner55a6d902005-11-10 18:06:33 +0000749/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000750/// the predicate isString is true.
751///
Chris Lattner55a6d902005-11-10 18:06:33 +0000752static void printAsCString(std::ostream &O, const ConstantArray *CVA,
753 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000754 assert(CVA->isString() && "Array is not string compatible!");
755
756 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000757 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000758 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000759 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskey1c055e82007-01-25 15:12:02 +0000760 printStringChar(O, C);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000761 }
762 O << "\"";
763}
764
Jeff Cohen24a62a92006-05-02 01:16:28 +0000765/// EmitString - Emit a zero-byte-terminated string constant.
766///
767void AsmPrinter::EmitString(const ConstantArray *CVA) const {
768 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000769 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000770 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000771 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000772 printAsCString(O, CVA, NumElts-1);
773 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000774 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000775 printAsCString(O, CVA, NumElts);
776 }
777 O << "\n";
778}
779
Chris Lattnerbb644e32005-11-21 07:51:36 +0000780/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000781///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000782void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000783 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000784
Chris Lattner61753bf2004-10-16 18:19:26 +0000785 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000786 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000787 return;
788 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
789 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000790 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000791 } else { // Not a string. Print the values in successive locations
792 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000793 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000794 }
795 return;
796 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
797 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000798 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000799 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000800 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
801 const Constant* field = CVS->getOperand(i);
802
803 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000804 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattnerb84892d2007-02-10 19:59:22 +0000805 uint64_t padSize = ((i == e-1? cvsLayout->getSizeInBytes()
Chris Lattnerc473d8e2007-02-10 19:55:17 +0000806 : cvsLayout->getElementOffset(i+1))
807 - cvsLayout->getElementOffset(i)) - fieldSize;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000808 sizeSoFar += fieldSize + padSize;
809
810 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000811 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000812
813 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000814 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000815 }
Chris Lattnerb84892d2007-02-10 19:59:22 +0000816 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
Chris Lattner8452a1f2004-08-17 06:36:49 +0000817 "Layout of constant struct may be incorrect!");
818 return;
819 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
820 // FP Constants are printed as integer constants to avoid losing
821 // precision...
822 double Val = CFP->getValue();
823 if (CFP->getType() == Type::DoubleTy) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000824 if (TAI->getData64bitsDirective())
825 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
826 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000827 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000828 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
829 << "\t" << TAI->getCommentString()
830 << " double most significant word " << Val << "\n";
831 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
832 << "\t" << TAI->getCommentString()
833 << " double least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000834 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000835 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
836 << "\t" << TAI->getCommentString()
837 << " double least significant word " << Val << "\n";
838 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
839 << "\t" << TAI->getCommentString()
840 << " double most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000841 }
842 return;
843 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000844 O << TAI->getData32bitsDirective() << FloatToBits(Val)
845 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000846 return;
847 }
Reid Spencere63b6512006-12-31 05:55:36 +0000848 } else if (CV->getType() == Type::Int64Ty) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000849 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000850 uint64_t Val = CI->getZExtValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000851
Jim Laskeya6211dc2006-09-06 18:34:40 +0000852 if (TAI->getData64bitsDirective())
853 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000854 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000855 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
856 << "\t" << TAI->getCommentString()
857 << " Double-word most significant word " << Val << "\n";
858 O << TAI->getData32bitsDirective() << unsigned(Val)
859 << "\t" << TAI->getCommentString()
860 << " Double-word least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000861 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000862 O << TAI->getData32bitsDirective() << unsigned(Val)
863 << "\t" << TAI->getCommentString()
864 << " Double-word least significant word " << Val << "\n";
865 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
866 << "\t" << TAI->getCommentString()
867 << " Double-word most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000868 }
869 return;
870 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000871 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
872 const VectorType *PTy = CP->getType();
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000873
874 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
875 EmitGlobalConstant(CP->getOperand(I));
876
877 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000878 }
879
880 const Type *type = CV->getType();
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000881 printDataDirective(type);
Chris Lattnerbb644e32005-11-21 07:51:36 +0000882 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000883 O << "\n";
884}
Chris Lattner061d9e22006-01-27 02:10:10 +0000885
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000886void
887AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
888 // Target doesn't support this yet!
889 abort();
890}
891
Chris Lattnera32814b2006-09-26 23:59:50 +0000892/// PrintSpecial - Print information related to the specified machine instr
893/// that is independent of the operand, and may be independent of the instr
894/// itself. This can be useful for portably encoding the comment character
895/// or other bits of target-specific knowledge into the asmstrings. The
896/// syntax used is ${:comment}. Targets can override this to add support
897/// for their own strange codes.
898void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattner30b47082006-09-27 00:06:07 +0000899 if (!strcmp(Code, "private")) {
900 O << TAI->getPrivateGlobalPrefix();
901 } else if (!strcmp(Code, "comment")) {
Chris Lattnera32814b2006-09-26 23:59:50 +0000902 O << TAI->getCommentString();
903 } else if (!strcmp(Code, "uid")) {
904 // Assign a unique ID to this machine instruction.
905 static const MachineInstr *LastMI = 0;
Chris Lattnera9428c42007-02-05 21:23:52 +0000906 static const Function *F = 0;
Chris Lattnera32814b2006-09-26 23:59:50 +0000907 static unsigned Counter = 0U-1;
Chris Lattnera9428c42007-02-05 21:23:52 +0000908
909 // Comparing the address of MI isn't sufficient, because machineinstrs may
910 // be allocated to the same address across functions.
911 const Function *ThisF = MI->getParent()->getParent()->getFunction();
912
Chris Lattnera32814b2006-09-26 23:59:50 +0000913 // If this is a new machine instruction, bump the counter.
Chris Lattnera9428c42007-02-05 21:23:52 +0000914 if (LastMI != MI || F != ThisF) {
915 ++Counter;
916 LastMI = MI;
Chris Lattner89e66e02007-02-06 01:56:31 +0000917 F = ThisF;
Chris Lattnera9428c42007-02-05 21:23:52 +0000918 }
Chris Lattnera32814b2006-09-26 23:59:50 +0000919 O << Counter;
920 } else {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000921 cerr << "Unknown special formatter '" << Code
922 << "' for machine instr: " << *MI;
Chris Lattnera32814b2006-09-26 23:59:50 +0000923 exit(1);
924 }
925}
926
927
Chris Lattner061d9e22006-01-27 02:10:10 +0000928/// printInlineAsm - This method formats and prints the specified machine
929/// instruction that is an inline asm.
930void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +0000931 unsigned NumOperands = MI->getNumOperands();
932
933 // Count the number of register definitions.
934 unsigned NumDefs = 0;
Chris Lattner45456d42006-09-05 20:02:51 +0000935 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
936 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +0000937 assert(NumDefs != NumOperands-1 && "No asm string?");
938
939 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000940
941 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000942 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000943
Chris Lattner3390cbe2006-07-28 00:17:20 +0000944 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
945 if (AsmStr[0] == 0) {
946 O << "\n"; // Tab already printed, avoid double indenting next instr.
947 return;
948 }
949
Jim Laskeya6211dc2006-09-06 18:34:40 +0000950 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +0000951
Bill Wendlinge21237e2007-01-16 03:42:04 +0000952 // The variant of the current asmprinter.
953 int AsmPrinterVariant = TAI->getAssemblerDialect();
954
Chris Lattneraa23fa92006-02-01 22:41:11 +0000955 int CurVariant = -1; // The number of the {.|.|.} region we are in.
956 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000957
Chris Lattneraa23fa92006-02-01 22:41:11 +0000958 while (*LastEmitted) {
959 switch (*LastEmitted) {
960 default: {
961 // Not a special case, emit the string section literally.
962 const char *LiteralEnd = LastEmitted+1;
963 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000964 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000965 ++LiteralEnd;
966 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
967 O.write(LastEmitted, LiteralEnd-LastEmitted);
968 LastEmitted = LiteralEnd;
969 break;
970 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000971 case '\n':
972 ++LastEmitted; // Consume newline character.
Chris Lattnerb58f93f2007-04-30 17:00:18 +0000973 O << "\n"; // Indent code with newline.
Chris Lattnera633c3132006-05-05 21:47:05 +0000974 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000975 case '$': {
976 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000977 bool Done = true;
978
979 // Handle escapes.
980 switch (*LastEmitted) {
981 default: Done = false; break;
982 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +0000983 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
984 O << '$';
985 ++LastEmitted; // Consume second '$' character.
986 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000987 case '(': // $( -> same as GCC's { character.
988 ++LastEmitted; // Consume '(' character.
989 if (CurVariant != -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000990 cerr << "Nested variants found in inline asm string: '"
991 << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000992 exit(1);
993 }
994 CurVariant = 0; // We're in the first variant now.
995 break;
996 case '|':
997 ++LastEmitted; // consume '|' character.
998 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000999 cerr << "Found '|' character outside of variant in inline asm "
1000 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001001 exit(1);
1002 }
1003 ++CurVariant; // We're in the next variant.
1004 break;
1005 case ')': // $) -> same as GCC's } char.
1006 ++LastEmitted; // consume ')' character.
1007 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001008 cerr << "Found '}' character outside of variant in inline asm "
1009 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001010 exit(1);
1011 }
1012 CurVariant = -1;
1013 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001014 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001015 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001016
1017 bool HasCurlyBraces = false;
1018 if (*LastEmitted == '{') { // ${variable}
1019 ++LastEmitted; // Consume '{' character.
1020 HasCurlyBraces = true;
1021 }
1022
1023 const char *IDStart = LastEmitted;
1024 char *IDEnd;
Chris Lattnerd39e3882007-01-23 00:36:17 +00001025 errno = 0;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001026 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1027 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001028 cerr << "Bad $ operand number in inline asm string: '"
1029 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001030 exit(1);
1031 }
1032 LastEmitted = IDEnd;
1033
Chris Lattner34f74c12006-02-06 22:17:23 +00001034 char Modifier[2] = { 0, 0 };
1035
Chris Lattneraa23fa92006-02-01 22:41:11 +00001036 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +00001037 // If we have curly braces, check for a modifier character. This
1038 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1039 if (*LastEmitted == ':') {
1040 ++LastEmitted; // Consume ':' character.
1041 if (*LastEmitted == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001042 cerr << "Bad ${:} expression in inline asm string: '"
1043 << AsmStr << "'\n";
Chris Lattner34f74c12006-02-06 22:17:23 +00001044 exit(1);
1045 }
1046
1047 Modifier[0] = *LastEmitted;
1048 ++LastEmitted; // Consume modifier character.
1049 }
1050
Chris Lattneraa23fa92006-02-01 22:41:11 +00001051 if (*LastEmitted != '}') {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001052 cerr << "Bad ${} expression in inline asm string: '"
1053 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001054 exit(1);
1055 }
1056 ++LastEmitted; // Consume '}' character.
1057 }
1058
1059 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001060 cerr << "Invalid $ operand number in inline asm string: '"
1061 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001062 exit(1);
1063 }
1064
Chris Lattner571d9642006-02-23 19:21:04 +00001065 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +00001066 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +00001067 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1068 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001069
1070 bool Error = false;
1071
Chris Lattner571d9642006-02-23 19:21:04 +00001072 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +00001073 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001074 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner5af3fde2006-02-24 19:50:58 +00001075 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1076 OpNo += (OpFlags >> 3) + 1;
1077 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001078
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001079 if (OpNo >= MI->getNumOperands()) {
1080 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +00001081 } else {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001082 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1083 ++OpNo; // Skip over the ID number.
1084
1085 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1086 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1087 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1088 Modifier[0] ? Modifier : 0);
1089 } else {
1090 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1091 Modifier[0] ? Modifier : 0);
1092 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001093 }
1094 if (Error) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001095 cerr << "Invalid operand found in inline asm: '"
1096 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001097 MI->dump();
1098 exit(1);
1099 }
Chris Lattner571d9642006-02-23 19:21:04 +00001100 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001101 break;
1102 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001103 }
1104 }
Jim Laskeya6211dc2006-09-06 18:34:40 +00001105 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001106}
1107
Jim Laskeyf9e54452007-01-26 14:34:52 +00001108/// printLabel - This method prints a local label used by debug and
1109/// exception handling tables.
1110void AsmPrinter::printLabel(const MachineInstr *MI) const {
Jim Laskeyc3de9b42007-02-01 16:31:34 +00001111 O << "\n"
1112 << TAI->getPrivateGlobalPrefix()
Jim Laskey09953e62007-02-21 22:48:45 +00001113 << "label"
Jim Laskeyf9e54452007-01-26 14:34:52 +00001114 << MI->getOperand(0).getImmedValue()
1115 << ":\n";
1116}
1117
Chris Lattneraa23fa92006-02-01 22:41:11 +00001118/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1119/// instruction, using the specified assembler variant. Targets should
1120/// overried this to format as appropriate.
1121bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +00001122 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +00001123 // Target doesn't support this yet!
1124 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +00001125}
Chris Lattner1d08c652006-02-24 20:21:58 +00001126
1127bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1128 unsigned AsmVariant,
1129 const char *ExtraCode) {
1130 // Target doesn't support this yet!
1131 return true;
1132}
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001133
1134/// printBasicBlockLabel - This method prints the label for the specified
1135/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +00001136void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
1137 bool printColon,
1138 bool printComment) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001139 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman4971ba52006-05-02 17:36:46 +00001140 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +00001141 if (printColon)
1142 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +00001143 if (printComment && MBB->getBasicBlock())
Jim Laskeya6211dc2006-09-06 18:34:40 +00001144 O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001145}
Nate Begeman984c1a42006-08-12 21:29:52 +00001146
1147/// printSetLabel - This method prints a set label for the specified
1148/// MachineBasicBlock
1149void AsmPrinter::printSetLabel(unsigned uid,
1150 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001151 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +00001152 return;
1153
Jim Laskeya6211dc2006-09-06 18:34:40 +00001154 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
1155 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman984c1a42006-08-12 21:29:52 +00001156 printBasicBlockLabel(MBB, false, false);
Jim Laskeya6211dc2006-09-06 18:34:40 +00001157 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman984c1a42006-08-12 21:29:52 +00001158 << '_' << uid << '\n';
1159}
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001160
Evan Cheng91f120f2006-11-01 09:23:08 +00001161void AsmPrinter::printSetLabel(unsigned uid, unsigned uid2,
1162 const MachineBasicBlock *MBB) const {
1163 if (!TAI->getSetDirective())
1164 return;
1165
1166 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
1167 << getFunctionNumber() << '_' << uid << '_' << uid2
1168 << "_set_" << MBB->getNumber() << ',';
1169 printBasicBlockLabel(MBB, false, false);
1170 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1171 << '_' << uid << '_' << uid2 << '\n';
1172}
1173
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001174/// printDataDirective - This method prints the asm directive for the
1175/// specified type.
1176void AsmPrinter::printDataDirective(const Type *type) {
1177 const TargetData *TD = TM.getTargetData();
1178 switch (type->getTypeID()) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001179 case Type::IntegerTyID: {
1180 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1181 if (BitWidth <= 8)
1182 O << TAI->getData8bitsDirective();
1183 else if (BitWidth <= 16)
1184 O << TAI->getData16bitsDirective();
1185 else if (BitWidth <= 32)
1186 O << TAI->getData32bitsDirective();
1187 else if (BitWidth <= 64) {
1188 assert(TAI->getData64bitsDirective() &&
1189 "Target cannot handle 64-bit constant exprs!");
1190 O << TAI->getData64bitsDirective();
1191 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001192 break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001193 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001194 case Type::PointerTyID:
1195 if (TD->getPointerSize() == 8) {
1196 assert(TAI->getData64bitsDirective() &&
1197 "Target cannot handle 64-bit pointer exprs!");
1198 O << TAI->getData64bitsDirective();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001199 } else {
1200 O << TAI->getData32bitsDirective();
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001201 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001202 break;
1203 case Type::FloatTyID: case Type::DoubleTyID:
1204 assert (0 && "Should have already output floating point constant.");
1205 default:
1206 assert (0 && "Can't handle printing this type of thing");
1207 break;
1208 }
1209}
Dale Johannesen915e1542007-02-16 01:54:53 +00001210