blob: ca9b50bb81229ebe917bcb245d602ac206dbded9 [file] [log] [blame]
Chris Lattner6a8e0f52004-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 Cheng993e9cf2006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng38d5e762006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman41b1cdc2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000020#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000021#include "llvm/Support/Mangler.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000022#include "llvm/Support/MathExtras.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000023#include "llvm/Target/TargetData.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000024#include "llvm/Target/TargetMachine.h"
Duraid Madina26b037e2005-12-28 06:29:02 +000025#include <iostream>
Chris Lattneraa23fa92006-02-01 22:41:11 +000026#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000027using namespace llvm;
28
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000029AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm)
30: FunctionNumber(0), O(o), TM(tm),
31 CommentString("#"),
32 GlobalPrefix(""),
33 PrivateGlobalPrefix("."),
34 GlobalVarAddrPrefix(""),
35 GlobalVarAddrSuffix(""),
36 FunctionAddrPrefix(""),
37 FunctionAddrSuffix(""),
Chris Lattnera633c3132006-05-05 21:47:05 +000038 InlineAsmStart("#APP"),
39 InlineAsmEnd("#NO_APP"),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000040 ZeroDirective("\t.zero\t"),
Jeff Cohenf34ddb12006-05-02 03:46:13 +000041 ZeroDirectiveSuffix(0),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000042 AsciiDirective("\t.ascii\t"),
43 AscizDirective("\t.asciz\t"),
44 Data8bitsDirective("\t.byte\t"),
45 Data16bitsDirective("\t.short\t"),
46 Data32bitsDirective("\t.long\t"),
47 Data64bitsDirective("\t.quad\t"),
48 AlignDirective("\t.align\t"),
49 AlignmentIsInBytes(true),
50 SwitchToSectionDirective("\t.section\t"),
Chris Lattner4ebc6a22006-05-09 05:33:48 +000051 TextSectionStartSuffix(""),
52 DataSectionStartSuffix(""),
53 SectionEndDirectiveSuffix(0),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000054 ConstantPoolSection("\t.section .rodata\n"),
Nate Begeman78756502006-07-27 01:13:04 +000055 JumpTableDataSection("\t.section .rodata\n"),
56 JumpTableTextSection("\t.text\n"),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000057 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
58 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
Evan Chengec1d60b2006-06-29 00:26:09 +000059 FourByteConstantSection(0),
60 EightByteConstantSection(0),
61 SixteenByteConstantSection(0),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000062 LCOMMDirective(0),
63 COMMDirective("\t.comm\t"),
64 COMMDirectiveTakesAlignment(true),
65 HasDotTypeDotSizeDirective(true) {
66}
67
68
Chris Lattner8488ba22006-05-09 04:59:56 +000069/// SwitchToTextSection - Switch to the specified text section of the executable
70/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000071///
Chris Lattner8488ba22006-05-09 04:59:56 +000072void AsmPrinter::SwitchToTextSection(const char *NewSection,
73 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000074 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000075 if (GV && GV->hasSection())
Chris Lattnerf8017922006-05-09 16:41:59 +000076 NS = SwitchToSectionDirective + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000077 else
78 NS = NewSection;
79
80 // If we're already in this section, we're done.
81 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000082
Chris Lattner4ebc6a22006-05-09 05:33:48 +000083 // Close the current section, if applicable.
84 if (SectionEndDirectiveSuffix && !CurrentSection.empty())
85 O << CurrentSection << SectionEndDirectiveSuffix << "\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000086
Chris Lattner4ebc6a22006-05-09 05:33:48 +000087 CurrentSection = NS;
88
89 if (!CurrentSection.empty())
90 O << CurrentSection << TextSectionStartSuffix << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000091}
92
Nate Begeman78756502006-07-27 01:13:04 +000093/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000094/// if we are not already in it!
95///
96void AsmPrinter::SwitchToDataSection(const char *NewSection,
97 const GlobalValue *GV) {
98 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000099 if (GV && GV->hasSection())
100 NS = SwitchToSectionDirective + GV->getSection();
101 else
102 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +0000103
Chris Lattner6341df82006-05-09 05:23:12 +0000104 // If we're already in this section, we're done.
105 if (CurrentSection == NS) return;
106
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000107 // Close the current section, if applicable.
108 if (SectionEndDirectiveSuffix && !CurrentSection.empty())
109 O << CurrentSection << SectionEndDirectiveSuffix << "\n";
110
111 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +0000112
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000113 if (!CurrentSection.empty())
114 O << CurrentSection << DataSectionStartSuffix << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +0000115}
116
117
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000118bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner6d42cbd2004-08-17 06:06:19 +0000119 Mang = new Mangler(M, GlobalPrefix);
Chris Lattnere3a79262006-01-23 23:47:53 +0000120
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000121 if (!M.getModuleInlineAsm().empty())
122 O << CommentString << " Start of file scope inline assembly\n"
123 << M.getModuleInlineAsm()
124 << "\n" << CommentString << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000125
Chris Lattner8488ba22006-05-09 04:59:56 +0000126 SwitchToDataSection("", 0); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000127
128 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
129 DebugInfo->AnalyzeModule(M);
130 }
131
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000132 return false;
133}
134
135bool AsmPrinter::doFinalization(Module &M) {
136 delete Mang; Mang = 0;
137 return false;
138}
139
Chris Lattnerbb644e32005-11-21 07:51:36 +0000140void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000141 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000142 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000143 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000144}
145
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000146/// EmitConstantPool - Print to the current output stream assembly
147/// representations of the constants in the constant pool MCP. This is
148/// used to print out constants which have been "spilled to memory" by
149/// the code generator.
150///
151void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000152 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000153 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000154
155 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
156 // in special sections.
157 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
158 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
159 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
160 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000161 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000162 MachineConstantPoolEntry CPE = CP[i];
163 const Constant *CV = CPE.Val;
164 const Type *Ty = CV->getType();
165 if (FourByteConstantSection &&
166 TM.getTargetData()->getTypeSize(Ty) == 4)
167 FourByteCPs.push_back(std::make_pair(CPE, i));
168 else if (EightByteConstantSection &&
169 TM.getTargetData()->getTypeSize(Ty) == 8)
170 EightByteCPs.push_back(std::make_pair(CPE, i));
171 else if (SixteenByteConstantSection &&
172 TM.getTargetData()->getTypeSize(Ty) == 16)
173 SixteenByteCPs.push_back(std::make_pair(CPE, i));
174 else
175 OtherCPs.push_back(std::make_pair(CPE, i));
176 }
177
178 unsigned Alignment = MCP->getConstantPoolAlignment();
179 EmitConstantPool(Alignment, FourByteConstantSection, FourByteCPs);
180 EmitConstantPool(Alignment, EightByteConstantSection, EightByteCPs);
181 EmitConstantPool(Alignment, SixteenByteConstantSection, SixteenByteCPs);
182 EmitConstantPool(Alignment, ConstantPoolSection, OtherCPs);
183}
184
185void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
186 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
187 if (CP.empty()) return;
188
189 SwitchToDataSection(Section, 0);
190 EmitAlignment(Alignment);
191 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
192 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_'
193 << CP[i].second << ":\t\t\t\t\t" << CommentString << " ";
194 WriteTypeSymbolic(O, CP[i].first.Val->getType(), 0) << '\n';
195 EmitGlobalConstant(CP[i].first.Val);
Chris Lattnerf6190822006-02-09 04:46:04 +0000196 if (i != e-1) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000197 unsigned EntSize =
198 TM.getTargetData()->getTypeSize(CP[i].first.Val->getType());
199 unsigned ValEnd = CP[i].first.Offset + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000200 // Emit inter-object padding for alignment.
Evan Chengec1d60b2006-06-29 00:26:09 +0000201 EmitZeros(CP[i+1].first.Offset-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000202 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000203 }
204}
205
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000206/// EmitJumpTableInfo - Print assembly representations of the jump tables used
207/// by the current function to the current output stream.
208///
209void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
210 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
211 if (JT.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000212 const TargetData *TD = TM.getTargetData();
Nate Begemanefc312a2006-07-27 16:46:58 +0000213
214 // JTEntryDirective is a string to print sizeof(ptr) for non-PIC jump tables,
215 // and 32 bits for PIC since PIC jump table entries are differences, not
216 // pointers to blocks.
217 const char *JTEntryDirective = Data32bitsDirective;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000218
Nate Begeman78756502006-07-27 01:13:04 +0000219 // Pick the directive to use to print the jump table entries, and switch to
220 // the appropriate section.
221 if (TM.getRelocationModel() == Reloc::PIC_) {
222 SwitchToTextSection(JumpTableTextSection, 0);
223 } else {
224 SwitchToDataSection(JumpTableDataSection, 0);
225 if (TD->getPointerSize() == 8)
Nate Begemanefc312a2006-07-27 16:46:58 +0000226 JTEntryDirective = Data64bitsDirective;
Nate Begeman78756502006-07-27 01:13:04 +0000227 }
Owen Anderson20a631f2006-05-03 01:29:57 +0000228 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000229
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000230 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
231 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i
232 << ":\n";
233 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
234 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begemanefc312a2006-07-27 16:46:58 +0000235 O << JTEntryDirective << ' ';
Nate Begeman78756502006-07-27 01:13:04 +0000236 printBasicBlockLabel(JTBBs[ii], false, false);
237 if (TM.getRelocationModel() == Reloc::PIC_) {
238 O << '-' << PrivateGlobalPrefix << "JTI" << getFunctionNumber()
239 << '_' << i;
240 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000241 O << '\n';
242 }
243 }
244}
245
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000246/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
247/// special global used by LLVM. If so, emit it and return true, otherwise
248/// do nothing and return false.
249bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000250 // Ignore debug and non-emitted data.
251 if (GV->getSection() == "llvm.metadata") return true;
252
253 if (!GV->hasAppendingLinkage()) return false;
254
255 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000256
257 if (GV->getName() == "llvm.used")
258 return true; // No need to emit this at all.
259
Chris Lattner3760e902006-01-12 19:17:23 +0000260 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000261 SwitchToDataSection(StaticCtorsSection, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000262 EmitAlignment(2, 0);
263 EmitXXStructorList(GV->getInitializer());
264 return true;
265 }
266
Chris Lattner3760e902006-01-12 19:17:23 +0000267 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000268 SwitchToDataSection(StaticDtorsSection, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000269 EmitAlignment(2, 0);
270 EmitXXStructorList(GV->getInitializer());
271 return true;
272 }
273
274 return false;
275}
276
277/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
278/// function pointers, ignoring the init priority.
279void AsmPrinter::EmitXXStructorList(Constant *List) {
280 // Should be an array of '{ int, void ()* }' structs. The first value is the
281 // init priority, which we ignore.
282 if (!isa<ConstantArray>(List)) return;
283 ConstantArray *InitList = cast<ConstantArray>(List);
284 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
285 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
286 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000287
288 if (CS->getOperand(1)->isNullValue())
289 return; // Found a null terminator, exit printing.
290 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000291 EmitGlobalConstant(CS->getOperand(1));
292 }
293}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000294
Chris Lattnera9b25252006-02-05 01:29:18 +0000295/// getPreferredAlignmentLog - Return the preferred alignment of the
296/// specified global, returned in log form. This includes an explicitly
297/// requested alignment (if the global has one).
298unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Jim Laskeydce07562006-06-15 13:10:58 +0000299 const Type *ElemType = GV->getType()->getElementType();
300 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(ElemType);
Chris Lattnera9b25252006-02-05 01:29:18 +0000301 if (GV->getAlignment() > (1U << Alignment))
302 Alignment = Log2_32(GV->getAlignment());
303
Chris Lattnercbab2842006-02-05 01:46:49 +0000304 if (GV->hasInitializer()) {
Jim Laskey3519b872006-06-15 19:37:14 +0000305 // Always round up alignment of global doubles to 8 bytes.
306 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
307 Alignment = 3;
Chris Lattnercbab2842006-02-05 01:46:49 +0000308 if (Alignment < 4) {
309 // If the global is not external, see if it is large. If so, give it a
310 // larger alignment.
Jim Laskeydce07562006-06-15 13:10:58 +0000311 if (TM.getTargetData()->getTypeSize(ElemType) > 128)
Chris Lattnercbab2842006-02-05 01:46:49 +0000312 Alignment = 4; // 16-byte alignment.
313 }
Chris Lattnera9b25252006-02-05 01:29:18 +0000314 }
315 return Alignment;
316}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000317
Chris Lattnerbb644e32005-11-21 07:51:36 +0000318// EmitAlignment - Emit an alignment directive to the specified power of two.
319void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000320 if (GV && GV->getAlignment())
321 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000322 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner1d35c162004-08-17 19:14:29 +0000323 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
324 O << AlignDirective << NumBits << "\n";
325}
326
Chris Lattnerbb644e32005-11-21 07:51:36 +0000327/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000328///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000329void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000330 if (NumZeros) {
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000331 if (ZeroDirective) {
332 O << ZeroDirective << NumZeros;
333 if (ZeroDirectiveSuffix)
334 O << ZeroDirectiveSuffix;
335 O << "\n";
336 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000337 for (; NumZeros; --NumZeros)
338 O << Data8bitsDirective << "0\n";
339 }
340 }
341}
342
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000343// Print out the specified constant, without a storage class. Only the
344// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000345void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000346 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000347 O << "0";
348 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
349 assert(CB == ConstantBool::True);
350 O << "1";
351 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
352 if (((CI->getValue() << 32) >> 32) == CI->getValue())
353 O << CI->getValue();
354 else
Duraid Madina73c4dba2005-05-15 13:05:48 +0000355 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000356 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
357 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000358 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000359 // This is a constant address for a global variable or function. Use the
360 // name of the variable or function as the address value, possibly
361 // decorating it with GlobalVarAddrPrefix/Suffix or
362 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000363 if (isa<Function>(GV))
364 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000365 else
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000366 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000367 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000368 const TargetData *TD = TM.getTargetData();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000369 switch(CE->getOpcode()) {
370 case Instruction::GetElementPtr: {
371 // generate a symbolic expression for the byte address
372 const Constant *ptrVal = CE->getOperand(0);
373 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Anderson20a631f2006-05-03 01:29:57 +0000374 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner145569b2005-02-14 21:40:26 +0000375 if (Offset)
376 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000377 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000378 if (Offset > 0)
379 O << ") + " << Offset;
380 else if (Offset < 0)
381 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000382 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000383 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000384 }
385 break;
386 }
387 case Instruction::Cast: {
Chris Lattner85492422006-07-29 01:57:19 +0000388 // Support only foldable casts to/from pointers that can be eliminated by
389 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000390 Constant *Op = CE->getOperand(0);
391 const Type *OpTy = Op->getType(), *Ty = CE->getType();
392
Chris Lattner85492422006-07-29 01:57:19 +0000393 // Handle casts to pointers by changing them into casts to the appropriate
394 // integer type. This promotes constant folding and simplifies this code.
395 if (isa<PointerType>(Ty)) {
396 const Type *IntPtrTy = TD->getIntPtrType();
397 Op = ConstantExpr::getCast(Op, IntPtrTy);
398 return EmitConstantValueOnly(Op);
399 }
400
401 // We know the dest type is not a pointer. Is the src value a pointer or
402 // integral?
403 if (isa<PointerType>(OpTy) || OpTy->isIntegral()) {
404 // We can emit the pointer value into this slot if the slot is an
405 // integer slot greater or equal to the size of the pointer.
406 if (Ty->isIntegral() && TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
407 return EmitConstantValueOnly(Op);
408 }
409
410 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000411 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000412 break;
413 }
414 case Instruction::Add:
415 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000416 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000417 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000418 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000419 O << ")";
420 break;
421 default:
422 assert(0 && "Unsupported operator!");
423 }
424 } else {
425 assert(0 && "Unknown constant value!");
426 }
427}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000428
429/// toOctal - Convert the low order bits of X into an octal digit.
430///
431static inline char toOctal(int X) {
432 return (X&7)+'0';
433}
434
Chris Lattner55a6d902005-11-10 18:06:33 +0000435/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000436/// the predicate isString is true.
437///
Chris Lattner55a6d902005-11-10 18:06:33 +0000438static void printAsCString(std::ostream &O, const ConstantArray *CVA,
439 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000440 assert(CVA->isString() && "Array is not string compatible!");
441
442 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000443 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000444 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000445 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000446
447 if (C == '"') {
448 O << "\\\"";
449 } else if (C == '\\') {
450 O << "\\\\";
451 } else if (isprint(C)) {
452 O << C;
453 } else {
454 switch(C) {
455 case '\b': O << "\\b"; break;
456 case '\f': O << "\\f"; break;
457 case '\n': O << "\\n"; break;
458 case '\r': O << "\\r"; break;
459 case '\t': O << "\\t"; break;
460 default:
461 O << '\\';
462 O << toOctal(C >> 6);
463 O << toOctal(C >> 3);
464 O << toOctal(C >> 0);
465 break;
466 }
467 }
468 }
469 O << "\"";
470}
471
Jeff Cohen24a62a92006-05-02 01:16:28 +0000472/// EmitString - Emit a zero-byte-terminated string constant.
473///
474void AsmPrinter::EmitString(const ConstantArray *CVA) const {
475 unsigned NumElts = CVA->getNumOperands();
476 if (AscizDirective && NumElts &&
477 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
478 O << AscizDirective;
479 printAsCString(O, CVA, NumElts-1);
480 } else {
481 O << AsciiDirective;
482 printAsCString(O, CVA, NumElts);
483 }
484 O << "\n";
485}
486
Chris Lattnerbb644e32005-11-21 07:51:36 +0000487/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000488///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000489void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000490 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000491
Chris Lattner61753bf2004-10-16 18:19:26 +0000492 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000493 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000494 return;
495 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
496 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000497 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000498 } else { // Not a string. Print the values in successive locations
499 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000500 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000501 }
502 return;
503 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
504 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000505 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000506 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000507 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
508 const Constant* field = CVS->getOperand(i);
509
510 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000511 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000512 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000513 : cvsLayout->MemberOffsets[i+1])
514 - cvsLayout->MemberOffsets[i]) - fieldSize;
515 sizeSoFar += fieldSize + padSize;
516
517 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000518 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000519
520 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000521 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000522 }
523 assert(sizeSoFar == cvsLayout->StructSize &&
524 "Layout of constant struct may be incorrect!");
525 return;
526 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
527 // FP Constants are printed as integer constants to avoid losing
528 // precision...
529 double Val = CFP->getValue();
530 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000531 if (Data64bitsDirective)
Jim Laskeyb74c6662005-08-17 19:34:49 +0000532 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000533 << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000534 else if (TD->isBigEndian()) {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000535 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000536 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000537 << Val << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000538 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000539 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000540 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000541 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000542 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000543 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000544 << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000545 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000546 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000547 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000548 }
549 return;
550 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000551 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000552 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000553 return;
554 }
555 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
556 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
557 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000558
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000559 if (Data64bitsDirective)
560 O << Data64bitsDirective << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000561 else if (TD->isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000562 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000563 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000564 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000565 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000566 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000567 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000568 } else {
569 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000570 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000571 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000572 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000573 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000574 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000575 }
576 return;
577 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000578 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
579 const PackedType *PTy = CP->getType();
580
581 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
582 EmitGlobalConstant(CP->getOperand(I));
583
584 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000585 }
586
587 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000588 switch (type->getTypeID()) {
Misha Brukman835702a2005-04-21 22:36:52 +0000589 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000590 case Type::UByteTyID: case Type::SByteTyID:
591 O << Data8bitsDirective;
592 break;
593 case Type::UShortTyID: case Type::ShortTyID:
594 O << Data16bitsDirective;
595 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000596 case Type::PointerTyID:
Owen Anderson20a631f2006-05-03 01:29:57 +0000597 if (TD->getPointerSize() == 8) {
Evan Chengdf631792006-06-15 08:10:56 +0000598 assert(Data64bitsDirective &&
599 "Target cannot handle 64-bit pointer exprs!");
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000600 O << Data64bitsDirective;
601 break;
602 }
603 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000604 case Type::UIntTyID: case Type::IntTyID:
605 O << Data32bitsDirective;
606 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000607 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner88e2d2e2005-08-08 04:26:32 +0000608 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
609 O << Data64bitsDirective;
610 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000611 case Type::FloatTyID: case Type::DoubleTyID:
612 assert (0 && "Should have already output floating point constant.");
613 default:
614 assert (0 && "Can't handle printing this type of thing");
615 break;
616 }
Chris Lattnerbb644e32005-11-21 07:51:36 +0000617 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000618 O << "\n";
619}
Chris Lattner061d9e22006-01-27 02:10:10 +0000620
621/// printInlineAsm - This method formats and prints the specified machine
622/// instruction that is an inline asm.
623void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +0000624 unsigned NumOperands = MI->getNumOperands();
625
626 // Count the number of register definitions.
627 unsigned NumDefs = 0;
628 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
629 assert(NumDefs != NumOperands-1 && "No asm string?");
630
631 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000632
633 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000634 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000635
Chris Lattner3390cbe2006-07-28 00:17:20 +0000636 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
637 if (AsmStr[0] == 0) {
638 O << "\n"; // Tab already printed, avoid double indenting next instr.
639 return;
640 }
641
642 O << InlineAsmStart << "\n\t";
643
Chris Lattneraa23fa92006-02-01 22:41:11 +0000644 // The variant of the current asmprinter: FIXME: change.
645 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000646
Chris Lattneraa23fa92006-02-01 22:41:11 +0000647 int CurVariant = -1; // The number of the {.|.|.} region we are in.
648 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000649
Chris Lattneraa23fa92006-02-01 22:41:11 +0000650 while (*LastEmitted) {
651 switch (*LastEmitted) {
652 default: {
653 // Not a special case, emit the string section literally.
654 const char *LiteralEnd = LastEmitted+1;
655 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000656 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000657 ++LiteralEnd;
658 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
659 O.write(LastEmitted, LiteralEnd-LastEmitted);
660 LastEmitted = LiteralEnd;
661 break;
662 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000663 case '\n':
664 ++LastEmitted; // Consume newline character.
665 O << "\n\t"; // Indent code with newline.
666 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000667 case '$': {
668 ++LastEmitted; // Consume '$' character.
669 if (*LastEmitted == '$') { // $$ -> $
670 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
671 O << '$';
672 ++LastEmitted; // Consume second '$' character.
673 break;
674 }
675
676 bool HasCurlyBraces = false;
677 if (*LastEmitted == '{') { // ${variable}
678 ++LastEmitted; // Consume '{' character.
679 HasCurlyBraces = true;
680 }
681
682 const char *IDStart = LastEmitted;
683 char *IDEnd;
684 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
685 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
686 std::cerr << "Bad $ operand number in inline asm string: '"
687 << AsmStr << "'\n";
688 exit(1);
689 }
690 LastEmitted = IDEnd;
691
Chris Lattner34f74c12006-02-06 22:17:23 +0000692 char Modifier[2] = { 0, 0 };
693
Chris Lattneraa23fa92006-02-01 22:41:11 +0000694 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000695 // If we have curly braces, check for a modifier character. This
696 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
697 if (*LastEmitted == ':') {
698 ++LastEmitted; // Consume ':' character.
699 if (*LastEmitted == 0) {
700 std::cerr << "Bad ${:} expression in inline asm string: '"
701 << AsmStr << "'\n";
702 exit(1);
703 }
704
705 Modifier[0] = *LastEmitted;
706 ++LastEmitted; // Consume modifier character.
707 }
708
Chris Lattneraa23fa92006-02-01 22:41:11 +0000709 if (*LastEmitted != '}') {
710 std::cerr << "Bad ${} expression in inline asm string: '"
711 << AsmStr << "'\n";
712 exit(1);
713 }
714 ++LastEmitted; // Consume '}' character.
715 }
716
717 if ((unsigned)Val >= NumOperands-1) {
718 std::cerr << "Invalid $ operand number in inline asm string: '"
719 << AsmStr << "'\n";
720 exit(1);
721 }
722
Chris Lattner571d9642006-02-23 19:21:04 +0000723 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000724 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000725 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
726 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000727
728 bool Error = false;
729
Chris Lattner571d9642006-02-23 19:21:04 +0000730 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000731 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000732 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner5af3fde2006-02-24 19:50:58 +0000733 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
734 OpNo += (OpFlags >> 3) + 1;
735 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000736
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000737 if (OpNo >= MI->getNumOperands()) {
738 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +0000739 } else {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000740 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
741 ++OpNo; // Skip over the ID number.
742
743 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
744 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
745 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
746 Modifier[0] ? Modifier : 0);
747 } else {
748 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
749 Modifier[0] ? Modifier : 0);
750 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000751 }
752 if (Error) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000753 std::cerr << "Invalid operand found in inline asm: '"
754 << AsmStr << "'\n";
755 MI->dump();
756 exit(1);
757 }
Chris Lattner571d9642006-02-23 19:21:04 +0000758 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000759 break;
760 }
761 case '{':
762 ++LastEmitted; // Consume '{' character.
763 if (CurVariant != -1) {
764 std::cerr << "Nested variants found in inline asm string: '"
765 << AsmStr << "'\n";
766 exit(1);
767 }
768 CurVariant = 0; // We're in the first variant now.
769 break;
770 case '|':
771 ++LastEmitted; // consume '|' character.
772 if (CurVariant == -1) {
773 std::cerr << "Found '|' character outside of variant in inline asm "
774 << "string: '" << AsmStr << "'\n";
775 exit(1);
776 }
777 ++CurVariant; // We're in the next variant.
778 break;
779 case '}':
780 ++LastEmitted; // consume '}' character.
781 if (CurVariant == -1) {
782 std::cerr << "Found '}' character outside of variant in inline asm "
783 << "string: '" << AsmStr << "'\n";
784 exit(1);
785 }
786 CurVariant = -1;
787 break;
788 }
789 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000790 O << "\n\t" << InlineAsmEnd << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000791}
792
793/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
794/// instruction, using the specified assembler variant. Targets should
795/// overried this to format as appropriate.
796bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000797 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000798 // Target doesn't support this yet!
799 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000800}
Chris Lattner1d08c652006-02-24 20:21:58 +0000801
802bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
803 unsigned AsmVariant,
804 const char *ExtraCode) {
805 // Target doesn't support this yet!
806 return true;
807}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000808
809/// printBasicBlockLabel - This method prints the label for the specified
810/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +0000811void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
812 bool printColon,
813 bool printComment) const {
Nate Begeman4971ba52006-05-02 17:36:46 +0000814 O << PrivateGlobalPrefix << "BB" << FunctionNumber << "_"
815 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +0000816 if (printColon)
817 O << ':';
818 if (printComment)
819 O << '\t' << CommentString << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000820}