blob: 92707528f97b2aa51c5dd50fbcd7a798747b2bba [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 Lattner6a8e0f52004-08-16 23:15:22 +000036// Print out the specified constant, without a storage class. Only the
37// constants valid in constant expressions can occur here.
38void AsmPrinter::emitConstantValueOnly(const Constant *CV) {
39 if (CV->isNullValue())
40 O << "0";
41 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
42 assert(CB == ConstantBool::True);
43 O << "1";
44 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
45 if (((CI->getValue() << 32) >> 32) == CI->getValue())
46 O << CI->getValue();
47 else
48 O << (unsigned long long)CI->getValue();
49 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
50 O << CI->getValue();
51 else if (isa<GlobalValue>((Value*)CV))
52 // This is a constant address for a global variable or function. Use the
53 // name of the variable or function as the address value.
54 O << Mang->getValueName(CV);
55 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
56 const TargetData &TD = TM.getTargetData();
57 switch(CE->getOpcode()) {
58 case Instruction::GetElementPtr: {
59 // generate a symbolic expression for the byte address
60 const Constant *ptrVal = CE->getOperand(0);
61 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
62 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
63 O << "(";
64 emitConstantValueOnly(ptrVal);
65 O << ") + " << Offset;
66 } else {
67 emitConstantValueOnly(ptrVal);
68 }
69 break;
70 }
71 case Instruction::Cast: {
72 // Support only non-converting or widening casts for now, that is, ones
73 // that do not involve a change in value. This assertion is really gross,
74 // and may not even be a complete check.
75 Constant *Op = CE->getOperand(0);
76 const Type *OpTy = Op->getType(), *Ty = CE->getType();
77
78 // Remember, kids, pointers can be losslessly converted back and forth
79 // into 32-bit or wider integers, regardless of signedness. :-P
80 assert(((isa<PointerType>(OpTy)
81 && (Ty == Type::LongTy || Ty == Type::ULongTy
82 || Ty == Type::IntTy || Ty == Type::UIntTy))
83 || (isa<PointerType>(Ty)
84 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
85 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
86 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
87 && OpTy->isLosslesslyConvertibleTo(Ty))))
88 && "FIXME: Don't yet support this kind of constant cast expr");
89 O << "(";
90 emitConstantValueOnly(Op);
91 O << ")";
92 break;
93 }
94 case Instruction::Add:
95 O << "(";
96 emitConstantValueOnly(CE->getOperand(0));
97 O << ") + (";
98 emitConstantValueOnly(CE->getOperand(1));
99 O << ")";
100 break;
101 default:
102 assert(0 && "Unsupported operator!");
103 }
104 } else {
105 assert(0 && "Unknown constant value!");
106 }
107}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000108
109/// toOctal - Convert the low order bits of X into an octal digit.
110///
111static inline char toOctal(int X) {
112 return (X&7)+'0';
113}
114
115/// getAsCString - Return the specified array as a C compatible string, only if
116/// the predicate isString is true.
117///
118static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
119 assert(CVA->isString() && "Array is not string compatible!");
120
121 O << "\"";
122 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
123 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
124
125 if (C == '"') {
126 O << "\\\"";
127 } else if (C == '\\') {
128 O << "\\\\";
129 } else if (isprint(C)) {
130 O << C;
131 } else {
132 switch(C) {
133 case '\b': O << "\\b"; break;
134 case '\f': O << "\\f"; break;
135 case '\n': O << "\\n"; break;
136 case '\r': O << "\\r"; break;
137 case '\t': O << "\\t"; break;
138 default:
139 O << '\\';
140 O << toOctal(C >> 6);
141 O << toOctal(C >> 3);
142 O << toOctal(C >> 0);
143 break;
144 }
145 }
146 }
147 O << "\"";
148}
149
150/// emitGlobalConstant - Print a general LLVM constant to the .s file.
151///
152void AsmPrinter::emitGlobalConstant(const Constant *CV) {
153 const TargetData &TD = TM.getTargetData();
154
155 if (CV->isNullValue()) {
156 O << ZeroDirective << TD.getTypeSize(CV->getType()) << "\n";
157 return;
158 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
159 if (CVA->isString()) {
160 O << AsciiDirective;
161 printAsCString(O, CVA);
162 O << "\n";
163 } else { // Not a string. Print the values in successive locations
164 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
165 emitGlobalConstant(CVA->getOperand(i));
166 }
167 return;
168 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
169 // Print the fields in successive locations. Pad to align if needed!
170 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
171 unsigned sizeSoFar = 0;
172 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
173 const Constant* field = CVS->getOperand(i);
174
175 // Check if padding is needed and insert one or more 0s.
176 unsigned fieldSize = TD.getTypeSize(field->getType());
177 unsigned padSize = ((i == e-1? cvsLayout->StructSize
178 : cvsLayout->MemberOffsets[i+1])
179 - cvsLayout->MemberOffsets[i]) - fieldSize;
180 sizeSoFar += fieldSize + padSize;
181
182 // Now print the actual field value
183 emitGlobalConstant(field);
184
185 // Insert the field padding unless it's zero bytes...
186 if (padSize)
187 O << ZeroDirective << padSize << "\n";
188 }
189 assert(sizeSoFar == cvsLayout->StructSize &&
190 "Layout of constant struct may be incorrect!");
191 return;
192 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
193 // FP Constants are printed as integer constants to avoid losing
194 // precision...
195 double Val = CFP->getValue();
196 if (CFP->getType() == Type::DoubleTy) {
197 union DU { // Abide by C TBAA rules
198 double FVal;
199 uint64_t UVal;
200 } U;
201 U.FVal = Val;
202
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000203 if (Data64bitsDirective)
204 O << Data64bitsDirective << U.UVal << "\n";
205 else if (TD.isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000206 O << Data32bitsDirective << unsigned(U.UVal >> 32)
207 << "\t; double most significant word " << Val << "\n";
208 O << Data32bitsDirective << unsigned(U.UVal)
209 << "\t; double least significant word " << Val << "\n";
210 } else {
211 O << Data32bitsDirective << unsigned(U.UVal)
212 << "\t; double least significant word " << Val << "\n";
213 O << Data32bitsDirective << unsigned(U.UVal >> 32)
214 << "\t; double most significant word " << Val << "\n";
215 }
216 return;
217 } else {
218 union FU { // Abide by C TBAA rules
219 float FVal;
220 int32_t UVal;
221 } U;
222 U.FVal = Val;
223
224 O << Data32bitsDirective << U.UVal << "\t; float " << Val << "\n";
225 return;
226 }
227 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
228 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
229 uint64_t Val = CI->getRawValue();
230
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000231 if (Data64bitsDirective)
232 O << Data64bitsDirective << Val << "\n";
233 else if (TD.isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000234 O << Data32bitsDirective << unsigned(Val >> 32)
235 << "\t; Double-word most significant word " << Val << "\n";
236 O << Data32bitsDirective << unsigned(Val)
237 << "\t; Double-word least significant word " << Val << "\n";
238 } else {
239 O << Data32bitsDirective << unsigned(Val)
240 << "\t; Double-word least significant word " << Val << "\n";
241 O << Data32bitsDirective << unsigned(Val >> 32)
242 << "\t; Double-word most significant word " << Val << "\n";
243 }
244 return;
245 }
246 }
247
248 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000249 switch (type->getTypeID()) {
250 case Type::UByteTyID: case Type::SByteTyID:
251 O << Data8bitsDirective;
252 break;
253 case Type::UShortTyID: case Type::ShortTyID:
254 O << Data16bitsDirective;
255 break;
256 case Type::BoolTyID:
257 case Type::PointerTyID:
258 case Type::UIntTyID: case Type::IntTyID:
259 O << Data32bitsDirective;
260 break;
261 case Type::ULongTyID: case Type::LongTyID:
262 assert (0 && "Should have already output double-word constant.");
263 case Type::FloatTyID: case Type::DoubleTyID:
264 assert (0 && "Should have already output floating point constant.");
265 default:
266 assert (0 && "Can't handle printing this type of thing");
267 break;
268 }
269 emitConstantValueOnly(CV);
270 O << "\n";
271}