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());
39 // Print out the specified constant, without a storage class. Only the
40 // constants valid in constant expressions can occur here.
41 void AsmPrinter::emitConstantValueOnly(const Constant *CV) {
42 if (CV->isNullValue())
44 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
45 assert(CB == ConstantBool::True);
47 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
48 if (((CI->getValue() << 32) >> 32) == CI->getValue())
51 O << (unsigned long long)CI->getValue();
52 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
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)) {
67 emitConstantValueOnly(ptrVal);
68 O << ") + " << Offset;
70 emitConstantValueOnly(ptrVal);
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();
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");
93 emitConstantValueOnly(Op);
97 case Instruction::Add:
99 emitConstantValueOnly(CE->getOperand(0));
101 emitConstantValueOnly(CE->getOperand(1));
105 assert(0 && "Unsupported operator!");
108 assert(0 && "Unknown constant value!");
112 /// toOctal - Convert the low order bits of X into an octal digit.
114 static inline char toOctal(int X) {
118 /// getAsCString - Return the specified array as a C compatible string, only if
119 /// the predicate isString is true.
121 static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
122 assert(CVA->isString() && "Array is not string compatible!");
125 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
126 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
130 } else if (C == '\\') {
132 } else if (isprint(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;
143 O << toOctal(C >> 6);
144 O << toOctal(C >> 3);
145 O << toOctal(C >> 0);
153 /// emitGlobalConstant - Print a general LLVM constant to the .s file.
155 void AsmPrinter::emitGlobalConstant(const Constant *CV) {
156 const TargetData &TD = TM.getTargetData();
158 if (CV->isNullValue()) {
159 O << ZeroDirective << TD.getTypeSize(CV->getType()) << "\n";
161 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
162 if (CVA->isString()) {
164 printAsCString(O, CVA);
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));
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);
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;
185 // Now print the actual field value
186 emitGlobalConstant(field);
188 // Insert the field padding unless it's zero bytes...
190 O << ZeroDirective << padSize << "\n";
192 assert(sizeSoFar == cvsLayout->StructSize &&
193 "Layout of constant struct may be incorrect!");
195 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
196 // FP Constants are printed as integer constants to avoid losing
198 double Val = CFP->getValue();
199 if (CFP->getType() == Type::DoubleTy) {
200 union DU { // Abide by C TBAA rules
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";
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";
219 union FU { // Abide by C TBAA rules
225 O << Data32bitsDirective << U.UVal << "\t; float " << Val << "\n";
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();
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";
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";
247 const Type *type = CV->getType();
249 switch (type->getTypeID()) {
250 case Type::UByteTyID: case Type::SByteTyID:
251 O << Data8bitsDirective;
253 case Type::UShortTyID: case Type::ShortTyID:
254 O << Data16bitsDirective;
257 case Type::PointerTyID:
258 case Type::UIntTyID: case Type::IntTyID:
259 O << Data32bitsDirective;
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.");
266 assert (0 && "Can't handle printing this type of thing");
269 emitConstantValueOnly(CV);