blob: eb57cced13ee9a7b918b7a6aa7c691b2de7c1763 [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"
18#include "llvm/Target/TargetMachine.h"
19using namespace llvm;
20
21bool AsmPrinter::doInitialization(Module &M) {
Chris Lattneraf2bf0a2004-08-17 06:06:19 +000022 Mang = new Mangler(M, GlobalPrefix);
Chris Lattnera80ba712004-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 Lattnera80ba712004-08-16 23:15:22 +000034}
35
Chris Lattnerbfddc202004-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 Lattner7d057a32004-08-17 21:38:40 +000042/// emitZeros - Emit a block of zeros.
43///
44void AsmPrinter::emitZeros(unsigned NumZeros) const {
45 if (NumZeros) {
46 if (ZeroDirective)
47 O << ZeroDirective << NumZeros << "\n";
48 else {
Chris Lattner7d057a32004-08-17 21:38:40 +000049 for (; NumZeros; --NumZeros)
50 O << Data8bitsDirective << "0\n";
51 }
52 }
53}
54
Chris Lattnera80ba712004-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 Lattnerbd1d3822004-10-16 18:19:26 +000058 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-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());
81 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
82 O << "(";
83 emitConstantValueOnly(ptrVal);
84 O << ") + " << Offset;
85 } else {
86 emitConstantValueOnly(ptrVal);
87 }
88 break;
89 }
90 case Instruction::Cast: {
91 // Support only non-converting or widening casts for now, that is, ones
92 // that do not involve a change in value. This assertion is really gross,
93 // and may not even be a complete check.
94 Constant *Op = CE->getOperand(0);
95 const Type *OpTy = Op->getType(), *Ty = CE->getType();
96
97 // Remember, kids, pointers can be losslessly converted back and forth
98 // into 32-bit or wider integers, regardless of signedness. :-P
99 assert(((isa<PointerType>(OpTy)
100 && (Ty == Type::LongTy || Ty == Type::ULongTy
101 || Ty == Type::IntTy || Ty == Type::UIntTy))
102 || (isa<PointerType>(Ty)
103 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
104 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
105 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
106 && OpTy->isLosslesslyConvertibleTo(Ty))))
107 && "FIXME: Don't yet support this kind of constant cast expr");
108 O << "(";
109 emitConstantValueOnly(Op);
110 O << ")";
111 break;
112 }
113 case Instruction::Add:
114 O << "(";
115 emitConstantValueOnly(CE->getOperand(0));
116 O << ") + (";
117 emitConstantValueOnly(CE->getOperand(1));
118 O << ")";
119 break;
120 default:
121 assert(0 && "Unsupported operator!");
122 }
123 } else {
124 assert(0 && "Unknown constant value!");
125 }
126}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000127
128/// toOctal - Convert the low order bits of X into an octal digit.
129///
130static inline char toOctal(int X) {
131 return (X&7)+'0';
132}
133
134/// getAsCString - Return the specified array as a C compatible string, only if
135/// the predicate isString is true.
136///
137static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
138 assert(CVA->isString() && "Array is not string compatible!");
139
140 O << "\"";
141 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
142 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
143
144 if (C == '"') {
145 O << "\\\"";
146 } else if (C == '\\') {
147 O << "\\\\";
148 } else if (isprint(C)) {
149 O << C;
150 } else {
151 switch(C) {
152 case '\b': O << "\\b"; break;
153 case '\f': O << "\\f"; break;
154 case '\n': O << "\\n"; break;
155 case '\r': O << "\\r"; break;
156 case '\t': O << "\\t"; break;
157 default:
158 O << '\\';
159 O << toOctal(C >> 6);
160 O << toOctal(C >> 3);
161 O << toOctal(C >> 0);
162 break;
163 }
164 }
165 }
166 O << "\"";
167}
168
169/// emitGlobalConstant - Print a general LLVM constant to the .s file.
170///
171void AsmPrinter::emitGlobalConstant(const Constant *CV) {
172 const TargetData &TD = TM.getTargetData();
173
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000174 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattner7d057a32004-08-17 21:38:40 +0000175 emitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000176 return;
177 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
178 if (CVA->isString()) {
179 O << AsciiDirective;
180 printAsCString(O, CVA);
181 O << "\n";
182 } else { // Not a string. Print the values in successive locations
183 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
184 emitGlobalConstant(CVA->getOperand(i));
185 }
186 return;
187 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
188 // Print the fields in successive locations. Pad to align if needed!
189 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
190 unsigned sizeSoFar = 0;
191 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
192 const Constant* field = CVS->getOperand(i);
193
194 // Check if padding is needed and insert one or more 0s.
195 unsigned fieldSize = TD.getTypeSize(field->getType());
196 unsigned padSize = ((i == e-1? cvsLayout->StructSize
197 : cvsLayout->MemberOffsets[i+1])
198 - cvsLayout->MemberOffsets[i]) - fieldSize;
199 sizeSoFar += fieldSize + padSize;
200
201 // Now print the actual field value
202 emitGlobalConstant(field);
203
204 // Insert the field padding unless it's zero bytes...
Chris Lattner7d057a32004-08-17 21:38:40 +0000205 emitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000206 }
207 assert(sizeSoFar == cvsLayout->StructSize &&
208 "Layout of constant struct may be incorrect!");
209 return;
210 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
211 // FP Constants are printed as integer constants to avoid losing
212 // precision...
213 double Val = CFP->getValue();
214 if (CFP->getType() == Type::DoubleTy) {
215 union DU { // Abide by C TBAA rules
216 double FVal;
217 uint64_t UVal;
218 } U;
219 U.FVal = Val;
220
Chris Lattnere85a5a92004-08-17 06:48:16 +0000221 if (Data64bitsDirective)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000222 O << Data64bitsDirective << U.UVal << "\t" << CommentString
Chris Lattner7d057a32004-08-17 21:38:40 +0000223 << " double value: " << Val << "\n";
Chris Lattnere85a5a92004-08-17 06:48:16 +0000224 else if (TD.isBigEndian()) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000225 O << Data32bitsDirective << unsigned(U.UVal >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000226 << "\t" << CommentString << " double most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000227 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000228 O << Data32bitsDirective << unsigned(U.UVal)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000229 << "\t" << CommentString << " double least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000230 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000231 } else {
232 O << Data32bitsDirective << unsigned(U.UVal)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000233 << "\t" << CommentString << " double least significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000234 << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000235 O << Data32bitsDirective << unsigned(U.UVal >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000236 << "\t" << CommentString << " double most significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000237 << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000238 }
239 return;
240 } else {
241 union FU { // Abide by C TBAA rules
242 float FVal;
243 int32_t UVal;
244 } U;
245 U.FVal = Val;
246
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000247 O << Data32bitsDirective << U.UVal << "\t" << CommentString
Chris Lattner0554fb62004-08-17 16:27:05 +0000248 << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000249 return;
250 }
251 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
252 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
253 uint64_t Val = CI->getRawValue();
254
Chris Lattnere85a5a92004-08-17 06:48:16 +0000255 if (Data64bitsDirective)
256 O << Data64bitsDirective << Val << "\n";
257 else if (TD.isBigEndian()) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000258 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000259 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000260 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000261 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000262 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000263 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000264 } else {
265 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000266 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000267 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000268 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000269 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000270 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000271 }
272 return;
273 }
274 }
275
276 const Type *type = CV->getType();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000277 switch (type->getTypeID()) {
278 case Type::UByteTyID: case Type::SByteTyID:
279 O << Data8bitsDirective;
280 break;
281 case Type::UShortTyID: case Type::ShortTyID:
282 O << Data16bitsDirective;
283 break;
284 case Type::BoolTyID:
285 case Type::PointerTyID:
286 case Type::UIntTyID: case Type::IntTyID:
287 O << Data32bitsDirective;
288 break;
289 case Type::ULongTyID: case Type::LongTyID:
290 assert (0 && "Should have already output double-word constant.");
291 case Type::FloatTyID: case Type::DoubleTyID:
292 assert (0 && "Should have already output floating point constant.");
293 default:
294 assert (0 && "Can't handle printing this type of thing");
295 break;
296 }
297 emitConstantValueOnly(CV);
298 O << "\n";
299}