blob: 4e77a78d81b1e7a628a1edd790c4d8838645281a [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"
16#include "llvm/Instruction.h"
17#include "llvm/Support/Mangler.h"
18#include "llvm/Target/TargetMachine.h"
19using namespace llvm;
20
21bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner6d42cbd2004-08-17 06:06:19 +000022 Mang = new Mangler(M, GlobalPrefix);
Chris Lattner6a8e0f52004-08-16 23:15:22 +000023 return false;
24}
25
26bool AsmPrinter::doFinalization(Module &M) {
27 delete Mang; Mang = 0;
28 return false;
29}
30
31void AsmPrinter::setupMachineFunction(MachineFunction &MF) {
32 // What's my mangled name?
33 CurrentFnName = Mang->getValueName((Value*)MF.getFunction());
Chris Lattner6a8e0f52004-08-16 23:15:22 +000034}
35
Chris Lattner1d35c162004-08-17 19:14:29 +000036// emitAlignment - Emit an alignment directive to the specified power of two.
37void AsmPrinter::emitAlignment(unsigned NumBits) const {
38 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
39 O << AlignDirective << NumBits << "\n";
40}
41
Chris Lattnerea751992004-08-17 21:38:40 +000042/// emitZeros - Emit a block of zeros.
43///
Chris Lattner0b955fd2005-01-08 19:59:10 +000044void AsmPrinter::emitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +000045 if (NumZeros) {
46 if (ZeroDirective)
47 O << ZeroDirective << NumZeros << "\n";
48 else {
Chris Lattnerea751992004-08-17 21:38:40 +000049 for (; NumZeros; --NumZeros)
50 O << Data8bitsDirective << "0\n";
51 }
52 }
53}
54
Chris Lattner6a8e0f52004-08-16 23:15:22 +000055// Print out the specified constant, without a storage class. Only the
56// constants valid in constant expressions can occur here.
57void AsmPrinter::emitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +000058 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +000059 O << "0";
60 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
61 assert(CB == ConstantBool::True);
62 O << "1";
63 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
64 if (((CI->getValue() << 32) >> 32) == CI->getValue())
65 O << CI->getValue();
66 else
67 O << (unsigned long long)CI->getValue();
68 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
69 O << CI->getValue();
70 else if (isa<GlobalValue>((Value*)CV))
71 // This is a constant address for a global variable or function. Use the
72 // name of the variable or function as the address value.
73 O << Mang->getValueName(CV);
74 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
75 const TargetData &TD = TM.getTargetData();
76 switch(CE->getOpcode()) {
77 case Instruction::GetElementPtr: {
78 // generate a symbolic expression for the byte address
79 const Constant *ptrVal = CE->getOperand(0);
80 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner145569b2005-02-14 21:40:26 +000081 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
82 if (Offset)
83 O << "(";
Chris Lattner6a8e0f52004-08-16 23:15:22 +000084 emitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +000085 if (Offset > 0)
86 O << ") + " << Offset;
87 else if (Offset < 0)
88 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +000089 } else {
90 emitConstantValueOnly(ptrVal);
91 }
92 break;
93 }
94 case Instruction::Cast: {
95 // Support only non-converting or widening casts for now, that is, ones
96 // that do not involve a change in value. This assertion is really gross,
97 // and may not even be a complete check.
98 Constant *Op = CE->getOperand(0);
99 const Type *OpTy = Op->getType(), *Ty = CE->getType();
100
101 // Remember, kids, pointers can be losslessly converted back and forth
102 // into 32-bit or wider integers, regardless of signedness. :-P
103 assert(((isa<PointerType>(OpTy)
104 && (Ty == Type::LongTy || Ty == Type::ULongTy
105 || Ty == Type::IntTy || Ty == Type::UIntTy))
106 || (isa<PointerType>(Ty)
107 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
108 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
109 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
110 && OpTy->isLosslesslyConvertibleTo(Ty))))
111 && "FIXME: Don't yet support this kind of constant cast expr");
112 O << "(";
113 emitConstantValueOnly(Op);
114 O << ")";
115 break;
116 }
117 case Instruction::Add:
118 O << "(";
119 emitConstantValueOnly(CE->getOperand(0));
120 O << ") + (";
121 emitConstantValueOnly(CE->getOperand(1));
122 O << ")";
123 break;
124 default:
125 assert(0 && "Unsupported operator!");
126 }
127 } else {
128 assert(0 && "Unknown constant value!");
129 }
130}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000131
132/// toOctal - Convert the low order bits of X into an octal digit.
133///
134static inline char toOctal(int X) {
135 return (X&7)+'0';
136}
137
138/// getAsCString - Return the specified array as a C compatible string, only if
139/// the predicate isString is true.
140///
141static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
142 assert(CVA->isString() && "Array is not string compatible!");
143
144 O << "\"";
145 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattner0b955fd2005-01-08 19:59:10 +0000146 unsigned char C =
147 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000148
149 if (C == '"') {
150 O << "\\\"";
151 } else if (C == '\\') {
152 O << "\\\\";
153 } else if (isprint(C)) {
154 O << C;
155 } else {
156 switch(C) {
157 case '\b': O << "\\b"; break;
158 case '\f': O << "\\f"; break;
159 case '\n': O << "\\n"; break;
160 case '\r': O << "\\r"; break;
161 case '\t': O << "\\t"; break;
162 default:
163 O << '\\';
164 O << toOctal(C >> 6);
165 O << toOctal(C >> 3);
166 O << toOctal(C >> 0);
167 break;
168 }
169 }
170 }
171 O << "\"";
172}
173
174/// emitGlobalConstant - Print a general LLVM constant to the .s file.
175///
176void AsmPrinter::emitGlobalConstant(const Constant *CV) {
177 const TargetData &TD = TM.getTargetData();
178
Chris Lattner61753bf2004-10-16 18:19:26 +0000179 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattnerea751992004-08-17 21:38:40 +0000180 emitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000181 return;
182 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
183 if (CVA->isString()) {
184 O << AsciiDirective;
185 printAsCString(O, CVA);
186 O << "\n";
187 } else { // Not a string. Print the values in successive locations
188 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
189 emitGlobalConstant(CVA->getOperand(i));
190 }
191 return;
192 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
193 // Print the fields in successive locations. Pad to align if needed!
194 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000195 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000196 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
197 const Constant* field = CVS->getOperand(i);
198
199 // Check if padding is needed and insert one or more 0s.
Chris Lattner0b955fd2005-01-08 19:59:10 +0000200 uint64_t fieldSize = TD.getTypeSize(field->getType());
201 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000202 : cvsLayout->MemberOffsets[i+1])
203 - cvsLayout->MemberOffsets[i]) - fieldSize;
204 sizeSoFar += fieldSize + padSize;
205
206 // Now print the actual field value
207 emitGlobalConstant(field);
208
209 // Insert the field padding unless it's zero bytes...
Chris Lattnerea751992004-08-17 21:38:40 +0000210 emitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000211 }
212 assert(sizeSoFar == cvsLayout->StructSize &&
213 "Layout of constant struct may be incorrect!");
214 return;
215 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
216 // FP Constants are printed as integer constants to avoid losing
217 // precision...
218 double Val = CFP->getValue();
219 if (CFP->getType() == Type::DoubleTy) {
220 union DU { // Abide by C TBAA rules
221 double FVal;
222 uint64_t UVal;
223 } U;
224 U.FVal = Val;
225
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000226 if (Data64bitsDirective)
Chris Lattner10262ab2004-08-18 02:22:55 +0000227 O << Data64bitsDirective << U.UVal << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000228 << " double value: " << Val << "\n";
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000229 else if (TD.isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000230 O << Data32bitsDirective << unsigned(U.UVal >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000231 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000232 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000233 O << Data32bitsDirective << unsigned(U.UVal)
Chris Lattner10262ab2004-08-18 02:22:55 +0000234 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000235 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000236 } else {
237 O << Data32bitsDirective << unsigned(U.UVal)
Chris Lattner10262ab2004-08-18 02:22:55 +0000238 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000239 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000240 O << Data32bitsDirective << unsigned(U.UVal >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000241 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000242 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000243 }
244 return;
245 } else {
246 union FU { // Abide by C TBAA rules
247 float FVal;
248 int32_t UVal;
249 } U;
Chris Lattner0b955fd2005-01-08 19:59:10 +0000250 U.FVal = (float)Val;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000251
Chris Lattner10262ab2004-08-18 02:22:55 +0000252 O << Data32bitsDirective << U.UVal << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000253 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000254 return;
255 }
256 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
257 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
258 uint64_t Val = CI->getRawValue();
259
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000260 if (Data64bitsDirective)
261 O << Data64bitsDirective << Val << "\n";
262 else if (TD.isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000263 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000264 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000265 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000266 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000267 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000268 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000269 } else {
270 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000271 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000272 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000273 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000274 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000275 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000276 }
277 return;
278 }
279 }
280
281 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000282 switch (type->getTypeID()) {
Chris Lattnerebb54c52004-11-28 17:56:47 +0000283 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000284 case Type::UByteTyID: case Type::SByteTyID:
285 O << Data8bitsDirective;
286 break;
287 case Type::UShortTyID: case Type::ShortTyID:
288 O << Data16bitsDirective;
289 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000290 case Type::PointerTyID:
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000291 if (TD.getPointerSize() == 8) {
292 O << Data64bitsDirective;
293 break;
294 }
295 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000296 case Type::UIntTyID: case Type::IntTyID:
297 O << Data32bitsDirective;
298 break;
299 case Type::ULongTyID: case Type::LongTyID:
300 assert (0 && "Should have already output double-word constant.");
301 case Type::FloatTyID: case Type::DoubleTyID:
302 assert (0 && "Should have already output floating point constant.");
303 default:
304 assert (0 && "Can't handle printing this type of thing");
305 break;
306 }
307 emitConstantValueOnly(CV);
308 O << "\n";
309}