blob: 80965020d0df2a2af60d99cec9de17f516f499b6 [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 Henriksen5180e852008-01-07 01:30:38 +000019#include "llvm/CodeGen/Collector.h"
20#include "llvm/CodeGen/CollectorMetadata.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000022#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey1c055e82007-01-25 15:12:02 +000024#include "llvm/Support/CommandLine.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000025#include "llvm/Support/Mangler.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000026#include "llvm/Support/MathExtras.h"
Bill Wendling5c3966a2006-11-29 00:39:47 +000027#include "llvm/Support/Streams.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000028#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000029#include "llvm/Target/TargetData.h"
Chris Lattner90438232006-10-06 22:50:56 +000030#include "llvm/Target/TargetLowering.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000031#include "llvm/Target/TargetMachine.h"
Evan Cheng797d56f2007-11-09 01:32:10 +000032#include "llvm/ADT/SmallPtrSet.h"
Chris Lattneraa23fa92006-02-01 22:41:11 +000033#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000034using namespace llvm;
35
Jim Laskey1c055e82007-01-25 15:12:02 +000036static cl::opt<bool>
37AsmVerbose("asm-verbose", cl::Hidden, cl::desc("Add comments to directives."));
38
Devang Patel8c78a0b2007-05-03 01:11:54 +000039char AsmPrinter::ID = 0;
Jim Laskey261779b2006-09-07 22:06:40 +000040AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
41 const TargetAsmInfo *T)
Evan Chengc7990652008-02-28 00:43:03 +000042 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o), TM(tm), TAI(T),
43 IsInTextSection(false)
Jim Laskeya6211dc2006-09-06 18:34:40 +000044{}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000045
Chris Lattnerdc822412006-10-05 02:42:47 +000046std::string AsmPrinter::getSectionForFunction(const Function &F) const {
47 return TAI->getTextSection();
48}
49
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000050
Chris Lattner8488ba22006-05-09 04:59:56 +000051/// SwitchToTextSection - Switch to the specified text section of the executable
52/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000053///
Chris Lattner8488ba22006-05-09 04:59:56 +000054void AsmPrinter::SwitchToTextSection(const char *NewSection,
55 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000056 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000057 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000058 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000059 else
60 NS = NewSection;
61
62 // If we're already in this section, we're done.
63 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000064
Chris Lattner4ebc6a22006-05-09 05:33:48 +000065 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000066 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
67 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000068
Chris Lattner4ebc6a22006-05-09 05:33:48 +000069 CurrentSection = NS;
70
71 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000072 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Evan Chengc7990652008-02-28 00:43:03 +000073
74 IsInTextSection = true;
Chris Lattner2ea5c992005-11-21 07:06:27 +000075}
76
Nate Begeman78756502006-07-27 01:13:04 +000077/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000078/// if we are not already in it!
79///
80void AsmPrinter::SwitchToDataSection(const char *NewSection,
81 const GlobalValue *GV) {
82 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000083 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000084 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner6341df82006-05-09 05:23:12 +000085 else
86 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +000087
Chris Lattner6341df82006-05-09 05:23:12 +000088 // If we're already in this section, we're done.
89 if (CurrentSection == NS) return;
90
Chris Lattner4ebc6a22006-05-09 05:33:48 +000091 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000092 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
93 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner4ebc6a22006-05-09 05:33:48 +000094
95 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +000096
Chris Lattner4ebc6a22006-05-09 05:33:48 +000097 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000098 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Evan Chengc7990652008-02-28 00:43:03 +000099
100 IsInTextSection = false;
Chris Lattner8488ba22006-05-09 04:59:56 +0000101}
102
103
Gordon Henriksen5180e852008-01-07 01:30:38 +0000104void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
105 MachineFunctionPass::getAnalysisUsage(AU);
106 AU.addRequired<CollectorModuleMetadata>();
107}
108
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000109bool AsmPrinter::doInitialization(Module &M) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000110 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +0000111
Gordon Henriksen5180e852008-01-07 01:30:38 +0000112 CollectorModuleMetadata *CMM = getAnalysisToUpdate<CollectorModuleMetadata>();
113 assert(CMM && "AsmPrinter didn't require CollectorModuleMetadata?");
114 for (CollectorModuleMetadata::iterator I = CMM->begin(),
115 E = CMM->end(); I != E; ++I)
116 (*I)->beginAssembly(O, *this, *TAI);
117
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000118 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000119 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000120 << M.getModuleInlineAsm()
Jim Laskeya6211dc2006-09-06 18:34:40 +0000121 << "\n" << TAI->getCommentString()
122 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000123
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000124 SwitchToDataSection(""); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000125
Evan Cheng2cb90682008-02-04 23:06:48 +0000126 MMI = getAnalysisToUpdate<MachineModuleInfo>();
127 if (MMI) MMI->AnalyzeModule(M);
Jim Laskey0bbdc552006-01-26 20:21:46 +0000128
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000129 return false;
130}
131
132bool AsmPrinter::doFinalization(Module &M) {
Rafael Espindolad7998d02006-12-18 03:37:18 +0000133 if (TAI->getWeakRefDirective()) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000134 if (!ExtWeakSymbols.empty())
Rafael Espindolad7998d02006-12-18 03:37:18 +0000135 SwitchToDataSection("");
136
137 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
138 e = ExtWeakSymbols.end(); i != e; ++i) {
139 const GlobalValue *GV = *i;
140 std::string Name = Mang->getValueName(GV);
141 O << TAI->getWeakRefDirective() << Name << "\n";
142 }
143 }
144
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000145 if (TAI->getSetDirective()) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000146 if (!M.alias_empty())
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000147 SwitchToTextSection(TAI->getTextSection());
148
149 O << "\n";
150 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
151 I!=E; ++I) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000152 std::string Name = Mang->getValueName(I);
153 std::string Target;
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000154
155 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
156 Target = Mang->getValueName(GV);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000157
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000158 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000159 O << "\t.globl\t" << Name << "\n";
160 else if (I->hasWeakLinkage())
161 O << TAI->getWeakRefDirective() << Name << "\n";
162 else if (!I->hasInternalLinkage())
163 assert(0 && "Invalid alias linkage");
164
Nick Lewycky7c1d7872008-02-07 06:36:26 +0000165 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << "\n";
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000166
167 // If the aliasee has external weak linkage it can be referenced only by
168 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
169 // weak reference in such case.
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000170 if (GV->hasExternalWeakLinkage()) {
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000171 if (TAI->getWeakRefDirective())
172 O << TAI->getWeakRefDirective() << Target << "\n";
173 else
174 O << "\t.globl\t" << Target << "\n";
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000175 }
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000176 }
177 }
178
Gordon Henriksen5180e852008-01-07 01:30:38 +0000179 CollectorModuleMetadata *CMM = getAnalysisToUpdate<CollectorModuleMetadata>();
180 assert(CMM && "AsmPrinter didn't require CollectorModuleMetadata?");
181 for (CollectorModuleMetadata::iterator I = CMM->end(),
182 E = CMM->begin(); I != E; )
183 (*--I)->finishAssembly(O, *this, *TAI);
184
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000185 delete Mang; Mang = 0;
186 return false;
187}
188
Bill Wendlinge8c885f2007-09-18 09:10:16 +0000189std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
Bill Wendling067f1d82007-09-18 01:47:22 +0000190 assert(MF && "No machine function?");
Bill Wendling69833b62007-09-18 05:03:44 +0000191 return Mang->makeNameProper(MF->getFunction()->getName() + ".eh",
192 TAI->getGlobalPrefix());
Bill Wendling067f1d82007-09-18 01:47:22 +0000193}
194
Chris Lattnerbb644e32005-11-21 07:51:36 +0000195void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000196 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000197 CurrentFnName = Mang->getValueName(MF.getFunction());
Evan Chengcdf36092007-10-14 05:57:21 +0000198 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000199}
200
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000201/// EmitConstantPool - Print to the current output stream assembly
202/// representations of the constants in the constant pool MCP. This is
203/// used to print out constants which have been "spilled to memory" by
204/// the code generator.
205///
206void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000207 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000208 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000209
210 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
211 // in special sections.
212 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
213 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
214 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
215 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000216 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000217 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000218 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng2ad050f2006-09-14 07:35:00 +0000219 const Type *Ty = CPE.getType();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000220 if (TAI->getFourByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000221 TM.getTargetData()->getABITypeSize(Ty) == 4)
Evan Chengec1d60b2006-06-29 00:26:09 +0000222 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000223 else if (TAI->getEightByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000224 TM.getTargetData()->getABITypeSize(Ty) == 8)
Evan Chengec1d60b2006-06-29 00:26:09 +0000225 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000226 else if (TAI->getSixteenByteConstantSection() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000227 TM.getTargetData()->getABITypeSize(Ty) == 16)
Evan Chengec1d60b2006-06-29 00:26:09 +0000228 SixteenByteCPs.push_back(std::make_pair(CPE, i));
229 else
230 OtherCPs.push_back(std::make_pair(CPE, i));
231 }
232
233 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000234 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
235 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
236 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
237 SixteenByteCPs);
238 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Chengec1d60b2006-06-29 00:26:09 +0000239}
240
241void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
242 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
243 if (CP.empty()) return;
244
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000245 SwitchToDataSection(Section);
Evan Chengec1d60b2006-06-29 00:26:09 +0000246 EmitAlignment(Alignment);
247 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengcdf36092007-10-14 05:57:21 +0000248 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
249 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Cheng2ad050f2006-09-14 07:35:00 +0000250 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
251 if (CP[i].first.isMachineConstantPoolEntry())
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000252 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000253 else
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000254 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000255 if (i != e-1) {
Evan Cheng2ad050f2006-09-14 07:35:00 +0000256 const Type *Ty = CP[i].first.getType();
Evan Chengec1d60b2006-06-29 00:26:09 +0000257 unsigned EntSize =
Duncan Sands283207a2007-11-05 00:04:43 +0000258 TM.getTargetData()->getABITypeSize(Ty);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000259 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000260 // Emit inter-object padding for alignment.
Evan Cheng2ad050f2006-09-14 07:35:00 +0000261 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000262 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000263 }
264}
265
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000266/// EmitJumpTableInfo - Print assembly representations of the jump tables used
267/// by the current function to the current output stream.
268///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000269void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
270 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000271 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
272 if (JT.empty()) return;
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000273
Jim Laskey70323a82006-12-14 19:17:33 +0000274 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begemanefc312a2006-07-27 16:46:58 +0000275
Nate Begeman78756502006-07-27 01:13:04 +0000276 // Pick the directive to use to print the jump table entries, and switch to
277 // the appropriate section.
Jim Laskey70323a82006-12-14 19:17:33 +0000278 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000279
280 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
281 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
282 !JumpTableDataSection) {
Jim Laskey70323a82006-12-14 19:17:33 +0000283 // In PIC mode, we need to emit the jump table to the same section as the
284 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000285 // We should also do if the section name is NULL.
Jim Laskey70323a82006-12-14 19:17:33 +0000286 const Function *F = MF.getFunction();
287 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman78756502006-07-27 01:13:04 +0000288 } else {
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000289 SwitchToDataSection(JumpTableDataSection);
Nate Begeman78756502006-07-27 01:13:04 +0000290 }
Jim Laskey70323a82006-12-14 19:17:33 +0000291
292 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000293
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000294 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000295 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner28bfe382006-10-28 18:10:06 +0000296
297 // If this jump table was deleted, ignore it.
298 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-08-12 21:29:52 +0000299
300 // For PIC codegen, if possible we want to use the SetDirective to reduce
301 // the number of relocations the assembler will generate for the jump table.
302 // Set directives are all printed before the jump table itself.
Evan Cheng797d56f2007-11-09 01:32:10 +0000303 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Jim Laskey70323a82006-12-14 19:17:33 +0000304 if (TAI->getSetDirective() && IsPic)
Nate Begeman984c1a42006-08-12 21:29:52 +0000305 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng797d56f2007-11-09 01:32:10 +0000306 if (EmittedSets.insert(JTBBs[ii]))
307 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman984c1a42006-08-12 21:29:52 +0000308
Chris Lattner0ee2d462007-01-18 01:12:56 +0000309 // On some targets (e.g. darwin) we want to emit two consequtive labels
310 // before each jump table. The first label is never referenced, but tells
311 // the assembler and linker the extents of the jump table object. The
312 // second label is actually referenced by the code.
313 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
Evan Chengcdf36092007-10-14 05:57:21 +0000314 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Chris Lattner0ee2d462007-01-18 01:12:56 +0000315
Evan Chengcdf36092007-10-14 05:57:21 +0000316 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
317 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000318
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000319 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000320 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000321 O << '\n';
322 }
323 }
324}
325
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000326void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
327 const MachineBasicBlock *MBB,
328 unsigned uid) const {
329 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
330
331 // Use JumpTableDirective otherwise honor the entry size from the jump table
332 // info.
333 const char *JTEntryDirective = TAI->getJumpTableDirective();
334 bool HadJTEntryDirective = JTEntryDirective != NULL;
335 if (!HadJTEntryDirective) {
336 JTEntryDirective = MJTI->getEntrySize() == 4 ?
337 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
338 }
339
340 O << JTEntryDirective << ' ';
341
342 // If we have emitted set directives for the jump table entries, print
343 // them rather than the entries themselves. If we're emitting PIC, then
344 // emit the table entries as differences between two text section labels.
345 // If we're emitting non-PIC code, then emit the entries as direct
346 // references to the target basic blocks.
347 if (IsPic) {
348 if (TAI->getSetDirective()) {
349 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
350 << '_' << uid << "_set_" << MBB->getNumber();
351 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000352 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000353 // If the arch uses custom Jump Table directives, don't calc relative to
354 // JT
355 if (!HadJTEntryDirective)
356 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
357 << getFunctionNumber() << '_' << uid;
358 }
359 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000360 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000361 }
362}
363
364
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000365/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
366/// special global used by LLVM. If so, emit it and return true, otherwise
367/// do nothing and return false.
368bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Andrew Lenharthbeb80a92007-08-22 19:33:11 +0000369 if (GV->getName() == "llvm.used") {
370 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
371 EmitLLVMUsedList(GV->getInitializer());
372 return true;
373 }
374
Jim Laskey313570f2006-03-07 22:00:35 +0000375 // Ignore debug and non-emitted data.
376 if (GV->getSection() == "llvm.metadata") return true;
377
378 if (!GV->hasAppendingLinkage()) return false;
379
380 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000381
Evan Cheng53ce7de2007-06-04 20:39:18 +0000382 const TargetData *TD = TM.getTargetData();
383 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner3760e902006-01-12 19:17:23 +0000384 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000385 SwitchToDataSection(TAI->getStaticCtorsSection());
Evan Cheng53ce7de2007-06-04 20:39:18 +0000386 EmitAlignment(Align, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000387 EmitXXStructorList(GV->getInitializer());
388 return true;
389 }
390
Chris Lattner3760e902006-01-12 19:17:23 +0000391 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000392 SwitchToDataSection(TAI->getStaticDtorsSection());
Evan Cheng53ce7de2007-06-04 20:39:18 +0000393 EmitAlignment(Align, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000394 EmitXXStructorList(GV->getInitializer());
395 return true;
396 }
397
398 return false;
399}
400
Chris Lattner66af3902006-09-26 03:38:18 +0000401/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
402/// global in the specified llvm.used list as being used with this directive.
403void AsmPrinter::EmitLLVMUsedList(Constant *List) {
404 const char *Directive = TAI->getUsedDirective();
405
406 // Should be an array of 'sbyte*'.
407 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
408 if (InitList == 0) return;
409
410 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
411 O << Directive;
412 EmitConstantValueOnly(InitList->getOperand(i));
413 O << "\n";
414 }
415}
416
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000417/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
418/// function pointers, ignoring the init priority.
419void AsmPrinter::EmitXXStructorList(Constant *List) {
420 // Should be an array of '{ int, void ()* }' structs. The first value is the
421 // init priority, which we ignore.
422 if (!isa<ConstantArray>(List)) return;
423 ConstantArray *InitList = cast<ConstantArray>(List);
424 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
425 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
426 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000427
428 if (CS->getOperand(1)->isNullValue())
429 return; // Found a null terminator, exit printing.
430 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000431 EmitGlobalConstant(CS->getOperand(1));
432 }
433}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000434
Jim Laskey71262542006-10-17 13:41:07 +0000435/// getGlobalLinkName - Returns the asm/link name of of the specified
436/// global variable. Should be overridden by each target asm printer to
437/// generate the appropriate value.
Jim Laskeyd24b9132006-10-17 17:17:24 +0000438const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
439 std::string LinkName;
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000440
441 if (isa<Function>(GV)) {
442 LinkName += TAI->getFunctionAddrPrefix();
443 LinkName += Mang->getValueName(GV);
444 LinkName += TAI->getFunctionAddrSuffix();
445 } else {
446 LinkName += TAI->getGlobalVarAddrPrefix();
447 LinkName += Mang->getValueName(GV);
448 LinkName += TAI->getGlobalVarAddrSuffix();
449 }
450
Jim Laskeyd24b9132006-10-17 17:17:24 +0000451 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000452}
453
Jim Laskey18fc0972007-02-21 22:47:38 +0000454/// EmitExternalGlobal - Emit the external reference to a global variable.
455/// Should be overridden if an indirect reference should be used.
456void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
457 O << getGlobalLinkName(GV);
458}
459
460
461
Jim Laskey1c055e82007-01-25 15:12:02 +0000462//===----------------------------------------------------------------------===//
463/// LEB 128 number encoding.
464
465/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
466/// representing an unsigned leb128 value.
467void AsmPrinter::PrintULEB128(unsigned Value) const {
468 do {
469 unsigned Byte = Value & 0x7f;
470 Value >>= 7;
471 if (Value) Byte |= 0x80;
472 O << "0x" << std::hex << Byte << std::dec;
473 if (Value) O << ", ";
474 } while (Value);
475}
476
477/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
478/// value.
479unsigned AsmPrinter::SizeULEB128(unsigned Value) {
480 unsigned Size = 0;
481 do {
482 Value >>= 7;
483 Size += sizeof(int8_t);
484 } while (Value);
485 return Size;
486}
487
488/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
489/// representing a signed leb128 value.
490void AsmPrinter::PrintSLEB128(int Value) const {
491 int Sign = Value >> (8 * sizeof(Value) - 1);
492 bool IsMore;
493
494 do {
495 unsigned Byte = Value & 0x7f;
496 Value >>= 7;
497 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
498 if (IsMore) Byte |= 0x80;
499 O << "0x" << std::hex << Byte << std::dec;
500 if (IsMore) O << ", ";
501 } while (IsMore);
502}
503
504/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
505/// value.
506unsigned AsmPrinter::SizeSLEB128(int Value) {
507 unsigned Size = 0;
508 int Sign = Value >> (8 * sizeof(Value) - 1);
509 bool IsMore;
510
511 do {
512 unsigned Byte = Value & 0x7f;
513 Value >>= 7;
514 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
515 Size += sizeof(int8_t);
516 } while (IsMore);
517 return Size;
518}
519
520//===--------------------------------------------------------------------===//
521// Emission and print routines
522//
523
524/// PrintHex - Print a value as a hexidecimal value.
525///
526void AsmPrinter::PrintHex(int Value) const {
527 O << "0x" << std::hex << Value << std::dec;
528}
529
530/// EOL - Print a newline character to asm stream. If a comment is present
531/// then it will be printed first. Comments should not contain '\n'.
Jim Laskey18fc0972007-02-21 22:47:38 +0000532void AsmPrinter::EOL() const {
533 O << "\n";
534}
Jim Laskey1c055e82007-01-25 15:12:02 +0000535void AsmPrinter::EOL(const std::string &Comment) const {
536 if (AsmVerbose && !Comment.empty()) {
537 O << "\t"
538 << TAI->getCommentString()
539 << " "
540 << Comment;
541 }
542 O << "\n";
543}
544
545/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
546/// unsigned leb128 value.
547void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
548 if (TAI->hasLEB128()) {
549 O << "\t.uleb128\t"
550 << Value;
551 } else {
552 O << TAI->getData8bitsDirective();
553 PrintULEB128(Value);
554 }
555}
556
557/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
558/// signed leb128 value.
559void AsmPrinter::EmitSLEB128Bytes(int Value) const {
560 if (TAI->hasLEB128()) {
561 O << "\t.sleb128\t"
562 << Value;
563 } else {
564 O << TAI->getData8bitsDirective();
565 PrintSLEB128(Value);
566 }
567}
568
569/// EmitInt8 - Emit a byte directive and value.
570///
571void AsmPrinter::EmitInt8(int Value) const {
572 O << TAI->getData8bitsDirective();
573 PrintHex(Value & 0xFF);
574}
575
576/// EmitInt16 - Emit a short directive and value.
577///
578void AsmPrinter::EmitInt16(int Value) const {
579 O << TAI->getData16bitsDirective();
580 PrintHex(Value & 0xFFFF);
581}
582
583/// EmitInt32 - Emit a long directive and value.
584///
585void AsmPrinter::EmitInt32(int Value) const {
586 O << TAI->getData32bitsDirective();
587 PrintHex(Value);
588}
589
590/// EmitInt64 - Emit a long long directive and value.
591///
592void AsmPrinter::EmitInt64(uint64_t Value) const {
593 if (TAI->getData64bitsDirective()) {
594 O << TAI->getData64bitsDirective();
595 PrintHex(Value);
596 } else {
597 if (TM.getTargetData()->isBigEndian()) {
598 EmitInt32(unsigned(Value >> 32)); O << "\n";
599 EmitInt32(unsigned(Value));
600 } else {
601 EmitInt32(unsigned(Value)); O << "\n";
602 EmitInt32(unsigned(Value >> 32));
603 }
604 }
605}
606
607/// toOctal - Convert the low order bits of X into an octal digit.
608///
609static inline char toOctal(int X) {
610 return (X&7)+'0';
611}
612
613/// printStringChar - Print a char, escaped if necessary.
614///
615static void printStringChar(std::ostream &O, unsigned char C) {
616 if (C == '"') {
617 O << "\\\"";
618 } else if (C == '\\') {
619 O << "\\\\";
620 } else if (isprint(C)) {
621 O << C;
622 } else {
623 switch(C) {
624 case '\b': O << "\\b"; break;
625 case '\f': O << "\\f"; break;
626 case '\n': O << "\\n"; break;
627 case '\r': O << "\\r"; break;
628 case '\t': O << "\\t"; break;
629 default:
630 O << '\\';
631 O << toOctal(C >> 6);
632 O << toOctal(C >> 3);
633 O << toOctal(C >> 0);
634 break;
635 }
636 }
637}
638
639/// EmitString - Emit a string with quotes and a null terminator.
640/// Special characters are emitted properly.
641/// \literal (Eg. '\t') \endliteral
642void AsmPrinter::EmitString(const std::string &String) const {
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000643 const char* AscizDirective = TAI->getAscizDirective();
644 if (AscizDirective)
645 O << AscizDirective;
646 else
647 O << TAI->getAsciiDirective();
648 O << "\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000649 for (unsigned i = 0, N = String.size(); i < N; ++i) {
650 unsigned char C = String[i];
651 printStringChar(O, C);
652 }
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000653 if (AscizDirective)
654 O << "\"";
655 else
656 O << "\\0\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000657}
658
659
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000660/// EmitFile - Emit a .file directive.
661void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
662 O << "\t.file\t" << Number << " \"";
663 for (unsigned i = 0, N = Name.size(); i < N; ++i) {
664 unsigned char C = Name[i];
665 printStringChar(O, C);
666 }
667 O << "\"";
668}
669
670
Jim Laskey1c055e82007-01-25 15:12:02 +0000671//===----------------------------------------------------------------------===//
672
Chris Lattner3e3ff302007-05-31 18:57:45 +0000673// EmitAlignment - Emit an alignment directive to the specified power of
674// two boundary. For example, if you pass in 3 here, you will get an 8
675// byte alignment. If a global value is specified, and if that global has
676// an explicit alignment requested, it will unconditionally override the
677// alignment request. However, if ForcedAlignBits is specified, this value
678// has final say: the ultimate alignment will be the max of ForcedAlignBits
679// and the alignment computed with NumBits and the global.
680//
681// The algorithm is:
682// Align = NumBits;
683// if (GV && GV->hasalignment) Align = GV->getalignment();
684// Align = std::max(Align, ForcedAlignBits);
685//
686void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng26edb592008-02-29 19:36:59 +0000687 unsigned ForcedAlignBits,
688 bool UseFillExpr) const {
Dale Johannesen8653d292007-04-23 23:33:31 +0000689 if (GV && GV->getAlignment())
Chris Lattner3e3ff302007-05-31 18:57:45 +0000690 NumBits = Log2_32(GV->getAlignment());
691 NumBits = std::max(NumBits, ForcedAlignBits);
692
Chris Lattner747960d2005-11-10 18:09:27 +0000693 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000694 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng86eb3fd2007-07-25 23:35:07 +0000695 O << TAI->getAlignDirective() << NumBits;
Evan Chengc7990652008-02-28 00:43:03 +0000696
697 unsigned FillValue = TAI->getTextAlignFillValue();
Evan Cheng26edb592008-02-29 19:36:59 +0000698 UseFillExpr &= IsInTextSection && FillValue;
Evan Cheng86eb3fd2007-07-25 23:35:07 +0000699 if (UseFillExpr) O << ",0x" << std::hex << FillValue << std::dec;
700 O << "\n";
Chris Lattner1d35c162004-08-17 19:14:29 +0000701}
702
Jim Laskey18fc0972007-02-21 22:47:38 +0000703
Chris Lattnerbb644e32005-11-21 07:51:36 +0000704/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000705///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000706void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000707 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000708 if (TAI->getZeroDirective()) {
709 O << TAI->getZeroDirective() << NumZeros;
710 if (TAI->getZeroDirectiveSuffix())
711 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000712 O << "\n";
713 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000714 for (; NumZeros; --NumZeros)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000715 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000716 }
717 }
718}
719
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000720// Print out the specified constant, without a storage class. Only the
721// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000722void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000723 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000724 O << "0";
Zhou Sheng75b871f2007-01-11 12:24:14 +0000725 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner7f6fac42007-01-12 18:15:09 +0000726 O << CI->getZExtValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000727 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000728 // This is a constant address for a global variable or function. Use the
729 // name of the variable or function as the address value, possibly
730 // decorating it with GlobalVarAddrPrefix/Suffix or
731 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000732 if (isa<Function>(GV)) {
733 O << TAI->getFunctionAddrPrefix()
734 << Mang->getValueName(GV)
735 << TAI->getFunctionAddrSuffix();
736 } else {
737 O << TAI->getGlobalVarAddrPrefix()
738 << Mang->getValueName(GV)
739 << TAI->getGlobalVarAddrSuffix();
740 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000741 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000742 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000743 unsigned Opcode = CE->getOpcode();
744 switch (Opcode) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000745 case Instruction::GetElementPtr: {
746 // generate a symbolic expression for the byte address
747 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner83dfca82007-02-10 20:31:59 +0000748 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
749 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
750 idxVec.size())) {
Chris Lattner145569b2005-02-14 21:40:26 +0000751 if (Offset)
752 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000753 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000754 if (Offset > 0)
755 O << ") + " << Offset;
756 else if (Offset < 0)
757 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000758 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000759 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000760 }
761 break;
762 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000763 case Instruction::Trunc:
764 case Instruction::ZExt:
765 case Instruction::SExt:
766 case Instruction::FPTrunc:
767 case Instruction::FPExt:
768 case Instruction::UIToFP:
769 case Instruction::SIToFP:
770 case Instruction::FPToUI:
771 case Instruction::FPToSI:
772 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
773 break;
Chris Lattnerb9e41f52006-12-12 05:14:13 +0000774 case Instruction::BitCast:
775 return EmitConstantValueOnly(CE->getOperand(0));
776
Chris Lattner0c537da2006-12-12 05:18:19 +0000777 case Instruction::IntToPtr: {
778 // Handle casts to pointers by changing them into casts to the appropriate
779 // integer type. This promotes constant folding and simplifies this code.
780 Constant *Op = CE->getOperand(0);
781 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
782 return EmitConstantValueOnly(Op);
783 }
784
785
786 case Instruction::PtrToInt: {
Chris Lattner85492422006-07-29 01:57:19 +0000787 // Support only foldable casts to/from pointers that can be eliminated by
788 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000789 Constant *Op = CE->getOperand(0);
Chris Lattner0c537da2006-12-12 05:18:19 +0000790 const Type *Ty = CE->getType();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000791
Chris Lattner0c537da2006-12-12 05:18:19 +0000792 // We can emit the pointer value into this slot if the slot is an
793 // integer slot greater or equal to the size of the pointer.
Chris Lattner03c49532007-01-15 02:27:26 +0000794 if (Ty->isInteger() &&
Duncan Sands283207a2007-11-05 00:04:43 +0000795 TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
Chris Lattner85492422006-07-29 01:57:19 +0000796 return EmitConstantValueOnly(Op);
Chris Lattner85492422006-07-29 01:57:19 +0000797
798 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000799 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000800 break;
801 }
802 case Instruction::Add:
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000803 case Instruction::Sub:
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000804 case Instruction::And:
805 case Instruction::Or:
806 case Instruction::Xor:
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000807 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000808 EmitConstantValueOnly(CE->getOperand(0));
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000809 O << ")";
810 switch (Opcode) {
811 case Instruction::Add:
812 O << " + ";
813 break;
814 case Instruction::Sub:
815 O << " - ";
816 break;
817 case Instruction::And:
818 O << " & ";
819 break;
820 case Instruction::Or:
821 O << " | ";
822 break;
823 case Instruction::Xor:
824 O << " ^ ";
825 break;
826 default:
827 break;
828 }
829 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000830 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000831 O << ")";
832 break;
833 default:
834 assert(0 && "Unsupported operator!");
835 }
836 } else {
837 assert(0 && "Unknown constant value!");
838 }
839}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000840
Chris Lattner55a6d902005-11-10 18:06:33 +0000841/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000842/// the predicate isString is true.
843///
Chris Lattner55a6d902005-11-10 18:06:33 +0000844static void printAsCString(std::ostream &O, const ConstantArray *CVA,
845 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000846 assert(CVA->isString() && "Array is not string compatible!");
847
848 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000849 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000850 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000851 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskey1c055e82007-01-25 15:12:02 +0000852 printStringChar(O, C);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000853 }
854 O << "\"";
855}
856
Jeff Cohen24a62a92006-05-02 01:16:28 +0000857/// EmitString - Emit a zero-byte-terminated string constant.
858///
859void AsmPrinter::EmitString(const ConstantArray *CVA) const {
860 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000861 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000862 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000863 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000864 printAsCString(O, CVA, NumElts-1);
865 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000866 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000867 printAsCString(O, CVA, NumElts);
868 }
869 O << "\n";
870}
871
Chris Lattnerbb644e32005-11-21 07:51:36 +0000872/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Duncan Sands283207a2007-11-05 00:04:43 +0000873/// If Packed is false, pad to the ABI size.
874void AsmPrinter::EmitGlobalConstant(const Constant *CV, bool Packed) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000875 const TargetData *TD = TM.getTargetData();
Duncan Sands283207a2007-11-05 00:04:43 +0000876 unsigned Size = Packed ?
877 TD->getTypeStoreSize(CV->getType()) : TD->getABITypeSize(CV->getType());
Chris Lattner8452a1f2004-08-17 06:36:49 +0000878
Chris Lattner61753bf2004-10-16 18:19:26 +0000879 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Duncan Sands283207a2007-11-05 00:04:43 +0000880 EmitZeros(Size);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000881 return;
882 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
883 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000884 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000885 } else { // Not a string. Print the values in successive locations
Duncan Sands283207a2007-11-05 00:04:43 +0000886 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
887 EmitGlobalConstant(CVA->getOperand(i), false);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000888 }
889 return;
890 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
891 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000892 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000893 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000894 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
895 const Constant* field = CVS->getOperand(i);
896
897 // Check if padding is needed and insert one or more 0s.
Duncan Sands283207a2007-11-05 00:04:43 +0000898 uint64_t fieldSize = TD->getTypeStoreSize(field->getType());
Duncan Sandsf7ae8bd2007-11-05 18:03:02 +0000899 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
Chris Lattnerc473d8e2007-02-10 19:55:17 +0000900 - cvsLayout->getElementOffset(i)) - fieldSize;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000901 sizeSoFar += fieldSize + padSize;
902
Duncan Sandsf7ae8bd2007-11-05 18:03:02 +0000903 // Now print the actual field value without ABI size padding.
904 EmitGlobalConstant(field, true);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000905
Duncan Sandsf7ae8bd2007-11-05 18:03:02 +0000906 // Insert padding - this may include padding to increase the size of the
907 // current field up to the ABI size (if the struct is not packed) as well
908 // as padding to ensure that the next field starts at the right offset.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000909 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000910 }
Chris Lattnerb84892d2007-02-10 19:59:22 +0000911 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
Chris Lattner8452a1f2004-08-17 06:36:49 +0000912 "Layout of constant struct may be incorrect!");
913 return;
914 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
915 // FP Constants are printed as integer constants to avoid losing
916 // precision...
Chris Lattner8452a1f2004-08-17 06:36:49 +0000917 if (CFP->getType() == Type::DoubleTy) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000918 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
Dale Johannesen028084e2007-09-12 03:30:33 +0000919 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000920 if (TAI->getData64bitsDirective())
Dale Johannesen245dceb2007-09-11 18:32:33 +0000921 O << TAI->getData64bitsDirective() << i << "\t"
Jim Laskeya6211dc2006-09-06 18:34:40 +0000922 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000923 else if (TD->isBigEndian()) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000924 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000925 << "\t" << TAI->getCommentString()
926 << " double most significant word " << Val << "\n";
Dale Johannesen245dceb2007-09-11 18:32:33 +0000927 O << TAI->getData32bitsDirective() << unsigned(i)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000928 << "\t" << TAI->getCommentString()
929 << " double least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000930 } else {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000931 O << TAI->getData32bitsDirective() << unsigned(i)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000932 << "\t" << TAI->getCommentString()
933 << " double least significant word " << Val << "\n";
Dale Johannesen245dceb2007-09-11 18:32:33 +0000934 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000935 << "\t" << TAI->getCommentString()
936 << " double most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000937 }
938 return;
Dale Johannesen028084e2007-09-12 03:30:33 +0000939 } else if (CFP->getType() == Type::FloatTy) {
Dale Johannesen245dceb2007-09-11 18:32:33 +0000940 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
941 O << TAI->getData32bitsDirective()
Dale Johannesen028084e2007-09-12 03:30:33 +0000942 << CFP->getValueAPF().convertToAPInt().getZExtValue()
Jim Laskeya6211dc2006-09-06 18:34:40 +0000943 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000944 return;
Dale Johannesen028084e2007-09-12 03:30:33 +0000945 } else if (CFP->getType() == Type::X86_FP80Ty) {
946 // all long double variants are printed as hex
Dale Johannesen34aa41c2007-09-26 23:20:33 +0000947 // api needed to prevent premature destruction
948 APInt api = CFP->getValueAPF().convertToAPInt();
949 const uint64_t *p = api.getRawData();
Chris Lattnerf1a6c9f2008-01-27 06:09:28 +0000950 APFloat DoubleVal = CFP->getValueAPF();
951 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven);
Dale Johannesen028084e2007-09-12 03:30:33 +0000952 if (TD->isBigEndian()) {
953 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
954 << "\t" << TAI->getCommentString()
Chris Lattnerf1a6c9f2008-01-27 06:09:28 +0000955 << " long double most significant halfword of ~"
956 << DoubleVal.convertToDouble() << "\n";
Dale Johannesen028084e2007-09-12 03:30:33 +0000957 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
958 << "\t" << TAI->getCommentString()
959 << " long double next halfword\n";
960 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
961 << "\t" << TAI->getCommentString()
962 << " long double next halfword\n";
963 O << TAI->getData16bitsDirective() << uint16_t(p[0])
964 << "\t" << TAI->getCommentString()
965 << " long double next halfword\n";
966 O << TAI->getData16bitsDirective() << uint16_t(p[1])
967 << "\t" << TAI->getCommentString()
968 << " long double least significant halfword\n";
969 } else {
970 O << TAI->getData16bitsDirective() << uint16_t(p[1])
971 << "\t" << TAI->getCommentString()
Chris Lattnerf1a6c9f2008-01-27 06:09:28 +0000972 << " long double least significant halfword of ~"
973 << DoubleVal.convertToDouble() << "\n";
Dale Johannesen028084e2007-09-12 03:30:33 +0000974 O << TAI->getData16bitsDirective() << uint16_t(p[0])
975 << "\t" << TAI->getCommentString()
976 << " long double next halfword\n";
977 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
978 << "\t" << TAI->getCommentString()
979 << " long double next halfword\n";
980 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
981 << "\t" << TAI->getCommentString()
982 << " long double next halfword\n";
983 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
984 << "\t" << TAI->getCommentString()
985 << " long double most significant halfword\n";
986 }
Duncan Sands283207a2007-11-05 00:04:43 +0000987 EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty));
Dale Johannesen028084e2007-09-12 03:30:33 +0000988 return;
Dale Johannesen6472eb62007-10-11 23:32:15 +0000989 } else if (CFP->getType() == Type::PPC_FP128Ty) {
990 // all long double variants are printed as hex
991 // api needed to prevent premature destruction
992 APInt api = CFP->getValueAPF().convertToAPInt();
993 const uint64_t *p = api.getRawData();
994 if (TD->isBigEndian()) {
995 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
996 << "\t" << TAI->getCommentString()
997 << " long double most significant word\n";
998 O << TAI->getData32bitsDirective() << uint32_t(p[0])
999 << "\t" << TAI->getCommentString()
1000 << " long double next word\n";
1001 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
1002 << "\t" << TAI->getCommentString()
1003 << " long double next word\n";
1004 O << TAI->getData32bitsDirective() << uint32_t(p[1])
1005 << "\t" << TAI->getCommentString()
1006 << " long double least significant word\n";
1007 } else {
1008 O << TAI->getData32bitsDirective() << uint32_t(p[1])
1009 << "\t" << TAI->getCommentString()
1010 << " long double least significant word\n";
1011 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
1012 << "\t" << TAI->getCommentString()
1013 << " long double next word\n";
1014 O << TAI->getData32bitsDirective() << uint32_t(p[0])
1015 << "\t" << TAI->getCommentString()
1016 << " long double next word\n";
1017 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
1018 << "\t" << TAI->getCommentString()
1019 << " long double most significant word\n";
1020 }
1021 return;
Dale Johannesen028084e2007-09-12 03:30:33 +00001022 } else assert(0 && "Floating point constant type not handled");
Reid Spencere63b6512006-12-31 05:55:36 +00001023 } else if (CV->getType() == Type::Int64Ty) {
Chris Lattner8452a1f2004-08-17 06:36:49 +00001024 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00001025 uint64_t Val = CI->getZExtValue();
Misha Brukman835702a2005-04-21 22:36:52 +00001026
Jim Laskeya6211dc2006-09-06 18:34:40 +00001027 if (TAI->getData64bitsDirective())
1028 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +00001029 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001030 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
1031 << "\t" << TAI->getCommentString()
1032 << " Double-word most significant word " << Val << "\n";
1033 O << TAI->getData32bitsDirective() << unsigned(Val)
1034 << "\t" << TAI->getCommentString()
1035 << " Double-word least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +00001036 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001037 O << TAI->getData32bitsDirective() << unsigned(Val)
1038 << "\t" << TAI->getCommentString()
1039 << " Double-word least significant word " << Val << "\n";
1040 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
1041 << "\t" << TAI->getCommentString()
1042 << " Double-word most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +00001043 }
1044 return;
1045 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001046 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1047 const VectorType *PTy = CP->getType();
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001048
1049 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
Duncan Sands283207a2007-11-05 00:04:43 +00001050 EmitGlobalConstant(CP->getOperand(I), false);
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001051
1052 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +00001053 }
1054
1055 const Type *type = CV->getType();
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001056 printDataDirective(type);
Chris Lattnerbb644e32005-11-21 07:51:36 +00001057 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001058 O << "\n";
1059}
Chris Lattner061d9e22006-01-27 02:10:10 +00001060
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001061void
1062AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
1063 // Target doesn't support this yet!
1064 abort();
1065}
1066
Chris Lattnera32814b2006-09-26 23:59:50 +00001067/// PrintSpecial - Print information related to the specified machine instr
1068/// that is independent of the operand, and may be independent of the instr
1069/// itself. This can be useful for portably encoding the comment character
1070/// or other bits of target-specific knowledge into the asmstrings. The
1071/// syntax used is ${:comment}. Targets can override this to add support
1072/// for their own strange codes.
1073void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattner30b47082006-09-27 00:06:07 +00001074 if (!strcmp(Code, "private")) {
1075 O << TAI->getPrivateGlobalPrefix();
1076 } else if (!strcmp(Code, "comment")) {
Chris Lattnera32814b2006-09-26 23:59:50 +00001077 O << TAI->getCommentString();
1078 } else if (!strcmp(Code, "uid")) {
1079 // Assign a unique ID to this machine instruction.
1080 static const MachineInstr *LastMI = 0;
Chris Lattnera9428c42007-02-05 21:23:52 +00001081 static const Function *F = 0;
Chris Lattnera32814b2006-09-26 23:59:50 +00001082 static unsigned Counter = 0U-1;
Chris Lattnera9428c42007-02-05 21:23:52 +00001083
1084 // Comparing the address of MI isn't sufficient, because machineinstrs may
1085 // be allocated to the same address across functions.
1086 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1087
Chris Lattnera32814b2006-09-26 23:59:50 +00001088 // If this is a new machine instruction, bump the counter.
Chris Lattnera9428c42007-02-05 21:23:52 +00001089 if (LastMI != MI || F != ThisF) {
1090 ++Counter;
1091 LastMI = MI;
Chris Lattner89e66e02007-02-06 01:56:31 +00001092 F = ThisF;
Chris Lattnera9428c42007-02-05 21:23:52 +00001093 }
Chris Lattnera32814b2006-09-26 23:59:50 +00001094 O << Counter;
1095 } else {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001096 cerr << "Unknown special formatter '" << Code
1097 << "' for machine instr: " << *MI;
Chris Lattnera32814b2006-09-26 23:59:50 +00001098 exit(1);
1099 }
1100}
1101
1102
Chris Lattner061d9e22006-01-27 02:10:10 +00001103/// printInlineAsm - This method formats and prints the specified machine
1104/// instruction that is an inline asm.
1105void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +00001106 unsigned NumOperands = MI->getNumOperands();
1107
1108 // Count the number of register definitions.
1109 unsigned NumDefs = 0;
Dan Gohman9da02f52007-09-14 20:33:02 +00001110 for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef();
Chris Lattner45456d42006-09-05 20:02:51 +00001111 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +00001112 assert(NumDefs != NumOperands-1 && "No asm string?");
1113
1114 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001115
1116 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +00001117 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +00001118
Dale Johannesen2b3bc302008-01-29 02:21:21 +00001119 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1120 // These are useful to see where empty asm's wound up.
Chris Lattner3390cbe2006-07-28 00:17:20 +00001121 if (AsmStr[0] == 0) {
Dale Johannesen2b3bc302008-01-29 02:21:21 +00001122 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattner3390cbe2006-07-28 00:17:20 +00001123 return;
1124 }
1125
Jim Laskeya6211dc2006-09-06 18:34:40 +00001126 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +00001127
Bill Wendlinge21237e2007-01-16 03:42:04 +00001128 // The variant of the current asmprinter.
1129 int AsmPrinterVariant = TAI->getAssemblerDialect();
1130
Chris Lattneraa23fa92006-02-01 22:41:11 +00001131 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1132 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +00001133
Chris Lattneraa23fa92006-02-01 22:41:11 +00001134 while (*LastEmitted) {
1135 switch (*LastEmitted) {
1136 default: {
1137 // Not a special case, emit the string section literally.
1138 const char *LiteralEnd = LastEmitted+1;
1139 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +00001140 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +00001141 ++LiteralEnd;
1142 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1143 O.write(LastEmitted, LiteralEnd-LastEmitted);
1144 LastEmitted = LiteralEnd;
1145 break;
1146 }
Chris Lattnera633c3132006-05-05 21:47:05 +00001147 case '\n':
1148 ++LastEmitted; // Consume newline character.
Chris Lattnerb58f93f2007-04-30 17:00:18 +00001149 O << "\n"; // Indent code with newline.
Chris Lattnera633c3132006-05-05 21:47:05 +00001150 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001151 case '$': {
1152 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001153 bool Done = true;
1154
1155 // Handle escapes.
1156 switch (*LastEmitted) {
1157 default: Done = false; break;
1158 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +00001159 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1160 O << '$';
1161 ++LastEmitted; // Consume second '$' character.
1162 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001163 case '(': // $( -> same as GCC's { character.
1164 ++LastEmitted; // Consume '(' character.
1165 if (CurVariant != -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001166 cerr << "Nested variants found in inline asm string: '"
1167 << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001168 exit(1);
1169 }
1170 CurVariant = 0; // We're in the first variant now.
1171 break;
1172 case '|':
1173 ++LastEmitted; // consume '|' character.
1174 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001175 cerr << "Found '|' character outside of variant in inline asm "
1176 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001177 exit(1);
1178 }
1179 ++CurVariant; // We're in the next variant.
1180 break;
1181 case ')': // $) -> same as GCC's } char.
1182 ++LastEmitted; // consume ')' character.
1183 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001184 cerr << "Found '}' character outside of variant in inline asm "
1185 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001186 exit(1);
1187 }
1188 CurVariant = -1;
1189 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001190 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001191 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001192
1193 bool HasCurlyBraces = false;
1194 if (*LastEmitted == '{') { // ${variable}
1195 ++LastEmitted; // Consume '{' character.
1196 HasCurlyBraces = true;
1197 }
1198
1199 const char *IDStart = LastEmitted;
1200 char *IDEnd;
Chris Lattnerd39e3882007-01-23 00:36:17 +00001201 errno = 0;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001202 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1203 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001204 cerr << "Bad $ operand number in inline asm string: '"
1205 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001206 exit(1);
1207 }
1208 LastEmitted = IDEnd;
1209
Chris Lattner34f74c12006-02-06 22:17:23 +00001210 char Modifier[2] = { 0, 0 };
1211
Chris Lattneraa23fa92006-02-01 22:41:11 +00001212 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +00001213 // If we have curly braces, check for a modifier character. This
1214 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1215 if (*LastEmitted == ':') {
1216 ++LastEmitted; // Consume ':' character.
1217 if (*LastEmitted == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001218 cerr << "Bad ${:} expression in inline asm string: '"
1219 << AsmStr << "'\n";
Chris Lattner34f74c12006-02-06 22:17:23 +00001220 exit(1);
1221 }
1222
1223 Modifier[0] = *LastEmitted;
1224 ++LastEmitted; // Consume modifier character.
1225 }
1226
Chris Lattneraa23fa92006-02-01 22:41:11 +00001227 if (*LastEmitted != '}') {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001228 cerr << "Bad ${} expression in inline asm string: '"
1229 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001230 exit(1);
1231 }
1232 ++LastEmitted; // Consume '}' character.
1233 }
1234
1235 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001236 cerr << "Invalid $ operand number in inline asm string: '"
1237 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001238 exit(1);
1239 }
1240
Chris Lattner571d9642006-02-23 19:21:04 +00001241 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +00001242 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +00001243 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1244 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001245
1246 bool Error = false;
1247
Chris Lattner571d9642006-02-23 19:21:04 +00001248 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +00001249 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001250 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner81798412007-12-30 20:50:28 +00001251 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner5af3fde2006-02-24 19:50:58 +00001252 OpNo += (OpFlags >> 3) + 1;
1253 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001254
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001255 if (OpNo >= MI->getNumOperands()) {
1256 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +00001257 } else {
Chris Lattner81798412007-12-30 20:50:28 +00001258 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001259 ++OpNo; // Skip over the ID number.
1260
Dale Johannesen4646aa32007-11-05 21:20:28 +00001261 if (Modifier[0]=='l') // labels are target independent
Chris Lattnera5bb3702007-12-30 23:10:15 +00001262 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Chengc7990652008-02-28 00:43:03 +00001263 false, false, false);
Dale Johannesen4646aa32007-11-05 21:20:28 +00001264 else {
1265 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1266 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1267 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1268 Modifier[0] ? Modifier : 0);
1269 } else {
1270 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1271 Modifier[0] ? Modifier : 0);
1272 }
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001273 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001274 }
1275 if (Error) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001276 cerr << "Invalid operand found in inline asm: '"
1277 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001278 MI->dump();
1279 exit(1);
1280 }
Chris Lattner571d9642006-02-23 19:21:04 +00001281 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001282 break;
1283 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001284 }
1285 }
Jim Laskeya6211dc2006-09-06 18:34:40 +00001286 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +00001287}
1288
Jim Laskeyf9e54452007-01-26 14:34:52 +00001289/// printLabel - This method prints a local label used by debug and
1290/// exception handling tables.
1291void AsmPrinter::printLabel(const MachineInstr *MI) const {
Evan Cheng32e53472008-02-02 08:39:46 +00001292 O << TAI->getPrivateGlobalPrefix()
Chris Lattner81798412007-12-30 20:50:28 +00001293 << "label" << MI->getOperand(0).getImm() << ":\n";
Jim Laskeyf9e54452007-01-26 14:34:52 +00001294}
1295
Evan Chengd6e44ab2008-02-01 09:10:45 +00001296void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng32e53472008-02-02 08:39:46 +00001297 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chengd6e44ab2008-02-01 09:10:45 +00001298}
1299
Evan Chengefd142a2008-02-02 04:07:54 +00001300/// printDeclare - This method prints a local variable declaration used by
1301/// debug tables.
Evan Cheng2cb90682008-02-04 23:06:48 +00001302/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1303/// entry into dwarf table.
Evan Chengefd142a2008-02-02 04:07:54 +00001304void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Evan Cheng2cb90682008-02-04 23:06:48 +00001305 int FI = MI->getOperand(0).getIndex();
1306 GlobalValue *GV = MI->getOperand(1).getGlobal();
1307 MMI->RecordVariable(GV, FI);
Evan Chengefd142a2008-02-02 04:07:54 +00001308}
1309
Chris Lattneraa23fa92006-02-01 22:41:11 +00001310/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1311/// instruction, using the specified assembler variant. Targets should
1312/// overried this to format as appropriate.
1313bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +00001314 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +00001315 // Target doesn't support this yet!
1316 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +00001317}
Chris Lattner1d08c652006-02-24 20:21:58 +00001318
1319bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1320 unsigned AsmVariant,
1321 const char *ExtraCode) {
1322 // Target doesn't support this yet!
1323 return true;
1324}
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001325
1326/// printBasicBlockLabel - This method prints the label for the specified
1327/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +00001328void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Chengc7990652008-02-28 00:43:03 +00001329 bool printAlign,
Nate Begemanb9d4f832006-05-02 05:37:32 +00001330 bool printColon,
1331 bool printComment) const {
Evan Chengc7990652008-02-28 00:43:03 +00001332 if (printAlign) {
1333 unsigned Align = MBB->getAlignment();
1334 if (Align)
1335 EmitAlignment(Log2_32(Align));
1336 }
1337
Evan Chengcdf36092007-10-14 05:57:21 +00001338 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << "_"
1339 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +00001340 if (printColon)
1341 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +00001342 if (printComment && MBB->getBasicBlock())
Dan Gohman33d0ea22007-07-30 15:06:25 +00001343 O << '\t' << TAI->getCommentString() << ' '
1344 << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001345}
Nate Begeman984c1a42006-08-12 21:29:52 +00001346
Evan Cheng797d56f2007-11-09 01:32:10 +00001347/// printPICJumpTableSetLabel - This method prints a set label for the
1348/// specified MachineBasicBlock for a jumptable entry.
1349void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1350 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001351 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +00001352 return;
1353
Jim Laskeya6211dc2006-09-06 18:34:40 +00001354 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001355 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001356 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001357 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1358 << '_' << uid << '\n';
Nate Begeman984c1a42006-08-12 21:29:52 +00001359}
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001360
Evan Cheng797d56f2007-11-09 01:32:10 +00001361void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1362 const MachineBasicBlock *MBB) const {
Evan Cheng91f120f2006-11-01 09:23:08 +00001363 if (!TAI->getSetDirective())
1364 return;
1365
1366 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001367 << getFunctionNumber() << '_' << uid << '_' << uid2
1368 << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001369 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001370 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1371 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng91f120f2006-11-01 09:23:08 +00001372}
1373
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001374/// printDataDirective - This method prints the asm directive for the
1375/// specified type.
1376void AsmPrinter::printDataDirective(const Type *type) {
1377 const TargetData *TD = TM.getTargetData();
1378 switch (type->getTypeID()) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001379 case Type::IntegerTyID: {
1380 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1381 if (BitWidth <= 8)
1382 O << TAI->getData8bitsDirective();
1383 else if (BitWidth <= 16)
1384 O << TAI->getData16bitsDirective();
1385 else if (BitWidth <= 32)
1386 O << TAI->getData32bitsDirective();
1387 else if (BitWidth <= 64) {
1388 assert(TAI->getData64bitsDirective() &&
1389 "Target cannot handle 64-bit constant exprs!");
1390 O << TAI->getData64bitsDirective();
1391 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001392 break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001393 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001394 case Type::PointerTyID:
1395 if (TD->getPointerSize() == 8) {
1396 assert(TAI->getData64bitsDirective() &&
1397 "Target cannot handle 64-bit pointer exprs!");
1398 O << TAI->getData64bitsDirective();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001399 } else {
1400 O << TAI->getData32bitsDirective();
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001401 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001402 break;
1403 case Type::FloatTyID: case Type::DoubleTyID:
Dale Johannesen6bf69ed2007-09-28 18:06:58 +00001404 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001405 assert (0 && "Should have already output floating point constant.");
1406 default:
1407 assert (0 && "Can't handle printing this type of thing");
1408 break;
1409 }
1410}
Dale Johannesen915e1542007-02-16 01:54:53 +00001411