blob: 498339c014e9dd8d51f9d5f09a9ad82fca5402e0 [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) {
109 delete Mang; Mang = 0;
110 return false;
111}
112
Chris Lattner25045bd2005-11-21 07:51:36 +0000113void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000114 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +0000115 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner77bc2282005-11-21 08:13:27 +0000116 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +0000117}
118
Chris Lattner3b4fd322005-11-21 08:25:09 +0000119/// EmitConstantPool - Print to the current output stream assembly
120/// representations of the constants in the constant pool MCP. This is
121/// used to print out constants which have been "spilled to memory" by
122/// the code generator.
123///
124void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000125 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000126 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000127
128 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
129 // in special sections.
130 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
131 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
132 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
133 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Chengd6594ae2006-09-12 21:00:35 +0000134 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000135 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng2d2cec12006-06-29 00:26:09 +0000136 MachineConstantPoolEntry CPE = CP[i];
Evan Chengd5a99d72006-09-14 07:35:00 +0000137 const Type *Ty = CPE.getType();
Jim Laskey563321a2006-09-06 18:34:40 +0000138 if (TAI->getFourByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000139 TM.getTargetData()->getTypeSize(Ty) == 4)
140 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000141 else if (TAI->getEightByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000142 TM.getTargetData()->getTypeSize(Ty) == 8)
143 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000144 else if (TAI->getSixteenByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000145 TM.getTargetData()->getTypeSize(Ty) == 16)
146 SixteenByteCPs.push_back(std::make_pair(CPE, i));
147 else
148 OtherCPs.push_back(std::make_pair(CPE, i));
149 }
150
151 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskey563321a2006-09-06 18:34:40 +0000152 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
153 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
154 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
155 SixteenByteCPs);
156 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000157}
158
159void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
160 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
161 if (CP.empty()) return;
162
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000163 SwitchToDataSection(Section);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000164 EmitAlignment(Alignment);
165 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Jim Laskey563321a2006-09-06 18:34:40 +0000166 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
167 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Chengd5a99d72006-09-14 07:35:00 +0000168 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
169 if (CP[i].first.isMachineConstantPoolEntry())
Evan Chengd6594ae2006-09-12 21:00:35 +0000170 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Chengd5a99d72006-09-14 07:35:00 +0000171 else
Evan Chengd6594ae2006-09-12 21:00:35 +0000172 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000173 if (i != e-1) {
Evan Chengd5a99d72006-09-14 07:35:00 +0000174 const Type *Ty = CP[i].first.getType();
Evan Cheng2d2cec12006-06-29 00:26:09 +0000175 unsigned EntSize =
Evan Chengd6594ae2006-09-12 21:00:35 +0000176 TM.getTargetData()->getTypeSize(Ty);
Evan Chengd5a99d72006-09-14 07:35:00 +0000177 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattner3029f922006-02-09 04:46:04 +0000178 // Emit inter-object padding for alignment.
Evan Chengd5a99d72006-09-14 07:35:00 +0000179 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattner3029f922006-02-09 04:46:04 +0000180 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000181 }
182}
183
Nate Begeman37efe672006-04-22 18:53:45 +0000184/// EmitJumpTableInfo - Print assembly representations of the jump tables used
185/// by the current function to the current output stream.
186///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000187void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
188 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000189 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
190 if (JT.empty()) return;
Owen Andersona69571c2006-05-03 01:29:57 +0000191 const TargetData *TD = TM.getTargetData();
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000192
193 // JTEntryDirective is a string to print sizeof(ptr) for non-PIC jump tables,
194 // and 32 bits for PIC since PIC jump table entries are differences, not
195 // pointers to blocks.
Andrew Lenharthbeec30e2006-09-24 19:45:58 +0000196 // Use the architecture specific relocation directive, if it is set
197 const char *JTEntryDirective = TAI->getJumpTableDirective();
198 if (!JTEntryDirective)
199 JTEntryDirective = TAI->getData32bitsDirective();
Nate Begeman37efe672006-04-22 18:53:45 +0000200
Nate Begeman2f1ae882006-07-27 01:13:04 +0000201 // Pick the directive to use to print the jump table entries, and switch to
202 // the appropriate section.
203 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner0336fdb2006-10-06 22:50:56 +0000204 TargetLowering *LoweringInfo = TM.getTargetLowering();
205 if (LoweringInfo && LoweringInfo->usesGlobalOffsetTable()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000206 SwitchToDataSection(TAI->getJumpTableDataSection());
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +0000207 if (TD->getPointerSize() == 8 && !JTEntryDirective)
Chris Lattner0336fdb2006-10-06 22:50:56 +0000208 JTEntryDirective = TAI->getData64bitsDirective();
209 } else {
210 // In PIC mode, we need to emit the jump table to the same section as the
211 // function body itself, otherwise the label differences won't make sense.
212 const Function *F = MF.getFunction();
213 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
214 }
Nate Begeman2f1ae882006-07-27 01:13:04 +0000215 } else {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000216 SwitchToDataSection(TAI->getJumpTableDataSection());
Nate Begeman2f1ae882006-07-27 01:13:04 +0000217 if (TD->getPointerSize() == 8)
Jim Laskey563321a2006-09-06 18:34:40 +0000218 JTEntryDirective = TAI->getData64bitsDirective();
Nate Begeman2f1ae882006-07-27 01:13:04 +0000219 }
Owen Andersona69571c2006-05-03 01:29:57 +0000220 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000221
Nate Begeman37efe672006-04-22 18:53:45 +0000222 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000223 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000224
225 // If this jump table was deleted, ignore it.
226 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000227
228 // For PIC codegen, if possible we want to use the SetDirective to reduce
229 // the number of relocations the assembler will generate for the jump table.
230 // Set directives are all printed before the jump table itself.
231 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskey563321a2006-09-06 18:34:40 +0000232 if (TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_)
Nate Begeman52a51e382006-08-12 21:29:52 +0000233 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
234 if (EmittedSets.insert(JTBBs[ii]).second)
235 printSetLabel(i, JTBBs[ii]);
236
Jim Laskey563321a2006-09-06 18:34:40 +0000237 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
238 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000239
Nate Begeman37efe672006-04-22 18:53:45 +0000240 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000241 O << JTEntryDirective << ' ';
Nate Begeman52a51e382006-08-12 21:29:52 +0000242 // If we have emitted set directives for the jump table entries, print
243 // them rather than the entries themselves. If we're emitting PIC, then
244 // emit the table entries as differences between two text section labels.
245 // If we're emitting non-PIC code, then emit the entries as direct
246 // references to the target basic blocks.
247 if (!EmittedSets.empty()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000248 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
249 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Nate Begeman52a51e382006-08-12 21:29:52 +0000250 } else if (TM.getRelocationModel() == Reloc::PIC_) {
251 printBasicBlockLabel(JTBBs[ii], false, false);
Andrew Lenharthbeec30e2006-09-24 19:45:58 +0000252 //If the arch uses custom Jump Table directives, don't calc relative to JT
253 if (!TAI->getJumpTableDirective())
254 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
255 << getFunctionNumber() << '_' << i;
Nate Begeman52a51e382006-08-12 21:29:52 +0000256 } else {
257 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000258 }
Nate Begeman37efe672006-04-22 18:53:45 +0000259 O << '\n';
260 }
261 }
262}
263
Chris Lattnered138932005-12-13 06:32:10 +0000264/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
265/// special global used by LLVM. If so, emit it and return true, otherwise
266/// do nothing and return false.
267bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey78098112006-03-07 22:00:35 +0000268 // Ignore debug and non-emitted data.
269 if (GV->getSection() == "llvm.metadata") return true;
270
271 if (!GV->hasAppendingLinkage()) return false;
272
273 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000274
Chris Lattnercb05af82006-09-26 03:38:18 +0000275 if (GV->getName() == "llvm.used") {
276 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
277 EmitLLVMUsedList(GV->getInitializer());
278 return true;
279 }
Chris Lattnered138932005-12-13 06:32:10 +0000280
Chris Lattner5166b822006-01-12 19:17:23 +0000281 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000282 SwitchToDataSection(TAI->getStaticCtorsSection());
Chris Lattnered138932005-12-13 06:32:10 +0000283 EmitAlignment(2, 0);
284 EmitXXStructorList(GV->getInitializer());
285 return true;
286 }
287
Chris Lattner5166b822006-01-12 19:17:23 +0000288 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000289 SwitchToDataSection(TAI->getStaticDtorsSection());
Chris Lattnered138932005-12-13 06:32:10 +0000290 EmitAlignment(2, 0);
291 EmitXXStructorList(GV->getInitializer());
292 return true;
293 }
294
295 return false;
296}
297
Chris Lattnercb05af82006-09-26 03:38:18 +0000298/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
299/// global in the specified llvm.used list as being used with this directive.
300void AsmPrinter::EmitLLVMUsedList(Constant *List) {
301 const char *Directive = TAI->getUsedDirective();
302
303 // Should be an array of 'sbyte*'.
304 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
305 if (InitList == 0) return;
306
307 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
308 O << Directive;
309 EmitConstantValueOnly(InitList->getOperand(i));
310 O << "\n";
311 }
312}
313
Chris Lattnered138932005-12-13 06:32:10 +0000314/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
315/// function pointers, ignoring the init priority.
316void AsmPrinter::EmitXXStructorList(Constant *List) {
317 // Should be an array of '{ int, void ()* }' structs. The first value is the
318 // init priority, which we ignore.
319 if (!isa<ConstantArray>(List)) return;
320 ConstantArray *InitList = cast<ConstantArray>(List);
321 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
322 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
323 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000324
325 if (CS->getOperand(1)->isNullValue())
326 return; // Found a null terminator, exit printing.
327 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000328 EmitGlobalConstant(CS->getOperand(1));
329 }
330}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000331
Jim Laskeya1a19f82006-10-17 13:41:07 +0000332/// getGlobalLinkName - Returns the asm/link name of of the specified
333/// global variable. Should be overridden by each target asm printer to
334/// generate the appropriate value.
Jim Laskey99e41ee2006-10-17 17:17:24 +0000335const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
336 std::string LinkName;
Jim Laskey03742482006-11-20 20:29:06 +0000337
338 if (isa<Function>(GV)) {
339 LinkName += TAI->getFunctionAddrPrefix();
340 LinkName += Mang->getValueName(GV);
341 LinkName += TAI->getFunctionAddrSuffix();
342 } else {
343 LinkName += TAI->getGlobalVarAddrPrefix();
344 LinkName += Mang->getValueName(GV);
345 LinkName += TAI->getGlobalVarAddrSuffix();
346 }
347
Jim Laskey99e41ee2006-10-17 17:17:24 +0000348 return LinkName;
Jim Laskeya1a19f82006-10-17 13:41:07 +0000349}
350
Chris Lattner25045bd2005-11-21 07:51:36 +0000351// EmitAlignment - Emit an alignment directive to the specified power of two.
352void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnera1ab72d2005-11-14 19:00:06 +0000353 if (GV && GV->getAlignment())
354 NumBits = Log2_32(GV->getAlignment());
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000355 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskey563321a2006-09-06 18:34:40 +0000356 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
357 O << TAI->getAlignDirective() << NumBits << "\n";
Chris Lattnerbfddc202004-08-17 19:14:29 +0000358}
359
Chris Lattner25045bd2005-11-21 07:51:36 +0000360/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000361///
Chris Lattner25045bd2005-11-21 07:51:36 +0000362void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000363 if (NumZeros) {
Jim Laskey563321a2006-09-06 18:34:40 +0000364 if (TAI->getZeroDirective()) {
365 O << TAI->getZeroDirective() << NumZeros;
366 if (TAI->getZeroDirectiveSuffix())
367 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000368 O << "\n";
369 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000370 for (; NumZeros; --NumZeros)
Jim Laskey563321a2006-09-06 18:34:40 +0000371 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000372 }
373 }
374}
375
Chris Lattnera80ba712004-08-16 23:15:22 +0000376// Print out the specified constant, without a storage class. Only the
377// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000378void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000379 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000380 O << "0";
381 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
Chris Lattnerf8f791e2006-09-28 23:17:41 +0000382 assert(CB->getValue());
Chris Lattnera80ba712004-08-16 23:15:22 +0000383 O << "1";
Reid Spencerb83eb642006-10-20 07:07:24 +0000384 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
385 if (CI->getType()->isSigned()) {
386 if (((CI->getSExtValue() << 32) >> 32) == CI->getSExtValue())
387 O << CI->getSExtValue();
388 else
389 O << (uint64_t)CI->getSExtValue();
390 } else
391 O << CI->getZExtValue();
392 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000393 // This is a constant address for a global variable or function. Use the
394 // name of the variable or function as the address value, possibly
395 // decorating it with GlobalVarAddrPrefix/Suffix or
396 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskey563321a2006-09-06 18:34:40 +0000397 if (isa<Function>(GV)) {
398 O << TAI->getFunctionAddrPrefix()
399 << Mang->getValueName(GV)
400 << TAI->getFunctionAddrSuffix();
401 } else {
402 O << TAI->getGlobalVarAddrPrefix()
403 << Mang->getValueName(GV)
404 << TAI->getGlobalVarAddrSuffix();
405 }
Duraid Madina855a5192005-04-02 12:21:51 +0000406 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000407 const TargetData *TD = TM.getTargetData();
Chris Lattnera80ba712004-08-16 23:15:22 +0000408 switch(CE->getOpcode()) {
409 case Instruction::GetElementPtr: {
410 // generate a symbolic expression for the byte address
411 const Constant *ptrVal = CE->getOperand(0);
412 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Andersona69571c2006-05-03 01:29:57 +0000413 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner27e19212005-02-14 21:40:26 +0000414 if (Offset)
415 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000416 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000417 if (Offset > 0)
418 O << ") + " << Offset;
419 else if (Offset < 0)
420 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000421 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000422 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000423 }
424 break;
425 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000426 case Instruction::Trunc:
427 case Instruction::ZExt:
428 case Instruction::SExt:
429 case Instruction::FPTrunc:
430 case Instruction::FPExt:
431 case Instruction::UIToFP:
432 case Instruction::SIToFP:
433 case Instruction::FPToUI:
434 case Instruction::FPToSI:
435 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
436 break;
437 case Instruction::IntToPtr:
438 case Instruction::PtrToInt:
439 case Instruction::BitCast: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000440 // Support only foldable casts to/from pointers that can be eliminated by
441 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000442 Constant *Op = CE->getOperand(0);
443 const Type *OpTy = Op->getType(), *Ty = CE->getType();
444
Chris Lattner4a6bd332006-07-29 01:57:19 +0000445 // Handle casts to pointers by changing them into casts to the appropriate
446 // integer type. This promotes constant folding and simplifies this code.
447 if (isa<PointerType>(Ty)) {
448 const Type *IntPtrTy = TD->getIntPtrType();
Reid Spencer15f46d62006-12-12 01:17:41 +0000449 Instruction::CastOps opcode = Instruction::CastOps(CE->getOpcode());
450 if (opcode == Instruction::IntToPtr)
451 Op = ConstantExpr::getIntegerCast(Op, IntPtrTy, false /*ZExt*/);
452 else
453 Op = ConstantExpr::getCast(Instruction::PtrToInt, Op, IntPtrTy);
Chris Lattner4a6bd332006-07-29 01:57:19 +0000454 return EmitConstantValueOnly(Op);
455 }
456
457 // We know the dest type is not a pointer. Is the src value a pointer or
458 // integral?
459 if (isa<PointerType>(OpTy) || OpTy->isIntegral()) {
460 // We can emit the pointer value into this slot if the slot is an
461 // integer slot greater or equal to the size of the pointer.
462 if (Ty->isIntegral() && TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
463 return EmitConstantValueOnly(Op);
464 }
465
466 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000467 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000468 break;
469 }
470 case Instruction::Add:
471 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000472 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattnera80ba712004-08-16 23:15:22 +0000473 O << ") + (";
Chris Lattner25045bd2005-11-21 07:51:36 +0000474 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000475 O << ")";
476 break;
477 default:
478 assert(0 && "Unsupported operator!");
479 }
480 } else {
481 assert(0 && "Unknown constant value!");
482 }
483}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000484
485/// toOctal - Convert the low order bits of X into an octal digit.
486///
487static inline char toOctal(int X) {
488 return (X&7)+'0';
489}
490
Chris Lattner2980cef2005-11-10 18:06:33 +0000491/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000492/// the predicate isString is true.
493///
Chris Lattner2980cef2005-11-10 18:06:33 +0000494static void printAsCString(std::ostream &O, const ConstantArray *CVA,
495 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000496 assert(CVA->isString() && "Array is not string compatible!");
497
498 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000499 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000500 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000501 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000502
503 if (C == '"') {
504 O << "\\\"";
505 } else if (C == '\\') {
506 O << "\\\\";
507 } else if (isprint(C)) {
508 O << C;
509 } else {
510 switch(C) {
511 case '\b': O << "\\b"; break;
512 case '\f': O << "\\f"; break;
513 case '\n': O << "\\n"; break;
514 case '\r': O << "\\r"; break;
515 case '\t': O << "\\t"; break;
516 default:
517 O << '\\';
518 O << toOctal(C >> 6);
519 O << toOctal(C >> 3);
520 O << toOctal(C >> 0);
521 break;
522 }
523 }
524 }
525 O << "\"";
526}
527
Jeff Cohenc884db42006-05-02 01:16:28 +0000528/// EmitString - Emit a zero-byte-terminated string constant.
529///
530void AsmPrinter::EmitString(const ConstantArray *CVA) const {
531 unsigned NumElts = CVA->getNumOperands();
Jim Laskey563321a2006-09-06 18:34:40 +0000532 if (TAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000533 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskey563321a2006-09-06 18:34:40 +0000534 O << TAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000535 printAsCString(O, CVA, NumElts-1);
536 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000537 O << TAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000538 printAsCString(O, CVA, NumElts);
539 }
540 O << "\n";
541}
542
Chris Lattner25045bd2005-11-21 07:51:36 +0000543/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000544///
Chris Lattner25045bd2005-11-21 07:51:36 +0000545void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Andersona69571c2006-05-03 01:29:57 +0000546 const TargetData *TD = TM.getTargetData();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000547
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000548 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000549 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000550 return;
551 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
552 if (CVA->isString()) {
Jeff Cohenc884db42006-05-02 01:16:28 +0000553 EmitString(CVA);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000554 } else { // Not a string. Print the values in successive locations
555 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000556 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000557 }
558 return;
559 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
560 // Print the fields in successive locations. Pad to align if needed!
Owen Andersona69571c2006-05-03 01:29:57 +0000561 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000562 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000563 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
564 const Constant* field = CVS->getOperand(i);
565
566 // Check if padding is needed and insert one or more 0s.
Owen Andersona69571c2006-05-03 01:29:57 +0000567 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000568 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner1b7e2352004-08-17 06:36:49 +0000569 : cvsLayout->MemberOffsets[i+1])
570 - cvsLayout->MemberOffsets[i]) - fieldSize;
571 sizeSoFar += fieldSize + padSize;
572
573 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000574 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000575
576 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000577 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000578 }
579 assert(sizeSoFar == cvsLayout->StructSize &&
580 "Layout of constant struct may be incorrect!");
581 return;
582 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
583 // FP Constants are printed as integer constants to avoid losing
584 // precision...
585 double Val = CFP->getValue();
586 if (CFP->getType() == Type::DoubleTy) {
Jim Laskey563321a2006-09-06 18:34:40 +0000587 if (TAI->getData64bitsDirective())
588 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
589 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000590 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000591 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
592 << "\t" << TAI->getCommentString()
593 << " double most significant word " << Val << "\n";
594 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
595 << "\t" << TAI->getCommentString()
596 << " double least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000597 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000598 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
599 << "\t" << TAI->getCommentString()
600 << " double least significant word " << Val << "\n";
601 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
602 << "\t" << TAI->getCommentString()
603 << " double most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000604 }
605 return;
606 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000607 O << TAI->getData32bitsDirective() << FloatToBits(Val)
608 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000609 return;
610 }
611 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
612 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000613 uint64_t Val = CI->getZExtValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000614
Jim Laskey563321a2006-09-06 18:34:40 +0000615 if (TAI->getData64bitsDirective())
616 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000617 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000618 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
619 << "\t" << TAI->getCommentString()
620 << " Double-word most significant word " << Val << "\n";
621 O << TAI->getData32bitsDirective() << unsigned(Val)
622 << "\t" << TAI->getCommentString()
623 << " Double-word least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000624 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000625 O << TAI->getData32bitsDirective() << unsigned(Val)
626 << "\t" << TAI->getCommentString()
627 << " Double-word least significant word " << Val << "\n";
628 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
629 << "\t" << TAI->getCommentString()
630 << " Double-word most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000631 }
632 return;
633 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000634 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
635 const PackedType *PTy = CP->getType();
636
637 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
638 EmitGlobalConstant(CP->getOperand(I));
639
640 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000641 }
642
643 const Type *type = CV->getType();
Evan Chengd6594ae2006-09-12 21:00:35 +0000644 printDataDirective(type);
Chris Lattner25045bd2005-11-21 07:51:36 +0000645 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000646 O << "\n";
647}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000648
Evan Chengd6594ae2006-09-12 21:00:35 +0000649void
650AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
651 // Target doesn't support this yet!
652 abort();
653}
654
Chris Lattner3ce9b672006-09-26 23:59:50 +0000655/// PrintSpecial - Print information related to the specified machine instr
656/// that is independent of the operand, and may be independent of the instr
657/// itself. This can be useful for portably encoding the comment character
658/// or other bits of target-specific knowledge into the asmstrings. The
659/// syntax used is ${:comment}. Targets can override this to add support
660/// for their own strange codes.
661void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattnerbae02cf2006-09-27 00:06:07 +0000662 if (!strcmp(Code, "private")) {
663 O << TAI->getPrivateGlobalPrefix();
664 } else if (!strcmp(Code, "comment")) {
Chris Lattner3ce9b672006-09-26 23:59:50 +0000665 O << TAI->getCommentString();
666 } else if (!strcmp(Code, "uid")) {
667 // Assign a unique ID to this machine instruction.
668 static const MachineInstr *LastMI = 0;
669 static unsigned Counter = 0U-1;
670 // If this is a new machine instruction, bump the counter.
671 if (LastMI != MI) { ++Counter; LastMI = MI; }
672 O << Counter;
673 } else {
Bill Wendlinge8156192006-12-07 01:30:32 +0000674 cerr << "Unknown special formatter '" << Code
675 << "' for machine instr: " << *MI;
Chris Lattner3ce9b672006-09-26 23:59:50 +0000676 exit(1);
677 }
678}
679
680
Chris Lattner0264d1a2006-01-27 02:10:10 +0000681/// printInlineAsm - This method formats and prints the specified machine
682/// instruction that is an inline asm.
683void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000684 unsigned NumOperands = MI->getNumOperands();
685
686 // Count the number of register definitions.
687 unsigned NumDefs = 0;
Chris Lattner67942f52006-09-05 20:02:51 +0000688 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
689 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000690 assert(NumDefs != NumOperands-1 && "No asm string?");
691
692 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +0000693
694 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000695 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +0000696
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000697 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
698 if (AsmStr[0] == 0) {
699 O << "\n"; // Tab already printed, avoid double indenting next instr.
700 return;
701 }
702
Jim Laskey563321a2006-09-06 18:34:40 +0000703 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000704
Chris Lattner66099132006-02-01 22:41:11 +0000705 // The variant of the current asmprinter: FIXME: change.
706 int AsmPrinterVariant = 0;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000707
Chris Lattner66099132006-02-01 22:41:11 +0000708 int CurVariant = -1; // The number of the {.|.|.} region we are in.
709 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +0000710
Chris Lattner66099132006-02-01 22:41:11 +0000711 while (*LastEmitted) {
712 switch (*LastEmitted) {
713 default: {
714 // Not a special case, emit the string section literally.
715 const char *LiteralEnd = LastEmitted+1;
716 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +0000717 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +0000718 ++LiteralEnd;
719 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
720 O.write(LastEmitted, LiteralEnd-LastEmitted);
721 LastEmitted = LiteralEnd;
722 break;
723 }
Chris Lattner1c059972006-05-05 21:47:05 +0000724 case '\n':
725 ++LastEmitted; // Consume newline character.
726 O << "\n\t"; // Indent code with newline.
727 break;
Chris Lattner66099132006-02-01 22:41:11 +0000728 case '$': {
729 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000730 bool Done = true;
731
732 // Handle escapes.
733 switch (*LastEmitted) {
734 default: Done = false; break;
735 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +0000736 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
737 O << '$';
738 ++LastEmitted; // Consume second '$' character.
739 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000740 case '(': // $( -> same as GCC's { character.
741 ++LastEmitted; // Consume '(' character.
742 if (CurVariant != -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000743 cerr << "Nested variants found in inline asm string: '"
744 << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000745 exit(1);
746 }
747 CurVariant = 0; // We're in the first variant now.
748 break;
749 case '|':
750 ++LastEmitted; // consume '|' character.
751 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000752 cerr << "Found '|' character outside of variant in inline asm "
753 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000754 exit(1);
755 }
756 ++CurVariant; // We're in the next variant.
757 break;
758 case ')': // $) -> same as GCC's } char.
759 ++LastEmitted; // consume ')' character.
760 if (CurVariant == -1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000761 cerr << "Found '}' character outside of variant in inline asm "
762 << "string: '" << AsmStr << "'\n";
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000763 exit(1);
764 }
765 CurVariant = -1;
766 break;
Chris Lattner66099132006-02-01 22:41:11 +0000767 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +0000768 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +0000769
770 bool HasCurlyBraces = false;
771 if (*LastEmitted == '{') { // ${variable}
772 ++LastEmitted; // Consume '{' character.
773 HasCurlyBraces = true;
774 }
775
776 const char *IDStart = LastEmitted;
777 char *IDEnd;
778 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
779 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000780 cerr << "Bad $ operand number in inline asm string: '"
781 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +0000782 exit(1);
783 }
784 LastEmitted = IDEnd;
785
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000786 char Modifier[2] = { 0, 0 };
787
Chris Lattner66099132006-02-01 22:41:11 +0000788 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000789 // If we have curly braces, check for a modifier character. This
790 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
791 if (*LastEmitted == ':') {
792 ++LastEmitted; // Consume ':' character.
793 if (*LastEmitted == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000794 cerr << "Bad ${:} expression in inline asm string: '"
795 << AsmStr << "'\n";
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000796 exit(1);
797 }
798
799 Modifier[0] = *LastEmitted;
800 ++LastEmitted; // Consume modifier character.
801 }
802
Chris Lattner66099132006-02-01 22:41:11 +0000803 if (*LastEmitted != '}') {
Bill Wendlinge8156192006-12-07 01:30:32 +0000804 cerr << "Bad ${} expression in inline asm string: '"
805 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +0000806 exit(1);
807 }
808 ++LastEmitted; // Consume '}' character.
809 }
810
811 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000812 cerr << "Invalid $ operand number in inline asm string: '"
813 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +0000814 exit(1);
815 }
816
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000817 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +0000818 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000819 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
820 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000821
822 bool Error = false;
823
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000824 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000825 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000826 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000827 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
828 OpNo += (OpFlags >> 3) + 1;
829 }
Chris Lattnerdd260332006-02-24 20:21:58 +0000830
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000831 if (OpNo >= MI->getNumOperands()) {
832 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +0000833 } else {
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000834 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
835 ++OpNo; // Skip over the ID number.
836
837 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
838 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
839 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
840 Modifier[0] ? Modifier : 0);
841 } else {
842 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
843 Modifier[0] ? Modifier : 0);
844 }
Chris Lattnerdd260332006-02-24 20:21:58 +0000845 }
846 if (Error) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000847 cerr << "Invalid operand found in inline asm: '"
848 << AsmStr << "'\n";
Chris Lattner66099132006-02-01 22:41:11 +0000849 MI->dump();
850 exit(1);
851 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000852 }
Chris Lattner66099132006-02-01 22:41:11 +0000853 break;
854 }
Chris Lattner66099132006-02-01 22:41:11 +0000855 }
856 }
Jim Laskey563321a2006-09-06 18:34:40 +0000857 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattner66099132006-02-01 22:41:11 +0000858}
859
860/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
861/// instruction, using the specified assembler variant. Targets should
862/// overried this to format as appropriate.
863bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000864 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +0000865 // Target doesn't support this yet!
866 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +0000867}
Chris Lattnerdd260332006-02-24 20:21:58 +0000868
869bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
870 unsigned AsmVariant,
871 const char *ExtraCode) {
872 // Target doesn't support this yet!
873 return true;
874}
Nate Begeman37efe672006-04-22 18:53:45 +0000875
876/// printBasicBlockLabel - This method prints the label for the specified
877/// MachineBasicBlock
Nate Begemancdf38c42006-05-02 05:37:32 +0000878void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
879 bool printColon,
880 bool printComment) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000881 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman9d51eeb2006-05-02 17:36:46 +0000882 << MBB->getNumber();
Nate Begemancdf38c42006-05-02 05:37:32 +0000883 if (printColon)
884 O << ':';
Chris Lattner9c78ecb2006-10-05 21:40:14 +0000885 if (printComment && MBB->getBasicBlock())
Jim Laskey563321a2006-09-06 18:34:40 +0000886 O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
Nate Begeman37efe672006-04-22 18:53:45 +0000887}
Nate Begeman52a51e382006-08-12 21:29:52 +0000888
889/// printSetLabel - This method prints a set label for the specified
890/// MachineBasicBlock
891void AsmPrinter::printSetLabel(unsigned uid,
892 const MachineBasicBlock *MBB) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000893 if (!TAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +0000894 return;
895
Jim Laskey563321a2006-09-06 18:34:40 +0000896 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
897 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman52a51e382006-08-12 21:29:52 +0000898 printBasicBlockLabel(MBB, false, false);
Jim Laskey563321a2006-09-06 18:34:40 +0000899 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman52a51e382006-08-12 21:29:52 +0000900 << '_' << uid << '\n';
901}
Evan Chengd6594ae2006-09-12 21:00:35 +0000902
Evan Cheng41349c12006-11-01 09:23:08 +0000903void AsmPrinter::printSetLabel(unsigned uid, unsigned uid2,
904 const MachineBasicBlock *MBB) const {
905 if (!TAI->getSetDirective())
906 return;
907
908 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
909 << getFunctionNumber() << '_' << uid << '_' << uid2
910 << "_set_" << MBB->getNumber() << ',';
911 printBasicBlockLabel(MBB, false, false);
912 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
913 << '_' << uid << '_' << uid2 << '\n';
914}
915
Evan Chengd6594ae2006-09-12 21:00:35 +0000916/// printDataDirective - This method prints the asm directive for the
917/// specified type.
918void AsmPrinter::printDataDirective(const Type *type) {
919 const TargetData *TD = TM.getTargetData();
920 switch (type->getTypeID()) {
921 case Type::BoolTyID:
922 case Type::UByteTyID: case Type::SByteTyID:
923 O << TAI->getData8bitsDirective();
924 break;
925 case Type::UShortTyID: case Type::ShortTyID:
926 O << TAI->getData16bitsDirective();
927 break;
928 case Type::PointerTyID:
929 if (TD->getPointerSize() == 8) {
930 assert(TAI->getData64bitsDirective() &&
931 "Target cannot handle 64-bit pointer exprs!");
932 O << TAI->getData64bitsDirective();
933 break;
934 }
935 //Fall through for pointer size == int size
936 case Type::UIntTyID: case Type::IntTyID:
937 O << TAI->getData32bitsDirective();
938 break;
939 case Type::ULongTyID: case Type::LongTyID:
940 assert(TAI->getData64bitsDirective() &&
941 "Target cannot handle 64-bit constant exprs!");
942 O << TAI->getData64bitsDirective();
943 break;
944 case Type::FloatTyID: case Type::DoubleTyID:
945 assert (0 && "Should have already output floating point constant.");
946 default:
947 assert (0 && "Can't handle printing this type of thing");
948 break;
949 }
950}