blob: d9b97f7005d10c1416287ca4122f5f4ea1bfdc83 [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng932f0222006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng246ae0d2006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman8cfa57b2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman37efe672006-04-22 18:53:45 +000020#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000021#include "llvm/Support/Mangler.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000022#include "llvm/Support/MathExtras.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000023#include "llvm/Support/Streams.h"
Jim Laskey563321a2006-09-06 18:34:40 +000024#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000025#include "llvm/Target/TargetData.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000026#include "llvm/Target/TargetLowering.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000027#include "llvm/Target/TargetMachine.h"
Chris Lattner66099132006-02-01 22:41:11 +000028#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000029using namespace llvm;
30
Jim Laskeya0f3d172006-09-07 22:06:40 +000031AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
32 const TargetAsmInfo *T)
Jim Laskey563321a2006-09-06 18:34:40 +000033: FunctionNumber(0), O(o), TM(tm), TAI(T)
34{}
Chris Lattnered138932005-12-13 06:32:10 +000035
Chris Lattner52f06702006-10-05 02:42:47 +000036std::string AsmPrinter::getSectionForFunction(const Function &F) const {
37 return TAI->getTextSection();
38}
39
Chris Lattnered138932005-12-13 06:32:10 +000040
Chris Lattner4632d7a2006-05-09 04:59:56 +000041/// SwitchToTextSection - Switch to the specified text section of the executable
42/// if we are not already in it!
Chris Lattnerac28fbd2005-11-21 07:06:27 +000043///
Chris Lattner4632d7a2006-05-09 04:59:56 +000044void AsmPrinter::SwitchToTextSection(const char *NewSection,
45 const GlobalValue *GV) {
Chris Lattnerac28fbd2005-11-21 07:06:27 +000046 std::string NS;
Chris Lattnera7090ae2006-05-09 05:24:50 +000047 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000048 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnera7090ae2006-05-09 05:24:50 +000049 else
50 NS = NewSection;
51
52 // If we're already in this section, we're done.
53 if (CurrentSection == NS) return;
Jeff Cohen51b776d2006-05-02 03:58:45 +000054
Chris Lattner7faec9b2006-05-09 05:33:48 +000055 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000056 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
57 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen51b776d2006-05-02 03:58:45 +000058
Chris Lattner7faec9b2006-05-09 05:33:48 +000059 CurrentSection = NS;
60
61 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000062 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattnerac28fbd2005-11-21 07:06:27 +000063}
64
Nate Begeman2f1ae882006-07-27 01:13:04 +000065/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner4632d7a2006-05-09 04:59:56 +000066/// if we are not already in it!
67///
68void AsmPrinter::SwitchToDataSection(const char *NewSection,
69 const GlobalValue *GV) {
70 std::string NS;
Chris Lattnerb81cb612006-05-09 05:23:12 +000071 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000072 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnerb81cb612006-05-09 05:23:12 +000073 else
74 NS = NewSection;
Chris Lattner4632d7a2006-05-09 04:59:56 +000075
Chris Lattnerb81cb612006-05-09 05:23:12 +000076 // If we're already in this section, we're done.
77 if (CurrentSection == NS) return;
78
Chris Lattner7faec9b2006-05-09 05:33:48 +000079 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000080 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
81 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner7faec9b2006-05-09 05:33:48 +000082
83 CurrentSection = NS;
Chris Lattner4632d7a2006-05-09 04:59:56 +000084
Chris Lattner7faec9b2006-05-09 05:33:48 +000085 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000086 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner4632d7a2006-05-09 04:59:56 +000087}
88
89
Chris Lattnera80ba712004-08-16 23:15:22 +000090bool AsmPrinter::doInitialization(Module &M) {
Jim Laskey563321a2006-09-06 18:34:40 +000091 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattner2c1b1592006-01-23 23:47:53 +000092
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000093 if (!M.getModuleInlineAsm().empty())
Jim Laskey563321a2006-09-06 18:34:40 +000094 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000095 << M.getModuleInlineAsm()
Jim Laskey563321a2006-09-06 18:34:40 +000096 << "\n" << TAI->getCommentString()
97 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +000098
Anton Korobeynikovab4022f2006-10-31 08:31:24 +000099 SwitchToDataSection(""); // Reset back to no section.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000100
101 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
102 DebugInfo->AnalyzeModule(M);
103 }
104
Chris Lattnera80ba712004-08-16 23:15:22 +0000105 return false;
106}
107
108bool AsmPrinter::doFinalization(Module &M) {
Rafael Espindola15404d02006-12-18 03:37:18 +0000109 if (TAI->getWeakRefDirective()) {
110 if (ExtWeakSymbols.begin() != ExtWeakSymbols.end())
111 SwitchToDataSection("");
112
113 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
114 e = ExtWeakSymbols.end(); i != e; ++i) {
115 const GlobalValue *GV = *i;
116 std::string Name = Mang->getValueName(GV);
117 O << TAI->getWeakRefDirective() << Name << "\n";
118 }
119 }
120
Chris Lattnera80ba712004-08-16 23:15:22 +0000121 delete Mang; Mang = 0;
122 return false;
123}
124
Chris Lattner25045bd2005-11-21 07:51:36 +0000125void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000126 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +0000127 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner77bc2282005-11-21 08:13:27 +0000128 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +0000129}
130
Chris Lattner3b4fd322005-11-21 08:25:09 +0000131/// EmitConstantPool - Print to the current output stream assembly
132/// representations of the constants in the constant pool MCP. This is
133/// used to print out constants which have been "spilled to memory" by
134/// the code generator.
135///
136void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000137 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000138 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000139
140 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
141 // in special sections.
142 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
143 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
144 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
145 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Chengd6594ae2006-09-12 21:00:35 +0000146 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000147 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng2d2cec12006-06-29 00:26:09 +0000148 MachineConstantPoolEntry CPE = CP[i];
Evan Chengd5a99d72006-09-14 07:35:00 +0000149 const Type *Ty = CPE.getType();
Jim Laskey563321a2006-09-06 18:34:40 +0000150 if (TAI->getFourByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000151 TM.getTargetData()->getTypeSize(Ty) == 4)
152 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000153 else if (TAI->getEightByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000154 TM.getTargetData()->getTypeSize(Ty) == 8)
155 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000156 else if (TAI->getSixteenByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000157 TM.getTargetData()->getTypeSize(Ty) == 16)
158 SixteenByteCPs.push_back(std::make_pair(CPE, i));
159 else
160 OtherCPs.push_back(std::make_pair(CPE, i));
161 }
162
163 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskey563321a2006-09-06 18:34:40 +0000164 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
165 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
166 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
167 SixteenByteCPs);
168 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000169}
170
171void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
172 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
173 if (CP.empty()) return;
174
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000175 SwitchToDataSection(Section);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000176 EmitAlignment(Alignment);
177 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Jim Laskey563321a2006-09-06 18:34:40 +0000178 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
179 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Chengd5a99d72006-09-14 07:35:00 +0000180 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
181 if (CP[i].first.isMachineConstantPoolEntry())
Evan Chengd6594ae2006-09-12 21:00:35 +0000182 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Chengd5a99d72006-09-14 07:35:00 +0000183 else
Evan Chengd6594ae2006-09-12 21:00:35 +0000184 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000185 if (i != e-1) {
Evan Chengd5a99d72006-09-14 07:35:00 +0000186 const Type *Ty = CP[i].first.getType();
Evan Cheng2d2cec12006-06-29 00:26:09 +0000187 unsigned EntSize =
Evan Chengd6594ae2006-09-12 21:00:35 +0000188 TM.getTargetData()->getTypeSize(Ty);
Evan Chengd5a99d72006-09-14 07:35:00 +0000189 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattner3029f922006-02-09 04:46:04 +0000190 // Emit inter-object padding for alignment.
Evan Chengd5a99d72006-09-14 07:35:00 +0000191 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattner3029f922006-02-09 04:46:04 +0000192 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000193 }
194}
195
Nate Begeman37efe672006-04-22 18:53:45 +0000196/// EmitJumpTableInfo - Print assembly representations of the jump tables used
197/// by the current function to the current output stream.
198///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000199void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
200 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000201 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
202 if (JT.empty()) return;
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000203 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000204
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000205 // Use JumpTableDirective otherwise honor the entry size from the jump table
206 // info.
Andrew Lenharthbeec30e2006-09-24 19:45:58 +0000207 const char *JTEntryDirective = TAI->getJumpTableDirective();
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000208 bool HadJTEntryDirective = JTEntryDirective != NULL;
209 if (!HadJTEntryDirective) {
210 JTEntryDirective = MJTI->getEntrySize() == 4 ?
211 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
212 }
Nate Begeman37efe672006-04-22 18:53:45 +0000213
Nate Begeman2f1ae882006-07-27 01:13:04 +0000214 // Pick the directive to use to print the jump table entries, and switch to
215 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000216 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000217
218 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
219 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
220 !JumpTableDataSection) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000221 // In PIC mode, we need to emit the jump table to the same section as the
222 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000223 // We should also do if the section name is NULL.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000224 const Function *F = MF.getFunction();
225 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000226 } else {
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000227 SwitchToDataSection(JumpTableDataSection);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000228 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000229
230 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000231
Nate Begeman37efe672006-04-22 18:53:45 +0000232 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000233 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000234
235 // If this jump table was deleted, ignore it.
236 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000237
238 // For PIC codegen, if possible we want to use the SetDirective to reduce
239 // the number of relocations the assembler will generate for the jump table.
240 // Set directives are all printed before the jump table itself.
241 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000242 if (TAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000243 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
244 if (EmittedSets.insert(JTBBs[ii]).second)
245 printSetLabel(i, JTBBs[ii]);
246
Chris Lattner393a8ee2007-01-18 01:12:56 +0000247 // On some targets (e.g. darwin) we want to emit two consequtive labels
248 // before each jump table. The first label is never referenced, but tells
249 // the assembler and linker the extents of the jump table object. The
250 // second label is actually referenced by the code.
251 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
252 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
253
Jim Laskey563321a2006-09-06 18:34:40 +0000254 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
255 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000256
Nate Begeman37efe672006-04-22 18:53:45 +0000257 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000258 O << JTEntryDirective << ' ';
Nate Begeman52a51e382006-08-12 21:29:52 +0000259 // If we have emitted set directives for the jump table entries, print
260 // them rather than the entries themselves. If we're emitting PIC, then
261 // emit the table entries as differences between two text section labels.
262 // If we're emitting non-PIC code, then emit the entries as direct
263 // references to the target basic blocks.
264 if (!EmittedSets.empty()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000265 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
266 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000267 } else if (IsPic) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000268 printBasicBlockLabel(JTBBs[ii], false, false);
Chris Lattner393a8ee2007-01-18 01:12:56 +0000269 // If the arch uses custom Jump Table directives, don't calc relative to
270 // JT
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000271 if (!HadJTEntryDirective)
272 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
273 << getFunctionNumber() << '_' << i;
Nate Begeman52a51e382006-08-12 21:29:52 +0000274 } else {
275 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000276 }
Nate Begeman37efe672006-04-22 18:53:45 +0000277 O << '\n';
278 }
279 }
280}
281
Chris Lattnered138932005-12-13 06:32:10 +0000282/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
283/// special global used by LLVM. If so, emit it and return true, otherwise
284/// do nothing and return false.
285bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey78098112006-03-07 22:00:35 +0000286 // Ignore debug and non-emitted data.
287 if (GV->getSection() == "llvm.metadata") return true;
288
289 if (!GV->hasAppendingLinkage()) return false;
290
291 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000292
Chris Lattnercb05af82006-09-26 03:38:18 +0000293 if (GV->getName() == "llvm.used") {
294 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
295 EmitLLVMUsedList(GV->getInitializer());
296 return true;
297 }
Chris Lattnered138932005-12-13 06:32:10 +0000298
Chris Lattner5166b822006-01-12 19:17:23 +0000299 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000300 SwitchToDataSection(TAI->getStaticCtorsSection());
Chris Lattnered138932005-12-13 06:32:10 +0000301 EmitAlignment(2, 0);
302 EmitXXStructorList(GV->getInitializer());
303 return true;
304 }
305
Chris Lattner5166b822006-01-12 19:17:23 +0000306 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000307 SwitchToDataSection(TAI->getStaticDtorsSection());
Chris Lattnered138932005-12-13 06:32:10 +0000308 EmitAlignment(2, 0);
309 EmitXXStructorList(GV->getInitializer());
310 return true;
311 }
312
313 return false;
314}
315
Chris Lattnercb05af82006-09-26 03:38:18 +0000316/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
317/// global in the specified llvm.used list as being used with this directive.
318void AsmPrinter::EmitLLVMUsedList(Constant *List) {
319 const char *Directive = TAI->getUsedDirective();
320
321 // Should be an array of 'sbyte*'.
322 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
323 if (InitList == 0) return;
324
325 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
326 O << Directive;
327 EmitConstantValueOnly(InitList->getOperand(i));
328 O << "\n";
329 }
330}
331
Chris Lattnered138932005-12-13 06:32:10 +0000332/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
333/// function pointers, ignoring the init priority.
334void AsmPrinter::EmitXXStructorList(Constant *List) {
335 // Should be an array of '{ int, void ()* }' structs. The first value is the
336 // init priority, which we ignore.
337 if (!isa<ConstantArray>(List)) return;
338 ConstantArray *InitList = cast<ConstantArray>(List);
339 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
340 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
341 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000342
343 if (CS->getOperand(1)->isNullValue())
344 return; // Found a null terminator, exit printing.
345 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000346 EmitGlobalConstant(CS->getOperand(1));
347 }
348}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000349
Jim Laskeya1a19f82006-10-17 13:41:07 +0000350/// getGlobalLinkName - Returns the asm/link name of of the specified
351/// global variable. Should be overridden by each target asm printer to
352/// generate the appropriate value.
Jim Laskey99e41ee2006-10-17 17:17:24 +0000353const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
354 std::string LinkName;
Jim Laskey03742482006-11-20 20:29:06 +0000355
356 if (isa<Function>(GV)) {
357 LinkName += TAI->getFunctionAddrPrefix();
358 LinkName += Mang->getValueName(GV);
359 LinkName += TAI->getFunctionAddrSuffix();
360 } else {
361 LinkName += TAI->getGlobalVarAddrPrefix();
362 LinkName += Mang->getValueName(GV);
363 LinkName += TAI->getGlobalVarAddrSuffix();
364 }
365
Jim Laskey99e41ee2006-10-17 17:17:24 +0000366 return LinkName;
Jim Laskeya1a19f82006-10-17 13:41:07 +0000367}
368
Chris Lattner25045bd2005-11-21 07:51:36 +0000369// EmitAlignment - Emit an alignment directive to the specified power of two.
370void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnera1ab72d2005-11-14 19:00:06 +0000371 if (GV && GV->getAlignment())
372 NumBits = Log2_32(GV->getAlignment());
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000373 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskey563321a2006-09-06 18:34:40 +0000374 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
375 O << TAI->getAlignDirective() << NumBits << "\n";
Chris Lattnerbfddc202004-08-17 19:14:29 +0000376}
377
Chris Lattner25045bd2005-11-21 07:51:36 +0000378/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000379///
Chris Lattner25045bd2005-11-21 07:51:36 +0000380void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000381 if (NumZeros) {
Jim Laskey563321a2006-09-06 18:34:40 +0000382 if (TAI->getZeroDirective()) {
383 O << TAI->getZeroDirective() << NumZeros;
384 if (TAI->getZeroDirectiveSuffix())
385 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000386 O << "\n";
387 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000388 for (; NumZeros; --NumZeros)
Jim Laskey563321a2006-09-06 18:34:40 +0000389 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000390 }
391 }
392}
393
Chris Lattnera80ba712004-08-16 23:15:22 +0000394// Print out the specified constant, without a storage class. Only the
395// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000396void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000397 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000398 O << "0";
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000399 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattnera875df32007-01-12 18:15:09 +0000400 O << CI->getZExtValue();
Reid Spencerb83eb642006-10-20 07:07:24 +0000401 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000402 // This is a constant address for a global variable or function. Use the
403 // name of the variable or function as the address value, possibly
404 // decorating it with GlobalVarAddrPrefix/Suffix or
405 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskey563321a2006-09-06 18:34:40 +0000406 if (isa<Function>(GV)) {
407 O << TAI->getFunctionAddrPrefix()
408 << Mang->getValueName(GV)
409 << TAI->getFunctionAddrSuffix();
410 } else {
411 O << TAI->getGlobalVarAddrPrefix()
412 << Mang->getValueName(GV)
413 << TAI->getGlobalVarAddrSuffix();
414 }
Duraid Madina855a5192005-04-02 12:21:51 +0000415 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000416 const TargetData *TD = TM.getTargetData();
Chris Lattnera80ba712004-08-16 23:15:22 +0000417 switch(CE->getOpcode()) {
418 case Instruction::GetElementPtr: {
419 // generate a symbolic expression for the byte address
420 const Constant *ptrVal = CE->getOperand(0);
421 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Andersona69571c2006-05-03 01:29:57 +0000422 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner27e19212005-02-14 21:40:26 +0000423 if (Offset)
424 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000425 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000426 if (Offset > 0)
427 O << ") + " << Offset;
428 else if (Offset < 0)
429 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000430 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000431 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000432 }
433 break;
434 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000435 case Instruction::Trunc:
436 case Instruction::ZExt:
437 case Instruction::SExt:
438 case Instruction::FPTrunc:
439 case Instruction::FPExt:
440 case Instruction::UIToFP:
441 case Instruction::SIToFP:
442 case Instruction::FPToUI:
443 case Instruction::FPToSI:
444 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
445 break;
Chris Lattnercb0a6812006-12-12 05:14:13 +0000446 case Instruction::BitCast:
447 return EmitConstantValueOnly(CE->getOperand(0));
448
Chris Lattnerddc94012006-12-12 05:18:19 +0000449 case Instruction::IntToPtr: {
450 // Handle casts to pointers by changing them into casts to the appropriate
451 // integer type. This promotes constant folding and simplifies this code.
452 Constant *Op = CE->getOperand(0);
453 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
454 return EmitConstantValueOnly(Op);
455 }
456
457
458 case Instruction::PtrToInt: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000459 // Support only foldable casts to/from pointers that can be eliminated by
460 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000461 Constant *Op = CE->getOperand(0);
Chris Lattnerddc94012006-12-12 05:18:19 +0000462 const Type *Ty = CE->getType();
Chris Lattnera80ba712004-08-16 23:15:22 +0000463
Chris Lattnerddc94012006-12-12 05:18:19 +0000464 // We can emit the pointer value into this slot if the slot is an
465 // integer slot greater or equal to the size of the pointer.
Chris Lattner42a75512007-01-15 02:27:26 +0000466 if (Ty->isInteger() &&
Reid Spencera54b7cb2007-01-12 07:05:14 +0000467 TD->getTypeSize(Ty) >= TD->getTypeSize(Op->getType()))
Chris Lattner4a6bd332006-07-29 01:57:19 +0000468 return EmitConstantValueOnly(Op);
Chris Lattner4a6bd332006-07-29 01:57:19 +0000469
470 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000471 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000472 break;
473 }
474 case Instruction::Add:
475 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000476 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattnera80ba712004-08-16 23:15:22 +0000477 O << ") + (";
Chris Lattner25045bd2005-11-21 07:51:36 +0000478 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000479 O << ")";
480 break;
481 default:
482 assert(0 && "Unsupported operator!");
483 }
484 } else {
485 assert(0 && "Unknown constant value!");
486 }
487}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000488
489/// toOctal - Convert the low order bits of X into an octal digit.
490///
491static inline char toOctal(int X) {
492 return (X&7)+'0';
493}
494
Chris Lattner2980cef2005-11-10 18:06:33 +0000495/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000496/// the predicate isString is true.
497///
Chris Lattner2980cef2005-11-10 18:06:33 +0000498static void printAsCString(std::ostream &O, const ConstantArray *CVA,
499 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000500 assert(CVA->isString() && "Array is not string compatible!");
501
502 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000503 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000504 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000505 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000506
507 if (C == '"') {
508 O << "\\\"";
509 } else if (C == '\\') {
510 O << "\\\\";
511 } else if (isprint(C)) {
512 O << C;
513 } else {
514 switch(C) {
515 case '\b': O << "\\b"; break;
516 case '\f': O << "\\f"; break;
517 case '\n': O << "\\n"; break;
518 case '\r': O << "\\r"; break;
519 case '\t': O << "\\t"; break;
520 default:
521 O << '\\';
522 O << toOctal(C >> 6);
523 O << toOctal(C >> 3);
524 O << toOctal(C >> 0);
525 break;
526 }
527 }
528 }
529 O << "\"";
530}
531
Jeff Cohenc884db42006-05-02 01:16:28 +0000532/// EmitString - Emit a zero-byte-terminated string constant.
533///
534void AsmPrinter::EmitString(const ConstantArray *CVA) const {
535 unsigned NumElts = CVA->getNumOperands();
Jim Laskey563321a2006-09-06 18:34:40 +0000536 if (TAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000537 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskey563321a2006-09-06 18:34:40 +0000538 O << TAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000539 printAsCString(O, CVA, NumElts-1);
540 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000541 O << TAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000542 printAsCString(O, CVA, NumElts);
543 }
544 O << "\n";
545}
546
Chris Lattner25045bd2005-11-21 07:51:36 +0000547/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000548///
Chris Lattner25045bd2005-11-21 07:51:36 +0000549void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Andersona69571c2006-05-03 01:29:57 +0000550 const TargetData *TD = TM.getTargetData();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000551
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000552 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000553 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000554 return;
555 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
556 if (CVA->isString()) {
Jeff Cohenc884db42006-05-02 01:16:28 +0000557 EmitString(CVA);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000558 } else { // Not a string. Print the values in successive locations
559 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000560 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000561 }
562 return;
563 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
564 // Print the fields in successive locations. Pad to align if needed!
Owen Andersona69571c2006-05-03 01:29:57 +0000565 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000566 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000567 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
568 const Constant* field = CVS->getOperand(i);
569
570 // Check if padding is needed and insert one or more 0s.
Owen Andersona69571c2006-05-03 01:29:57 +0000571 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000572 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner1b7e2352004-08-17 06:36:49 +0000573 : cvsLayout->MemberOffsets[i+1])
574 - cvsLayout->MemberOffsets[i]) - fieldSize;
575 sizeSoFar += fieldSize + padSize;
576
577 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000578 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000579
580 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000581 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000582 }
583 assert(sizeSoFar == cvsLayout->StructSize &&
584 "Layout of constant struct may be incorrect!");
585 return;
586 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
587 // FP Constants are printed as integer constants to avoid losing
588 // precision...
589 double Val = CFP->getValue();
590 if (CFP->getType() == Type::DoubleTy) {
Jim Laskey563321a2006-09-06 18:34:40 +0000591 if (TAI->getData64bitsDirective())
592 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
593 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000594 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000595 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
596 << "\t" << TAI->getCommentString()
597 << " double most significant word " << Val << "\n";
598 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
599 << "\t" << TAI->getCommentString()
600 << " double least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000601 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000602 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
603 << "\t" << TAI->getCommentString()
604 << " double least significant word " << Val << "\n";
605 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
606 << "\t" << TAI->getCommentString()
607 << " double most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000608 }
609 return;
610 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000611 O << TAI->getData32bitsDirective() << FloatToBits(Val)
612 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000613 return;
614 }
Reid Spencer47857812006-12-31 05:55:36 +0000615 } else if (CV->getType() == Type::Int64Ty) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000616 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000617 uint64_t Val = CI->getZExtValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000618
Jim Laskey563321a2006-09-06 18:34:40 +0000619 if (TAI->getData64bitsDirective())
620 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000621 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000622 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
623 << "\t" << TAI->getCommentString()
624 << " Double-word most significant word " << Val << "\n";
625 O << TAI->getData32bitsDirective() << unsigned(Val)
626 << "\t" << TAI->getCommentString()
627 << " Double-word least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000628 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000629 O << TAI->getData32bitsDirective() << unsigned(Val)
630 << "\t" << TAI->getCommentString()
631 << " Double-word least significant word " << Val << "\n";
632 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
633 << "\t" << TAI->getCommentString()
634 << " Double-word most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000635 }
636 return;
637 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000638 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
639 const PackedType *PTy = CP->getType();
640
641 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
642 EmitGlobalConstant(CP->getOperand(I));
643
644 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000645 }
646
647 const Type *type = CV->getType();
Evan Chengd6594ae2006-09-12 21:00:35 +0000648 printDataDirective(type);
Chris Lattner25045bd2005-11-21 07:51:36 +0000649 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000650 O << "\n";
651}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000652
Evan Chengd6594ae2006-09-12 21:00:35 +0000653void
654AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
655 // Target doesn't support this yet!
656 abort();
657}
658
Chris Lattner3ce9b672006-09-26 23:59:50 +0000659/// PrintSpecial - Print information related to the specified machine instr
660/// that is independent of the operand, and may be independent of the instr
661/// itself. This can be useful for portably encoding the comment character
662/// or other bits of target-specific knowledge into the asmstrings. The
663/// syntax used is ${:comment}. Targets can override this to add support
664/// for their own strange codes.
665void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattnerbae02cf2006-09-27 00:06:07 +0000666 if (!strcmp(Code, "private")) {
667 O << TAI->getPrivateGlobalPrefix();
668 } else if (!strcmp(Code, "comment")) {
Chris Lattner3ce9b672006-09-26 23:59:50 +0000669 O << TAI->getCommentString();
670 } else if (!strcmp(Code, "uid")) {
671 // Assign a unique ID to this machine instruction.
672 static const MachineInstr *LastMI = 0;
673 static unsigned Counter = 0U-1;
674 // If this is a new machine instruction, bump the counter.
675 if (LastMI != MI) { ++Counter; LastMI = MI; }
676 O << Counter;
677 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +0000678 cerr << "Unknown special formatter '" << Code
679 << "' for machine instr: " << *MI;
Chris Lattner3ce9b672006-09-26 23:59:50 +0000680 exit(1);
681 }
682}
683
684
Chris Lattner0264d1a2006-01-27 02:10:10 +0000685/// printInlineAsm - This method formats and prints the specified machine
686/// instruction that is an inline asm.
687void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000688 unsigned NumOperands = MI->getNumOperands();
689
690 // Count the number of register definitions.
691 unsigned NumDefs = 0;
Chris Lattner67942f52006-09-05 20:02:51 +0000692 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
693 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000694 assert(NumDefs != NumOperands-1 && "No asm string?");
695
696 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +0000697
698 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000699 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +0000700
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000701 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
702 if (AsmStr[0] == 0) {
703 O << "\n"; // Tab already printed, avoid double indenting next instr.
704 return;
705 }
706
Jim Laskey563321a2006-09-06 18:34:40 +0000707 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000708
Bill Wendlingeb9a42c2007-01-16 03:42:04 +0000709 // The variant of the current asmprinter.
710 int AsmPrinterVariant = TAI->getAssemblerDialect();
711
Chris Lattner66099132006-02-01 22:41:11 +0000712 int CurVariant = -1; // The number of the {.|.|.} region we are in.
713 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +0000714
Chris Lattner66099132006-02-01 22:41:11 +0000715 while (*LastEmitted) {
716 switch (*LastEmitted) {
717 default: {
718 // Not a special case, emit the string section literally.
719 const char *LiteralEnd = LastEmitted+1;
720 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +0000721 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +0000722 ++LiteralEnd;
723 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
724 O.write(LastEmitted, LiteralEnd-LastEmitted);
725 LastEmitted = LiteralEnd;
726 break;
727 }
Chris Lattner1c059972006-05-05 21:47:05 +0000728 case '\n':
729 ++LastEmitted; // Consume newline character.
730 O << "\n\t"; // Indent code with newline.
731 break;
Chris Lattner66099132006-02-01 22:41:11 +0000732 case '$': {
733 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000734 bool Done = true;
735
736 // Handle escapes.
737 switch (*LastEmitted) {
738 default: Done = false; break;
739 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +0000740 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
741 O << '$';
742 ++LastEmitted; // Consume second '$' character.
743 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000744 case '(': // $( -> same as GCC's { character.
745 ++LastEmitted; // Consume '(' character.
746 if (CurVariant != -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000747 cerr << "Nested variants found in inline asm string: '"
748 << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000749 exit(1);
750 }
751 CurVariant = 0; // We're in the first variant now.
752 break;
753 case '|':
754 ++LastEmitted; // consume '|' character.
755 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000756 cerr << "Found '|' character outside of variant in inline asm "
757 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000758 exit(1);
759 }
760 ++CurVariant; // We're in the next variant.
761 break;
762 case ')': // $) -> same as GCC's } char.
763 ++LastEmitted; // consume ')' character.
764 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000765 cerr << "Found '}' character outside of variant in inline asm "
766 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000767 exit(1);
768 }
769 CurVariant = -1;
770 break;
Chris Lattner66099132006-02-01 22:41:11 +0000771 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000772 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +0000773
774 bool HasCurlyBraces = false;
775 if (*LastEmitted == '{') { // ${variable}
776 ++LastEmitted; // Consume '{' character.
777 HasCurlyBraces = true;
778 }
779
780 const char *IDStart = LastEmitted;
781 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +0000782 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +0000783 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
784 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000785 cerr << "Bad $ operand number in inline asm string: '"
786 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +0000787 exit(1);
788 }
789 LastEmitted = IDEnd;
790
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000791 char Modifier[2] = { 0, 0 };
792
Chris Lattner66099132006-02-01 22:41:11 +0000793 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000794 // If we have curly braces, check for a modifier character. This
795 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
796 if (*LastEmitted == ':') {
797 ++LastEmitted; // Consume ':' character.
798 if (*LastEmitted == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000799 cerr << "Bad ${:} expression in inline asm string: '"
800 << AsmStr << "'\n";
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000801 exit(1);
802 }
803
804 Modifier[0] = *LastEmitted;
805 ++LastEmitted; // Consume modifier character.
806 }
807
Chris Lattner66099132006-02-01 22:41:11 +0000808 if (*LastEmitted != '}') {
Bill Wendlinge8156192006-12-07 01:30:32 +0000809 cerr << "Bad ${} expression in inline asm string: '"
810 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +0000811 exit(1);
812 }
813 ++LastEmitted; // Consume '}' character.
814 }
815
816 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000817 cerr << "Invalid $ operand number in inline asm string: '"
818 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +0000819 exit(1);
820 }
821
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000822 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +0000823 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000824 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
825 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000826
827 bool Error = false;
828
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000829 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000830 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000831 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000832 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
833 OpNo += (OpFlags >> 3) + 1;
834 }
Chris Lattnerdd260332006-02-24 20:21:58 +0000835
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000836 if (OpNo >= MI->getNumOperands()) {
837 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +0000838 } else {
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000839 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
840 ++OpNo; // Skip over the ID number.
841
842 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
843 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
844 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
845 Modifier[0] ? Modifier : 0);
846 } else {
847 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
848 Modifier[0] ? Modifier : 0);
849 }
Chris Lattnerdd260332006-02-24 20:21:58 +0000850 }
851 if (Error) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000852 cerr << "Invalid operand found in inline asm: '"
853 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +0000854 MI->dump();
855 exit(1);
856 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000857 }
Chris Lattner66099132006-02-01 22:41:11 +0000858 break;
859 }
Chris Lattner66099132006-02-01 22:41:11 +0000860 }
861 }
Jim Laskey563321a2006-09-06 18:34:40 +0000862 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattner66099132006-02-01 22:41:11 +0000863}
864
865/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
866/// instruction, using the specified assembler variant. Targets should
867/// overried this to format as appropriate.
868bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000869 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +0000870 // Target doesn't support this yet!
871 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +0000872}
Chris Lattnerdd260332006-02-24 20:21:58 +0000873
874bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
875 unsigned AsmVariant,
876 const char *ExtraCode) {
877 // Target doesn't support this yet!
878 return true;
879}
Nate Begeman37efe672006-04-22 18:53:45 +0000880
881/// printBasicBlockLabel - This method prints the label for the specified
882/// MachineBasicBlock
Nate Begemancdf38c42006-05-02 05:37:32 +0000883void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
884 bool printColon,
885 bool printComment) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000886 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman9d51eeb2006-05-02 17:36:46 +0000887 << MBB->getNumber();
Nate Begemancdf38c42006-05-02 05:37:32 +0000888 if (printColon)
889 O << ':';
Chris Lattner9c78ecb2006-10-05 21:40:14 +0000890 if (printComment && MBB->getBasicBlock())
Jim Laskey563321a2006-09-06 18:34:40 +0000891 O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
Nate Begeman37efe672006-04-22 18:53:45 +0000892}
Nate Begeman52a51e382006-08-12 21:29:52 +0000893
894/// printSetLabel - This method prints a set label for the specified
895/// MachineBasicBlock
896void AsmPrinter::printSetLabel(unsigned uid,
897 const MachineBasicBlock *MBB) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000898 if (!TAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +0000899 return;
900
Jim Laskey563321a2006-09-06 18:34:40 +0000901 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
902 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman52a51e382006-08-12 21:29:52 +0000903 printBasicBlockLabel(MBB, false, false);
Jim Laskey563321a2006-09-06 18:34:40 +0000904 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman52a51e382006-08-12 21:29:52 +0000905 << '_' << uid << '\n';
906}
Evan Chengd6594ae2006-09-12 21:00:35 +0000907
Evan Cheng41349c12006-11-01 09:23:08 +0000908void AsmPrinter::printSetLabel(unsigned uid, unsigned uid2,
909 const MachineBasicBlock *MBB) const {
910 if (!TAI->getSetDirective())
911 return;
912
913 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
914 << getFunctionNumber() << '_' << uid << '_' << uid2
915 << "_set_" << MBB->getNumber() << ',';
916 printBasicBlockLabel(MBB, false, false);
917 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
918 << '_' << uid << '_' << uid2 << '\n';
919}
920
Evan Chengd6594ae2006-09-12 21:00:35 +0000921/// printDataDirective - This method prints the asm directive for the
922/// specified type.
923void AsmPrinter::printDataDirective(const Type *type) {
924 const TargetData *TD = TM.getTargetData();
925 switch (type->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000926 case Type::IntegerTyID: {
927 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
928 if (BitWidth <= 8)
929 O << TAI->getData8bitsDirective();
930 else if (BitWidth <= 16)
931 O << TAI->getData16bitsDirective();
932 else if (BitWidth <= 32)
933 O << TAI->getData32bitsDirective();
934 else if (BitWidth <= 64) {
935 assert(TAI->getData64bitsDirective() &&
936 "Target cannot handle 64-bit constant exprs!");
937 O << TAI->getData64bitsDirective();
938 }
Evan Chengd6594ae2006-09-12 21:00:35 +0000939 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000940 }
Evan Chengd6594ae2006-09-12 21:00:35 +0000941 case Type::PointerTyID:
942 if (TD->getPointerSize() == 8) {
943 assert(TAI->getData64bitsDirective() &&
944 "Target cannot handle 64-bit pointer exprs!");
945 O << TAI->getData64bitsDirective();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000946 } else {
947 O << TAI->getData32bitsDirective();
Evan Chengd6594ae2006-09-12 21:00:35 +0000948 }
Evan Chengd6594ae2006-09-12 21:00:35 +0000949 break;
950 case Type::FloatTyID: case Type::DoubleTyID:
951 assert (0 && "Should have already output floating point constant.");
952 default:
953 assert (0 && "Can't handle printing this type of thing");
954 break;
955 }
956}