blob: 0394a02aa16bd2adee1be9fc11090eccf0372f9c [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng932f0222006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng246ae0d2006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman8cfa57b2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman37efe672006-04-22 18:53:45 +000020#include "llvm/CodeGen/MachineJumpTableInfo.h"
Jim Laskeyf1cdea12007-01-25 15:12:02 +000021#include "llvm/Support/CommandLine.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000022#include "llvm/Support/Mangler.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000023#include "llvm/Support/MathExtras.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000024#include "llvm/Support/Streams.h"
Jim Laskey563321a2006-09-06 18:34:40 +000025#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000026#include "llvm/Target/TargetData.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000027#include "llvm/Target/TargetLowering.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000028#include "llvm/Target/TargetMachine.h"
Chris Lattner66099132006-02-01 22:41:11 +000029#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000030using namespace llvm;
31
Jim Laskeyf1cdea12007-01-25 15:12:02 +000032static cl::opt<bool>
33AsmVerbose("asm-verbose", cl::Hidden, cl::desc("Add comments to directives."));
34
Jim Laskeya0f3d172006-09-07 22:06:40 +000035AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
36 const TargetAsmInfo *T)
Jim Laskey563321a2006-09-06 18:34:40 +000037: FunctionNumber(0), O(o), TM(tm), TAI(T)
38{}
Chris Lattnered138932005-12-13 06:32:10 +000039
Chris Lattner52f06702006-10-05 02:42:47 +000040std::string AsmPrinter::getSectionForFunction(const Function &F) const {
41 return TAI->getTextSection();
42}
43
Chris Lattnered138932005-12-13 06:32:10 +000044
Chris Lattner4632d7a2006-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 Lattnerac28fbd2005-11-21 07:06:27 +000047///
Chris Lattner4632d7a2006-05-09 04:59:56 +000048void AsmPrinter::SwitchToTextSection(const char *NewSection,
49 const GlobalValue *GV) {
Chris Lattnerac28fbd2005-11-21 07:06:27 +000050 std::string NS;
Chris Lattnera7090ae2006-05-09 05:24:50 +000051 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000052 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnera7090ae2006-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 Cohen51b776d2006-05-02 03:58:45 +000058
Chris Lattner7faec9b2006-05-09 05:33:48 +000059 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000060 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
61 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen51b776d2006-05-02 03:58:45 +000062
Chris Lattner7faec9b2006-05-09 05:33:48 +000063 CurrentSection = NS;
64
65 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000066 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattnerac28fbd2005-11-21 07:06:27 +000067}
68
Nate Begeman2f1ae882006-07-27 01:13:04 +000069/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner4632d7a2006-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 Lattnerb81cb612006-05-09 05:23:12 +000075 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000076 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnerb81cb612006-05-09 05:23:12 +000077 else
78 NS = NewSection;
Chris Lattner4632d7a2006-05-09 04:59:56 +000079
Chris Lattnerb81cb612006-05-09 05:23:12 +000080 // If we're already in this section, we're done.
81 if (CurrentSection == NS) return;
82
Chris Lattner7faec9b2006-05-09 05:33:48 +000083 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000084 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
85 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner7faec9b2006-05-09 05:33:48 +000086
87 CurrentSection = NS;
Chris Lattner4632d7a2006-05-09 04:59:56 +000088
Chris Lattner7faec9b2006-05-09 05:33:48 +000089 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000090 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner4632d7a2006-05-09 04:59:56 +000091}
92
93
Chris Lattnera80ba712004-08-16 23:15:22 +000094bool AsmPrinter::doInitialization(Module &M) {
Jim Laskey563321a2006-09-06 18:34:40 +000095 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattner2c1b1592006-01-23 23:47:53 +000096
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000097 if (!M.getModuleInlineAsm().empty())
Jim Laskey563321a2006-09-06 18:34:40 +000098 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000099 << M.getModuleInlineAsm()
Jim Laskey563321a2006-09-06 18:34:40 +0000100 << "\n" << TAI->getCommentString()
101 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000102
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000103 SwitchToDataSection(""); // Reset back to no section.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000104
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000105 if (MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>()) {
106 MMI->AnalyzeModule(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000107 }
108
Chris Lattnera80ba712004-08-16 23:15:22 +0000109 return false;
110}
111
112bool AsmPrinter::doFinalization(Module &M) {
Rafael Espindola15404d02006-12-18 03:37:18 +0000113 if (TAI->getWeakRefDirective()) {
114 if (ExtWeakSymbols.begin() != ExtWeakSymbols.end())
115 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
Chris Lattnera80ba712004-08-16 23:15:22 +0000125 delete Mang; Mang = 0;
126 return false;
127}
128
Chris Lattner25045bd2005-11-21 07:51:36 +0000129void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000130 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +0000131 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner77bc2282005-11-21 08:13:27 +0000132 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +0000133}
134
Chris Lattner3b4fd322005-11-21 08:25:09 +0000135/// EmitConstantPool - Print to the current output stream assembly
136/// representations of the constants in the constant pool MCP. This is
137/// used to print out constants which have been "spilled to memory" by
138/// the code generator.
139///
140void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000141 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000142 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000143
144 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
145 // in special sections.
146 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
147 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
148 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
149 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Chengd6594ae2006-09-12 21:00:35 +0000150 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000151 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng2d2cec12006-06-29 00:26:09 +0000152 MachineConstantPoolEntry CPE = CP[i];
Evan Chengd5a99d72006-09-14 07:35:00 +0000153 const Type *Ty = CPE.getType();
Jim Laskey563321a2006-09-06 18:34:40 +0000154 if (TAI->getFourByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000155 TM.getTargetData()->getTypeSize(Ty) == 4)
156 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000157 else if (TAI->getEightByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000158 TM.getTargetData()->getTypeSize(Ty) == 8)
159 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000160 else if (TAI->getSixteenByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000161 TM.getTargetData()->getTypeSize(Ty) == 16)
162 SixteenByteCPs.push_back(std::make_pair(CPE, i));
163 else
164 OtherCPs.push_back(std::make_pair(CPE, i));
165 }
166
167 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskey563321a2006-09-06 18:34:40 +0000168 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
169 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
170 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
171 SixteenByteCPs);
172 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000173}
174
175void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
176 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
177 if (CP.empty()) return;
178
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000179 SwitchToDataSection(Section);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000180 EmitAlignment(Alignment);
181 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Jim Laskey563321a2006-09-06 18:34:40 +0000182 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
183 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Chengd5a99d72006-09-14 07:35:00 +0000184 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
185 if (CP[i].first.isMachineConstantPoolEntry())
Evan Chengd6594ae2006-09-12 21:00:35 +0000186 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Chengd5a99d72006-09-14 07:35:00 +0000187 else
Evan Chengd6594ae2006-09-12 21:00:35 +0000188 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000189 if (i != e-1) {
Evan Chengd5a99d72006-09-14 07:35:00 +0000190 const Type *Ty = CP[i].first.getType();
Evan Cheng2d2cec12006-06-29 00:26:09 +0000191 unsigned EntSize =
Evan Chengd6594ae2006-09-12 21:00:35 +0000192 TM.getTargetData()->getTypeSize(Ty);
Evan Chengd5a99d72006-09-14 07:35:00 +0000193 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattner3029f922006-02-09 04:46:04 +0000194 // Emit inter-object padding for alignment.
Evan Chengd5a99d72006-09-14 07:35:00 +0000195 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattner3029f922006-02-09 04:46:04 +0000196 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000197 }
198}
199
Nate Begeman37efe672006-04-22 18:53:45 +0000200/// EmitJumpTableInfo - Print assembly representations of the jump tables used
201/// by the current function to the current output stream.
202///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000203void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
204 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000205 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
206 if (JT.empty()) return;
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000207 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000208
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000209 // Use JumpTableDirective otherwise honor the entry size from the jump table
210 // info.
Andrew Lenharthbeec30e2006-09-24 19:45:58 +0000211 const char *JTEntryDirective = TAI->getJumpTableDirective();
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000212 bool HadJTEntryDirective = JTEntryDirective != NULL;
213 if (!HadJTEntryDirective) {
214 JTEntryDirective = MJTI->getEntrySize() == 4 ?
215 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
216 }
Nate Begeman37efe672006-04-22 18:53:45 +0000217
Nate Begeman2f1ae882006-07-27 01:13:04 +0000218 // Pick the directive to use to print the jump table entries, and switch to
219 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000220 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000221
222 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
223 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
224 !JumpTableDataSection) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000225 // In PIC mode, we need to emit the jump table to the same section as the
226 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000227 // We should also do if the section name is NULL.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000228 const Function *F = MF.getFunction();
229 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000230 } else {
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000231 SwitchToDataSection(JumpTableDataSection);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000232 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000233
234 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000235
Nate Begeman37efe672006-04-22 18:53:45 +0000236 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000237 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000238
239 // If this jump table was deleted, ignore it.
240 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000241
242 // For PIC codegen, if possible we want to use the SetDirective to reduce
243 // the number of relocations the assembler will generate for the jump table.
244 // Set directives are all printed before the jump table itself.
245 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000246 if (TAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000247 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
248 if (EmittedSets.insert(JTBBs[ii]).second)
249 printSetLabel(i, JTBBs[ii]);
250
Chris Lattner393a8ee2007-01-18 01:12:56 +0000251 // On some targets (e.g. darwin) we want to emit two consequtive labels
252 // before each jump table. The first label is never referenced, but tells
253 // the assembler and linker the extents of the jump table object. The
254 // second label is actually referenced by the code.
255 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
256 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
257
Jim Laskey563321a2006-09-06 18:34:40 +0000258 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
259 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000260
Nate Begeman37efe672006-04-22 18:53:45 +0000261 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000262 O << JTEntryDirective << ' ';
Nate Begeman52a51e382006-08-12 21:29:52 +0000263 // If we have emitted set directives for the jump table entries, print
264 // them rather than the entries themselves. If we're emitting PIC, then
265 // emit the table entries as differences between two text section labels.
266 // If we're emitting non-PIC code, then emit the entries as direct
267 // references to the target basic blocks.
268 if (!EmittedSets.empty()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000269 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
270 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000271 } else if (IsPic) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000272 printBasicBlockLabel(JTBBs[ii], false, false);
Chris Lattner393a8ee2007-01-18 01:12:56 +0000273 // If the arch uses custom Jump Table directives, don't calc relative to
274 // JT
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000275 if (!HadJTEntryDirective)
276 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
277 << getFunctionNumber() << '_' << i;
Nate Begeman52a51e382006-08-12 21:29:52 +0000278 } else {
279 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000280 }
Nate Begeman37efe672006-04-22 18:53:45 +0000281 O << '\n';
282 }
283 }
284}
285
Chris Lattnered138932005-12-13 06:32:10 +0000286/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
287/// special global used by LLVM. If so, emit it and return true, otherwise
288/// do nothing and return false.
289bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey78098112006-03-07 22:00:35 +0000290 // Ignore debug and non-emitted data.
291 if (GV->getSection() == "llvm.metadata") return true;
292
293 if (!GV->hasAppendingLinkage()) return false;
294
295 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000296
Chris Lattnercb05af82006-09-26 03:38:18 +0000297 if (GV->getName() == "llvm.used") {
298 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
299 EmitLLVMUsedList(GV->getInitializer());
300 return true;
301 }
Chris Lattnered138932005-12-13 06:32:10 +0000302
Chris Lattner5166b822006-01-12 19:17:23 +0000303 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000304 SwitchToDataSection(TAI->getStaticCtorsSection());
Chris Lattnered138932005-12-13 06:32:10 +0000305 EmitAlignment(2, 0);
306 EmitXXStructorList(GV->getInitializer());
307 return true;
308 }
309
Chris Lattner5166b822006-01-12 19:17:23 +0000310 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000311 SwitchToDataSection(TAI->getStaticDtorsSection());
Chris Lattnered138932005-12-13 06:32:10 +0000312 EmitAlignment(2, 0);
313 EmitXXStructorList(GV->getInitializer());
314 return true;
315 }
316
317 return false;
318}
319
Chris Lattnercb05af82006-09-26 03:38:18 +0000320/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
321/// global in the specified llvm.used list as being used with this directive.
322void AsmPrinter::EmitLLVMUsedList(Constant *List) {
323 const char *Directive = TAI->getUsedDirective();
324
325 // Should be an array of 'sbyte*'.
326 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
327 if (InitList == 0) return;
328
329 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
330 O << Directive;
331 EmitConstantValueOnly(InitList->getOperand(i));
332 O << "\n";
333 }
334}
335
Chris Lattnered138932005-12-13 06:32:10 +0000336/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
337/// function pointers, ignoring the init priority.
338void AsmPrinter::EmitXXStructorList(Constant *List) {
339 // Should be an array of '{ int, void ()* }' structs. The first value is the
340 // init priority, which we ignore.
341 if (!isa<ConstantArray>(List)) return;
342 ConstantArray *InitList = cast<ConstantArray>(List);
343 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
344 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
345 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000346
347 if (CS->getOperand(1)->isNullValue())
348 return; // Found a null terminator, exit printing.
349 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000350 EmitGlobalConstant(CS->getOperand(1));
351 }
352}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000353
Jim Laskeya1a19f82006-10-17 13:41:07 +0000354/// getGlobalLinkName - Returns the asm/link name of of the specified
355/// global variable. Should be overridden by each target asm printer to
356/// generate the appropriate value.
Jim Laskey99e41ee2006-10-17 17:17:24 +0000357const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
358 std::string LinkName;
Jim Laskey03742482006-11-20 20:29:06 +0000359
360 if (isa<Function>(GV)) {
361 LinkName += TAI->getFunctionAddrPrefix();
362 LinkName += Mang->getValueName(GV);
363 LinkName += TAI->getFunctionAddrSuffix();
364 } else {
365 LinkName += TAI->getGlobalVarAddrPrefix();
366 LinkName += Mang->getValueName(GV);
367 LinkName += TAI->getGlobalVarAddrSuffix();
368 }
369
Jim Laskey99e41ee2006-10-17 17:17:24 +0000370 return LinkName;
Jim Laskeya1a19f82006-10-17 13:41:07 +0000371}
372
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000373//===----------------------------------------------------------------------===//
374/// LEB 128 number encoding.
375
376/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
377/// representing an unsigned leb128 value.
378void AsmPrinter::PrintULEB128(unsigned Value) const {
379 do {
380 unsigned Byte = Value & 0x7f;
381 Value >>= 7;
382 if (Value) Byte |= 0x80;
383 O << "0x" << std::hex << Byte << std::dec;
384 if (Value) O << ", ";
385 } while (Value);
386}
387
388/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
389/// value.
390unsigned AsmPrinter::SizeULEB128(unsigned Value) {
391 unsigned Size = 0;
392 do {
393 Value >>= 7;
394 Size += sizeof(int8_t);
395 } while (Value);
396 return Size;
397}
398
399/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
400/// representing a signed leb128 value.
401void AsmPrinter::PrintSLEB128(int Value) const {
402 int Sign = Value >> (8 * sizeof(Value) - 1);
403 bool IsMore;
404
405 do {
406 unsigned Byte = Value & 0x7f;
407 Value >>= 7;
408 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
409 if (IsMore) Byte |= 0x80;
410 O << "0x" << std::hex << Byte << std::dec;
411 if (IsMore) O << ", ";
412 } while (IsMore);
413}
414
415/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
416/// value.
417unsigned AsmPrinter::SizeSLEB128(int Value) {
418 unsigned Size = 0;
419 int Sign = Value >> (8 * sizeof(Value) - 1);
420 bool IsMore;
421
422 do {
423 unsigned Byte = Value & 0x7f;
424 Value >>= 7;
425 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
426 Size += sizeof(int8_t);
427 } while (IsMore);
428 return Size;
429}
430
431//===--------------------------------------------------------------------===//
432// Emission and print routines
433//
434
435/// PrintHex - Print a value as a hexidecimal value.
436///
437void AsmPrinter::PrintHex(int Value) const {
438 O << "0x" << std::hex << Value << std::dec;
439}
440
441/// EOL - Print a newline character to asm stream. If a comment is present
442/// then it will be printed first. Comments should not contain '\n'.
443void AsmPrinter::EOL(const std::string &Comment) const {
444 if (AsmVerbose && !Comment.empty()) {
445 O << "\t"
446 << TAI->getCommentString()
447 << " "
448 << Comment;
449 }
450 O << "\n";
451}
452
453/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
454/// unsigned leb128 value.
455void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
456 if (TAI->hasLEB128()) {
457 O << "\t.uleb128\t"
458 << Value;
459 } else {
460 O << TAI->getData8bitsDirective();
461 PrintULEB128(Value);
462 }
463}
464
465/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
466/// signed leb128 value.
467void AsmPrinter::EmitSLEB128Bytes(int Value) const {
468 if (TAI->hasLEB128()) {
469 O << "\t.sleb128\t"
470 << Value;
471 } else {
472 O << TAI->getData8bitsDirective();
473 PrintSLEB128(Value);
474 }
475}
476
477/// EmitInt8 - Emit a byte directive and value.
478///
479void AsmPrinter::EmitInt8(int Value) const {
480 O << TAI->getData8bitsDirective();
481 PrintHex(Value & 0xFF);
482}
483
484/// EmitInt16 - Emit a short directive and value.
485///
486void AsmPrinter::EmitInt16(int Value) const {
487 O << TAI->getData16bitsDirective();
488 PrintHex(Value & 0xFFFF);
489}
490
491/// EmitInt32 - Emit a long directive and value.
492///
493void AsmPrinter::EmitInt32(int Value) const {
494 O << TAI->getData32bitsDirective();
495 PrintHex(Value);
496}
497
498/// EmitInt64 - Emit a long long directive and value.
499///
500void AsmPrinter::EmitInt64(uint64_t Value) const {
501 if (TAI->getData64bitsDirective()) {
502 O << TAI->getData64bitsDirective();
503 PrintHex(Value);
504 } else {
505 if (TM.getTargetData()->isBigEndian()) {
506 EmitInt32(unsigned(Value >> 32)); O << "\n";
507 EmitInt32(unsigned(Value));
508 } else {
509 EmitInt32(unsigned(Value)); O << "\n";
510 EmitInt32(unsigned(Value >> 32));
511 }
512 }
513}
514
515/// toOctal - Convert the low order bits of X into an octal digit.
516///
517static inline char toOctal(int X) {
518 return (X&7)+'0';
519}
520
521/// printStringChar - Print a char, escaped if necessary.
522///
523static void printStringChar(std::ostream &O, unsigned char C) {
524 if (C == '"') {
525 O << "\\\"";
526 } else if (C == '\\') {
527 O << "\\\\";
528 } else if (isprint(C)) {
529 O << C;
530 } else {
531 switch(C) {
532 case '\b': O << "\\b"; break;
533 case '\f': O << "\\f"; break;
534 case '\n': O << "\\n"; break;
535 case '\r': O << "\\r"; break;
536 case '\t': O << "\\t"; break;
537 default:
538 O << '\\';
539 O << toOctal(C >> 6);
540 O << toOctal(C >> 3);
541 O << toOctal(C >> 0);
542 break;
543 }
544 }
545}
546
547/// EmitString - Emit a string with quotes and a null terminator.
548/// Special characters are emitted properly.
549/// \literal (Eg. '\t') \endliteral
550void AsmPrinter::EmitString(const std::string &String) const {
551 O << TAI->getAsciiDirective()
552 << "\"";
553 for (unsigned i = 0, N = String.size(); i < N; ++i) {
554 unsigned char C = String[i];
555 printStringChar(O, C);
556 }
557 O << "\\0\"";
558}
559
560
561//===----------------------------------------------------------------------===//
562
Chris Lattner25045bd2005-11-21 07:51:36 +0000563// EmitAlignment - Emit an alignment directive to the specified power of two.
564void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnera1ab72d2005-11-14 19:00:06 +0000565 if (GV && GV->getAlignment())
566 NumBits = Log2_32(GV->getAlignment());
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000567 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskey563321a2006-09-06 18:34:40 +0000568 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
569 O << TAI->getAlignDirective() << NumBits << "\n";
Chris Lattnerbfddc202004-08-17 19:14:29 +0000570}
571
Chris Lattner25045bd2005-11-21 07:51:36 +0000572/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000573///
Chris Lattner25045bd2005-11-21 07:51:36 +0000574void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000575 if (NumZeros) {
Jim Laskey563321a2006-09-06 18:34:40 +0000576 if (TAI->getZeroDirective()) {
577 O << TAI->getZeroDirective() << NumZeros;
578 if (TAI->getZeroDirectiveSuffix())
579 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000580 O << "\n";
581 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000582 for (; NumZeros; --NumZeros)
Jim Laskey563321a2006-09-06 18:34:40 +0000583 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000584 }
585 }
586}
587
Chris Lattnera80ba712004-08-16 23:15:22 +0000588// Print out the specified constant, without a storage class. Only the
589// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000590void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000591 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000592 O << "0";
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000593 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattnera875df32007-01-12 18:15:09 +0000594 O << CI->getZExtValue();
Reid Spencerb83eb642006-10-20 07:07:24 +0000595 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000596 // This is a constant address for a global variable or function. Use the
597 // name of the variable or function as the address value, possibly
598 // decorating it with GlobalVarAddrPrefix/Suffix or
599 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskey563321a2006-09-06 18:34:40 +0000600 if (isa<Function>(GV)) {
601 O << TAI->getFunctionAddrPrefix()
602 << Mang->getValueName(GV)
603 << TAI->getFunctionAddrSuffix();
604 } else {
605 O << TAI->getGlobalVarAddrPrefix()
606 << Mang->getValueName(GV)
607 << TAI->getGlobalVarAddrSuffix();
608 }
Duraid Madina855a5192005-04-02 12:21:51 +0000609 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000610 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000611 unsigned Opcode = CE->getOpcode();
612 switch (Opcode) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000613 case Instruction::GetElementPtr: {
614 // generate a symbolic expression for the byte address
615 const Constant *ptrVal = CE->getOperand(0);
616 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Andersona69571c2006-05-03 01:29:57 +0000617 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner27e19212005-02-14 21:40:26 +0000618 if (Offset)
619 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000620 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000621 if (Offset > 0)
622 O << ") + " << Offset;
623 else if (Offset < 0)
624 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000625 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000626 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000627 }
628 break;
629 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000630 case Instruction::Trunc:
631 case Instruction::ZExt:
632 case Instruction::SExt:
633 case Instruction::FPTrunc:
634 case Instruction::FPExt:
635 case Instruction::UIToFP:
636 case Instruction::SIToFP:
637 case Instruction::FPToUI:
638 case Instruction::FPToSI:
639 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
640 break;
Chris Lattnercb0a6812006-12-12 05:14:13 +0000641 case Instruction::BitCast:
642 return EmitConstantValueOnly(CE->getOperand(0));
643
Chris Lattnerddc94012006-12-12 05:18:19 +0000644 case Instruction::IntToPtr: {
645 // Handle casts to pointers by changing them into casts to the appropriate
646 // integer type. This promotes constant folding and simplifies this code.
647 Constant *Op = CE->getOperand(0);
648 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
649 return EmitConstantValueOnly(Op);
650 }
651
652
653 case Instruction::PtrToInt: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000654 // Support only foldable casts to/from pointers that can be eliminated by
655 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000656 Constant *Op = CE->getOperand(0);
Chris Lattnerddc94012006-12-12 05:18:19 +0000657 const Type *Ty = CE->getType();
Chris Lattnera80ba712004-08-16 23:15:22 +0000658
Chris Lattnerddc94012006-12-12 05:18:19 +0000659 // We can emit the pointer value into this slot if the slot is an
660 // integer slot greater or equal to the size of the pointer.
Chris Lattner42a75512007-01-15 02:27:26 +0000661 if (Ty->isInteger() &&
Reid Spencera54b7cb2007-01-12 07:05:14 +0000662 TD->getTypeSize(Ty) >= TD->getTypeSize(Op->getType()))
Chris Lattner4a6bd332006-07-29 01:57:19 +0000663 return EmitConstantValueOnly(Op);
Chris Lattner4a6bd332006-07-29 01:57:19 +0000664
665 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000666 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000667 break;
668 }
669 case Instruction::Add:
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000670 case Instruction::Sub:
Chris Lattnera80ba712004-08-16 23:15:22 +0000671 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000672 EmitConstantValueOnly(CE->getOperand(0));
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000673 O << (Opcode==Instruction::Add ? ") + (" : ") - (");
Chris Lattner25045bd2005-11-21 07:51:36 +0000674 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000675 O << ")";
676 break;
677 default:
678 assert(0 && "Unsupported operator!");
679 }
680 } else {
681 assert(0 && "Unknown constant value!");
682 }
683}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000684
Chris Lattner2980cef2005-11-10 18:06:33 +0000685/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000686/// the predicate isString is true.
687///
Chris Lattner2980cef2005-11-10 18:06:33 +0000688static void printAsCString(std::ostream &O, const ConstantArray *CVA,
689 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000690 assert(CVA->isString() && "Array is not string compatible!");
691
692 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000693 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000694 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000695 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000696 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000697 }
698 O << "\"";
699}
700
Jeff Cohenc884db42006-05-02 01:16:28 +0000701/// EmitString - Emit a zero-byte-terminated string constant.
702///
703void AsmPrinter::EmitString(const ConstantArray *CVA) const {
704 unsigned NumElts = CVA->getNumOperands();
Jim Laskey563321a2006-09-06 18:34:40 +0000705 if (TAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000706 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskey563321a2006-09-06 18:34:40 +0000707 O << TAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000708 printAsCString(O, CVA, NumElts-1);
709 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000710 O << TAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000711 printAsCString(O, CVA, NumElts);
712 }
713 O << "\n";
714}
715
Chris Lattner25045bd2005-11-21 07:51:36 +0000716/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000717///
Chris Lattner25045bd2005-11-21 07:51:36 +0000718void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Andersona69571c2006-05-03 01:29:57 +0000719 const TargetData *TD = TM.getTargetData();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000720
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000721 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000722 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000723 return;
724 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
725 if (CVA->isString()) {
Jeff Cohenc884db42006-05-02 01:16:28 +0000726 EmitString(CVA);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000727 } else { // Not a string. Print the values in successive locations
728 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000729 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000730 }
731 return;
732 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
733 // Print the fields in successive locations. Pad to align if needed!
Owen Andersona69571c2006-05-03 01:29:57 +0000734 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000735 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000736 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
737 const Constant* field = CVS->getOperand(i);
738
739 // Check if padding is needed and insert one or more 0s.
Owen Andersona69571c2006-05-03 01:29:57 +0000740 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000741 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner1b7e2352004-08-17 06:36:49 +0000742 : cvsLayout->MemberOffsets[i+1])
743 - cvsLayout->MemberOffsets[i]) - fieldSize;
744 sizeSoFar += fieldSize + padSize;
745
746 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000747 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000748
749 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000750 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000751 }
752 assert(sizeSoFar == cvsLayout->StructSize &&
753 "Layout of constant struct may be incorrect!");
754 return;
755 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
756 // FP Constants are printed as integer constants to avoid losing
757 // precision...
758 double Val = CFP->getValue();
759 if (CFP->getType() == Type::DoubleTy) {
Jim Laskey563321a2006-09-06 18:34:40 +0000760 if (TAI->getData64bitsDirective())
761 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
762 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000763 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000764 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
765 << "\t" << TAI->getCommentString()
766 << " double most significant word " << Val << "\n";
767 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
768 << "\t" << TAI->getCommentString()
769 << " double least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000770 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000771 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
772 << "\t" << TAI->getCommentString()
773 << " double least significant word " << Val << "\n";
774 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
775 << "\t" << TAI->getCommentString()
776 << " double most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000777 }
778 return;
779 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000780 O << TAI->getData32bitsDirective() << FloatToBits(Val)
781 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000782 return;
783 }
Reid Spencer47857812006-12-31 05:55:36 +0000784 } else if (CV->getType() == Type::Int64Ty) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000785 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000786 uint64_t Val = CI->getZExtValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000787
Jim Laskey563321a2006-09-06 18:34:40 +0000788 if (TAI->getData64bitsDirective())
789 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000790 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000791 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
792 << "\t" << TAI->getCommentString()
793 << " Double-word most significant word " << Val << "\n";
794 O << TAI->getData32bitsDirective() << unsigned(Val)
795 << "\t" << TAI->getCommentString()
796 << " Double-word least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000797 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000798 O << TAI->getData32bitsDirective() << unsigned(Val)
799 << "\t" << TAI->getCommentString()
800 << " Double-word least significant word " << Val << "\n";
801 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
802 << "\t" << TAI->getCommentString()
803 << " Double-word most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000804 }
805 return;
806 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000807 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
808 const PackedType *PTy = CP->getType();
809
810 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
811 EmitGlobalConstant(CP->getOperand(I));
812
813 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000814 }
815
816 const Type *type = CV->getType();
Evan Chengd6594ae2006-09-12 21:00:35 +0000817 printDataDirective(type);
Chris Lattner25045bd2005-11-21 07:51:36 +0000818 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000819 O << "\n";
820}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000821
Evan Chengd6594ae2006-09-12 21:00:35 +0000822void
823AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
824 // Target doesn't support this yet!
825 abort();
826}
827
Chris Lattner3ce9b672006-09-26 23:59:50 +0000828/// PrintSpecial - Print information related to the specified machine instr
829/// that is independent of the operand, and may be independent of the instr
830/// itself. This can be useful for portably encoding the comment character
831/// or other bits of target-specific knowledge into the asmstrings. The
832/// syntax used is ${:comment}. Targets can override this to add support
833/// for their own strange codes.
834void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattnerbae02cf2006-09-27 00:06:07 +0000835 if (!strcmp(Code, "private")) {
836 O << TAI->getPrivateGlobalPrefix();
837 } else if (!strcmp(Code, "comment")) {
Chris Lattner3ce9b672006-09-26 23:59:50 +0000838 O << TAI->getCommentString();
839 } else if (!strcmp(Code, "uid")) {
840 // Assign a unique ID to this machine instruction.
841 static const MachineInstr *LastMI = 0;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +0000842 static const Function *F = 0;
Chris Lattner3ce9b672006-09-26 23:59:50 +0000843 static unsigned Counter = 0U-1;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +0000844
845 // Comparing the address of MI isn't sufficient, because machineinstrs may
846 // be allocated to the same address across functions.
847 const Function *ThisF = MI->getParent()->getParent()->getFunction();
848
Chris Lattner3ce9b672006-09-26 23:59:50 +0000849 // If this is a new machine instruction, bump the counter.
Chris Lattnerb6a24bf2007-02-05 21:23:52 +0000850 if (LastMI != MI || F != ThisF) {
851 ++Counter;
852 LastMI = MI;
Chris Lattner4b092522007-02-06 01:56:31 +0000853 F = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +0000854 }
Chris Lattner3ce9b672006-09-26 23:59:50 +0000855 O << Counter;
856 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +0000857 cerr << "Unknown special formatter '" << Code
858 << "' for machine instr: " << *MI;
Chris Lattner3ce9b672006-09-26 23:59:50 +0000859 exit(1);
860 }
861}
862
863
Chris Lattner0264d1a2006-01-27 02:10:10 +0000864/// printInlineAsm - This method formats and prints the specified machine
865/// instruction that is an inline asm.
866void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000867 unsigned NumOperands = MI->getNumOperands();
868
869 // Count the number of register definitions.
870 unsigned NumDefs = 0;
Chris Lattner67942f52006-09-05 20:02:51 +0000871 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
872 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000873 assert(NumDefs != NumOperands-1 && "No asm string?");
874
875 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +0000876
877 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000878 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +0000879
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000880 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
881 if (AsmStr[0] == 0) {
882 O << "\n"; // Tab already printed, avoid double indenting next instr.
883 return;
884 }
885
Jim Laskey563321a2006-09-06 18:34:40 +0000886 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000887
Bill Wendlingeb9a42c2007-01-16 03:42:04 +0000888 // The variant of the current asmprinter.
889 int AsmPrinterVariant = TAI->getAssemblerDialect();
890
Chris Lattner66099132006-02-01 22:41:11 +0000891 int CurVariant = -1; // The number of the {.|.|.} region we are in.
892 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +0000893
Chris Lattner66099132006-02-01 22:41:11 +0000894 while (*LastEmitted) {
895 switch (*LastEmitted) {
896 default: {
897 // Not a special case, emit the string section literally.
898 const char *LiteralEnd = LastEmitted+1;
899 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +0000900 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +0000901 ++LiteralEnd;
902 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
903 O.write(LastEmitted, LiteralEnd-LastEmitted);
904 LastEmitted = LiteralEnd;
905 break;
906 }
Chris Lattner1c059972006-05-05 21:47:05 +0000907 case '\n':
908 ++LastEmitted; // Consume newline character.
909 O << "\n\t"; // Indent code with newline.
910 break;
Chris Lattner66099132006-02-01 22:41:11 +0000911 case '$': {
912 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000913 bool Done = true;
914
915 // Handle escapes.
916 switch (*LastEmitted) {
917 default: Done = false; break;
918 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +0000919 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
920 O << '$';
921 ++LastEmitted; // Consume second '$' character.
922 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000923 case '(': // $( -> same as GCC's { character.
924 ++LastEmitted; // Consume '(' character.
925 if (CurVariant != -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000926 cerr << "Nested variants found in inline asm string: '"
927 << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000928 exit(1);
929 }
930 CurVariant = 0; // We're in the first variant now.
931 break;
932 case '|':
933 ++LastEmitted; // consume '|' character.
934 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000935 cerr << "Found '|' character outside of variant in inline asm "
936 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000937 exit(1);
938 }
939 ++CurVariant; // We're in the next variant.
940 break;
941 case ')': // $) -> same as GCC's } char.
942 ++LastEmitted; // consume ')' character.
943 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000944 cerr << "Found '}' character outside of variant in inline asm "
945 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000946 exit(1);
947 }
948 CurVariant = -1;
949 break;
Chris Lattner66099132006-02-01 22:41:11 +0000950 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000951 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +0000952
953 bool HasCurlyBraces = false;
954 if (*LastEmitted == '{') { // ${variable}
955 ++LastEmitted; // Consume '{' character.
956 HasCurlyBraces = true;
957 }
958
959 const char *IDStart = LastEmitted;
960 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +0000961 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +0000962 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
963 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000964 cerr << "Bad $ operand number in inline asm string: '"
965 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +0000966 exit(1);
967 }
968 LastEmitted = IDEnd;
969
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000970 char Modifier[2] = { 0, 0 };
971
Chris Lattner66099132006-02-01 22:41:11 +0000972 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000973 // If we have curly braces, check for a modifier character. This
974 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
975 if (*LastEmitted == ':') {
976 ++LastEmitted; // Consume ':' character.
977 if (*LastEmitted == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000978 cerr << "Bad ${:} expression in inline asm string: '"
979 << AsmStr << "'\n";
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000980 exit(1);
981 }
982
983 Modifier[0] = *LastEmitted;
984 ++LastEmitted; // Consume modifier character.
985 }
986
Chris Lattner66099132006-02-01 22:41:11 +0000987 if (*LastEmitted != '}') {
Bill Wendlinge8156192006-12-07 01:30:32 +0000988 cerr << "Bad ${} expression in inline asm string: '"
989 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +0000990 exit(1);
991 }
992 ++LastEmitted; // Consume '}' character.
993 }
994
995 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000996 cerr << "Invalid $ operand number in inline asm string: '"
997 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +0000998 exit(1);
999 }
1000
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001001 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001002 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001003 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1004 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001005
1006 bool Error = false;
1007
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001008 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001009 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001010 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001011 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1012 OpNo += (OpFlags >> 3) + 1;
1013 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001014
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001015 if (OpNo >= MI->getNumOperands()) {
1016 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001017 } else {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001018 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1019 ++OpNo; // Skip over the ID number.
1020
1021 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1022 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1023 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1024 Modifier[0] ? Modifier : 0);
1025 } else {
1026 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1027 Modifier[0] ? Modifier : 0);
1028 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001029 }
1030 if (Error) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001031 cerr << "Invalid operand found in inline asm: '"
1032 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001033 MI->dump();
1034 exit(1);
1035 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001036 }
Chris Lattner66099132006-02-01 22:41:11 +00001037 break;
1038 }
Chris Lattner66099132006-02-01 22:41:11 +00001039 }
1040 }
Jim Laskey563321a2006-09-06 18:34:40 +00001041 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattner66099132006-02-01 22:41:11 +00001042}
1043
Jim Laskey1ee29252007-01-26 14:34:52 +00001044/// printLabel - This method prints a local label used by debug and
1045/// exception handling tables.
1046void AsmPrinter::printLabel(const MachineInstr *MI) const {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001047 O << "\n"
1048 << TAI->getPrivateGlobalPrefix()
Jim Laskey29bf16a2007-02-01 17:46:10 +00001049 << "label_"
Jim Laskey1ee29252007-01-26 14:34:52 +00001050 << MI->getOperand(0).getImmedValue()
1051 << ":\n";
1052}
1053
Chris Lattner66099132006-02-01 22:41:11 +00001054/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1055/// instruction, using the specified assembler variant. Targets should
1056/// overried this to format as appropriate.
1057bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001058 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001059 // Target doesn't support this yet!
1060 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001061}
Chris Lattnerdd260332006-02-24 20:21:58 +00001062
1063bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1064 unsigned AsmVariant,
1065 const char *ExtraCode) {
1066 // Target doesn't support this yet!
1067 return true;
1068}
Nate Begeman37efe672006-04-22 18:53:45 +00001069
1070/// printBasicBlockLabel - This method prints the label for the specified
1071/// MachineBasicBlock
Nate Begemancdf38c42006-05-02 05:37:32 +00001072void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
1073 bool printColon,
1074 bool printComment) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001075 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman9d51eeb2006-05-02 17:36:46 +00001076 << MBB->getNumber();
Nate Begemancdf38c42006-05-02 05:37:32 +00001077 if (printColon)
1078 O << ':';
Chris Lattner9c78ecb2006-10-05 21:40:14 +00001079 if (printComment && MBB->getBasicBlock())
Jim Laskey563321a2006-09-06 18:34:40 +00001080 O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
Nate Begeman37efe672006-04-22 18:53:45 +00001081}
Nate Begeman52a51e382006-08-12 21:29:52 +00001082
1083/// printSetLabel - This method prints a set label for the specified
1084/// MachineBasicBlock
1085void AsmPrinter::printSetLabel(unsigned uid,
1086 const MachineBasicBlock *MBB) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001087 if (!TAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001088 return;
1089
Jim Laskey563321a2006-09-06 18:34:40 +00001090 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
1091 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman52a51e382006-08-12 21:29:52 +00001092 printBasicBlockLabel(MBB, false, false);
Jim Laskey563321a2006-09-06 18:34:40 +00001093 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman52a51e382006-08-12 21:29:52 +00001094 << '_' << uid << '\n';
1095}
Evan Chengd6594ae2006-09-12 21:00:35 +00001096
Evan Cheng41349c12006-11-01 09:23:08 +00001097void AsmPrinter::printSetLabel(unsigned uid, unsigned uid2,
1098 const MachineBasicBlock *MBB) const {
1099 if (!TAI->getSetDirective())
1100 return;
1101
1102 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
1103 << getFunctionNumber() << '_' << uid << '_' << uid2
1104 << "_set_" << MBB->getNumber() << ',';
1105 printBasicBlockLabel(MBB, false, false);
1106 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1107 << '_' << uid << '_' << uid2 << '\n';
1108}
1109
Evan Chengd6594ae2006-09-12 21:00:35 +00001110/// printDataDirective - This method prints the asm directive for the
1111/// specified type.
1112void AsmPrinter::printDataDirective(const Type *type) {
1113 const TargetData *TD = TM.getTargetData();
1114 switch (type->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001115 case Type::IntegerTyID: {
1116 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1117 if (BitWidth <= 8)
1118 O << TAI->getData8bitsDirective();
1119 else if (BitWidth <= 16)
1120 O << TAI->getData16bitsDirective();
1121 else if (BitWidth <= 32)
1122 O << TAI->getData32bitsDirective();
1123 else if (BitWidth <= 64) {
1124 assert(TAI->getData64bitsDirective() &&
1125 "Target cannot handle 64-bit constant exprs!");
1126 O << TAI->getData64bitsDirective();
1127 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001128 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001129 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001130 case Type::PointerTyID:
1131 if (TD->getPointerSize() == 8) {
1132 assert(TAI->getData64bitsDirective() &&
1133 "Target cannot handle 64-bit pointer exprs!");
1134 O << TAI->getData64bitsDirective();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001135 } else {
1136 O << TAI->getData32bitsDirective();
Evan Chengd6594ae2006-09-12 21:00:35 +00001137 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001138 break;
1139 case Type::FloatTyID: case Type::DoubleTyID:
1140 assert (0 && "Should have already output floating point constant.");
1141 default:
1142 assert (0 && "Can't handle printing this type of thing");
1143 break;
1144 }
1145}