blob: dfac3c07788422f0e167e31da56ef4eb50d24c2c [file] [log] [blame]
Chris Lattner6a8e0f52004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng993e9cf2006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng38d5e762006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman41b1cdc2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000020#include "llvm/Support/Mangler.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000021#include "llvm/Support/MathExtras.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000022#include "llvm/Target/TargetMachine.h"
Duraid Madina26b037e2005-12-28 06:29:02 +000023#include <iostream>
Chris Lattneraa23fa92006-02-01 22:41:11 +000024#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000025using namespace llvm;
26
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000027AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm)
28: FunctionNumber(0), O(o), TM(tm),
29 CommentString("#"),
30 GlobalPrefix(""),
31 PrivateGlobalPrefix("."),
32 GlobalVarAddrPrefix(""),
33 GlobalVarAddrSuffix(""),
34 FunctionAddrPrefix(""),
35 FunctionAddrSuffix(""),
Chris Lattnered87dcd2006-02-08 23:41:56 +000036 InlineAsmStart("#APP\n"),
37 InlineAsmEnd("#NO_APP\n"),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000038 ZeroDirective("\t.zero\t"),
39 AsciiDirective("\t.ascii\t"),
40 AscizDirective("\t.asciz\t"),
41 Data8bitsDirective("\t.byte\t"),
42 Data16bitsDirective("\t.short\t"),
43 Data32bitsDirective("\t.long\t"),
44 Data64bitsDirective("\t.quad\t"),
45 AlignDirective("\t.align\t"),
46 AlignmentIsInBytes(true),
47 SwitchToSectionDirective("\t.section\t"),
48 ConstantPoolSection("\t.section .rodata\n"),
49 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
50 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
51 LCOMMDirective(0),
52 COMMDirective("\t.comm\t"),
53 COMMDirectiveTakesAlignment(true),
54 HasDotTypeDotSizeDirective(true) {
55}
56
57
Chris Lattner2ea5c992005-11-21 07:06:27 +000058/// SwitchSection - Switch to the specified section of the executable if we
59/// are not already in it!
60///
61void AsmPrinter::SwitchSection(const char *NewSection, const GlobalValue *GV) {
62 std::string NS;
63
64 if (GV && GV->hasSection())
Chris Lattnerf2991ce2005-11-21 08:25:09 +000065 NS = SwitchToSectionDirective + GV->getSection();
Chris Lattner2ea5c992005-11-21 07:06:27 +000066 else
Chris Lattnera6f835f2005-12-09 19:28:49 +000067 NS = std::string("\t")+NewSection;
Chris Lattner2ea5c992005-11-21 07:06:27 +000068
69 if (CurrentSection != NS) {
70 CurrentSection = NS;
71 if (!CurrentSection.empty())
Chris Lattnera6f835f2005-12-09 19:28:49 +000072 O << CurrentSection << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000073 }
74}
75
Chris Lattner6a8e0f52004-08-16 23:15:22 +000076bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner6d42cbd2004-08-17 06:06:19 +000077 Mang = new Mangler(M, GlobalPrefix);
Chris Lattnere3a79262006-01-23 23:47:53 +000078
Chris Lattner00fcdfe2006-01-24 04:16:34 +000079 if (!M.getModuleInlineAsm().empty())
80 O << CommentString << " Start of file scope inline assembly\n"
81 << M.getModuleInlineAsm()
82 << "\n" << CommentString << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +000083
Chris Lattner2ea5c992005-11-21 07:06:27 +000084 SwitchSection("", 0); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +000085
86 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
87 DebugInfo->AnalyzeModule(M);
88 }
89
Chris Lattner6a8e0f52004-08-16 23:15:22 +000090 return false;
91}
92
93bool AsmPrinter::doFinalization(Module &M) {
94 delete Mang; Mang = 0;
95 return false;
96}
97
Chris Lattnerbb644e32005-11-21 07:51:36 +000098void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +000099 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000100 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000101 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000102}
103
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000104/// EmitConstantPool - Print to the current output stream assembly
105/// representations of the constants in the constant pool MCP. This is
106/// used to print out constants which have been "spilled to memory" by
107/// the code generator.
108///
109void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000110 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000111 if (CP.empty()) return;
112 const TargetData &TD = TM.getTargetData();
113
114 SwitchSection(ConstantPoolSection, 0);
Chris Lattnerf6190822006-02-09 04:46:04 +0000115 EmitAlignment(MCP->getConstantPoolAlignment());
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000116 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000117 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
Evan Cheng38d5e762006-03-01 22:18:09 +0000118 << ":\t\t\t\t\t" << CommentString << " ";
119 WriteTypeSymbolic(O, CP[i].Val->getType(), 0) << '\n';
Chris Lattnerba972642006-02-09 04:22:52 +0000120 EmitGlobalConstant(CP[i].Val);
Chris Lattnerf6190822006-02-09 04:46:04 +0000121 if (i != e-1) {
122 unsigned EntSize = TM.getTargetData().getTypeSize(CP[i].Val->getType());
123 unsigned ValEnd = CP[i].Offset + EntSize;
124 // Emit inter-object padding for alignment.
125 EmitZeros(CP[i+1].Offset-ValEnd);
126 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000127 }
128}
129
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000130/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
131/// special global used by LLVM. If so, emit it and return true, otherwise
132/// do nothing and return false.
133bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
134 assert(GV->hasInitializer() && GV->hasAppendingLinkage() &&
135 "Not a special LLVM global!");
136
137 if (GV->getName() == "llvm.used")
138 return true; // No need to emit this at all.
139
Chris Lattner3760e902006-01-12 19:17:23 +0000140 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000141 SwitchSection(StaticCtorsSection, 0);
142 EmitAlignment(2, 0);
143 EmitXXStructorList(GV->getInitializer());
144 return true;
145 }
146
Chris Lattner3760e902006-01-12 19:17:23 +0000147 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000148 SwitchSection(StaticDtorsSection, 0);
149 EmitAlignment(2, 0);
150 EmitXXStructorList(GV->getInitializer());
151 return true;
152 }
153
154 return false;
155}
156
157/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
158/// function pointers, ignoring the init priority.
159void AsmPrinter::EmitXXStructorList(Constant *List) {
160 // Should be an array of '{ int, void ()* }' structs. The first value is the
161 // init priority, which we ignore.
162 if (!isa<ConstantArray>(List)) return;
163 ConstantArray *InitList = cast<ConstantArray>(List);
164 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
165 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
166 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000167
168 if (CS->getOperand(1)->isNullValue())
169 return; // Found a null terminator, exit printing.
170 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000171 EmitGlobalConstant(CS->getOperand(1));
172 }
173}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000174
Chris Lattnera9b25252006-02-05 01:29:18 +0000175/// getPreferredAlignmentLog - Return the preferred alignment of the
176/// specified global, returned in log form. This includes an explicitly
177/// requested alignment (if the global has one).
178unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
179 unsigned Alignment = TM.getTargetData().getTypeAlignmentShift(GV->getType());
180 if (GV->getAlignment() > (1U << Alignment))
181 Alignment = Log2_32(GV->getAlignment());
182
Chris Lattnercbab2842006-02-05 01:46:49 +0000183 if (GV->hasInitializer()) {
184 // Always round up alignment of global doubles to 8 bytes.
185 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
186 Alignment = 3;
187 if (Alignment < 4) {
188 // If the global is not external, see if it is large. If so, give it a
189 // larger alignment.
190 if (TM.getTargetData().getTypeSize(GV->getType()->getElementType()) > 128)
191 Alignment = 4; // 16-byte alignment.
192 }
Chris Lattnera9b25252006-02-05 01:29:18 +0000193 }
194 return Alignment;
195}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000196
Chris Lattnerbb644e32005-11-21 07:51:36 +0000197// EmitAlignment - Emit an alignment directive to the specified power of two.
198void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000199 if (GV && GV->getAlignment())
200 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000201 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner1d35c162004-08-17 19:14:29 +0000202 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
203 O << AlignDirective << NumBits << "\n";
204}
205
Chris Lattnerbb644e32005-11-21 07:51:36 +0000206/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000207///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000208void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000209 if (NumZeros) {
210 if (ZeroDirective)
211 O << ZeroDirective << NumZeros << "\n";
212 else {
Chris Lattnerea751992004-08-17 21:38:40 +0000213 for (; NumZeros; --NumZeros)
214 O << Data8bitsDirective << "0\n";
215 }
216 }
217}
218
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000219// Print out the specified constant, without a storage class. Only the
220// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000221void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000222 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000223 O << "0";
224 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
225 assert(CB == ConstantBool::True);
226 O << "1";
227 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
228 if (((CI->getValue() << 32) >> 32) == CI->getValue())
229 O << CI->getValue();
230 else
Duraid Madina73c4dba2005-05-15 13:05:48 +0000231 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000232 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
233 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000234 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000235 // This is a constant address for a global variable or function. Use the
236 // name of the variable or function as the address value, possibly
237 // decorating it with GlobalVarAddrPrefix/Suffix or
238 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000239 if (isa<Function>(GV))
240 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000241 else
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000242 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000243 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000244 const TargetData &TD = TM.getTargetData();
245 switch(CE->getOpcode()) {
246 case Instruction::GetElementPtr: {
247 // generate a symbolic expression for the byte address
248 const Constant *ptrVal = CE->getOperand(0);
249 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner145569b2005-02-14 21:40:26 +0000250 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
251 if (Offset)
252 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000253 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000254 if (Offset > 0)
255 O << ") + " << Offset;
256 else if (Offset < 0)
257 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000258 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000259 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000260 }
261 break;
262 }
263 case Instruction::Cast: {
264 // Support only non-converting or widening casts for now, that is, ones
265 // that do not involve a change in value. This assertion is really gross,
266 // and may not even be a complete check.
267 Constant *Op = CE->getOperand(0);
268 const Type *OpTy = Op->getType(), *Ty = CE->getType();
269
270 // Remember, kids, pointers can be losslessly converted back and forth
271 // into 32-bit or wider integers, regardless of signedness. :-P
272 assert(((isa<PointerType>(OpTy)
273 && (Ty == Type::LongTy || Ty == Type::ULongTy
274 || Ty == Type::IntTy || Ty == Type::UIntTy))
275 || (isa<PointerType>(Ty)
276 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
277 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
278 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
279 && OpTy->isLosslesslyConvertibleTo(Ty))))
280 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000281 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000282 break;
283 }
284 case Instruction::Add:
285 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000286 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000287 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000288 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000289 O << ")";
290 break;
291 default:
292 assert(0 && "Unsupported operator!");
293 }
294 } else {
295 assert(0 && "Unknown constant value!");
296 }
297}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000298
299/// toOctal - Convert the low order bits of X into an octal digit.
300///
301static inline char toOctal(int X) {
302 return (X&7)+'0';
303}
304
Chris Lattner55a6d902005-11-10 18:06:33 +0000305/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000306/// the predicate isString is true.
307///
Chris Lattner55a6d902005-11-10 18:06:33 +0000308static void printAsCString(std::ostream &O, const ConstantArray *CVA,
309 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000310 assert(CVA->isString() && "Array is not string compatible!");
311
312 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000313 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000314 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000315 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000316
317 if (C == '"') {
318 O << "\\\"";
319 } else if (C == '\\') {
320 O << "\\\\";
321 } else if (isprint(C)) {
322 O << C;
323 } else {
324 switch(C) {
325 case '\b': O << "\\b"; break;
326 case '\f': O << "\\f"; break;
327 case '\n': O << "\\n"; break;
328 case '\r': O << "\\r"; break;
329 case '\t': O << "\\t"; break;
330 default:
331 O << '\\';
332 O << toOctal(C >> 6);
333 O << toOctal(C >> 3);
334 O << toOctal(C >> 0);
335 break;
336 }
337 }
338 }
339 O << "\"";
340}
341
Chris Lattnerbb644e32005-11-21 07:51:36 +0000342/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000343///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000344void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000345 const TargetData &TD = TM.getTargetData();
346
Chris Lattner61753bf2004-10-16 18:19:26 +0000347 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000348 EmitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000349 return;
350 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
351 if (CVA->isString()) {
Chris Lattner55a6d902005-11-10 18:06:33 +0000352 unsigned NumElts = CVA->getNumOperands();
353 if (AscizDirective && NumElts &&
354 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
355 O << AscizDirective;
356 printAsCString(O, CVA, NumElts-1);
357 } else {
358 O << AsciiDirective;
359 printAsCString(O, CVA, NumElts);
360 }
Chris Lattner8452a1f2004-08-17 06:36:49 +0000361 O << "\n";
362 } else { // Not a string. Print the values in successive locations
363 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000364 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000365 }
366 return;
367 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
368 // Print the fields in successive locations. Pad to align if needed!
369 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000370 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000371 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
372 const Constant* field = CVS->getOperand(i);
373
374 // Check if padding is needed and insert one or more 0s.
Chris Lattner0b955fd2005-01-08 19:59:10 +0000375 uint64_t fieldSize = TD.getTypeSize(field->getType());
376 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000377 : cvsLayout->MemberOffsets[i+1])
378 - cvsLayout->MemberOffsets[i]) - fieldSize;
379 sizeSoFar += fieldSize + padSize;
380
381 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000382 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000383
384 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000385 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000386 }
387 assert(sizeSoFar == cvsLayout->StructSize &&
388 "Layout of constant struct may be incorrect!");
389 return;
390 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
391 // FP Constants are printed as integer constants to avoid losing
392 // precision...
393 double Val = CFP->getValue();
394 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000395 if (Data64bitsDirective)
Jim Laskeyb74c6662005-08-17 19:34:49 +0000396 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000397 << " double value: " << Val << "\n";
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000398 else if (TD.isBigEndian()) {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000399 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000400 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000401 << Val << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000402 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000403 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000404 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000405 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000406 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000407 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000408 << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000409 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000410 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000411 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000412 }
413 return;
414 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000415 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000416 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000417 return;
418 }
419 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
420 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
421 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000422
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000423 if (Data64bitsDirective)
424 O << Data64bitsDirective << Val << "\n";
425 else if (TD.isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000426 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000427 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000428 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000429 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000430 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000431 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000432 } else {
433 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000434 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000435 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000436 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000437 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000438 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000439 }
440 return;
441 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000442 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
443 const PackedType *PTy = CP->getType();
444
445 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
446 EmitGlobalConstant(CP->getOperand(I));
447
448 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000449 }
450
451 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000452 switch (type->getTypeID()) {
Misha Brukman835702a2005-04-21 22:36:52 +0000453 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000454 case Type::UByteTyID: case Type::SByteTyID:
455 O << Data8bitsDirective;
456 break;
457 case Type::UShortTyID: case Type::ShortTyID:
458 O << Data16bitsDirective;
459 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000460 case Type::PointerTyID:
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000461 if (TD.getPointerSize() == 8) {
462 O << Data64bitsDirective;
463 break;
464 }
465 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000466 case Type::UIntTyID: case Type::IntTyID:
467 O << Data32bitsDirective;
468 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000469 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner88e2d2e2005-08-08 04:26:32 +0000470 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
471 O << Data64bitsDirective;
472 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000473 case Type::FloatTyID: case Type::DoubleTyID:
474 assert (0 && "Should have already output floating point constant.");
475 default:
476 assert (0 && "Can't handle printing this type of thing");
477 break;
478 }
Chris Lattnerbb644e32005-11-21 07:51:36 +0000479 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000480 O << "\n";
481}
Chris Lattner061d9e22006-01-27 02:10:10 +0000482
483/// printInlineAsm - This method formats and prints the specified machine
484/// instruction that is an inline asm.
485void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnered87dcd2006-02-08 23:41:56 +0000486 O << InlineAsmStart;
Chris Lattner57ecb562006-01-30 23:00:08 +0000487 unsigned NumOperands = MI->getNumOperands();
488
489 // Count the number of register definitions.
490 unsigned NumDefs = 0;
491 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
492 assert(NumDefs != NumOperands-1 && "No asm string?");
493
494 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000495
496 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000497 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000498
499 // The variant of the current asmprinter: FIXME: change.
500 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000501
Chris Lattneraa23fa92006-02-01 22:41:11 +0000502 int CurVariant = -1; // The number of the {.|.|.} region we are in.
503 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000504
Chris Lattneraa23fa92006-02-01 22:41:11 +0000505 while (*LastEmitted) {
506 switch (*LastEmitted) {
507 default: {
508 // Not a special case, emit the string section literally.
509 const char *LiteralEnd = LastEmitted+1;
510 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
511 *LiteralEnd != '}' && *LiteralEnd != '$')
512 ++LiteralEnd;
513 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
514 O.write(LastEmitted, LiteralEnd-LastEmitted);
515 LastEmitted = LiteralEnd;
516 break;
517 }
518 case '$': {
519 ++LastEmitted; // Consume '$' character.
520 if (*LastEmitted == '$') { // $$ -> $
521 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
522 O << '$';
523 ++LastEmitted; // Consume second '$' character.
524 break;
525 }
526
527 bool HasCurlyBraces = false;
528 if (*LastEmitted == '{') { // ${variable}
529 ++LastEmitted; // Consume '{' character.
530 HasCurlyBraces = true;
531 }
532
533 const char *IDStart = LastEmitted;
534 char *IDEnd;
535 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
536 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
537 std::cerr << "Bad $ operand number in inline asm string: '"
538 << AsmStr << "'\n";
539 exit(1);
540 }
541 LastEmitted = IDEnd;
542
Chris Lattner34f74c12006-02-06 22:17:23 +0000543 char Modifier[2] = { 0, 0 };
544
Chris Lattneraa23fa92006-02-01 22:41:11 +0000545 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000546 // If we have curly braces, check for a modifier character. This
547 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
548 if (*LastEmitted == ':') {
549 ++LastEmitted; // Consume ':' character.
550 if (*LastEmitted == 0) {
551 std::cerr << "Bad ${:} expression in inline asm string: '"
552 << AsmStr << "'\n";
553 exit(1);
554 }
555
556 Modifier[0] = *LastEmitted;
557 ++LastEmitted; // Consume modifier character.
558 }
559
Chris Lattneraa23fa92006-02-01 22:41:11 +0000560 if (*LastEmitted != '}') {
561 std::cerr << "Bad ${} expression in inline asm string: '"
562 << AsmStr << "'\n";
563 exit(1);
564 }
565 ++LastEmitted; // Consume '}' character.
566 }
567
568 if ((unsigned)Val >= NumOperands-1) {
569 std::cerr << "Invalid $ operand number in inline asm string: '"
570 << AsmStr << "'\n";
571 exit(1);
572 }
573
Chris Lattner571d9642006-02-23 19:21:04 +0000574 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000575 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000576 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
577 unsigned OpNo = 1;
578
579 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000580 for (; Val; --Val) {
581 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
582 OpNo += (OpFlags >> 3) + 1;
583 }
Chris Lattner571d9642006-02-23 19:21:04 +0000584
Chris Lattner1d08c652006-02-24 20:21:58 +0000585 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
Chris Lattner571d9642006-02-23 19:21:04 +0000586 ++OpNo; // Skip over the ID number.
Chris Lattner1d08c652006-02-24 20:21:58 +0000587
588 bool Error;
589 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
590 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
591 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
592 Modifier[0] ? Modifier : 0);
593 } else {
594 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
595 Modifier[0] ? Modifier : 0);
596 }
597 if (Error) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000598 std::cerr << "Invalid operand found in inline asm: '"
599 << AsmStr << "'\n";
600 MI->dump();
601 exit(1);
602 }
Chris Lattner571d9642006-02-23 19:21:04 +0000603 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000604 break;
605 }
606 case '{':
607 ++LastEmitted; // Consume '{' character.
608 if (CurVariant != -1) {
609 std::cerr << "Nested variants found in inline asm string: '"
610 << AsmStr << "'\n";
611 exit(1);
612 }
613 CurVariant = 0; // We're in the first variant now.
614 break;
615 case '|':
616 ++LastEmitted; // consume '|' character.
617 if (CurVariant == -1) {
618 std::cerr << "Found '|' character outside of variant in inline asm "
619 << "string: '" << AsmStr << "'\n";
620 exit(1);
621 }
622 ++CurVariant; // We're in the next variant.
623 break;
624 case '}':
625 ++LastEmitted; // consume '}' character.
626 if (CurVariant == -1) {
627 std::cerr << "Found '}' character outside of variant in inline asm "
628 << "string: '" << AsmStr << "'\n";
629 exit(1);
630 }
631 CurVariant = -1;
632 break;
633 }
634 }
Chris Lattnered87dcd2006-02-08 23:41:56 +0000635 O << "\n" << InlineAsmEnd;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000636}
637
638/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
639/// instruction, using the specified assembler variant. Targets should
640/// overried this to format as appropriate.
641bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000642 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000643 // Target doesn't support this yet!
644 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000645}
Chris Lattner1d08c652006-02-24 20:21:58 +0000646
647bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
648 unsigned AsmVariant,
649 const char *ExtraCode) {
650 // Target doesn't support this yet!
651 return true;
652}