blob: f7571831a344184a2163ce8f7dae88cc1ea8544b [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
Jim Laskey261779b2006-09-07 22:06:40 +000035AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
36 const TargetAsmInfo *T)
Jim Laskeya6211dc2006-09-06 18:34:40 +000037: FunctionNumber(0), O(o), TM(tm), TAI(T)
38{}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000039
Chris Lattnerdc822412006-10-05 02:42:47 +000040std::string AsmPrinter::getSectionForFunction(const Function &F) const {
41 return TAI->getTextSection();
42}
43
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000044
Chris Lattner8488ba22006-05-09 04:59:56 +000045/// SwitchToTextSection - Switch to the specified text section of the executable
46/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000047///
Chris Lattner8488ba22006-05-09 04:59:56 +000048void AsmPrinter::SwitchToTextSection(const char *NewSection,
49 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000050 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000051 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000052 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000053 else
54 NS = NewSection;
55
56 // If we're already in this section, we're done.
57 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000058
Chris Lattner4ebc6a22006-05-09 05:33:48 +000059 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000060 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
61 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000062
Chris Lattner4ebc6a22006-05-09 05:33:48 +000063 CurrentSection = NS;
64
65 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000066 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000067}
68
Nate Begeman78756502006-07-27 01:13:04 +000069/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000070/// if we are not already in it!
71///
72void AsmPrinter::SwitchToDataSection(const char *NewSection,
73 const GlobalValue *GV) {
74 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000075 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000076 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner6341df82006-05-09 05:23:12 +000077 else
78 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +000079
Chris Lattner6341df82006-05-09 05:23:12 +000080 // If we're already in this section, we're done.
81 if (CurrentSection == NS) return;
82
Chris Lattner4ebc6a22006-05-09 05:33:48 +000083 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000084 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
85 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner4ebc6a22006-05-09 05:33:48 +000086
87 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +000088
Chris Lattner4ebc6a22006-05-09 05:33:48 +000089 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000090 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +000091}
92
93
Chris Lattner6a8e0f52004-08-16 23:15:22 +000094bool AsmPrinter::doInitialization(Module &M) {
Jim Laskeya6211dc2006-09-06 18:34:40 +000095 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +000096
Chris Lattner00fcdfe2006-01-24 04:16:34 +000097 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000098 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +000099 << M.getModuleInlineAsm()
Jim Laskeya6211dc2006-09-06 18:34:40 +0000100 << "\n" << TAI->getCommentString()
101 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000102
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000103 SwitchToDataSection(""); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000104
Jim Laskeyc56315c2007-01-26 21:22:28 +0000105 if (MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>()) {
106 MMI->AnalyzeModule(M);
Jim Laskey0bbdc552006-01-26 20:21:46 +0000107 }
108
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000109 return false;
110}
111
112bool AsmPrinter::doFinalization(Module &M) {
Rafael Espindolad7998d02006-12-18 03:37:18 +0000113 if (TAI->getWeakRefDirective()) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000114 if (!ExtWeakSymbols.empty())
Rafael Espindolad7998d02006-12-18 03:37:18 +0000115 SwitchToDataSection("");
116
117 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
118 e = ExtWeakSymbols.end(); i != e; ++i) {
119 const GlobalValue *GV = *i;
120 std::string Name = Mang->getValueName(GV);
121 O << TAI->getWeakRefDirective() << Name << "\n";
122 }
123 }
124
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000125 if (TAI->getSetDirective()) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000126 if (!M.alias_empty())
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000127 SwitchToTextSection(TAI->getTextSection());
128
129 O << "\n";
130 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
131 I!=E; ++I) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000132 const Constant *Aliasee = dyn_cast_or_null<Constant>(I->getAliasee());
133 assert(Aliasee && "Aliasee cannot be null");
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000134
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000135 std::string Name = Mang->getValueName(I);
136 std::string Target;
137
138 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Aliasee))
139 Target = Mang->getValueName(GV);
140 else {
141 const ConstantExpr *CE = 0;
142 if ((CE = dyn_cast<ConstantExpr>(Aliasee)) &&
143 (CE->getOpcode() == Instruction::BitCast))
144 Target = Mang->getValueName(CE->getOperand(0));
145 else
146 assert(0 && "Unsupported aliasee");
147 }
148
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000149 if (I->hasExternalLinkage())
150 O << "\t.globl\t" << Name << "\n";
151 else if (I->hasWeakLinkage())
152 O << TAI->getWeakRefDirective() << Name << "\n";
153 else if (!I->hasInternalLinkage())
154 assert(0 && "Invalid alias linkage");
155
156 O << TAI->getSetDirective() << Name << ", " << Target << "\n";
157 }
158 }
159
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000160 delete Mang; Mang = 0;
161 return false;
162}
163
Chris Lattnerbb644e32005-11-21 07:51:36 +0000164void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000165 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000166 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000167 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000168}
169
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000170/// EmitConstantPool - Print to the current output stream assembly
171/// representations of the constants in the constant pool MCP. This is
172/// used to print out constants which have been "spilled to memory" by
173/// the code generator.
174///
175void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000176 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000177 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000178
179 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
180 // in special sections.
181 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
182 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
183 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
184 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000185 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000186 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000187 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng2ad050f2006-09-14 07:35:00 +0000188 const Type *Ty = CPE.getType();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000189 if (TAI->getFourByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000190 TM.getTargetData()->getTypeSize(Ty) == 4)
191 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000192 else if (TAI->getEightByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000193 TM.getTargetData()->getTypeSize(Ty) == 8)
194 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000195 else if (TAI->getSixteenByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000196 TM.getTargetData()->getTypeSize(Ty) == 16)
197 SixteenByteCPs.push_back(std::make_pair(CPE, i));
198 else
199 OtherCPs.push_back(std::make_pair(CPE, i));
200 }
201
202 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000203 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
204 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
205 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
206 SixteenByteCPs);
207 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Chengec1d60b2006-06-29 00:26:09 +0000208}
209
210void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
211 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
212 if (CP.empty()) return;
213
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000214 SwitchToDataSection(Section);
Evan Chengec1d60b2006-06-29 00:26:09 +0000215 EmitAlignment(Alignment);
216 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000217 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
218 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Cheng2ad050f2006-09-14 07:35:00 +0000219 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
220 if (CP[i].first.isMachineConstantPoolEntry())
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000221 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000222 else
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000223 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000224 if (i != e-1) {
Evan Cheng2ad050f2006-09-14 07:35:00 +0000225 const Type *Ty = CP[i].first.getType();
Evan Chengec1d60b2006-06-29 00:26:09 +0000226 unsigned EntSize =
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000227 TM.getTargetData()->getTypeSize(Ty);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000228 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000229 // Emit inter-object padding for alignment.
Evan Cheng2ad050f2006-09-14 07:35:00 +0000230 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000231 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000232 }
233}
234
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000235/// EmitJumpTableInfo - Print assembly representations of the jump tables used
236/// by the current function to the current output stream.
237///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000238void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
239 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000240 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
241 if (JT.empty()) return;
Jim Laskey70323a82006-12-14 19:17:33 +0000242 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begemanefc312a2006-07-27 16:46:58 +0000243
Jim Laskey70323a82006-12-14 19:17:33 +0000244 // Use JumpTableDirective otherwise honor the entry size from the jump table
245 // info.
Andrew Lenharth783a4a92006-09-24 19:45:58 +0000246 const char *JTEntryDirective = TAI->getJumpTableDirective();
Jim Laskey70323a82006-12-14 19:17:33 +0000247 bool HadJTEntryDirective = JTEntryDirective != NULL;
248 if (!HadJTEntryDirective) {
249 JTEntryDirective = MJTI->getEntrySize() == 4 ?
250 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
251 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000252
Nate Begeman78756502006-07-27 01:13:04 +0000253 // Pick the directive to use to print the jump table entries, and switch to
254 // the appropriate section.
Jim Laskey70323a82006-12-14 19:17:33 +0000255 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000256
257 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
258 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
259 !JumpTableDataSection) {
Jim Laskey70323a82006-12-14 19:17:33 +0000260 // In PIC mode, we need to emit the jump table to the same section as the
261 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000262 // We should also do if the section name is NULL.
Jim Laskey70323a82006-12-14 19:17:33 +0000263 const Function *F = MF.getFunction();
264 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman78756502006-07-27 01:13:04 +0000265 } else {
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000266 SwitchToDataSection(JumpTableDataSection);
Nate Begeman78756502006-07-27 01:13:04 +0000267 }
Jim Laskey70323a82006-12-14 19:17:33 +0000268
269 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000270
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000271 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000272 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner28bfe382006-10-28 18:10:06 +0000273
274 // If this jump table was deleted, ignore it.
275 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-08-12 21:29:52 +0000276
277 // For PIC codegen, if possible we want to use the SetDirective to reduce
278 // the number of relocations the assembler will generate for the jump table.
279 // Set directives are all printed before the jump table itself.
280 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskey70323a82006-12-14 19:17:33 +0000281 if (TAI->getSetDirective() && IsPic)
Nate Begeman984c1a42006-08-12 21:29:52 +0000282 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
283 if (EmittedSets.insert(JTBBs[ii]).second)
284 printSetLabel(i, JTBBs[ii]);
285
Chris Lattner0ee2d462007-01-18 01:12:56 +0000286 // On some targets (e.g. darwin) we want to emit two consequtive labels
287 // before each jump table. The first label is never referenced, but tells
288 // the assembler and linker the extents of the jump table object. The
289 // second label is actually referenced by the code.
290 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
291 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
292
Jim Laskeya6211dc2006-09-06 18:34:40 +0000293 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
294 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000295
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000296 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begemanefc312a2006-07-27 16:46:58 +0000297 O << JTEntryDirective << ' ';
Nate Begeman984c1a42006-08-12 21:29:52 +0000298 // If we have emitted set directives for the jump table entries, print
299 // them rather than the entries themselves. If we're emitting PIC, then
300 // emit the table entries as differences between two text section labels.
301 // If we're emitting non-PIC code, then emit the entries as direct
302 // references to the target basic blocks.
303 if (!EmittedSets.empty()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000304 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
305 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Jim Laskey70323a82006-12-14 19:17:33 +0000306 } else if (IsPic) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000307 printBasicBlockLabel(JTBBs[ii], false, false);
Chris Lattner0ee2d462007-01-18 01:12:56 +0000308 // If the arch uses custom Jump Table directives, don't calc relative to
309 // JT
Jim Laskey70323a82006-12-14 19:17:33 +0000310 if (!HadJTEntryDirective)
311 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
312 << getFunctionNumber() << '_' << i;
Nate Begeman984c1a42006-08-12 21:29:52 +0000313 } else {
314 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman78756502006-07-27 01:13:04 +0000315 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000316 O << '\n';
317 }
318 }
319}
320
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000321/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
322/// special global used by LLVM. If so, emit it and return true, otherwise
323/// do nothing and return false.
324bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000325 // Ignore debug and non-emitted data.
326 if (GV->getSection() == "llvm.metadata") return true;
327
328 if (!GV->hasAppendingLinkage()) return false;
329
330 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000331
Chris Lattner66af3902006-09-26 03:38:18 +0000332 if (GV->getName() == "llvm.used") {
333 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
334 EmitLLVMUsedList(GV->getInitializer());
335 return true;
336 }
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000337
Chris Lattner3760e902006-01-12 19:17:23 +0000338 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000339 SwitchToDataSection(TAI->getStaticCtorsSection());
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000340 EmitAlignment(2, 0);
341 EmitXXStructorList(GV->getInitializer());
342 return true;
343 }
344
Chris Lattner3760e902006-01-12 19:17:23 +0000345 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000346 SwitchToDataSection(TAI->getStaticDtorsSection());
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000347 EmitAlignment(2, 0);
348 EmitXXStructorList(GV->getInitializer());
349 return true;
350 }
351
352 return false;
353}
354
Chris Lattner66af3902006-09-26 03:38:18 +0000355/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
356/// global in the specified llvm.used list as being used with this directive.
357void AsmPrinter::EmitLLVMUsedList(Constant *List) {
358 const char *Directive = TAI->getUsedDirective();
359
360 // Should be an array of 'sbyte*'.
361 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
362 if (InitList == 0) return;
363
364 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
365 O << Directive;
366 EmitConstantValueOnly(InitList->getOperand(i));
367 O << "\n";
368 }
369}
370
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000371/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
372/// function pointers, ignoring the init priority.
373void AsmPrinter::EmitXXStructorList(Constant *List) {
374 // Should be an array of '{ int, void ()* }' structs. The first value is the
375 // init priority, which we ignore.
376 if (!isa<ConstantArray>(List)) return;
377 ConstantArray *InitList = cast<ConstantArray>(List);
378 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
379 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
380 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000381
382 if (CS->getOperand(1)->isNullValue())
383 return; // Found a null terminator, exit printing.
384 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000385 EmitGlobalConstant(CS->getOperand(1));
386 }
387}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000388
Jim Laskey71262542006-10-17 13:41:07 +0000389/// getGlobalLinkName - Returns the asm/link name of of the specified
390/// global variable. Should be overridden by each target asm printer to
391/// generate the appropriate value.
Jim Laskeyd24b9132006-10-17 17:17:24 +0000392const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
393 std::string LinkName;
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000394
395 if (isa<Function>(GV)) {
396 LinkName += TAI->getFunctionAddrPrefix();
397 LinkName += Mang->getValueName(GV);
398 LinkName += TAI->getFunctionAddrSuffix();
399 } else {
400 LinkName += TAI->getGlobalVarAddrPrefix();
401 LinkName += Mang->getValueName(GV);
402 LinkName += TAI->getGlobalVarAddrSuffix();
403 }
404
Jim Laskeyd24b9132006-10-17 17:17:24 +0000405 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000406}
407
Jim Laskey18fc0972007-02-21 22:47:38 +0000408/// EmitExternalGlobal - Emit the external reference to a global variable.
409/// Should be overridden if an indirect reference should be used.
410void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
411 O << getGlobalLinkName(GV);
412}
413
414
415
Jim Laskey1c055e82007-01-25 15:12:02 +0000416//===----------------------------------------------------------------------===//
417/// LEB 128 number encoding.
418
419/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
420/// representing an unsigned leb128 value.
421void AsmPrinter::PrintULEB128(unsigned Value) const {
422 do {
423 unsigned Byte = Value & 0x7f;
424 Value >>= 7;
425 if (Value) Byte |= 0x80;
426 O << "0x" << std::hex << Byte << std::dec;
427 if (Value) O << ", ";
428 } while (Value);
429}
430
431/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
432/// value.
433unsigned AsmPrinter::SizeULEB128(unsigned Value) {
434 unsigned Size = 0;
435 do {
436 Value >>= 7;
437 Size += sizeof(int8_t);
438 } while (Value);
439 return Size;
440}
441
442/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
443/// representing a signed leb128 value.
444void AsmPrinter::PrintSLEB128(int Value) const {
445 int Sign = Value >> (8 * sizeof(Value) - 1);
446 bool IsMore;
447
448 do {
449 unsigned Byte = Value & 0x7f;
450 Value >>= 7;
451 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
452 if (IsMore) Byte |= 0x80;
453 O << "0x" << std::hex << Byte << std::dec;
454 if (IsMore) O << ", ";
455 } while (IsMore);
456}
457
458/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
459/// value.
460unsigned AsmPrinter::SizeSLEB128(int Value) {
461 unsigned Size = 0;
462 int Sign = Value >> (8 * sizeof(Value) - 1);
463 bool IsMore;
464
465 do {
466 unsigned Byte = Value & 0x7f;
467 Value >>= 7;
468 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
469 Size += sizeof(int8_t);
470 } while (IsMore);
471 return Size;
472}
473
474//===--------------------------------------------------------------------===//
475// Emission and print routines
476//
477
478/// PrintHex - Print a value as a hexidecimal value.
479///
480void AsmPrinter::PrintHex(int Value) const {
481 O << "0x" << std::hex << Value << std::dec;
482}
483
484/// EOL - Print a newline character to asm stream. If a comment is present
485/// then it will be printed first. Comments should not contain '\n'.
Jim Laskey18fc0972007-02-21 22:47:38 +0000486void AsmPrinter::EOL() const {
487 O << "\n";
488}
Jim Laskey1c055e82007-01-25 15:12:02 +0000489void AsmPrinter::EOL(const std::string &Comment) const {
490 if (AsmVerbose && !Comment.empty()) {
491 O << "\t"
492 << TAI->getCommentString()
493 << " "
494 << Comment;
495 }
496 O << "\n";
497}
498
499/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
500/// unsigned leb128 value.
501void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
502 if (TAI->hasLEB128()) {
503 O << "\t.uleb128\t"
504 << Value;
505 } else {
506 O << TAI->getData8bitsDirective();
507 PrintULEB128(Value);
508 }
509}
510
511/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
512/// signed leb128 value.
513void AsmPrinter::EmitSLEB128Bytes(int Value) const {
514 if (TAI->hasLEB128()) {
515 O << "\t.sleb128\t"
516 << Value;
517 } else {
518 O << TAI->getData8bitsDirective();
519 PrintSLEB128(Value);
520 }
521}
522
523/// EmitInt8 - Emit a byte directive and value.
524///
525void AsmPrinter::EmitInt8(int Value) const {
526 O << TAI->getData8bitsDirective();
527 PrintHex(Value & 0xFF);
528}
529
530/// EmitInt16 - Emit a short directive and value.
531///
532void AsmPrinter::EmitInt16(int Value) const {
533 O << TAI->getData16bitsDirective();
534 PrintHex(Value & 0xFFFF);
535}
536
537/// EmitInt32 - Emit a long directive and value.
538///
539void AsmPrinter::EmitInt32(int Value) const {
540 O << TAI->getData32bitsDirective();
541 PrintHex(Value);
542}
543
544/// EmitInt64 - Emit a long long directive and value.
545///
546void AsmPrinter::EmitInt64(uint64_t Value) const {
547 if (TAI->getData64bitsDirective()) {
548 O << TAI->getData64bitsDirective();
549 PrintHex(Value);
550 } else {
551 if (TM.getTargetData()->isBigEndian()) {
552 EmitInt32(unsigned(Value >> 32)); O << "\n";
553 EmitInt32(unsigned(Value));
554 } else {
555 EmitInt32(unsigned(Value)); O << "\n";
556 EmitInt32(unsigned(Value >> 32));
557 }
558 }
559}
560
561/// toOctal - Convert the low order bits of X into an octal digit.
562///
563static inline char toOctal(int X) {
564 return (X&7)+'0';
565}
566
567/// printStringChar - Print a char, escaped if necessary.
568///
569static void printStringChar(std::ostream &O, unsigned char C) {
570 if (C == '"') {
571 O << "\\\"";
572 } else if (C == '\\') {
573 O << "\\\\";
574 } else if (isprint(C)) {
575 O << C;
576 } else {
577 switch(C) {
578 case '\b': O << "\\b"; break;
579 case '\f': O << "\\f"; break;
580 case '\n': O << "\\n"; break;
581 case '\r': O << "\\r"; break;
582 case '\t': O << "\\t"; break;
583 default:
584 O << '\\';
585 O << toOctal(C >> 6);
586 O << toOctal(C >> 3);
587 O << toOctal(C >> 0);
588 break;
589 }
590 }
591}
592
593/// EmitString - Emit a string with quotes and a null terminator.
594/// Special characters are emitted properly.
595/// \literal (Eg. '\t') \endliteral
596void AsmPrinter::EmitString(const std::string &String) const {
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000597 const char* AscizDirective = TAI->getAscizDirective();
598 if (AscizDirective)
599 O << AscizDirective;
600 else
601 O << TAI->getAsciiDirective();
602 O << "\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000603 for (unsigned i = 0, N = String.size(); i < N; ++i) {
604 unsigned char C = String[i];
605 printStringChar(O, C);
606 }
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000607 if (AscizDirective)
608 O << "\"";
609 else
610 O << "\\0\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000611}
612
613
614//===----------------------------------------------------------------------===//
615
Chris Lattnerbb644e32005-11-21 07:51:36 +0000616// EmitAlignment - Emit an alignment directive to the specified power of two.
Dale Johannesen14a28f12007-04-23 19:58:54 +0000617// Use the maximum of the specified alignment and the alignment from the
618// specified GlobalValue (if any).
Chris Lattnerbb644e32005-11-21 07:51:36 +0000619void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Dale Johannesen8653d292007-04-23 23:33:31 +0000620 if (GV && GV->getAlignment())
621 NumBits = std::max(NumBits, Log2_32(GV->getAlignment()));
Chris Lattner747960d2005-11-10 18:09:27 +0000622 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000623 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
624 O << TAI->getAlignDirective() << NumBits << "\n";
Chris Lattner1d35c162004-08-17 19:14:29 +0000625}
626
Jim Laskey18fc0972007-02-21 22:47:38 +0000627
Chris Lattnerbb644e32005-11-21 07:51:36 +0000628/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000629///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000630void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000631 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000632 if (TAI->getZeroDirective()) {
633 O << TAI->getZeroDirective() << NumZeros;
634 if (TAI->getZeroDirectiveSuffix())
635 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000636 O << "\n";
637 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000638 for (; NumZeros; --NumZeros)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000639 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000640 }
641 }
642}
643
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000644// Print out the specified constant, without a storage class. Only the
645// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000646void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000647 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000648 O << "0";
Zhou Sheng75b871f2007-01-11 12:24:14 +0000649 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner7f6fac42007-01-12 18:15:09 +0000650 O << CI->getZExtValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000651 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000652 // This is a constant address for a global variable or function. Use the
653 // name of the variable or function as the address value, possibly
654 // decorating it with GlobalVarAddrPrefix/Suffix or
655 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000656 if (isa<Function>(GV)) {
657 O << TAI->getFunctionAddrPrefix()
658 << Mang->getValueName(GV)
659 << TAI->getFunctionAddrSuffix();
660 } else {
661 O << TAI->getGlobalVarAddrPrefix()
662 << Mang->getValueName(GV)
663 << TAI->getGlobalVarAddrSuffix();
664 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000665 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000666 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000667 unsigned Opcode = CE->getOpcode();
668 switch (Opcode) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000669 case Instruction::GetElementPtr: {
670 // generate a symbolic expression for the byte address
671 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner83dfca82007-02-10 20:31:59 +0000672 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
673 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
674 idxVec.size())) {
Chris Lattner145569b2005-02-14 21:40:26 +0000675 if (Offset)
676 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000677 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000678 if (Offset > 0)
679 O << ") + " << Offset;
680 else if (Offset < 0)
681 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000682 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000683 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000684 }
685 break;
686 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000687 case Instruction::Trunc:
688 case Instruction::ZExt:
689 case Instruction::SExt:
690 case Instruction::FPTrunc:
691 case Instruction::FPExt:
692 case Instruction::UIToFP:
693 case Instruction::SIToFP:
694 case Instruction::FPToUI:
695 case Instruction::FPToSI:
696 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
697 break;
Chris Lattnerb9e41f52006-12-12 05:14:13 +0000698 case Instruction::BitCast:
699 return EmitConstantValueOnly(CE->getOperand(0));
700
Chris Lattner0c537da2006-12-12 05:18:19 +0000701 case Instruction::IntToPtr: {
702 // Handle casts to pointers by changing them into casts to the appropriate
703 // integer type. This promotes constant folding and simplifies this code.
704 Constant *Op = CE->getOperand(0);
705 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
706 return EmitConstantValueOnly(Op);
707 }
708
709
710 case Instruction::PtrToInt: {
Chris Lattner85492422006-07-29 01:57:19 +0000711 // Support only foldable casts to/from pointers that can be eliminated by
712 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000713 Constant *Op = CE->getOperand(0);
Chris Lattner0c537da2006-12-12 05:18:19 +0000714 const Type *Ty = CE->getType();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000715
Chris Lattner0c537da2006-12-12 05:18:19 +0000716 // We can emit the pointer value into this slot if the slot is an
717 // integer slot greater or equal to the size of the pointer.
Chris Lattner03c49532007-01-15 02:27:26 +0000718 if (Ty->isInteger() &&
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000719 TD->getTypeSize(Ty) >= TD->getTypeSize(Op->getType()))
Chris Lattner85492422006-07-29 01:57:19 +0000720 return EmitConstantValueOnly(Op);
Chris Lattner85492422006-07-29 01:57:19 +0000721
722 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000723 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000724 break;
725 }
726 case Instruction::Add:
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000727 case Instruction::Sub:
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000728 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000729 EmitConstantValueOnly(CE->getOperand(0));
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000730 O << (Opcode==Instruction::Add ? ") + (" : ") - (");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000731 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000732 O << ")";
733 break;
734 default:
735 assert(0 && "Unsupported operator!");
736 }
737 } else {
738 assert(0 && "Unknown constant value!");
739 }
740}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000741
Chris Lattner55a6d902005-11-10 18:06:33 +0000742/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000743/// the predicate isString is true.
744///
Chris Lattner55a6d902005-11-10 18:06:33 +0000745static void printAsCString(std::ostream &O, const ConstantArray *CVA,
746 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000747 assert(CVA->isString() && "Array is not string compatible!");
748
749 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000750 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000751 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000752 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskey1c055e82007-01-25 15:12:02 +0000753 printStringChar(O, C);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000754 }
755 O << "\"";
756}
757
Jeff Cohen24a62a92006-05-02 01:16:28 +0000758/// EmitString - Emit a zero-byte-terminated string constant.
759///
760void AsmPrinter::EmitString(const ConstantArray *CVA) const {
761 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000762 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000763 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000764 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000765 printAsCString(O, CVA, NumElts-1);
766 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000767 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000768 printAsCString(O, CVA, NumElts);
769 }
770 O << "\n";
771}
772
Chris Lattnerbb644e32005-11-21 07:51:36 +0000773/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000774///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000775void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000776 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000777
Chris Lattner61753bf2004-10-16 18:19:26 +0000778 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000779 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000780 return;
781 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
782 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000783 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000784 } else { // Not a string. Print the values in successive locations
785 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000786 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000787 }
788 return;
789 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
790 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000791 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000792 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000793 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
794 const Constant* field = CVS->getOperand(i);
795
796 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000797 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattnerb84892d2007-02-10 19:59:22 +0000798 uint64_t padSize = ((i == e-1? cvsLayout->getSizeInBytes()
Chris Lattnerc473d8e2007-02-10 19:55:17 +0000799 : cvsLayout->getElementOffset(i+1))
800 - cvsLayout->getElementOffset(i)) - fieldSize;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000801 sizeSoFar += fieldSize + padSize;
802
803 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000804 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000805
806 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000807 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000808 }
Chris Lattnerb84892d2007-02-10 19:59:22 +0000809 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
Chris Lattner8452a1f2004-08-17 06:36:49 +0000810 "Layout of constant struct may be incorrect!");
811 return;
812 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
813 // FP Constants are printed as integer constants to avoid losing
814 // precision...
815 double Val = CFP->getValue();
816 if (CFP->getType() == Type::DoubleTy) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000817 if (TAI->getData64bitsDirective())
818 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
819 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000820 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000821 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
822 << "\t" << TAI->getCommentString()
823 << " double most significant word " << Val << "\n";
824 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
825 << "\t" << TAI->getCommentString()
826 << " double least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000827 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000828 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
829 << "\t" << TAI->getCommentString()
830 << " double least significant word " << Val << "\n";
831 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
832 << "\t" << TAI->getCommentString()
833 << " double most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000834 }
835 return;
836 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000837 O << TAI->getData32bitsDirective() << FloatToBits(Val)
838 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000839 return;
840 }
Reid Spencere63b6512006-12-31 05:55:36 +0000841 } else if (CV->getType() == Type::Int64Ty) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000842 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000843 uint64_t Val = CI->getZExtValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000844
Jim Laskeya6211dc2006-09-06 18:34:40 +0000845 if (TAI->getData64bitsDirective())
846 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000847 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000848 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
849 << "\t" << TAI->getCommentString()
850 << " Double-word most significant word " << Val << "\n";
851 O << TAI->getData32bitsDirective() << unsigned(Val)
852 << "\t" << TAI->getCommentString()
853 << " Double-word least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000854 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000855 O << TAI->getData32bitsDirective() << unsigned(Val)
856 << "\t" << TAI->getCommentString()
857 << " Double-word least significant word " << Val << "\n";
858 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
859 << "\t" << TAI->getCommentString()
860 << " Double-word most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000861 }
862 return;
863 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000864 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
865 const VectorType *PTy = CP->getType();
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000866
867 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
868 EmitGlobalConstant(CP->getOperand(I));
869
870 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000871 }
872
873 const Type *type = CV->getType();
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000874 printDataDirective(type);
Chris Lattnerbb644e32005-11-21 07:51:36 +0000875 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000876 O << "\n";
877}
Chris Lattner061d9e22006-01-27 02:10:10 +0000878
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000879void
880AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
881 // Target doesn't support this yet!
882 abort();
883}
884
Chris Lattnera32814b2006-09-26 23:59:50 +0000885/// PrintSpecial - Print information related to the specified machine instr
886/// that is independent of the operand, and may be independent of the instr
887/// itself. This can be useful for portably encoding the comment character
888/// or other bits of target-specific knowledge into the asmstrings. The
889/// syntax used is ${:comment}. Targets can override this to add support
890/// for their own strange codes.
891void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattner30b47082006-09-27 00:06:07 +0000892 if (!strcmp(Code, "private")) {
893 O << TAI->getPrivateGlobalPrefix();
894 } else if (!strcmp(Code, "comment")) {
Chris Lattnera32814b2006-09-26 23:59:50 +0000895 O << TAI->getCommentString();
896 } else if (!strcmp(Code, "uid")) {
897 // Assign a unique ID to this machine instruction.
898 static const MachineInstr *LastMI = 0;
Chris Lattnera9428c42007-02-05 21:23:52 +0000899 static const Function *F = 0;
Chris Lattnera32814b2006-09-26 23:59:50 +0000900 static unsigned Counter = 0U-1;
Chris Lattnera9428c42007-02-05 21:23:52 +0000901
902 // Comparing the address of MI isn't sufficient, because machineinstrs may
903 // be allocated to the same address across functions.
904 const Function *ThisF = MI->getParent()->getParent()->getFunction();
905
Chris Lattnera32814b2006-09-26 23:59:50 +0000906 // If this is a new machine instruction, bump the counter.
Chris Lattnera9428c42007-02-05 21:23:52 +0000907 if (LastMI != MI || F != ThisF) {
908 ++Counter;
909 LastMI = MI;
Chris Lattner89e66e02007-02-06 01:56:31 +0000910 F = ThisF;
Chris Lattnera9428c42007-02-05 21:23:52 +0000911 }
Chris Lattnera32814b2006-09-26 23:59:50 +0000912 O << Counter;
913 } else {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000914 cerr << "Unknown special formatter '" << Code
915 << "' for machine instr: " << *MI;
Chris Lattnera32814b2006-09-26 23:59:50 +0000916 exit(1);
917 }
918}
919
920
Chris Lattner061d9e22006-01-27 02:10:10 +0000921/// printInlineAsm - This method formats and prints the specified machine
922/// instruction that is an inline asm.
923void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +0000924 unsigned NumOperands = MI->getNumOperands();
925
926 // Count the number of register definitions.
927 unsigned NumDefs = 0;
Chris Lattner45456d42006-09-05 20:02:51 +0000928 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
929 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +0000930 assert(NumDefs != NumOperands-1 && "No asm string?");
931
932 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000933
934 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000935 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000936
Chris Lattner3390cbe2006-07-28 00:17:20 +0000937 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
938 if (AsmStr[0] == 0) {
939 O << "\n"; // Tab already printed, avoid double indenting next instr.
940 return;
941 }
942
Jim Laskeya6211dc2006-09-06 18:34:40 +0000943 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +0000944
Bill Wendlinge21237e2007-01-16 03:42:04 +0000945 // The variant of the current asmprinter.
946 int AsmPrinterVariant = TAI->getAssemblerDialect();
947
Chris Lattneraa23fa92006-02-01 22:41:11 +0000948 int CurVariant = -1; // The number of the {.|.|.} region we are in.
949 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000950
Chris Lattneraa23fa92006-02-01 22:41:11 +0000951 while (*LastEmitted) {
952 switch (*LastEmitted) {
953 default: {
954 // Not a special case, emit the string section literally.
955 const char *LiteralEnd = LastEmitted+1;
956 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000957 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000958 ++LiteralEnd;
959 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
960 O.write(LastEmitted, LiteralEnd-LastEmitted);
961 LastEmitted = LiteralEnd;
962 break;
963 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000964 case '\n':
965 ++LastEmitted; // Consume newline character.
966 O << "\n\t"; // Indent code with newline.
967 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000968 case '$': {
969 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000970 bool Done = true;
971
972 // Handle escapes.
973 switch (*LastEmitted) {
974 default: Done = false; break;
975 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +0000976 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
977 O << '$';
978 ++LastEmitted; // Consume second '$' character.
979 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000980 case '(': // $( -> same as GCC's { character.
981 ++LastEmitted; // Consume '(' character.
982 if (CurVariant != -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000983 cerr << "Nested variants found in inline asm string: '"
984 << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000985 exit(1);
986 }
987 CurVariant = 0; // We're in the first variant now.
988 break;
989 case '|':
990 ++LastEmitted; // consume '|' character.
991 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000992 cerr << "Found '|' character outside of variant in inline asm "
993 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000994 exit(1);
995 }
996 ++CurVariant; // We're in the next variant.
997 break;
998 case ')': // $) -> same as GCC's } char.
999 ++LastEmitted; // consume ')' character.
1000 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001001 cerr << "Found '}' character outside of variant in inline asm "
1002 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001003 exit(1);
1004 }
1005 CurVariant = -1;
1006 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001007 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001008 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001009
1010 bool HasCurlyBraces = false;
1011 if (*LastEmitted == '{') { // ${variable}
1012 ++LastEmitted; // Consume '{' character.
1013 HasCurlyBraces = true;
1014 }
1015
1016 const char *IDStart = LastEmitted;
1017 char *IDEnd;
Chris Lattnerd39e3882007-01-23 00:36:17 +00001018 errno = 0;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001019 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1020 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001021 cerr << "Bad $ operand number in inline asm string: '"
1022 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001023 exit(1);
1024 }
1025 LastEmitted = IDEnd;
1026
Chris Lattner34f74c12006-02-06 22:17:23 +00001027 char Modifier[2] = { 0, 0 };
1028
Chris Lattneraa23fa92006-02-01 22:41:11 +00001029 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +00001030 // If we have curly braces, check for a modifier character. This
1031 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1032 if (*LastEmitted == ':') {
1033 ++LastEmitted; // Consume ':' character.
1034 if (*LastEmitted == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001035 cerr << "Bad ${:} expression in inline asm string: '"
1036 << AsmStr << "'\n";
Chris Lattner34f74c12006-02-06 22:17:23 +00001037 exit(1);
1038 }
1039
1040 Modifier[0] = *LastEmitted;
1041 ++LastEmitted; // Consume modifier character.
1042 }
1043
Chris Lattneraa23fa92006-02-01 22:41:11 +00001044 if (*LastEmitted != '}') {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001045 cerr << "Bad ${} expression in inline asm string: '"
1046 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001047 exit(1);
1048 }
1049 ++LastEmitted; // Consume '}' character.
1050 }
1051
1052 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001053 cerr << "Invalid $ operand number in inline asm string: '"
1054 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001055 exit(1);
1056 }
1057
Chris Lattner571d9642006-02-23 19:21:04 +00001058 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +00001059 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +00001060 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1061 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001062
1063 bool Error = false;
1064
Chris Lattner571d9642006-02-23 19:21:04 +00001065 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +00001066 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001067 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner5af3fde2006-02-24 19:50:58 +00001068 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1069 OpNo += (OpFlags >> 3) + 1;
1070 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001071
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001072 if (OpNo >= MI->getNumOperands()) {
1073 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +00001074 } else {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001075 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1076 ++OpNo; // Skip over the ID number.
1077
1078 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1079 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1080 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1081 Modifier[0] ? Modifier : 0);
1082 } else {
1083 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1084 Modifier[0] ? Modifier : 0);
1085 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001086 }
1087 if (Error) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001088 cerr << "Invalid operand found in inline asm: '"
1089 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001090 MI->dump();
1091 exit(1);
1092 }
Chris Lattner571d9642006-02-23 19:21:04 +00001093 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001094 break;
1095 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001096 }
1097 }
Jim Laskeya6211dc2006-09-06 18:34:40 +00001098 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001099}
1100
Jim Laskeyf9e54452007-01-26 14:34:52 +00001101/// printLabel - This method prints a local label used by debug and
1102/// exception handling tables.
1103void AsmPrinter::printLabel(const MachineInstr *MI) const {
Jim Laskeyc3de9b42007-02-01 16:31:34 +00001104 O << "\n"
1105 << TAI->getPrivateGlobalPrefix()
Jim Laskey09953e62007-02-21 22:48:45 +00001106 << "label"
Jim Laskeyf9e54452007-01-26 14:34:52 +00001107 << MI->getOperand(0).getImmedValue()
1108 << ":\n";
1109}
1110
Chris Lattneraa23fa92006-02-01 22:41:11 +00001111/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1112/// instruction, using the specified assembler variant. Targets should
1113/// overried this to format as appropriate.
1114bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +00001115 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +00001116 // Target doesn't support this yet!
1117 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +00001118}
Chris Lattner1d08c652006-02-24 20:21:58 +00001119
1120bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1121 unsigned AsmVariant,
1122 const char *ExtraCode) {
1123 // Target doesn't support this yet!
1124 return true;
1125}
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001126
1127/// printBasicBlockLabel - This method prints the label for the specified
1128/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +00001129void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
1130 bool printColon,
1131 bool printComment) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001132 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman4971ba52006-05-02 17:36:46 +00001133 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +00001134 if (printColon)
1135 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +00001136 if (printComment && MBB->getBasicBlock())
Jim Laskeya6211dc2006-09-06 18:34:40 +00001137 O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001138}
Nate Begeman984c1a42006-08-12 21:29:52 +00001139
1140/// printSetLabel - This method prints a set label for the specified
1141/// MachineBasicBlock
1142void AsmPrinter::printSetLabel(unsigned uid,
1143 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001144 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +00001145 return;
1146
Jim Laskeya6211dc2006-09-06 18:34:40 +00001147 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
1148 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman984c1a42006-08-12 21:29:52 +00001149 printBasicBlockLabel(MBB, false, false);
Jim Laskeya6211dc2006-09-06 18:34:40 +00001150 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman984c1a42006-08-12 21:29:52 +00001151 << '_' << uid << '\n';
1152}
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001153
Evan Cheng91f120f2006-11-01 09:23:08 +00001154void AsmPrinter::printSetLabel(unsigned uid, unsigned uid2,
1155 const MachineBasicBlock *MBB) const {
1156 if (!TAI->getSetDirective())
1157 return;
1158
1159 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
1160 << getFunctionNumber() << '_' << uid << '_' << uid2
1161 << "_set_" << MBB->getNumber() << ',';
1162 printBasicBlockLabel(MBB, false, false);
1163 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1164 << '_' << uid << '_' << uid2 << '\n';
1165}
1166
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001167/// printDataDirective - This method prints the asm directive for the
1168/// specified type.
1169void AsmPrinter::printDataDirective(const Type *type) {
1170 const TargetData *TD = TM.getTargetData();
1171 switch (type->getTypeID()) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001172 case Type::IntegerTyID: {
1173 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1174 if (BitWidth <= 8)
1175 O << TAI->getData8bitsDirective();
1176 else if (BitWidth <= 16)
1177 O << TAI->getData16bitsDirective();
1178 else if (BitWidth <= 32)
1179 O << TAI->getData32bitsDirective();
1180 else if (BitWidth <= 64) {
1181 assert(TAI->getData64bitsDirective() &&
1182 "Target cannot handle 64-bit constant exprs!");
1183 O << TAI->getData64bitsDirective();
1184 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001185 break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001186 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001187 case Type::PointerTyID:
1188 if (TD->getPointerSize() == 8) {
1189 assert(TAI->getData64bitsDirective() &&
1190 "Target cannot handle 64-bit pointer exprs!");
1191 O << TAI->getData64bitsDirective();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001192 } else {
1193 O << TAI->getData32bitsDirective();
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001194 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001195 break;
1196 case Type::FloatTyID: case Type::DoubleTyID:
1197 assert (0 && "Should have already output floating point constant.");
1198 default:
1199 assert (0 && "Can't handle printing this type of thing");
1200 break;
1201 }
1202}
Dale Johannesen915e1542007-02-16 01:54:53 +00001203