blob: db223dee179a3f774ddae19619d6682e85d04a5b [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
Nate Begeman8cfa57b2005-12-06 06:18:55 +000014#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000015#include "llvm/CodeGen/AsmPrinter.h"
16#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000017#include "llvm/Module.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000018#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000019#include "llvm/Support/Mangler.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000020#include "llvm/Support/MathExtras.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000021#include "llvm/Target/TargetMachine.h"
Duraid Madina2e096c12005-12-28 06:29:02 +000022#include <iostream>
Chris Lattner66099132006-02-01 22:41:11 +000023#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000024using namespace llvm;
25
Chris Lattnered138932005-12-13 06:32:10 +000026AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm)
27: FunctionNumber(0), O(o), TM(tm),
28 CommentString("#"),
29 GlobalPrefix(""),
30 PrivateGlobalPrefix("."),
31 GlobalVarAddrPrefix(""),
32 GlobalVarAddrSuffix(""),
33 FunctionAddrPrefix(""),
34 FunctionAddrSuffix(""),
35 ZeroDirective("\t.zero\t"),
36 AsciiDirective("\t.ascii\t"),
37 AscizDirective("\t.asciz\t"),
38 Data8bitsDirective("\t.byte\t"),
39 Data16bitsDirective("\t.short\t"),
40 Data32bitsDirective("\t.long\t"),
41 Data64bitsDirective("\t.quad\t"),
42 AlignDirective("\t.align\t"),
43 AlignmentIsInBytes(true),
44 SwitchToSectionDirective("\t.section\t"),
45 ConstantPoolSection("\t.section .rodata\n"),
46 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
47 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
48 LCOMMDirective(0),
49 COMMDirective("\t.comm\t"),
50 COMMDirectiveTakesAlignment(true),
51 HasDotTypeDotSizeDirective(true) {
52}
53
54
Chris Lattnerac28fbd2005-11-21 07:06:27 +000055/// SwitchSection - Switch to the specified section of the executable if we
56/// are not already in it!
57///
58void AsmPrinter::SwitchSection(const char *NewSection, const GlobalValue *GV) {
59 std::string NS;
60
61 if (GV && GV->hasSection())
Chris Lattner3b4fd322005-11-21 08:25:09 +000062 NS = SwitchToSectionDirective + GV->getSection();
Chris Lattnerac28fbd2005-11-21 07:06:27 +000063 else
Chris Lattner42a80fe2005-12-09 19:28:49 +000064 NS = std::string("\t")+NewSection;
Chris Lattnerac28fbd2005-11-21 07:06:27 +000065
66 if (CurrentSection != NS) {
67 CurrentSection = NS;
68 if (!CurrentSection.empty())
Chris Lattner42a80fe2005-12-09 19:28:49 +000069 O << CurrentSection << '\n';
Chris Lattnerac28fbd2005-11-21 07:06:27 +000070 }
71}
72
Chris Lattnera80ba712004-08-16 23:15:22 +000073bool AsmPrinter::doInitialization(Module &M) {
Chris Lattneraf2bf0a2004-08-17 06:06:19 +000074 Mang = new Mangler(M, GlobalPrefix);
Chris Lattner2c1b1592006-01-23 23:47:53 +000075
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000076 if (!M.getModuleInlineAsm().empty())
77 O << CommentString << " Start of file scope inline assembly\n"
78 << M.getModuleInlineAsm()
79 << "\n" << CommentString << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +000080
Chris Lattnerac28fbd2005-11-21 07:06:27 +000081 SwitchSection("", 0); // Reset back to no section.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000082
83 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
84 DebugInfo->AnalyzeModule(M);
85 }
86
Chris Lattnera80ba712004-08-16 23:15:22 +000087 return false;
88}
89
90bool AsmPrinter::doFinalization(Module &M) {
91 delete Mang; Mang = 0;
92 return false;
93}
94
Chris Lattner25045bd2005-11-21 07:51:36 +000095void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +000096 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +000097 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner77bc2282005-11-21 08:13:27 +000098 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +000099}
100
Chris Lattner3b4fd322005-11-21 08:25:09 +0000101/// EmitConstantPool - Print to the current output stream assembly
102/// representations of the constants in the constant pool MCP. This is
103/// used to print out constants which have been "spilled to memory" by
104/// the code generator.
105///
106void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Evan Chengb8973bd2006-01-31 22:23:14 +0000107 const std::vector<std::pair<Constant*, unsigned> > &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000108 if (CP.empty()) return;
109 const TargetData &TD = TM.getTargetData();
110
111 SwitchSection(ConstantPoolSection, 0);
112 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
113 // FIXME: force doubles to be naturally aligned. We should handle this
114 // more correctly in the future.
Evan Chengb8973bd2006-01-31 22:23:14 +0000115 unsigned Alignment = CP[i].second;
116 if (Alignment == 0) {
117 Alignment = TD.getTypeAlignmentShift(CP[i].first->getType());
118 if (CP[i].first->getType() == Type::DoubleTy && Alignment < 3)
119 Alignment = 3;
120 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000121
122 EmitAlignment(Alignment);
123 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
Evan Chengb8973bd2006-01-31 22:23:14 +0000124 << ":\t\t\t\t\t" << CommentString << *CP[i].first << '\n';
125 EmitGlobalConstant(CP[i].first);
Chris Lattner3b4fd322005-11-21 08:25:09 +0000126 }
127}
128
Chris Lattnered138932005-12-13 06:32:10 +0000129/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
130/// special global used by LLVM. If so, emit it and return true, otherwise
131/// do nothing and return false.
132bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
133 assert(GV->hasInitializer() && GV->hasAppendingLinkage() &&
134 "Not a special LLVM global!");
135
136 if (GV->getName() == "llvm.used")
137 return true; // No need to emit this at all.
138
Chris Lattner5166b822006-01-12 19:17:23 +0000139 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattnered138932005-12-13 06:32:10 +0000140 SwitchSection(StaticCtorsSection, 0);
141 EmitAlignment(2, 0);
142 EmitXXStructorList(GV->getInitializer());
143 return true;
144 }
145
Chris Lattner5166b822006-01-12 19:17:23 +0000146 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattnered138932005-12-13 06:32:10 +0000147 SwitchSection(StaticDtorsSection, 0);
148 EmitAlignment(2, 0);
149 EmitXXStructorList(GV->getInitializer());
150 return true;
151 }
152
153 return false;
154}
155
156/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
157/// function pointers, ignoring the init priority.
158void AsmPrinter::EmitXXStructorList(Constant *List) {
159 // Should be an array of '{ int, void ()* }' structs. The first value is the
160 // init priority, which we ignore.
161 if (!isa<ConstantArray>(List)) return;
162 ConstantArray *InitList = cast<ConstantArray>(List);
163 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
164 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
165 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000166
167 if (CS->getOperand(1)->isNullValue())
168 return; // Found a null terminator, exit printing.
169 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000170 EmitGlobalConstant(CS->getOperand(1));
171 }
172}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000173
174
Chris Lattner25045bd2005-11-21 07:51:36 +0000175// EmitAlignment - Emit an alignment directive to the specified power of two.
176void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnera1ab72d2005-11-14 19:00:06 +0000177 if (GV && GV->getAlignment())
178 NumBits = Log2_32(GV->getAlignment());
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000179 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattnerbfddc202004-08-17 19:14:29 +0000180 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
181 O << AlignDirective << NumBits << "\n";
182}
183
Chris Lattner25045bd2005-11-21 07:51:36 +0000184/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000185///
Chris Lattner25045bd2005-11-21 07:51:36 +0000186void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000187 if (NumZeros) {
188 if (ZeroDirective)
189 O << ZeroDirective << NumZeros << "\n";
190 else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000191 for (; NumZeros; --NumZeros)
192 O << Data8bitsDirective << "0\n";
193 }
194 }
195}
196
Chris Lattnera80ba712004-08-16 23:15:22 +0000197// Print out the specified constant, without a storage class. Only the
198// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000199void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000200 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000201 O << "0";
202 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
203 assert(CB == ConstantBool::True);
204 O << "1";
205 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
206 if (((CI->getValue() << 32) >> 32) == CI->getValue())
207 O << CI->getValue();
208 else
Duraid Madina45606572005-05-15 13:05:48 +0000209 O << (uint64_t)CI->getValue();
Chris Lattnera80ba712004-08-16 23:15:22 +0000210 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
211 O << CI->getValue();
Chris Lattner450de392005-11-10 18:36:17 +0000212 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000213 // This is a constant address for a global variable or function. Use the
214 // name of the variable or function as the address value, possibly
215 // decorating it with GlobalVarAddrPrefix/Suffix or
216 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattner450de392005-11-10 18:36:17 +0000217 if (isa<Function>(GV))
218 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000219 else
Chris Lattner450de392005-11-10 18:36:17 +0000220 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000221 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000222 const TargetData &TD = TM.getTargetData();
223 switch(CE->getOpcode()) {
224 case Instruction::GetElementPtr: {
225 // generate a symbolic expression for the byte address
226 const Constant *ptrVal = CE->getOperand(0);
227 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner27e19212005-02-14 21:40:26 +0000228 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
229 if (Offset)
230 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000231 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000232 if (Offset > 0)
233 O << ") + " << Offset;
234 else if (Offset < 0)
235 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000236 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000237 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000238 }
239 break;
240 }
241 case Instruction::Cast: {
242 // Support only non-converting or widening casts for now, that is, ones
243 // that do not involve a change in value. This assertion is really gross,
244 // and may not even be a complete check.
245 Constant *Op = CE->getOperand(0);
246 const Type *OpTy = Op->getType(), *Ty = CE->getType();
247
248 // Remember, kids, pointers can be losslessly converted back and forth
249 // into 32-bit or wider integers, regardless of signedness. :-P
250 assert(((isa<PointerType>(OpTy)
251 && (Ty == Type::LongTy || Ty == Type::ULongTy
252 || Ty == Type::IntTy || Ty == Type::UIntTy))
253 || (isa<PointerType>(Ty)
254 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
255 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
256 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
257 && OpTy->isLosslesslyConvertibleTo(Ty))))
258 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000259 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000260 break;
261 }
262 case Instruction::Add:
263 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000264 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattnera80ba712004-08-16 23:15:22 +0000265 O << ") + (";
Chris Lattner25045bd2005-11-21 07:51:36 +0000266 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000267 O << ")";
268 break;
269 default:
270 assert(0 && "Unsupported operator!");
271 }
272 } else {
273 assert(0 && "Unknown constant value!");
274 }
275}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000276
277/// toOctal - Convert the low order bits of X into an octal digit.
278///
279static inline char toOctal(int X) {
280 return (X&7)+'0';
281}
282
Chris Lattner2980cef2005-11-10 18:06:33 +0000283/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000284/// the predicate isString is true.
285///
Chris Lattner2980cef2005-11-10 18:06:33 +0000286static void printAsCString(std::ostream &O, const ConstantArray *CVA,
287 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000288 assert(CVA->isString() && "Array is not string compatible!");
289
290 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000291 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000292 unsigned char C =
Chris Lattnerdea18b62005-01-08 19:59:10 +0000293 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000294
295 if (C == '"') {
296 O << "\\\"";
297 } else if (C == '\\') {
298 O << "\\\\";
299 } else if (isprint(C)) {
300 O << C;
301 } else {
302 switch(C) {
303 case '\b': O << "\\b"; break;
304 case '\f': O << "\\f"; break;
305 case '\n': O << "\\n"; break;
306 case '\r': O << "\\r"; break;
307 case '\t': O << "\\t"; break;
308 default:
309 O << '\\';
310 O << toOctal(C >> 6);
311 O << toOctal(C >> 3);
312 O << toOctal(C >> 0);
313 break;
314 }
315 }
316 }
317 O << "\"";
318}
319
Chris Lattner25045bd2005-11-21 07:51:36 +0000320/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000321///
Chris Lattner25045bd2005-11-21 07:51:36 +0000322void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000323 const TargetData &TD = TM.getTargetData();
324
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000325 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattner25045bd2005-11-21 07:51:36 +0000326 EmitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000327 return;
328 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
329 if (CVA->isString()) {
Chris Lattner2980cef2005-11-10 18:06:33 +0000330 unsigned NumElts = CVA->getNumOperands();
331 if (AscizDirective && NumElts &&
332 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
333 O << AscizDirective;
334 printAsCString(O, CVA, NumElts-1);
335 } else {
336 O << AsciiDirective;
337 printAsCString(O, CVA, NumElts);
338 }
Chris Lattner1b7e2352004-08-17 06:36:49 +0000339 O << "\n";
340 } else { // Not a string. Print the values in successive locations
341 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000342 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000343 }
344 return;
345 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
346 // Print the fields in successive locations. Pad to align if needed!
347 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000348 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000349 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
350 const Constant* field = CVS->getOperand(i);
351
352 // Check if padding is needed and insert one or more 0s.
Chris Lattnerdea18b62005-01-08 19:59:10 +0000353 uint64_t fieldSize = TD.getTypeSize(field->getType());
354 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner1b7e2352004-08-17 06:36:49 +0000355 : cvsLayout->MemberOffsets[i+1])
356 - cvsLayout->MemberOffsets[i]) - fieldSize;
357 sizeSoFar += fieldSize + padSize;
358
359 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000360 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000361
362 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000363 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000364 }
365 assert(sizeSoFar == cvsLayout->StructSize &&
366 "Layout of constant struct may be incorrect!");
367 return;
368 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
369 // FP Constants are printed as integer constants to avoid losing
370 // precision...
371 double Val = CFP->getValue();
372 if (CFP->getType() == Type::DoubleTy) {
Chris Lattnere85a5a92004-08-17 06:48:16 +0000373 if (Data64bitsDirective)
Jim Laskeycb6682f2005-08-17 19:34:49 +0000374 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattner7d057a32004-08-17 21:38:40 +0000375 << " double value: " << Val << "\n";
Chris Lattnere85a5a92004-08-17 06:48:16 +0000376 else if (TD.isBigEndian()) {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000377 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000378 << "\t" << CommentString << " double most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000379 << Val << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000380 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000381 << "\t" << CommentString << " double least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000382 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000383 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000384 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000385 << "\t" << CommentString << " double least significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000386 << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000387 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000388 << "\t" << CommentString << " double most significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000389 << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000390 }
391 return;
392 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000393 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattner0554fb62004-08-17 16:27:05 +0000394 << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000395 return;
396 }
397 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
398 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
399 uint64_t Val = CI->getRawValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000400
Chris Lattnere85a5a92004-08-17 06:48:16 +0000401 if (Data64bitsDirective)
402 O << Data64bitsDirective << Val << "\n";
403 else if (TD.isBigEndian()) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000404 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000405 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000406 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000407 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000408 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000409 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000410 } else {
411 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000412 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000413 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000414 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000415 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000416 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000417 }
418 return;
419 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000420 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
421 const PackedType *PTy = CP->getType();
422
423 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
424 EmitGlobalConstant(CP->getOperand(I));
425
426 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000427 }
428
429 const Type *type = CV->getType();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000430 switch (type->getTypeID()) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000431 case Type::BoolTyID:
Chris Lattner1b7e2352004-08-17 06:36:49 +0000432 case Type::UByteTyID: case Type::SByteTyID:
433 O << Data8bitsDirective;
434 break;
435 case Type::UShortTyID: case Type::ShortTyID:
436 O << Data16bitsDirective;
437 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000438 case Type::PointerTyID:
Andrew Lenharth571c9c32005-02-04 13:47:16 +0000439 if (TD.getPointerSize() == 8) {
440 O << Data64bitsDirective;
441 break;
442 }
443 //Fall through for pointer size == int size
Chris Lattner1b7e2352004-08-17 06:36:49 +0000444 case Type::UIntTyID: case Type::IntTyID:
445 O << Data32bitsDirective;
446 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000447 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner660538c2005-08-08 04:26:32 +0000448 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
449 O << Data64bitsDirective;
450 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000451 case Type::FloatTyID: case Type::DoubleTyID:
452 assert (0 && "Should have already output floating point constant.");
453 default:
454 assert (0 && "Can't handle printing this type of thing");
455 break;
456 }
Chris Lattner25045bd2005-11-21 07:51:36 +0000457 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000458 O << "\n";
459}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000460
461/// printInlineAsm - This method formats and prints the specified machine
462/// instruction that is an inline asm.
463void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000464 unsigned NumOperands = MI->getNumOperands();
465
466 // Count the number of register definitions.
467 unsigned NumDefs = 0;
468 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
469 assert(NumDefs != NumOperands-1 && "No asm string?");
470
471 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +0000472
473 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000474 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +0000475
476 // The variant of the current asmprinter: FIXME: change.
477 int AsmPrinterVariant = 0;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000478
Chris Lattner66099132006-02-01 22:41:11 +0000479 int CurVariant = -1; // The number of the {.|.|.} region we are in.
480 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +0000481
Chris Lattner66099132006-02-01 22:41:11 +0000482 while (*LastEmitted) {
483 switch (*LastEmitted) {
484 default: {
485 // Not a special case, emit the string section literally.
486 const char *LiteralEnd = LastEmitted+1;
487 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
488 *LiteralEnd != '}' && *LiteralEnd != '$')
489 ++LiteralEnd;
490 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
491 O.write(LastEmitted, LiteralEnd-LastEmitted);
492 LastEmitted = LiteralEnd;
493 break;
494 }
495 case '$': {
496 ++LastEmitted; // Consume '$' character.
497 if (*LastEmitted == '$') { // $$ -> $
498 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
499 O << '$';
500 ++LastEmitted; // Consume second '$' character.
501 break;
502 }
503
504 bool HasCurlyBraces = false;
505 if (*LastEmitted == '{') { // ${variable}
506 ++LastEmitted; // Consume '{' character.
507 HasCurlyBraces = true;
508 }
509
510 const char *IDStart = LastEmitted;
511 char *IDEnd;
512 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
513 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
514 std::cerr << "Bad $ operand number in inline asm string: '"
515 << AsmStr << "'\n";
516 exit(1);
517 }
518 LastEmitted = IDEnd;
519
520 if (HasCurlyBraces) {
521 if (*LastEmitted != '}') {
522 std::cerr << "Bad ${} expression in inline asm string: '"
523 << AsmStr << "'\n";
524 exit(1);
525 }
526 ++LastEmitted; // Consume '}' character.
527 }
528
529 if ((unsigned)Val >= NumOperands-1) {
530 std::cerr << "Invalid $ operand number in inline asm string: '"
531 << AsmStr << "'\n";
532 exit(1);
533 }
534
535 // Okay, we finally have an operand number. Ask the target to print this
536 // operand!
537 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
538 if (const_cast<AsmPrinter*>(this)->
539 PrintAsmOperand(MI, Val+1, AsmPrinterVariant)) {
540 std::cerr << "Invalid operand found in inline asm: '"
541 << AsmStr << "'\n";
542 MI->dump();
543 exit(1);
544 }
545 break;
546 }
547 case '{':
548 ++LastEmitted; // Consume '{' character.
549 if (CurVariant != -1) {
550 std::cerr << "Nested variants found in inline asm string: '"
551 << AsmStr << "'\n";
552 exit(1);
553 }
554 CurVariant = 0; // We're in the first variant now.
555 break;
556 case '|':
557 ++LastEmitted; // consume '|' character.
558 if (CurVariant == -1) {
559 std::cerr << "Found '|' character outside of variant in inline asm "
560 << "string: '" << AsmStr << "'\n";
561 exit(1);
562 }
563 ++CurVariant; // We're in the next variant.
564 break;
565 case '}':
566 ++LastEmitted; // consume '}' character.
567 if (CurVariant == -1) {
568 std::cerr << "Found '}' character outside of variant in inline asm "
569 << "string: '" << AsmStr << "'\n";
570 exit(1);
571 }
572 CurVariant = -1;
573 break;
574 }
575 }
576 O << "\n";
577}
578
579/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
580/// instruction, using the specified assembler variant. Targets should
581/// overried this to format as appropriate.
582bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
583 unsigned AsmVariant) {
584 // Target doesn't support this yet!
585 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +0000586}