blob: d907d67b5a2839daa9a27d554721bace32dcc6f7 [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
14#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/Constants.h"
16#include "llvm/Instruction.h"
17#include "llvm/Support/Mangler.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000018#include "llvm/Support/MathExtras.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000019#include "llvm/Target/TargetMachine.h"
20using namespace llvm;
21
22bool AsmPrinter::doInitialization(Module &M) {
Chris Lattneraf2bf0a2004-08-17 06:06:19 +000023 Mang = new Mangler(M, GlobalPrefix);
Chris Lattnera80ba712004-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?
34 CurrentFnName = Mang->getValueName((Value*)MF.getFunction());
Chris Lattnera80ba712004-08-16 23:15:22 +000035}
36
Chris Lattnerbfddc202004-08-17 19:14:29 +000037// emitAlignment - Emit an alignment directive to the specified power of two.
38void AsmPrinter::emitAlignment(unsigned NumBits) const {
39 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
40 O << AlignDirective << NumBits << "\n";
41}
42
Chris Lattner7d057a32004-08-17 21:38:40 +000043/// emitZeros - Emit a block of zeros.
44///
Chris Lattnerdea18b62005-01-08 19:59:10 +000045void AsmPrinter::emitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +000046 if (NumZeros) {
47 if (ZeroDirective)
48 O << ZeroDirective << NumZeros << "\n";
49 else {
Chris Lattner7d057a32004-08-17 21:38:40 +000050 for (; NumZeros; --NumZeros)
51 O << Data8bitsDirective << "0\n";
52 }
53 }
54}
55
Chris Lattnera80ba712004-08-16 23:15:22 +000056// Print out the specified constant, without a storage class. Only the
57// constants valid in constant expressions can occur here.
58void AsmPrinter::emitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +000059 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +000060 O << "0";
61 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
62 assert(CB == ConstantBool::True);
63 O << "1";
64 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
65 if (((CI->getValue() << 32) >> 32) == CI->getValue())
66 O << CI->getValue();
67 else
Duraid Madina45606572005-05-15 13:05:48 +000068 O << (uint64_t)CI->getValue();
Chris Lattnera80ba712004-08-16 23:15:22 +000069 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
70 O << CI->getValue();
Duraid Madina855a5192005-04-02 12:21:51 +000071 else if (isa<GlobalValue>((Value*)CV)) {
72 // This is a constant address for a global variable or function. Use the
73 // name of the variable or function as the address value, possibly
74 // decorating it with GlobalVarAddrPrefix/Suffix or
75 // FunctionAddrPrefix/Suffix (these all default to "" )
76 if (isa<Function>((Value*)CV))
77 O << FunctionAddrPrefix << Mang->getValueName(CV) << FunctionAddrSuffix;
78 else
79 O << GlobalVarAddrPrefix << Mang->getValueName(CV) << GlobalVarAddrSuffix;
80 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnera80ba712004-08-16 23:15:22 +000081 const TargetData &TD = TM.getTargetData();
82 switch(CE->getOpcode()) {
83 case Instruction::GetElementPtr: {
84 // generate a symbolic expression for the byte address
85 const Constant *ptrVal = CE->getOperand(0);
86 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner27e19212005-02-14 21:40:26 +000087 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
88 if (Offset)
89 O << "(";
Chris Lattnera80ba712004-08-16 23:15:22 +000090 emitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +000091 if (Offset > 0)
92 O << ") + " << Offset;
93 else if (Offset < 0)
94 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +000095 } else {
96 emitConstantValueOnly(ptrVal);
97 }
98 break;
99 }
100 case Instruction::Cast: {
101 // Support only non-converting or widening casts for now, that is, ones
102 // that do not involve a change in value. This assertion is really gross,
103 // and may not even be a complete check.
104 Constant *Op = CE->getOperand(0);
105 const Type *OpTy = Op->getType(), *Ty = CE->getType();
106
107 // Remember, kids, pointers can be losslessly converted back and forth
108 // into 32-bit or wider integers, regardless of signedness. :-P
109 assert(((isa<PointerType>(OpTy)
110 && (Ty == Type::LongTy || Ty == Type::ULongTy
111 || Ty == Type::IntTy || Ty == Type::UIntTy))
112 || (isa<PointerType>(Ty)
113 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
114 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
115 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
116 && OpTy->isLosslesslyConvertibleTo(Ty))))
117 && "FIXME: Don't yet support this kind of constant cast expr");
118 O << "(";
119 emitConstantValueOnly(Op);
120 O << ")";
121 break;
122 }
123 case Instruction::Add:
124 O << "(";
125 emitConstantValueOnly(CE->getOperand(0));
126 O << ") + (";
127 emitConstantValueOnly(CE->getOperand(1));
128 O << ")";
129 break;
130 default:
131 assert(0 && "Unsupported operator!");
132 }
133 } else {
134 assert(0 && "Unknown constant value!");
135 }
136}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000137
138/// toOctal - Convert the low order bits of X into an octal digit.
139///
140static inline char toOctal(int X) {
141 return (X&7)+'0';
142}
143
144/// getAsCString - Return the specified array as a C compatible string, only if
145/// the predicate isString is true.
146///
147static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
148 assert(CVA->isString() && "Array is not string compatible!");
149
150 O << "\"";
151 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000152 unsigned char C =
Chris Lattnerdea18b62005-01-08 19:59:10 +0000153 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000154
155 if (C == '"') {
156 O << "\\\"";
157 } else if (C == '\\') {
158 O << "\\\\";
159 } else if (isprint(C)) {
160 O << C;
161 } else {
162 switch(C) {
163 case '\b': O << "\\b"; break;
164 case '\f': O << "\\f"; break;
165 case '\n': O << "\\n"; break;
166 case '\r': O << "\\r"; break;
167 case '\t': O << "\\t"; break;
168 default:
169 O << '\\';
170 O << toOctal(C >> 6);
171 O << toOctal(C >> 3);
172 O << toOctal(C >> 0);
173 break;
174 }
175 }
176 }
177 O << "\"";
178}
179
180/// emitGlobalConstant - Print a general LLVM constant to the .s file.
181///
Misha Brukmanedf128a2005-04-21 22:36:52 +0000182void AsmPrinter::emitGlobalConstant(const Constant *CV) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000183 const TargetData &TD = TM.getTargetData();
184
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000185 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattner7d057a32004-08-17 21:38:40 +0000186 emitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000187 return;
188 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
189 if (CVA->isString()) {
190 O << AsciiDirective;
191 printAsCString(O, CVA);
192 O << "\n";
193 } else { // Not a string. Print the values in successive locations
194 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
195 emitGlobalConstant(CVA->getOperand(i));
196 }
197 return;
198 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
199 // Print the fields in successive locations. Pad to align if needed!
200 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000201 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000202 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
203 const Constant* field = CVS->getOperand(i);
204
205 // Check if padding is needed and insert one or more 0s.
Chris Lattnerdea18b62005-01-08 19:59:10 +0000206 uint64_t fieldSize = TD.getTypeSize(field->getType());
207 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner1b7e2352004-08-17 06:36:49 +0000208 : cvsLayout->MemberOffsets[i+1])
209 - cvsLayout->MemberOffsets[i]) - fieldSize;
210 sizeSoFar += fieldSize + padSize;
211
212 // Now print the actual field value
213 emitGlobalConstant(field);
214
215 // Insert the field padding unless it's zero bytes...
Chris Lattner7d057a32004-08-17 21:38:40 +0000216 emitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000217 }
218 assert(sizeSoFar == cvsLayout->StructSize &&
219 "Layout of constant struct may be incorrect!");
220 return;
221 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
222 // FP Constants are printed as integer constants to avoid losing
223 // precision...
224 double Val = CFP->getValue();
225 if (CFP->getType() == Type::DoubleTy) {
Chris Lattnere85a5a92004-08-17 06:48:16 +0000226 if (Data64bitsDirective)
Jim Laskeycb6682f2005-08-17 19:34:49 +0000227 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattner7d057a32004-08-17 21:38:40 +0000228 << " double value: " << Val << "\n";
Chris Lattnere85a5a92004-08-17 06:48:16 +0000229 else if (TD.isBigEndian()) {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000230 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000231 << "\t" << CommentString << " double most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000232 << Val << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000233 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000234 << "\t" << CommentString << " double least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000235 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000236 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000237 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000238 << "\t" << CommentString << " double least significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000239 << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000240 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000241 << "\t" << CommentString << " double most significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000242 << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000243 }
244 return;
245 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000246 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattner0554fb62004-08-17 16:27:05 +0000247 << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000248 return;
249 }
250 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
251 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
252 uint64_t Val = CI->getRawValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000253
Chris Lattnere85a5a92004-08-17 06:48:16 +0000254 if (Data64bitsDirective)
255 O << Data64bitsDirective << Val << "\n";
256 else if (TD.isBigEndian()) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000257 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000258 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000259 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000260 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000261 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000262 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000263 } else {
264 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000265 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000266 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000267 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000268 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000269 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000270 }
271 return;
272 }
273 }
274
275 const Type *type = CV->getType();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000276 switch (type->getTypeID()) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000277 case Type::BoolTyID:
Chris Lattner1b7e2352004-08-17 06:36:49 +0000278 case Type::UByteTyID: case Type::SByteTyID:
279 O << Data8bitsDirective;
280 break;
281 case Type::UShortTyID: case Type::ShortTyID:
282 O << Data16bitsDirective;
283 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000284 case Type::PointerTyID:
Andrew Lenharth571c9c32005-02-04 13:47:16 +0000285 if (TD.getPointerSize() == 8) {
286 O << Data64bitsDirective;
287 break;
288 }
289 //Fall through for pointer size == int size
Chris Lattner1b7e2352004-08-17 06:36:49 +0000290 case Type::UIntTyID: case Type::IntTyID:
291 O << Data32bitsDirective;
292 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000293 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner660538c2005-08-08 04:26:32 +0000294 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
295 O << Data64bitsDirective;
296 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000297 case Type::FloatTyID: case Type::DoubleTyID:
298 assert (0 && "Should have already output floating point constant.");
299 default:
300 assert (0 && "Can't handle printing this type of thing");
301 break;
302 }
303 emitConstantValueOnly(CV);
304 O << "\n";
305}