blob: 8e59550028da0e3a727f88779e22c7cb73192a2a [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"
Gordon Henriksend930f912008-08-17 18:44:35 +000019#include "llvm/CodeGen/GCMetadataPrinter.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000021#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greenef354b6e2009-08-10 16:38:07 +000022#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Pateldd25a9d2009-01-13 21:44:10 +000024#include "llvm/CodeGen/DwarfWriter.h"
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +000025#include "llvm/Analysis/DebugInfo.h"
Chris Lattner9d0e7622009-07-27 21:28:04 +000026#include "llvm/MC/MCContext.h"
David Greenede9cd442009-07-16 22:24:20 +000027#include "llvm/MC/MCInst.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000028#include "llvm/MC/MCSection.h"
29#include "llvm/MC/MCStreamer.h"
Evan Cheng5e5a63c2009-03-25 01:47:28 +000030#include "llvm/Support/CommandLine.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000031#include "llvm/Support/ErrorHandling.h"
David Greenede544782009-07-13 20:25:48 +000032#include "llvm/Support/FormattedStream.h"
David Greenef354b6e2009-08-10 16:38:07 +000033#include "llvm/Support/IOManip.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000034#include "llvm/Support/Mangler.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000035#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000036#include "llvm/Target/TargetData.h"
Chris Lattner90438232006-10-06 22:50:56 +000037#include "llvm/Target/TargetLowering.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000038#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Chengc963f6c2008-07-01 23:18:29 +000039#include "llvm/Target/TargetOptions.h"
Evan Cheng0e7b00d2008-03-15 00:03:38 +000040#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng797d56f2007-11-09 01:32:10 +000041#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner17f71652008-08-17 07:19:36 +000042#include "llvm/ADT/SmallString.h"
Owen Anderson93719642008-08-21 00:14:44 +000043#include "llvm/ADT/StringExtras.h"
Chris Lattneraa23fa92006-02-01 22:41:11 +000044#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000045using namespace llvm;
46
Evan Cheng5e5a63c2009-03-25 01:47:28 +000047static cl::opt<cl::boolOrDefault>
48AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
49 cl::init(cl::BOU_UNSET));
50
David Greenef354b6e2009-08-10 16:38:07 +000051static cl::opt<cl::boolOrDefault>
52AsmExuberant("asm-exuberant", cl::desc("Add many comments."),
53 cl::init(cl::BOU_FALSE));
54
Devang Patel8c78a0b2007-05-03 01:11:54 +000055char AsmPrinter::ID = 0;
David Greenea31f96c2009-07-14 20:18:05 +000056AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Daniel Dunbar75c12e12009-07-01 01:48:54 +000057 const TargetAsmInfo *T, bool VDef)
58 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Evan Cheng0e7b00d2008-03-15 00:03:38 +000059 TM(tm), TAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner9d0e7622009-07-27 21:28:04 +000060
61 OutContext(*new MCContext()),
62 OutStreamer(*createAsmStreamer(OutContext, O)),
63
Chris Lattner8ce12532009-08-03 23:20:21 +000064 LastMI(0), LastFn(0), Counter(~0U),
Owen Andersone3849522009-06-25 16:55:32 +000065 PrevDLT(0, ~0U, ~0U) {
Chris Lattner8ce12532009-08-03 23:20:21 +000066 CurrentSection = 0;
Chris Lattner1fd58882009-06-24 19:09:55 +000067 DW = 0; MMI = 0;
Evan Cheng5e5a63c2009-03-25 01:47:28 +000068 switch (AsmVerbose) {
69 case cl::BOU_UNSET: VerboseAsm = VDef; break;
70 case cl::BOU_TRUE: VerboseAsm = true; break;
71 case cl::BOU_FALSE: VerboseAsm = false; break;
72 }
David Greenef354b6e2009-08-10 16:38:07 +000073 switch (AsmExuberant) {
74 case cl::BOU_UNSET: ExuberantAsm = false; break;
75 case cl::BOU_TRUE: ExuberantAsm = true; break;
76 case cl::BOU_FALSE: ExuberantAsm = false; break;
77 }
Evan Cheng5e5a63c2009-03-25 01:47:28 +000078}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000079
Gordon Henriksendbe06d32008-08-17 12:08:44 +000080AsmPrinter::~AsmPrinter() {
81 for (gcp_iterator I = GCMetadataPrinters.begin(),
82 E = GCMetadataPrinters.end(); I != E; ++I)
83 delete I->second;
Chris Lattner9d0e7622009-07-27 21:28:04 +000084
85 delete &OutStreamer;
86 delete &OutContext;
Gordon Henriksendbe06d32008-08-17 12:08:44 +000087}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000088
Chris Lattner9170f362009-08-03 19:12:26 +000089TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattner5e693ed2009-07-28 03:13:23 +000090 return TM.getTargetLowering()->getObjFileLowering();
91}
92
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +000093/// SwitchToSection - Switch to the specified section of the executable if we
Chris Lattnerc8565212009-08-03 18:04:42 +000094/// are not already in it! If "NS" is null, then this causes us to exit the
95/// current section and not reenter another one. This is generally used for
96/// asmprinter hacks.
97///
98/// FIXME: Remove support for null sections.
99///
Chris Lattner4d2c0f92009-07-31 18:48:30 +0000100void AsmPrinter::SwitchToSection(const MCSection *NS) {
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +0000101 // If we're already in this section, we're done.
Chris Lattner8ce12532009-08-03 23:20:21 +0000102 if (CurrentSection == NS) return;
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +0000103
Chris Lattner8ce12532009-08-03 23:20:21 +0000104 CurrentSection = NS;
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +0000105
Chris Lattner39fb5462009-08-05 20:49:52 +0000106 if (NS == 0) return;
107
Chris Lattner1cb93962009-08-08 22:41:53 +0000108 NS->PrintSwitchToSection(*TAI, O);
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +0000109}
Chris Lattner8488ba22006-05-09 04:59:56 +0000110
Gordon Henriksen5180e852008-01-07 01:30:38 +0000111void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman04023152009-07-31 23:37:33 +0000112 AU.setPreservesAll();
Gordon Henriksen5180e852008-01-07 01:30:38 +0000113 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksend930f912008-08-17 18:44:35 +0000114 AU.addRequired<GCModuleInfo>();
David Greenef354b6e2009-08-10 16:38:07 +0000115 if (ExuberantAsm) {
116 AU.addRequired<MachineLoopInfo>();
117 }
Gordon Henriksen5180e852008-01-07 01:30:38 +0000118}
119
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000120bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner51d5b432009-07-31 17:42:42 +0000121 // Initialize TargetLoweringObjectFile.
122 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
123 .Initialize(OutContext, TM);
124
Bill Wendlingdde1a8e2009-07-20 21:30:28 +0000125 Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix(),
Chris Lattner1177cee2009-07-21 17:30:51 +0000126 TAI->getLinkerPrivateGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +0000127
Chris Lattnerb8476452009-06-18 23:41:35 +0000128 if (TAI->doesAllowQuotesInName())
129 Mang->setUseQuotes(true);
130
Duncan Sands5a913d62009-01-28 13:14:17 +0000131 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksend930f912008-08-17 18:44:35 +0000132 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindolacda011b2008-12-03 11:01:37 +0000133
134 if (TAI->hasSingleParameterDotFile()) {
135 /* Very minimal debug info. It is ignored if we emit actual
136 debug info. If we don't, this at helps the user find where
137 a function came from. */
138 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
139 }
140
Gordon Henriksend930f912008-08-17 18:44:35 +0000141 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
142 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
143 MP->beginAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000144
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000145 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000146 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000147 << M.getModuleInlineAsm()
Dan Gohman6896901e2008-06-30 22:03:41 +0000148 << '\n' << TAI->getCommentString()
Jim Laskeya6211dc2006-09-06 18:34:40 +0000149 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000150
Chris Lattner00753fd2009-08-03 23:10:34 +0000151 SwitchToSection(0); // Reset back to no section to close off sections.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000152
Chris Lattner1fd58882009-06-24 19:09:55 +0000153 if (TAI->doesSupportDebugInformation() ||
154 TAI->doesSupportExceptionHandling()) {
155 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
156 if (MMI)
Devang Patel33f4eb42009-06-19 21:54:26 +0000157 MMI->AnalyzeModule(M);
Chris Lattner1fd58882009-06-24 19:09:55 +0000158 DW = getAnalysisIfAvailable<DwarfWriter>();
159 if (DW)
160 DW->BeginModule(&M, MMI, O, this, TAI);
Devang Patel33f4eb42009-06-19 21:54:26 +0000161 }
162
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000163 return false;
164}
165
166bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner100865e2009-07-21 18:38:57 +0000167 // Emit global variables.
168 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
169 I != E; ++I)
170 PrintGlobalVariable(I);
171
Chris Lattner70413122009-06-24 18:54:37 +0000172 // Emit final debug information.
173 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
174 DW->EndModule();
175
Chris Lattner2981dc12009-06-24 18:52:01 +0000176 // If the target wants to know about weak references, print them all.
Rafael Espindolad7998d02006-12-18 03:37:18 +0000177 if (TAI->getWeakRefDirective()) {
Chris Lattner2981dc12009-06-24 18:52:01 +0000178 // FIXME: This is not lazy, it would be nice to only print weak references
179 // to stuff that is actually used. Note that doing so would require targets
180 // to notice uses in operands (due to constant exprs etc). This should
181 // happen with the MC stuff eventually.
Chris Lattner00753fd2009-08-03 23:10:34 +0000182 SwitchToSection(0);
Rafael Espindolad7998d02006-12-18 03:37:18 +0000183
Chris Lattner2981dc12009-06-24 18:52:01 +0000184 // Print out module-level global variables here.
185 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
186 I != E; ++I) {
187 if (I->hasExternalWeakLinkage())
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000188 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner2981dc12009-06-24 18:52:01 +0000189 }
190
Chris Lattner00753fd2009-08-03 23:10:34 +0000191 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner2981dc12009-06-24 18:52:01 +0000192 if (I->hasExternalWeakLinkage())
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000193 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner2981dc12009-06-24 18:52:01 +0000194 }
Rafael Espindolad7998d02006-12-18 03:37:18 +0000195 }
196
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000197 if (TAI->getSetDirective()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000198 O << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000199 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner2981dc12009-06-24 18:52:01 +0000200 I != E; ++I) {
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000201 std::string Name = Mang->getMangledName(I);
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000202
203 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000204 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikovfd7faec2008-09-24 22:21:04 +0000205
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000206 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000207 O << "\t.globl\t" << Name << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000208 else if (I->hasWeakLinkage())
Dan Gohman6896901e2008-06-30 22:03:41 +0000209 O << TAI->getWeakRefDirective() << Name << '\n';
Rafael Espindola6de96a12009-01-15 20:18:42 +0000210 else if (!I->hasLocalLinkage())
Torok Edwinfbcc6632009-07-14 16:55:14 +0000211 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000212
Anton Korobeynikovfd7faec2008-09-24 22:21:04 +0000213 printVisibility(Name, I->getVisibility());
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000214
Dan Gohman6896901e2008-06-30 22:03:41 +0000215 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000216 }
217 }
218
Duncan Sands5a913d62009-01-28 13:14:17 +0000219 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksend930f912008-08-17 18:44:35 +0000220 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
221 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
222 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
223 MP->finishAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000224
Dan Gohmanbcde1722008-05-05 00:28:39 +0000225 // If we don't have any trampolines, then we don't require stack memory
226 // to be executable. Some targets have a directive to declare this.
Chris Lattner2981dc12009-06-24 18:52:01 +0000227 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmanbcde1722008-05-05 00:28:39 +0000228 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
229 if (TAI->getNonexecutableStackDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000230 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmanbcde1722008-05-05 00:28:39 +0000231
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000232 delete Mang; Mang = 0;
Chris Lattner1fd58882009-06-24 19:09:55 +0000233 DW = 0; MMI = 0;
Chris Lattner9d0e7622009-07-27 21:28:04 +0000234
235 OutStreamer.Finish();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000236 return false;
237}
238
Chris Lattnere79b2bc2009-07-17 20:46:40 +0000239std::string
240AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) const {
Bill Wendling067f1d82007-09-18 01:47:22 +0000241 assert(MF && "No machine function?");
Chris Lattnere79b2bc2009-07-17 20:46:40 +0000242 return Mang->getMangledName(MF->getFunction(), ".eh",
243 TAI->is_EHSymbolPrivate());
Bill Wendling067f1d82007-09-18 01:47:22 +0000244}
245
Chris Lattnerbb644e32005-11-21 07:51:36 +0000246void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000247 // What's my mangled name?
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000248 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Chengcdf36092007-10-14 05:57:21 +0000249 IncrementFunctionNumber();
David Greenef354b6e2009-08-10 16:38:07 +0000250
251 if (ExuberantAsm) {
252 LI = &getAnalysis<MachineLoopInfo>();
253 }
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000254}
255
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000256namespace {
257 // SectionCPs - Keep track the alignment, constpool entries per Section.
258 struct SectionCPs {
Chris Lattner4d2c0f92009-07-31 18:48:30 +0000259 const MCSection *S;
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000260 unsigned Alignment;
261 SmallVector<unsigned, 4> CPEs;
Chris Lattner4d2c0f92009-07-31 18:48:30 +0000262 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {};
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000263 };
264}
265
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000266/// EmitConstantPool - Print to the current output stream assembly
267/// representations of the constants in the constant pool MCP. This is
268/// used to print out constants which have been "spilled to memory" by
269/// the code generator.
270///
271void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000272 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000273 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000274
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000275 // Calculate sections for constant pool entries. We collect entries to go into
276 // the same section together to reduce amount of section switch statements.
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000277 SmallVector<SectionCPs, 4> CPSections;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000278 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattnerb300a4f2009-07-22 00:28:43 +0000279 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000280 unsigned Align = CPE.getAlignment();
Chris Lattnerfb6867c2009-07-26 06:26:55 +0000281
282 SectionKind Kind;
283 switch (CPE.getRelocationInfo()) {
284 default: llvm_unreachable("Unknown section kind");
Chris Lattnerf8d97102009-08-01 23:57:16 +0000285 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattnere45ff5c2009-07-26 07:00:12 +0000286 case 1:
Chris Lattnerf8d97102009-08-01 23:57:16 +0000287 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattnere45ff5c2009-07-26 07:00:12 +0000288 break;
Chris Lattnerfb6867c2009-07-26 06:26:55 +0000289 case 0:
Chris Lattnere45ff5c2009-07-26 07:00:12 +0000290 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattnerf8d97102009-08-01 23:57:16 +0000291 case 4: Kind = SectionKind::getMergeableConst4(); break;
292 case 8: Kind = SectionKind::getMergeableConst8(); break;
293 case 16: Kind = SectionKind::getMergeableConst16();break;
294 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattnere45ff5c2009-07-26 07:00:12 +0000295 }
Chris Lattnerfb6867c2009-07-26 06:26:55 +0000296 }
297
Chris Lattner0c402662009-08-01 23:46:12 +0000298 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattnerb300a4f2009-07-22 00:28:43 +0000299
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000300 // The number of sections are small, just do a linear search from the
301 // last section to the first.
302 bool Found = false;
303 unsigned SecIdx = CPSections.size();
304 while (SecIdx != 0) {
305 if (CPSections[--SecIdx].S == S) {
306 Found = true;
307 break;
308 }
309 }
310 if (!Found) {
311 SecIdx = CPSections.size();
312 CPSections.push_back(SectionCPs(S, Align));
313 }
314
315 if (Align > CPSections[SecIdx].Alignment)
316 CPSections[SecIdx].Alignment = Align;
317 CPSections[SecIdx].CPEs.push_back(i);
Evan Chengec1d60b2006-06-29 00:26:09 +0000318 }
319
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000320 // Now print stuff into the calculated sections.
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000321 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
322 SwitchToSection(CPSections[i].S);
323 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Chengec1d60b2006-06-29 00:26:09 +0000324
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000325 unsigned Offset = 0;
326 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
327 unsigned CPI = CPSections[i].CPEs[j];
328 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000329
Chris Lattnerf6190822006-02-09 04:46:04 +0000330 // Emit inter-object padding for alignment.
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000331 unsigned AlignMask = CPE.getAlignment() - 1;
332 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
333 EmitZeros(NewOffset - Offset);
334
335 const Type *Ty = CPE.getType();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000336 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000337
338 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
339 << CPI << ":\t\t\t\t\t";
340 if (VerboseAsm) {
341 O << TAI->getCommentString() << ' ';
342 WriteTypeSymbolic(O, CPE.getType(), 0);
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000343 }
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000344 O << '\n';
345 if (CPE.isMachineConstantPoolEntry())
346 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
347 else
348 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000349 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000350 }
351}
352
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000353/// EmitJumpTableInfo - Print assembly representations of the jump tables used
354/// by the current function to the current output stream.
355///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000356void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
357 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000358 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
359 if (JT.empty()) return;
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000360
Jim Laskey70323a82006-12-14 19:17:33 +0000361 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begemanefc312a2006-07-27 16:46:58 +0000362
Nate Begeman78756502006-07-27 01:13:04 +0000363 // Pick the directive to use to print the jump table entries, and switch to
364 // the appropriate section.
Jim Laskey70323a82006-12-14 19:17:33 +0000365 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000366
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000367 const Function *F = MF.getFunction();
Evan Chengde9e36a2009-06-18 20:37:15 +0000368 bool JTInDiffSection = false;
Chris Lattner0c402662009-08-01 23:46:12 +0000369 if (F->isWeakForLinker() ||
370 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskey70323a82006-12-14 19:17:33 +0000371 // In PIC mode, we need to emit the jump table to the same section as the
372 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000373 // We should also do if the section name is NULL or function is declared in
374 // discardable section.
Chris Lattner0c402662009-08-01 23:46:12 +0000375 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Nate Begeman78756502006-07-27 01:13:04 +0000376 } else {
Chris Lattner0c402662009-08-01 23:46:12 +0000377 // Otherwise, drop it in the readonly section.
378 const MCSection *ReadOnlySection =
Chris Lattnerf8d97102009-08-01 23:57:16 +0000379 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner0c402662009-08-01 23:46:12 +0000380 SwitchToSection(ReadOnlySection);
Evan Chengde9e36a2009-06-18 20:37:15 +0000381 JTInDiffSection = true;
Nate Begeman78756502006-07-27 01:13:04 +0000382 }
Jim Laskey70323a82006-12-14 19:17:33 +0000383
384 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000385
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000386 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000387 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner28bfe382006-10-28 18:10:06 +0000388
389 // If this jump table was deleted, ignore it.
390 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-08-12 21:29:52 +0000391
392 // For PIC codegen, if possible we want to use the SetDirective to reduce
393 // the number of relocations the assembler will generate for the jump table.
394 // Set directives are all printed before the jump table itself.
Evan Cheng797d56f2007-11-09 01:32:10 +0000395 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Jim Laskey70323a82006-12-14 19:17:33 +0000396 if (TAI->getSetDirective() && IsPic)
Nate Begeman984c1a42006-08-12 21:29:52 +0000397 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng797d56f2007-11-09 01:32:10 +0000398 if (EmittedSets.insert(JTBBs[ii]))
399 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman984c1a42006-08-12 21:29:52 +0000400
Chris Lattner0ee2d462007-01-18 01:12:56 +0000401 // On some targets (e.g. darwin) we want to emit two consequtive labels
402 // before each jump table. The first label is never referenced, but tells
403 // the assembler and linker the extents of the jump table object. The
404 // second label is actually referenced by the code.
Evan Chengde9e36a2009-06-18 20:37:15 +0000405 if (JTInDiffSection) {
406 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
407 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
408 }
Chris Lattner0ee2d462007-01-18 01:12:56 +0000409
Evan Chengcdf36092007-10-14 05:57:21 +0000410 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
411 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000412
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000413 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000414 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000415 O << '\n';
416 }
417 }
418}
419
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000420void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
421 const MachineBasicBlock *MBB,
422 unsigned uid) const {
423 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
424
425 // Use JumpTableDirective otherwise honor the entry size from the jump table
426 // info.
427 const char *JTEntryDirective = TAI->getJumpTableDirective();
428 bool HadJTEntryDirective = JTEntryDirective != NULL;
429 if (!HadJTEntryDirective) {
430 JTEntryDirective = MJTI->getEntrySize() == 4 ?
431 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
432 }
433
434 O << JTEntryDirective << ' ';
435
436 // If we have emitted set directives for the jump table entries, print
437 // them rather than the entries themselves. If we're emitting PIC, then
438 // emit the table entries as differences between two text section labels.
439 // If we're emitting non-PIC code, then emit the entries as direct
440 // references to the target basic blocks.
441 if (IsPic) {
442 if (TAI->getSetDirective()) {
443 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
444 << '_' << uid << "_set_" << MBB->getNumber();
445 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000446 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000447 // If the arch uses custom Jump Table directives, don't calc relative to
448 // JT
449 if (!HadJTEntryDirective)
450 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
451 << getFunctionNumber() << '_' << uid;
452 }
453 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000454 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000455 }
456}
457
458
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000459/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
460/// special global used by LLVM. If so, emit it and return true, otherwise
461/// do nothing and return false.
462bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000463 if (GV->getName() == "llvm.used") {
Andrew Lenharthbeb80a92007-08-22 19:33:11 +0000464 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
465 EmitLLVMUsedList(GV->getInitializer());
466 return true;
467 }
468
Chris Lattner58f9bb22009-07-20 06:14:25 +0000469 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner184f1be2009-04-13 05:44:34 +0000470 if (GV->getSection() == "llvm.metadata" ||
471 GV->hasAvailableExternallyLinkage())
472 return true;
Jim Laskey313570f2006-03-07 22:00:35 +0000473
474 if (!GV->hasAppendingLinkage()) return false;
475
476 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000477
Evan Cheng53ce7de2007-06-04 20:39:18 +0000478 const TargetData *TD = TM.getTargetData();
479 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner317293b2009-03-09 05:52:15 +0000480 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner4e7dfaf2009-08-02 00:34:36 +0000481 SwitchToSection(getObjFileLowering().getStaticCtorSection());
Chris Lattner126dab22009-03-09 08:18:48 +0000482 EmitAlignment(Align, 0);
483 EmitXXStructorList(GV->getInitializer());
484 return true;
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000485 }
486
Chris Lattner317293b2009-03-09 05:52:15 +0000487 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner4e7dfaf2009-08-02 00:34:36 +0000488 SwitchToSection(getObjFileLowering().getStaticDtorSection());
Chris Lattner126dab22009-03-09 08:18:48 +0000489 EmitAlignment(Align, 0);
490 EmitXXStructorList(GV->getInitializer());
491 return true;
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000492 }
493
494 return false;
495}
496
Chris Lattner66af3902006-09-26 03:38:18 +0000497/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
Dale Johannesenabb1e772008-09-09 22:29:13 +0000498/// global in the specified llvm.used list for which emitUsedDirectiveFor
499/// is true, as being used with this directive.
Chris Lattner66af3902006-09-26 03:38:18 +0000500void AsmPrinter::EmitLLVMUsedList(Constant *List) {
501 const char *Directive = TAI->getUsedDirective();
502
Dan Gohman4fe64de2009-06-14 23:30:43 +0000503 // Should be an array of 'i8*'.
Chris Lattner66af3902006-09-26 03:38:18 +0000504 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
505 if (InitList == 0) return;
506
507 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner1fcac9f2009-07-17 22:00:23 +0000508 const GlobalValue *GV =
509 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattnerd25701c2009-07-31 20:52:39 +0000510 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen5c1ff112008-09-03 20:34:58 +0000511 O << Directive;
512 EmitConstantValueOnly(InitList->getOperand(i));
513 O << '\n';
514 }
Chris Lattner66af3902006-09-26 03:38:18 +0000515 }
516}
517
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000518/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
519/// function pointers, ignoring the init priority.
520void AsmPrinter::EmitXXStructorList(Constant *List) {
521 // Should be an array of '{ int, void ()* }' structs. The first value is the
522 // init priority, which we ignore.
523 if (!isa<ConstantArray>(List)) return;
524 ConstantArray *InitList = cast<ConstantArray>(List);
525 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
526 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
527 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000528
529 if (CS->getOperand(1)->isNullValue())
530 return; // Found a null terminator, exit printing.
531 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000532 EmitGlobalConstant(CS->getOperand(1));
533 }
534}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000535
Jim Laskey71262542006-10-17 13:41:07 +0000536/// getGlobalLinkName - Returns the asm/link name of of the specified
537/// global variable. Should be overridden by each target asm printer to
538/// generate the appropriate value.
Bill Wendling992f8462009-04-10 00:12:49 +0000539const std::string &AsmPrinter::getGlobalLinkName(const GlobalVariable *GV,
540 std::string &LinkName) const {
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000541 if (isa<Function>(GV)) {
542 LinkName += TAI->getFunctionAddrPrefix();
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000543 LinkName += Mang->getMangledName(GV);
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000544 LinkName += TAI->getFunctionAddrSuffix();
545 } else {
546 LinkName += TAI->getGlobalVarAddrPrefix();
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000547 LinkName += Mang->getMangledName(GV);
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000548 LinkName += TAI->getGlobalVarAddrSuffix();
549 }
550
Jim Laskeyd24b9132006-10-17 17:17:24 +0000551 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000552}
553
Jim Laskey18fc0972007-02-21 22:47:38 +0000554/// EmitExternalGlobal - Emit the external reference to a global variable.
555/// Should be overridden if an indirect reference should be used.
556void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling992f8462009-04-10 00:12:49 +0000557 std::string GLN;
558 O << getGlobalLinkName(GV, GLN);
Jim Laskey18fc0972007-02-21 22:47:38 +0000559}
560
561
562
Jim Laskey1c055e82007-01-25 15:12:02 +0000563//===----------------------------------------------------------------------===//
564/// LEB 128 number encoding.
565
566/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
567/// representing an unsigned leb128 value.
568void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattner09487cd2008-11-10 04:35:24 +0000569 char Buffer[20];
Jim Laskey1c055e82007-01-25 15:12:02 +0000570 do {
Chris Lattner09487cd2008-11-10 04:35:24 +0000571 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskey1c055e82007-01-25 15:12:02 +0000572 Value >>= 7;
573 if (Value) Byte |= 0x80;
Chris Lattner09487cd2008-11-10 04:35:24 +0000574 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskey1c055e82007-01-25 15:12:02 +0000575 if (Value) O << ", ";
576 } while (Value);
577}
578
Jim Laskey1c055e82007-01-25 15:12:02 +0000579/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
580/// representing a signed leb128 value.
581void AsmPrinter::PrintSLEB128(int Value) const {
582 int Sign = Value >> (8 * sizeof(Value) - 1);
583 bool IsMore;
Chris Lattner09487cd2008-11-10 04:35:24 +0000584 char Buffer[20];
Anton Korobeynikovbd890b12008-08-16 12:57:46 +0000585
Jim Laskey1c055e82007-01-25 15:12:02 +0000586 do {
Chris Lattner09487cd2008-11-10 04:35:24 +0000587 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskey1c055e82007-01-25 15:12:02 +0000588 Value >>= 7;
589 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
590 if (IsMore) Byte |= 0x80;
Chris Lattner09487cd2008-11-10 04:35:24 +0000591 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskey1c055e82007-01-25 15:12:02 +0000592 if (IsMore) O << ", ";
593 } while (IsMore);
594}
595
Jim Laskey1c055e82007-01-25 15:12:02 +0000596//===--------------------------------------------------------------------===//
597// Emission and print routines
598//
599
600/// PrintHex - Print a value as a hexidecimal value.
601///
602void AsmPrinter::PrintHex(int Value) const {
Chris Lattner5505eed2008-11-10 04:30:26 +0000603 char Buffer[20];
Chris Lattner5505eed2008-11-10 04:30:26 +0000604 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Jim Laskey1c055e82007-01-25 15:12:02 +0000605}
606
607/// EOL - Print a newline character to asm stream. If a comment is present
608/// then it will be printed first. Comments should not contain '\n'.
Jim Laskey18fc0972007-02-21 22:47:38 +0000609void AsmPrinter::EOL() const {
Dan Gohman6896901e2008-06-30 22:03:41 +0000610 O << '\n';
Jim Laskey18fc0972007-02-21 22:47:38 +0000611}
Owen Anderson1d952532008-07-01 21:16:27 +0000612
Jim Laskey1c055e82007-01-25 15:12:02 +0000613void AsmPrinter::EOL(const std::string &Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000614 if (VerboseAsm && !Comment.empty()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000615 O << '\t'
Jim Laskey1c055e82007-01-25 15:12:02 +0000616 << TAI->getCommentString()
Dan Gohman6896901e2008-06-30 22:03:41 +0000617 << ' '
Jim Laskey1c055e82007-01-25 15:12:02 +0000618 << Comment;
619 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000620 O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000621}
622
Owen Anderson1d952532008-07-01 21:16:27 +0000623void AsmPrinter::EOL(const char* Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000624 if (VerboseAsm && *Comment) {
Owen Anderson1d952532008-07-01 21:16:27 +0000625 O << '\t'
626 << TAI->getCommentString()
627 << ' '
628 << Comment;
629 }
630 O << '\n';
631}
632
Jim Laskey1c055e82007-01-25 15:12:02 +0000633/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
634/// unsigned leb128 value.
635void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
636 if (TAI->hasLEB128()) {
637 O << "\t.uleb128\t"
638 << Value;
639 } else {
640 O << TAI->getData8bitsDirective();
641 PrintULEB128(Value);
642 }
643}
644
645/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
646/// signed leb128 value.
647void AsmPrinter::EmitSLEB128Bytes(int Value) const {
648 if (TAI->hasLEB128()) {
649 O << "\t.sleb128\t"
650 << Value;
651 } else {
652 O << TAI->getData8bitsDirective();
653 PrintSLEB128(Value);
654 }
655}
656
657/// EmitInt8 - Emit a byte directive and value.
658///
659void AsmPrinter::EmitInt8(int Value) const {
660 O << TAI->getData8bitsDirective();
661 PrintHex(Value & 0xFF);
662}
663
664/// EmitInt16 - Emit a short directive and value.
665///
666void AsmPrinter::EmitInt16(int Value) const {
667 O << TAI->getData16bitsDirective();
668 PrintHex(Value & 0xFFFF);
669}
670
671/// EmitInt32 - Emit a long directive and value.
672///
673void AsmPrinter::EmitInt32(int Value) const {
674 O << TAI->getData32bitsDirective();
675 PrintHex(Value);
676}
677
678/// EmitInt64 - Emit a long long directive and value.
679///
680void AsmPrinter::EmitInt64(uint64_t Value) const {
681 if (TAI->getData64bitsDirective()) {
682 O << TAI->getData64bitsDirective();
683 PrintHex(Value);
684 } else {
685 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000686 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000687 EmitInt32(unsigned(Value));
688 } else {
Dan Gohman6896901e2008-06-30 22:03:41 +0000689 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000690 EmitInt32(unsigned(Value >> 32));
691 }
692 }
693}
694
695/// toOctal - Convert the low order bits of X into an octal digit.
696///
697static inline char toOctal(int X) {
698 return (X&7)+'0';
699}
700
701/// printStringChar - Print a char, escaped if necessary.
702///
David Greenea31f96c2009-07-14 20:18:05 +0000703static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskey1c055e82007-01-25 15:12:02 +0000704 if (C == '"') {
705 O << "\\\"";
706 } else if (C == '\\') {
707 O << "\\\\";
Chris Lattner1f9053a2009-01-22 23:38:45 +0000708 } else if (isprint((unsigned char)C)) {
Jim Laskey1c055e82007-01-25 15:12:02 +0000709 O << C;
710 } else {
711 switch(C) {
712 case '\b': O << "\\b"; break;
713 case '\f': O << "\\f"; break;
714 case '\n': O << "\\n"; break;
715 case '\r': O << "\\r"; break;
716 case '\t': O << "\\t"; break;
717 default:
718 O << '\\';
719 O << toOctal(C >> 6);
720 O << toOctal(C >> 3);
721 O << toOctal(C >> 0);
722 break;
723 }
724 }
725}
726
727/// EmitString - Emit a string with quotes and a null terminator.
728/// Special characters are emitted properly.
729/// \literal (Eg. '\t') \endliteral
730void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendling16abfc92009-04-09 23:51:31 +0000731 EmitString(String.c_str(), String.size());
732}
733
734void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000735 const char* AscizDirective = TAI->getAscizDirective();
736 if (AscizDirective)
737 O << AscizDirective;
738 else
739 O << TAI->getAsciiDirective();
Dan Gohman6896901e2008-06-30 22:03:41 +0000740 O << '\"';
Bill Wendling16abfc92009-04-09 23:51:31 +0000741 for (unsigned i = 0; i < Size; ++i)
Chris Lattner69b586e2009-04-08 00:28:38 +0000742 printStringChar(O, String[i]);
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000743 if (AscizDirective)
Dan Gohman6896901e2008-06-30 22:03:41 +0000744 O << '\"';
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000745 else
746 O << "\\0\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000747}
748
749
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000750/// EmitFile - Emit a .file directive.
751void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
752 O << "\t.file\t" << Number << " \"";
Chris Lattner69b586e2009-04-08 00:28:38 +0000753 for (unsigned i = 0, N = Name.size(); i < N; ++i)
754 printStringChar(O, Name[i]);
Dan Gohman6896901e2008-06-30 22:03:41 +0000755 O << '\"';
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000756}
757
758
Jim Laskey1c055e82007-01-25 15:12:02 +0000759//===----------------------------------------------------------------------===//
760
Chris Lattner3e3ff302007-05-31 18:57:45 +0000761// EmitAlignment - Emit an alignment directive to the specified power of
762// two boundary. For example, if you pass in 3 here, you will get an 8
763// byte alignment. If a global value is specified, and if that global has
764// an explicit alignment requested, it will unconditionally override the
765// alignment request. However, if ForcedAlignBits is specified, this value
766// has final say: the ultimate alignment will be the max of ForcedAlignBits
767// and the alignment computed with NumBits and the global.
768//
769// The algorithm is:
770// Align = NumBits;
771// if (GV && GV->hasalignment) Align = GV->getalignment();
772// Align = std::max(Align, ForcedAlignBits);
773//
774void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng26edb592008-02-29 19:36:59 +0000775 unsigned ForcedAlignBits,
776 bool UseFillExpr) const {
Dale Johannesen8653d292007-04-23 23:33:31 +0000777 if (GV && GV->getAlignment())
Chris Lattner3e3ff302007-05-31 18:57:45 +0000778 NumBits = Log2_32(GV->getAlignment());
779 NumBits = std::max(NumBits, ForcedAlignBits);
780
Chris Lattner747960d2005-11-10 18:09:27 +0000781 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000782 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng86eb3fd2007-07-25 23:35:07 +0000783 O << TAI->getAlignDirective() << NumBits;
Evan Chengc7990652008-02-28 00:43:03 +0000784
Chris Lattner8ce12532009-08-03 23:20:21 +0000785 if (CurrentSection && CurrentSection->getKind().isText())
786 if (unsigned FillValue = TAI->getTextAlignFillValue()) {
787 O << ',';
788 PrintHex(FillValue);
789 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000790 O << '\n';
Chris Lattner1d35c162004-08-17 19:14:29 +0000791}
David Greenefdd25192009-08-05 21:00:52 +0000792
Chris Lattnerbb644e32005-11-21 07:51:36 +0000793/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000794///
Sanjiv Gupta964a29f2009-01-30 04:25:10 +0000795void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000796 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000797 if (TAI->getZeroDirective()) {
798 O << TAI->getZeroDirective() << NumZeros;
799 if (TAI->getZeroDirectiveSuffix())
800 O << TAI->getZeroDirectiveSuffix();
Dan Gohman6896901e2008-06-30 22:03:41 +0000801 O << '\n';
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000802 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000803 for (; NumZeros; --NumZeros)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +0000804 O << TAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000805 }
806 }
807}
808
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000809// Print out the specified constant, without a storage class. Only the
810// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000811void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000812 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman6896901e2008-06-30 22:03:41 +0000813 O << '0';
Zhou Sheng75b871f2007-01-11 12:24:14 +0000814 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel499c1192008-06-03 06:18:19 +0000815 O << CI->getZExtValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000816 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000817 // This is a constant address for a global variable or function. Use the
818 // name of the variable or function as the address value, possibly
819 // decorating it with GlobalVarAddrPrefix/Suffix or
820 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000821 if (isa<Function>(GV)) {
822 O << TAI->getFunctionAddrPrefix()
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000823 << Mang->getMangledName(GV)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000824 << TAI->getFunctionAddrSuffix();
825 } else {
826 O << TAI->getGlobalVarAddrPrefix()
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000827 << Mang->getMangledName(GV)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000828 << TAI->getGlobalVarAddrSuffix();
829 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000830 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000831 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000832 unsigned Opcode = CE->getOpcode();
833 switch (Opcode) {
Chris Lattnerb4b10122009-08-01 22:25:12 +0000834 case Instruction::Trunc:
835 case Instruction::ZExt:
836 case Instruction::SExt:
837 case Instruction::FPTrunc:
838 case Instruction::FPExt:
839 case Instruction::UIToFP:
840 case Instruction::SIToFP:
841 case Instruction::FPToUI:
842 case Instruction::FPToSI:
843 llvm_unreachable("FIXME: Don't support this constant cast expr");
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000844 case Instruction::GetElementPtr: {
845 // generate a symbolic expression for the byte address
846 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner83dfca82007-02-10 20:31:59 +0000847 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
848 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
849 idxVec.size())) {
Chris Lattner7e1d2862009-02-05 06:55:21 +0000850 // Truncate/sext the offset to the pointer size.
851 if (TD->getPointerSizeInBits() != 64) {
852 int SExtAmount = 64-TD->getPointerSizeInBits();
853 Offset = (Offset << SExtAmount) >> SExtAmount;
854 }
855
Chris Lattner145569b2005-02-14 21:40:26 +0000856 if (Offset)
Dan Gohman6896901e2008-06-30 22:03:41 +0000857 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000858 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000859 if (Offset > 0)
860 O << ") + " << Offset;
861 else if (Offset < 0)
862 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000863 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000864 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000865 }
866 break;
867 }
Chris Lattnerb9e41f52006-12-12 05:14:13 +0000868 case Instruction::BitCast:
869 return EmitConstantValueOnly(CE->getOperand(0));
870
Chris Lattner0c537da2006-12-12 05:18:19 +0000871 case Instruction::IntToPtr: {
872 // Handle casts to pointers by changing them into casts to the appropriate
873 // integer type. This promotes constant folding and simplifies this code.
874 Constant *Op = CE->getOperand(0);
875 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
876 return EmitConstantValueOnly(Op);
877 }
878
879
880 case Instruction::PtrToInt: {
Chris Lattner85492422006-07-29 01:57:19 +0000881 // Support only foldable casts to/from pointers that can be eliminated by
882 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000883 Constant *Op = CE->getOperand(0);
Chris Lattner0c537da2006-12-12 05:18:19 +0000884 const Type *Ty = CE->getType();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000885
Chris Lattner0c537da2006-12-12 05:18:19 +0000886 // We can emit the pointer value into this slot if the slot is an
887 // integer slot greater or equal to the size of the pointer.
Chris Lattnerb4b10122009-08-01 22:25:12 +0000888 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Chris Lattner85492422006-07-29 01:57:19 +0000889 return EmitConstantValueOnly(Op);
Nick Lewycky42a19b62008-08-08 06:34:07 +0000890
891 O << "((";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000892 EmitConstantValueOnly(Op);
Chris Lattnerb4b10122009-08-01 22:25:12 +0000893 APInt ptrMask =
894 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattner17f71652008-08-17 07:19:36 +0000895
896 SmallString<40> S;
897 ptrMask.toStringUnsigned(S);
898 O << ") & " << S.c_str() << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000899 break;
900 }
901 case Instruction::Add:
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000902 case Instruction::Sub:
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000903 case Instruction::And:
904 case Instruction::Or:
905 case Instruction::Xor:
Dan Gohman6896901e2008-06-30 22:03:41 +0000906 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000907 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman6896901e2008-06-30 22:03:41 +0000908 O << ')';
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000909 switch (Opcode) {
910 case Instruction::Add:
911 O << " + ";
912 break;
913 case Instruction::Sub:
914 O << " - ";
915 break;
916 case Instruction::And:
917 O << " & ";
918 break;
919 case Instruction::Or:
920 O << " | ";
921 break;
922 case Instruction::Xor:
923 O << " ^ ";
924 break;
925 default:
926 break;
927 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000928 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000929 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman6896901e2008-06-30 22:03:41 +0000930 O << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000931 break;
932 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000933 llvm_unreachable("Unsupported operator!");
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000934 }
935 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000936 llvm_unreachable("Unknown constant value!");
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000937 }
938}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000939
Chris Lattner55a6d902005-11-10 18:06:33 +0000940/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000941/// the predicate isString is true.
942///
David Greenea31f96c2009-07-14 20:18:05 +0000943static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner55a6d902005-11-10 18:06:33 +0000944 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000945 assert(CVA->isString() && "Array is not string compatible!");
946
Dan Gohman6896901e2008-06-30 22:03:41 +0000947 O << '\"';
Chris Lattner55a6d902005-11-10 18:06:33 +0000948 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000949 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000950 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskey1c055e82007-01-25 15:12:02 +0000951 printStringChar(O, C);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000952 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000953 O << '\"';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000954}
955
Jeff Cohen24a62a92006-05-02 01:16:28 +0000956/// EmitString - Emit a zero-byte-terminated string constant.
957///
958void AsmPrinter::EmitString(const ConstantArray *CVA) const {
959 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000960 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000961 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000962 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000963 printAsCString(O, CVA, NumElts-1);
964 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000965 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000966 printAsCString(O, CVA, NumElts);
967 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000968 O << '\n';
Jeff Cohen24a62a92006-05-02 01:16:28 +0000969}
970
Sanjiv Gupta99b1b272009-04-28 16:34:20 +0000971void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
972 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +0000973 if (CVA->isString()) {
974 EmitString(CVA);
975 } else { // Not a string. Print the values in successive locations
976 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Gupta99b1b272009-04-28 16:34:20 +0000977 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +0000978 }
979}
980
981void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
982 const VectorType *PTy = CP->getType();
983
984 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
985 EmitGlobalConstant(CP->getOperand(I));
986}
987
Sanjiv Gupta964a29f2009-01-30 04:25:10 +0000988void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
989 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +0000990 // Print the fields in successive locations. Pad to align if needed!
991 const TargetData *TD = TM.getTargetData();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000992 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman014caa42008-12-22 21:14:27 +0000993 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
994 uint64_t sizeSoFar = 0;
995 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
996 const Constant* field = CVS->getOperand(i);
997
998 // Check if padding is needed and insert one or more 0s.
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000999 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman014caa42008-12-22 21:14:27 +00001000 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1001 - cvsLayout->getElementOffset(i)) - fieldSize;
1002 sizeSoFar += fieldSize + padSize;
1003
1004 // Now print the actual field value.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001005 EmitGlobalConstant(field, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001006
1007 // Insert padding - this may include padding to increase the size of the
1008 // current field up to the ABI size (if the struct is not packed) as well
1009 // as padding to ensure that the next field starts at the right offset.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001010 EmitZeros(padSize, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001011 }
1012 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1013 "Layout of constant struct may be incorrect!");
1014}
1015
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001016void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1017 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001018 // FP Constants are printed as integer constants to avoid losing
1019 // precision...
1020 const TargetData *TD = TM.getTargetData();
1021 if (CFP->getType() == Type::DoubleTy) {
1022 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1023 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Evan Chenga774a992009-03-24 00:17:40 +00001024 if (TAI->getData64bitsDirective(AddrSpace)) {
1025 O << TAI->getData64bitsDirective(AddrSpace) << i;
1026 if (VerboseAsm)
1027 O << '\t' << TAI->getCommentString() << " double value: " << Val;
1028 O << '\n';
1029 } else if (TD->isBigEndian()) {
1030 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1031 if (VerboseAsm)
1032 O << '\t' << TAI->getCommentString()
1033 << " double most significant word " << Val;
1034 O << '\n';
1035 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1036 if (VerboseAsm)
1037 O << '\t' << TAI->getCommentString()
1038 << " double least significant word " << Val;
1039 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001040 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001041 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1042 if (VerboseAsm)
1043 O << '\t' << TAI->getCommentString()
1044 << " double least significant word " << Val;
1045 O << '\n';
1046 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1047 if (VerboseAsm)
1048 O << '\t' << TAI->getCommentString()
1049 << " double most significant word " << Val;
1050 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001051 }
1052 return;
1053 } else if (CFP->getType() == Type::FloatTy) {
1054 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001055 O << TAI->getData32bitsDirective(AddrSpace)
Evan Chenga774a992009-03-24 00:17:40 +00001056 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1057 if (VerboseAsm)
1058 O << '\t' << TAI->getCommentString() << " float " << Val;
1059 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001060 return;
1061 } else if (CFP->getType() == Type::X86_FP80Ty) {
1062 // all long double variants are printed as hex
1063 // api needed to prevent premature destruction
1064 APInt api = CFP->getValueAPF().bitcastToAPInt();
1065 const uint64_t *p = api.getRawData();
1066 // Convert to double so we can print the approximate val as a comment.
1067 APFloat DoubleVal = CFP->getValueAPF();
1068 bool ignored;
1069 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1070 &ignored);
1071 if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001072 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1073 if (VerboseAsm)
1074 O << '\t' << TAI->getCommentString()
1075 << " long double most significant halfword of ~"
1076 << DoubleVal.convertToDouble();
1077 O << '\n';
1078 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1079 if (VerboseAsm)
1080 O << '\t' << TAI->getCommentString() << " long double next halfword";
1081 O << '\n';
1082 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1083 if (VerboseAsm)
1084 O << '\t' << TAI->getCommentString() << " long double next halfword";
1085 O << '\n';
1086 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1087 if (VerboseAsm)
1088 O << '\t' << TAI->getCommentString() << " long double next halfword";
1089 O << '\n';
1090 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1091 if (VerboseAsm)
1092 O << '\t' << TAI->getCommentString()
1093 << " long double least significant halfword";
1094 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001095 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001096 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1097 if (VerboseAsm)
1098 O << '\t' << TAI->getCommentString()
1099 << " long double least significant halfword of ~"
1100 << DoubleVal.convertToDouble();
1101 O << '\n';
1102 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1103 if (VerboseAsm)
1104 O << '\t' << TAI->getCommentString()
1105 << " long double next halfword";
1106 O << '\n';
1107 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1108 if (VerboseAsm)
1109 O << '\t' << TAI->getCommentString()
1110 << " long double next halfword";
1111 O << '\n';
1112 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1113 if (VerboseAsm)
1114 O << '\t' << TAI->getCommentString()
1115 << " long double next halfword";
1116 O << '\n';
1117 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1118 if (VerboseAsm)
1119 O << '\t' << TAI->getCommentString()
1120 << " long double most significant halfword";
1121 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001122 }
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001123 EmitZeros(TD->getTypeAllocSize(Type::X86_FP80Ty) -
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001124 TD->getTypeStoreSize(Type::X86_FP80Ty), AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001125 return;
1126 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1127 // all long double variants are printed as hex
1128 // api needed to prevent premature destruction
1129 APInt api = CFP->getValueAPF().bitcastToAPInt();
1130 const uint64_t *p = api.getRawData();
1131 if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001132 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1133 if (VerboseAsm)
1134 O << '\t' << TAI->getCommentString()
1135 << " long double most significant word";
1136 O << '\n';
1137 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1138 if (VerboseAsm)
1139 O << '\t' << TAI->getCommentString()
1140 << " long double next word";
1141 O << '\n';
1142 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1143 if (VerboseAsm)
1144 O << '\t' << TAI->getCommentString()
1145 << " long double next word";
1146 O << '\n';
1147 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1148 if (VerboseAsm)
1149 O << '\t' << TAI->getCommentString()
1150 << " long double least significant word";
1151 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001152 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001153 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1154 if (VerboseAsm)
1155 O << '\t' << TAI->getCommentString()
1156 << " long double least significant word";
1157 O << '\n';
1158 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1159 if (VerboseAsm)
1160 O << '\t' << TAI->getCommentString()
1161 << " long double next word";
1162 O << '\n';
1163 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1164 if (VerboseAsm)
1165 O << '\t' << TAI->getCommentString()
1166 << " long double next word";
1167 O << '\n';
1168 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1169 if (VerboseAsm)
1170 O << '\t' << TAI->getCommentString()
1171 << " long double most significant word";
1172 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001173 }
1174 return;
Torok Edwinfbcc6632009-07-14 16:55:14 +00001175 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman014caa42008-12-22 21:14:27 +00001176}
1177
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001178void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1179 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001180 const TargetData *TD = TM.getTargetData();
1181 unsigned BitWidth = CI->getBitWidth();
1182 assert(isPowerOf2_32(BitWidth) &&
1183 "Non-power-of-2-sized integers not handled!");
1184
1185 // We don't expect assemblers to support integer data directives
1186 // for more than 64 bits, so we emit the data in at most 64-bit
1187 // quantities at a time.
1188 const uint64_t *RawData = CI->getValue().getRawData();
1189 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1190 uint64_t Val;
1191 if (TD->isBigEndian())
1192 Val = RawData[e - i - 1];
1193 else
1194 Val = RawData[i];
1195
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001196 if (TAI->getData64bitsDirective(AddrSpace))
1197 O << TAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001198 else if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001199 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1200 if (VerboseAsm)
1201 O << '\t' << TAI->getCommentString()
1202 << " Double-word most significant word " << Val;
1203 O << '\n';
1204 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1205 if (VerboseAsm)
1206 O << '\t' << TAI->getCommentString()
1207 << " Double-word least significant word " << Val;
1208 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001209 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001210 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1211 if (VerboseAsm)
1212 O << '\t' << TAI->getCommentString()
1213 << " Double-word least significant word " << Val;
1214 O << '\n';
1215 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1216 if (VerboseAsm)
1217 O << '\t' << TAI->getCommentString()
1218 << " Double-word most significant word " << Val;
1219 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001220 }
1221 }
1222}
1223
Chris Lattnerbb644e32005-11-21 07:51:36 +00001224/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001225void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Anderson20a631f2006-05-03 01:29:57 +00001226 const TargetData *TD = TM.getTargetData();
Dan Gohman014caa42008-12-22 21:14:27 +00001227 const Type *type = CV->getType();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001228 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001229
Chris Lattner61753bf2004-10-16 18:19:26 +00001230 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001231 EmitZeros(Size, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001232 return;
1233 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Gupta99b1b272009-04-28 16:34:20 +00001234 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001235 return;
1236 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001237 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001238 return;
1239 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001240 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001241 return;
1242 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1243 // Small integers are handled below; large integers are handled here.
1244 if (Size > 4) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001245 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001246 return;
1247 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001248 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman014caa42008-12-22 21:14:27 +00001249 EmitGlobalConstantVector(CP);
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001250 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +00001251 }
1252
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001253 printDataDirective(type, AddrSpace);
Chris Lattnerbb644e32005-11-21 07:51:36 +00001254 EmitConstantValueOnly(CV);
Evan Chenga774a992009-03-24 00:17:40 +00001255 if (VerboseAsm) {
1256 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1257 SmallString<40> S;
1258 CI->getValue().toStringUnsigned(S, 16);
1259 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
1260 }
Scott Michelc0e9ff62008-06-03 15:39:51 +00001261 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001262 O << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001263}
Chris Lattner061d9e22006-01-27 02:10:10 +00001264
Chris Lattner17f71652008-08-17 07:19:36 +00001265void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001266 // Target doesn't support this yet!
Torok Edwinfbcc6632009-07-14 16:55:14 +00001267 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001268}
1269
Chris Lattnera32814b2006-09-26 23:59:50 +00001270/// PrintSpecial - Print information related to the specified machine instr
1271/// that is independent of the operand, and may be independent of the instr
1272/// itself. This can be useful for portably encoding the comment character
1273/// or other bits of target-specific knowledge into the asmstrings. The
1274/// syntax used is ${:comment}. Targets can override this to add support
1275/// for their own strange codes.
Chris Lattner1522e242009-03-10 05:37:13 +00001276void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattner30b47082006-09-27 00:06:07 +00001277 if (!strcmp(Code, "private")) {
1278 O << TAI->getPrivateGlobalPrefix();
1279 } else if (!strcmp(Code, "comment")) {
Evan Chenga774a992009-03-24 00:17:40 +00001280 if (VerboseAsm)
1281 O << TAI->getCommentString();
Chris Lattnera32814b2006-09-26 23:59:50 +00001282 } else if (!strcmp(Code, "uid")) {
Chris Lattnera9428c42007-02-05 21:23:52 +00001283 // Comparing the address of MI isn't sufficient, because machineinstrs may
1284 // be allocated to the same address across functions.
1285 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1286
Owen Andersonae471cf2009-06-24 22:28:12 +00001287 // If this is a new LastFn instruction, bump the counter.
1288 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnera9428c42007-02-05 21:23:52 +00001289 ++Counter;
1290 LastMI = MI;
Owen Andersonae471cf2009-06-24 22:28:12 +00001291 LastFn = ThisF;
Chris Lattnera9428c42007-02-05 21:23:52 +00001292 }
Chris Lattnera32814b2006-09-26 23:59:50 +00001293 O << Counter;
1294 } else {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001295 std::string msg;
1296 raw_string_ostream Msg(msg);
1297 Msg << "Unknown special formatter '" << Code
Bill Wendlingf3baad32006-12-07 01:30:32 +00001298 << "' for machine instr: " << *MI;
Torok Edwinccb29cd2009-07-11 13:10:19 +00001299 llvm_report_error(Msg.str());
Chris Lattnera32814b2006-09-26 23:59:50 +00001300 }
1301}
1302
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +00001303/// processDebugLoc - Processes the debug information of each machine
1304/// instruction's DebugLoc.
1305void AsmPrinter::processDebugLoc(DebugLoc DL) {
Chris Lattner3e5b2722009-08-05 04:09:18 +00001306 if (!TAI || !DW)
1307 return;
1308
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +00001309 if (TAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
1310 if (!DL.isUnknown()) {
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +00001311 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1312
1313 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT)
1314 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1315 DICompileUnit(CurDLT.CompileUnit)));
1316
1317 PrevDLT = CurDLT;
1318 }
1319 }
1320}
Chris Lattnera32814b2006-09-26 23:59:50 +00001321
Chris Lattner061d9e22006-01-27 02:10:10 +00001322/// printInlineAsm - This method formats and prints the specified machine
1323/// instruction that is an inline asm.
1324void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +00001325 unsigned NumOperands = MI->getNumOperands();
1326
1327 // Count the number of register definitions.
1328 unsigned NumDefs = 0;
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001329 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner45456d42006-09-05 20:02:51 +00001330 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +00001331 assert(NumDefs != NumOperands-1 && "No asm string?");
1332
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001333 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001334
1335 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +00001336 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +00001337
Dale Johannesen2b3bc302008-01-29 02:21:21 +00001338 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1339 // These are useful to see where empty asm's wound up.
Chris Lattner3390cbe2006-07-28 00:17:20 +00001340 if (AsmStr[0] == 0) {
Dan Gohman6896901e2008-06-30 22:03:41 +00001341 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattner3390cbe2006-07-28 00:17:20 +00001342 return;
1343 }
1344
Jim Laskeya6211dc2006-09-06 18:34:40 +00001345 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +00001346
Bill Wendlinge21237e2007-01-16 03:42:04 +00001347 // The variant of the current asmprinter.
1348 int AsmPrinterVariant = TAI->getAssemblerDialect();
1349
Chris Lattneraa23fa92006-02-01 22:41:11 +00001350 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1351 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +00001352
Chris Lattneraa23fa92006-02-01 22:41:11 +00001353 while (*LastEmitted) {
1354 switch (*LastEmitted) {
1355 default: {
1356 // Not a special case, emit the string section literally.
1357 const char *LiteralEnd = LastEmitted+1;
1358 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +00001359 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +00001360 ++LiteralEnd;
1361 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1362 O.write(LastEmitted, LiteralEnd-LastEmitted);
1363 LastEmitted = LiteralEnd;
1364 break;
1365 }
Chris Lattnera633c3132006-05-05 21:47:05 +00001366 case '\n':
1367 ++LastEmitted; // Consume newline character.
Dan Gohman6896901e2008-06-30 22:03:41 +00001368 O << '\n'; // Indent code with newline.
Chris Lattnera633c3132006-05-05 21:47:05 +00001369 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001370 case '$': {
1371 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001372 bool Done = true;
1373
1374 // Handle escapes.
1375 switch (*LastEmitted) {
1376 default: Done = false; break;
1377 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +00001378 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1379 O << '$';
1380 ++LastEmitted; // Consume second '$' character.
1381 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001382 case '(': // $( -> same as GCC's { character.
1383 ++LastEmitted; // Consume '(' character.
1384 if (CurVariant != -1) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001385 llvm_report_error("Nested variants found in inline asm string: '"
1386 + std::string(AsmStr) + "'");
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001387 }
1388 CurVariant = 0; // We're in the first variant now.
1389 break;
1390 case '|':
1391 ++LastEmitted; // consume '|' character.
Dale Johannesen4f279162008-10-10 21:04:42 +00001392 if (CurVariant == -1)
1393 O << '|'; // this is gcc's behavior for | outside a variant
1394 else
1395 ++CurVariant; // We're in the next variant.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001396 break;
1397 case ')': // $) -> same as GCC's } char.
1398 ++LastEmitted; // consume ')' character.
Dale Johannesen4f279162008-10-10 21:04:42 +00001399 if (CurVariant == -1)
1400 O << '}'; // this is gcc's behavior for } outside a variant
1401 else
1402 CurVariant = -1;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001403 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001404 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001405 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001406
1407 bool HasCurlyBraces = false;
1408 if (*LastEmitted == '{') { // ${variable}
1409 ++LastEmitted; // Consume '{' character.
1410 HasCurlyBraces = true;
1411 }
1412
Chris Lattner1522e242009-03-10 05:37:13 +00001413 // If we have ${:foo}, then this is not a real operand reference, it is a
1414 // "magic" string reference, just like in .td files. Arrange to call
1415 // PrintSpecial.
1416 if (HasCurlyBraces && *LastEmitted == ':') {
1417 ++LastEmitted;
1418 const char *StrStart = LastEmitted;
1419 const char *StrEnd = strchr(StrStart, '}');
1420 if (StrEnd == 0) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001421 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1422 + std::string(AsmStr) + "'");
Chris Lattner1522e242009-03-10 05:37:13 +00001423 }
1424
1425 std::string Val(StrStart, StrEnd);
1426 PrintSpecial(MI, Val.c_str());
1427 LastEmitted = StrEnd+1;
1428 break;
1429 }
1430
Chris Lattneraa23fa92006-02-01 22:41:11 +00001431 const char *IDStart = LastEmitted;
1432 char *IDEnd;
Chris Lattnerd39e3882007-01-23 00:36:17 +00001433 errno = 0;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001434 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1435 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001436 llvm_report_error("Bad $ operand number in inline asm string: '"
1437 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001438 }
1439 LastEmitted = IDEnd;
1440
Chris Lattner34f74c12006-02-06 22:17:23 +00001441 char Modifier[2] = { 0, 0 };
1442
Chris Lattneraa23fa92006-02-01 22:41:11 +00001443 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +00001444 // If we have curly braces, check for a modifier character. This
1445 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1446 if (*LastEmitted == ':') {
1447 ++LastEmitted; // Consume ':' character.
1448 if (*LastEmitted == 0) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001449 llvm_report_error("Bad ${:} expression in inline asm string: '"
1450 + std::string(AsmStr) + "'");
Chris Lattner34f74c12006-02-06 22:17:23 +00001451 }
1452
1453 Modifier[0] = *LastEmitted;
1454 ++LastEmitted; // Consume modifier character.
1455 }
1456
Chris Lattneraa23fa92006-02-01 22:41:11 +00001457 if (*LastEmitted != '}') {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001458 llvm_report_error("Bad ${} expression in inline asm string: '"
1459 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001460 }
1461 ++LastEmitted; // Consume '}' character.
1462 }
1463
1464 if ((unsigned)Val >= NumOperands-1) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001465 llvm_report_error("Invalid $ operand number in inline asm string: '"
1466 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001467 }
1468
Chris Lattner571d9642006-02-23 19:21:04 +00001469 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +00001470 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +00001471 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1472 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001473
1474 bool Error = false;
1475
Chris Lattner571d9642006-02-23 19:21:04 +00001476 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +00001477 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001478 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner81798412007-12-30 20:50:28 +00001479 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng2e559232009-03-20 18:03:34 +00001480 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattner5af3fde2006-02-24 19:50:58 +00001481 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001482
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001483 if (OpNo >= MI->getNumOperands()) {
1484 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +00001485 } else {
Chris Lattner81798412007-12-30 20:50:28 +00001486 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001487 ++OpNo; // Skip over the ID number.
1488
Dale Johannesen4646aa32007-11-05 21:20:28 +00001489 if (Modifier[0]=='l') // labels are target independent
Chris Lattnera5bb3702007-12-30 23:10:15 +00001490 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Chengc7990652008-02-28 00:43:03 +00001491 false, false, false);
Dale Johannesen4646aa32007-11-05 21:20:28 +00001492 else {
1493 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesenc36660d2008-09-24 01:07:17 +00001494 if ((OpFlags & 7) == 4) {
Dale Johannesen4646aa32007-11-05 21:20:28 +00001495 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1496 Modifier[0] ? Modifier : 0);
1497 } else {
1498 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1499 Modifier[0] ? Modifier : 0);
1500 }
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001501 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001502 }
1503 if (Error) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001504 std::string msg;
1505 raw_string_ostream Msg(msg);
1506 Msg << "Invalid operand found in inline asm: '"
Bill Wendlingf3baad32006-12-07 01:30:32 +00001507 << AsmStr << "'\n";
Torok Edwinccb29cd2009-07-11 13:10:19 +00001508 MI->print(Msg);
1509 llvm_report_error(Msg.str());
Chris Lattneraa23fa92006-02-01 22:41:11 +00001510 }
Chris Lattner571d9642006-02-23 19:21:04 +00001511 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001512 break;
1513 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001514 }
1515 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001516 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattneraa23fa92006-02-01 22:41:11 +00001517}
1518
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001519/// printImplicitDef - This method prints the specified machine instruction
1520/// that is an implicit def.
1521void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Evan Chenga774a992009-03-24 00:17:40 +00001522 if (VerboseAsm)
1523 O << '\t' << TAI->getCommentString() << " implicit-def: "
1524 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001525}
1526
Jim Laskeyf9e54452007-01-26 14:34:52 +00001527/// printLabel - This method prints a local label used by debug and
1528/// exception handling tables.
1529void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohmanb58aff42008-07-01 00:16:26 +00001530 printLabel(MI->getOperand(0).getImm());
Jim Laskeyf9e54452007-01-26 14:34:52 +00001531}
1532
Evan Chengd6e44ab2008-02-01 09:10:45 +00001533void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng32e53472008-02-02 08:39:46 +00001534 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chengd6e44ab2008-02-01 09:10:45 +00001535}
1536
Evan Chengefd142a2008-02-02 04:07:54 +00001537/// printDeclare - This method prints a local variable declaration used by
1538/// debug tables.
Evan Cheng2cb90682008-02-04 23:06:48 +00001539/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1540/// entry into dwarf table.
Evan Chengefd142a2008-02-02 04:07:54 +00001541void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Devang Pateldd25a9d2009-01-13 21:44:10 +00001542 unsigned FI = MI->getOperand(0).getIndex();
Evan Cheng2cb90682008-02-04 23:06:48 +00001543 GlobalValue *GV = MI->getOperand(1).getGlobal();
Devang Patel32d17a12009-04-15 00:10:26 +00001544 DW->RecordVariable(cast<GlobalVariable>(GV), FI, MI);
Evan Chengefd142a2008-02-02 04:07:54 +00001545}
1546
Chris Lattneraa23fa92006-02-01 22:41:11 +00001547/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1548/// instruction, using the specified assembler variant. Targets should
1549/// overried this to format as appropriate.
1550bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +00001551 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +00001552 // Target doesn't support this yet!
1553 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +00001554}
Chris Lattner1d08c652006-02-24 20:21:58 +00001555
1556bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1557 unsigned AsmVariant,
1558 const char *ExtraCode) {
1559 // Target doesn't support this yet!
1560 return true;
1561}
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001562
1563/// printBasicBlockLabel - This method prints the label for the specified
1564/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +00001565void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Chengc7990652008-02-28 00:43:03 +00001566 bool printAlign,
Nate Begemanb9d4f832006-05-02 05:37:32 +00001567 bool printColon,
1568 bool printComment) const {
Evan Chengc7990652008-02-28 00:43:03 +00001569 if (printAlign) {
1570 unsigned Align = MBB->getAlignment();
1571 if (Align)
1572 EmitAlignment(Log2_32(Align));
1573 }
1574
Dan Gohman6896901e2008-06-30 22:03:41 +00001575 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Chengcdf36092007-10-14 05:57:21 +00001576 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +00001577 if (printColon)
1578 O << ':';
David Greenef354b6e2009-08-10 16:38:07 +00001579 if (printComment) {
1580 O.PadToColumn(TAI->getCommentColumn(), 1);
1581
1582 if (MBB->getBasicBlock())
1583 O << '\t' << TAI->getCommentString() << ' '
1584 << MBB->getBasicBlock()->getNameStr();
1585
1586 if (printColon)
1587 EmitComments(*MBB);
1588 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001589}
Nate Begeman984c1a42006-08-12 21:29:52 +00001590
Evan Cheng797d56f2007-11-09 01:32:10 +00001591/// printPICJumpTableSetLabel - This method prints a set label for the
1592/// specified MachineBasicBlock for a jumptable entry.
1593void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1594 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001595 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +00001596 return;
1597
Jim Laskeya6211dc2006-09-06 18:34:40 +00001598 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001599 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001600 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001601 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1602 << '_' << uid << '\n';
Nate Begeman984c1a42006-08-12 21:29:52 +00001603}
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001604
Evan Cheng797d56f2007-11-09 01:32:10 +00001605void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1606 const MachineBasicBlock *MBB) const {
Evan Cheng91f120f2006-11-01 09:23:08 +00001607 if (!TAI->getSetDirective())
1608 return;
1609
1610 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001611 << getFunctionNumber() << '_' << uid << '_' << uid2
1612 << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001613 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001614 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1615 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng91f120f2006-11-01 09:23:08 +00001616}
1617
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001618/// printDataDirective - This method prints the asm directive for the
1619/// specified type.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001620void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001621 const TargetData *TD = TM.getTargetData();
1622 switch (type->getTypeID()) {
Chris Lattnerfe785582009-07-15 04:42:49 +00001623 case Type::FloatTyID: case Type::DoubleTyID:
1624 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1625 assert(0 && "Should have already output floating point constant.");
1626 default:
1627 assert(0 && "Can't handle printing this type of thing");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001628 case Type::IntegerTyID: {
1629 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1630 if (BitWidth <= 8)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001631 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001632 else if (BitWidth <= 16)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001633 O << TAI->getData16bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001634 else if (BitWidth <= 32)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001635 O << TAI->getData32bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001636 else if (BitWidth <= 64) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001637 assert(TAI->getData64bitsDirective(AddrSpace) &&
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001638 "Target cannot handle 64-bit constant exprs!");
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001639 O << TAI->getData64bitsDirective(AddrSpace);
Dan Gohmanf9b20542008-09-08 16:40:13 +00001640 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001641 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001642 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001643 break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001644 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001645 case Type::PointerTyID:
1646 if (TD->getPointerSize() == 8) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001647 assert(TAI->getData64bitsDirective(AddrSpace) &&
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001648 "Target cannot handle 64-bit pointer exprs!");
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001649 O << TAI->getData64bitsDirective(AddrSpace);
Sanjiv Gupta4186ddf2009-01-22 10:14:21 +00001650 } else if (TD->getPointerSize() == 2) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001651 O << TAI->getData16bitsDirective(AddrSpace);
Sanjiv Gupta4186ddf2009-01-22 10:14:21 +00001652 } else if (TD->getPointerSize() == 1) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001653 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001654 } else {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001655 O << TAI->getData32bitsDirective(AddrSpace);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001656 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001657 break;
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001658 }
1659}
Dale Johannesen915e1542007-02-16 01:54:53 +00001660
Anton Korobeynikoved473292008-08-08 18:25:07 +00001661void AsmPrinter::printVisibility(const std::string& Name,
1662 unsigned Visibility) const {
1663 if (Visibility == GlobalValue::HiddenVisibility) {
1664 if (const char *Directive = TAI->getHiddenDirective())
1665 O << Directive << Name << '\n';
1666 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1667 if (const char *Directive = TAI->getProtectedDirective())
1668 O << Directive << Name << '\n';
1669 }
1670}
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001671
Anton Korobeynikovbff4b372008-11-22 16:15:34 +00001672void AsmPrinter::printOffset(int64_t Offset) const {
1673 if (Offset > 0)
1674 O << '+' << Offset;
1675 else if (Offset < 0)
1676 O << Offset;
1677}
1678
Gordon Henriksend930f912008-08-17 18:44:35 +00001679GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1680 if (!S->usesMetadata())
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001681 return 0;
1682
Gordon Henriksend930f912008-08-17 18:44:35 +00001683 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001684 if (GCPI != GCMetadataPrinters.end())
1685 return GCPI->second;
1686
Gordon Henriksend930f912008-08-17 18:44:35 +00001687 const char *Name = S->getName().c_str();
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001688
1689 for (GCMetadataPrinterRegistry::iterator
1690 I = GCMetadataPrinterRegistry::begin(),
1691 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1692 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001693 GCMetadataPrinter *GMP = I->instantiate();
1694 GMP->S = S;
1695 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1696 return GMP;
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001697 }
1698
Gordon Henriksend930f912008-08-17 18:44:35 +00001699 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +00001700 llvm_unreachable(0);
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001701}
David Greenede544782009-07-13 20:25:48 +00001702
1703/// EmitComments - Pretty-print comments for instructions
Chris Lattner84b9db22009-08-07 23:42:01 +00001704void AsmPrinter::EmitComments(const MachineInstr &MI) const {
1705 if (!VerboseAsm ||
1706 MI.getDebugLoc().isUnknown())
1707 return;
1708
1709 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greenede9cd442009-07-16 22:24:20 +00001710
Chris Lattner84b9db22009-08-07 23:42:01 +00001711 // Print source line info
1712 O.PadToColumn(TAI->getCommentColumn(), 1);
1713 O << TAI->getCommentString() << " SrcLine ";
1714 if (DLT.CompileUnit->hasInitializer()) {
1715 Constant *Name = DLT.CompileUnit->getInitializer();
1716 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1717 if (NameString->isString())
1718 O << NameString->getAsString() << " ";
David Greenede9cd442009-07-16 22:24:20 +00001719 }
Chris Lattner84b9db22009-08-07 23:42:01 +00001720 O << DLT.Line;
1721 if (DLT.Col != 0)
1722 O << ":" << DLT.Col;
David Greenede544782009-07-13 20:25:48 +00001723}
1724
1725/// EmitComments - Pretty-print comments for instructions
1726void AsmPrinter::EmitComments(const MCInst &MI) const
1727{
David Greene6e2eda12009-07-22 20:33:26 +00001728 if (VerboseAsm) {
1729 if (!MI.getDebugLoc().isUnknown()) {
1730 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greenede9cd442009-07-16 22:24:20 +00001731
David Greene6e2eda12009-07-22 20:33:26 +00001732 // Print source line info
1733 O.PadToColumn(TAI->getCommentColumn(), 1);
1734 O << TAI->getCommentString() << " SrcLine ";
1735 if (DLT.CompileUnit->hasInitializer()) {
1736 Constant *Name = DLT.CompileUnit->getInitializer();
1737 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1738 if (NameString->isString()) {
1739 O << NameString->getAsString() << " ";
1740 }
1741 }
1742 O << DLT.Line;
1743 if (DLT.Col != 0)
1744 O << ":" << DLT.Col;
1745 }
David Greenede9cd442009-07-16 22:24:20 +00001746 }
David Greenede544782009-07-13 20:25:48 +00001747}
David Greenef354b6e2009-08-10 16:38:07 +00001748
1749/// EmitComments - Pretty-print comments for basic blocks
1750void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const
1751{
1752 if (ExuberantAsm) {
1753 // Add loop depth information
1754 const MachineLoop *loop = LI->getLoopFor(&MBB);
1755
1756 if (loop) {
1757 // Print a newline after bb# annotation.
1758 O << "\n";
1759 O.PadToColumn(TAI->getCommentColumn(), 1);
1760 O << TAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
1761 << '\n';
1762
1763 O.PadToColumn(TAI->getCommentColumn(), 1);
1764
1765 MachineBasicBlock *Header = loop->getHeader();
1766 assert(Header && "No header for loop");
1767
1768 if (Header == &MBB) {
1769 O << TAI->getCommentString() << " Loop Header";
1770 PrintChildLoopComment(loop);
1771 }
1772 else {
1773 O << TAI->getCommentString() << " Loop Header is BB"
1774 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
1775 }
1776
1777 if (loop->empty()) {
1778 O << '\n';
1779 O.PadToColumn(TAI->getCommentColumn(), 1);
1780 O << TAI->getCommentString() << " Inner Loop";
1781 }
1782
1783 // Add parent loop information
1784 for (const MachineLoop *CurLoop = loop->getParentLoop();
1785 CurLoop;
1786 CurLoop = CurLoop->getParentLoop()) {
1787 MachineBasicBlock *Header = CurLoop->getHeader();
1788 assert(Header && "No header for loop");
1789
1790 O << '\n';
1791 O.PadToColumn(TAI->getCommentColumn(), 1);
1792 O << TAI->getCommentString() << Indent(CurLoop->getLoopDepth()-1)
1793 << " Inside Loop BB" << getFunctionNumber() << "_"
1794 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
1795 }
1796 }
1797 }
1798}
1799
1800void AsmPrinter::PrintChildLoopComment(const MachineLoop *loop) const {
1801 // Add child loop information
1802 for(MachineLoop::iterator cl = loop->begin(),
1803 clend = loop->end();
1804 cl != clend;
1805 ++cl) {
1806 MachineBasicBlock *Header = (*cl)->getHeader();
1807 assert(Header && "No header for loop");
1808
1809 O << '\n';
1810 O.PadToColumn(TAI->getCommentColumn(), 1);
1811
1812 O << TAI->getCommentString() << Indent((*cl)->getLoopDepth()-1)
1813 << " Child Loop BB" << getFunctionNumber() << "_"
1814 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1815
1816 PrintChildLoopComment(*cl);
1817 }
1818}