blob: a6028e43de89fb1ac01b840a359dd5525e37d521 [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
14#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/Constants.h"
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000016#include "llvm/Module.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000017#include "llvm/Support/Mangler.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000018#include "llvm/Support/MathExtras.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000019#include "llvm/Target/TargetMachine.h"
20using namespace llvm;
21
22bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner6d42cbd2004-08-17 06:06:19 +000023 Mang = new Mangler(M, GlobalPrefix);
Chris Lattner6a8e0f52004-08-16 23:15:22 +000024 return false;
25}
26
27bool AsmPrinter::doFinalization(Module &M) {
28 delete Mang; Mang = 0;
29 return false;
30}
31
32void AsmPrinter::setupMachineFunction(MachineFunction &MF) {
33 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000034 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner6a8e0f52004-08-16 23:15:22 +000035}
36
Chris Lattner1d35c162004-08-17 19:14:29 +000037// emitAlignment - Emit an alignment directive to the specified power of two.
Chris Lattnerdd8eeed2005-11-14 19:00:06 +000038void AsmPrinter::emitAlignment(unsigned NumBits, const GlobalValue *GV) const {
39 if (GV && GV->getAlignment())
40 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +000041 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner1d35c162004-08-17 19:14:29 +000042 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
43 O << AlignDirective << NumBits << "\n";
44}
45
Chris Lattnerea751992004-08-17 21:38:40 +000046/// emitZeros - Emit a block of zeros.
47///
Chris Lattner0b955fd2005-01-08 19:59:10 +000048void AsmPrinter::emitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +000049 if (NumZeros) {
50 if (ZeroDirective)
51 O << ZeroDirective << NumZeros << "\n";
52 else {
Chris Lattnerea751992004-08-17 21:38:40 +000053 for (; NumZeros; --NumZeros)
54 O << Data8bitsDirective << "0\n";
55 }
56 }
57}
58
Chris Lattner6a8e0f52004-08-16 23:15:22 +000059// Print out the specified constant, without a storage class. Only the
60// constants valid in constant expressions can occur here.
61void AsmPrinter::emitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +000062 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +000063 O << "0";
64 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
65 assert(CB == ConstantBool::True);
66 O << "1";
67 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
68 if (((CI->getValue() << 32) >> 32) == CI->getValue())
69 O << CI->getValue();
70 else
Duraid Madina73c4dba2005-05-15 13:05:48 +000071 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +000072 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
73 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000074 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +000075 // This is a constant address for a global variable or function. Use the
76 // name of the variable or function as the address value, possibly
77 // decorating it with GlobalVarAddrPrefix/Suffix or
78 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000079 if (isa<Function>(GV))
80 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +000081 else
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000082 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +000083 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +000084 const TargetData &TD = TM.getTargetData();
85 switch(CE->getOpcode()) {
86 case Instruction::GetElementPtr: {
87 // generate a symbolic expression for the byte address
88 const Constant *ptrVal = CE->getOperand(0);
89 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner145569b2005-02-14 21:40:26 +000090 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
91 if (Offset)
92 O << "(";
Chris Lattner6a8e0f52004-08-16 23:15:22 +000093 emitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +000094 if (Offset > 0)
95 O << ") + " << Offset;
96 else if (Offset < 0)
97 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +000098 } else {
99 emitConstantValueOnly(ptrVal);
100 }
101 break;
102 }
103 case Instruction::Cast: {
104 // Support only non-converting or widening casts for now, that is, ones
105 // that do not involve a change in value. This assertion is really gross,
106 // and may not even be a complete check.
107 Constant *Op = CE->getOperand(0);
108 const Type *OpTy = Op->getType(), *Ty = CE->getType();
109
110 // Remember, kids, pointers can be losslessly converted back and forth
111 // into 32-bit or wider integers, regardless of signedness. :-P
112 assert(((isa<PointerType>(OpTy)
113 && (Ty == Type::LongTy || Ty == Type::ULongTy
114 || Ty == Type::IntTy || Ty == Type::UIntTy))
115 || (isa<PointerType>(Ty)
116 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
117 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
118 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
119 && OpTy->isLosslesslyConvertibleTo(Ty))))
120 && "FIXME: Don't yet support this kind of constant cast expr");
121 O << "(";
122 emitConstantValueOnly(Op);
123 O << ")";
124 break;
125 }
126 case Instruction::Add:
127 O << "(";
128 emitConstantValueOnly(CE->getOperand(0));
129 O << ") + (";
130 emitConstantValueOnly(CE->getOperand(1));
131 O << ")";
132 break;
133 default:
134 assert(0 && "Unsupported operator!");
135 }
136 } else {
137 assert(0 && "Unknown constant value!");
138 }
139}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000140
141/// toOctal - Convert the low order bits of X into an octal digit.
142///
143static inline char toOctal(int X) {
144 return (X&7)+'0';
145}
146
Chris Lattner55a6d902005-11-10 18:06:33 +0000147/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000148/// the predicate isString is true.
149///
Chris Lattner55a6d902005-11-10 18:06:33 +0000150static void printAsCString(std::ostream &O, const ConstantArray *CVA,
151 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000152 assert(CVA->isString() && "Array is not string compatible!");
153
154 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000155 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000156 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000157 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000158
159 if (C == '"') {
160 O << "\\\"";
161 } else if (C == '\\') {
162 O << "\\\\";
163 } else if (isprint(C)) {
164 O << C;
165 } else {
166 switch(C) {
167 case '\b': O << "\\b"; break;
168 case '\f': O << "\\f"; break;
169 case '\n': O << "\\n"; break;
170 case '\r': O << "\\r"; break;
171 case '\t': O << "\\t"; break;
172 default:
173 O << '\\';
174 O << toOctal(C >> 6);
175 O << toOctal(C >> 3);
176 O << toOctal(C >> 0);
177 break;
178 }
179 }
180 }
181 O << "\"";
182}
183
184/// emitGlobalConstant - Print a general LLVM constant to the .s file.
185///
Misha Brukman835702a2005-04-21 22:36:52 +0000186void AsmPrinter::emitGlobalConstant(const Constant *CV) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000187 const TargetData &TD = TM.getTargetData();
188
Chris Lattner61753bf2004-10-16 18:19:26 +0000189 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattnerea751992004-08-17 21:38:40 +0000190 emitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000191 return;
192 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
193 if (CVA->isString()) {
Chris Lattner55a6d902005-11-10 18:06:33 +0000194 unsigned NumElts = CVA->getNumOperands();
195 if (AscizDirective && NumElts &&
196 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
197 O << AscizDirective;
198 printAsCString(O, CVA, NumElts-1);
199 } else {
200 O << AsciiDirective;
201 printAsCString(O, CVA, NumElts);
202 }
Chris Lattner8452a1f2004-08-17 06:36:49 +0000203 O << "\n";
204 } else { // Not a string. Print the values in successive locations
205 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
206 emitGlobalConstant(CVA->getOperand(i));
207 }
208 return;
209 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
210 // Print the fields in successive locations. Pad to align if needed!
211 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000212 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000213 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
214 const Constant* field = CVS->getOperand(i);
215
216 // Check if padding is needed and insert one or more 0s.
Chris Lattner0b955fd2005-01-08 19:59:10 +0000217 uint64_t fieldSize = TD.getTypeSize(field->getType());
218 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000219 : cvsLayout->MemberOffsets[i+1])
220 - cvsLayout->MemberOffsets[i]) - fieldSize;
221 sizeSoFar += fieldSize + padSize;
222
223 // Now print the actual field value
224 emitGlobalConstant(field);
225
226 // Insert the field padding unless it's zero bytes...
Chris Lattnerea751992004-08-17 21:38:40 +0000227 emitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000228 }
229 assert(sizeSoFar == cvsLayout->StructSize &&
230 "Layout of constant struct may be incorrect!");
231 return;
232 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
233 // FP Constants are printed as integer constants to avoid losing
234 // precision...
235 double Val = CFP->getValue();
236 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000237 if (Data64bitsDirective)
Jim Laskeyb74c6662005-08-17 19:34:49 +0000238 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000239 << " double value: " << Val << "\n";
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000240 else if (TD.isBigEndian()) {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000241 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000242 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000243 << Val << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000244 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000245 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000246 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000247 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000248 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000249 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000250 << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000251 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000252 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000253 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000254 }
255 return;
256 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000257 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000258 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000259 return;
260 }
261 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
262 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
263 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000264
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000265 if (Data64bitsDirective)
266 O << Data64bitsDirective << Val << "\n";
267 else if (TD.isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000268 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000269 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000270 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000271 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000272 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000273 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000274 } else {
275 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000276 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000277 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000278 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000279 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000280 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000281 }
282 return;
283 }
284 }
285
286 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000287 switch (type->getTypeID()) {
Misha Brukman835702a2005-04-21 22:36:52 +0000288 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000289 case Type::UByteTyID: case Type::SByteTyID:
290 O << Data8bitsDirective;
291 break;
292 case Type::UShortTyID: case Type::ShortTyID:
293 O << Data16bitsDirective;
294 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000295 case Type::PointerTyID:
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000296 if (TD.getPointerSize() == 8) {
297 O << Data64bitsDirective;
298 break;
299 }
300 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000301 case Type::UIntTyID: case Type::IntTyID:
302 O << Data32bitsDirective;
303 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000304 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner88e2d2e2005-08-08 04:26:32 +0000305 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
306 O << Data64bitsDirective;
307 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000308 case Type::FloatTyID: case Type::DoubleTyID:
309 assert (0 && "Should have already output floating point constant.");
310 default:
311 assert (0 && "Can't handle printing this type of thing");
312 break;
313 }
314 emitConstantValueOnly(CV);
315 O << "\n";
316}