blob: f6b4bfcbfe357a520f9c8417fd61503e36619827 [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;
Evan Chengd6594ae2006-09-12 21:00:35 +0000129 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000130 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng2d2cec12006-06-29 00:26:09 +0000131 MachineConstantPoolEntry CPE = CP[i];
Evan Chengd5a99d72006-09-14 07:35:00 +0000132 const Type *Ty = CPE.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 Chengd5a99d72006-09-14 07:35:00 +0000163 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
164 if (CP[i].first.isMachineConstantPoolEntry())
Evan Chengd6594ae2006-09-12 21:00:35 +0000165 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Chengd5a99d72006-09-14 07:35:00 +0000166 else
Evan Chengd6594ae2006-09-12 21:00:35 +0000167 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000168 if (i != e-1) {
Evan Chengd5a99d72006-09-14 07:35:00 +0000169 const Type *Ty = CP[i].first.getType();
Evan Cheng2d2cec12006-06-29 00:26:09 +0000170 unsigned EntSize =
Evan Chengd6594ae2006-09-12 21:00:35 +0000171 TM.getTargetData()->getTypeSize(Ty);
Evan Chengd5a99d72006-09-14 07:35:00 +0000172 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattner3029f922006-02-09 04:46:04 +0000173 // Emit inter-object padding for alignment.
Evan Chengd5a99d72006-09-14 07:35:00 +0000174 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattner3029f922006-02-09 04:46:04 +0000175 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000176 }
177}
178
Nate Begeman37efe672006-04-22 18:53:45 +0000179/// EmitJumpTableInfo - Print assembly representations of the jump tables used
180/// by the current function to the current output stream.
181///
182void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
183 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
184 if (JT.empty()) return;
Owen Andersona69571c2006-05-03 01:29:57 +0000185 const TargetData *TD = TM.getTargetData();
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000186
187 // JTEntryDirective is a string to print sizeof(ptr) for non-PIC jump tables,
188 // and 32 bits for PIC since PIC jump table entries are differences, not
189 // pointers to blocks.
Jim Laskey563321a2006-09-06 18:34:40 +0000190 const char *JTEntryDirective = TAI->getData32bitsDirective();
Nate Begeman37efe672006-04-22 18:53:45 +0000191
Nate Begeman2f1ae882006-07-27 01:13:04 +0000192 // Pick the directive to use to print the jump table entries, and switch to
193 // the appropriate section.
194 if (TM.getRelocationModel() == Reloc::PIC_) {
Jim Laskey563321a2006-09-06 18:34:40 +0000195 SwitchToTextSection(TAI->getJumpTableTextSection(), 0);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000196 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000197 SwitchToDataSection(TAI->getJumpTableDataSection(), 0);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000198 if (TD->getPointerSize() == 8)
Jim Laskey563321a2006-09-06 18:34:40 +0000199 JTEntryDirective = TAI->getData64bitsDirective();
Nate Begeman2f1ae882006-07-27 01:13:04 +0000200 }
Owen Andersona69571c2006-05-03 01:29:57 +0000201 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000202
Nate Begeman37efe672006-04-22 18:53:45 +0000203 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000204 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
205
206 // For PIC codegen, if possible we want to use the SetDirective to reduce
207 // the number of relocations the assembler will generate for the jump table.
208 // Set directives are all printed before the jump table itself.
209 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskey563321a2006-09-06 18:34:40 +0000210 if (TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_)
Nate Begeman52a51e382006-08-12 21:29:52 +0000211 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
212 if (EmittedSets.insert(JTBBs[ii]).second)
213 printSetLabel(i, JTBBs[ii]);
214
Jim Laskey563321a2006-09-06 18:34:40 +0000215 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
216 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000217
Nate Begeman37efe672006-04-22 18:53:45 +0000218 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000219 O << JTEntryDirective << ' ';
Nate Begeman52a51e382006-08-12 21:29:52 +0000220 // If we have emitted set directives for the jump table entries, print
221 // them rather than the entries themselves. If we're emitting PIC, then
222 // emit the table entries as differences between two text section labels.
223 // If we're emitting non-PIC code, then emit the entries as direct
224 // references to the target basic blocks.
225 if (!EmittedSets.empty()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000226 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
227 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Nate Begeman52a51e382006-08-12 21:29:52 +0000228 } else if (TM.getRelocationModel() == Reloc::PIC_) {
229 printBasicBlockLabel(JTBBs[ii], false, false);
Jim Laskey563321a2006-09-06 18:34:40 +0000230 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
231 << getFunctionNumber() << '_' << i;
Nate Begeman52a51e382006-08-12 21:29:52 +0000232 } else {
233 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman2f1ae882006-07-27 01:13:04 +0000234 }
Nate Begeman37efe672006-04-22 18:53:45 +0000235 O << '\n';
236 }
237 }
238}
239
Chris Lattnered138932005-12-13 06:32:10 +0000240/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
241/// special global used by LLVM. If so, emit it and return true, otherwise
242/// do nothing and return false.
243bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey78098112006-03-07 22:00:35 +0000244 // Ignore debug and non-emitted data.
245 if (GV->getSection() == "llvm.metadata") return true;
246
247 if (!GV->hasAppendingLinkage()) return false;
248
249 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000250
251 if (GV->getName() == "llvm.used")
252 return true; // No need to emit this at all.
253
Chris Lattner5166b822006-01-12 19:17:23 +0000254 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000255 SwitchToDataSection(TAI->getStaticCtorsSection(), 0);
Chris Lattnered138932005-12-13 06:32:10 +0000256 EmitAlignment(2, 0);
257 EmitXXStructorList(GV->getInitializer());
258 return true;
259 }
260
Chris Lattner5166b822006-01-12 19:17:23 +0000261 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000262 SwitchToDataSection(TAI->getStaticDtorsSection(), 0);
Chris Lattnered138932005-12-13 06:32:10 +0000263 EmitAlignment(2, 0);
264 EmitXXStructorList(GV->getInitializer());
265 return true;
266 }
267
268 return false;
269}
270
271/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
272/// function pointers, ignoring the init priority.
273void AsmPrinter::EmitXXStructorList(Constant *List) {
274 // Should be an array of '{ int, void ()* }' structs. The first value is the
275 // init priority, which we ignore.
276 if (!isa<ConstantArray>(List)) return;
277 ConstantArray *InitList = cast<ConstantArray>(List);
278 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
279 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
280 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000281
282 if (CS->getOperand(1)->isNullValue())
283 return; // Found a null terminator, exit printing.
284 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000285 EmitGlobalConstant(CS->getOperand(1));
286 }
287}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000288
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000289/// getPreferredAlignmentLog - Return the preferred alignment of the
290/// specified global, returned in log form. This includes an explicitly
291/// requested alignment (if the global has one).
292unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Jim Laskey35f8c202006-06-15 13:10:58 +0000293 const Type *ElemType = GV->getType()->getElementType();
294 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(ElemType);
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000295 if (GV->getAlignment() > (1U << Alignment))
296 Alignment = Log2_32(GV->getAlignment());
297
Chris Lattner519ea2a2006-02-05 01:46:49 +0000298 if (GV->hasInitializer()) {
Jim Laskeyd5a932b2006-06-15 19:37:14 +0000299 // Always round up alignment of global doubles to 8 bytes.
300 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
301 Alignment = 3;
Chris Lattner519ea2a2006-02-05 01:46:49 +0000302 if (Alignment < 4) {
303 // If the global is not external, see if it is large. If so, give it a
304 // larger alignment.
Jim Laskey35f8c202006-06-15 13:10:58 +0000305 if (TM.getTargetData()->getTypeSize(ElemType) > 128)
Chris Lattner519ea2a2006-02-05 01:46:49 +0000306 Alignment = 4; // 16-byte alignment.
307 }
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000308 }
309 return Alignment;
310}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000311
Chris Lattner25045bd2005-11-21 07:51:36 +0000312// EmitAlignment - Emit an alignment directive to the specified power of two.
313void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnera1ab72d2005-11-14 19:00:06 +0000314 if (GV && GV->getAlignment())
315 NumBits = Log2_32(GV->getAlignment());
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000316 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskey563321a2006-09-06 18:34:40 +0000317 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
318 O << TAI->getAlignDirective() << NumBits << "\n";
Chris Lattnerbfddc202004-08-17 19:14:29 +0000319}
320
Chris Lattner25045bd2005-11-21 07:51:36 +0000321/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000322///
Chris Lattner25045bd2005-11-21 07:51:36 +0000323void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000324 if (NumZeros) {
Jim Laskey563321a2006-09-06 18:34:40 +0000325 if (TAI->getZeroDirective()) {
326 O << TAI->getZeroDirective() << NumZeros;
327 if (TAI->getZeroDirectiveSuffix())
328 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000329 O << "\n";
330 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000331 for (; NumZeros; --NumZeros)
Jim Laskey563321a2006-09-06 18:34:40 +0000332 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000333 }
334 }
335}
336
Chris Lattnera80ba712004-08-16 23:15:22 +0000337// Print out the specified constant, without a storage class. Only the
338// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000339void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000340 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000341 O << "0";
342 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
343 assert(CB == ConstantBool::True);
344 O << "1";
345 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
346 if (((CI->getValue() << 32) >> 32) == CI->getValue())
347 O << CI->getValue();
348 else
Duraid Madina45606572005-05-15 13:05:48 +0000349 O << (uint64_t)CI->getValue();
Chris Lattnera80ba712004-08-16 23:15:22 +0000350 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
351 O << CI->getValue();
Chris Lattner450de392005-11-10 18:36:17 +0000352 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000353 // This is a constant address for a global variable or function. Use the
354 // name of the variable or function as the address value, possibly
355 // decorating it with GlobalVarAddrPrefix/Suffix or
356 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskey563321a2006-09-06 18:34:40 +0000357 if (isa<Function>(GV)) {
358 O << TAI->getFunctionAddrPrefix()
359 << Mang->getValueName(GV)
360 << TAI->getFunctionAddrSuffix();
361 } else {
362 O << TAI->getGlobalVarAddrPrefix()
363 << Mang->getValueName(GV)
364 << TAI->getGlobalVarAddrSuffix();
365 }
Duraid Madina855a5192005-04-02 12:21:51 +0000366 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000367 const TargetData *TD = TM.getTargetData();
Chris Lattnera80ba712004-08-16 23:15:22 +0000368 switch(CE->getOpcode()) {
369 case Instruction::GetElementPtr: {
370 // generate a symbolic expression for the byte address
371 const Constant *ptrVal = CE->getOperand(0);
372 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Andersona69571c2006-05-03 01:29:57 +0000373 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner27e19212005-02-14 21:40:26 +0000374 if (Offset)
375 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000376 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000377 if (Offset > 0)
378 O << ") + " << Offset;
379 else if (Offset < 0)
380 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000381 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000382 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000383 }
384 break;
385 }
386 case Instruction::Cast: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000387 // Support only foldable casts to/from pointers that can be eliminated by
388 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000389 Constant *Op = CE->getOperand(0);
390 const Type *OpTy = Op->getType(), *Ty = CE->getType();
391
Chris Lattner4a6bd332006-07-29 01:57:19 +0000392 // Handle casts to pointers by changing them into casts to the appropriate
393 // integer type. This promotes constant folding and simplifies this code.
394 if (isa<PointerType>(Ty)) {
395 const Type *IntPtrTy = TD->getIntPtrType();
396 Op = ConstantExpr::getCast(Op, IntPtrTy);
397 return EmitConstantValueOnly(Op);
398 }
399
400 // We know the dest type is not a pointer. Is the src value a pointer or
401 // integral?
402 if (isa<PointerType>(OpTy) || OpTy->isIntegral()) {
403 // We can emit the pointer value into this slot if the slot is an
404 // integer slot greater or equal to the size of the pointer.
405 if (Ty->isIntegral() && TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
406 return EmitConstantValueOnly(Op);
407 }
408
409 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000410 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000411 break;
412 }
413 case Instruction::Add:
414 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000415 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattnera80ba712004-08-16 23:15:22 +0000416 O << ") + (";
Chris Lattner25045bd2005-11-21 07:51:36 +0000417 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000418 O << ")";
419 break;
420 default:
421 assert(0 && "Unsupported operator!");
422 }
423 } else {
424 assert(0 && "Unknown constant value!");
425 }
426}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000427
428/// toOctal - Convert the low order bits of X into an octal digit.
429///
430static inline char toOctal(int X) {
431 return (X&7)+'0';
432}
433
Chris Lattner2980cef2005-11-10 18:06:33 +0000434/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000435/// the predicate isString is true.
436///
Chris Lattner2980cef2005-11-10 18:06:33 +0000437static void printAsCString(std::ostream &O, const ConstantArray *CVA,
438 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000439 assert(CVA->isString() && "Array is not string compatible!");
440
441 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000442 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000443 unsigned char C =
Chris Lattnerdea18b62005-01-08 19:59:10 +0000444 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000445
446 if (C == '"') {
447 O << "\\\"";
448 } else if (C == '\\') {
449 O << "\\\\";
450 } else if (isprint(C)) {
451 O << C;
452 } else {
453 switch(C) {
454 case '\b': O << "\\b"; break;
455 case '\f': O << "\\f"; break;
456 case '\n': O << "\\n"; break;
457 case '\r': O << "\\r"; break;
458 case '\t': O << "\\t"; break;
459 default:
460 O << '\\';
461 O << toOctal(C >> 6);
462 O << toOctal(C >> 3);
463 O << toOctal(C >> 0);
464 break;
465 }
466 }
467 }
468 O << "\"";
469}
470
Jeff Cohenc884db42006-05-02 01:16:28 +0000471/// EmitString - Emit a zero-byte-terminated string constant.
472///
473void AsmPrinter::EmitString(const ConstantArray *CVA) const {
474 unsigned NumElts = CVA->getNumOperands();
Jim Laskey563321a2006-09-06 18:34:40 +0000475 if (TAI->getAscizDirective() && NumElts &&
Jeff Cohenc884db42006-05-02 01:16:28 +0000476 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
Jim Laskey563321a2006-09-06 18:34:40 +0000477 O << TAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000478 printAsCString(O, CVA, NumElts-1);
479 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000480 O << TAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000481 printAsCString(O, CVA, NumElts);
482 }
483 O << "\n";
484}
485
Chris Lattner25045bd2005-11-21 07:51:36 +0000486/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000487///
Chris Lattner25045bd2005-11-21 07:51:36 +0000488void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Andersona69571c2006-05-03 01:29:57 +0000489 const TargetData *TD = TM.getTargetData();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000490
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000491 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000492 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000493 return;
494 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
495 if (CVA->isString()) {
Jeff Cohenc884db42006-05-02 01:16:28 +0000496 EmitString(CVA);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000497 } else { // Not a string. Print the values in successive locations
498 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000499 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000500 }
501 return;
502 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
503 // Print the fields in successive locations. Pad to align if needed!
Owen Andersona69571c2006-05-03 01:29:57 +0000504 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000505 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000506 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
507 const Constant* field = CVS->getOperand(i);
508
509 // Check if padding is needed and insert one or more 0s.
Owen Andersona69571c2006-05-03 01:29:57 +0000510 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000511 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner1b7e2352004-08-17 06:36:49 +0000512 : cvsLayout->MemberOffsets[i+1])
513 - cvsLayout->MemberOffsets[i]) - fieldSize;
514 sizeSoFar += fieldSize + padSize;
515
516 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000517 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000518
519 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000520 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000521 }
522 assert(sizeSoFar == cvsLayout->StructSize &&
523 "Layout of constant struct may be incorrect!");
524 return;
525 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
526 // FP Constants are printed as integer constants to avoid losing
527 // precision...
528 double Val = CFP->getValue();
529 if (CFP->getType() == Type::DoubleTy) {
Jim Laskey563321a2006-09-06 18:34:40 +0000530 if (TAI->getData64bitsDirective())
531 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
532 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000533 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000534 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
535 << "\t" << TAI->getCommentString()
536 << " double most significant word " << Val << "\n";
537 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
538 << "\t" << TAI->getCommentString()
539 << " double least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000540 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000541 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
542 << "\t" << TAI->getCommentString()
543 << " double least significant word " << Val << "\n";
544 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
545 << "\t" << TAI->getCommentString()
546 << " double most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000547 }
548 return;
549 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000550 O << TAI->getData32bitsDirective() << FloatToBits(Val)
551 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000552 return;
553 }
554 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
555 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
556 uint64_t Val = CI->getRawValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000557
Jim Laskey563321a2006-09-06 18:34:40 +0000558 if (TAI->getData64bitsDirective())
559 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000560 else if (TD->isBigEndian()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000561 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
562 << "\t" << TAI->getCommentString()
563 << " Double-word most significant word " << Val << "\n";
564 O << TAI->getData32bitsDirective() << unsigned(Val)
565 << "\t" << TAI->getCommentString()
566 << " Double-word least significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000567 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000568 O << TAI->getData32bitsDirective() << unsigned(Val)
569 << "\t" << TAI->getCommentString()
570 << " Double-word least significant word " << Val << "\n";
571 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
572 << "\t" << TAI->getCommentString()
573 << " Double-word most significant word " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000574 }
575 return;
576 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000577 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
578 const PackedType *PTy = CP->getType();
579
580 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
581 EmitGlobalConstant(CP->getOperand(I));
582
583 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000584 }
585
586 const Type *type = CV->getType();
Evan Chengd6594ae2006-09-12 21:00:35 +0000587 printDataDirective(type);
Chris Lattner25045bd2005-11-21 07:51:36 +0000588 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000589 O << "\n";
590}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000591
Evan Chengd6594ae2006-09-12 21:00:35 +0000592void
593AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
594 // Target doesn't support this yet!
595 abort();
596}
597
Chris Lattner0264d1a2006-01-27 02:10:10 +0000598/// printInlineAsm - This method formats and prints the specified machine
599/// instruction that is an inline asm.
600void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000601 unsigned NumOperands = MI->getNumOperands();
602
603 // Count the number of register definitions.
604 unsigned NumDefs = 0;
Chris Lattner67942f52006-09-05 20:02:51 +0000605 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
606 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000607 assert(NumDefs != NumOperands-1 && "No asm string?");
608
609 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +0000610
611 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000612 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +0000613
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000614 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
615 if (AsmStr[0] == 0) {
616 O << "\n"; // Tab already printed, avoid double indenting next instr.
617 return;
618 }
619
Jim Laskey563321a2006-09-06 18:34:40 +0000620 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +0000621
Chris Lattner66099132006-02-01 22:41:11 +0000622 // The variant of the current asmprinter: FIXME: change.
623 int AsmPrinterVariant = 0;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000624
Chris Lattner66099132006-02-01 22:41:11 +0000625 int CurVariant = -1; // The number of the {.|.|.} region we are in.
626 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +0000627
Chris Lattner66099132006-02-01 22:41:11 +0000628 while (*LastEmitted) {
629 switch (*LastEmitted) {
630 default: {
631 // Not a special case, emit the string section literally.
632 const char *LiteralEnd = LastEmitted+1;
633 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +0000634 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +0000635 ++LiteralEnd;
636 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
637 O.write(LastEmitted, LiteralEnd-LastEmitted);
638 LastEmitted = LiteralEnd;
639 break;
640 }
Chris Lattner1c059972006-05-05 21:47:05 +0000641 case '\n':
642 ++LastEmitted; // Consume newline character.
643 O << "\n\t"; // Indent code with newline.
644 break;
Chris Lattner66099132006-02-01 22:41:11 +0000645 case '$': {
646 ++LastEmitted; // Consume '$' character.
647 if (*LastEmitted == '$') { // $$ -> $
648 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
649 O << '$';
650 ++LastEmitted; // Consume second '$' character.
651 break;
652 }
653
654 bool HasCurlyBraces = false;
655 if (*LastEmitted == '{') { // ${variable}
656 ++LastEmitted; // Consume '{' character.
657 HasCurlyBraces = true;
658 }
659
660 const char *IDStart = LastEmitted;
661 char *IDEnd;
662 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
663 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
664 std::cerr << "Bad $ operand number in inline asm string: '"
665 << AsmStr << "'\n";
666 exit(1);
667 }
668 LastEmitted = IDEnd;
669
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000670 char Modifier[2] = { 0, 0 };
671
Chris Lattner66099132006-02-01 22:41:11 +0000672 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000673 // If we have curly braces, check for a modifier character. This
674 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
675 if (*LastEmitted == ':') {
676 ++LastEmitted; // Consume ':' character.
677 if (*LastEmitted == 0) {
678 std::cerr << "Bad ${:} expression in inline asm string: '"
679 << AsmStr << "'\n";
680 exit(1);
681 }
682
683 Modifier[0] = *LastEmitted;
684 ++LastEmitted; // Consume modifier character.
685 }
686
Chris Lattner66099132006-02-01 22:41:11 +0000687 if (*LastEmitted != '}') {
688 std::cerr << "Bad ${} expression in inline asm string: '"
689 << AsmStr << "'\n";
690 exit(1);
691 }
692 ++LastEmitted; // Consume '}' character.
693 }
694
695 if ((unsigned)Val >= NumOperands-1) {
696 std::cerr << "Invalid $ operand number in inline asm string: '"
697 << AsmStr << "'\n";
698 exit(1);
699 }
700
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000701 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +0000702 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000703 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
704 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000705
706 bool Error = false;
707
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000708 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000709 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000710 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000711 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
712 OpNo += (OpFlags >> 3) + 1;
713 }
Chris Lattnerdd260332006-02-24 20:21:58 +0000714
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000715 if (OpNo >= MI->getNumOperands()) {
716 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +0000717 } else {
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000718 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
719 ++OpNo; // Skip over the ID number.
720
721 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
722 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
723 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
724 Modifier[0] ? Modifier : 0);
725 } else {
726 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
727 Modifier[0] ? Modifier : 0);
728 }
Chris Lattnerdd260332006-02-24 20:21:58 +0000729 }
730 if (Error) {
Chris Lattner66099132006-02-01 22:41:11 +0000731 std::cerr << "Invalid operand found in inline asm: '"
732 << AsmStr << "'\n";
733 MI->dump();
734 exit(1);
735 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000736 }
Chris Lattner66099132006-02-01 22:41:11 +0000737 break;
738 }
739 case '{':
740 ++LastEmitted; // Consume '{' character.
741 if (CurVariant != -1) {
742 std::cerr << "Nested variants found in inline asm string: '"
743 << AsmStr << "'\n";
744 exit(1);
745 }
746 CurVariant = 0; // We're in the first variant now.
747 break;
748 case '|':
749 ++LastEmitted; // consume '|' character.
750 if (CurVariant == -1) {
751 std::cerr << "Found '|' character outside of variant in inline asm "
752 << "string: '" << AsmStr << "'\n";
753 exit(1);
754 }
755 ++CurVariant; // We're in the next variant.
756 break;
757 case '}':
758 ++LastEmitted; // consume '}' character.
759 if (CurVariant == -1) {
760 std::cerr << "Found '}' character outside of variant in inline asm "
761 << "string: '" << AsmStr << "'\n";
762 exit(1);
763 }
764 CurVariant = -1;
765 break;
766 }
767 }
Jim Laskey563321a2006-09-06 18:34:40 +0000768 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattner66099132006-02-01 22:41:11 +0000769}
770
771/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
772/// instruction, using the specified assembler variant. Targets should
773/// overried this to format as appropriate.
774bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000775 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +0000776 // Target doesn't support this yet!
777 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +0000778}
Chris Lattnerdd260332006-02-24 20:21:58 +0000779
780bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
781 unsigned AsmVariant,
782 const char *ExtraCode) {
783 // Target doesn't support this yet!
784 return true;
785}
Nate Begeman37efe672006-04-22 18:53:45 +0000786
787/// printBasicBlockLabel - This method prints the label for the specified
788/// MachineBasicBlock
Nate Begemancdf38c42006-05-02 05:37:32 +0000789void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
790 bool printColon,
791 bool printComment) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000792 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman9d51eeb2006-05-02 17:36:46 +0000793 << MBB->getNumber();
Nate Begemancdf38c42006-05-02 05:37:32 +0000794 if (printColon)
795 O << ':';
796 if (printComment)
Jim Laskey563321a2006-09-06 18:34:40 +0000797 O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
Nate Begeman37efe672006-04-22 18:53:45 +0000798}
Nate Begeman52a51e382006-08-12 21:29:52 +0000799
800/// printSetLabel - This method prints a set label for the specified
801/// MachineBasicBlock
802void AsmPrinter::printSetLabel(unsigned uid,
803 const MachineBasicBlock *MBB) const {
Jim Laskey563321a2006-09-06 18:34:40 +0000804 if (!TAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +0000805 return;
806
Jim Laskey563321a2006-09-06 18:34:40 +0000807 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
808 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman52a51e382006-08-12 21:29:52 +0000809 printBasicBlockLabel(MBB, false, false);
Jim Laskey563321a2006-09-06 18:34:40 +0000810 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman52a51e382006-08-12 21:29:52 +0000811 << '_' << uid << '\n';
812}
Evan Chengd6594ae2006-09-12 21:00:35 +0000813
814/// printDataDirective - This method prints the asm directive for the
815/// specified type.
816void AsmPrinter::printDataDirective(const Type *type) {
817 const TargetData *TD = TM.getTargetData();
818 switch (type->getTypeID()) {
819 case Type::BoolTyID:
820 case Type::UByteTyID: case Type::SByteTyID:
821 O << TAI->getData8bitsDirective();
822 break;
823 case Type::UShortTyID: case Type::ShortTyID:
824 O << TAI->getData16bitsDirective();
825 break;
826 case Type::PointerTyID:
827 if (TD->getPointerSize() == 8) {
828 assert(TAI->getData64bitsDirective() &&
829 "Target cannot handle 64-bit pointer exprs!");
830 O << TAI->getData64bitsDirective();
831 break;
832 }
833 //Fall through for pointer size == int size
834 case Type::UIntTyID: case Type::IntTyID:
835 O << TAI->getData32bitsDirective();
836 break;
837 case Type::ULongTyID: case Type::LongTyID:
838 assert(TAI->getData64bitsDirective() &&
839 "Target cannot handle 64-bit constant exprs!");
840 O << TAI->getData64bitsDirective();
841 break;
842 case Type::FloatTyID: case Type::DoubleTyID:
843 assert (0 && "Should have already output floating point constant.");
844 default:
845 assert (0 && "Can't handle printing this type of thing");
846 break;
847 }
848}