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