blob: bb66bd2ad5c3b0dbd3abce94d53ec5858c54785b [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera80ba712004-08-16 23:15:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng932f0222006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng246ae0d2006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman8cfa57b2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000018#include "llvm/Module.h"
Gordon Henriksence224772008-01-07 01:30:38 +000019#include "llvm/CodeGen/Collector.h"
20#include "llvm/CodeGen/CollectorMetadata.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman37efe672006-04-22 18:53:45 +000022#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskeyf1cdea12007-01-25 15:12:02 +000024#include "llvm/Support/CommandLine.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000025#include "llvm/Support/Mangler.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000026#include "llvm/Support/MathExtras.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000027#include "llvm/Support/Streams.h"
Jim Laskey563321a2006-09-06 18:34:40 +000028#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000029#include "llvm/Target/TargetData.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000030#include "llvm/Target/TargetLowering.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000031#include "llvm/Target/TargetMachine.h"
Evan Chengcc415862007-11-09 01:32:10 +000032#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner66099132006-02-01 22:41:11 +000033#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000034using namespace llvm;
35
Jim Laskeyf1cdea12007-01-25 15:12:02 +000036static cl::opt<bool>
37AsmVerbose("asm-verbose", cl::Hidden, cl::desc("Add comments to directives."));
38
Devang Patel19974732007-05-03 01:11:54 +000039char AsmPrinter::ID = 0;
Jim Laskeya0f3d172006-09-07 22:06:40 +000040AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
41 const TargetAsmInfo *T)
Evan Cheng347d39f2007-10-14 05:57:21 +000042 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o), TM(tm), TAI(T)
Jim Laskey563321a2006-09-06 18:34:40 +000043{}
Chris Lattnered138932005-12-13 06:32:10 +000044
Chris Lattner52f06702006-10-05 02:42:47 +000045std::string AsmPrinter::getSectionForFunction(const Function &F) const {
46 return TAI->getTextSection();
47}
48
Chris Lattnered138932005-12-13 06:32:10 +000049
Chris Lattner4632d7a2006-05-09 04:59:56 +000050/// SwitchToTextSection - Switch to the specified text section of the executable
51/// if we are not already in it!
Chris Lattnerac28fbd2005-11-21 07:06:27 +000052///
Chris Lattner4632d7a2006-05-09 04:59:56 +000053void AsmPrinter::SwitchToTextSection(const char *NewSection,
54 const GlobalValue *GV) {
Chris Lattnerac28fbd2005-11-21 07:06:27 +000055 std::string NS;
Chris Lattnera7090ae2006-05-09 05:24:50 +000056 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000057 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnera7090ae2006-05-09 05:24:50 +000058 else
59 NS = NewSection;
60
61 // If we're already in this section, we're done.
62 if (CurrentSection == NS) return;
Jeff Cohen51b776d2006-05-02 03:58:45 +000063
Chris Lattner7faec9b2006-05-09 05:33:48 +000064 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000065 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
66 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen51b776d2006-05-02 03:58:45 +000067
Chris Lattner7faec9b2006-05-09 05:33:48 +000068 CurrentSection = NS;
69
70 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000071 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattnerac28fbd2005-11-21 07:06:27 +000072}
73
Nate Begeman2f1ae882006-07-27 01:13:04 +000074/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner4632d7a2006-05-09 04:59:56 +000075/// if we are not already in it!
76///
77void AsmPrinter::SwitchToDataSection(const char *NewSection,
78 const GlobalValue *GV) {
79 std::string NS;
Chris Lattnerb81cb612006-05-09 05:23:12 +000080 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000081 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnerb81cb612006-05-09 05:23:12 +000082 else
83 NS = NewSection;
Chris Lattner4632d7a2006-05-09 04:59:56 +000084
Chris Lattnerb81cb612006-05-09 05:23:12 +000085 // If we're already in this section, we're done.
86 if (CurrentSection == NS) return;
87
Chris Lattner7faec9b2006-05-09 05:33:48 +000088 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000089 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
90 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner7faec9b2006-05-09 05:33:48 +000091
92 CurrentSection = NS;
Chris Lattner4632d7a2006-05-09 04:59:56 +000093
Chris Lattner7faec9b2006-05-09 05:33:48 +000094 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000095 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner4632d7a2006-05-09 04:59:56 +000096}
97
98
Gordon Henriksence224772008-01-07 01:30:38 +000099void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
100 MachineFunctionPass::getAnalysisUsage(AU);
101 AU.addRequired<CollectorModuleMetadata>();
102}
103
Chris Lattnera80ba712004-08-16 23:15:22 +0000104bool AsmPrinter::doInitialization(Module &M) {
Jim Laskey563321a2006-09-06 18:34:40 +0000105 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattner2c1b1592006-01-23 23:47:53 +0000106
Gordon Henriksence224772008-01-07 01:30:38 +0000107 CollectorModuleMetadata *CMM = getAnalysisToUpdate<CollectorModuleMetadata>();
108 assert(CMM && "AsmPrinter didn't require CollectorModuleMetadata?");
109 for (CollectorModuleMetadata::iterator I = CMM->begin(),
110 E = CMM->end(); I != E; ++I)
111 (*I)->beginAssembly(O, *this, *TAI);
112
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000113 if (!M.getModuleInlineAsm().empty())
Jim Laskey563321a2006-09-06 18:34:40 +0000114 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000115 << M.getModuleInlineAsm()
Jim Laskey563321a2006-09-06 18:34:40 +0000116 << "\n" << TAI->getCommentString()
117 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000118
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000119 SwitchToDataSection(""); // Reset back to no section.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000120
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000121 if (MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>()) {
122 MMI->AnalyzeModule(M);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000123 }
124
Chris Lattnera80ba712004-08-16 23:15:22 +0000125 return false;
126}
127
128bool AsmPrinter::doFinalization(Module &M) {
Rafael Espindola15404d02006-12-18 03:37:18 +0000129 if (TAI->getWeakRefDirective()) {
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000130 if (!ExtWeakSymbols.empty())
Rafael Espindola15404d02006-12-18 03:37:18 +0000131 SwitchToDataSection("");
132
133 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
134 e = ExtWeakSymbols.end(); i != e; ++i) {
135 const GlobalValue *GV = *i;
136 std::string Name = Mang->getValueName(GV);
137 O << TAI->getWeakRefDirective() << Name << "\n";
138 }
139 }
140
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000141 if (TAI->getSetDirective()) {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000142 if (!M.alias_empty())
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000143 SwitchToTextSection(TAI->getTextSection());
144
145 O << "\n";
146 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
147 I!=E; ++I) {
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000148 std::string Name = Mang->getValueName(I);
149 std::string Target;
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000150
151 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
152 Target = Mang->getValueName(GV);
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000153
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000154 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000155 O << "\t.globl\t" << Name << "\n";
156 else if (I->hasWeakLinkage())
157 O << TAI->getWeakRefDirective() << Name << "\n";
158 else if (!I->hasInternalLinkage())
159 assert(0 && "Invalid alias linkage");
160
161 O << TAI->getSetDirective() << Name << ", " << Target << "\n";
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000162
163 // If the aliasee has external weak linkage it can be referenced only by
164 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
165 // weak reference in such case.
166 if (GV->hasExternalWeakLinkage())
167 if (TAI->getWeakRefDirective())
168 O << TAI->getWeakRefDirective() << Target << "\n";
169 else
170 O << "\t.globl\t" << Target << "\n";
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000171 }
172 }
173
Gordon Henriksence224772008-01-07 01:30:38 +0000174 CollectorModuleMetadata *CMM = getAnalysisToUpdate<CollectorModuleMetadata>();
175 assert(CMM && "AsmPrinter didn't require CollectorModuleMetadata?");
176 for (CollectorModuleMetadata::iterator I = CMM->end(),
177 E = CMM->begin(); I != E; )
178 (*--I)->finishAssembly(O, *this, *TAI);
179
Chris Lattnera80ba712004-08-16 23:15:22 +0000180 delete Mang; Mang = 0;
181 return false;
182}
183
Bill Wendlingce613282007-09-18 09:10:16 +0000184std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
Bill Wendling6e198962007-09-18 01:47:22 +0000185 assert(MF && "No machine function?");
Bill Wendling5f19cf52007-09-18 05:03:44 +0000186 return Mang->makeNameProper(MF->getFunction()->getName() + ".eh",
187 TAI->getGlobalPrefix());
Bill Wendling6e198962007-09-18 01:47:22 +0000188}
189
Chris Lattner25045bd2005-11-21 07:51:36 +0000190void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000191 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +0000192 CurrentFnName = Mang->getValueName(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000193 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +0000194}
195
Chris Lattner3b4fd322005-11-21 08:25:09 +0000196/// EmitConstantPool - Print to the current output stream assembly
197/// representations of the constants in the constant pool MCP. This is
198/// used to print out constants which have been "spilled to memory" by
199/// the code generator.
200///
201void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000202 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000203 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000204
205 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
206 // in special sections.
207 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
208 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
209 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
210 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Chengd6594ae2006-09-12 21:00:35 +0000211 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000212 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng2d2cec12006-06-29 00:26:09 +0000213 MachineConstantPoolEntry CPE = CP[i];
Evan Chengd5a99d72006-09-14 07:35:00 +0000214 const Type *Ty = CPE.getType();
Jim Laskey563321a2006-09-06 18:34:40 +0000215 if (TAI->getFourByteConstantSection() &&
Duncan Sandsca0ed742007-11-05 00:04:43 +0000216 TM.getTargetData()->getABITypeSize(Ty) == 4)
Evan Cheng2d2cec12006-06-29 00:26:09 +0000217 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000218 else if (TAI->getEightByteConstantSection() &&
Duncan Sandsca0ed742007-11-05 00:04:43 +0000219 TM.getTargetData()->getABITypeSize(Ty) == 8)
Evan Cheng2d2cec12006-06-29 00:26:09 +0000220 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000221 else if (TAI->getSixteenByteConstantSection() &&
Duncan Sandsca0ed742007-11-05 00:04:43 +0000222 TM.getTargetData()->getABITypeSize(Ty) == 16)
Evan Cheng2d2cec12006-06-29 00:26:09 +0000223 SixteenByteCPs.push_back(std::make_pair(CPE, i));
224 else
225 OtherCPs.push_back(std::make_pair(CPE, i));
226 }
227
228 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskey563321a2006-09-06 18:34:40 +0000229 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
230 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
231 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
232 SixteenByteCPs);
233 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000234}
235
236void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
237 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
238 if (CP.empty()) return;
239
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000240 SwitchToDataSection(Section);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000241 EmitAlignment(Alignment);
242 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000243 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
244 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Chengd5a99d72006-09-14 07:35:00 +0000245 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
246 if (CP[i].first.isMachineConstantPoolEntry())
Evan Chengd6594ae2006-09-12 21:00:35 +0000247 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Chengd5a99d72006-09-14 07:35:00 +0000248 else
Evan Chengd6594ae2006-09-12 21:00:35 +0000249 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000250 if (i != e-1) {
Evan Chengd5a99d72006-09-14 07:35:00 +0000251 const Type *Ty = CP[i].first.getType();
Evan Cheng2d2cec12006-06-29 00:26:09 +0000252 unsigned EntSize =
Duncan Sandsca0ed742007-11-05 00:04:43 +0000253 TM.getTargetData()->getABITypeSize(Ty);
Evan Chengd5a99d72006-09-14 07:35:00 +0000254 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattner3029f922006-02-09 04:46:04 +0000255 // Emit inter-object padding for alignment.
Evan Chengd5a99d72006-09-14 07:35:00 +0000256 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattner3029f922006-02-09 04:46:04 +0000257 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000258 }
259}
260
Nate Begeman37efe672006-04-22 18:53:45 +0000261/// EmitJumpTableInfo - Print assembly representations of the jump tables used
262/// by the current function to the current output stream.
263///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000264void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
265 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000266 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
267 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000268
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000269 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000270
Nate Begeman2f1ae882006-07-27 01:13:04 +0000271 // Pick the directive to use to print the jump table entries, and switch to
272 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000273 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000274
275 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
276 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
277 !JumpTableDataSection) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000278 // In PIC mode, we need to emit the jump table to the same section as the
279 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000280 // We should also do if the section name is NULL.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000281 const Function *F = MF.getFunction();
282 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000283 } else {
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000284 SwitchToDataSection(JumpTableDataSection);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000285 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000286
287 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000288
Nate Begeman37efe672006-04-22 18:53:45 +0000289 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000290 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000291
292 // If this jump table was deleted, ignore it.
293 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000294
295 // For PIC codegen, if possible we want to use the SetDirective to reduce
296 // the number of relocations the assembler will generate for the jump table.
297 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000298 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000299 if (TAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000300 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000301 if (EmittedSets.insert(JTBBs[ii]))
302 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000303
Chris Lattner393a8ee2007-01-18 01:12:56 +0000304 // On some targets (e.g. darwin) we want to emit two consequtive labels
305 // before each jump table. The first label is never referenced, but tells
306 // the assembler and linker the extents of the jump table object. The
307 // second label is actually referenced by the code.
308 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
Evan Cheng347d39f2007-10-14 05:57:21 +0000309 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Chris Lattner393a8ee2007-01-18 01:12:56 +0000310
Evan Cheng347d39f2007-10-14 05:57:21 +0000311 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
312 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000313
Nate Begeman37efe672006-04-22 18:53:45 +0000314 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000315 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000316 O << '\n';
317 }
318 }
319}
320
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000321void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
322 const MachineBasicBlock *MBB,
323 unsigned uid) const {
324 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
325
326 // Use JumpTableDirective otherwise honor the entry size from the jump table
327 // info.
328 const char *JTEntryDirective = TAI->getJumpTableDirective();
329 bool HadJTEntryDirective = JTEntryDirective != NULL;
330 if (!HadJTEntryDirective) {
331 JTEntryDirective = MJTI->getEntrySize() == 4 ?
332 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
333 }
334
335 O << JTEntryDirective << ' ';
336
337 // If we have emitted set directives for the jump table entries, print
338 // them rather than the entries themselves. If we're emitting PIC, then
339 // emit the table entries as differences between two text section labels.
340 // If we're emitting non-PIC code, then emit the entries as direct
341 // references to the target basic blocks.
342 if (IsPic) {
343 if (TAI->getSetDirective()) {
344 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
345 << '_' << uid << "_set_" << MBB->getNumber();
346 } else {
347 printBasicBlockLabel(MBB, false, false);
348 // If the arch uses custom Jump Table directives, don't calc relative to
349 // JT
350 if (!HadJTEntryDirective)
351 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
352 << getFunctionNumber() << '_' << uid;
353 }
354 } else {
355 printBasicBlockLabel(MBB, false, false);
356 }
357}
358
359
Chris Lattnered138932005-12-13 06:32:10 +0000360/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
361/// special global used by LLVM. If so, emit it and return true, otherwise
362/// do nothing and return false.
363bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000364 if (GV->getName() == "llvm.used") {
365 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
366 EmitLLVMUsedList(GV->getInitializer());
367 return true;
368 }
369
Jim Laskey78098112006-03-07 22:00:35 +0000370 // Ignore debug and non-emitted data.
371 if (GV->getSection() == "llvm.metadata") return true;
372
373 if (!GV->hasAppendingLinkage()) return false;
374
375 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000376
Evan Cheng916d07c2007-06-04 20:39:18 +0000377 const TargetData *TD = TM.getTargetData();
378 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner5166b822006-01-12 19:17:23 +0000379 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000380 SwitchToDataSection(TAI->getStaticCtorsSection());
Evan Cheng916d07c2007-06-04 20:39:18 +0000381 EmitAlignment(Align, 0);
Chris Lattnered138932005-12-13 06:32:10 +0000382 EmitXXStructorList(GV->getInitializer());
383 return true;
384 }
385
Chris Lattner5166b822006-01-12 19:17:23 +0000386 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000387 SwitchToDataSection(TAI->getStaticDtorsSection());
Evan Cheng916d07c2007-06-04 20:39:18 +0000388 EmitAlignment(Align, 0);
Chris Lattnered138932005-12-13 06:32:10 +0000389 EmitXXStructorList(GV->getInitializer());
390 return true;
391 }
392
393 return false;
394}
395
Chris Lattnercb05af82006-09-26 03:38:18 +0000396/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
397/// global in the specified llvm.used list as being used with this directive.
398void AsmPrinter::EmitLLVMUsedList(Constant *List) {
399 const char *Directive = TAI->getUsedDirective();
400
401 // Should be an array of 'sbyte*'.
402 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
403 if (InitList == 0) return;
404
405 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
406 O << Directive;
407 EmitConstantValueOnly(InitList->getOperand(i));
408 O << "\n";
409 }
410}
411
Chris Lattnered138932005-12-13 06:32:10 +0000412/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
413/// function pointers, ignoring the init priority.
414void AsmPrinter::EmitXXStructorList(Constant *List) {
415 // Should be an array of '{ int, void ()* }' structs. The first value is the
416 // init priority, which we ignore.
417 if (!isa<ConstantArray>(List)) return;
418 ConstantArray *InitList = cast<ConstantArray>(List);
419 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
420 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
421 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000422
423 if (CS->getOperand(1)->isNullValue())
424 return; // Found a null terminator, exit printing.
425 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000426 EmitGlobalConstant(CS->getOperand(1));
427 }
428}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000429
Jim Laskeya1a19f82006-10-17 13:41:07 +0000430/// getGlobalLinkName - Returns the asm/link name of of the specified
431/// global variable. Should be overridden by each target asm printer to
432/// generate the appropriate value.
Jim Laskey99e41ee2006-10-17 17:17:24 +0000433const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
434 std::string LinkName;
Jim Laskey03742482006-11-20 20:29:06 +0000435
436 if (isa<Function>(GV)) {
437 LinkName += TAI->getFunctionAddrPrefix();
438 LinkName += Mang->getValueName(GV);
439 LinkName += TAI->getFunctionAddrSuffix();
440 } else {
441 LinkName += TAI->getGlobalVarAddrPrefix();
442 LinkName += Mang->getValueName(GV);
443 LinkName += TAI->getGlobalVarAddrSuffix();
444 }
445
Jim Laskey99e41ee2006-10-17 17:17:24 +0000446 return LinkName;
Jim Laskeya1a19f82006-10-17 13:41:07 +0000447}
448
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000449/// EmitExternalGlobal - Emit the external reference to a global variable.
450/// Should be overridden if an indirect reference should be used.
451void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
452 O << getGlobalLinkName(GV);
453}
454
455
456
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000457//===----------------------------------------------------------------------===//
458/// LEB 128 number encoding.
459
460/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
461/// representing an unsigned leb128 value.
462void AsmPrinter::PrintULEB128(unsigned Value) const {
463 do {
464 unsigned Byte = Value & 0x7f;
465 Value >>= 7;
466 if (Value) Byte |= 0x80;
467 O << "0x" << std::hex << Byte << std::dec;
468 if (Value) O << ", ";
469 } while (Value);
470}
471
472/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
473/// value.
474unsigned AsmPrinter::SizeULEB128(unsigned Value) {
475 unsigned Size = 0;
476 do {
477 Value >>= 7;
478 Size += sizeof(int8_t);
479 } while (Value);
480 return Size;
481}
482
483/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
484/// representing a signed leb128 value.
485void AsmPrinter::PrintSLEB128(int Value) const {
486 int Sign = Value >> (8 * sizeof(Value) - 1);
487 bool IsMore;
488
489 do {
490 unsigned Byte = Value & 0x7f;
491 Value >>= 7;
492 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
493 if (IsMore) Byte |= 0x80;
494 O << "0x" << std::hex << Byte << std::dec;
495 if (IsMore) O << ", ";
496 } while (IsMore);
497}
498
499/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
500/// value.
501unsigned AsmPrinter::SizeSLEB128(int Value) {
502 unsigned Size = 0;
503 int Sign = Value >> (8 * sizeof(Value) - 1);
504 bool IsMore;
505
506 do {
507 unsigned Byte = Value & 0x7f;
508 Value >>= 7;
509 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
510 Size += sizeof(int8_t);
511 } while (IsMore);
512 return Size;
513}
514
515//===--------------------------------------------------------------------===//
516// Emission and print routines
517//
518
519/// PrintHex - Print a value as a hexidecimal value.
520///
521void AsmPrinter::PrintHex(int Value) const {
522 O << "0x" << std::hex << Value << std::dec;
523}
524
525/// EOL - Print a newline character to asm stream. If a comment is present
526/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000527void AsmPrinter::EOL() const {
528 O << "\n";
529}
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000530void AsmPrinter::EOL(const std::string &Comment) const {
531 if (AsmVerbose && !Comment.empty()) {
532 O << "\t"
533 << TAI->getCommentString()
534 << " "
535 << Comment;
536 }
537 O << "\n";
538}
539
540/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
541/// unsigned leb128 value.
542void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
543 if (TAI->hasLEB128()) {
544 O << "\t.uleb128\t"
545 << Value;
546 } else {
547 O << TAI->getData8bitsDirective();
548 PrintULEB128(Value);
549 }
550}
551
552/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
553/// signed leb128 value.
554void AsmPrinter::EmitSLEB128Bytes(int Value) const {
555 if (TAI->hasLEB128()) {
556 O << "\t.sleb128\t"
557 << Value;
558 } else {
559 O << TAI->getData8bitsDirective();
560 PrintSLEB128(Value);
561 }
562}
563
564/// EmitInt8 - Emit a byte directive and value.
565///
566void AsmPrinter::EmitInt8(int Value) const {
567 O << TAI->getData8bitsDirective();
568 PrintHex(Value & 0xFF);
569}
570
571/// EmitInt16 - Emit a short directive and value.
572///
573void AsmPrinter::EmitInt16(int Value) const {
574 O << TAI->getData16bitsDirective();
575 PrintHex(Value & 0xFFFF);
576}
577
578/// EmitInt32 - Emit a long directive and value.
579///
580void AsmPrinter::EmitInt32(int Value) const {
581 O << TAI->getData32bitsDirective();
582 PrintHex(Value);
583}
584
585/// EmitInt64 - Emit a long long directive and value.
586///
587void AsmPrinter::EmitInt64(uint64_t Value) const {
588 if (TAI->getData64bitsDirective()) {
589 O << TAI->getData64bitsDirective();
590 PrintHex(Value);
591 } else {
592 if (TM.getTargetData()->isBigEndian()) {
593 EmitInt32(unsigned(Value >> 32)); O << "\n";
594 EmitInt32(unsigned(Value));
595 } else {
596 EmitInt32(unsigned(Value)); O << "\n";
597 EmitInt32(unsigned(Value >> 32));
598 }
599 }
600}
601
602/// toOctal - Convert the low order bits of X into an octal digit.
603///
604static inline char toOctal(int X) {
605 return (X&7)+'0';
606}
607
608/// printStringChar - Print a char, escaped if necessary.
609///
610static void printStringChar(std::ostream &O, unsigned char C) {
611 if (C == '"') {
612 O << "\\\"";
613 } else if (C == '\\') {
614 O << "\\\\";
615 } else if (isprint(C)) {
616 O << C;
617 } else {
618 switch(C) {
619 case '\b': O << "\\b"; break;
620 case '\f': O << "\\f"; break;
621 case '\n': O << "\\n"; break;
622 case '\r': O << "\\r"; break;
623 case '\t': O << "\\t"; break;
624 default:
625 O << '\\';
626 O << toOctal(C >> 6);
627 O << toOctal(C >> 3);
628 O << toOctal(C >> 0);
629 break;
630 }
631 }
632}
633
634/// EmitString - Emit a string with quotes and a null terminator.
635/// Special characters are emitted properly.
636/// \literal (Eg. '\t') \endliteral
637void AsmPrinter::EmitString(const std::string &String) const {
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000638 const char* AscizDirective = TAI->getAscizDirective();
639 if (AscizDirective)
640 O << AscizDirective;
641 else
642 O << TAI->getAsciiDirective();
643 O << "\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000644 for (unsigned i = 0, N = String.size(); i < N; ++i) {
645 unsigned char C = String[i];
646 printStringChar(O, C);
647 }
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000648 if (AscizDirective)
649 O << "\"";
650 else
651 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000652}
653
654
Dan Gohman189f80d2007-09-24 20:58:13 +0000655/// EmitFile - Emit a .file directive.
656void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
657 O << "\t.file\t" << Number << " \"";
658 for (unsigned i = 0, N = Name.size(); i < N; ++i) {
659 unsigned char C = Name[i];
660 printStringChar(O, C);
661 }
662 O << "\"";
663}
664
665
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000666//===----------------------------------------------------------------------===//
667
Chris Lattner3a420532007-05-31 18:57:45 +0000668// EmitAlignment - Emit an alignment directive to the specified power of
669// two boundary. For example, if you pass in 3 here, you will get an 8
670// byte alignment. If a global value is specified, and if that global has
671// an explicit alignment requested, it will unconditionally override the
672// alignment request. However, if ForcedAlignBits is specified, this value
673// has final say: the ultimate alignment will be the max of ForcedAlignBits
674// and the alignment computed with NumBits and the global.
675//
676// The algorithm is:
677// Align = NumBits;
678// if (GV && GV->hasalignment) Align = GV->getalignment();
679// Align = std::max(Align, ForcedAlignBits);
680//
681void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng73a259a2007-07-25 23:35:07 +0000682 unsigned ForcedAlignBits, bool UseFillExpr,
683 unsigned FillValue) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000684 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000685 NumBits = Log2_32(GV->getAlignment());
686 NumBits = std::max(NumBits, ForcedAlignBits);
687
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000688 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskey563321a2006-09-06 18:34:40 +0000689 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng73a259a2007-07-25 23:35:07 +0000690 O << TAI->getAlignDirective() << NumBits;
691 if (UseFillExpr) O << ",0x" << std::hex << FillValue << std::dec;
692 O << "\n";
Chris Lattnerbfddc202004-08-17 19:14:29 +0000693}
694
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000695
Chris Lattner25045bd2005-11-21 07:51:36 +0000696/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000697///
Chris Lattner25045bd2005-11-21 07:51:36 +0000698void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000699 if (NumZeros) {
Jim Laskey563321a2006-09-06 18:34:40 +0000700 if (TAI->getZeroDirective()) {
701 O << TAI->getZeroDirective() << NumZeros;
702 if (TAI->getZeroDirectiveSuffix())
703 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000704 O << "\n";
705 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000706 for (; NumZeros; --NumZeros)
Jim Laskey563321a2006-09-06 18:34:40 +0000707 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000708 }
709 }
710}
711
Chris Lattnera80ba712004-08-16 23:15:22 +0000712// Print out the specified constant, without a storage class. Only the
713// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000714void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000715 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000716 O << "0";
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000717 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattnera875df32007-01-12 18:15:09 +0000718 O << CI->getZExtValue();
Reid Spencerb83eb642006-10-20 07:07:24 +0000719 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000720 // This is a constant address for a global variable or function. Use the
721 // name of the variable or function as the address value, possibly
722 // decorating it with GlobalVarAddrPrefix/Suffix or
723 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskey563321a2006-09-06 18:34:40 +0000724 if (isa<Function>(GV)) {
725 O << TAI->getFunctionAddrPrefix()
726 << Mang->getValueName(GV)
727 << TAI->getFunctionAddrSuffix();
728 } else {
729 O << TAI->getGlobalVarAddrPrefix()
730 << Mang->getValueName(GV)
731 << TAI->getGlobalVarAddrSuffix();
732 }
Duraid Madina855a5192005-04-02 12:21:51 +0000733 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000734 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000735 unsigned Opcode = CE->getOpcode();
736 switch (Opcode) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000737 case Instruction::GetElementPtr: {
738 // generate a symbolic expression for the byte address
739 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000740 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
741 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
742 idxVec.size())) {
Chris Lattner27e19212005-02-14 21:40:26 +0000743 if (Offset)
744 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000745 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000746 if (Offset > 0)
747 O << ") + " << Offset;
748 else if (Offset < 0)
749 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000750 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000751 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000752 }
753 break;
754 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000755 case Instruction::Trunc:
756 case Instruction::ZExt:
757 case Instruction::SExt:
758 case Instruction::FPTrunc:
759 case Instruction::FPExt:
760 case Instruction::UIToFP:
761 case Instruction::SIToFP:
762 case Instruction::FPToUI:
763 case Instruction::FPToSI:
764 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
765 break;
Chris Lattnercb0a6812006-12-12 05:14:13 +0000766 case Instruction::BitCast:
767 return EmitConstantValueOnly(CE->getOperand(0));
768
Chris Lattnerddc94012006-12-12 05:18:19 +0000769 case Instruction::IntToPtr: {
770 // Handle casts to pointers by changing them into casts to the appropriate
771 // integer type. This promotes constant folding and simplifies this code.
772 Constant *Op = CE->getOperand(0);
773 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
774 return EmitConstantValueOnly(Op);
775 }
776
777
778 case Instruction::PtrToInt: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000779 // Support only foldable casts to/from pointers that can be eliminated by
780 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000781 Constant *Op = CE->getOperand(0);
Chris Lattnerddc94012006-12-12 05:18:19 +0000782 const Type *Ty = CE->getType();
Chris Lattnera80ba712004-08-16 23:15:22 +0000783
Chris Lattnerddc94012006-12-12 05:18:19 +0000784 // We can emit the pointer value into this slot if the slot is an
785 // integer slot greater or equal to the size of the pointer.
Chris Lattner42a75512007-01-15 02:27:26 +0000786 if (Ty->isInteger() &&
Duncan Sandsca0ed742007-11-05 00:04:43 +0000787 TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
Chris Lattner4a6bd332006-07-29 01:57:19 +0000788 return EmitConstantValueOnly(Op);
Chris Lattner4a6bd332006-07-29 01:57:19 +0000789
790 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000791 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000792 break;
793 }
794 case Instruction::Add:
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000795 case Instruction::Sub:
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000796 case Instruction::And:
797 case Instruction::Or:
798 case Instruction::Xor:
Chris Lattnera80ba712004-08-16 23:15:22 +0000799 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000800 EmitConstantValueOnly(CE->getOperand(0));
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000801 O << ")";
802 switch (Opcode) {
803 case Instruction::Add:
804 O << " + ";
805 break;
806 case Instruction::Sub:
807 O << " - ";
808 break;
809 case Instruction::And:
810 O << " & ";
811 break;
812 case Instruction::Or:
813 O << " | ";
814 break;
815 case Instruction::Xor:
816 O << " ^ ";
817 break;
818 default:
819 break;
820 }
821 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000822 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000823 O << ")";
824 break;
825 default:
826 assert(0 && "Unsupported operator!");
827 }
828 } else {
829 assert(0 && "Unknown constant value!");
830 }
831}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000832
Chris Lattner2980cef2005-11-10 18:06:33 +0000833/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000834/// the predicate isString is true.
835///
Chris Lattner2980cef2005-11-10 18:06:33 +0000836static void printAsCString(std::ostream &O, const ConstantArray *CVA,
837 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000838 assert(CVA->isString() && "Array is not string compatible!");
839
840 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000841 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000842 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000843 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000844 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000845 }
846 O << "\"";
847}
848
Jeff Cohenc884db42006-05-02 01:16:28 +0000849/// EmitString - Emit a zero-byte-terminated string constant.
850///
851void AsmPrinter::EmitString(const ConstantArray *CVA) const {
852 unsigned NumElts = CVA->getNumOperands();
Jim Laskey563321a2006-09-06 18:34:40 +0000853 if (TAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000854 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskey563321a2006-09-06 18:34:40 +0000855 O << TAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000856 printAsCString(O, CVA, NumElts-1);
857 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000858 O << TAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000859 printAsCString(O, CVA, NumElts);
860 }
861 O << "\n";
862}
863
Chris Lattner25045bd2005-11-21 07:51:36 +0000864/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Duncan Sandsca0ed742007-11-05 00:04:43 +0000865/// If Packed is false, pad to the ABI size.
866void AsmPrinter::EmitGlobalConstant(const Constant *CV, bool Packed) {
Owen Andersona69571c2006-05-03 01:29:57 +0000867 const TargetData *TD = TM.getTargetData();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000868 unsigned Size = Packed ?
869 TD->getTypeStoreSize(CV->getType()) : TD->getABITypeSize(CV->getType());
Chris Lattner1b7e2352004-08-17 06:36:49 +0000870
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000871 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Duncan Sandsca0ed742007-11-05 00:04:43 +0000872 EmitZeros(Size);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000873 return;
874 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
875 if (CVA->isString()) {
Jeff Cohenc884db42006-05-02 01:16:28 +0000876 EmitString(CVA);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000877 } else { // Not a string. Print the values in successive locations
Duncan Sandsca0ed742007-11-05 00:04:43 +0000878 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
879 EmitGlobalConstant(CVA->getOperand(i), false);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000880 }
881 return;
882 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
883 // Print the fields in successive locations. Pad to align if needed!
Owen Andersona69571c2006-05-03 01:29:57 +0000884 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000885 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000886 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
887 const Constant* field = CVS->getOperand(i);
888
889 // Check if padding is needed and insert one or more 0s.
Duncan Sandsca0ed742007-11-05 00:04:43 +0000890 uint64_t fieldSize = TD->getTypeStoreSize(field->getType());
Duncan Sands0c8a13b2007-11-05 18:03:02 +0000891 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
Chris Lattnerb1919e22007-02-10 19:55:17 +0000892 - cvsLayout->getElementOffset(i)) - fieldSize;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000893 sizeSoFar += fieldSize + padSize;
894
Duncan Sands0c8a13b2007-11-05 18:03:02 +0000895 // Now print the actual field value without ABI size padding.
896 EmitGlobalConstant(field, true);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000897
Duncan Sands0c8a13b2007-11-05 18:03:02 +0000898 // Insert padding - this may include padding to increase the size of the
899 // current field up to the ABI size (if the struct is not packed) as well
900 // as padding to ensure that the next field starts at the right offset.
Chris Lattner25045bd2005-11-21 07:51:36 +0000901 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000902 }
Chris Lattnerb0c39a32007-02-10 19:59:22 +0000903 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
Chris Lattner1b7e2352004-08-17 06:36:49 +0000904 "Layout of constant struct may be incorrect!");
905 return;
906 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
907 // FP Constants are printed as integer constants to avoid losing
908 // precision...
Chris Lattner1b7e2352004-08-17 06:36:49 +0000909 if (CFP->getType() == Type::DoubleTy) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000910 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000911 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
Jim Laskey563321a2006-09-06 18:34:40 +0000912 if (TAI->getData64bitsDirective())
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000913 O << TAI->getData64bitsDirective() << i << "\t"
Jim Laskey563321a2006-09-06 18:34:40 +0000914 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000915 else if (TD->isBigEndian()) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000916 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Jim Laskey563321a2006-09-06 18:34:40 +0000917 << "\t" << TAI->getCommentString()
918 << " double most significant word " << Val << "\n";
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000919 O << TAI->getData32bitsDirective() << unsigned(i)
Jim Laskey563321a2006-09-06 18:34:40 +0000920 << "\t" << TAI->getCommentString()
921 << " double least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000922 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000923 O << TAI->getData32bitsDirective() << unsigned(i)
Jim Laskey563321a2006-09-06 18:34:40 +0000924 << "\t" << TAI->getCommentString()
925 << " double least significant word " << Val << "\n";
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000926 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Jim Laskey563321a2006-09-06 18:34:40 +0000927 << "\t" << TAI->getCommentString()
928 << " double most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000929 }
930 return;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000931 } else if (CFP->getType() == Type::FloatTy) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000932 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
933 O << TAI->getData32bitsDirective()
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000934 << CFP->getValueAPF().convertToAPInt().getZExtValue()
Jim Laskey563321a2006-09-06 18:34:40 +0000935 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000936 return;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000937 } else if (CFP->getType() == Type::X86_FP80Ty) {
938 // all long double variants are printed as hex
Dale Johannesen693717f2007-09-26 23:20:33 +0000939 // api needed to prevent premature destruction
940 APInt api = CFP->getValueAPF().convertToAPInt();
941 const uint64_t *p = api.getRawData();
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000942 if (TD->isBigEndian()) {
943 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
944 << "\t" << TAI->getCommentString()
945 << " long double most significant halfword\n";
946 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
947 << "\t" << TAI->getCommentString()
948 << " long double next halfword\n";
949 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
950 << "\t" << TAI->getCommentString()
951 << " long double next halfword\n";
952 O << TAI->getData16bitsDirective() << uint16_t(p[0])
953 << "\t" << TAI->getCommentString()
954 << " long double next halfword\n";
955 O << TAI->getData16bitsDirective() << uint16_t(p[1])
956 << "\t" << TAI->getCommentString()
957 << " long double least significant halfword\n";
958 } else {
959 O << TAI->getData16bitsDirective() << uint16_t(p[1])
960 << "\t" << TAI->getCommentString()
961 << " long double least significant halfword\n";
962 O << TAI->getData16bitsDirective() << uint16_t(p[0])
963 << "\t" << TAI->getCommentString()
964 << " long double next halfword\n";
965 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
966 << "\t" << TAI->getCommentString()
967 << " long double next halfword\n";
968 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
969 << "\t" << TAI->getCommentString()
970 << " long double next halfword\n";
971 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
972 << "\t" << TAI->getCommentString()
973 << " long double most significant halfword\n";
974 }
Duncan Sandsca0ed742007-11-05 00:04:43 +0000975 EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty));
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000976 return;
Dale Johannesenfcf4d242007-10-11 23:32:15 +0000977 } else if (CFP->getType() == Type::PPC_FP128Ty) {
978 // all long double variants are printed as hex
979 // api needed to prevent premature destruction
980 APInt api = CFP->getValueAPF().convertToAPInt();
981 const uint64_t *p = api.getRawData();
982 if (TD->isBigEndian()) {
983 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
984 << "\t" << TAI->getCommentString()
985 << " long double most significant word\n";
986 O << TAI->getData32bitsDirective() << uint32_t(p[0])
987 << "\t" << TAI->getCommentString()
988 << " long double next word\n";
989 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
990 << "\t" << TAI->getCommentString()
991 << " long double next word\n";
992 O << TAI->getData32bitsDirective() << uint32_t(p[1])
993 << "\t" << TAI->getCommentString()
994 << " long double least significant word\n";
995 } else {
996 O << TAI->getData32bitsDirective() << uint32_t(p[1])
997 << "\t" << TAI->getCommentString()
998 << " long double least significant word\n";
999 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
1000 << "\t" << TAI->getCommentString()
1001 << " long double next word\n";
1002 O << TAI->getData32bitsDirective() << uint32_t(p[0])
1003 << "\t" << TAI->getCommentString()
1004 << " long double next word\n";
1005 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
1006 << "\t" << TAI->getCommentString()
1007 << " long double most significant word\n";
1008 }
1009 return;
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001010 } else assert(0 && "Floating point constant type not handled");
Reid Spencer47857812006-12-31 05:55:36 +00001011 } else if (CV->getType() == Type::Int64Ty) {
Chris Lattner1b7e2352004-08-17 06:36:49 +00001012 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencerb83eb642006-10-20 07:07:24 +00001013 uint64_t Val = CI->getZExtValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001014
Jim Laskey563321a2006-09-06 18:34:40 +00001015 if (TAI->getData64bitsDirective())
1016 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +00001017 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +00001018 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
1019 << "\t" << TAI->getCommentString()
1020 << " Double-word most significant word " << Val << "\n";
1021 O << TAI->getData32bitsDirective() << unsigned(Val)
1022 << "\t" << TAI->getCommentString()
1023 << " Double-word least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +00001024 } else {
Jim Laskey563321a2006-09-06 18:34:40 +00001025 O << TAI->getData32bitsDirective() << unsigned(Val)
1026 << "\t" << TAI->getCommentString()
1027 << " Double-word least significant word " << Val << "\n";
1028 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
1029 << "\t" << TAI->getCommentString()
1030 << " Double-word most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +00001031 }
1032 return;
1033 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00001034 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1035 const VectorType *PTy = CP->getType();
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001036
1037 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
Duncan Sandsca0ed742007-11-05 00:04:43 +00001038 EmitGlobalConstant(CP->getOperand(I), false);
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001039
1040 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +00001041 }
1042
1043 const Type *type = CV->getType();
Evan Chengd6594ae2006-09-12 21:00:35 +00001044 printDataDirective(type);
Chris Lattner25045bd2005-11-21 07:51:36 +00001045 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001046 O << "\n";
1047}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001048
Evan Chengd6594ae2006-09-12 21:00:35 +00001049void
1050AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
1051 // Target doesn't support this yet!
1052 abort();
1053}
1054
Chris Lattner3ce9b672006-09-26 23:59:50 +00001055/// PrintSpecial - Print information related to the specified machine instr
1056/// that is independent of the operand, and may be independent of the instr
1057/// itself. This can be useful for portably encoding the comment character
1058/// or other bits of target-specific knowledge into the asmstrings. The
1059/// syntax used is ${:comment}. Targets can override this to add support
1060/// for their own strange codes.
1061void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001062 if (!strcmp(Code, "private")) {
1063 O << TAI->getPrivateGlobalPrefix();
1064 } else if (!strcmp(Code, "comment")) {
Chris Lattner3ce9b672006-09-26 23:59:50 +00001065 O << TAI->getCommentString();
1066 } else if (!strcmp(Code, "uid")) {
1067 // Assign a unique ID to this machine instruction.
1068 static const MachineInstr *LastMI = 0;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001069 static const Function *F = 0;
Chris Lattner3ce9b672006-09-26 23:59:50 +00001070 static unsigned Counter = 0U-1;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001071
1072 // Comparing the address of MI isn't sufficient, because machineinstrs may
1073 // be allocated to the same address across functions.
1074 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1075
Chris Lattner3ce9b672006-09-26 23:59:50 +00001076 // If this is a new machine instruction, bump the counter.
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001077 if (LastMI != MI || F != ThisF) {
1078 ++Counter;
1079 LastMI = MI;
Chris Lattner4b092522007-02-06 01:56:31 +00001080 F = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001081 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001082 O << Counter;
1083 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +00001084 cerr << "Unknown special formatter '" << Code
1085 << "' for machine instr: " << *MI;
Chris Lattner3ce9b672006-09-26 23:59:50 +00001086 exit(1);
1087 }
1088}
1089
1090
Chris Lattner0264d1a2006-01-27 02:10:10 +00001091/// printInlineAsm - This method formats and prints the specified machine
1092/// instruction that is an inline asm.
1093void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001094 unsigned NumOperands = MI->getNumOperands();
1095
1096 // Count the number of register definitions.
1097 unsigned NumDefs = 0;
Dan Gohman92dfe202007-09-14 20:33:02 +00001098 for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001099 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001100 assert(NumDefs != NumOperands-1 && "No asm string?");
1101
1102 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001103
1104 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001105 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001106
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001107 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
1108 if (AsmStr[0] == 0) {
1109 O << "\n"; // Tab already printed, avoid double indenting next instr.
1110 return;
1111 }
1112
Jim Laskey563321a2006-09-06 18:34:40 +00001113 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001114
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001115 // The variant of the current asmprinter.
1116 int AsmPrinterVariant = TAI->getAssemblerDialect();
1117
Chris Lattner66099132006-02-01 22:41:11 +00001118 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1119 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001120
Chris Lattner66099132006-02-01 22:41:11 +00001121 while (*LastEmitted) {
1122 switch (*LastEmitted) {
1123 default: {
1124 // Not a special case, emit the string section literally.
1125 const char *LiteralEnd = LastEmitted+1;
1126 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001127 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001128 ++LiteralEnd;
1129 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1130 O.write(LastEmitted, LiteralEnd-LastEmitted);
1131 LastEmitted = LiteralEnd;
1132 break;
1133 }
Chris Lattner1c059972006-05-05 21:47:05 +00001134 case '\n':
1135 ++LastEmitted; // Consume newline character.
Chris Lattner1f6f4c72007-04-30 17:00:18 +00001136 O << "\n"; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001137 break;
Chris Lattner66099132006-02-01 22:41:11 +00001138 case '$': {
1139 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001140 bool Done = true;
1141
1142 // Handle escapes.
1143 switch (*LastEmitted) {
1144 default: Done = false; break;
1145 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001146 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1147 O << '$';
1148 ++LastEmitted; // Consume second '$' character.
1149 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001150 case '(': // $( -> same as GCC's { character.
1151 ++LastEmitted; // Consume '(' character.
1152 if (CurVariant != -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001153 cerr << "Nested variants found in inline asm string: '"
1154 << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001155 exit(1);
1156 }
1157 CurVariant = 0; // We're in the first variant now.
1158 break;
1159 case '|':
1160 ++LastEmitted; // consume '|' character.
1161 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001162 cerr << "Found '|' character outside of variant in inline asm "
1163 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001164 exit(1);
1165 }
1166 ++CurVariant; // We're in the next variant.
1167 break;
1168 case ')': // $) -> same as GCC's } char.
1169 ++LastEmitted; // consume ')' character.
1170 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001171 cerr << "Found '}' character outside of variant in inline asm "
1172 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001173 exit(1);
1174 }
1175 CurVariant = -1;
1176 break;
Chris Lattner66099132006-02-01 22:41:11 +00001177 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001178 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001179
1180 bool HasCurlyBraces = false;
1181 if (*LastEmitted == '{') { // ${variable}
1182 ++LastEmitted; // Consume '{' character.
1183 HasCurlyBraces = true;
1184 }
1185
1186 const char *IDStart = LastEmitted;
1187 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001188 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001189 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1190 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001191 cerr << "Bad $ operand number in inline asm string: '"
1192 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001193 exit(1);
1194 }
1195 LastEmitted = IDEnd;
1196
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001197 char Modifier[2] = { 0, 0 };
1198
Chris Lattner66099132006-02-01 22:41:11 +00001199 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001200 // If we have curly braces, check for a modifier character. This
1201 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1202 if (*LastEmitted == ':') {
1203 ++LastEmitted; // Consume ':' character.
1204 if (*LastEmitted == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001205 cerr << "Bad ${:} expression in inline asm string: '"
1206 << AsmStr << "'\n";
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001207 exit(1);
1208 }
1209
1210 Modifier[0] = *LastEmitted;
1211 ++LastEmitted; // Consume modifier character.
1212 }
1213
Chris Lattner66099132006-02-01 22:41:11 +00001214 if (*LastEmitted != '}') {
Bill Wendlinge8156192006-12-07 01:30:32 +00001215 cerr << "Bad ${} expression in inline asm string: '"
1216 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001217 exit(1);
1218 }
1219 ++LastEmitted; // Consume '}' character.
1220 }
1221
1222 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001223 cerr << "Invalid $ operand number in inline asm string: '"
1224 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001225 exit(1);
1226 }
1227
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001228 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001229 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001230 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1231 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001232
1233 bool Error = false;
1234
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001235 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001236 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001237 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001238 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001239 OpNo += (OpFlags >> 3) + 1;
1240 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001241
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001242 if (OpNo >= MI->getNumOperands()) {
1243 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001244 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001245 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001246 ++OpNo; // Skip over the ID number.
1247
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001248 if (Modifier[0]=='l') // labels are target independent
Chris Lattner8aa797a2007-12-30 23:10:15 +00001249 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001250 false, false);
1251 else {
1252 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1253 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1254 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1255 Modifier[0] ? Modifier : 0);
1256 } else {
1257 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1258 Modifier[0] ? Modifier : 0);
1259 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001260 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001261 }
1262 if (Error) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001263 cerr << "Invalid operand found in inline asm: '"
1264 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +00001265 MI->dump();
1266 exit(1);
1267 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001268 }
Chris Lattner66099132006-02-01 22:41:11 +00001269 break;
1270 }
Chris Lattner66099132006-02-01 22:41:11 +00001271 }
1272 }
Jim Laskey563321a2006-09-06 18:34:40 +00001273 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattner66099132006-02-01 22:41:11 +00001274}
1275
Jim Laskey1ee29252007-01-26 14:34:52 +00001276/// printLabel - This method prints a local label used by debug and
1277/// exception handling tables.
1278void AsmPrinter::printLabel(const MachineInstr *MI) const {
Chris Lattner9e330492007-12-30 20:50:28 +00001279 O << "\n" << TAI->getPrivateGlobalPrefix()
1280 << "label" << MI->getOperand(0).getImm() << ":\n";
Jim Laskey1ee29252007-01-26 14:34:52 +00001281}
1282
Chris Lattner66099132006-02-01 22:41:11 +00001283/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1284/// instruction, using the specified assembler variant. Targets should
1285/// overried this to format as appropriate.
1286bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001287 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001288 // Target doesn't support this yet!
1289 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001290}
Chris Lattnerdd260332006-02-24 20:21:58 +00001291
1292bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1293 unsigned AsmVariant,
1294 const char *ExtraCode) {
1295 // Target doesn't support this yet!
1296 return true;
1297}
Nate Begeman37efe672006-04-22 18:53:45 +00001298
1299/// printBasicBlockLabel - This method prints the label for the specified
1300/// MachineBasicBlock
Nate Begemancdf38c42006-05-02 05:37:32 +00001301void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
1302 bool printColon,
1303 bool printComment) const {
Evan Cheng347d39f2007-10-14 05:57:21 +00001304 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << "_"
1305 << MBB->getNumber();
Nate Begemancdf38c42006-05-02 05:37:32 +00001306 if (printColon)
1307 O << ':';
Chris Lattner9c78ecb2006-10-05 21:40:14 +00001308 if (printComment && MBB->getBasicBlock())
Dan Gohman286d5692007-07-30 15:06:25 +00001309 O << '\t' << TAI->getCommentString() << ' '
1310 << MBB->getBasicBlock()->getName();
Nate Begeman37efe672006-04-22 18:53:45 +00001311}
Nate Begeman52a51e382006-08-12 21:29:52 +00001312
Evan Chengcc415862007-11-09 01:32:10 +00001313/// printPICJumpTableSetLabel - This method prints a set label for the
1314/// specified MachineBasicBlock for a jumptable entry.
1315void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1316 const MachineBasicBlock *MBB) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001317 if (!TAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001318 return;
1319
Jim Laskey563321a2006-09-06 18:34:40 +00001320 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001321 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman52a51e382006-08-12 21:29:52 +00001322 printBasicBlockLabel(MBB, false, false);
Evan Cheng347d39f2007-10-14 05:57:21 +00001323 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1324 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001325}
Evan Chengd6594ae2006-09-12 21:00:35 +00001326
Evan Chengcc415862007-11-09 01:32:10 +00001327void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1328 const MachineBasicBlock *MBB) const {
Evan Cheng41349c12006-11-01 09:23:08 +00001329 if (!TAI->getSetDirective())
1330 return;
1331
1332 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001333 << getFunctionNumber() << '_' << uid << '_' << uid2
1334 << "_set_" << MBB->getNumber() << ',';
Evan Cheng41349c12006-11-01 09:23:08 +00001335 printBasicBlockLabel(MBB, false, false);
Evan Cheng347d39f2007-10-14 05:57:21 +00001336 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1337 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001338}
1339
Evan Chengd6594ae2006-09-12 21:00:35 +00001340/// printDataDirective - This method prints the asm directive for the
1341/// specified type.
1342void AsmPrinter::printDataDirective(const Type *type) {
1343 const TargetData *TD = TM.getTargetData();
1344 switch (type->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001345 case Type::IntegerTyID: {
1346 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1347 if (BitWidth <= 8)
1348 O << TAI->getData8bitsDirective();
1349 else if (BitWidth <= 16)
1350 O << TAI->getData16bitsDirective();
1351 else if (BitWidth <= 32)
1352 O << TAI->getData32bitsDirective();
1353 else if (BitWidth <= 64) {
1354 assert(TAI->getData64bitsDirective() &&
1355 "Target cannot handle 64-bit constant exprs!");
1356 O << TAI->getData64bitsDirective();
1357 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001358 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001359 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001360 case Type::PointerTyID:
1361 if (TD->getPointerSize() == 8) {
1362 assert(TAI->getData64bitsDirective() &&
1363 "Target cannot handle 64-bit pointer exprs!");
1364 O << TAI->getData64bitsDirective();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001365 } else {
1366 O << TAI->getData32bitsDirective();
Evan Chengd6594ae2006-09-12 21:00:35 +00001367 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001368 break;
1369 case Type::FloatTyID: case Type::DoubleTyID:
Dale Johannesen4292d1c2007-09-28 18:06:58 +00001370 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
Evan Chengd6594ae2006-09-12 21:00:35 +00001371 assert (0 && "Should have already output floating point constant.");
1372 default:
1373 assert (0 && "Can't handle printing this type of thing");
1374 break;
1375 }
1376}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001377