blob: 5af89c79c801da16b29d8d09e07ceff2b8014ff1 [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 Laskeya0f3d172006-09-07 22:06:40 +000030AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
31 const TargetAsmInfo *T)
Jim Laskey563321a2006-09-06 18:34:40 +000032: FunctionNumber(0), O(o), TM(tm), TAI(T)
33{}
Chris Lattnered138932005-12-13 06:32:10 +000034
35
Chris Lattner4632d7a2006-05-09 04:59:56 +000036/// SwitchToTextSection - Switch to the specified text section of the executable
37/// if we are not already in it!
Chris Lattnerac28fbd2005-11-21 07:06:27 +000038///
Chris Lattner4632d7a2006-05-09 04:59:56 +000039void AsmPrinter::SwitchToTextSection(const char *NewSection,
40 const GlobalValue *GV) {
Chris Lattnerac28fbd2005-11-21 07:06:27 +000041 std::string NS;
Chris Lattnera7090ae2006-05-09 05:24:50 +000042 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000043 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnera7090ae2006-05-09 05:24:50 +000044 else
45 NS = NewSection;
46
47 // If we're already in this section, we're done.
48 if (CurrentSection == NS) return;
Jeff Cohen51b776d2006-05-02 03:58:45 +000049
Chris Lattner7faec9b2006-05-09 05:33:48 +000050 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000051 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
52 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen51b776d2006-05-02 03:58:45 +000053
Chris Lattner7faec9b2006-05-09 05:33:48 +000054 CurrentSection = NS;
55
56 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000057 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattnerac28fbd2005-11-21 07:06:27 +000058}
59
Nate Begeman2f1ae882006-07-27 01:13:04 +000060/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner4632d7a2006-05-09 04:59:56 +000061/// if we are not already in it!
62///
63void AsmPrinter::SwitchToDataSection(const char *NewSection,
64 const GlobalValue *GV) {
65 std::string NS;
Chris Lattnerb81cb612006-05-09 05:23:12 +000066 if (GV && GV->hasSection())
Jim Laskey563321a2006-09-06 18:34:40 +000067 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattnerb81cb612006-05-09 05:23:12 +000068 else
69 NS = NewSection;
Chris Lattner4632d7a2006-05-09 04:59:56 +000070
Chris Lattnerb81cb612006-05-09 05:23:12 +000071 // If we're already in this section, we're done.
72 if (CurrentSection == NS) return;
73
Chris Lattner7faec9b2006-05-09 05:33:48 +000074 // Close the current section, if applicable.
Jim Laskey563321a2006-09-06 18:34:40 +000075 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
76 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner7faec9b2006-05-09 05:33:48 +000077
78 CurrentSection = NS;
Chris Lattner4632d7a2006-05-09 04:59:56 +000079
Chris Lattner7faec9b2006-05-09 05:33:48 +000080 if (!CurrentSection.empty())
Jim Laskey563321a2006-09-06 18:34:40 +000081 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner4632d7a2006-05-09 04:59:56 +000082}
83
84
Chris Lattnera80ba712004-08-16 23:15:22 +000085bool AsmPrinter::doInitialization(Module &M) {
Jim Laskey563321a2006-09-06 18:34:40 +000086 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattner2c1b1592006-01-23 23:47:53 +000087
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000088 if (!M.getModuleInlineAsm().empty())
Jim Laskey563321a2006-09-06 18:34:40 +000089 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000090 << M.getModuleInlineAsm()
Jim Laskey563321a2006-09-06 18:34:40 +000091 << "\n" << TAI->getCommentString()
92 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +000093
Chris Lattner4632d7a2006-05-09 04:59:56 +000094 SwitchToDataSection("", 0); // Reset back to no section.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000095
96 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
97 DebugInfo->AnalyzeModule(M);
98 }
99
Chris Lattnera80ba712004-08-16 23:15:22 +0000100 return false;
101}
102
103bool AsmPrinter::doFinalization(Module &M) {
104 delete Mang; Mang = 0;
105 return false;
106}
107
Chris Lattner25045bd2005-11-21 07:51:36 +0000108void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000109 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +0000110 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner77bc2282005-11-21 08:13:27 +0000111 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +0000112}
113
Chris Lattner3b4fd322005-11-21 08:25:09 +0000114/// EmitConstantPool - Print to the current output stream assembly
115/// representations of the constants in the constant pool MCP. This is
116/// used to print out constants which have been "spilled to memory" by
117/// the code generator.
118///
119void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000120 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000121 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000122
123 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
124 // in special sections.
125 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
126 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
127 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
128 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000129 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng2d2cec12006-06-29 00:26:09 +0000130 MachineConstantPoolEntry CPE = CP[i];
131 const Constant *CV = CPE.Val;
132 const Type *Ty = CV->getType();
Jim Laskey563321a2006-09-06 18:34:40 +0000133 if (TAI->getFourByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000134 TM.getTargetData()->getTypeSize(Ty) == 4)
135 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000136 else if (TAI->getEightByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000137 TM.getTargetData()->getTypeSize(Ty) == 8)
138 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng61c958e2006-09-07 18:50:20 +0000139 else if (TAI->getSixteenByteConstantSection() &&
Evan Cheng2d2cec12006-06-29 00:26:09 +0000140 TM.getTargetData()->getTypeSize(Ty) == 16)
141 SixteenByteCPs.push_back(std::make_pair(CPE, i));
142 else
143 OtherCPs.push_back(std::make_pair(CPE, i));
144 }
145
146 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskey563321a2006-09-06 18:34:40 +0000147 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
148 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
149 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
150 SixteenByteCPs);
151 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000152}
153
154void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
155 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
156 if (CP.empty()) return;
157
158 SwitchToDataSection(Section, 0);
159 EmitAlignment(Alignment);
160 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Jim Laskey563321a2006-09-06 18:34:40 +0000161 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
162 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Cheng2d2cec12006-06-29 00:26:09 +0000163 WriteTypeSymbolic(O, CP[i].first.Val->getType(), 0) << '\n';
164 EmitGlobalConstant(CP[i].first.Val);
Chris Lattner3029f922006-02-09 04:46:04 +0000165 if (i != e-1) {
Evan Cheng2d2cec12006-06-29 00:26:09 +0000166 unsigned EntSize =
167 TM.getTargetData()->getTypeSize(CP[i].first.Val->getType());
168 unsigned ValEnd = CP[i].first.Offset + EntSize;
Chris Lattner3029f922006-02-09 04:46:04 +0000169 // Emit inter-object padding for alignment.
Evan Cheng2d2cec12006-06-29 00:26:09 +0000170 EmitZeros(CP[i+1].first.Offset-ValEnd);
Chris Lattner3029f922006-02-09 04:46:04 +0000171 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000172 }
173}
174
Nate Begeman37efe672006-04-22 18:53:45 +0000175/// EmitJumpTableInfo - Print assembly representations of the jump tables used
176/// by the current function to the current output stream.
177///
178void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
179 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
180 if (JT.empty()) return;
Owen Andersona69571c2006-05-03 01:29:57 +0000181 const TargetData *TD = TM.getTargetData();
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000182
183 // JTEntryDirective is a string to print sizeof(ptr) for non-PIC jump tables,
184 // and 32 bits for PIC since PIC jump table entries are differences, not
185 // pointers to blocks.
Jim Laskey563321a2006-09-06 18:34:40 +0000186 const char *JTEntryDirective = TAI->getData32bitsDirective();
Nate Begeman37efe672006-04-22 18:53:45 +0000187
Nate Begeman2f1ae882006-07-27 01:13:04 +0000188 // Pick the directive to use to print the jump table entries, and switch to
189 // the appropriate section.
190 if (TM.getRelocationModel() == Reloc::PIC_) {
Jim Laskey563321a2006-09-06 18:34:40 +0000191 SwitchToTextSection(TAI->getJumpTableTextSection(), 0);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000192 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000193 SwitchToDataSection(TAI->getJumpTableDataSection(), 0);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000194 if (TD->getPointerSize() == 8)
Jim Laskey563321a2006-09-06 18:34:40 +0000195 JTEntryDirective = TAI->getData64bitsDirective();
Nate Begeman2f1ae882006-07-27 01:13:04 +0000196 }
Owen Andersona69571c2006-05-03 01:29:57 +0000197 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000198
Nate Begeman37efe672006-04-22 18:53:45 +0000199 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000200 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
201
202 // For PIC codegen, if possible we want to use the SetDirective to reduce
203 // the number of relocations the assembler will generate for the jump table.
204 // Set directives are all printed before the jump table itself.
205 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskey563321a2006-09-06 18:34:40 +0000206 if (TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_)
Nate Begeman52a51e382006-08-12 21:29:52 +0000207 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
208 if (EmittedSets.insert(JTBBs[ii]).second)
209 printSetLabel(i, JTBBs[ii]);
210
Jim Laskey563321a2006-09-06 18:34:40 +0000211 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
212 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000213
Nate Begeman37efe672006-04-22 18:53:45 +0000214 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000215 O << JTEntryDirective << ' ';
Nate Begeman52a51e382006-08-12 21:29:52 +0000216 // If we have emitted set directives for the jump table entries, print
217 // them rather than the entries themselves. If we're emitting PIC, then
218 // emit the table entries as differences between two text section labels.
219 // If we're emitting non-PIC code, then emit the entries as direct
220 // references to the target basic blocks.
221 if (!EmittedSets.empty()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000222 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
223 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Nate Begeman52a51e382006-08-12 21:29:52 +0000224 } else if (TM.getRelocationModel() == Reloc::PIC_) {
225 printBasicBlockLabel(JTBBs[ii], false, false);
Jim Laskey563321a2006-09-06 18:34:40 +0000226 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
227 << getFunctionNumber() << '_' << i;
Nate Begeman52a51e382006-08-12 21:29:52 +0000228 } else {
229 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000230 }
Nate Begeman37efe672006-04-22 18:53:45 +0000231 O << '\n';
232 }
233 }
234}
235
Chris Lattnered138932005-12-13 06:32:10 +0000236/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
237/// special global used by LLVM. If so, emit it and return true, otherwise
238/// do nothing and return false.
239bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey78098112006-03-07 22:00:35 +0000240 // Ignore debug and non-emitted data.
241 if (GV->getSection() == "llvm.metadata") return true;
242
243 if (!GV->hasAppendingLinkage()) return false;
244
245 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000246
247 if (GV->getName() == "llvm.used")
248 return true; // No need to emit this at all.
249
Chris Lattner5166b822006-01-12 19:17:23 +0000250 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000251 SwitchToDataSection(TAI->getStaticCtorsSection(), 0);
Chris Lattnered138932005-12-13 06:32:10 +0000252 EmitAlignment(2, 0);
253 EmitXXStructorList(GV->getInitializer());
254 return true;
255 }
256
Chris Lattner5166b822006-01-12 19:17:23 +0000257 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000258 SwitchToDataSection(TAI->getStaticDtorsSection(), 0);
Chris Lattnered138932005-12-13 06:32:10 +0000259 EmitAlignment(2, 0);
260 EmitXXStructorList(GV->getInitializer());
261 return true;
262 }
263
264 return false;
265}
266
267/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
268/// function pointers, ignoring the init priority.
269void AsmPrinter::EmitXXStructorList(Constant *List) {
270 // Should be an array of '{ int, void ()* }' structs. The first value is the
271 // init priority, which we ignore.
272 if (!isa<ConstantArray>(List)) return;
273 ConstantArray *InitList = cast<ConstantArray>(List);
274 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
275 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
276 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000277
278 if (CS->getOperand(1)->isNullValue())
279 return; // Found a null terminator, exit printing.
280 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000281 EmitGlobalConstant(CS->getOperand(1));
282 }
283}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000284
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000285/// getPreferredAlignmentLog - Return the preferred alignment of the
286/// specified global, returned in log form. This includes an explicitly
287/// requested alignment (if the global has one).
288unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Jim Laskey35f8c202006-06-15 13:10:58 +0000289 const Type *ElemType = GV->getType()->getElementType();
290 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(ElemType);
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000291 if (GV->getAlignment() > (1U << Alignment))
292 Alignment = Log2_32(GV->getAlignment());
293
Chris Lattner519ea2a2006-02-05 01:46:49 +0000294 if (GV->hasInitializer()) {
Jim Laskeyd5a932b2006-06-15 19:37:14 +0000295 // Always round up alignment of global doubles to 8 bytes.
296 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
297 Alignment = 3;
Chris Lattner519ea2a2006-02-05 01:46:49 +0000298 if (Alignment < 4) {
299 // If the global is not external, see if it is large. If so, give it a
300 // larger alignment.
Jim Laskey35f8c202006-06-15 13:10:58 +0000301 if (TM.getTargetData()->getTypeSize(ElemType) > 128)
Chris Lattner519ea2a2006-02-05 01:46:49 +0000302 Alignment = 4; // 16-byte alignment.
303 }
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000304 }
305 return Alignment;
306}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000307
Chris Lattner25045bd2005-11-21 07:51:36 +0000308// EmitAlignment - Emit an alignment directive to the specified power of two.
309void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnera1ab72d2005-11-14 19:00:06 +0000310 if (GV && GV->getAlignment())
311 NumBits = Log2_32(GV->getAlignment());
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000312 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskey563321a2006-09-06 18:34:40 +0000313 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
314 O << TAI->getAlignDirective() << NumBits << "\n";
Chris Lattnerbfddc202004-08-17 19:14:29 +0000315}
316
Chris Lattner25045bd2005-11-21 07:51:36 +0000317/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000318///
Chris Lattner25045bd2005-11-21 07:51:36 +0000319void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000320 if (NumZeros) {
Jim Laskey563321a2006-09-06 18:34:40 +0000321 if (TAI->getZeroDirective()) {
322 O << TAI->getZeroDirective() << NumZeros;
323 if (TAI->getZeroDirectiveSuffix())
324 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000325 O << "\n";
326 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000327 for (; NumZeros; --NumZeros)
Jim Laskey563321a2006-09-06 18:34:40 +0000328 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000329 }
330 }
331}
332
Chris Lattnera80ba712004-08-16 23:15:22 +0000333// Print out the specified constant, without a storage class. Only the
334// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000335void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000336 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000337 O << "0";
338 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
339 assert(CB == ConstantBool::True);
340 O << "1";
341 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
342 if (((CI->getValue() << 32) >> 32) == CI->getValue())
343 O << CI->getValue();
344 else
Duraid Madina45606572005-05-15 13:05:48 +0000345 O << (uint64_t)CI->getValue();
Chris Lattnera80ba712004-08-16 23:15:22 +0000346 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
347 O << CI->getValue();
Chris Lattner450de392005-11-10 18:36:17 +0000348 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000349 // This is a constant address for a global variable or function. Use the
350 // name of the variable or function as the address value, possibly
351 // decorating it with GlobalVarAddrPrefix/Suffix or
352 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskey563321a2006-09-06 18:34:40 +0000353 if (isa<Function>(GV)) {
354 O << TAI->getFunctionAddrPrefix()
355 << Mang->getValueName(GV)
356 << TAI->getFunctionAddrSuffix();
357 } else {
358 O << TAI->getGlobalVarAddrPrefix()
359 << Mang->getValueName(GV)
360 << TAI->getGlobalVarAddrSuffix();
361 }
Duraid Madina855a5192005-04-02 12:21:51 +0000362 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000363 const TargetData *TD = TM.getTargetData();
Chris Lattnera80ba712004-08-16 23:15:22 +0000364 switch(CE->getOpcode()) {
365 case Instruction::GetElementPtr: {
366 // generate a symbolic expression for the byte address
367 const Constant *ptrVal = CE->getOperand(0);
368 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Andersona69571c2006-05-03 01:29:57 +0000369 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner27e19212005-02-14 21:40:26 +0000370 if (Offset)
371 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000372 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000373 if (Offset > 0)
374 O << ") + " << Offset;
375 else if (Offset < 0)
376 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000377 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000378 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000379 }
380 break;
381 }
382 case Instruction::Cast: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000383 // Support only foldable casts to/from pointers that can be eliminated by
384 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000385 Constant *Op = CE->getOperand(0);
386 const Type *OpTy = Op->getType(), *Ty = CE->getType();
387
Chris Lattner4a6bd332006-07-29 01:57:19 +0000388 // Handle casts to pointers by changing them into casts to the appropriate
389 // integer type. This promotes constant folding and simplifies this code.
390 if (isa<PointerType>(Ty)) {
391 const Type *IntPtrTy = TD->getIntPtrType();
392 Op = ConstantExpr::getCast(Op, IntPtrTy);
393 return EmitConstantValueOnly(Op);
394 }
395
396 // We know the dest type is not a pointer. Is the src value a pointer or
397 // integral?
398 if (isa<PointerType>(OpTy) || OpTy->isIntegral()) {
399 // We can emit the pointer value into this slot if the slot is an
400 // integer slot greater or equal to the size of the pointer.
401 if (Ty->isIntegral() && TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
402 return EmitConstantValueOnly(Op);
403 }
404
405 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000406 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000407 break;
408 }
409 case Instruction::Add:
410 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000411 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattnera80ba712004-08-16 23:15:22 +0000412 O << ") + (";
Chris Lattner25045bd2005-11-21 07:51:36 +0000413 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000414 O << ")";
415 break;
416 default:
417 assert(0 && "Unsupported operator!");
418 }
419 } else {
420 assert(0 && "Unknown constant value!");
421 }
422}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000423
424/// toOctal - Convert the low order bits of X into an octal digit.
425///
426static inline char toOctal(int X) {
427 return (X&7)+'0';
428}
429
Chris Lattner2980cef2005-11-10 18:06:33 +0000430/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000431/// the predicate isString is true.
432///
Chris Lattner2980cef2005-11-10 18:06:33 +0000433static void printAsCString(std::ostream &O, const ConstantArray *CVA,
434 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000435 assert(CVA->isString() && "Array is not string compatible!");
436
437 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000438 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000439 unsigned char C =
Chris Lattnerdea18b62005-01-08 19:59:10 +0000440 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000441
442 if (C == '"') {
443 O << "\\\"";
444 } else if (C == '\\') {
445 O << "\\\\";
446 } else if (isprint(C)) {
447 O << C;
448 } else {
449 switch(C) {
450 case '\b': O << "\\b"; break;
451 case '\f': O << "\\f"; break;
452 case '\n': O << "\\n"; break;
453 case '\r': O << "\\r"; break;
454 case '\t': O << "\\t"; break;
455 default:
456 O << '\\';
457 O << toOctal(C >> 6);
458 O << toOctal(C >> 3);
459 O << toOctal(C >> 0);
460 break;
461 }
462 }
463 }
464 O << "\"";
465}
466
Jeff Cohenc884db42006-05-02 01:16:28 +0000467/// EmitString - Emit a zero-byte-terminated string constant.
468///
469void AsmPrinter::EmitString(const ConstantArray *CVA) const {
470 unsigned NumElts = CVA->getNumOperands();
Jim Laskey563321a2006-09-06 18:34:40 +0000471 if (TAI->getAscizDirective() && NumElts &&
Jeff Cohenc884db42006-05-02 01:16:28 +0000472 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
Jim Laskey563321a2006-09-06 18:34:40 +0000473 O << TAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000474 printAsCString(O, CVA, NumElts-1);
475 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000476 O << TAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000477 printAsCString(O, CVA, NumElts);
478 }
479 O << "\n";
480}
481
Chris Lattner25045bd2005-11-21 07:51:36 +0000482/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000483///
Chris Lattner25045bd2005-11-21 07:51:36 +0000484void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Andersona69571c2006-05-03 01:29:57 +0000485 const TargetData *TD = TM.getTargetData();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000486
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000487 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000488 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000489 return;
490 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
491 if (CVA->isString()) {
Jeff Cohenc884db42006-05-02 01:16:28 +0000492 EmitString(CVA);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000493 } else { // Not a string. Print the values in successive locations
494 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000495 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000496 }
497 return;
498 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
499 // Print the fields in successive locations. Pad to align if needed!
Owen Andersona69571c2006-05-03 01:29:57 +0000500 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000501 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000502 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
503 const Constant* field = CVS->getOperand(i);
504
505 // Check if padding is needed and insert one or more 0s.
Owen Andersona69571c2006-05-03 01:29:57 +0000506 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000507 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner1b7e2352004-08-17 06:36:49 +0000508 : cvsLayout->MemberOffsets[i+1])
509 - cvsLayout->MemberOffsets[i]) - fieldSize;
510 sizeSoFar += fieldSize + padSize;
511
512 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000513 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000514
515 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000516 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000517 }
518 assert(sizeSoFar == cvsLayout->StructSize &&
519 "Layout of constant struct may be incorrect!");
520 return;
521 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
522 // FP Constants are printed as integer constants to avoid losing
523 // precision...
524 double Val = CFP->getValue();
525 if (CFP->getType() == Type::DoubleTy) {
Jim Laskey563321a2006-09-06 18:34:40 +0000526 if (TAI->getData64bitsDirective())
527 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
528 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000529 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000530 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
531 << "\t" << TAI->getCommentString()
532 << " double most significant word " << Val << "\n";
533 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
534 << "\t" << TAI->getCommentString()
535 << " double least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000536 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000537 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
538 << "\t" << TAI->getCommentString()
539 << " double least significant word " << Val << "\n";
540 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
541 << "\t" << TAI->getCommentString()
542 << " double most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000543 }
544 return;
545 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000546 O << TAI->getData32bitsDirective() << FloatToBits(Val)
547 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000548 return;
549 }
550 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
551 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
552 uint64_t Val = CI->getRawValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000553
Jim Laskey563321a2006-09-06 18:34:40 +0000554 if (TAI->getData64bitsDirective())
555 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000556 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000557 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
558 << "\t" << TAI->getCommentString()
559 << " Double-word most significant word " << Val << "\n";
560 O << TAI->getData32bitsDirective() << unsigned(Val)
561 << "\t" << TAI->getCommentString()
562 << " Double-word least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000563 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000564 O << TAI->getData32bitsDirective() << unsigned(Val)
565 << "\t" << TAI->getCommentString()
566 << " Double-word least significant word " << Val << "\n";
567 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
568 << "\t" << TAI->getCommentString()
569 << " Double-word most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000570 }
571 return;
572 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000573 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
574 const PackedType *PTy = CP->getType();
575
576 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
577 EmitGlobalConstant(CP->getOperand(I));
578
579 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000580 }
581
582 const Type *type = CV->getType();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000583 switch (type->getTypeID()) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000584 case Type::BoolTyID:
Chris Lattner1b7e2352004-08-17 06:36:49 +0000585 case Type::UByteTyID: case Type::SByteTyID:
Jim Laskey563321a2006-09-06 18:34:40 +0000586 O << TAI->getData8bitsDirective();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000587 break;
588 case Type::UShortTyID: case Type::ShortTyID:
Jim Laskey563321a2006-09-06 18:34:40 +0000589 O << TAI->getData16bitsDirective();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000590 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000591 case Type::PointerTyID:
Owen Andersona69571c2006-05-03 01:29:57 +0000592 if (TD->getPointerSize() == 8) {
Jim Laskey563321a2006-09-06 18:34:40 +0000593 assert(TAI->getData64bitsDirective() &&
Evan Chenga9767f62006-06-15 08:10:56 +0000594 "Target cannot handle 64-bit pointer exprs!");
Jim Laskey563321a2006-09-06 18:34:40 +0000595 O << TAI->getData64bitsDirective();
Andrew Lenharth571c9c32005-02-04 13:47:16 +0000596 break;
597 }
598 //Fall through for pointer size == int size
Chris Lattner1b7e2352004-08-17 06:36:49 +0000599 case Type::UIntTyID: case Type::IntTyID:
Jim Laskey563321a2006-09-06 18:34:40 +0000600 O << TAI->getData32bitsDirective();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000601 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000602 case Type::ULongTyID: case Type::LongTyID:
Jim Laskey563321a2006-09-06 18:34:40 +0000603 assert(TAI->getData64bitsDirective() &&
604 "Target cannot handle 64-bit constant exprs!");
605 O << TAI->getData64bitsDirective();
Chris Lattner660538c2005-08-08 04:26:32 +0000606 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000607 case Type::FloatTyID: case Type::DoubleTyID:
608 assert (0 && "Should have already output floating point constant.");
609 default:
610 assert (0 && "Can't handle printing this type of thing");
611 break;
612 }
Chris Lattner25045bd2005-11-21 07:51:36 +0000613 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000614 O << "\n";
615}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000616
617/// printInlineAsm - This method formats and prints the specified machine
618/// instruction that is an inline asm.
619void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000620 unsigned NumOperands = MI->getNumOperands();
621
622 // Count the number of register definitions.
623 unsigned NumDefs = 0;
Chris Lattner67942f52006-09-05 20:02:51 +0000624 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
625 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000626 assert(NumDefs != NumOperands-1 && "No asm string?");
627
628 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +0000629
630 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000631 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +0000632
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000633 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
634 if (AsmStr[0] == 0) {
635 O << "\n"; // Tab already printed, avoid double indenting next instr.
636 return;
637 }
638
Jim Laskey563321a2006-09-06 18:34:40 +0000639 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000640
Chris Lattner66099132006-02-01 22:41:11 +0000641 // The variant of the current asmprinter: FIXME: change.
642 int AsmPrinterVariant = 0;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000643
Chris Lattner66099132006-02-01 22:41:11 +0000644 int CurVariant = -1; // The number of the {.|.|.} region we are in.
645 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +0000646
Chris Lattner66099132006-02-01 22:41:11 +0000647 while (*LastEmitted) {
648 switch (*LastEmitted) {
649 default: {
650 // Not a special case, emit the string section literally.
651 const char *LiteralEnd = LastEmitted+1;
652 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +0000653 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +0000654 ++LiteralEnd;
655 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
656 O.write(LastEmitted, LiteralEnd-LastEmitted);
657 LastEmitted = LiteralEnd;
658 break;
659 }
Chris Lattner1c059972006-05-05 21:47:05 +0000660 case '\n':
661 ++LastEmitted; // Consume newline character.
662 O << "\n\t"; // Indent code with newline.
663 break;
Chris Lattner66099132006-02-01 22:41:11 +0000664 case '$': {
665 ++LastEmitted; // Consume '$' character.
666 if (*LastEmitted == '$') { // $$ -> $
667 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
668 O << '$';
669 ++LastEmitted; // Consume second '$' character.
670 break;
671 }
672
673 bool HasCurlyBraces = false;
674 if (*LastEmitted == '{') { // ${variable}
675 ++LastEmitted; // Consume '{' character.
676 HasCurlyBraces = true;
677 }
678
679 const char *IDStart = LastEmitted;
680 char *IDEnd;
681 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
682 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
683 std::cerr << "Bad $ operand number in inline asm string: '"
684 << AsmStr << "'\n";
685 exit(1);
686 }
687 LastEmitted = IDEnd;
688
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000689 char Modifier[2] = { 0, 0 };
690
Chris Lattner66099132006-02-01 22:41:11 +0000691 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000692 // If we have curly braces, check for a modifier character. This
693 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
694 if (*LastEmitted == ':') {
695 ++LastEmitted; // Consume ':' character.
696 if (*LastEmitted == 0) {
697 std::cerr << "Bad ${:} expression in inline asm string: '"
698 << AsmStr << "'\n";
699 exit(1);
700 }
701
702 Modifier[0] = *LastEmitted;
703 ++LastEmitted; // Consume modifier character.
704 }
705
Chris Lattner66099132006-02-01 22:41:11 +0000706 if (*LastEmitted != '}') {
707 std::cerr << "Bad ${} expression in inline asm string: '"
708 << AsmStr << "'\n";
709 exit(1);
710 }
711 ++LastEmitted; // Consume '}' character.
712 }
713
714 if ((unsigned)Val >= NumOperands-1) {
715 std::cerr << "Invalid $ operand number in inline asm string: '"
716 << AsmStr << "'\n";
717 exit(1);
718 }
719
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000720 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +0000721 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000722 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
723 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000724
725 bool Error = false;
726
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000727 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000728 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000729 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000730 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
731 OpNo += (OpFlags >> 3) + 1;
732 }
Chris Lattnerdd260332006-02-24 20:21:58 +0000733
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000734 if (OpNo >= MI->getNumOperands()) {
735 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +0000736 } else {
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000737 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
738 ++OpNo; // Skip over the ID number.
739
740 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
741 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
742 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
743 Modifier[0] ? Modifier : 0);
744 } else {
745 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
746 Modifier[0] ? Modifier : 0);
747 }
Chris Lattnerdd260332006-02-24 20:21:58 +0000748 }
749 if (Error) {
Chris Lattner66099132006-02-01 22:41:11 +0000750 std::cerr << "Invalid operand found in inline asm: '"
751 << AsmStr << "'\n";
752 MI->dump();
753 exit(1);
754 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000755 }
Chris Lattner66099132006-02-01 22:41:11 +0000756 break;
757 }
758 case '{':
759 ++LastEmitted; // Consume '{' character.
760 if (CurVariant != -1) {
761 std::cerr << "Nested variants found in inline asm string: '"
762 << AsmStr << "'\n";
763 exit(1);
764 }
765 CurVariant = 0; // We're in the first variant now.
766 break;
767 case '|':
768 ++LastEmitted; // consume '|' character.
769 if (CurVariant == -1) {
770 std::cerr << "Found '|' character outside of variant in inline asm "
771 << "string: '" << AsmStr << "'\n";
772 exit(1);
773 }
774 ++CurVariant; // We're in the next variant.
775 break;
776 case '}':
777 ++LastEmitted; // consume '}' character.
778 if (CurVariant == -1) {
779 std::cerr << "Found '}' character outside of variant in inline asm "
780 << "string: '" << AsmStr << "'\n";
781 exit(1);
782 }
783 CurVariant = -1;
784 break;
785 }
786 }
Jim Laskey563321a2006-09-06 18:34:40 +0000787 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattner66099132006-02-01 22:41:11 +0000788}
789
790/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
791/// instruction, using the specified assembler variant. Targets should
792/// overried this to format as appropriate.
793bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000794 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +0000795 // Target doesn't support this yet!
796 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +0000797}
Chris Lattnerdd260332006-02-24 20:21:58 +0000798
799bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
800 unsigned AsmVariant,
801 const char *ExtraCode) {
802 // Target doesn't support this yet!
803 return true;
804}
Nate Begeman37efe672006-04-22 18:53:45 +0000805
806/// printBasicBlockLabel - This method prints the label for the specified
807/// MachineBasicBlock
Nate Begemancdf38c42006-05-02 05:37:32 +0000808void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
809 bool printColon,
810 bool printComment) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000811 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman9d51eeb2006-05-02 17:36:46 +0000812 << MBB->getNumber();
Nate Begemancdf38c42006-05-02 05:37:32 +0000813 if (printColon)
814 O << ':';
815 if (printComment)
Jim Laskey563321a2006-09-06 18:34:40 +0000816 O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
Nate Begeman37efe672006-04-22 18:53:45 +0000817}
Nate Begeman52a51e382006-08-12 21:29:52 +0000818
819/// printSetLabel - This method prints a set label for the specified
820/// MachineBasicBlock
821void AsmPrinter::printSetLabel(unsigned uid,
822 const MachineBasicBlock *MBB) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000823 if (!TAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +0000824 return;
825
Jim Laskey563321a2006-09-06 18:34:40 +0000826 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
827 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman52a51e382006-08-12 21:29:52 +0000828 printBasicBlockLabel(MBB, false, false);
Jim Laskey563321a2006-09-06 18:34:40 +0000829 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman52a51e382006-08-12 21:29:52 +0000830 << '_' << uid << '\n';
831}