blob: 65892c10a58a505b8ef99efc5a2c06500447db04 [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera80ba712004-08-16 23:15:22 +00007//
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"
Evan Chengcc415862007-11-09 01:32:10 +000029#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner66099132006-02-01 22:41:11 +000030#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000031using namespace llvm;
32
Jim Laskeyf1cdea12007-01-25 15:12:02 +000033static cl::opt<bool>
34AsmVerbose("asm-verbose", cl::Hidden, cl::desc("Add comments to directives."));
35
Devang Patel19974732007-05-03 01:11:54 +000036char AsmPrinter::ID = 0;
Jim Laskeya0f3d172006-09-07 22:06:40 +000037AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
38 const TargetAsmInfo *T)
Evan Cheng347d39f2007-10-14 05:57:21 +000039 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o), TM(tm), TAI(T)
Jim Laskey563321a2006-09-06 18:34:40 +000040{}
Chris Lattnered138932005-12-13 06:32:10 +000041
Chris Lattner52f06702006-10-05 02:42:47 +000042std::string AsmPrinter::getSectionForFunction(const Function &F) const {
43 return TAI->getTextSection();
44}
45
Chris Lattnered138932005-12-13 06:32:10 +000046
Chris Lattner4632d7a2006-05-09 04:59:56 +000047/// SwitchToTextSection - Switch to the specified text section of the executable
48/// if we are not already in it!
Chris Lattnerac28fbd2005-11-21 07:06:27 +000049///
Chris Lattner4632d7a2006-05-09 04:59:56 +000050void AsmPrinter::SwitchToTextSection(const char *NewSection,
51 const GlobalValue *GV) {
Chris Lattnerac28fbd2005-11-21 07:06:27 +000052 std::string NS;
Chris Lattnera7090ae2006-05-09 05:24:50 +000053 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000054 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnera7090ae2006-05-09 05:24:50 +000055 else
56 NS = NewSection;
57
58 // If we're already in this section, we're done.
59 if (CurrentSection == NS) return;
Jeff Cohen51b776d2006-05-02 03:58:45 +000060
Chris Lattner7faec9b2006-05-09 05:33:48 +000061 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000062 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
63 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen51b776d2006-05-02 03:58:45 +000064
Chris Lattner7faec9b2006-05-09 05:33:48 +000065 CurrentSection = NS;
66
67 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000068 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattnerac28fbd2005-11-21 07:06:27 +000069}
70
Nate Begeman2f1ae882006-07-27 01:13:04 +000071/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner4632d7a2006-05-09 04:59:56 +000072/// if we are not already in it!
73///
74void AsmPrinter::SwitchToDataSection(const char *NewSection,
75 const GlobalValue *GV) {
76 std::string NS;
Chris Lattnerb81cb612006-05-09 05:23:12 +000077 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000078 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnerb81cb612006-05-09 05:23:12 +000079 else
80 NS = NewSection;
Chris Lattner4632d7a2006-05-09 04:59:56 +000081
Chris Lattnerb81cb612006-05-09 05:23:12 +000082 // If we're already in this section, we're done.
83 if (CurrentSection == NS) return;
84
Chris Lattner7faec9b2006-05-09 05:33:48 +000085 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000086 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
87 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner7faec9b2006-05-09 05:33:48 +000088
89 CurrentSection = NS;
Chris Lattner4632d7a2006-05-09 04:59:56 +000090
Chris Lattner7faec9b2006-05-09 05:33:48 +000091 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000092 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner4632d7a2006-05-09 04:59:56 +000093}
94
95
Chris Lattnera80ba712004-08-16 23:15:22 +000096bool AsmPrinter::doInitialization(Module &M) {
Jim Laskey563321a2006-09-06 18:34:40 +000097 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattner2c1b1592006-01-23 23:47:53 +000098
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000099 if (!M.getModuleInlineAsm().empty())
Jim Laskey563321a2006-09-06 18:34:40 +0000100 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000101 << M.getModuleInlineAsm()
Jim Laskey563321a2006-09-06 18:34:40 +0000102 << "\n" << TAI->getCommentString()
103 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000104
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000105 SwitchToDataSection(""); // Reset back to no section.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000106
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000107 if (MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>()) {
108 MMI->AnalyzeModule(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000109 }
110
Chris Lattnera80ba712004-08-16 23:15:22 +0000111 return false;
112}
113
114bool AsmPrinter::doFinalization(Module &M) {
Rafael Espindola15404d02006-12-18 03:37:18 +0000115 if (TAI->getWeakRefDirective()) {
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000116 if (!ExtWeakSymbols.empty())
Rafael Espindola15404d02006-12-18 03:37:18 +0000117 SwitchToDataSection("");
118
119 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
120 e = ExtWeakSymbols.end(); i != e; ++i) {
121 const GlobalValue *GV = *i;
122 std::string Name = Mang->getValueName(GV);
123 O << TAI->getWeakRefDirective() << Name << "\n";
124 }
125 }
126
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000127 if (TAI->getSetDirective()) {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000128 if (!M.alias_empty())
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000129 SwitchToTextSection(TAI->getTextSection());
130
131 O << "\n";
132 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
133 I!=E; ++I) {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000134 std::string Name = Mang->getValueName(I);
135 std::string Target;
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000136
137 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
138 Target = Mang->getValueName(GV);
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000139
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000140 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000141 O << "\t.globl\t" << Name << "\n";
142 else if (I->hasWeakLinkage())
143 O << TAI->getWeakRefDirective() << Name << "\n";
144 else if (!I->hasInternalLinkage())
145 assert(0 && "Invalid alias linkage");
146
147 O << TAI->getSetDirective() << Name << ", " << Target << "\n";
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000148
149 // If the aliasee has external weak linkage it can be referenced only by
150 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
151 // weak reference in such case.
152 if (GV->hasExternalWeakLinkage())
153 if (TAI->getWeakRefDirective())
154 O << TAI->getWeakRefDirective() << Target << "\n";
155 else
156 O << "\t.globl\t" << Target << "\n";
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000157 }
158 }
159
Chris Lattnera80ba712004-08-16 23:15:22 +0000160 delete Mang; Mang = 0;
161 return false;
162}
163
Bill Wendlingce613282007-09-18 09:10:16 +0000164std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
Bill Wendling6e198962007-09-18 01:47:22 +0000165 assert(MF && "No machine function?");
Bill Wendling5f19cf52007-09-18 05:03:44 +0000166 return Mang->makeNameProper(MF->getFunction()->getName() + ".eh",
167 TAI->getGlobalPrefix());
Bill Wendling6e198962007-09-18 01:47:22 +0000168}
169
Chris Lattner25045bd2005-11-21 07:51:36 +0000170void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000171 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +0000172 CurrentFnName = Mang->getValueName(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000173 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +0000174}
175
Chris Lattner3b4fd322005-11-21 08:25:09 +0000176/// EmitConstantPool - Print to the current output stream assembly
177/// representations of the constants in the constant pool MCP. This is
178/// used to print out constants which have been "spilled to memory" by
179/// the code generator.
180///
181void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000182 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000183 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000184
185 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
186 // in special sections.
187 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
188 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
189 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
190 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Chengd6594ae2006-09-12 21:00:35 +0000191 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000192 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng2d2cec12006-06-29 00:26:09 +0000193 MachineConstantPoolEntry CPE = CP[i];
Evan Chengd5a99d72006-09-14 07:35:00 +0000194 const Type *Ty = CPE.getType();
Jim Laskey563321a2006-09-06 18:34:40 +0000195 if (TAI->getFourByteConstantSection() &&
Duncan Sandsca0ed742007-11-05 00:04:43 +0000196 TM.getTargetData()->getABITypeSize(Ty) == 4)
Evan Cheng2d2cec12006-06-29 00:26:09 +0000197 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000198 else if (TAI->getEightByteConstantSection() &&
Duncan Sandsca0ed742007-11-05 00:04:43 +0000199 TM.getTargetData()->getABITypeSize(Ty) == 8)
Evan Cheng2d2cec12006-06-29 00:26:09 +0000200 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000201 else if (TAI->getSixteenByteConstantSection() &&
Duncan Sandsca0ed742007-11-05 00:04:43 +0000202 TM.getTargetData()->getABITypeSize(Ty) == 16)
Evan Cheng2d2cec12006-06-29 00:26:09 +0000203 SixteenByteCPs.push_back(std::make_pair(CPE, i));
204 else
205 OtherCPs.push_back(std::make_pair(CPE, i));
206 }
207
208 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskey563321a2006-09-06 18:34:40 +0000209 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
210 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
211 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
212 SixteenByteCPs);
213 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000214}
215
216void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
217 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
218 if (CP.empty()) return;
219
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000220 SwitchToDataSection(Section);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000221 EmitAlignment(Alignment);
222 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000223 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
224 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Chengd5a99d72006-09-14 07:35:00 +0000225 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
226 if (CP[i].first.isMachineConstantPoolEntry())
Evan Chengd6594ae2006-09-12 21:00:35 +0000227 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Chengd5a99d72006-09-14 07:35:00 +0000228 else
Evan Chengd6594ae2006-09-12 21:00:35 +0000229 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000230 if (i != e-1) {
Evan Chengd5a99d72006-09-14 07:35:00 +0000231 const Type *Ty = CP[i].first.getType();
Evan Cheng2d2cec12006-06-29 00:26:09 +0000232 unsigned EntSize =
Duncan Sandsca0ed742007-11-05 00:04:43 +0000233 TM.getTargetData()->getABITypeSize(Ty);
Evan Chengd5a99d72006-09-14 07:35:00 +0000234 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattner3029f922006-02-09 04:46:04 +0000235 // Emit inter-object padding for alignment.
Evan Chengd5a99d72006-09-14 07:35:00 +0000236 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattner3029f922006-02-09 04:46:04 +0000237 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000238 }
239}
240
Nate Begeman37efe672006-04-22 18:53:45 +0000241/// EmitJumpTableInfo - Print assembly representations of the jump tables used
242/// by the current function to the current output stream.
243///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000244void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
245 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000246 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
247 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000248
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000249 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000250
Nate Begeman2f1ae882006-07-27 01:13:04 +0000251 // Pick the directive to use to print the jump table entries, and switch to
252 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000253 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000254
255 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
256 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
257 !JumpTableDataSection) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000258 // In PIC mode, we need to emit the jump table to the same section as the
259 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000260 // We should also do if the section name is NULL.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000261 const Function *F = MF.getFunction();
262 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000263 } else {
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000264 SwitchToDataSection(JumpTableDataSection);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000265 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000266
267 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000268
Nate Begeman37efe672006-04-22 18:53:45 +0000269 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000270 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000271
272 // If this jump table was deleted, ignore it.
273 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000274
275 // For PIC codegen, if possible we want to use the SetDirective to reduce
276 // the number of relocations the assembler will generate for the jump table.
277 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000278 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000279 if (TAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000280 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000281 if (EmittedSets.insert(JTBBs[ii]))
282 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000283
Chris Lattner393a8ee2007-01-18 01:12:56 +0000284 // On some targets (e.g. darwin) we want to emit two consequtive labels
285 // before each jump table. The first label is never referenced, but tells
286 // the assembler and linker the extents of the jump table object. The
287 // second label is actually referenced by the code.
288 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
Evan Cheng347d39f2007-10-14 05:57:21 +0000289 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Chris Lattner393a8ee2007-01-18 01:12:56 +0000290
Evan Cheng347d39f2007-10-14 05:57:21 +0000291 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
292 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000293
Nate Begeman37efe672006-04-22 18:53:45 +0000294 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000295 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000296 O << '\n';
297 }
298 }
299}
300
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000301void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
302 const MachineBasicBlock *MBB,
303 unsigned uid) const {
304 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
305
306 // Use JumpTableDirective otherwise honor the entry size from the jump table
307 // info.
308 const char *JTEntryDirective = TAI->getJumpTableDirective();
309 bool HadJTEntryDirective = JTEntryDirective != NULL;
310 if (!HadJTEntryDirective) {
311 JTEntryDirective = MJTI->getEntrySize() == 4 ?
312 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
313 }
314
315 O << JTEntryDirective << ' ';
316
317 // If we have emitted set directives for the jump table entries, print
318 // them rather than the entries themselves. If we're emitting PIC, then
319 // emit the table entries as differences between two text section labels.
320 // If we're emitting non-PIC code, then emit the entries as direct
321 // references to the target basic blocks.
322 if (IsPic) {
323 if (TAI->getSetDirective()) {
324 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
325 << '_' << uid << "_set_" << MBB->getNumber();
326 } else {
327 printBasicBlockLabel(MBB, false, false);
328 // If the arch uses custom Jump Table directives, don't calc relative to
329 // JT
330 if (!HadJTEntryDirective)
331 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
332 << getFunctionNumber() << '_' << uid;
333 }
334 } else {
335 printBasicBlockLabel(MBB, false, false);
336 }
337}
338
339
Chris Lattnered138932005-12-13 06:32:10 +0000340/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
341/// special global used by LLVM. If so, emit it and return true, otherwise
342/// do nothing and return false.
343bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000344 if (GV->getName() == "llvm.used") {
345 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
346 EmitLLVMUsedList(GV->getInitializer());
347 return true;
348 }
349
Jim Laskey78098112006-03-07 22:00:35 +0000350 // Ignore debug and non-emitted data.
351 if (GV->getSection() == "llvm.metadata") return true;
352
353 if (!GV->hasAppendingLinkage()) return false;
354
355 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000356
Evan Cheng916d07c2007-06-04 20:39:18 +0000357 const TargetData *TD = TM.getTargetData();
358 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner5166b822006-01-12 19:17:23 +0000359 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000360 SwitchToDataSection(TAI->getStaticCtorsSection());
Evan Cheng916d07c2007-06-04 20:39:18 +0000361 EmitAlignment(Align, 0);
Chris Lattnered138932005-12-13 06:32:10 +0000362 EmitXXStructorList(GV->getInitializer());
363 return true;
364 }
365
Chris Lattner5166b822006-01-12 19:17:23 +0000366 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000367 SwitchToDataSection(TAI->getStaticDtorsSection());
Evan Cheng916d07c2007-06-04 20:39:18 +0000368 EmitAlignment(Align, 0);
Chris Lattnered138932005-12-13 06:32:10 +0000369 EmitXXStructorList(GV->getInitializer());
370 return true;
371 }
372
373 return false;
374}
375
Chris Lattnercb05af82006-09-26 03:38:18 +0000376/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
377/// global in the specified llvm.used list as being used with this directive.
378void AsmPrinter::EmitLLVMUsedList(Constant *List) {
379 const char *Directive = TAI->getUsedDirective();
380
381 // Should be an array of 'sbyte*'.
382 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
383 if (InitList == 0) return;
384
385 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
386 O << Directive;
387 EmitConstantValueOnly(InitList->getOperand(i));
388 O << "\n";
389 }
390}
391
Chris Lattnered138932005-12-13 06:32:10 +0000392/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
393/// function pointers, ignoring the init priority.
394void AsmPrinter::EmitXXStructorList(Constant *List) {
395 // Should be an array of '{ int, void ()* }' structs. The first value is the
396 // init priority, which we ignore.
397 if (!isa<ConstantArray>(List)) return;
398 ConstantArray *InitList = cast<ConstantArray>(List);
399 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
400 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
401 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000402
403 if (CS->getOperand(1)->isNullValue())
404 return; // Found a null terminator, exit printing.
405 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000406 EmitGlobalConstant(CS->getOperand(1));
407 }
408}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000409
Jim Laskeya1a19f82006-10-17 13:41:07 +0000410/// getGlobalLinkName - Returns the asm/link name of of the specified
411/// global variable. Should be overridden by each target asm printer to
412/// generate the appropriate value.
Jim Laskey99e41ee2006-10-17 17:17:24 +0000413const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
414 std::string LinkName;
Jim Laskey03742482006-11-20 20:29:06 +0000415
416 if (isa<Function>(GV)) {
417 LinkName += TAI->getFunctionAddrPrefix();
418 LinkName += Mang->getValueName(GV);
419 LinkName += TAI->getFunctionAddrSuffix();
420 } else {
421 LinkName += TAI->getGlobalVarAddrPrefix();
422 LinkName += Mang->getValueName(GV);
423 LinkName += TAI->getGlobalVarAddrSuffix();
424 }
425
Jim Laskey99e41ee2006-10-17 17:17:24 +0000426 return LinkName;
Jim Laskeya1a19f82006-10-17 13:41:07 +0000427}
428
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000429/// EmitExternalGlobal - Emit the external reference to a global variable.
430/// Should be overridden if an indirect reference should be used.
431void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
432 O << getGlobalLinkName(GV);
433}
434
435
436
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000437//===----------------------------------------------------------------------===//
438/// LEB 128 number encoding.
439
440/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
441/// representing an unsigned leb128 value.
442void AsmPrinter::PrintULEB128(unsigned Value) const {
443 do {
444 unsigned Byte = Value & 0x7f;
445 Value >>= 7;
446 if (Value) Byte |= 0x80;
447 O << "0x" << std::hex << Byte << std::dec;
448 if (Value) O << ", ";
449 } while (Value);
450}
451
452/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
453/// value.
454unsigned AsmPrinter::SizeULEB128(unsigned Value) {
455 unsigned Size = 0;
456 do {
457 Value >>= 7;
458 Size += sizeof(int8_t);
459 } while (Value);
460 return Size;
461}
462
463/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
464/// representing a signed leb128 value.
465void AsmPrinter::PrintSLEB128(int Value) const {
466 int Sign = Value >> (8 * sizeof(Value) - 1);
467 bool IsMore;
468
469 do {
470 unsigned Byte = Value & 0x7f;
471 Value >>= 7;
472 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
473 if (IsMore) Byte |= 0x80;
474 O << "0x" << std::hex << Byte << std::dec;
475 if (IsMore) O << ", ";
476 } while (IsMore);
477}
478
479/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
480/// value.
481unsigned AsmPrinter::SizeSLEB128(int Value) {
482 unsigned Size = 0;
483 int Sign = Value >> (8 * sizeof(Value) - 1);
484 bool IsMore;
485
486 do {
487 unsigned Byte = Value & 0x7f;
488 Value >>= 7;
489 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
490 Size += sizeof(int8_t);
491 } while (IsMore);
492 return Size;
493}
494
495//===--------------------------------------------------------------------===//
496// Emission and print routines
497//
498
499/// PrintHex - Print a value as a hexidecimal value.
500///
501void AsmPrinter::PrintHex(int Value) const {
502 O << "0x" << std::hex << Value << std::dec;
503}
504
505/// EOL - Print a newline character to asm stream. If a comment is present
506/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000507void AsmPrinter::EOL() const {
508 O << "\n";
509}
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000510void AsmPrinter::EOL(const std::string &Comment) const {
511 if (AsmVerbose && !Comment.empty()) {
512 O << "\t"
513 << TAI->getCommentString()
514 << " "
515 << Comment;
516 }
517 O << "\n";
518}
519
520/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
521/// unsigned leb128 value.
522void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
523 if (TAI->hasLEB128()) {
524 O << "\t.uleb128\t"
525 << Value;
526 } else {
527 O << TAI->getData8bitsDirective();
528 PrintULEB128(Value);
529 }
530}
531
532/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
533/// signed leb128 value.
534void AsmPrinter::EmitSLEB128Bytes(int Value) const {
535 if (TAI->hasLEB128()) {
536 O << "\t.sleb128\t"
537 << Value;
538 } else {
539 O << TAI->getData8bitsDirective();
540 PrintSLEB128(Value);
541 }
542}
543
544/// EmitInt8 - Emit a byte directive and value.
545///
546void AsmPrinter::EmitInt8(int Value) const {
547 O << TAI->getData8bitsDirective();
548 PrintHex(Value & 0xFF);
549}
550
551/// EmitInt16 - Emit a short directive and value.
552///
553void AsmPrinter::EmitInt16(int Value) const {
554 O << TAI->getData16bitsDirective();
555 PrintHex(Value & 0xFFFF);
556}
557
558/// EmitInt32 - Emit a long directive and value.
559///
560void AsmPrinter::EmitInt32(int Value) const {
561 O << TAI->getData32bitsDirective();
562 PrintHex(Value);
563}
564
565/// EmitInt64 - Emit a long long directive and value.
566///
567void AsmPrinter::EmitInt64(uint64_t Value) const {
568 if (TAI->getData64bitsDirective()) {
569 O << TAI->getData64bitsDirective();
570 PrintHex(Value);
571 } else {
572 if (TM.getTargetData()->isBigEndian()) {
573 EmitInt32(unsigned(Value >> 32)); O << "\n";
574 EmitInt32(unsigned(Value));
575 } else {
576 EmitInt32(unsigned(Value)); O << "\n";
577 EmitInt32(unsigned(Value >> 32));
578 }
579 }
580}
581
582/// toOctal - Convert the low order bits of X into an octal digit.
583///
584static inline char toOctal(int X) {
585 return (X&7)+'0';
586}
587
588/// printStringChar - Print a char, escaped if necessary.
589///
590static void printStringChar(std::ostream &O, unsigned char C) {
591 if (C == '"') {
592 O << "\\\"";
593 } else if (C == '\\') {
594 O << "\\\\";
595 } else if (isprint(C)) {
596 O << C;
597 } else {
598 switch(C) {
599 case '\b': O << "\\b"; break;
600 case '\f': O << "\\f"; break;
601 case '\n': O << "\\n"; break;
602 case '\r': O << "\\r"; break;
603 case '\t': O << "\\t"; break;
604 default:
605 O << '\\';
606 O << toOctal(C >> 6);
607 O << toOctal(C >> 3);
608 O << toOctal(C >> 0);
609 break;
610 }
611 }
612}
613
614/// EmitString - Emit a string with quotes and a null terminator.
615/// Special characters are emitted properly.
616/// \literal (Eg. '\t') \endliteral
617void AsmPrinter::EmitString(const std::string &String) const {
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000618 const char* AscizDirective = TAI->getAscizDirective();
619 if (AscizDirective)
620 O << AscizDirective;
621 else
622 O << TAI->getAsciiDirective();
623 O << "\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000624 for (unsigned i = 0, N = String.size(); i < N; ++i) {
625 unsigned char C = String[i];
626 printStringChar(O, C);
627 }
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000628 if (AscizDirective)
629 O << "\"";
630 else
631 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000632}
633
634
Dan Gohman189f80d2007-09-24 20:58:13 +0000635/// EmitFile - Emit a .file directive.
636void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
637 O << "\t.file\t" << Number << " \"";
638 for (unsigned i = 0, N = Name.size(); i < N; ++i) {
639 unsigned char C = Name[i];
640 printStringChar(O, C);
641 }
642 O << "\"";
643}
644
645
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000646//===----------------------------------------------------------------------===//
647
Chris Lattner3a420532007-05-31 18:57:45 +0000648// EmitAlignment - Emit an alignment directive to the specified power of
649// two boundary. For example, if you pass in 3 here, you will get an 8
650// byte alignment. If a global value is specified, and if that global has
651// an explicit alignment requested, it will unconditionally override the
652// alignment request. However, if ForcedAlignBits is specified, this value
653// has final say: the ultimate alignment will be the max of ForcedAlignBits
654// and the alignment computed with NumBits and the global.
655//
656// The algorithm is:
657// Align = NumBits;
658// if (GV && GV->hasalignment) Align = GV->getalignment();
659// Align = std::max(Align, ForcedAlignBits);
660//
661void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng73a259a2007-07-25 23:35:07 +0000662 unsigned ForcedAlignBits, bool UseFillExpr,
663 unsigned FillValue) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000664 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000665 NumBits = Log2_32(GV->getAlignment());
666 NumBits = std::max(NumBits, ForcedAlignBits);
667
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000668 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskey563321a2006-09-06 18:34:40 +0000669 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng73a259a2007-07-25 23:35:07 +0000670 O << TAI->getAlignDirective() << NumBits;
671 if (UseFillExpr) O << ",0x" << std::hex << FillValue << std::dec;
672 O << "\n";
Chris Lattnerbfddc202004-08-17 19:14:29 +0000673}
674
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000675
Chris Lattner25045bd2005-11-21 07:51:36 +0000676/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000677///
Chris Lattner25045bd2005-11-21 07:51:36 +0000678void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000679 if (NumZeros) {
Jim Laskey563321a2006-09-06 18:34:40 +0000680 if (TAI->getZeroDirective()) {
681 O << TAI->getZeroDirective() << NumZeros;
682 if (TAI->getZeroDirectiveSuffix())
683 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000684 O << "\n";
685 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000686 for (; NumZeros; --NumZeros)
Jim Laskey563321a2006-09-06 18:34:40 +0000687 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000688 }
689 }
690}
691
Chris Lattnera80ba712004-08-16 23:15:22 +0000692// Print out the specified constant, without a storage class. Only the
693// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000694void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000695 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000696 O << "0";
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000697 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattnera875df32007-01-12 18:15:09 +0000698 O << CI->getZExtValue();
Reid Spencerb83eb642006-10-20 07:07:24 +0000699 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000700 // This is a constant address for a global variable or function. Use the
701 // name of the variable or function as the address value, possibly
702 // decorating it with GlobalVarAddrPrefix/Suffix or
703 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskey563321a2006-09-06 18:34:40 +0000704 if (isa<Function>(GV)) {
705 O << TAI->getFunctionAddrPrefix()
706 << Mang->getValueName(GV)
707 << TAI->getFunctionAddrSuffix();
708 } else {
709 O << TAI->getGlobalVarAddrPrefix()
710 << Mang->getValueName(GV)
711 << TAI->getGlobalVarAddrSuffix();
712 }
Duraid Madina855a5192005-04-02 12:21:51 +0000713 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000714 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000715 unsigned Opcode = CE->getOpcode();
716 switch (Opcode) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000717 case Instruction::GetElementPtr: {
718 // generate a symbolic expression for the byte address
719 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000720 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
721 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
722 idxVec.size())) {
Chris Lattner27e19212005-02-14 21:40:26 +0000723 if (Offset)
724 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000725 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000726 if (Offset > 0)
727 O << ") + " << Offset;
728 else if (Offset < 0)
729 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000730 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000731 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000732 }
733 break;
734 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000735 case Instruction::Trunc:
736 case Instruction::ZExt:
737 case Instruction::SExt:
738 case Instruction::FPTrunc:
739 case Instruction::FPExt:
740 case Instruction::UIToFP:
741 case Instruction::SIToFP:
742 case Instruction::FPToUI:
743 case Instruction::FPToSI:
744 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
745 break;
Chris Lattnercb0a6812006-12-12 05:14:13 +0000746 case Instruction::BitCast:
747 return EmitConstantValueOnly(CE->getOperand(0));
748
Chris Lattnerddc94012006-12-12 05:18:19 +0000749 case Instruction::IntToPtr: {
750 // Handle casts to pointers by changing them into casts to the appropriate
751 // integer type. This promotes constant folding and simplifies this code.
752 Constant *Op = CE->getOperand(0);
753 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
754 return EmitConstantValueOnly(Op);
755 }
756
757
758 case Instruction::PtrToInt: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000759 // Support only foldable casts to/from pointers that can be eliminated by
760 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000761 Constant *Op = CE->getOperand(0);
Chris Lattnerddc94012006-12-12 05:18:19 +0000762 const Type *Ty = CE->getType();
Chris Lattnera80ba712004-08-16 23:15:22 +0000763
Chris Lattnerddc94012006-12-12 05:18:19 +0000764 // We can emit the pointer value into this slot if the slot is an
765 // integer slot greater or equal to the size of the pointer.
Chris Lattner42a75512007-01-15 02:27:26 +0000766 if (Ty->isInteger() &&
Duncan Sandsca0ed742007-11-05 00:04:43 +0000767 TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
Chris Lattner4a6bd332006-07-29 01:57:19 +0000768 return EmitConstantValueOnly(Op);
Chris Lattner4a6bd332006-07-29 01:57:19 +0000769
770 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000771 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000772 break;
773 }
774 case Instruction::Add:
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000775 case Instruction::Sub:
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000776 case Instruction::And:
777 case Instruction::Or:
778 case Instruction::Xor:
Chris Lattnera80ba712004-08-16 23:15:22 +0000779 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000780 EmitConstantValueOnly(CE->getOperand(0));
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000781 O << ")";
782 switch (Opcode) {
783 case Instruction::Add:
784 O << " + ";
785 break;
786 case Instruction::Sub:
787 O << " - ";
788 break;
789 case Instruction::And:
790 O << " & ";
791 break;
792 case Instruction::Or:
793 O << " | ";
794 break;
795 case Instruction::Xor:
796 O << " ^ ";
797 break;
798 default:
799 break;
800 }
801 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000802 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000803 O << ")";
804 break;
805 default:
806 assert(0 && "Unsupported operator!");
807 }
808 } else {
809 assert(0 && "Unknown constant value!");
810 }
811}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000812
Chris Lattner2980cef2005-11-10 18:06:33 +0000813/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000814/// the predicate isString is true.
815///
Chris Lattner2980cef2005-11-10 18:06:33 +0000816static void printAsCString(std::ostream &O, const ConstantArray *CVA,
817 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000818 assert(CVA->isString() && "Array is not string compatible!");
819
820 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000821 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000822 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000823 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000824 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000825 }
826 O << "\"";
827}
828
Jeff Cohenc884db42006-05-02 01:16:28 +0000829/// EmitString - Emit a zero-byte-terminated string constant.
830///
831void AsmPrinter::EmitString(const ConstantArray *CVA) const {
832 unsigned NumElts = CVA->getNumOperands();
Jim Laskey563321a2006-09-06 18:34:40 +0000833 if (TAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000834 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskey563321a2006-09-06 18:34:40 +0000835 O << TAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000836 printAsCString(O, CVA, NumElts-1);
837 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000838 O << TAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000839 printAsCString(O, CVA, NumElts);
840 }
841 O << "\n";
842}
843
Chris Lattner25045bd2005-11-21 07:51:36 +0000844/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Duncan Sandsca0ed742007-11-05 00:04:43 +0000845/// If Packed is false, pad to the ABI size.
846void AsmPrinter::EmitGlobalConstant(const Constant *CV, bool Packed) {
Owen Andersona69571c2006-05-03 01:29:57 +0000847 const TargetData *TD = TM.getTargetData();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000848 unsigned Size = Packed ?
849 TD->getTypeStoreSize(CV->getType()) : TD->getABITypeSize(CV->getType());
Chris Lattner1b7e2352004-08-17 06:36:49 +0000850
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000851 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Duncan Sandsca0ed742007-11-05 00:04:43 +0000852 EmitZeros(Size);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000853 return;
854 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
855 if (CVA->isString()) {
Jeff Cohenc884db42006-05-02 01:16:28 +0000856 EmitString(CVA);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000857 } else { // Not a string. Print the values in successive locations
Duncan Sandsca0ed742007-11-05 00:04:43 +0000858 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
859 EmitGlobalConstant(CVA->getOperand(i), false);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000860 }
861 return;
862 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
863 // Print the fields in successive locations. Pad to align if needed!
Owen Andersona69571c2006-05-03 01:29:57 +0000864 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000865 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000866 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
867 const Constant* field = CVS->getOperand(i);
868
869 // Check if padding is needed and insert one or more 0s.
Duncan Sandsca0ed742007-11-05 00:04:43 +0000870 uint64_t fieldSize = TD->getTypeStoreSize(field->getType());
Duncan Sands0c8a13b2007-11-05 18:03:02 +0000871 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
Chris Lattnerb1919e22007-02-10 19:55:17 +0000872 - cvsLayout->getElementOffset(i)) - fieldSize;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000873 sizeSoFar += fieldSize + padSize;
874
Duncan Sands0c8a13b2007-11-05 18:03:02 +0000875 // Now print the actual field value without ABI size padding.
876 EmitGlobalConstant(field, true);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000877
Duncan Sands0c8a13b2007-11-05 18:03:02 +0000878 // Insert padding - this may include padding to increase the size of the
879 // current field up to the ABI size (if the struct is not packed) as well
880 // as padding to ensure that the next field starts at the right offset.
Chris Lattner25045bd2005-11-21 07:51:36 +0000881 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000882 }
Chris Lattnerb0c39a32007-02-10 19:59:22 +0000883 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
Chris Lattner1b7e2352004-08-17 06:36:49 +0000884 "Layout of constant struct may be incorrect!");
885 return;
886 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
887 // FP Constants are printed as integer constants to avoid losing
888 // precision...
Chris Lattner1b7e2352004-08-17 06:36:49 +0000889 if (CFP->getType() == Type::DoubleTy) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000890 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000891 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
Jim Laskey563321a2006-09-06 18:34:40 +0000892 if (TAI->getData64bitsDirective())
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000893 O << TAI->getData64bitsDirective() << i << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +0000894 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000895 else if (TD->isBigEndian()) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000896 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Jim Laskey563321a2006-09-06 18:34:40 +0000897 << "\t" << TAI->getCommentString()
898 << " double most significant word " << Val << "\n";
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000899 O << TAI->getData32bitsDirective() << unsigned(i)
Jim Laskey563321a2006-09-06 18:34:40 +0000900 << "\t" << TAI->getCommentString()
901 << " double least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000902 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000903 O << TAI->getData32bitsDirective() << unsigned(i)
Jim Laskey563321a2006-09-06 18:34:40 +0000904 << "\t" << TAI->getCommentString()
905 << " double least significant word " << Val << "\n";
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000906 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Jim Laskey563321a2006-09-06 18:34:40 +0000907 << "\t" << TAI->getCommentString()
908 << " double most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000909 }
910 return;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000911 } else if (CFP->getType() == Type::FloatTy) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000912 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
913 O << TAI->getData32bitsDirective()
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000914 << CFP->getValueAPF().convertToAPInt().getZExtValue()
Jim Laskey563321a2006-09-06 18:34:40 +0000915 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000916 return;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000917 } else if (CFP->getType() == Type::X86_FP80Ty) {
918 // all long double variants are printed as hex
Dale Johannesen693717f2007-09-26 23:20:33 +0000919 // api needed to prevent premature destruction
920 APInt api = CFP->getValueAPF().convertToAPInt();
921 const uint64_t *p = api.getRawData();
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000922 if (TD->isBigEndian()) {
923 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
924 << "\t" << TAI->getCommentString()
925 << " long double most significant halfword\n";
926 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
927 << "\t" << TAI->getCommentString()
928 << " long double next halfword\n";
929 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
930 << "\t" << TAI->getCommentString()
931 << " long double next halfword\n";
932 O << TAI->getData16bitsDirective() << uint16_t(p[0])
933 << "\t" << TAI->getCommentString()
934 << " long double next halfword\n";
935 O << TAI->getData16bitsDirective() << uint16_t(p[1])
936 << "\t" << TAI->getCommentString()
937 << " long double least significant halfword\n";
938 } else {
939 O << TAI->getData16bitsDirective() << uint16_t(p[1])
940 << "\t" << TAI->getCommentString()
941 << " long double least significant halfword\n";
942 O << TAI->getData16bitsDirective() << uint16_t(p[0])
943 << "\t" << TAI->getCommentString()
944 << " long double next halfword\n";
945 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
946 << "\t" << TAI->getCommentString()
947 << " long double next halfword\n";
948 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
949 << "\t" << TAI->getCommentString()
950 << " long double next halfword\n";
951 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
952 << "\t" << TAI->getCommentString()
953 << " long double most significant halfword\n";
954 }
Duncan Sandsca0ed742007-11-05 00:04:43 +0000955 EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty));
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000956 return;
Dale Johannesenfcf4d242007-10-11 23:32:15 +0000957 } else if (CFP->getType() == Type::PPC_FP128Ty) {
958 // all long double variants are printed as hex
959 // api needed to prevent premature destruction
960 APInt api = CFP->getValueAPF().convertToAPInt();
961 const uint64_t *p = api.getRawData();
962 if (TD->isBigEndian()) {
963 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
964 << "\t" << TAI->getCommentString()
965 << " long double most significant word\n";
966 O << TAI->getData32bitsDirective() << uint32_t(p[0])
967 << "\t" << TAI->getCommentString()
968 << " long double next word\n";
969 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
970 << "\t" << TAI->getCommentString()
971 << " long double next word\n";
972 O << TAI->getData32bitsDirective() << uint32_t(p[1])
973 << "\t" << TAI->getCommentString()
974 << " long double least significant word\n";
975 } else {
976 O << TAI->getData32bitsDirective() << uint32_t(p[1])
977 << "\t" << TAI->getCommentString()
978 << " long double least significant word\n";
979 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
980 << "\t" << TAI->getCommentString()
981 << " long double next word\n";
982 O << TAI->getData32bitsDirective() << uint32_t(p[0])
983 << "\t" << TAI->getCommentString()
984 << " long double next word\n";
985 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
986 << "\t" << TAI->getCommentString()
987 << " long double most significant word\n";
988 }
989 return;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000990 } else assert(0 && "Floating point constant type not handled");
Reid Spencer47857812006-12-31 05:55:36 +0000991 } else if (CV->getType() == Type::Int64Ty) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000992 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000993 uint64_t Val = CI->getZExtValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000994
Jim Laskey563321a2006-09-06 18:34:40 +0000995 if (TAI->getData64bitsDirective())
996 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000997 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000998 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
999 << "\t" << TAI->getCommentString()
1000 << " Double-word most significant word " << Val << "\n";
1001 O << TAI->getData32bitsDirective() << unsigned(Val)
1002 << "\t" << TAI->getCommentString()
1003 << " Double-word least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +00001004 } else {
Jim Laskey563321a2006-09-06 18:34:40 +00001005 O << TAI->getData32bitsDirective() << unsigned(Val)
1006 << "\t" << TAI->getCommentString()
1007 << " Double-word least significant word " << Val << "\n";
1008 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
1009 << "\t" << TAI->getCommentString()
1010 << " Double-word most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +00001011 }
1012 return;
1013 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00001014 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1015 const VectorType *PTy = CP->getType();
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001016
1017 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
Duncan Sandsca0ed742007-11-05 00:04:43 +00001018 EmitGlobalConstant(CP->getOperand(I), false);
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001019
1020 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +00001021 }
1022
1023 const Type *type = CV->getType();
Evan Chengd6594ae2006-09-12 21:00:35 +00001024 printDataDirective(type);
Chris Lattner25045bd2005-11-21 07:51:36 +00001025 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001026 O << "\n";
1027}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001028
Evan Chengd6594ae2006-09-12 21:00:35 +00001029void
1030AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
1031 // Target doesn't support this yet!
1032 abort();
1033}
1034
Chris Lattner3ce9b672006-09-26 23:59:50 +00001035/// PrintSpecial - Print information related to the specified machine instr
1036/// that is independent of the operand, and may be independent of the instr
1037/// itself. This can be useful for portably encoding the comment character
1038/// or other bits of target-specific knowledge into the asmstrings. The
1039/// syntax used is ${:comment}. Targets can override this to add support
1040/// for their own strange codes.
1041void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001042 if (!strcmp(Code, "private")) {
1043 O << TAI->getPrivateGlobalPrefix();
1044 } else if (!strcmp(Code, "comment")) {
Chris Lattner3ce9b672006-09-26 23:59:50 +00001045 O << TAI->getCommentString();
1046 } else if (!strcmp(Code, "uid")) {
1047 // Assign a unique ID to this machine instruction.
1048 static const MachineInstr *LastMI = 0;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001049 static const Function *F = 0;
Chris Lattner3ce9b672006-09-26 23:59:50 +00001050 static unsigned Counter = 0U-1;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001051
1052 // Comparing the address of MI isn't sufficient, because machineinstrs may
1053 // be allocated to the same address across functions.
1054 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1055
Chris Lattner3ce9b672006-09-26 23:59:50 +00001056 // If this is a new machine instruction, bump the counter.
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001057 if (LastMI != MI || F != ThisF) {
1058 ++Counter;
1059 LastMI = MI;
Chris Lattner4b092522007-02-06 01:56:31 +00001060 F = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001061 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001062 O << Counter;
1063 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +00001064 cerr << "Unknown special formatter '" << Code
1065 << "' for machine instr: " << *MI;
Chris Lattner3ce9b672006-09-26 23:59:50 +00001066 exit(1);
1067 }
1068}
1069
1070
Chris Lattner0264d1a2006-01-27 02:10:10 +00001071/// printInlineAsm - This method formats and prints the specified machine
1072/// instruction that is an inline asm.
1073void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001074 unsigned NumOperands = MI->getNumOperands();
1075
1076 // Count the number of register definitions.
1077 unsigned NumDefs = 0;
Dan Gohman92dfe202007-09-14 20:33:02 +00001078 for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001079 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001080 assert(NumDefs != NumOperands-1 && "No asm string?");
1081
1082 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001083
1084 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001085 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001086
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001087 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
1088 if (AsmStr[0] == 0) {
1089 O << "\n"; // Tab already printed, avoid double indenting next instr.
1090 return;
1091 }
1092
Jim Laskey563321a2006-09-06 18:34:40 +00001093 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001094
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001095 // The variant of the current asmprinter.
1096 int AsmPrinterVariant = TAI->getAssemblerDialect();
1097
Chris Lattner66099132006-02-01 22:41:11 +00001098 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1099 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001100
Chris Lattner66099132006-02-01 22:41:11 +00001101 while (*LastEmitted) {
1102 switch (*LastEmitted) {
1103 default: {
1104 // Not a special case, emit the string section literally.
1105 const char *LiteralEnd = LastEmitted+1;
1106 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001107 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001108 ++LiteralEnd;
1109 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1110 O.write(LastEmitted, LiteralEnd-LastEmitted);
1111 LastEmitted = LiteralEnd;
1112 break;
1113 }
Chris Lattner1c059972006-05-05 21:47:05 +00001114 case '\n':
1115 ++LastEmitted; // Consume newline character.
Chris Lattner1f6f4c72007-04-30 17:00:18 +00001116 O << "\n"; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001117 break;
Chris Lattner66099132006-02-01 22:41:11 +00001118 case '$': {
1119 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001120 bool Done = true;
1121
1122 // Handle escapes.
1123 switch (*LastEmitted) {
1124 default: Done = false; break;
1125 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001126 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1127 O << '$';
1128 ++LastEmitted; // Consume second '$' character.
1129 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001130 case '(': // $( -> same as GCC's { character.
1131 ++LastEmitted; // Consume '(' character.
1132 if (CurVariant != -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001133 cerr << "Nested variants found in inline asm string: '"
1134 << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001135 exit(1);
1136 }
1137 CurVariant = 0; // We're in the first variant now.
1138 break;
1139 case '|':
1140 ++LastEmitted; // consume '|' character.
1141 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001142 cerr << "Found '|' character outside of variant in inline asm "
1143 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001144 exit(1);
1145 }
1146 ++CurVariant; // We're in the next variant.
1147 break;
1148 case ')': // $) -> same as GCC's } char.
1149 ++LastEmitted; // consume ')' character.
1150 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001151 cerr << "Found '}' character outside of variant in inline asm "
1152 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001153 exit(1);
1154 }
1155 CurVariant = -1;
1156 break;
Chris Lattner66099132006-02-01 22:41:11 +00001157 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001158 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001159
1160 bool HasCurlyBraces = false;
1161 if (*LastEmitted == '{') { // ${variable}
1162 ++LastEmitted; // Consume '{' character.
1163 HasCurlyBraces = true;
1164 }
1165
1166 const char *IDStart = LastEmitted;
1167 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001168 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001169 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1170 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001171 cerr << "Bad $ operand number in inline asm string: '"
1172 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001173 exit(1);
1174 }
1175 LastEmitted = IDEnd;
1176
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001177 char Modifier[2] = { 0, 0 };
1178
Chris Lattner66099132006-02-01 22:41:11 +00001179 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001180 // If we have curly braces, check for a modifier character. This
1181 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1182 if (*LastEmitted == ':') {
1183 ++LastEmitted; // Consume ':' character.
1184 if (*LastEmitted == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001185 cerr << "Bad ${:} expression in inline asm string: '"
1186 << AsmStr << "'\n";
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001187 exit(1);
1188 }
1189
1190 Modifier[0] = *LastEmitted;
1191 ++LastEmitted; // Consume modifier character.
1192 }
1193
Chris Lattner66099132006-02-01 22:41:11 +00001194 if (*LastEmitted != '}') {
Bill Wendlinge8156192006-12-07 01:30:32 +00001195 cerr << "Bad ${} expression in inline asm string: '"
1196 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001197 exit(1);
1198 }
1199 ++LastEmitted; // Consume '}' character.
1200 }
1201
1202 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001203 cerr << "Invalid $ operand number in inline asm string: '"
1204 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001205 exit(1);
1206 }
1207
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001208 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001209 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001210 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1211 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001212
1213 bool Error = false;
1214
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001215 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001216 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001217 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001218 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1219 OpNo += (OpFlags >> 3) + 1;
1220 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001221
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001222 if (OpNo >= MI->getNumOperands()) {
1223 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001224 } else {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001225 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1226 ++OpNo; // Skip over the ID number.
1227
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001228 if (Modifier[0]=='l') // labels are target independent
1229 printBasicBlockLabel(MI->getOperand(OpNo).getMachineBasicBlock(),
1230 false, false);
1231 else {
1232 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1233 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1234 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1235 Modifier[0] ? Modifier : 0);
1236 } else {
1237 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1238 Modifier[0] ? Modifier : 0);
1239 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001240 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001241 }
1242 if (Error) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001243 cerr << "Invalid operand found in inline asm: '"
1244 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001245 MI->dump();
1246 exit(1);
1247 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001248 }
Chris Lattner66099132006-02-01 22:41:11 +00001249 break;
1250 }
Chris Lattner66099132006-02-01 22:41:11 +00001251 }
1252 }
Jim Laskey563321a2006-09-06 18:34:40 +00001253 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattner66099132006-02-01 22:41:11 +00001254}
1255
Jim Laskey1ee29252007-01-26 14:34:52 +00001256/// printLabel - This method prints a local label used by debug and
1257/// exception handling tables.
1258void AsmPrinter::printLabel(const MachineInstr *MI) const {
Jim Laskeyb82313f2007-02-01 16:31:34 +00001259 O << "\n"
1260 << TAI->getPrivateGlobalPrefix()
Jim Laskeybacd3042007-02-21 22:48:45 +00001261 << "label"
Jim Laskey1ee29252007-01-26 14:34:52 +00001262 << MI->getOperand(0).getImmedValue()
1263 << ":\n";
1264}
1265
Chris Lattner66099132006-02-01 22:41:11 +00001266/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1267/// instruction, using the specified assembler variant. Targets should
1268/// overried this to format as appropriate.
1269bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001270 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001271 // Target doesn't support this yet!
1272 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001273}
Chris Lattnerdd260332006-02-24 20:21:58 +00001274
1275bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1276 unsigned AsmVariant,
1277 const char *ExtraCode) {
1278 // Target doesn't support this yet!
1279 return true;
1280}
Nate Begeman37efe672006-04-22 18:53:45 +00001281
1282/// printBasicBlockLabel - This method prints the label for the specified
1283/// MachineBasicBlock
Nate Begemancdf38c42006-05-02 05:37:32 +00001284void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
1285 bool printColon,
1286 bool printComment) const {
Evan Cheng347d39f2007-10-14 05:57:21 +00001287 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << "_"
1288 << MBB->getNumber();
Nate Begemancdf38c42006-05-02 05:37:32 +00001289 if (printColon)
1290 O << ':';
Chris Lattner9c78ecb2006-10-05 21:40:14 +00001291 if (printComment && MBB->getBasicBlock())
Dan Gohman286d5692007-07-30 15:06:25 +00001292 O << '\t' << TAI->getCommentString() << ' '
1293 << MBB->getBasicBlock()->getName();
Nate Begeman37efe672006-04-22 18:53:45 +00001294}
Nate Begeman52a51e382006-08-12 21:29:52 +00001295
Evan Chengcc415862007-11-09 01:32:10 +00001296/// printPICJumpTableSetLabel - This method prints a set label for the
1297/// specified MachineBasicBlock for a jumptable entry.
1298void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1299 const MachineBasicBlock *MBB) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001300 if (!TAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001301 return;
1302
Jim Laskey563321a2006-09-06 18:34:40 +00001303 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001304 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman52a51e382006-08-12 21:29:52 +00001305 printBasicBlockLabel(MBB, false, false);
Evan Cheng347d39f2007-10-14 05:57:21 +00001306 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1307 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001308}
Evan Chengd6594ae2006-09-12 21:00:35 +00001309
Evan Chengcc415862007-11-09 01:32:10 +00001310void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1311 const MachineBasicBlock *MBB) const {
Evan Cheng41349c12006-11-01 09:23:08 +00001312 if (!TAI->getSetDirective())
1313 return;
1314
1315 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001316 << getFunctionNumber() << '_' << uid << '_' << uid2
1317 << "_set_" << MBB->getNumber() << ',';
Evan Cheng41349c12006-11-01 09:23:08 +00001318 printBasicBlockLabel(MBB, false, false);
Evan Cheng347d39f2007-10-14 05:57:21 +00001319 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1320 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001321}
1322
Evan Chengd6594ae2006-09-12 21:00:35 +00001323/// printDataDirective - This method prints the asm directive for the
1324/// specified type.
1325void AsmPrinter::printDataDirective(const Type *type) {
1326 const TargetData *TD = TM.getTargetData();
1327 switch (type->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001328 case Type::IntegerTyID: {
1329 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1330 if (BitWidth <= 8)
1331 O << TAI->getData8bitsDirective();
1332 else if (BitWidth <= 16)
1333 O << TAI->getData16bitsDirective();
1334 else if (BitWidth <= 32)
1335 O << TAI->getData32bitsDirective();
1336 else if (BitWidth <= 64) {
1337 assert(TAI->getData64bitsDirective() &&
1338 "Target cannot handle 64-bit constant exprs!");
1339 O << TAI->getData64bitsDirective();
1340 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001341 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001342 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001343 case Type::PointerTyID:
1344 if (TD->getPointerSize() == 8) {
1345 assert(TAI->getData64bitsDirective() &&
1346 "Target cannot handle 64-bit pointer exprs!");
1347 O << TAI->getData64bitsDirective();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001348 } else {
1349 O << TAI->getData32bitsDirective();
Evan Chengd6594ae2006-09-12 21:00:35 +00001350 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001351 break;
1352 case Type::FloatTyID: case Type::DoubleTyID:
Dale Johannesen4292d1c2007-09-28 18:06:58 +00001353 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
Evan Chengd6594ae2006-09-12 21:00:35 +00001354 assert (0 && "Should have already output floating point constant.");
1355 default:
1356 assert (0 && "Can't handle printing this type of thing");
1357 break;
1358 }
1359}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001360