blob: 3500d1245a08582f7ddde310e05a9d1065c42693 [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng932f0222006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng246ae0d2006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman8cfa57b2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman37efe672006-04-22 18:53:45 +000020#include "llvm/CodeGen/MachineJumpTableInfo.h"
Jim Laskeyf1cdea12007-01-25 15:12:02 +000021#include "llvm/Support/CommandLine.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000022#include "llvm/Support/Mangler.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000023#include "llvm/Support/MathExtras.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000024#include "llvm/Support/Streams.h"
Jim Laskey563321a2006-09-06 18:34:40 +000025#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000026#include "llvm/Target/TargetData.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000027#include "llvm/Target/TargetLowering.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000028#include "llvm/Target/TargetMachine.h"
Chris Lattner66099132006-02-01 22:41:11 +000029#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000030using namespace llvm;
31
Jim Laskeyf1cdea12007-01-25 15:12:02 +000032static cl::opt<bool>
33AsmVerbose("asm-verbose", cl::Hidden, cl::desc("Add comments to directives."));
34
Devang Patel19974732007-05-03 01:11:54 +000035char AsmPrinter::ID = 0;
Jim Laskeya0f3d172006-09-07 22:06:40 +000036AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
37 const TargetAsmInfo *T)
Evan Cheng347d39f2007-10-14 05:57:21 +000038 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o), TM(tm), TAI(T)
Jim Laskey563321a2006-09-06 18:34:40 +000039{}
Chris Lattnered138932005-12-13 06:32:10 +000040
Chris Lattner52f06702006-10-05 02:42:47 +000041std::string AsmPrinter::getSectionForFunction(const Function &F) const {
42 return TAI->getTextSection();
43}
44
Chris Lattnered138932005-12-13 06:32:10 +000045
Chris Lattner4632d7a2006-05-09 04:59:56 +000046/// SwitchToTextSection - Switch to the specified text section of the executable
47/// if we are not already in it!
Chris Lattnerac28fbd2005-11-21 07:06:27 +000048///
Chris Lattner4632d7a2006-05-09 04:59:56 +000049void AsmPrinter::SwitchToTextSection(const char *NewSection,
50 const GlobalValue *GV) {
Chris Lattnerac28fbd2005-11-21 07:06:27 +000051 std::string NS;
Chris Lattnera7090ae2006-05-09 05:24:50 +000052 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000053 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnera7090ae2006-05-09 05:24:50 +000054 else
55 NS = NewSection;
56
57 // If we're already in this section, we're done.
58 if (CurrentSection == NS) return;
Jeff Cohen51b776d2006-05-02 03:58:45 +000059
Chris Lattner7faec9b2006-05-09 05:33:48 +000060 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000061 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
62 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen51b776d2006-05-02 03:58:45 +000063
Chris Lattner7faec9b2006-05-09 05:33:48 +000064 CurrentSection = NS;
65
66 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000067 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattnerac28fbd2005-11-21 07:06:27 +000068}
69
Nate Begeman2f1ae882006-07-27 01:13:04 +000070/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner4632d7a2006-05-09 04:59:56 +000071/// if we are not already in it!
72///
73void AsmPrinter::SwitchToDataSection(const char *NewSection,
74 const GlobalValue *GV) {
75 std::string NS;
Chris Lattnerb81cb612006-05-09 05:23:12 +000076 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000077 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnerb81cb612006-05-09 05:23:12 +000078 else
79 NS = NewSection;
Chris Lattner4632d7a2006-05-09 04:59:56 +000080
Chris Lattnerb81cb612006-05-09 05:23:12 +000081 // If we're already in this section, we're done.
82 if (CurrentSection == NS) return;
83
Chris Lattner7faec9b2006-05-09 05:33:48 +000084 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000085 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
86 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner7faec9b2006-05-09 05:33:48 +000087
88 CurrentSection = NS;
Chris Lattner4632d7a2006-05-09 04:59:56 +000089
Chris Lattner7faec9b2006-05-09 05:33:48 +000090 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000091 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner4632d7a2006-05-09 04:59:56 +000092}
93
94
Chris Lattnera80ba712004-08-16 23:15:22 +000095bool AsmPrinter::doInitialization(Module &M) {
Jim Laskey563321a2006-09-06 18:34:40 +000096 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattner2c1b1592006-01-23 23:47:53 +000097
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000098 if (!M.getModuleInlineAsm().empty())
Jim Laskey563321a2006-09-06 18:34:40 +000099 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000100 << M.getModuleInlineAsm()
Jim Laskey563321a2006-09-06 18:34:40 +0000101 << "\n" << TAI->getCommentString()
102 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000103
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000104 SwitchToDataSection(""); // Reset back to no section.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000105
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000106 if (MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>()) {
107 MMI->AnalyzeModule(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000108 }
109
Chris Lattnera80ba712004-08-16 23:15:22 +0000110 return false;
111}
112
113bool AsmPrinter::doFinalization(Module &M) {
Rafael Espindola15404d02006-12-18 03:37:18 +0000114 if (TAI->getWeakRefDirective()) {
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000115 if (!ExtWeakSymbols.empty())
Rafael Espindola15404d02006-12-18 03:37:18 +0000116 SwitchToDataSection("");
117
118 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
119 e = ExtWeakSymbols.end(); i != e; ++i) {
120 const GlobalValue *GV = *i;
121 std::string Name = Mang->getValueName(GV);
122 O << TAI->getWeakRefDirective() << Name << "\n";
123 }
124 }
125
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000126 if (TAI->getSetDirective()) {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000127 if (!M.alias_empty())
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000128 SwitchToTextSection(TAI->getTextSection());
129
130 O << "\n";
131 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
132 I!=E; ++I) {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000133 std::string Name = Mang->getValueName(I);
134 std::string Target;
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000135
136 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
137 Target = Mang->getValueName(GV);
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000138
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000139 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000140 O << "\t.globl\t" << Name << "\n";
141 else if (I->hasWeakLinkage())
142 O << TAI->getWeakRefDirective() << Name << "\n";
143 else if (!I->hasInternalLinkage())
144 assert(0 && "Invalid alias linkage");
145
146 O << TAI->getSetDirective() << Name << ", " << Target << "\n";
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000147
148 // If the aliasee has external weak linkage it can be referenced only by
149 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
150 // weak reference in such case.
151 if (GV->hasExternalWeakLinkage())
152 if (TAI->getWeakRefDirective())
153 O << TAI->getWeakRefDirective() << Target << "\n";
154 else
155 O << "\t.globl\t" << Target << "\n";
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000156 }
157 }
158
Chris Lattnera80ba712004-08-16 23:15:22 +0000159 delete Mang; Mang = 0;
160 return false;
161}
162
Bill Wendlingce613282007-09-18 09:10:16 +0000163std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
Bill Wendling6e198962007-09-18 01:47:22 +0000164 assert(MF && "No machine function?");
Bill Wendling5f19cf52007-09-18 05:03:44 +0000165 return Mang->makeNameProper(MF->getFunction()->getName() + ".eh",
166 TAI->getGlobalPrefix());
Bill Wendling6e198962007-09-18 01:47:22 +0000167}
168
Chris Lattner25045bd2005-11-21 07:51:36 +0000169void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000170 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +0000171 CurrentFnName = Mang->getValueName(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000172 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +0000173}
174
Chris Lattner3b4fd322005-11-21 08:25:09 +0000175/// EmitConstantPool - Print to the current output stream assembly
176/// representations of the constants in the constant pool MCP. This is
177/// used to print out constants which have been "spilled to memory" by
178/// the code generator.
179///
180void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000181 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000182 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000183
184 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
185 // in special sections.
186 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
187 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
188 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
189 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Chengd6594ae2006-09-12 21:00:35 +0000190 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000191 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng2d2cec12006-06-29 00:26:09 +0000192 MachineConstantPoolEntry CPE = CP[i];
Evan Chengd5a99d72006-09-14 07:35:00 +0000193 const Type *Ty = CPE.getType();
Jim Laskey563321a2006-09-06 18:34:40 +0000194 if (TAI->getFourByteConstantSection() &&
Duncan Sandsca0ed742007-11-05 00:04:43 +0000195 TM.getTargetData()->getABITypeSize(Ty) == 4)
Evan Cheng2d2cec12006-06-29 00:26:09 +0000196 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000197 else if (TAI->getEightByteConstantSection() &&
Duncan Sandsca0ed742007-11-05 00:04:43 +0000198 TM.getTargetData()->getABITypeSize(Ty) == 8)
Evan Cheng2d2cec12006-06-29 00:26:09 +0000199 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000200 else if (TAI->getSixteenByteConstantSection() &&
Duncan Sandsca0ed742007-11-05 00:04:43 +0000201 TM.getTargetData()->getABITypeSize(Ty) == 16)
Evan Cheng2d2cec12006-06-29 00:26:09 +0000202 SixteenByteCPs.push_back(std::make_pair(CPE, i));
203 else
204 OtherCPs.push_back(std::make_pair(CPE, i));
205 }
206
207 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskey563321a2006-09-06 18:34:40 +0000208 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
209 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
210 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
211 SixteenByteCPs);
212 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000213}
214
215void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
216 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
217 if (CP.empty()) return;
218
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000219 SwitchToDataSection(Section);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000220 EmitAlignment(Alignment);
221 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000222 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
223 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Chengd5a99d72006-09-14 07:35:00 +0000224 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
225 if (CP[i].first.isMachineConstantPoolEntry())
Evan Chengd6594ae2006-09-12 21:00:35 +0000226 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Chengd5a99d72006-09-14 07:35:00 +0000227 else
Evan Chengd6594ae2006-09-12 21:00:35 +0000228 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000229 if (i != e-1) {
Evan Chengd5a99d72006-09-14 07:35:00 +0000230 const Type *Ty = CP[i].first.getType();
Evan Cheng2d2cec12006-06-29 00:26:09 +0000231 unsigned EntSize =
Duncan Sandsca0ed742007-11-05 00:04:43 +0000232 TM.getTargetData()->getABITypeSize(Ty);
Evan Chengd5a99d72006-09-14 07:35:00 +0000233 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattner3029f922006-02-09 04:46:04 +0000234 // Emit inter-object padding for alignment.
Evan Chengd5a99d72006-09-14 07:35:00 +0000235 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattner3029f922006-02-09 04:46:04 +0000236 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000237 }
238}
239
Nate Begeman37efe672006-04-22 18:53:45 +0000240/// EmitJumpTableInfo - Print assembly representations of the jump tables used
241/// by the current function to the current output stream.
242///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000243void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
244 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000245 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
246 if (JT.empty()) return;
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000247 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000248
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000249 // Use JumpTableDirective otherwise honor the entry size from the jump table
250 // info.
Andrew Lenharthbeec30e2006-09-24 19:45:58 +0000251 const char *JTEntryDirective = TAI->getJumpTableDirective();
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000252 bool HadJTEntryDirective = JTEntryDirective != NULL;
253 if (!HadJTEntryDirective) {
254 JTEntryDirective = MJTI->getEntrySize() == 4 ?
255 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
256 }
Nate Begeman37efe672006-04-22 18:53:45 +0000257
Nate Begeman2f1ae882006-07-27 01:13:04 +0000258 // Pick the directive to use to print the jump table entries, and switch to
259 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000260 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000261
262 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
263 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
264 !JumpTableDataSection) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000265 // In PIC mode, we need to emit the jump table to the same section as the
266 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000267 // We should also do if the section name is NULL.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000268 const Function *F = MF.getFunction();
269 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000270 } else {
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000271 SwitchToDataSection(JumpTableDataSection);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000272 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000273
274 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000275
Nate Begeman37efe672006-04-22 18:53:45 +0000276 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000277 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000278
279 // If this jump table was deleted, ignore it.
280 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000281
282 // For PIC codegen, if possible we want to use the SetDirective to reduce
283 // the number of relocations the assembler will generate for the jump table.
284 // Set directives are all printed before the jump table itself.
285 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000286 if (TAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000287 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
288 if (EmittedSets.insert(JTBBs[ii]).second)
289 printSetLabel(i, JTBBs[ii]);
290
Chris Lattner393a8ee2007-01-18 01:12:56 +0000291 // On some targets (e.g. darwin) we want to emit two consequtive labels
292 // before each jump table. The first label is never referenced, but tells
293 // the assembler and linker the extents of the jump table object. The
294 // second label is actually referenced by the code.
295 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
Evan Cheng347d39f2007-10-14 05:57:21 +0000296 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Chris Lattner393a8ee2007-01-18 01:12:56 +0000297
Evan Cheng347d39f2007-10-14 05:57:21 +0000298 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
299 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000300
Nate Begeman37efe672006-04-22 18:53:45 +0000301 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000302 O << JTEntryDirective << ' ';
Nate Begeman52a51e382006-08-12 21:29:52 +0000303 // If we have emitted set directives for the jump table entries, print
304 // them rather than the entries themselves. If we're emitting PIC, then
305 // emit the table entries as differences between two text section labels.
306 // If we're emitting non-PIC code, then emit the entries as direct
307 // references to the target basic blocks.
308 if (!EmittedSets.empty()) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000309 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
310 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000311 } else if (IsPic) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000312 printBasicBlockLabel(JTBBs[ii], false, false);
Chris Lattner393a8ee2007-01-18 01:12:56 +0000313 // If the arch uses custom Jump Table directives, don't calc relative to
314 // JT
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000315 if (!HadJTEntryDirective)
316 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
Evan Cheng347d39f2007-10-14 05:57:21 +0000317 << getFunctionNumber() << '_' << i;
Nate Begeman52a51e382006-08-12 21:29:52 +0000318 } else {
319 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000320 }
Nate Begeman37efe672006-04-22 18:53:45 +0000321 O << '\n';
322 }
323 }
324}
325
Chris Lattnered138932005-12-13 06:32:10 +0000326/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
327/// special global used by LLVM. If so, emit it and return true, otherwise
328/// do nothing and return false.
329bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000330 if (GV->getName() == "llvm.used") {
331 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
332 EmitLLVMUsedList(GV->getInitializer());
333 return true;
334 }
335
Jim Laskey78098112006-03-07 22:00:35 +0000336 // Ignore debug and non-emitted data.
337 if (GV->getSection() == "llvm.metadata") return true;
338
339 if (!GV->hasAppendingLinkage()) return false;
340
341 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000342
Evan Cheng916d07c2007-06-04 20:39:18 +0000343 const TargetData *TD = TM.getTargetData();
344 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner5166b822006-01-12 19:17:23 +0000345 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000346 SwitchToDataSection(TAI->getStaticCtorsSection());
Evan Cheng916d07c2007-06-04 20:39:18 +0000347 EmitAlignment(Align, 0);
Chris Lattnered138932005-12-13 06:32:10 +0000348 EmitXXStructorList(GV->getInitializer());
349 return true;
350 }
351
Chris Lattner5166b822006-01-12 19:17:23 +0000352 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000353 SwitchToDataSection(TAI->getStaticDtorsSection());
Evan Cheng916d07c2007-06-04 20:39:18 +0000354 EmitAlignment(Align, 0);
Chris Lattnered138932005-12-13 06:32:10 +0000355 EmitXXStructorList(GV->getInitializer());
356 return true;
357 }
358
359 return false;
360}
361
Chris Lattnercb05af82006-09-26 03:38:18 +0000362/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
363/// global in the specified llvm.used list as being used with this directive.
364void AsmPrinter::EmitLLVMUsedList(Constant *List) {
365 const char *Directive = TAI->getUsedDirective();
366
367 // Should be an array of 'sbyte*'.
368 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
369 if (InitList == 0) return;
370
371 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
372 O << Directive;
373 EmitConstantValueOnly(InitList->getOperand(i));
374 O << "\n";
375 }
376}
377
Chris Lattnered138932005-12-13 06:32:10 +0000378/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
379/// function pointers, ignoring the init priority.
380void AsmPrinter::EmitXXStructorList(Constant *List) {
381 // Should be an array of '{ int, void ()* }' structs. The first value is the
382 // init priority, which we ignore.
383 if (!isa<ConstantArray>(List)) return;
384 ConstantArray *InitList = cast<ConstantArray>(List);
385 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
386 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
387 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000388
389 if (CS->getOperand(1)->isNullValue())
390 return; // Found a null terminator, exit printing.
391 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000392 EmitGlobalConstant(CS->getOperand(1));
393 }
394}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000395
Jim Laskeya1a19f82006-10-17 13:41:07 +0000396/// getGlobalLinkName - Returns the asm/link name of of the specified
397/// global variable. Should be overridden by each target asm printer to
398/// generate the appropriate value.
Jim Laskey99e41ee2006-10-17 17:17:24 +0000399const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
400 std::string LinkName;
Jim Laskey03742482006-11-20 20:29:06 +0000401
402 if (isa<Function>(GV)) {
403 LinkName += TAI->getFunctionAddrPrefix();
404 LinkName += Mang->getValueName(GV);
405 LinkName += TAI->getFunctionAddrSuffix();
406 } else {
407 LinkName += TAI->getGlobalVarAddrPrefix();
408 LinkName += Mang->getValueName(GV);
409 LinkName += TAI->getGlobalVarAddrSuffix();
410 }
411
Jim Laskey99e41ee2006-10-17 17:17:24 +0000412 return LinkName;
Jim Laskeya1a19f82006-10-17 13:41:07 +0000413}
414
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000415/// EmitExternalGlobal - Emit the external reference to a global variable.
416/// Should be overridden if an indirect reference should be used.
417void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
418 O << getGlobalLinkName(GV);
419}
420
421
422
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000423//===----------------------------------------------------------------------===//
424/// LEB 128 number encoding.
425
426/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
427/// representing an unsigned leb128 value.
428void AsmPrinter::PrintULEB128(unsigned Value) const {
429 do {
430 unsigned Byte = Value & 0x7f;
431 Value >>= 7;
432 if (Value) Byte |= 0x80;
433 O << "0x" << std::hex << Byte << std::dec;
434 if (Value) O << ", ";
435 } while (Value);
436}
437
438/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
439/// value.
440unsigned AsmPrinter::SizeULEB128(unsigned Value) {
441 unsigned Size = 0;
442 do {
443 Value >>= 7;
444 Size += sizeof(int8_t);
445 } while (Value);
446 return Size;
447}
448
449/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
450/// representing a signed leb128 value.
451void AsmPrinter::PrintSLEB128(int Value) const {
452 int Sign = Value >> (8 * sizeof(Value) - 1);
453 bool IsMore;
454
455 do {
456 unsigned Byte = Value & 0x7f;
457 Value >>= 7;
458 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
459 if (IsMore) Byte |= 0x80;
460 O << "0x" << std::hex << Byte << std::dec;
461 if (IsMore) O << ", ";
462 } while (IsMore);
463}
464
465/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
466/// value.
467unsigned AsmPrinter::SizeSLEB128(int Value) {
468 unsigned Size = 0;
469 int Sign = Value >> (8 * sizeof(Value) - 1);
470 bool IsMore;
471
472 do {
473 unsigned Byte = Value & 0x7f;
474 Value >>= 7;
475 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
476 Size += sizeof(int8_t);
477 } while (IsMore);
478 return Size;
479}
480
481//===--------------------------------------------------------------------===//
482// Emission and print routines
483//
484
485/// PrintHex - Print a value as a hexidecimal value.
486///
487void AsmPrinter::PrintHex(int Value) const {
488 O << "0x" << std::hex << Value << std::dec;
489}
490
491/// EOL - Print a newline character to asm stream. If a comment is present
492/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000493void AsmPrinter::EOL() const {
494 O << "\n";
495}
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000496void AsmPrinter::EOL(const std::string &Comment) const {
497 if (AsmVerbose && !Comment.empty()) {
498 O << "\t"
499 << TAI->getCommentString()
500 << " "
501 << Comment;
502 }
503 O << "\n";
504}
505
506/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
507/// unsigned leb128 value.
508void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
509 if (TAI->hasLEB128()) {
510 O << "\t.uleb128\t"
511 << Value;
512 } else {
513 O << TAI->getData8bitsDirective();
514 PrintULEB128(Value);
515 }
516}
517
518/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
519/// signed leb128 value.
520void AsmPrinter::EmitSLEB128Bytes(int Value) const {
521 if (TAI->hasLEB128()) {
522 O << "\t.sleb128\t"
523 << Value;
524 } else {
525 O << TAI->getData8bitsDirective();
526 PrintSLEB128(Value);
527 }
528}
529
530/// EmitInt8 - Emit a byte directive and value.
531///
532void AsmPrinter::EmitInt8(int Value) const {
533 O << TAI->getData8bitsDirective();
534 PrintHex(Value & 0xFF);
535}
536
537/// EmitInt16 - Emit a short directive and value.
538///
539void AsmPrinter::EmitInt16(int Value) const {
540 O << TAI->getData16bitsDirective();
541 PrintHex(Value & 0xFFFF);
542}
543
544/// EmitInt32 - Emit a long directive and value.
545///
546void AsmPrinter::EmitInt32(int Value) const {
547 O << TAI->getData32bitsDirective();
548 PrintHex(Value);
549}
550
551/// EmitInt64 - Emit a long long directive and value.
552///
553void AsmPrinter::EmitInt64(uint64_t Value) const {
554 if (TAI->getData64bitsDirective()) {
555 O << TAI->getData64bitsDirective();
556 PrintHex(Value);
557 } else {
558 if (TM.getTargetData()->isBigEndian()) {
559 EmitInt32(unsigned(Value >> 32)); O << "\n";
560 EmitInt32(unsigned(Value));
561 } else {
562 EmitInt32(unsigned(Value)); O << "\n";
563 EmitInt32(unsigned(Value >> 32));
564 }
565 }
566}
567
568/// toOctal - Convert the low order bits of X into an octal digit.
569///
570static inline char toOctal(int X) {
571 return (X&7)+'0';
572}
573
574/// printStringChar - Print a char, escaped if necessary.
575///
576static void printStringChar(std::ostream &O, unsigned char C) {
577 if (C == '"') {
578 O << "\\\"";
579 } else if (C == '\\') {
580 O << "\\\\";
581 } else if (isprint(C)) {
582 O << C;
583 } else {
584 switch(C) {
585 case '\b': O << "\\b"; break;
586 case '\f': O << "\\f"; break;
587 case '\n': O << "\\n"; break;
588 case '\r': O << "\\r"; break;
589 case '\t': O << "\\t"; break;
590 default:
591 O << '\\';
592 O << toOctal(C >> 6);
593 O << toOctal(C >> 3);
594 O << toOctal(C >> 0);
595 break;
596 }
597 }
598}
599
600/// EmitString - Emit a string with quotes and a null terminator.
601/// Special characters are emitted properly.
602/// \literal (Eg. '\t') \endliteral
603void AsmPrinter::EmitString(const std::string &String) const {
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000604 const char* AscizDirective = TAI->getAscizDirective();
605 if (AscizDirective)
606 O << AscizDirective;
607 else
608 O << TAI->getAsciiDirective();
609 O << "\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000610 for (unsigned i = 0, N = String.size(); i < N; ++i) {
611 unsigned char C = String[i];
612 printStringChar(O, C);
613 }
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000614 if (AscizDirective)
615 O << "\"";
616 else
617 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000618}
619
620
Dan Gohman189f80d2007-09-24 20:58:13 +0000621/// EmitFile - Emit a .file directive.
622void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
623 O << "\t.file\t" << Number << " \"";
624 for (unsigned i = 0, N = Name.size(); i < N; ++i) {
625 unsigned char C = Name[i];
626 printStringChar(O, C);
627 }
628 O << "\"";
629}
630
631
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000632//===----------------------------------------------------------------------===//
633
Chris Lattner3a420532007-05-31 18:57:45 +0000634// EmitAlignment - Emit an alignment directive to the specified power of
635// two boundary. For example, if you pass in 3 here, you will get an 8
636// byte alignment. If a global value is specified, and if that global has
637// an explicit alignment requested, it will unconditionally override the
638// alignment request. However, if ForcedAlignBits is specified, this value
639// has final say: the ultimate alignment will be the max of ForcedAlignBits
640// and the alignment computed with NumBits and the global.
641//
642// The algorithm is:
643// Align = NumBits;
644// if (GV && GV->hasalignment) Align = GV->getalignment();
645// Align = std::max(Align, ForcedAlignBits);
646//
647void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng73a259a2007-07-25 23:35:07 +0000648 unsigned ForcedAlignBits, bool UseFillExpr,
649 unsigned FillValue) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000650 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000651 NumBits = Log2_32(GV->getAlignment());
652 NumBits = std::max(NumBits, ForcedAlignBits);
653
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000654 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskey563321a2006-09-06 18:34:40 +0000655 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng73a259a2007-07-25 23:35:07 +0000656 O << TAI->getAlignDirective() << NumBits;
657 if (UseFillExpr) O << ",0x" << std::hex << FillValue << std::dec;
658 O << "\n";
Chris Lattnerbfddc202004-08-17 19:14:29 +0000659}
660
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000661
Chris Lattner25045bd2005-11-21 07:51:36 +0000662/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000663///
Chris Lattner25045bd2005-11-21 07:51:36 +0000664void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000665 if (NumZeros) {
Jim Laskey563321a2006-09-06 18:34:40 +0000666 if (TAI->getZeroDirective()) {
667 O << TAI->getZeroDirective() << NumZeros;
668 if (TAI->getZeroDirectiveSuffix())
669 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000670 O << "\n";
671 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000672 for (; NumZeros; --NumZeros)
Jim Laskey563321a2006-09-06 18:34:40 +0000673 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000674 }
675 }
676}
677
Chris Lattnera80ba712004-08-16 23:15:22 +0000678// Print out the specified constant, without a storage class. Only the
679// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000680void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000681 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000682 O << "0";
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000683 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattnera875df32007-01-12 18:15:09 +0000684 O << CI->getZExtValue();
Reid Spencerb83eb642006-10-20 07:07:24 +0000685 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000686 // This is a constant address for a global variable or function. Use the
687 // name of the variable or function as the address value, possibly
688 // decorating it with GlobalVarAddrPrefix/Suffix or
689 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskey563321a2006-09-06 18:34:40 +0000690 if (isa<Function>(GV)) {
691 O << TAI->getFunctionAddrPrefix()
692 << Mang->getValueName(GV)
693 << TAI->getFunctionAddrSuffix();
694 } else {
695 O << TAI->getGlobalVarAddrPrefix()
696 << Mang->getValueName(GV)
697 << TAI->getGlobalVarAddrSuffix();
698 }
Duraid Madina855a5192005-04-02 12:21:51 +0000699 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000700 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000701 unsigned Opcode = CE->getOpcode();
702 switch (Opcode) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000703 case Instruction::GetElementPtr: {
704 // generate a symbolic expression for the byte address
705 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000706 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
707 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
708 idxVec.size())) {
Chris Lattner27e19212005-02-14 21:40:26 +0000709 if (Offset)
710 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000711 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000712 if (Offset > 0)
713 O << ") + " << Offset;
714 else if (Offset < 0)
715 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000716 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000717 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000718 }
719 break;
720 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000721 case Instruction::Trunc:
722 case Instruction::ZExt:
723 case Instruction::SExt:
724 case Instruction::FPTrunc:
725 case Instruction::FPExt:
726 case Instruction::UIToFP:
727 case Instruction::SIToFP:
728 case Instruction::FPToUI:
729 case Instruction::FPToSI:
730 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
731 break;
Chris Lattnercb0a6812006-12-12 05:14:13 +0000732 case Instruction::BitCast:
733 return EmitConstantValueOnly(CE->getOperand(0));
734
Chris Lattnerddc94012006-12-12 05:18:19 +0000735 case Instruction::IntToPtr: {
736 // Handle casts to pointers by changing them into casts to the appropriate
737 // integer type. This promotes constant folding and simplifies this code.
738 Constant *Op = CE->getOperand(0);
739 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
740 return EmitConstantValueOnly(Op);
741 }
742
743
744 case Instruction::PtrToInt: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000745 // Support only foldable casts to/from pointers that can be eliminated by
746 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000747 Constant *Op = CE->getOperand(0);
Chris Lattnerddc94012006-12-12 05:18:19 +0000748 const Type *Ty = CE->getType();
Chris Lattnera80ba712004-08-16 23:15:22 +0000749
Chris Lattnerddc94012006-12-12 05:18:19 +0000750 // We can emit the pointer value into this slot if the slot is an
751 // integer slot greater or equal to the size of the pointer.
Chris Lattner42a75512007-01-15 02:27:26 +0000752 if (Ty->isInteger() &&
Duncan Sandsca0ed742007-11-05 00:04:43 +0000753 TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
Chris Lattner4a6bd332006-07-29 01:57:19 +0000754 return EmitConstantValueOnly(Op);
Chris Lattner4a6bd332006-07-29 01:57:19 +0000755
756 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000757 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000758 break;
759 }
760 case Instruction::Add:
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000761 case Instruction::Sub:
Chris Lattnera80ba712004-08-16 23:15:22 +0000762 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000763 EmitConstantValueOnly(CE->getOperand(0));
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000764 O << (Opcode==Instruction::Add ? ") + (" : ") - (");
Chris Lattner25045bd2005-11-21 07:51:36 +0000765 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000766 O << ")";
767 break;
768 default:
769 assert(0 && "Unsupported operator!");
770 }
771 } else {
772 assert(0 && "Unknown constant value!");
773 }
774}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000775
Chris Lattner2980cef2005-11-10 18:06:33 +0000776/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000777/// the predicate isString is true.
778///
Chris Lattner2980cef2005-11-10 18:06:33 +0000779static void printAsCString(std::ostream &O, const ConstantArray *CVA,
780 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000781 assert(CVA->isString() && "Array is not string compatible!");
782
783 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000784 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000785 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000786 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000787 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000788 }
789 O << "\"";
790}
791
Jeff Cohenc884db42006-05-02 01:16:28 +0000792/// EmitString - Emit a zero-byte-terminated string constant.
793///
794void AsmPrinter::EmitString(const ConstantArray *CVA) const {
795 unsigned NumElts = CVA->getNumOperands();
Jim Laskey563321a2006-09-06 18:34:40 +0000796 if (TAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000797 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskey563321a2006-09-06 18:34:40 +0000798 O << TAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000799 printAsCString(O, CVA, NumElts-1);
800 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000801 O << TAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000802 printAsCString(O, CVA, NumElts);
803 }
804 O << "\n";
805}
806
Chris Lattner25045bd2005-11-21 07:51:36 +0000807/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Duncan Sandsca0ed742007-11-05 00:04:43 +0000808/// If Packed is false, pad to the ABI size.
809void AsmPrinter::EmitGlobalConstant(const Constant *CV, bool Packed) {
Owen Andersona69571c2006-05-03 01:29:57 +0000810 const TargetData *TD = TM.getTargetData();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000811 unsigned Size = Packed ?
812 TD->getTypeStoreSize(CV->getType()) : TD->getABITypeSize(CV->getType());
Chris Lattner1b7e2352004-08-17 06:36:49 +0000813
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000814 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Duncan Sandsca0ed742007-11-05 00:04:43 +0000815 EmitZeros(Size);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000816 return;
817 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
818 if (CVA->isString()) {
Jeff Cohenc884db42006-05-02 01:16:28 +0000819 EmitString(CVA);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000820 } else { // Not a string. Print the values in successive locations
Duncan Sandsca0ed742007-11-05 00:04:43 +0000821 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
822 EmitGlobalConstant(CVA->getOperand(i), false);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000823 }
824 return;
825 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
826 // Print the fields in successive locations. Pad to align if needed!
Owen Andersona69571c2006-05-03 01:29:57 +0000827 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000828 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000829 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
830 const Constant* field = CVS->getOperand(i);
831
832 // Check if padding is needed and insert one or more 0s.
Duncan Sandsca0ed742007-11-05 00:04:43 +0000833 uint64_t fieldSize = TD->getTypeStoreSize(field->getType());
Duncan Sands0c8a13b2007-11-05 18:03:02 +0000834 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
Chris Lattnerb1919e22007-02-10 19:55:17 +0000835 - cvsLayout->getElementOffset(i)) - fieldSize;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000836 sizeSoFar += fieldSize + padSize;
837
Duncan Sands0c8a13b2007-11-05 18:03:02 +0000838 // Now print the actual field value without ABI size padding.
839 EmitGlobalConstant(field, true);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000840
Duncan Sands0c8a13b2007-11-05 18:03:02 +0000841 // Insert padding - this may include padding to increase the size of the
842 // current field up to the ABI size (if the struct is not packed) as well
843 // as padding to ensure that the next field starts at the right offset.
Chris Lattner25045bd2005-11-21 07:51:36 +0000844 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000845 }
Chris Lattnerb0c39a32007-02-10 19:59:22 +0000846 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
Chris Lattner1b7e2352004-08-17 06:36:49 +0000847 "Layout of constant struct may be incorrect!");
848 return;
849 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
850 // FP Constants are printed as integer constants to avoid losing
851 // precision...
Chris Lattner1b7e2352004-08-17 06:36:49 +0000852 if (CFP->getType() == Type::DoubleTy) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000853 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000854 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
Jim Laskey563321a2006-09-06 18:34:40 +0000855 if (TAI->getData64bitsDirective())
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000856 O << TAI->getData64bitsDirective() << i << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +0000857 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000858 else if (TD->isBigEndian()) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000859 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Jim Laskey563321a2006-09-06 18:34:40 +0000860 << "\t" << TAI->getCommentString()
861 << " double most significant word " << Val << "\n";
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000862 O << TAI->getData32bitsDirective() << unsigned(i)
Jim Laskey563321a2006-09-06 18:34:40 +0000863 << "\t" << TAI->getCommentString()
864 << " double least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000865 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000866 O << TAI->getData32bitsDirective() << unsigned(i)
Jim Laskey563321a2006-09-06 18:34:40 +0000867 << "\t" << TAI->getCommentString()
868 << " double least significant word " << Val << "\n";
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000869 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Jim Laskey563321a2006-09-06 18:34:40 +0000870 << "\t" << TAI->getCommentString()
871 << " double most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000872 }
873 return;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000874 } else if (CFP->getType() == Type::FloatTy) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000875 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
876 O << TAI->getData32bitsDirective()
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000877 << CFP->getValueAPF().convertToAPInt().getZExtValue()
Jim Laskey563321a2006-09-06 18:34:40 +0000878 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000879 return;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000880 } else if (CFP->getType() == Type::X86_FP80Ty) {
881 // all long double variants are printed as hex
Dale Johannesen693717f2007-09-26 23:20:33 +0000882 // api needed to prevent premature destruction
883 APInt api = CFP->getValueAPF().convertToAPInt();
884 const uint64_t *p = api.getRawData();
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000885 if (TD->isBigEndian()) {
886 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
887 << "\t" << TAI->getCommentString()
888 << " long double most significant halfword\n";
889 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
890 << "\t" << TAI->getCommentString()
891 << " long double next halfword\n";
892 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
893 << "\t" << TAI->getCommentString()
894 << " long double next halfword\n";
895 O << TAI->getData16bitsDirective() << uint16_t(p[0])
896 << "\t" << TAI->getCommentString()
897 << " long double next halfword\n";
898 O << TAI->getData16bitsDirective() << uint16_t(p[1])
899 << "\t" << TAI->getCommentString()
900 << " long double least significant halfword\n";
901 } else {
902 O << TAI->getData16bitsDirective() << uint16_t(p[1])
903 << "\t" << TAI->getCommentString()
904 << " long double least significant halfword\n";
905 O << TAI->getData16bitsDirective() << uint16_t(p[0])
906 << "\t" << TAI->getCommentString()
907 << " long double next halfword\n";
908 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
909 << "\t" << TAI->getCommentString()
910 << " long double next halfword\n";
911 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
912 << "\t" << TAI->getCommentString()
913 << " long double next halfword\n";
914 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
915 << "\t" << TAI->getCommentString()
916 << " long double most significant halfword\n";
917 }
Duncan Sandsca0ed742007-11-05 00:04:43 +0000918 EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty));
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000919 return;
Dale Johannesenfcf4d242007-10-11 23:32:15 +0000920 } else if (CFP->getType() == Type::PPC_FP128Ty) {
921 // all long double variants are printed as hex
922 // api needed to prevent premature destruction
923 APInt api = CFP->getValueAPF().convertToAPInt();
924 const uint64_t *p = api.getRawData();
925 if (TD->isBigEndian()) {
926 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
927 << "\t" << TAI->getCommentString()
928 << " long double most significant word\n";
929 O << TAI->getData32bitsDirective() << uint32_t(p[0])
930 << "\t" << TAI->getCommentString()
931 << " long double next word\n";
932 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
933 << "\t" << TAI->getCommentString()
934 << " long double next word\n";
935 O << TAI->getData32bitsDirective() << uint32_t(p[1])
936 << "\t" << TAI->getCommentString()
937 << " long double least significant word\n";
938 } else {
939 O << TAI->getData32bitsDirective() << uint32_t(p[1])
940 << "\t" << TAI->getCommentString()
941 << " long double least significant word\n";
942 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
943 << "\t" << TAI->getCommentString()
944 << " long double next word\n";
945 O << TAI->getData32bitsDirective() << uint32_t(p[0])
946 << "\t" << TAI->getCommentString()
947 << " long double next word\n";
948 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
949 << "\t" << TAI->getCommentString()
950 << " long double most significant word\n";
951 }
952 return;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000953 } else assert(0 && "Floating point constant type not handled");
Reid Spencer47857812006-12-31 05:55:36 +0000954 } else if (CV->getType() == Type::Int64Ty) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000955 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000956 uint64_t Val = CI->getZExtValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000957
Jim Laskey563321a2006-09-06 18:34:40 +0000958 if (TAI->getData64bitsDirective())
959 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000960 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000961 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
962 << "\t" << TAI->getCommentString()
963 << " Double-word most significant word " << Val << "\n";
964 O << TAI->getData32bitsDirective() << unsigned(Val)
965 << "\t" << TAI->getCommentString()
966 << " Double-word least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000967 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000968 O << TAI->getData32bitsDirective() << unsigned(Val)
969 << "\t" << TAI->getCommentString()
970 << " Double-word least significant word " << Val << "\n";
971 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
972 << "\t" << TAI->getCommentString()
973 << " Double-word most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000974 }
975 return;
976 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000977 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
978 const VectorType *PTy = CP->getType();
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000979
980 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
Duncan Sandsca0ed742007-11-05 00:04:43 +0000981 EmitGlobalConstant(CP->getOperand(I), false);
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000982
983 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000984 }
985
986 const Type *type = CV->getType();
Evan Chengd6594ae2006-09-12 21:00:35 +0000987 printDataDirective(type);
Chris Lattner25045bd2005-11-21 07:51:36 +0000988 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000989 O << "\n";
990}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000991
Evan Chengd6594ae2006-09-12 21:00:35 +0000992void
993AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
994 // Target doesn't support this yet!
995 abort();
996}
997
Chris Lattner3ce9b672006-09-26 23:59:50 +0000998/// PrintSpecial - Print information related to the specified machine instr
999/// that is independent of the operand, and may be independent of the instr
1000/// itself. This can be useful for portably encoding the comment character
1001/// or other bits of target-specific knowledge into the asmstrings. The
1002/// syntax used is ${:comment}. Targets can override this to add support
1003/// for their own strange codes.
1004void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001005 if (!strcmp(Code, "private")) {
1006 O << TAI->getPrivateGlobalPrefix();
1007 } else if (!strcmp(Code, "comment")) {
Chris Lattner3ce9b672006-09-26 23:59:50 +00001008 O << TAI->getCommentString();
1009 } else if (!strcmp(Code, "uid")) {
1010 // Assign a unique ID to this machine instruction.
1011 static const MachineInstr *LastMI = 0;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001012 static const Function *F = 0;
Chris Lattner3ce9b672006-09-26 23:59:50 +00001013 static unsigned Counter = 0U-1;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001014
1015 // Comparing the address of MI isn't sufficient, because machineinstrs may
1016 // be allocated to the same address across functions.
1017 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1018
Chris Lattner3ce9b672006-09-26 23:59:50 +00001019 // If this is a new machine instruction, bump the counter.
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001020 if (LastMI != MI || F != ThisF) {
1021 ++Counter;
1022 LastMI = MI;
Chris Lattner4b092522007-02-06 01:56:31 +00001023 F = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001024 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001025 O << Counter;
1026 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +00001027 cerr << "Unknown special formatter '" << Code
1028 << "' for machine instr: " << *MI;
Chris Lattner3ce9b672006-09-26 23:59:50 +00001029 exit(1);
1030 }
1031}
1032
1033
Chris Lattner0264d1a2006-01-27 02:10:10 +00001034/// printInlineAsm - This method formats and prints the specified machine
1035/// instruction that is an inline asm.
1036void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001037 unsigned NumOperands = MI->getNumOperands();
1038
1039 // Count the number of register definitions.
1040 unsigned NumDefs = 0;
Dan Gohman92dfe202007-09-14 20:33:02 +00001041 for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001042 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001043 assert(NumDefs != NumOperands-1 && "No asm string?");
1044
1045 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001046
1047 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001048 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001049
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001050 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
1051 if (AsmStr[0] == 0) {
1052 O << "\n"; // Tab already printed, avoid double indenting next instr.
1053 return;
1054 }
1055
Jim Laskey563321a2006-09-06 18:34:40 +00001056 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001057
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001058 // The variant of the current asmprinter.
1059 int AsmPrinterVariant = TAI->getAssemblerDialect();
1060
Chris Lattner66099132006-02-01 22:41:11 +00001061 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1062 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001063
Chris Lattner66099132006-02-01 22:41:11 +00001064 while (*LastEmitted) {
1065 switch (*LastEmitted) {
1066 default: {
1067 // Not a special case, emit the string section literally.
1068 const char *LiteralEnd = LastEmitted+1;
1069 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001070 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001071 ++LiteralEnd;
1072 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1073 O.write(LastEmitted, LiteralEnd-LastEmitted);
1074 LastEmitted = LiteralEnd;
1075 break;
1076 }
Chris Lattner1c059972006-05-05 21:47:05 +00001077 case '\n':
1078 ++LastEmitted; // Consume newline character.
Chris Lattner1f6f4c72007-04-30 17:00:18 +00001079 O << "\n"; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001080 break;
Chris Lattner66099132006-02-01 22:41:11 +00001081 case '$': {
1082 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001083 bool Done = true;
1084
1085 // Handle escapes.
1086 switch (*LastEmitted) {
1087 default: Done = false; break;
1088 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001089 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1090 O << '$';
1091 ++LastEmitted; // Consume second '$' character.
1092 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001093 case '(': // $( -> same as GCC's { character.
1094 ++LastEmitted; // Consume '(' character.
1095 if (CurVariant != -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001096 cerr << "Nested variants found in inline asm string: '"
1097 << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001098 exit(1);
1099 }
1100 CurVariant = 0; // We're in the first variant now.
1101 break;
1102 case '|':
1103 ++LastEmitted; // consume '|' character.
1104 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001105 cerr << "Found '|' character outside of variant in inline asm "
1106 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001107 exit(1);
1108 }
1109 ++CurVariant; // We're in the next variant.
1110 break;
1111 case ')': // $) -> same as GCC's } char.
1112 ++LastEmitted; // consume ')' character.
1113 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001114 cerr << "Found '}' character outside of variant in inline asm "
1115 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001116 exit(1);
1117 }
1118 CurVariant = -1;
1119 break;
Chris Lattner66099132006-02-01 22:41:11 +00001120 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001121 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001122
1123 bool HasCurlyBraces = false;
1124 if (*LastEmitted == '{') { // ${variable}
1125 ++LastEmitted; // Consume '{' character.
1126 HasCurlyBraces = true;
1127 }
1128
1129 const char *IDStart = LastEmitted;
1130 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001131 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001132 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1133 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001134 cerr << "Bad $ operand number in inline asm string: '"
1135 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001136 exit(1);
1137 }
1138 LastEmitted = IDEnd;
1139
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001140 char Modifier[2] = { 0, 0 };
1141
Chris Lattner66099132006-02-01 22:41:11 +00001142 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001143 // If we have curly braces, check for a modifier character. This
1144 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1145 if (*LastEmitted == ':') {
1146 ++LastEmitted; // Consume ':' character.
1147 if (*LastEmitted == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001148 cerr << "Bad ${:} expression in inline asm string: '"
1149 << AsmStr << "'\n";
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001150 exit(1);
1151 }
1152
1153 Modifier[0] = *LastEmitted;
1154 ++LastEmitted; // Consume modifier character.
1155 }
1156
Chris Lattner66099132006-02-01 22:41:11 +00001157 if (*LastEmitted != '}') {
Bill Wendlinge8156192006-12-07 01:30:32 +00001158 cerr << "Bad ${} expression in inline asm string: '"
1159 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001160 exit(1);
1161 }
1162 ++LastEmitted; // Consume '}' character.
1163 }
1164
1165 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001166 cerr << "Invalid $ operand number in inline asm string: '"
1167 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001168 exit(1);
1169 }
1170
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001171 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001172 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001173 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1174 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001175
1176 bool Error = false;
1177
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001178 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001179 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001180 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001181 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1182 OpNo += (OpFlags >> 3) + 1;
1183 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001184
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001185 if (OpNo >= MI->getNumOperands()) {
1186 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001187 } else {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001188 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1189 ++OpNo; // Skip over the ID number.
1190
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001191 if (Modifier[0]=='l') // labels are target independent
1192 printBasicBlockLabel(MI->getOperand(OpNo).getMachineBasicBlock(),
1193 false, false);
1194 else {
1195 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1196 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1197 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1198 Modifier[0] ? Modifier : 0);
1199 } else {
1200 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1201 Modifier[0] ? Modifier : 0);
1202 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001203 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001204 }
1205 if (Error) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001206 cerr << "Invalid operand found in inline asm: '"
1207 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001208 MI->dump();
1209 exit(1);
1210 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001211 }
Chris Lattner66099132006-02-01 22:41:11 +00001212 break;
1213 }
Chris Lattner66099132006-02-01 22:41:11 +00001214 }
1215 }
Jim Laskey563321a2006-09-06 18:34:40 +00001216 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattner66099132006-02-01 22:41:11 +00001217}
1218
Jim Laskey1ee29252007-01-26 14:34:52 +00001219/// printLabel - This method prints a local label used by debug and
1220/// exception handling tables.
1221void AsmPrinter::printLabel(const MachineInstr *MI) const {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001222 O << "\n"
1223 << TAI->getPrivateGlobalPrefix()
Jim Laskeybacd3042007-02-21 22:48:45 +00001224 << "label"
Jim Laskey1ee29252007-01-26 14:34:52 +00001225 << MI->getOperand(0).getImmedValue()
1226 << ":\n";
1227}
1228
Chris Lattner66099132006-02-01 22:41:11 +00001229/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1230/// instruction, using the specified assembler variant. Targets should
1231/// overried this to format as appropriate.
1232bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001233 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001234 // Target doesn't support this yet!
1235 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001236}
Chris Lattnerdd260332006-02-24 20:21:58 +00001237
1238bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1239 unsigned AsmVariant,
1240 const char *ExtraCode) {
1241 // Target doesn't support this yet!
1242 return true;
1243}
Nate Begeman37efe672006-04-22 18:53:45 +00001244
1245/// printBasicBlockLabel - This method prints the label for the specified
1246/// MachineBasicBlock
Nate Begemancdf38c42006-05-02 05:37:32 +00001247void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
1248 bool printColon,
1249 bool printComment) const {
Evan Cheng347d39f2007-10-14 05:57:21 +00001250 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << "_"
1251 << MBB->getNumber();
Nate Begemancdf38c42006-05-02 05:37:32 +00001252 if (printColon)
1253 O << ':';
Chris Lattner9c78ecb2006-10-05 21:40:14 +00001254 if (printComment && MBB->getBasicBlock())
Dan Gohman286d5692007-07-30 15:06:25 +00001255 O << '\t' << TAI->getCommentString() << ' '
1256 << MBB->getBasicBlock()->getName();
Nate Begeman37efe672006-04-22 18:53:45 +00001257}
Nate Begeman52a51e382006-08-12 21:29:52 +00001258
1259/// printSetLabel - This method prints a set label for the specified
1260/// MachineBasicBlock
1261void AsmPrinter::printSetLabel(unsigned uid,
1262 const MachineBasicBlock *MBB) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001263 if (!TAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001264 return;
1265
Jim Laskey563321a2006-09-06 18:34:40 +00001266 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001267 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman52a51e382006-08-12 21:29:52 +00001268 printBasicBlockLabel(MBB, false, false);
Evan Cheng347d39f2007-10-14 05:57:21 +00001269 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1270 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001271}
Evan Chengd6594ae2006-09-12 21:00:35 +00001272
Evan Cheng41349c12006-11-01 09:23:08 +00001273void AsmPrinter::printSetLabel(unsigned uid, unsigned uid2,
1274 const MachineBasicBlock *MBB) const {
1275 if (!TAI->getSetDirective())
1276 return;
1277
1278 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001279 << getFunctionNumber() << '_' << uid << '_' << uid2
1280 << "_set_" << MBB->getNumber() << ',';
Evan Cheng41349c12006-11-01 09:23:08 +00001281 printBasicBlockLabel(MBB, false, false);
Evan Cheng347d39f2007-10-14 05:57:21 +00001282 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1283 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001284}
1285
Evan Chengd6594ae2006-09-12 21:00:35 +00001286/// printDataDirective - This method prints the asm directive for the
1287/// specified type.
1288void AsmPrinter::printDataDirective(const Type *type) {
1289 const TargetData *TD = TM.getTargetData();
1290 switch (type->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001291 case Type::IntegerTyID: {
1292 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1293 if (BitWidth <= 8)
1294 O << TAI->getData8bitsDirective();
1295 else if (BitWidth <= 16)
1296 O << TAI->getData16bitsDirective();
1297 else if (BitWidth <= 32)
1298 O << TAI->getData32bitsDirective();
1299 else if (BitWidth <= 64) {
1300 assert(TAI->getData64bitsDirective() &&
1301 "Target cannot handle 64-bit constant exprs!");
1302 O << TAI->getData64bitsDirective();
1303 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001304 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001305 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001306 case Type::PointerTyID:
1307 if (TD->getPointerSize() == 8) {
1308 assert(TAI->getData64bitsDirective() &&
1309 "Target cannot handle 64-bit pointer exprs!");
1310 O << TAI->getData64bitsDirective();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001311 } else {
1312 O << TAI->getData32bitsDirective();
Evan Chengd6594ae2006-09-12 21:00:35 +00001313 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001314 break;
1315 case Type::FloatTyID: case Type::DoubleTyID:
Dale Johannesen4292d1c2007-09-28 18:06:58 +00001316 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
Evan Chengd6594ae2006-09-12 21:00:35 +00001317 assert (0 && "Should have already output floating point constant.");
1318 default:
1319 assert (0 && "Can't handle printing this type of thing");
1320 break;
1321 }
1322}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001323