LLVM API Documentation
00001 //===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the AsmPrinter class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/CodeGen/AsmPrinter.h" 00015 #include "llvm/Constants.h" 00016 #include "llvm/Instruction.h" 00017 #include "llvm/Support/Mangler.h" 00018 #include "llvm/Target/TargetMachine.h" 00019 using namespace llvm; 00020 00021 bool AsmPrinter::doInitialization(Module &M) { 00022 Mang = new Mangler(M, GlobalPrefix); 00023 return false; 00024 } 00025 00026 bool AsmPrinter::doFinalization(Module &M) { 00027 delete Mang; Mang = 0; 00028 return false; 00029 } 00030 00031 void AsmPrinter::setupMachineFunction(MachineFunction &MF) { 00032 // What's my mangled name? 00033 CurrentFnName = Mang->getValueName((Value*)MF.getFunction()); 00034 } 00035 00036 // emitAlignment - Emit an alignment directive to the specified power of two. 00037 void AsmPrinter::emitAlignment(unsigned NumBits) const { 00038 if (AlignmentIsInBytes) NumBits = 1 << NumBits; 00039 O << AlignDirective << NumBits << "\n"; 00040 } 00041 00042 /// emitZeros - Emit a block of zeros. 00043 /// 00044 void AsmPrinter::emitZeros(unsigned NumZeros) const { 00045 if (NumZeros) { 00046 if (ZeroDirective) 00047 O << ZeroDirective << NumZeros << "\n"; 00048 else { 00049 for (; NumZeros; --NumZeros) 00050 O << Data8bitsDirective << "0\n"; 00051 } 00052 } 00053 } 00054 00055 // Print out the specified constant, without a storage class. Only the 00056 // constants valid in constant expressions can occur here. 00057 void AsmPrinter::emitConstantValueOnly(const Constant *CV) { 00058 if (CV->isNullValue() || isa<UndefValue>(CV)) 00059 O << "0"; 00060 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { 00061 assert(CB == ConstantBool::True); 00062 O << "1"; 00063 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) 00064 if (((CI->getValue() << 32) >> 32) == CI->getValue()) 00065 O << CI->getValue(); 00066 else 00067 O << (unsigned long long)CI->getValue(); 00068 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) 00069 O << CI->getValue(); 00070 else if (isa<GlobalValue>((Value*)CV)) 00071 // This is a constant address for a global variable or function. Use the 00072 // name of the variable or function as the address value. 00073 O << Mang->getValueName(CV); 00074 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { 00075 const TargetData &TD = TM.getTargetData(); 00076 switch(CE->getOpcode()) { 00077 case Instruction::GetElementPtr: { 00078 // generate a symbolic expression for the byte address 00079 const Constant *ptrVal = CE->getOperand(0); 00080 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end()); 00081 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) { 00082 O << "("; 00083 emitConstantValueOnly(ptrVal); 00084 O << ") + " << Offset; 00085 } else { 00086 emitConstantValueOnly(ptrVal); 00087 } 00088 break; 00089 } 00090 case Instruction::Cast: { 00091 // Support only non-converting or widening casts for now, that is, ones 00092 // that do not involve a change in value. This assertion is really gross, 00093 // and may not even be a complete check. 00094 Constant *Op = CE->getOperand(0); 00095 const Type *OpTy = Op->getType(), *Ty = CE->getType(); 00096 00097 // Remember, kids, pointers can be losslessly converted back and forth 00098 // into 32-bit or wider integers, regardless of signedness. :-P 00099 assert(((isa<PointerType>(OpTy) 00100 && (Ty == Type::LongTy || Ty == Type::ULongTy 00101 || Ty == Type::IntTy || Ty == Type::UIntTy)) 00102 || (isa<PointerType>(Ty) 00103 && (OpTy == Type::LongTy || OpTy == Type::ULongTy 00104 || OpTy == Type::IntTy || OpTy == Type::UIntTy)) 00105 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy)) 00106 && OpTy->isLosslesslyConvertibleTo(Ty)))) 00107 && "FIXME: Don't yet support this kind of constant cast expr"); 00108 O << "("; 00109 emitConstantValueOnly(Op); 00110 O << ")"; 00111 break; 00112 } 00113 case Instruction::Add: 00114 O << "("; 00115 emitConstantValueOnly(CE->getOperand(0)); 00116 O << ") + ("; 00117 emitConstantValueOnly(CE->getOperand(1)); 00118 O << ")"; 00119 break; 00120 default: 00121 assert(0 && "Unsupported operator!"); 00122 } 00123 } else { 00124 assert(0 && "Unknown constant value!"); 00125 } 00126 } 00127 00128 /// toOctal - Convert the low order bits of X into an octal digit. 00129 /// 00130 static inline char toOctal(int X) { 00131 return (X&7)+'0'; 00132 } 00133 00134 /// getAsCString - Return the specified array as a C compatible string, only if 00135 /// the predicate isString is true. 00136 /// 00137 static void printAsCString(std::ostream &O, const ConstantArray *CVA) { 00138 assert(CVA->isString() && "Array is not string compatible!"); 00139 00140 O << "\""; 00141 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) { 00142 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue(); 00143 00144 if (C == '"') { 00145 O << "\\\""; 00146 } else if (C == '\\') { 00147 O << "\\\\"; 00148 } else if (isprint(C)) { 00149 O << C; 00150 } else { 00151 switch(C) { 00152 case '\b': O << "\\b"; break; 00153 case '\f': O << "\\f"; break; 00154 case '\n': O << "\\n"; break; 00155 case '\r': O << "\\r"; break; 00156 case '\t': O << "\\t"; break; 00157 default: 00158 O << '\\'; 00159 O << toOctal(C >> 6); 00160 O << toOctal(C >> 3); 00161 O << toOctal(C >> 0); 00162 break; 00163 } 00164 } 00165 } 00166 O << "\""; 00167 } 00168 00169 /// emitGlobalConstant - Print a general LLVM constant to the .s file. 00170 /// 00171 void AsmPrinter::emitGlobalConstant(const Constant *CV) { 00172 const TargetData &TD = TM.getTargetData(); 00173 00174 if (CV->isNullValue() || isa<UndefValue>(CV)) { 00175 emitZeros(TD.getTypeSize(CV->getType())); 00176 return; 00177 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { 00178 if (CVA->isString()) { 00179 O << AsciiDirective; 00180 printAsCString(O, CVA); 00181 O << "\n"; 00182 } else { // Not a string. Print the values in successive locations 00183 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) 00184 emitGlobalConstant(CVA->getOperand(i)); 00185 } 00186 return; 00187 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { 00188 // Print the fields in successive locations. Pad to align if needed! 00189 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType()); 00190 unsigned sizeSoFar = 0; 00191 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { 00192 const Constant* field = CVS->getOperand(i); 00193 00194 // Check if padding is needed and insert one or more 0s. 00195 unsigned fieldSize = TD.getTypeSize(field->getType()); 00196 unsigned padSize = ((i == e-1? cvsLayout->StructSize 00197 : cvsLayout->MemberOffsets[i+1]) 00198 - cvsLayout->MemberOffsets[i]) - fieldSize; 00199 sizeSoFar += fieldSize + padSize; 00200 00201 // Now print the actual field value 00202 emitGlobalConstant(field); 00203 00204 // Insert the field padding unless it's zero bytes... 00205 emitZeros(padSize); 00206 } 00207 assert(sizeSoFar == cvsLayout->StructSize && 00208 "Layout of constant struct may be incorrect!"); 00209 return; 00210 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { 00211 // FP Constants are printed as integer constants to avoid losing 00212 // precision... 00213 double Val = CFP->getValue(); 00214 if (CFP->getType() == Type::DoubleTy) { 00215 union DU { // Abide by C TBAA rules 00216 double FVal; 00217 uint64_t UVal; 00218 } U; 00219 U.FVal = Val; 00220 00221 if (Data64bitsDirective) 00222 O << Data64bitsDirective << U.UVal << "\t" << CommentString 00223 << " double value: " << Val << "\n"; 00224 else if (TD.isBigEndian()) { 00225 O << Data32bitsDirective << unsigned(U.UVal >> 32) 00226 << "\t" << CommentString << " double most significant word " 00227 << Val << "\n"; 00228 O << Data32bitsDirective << unsigned(U.UVal) 00229 << "\t" << CommentString << " double least significant word " 00230 << Val << "\n"; 00231 } else { 00232 O << Data32bitsDirective << unsigned(U.UVal) 00233 << "\t" << CommentString << " double least significant word " << Val 00234 << "\n"; 00235 O << Data32bitsDirective << unsigned(U.UVal >> 32) 00236 << "\t" << CommentString << " double most significant word " << Val 00237 << "\n"; 00238 } 00239 return; 00240 } else { 00241 union FU { // Abide by C TBAA rules 00242 float FVal; 00243 int32_t UVal; 00244 } U; 00245 U.FVal = Val; 00246 00247 O << Data32bitsDirective << U.UVal << "\t" << CommentString 00248 << " float " << Val << "\n"; 00249 return; 00250 } 00251 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) { 00252 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 00253 uint64_t Val = CI->getRawValue(); 00254 00255 if (Data64bitsDirective) 00256 O << Data64bitsDirective << Val << "\n"; 00257 else if (TD.isBigEndian()) { 00258 O << Data32bitsDirective << unsigned(Val >> 32) 00259 << "\t" << CommentString << " Double-word most significant word " 00260 << Val << "\n"; 00261 O << Data32bitsDirective << unsigned(Val) 00262 << "\t" << CommentString << " Double-word least significant word " 00263 << Val << "\n"; 00264 } else { 00265 O << Data32bitsDirective << unsigned(Val) 00266 << "\t" << CommentString << " Double-word least significant word " 00267 << Val << "\n"; 00268 O << Data32bitsDirective << unsigned(Val >> 32) 00269 << "\t" << CommentString << " Double-word most significant word " 00270 << Val << "\n"; 00271 } 00272 return; 00273 } 00274 } 00275 00276 const Type *type = CV->getType(); 00277 switch (type->getTypeID()) { 00278 case Type::BoolTyID: 00279 case Type::UByteTyID: case Type::SByteTyID: 00280 O << Data8bitsDirective; 00281 break; 00282 case Type::UShortTyID: case Type::ShortTyID: 00283 O << Data16bitsDirective; 00284 break; 00285 case Type::PointerTyID: 00286 case Type::UIntTyID: case Type::IntTyID: 00287 O << Data32bitsDirective; 00288 break; 00289 case Type::ULongTyID: case Type::LongTyID: 00290 assert (0 && "Should have already output double-word constant."); 00291 case Type::FloatTyID: case Type::DoubleTyID: 00292 assert (0 && "Should have already output floating point constant."); 00293 default: 00294 assert (0 && "Can't handle printing this type of thing"); 00295 break; 00296 } 00297 emitConstantValueOnly(CV); 00298 O << "\n"; 00299 }