blob: 6fa6623550819049b5eb40fe0b4debe630503e3d [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
Nate Begeman41b1cdc2005-12-06 06:18:55 +000014#include "llvm/DerivedTypes.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000015#include "llvm/CodeGen/AsmPrinter.h"
16#include "llvm/Constants.h"
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000017#include "llvm/Module.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000018#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000019#include "llvm/Support/Mangler.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000020#include "llvm/Support/MathExtras.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000021#include "llvm/Target/TargetMachine.h"
Duraid Madina26b037e2005-12-28 06:29:02 +000022#include <iostream>
Chris Lattneraa23fa92006-02-01 22:41:11 +000023#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000024using namespace llvm;
25
Chris Lattnerf0e9aef2005-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 Lattner2ea5c992005-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 Lattnerf2991ce2005-11-21 08:25:09 +000062 NS = SwitchToSectionDirective + GV->getSection();
Chris Lattner2ea5c992005-11-21 07:06:27 +000063 else
Chris Lattnera6f835f2005-12-09 19:28:49 +000064 NS = std::string("\t")+NewSection;
Chris Lattner2ea5c992005-11-21 07:06:27 +000065
66 if (CurrentSection != NS) {
67 CurrentSection = NS;
68 if (!CurrentSection.empty())
Chris Lattnera6f835f2005-12-09 19:28:49 +000069 O << CurrentSection << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000070 }
71}
72
Chris Lattner6a8e0f52004-08-16 23:15:22 +000073bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner6d42cbd2004-08-17 06:06:19 +000074 Mang = new Mangler(M, GlobalPrefix);
Chris Lattnere3a79262006-01-23 23:47:53 +000075
Chris Lattner00fcdfe2006-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 Lattnere3a79262006-01-23 23:47:53 +000080
Chris Lattner2ea5c992005-11-21 07:06:27 +000081 SwitchSection("", 0); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +000082
83 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
84 DebugInfo->AnalyzeModule(M);
85 }
86
Chris Lattner6a8e0f52004-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 Lattnerbb644e32005-11-21 07:51:36 +000095void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +000096 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000097 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +000098 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +000099}
100
Chris Lattnerf2991ce2005-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 Cheng32be2dc2006-01-31 22:23:14 +0000107 const std::vector<std::pair<Constant*, unsigned> > &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-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 Cheng32be2dc2006-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 Lattnerf2991ce2005-11-21 08:25:09 +0000121
122 EmitAlignment(Alignment);
123 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
Evan Cheng32be2dc2006-01-31 22:23:14 +0000124 << ":\t\t\t\t\t" << CommentString << *CP[i].first << '\n';
125 EmitGlobalConstant(CP[i].first);
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000126 }
127}
128
Chris Lattnerf0e9aef2005-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 Lattner3760e902006-01-12 19:17:23 +0000139 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000140 SwitchSection(StaticCtorsSection, 0);
141 EmitAlignment(2, 0);
142 EmitXXStructorList(GV->getInitializer());
143 return true;
144 }
145
Chris Lattner3760e902006-01-12 19:17:23 +0000146 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattnerf0e9aef2005-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 Lattner434ffe42005-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 Lattnerf0e9aef2005-12-13 06:32:10 +0000170 EmitGlobalConstant(CS->getOperand(1));
171 }
172}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000173
Chris Lattnera9b25252006-02-05 01:29:18 +0000174/// getPreferredAlignmentLog - Return the preferred alignment of the
175/// specified global, returned in log form. This includes an explicitly
176/// requested alignment (if the global has one).
177unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
178 unsigned Alignment = TM.getTargetData().getTypeAlignmentShift(GV->getType());
179 if (GV->getAlignment() > (1U << Alignment))
180 Alignment = Log2_32(GV->getAlignment());
181
Chris Lattnercbab2842006-02-05 01:46:49 +0000182 if (GV->hasInitializer()) {
183 // Always round up alignment of global doubles to 8 bytes.
184 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
185 Alignment = 3;
186 if (Alignment < 4) {
187 // If the global is not external, see if it is large. If so, give it a
188 // larger alignment.
189 if (TM.getTargetData().getTypeSize(GV->getType()->getElementType()) > 128)
190 Alignment = 4; // 16-byte alignment.
191 }
Chris Lattnera9b25252006-02-05 01:29:18 +0000192 }
193 return Alignment;
194}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000195
Chris Lattnerbb644e32005-11-21 07:51:36 +0000196// EmitAlignment - Emit an alignment directive to the specified power of two.
197void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000198 if (GV && GV->getAlignment())
199 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000200 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner1d35c162004-08-17 19:14:29 +0000201 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
202 O << AlignDirective << NumBits << "\n";
203}
204
Chris Lattnerbb644e32005-11-21 07:51:36 +0000205/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000206///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000207void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000208 if (NumZeros) {
209 if (ZeroDirective)
210 O << ZeroDirective << NumZeros << "\n";
211 else {
Chris Lattnerea751992004-08-17 21:38:40 +0000212 for (; NumZeros; --NumZeros)
213 O << Data8bitsDirective << "0\n";
214 }
215 }
216}
217
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000218// Print out the specified constant, without a storage class. Only the
219// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000220void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000221 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000222 O << "0";
223 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
224 assert(CB == ConstantBool::True);
225 O << "1";
226 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
227 if (((CI->getValue() << 32) >> 32) == CI->getValue())
228 O << CI->getValue();
229 else
Duraid Madina73c4dba2005-05-15 13:05:48 +0000230 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000231 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
232 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000233 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000234 // This is a constant address for a global variable or function. Use the
235 // name of the variable or function as the address value, possibly
236 // decorating it with GlobalVarAddrPrefix/Suffix or
237 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000238 if (isa<Function>(GV))
239 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000240 else
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000241 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000242 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000243 const TargetData &TD = TM.getTargetData();
244 switch(CE->getOpcode()) {
245 case Instruction::GetElementPtr: {
246 // generate a symbolic expression for the byte address
247 const Constant *ptrVal = CE->getOperand(0);
248 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner145569b2005-02-14 21:40:26 +0000249 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
250 if (Offset)
251 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000252 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000253 if (Offset > 0)
254 O << ") + " << Offset;
255 else if (Offset < 0)
256 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000257 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000258 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000259 }
260 break;
261 }
262 case Instruction::Cast: {
263 // Support only non-converting or widening casts for now, that is, ones
264 // that do not involve a change in value. This assertion is really gross,
265 // and may not even be a complete check.
266 Constant *Op = CE->getOperand(0);
267 const Type *OpTy = Op->getType(), *Ty = CE->getType();
268
269 // Remember, kids, pointers can be losslessly converted back and forth
270 // into 32-bit or wider integers, regardless of signedness. :-P
271 assert(((isa<PointerType>(OpTy)
272 && (Ty == Type::LongTy || Ty == Type::ULongTy
273 || Ty == Type::IntTy || Ty == Type::UIntTy))
274 || (isa<PointerType>(Ty)
275 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
276 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
277 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
278 && OpTy->isLosslesslyConvertibleTo(Ty))))
279 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000280 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000281 break;
282 }
283 case Instruction::Add:
284 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000285 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000286 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000287 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000288 O << ")";
289 break;
290 default:
291 assert(0 && "Unsupported operator!");
292 }
293 } else {
294 assert(0 && "Unknown constant value!");
295 }
296}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000297
298/// toOctal - Convert the low order bits of X into an octal digit.
299///
300static inline char toOctal(int X) {
301 return (X&7)+'0';
302}
303
Chris Lattner55a6d902005-11-10 18:06:33 +0000304/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000305/// the predicate isString is true.
306///
Chris Lattner55a6d902005-11-10 18:06:33 +0000307static void printAsCString(std::ostream &O, const ConstantArray *CVA,
308 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000309 assert(CVA->isString() && "Array is not string compatible!");
310
311 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000312 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000313 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000314 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000315
316 if (C == '"') {
317 O << "\\\"";
318 } else if (C == '\\') {
319 O << "\\\\";
320 } else if (isprint(C)) {
321 O << C;
322 } else {
323 switch(C) {
324 case '\b': O << "\\b"; break;
325 case '\f': O << "\\f"; break;
326 case '\n': O << "\\n"; break;
327 case '\r': O << "\\r"; break;
328 case '\t': O << "\\t"; break;
329 default:
330 O << '\\';
331 O << toOctal(C >> 6);
332 O << toOctal(C >> 3);
333 O << toOctal(C >> 0);
334 break;
335 }
336 }
337 }
338 O << "\"";
339}
340
Chris Lattnerbb644e32005-11-21 07:51:36 +0000341/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000342///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000343void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000344 const TargetData &TD = TM.getTargetData();
345
Chris Lattner61753bf2004-10-16 18:19:26 +0000346 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000347 EmitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000348 return;
349 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
350 if (CVA->isString()) {
Chris Lattner55a6d902005-11-10 18:06:33 +0000351 unsigned NumElts = CVA->getNumOperands();
352 if (AscizDirective && NumElts &&
353 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
354 O << AscizDirective;
355 printAsCString(O, CVA, NumElts-1);
356 } else {
357 O << AsciiDirective;
358 printAsCString(O, CVA, NumElts);
359 }
Chris Lattner8452a1f2004-08-17 06:36:49 +0000360 O << "\n";
361 } else { // Not a string. Print the values in successive locations
362 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000363 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000364 }
365 return;
366 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
367 // Print the fields in successive locations. Pad to align if needed!
368 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000369 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000370 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
371 const Constant* field = CVS->getOperand(i);
372
373 // Check if padding is needed and insert one or more 0s.
Chris Lattner0b955fd2005-01-08 19:59:10 +0000374 uint64_t fieldSize = TD.getTypeSize(field->getType());
375 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000376 : cvsLayout->MemberOffsets[i+1])
377 - cvsLayout->MemberOffsets[i]) - fieldSize;
378 sizeSoFar += fieldSize + padSize;
379
380 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000381 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000382
383 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000384 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000385 }
386 assert(sizeSoFar == cvsLayout->StructSize &&
387 "Layout of constant struct may be incorrect!");
388 return;
389 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
390 // FP Constants are printed as integer constants to avoid losing
391 // precision...
392 double Val = CFP->getValue();
393 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000394 if (Data64bitsDirective)
Jim Laskeyb74c6662005-08-17 19:34:49 +0000395 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000396 << " double value: " << Val << "\n";
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000397 else if (TD.isBigEndian()) {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000398 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000399 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000400 << Val << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000401 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000402 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000403 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000404 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000405 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000406 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000407 << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000408 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000409 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000410 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000411 }
412 return;
413 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000414 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000415 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000416 return;
417 }
418 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
419 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
420 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000421
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000422 if (Data64bitsDirective)
423 O << Data64bitsDirective << Val << "\n";
424 else if (TD.isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000425 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000426 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000427 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000428 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000429 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000430 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000431 } else {
432 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000433 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000434 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000435 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000436 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000437 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000438 }
439 return;
440 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000441 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
442 const PackedType *PTy = CP->getType();
443
444 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
445 EmitGlobalConstant(CP->getOperand(I));
446
447 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000448 }
449
450 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000451 switch (type->getTypeID()) {
Misha Brukman835702a2005-04-21 22:36:52 +0000452 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000453 case Type::UByteTyID: case Type::SByteTyID:
454 O << Data8bitsDirective;
455 break;
456 case Type::UShortTyID: case Type::ShortTyID:
457 O << Data16bitsDirective;
458 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000459 case Type::PointerTyID:
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000460 if (TD.getPointerSize() == 8) {
461 O << Data64bitsDirective;
462 break;
463 }
464 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000465 case Type::UIntTyID: case Type::IntTyID:
466 O << Data32bitsDirective;
467 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000468 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner88e2d2e2005-08-08 04:26:32 +0000469 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
470 O << Data64bitsDirective;
471 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000472 case Type::FloatTyID: case Type::DoubleTyID:
473 assert (0 && "Should have already output floating point constant.");
474 default:
475 assert (0 && "Can't handle printing this type of thing");
476 break;
477 }
Chris Lattnerbb644e32005-11-21 07:51:36 +0000478 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000479 O << "\n";
480}
Chris Lattner061d9e22006-01-27 02:10:10 +0000481
482/// printInlineAsm - This method formats and prints the specified machine
483/// instruction that is an inline asm.
484void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +0000485 unsigned NumOperands = MI->getNumOperands();
486
487 // Count the number of register definitions.
488 unsigned NumDefs = 0;
489 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
490 assert(NumDefs != NumOperands-1 && "No asm string?");
491
492 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000493
494 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000495 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000496
497 // The variant of the current asmprinter: FIXME: change.
498 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000499
Chris Lattneraa23fa92006-02-01 22:41:11 +0000500 int CurVariant = -1; // The number of the {.|.|.} region we are in.
501 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000502
Chris Lattneraa23fa92006-02-01 22:41:11 +0000503 while (*LastEmitted) {
504 switch (*LastEmitted) {
505 default: {
506 // Not a special case, emit the string section literally.
507 const char *LiteralEnd = LastEmitted+1;
508 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
509 *LiteralEnd != '}' && *LiteralEnd != '$')
510 ++LiteralEnd;
511 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
512 O.write(LastEmitted, LiteralEnd-LastEmitted);
513 LastEmitted = LiteralEnd;
514 break;
515 }
516 case '$': {
517 ++LastEmitted; // Consume '$' character.
518 if (*LastEmitted == '$') { // $$ -> $
519 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
520 O << '$';
521 ++LastEmitted; // Consume second '$' character.
522 break;
523 }
524
525 bool HasCurlyBraces = false;
526 if (*LastEmitted == '{') { // ${variable}
527 ++LastEmitted; // Consume '{' character.
528 HasCurlyBraces = true;
529 }
530
531 const char *IDStart = LastEmitted;
532 char *IDEnd;
533 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
534 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
535 std::cerr << "Bad $ operand number in inline asm string: '"
536 << AsmStr << "'\n";
537 exit(1);
538 }
539 LastEmitted = IDEnd;
540
Chris Lattner34f74c12006-02-06 22:17:23 +0000541 char Modifier[2] = { 0, 0 };
542
Chris Lattneraa23fa92006-02-01 22:41:11 +0000543 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000544 // If we have curly braces, check for a modifier character. This
545 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
546 if (*LastEmitted == ':') {
547 ++LastEmitted; // Consume ':' character.
548 if (*LastEmitted == 0) {
549 std::cerr << "Bad ${:} expression in inline asm string: '"
550 << AsmStr << "'\n";
551 exit(1);
552 }
553
554 Modifier[0] = *LastEmitted;
555 ++LastEmitted; // Consume modifier character.
556 }
557
Chris Lattneraa23fa92006-02-01 22:41:11 +0000558 if (*LastEmitted != '}') {
559 std::cerr << "Bad ${} expression in inline asm string: '"
560 << AsmStr << "'\n";
561 exit(1);
562 }
563 ++LastEmitted; // Consume '}' character.
564 }
565
566 if ((unsigned)Val >= NumOperands-1) {
567 std::cerr << "Invalid $ operand number in inline asm string: '"
568 << AsmStr << "'\n";
569 exit(1);
570 }
571
Chris Lattner34f74c12006-02-06 22:17:23 +0000572 char ExtraCode = 0; // FIXME:
573
Chris Lattneraa23fa92006-02-01 22:41:11 +0000574 // Okay, we finally have an operand number. Ask the target to print this
575 // operand!
576 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
577 if (const_cast<AsmPrinter*>(this)->
Chris Lattner34f74c12006-02-06 22:17:23 +0000578 PrintAsmOperand(MI, Val+1, AsmPrinterVariant,
579 Modifier[0] ? Modifier : 0)) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000580 std::cerr << "Invalid operand found in inline asm: '"
581 << AsmStr << "'\n";
582 MI->dump();
583 exit(1);
584 }
585 break;
586 }
587 case '{':
588 ++LastEmitted; // Consume '{' character.
589 if (CurVariant != -1) {
590 std::cerr << "Nested variants found in inline asm string: '"
591 << AsmStr << "'\n";
592 exit(1);
593 }
594 CurVariant = 0; // We're in the first variant now.
595 break;
596 case '|':
597 ++LastEmitted; // consume '|' character.
598 if (CurVariant == -1) {
599 std::cerr << "Found '|' character outside of variant in inline asm "
600 << "string: '" << AsmStr << "'\n";
601 exit(1);
602 }
603 ++CurVariant; // We're in the next variant.
604 break;
605 case '}':
606 ++LastEmitted; // consume '}' character.
607 if (CurVariant == -1) {
608 std::cerr << "Found '}' character outside of variant in inline asm "
609 << "string: '" << AsmStr << "'\n";
610 exit(1);
611 }
612 CurVariant = -1;
613 break;
614 }
615 }
616 O << "\n";
617}
618
619/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
620/// instruction, using the specified assembler variant. Targets should
621/// overried this to format as appropriate.
622bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000623 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000624 // Target doesn't support this yet!
625 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000626}