blob: 9414e69b4ce58e266efbde2f74d5ee0685f62298 [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),
Nate Begeman984c1a42006-08-12 21:29:52 +000062 SetDirective(0),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000063 LCOMMDirective(0),
64 COMMDirective("\t.comm\t"),
65 COMMDirectiveTakesAlignment(true),
66 HasDotTypeDotSizeDirective(true) {
67}
68
69
Chris Lattner8488ba22006-05-09 04:59:56 +000070/// SwitchToTextSection - Switch to the specified text section of the executable
71/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000072///
Chris Lattner8488ba22006-05-09 04:59:56 +000073void AsmPrinter::SwitchToTextSection(const char *NewSection,
74 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000075 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000076 if (GV && GV->hasSection())
Chris Lattnerf8017922006-05-09 16:41:59 +000077 NS = SwitchToSectionDirective + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000078 else
79 NS = NewSection;
80
81 // If we're already in this section, we're done.
82 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000083
Chris Lattner4ebc6a22006-05-09 05:33:48 +000084 // Close the current section, if applicable.
85 if (SectionEndDirectiveSuffix && !CurrentSection.empty())
86 O << CurrentSection << SectionEndDirectiveSuffix << "\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000087
Chris Lattner4ebc6a22006-05-09 05:33:48 +000088 CurrentSection = NS;
89
90 if (!CurrentSection.empty())
91 O << CurrentSection << TextSectionStartSuffix << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000092}
93
Nate Begeman78756502006-07-27 01:13:04 +000094/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000095/// if we are not already in it!
96///
97void AsmPrinter::SwitchToDataSection(const char *NewSection,
98 const GlobalValue *GV) {
99 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +0000100 if (GV && GV->hasSection())
101 NS = SwitchToSectionDirective + GV->getSection();
102 else
103 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +0000104
Chris Lattner6341df82006-05-09 05:23:12 +0000105 // If we're already in this section, we're done.
106 if (CurrentSection == NS) return;
107
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000108 // Close the current section, if applicable.
109 if (SectionEndDirectiveSuffix && !CurrentSection.empty())
110 O << CurrentSection << SectionEndDirectiveSuffix << "\n";
111
112 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +0000113
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000114 if (!CurrentSection.empty())
115 O << CurrentSection << DataSectionStartSuffix << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +0000116}
117
118
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000119bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner6d42cbd2004-08-17 06:06:19 +0000120 Mang = new Mangler(M, GlobalPrefix);
Chris Lattnere3a79262006-01-23 23:47:53 +0000121
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000122 if (!M.getModuleInlineAsm().empty())
123 O << CommentString << " Start of file scope inline assembly\n"
124 << M.getModuleInlineAsm()
125 << "\n" << CommentString << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000126
Chris Lattner8488ba22006-05-09 04:59:56 +0000127 SwitchToDataSection("", 0); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000128
129 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
130 DebugInfo->AnalyzeModule(M);
131 }
132
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000133 return false;
134}
135
136bool AsmPrinter::doFinalization(Module &M) {
137 delete Mang; Mang = 0;
138 return false;
139}
140
Chris Lattnerbb644e32005-11-21 07:51:36 +0000141void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000142 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000143 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000144 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000145}
146
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000147/// EmitConstantPool - Print to the current output stream assembly
148/// representations of the constants in the constant pool MCP. This is
149/// used to print out constants which have been "spilled to memory" by
150/// the code generator.
151///
152void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000153 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000154 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000155
156 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
157 // in special sections.
158 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
159 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
160 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
161 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000162 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000163 MachineConstantPoolEntry CPE = CP[i];
164 const Constant *CV = CPE.Val;
165 const Type *Ty = CV->getType();
166 if (FourByteConstantSection &&
167 TM.getTargetData()->getTypeSize(Ty) == 4)
168 FourByteCPs.push_back(std::make_pair(CPE, i));
169 else if (EightByteConstantSection &&
170 TM.getTargetData()->getTypeSize(Ty) == 8)
171 EightByteCPs.push_back(std::make_pair(CPE, i));
172 else if (SixteenByteConstantSection &&
173 TM.getTargetData()->getTypeSize(Ty) == 16)
174 SixteenByteCPs.push_back(std::make_pair(CPE, i));
175 else
176 OtherCPs.push_back(std::make_pair(CPE, i));
177 }
178
179 unsigned Alignment = MCP->getConstantPoolAlignment();
180 EmitConstantPool(Alignment, FourByteConstantSection, FourByteCPs);
181 EmitConstantPool(Alignment, EightByteConstantSection, EightByteCPs);
182 EmitConstantPool(Alignment, SixteenByteConstantSection, SixteenByteCPs);
183 EmitConstantPool(Alignment, ConstantPoolSection, OtherCPs);
184}
185
186void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
187 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
188 if (CP.empty()) return;
189
190 SwitchToDataSection(Section, 0);
191 EmitAlignment(Alignment);
192 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
193 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_'
194 << CP[i].second << ":\t\t\t\t\t" << CommentString << " ";
195 WriteTypeSymbolic(O, CP[i].first.Val->getType(), 0) << '\n';
196 EmitGlobalConstant(CP[i].first.Val);
Chris Lattnerf6190822006-02-09 04:46:04 +0000197 if (i != e-1) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000198 unsigned EntSize =
199 TM.getTargetData()->getTypeSize(CP[i].first.Val->getType());
200 unsigned ValEnd = CP[i].first.Offset + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000201 // Emit inter-object padding for alignment.
Evan Chengec1d60b2006-06-29 00:26:09 +0000202 EmitZeros(CP[i+1].first.Offset-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000203 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000204 }
205}
206
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000207/// EmitJumpTableInfo - Print assembly representations of the jump tables used
208/// by the current function to the current output stream.
209///
210void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
211 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
212 if (JT.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000213 const TargetData *TD = TM.getTargetData();
Nate Begemanefc312a2006-07-27 16:46:58 +0000214
215 // JTEntryDirective is a string to print sizeof(ptr) for non-PIC jump tables,
216 // and 32 bits for PIC since PIC jump table entries are differences, not
217 // pointers to blocks.
218 const char *JTEntryDirective = Data32bitsDirective;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000219
Nate Begeman78756502006-07-27 01:13:04 +0000220 // Pick the directive to use to print the jump table entries, and switch to
221 // the appropriate section.
222 if (TM.getRelocationModel() == Reloc::PIC_) {
223 SwitchToTextSection(JumpTableTextSection, 0);
224 } else {
225 SwitchToDataSection(JumpTableDataSection, 0);
226 if (TD->getPointerSize() == 8)
Nate Begemanefc312a2006-07-27 16:46:58 +0000227 JTEntryDirective = Data64bitsDirective;
Nate Begeman78756502006-07-27 01:13:04 +0000228 }
Owen Anderson20a631f2006-05-03 01:29:57 +0000229 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000230
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000231 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000232 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
233
234 // For PIC codegen, if possible we want to use the SetDirective to reduce
235 // the number of relocations the assembler will generate for the jump table.
236 // Set directives are all printed before the jump table itself.
237 std::set<MachineBasicBlock*> EmittedSets;
238 if (SetDirective && TM.getRelocationModel() == Reloc::PIC_)
239 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
240 if (EmittedSets.insert(JTBBs[ii]).second)
241 printSetLabel(i, JTBBs[ii]);
242
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000243 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i
244 << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000245
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000246 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begemanefc312a2006-07-27 16:46:58 +0000247 O << JTEntryDirective << ' ';
Nate Begeman984c1a42006-08-12 21:29:52 +0000248 // If we have emitted set directives for the jump table entries, print
249 // them rather than the entries themselves. If we're emitting PIC, then
250 // emit the table entries as differences between two text section labels.
251 // If we're emitting non-PIC code, then emit the entries as direct
252 // references to the target basic blocks.
253 if (!EmittedSets.empty()) {
254 O << PrivateGlobalPrefix << getFunctionNumber() << '_' << i << "_set_"
255 << JTBBs[ii]->getNumber();
256 } else if (TM.getRelocationModel() == Reloc::PIC_) {
257 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman78756502006-07-27 01:13:04 +0000258 O << '-' << PrivateGlobalPrefix << "JTI" << getFunctionNumber()
259 << '_' << i;
Nate Begeman984c1a42006-08-12 21:29:52 +0000260 } else {
261 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman78756502006-07-27 01:13:04 +0000262 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000263 O << '\n';
264 }
265 }
266}
267
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000268/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
269/// special global used by LLVM. If so, emit it and return true, otherwise
270/// do nothing and return false.
271bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000272 // Ignore debug and non-emitted data.
273 if (GV->getSection() == "llvm.metadata") return true;
274
275 if (!GV->hasAppendingLinkage()) return false;
276
277 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000278
279 if (GV->getName() == "llvm.used")
280 return true; // No need to emit this at all.
281
Chris Lattner3760e902006-01-12 19:17:23 +0000282 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000283 SwitchToDataSection(StaticCtorsSection, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000284 EmitAlignment(2, 0);
285 EmitXXStructorList(GV->getInitializer());
286 return true;
287 }
288
Chris Lattner3760e902006-01-12 19:17:23 +0000289 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000290 SwitchToDataSection(StaticDtorsSection, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000291 EmitAlignment(2, 0);
292 EmitXXStructorList(GV->getInitializer());
293 return true;
294 }
295
296 return false;
297}
298
299/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
300/// function pointers, ignoring the init priority.
301void AsmPrinter::EmitXXStructorList(Constant *List) {
302 // Should be an array of '{ int, void ()* }' structs. The first value is the
303 // init priority, which we ignore.
304 if (!isa<ConstantArray>(List)) return;
305 ConstantArray *InitList = cast<ConstantArray>(List);
306 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
307 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
308 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000309
310 if (CS->getOperand(1)->isNullValue())
311 return; // Found a null terminator, exit printing.
312 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000313 EmitGlobalConstant(CS->getOperand(1));
314 }
315}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000316
Chris Lattnera9b25252006-02-05 01:29:18 +0000317/// getPreferredAlignmentLog - Return the preferred alignment of the
318/// specified global, returned in log form. This includes an explicitly
319/// requested alignment (if the global has one).
320unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Jim Laskeydce07562006-06-15 13:10:58 +0000321 const Type *ElemType = GV->getType()->getElementType();
322 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(ElemType);
Chris Lattnera9b25252006-02-05 01:29:18 +0000323 if (GV->getAlignment() > (1U << Alignment))
324 Alignment = Log2_32(GV->getAlignment());
325
Chris Lattnercbab2842006-02-05 01:46:49 +0000326 if (GV->hasInitializer()) {
Jim Laskey3519b872006-06-15 19:37:14 +0000327 // Always round up alignment of global doubles to 8 bytes.
328 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
329 Alignment = 3;
Chris Lattnercbab2842006-02-05 01:46:49 +0000330 if (Alignment < 4) {
331 // If the global is not external, see if it is large. If so, give it a
332 // larger alignment.
Jim Laskeydce07562006-06-15 13:10:58 +0000333 if (TM.getTargetData()->getTypeSize(ElemType) > 128)
Chris Lattnercbab2842006-02-05 01:46:49 +0000334 Alignment = 4; // 16-byte alignment.
335 }
Chris Lattnera9b25252006-02-05 01:29:18 +0000336 }
337 return Alignment;
338}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000339
Chris Lattnerbb644e32005-11-21 07:51:36 +0000340// EmitAlignment - Emit an alignment directive to the specified power of two.
341void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000342 if (GV && GV->getAlignment())
343 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000344 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner1d35c162004-08-17 19:14:29 +0000345 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
346 O << AlignDirective << NumBits << "\n";
347}
348
Chris Lattnerbb644e32005-11-21 07:51:36 +0000349/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000350///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000351void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000352 if (NumZeros) {
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000353 if (ZeroDirective) {
354 O << ZeroDirective << NumZeros;
355 if (ZeroDirectiveSuffix)
356 O << ZeroDirectiveSuffix;
357 O << "\n";
358 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000359 for (; NumZeros; --NumZeros)
360 O << Data8bitsDirective << "0\n";
361 }
362 }
363}
364
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000365// Print out the specified constant, without a storage class. Only the
366// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000367void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000368 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000369 O << "0";
370 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
371 assert(CB == ConstantBool::True);
372 O << "1";
373 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
374 if (((CI->getValue() << 32) >> 32) == CI->getValue())
375 O << CI->getValue();
376 else
Duraid Madina73c4dba2005-05-15 13:05:48 +0000377 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000378 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
379 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000380 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000381 // This is a constant address for a global variable or function. Use the
382 // name of the variable or function as the address value, possibly
383 // decorating it with GlobalVarAddrPrefix/Suffix or
384 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000385 if (isa<Function>(GV))
386 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000387 else
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000388 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000389 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000390 const TargetData *TD = TM.getTargetData();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000391 switch(CE->getOpcode()) {
392 case Instruction::GetElementPtr: {
393 // generate a symbolic expression for the byte address
394 const Constant *ptrVal = CE->getOperand(0);
395 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Anderson20a631f2006-05-03 01:29:57 +0000396 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner145569b2005-02-14 21:40:26 +0000397 if (Offset)
398 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000399 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000400 if (Offset > 0)
401 O << ") + " << Offset;
402 else if (Offset < 0)
403 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000404 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000405 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000406 }
407 break;
408 }
409 case Instruction::Cast: {
Chris Lattner85492422006-07-29 01:57:19 +0000410 // Support only foldable casts to/from pointers that can be eliminated by
411 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000412 Constant *Op = CE->getOperand(0);
413 const Type *OpTy = Op->getType(), *Ty = CE->getType();
414
Chris Lattner85492422006-07-29 01:57:19 +0000415 // Handle casts to pointers by changing them into casts to the appropriate
416 // integer type. This promotes constant folding and simplifies this code.
417 if (isa<PointerType>(Ty)) {
418 const Type *IntPtrTy = TD->getIntPtrType();
419 Op = ConstantExpr::getCast(Op, IntPtrTy);
420 return EmitConstantValueOnly(Op);
421 }
422
423 // We know the dest type is not a pointer. Is the src value a pointer or
424 // integral?
425 if (isa<PointerType>(OpTy) || OpTy->isIntegral()) {
426 // We can emit the pointer value into this slot if the slot is an
427 // integer slot greater or equal to the size of the pointer.
428 if (Ty->isIntegral() && TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
429 return EmitConstantValueOnly(Op);
430 }
431
432 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000433 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000434 break;
435 }
436 case Instruction::Add:
437 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000438 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000439 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000440 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000441 O << ")";
442 break;
443 default:
444 assert(0 && "Unsupported operator!");
445 }
446 } else {
447 assert(0 && "Unknown constant value!");
448 }
449}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000450
451/// toOctal - Convert the low order bits of X into an octal digit.
452///
453static inline char toOctal(int X) {
454 return (X&7)+'0';
455}
456
Chris Lattner55a6d902005-11-10 18:06:33 +0000457/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000458/// the predicate isString is true.
459///
Chris Lattner55a6d902005-11-10 18:06:33 +0000460static void printAsCString(std::ostream &O, const ConstantArray *CVA,
461 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000462 assert(CVA->isString() && "Array is not string compatible!");
463
464 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000465 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000466 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000467 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000468
469 if (C == '"') {
470 O << "\\\"";
471 } else if (C == '\\') {
472 O << "\\\\";
473 } else if (isprint(C)) {
474 O << C;
475 } else {
476 switch(C) {
477 case '\b': O << "\\b"; break;
478 case '\f': O << "\\f"; break;
479 case '\n': O << "\\n"; break;
480 case '\r': O << "\\r"; break;
481 case '\t': O << "\\t"; break;
482 default:
483 O << '\\';
484 O << toOctal(C >> 6);
485 O << toOctal(C >> 3);
486 O << toOctal(C >> 0);
487 break;
488 }
489 }
490 }
491 O << "\"";
492}
493
Jeff Cohen24a62a92006-05-02 01:16:28 +0000494/// EmitString - Emit a zero-byte-terminated string constant.
495///
496void AsmPrinter::EmitString(const ConstantArray *CVA) const {
497 unsigned NumElts = CVA->getNumOperands();
498 if (AscizDirective && NumElts &&
499 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
500 O << AscizDirective;
501 printAsCString(O, CVA, NumElts-1);
502 } else {
503 O << AsciiDirective;
504 printAsCString(O, CVA, NumElts);
505 }
506 O << "\n";
507}
508
Chris Lattnerbb644e32005-11-21 07:51:36 +0000509/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000510///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000511void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000512 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000513
Chris Lattner61753bf2004-10-16 18:19:26 +0000514 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000515 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000516 return;
517 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
518 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000519 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000520 } else { // Not a string. Print the values in successive locations
521 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000522 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000523 }
524 return;
525 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
526 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000527 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000528 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000529 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
530 const Constant* field = CVS->getOperand(i);
531
532 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000533 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000534 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000535 : cvsLayout->MemberOffsets[i+1])
536 - cvsLayout->MemberOffsets[i]) - fieldSize;
537 sizeSoFar += fieldSize + padSize;
538
539 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000540 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000541
542 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000543 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000544 }
545 assert(sizeSoFar == cvsLayout->StructSize &&
546 "Layout of constant struct may be incorrect!");
547 return;
548 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
549 // FP Constants are printed as integer constants to avoid losing
550 // precision...
551 double Val = CFP->getValue();
552 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000553 if (Data64bitsDirective)
Jim Laskeyb74c6662005-08-17 19:34:49 +0000554 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000555 << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000556 else if (TD->isBigEndian()) {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000557 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000558 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000559 << Val << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000560 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000561 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000562 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000563 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000564 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000565 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000566 << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000567 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000568 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000569 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000570 }
571 return;
572 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000573 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000574 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000575 return;
576 }
577 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
578 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
579 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000580
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000581 if (Data64bitsDirective)
582 O << Data64bitsDirective << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000583 else if (TD->isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000584 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000585 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000586 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000587 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000588 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000589 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000590 } else {
591 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000592 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000593 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000594 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000595 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000596 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000597 }
598 return;
599 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000600 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
601 const PackedType *PTy = CP->getType();
602
603 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
604 EmitGlobalConstant(CP->getOperand(I));
605
606 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000607 }
608
609 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000610 switch (type->getTypeID()) {
Misha Brukman835702a2005-04-21 22:36:52 +0000611 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000612 case Type::UByteTyID: case Type::SByteTyID:
613 O << Data8bitsDirective;
614 break;
615 case Type::UShortTyID: case Type::ShortTyID:
616 O << Data16bitsDirective;
617 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000618 case Type::PointerTyID:
Owen Anderson20a631f2006-05-03 01:29:57 +0000619 if (TD->getPointerSize() == 8) {
Evan Chengdf631792006-06-15 08:10:56 +0000620 assert(Data64bitsDirective &&
621 "Target cannot handle 64-bit pointer exprs!");
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000622 O << Data64bitsDirective;
623 break;
624 }
625 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000626 case Type::UIntTyID: case Type::IntTyID:
627 O << Data32bitsDirective;
628 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000629 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner88e2d2e2005-08-08 04:26:32 +0000630 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
631 O << Data64bitsDirective;
632 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000633 case Type::FloatTyID: case Type::DoubleTyID:
634 assert (0 && "Should have already output floating point constant.");
635 default:
636 assert (0 && "Can't handle printing this type of thing");
637 break;
638 }
Chris Lattnerbb644e32005-11-21 07:51:36 +0000639 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000640 O << "\n";
641}
Chris Lattner061d9e22006-01-27 02:10:10 +0000642
643/// printInlineAsm - This method formats and prints the specified machine
644/// instruction that is an inline asm.
645void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +0000646 unsigned NumOperands = MI->getNumOperands();
647
648 // Count the number of register definitions.
649 unsigned NumDefs = 0;
Chris Lattner45456d42006-09-05 20:02:51 +0000650 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
651 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +0000652 assert(NumDefs != NumOperands-1 && "No asm string?");
653
654 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000655
656 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000657 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000658
Chris Lattner3390cbe2006-07-28 00:17:20 +0000659 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
660 if (AsmStr[0] == 0) {
661 O << "\n"; // Tab already printed, avoid double indenting next instr.
662 return;
663 }
664
665 O << InlineAsmStart << "\n\t";
666
Chris Lattneraa23fa92006-02-01 22:41:11 +0000667 // The variant of the current asmprinter: FIXME: change.
668 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000669
Chris Lattneraa23fa92006-02-01 22:41:11 +0000670 int CurVariant = -1; // The number of the {.|.|.} region we are in.
671 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000672
Chris Lattneraa23fa92006-02-01 22:41:11 +0000673 while (*LastEmitted) {
674 switch (*LastEmitted) {
675 default: {
676 // Not a special case, emit the string section literally.
677 const char *LiteralEnd = LastEmitted+1;
678 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000679 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000680 ++LiteralEnd;
681 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
682 O.write(LastEmitted, LiteralEnd-LastEmitted);
683 LastEmitted = LiteralEnd;
684 break;
685 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000686 case '\n':
687 ++LastEmitted; // Consume newline character.
688 O << "\n\t"; // Indent code with newline.
689 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000690 case '$': {
691 ++LastEmitted; // Consume '$' character.
692 if (*LastEmitted == '$') { // $$ -> $
693 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
694 O << '$';
695 ++LastEmitted; // Consume second '$' character.
696 break;
697 }
698
699 bool HasCurlyBraces = false;
700 if (*LastEmitted == '{') { // ${variable}
701 ++LastEmitted; // Consume '{' character.
702 HasCurlyBraces = true;
703 }
704
705 const char *IDStart = LastEmitted;
706 char *IDEnd;
707 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
708 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
709 std::cerr << "Bad $ operand number in inline asm string: '"
710 << AsmStr << "'\n";
711 exit(1);
712 }
713 LastEmitted = IDEnd;
714
Chris Lattner34f74c12006-02-06 22:17:23 +0000715 char Modifier[2] = { 0, 0 };
716
Chris Lattneraa23fa92006-02-01 22:41:11 +0000717 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000718 // If we have curly braces, check for a modifier character. This
719 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
720 if (*LastEmitted == ':') {
721 ++LastEmitted; // Consume ':' character.
722 if (*LastEmitted == 0) {
723 std::cerr << "Bad ${:} expression in inline asm string: '"
724 << AsmStr << "'\n";
725 exit(1);
726 }
727
728 Modifier[0] = *LastEmitted;
729 ++LastEmitted; // Consume modifier character.
730 }
731
Chris Lattneraa23fa92006-02-01 22:41:11 +0000732 if (*LastEmitted != '}') {
733 std::cerr << "Bad ${} expression in inline asm string: '"
734 << AsmStr << "'\n";
735 exit(1);
736 }
737 ++LastEmitted; // Consume '}' character.
738 }
739
740 if ((unsigned)Val >= NumOperands-1) {
741 std::cerr << "Invalid $ operand number in inline asm string: '"
742 << AsmStr << "'\n";
743 exit(1);
744 }
745
Chris Lattner571d9642006-02-23 19:21:04 +0000746 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000747 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000748 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
749 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000750
751 bool Error = false;
752
Chris Lattner571d9642006-02-23 19:21:04 +0000753 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000754 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000755 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner5af3fde2006-02-24 19:50:58 +0000756 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
757 OpNo += (OpFlags >> 3) + 1;
758 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000759
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000760 if (OpNo >= MI->getNumOperands()) {
761 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +0000762 } else {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000763 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
764 ++OpNo; // Skip over the ID number.
765
766 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
767 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
768 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
769 Modifier[0] ? Modifier : 0);
770 } else {
771 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
772 Modifier[0] ? Modifier : 0);
773 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000774 }
775 if (Error) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000776 std::cerr << "Invalid operand found in inline asm: '"
777 << AsmStr << "'\n";
778 MI->dump();
779 exit(1);
780 }
Chris Lattner571d9642006-02-23 19:21:04 +0000781 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000782 break;
783 }
784 case '{':
785 ++LastEmitted; // Consume '{' character.
786 if (CurVariant != -1) {
787 std::cerr << "Nested variants found in inline asm string: '"
788 << AsmStr << "'\n";
789 exit(1);
790 }
791 CurVariant = 0; // We're in the first variant now.
792 break;
793 case '|':
794 ++LastEmitted; // consume '|' character.
795 if (CurVariant == -1) {
796 std::cerr << "Found '|' character outside of variant in inline asm "
797 << "string: '" << AsmStr << "'\n";
798 exit(1);
799 }
800 ++CurVariant; // We're in the next variant.
801 break;
802 case '}':
803 ++LastEmitted; // consume '}' character.
804 if (CurVariant == -1) {
805 std::cerr << "Found '}' character outside of variant in inline asm "
806 << "string: '" << AsmStr << "'\n";
807 exit(1);
808 }
809 CurVariant = -1;
810 break;
811 }
812 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000813 O << "\n\t" << InlineAsmEnd << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000814}
815
816/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
817/// instruction, using the specified assembler variant. Targets should
818/// overried this to format as appropriate.
819bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000820 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000821 // Target doesn't support this yet!
822 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000823}
Chris Lattner1d08c652006-02-24 20:21:58 +0000824
825bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
826 unsigned AsmVariant,
827 const char *ExtraCode) {
828 // Target doesn't support this yet!
829 return true;
830}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000831
832/// printBasicBlockLabel - This method prints the label for the specified
833/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +0000834void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
835 bool printColon,
836 bool printComment) const {
Nate Begeman4971ba52006-05-02 17:36:46 +0000837 O << PrivateGlobalPrefix << "BB" << FunctionNumber << "_"
838 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +0000839 if (printColon)
840 O << ':';
841 if (printComment)
842 O << '\t' << CommentString << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000843}
Nate Begeman984c1a42006-08-12 21:29:52 +0000844
845/// printSetLabel - This method prints a set label for the specified
846/// MachineBasicBlock
847void AsmPrinter::printSetLabel(unsigned uid,
848 const MachineBasicBlock *MBB) const {
849 if (!SetDirective)
850 return;
851
852 O << SetDirective << ' ' << PrivateGlobalPrefix << getFunctionNumber()
853 << '_' << uid << "_set_" << MBB->getNumber() << ',';
854 printBasicBlockLabel(MBB, false, false);
855 O << '-' << PrivateGlobalPrefix << "JTI" << getFunctionNumber()
856 << '_' << uid << '\n';
857}