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 // emitAlignment - Emit an alignment directive to the specified power of two.
37 void AsmPrinter::emitAlignment(unsigned NumBits) const {
38 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
39 O << AlignDirective << NumBits << "\n";
42 /// emitZeros - Emit a block of zeros.
44 void AsmPrinter::emitZeros(uint64_t NumZeros) const {
47 O << ZeroDirective << NumZeros << "\n";
49 for (; NumZeros; --NumZeros)
50 O << Data8bitsDirective << "0\n";
55 // Print out the specified constant, without a storage class. Only the
56 // constants valid in constant expressions can occur here.
57 void AsmPrinter::emitConstantValueOnly(const Constant *CV) {
58 if (CV->isNullValue() || isa<UndefValue>(CV))
60 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
61 assert(CB == ConstantBool::True);
63 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
64 if (((CI->getValue() << 32) >> 32) == CI->getValue())
67 O << (unsigned long long)CI->getValue();
68 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
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 (uint64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
83 emitConstantValueOnly(ptrVal);
84 O << ") + " << Offset;
86 emitConstantValueOnly(ptrVal);
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();
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");
109 emitConstantValueOnly(Op);
113 case Instruction::Add:
115 emitConstantValueOnly(CE->getOperand(0));
117 emitConstantValueOnly(CE->getOperand(1));
121 assert(0 && "Unsupported operator!");
124 assert(0 && "Unknown constant value!");
128 /// toOctal - Convert the low order bits of X into an octal digit.
130 static inline char toOctal(int X) {
134 /// getAsCString - Return the specified array as a C compatible string, only if
135 /// the predicate isString is true.
137 static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
138 assert(CVA->isString() && "Array is not string compatible!");
141 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
143 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
147 } else if (C == '\\') {
149 } else if (isprint(C)) {
153 case '\b': O << "\\b"; break;
154 case '\f': O << "\\f"; break;
155 case '\n': O << "\\n"; break;
156 case '\r': O << "\\r"; break;
157 case '\t': O << "\\t"; break;
160 O << toOctal(C >> 6);
161 O << toOctal(C >> 3);
162 O << toOctal(C >> 0);
170 /// emitGlobalConstant - Print a general LLVM constant to the .s file.
172 void AsmPrinter::emitGlobalConstant(const Constant *CV) {
173 const TargetData &TD = TM.getTargetData();
175 if (CV->isNullValue() || isa<UndefValue>(CV)) {
176 emitZeros(TD.getTypeSize(CV->getType()));
178 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
179 if (CVA->isString()) {
181 printAsCString(O, CVA);
183 } else { // Not a string. Print the values in successive locations
184 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
185 emitGlobalConstant(CVA->getOperand(i));
188 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
189 // Print the fields in successive locations. Pad to align if needed!
190 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
191 uint64_t sizeSoFar = 0;
192 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
193 const Constant* field = CVS->getOperand(i);
195 // Check if padding is needed and insert one or more 0s.
196 uint64_t fieldSize = TD.getTypeSize(field->getType());
197 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
198 : cvsLayout->MemberOffsets[i+1])
199 - cvsLayout->MemberOffsets[i]) - fieldSize;
200 sizeSoFar += fieldSize + padSize;
202 // Now print the actual field value
203 emitGlobalConstant(field);
205 // Insert the field padding unless it's zero bytes...
208 assert(sizeSoFar == cvsLayout->StructSize &&
209 "Layout of constant struct may be incorrect!");
211 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
212 // FP Constants are printed as integer constants to avoid losing
214 double Val = CFP->getValue();
215 if (CFP->getType() == Type::DoubleTy) {
216 union DU { // Abide by C TBAA rules
222 if (Data64bitsDirective)
223 O << Data64bitsDirective << U.UVal << "\t" << CommentString
224 << " double value: " << Val << "\n";
225 else if (TD.isBigEndian()) {
226 O << Data32bitsDirective << unsigned(U.UVal >> 32)
227 << "\t" << CommentString << " double most significant word "
229 O << Data32bitsDirective << unsigned(U.UVal)
230 << "\t" << CommentString << " double least significant word "
233 O << Data32bitsDirective << unsigned(U.UVal)
234 << "\t" << CommentString << " double least significant word " << Val
236 O << Data32bitsDirective << unsigned(U.UVal >> 32)
237 << "\t" << CommentString << " double most significant word " << Val
242 union FU { // Abide by C TBAA rules
248 O << Data32bitsDirective << U.UVal << "\t" << CommentString
249 << " float " << Val << "\n";
252 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
253 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
254 uint64_t Val = CI->getRawValue();
256 if (Data64bitsDirective)
257 O << Data64bitsDirective << Val << "\n";
258 else if (TD.isBigEndian()) {
259 O << Data32bitsDirective << unsigned(Val >> 32)
260 << "\t" << CommentString << " Double-word most significant word "
262 O << Data32bitsDirective << unsigned(Val)
263 << "\t" << CommentString << " Double-word least significant word "
266 O << Data32bitsDirective << unsigned(Val)
267 << "\t" << CommentString << " Double-word least significant word "
269 O << Data32bitsDirective << unsigned(Val >> 32)
270 << "\t" << CommentString << " Double-word most significant word "
277 const Type *type = CV->getType();
278 switch (type->getTypeID()) {
280 case Type::UByteTyID: case Type::SByteTyID:
281 O << Data8bitsDirective;
283 case Type::UShortTyID: case Type::ShortTyID:
284 O << Data16bitsDirective;
286 case Type::PointerTyID:
287 case Type::UIntTyID: case Type::IntTyID:
288 O << Data32bitsDirective;
290 case Type::ULongTyID: case Type::LongTyID:
291 assert (0 && "Should have already output double-word constant.");
292 case Type::FloatTyID: case Type::DoubleTyID:
293 assert (0 && "Should have already output floating point constant.");
295 assert (0 && "Can't handle printing this type of thing");
298 emitConstantValueOnly(CV);