blob: e6d56244c9c5e8c441c3d4f156192638b52c86ea [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"
Jim Laskey563321a2006-09-06 18:34:40 +000023#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000024#include "llvm/Target/TargetData.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000025#include "llvm/Target/TargetMachine.h"
Duraid Madina2e096c12005-12-28 06:29:02 +000026#include <iostream>
Chris Lattner66099132006-02-01 22:41:11 +000027#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000028using namespace llvm;
29
Jim Laskey563321a2006-09-06 18:34:40 +000030AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm, TargetAsmInfo *T)
31: FunctionNumber(0), O(o), TM(tm), TAI(T)
32{}
Chris Lattnered138932005-12-13 06:32:10 +000033
34
Chris Lattner4632d7a2006-05-09 04:59:56 +000035/// SwitchToTextSection - Switch to the specified text section of the executable
36/// if we are not already in it!
Chris Lattnerac28fbd2005-11-21 07:06:27 +000037///
Chris Lattner4632d7a2006-05-09 04:59:56 +000038void AsmPrinter::SwitchToTextSection(const char *NewSection,
39 const GlobalValue *GV) {
Chris Lattnerac28fbd2005-11-21 07:06:27 +000040 std::string NS;
Chris Lattnera7090ae2006-05-09 05:24:50 +000041 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000042 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnera7090ae2006-05-09 05:24:50 +000043 else
44 NS = NewSection;
45
46 // If we're already in this section, we're done.
47 if (CurrentSection == NS) return;
Jeff Cohen51b776d2006-05-02 03:58:45 +000048
Chris Lattner7faec9b2006-05-09 05:33:48 +000049 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000050 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
51 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen51b776d2006-05-02 03:58:45 +000052
Chris Lattner7faec9b2006-05-09 05:33:48 +000053 CurrentSection = NS;
54
55 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000056 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattnerac28fbd2005-11-21 07:06:27 +000057}
58
Nate Begeman2f1ae882006-07-27 01:13:04 +000059/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner4632d7a2006-05-09 04:59:56 +000060/// if we are not already in it!
61///
62void AsmPrinter::SwitchToDataSection(const char *NewSection,
63 const GlobalValue *GV) {
64 std::string NS;
Chris Lattnerb81cb612006-05-09 05:23:12 +000065 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000066 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnerb81cb612006-05-09 05:23:12 +000067 else
68 NS = NewSection;
Chris Lattner4632d7a2006-05-09 04:59:56 +000069
Chris Lattnerb81cb612006-05-09 05:23:12 +000070 // If we're already in this section, we're done.
71 if (CurrentSection == NS) return;
72
Chris Lattner7faec9b2006-05-09 05:33:48 +000073 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000074 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
75 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner7faec9b2006-05-09 05:33:48 +000076
77 CurrentSection = NS;
Chris Lattner4632d7a2006-05-09 04:59:56 +000078
Chris Lattner7faec9b2006-05-09 05:33:48 +000079 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000080 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner4632d7a2006-05-09 04:59:56 +000081}
82
83
Chris Lattnera80ba712004-08-16 23:15:22 +000084bool AsmPrinter::doInitialization(Module &M) {
Jim Laskey563321a2006-09-06 18:34:40 +000085 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattner2c1b1592006-01-23 23:47:53 +000086
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000087 if (!M.getModuleInlineAsm().empty())
Jim Laskey563321a2006-09-06 18:34:40 +000088 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000089 << M.getModuleInlineAsm()
Jim Laskey563321a2006-09-06 18:34:40 +000090 << "\n" << TAI->getCommentString()
91 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +000092
Chris Lattner4632d7a2006-05-09 04:59:56 +000093 SwitchToDataSection("", 0); // Reset back to no section.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000094
95 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
96 DebugInfo->AnalyzeModule(M);
97 }
98
Chris Lattnera80ba712004-08-16 23:15:22 +000099 return false;
100}
101
102bool AsmPrinter::doFinalization(Module &M) {
103 delete Mang; Mang = 0;
104 return false;
105}
106
Chris Lattner25045bd2005-11-21 07:51:36 +0000107void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000108 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +0000109 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner77bc2282005-11-21 08:13:27 +0000110 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +0000111}
112
Chris Lattner3b4fd322005-11-21 08:25:09 +0000113/// EmitConstantPool - Print to the current output stream assembly
114/// representations of the constants in the constant pool MCP. This is
115/// used to print out constants which have been "spilled to memory" by
116/// the code generator.
117///
118void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000119 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000120 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000121
122 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
123 // in special sections.
124 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
125 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
126 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
127 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000128 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng2d2cec12006-06-29 00:26:09 +0000129 MachineConstantPoolEntry CPE = CP[i];
130 const Constant *CV = CPE.Val;
131 const Type *Ty = CV->getType();
Jim Laskey563321a2006-09-06 18:34:40 +0000132 if (TAI->getFourByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000133 TM.getTargetData()->getTypeSize(Ty) == 4)
134 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000135 else if (TAI->getEightByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000136 TM.getTargetData()->getTypeSize(Ty) == 8)
137 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000138 else if (TAI->getSixteenByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000139 TM.getTargetData()->getTypeSize(Ty) == 16)
140 SixteenByteCPs.push_back(std::make_pair(CPE, i));
141 else
142 OtherCPs.push_back(std::make_pair(CPE, i));
143 }
144
145 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskey563321a2006-09-06 18:34:40 +0000146 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
147 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
148 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
149 SixteenByteCPs);
150 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000151}
152
153void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
154 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
155 if (CP.empty()) return;
156
157 SwitchToDataSection(Section, 0);
158 EmitAlignment(Alignment);
159 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Jim Laskey563321a2006-09-06 18:34:40 +0000160 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
161 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Cheng2d2cec12006-06-29 00:26:09 +0000162 WriteTypeSymbolic(O, CP[i].first.Val->getType(), 0) << '\n';
163 EmitGlobalConstant(CP[i].first.Val);
Chris Lattner3029f922006-02-09 04:46:04 +0000164 if (i != e-1) {
Evan Cheng2d2cec12006-06-29 00:26:09 +0000165 unsigned EntSize =
166 TM.getTargetData()->getTypeSize(CP[i].first.Val->getType());
167 unsigned ValEnd = CP[i].first.Offset + EntSize;
Chris Lattner3029f922006-02-09 04:46:04 +0000168 // Emit inter-object padding for alignment.
Evan Cheng2d2cec12006-06-29 00:26:09 +0000169 EmitZeros(CP[i+1].first.Offset-ValEnd);
Chris Lattner3029f922006-02-09 04:46:04 +0000170 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000171 }
172}
173
Nate Begeman37efe672006-04-22 18:53:45 +0000174/// EmitJumpTableInfo - Print assembly representations of the jump tables used
175/// by the current function to the current output stream.
176///
177void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
178 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
179 if (JT.empty()) return;
Owen Andersona69571c2006-05-03 01:29:57 +0000180 const TargetData *TD = TM.getTargetData();
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000181
182 // JTEntryDirective is a string to print sizeof(ptr) for non-PIC jump tables,
183 // and 32 bits for PIC since PIC jump table entries are differences, not
184 // pointers to blocks.
Jim Laskey563321a2006-09-06 18:34:40 +0000185 const char *JTEntryDirective = TAI->getData32bitsDirective();
Nate Begeman37efe672006-04-22 18:53:45 +0000186
Nate Begeman2f1ae882006-07-27 01:13:04 +0000187 // Pick the directive to use to print the jump table entries, and switch to
188 // the appropriate section.
189 if (TM.getRelocationModel() == Reloc::PIC_) {
Jim Laskey563321a2006-09-06 18:34:40 +0000190 SwitchToTextSection(TAI->getJumpTableTextSection(), 0);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000191 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000192 SwitchToDataSection(TAI->getJumpTableDataSection(), 0);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000193 if (TD->getPointerSize() == 8)
Jim Laskey563321a2006-09-06 18:34:40 +0000194 JTEntryDirective = TAI->getData64bitsDirective();
Nate Begeman2f1ae882006-07-27 01:13:04 +0000195 }
Owen Andersona69571c2006-05-03 01:29:57 +0000196 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000197
Nate Begeman37efe672006-04-22 18:53:45 +0000198 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000199 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
200
201 // For PIC codegen, if possible we want to use the SetDirective to reduce
202 // the number of relocations the assembler will generate for the jump table.
203 // Set directives are all printed before the jump table itself.
204 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskey563321a2006-09-06 18:34:40 +0000205 if (TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_)
Nate Begeman52a51e382006-08-12 21:29:52 +0000206 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
207 if (EmittedSets.insert(JTBBs[ii]).second)
208 printSetLabel(i, JTBBs[ii]);
209
Jim Laskey563321a2006-09-06 18:34:40 +0000210 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
211 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000212
Nate Begeman37efe672006-04-22 18:53:45 +0000213 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000214 O << JTEntryDirective << ' ';
Nate Begeman52a51e382006-08-12 21:29:52 +0000215 // If we have emitted set directives for the jump table entries, print
216 // them rather than the entries themselves. If we're emitting PIC, then
217 // emit the table entries as differences between two text section labels.
218 // If we're emitting non-PIC code, then emit the entries as direct
219 // references to the target basic blocks.
220 if (!EmittedSets.empty()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000221 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
222 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Nate Begeman52a51e382006-08-12 21:29:52 +0000223 } else if (TM.getRelocationModel() == Reloc::PIC_) {
224 printBasicBlockLabel(JTBBs[ii], false, false);
Jim Laskey563321a2006-09-06 18:34:40 +0000225 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
226 << getFunctionNumber() << '_' << i;
Nate Begeman52a51e382006-08-12 21:29:52 +0000227 } else {
228 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000229 }
Nate Begeman37efe672006-04-22 18:53:45 +0000230 O << '\n';
231 }
232 }
233}
234
Chris Lattnered138932005-12-13 06:32:10 +0000235/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
236/// special global used by LLVM. If so, emit it and return true, otherwise
237/// do nothing and return false.
238bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey78098112006-03-07 22:00:35 +0000239 // Ignore debug and non-emitted data.
240 if (GV->getSection() == "llvm.metadata") return true;
241
242 if (!GV->hasAppendingLinkage()) return false;
243
244 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000245
246 if (GV->getName() == "llvm.used")
247 return true; // No need to emit this at all.
248
Chris Lattner5166b822006-01-12 19:17:23 +0000249 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000250 SwitchToDataSection(TAI->getStaticCtorsSection(), 0);
Chris Lattnered138932005-12-13 06:32:10 +0000251 EmitAlignment(2, 0);
252 EmitXXStructorList(GV->getInitializer());
253 return true;
254 }
255
Chris Lattner5166b822006-01-12 19:17:23 +0000256 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000257 SwitchToDataSection(TAI->getStaticDtorsSection(), 0);
Chris Lattnered138932005-12-13 06:32:10 +0000258 EmitAlignment(2, 0);
259 EmitXXStructorList(GV->getInitializer());
260 return true;
261 }
262
263 return false;
264}
265
266/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
267/// function pointers, ignoring the init priority.
268void AsmPrinter::EmitXXStructorList(Constant *List) {
269 // Should be an array of '{ int, void ()* }' structs. The first value is the
270 // init priority, which we ignore.
271 if (!isa<ConstantArray>(List)) return;
272 ConstantArray *InitList = cast<ConstantArray>(List);
273 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
274 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
275 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000276
277 if (CS->getOperand(1)->isNullValue())
278 return; // Found a null terminator, exit printing.
279 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000280 EmitGlobalConstant(CS->getOperand(1));
281 }
282}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000283
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000284/// getPreferredAlignmentLog - Return the preferred alignment of the
285/// specified global, returned in log form. This includes an explicitly
286/// requested alignment (if the global has one).
287unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Jim Laskey35f8c202006-06-15 13:10:58 +0000288 const Type *ElemType = GV->getType()->getElementType();
289 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(ElemType);
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000290 if (GV->getAlignment() > (1U << Alignment))
291 Alignment = Log2_32(GV->getAlignment());
292
Chris Lattner519ea2a2006-02-05 01:46:49 +0000293 if (GV->hasInitializer()) {
Jim Laskeyd5a932b2006-06-15 19:37:14 +0000294 // Always round up alignment of global doubles to 8 bytes.
295 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
296 Alignment = 3;
Chris Lattner519ea2a2006-02-05 01:46:49 +0000297 if (Alignment < 4) {
298 // If the global is not external, see if it is large. If so, give it a
299 // larger alignment.
Jim Laskey35f8c202006-06-15 13:10:58 +0000300 if (TM.getTargetData()->getTypeSize(ElemType) > 128)
Chris Lattner519ea2a2006-02-05 01:46:49 +0000301 Alignment = 4; // 16-byte alignment.
302 }
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000303 }
304 return Alignment;
305}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000306
Chris Lattner25045bd2005-11-21 07:51:36 +0000307// EmitAlignment - Emit an alignment directive to the specified power of two.
308void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnera1ab72d2005-11-14 19:00:06 +0000309 if (GV && GV->getAlignment())
310 NumBits = Log2_32(GV->getAlignment());
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000311 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskey563321a2006-09-06 18:34:40 +0000312 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
313 O << TAI->getAlignDirective() << NumBits << "\n";
Chris Lattnerbfddc202004-08-17 19:14:29 +0000314}
315
Chris Lattner25045bd2005-11-21 07:51:36 +0000316/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000317///
Chris Lattner25045bd2005-11-21 07:51:36 +0000318void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000319 if (NumZeros) {
Jim Laskey563321a2006-09-06 18:34:40 +0000320 if (TAI->getZeroDirective()) {
321 O << TAI->getZeroDirective() << NumZeros;
322 if (TAI->getZeroDirectiveSuffix())
323 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000324 O << "\n";
325 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000326 for (; NumZeros; --NumZeros)
Jim Laskey563321a2006-09-06 18:34:40 +0000327 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000328 }
329 }
330}
331
Chris Lattnera80ba712004-08-16 23:15:22 +0000332// Print out the specified constant, without a storage class. Only the
333// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000334void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000335 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000336 O << "0";
337 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
338 assert(CB == ConstantBool::True);
339 O << "1";
340 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
341 if (((CI->getValue() << 32) >> 32) == CI->getValue())
342 O << CI->getValue();
343 else
Duraid Madina45606572005-05-15 13:05:48 +0000344 O << (uint64_t)CI->getValue();
Chris Lattnera80ba712004-08-16 23:15:22 +0000345 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
346 O << CI->getValue();
Chris Lattner450de392005-11-10 18:36:17 +0000347 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000348 // This is a constant address for a global variable or function. Use the
349 // name of the variable or function as the address value, possibly
350 // decorating it with GlobalVarAddrPrefix/Suffix or
351 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskey563321a2006-09-06 18:34:40 +0000352 if (isa<Function>(GV)) {
353 O << TAI->getFunctionAddrPrefix()
354 << Mang->getValueName(GV)
355 << TAI->getFunctionAddrSuffix();
356 } else {
357 O << TAI->getGlobalVarAddrPrefix()
358 << Mang->getValueName(GV)
359 << TAI->getGlobalVarAddrSuffix();
360 }
Duraid Madina855a5192005-04-02 12:21:51 +0000361 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000362 const TargetData *TD = TM.getTargetData();
Chris Lattnera80ba712004-08-16 23:15:22 +0000363 switch(CE->getOpcode()) {
364 case Instruction::GetElementPtr: {
365 // generate a symbolic expression for the byte address
366 const Constant *ptrVal = CE->getOperand(0);
367 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Andersona69571c2006-05-03 01:29:57 +0000368 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner27e19212005-02-14 21:40:26 +0000369 if (Offset)
370 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000371 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000372 if (Offset > 0)
373 O << ") + " << Offset;
374 else if (Offset < 0)
375 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000376 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000377 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000378 }
379 break;
380 }
381 case Instruction::Cast: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000382 // Support only foldable casts to/from pointers that can be eliminated by
383 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000384 Constant *Op = CE->getOperand(0);
385 const Type *OpTy = Op->getType(), *Ty = CE->getType();
386
Chris Lattner4a6bd332006-07-29 01:57:19 +0000387 // Handle casts to pointers by changing them into casts to the appropriate
388 // integer type. This promotes constant folding and simplifies this code.
389 if (isa<PointerType>(Ty)) {
390 const Type *IntPtrTy = TD->getIntPtrType();
391 Op = ConstantExpr::getCast(Op, IntPtrTy);
392 return EmitConstantValueOnly(Op);
393 }
394
395 // We know the dest type is not a pointer. Is the src value a pointer or
396 // integral?
397 if (isa<PointerType>(OpTy) || OpTy->isIntegral()) {
398 // We can emit the pointer value into this slot if the slot is an
399 // integer slot greater or equal to the size of the pointer.
400 if (Ty->isIntegral() && TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
401 return EmitConstantValueOnly(Op);
402 }
403
404 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000405 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000406 break;
407 }
408 case Instruction::Add:
409 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000410 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattnera80ba712004-08-16 23:15:22 +0000411 O << ") + (";
Chris Lattner25045bd2005-11-21 07:51:36 +0000412 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000413 O << ")";
414 break;
415 default:
416 assert(0 && "Unsupported operator!");
417 }
418 } else {
419 assert(0 && "Unknown constant value!");
420 }
421}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000422
423/// toOctal - Convert the low order bits of X into an octal digit.
424///
425static inline char toOctal(int X) {
426 return (X&7)+'0';
427}
428
Chris Lattner2980cef2005-11-10 18:06:33 +0000429/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000430/// the predicate isString is true.
431///
Chris Lattner2980cef2005-11-10 18:06:33 +0000432static void printAsCString(std::ostream &O, const ConstantArray *CVA,
433 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000434 assert(CVA->isString() && "Array is not string compatible!");
435
436 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000437 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000438 unsigned char C =
Chris Lattnerdea18b62005-01-08 19:59:10 +0000439 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000440
441 if (C == '"') {
442 O << "\\\"";
443 } else if (C == '\\') {
444 O << "\\\\";
445 } else if (isprint(C)) {
446 O << C;
447 } else {
448 switch(C) {
449 case '\b': O << "\\b"; break;
450 case '\f': O << "\\f"; break;
451 case '\n': O << "\\n"; break;
452 case '\r': O << "\\r"; break;
453 case '\t': O << "\\t"; break;
454 default:
455 O << '\\';
456 O << toOctal(C >> 6);
457 O << toOctal(C >> 3);
458 O << toOctal(C >> 0);
459 break;
460 }
461 }
462 }
463 O << "\"";
464}
465
Jeff Cohenc884db42006-05-02 01:16:28 +0000466/// EmitString - Emit a zero-byte-terminated string constant.
467///
468void AsmPrinter::EmitString(const ConstantArray *CVA) const {
469 unsigned NumElts = CVA->getNumOperands();
Jim Laskey563321a2006-09-06 18:34:40 +0000470 if (TAI->getAscizDirective() && NumElts &&
Jeff Cohenc884db42006-05-02 01:16:28 +0000471 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
Jim Laskey563321a2006-09-06 18:34:40 +0000472 O << TAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000473 printAsCString(O, CVA, NumElts-1);
474 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000475 O << TAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000476 printAsCString(O, CVA, NumElts);
477 }
478 O << "\n";
479}
480
Chris Lattner25045bd2005-11-21 07:51:36 +0000481/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000482///
Chris Lattner25045bd2005-11-21 07:51:36 +0000483void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Andersona69571c2006-05-03 01:29:57 +0000484 const TargetData *TD = TM.getTargetData();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000485
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000486 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000487 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000488 return;
489 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
490 if (CVA->isString()) {
Jeff Cohenc884db42006-05-02 01:16:28 +0000491 EmitString(CVA);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000492 } else { // Not a string. Print the values in successive locations
493 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000494 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000495 }
496 return;
497 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
498 // Print the fields in successive locations. Pad to align if needed!
Owen Andersona69571c2006-05-03 01:29:57 +0000499 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000500 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000501 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
502 const Constant* field = CVS->getOperand(i);
503
504 // Check if padding is needed and insert one or more 0s.
Owen Andersona69571c2006-05-03 01:29:57 +0000505 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000506 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner1b7e2352004-08-17 06:36:49 +0000507 : cvsLayout->MemberOffsets[i+1])
508 - cvsLayout->MemberOffsets[i]) - fieldSize;
509 sizeSoFar += fieldSize + padSize;
510
511 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000512 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000513
514 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000515 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000516 }
517 assert(sizeSoFar == cvsLayout->StructSize &&
518 "Layout of constant struct may be incorrect!");
519 return;
520 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
521 // FP Constants are printed as integer constants to avoid losing
522 // precision...
523 double Val = CFP->getValue();
524 if (CFP->getType() == Type::DoubleTy) {
Jim Laskey563321a2006-09-06 18:34:40 +0000525 if (TAI->getData64bitsDirective())
526 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
527 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000528 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000529 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
530 << "\t" << TAI->getCommentString()
531 << " double most significant word " << Val << "\n";
532 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
533 << "\t" << TAI->getCommentString()
534 << " double least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000535 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000536 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
537 << "\t" << TAI->getCommentString()
538 << " double least significant word " << Val << "\n";
539 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
540 << "\t" << TAI->getCommentString()
541 << " double most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000542 }
543 return;
544 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000545 O << TAI->getData32bitsDirective() << FloatToBits(Val)
546 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000547 return;
548 }
549 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
550 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
551 uint64_t Val = CI->getRawValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000552
Jim Laskey563321a2006-09-06 18:34:40 +0000553 if (TAI->getData64bitsDirective())
554 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000555 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000556 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
557 << "\t" << TAI->getCommentString()
558 << " Double-word most significant word " << Val << "\n";
559 O << TAI->getData32bitsDirective() << unsigned(Val)
560 << "\t" << TAI->getCommentString()
561 << " Double-word least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000562 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000563 O << TAI->getData32bitsDirective() << unsigned(Val)
564 << "\t" << TAI->getCommentString()
565 << " Double-word least significant word " << Val << "\n";
566 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
567 << "\t" << TAI->getCommentString()
568 << " Double-word most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000569 }
570 return;
571 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000572 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
573 const PackedType *PTy = CP->getType();
574
575 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
576 EmitGlobalConstant(CP->getOperand(I));
577
578 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000579 }
580
581 const Type *type = CV->getType();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000582 switch (type->getTypeID()) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000583 case Type::BoolTyID:
Chris Lattner1b7e2352004-08-17 06:36:49 +0000584 case Type::UByteTyID: case Type::SByteTyID:
Jim Laskey563321a2006-09-06 18:34:40 +0000585 O << TAI->getData8bitsDirective();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000586 break;
587 case Type::UShortTyID: case Type::ShortTyID:
Jim Laskey563321a2006-09-06 18:34:40 +0000588 O << TAI->getData16bitsDirective();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000589 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000590 case Type::PointerTyID:
Owen Andersona69571c2006-05-03 01:29:57 +0000591 if (TD->getPointerSize() == 8) {
Jim Laskey563321a2006-09-06 18:34:40 +0000592 assert(TAI->getData64bitsDirective() &&
Evan Chenga9767f62006-06-15 08:10:56 +0000593 "Target cannot handle 64-bit pointer exprs!");
Jim Laskey563321a2006-09-06 18:34:40 +0000594 O << TAI->getData64bitsDirective();
Andrew Lenharth571c9c32005-02-04 13:47:16 +0000595 break;
596 }
597 //Fall through for pointer size == int size
Chris Lattner1b7e2352004-08-17 06:36:49 +0000598 case Type::UIntTyID: case Type::IntTyID:
Jim Laskey563321a2006-09-06 18:34:40 +0000599 O << TAI->getData32bitsDirective();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000600 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000601 case Type::ULongTyID: case Type::LongTyID:
Jim Laskey563321a2006-09-06 18:34:40 +0000602 assert(TAI->getData64bitsDirective() &&
603 "Target cannot handle 64-bit constant exprs!");
604 O << TAI->getData64bitsDirective();
Chris Lattner660538c2005-08-08 04:26:32 +0000605 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000606 case Type::FloatTyID: case Type::DoubleTyID:
607 assert (0 && "Should have already output floating point constant.");
608 default:
609 assert (0 && "Can't handle printing this type of thing");
610 break;
611 }
Chris Lattner25045bd2005-11-21 07:51:36 +0000612 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000613 O << "\n";
614}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000615
616/// printInlineAsm - This method formats and prints the specified machine
617/// instruction that is an inline asm.
618void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000619 unsigned NumOperands = MI->getNumOperands();
620
621 // Count the number of register definitions.
622 unsigned NumDefs = 0;
Chris Lattner67942f52006-09-05 20:02:51 +0000623 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
624 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000625 assert(NumDefs != NumOperands-1 && "No asm string?");
626
627 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +0000628
629 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000630 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +0000631
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000632 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
633 if (AsmStr[0] == 0) {
634 O << "\n"; // Tab already printed, avoid double indenting next instr.
635 return;
636 }
637
Jim Laskey563321a2006-09-06 18:34:40 +0000638 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000639
Chris Lattner66099132006-02-01 22:41:11 +0000640 // The variant of the current asmprinter: FIXME: change.
641 int AsmPrinterVariant = 0;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000642
Chris Lattner66099132006-02-01 22:41:11 +0000643 int CurVariant = -1; // The number of the {.|.|.} region we are in.
644 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +0000645
Chris Lattner66099132006-02-01 22:41:11 +0000646 while (*LastEmitted) {
647 switch (*LastEmitted) {
648 default: {
649 // Not a special case, emit the string section literally.
650 const char *LiteralEnd = LastEmitted+1;
651 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +0000652 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +0000653 ++LiteralEnd;
654 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
655 O.write(LastEmitted, LiteralEnd-LastEmitted);
656 LastEmitted = LiteralEnd;
657 break;
658 }
Chris Lattner1c059972006-05-05 21:47:05 +0000659 case '\n':
660 ++LastEmitted; // Consume newline character.
661 O << "\n\t"; // Indent code with newline.
662 break;
Chris Lattner66099132006-02-01 22:41:11 +0000663 case '$': {
664 ++LastEmitted; // Consume '$' character.
665 if (*LastEmitted == '$') { // $$ -> $
666 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
667 O << '$';
668 ++LastEmitted; // Consume second '$' character.
669 break;
670 }
671
672 bool HasCurlyBraces = false;
673 if (*LastEmitted == '{') { // ${variable}
674 ++LastEmitted; // Consume '{' character.
675 HasCurlyBraces = true;
676 }
677
678 const char *IDStart = LastEmitted;
679 char *IDEnd;
680 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
681 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
682 std::cerr << "Bad $ operand number in inline asm string: '"
683 << AsmStr << "'\n";
684 exit(1);
685 }
686 LastEmitted = IDEnd;
687
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000688 char Modifier[2] = { 0, 0 };
689
Chris Lattner66099132006-02-01 22:41:11 +0000690 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000691 // If we have curly braces, check for a modifier character. This
692 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
693 if (*LastEmitted == ':') {
694 ++LastEmitted; // Consume ':' character.
695 if (*LastEmitted == 0) {
696 std::cerr << "Bad ${:} expression in inline asm string: '"
697 << AsmStr << "'\n";
698 exit(1);
699 }
700
701 Modifier[0] = *LastEmitted;
702 ++LastEmitted; // Consume modifier character.
703 }
704
Chris Lattner66099132006-02-01 22:41:11 +0000705 if (*LastEmitted != '}') {
706 std::cerr << "Bad ${} expression in inline asm string: '"
707 << AsmStr << "'\n";
708 exit(1);
709 }
710 ++LastEmitted; // Consume '}' character.
711 }
712
713 if ((unsigned)Val >= NumOperands-1) {
714 std::cerr << "Invalid $ operand number in inline asm string: '"
715 << AsmStr << "'\n";
716 exit(1);
717 }
718
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000719 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +0000720 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000721 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
722 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000723
724 bool Error = false;
725
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000726 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000727 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000728 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000729 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
730 OpNo += (OpFlags >> 3) + 1;
731 }
Chris Lattnerdd260332006-02-24 20:21:58 +0000732
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000733 if (OpNo >= MI->getNumOperands()) {
734 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +0000735 } else {
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000736 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
737 ++OpNo; // Skip over the ID number.
738
739 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
740 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
741 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
742 Modifier[0] ? Modifier : 0);
743 } else {
744 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
745 Modifier[0] ? Modifier : 0);
746 }
Chris Lattnerdd260332006-02-24 20:21:58 +0000747 }
748 if (Error) {
Chris Lattner66099132006-02-01 22:41:11 +0000749 std::cerr << "Invalid operand found in inline asm: '"
750 << AsmStr << "'\n";
751 MI->dump();
752 exit(1);
753 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000754 }
Chris Lattner66099132006-02-01 22:41:11 +0000755 break;
756 }
757 case '{':
758 ++LastEmitted; // Consume '{' character.
759 if (CurVariant != -1) {
760 std::cerr << "Nested variants found in inline asm string: '"
761 << AsmStr << "'\n";
762 exit(1);
763 }
764 CurVariant = 0; // We're in the first variant now.
765 break;
766 case '|':
767 ++LastEmitted; // consume '|' character.
768 if (CurVariant == -1) {
769 std::cerr << "Found '|' character outside of variant in inline asm "
770 << "string: '" << AsmStr << "'\n";
771 exit(1);
772 }
773 ++CurVariant; // We're in the next variant.
774 break;
775 case '}':
776 ++LastEmitted; // consume '}' character.
777 if (CurVariant == -1) {
778 std::cerr << "Found '}' character outside of variant in inline asm "
779 << "string: '" << AsmStr << "'\n";
780 exit(1);
781 }
782 CurVariant = -1;
783 break;
784 }
785 }
Jim Laskey563321a2006-09-06 18:34:40 +0000786 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattner66099132006-02-01 22:41:11 +0000787}
788
789/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
790/// instruction, using the specified assembler variant. Targets should
791/// overried this to format as appropriate.
792bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000793 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +0000794 // Target doesn't support this yet!
795 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +0000796}
Chris Lattnerdd260332006-02-24 20:21:58 +0000797
798bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
799 unsigned AsmVariant,
800 const char *ExtraCode) {
801 // Target doesn't support this yet!
802 return true;
803}
Nate Begeman37efe672006-04-22 18:53:45 +0000804
805/// printBasicBlockLabel - This method prints the label for the specified
806/// MachineBasicBlock
Nate Begemancdf38c42006-05-02 05:37:32 +0000807void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
808 bool printColon,
809 bool printComment) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000810 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman9d51eeb2006-05-02 17:36:46 +0000811 << MBB->getNumber();
Nate Begemancdf38c42006-05-02 05:37:32 +0000812 if (printColon)
813 O << ':';
814 if (printComment)
Jim Laskey563321a2006-09-06 18:34:40 +0000815 O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
Nate Begeman37efe672006-04-22 18:53:45 +0000816}
Nate Begeman52a51e382006-08-12 21:29:52 +0000817
818/// printSetLabel - This method prints a set label for the specified
819/// MachineBasicBlock
820void AsmPrinter::printSetLabel(unsigned uid,
821 const MachineBasicBlock *MBB) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000822 if (!TAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +0000823 return;
824
Jim Laskey563321a2006-09-06 18:34:40 +0000825 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
826 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman52a51e382006-08-12 21:29:52 +0000827 printBasicBlockLabel(MBB, false, false);
Jim Laskey563321a2006-09-06 18:34:40 +0000828 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman52a51e382006-08-12 21:29:52 +0000829 << '_' << uid << '\n';
830}