1 //===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AsmPrinter class.
12 //===----------------------------------------------------------------------===//
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"
21 bool AsmPrinter::doInitialization(Module &M) {
22 Mang = new Mangler(M, GlobalPrefix);
26 bool AsmPrinter::doFinalization(Module &M) {
27 delete Mang; Mang = 0;
31 void AsmPrinter::setupMachineFunction(MachineFunction &MF) {
32 // What's my mangled name?
33 CurrentFnName = Mang->getValueName((Value*)MF.getFunction());
36 // Print out the specified constant, without a storage class. Only the
37 // constants valid in constant expressions can occur here.
38 void AsmPrinter::emitConstantValueOnly(const Constant *CV) {
39 if (CV->isNullValue())
41 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
42 assert(CB == ConstantBool::True);
44 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
45 if (((CI->getValue() << 32) >> 32) == CI->getValue())
48 O << (unsigned long long)CI->getValue();
49 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
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)) {
64 emitConstantValueOnly(ptrVal);
65 O << ") + " << Offset;
67 emitConstantValueOnly(ptrVal);
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();
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");
90 emitConstantValueOnly(Op);
94 case Instruction::Add:
96 emitConstantValueOnly(CE->getOperand(0));
98 emitConstantValueOnly(CE->getOperand(1));
102 assert(0 && "Unsupported operator!");
105 assert(0 && "Unknown constant value!");
109 /// toOctal - Convert the low order bits of X into an octal digit.
111 static inline char toOctal(int X) {
115 /// getAsCString - Return the specified array as a C compatible string, only if
116 /// the predicate isString is true.
118 static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
119 assert(CVA->isString() && "Array is not string compatible!");
122 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
123 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
127 } else if (C == '\\') {
129 } else if (isprint(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;
140 O << toOctal(C >> 6);
141 O << toOctal(C >> 3);
142 O << toOctal(C >> 0);
150 /// emitGlobalConstant - Print a general LLVM constant to the .s file.
152 void AsmPrinter::emitGlobalConstant(const Constant *CV) {
153 const TargetData &TD = TM.getTargetData();
155 if (CV->isNullValue()) {
156 O << ZeroDirective << TD.getTypeSize(CV->getType()) << "\n";
158 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
159 if (CVA->isString()) {
161 printAsCString(O, CVA);
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));
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);
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;
182 // Now print the actual field value
183 emitGlobalConstant(field);
185 // Insert the field padding unless it's zero bytes...
187 O << ZeroDirective << padSize << "\n";
189 assert(sizeSoFar == cvsLayout->StructSize &&
190 "Layout of constant struct may be incorrect!");
192 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
193 // FP Constants are printed as integer constants to avoid losing
195 double Val = CFP->getValue();
196 if (CFP->getType() == Type::DoubleTy) {
197 union DU { // Abide by C TBAA rules
203 if (Data64bitsDirective)
204 O << Data64bitsDirective << U.UVal << "\n";
205 else if (TD.isBigEndian()) {
206 O << Data32bitsDirective << unsigned(U.UVal >> 32)
207 << "\t" << CommentChar << " double most significant word "
209 O << Data32bitsDirective << unsigned(U.UVal)
210 << "\t" << CommentChar << " double least significant word "
213 O << Data32bitsDirective << unsigned(U.UVal)
214 << "\t" << CommentChar << " double least significant word " << Val
216 O << Data32bitsDirective << unsigned(U.UVal >> 32)
217 << "\t" << CommentChar << " double most significant word " << Val
222 union FU { // Abide by C TBAA rules
228 O << Data32bitsDirective << U.UVal << "\t" << CommentChar
229 << " float " << Val << "\n";
232 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
233 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
234 uint64_t Val = CI->getRawValue();
236 if (Data64bitsDirective)
237 O << Data64bitsDirective << Val << "\n";
238 else if (TD.isBigEndian()) {
239 O << Data32bitsDirective << unsigned(Val >> 32)
240 << "\t" << CommentChar << " Double-word most significant word "
242 O << Data32bitsDirective << unsigned(Val)
243 << "\t" << CommentChar << " Double-word least significant word "
246 O << Data32bitsDirective << unsigned(Val)
247 << "\t" << CommentChar << " Double-word least significant word "
249 O << Data32bitsDirective << unsigned(Val >> 32)
250 << "\t" << CommentChar << " Double-word most significant word "
257 const Type *type = CV->getType();
258 switch (type->getTypeID()) {
259 case Type::UByteTyID: case Type::SByteTyID:
260 O << Data8bitsDirective;
262 case Type::UShortTyID: case Type::ShortTyID:
263 O << Data16bitsDirective;
266 case Type::PointerTyID:
267 case Type::UIntTyID: case Type::IntTyID:
268 O << Data32bitsDirective;
270 case Type::ULongTyID: case Type::LongTyID:
271 assert (0 && "Should have already output double-word constant.");
272 case Type::FloatTyID: case Type::DoubleTyID:
273 assert (0 && "Should have already output floating point constant.");
275 assert (0 && "Can't handle printing this type of thing");
278 emitConstantValueOnly(CV);