blob: b3643129d9f2e31364ee2582925e7b8aba33e192 [file] [log] [blame]
Chris Lattner6a8e0f52004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lattner6a8e0f52004-08-16 23:15:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng993e9cf2006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng38d5e762006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman41b1cdc2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000020#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey1c055e82007-01-25 15:12:02 +000022#include "llvm/Support/CommandLine.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000023#include "llvm/Support/Mangler.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000024#include "llvm/Support/MathExtras.h"
Bill Wendling5c3966a2006-11-29 00:39:47 +000025#include "llvm/Support/Streams.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000026#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000027#include "llvm/Target/TargetData.h"
Chris Lattner90438232006-10-06 22:50:56 +000028#include "llvm/Target/TargetLowering.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000029#include "llvm/Target/TargetMachine.h"
Evan Cheng797d56f2007-11-09 01:32:10 +000030#include "llvm/ADT/SmallPtrSet.h"
Chris Lattneraa23fa92006-02-01 22:41:11 +000031#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000032using namespace llvm;
33
Jim Laskey1c055e82007-01-25 15:12:02 +000034static cl::opt<bool>
35AsmVerbose("asm-verbose", cl::Hidden, cl::desc("Add comments to directives."));
36
Devang Patel8c78a0b2007-05-03 01:11:54 +000037char AsmPrinter::ID = 0;
Jim Laskey261779b2006-09-07 22:06:40 +000038AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
39 const TargetAsmInfo *T)
Evan Chengcdf36092007-10-14 05:57:21 +000040 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o), TM(tm), TAI(T)
Jim Laskeya6211dc2006-09-06 18:34:40 +000041{}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000042
Chris Lattnerdc822412006-10-05 02:42:47 +000043std::string AsmPrinter::getSectionForFunction(const Function &F) const {
44 return TAI->getTextSection();
45}
46
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000047
Chris Lattner8488ba22006-05-09 04:59:56 +000048/// SwitchToTextSection - Switch to the specified text section of the executable
49/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000050///
Chris Lattner8488ba22006-05-09 04:59:56 +000051void AsmPrinter::SwitchToTextSection(const char *NewSection,
52 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000053 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000054 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000055 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000056 else
57 NS = NewSection;
58
59 // If we're already in this section, we're done.
60 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000061
Chris Lattner4ebc6a22006-05-09 05:33:48 +000062 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000063 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
64 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000065
Chris Lattner4ebc6a22006-05-09 05:33:48 +000066 CurrentSection = NS;
67
68 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000069 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000070}
71
Nate Begeman78756502006-07-27 01:13:04 +000072/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000073/// if we are not already in it!
74///
75void AsmPrinter::SwitchToDataSection(const char *NewSection,
76 const GlobalValue *GV) {
77 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000078 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000079 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner6341df82006-05-09 05:23:12 +000080 else
81 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +000082
Chris Lattner6341df82006-05-09 05:23:12 +000083 // If we're already in this section, we're done.
84 if (CurrentSection == NS) return;
85
Chris Lattner4ebc6a22006-05-09 05:33:48 +000086 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000087 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
88 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner4ebc6a22006-05-09 05:33:48 +000089
90 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +000091
Chris Lattner4ebc6a22006-05-09 05:33:48 +000092 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000093 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +000094}
95
96
Chris Lattner6a8e0f52004-08-16 23:15:22 +000097bool AsmPrinter::doInitialization(Module &M) {
Jim Laskeya6211dc2006-09-06 18:34:40 +000098 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +000099
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000100 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000101 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000102 << M.getModuleInlineAsm()
Jim Laskeya6211dc2006-09-06 18:34:40 +0000103 << "\n" << TAI->getCommentString()
104 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000105
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000106 SwitchToDataSection(""); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000107
Jim Laskeyc56315c2007-01-26 21:22:28 +0000108 if (MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>()) {
109 MMI->AnalyzeModule(M);
Jim Laskey0bbdc552006-01-26 20:21:46 +0000110 }
111
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000112 return false;
113}
114
115bool AsmPrinter::doFinalization(Module &M) {
Rafael Espindolad7998d02006-12-18 03:37:18 +0000116 if (TAI->getWeakRefDirective()) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000117 if (!ExtWeakSymbols.empty())
Rafael Espindolad7998d02006-12-18 03:37:18 +0000118 SwitchToDataSection("");
119
120 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
121 e = ExtWeakSymbols.end(); i != e; ++i) {
122 const GlobalValue *GV = *i;
123 std::string Name = Mang->getValueName(GV);
124 O << TAI->getWeakRefDirective() << Name << "\n";
125 }
126 }
127
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000128 if (TAI->getSetDirective()) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000129 if (!M.alias_empty())
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000130 SwitchToTextSection(TAI->getTextSection());
131
132 O << "\n";
133 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
134 I!=E; ++I) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000135 std::string Name = Mang->getValueName(I);
136 std::string Target;
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000137
138 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
139 Target = Mang->getValueName(GV);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000140
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000141 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000142 O << "\t.globl\t" << Name << "\n";
143 else if (I->hasWeakLinkage())
144 O << TAI->getWeakRefDirective() << Name << "\n";
145 else if (!I->hasInternalLinkage())
146 assert(0 && "Invalid alias linkage");
147
148 O << TAI->getSetDirective() << Name << ", " << Target << "\n";
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000149
150 // If the aliasee has external weak linkage it can be referenced only by
151 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
152 // weak reference in such case.
153 if (GV->hasExternalWeakLinkage())
154 if (TAI->getWeakRefDirective())
155 O << TAI->getWeakRefDirective() << Target << "\n";
156 else
157 O << "\t.globl\t" << Target << "\n";
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000158 }
159 }
160
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000161 delete Mang; Mang = 0;
162 return false;
163}
164
Bill Wendlinge8c885f2007-09-18 09:10:16 +0000165std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
Bill Wendling067f1d82007-09-18 01:47:22 +0000166 assert(MF && "No machine function?");
Bill Wendling69833b62007-09-18 05:03:44 +0000167 return Mang->makeNameProper(MF->getFunction()->getName() + ".eh",
168 TAI->getGlobalPrefix());
Bill Wendling067f1d82007-09-18 01:47:22 +0000169}
170
Chris Lattnerbb644e32005-11-21 07:51:36 +0000171void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000172 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000173 CurrentFnName = Mang->getValueName(MF.getFunction());
Evan Chengcdf36092007-10-14 05:57:21 +0000174 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000175}
176
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000177/// EmitConstantPool - Print to the current output stream assembly
178/// representations of the constants in the constant pool MCP. This is
179/// used to print out constants which have been "spilled to memory" by
180/// the code generator.
181///
182void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000183 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000184 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000185
186 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
187 // in special sections.
188 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
189 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
190 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
191 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000192 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000193 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000194 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng2ad050f2006-09-14 07:35:00 +0000195 const Type *Ty = CPE.getType();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000196 if (TAI->getFourByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000197 TM.getTargetData()->getABITypeSize(Ty) == 4)
Evan Chengec1d60b2006-06-29 00:26:09 +0000198 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000199 else if (TAI->getEightByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000200 TM.getTargetData()->getABITypeSize(Ty) == 8)
Evan Chengec1d60b2006-06-29 00:26:09 +0000201 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000202 else if (TAI->getSixteenByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000203 TM.getTargetData()->getABITypeSize(Ty) == 16)
Evan Chengec1d60b2006-06-29 00:26:09 +0000204 SixteenByteCPs.push_back(std::make_pair(CPE, i));
205 else
206 OtherCPs.push_back(std::make_pair(CPE, i));
207 }
208
209 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000210 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
211 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
212 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
213 SixteenByteCPs);
214 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Chengec1d60b2006-06-29 00:26:09 +0000215}
216
217void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
218 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
219 if (CP.empty()) return;
220
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000221 SwitchToDataSection(Section);
Evan Chengec1d60b2006-06-29 00:26:09 +0000222 EmitAlignment(Alignment);
223 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengcdf36092007-10-14 05:57:21 +0000224 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
225 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Cheng2ad050f2006-09-14 07:35:00 +0000226 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
227 if (CP[i].first.isMachineConstantPoolEntry())
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000228 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000229 else
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000230 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000231 if (i != e-1) {
Evan Cheng2ad050f2006-09-14 07:35:00 +0000232 const Type *Ty = CP[i].first.getType();
Evan Chengec1d60b2006-06-29 00:26:09 +0000233 unsigned EntSize =
Duncan Sands283207a2007-11-05 00:04:43 +0000234 TM.getTargetData()->getABITypeSize(Ty);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000235 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000236 // Emit inter-object padding for alignment.
Evan Cheng2ad050f2006-09-14 07:35:00 +0000237 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000238 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000239 }
240}
241
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000242/// EmitJumpTableInfo - Print assembly representations of the jump tables used
243/// by the current function to the current output stream.
244///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000245void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
246 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000247 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
248 if (JT.empty()) return;
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000249
Jim Laskey70323a82006-12-14 19:17:33 +0000250 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begemanefc312a2006-07-27 16:46:58 +0000251
Nate Begeman78756502006-07-27 01:13:04 +0000252 // Pick the directive to use to print the jump table entries, and switch to
253 // the appropriate section.
Jim Laskey70323a82006-12-14 19:17:33 +0000254 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000255
256 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
257 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
258 !JumpTableDataSection) {
Jim Laskey70323a82006-12-14 19:17:33 +0000259 // In PIC mode, we need to emit the jump table to the same section as the
260 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000261 // We should also do if the section name is NULL.
Jim Laskey70323a82006-12-14 19:17:33 +0000262 const Function *F = MF.getFunction();
263 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman78756502006-07-27 01:13:04 +0000264 } else {
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000265 SwitchToDataSection(JumpTableDataSection);
Nate Begeman78756502006-07-27 01:13:04 +0000266 }
Jim Laskey70323a82006-12-14 19:17:33 +0000267
268 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000269
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000270 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000271 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner28bfe382006-10-28 18:10:06 +0000272
273 // If this jump table was deleted, ignore it.
274 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-08-12 21:29:52 +0000275
276 // For PIC codegen, if possible we want to use the SetDirective to reduce
277 // the number of relocations the assembler will generate for the jump table.
278 // Set directives are all printed before the jump table itself.
Evan Cheng797d56f2007-11-09 01:32:10 +0000279 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Jim Laskey70323a82006-12-14 19:17:33 +0000280 if (TAI->getSetDirective() && IsPic)
Nate Begeman984c1a42006-08-12 21:29:52 +0000281 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng797d56f2007-11-09 01:32:10 +0000282 if (EmittedSets.insert(JTBBs[ii]))
283 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman984c1a42006-08-12 21:29:52 +0000284
Chris Lattner0ee2d462007-01-18 01:12:56 +0000285 // On some targets (e.g. darwin) we want to emit two consequtive labels
286 // before each jump table. The first label is never referenced, but tells
287 // the assembler and linker the extents of the jump table object. The
288 // second label is actually referenced by the code.
289 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
Evan Chengcdf36092007-10-14 05:57:21 +0000290 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Chris Lattner0ee2d462007-01-18 01:12:56 +0000291
Evan Chengcdf36092007-10-14 05:57:21 +0000292 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
293 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000294
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000295 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000296 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000297 O << '\n';
298 }
299 }
300}
301
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000302void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
303 const MachineBasicBlock *MBB,
304 unsigned uid) const {
305 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
306
307 // Use JumpTableDirective otherwise honor the entry size from the jump table
308 // info.
309 const char *JTEntryDirective = TAI->getJumpTableDirective();
310 bool HadJTEntryDirective = JTEntryDirective != NULL;
311 if (!HadJTEntryDirective) {
312 JTEntryDirective = MJTI->getEntrySize() == 4 ?
313 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
314 }
315
316 O << JTEntryDirective << ' ';
317
318 // If we have emitted set directives for the jump table entries, print
319 // them rather than the entries themselves. If we're emitting PIC, then
320 // emit the table entries as differences between two text section labels.
321 // If we're emitting non-PIC code, then emit the entries as direct
322 // references to the target basic blocks.
323 if (IsPic) {
324 if (TAI->getSetDirective()) {
325 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
326 << '_' << uid << "_set_" << MBB->getNumber();
327 } else {
328 printBasicBlockLabel(MBB, false, false);
329 // If the arch uses custom Jump Table directives, don't calc relative to
330 // JT
331 if (!HadJTEntryDirective)
332 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
333 << getFunctionNumber() << '_' << uid;
334 }
335 } else {
336 printBasicBlockLabel(MBB, false, false);
337 }
338}
339
340
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000341/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
342/// special global used by LLVM. If so, emit it and return true, otherwise
343/// do nothing and return false.
344bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Andrew Lenharthbeb80a92007-08-22 19:33:11 +0000345 if (GV->getName() == "llvm.used") {
346 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
347 EmitLLVMUsedList(GV->getInitializer());
348 return true;
349 }
350
Jim Laskey313570f2006-03-07 22:00:35 +0000351 // Ignore debug and non-emitted data.
352 if (GV->getSection() == "llvm.metadata") return true;
353
354 if (!GV->hasAppendingLinkage()) return false;
355
356 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000357
Evan Cheng53ce7de2007-06-04 20:39:18 +0000358 const TargetData *TD = TM.getTargetData();
359 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner3760e902006-01-12 19:17:23 +0000360 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000361 SwitchToDataSection(TAI->getStaticCtorsSection());
Evan Cheng53ce7de2007-06-04 20:39:18 +0000362 EmitAlignment(Align, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000363 EmitXXStructorList(GV->getInitializer());
364 return true;
365 }
366
Chris Lattner3760e902006-01-12 19:17:23 +0000367 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000368 SwitchToDataSection(TAI->getStaticDtorsSection());
Evan Cheng53ce7de2007-06-04 20:39:18 +0000369 EmitAlignment(Align, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000370 EmitXXStructorList(GV->getInitializer());
371 return true;
372 }
373
374 return false;
375}
376
Chris Lattner66af3902006-09-26 03:38:18 +0000377/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
378/// global in the specified llvm.used list as being used with this directive.
379void AsmPrinter::EmitLLVMUsedList(Constant *List) {
380 const char *Directive = TAI->getUsedDirective();
381
382 // Should be an array of 'sbyte*'.
383 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
384 if (InitList == 0) return;
385
386 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
387 O << Directive;
388 EmitConstantValueOnly(InitList->getOperand(i));
389 O << "\n";
390 }
391}
392
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000393/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
394/// function pointers, ignoring the init priority.
395void AsmPrinter::EmitXXStructorList(Constant *List) {
396 // Should be an array of '{ int, void ()* }' structs. The first value is the
397 // init priority, which we ignore.
398 if (!isa<ConstantArray>(List)) return;
399 ConstantArray *InitList = cast<ConstantArray>(List);
400 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
401 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
402 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000403
404 if (CS->getOperand(1)->isNullValue())
405 return; // Found a null terminator, exit printing.
406 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000407 EmitGlobalConstant(CS->getOperand(1));
408 }
409}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000410
Jim Laskey71262542006-10-17 13:41:07 +0000411/// getGlobalLinkName - Returns the asm/link name of of the specified
412/// global variable. Should be overridden by each target asm printer to
413/// generate the appropriate value.
Jim Laskeyd24b9132006-10-17 17:17:24 +0000414const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
415 std::string LinkName;
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000416
417 if (isa<Function>(GV)) {
418 LinkName += TAI->getFunctionAddrPrefix();
419 LinkName += Mang->getValueName(GV);
420 LinkName += TAI->getFunctionAddrSuffix();
421 } else {
422 LinkName += TAI->getGlobalVarAddrPrefix();
423 LinkName += Mang->getValueName(GV);
424 LinkName += TAI->getGlobalVarAddrSuffix();
425 }
426
Jim Laskeyd24b9132006-10-17 17:17:24 +0000427 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000428}
429
Jim Laskey18fc0972007-02-21 22:47:38 +0000430/// EmitExternalGlobal - Emit the external reference to a global variable.
431/// Should be overridden if an indirect reference should be used.
432void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
433 O << getGlobalLinkName(GV);
434}
435
436
437
Jim Laskey1c055e82007-01-25 15:12:02 +0000438//===----------------------------------------------------------------------===//
439/// LEB 128 number encoding.
440
441/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
442/// representing an unsigned leb128 value.
443void AsmPrinter::PrintULEB128(unsigned Value) const {
444 do {
445 unsigned Byte = Value & 0x7f;
446 Value >>= 7;
447 if (Value) Byte |= 0x80;
448 O << "0x" << std::hex << Byte << std::dec;
449 if (Value) O << ", ";
450 } while (Value);
451}
452
453/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
454/// value.
455unsigned AsmPrinter::SizeULEB128(unsigned Value) {
456 unsigned Size = 0;
457 do {
458 Value >>= 7;
459 Size += sizeof(int8_t);
460 } while (Value);
461 return Size;
462}
463
464/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
465/// representing a signed leb128 value.
466void AsmPrinter::PrintSLEB128(int Value) const {
467 int Sign = Value >> (8 * sizeof(Value) - 1);
468 bool IsMore;
469
470 do {
471 unsigned Byte = Value & 0x7f;
472 Value >>= 7;
473 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
474 if (IsMore) Byte |= 0x80;
475 O << "0x" << std::hex << Byte << std::dec;
476 if (IsMore) O << ", ";
477 } while (IsMore);
478}
479
480/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
481/// value.
482unsigned AsmPrinter::SizeSLEB128(int Value) {
483 unsigned Size = 0;
484 int Sign = Value >> (8 * sizeof(Value) - 1);
485 bool IsMore;
486
487 do {
488 unsigned Byte = Value & 0x7f;
489 Value >>= 7;
490 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
491 Size += sizeof(int8_t);
492 } while (IsMore);
493 return Size;
494}
495
496//===--------------------------------------------------------------------===//
497// Emission and print routines
498//
499
500/// PrintHex - Print a value as a hexidecimal value.
501///
502void AsmPrinter::PrintHex(int Value) const {
503 O << "0x" << std::hex << Value << std::dec;
504}
505
506/// EOL - Print a newline character to asm stream. If a comment is present
507/// then it will be printed first. Comments should not contain '\n'.
Jim Laskey18fc0972007-02-21 22:47:38 +0000508void AsmPrinter::EOL() const {
509 O << "\n";
510}
Jim Laskey1c055e82007-01-25 15:12:02 +0000511void AsmPrinter::EOL(const std::string &Comment) const {
512 if (AsmVerbose && !Comment.empty()) {
513 O << "\t"
514 << TAI->getCommentString()
515 << " "
516 << Comment;
517 }
518 O << "\n";
519}
520
521/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
522/// unsigned leb128 value.
523void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
524 if (TAI->hasLEB128()) {
525 O << "\t.uleb128\t"
526 << Value;
527 } else {
528 O << TAI->getData8bitsDirective();
529 PrintULEB128(Value);
530 }
531}
532
533/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
534/// signed leb128 value.
535void AsmPrinter::EmitSLEB128Bytes(int Value) const {
536 if (TAI->hasLEB128()) {
537 O << "\t.sleb128\t"
538 << Value;
539 } else {
540 O << TAI->getData8bitsDirective();
541 PrintSLEB128(Value);
542 }
543}
544
545/// EmitInt8 - Emit a byte directive and value.
546///
547void AsmPrinter::EmitInt8(int Value) const {
548 O << TAI->getData8bitsDirective();
549 PrintHex(Value & 0xFF);
550}
551
552/// EmitInt16 - Emit a short directive and value.
553///
554void AsmPrinter::EmitInt16(int Value) const {
555 O << TAI->getData16bitsDirective();
556 PrintHex(Value & 0xFFFF);
557}
558
559/// EmitInt32 - Emit a long directive and value.
560///
561void AsmPrinter::EmitInt32(int Value) const {
562 O << TAI->getData32bitsDirective();
563 PrintHex(Value);
564}
565
566/// EmitInt64 - Emit a long long directive and value.
567///
568void AsmPrinter::EmitInt64(uint64_t Value) const {
569 if (TAI->getData64bitsDirective()) {
570 O << TAI->getData64bitsDirective();
571 PrintHex(Value);
572 } else {
573 if (TM.getTargetData()->isBigEndian()) {
574 EmitInt32(unsigned(Value >> 32)); O << "\n";
575 EmitInt32(unsigned(Value));
576 } else {
577 EmitInt32(unsigned(Value)); O << "\n";
578 EmitInt32(unsigned(Value >> 32));
579 }
580 }
581}
582
583/// toOctal - Convert the low order bits of X into an octal digit.
584///
585static inline char toOctal(int X) {
586 return (X&7)+'0';
587}
588
589/// printStringChar - Print a char, escaped if necessary.
590///
591static void printStringChar(std::ostream &O, unsigned char C) {
592 if (C == '"') {
593 O << "\\\"";
594 } else if (C == '\\') {
595 O << "\\\\";
596 } else if (isprint(C)) {
597 O << C;
598 } else {
599 switch(C) {
600 case '\b': O << "\\b"; break;
601 case '\f': O << "\\f"; break;
602 case '\n': O << "\\n"; break;
603 case '\r': O << "\\r"; break;
604 case '\t': O << "\\t"; break;
605 default:
606 O << '\\';
607 O << toOctal(C >> 6);
608 O << toOctal(C >> 3);
609 O << toOctal(C >> 0);
610 break;
611 }
612 }
613}
614
615/// EmitString - Emit a string with quotes and a null terminator.
616/// Special characters are emitted properly.
617/// \literal (Eg. '\t') \endliteral
618void AsmPrinter::EmitString(const std::string &String) const {
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000619 const char* AscizDirective = TAI->getAscizDirective();
620 if (AscizDirective)
621 O << AscizDirective;
622 else
623 O << TAI->getAsciiDirective();
624 O << "\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000625 for (unsigned i = 0, N = String.size(); i < N; ++i) {
626 unsigned char C = String[i];
627 printStringChar(O, C);
628 }
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000629 if (AscizDirective)
630 O << "\"";
631 else
632 O << "\\0\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000633}
634
635
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000636/// EmitFile - Emit a .file directive.
637void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
638 O << "\t.file\t" << Number << " \"";
639 for (unsigned i = 0, N = Name.size(); i < N; ++i) {
640 unsigned char C = Name[i];
641 printStringChar(O, C);
642 }
643 O << "\"";
644}
645
646
Jim Laskey1c055e82007-01-25 15:12:02 +0000647//===----------------------------------------------------------------------===//
648
Chris Lattner3e3ff302007-05-31 18:57:45 +0000649// EmitAlignment - Emit an alignment directive to the specified power of
650// two boundary. For example, if you pass in 3 here, you will get an 8
651// byte alignment. If a global value is specified, and if that global has
652// an explicit alignment requested, it will unconditionally override the
653// alignment request. However, if ForcedAlignBits is specified, this value
654// has final say: the ultimate alignment will be the max of ForcedAlignBits
655// and the alignment computed with NumBits and the global.
656//
657// The algorithm is:
658// Align = NumBits;
659// if (GV && GV->hasalignment) Align = GV->getalignment();
660// Align = std::max(Align, ForcedAlignBits);
661//
662void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng86eb3fd2007-07-25 23:35:07 +0000663 unsigned ForcedAlignBits, bool UseFillExpr,
664 unsigned FillValue) const {
Dale Johannesen8653d292007-04-23 23:33:31 +0000665 if (GV && GV->getAlignment())
Chris Lattner3e3ff302007-05-31 18:57:45 +0000666 NumBits = Log2_32(GV->getAlignment());
667 NumBits = std::max(NumBits, ForcedAlignBits);
668
Chris Lattner747960d2005-11-10 18:09:27 +0000669 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000670 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng86eb3fd2007-07-25 23:35:07 +0000671 O << TAI->getAlignDirective() << NumBits;
672 if (UseFillExpr) O << ",0x" << std::hex << FillValue << std::dec;
673 O << "\n";
Chris Lattner1d35c162004-08-17 19:14:29 +0000674}
675
Jim Laskey18fc0972007-02-21 22:47:38 +0000676
Chris Lattnerbb644e32005-11-21 07:51:36 +0000677/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000678///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000679void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000680 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000681 if (TAI->getZeroDirective()) {
682 O << TAI->getZeroDirective() << NumZeros;
683 if (TAI->getZeroDirectiveSuffix())
684 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000685 O << "\n";
686 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000687 for (; NumZeros; --NumZeros)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000688 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000689 }
690 }
691}
692
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000693// Print out the specified constant, without a storage class. Only the
694// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000695void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000696 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000697 O << "0";
Zhou Sheng75b871f2007-01-11 12:24:14 +0000698 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner7f6fac42007-01-12 18:15:09 +0000699 O << CI->getZExtValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000700 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000701 // This is a constant address for a global variable or function. Use the
702 // name of the variable or function as the address value, possibly
703 // decorating it with GlobalVarAddrPrefix/Suffix or
704 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000705 if (isa<Function>(GV)) {
706 O << TAI->getFunctionAddrPrefix()
707 << Mang->getValueName(GV)
708 << TAI->getFunctionAddrSuffix();
709 } else {
710 O << TAI->getGlobalVarAddrPrefix()
711 << Mang->getValueName(GV)
712 << TAI->getGlobalVarAddrSuffix();
713 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000714 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000715 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000716 unsigned Opcode = CE->getOpcode();
717 switch (Opcode) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000718 case Instruction::GetElementPtr: {
719 // generate a symbolic expression for the byte address
720 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner83dfca82007-02-10 20:31:59 +0000721 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
722 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
723 idxVec.size())) {
Chris Lattner145569b2005-02-14 21:40:26 +0000724 if (Offset)
725 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000726 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000727 if (Offset > 0)
728 O << ") + " << Offset;
729 else if (Offset < 0)
730 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000731 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000732 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000733 }
734 break;
735 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000736 case Instruction::Trunc:
737 case Instruction::ZExt:
738 case Instruction::SExt:
739 case Instruction::FPTrunc:
740 case Instruction::FPExt:
741 case Instruction::UIToFP:
742 case Instruction::SIToFP:
743 case Instruction::FPToUI:
744 case Instruction::FPToSI:
745 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
746 break;
Chris Lattnerb9e41f52006-12-12 05:14:13 +0000747 case Instruction::BitCast:
748 return EmitConstantValueOnly(CE->getOperand(0));
749
Chris Lattner0c537da2006-12-12 05:18:19 +0000750 case Instruction::IntToPtr: {
751 // Handle casts to pointers by changing them into casts to the appropriate
752 // integer type. This promotes constant folding and simplifies this code.
753 Constant *Op = CE->getOperand(0);
754 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
755 return EmitConstantValueOnly(Op);
756 }
757
758
759 case Instruction::PtrToInt: {
Chris Lattner85492422006-07-29 01:57:19 +0000760 // Support only foldable casts to/from pointers that can be eliminated by
761 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000762 Constant *Op = CE->getOperand(0);
Chris Lattner0c537da2006-12-12 05:18:19 +0000763 const Type *Ty = CE->getType();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000764
Chris Lattner0c537da2006-12-12 05:18:19 +0000765 // We can emit the pointer value into this slot if the slot is an
766 // integer slot greater or equal to the size of the pointer.
Chris Lattner03c49532007-01-15 02:27:26 +0000767 if (Ty->isInteger() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000768 TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
Chris Lattner85492422006-07-29 01:57:19 +0000769 return EmitConstantValueOnly(Op);
Chris Lattner85492422006-07-29 01:57:19 +0000770
771 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000772 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000773 break;
774 }
775 case Instruction::Add:
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000776 case Instruction::Sub:
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000777 case Instruction::And:
778 case Instruction::Or:
779 case Instruction::Xor:
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000780 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000781 EmitConstantValueOnly(CE->getOperand(0));
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000782 O << ")";
783 switch (Opcode) {
784 case Instruction::Add:
785 O << " + ";
786 break;
787 case Instruction::Sub:
788 O << " - ";
789 break;
790 case Instruction::And:
791 O << " & ";
792 break;
793 case Instruction::Or:
794 O << " | ";
795 break;
796 case Instruction::Xor:
797 O << " ^ ";
798 break;
799 default:
800 break;
801 }
802 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000803 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000804 O << ")";
805 break;
806 default:
807 assert(0 && "Unsupported operator!");
808 }
809 } else {
810 assert(0 && "Unknown constant value!");
811 }
812}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000813
Chris Lattner55a6d902005-11-10 18:06:33 +0000814/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000815/// the predicate isString is true.
816///
Chris Lattner55a6d902005-11-10 18:06:33 +0000817static void printAsCString(std::ostream &O, const ConstantArray *CVA,
818 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000819 assert(CVA->isString() && "Array is not string compatible!");
820
821 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000822 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000823 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000824 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskey1c055e82007-01-25 15:12:02 +0000825 printStringChar(O, C);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000826 }
827 O << "\"";
828}
829
Jeff Cohen24a62a92006-05-02 01:16:28 +0000830/// EmitString - Emit a zero-byte-terminated string constant.
831///
832void AsmPrinter::EmitString(const ConstantArray *CVA) const {
833 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000834 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000835 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000836 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000837 printAsCString(O, CVA, NumElts-1);
838 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000839 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000840 printAsCString(O, CVA, NumElts);
841 }
842 O << "\n";
843}
844
Chris Lattnerbb644e32005-11-21 07:51:36 +0000845/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Duncan Sands283207a2007-11-05 00:04:43 +0000846/// If Packed is false, pad to the ABI size.
847void AsmPrinter::EmitGlobalConstant(const Constant *CV, bool Packed) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000848 const TargetData *TD = TM.getTargetData();
Duncan Sands283207a2007-11-05 00:04:43 +0000849 unsigned Size = Packed ?
850 TD->getTypeStoreSize(CV->getType()) : TD->getABITypeSize(CV->getType());
Chris Lattner8452a1f2004-08-17 06:36:49 +0000851
Chris Lattner61753bf2004-10-16 18:19:26 +0000852 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Duncan Sands283207a2007-11-05 00:04:43 +0000853 EmitZeros(Size);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000854 return;
855 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
856 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000857 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000858 } else { // Not a string. Print the values in successive locations
Duncan Sands283207a2007-11-05 00:04:43 +0000859 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
860 EmitGlobalConstant(CVA->getOperand(i), false);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000861 }
862 return;
863 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
864 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000865 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000866 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000867 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
868 const Constant* field = CVS->getOperand(i);
869
870 // Check if padding is needed and insert one or more 0s.
Duncan Sands283207a2007-11-05 00:04:43 +0000871 uint64_t fieldSize = TD->getTypeStoreSize(field->getType());
Duncan Sandsf7ae8bd2007-11-05 18:03:02 +0000872 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
Chris Lattnerc473d8e2007-02-10 19:55:17 +0000873 - cvsLayout->getElementOffset(i)) - fieldSize;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000874 sizeSoFar += fieldSize + padSize;
875
Duncan Sandsf7ae8bd2007-11-05 18:03:02 +0000876 // Now print the actual field value without ABI size padding.
877 EmitGlobalConstant(field, true);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000878
Duncan Sandsf7ae8bd2007-11-05 18:03:02 +0000879 // Insert padding - this may include padding to increase the size of the
880 // current field up to the ABI size (if the struct is not packed) as well
881 // as padding to ensure that the next field starts at the right offset.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000882 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000883 }
Chris Lattnerb84892d2007-02-10 19:59:22 +0000884 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
Chris Lattner8452a1f2004-08-17 06:36:49 +0000885 "Layout of constant struct may be incorrect!");
886 return;
887 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
888 // FP Constants are printed as integer constants to avoid losing
889 // precision...
Chris Lattner8452a1f2004-08-17 06:36:49 +0000890 if (CFP->getType() == Type::DoubleTy) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000891 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
Dale Johannesen028084e2007-09-12 03:30:33 +0000892 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000893 if (TAI->getData64bitsDirective())
Dale Johannesen245dceb2007-09-11 18:32:33 +0000894 O << TAI->getData64bitsDirective() << i << "\t"
Jim Laskeya6211dc2006-09-06 18:34:40 +0000895 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000896 else if (TD->isBigEndian()) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000897 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000898 << "\t" << TAI->getCommentString()
899 << " double most significant word " << Val << "\n";
Dale Johannesen245dceb2007-09-11 18:32:33 +0000900 O << TAI->getData32bitsDirective() << unsigned(i)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000901 << "\t" << TAI->getCommentString()
902 << " double least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000903 } else {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000904 O << TAI->getData32bitsDirective() << unsigned(i)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000905 << "\t" << TAI->getCommentString()
906 << " double least significant word " << Val << "\n";
Dale Johannesen245dceb2007-09-11 18:32:33 +0000907 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000908 << "\t" << TAI->getCommentString()
909 << " double most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000910 }
911 return;
Dale Johannesen028084e2007-09-12 03:30:33 +0000912 } else if (CFP->getType() == Type::FloatTy) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000913 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
914 O << TAI->getData32bitsDirective()
Dale Johannesen028084e2007-09-12 03:30:33 +0000915 << CFP->getValueAPF().convertToAPInt().getZExtValue()
Jim Laskeya6211dc2006-09-06 18:34:40 +0000916 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000917 return;
Dale Johannesen028084e2007-09-12 03:30:33 +0000918 } else if (CFP->getType() == Type::X86_FP80Ty) {
919 // all long double variants are printed as hex
Dale Johannesen34aa41c2007-09-26 23:20:33 +0000920 // api needed to prevent premature destruction
921 APInt api = CFP->getValueAPF().convertToAPInt();
922 const uint64_t *p = api.getRawData();
Dale Johannesen028084e2007-09-12 03:30:33 +0000923 if (TD->isBigEndian()) {
924 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
925 << "\t" << TAI->getCommentString()
926 << " long double most significant halfword\n";
927 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
928 << "\t" << TAI->getCommentString()
929 << " long double next halfword\n";
930 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
931 << "\t" << TAI->getCommentString()
932 << " long double next halfword\n";
933 O << TAI->getData16bitsDirective() << uint16_t(p[0])
934 << "\t" << TAI->getCommentString()
935 << " long double next halfword\n";
936 O << TAI->getData16bitsDirective() << uint16_t(p[1])
937 << "\t" << TAI->getCommentString()
938 << " long double least significant halfword\n";
939 } else {
940 O << TAI->getData16bitsDirective() << uint16_t(p[1])
941 << "\t" << TAI->getCommentString()
942 << " long double least significant halfword\n";
943 O << TAI->getData16bitsDirective() << uint16_t(p[0])
944 << "\t" << TAI->getCommentString()
945 << " long double next halfword\n";
946 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
947 << "\t" << TAI->getCommentString()
948 << " long double next halfword\n";
949 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
950 << "\t" << TAI->getCommentString()
951 << " long double next halfword\n";
952 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
953 << "\t" << TAI->getCommentString()
954 << " long double most significant halfword\n";
955 }
Duncan Sands283207a2007-11-05 00:04:43 +0000956 EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty));
Dale Johannesen028084e2007-09-12 03:30:33 +0000957 return;
Dale Johannesen6472eb62007-10-11 23:32:15 +0000958 } else if (CFP->getType() == Type::PPC_FP128Ty) {
959 // all long double variants are printed as hex
960 // api needed to prevent premature destruction
961 APInt api = CFP->getValueAPF().convertToAPInt();
962 const uint64_t *p = api.getRawData();
963 if (TD->isBigEndian()) {
964 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
965 << "\t" << TAI->getCommentString()
966 << " long double most significant word\n";
967 O << TAI->getData32bitsDirective() << uint32_t(p[0])
968 << "\t" << TAI->getCommentString()
969 << " long double next word\n";
970 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
971 << "\t" << TAI->getCommentString()
972 << " long double next word\n";
973 O << TAI->getData32bitsDirective() << uint32_t(p[1])
974 << "\t" << TAI->getCommentString()
975 << " long double least significant word\n";
976 } else {
977 O << TAI->getData32bitsDirective() << uint32_t(p[1])
978 << "\t" << TAI->getCommentString()
979 << " long double least significant word\n";
980 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
981 << "\t" << TAI->getCommentString()
982 << " long double next word\n";
983 O << TAI->getData32bitsDirective() << uint32_t(p[0])
984 << "\t" << TAI->getCommentString()
985 << " long double next word\n";
986 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
987 << "\t" << TAI->getCommentString()
988 << " long double most significant word\n";
989 }
990 return;
Dale Johannesen028084e2007-09-12 03:30:33 +0000991 } else assert(0 && "Floating point constant type not handled");
Reid Spencere63b6512006-12-31 05:55:36 +0000992 } else if (CV->getType() == Type::Int64Ty) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000993 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000994 uint64_t Val = CI->getZExtValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000995
Jim Laskeya6211dc2006-09-06 18:34:40 +0000996 if (TAI->getData64bitsDirective())
997 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000998 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000999 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
1000 << "\t" << TAI->getCommentString()
1001 << " Double-word most significant word " << Val << "\n";
1002 O << TAI->getData32bitsDirective() << unsigned(Val)
1003 << "\t" << TAI->getCommentString()
1004 << " Double-word least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +00001005 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001006 O << TAI->getData32bitsDirective() << unsigned(Val)
1007 << "\t" << TAI->getCommentString()
1008 << " Double-word least significant word " << Val << "\n";
1009 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
1010 << "\t" << TAI->getCommentString()
1011 << " Double-word most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +00001012 }
1013 return;
1014 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001015 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1016 const VectorType *PTy = CP->getType();
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001017
1018 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
Duncan Sands283207a2007-11-05 00:04:43 +00001019 EmitGlobalConstant(CP->getOperand(I), false);
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001020
1021 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +00001022 }
1023
1024 const Type *type = CV->getType();
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001025 printDataDirective(type);
Chris Lattnerbb644e32005-11-21 07:51:36 +00001026 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001027 O << "\n";
1028}
Chris Lattner061d9e22006-01-27 02:10:10 +00001029
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001030void
1031AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
1032 // Target doesn't support this yet!
1033 abort();
1034}
1035
Chris Lattnera32814b2006-09-26 23:59:50 +00001036/// PrintSpecial - Print information related to the specified machine instr
1037/// that is independent of the operand, and may be independent of the instr
1038/// itself. This can be useful for portably encoding the comment character
1039/// or other bits of target-specific knowledge into the asmstrings. The
1040/// syntax used is ${:comment}. Targets can override this to add support
1041/// for their own strange codes.
1042void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattner30b47082006-09-27 00:06:07 +00001043 if (!strcmp(Code, "private")) {
1044 O << TAI->getPrivateGlobalPrefix();
1045 } else if (!strcmp(Code, "comment")) {
Chris Lattnera32814b2006-09-26 23:59:50 +00001046 O << TAI->getCommentString();
1047 } else if (!strcmp(Code, "uid")) {
1048 // Assign a unique ID to this machine instruction.
1049 static const MachineInstr *LastMI = 0;
Chris Lattnera9428c42007-02-05 21:23:52 +00001050 static const Function *F = 0;
Chris Lattnera32814b2006-09-26 23:59:50 +00001051 static unsigned Counter = 0U-1;
Chris Lattnera9428c42007-02-05 21:23:52 +00001052
1053 // Comparing the address of MI isn't sufficient, because machineinstrs may
1054 // be allocated to the same address across functions.
1055 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1056
Chris Lattnera32814b2006-09-26 23:59:50 +00001057 // If this is a new machine instruction, bump the counter.
Chris Lattnera9428c42007-02-05 21:23:52 +00001058 if (LastMI != MI || F != ThisF) {
1059 ++Counter;
1060 LastMI = MI;
Chris Lattner89e66e02007-02-06 01:56:31 +00001061 F = ThisF;
Chris Lattnera9428c42007-02-05 21:23:52 +00001062 }
Chris Lattnera32814b2006-09-26 23:59:50 +00001063 O << Counter;
1064 } else {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001065 cerr << "Unknown special formatter '" << Code
1066 << "' for machine instr: " << *MI;
Chris Lattnera32814b2006-09-26 23:59:50 +00001067 exit(1);
1068 }
1069}
1070
1071
Chris Lattner061d9e22006-01-27 02:10:10 +00001072/// printInlineAsm - This method formats and prints the specified machine
1073/// instruction that is an inline asm.
1074void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +00001075 unsigned NumOperands = MI->getNumOperands();
1076
1077 // Count the number of register definitions.
1078 unsigned NumDefs = 0;
Dan Gohman9da02f52007-09-14 20:33:02 +00001079 for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef();
Chris Lattner45456d42006-09-05 20:02:51 +00001080 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +00001081 assert(NumDefs != NumOperands-1 && "No asm string?");
1082
1083 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001084
1085 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +00001086 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +00001087
Chris Lattner3390cbe2006-07-28 00:17:20 +00001088 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
1089 if (AsmStr[0] == 0) {
1090 O << "\n"; // Tab already printed, avoid double indenting next instr.
1091 return;
1092 }
1093
Jim Laskeya6211dc2006-09-06 18:34:40 +00001094 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +00001095
Bill Wendlinge21237e2007-01-16 03:42:04 +00001096 // The variant of the current asmprinter.
1097 int AsmPrinterVariant = TAI->getAssemblerDialect();
1098
Chris Lattneraa23fa92006-02-01 22:41:11 +00001099 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1100 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +00001101
Chris Lattneraa23fa92006-02-01 22:41:11 +00001102 while (*LastEmitted) {
1103 switch (*LastEmitted) {
1104 default: {
1105 // Not a special case, emit the string section literally.
1106 const char *LiteralEnd = LastEmitted+1;
1107 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +00001108 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +00001109 ++LiteralEnd;
1110 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1111 O.write(LastEmitted, LiteralEnd-LastEmitted);
1112 LastEmitted = LiteralEnd;
1113 break;
1114 }
Chris Lattnera633c3132006-05-05 21:47:05 +00001115 case '\n':
1116 ++LastEmitted; // Consume newline character.
Chris Lattnerb58f93f2007-04-30 17:00:18 +00001117 O << "\n"; // Indent code with newline.
Chris Lattnera633c3132006-05-05 21:47:05 +00001118 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001119 case '$': {
1120 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001121 bool Done = true;
1122
1123 // Handle escapes.
1124 switch (*LastEmitted) {
1125 default: Done = false; break;
1126 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +00001127 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1128 O << '$';
1129 ++LastEmitted; // Consume second '$' character.
1130 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001131 case '(': // $( -> same as GCC's { character.
1132 ++LastEmitted; // Consume '(' character.
1133 if (CurVariant != -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001134 cerr << "Nested variants found in inline asm string: '"
1135 << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001136 exit(1);
1137 }
1138 CurVariant = 0; // We're in the first variant now.
1139 break;
1140 case '|':
1141 ++LastEmitted; // consume '|' character.
1142 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001143 cerr << "Found '|' character outside of variant in inline asm "
1144 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001145 exit(1);
1146 }
1147 ++CurVariant; // We're in the next variant.
1148 break;
1149 case ')': // $) -> same as GCC's } char.
1150 ++LastEmitted; // consume ')' character.
1151 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001152 cerr << "Found '}' character outside of variant in inline asm "
1153 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001154 exit(1);
1155 }
1156 CurVariant = -1;
1157 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001158 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001159 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001160
1161 bool HasCurlyBraces = false;
1162 if (*LastEmitted == '{') { // ${variable}
1163 ++LastEmitted; // Consume '{' character.
1164 HasCurlyBraces = true;
1165 }
1166
1167 const char *IDStart = LastEmitted;
1168 char *IDEnd;
Chris Lattnerd39e3882007-01-23 00:36:17 +00001169 errno = 0;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001170 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1171 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001172 cerr << "Bad $ operand number in inline asm string: '"
1173 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001174 exit(1);
1175 }
1176 LastEmitted = IDEnd;
1177
Chris Lattner34f74c12006-02-06 22:17:23 +00001178 char Modifier[2] = { 0, 0 };
1179
Chris Lattneraa23fa92006-02-01 22:41:11 +00001180 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +00001181 // If we have curly braces, check for a modifier character. This
1182 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1183 if (*LastEmitted == ':') {
1184 ++LastEmitted; // Consume ':' character.
1185 if (*LastEmitted == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001186 cerr << "Bad ${:} expression in inline asm string: '"
1187 << AsmStr << "'\n";
Chris Lattner34f74c12006-02-06 22:17:23 +00001188 exit(1);
1189 }
1190
1191 Modifier[0] = *LastEmitted;
1192 ++LastEmitted; // Consume modifier character.
1193 }
1194
Chris Lattneraa23fa92006-02-01 22:41:11 +00001195 if (*LastEmitted != '}') {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001196 cerr << "Bad ${} expression in inline asm string: '"
1197 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001198 exit(1);
1199 }
1200 ++LastEmitted; // Consume '}' character.
1201 }
1202
1203 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001204 cerr << "Invalid $ operand number in inline asm string: '"
1205 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001206 exit(1);
1207 }
1208
Chris Lattner571d9642006-02-23 19:21:04 +00001209 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +00001210 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +00001211 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1212 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001213
1214 bool Error = false;
1215
Chris Lattner571d9642006-02-23 19:21:04 +00001216 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +00001217 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001218 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner81798412007-12-30 20:50:28 +00001219 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner5af3fde2006-02-24 19:50:58 +00001220 OpNo += (OpFlags >> 3) + 1;
1221 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001222
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001223 if (OpNo >= MI->getNumOperands()) {
1224 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +00001225 } else {
Chris Lattner81798412007-12-30 20:50:28 +00001226 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001227 ++OpNo; // Skip over the ID number.
1228
Dale Johannesen4646aa32007-11-05 21:20:28 +00001229 if (Modifier[0]=='l') // labels are target independent
Chris Lattnera5bb3702007-12-30 23:10:15 +00001230 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Dale Johannesen4646aa32007-11-05 21:20:28 +00001231 false, false);
1232 else {
1233 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1234 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1235 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1236 Modifier[0] ? Modifier : 0);
1237 } else {
1238 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1239 Modifier[0] ? Modifier : 0);
1240 }
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001241 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001242 }
1243 if (Error) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001244 cerr << "Invalid operand found in inline asm: '"
1245 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001246 MI->dump();
1247 exit(1);
1248 }
Chris Lattner571d9642006-02-23 19:21:04 +00001249 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001250 break;
1251 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001252 }
1253 }
Jim Laskeya6211dc2006-09-06 18:34:40 +00001254 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001255}
1256
Jim Laskeyf9e54452007-01-26 14:34:52 +00001257/// printLabel - This method prints a local label used by debug and
1258/// exception handling tables.
1259void AsmPrinter::printLabel(const MachineInstr *MI) const {
Chris Lattner81798412007-12-30 20:50:28 +00001260 O << "\n" << TAI->getPrivateGlobalPrefix()
1261 << "label" << MI->getOperand(0).getImm() << ":\n";
Jim Laskeyf9e54452007-01-26 14:34:52 +00001262}
1263
Chris Lattneraa23fa92006-02-01 22:41:11 +00001264/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1265/// instruction, using the specified assembler variant. Targets should
1266/// overried this to format as appropriate.
1267bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +00001268 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +00001269 // Target doesn't support this yet!
1270 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +00001271}
Chris Lattner1d08c652006-02-24 20:21:58 +00001272
1273bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1274 unsigned AsmVariant,
1275 const char *ExtraCode) {
1276 // Target doesn't support this yet!
1277 return true;
1278}
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001279
1280/// printBasicBlockLabel - This method prints the label for the specified
1281/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +00001282void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
1283 bool printColon,
1284 bool printComment) const {
Evan Chengcdf36092007-10-14 05:57:21 +00001285 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << "_"
1286 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +00001287 if (printColon)
1288 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +00001289 if (printComment && MBB->getBasicBlock())
Dan Gohman33d0ea22007-07-30 15:06:25 +00001290 O << '\t' << TAI->getCommentString() << ' '
1291 << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001292}
Nate Begeman984c1a42006-08-12 21:29:52 +00001293
Evan Cheng797d56f2007-11-09 01:32:10 +00001294/// printPICJumpTableSetLabel - This method prints a set label for the
1295/// specified MachineBasicBlock for a jumptable entry.
1296void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1297 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001298 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +00001299 return;
1300
Jim Laskeya6211dc2006-09-06 18:34:40 +00001301 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001302 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman984c1a42006-08-12 21:29:52 +00001303 printBasicBlockLabel(MBB, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001304 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1305 << '_' << uid << '\n';
Nate Begeman984c1a42006-08-12 21:29:52 +00001306}
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001307
Evan Cheng797d56f2007-11-09 01:32:10 +00001308void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1309 const MachineBasicBlock *MBB) const {
Evan Cheng91f120f2006-11-01 09:23:08 +00001310 if (!TAI->getSetDirective())
1311 return;
1312
1313 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001314 << getFunctionNumber() << '_' << uid << '_' << uid2
1315 << "_set_" << MBB->getNumber() << ',';
Evan Cheng91f120f2006-11-01 09:23:08 +00001316 printBasicBlockLabel(MBB, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001317 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1318 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng91f120f2006-11-01 09:23:08 +00001319}
1320
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001321/// printDataDirective - This method prints the asm directive for the
1322/// specified type.
1323void AsmPrinter::printDataDirective(const Type *type) {
1324 const TargetData *TD = TM.getTargetData();
1325 switch (type->getTypeID()) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001326 case Type::IntegerTyID: {
1327 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1328 if (BitWidth <= 8)
1329 O << TAI->getData8bitsDirective();
1330 else if (BitWidth <= 16)
1331 O << TAI->getData16bitsDirective();
1332 else if (BitWidth <= 32)
1333 O << TAI->getData32bitsDirective();
1334 else if (BitWidth <= 64) {
1335 assert(TAI->getData64bitsDirective() &&
1336 "Target cannot handle 64-bit constant exprs!");
1337 O << TAI->getData64bitsDirective();
1338 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001339 break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001340 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001341 case Type::PointerTyID:
1342 if (TD->getPointerSize() == 8) {
1343 assert(TAI->getData64bitsDirective() &&
1344 "Target cannot handle 64-bit pointer exprs!");
1345 O << TAI->getData64bitsDirective();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001346 } else {
1347 O << TAI->getData32bitsDirective();
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001348 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001349 break;
1350 case Type::FloatTyID: case Type::DoubleTyID:
Dale Johannesen6bf69ed2007-09-28 18:06:58 +00001351 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001352 assert (0 && "Should have already output floating point constant.");
1353 default:
1354 assert (0 && "Can't handle printing this type of thing");
1355 break;
1356 }
1357}
Dale Johannesen915e1542007-02-16 01:54:53 +00001358