//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===// // // The LLVM Compiler Infrastructure // // This file was developed by the LLVM research group and is distributed under // the University of Illinois Open Source License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // This file implements the AsmPrinter class. // //===----------------------------------------------------------------------===// #include "llvm/CodeGen/AsmPrinter.h" #include "llvm/Constants.h" #include "llvm/Instruction.h" #include "llvm/Support/Mangler.h" #include "llvm/Target/TargetMachine.h" using namespace llvm; bool AsmPrinter::doInitialization(Module &M) { Mang = new Mangler(M, UsesUnderscorePrefix); return false; } bool AsmPrinter::doFinalization(Module &M) { delete Mang; Mang = 0; return false; } void AsmPrinter::setupMachineFunction(MachineFunction &MF) { // What's my mangled name? CurrentFnName = Mang->getValueName((Value*)MF.getFunction()); } // Print out the specified constant, without a storage class. Only the // constants valid in constant expressions can occur here. void AsmPrinter::emitConstantValueOnly(const Constant *CV) { if (CV->isNullValue()) O << "0"; else if (const ConstantBool *CB = dyn_cast(CV)) { assert(CB == ConstantBool::True); O << "1"; } else if (const ConstantSInt *CI = dyn_cast(CV)) if (((CI->getValue() << 32) >> 32) == CI->getValue()) O << CI->getValue(); else O << (unsigned long long)CI->getValue(); else if (const ConstantUInt *CI = dyn_cast(CV)) O << CI->getValue(); else if (isa((Value*)CV)) // This is a constant address for a global variable or function. Use the // name of the variable or function as the address value. O << Mang->getValueName(CV); else if (const ConstantExpr *CE = dyn_cast(CV)) { const TargetData &TD = TM.getTargetData(); switch(CE->getOpcode()) { case Instruction::GetElementPtr: { // generate a symbolic expression for the byte address const Constant *ptrVal = CE->getOperand(0); std::vector idxVec(CE->op_begin()+1, CE->op_end()); if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) { O << "("; emitConstantValueOnly(ptrVal); O << ") + " << Offset; } else { emitConstantValueOnly(ptrVal); } break; } case Instruction::Cast: { // Support only non-converting or widening casts for now, that is, ones // that do not involve a change in value. This assertion is really gross, // and may not even be a complete check. Constant *Op = CE->getOperand(0); const Type *OpTy = Op->getType(), *Ty = CE->getType(); // Remember, kids, pointers can be losslessly converted back and forth // into 32-bit or wider integers, regardless of signedness. :-P assert(((isa(OpTy) && (Ty == Type::LongTy || Ty == Type::ULongTy || Ty == Type::IntTy || Ty == Type::UIntTy)) || (isa(Ty) && (OpTy == Type::LongTy || OpTy == Type::ULongTy || OpTy == Type::IntTy || OpTy == Type::UIntTy)) || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy)) && OpTy->isLosslesslyConvertibleTo(Ty)))) && "FIXME: Don't yet support this kind of constant cast expr"); O << "("; emitConstantValueOnly(Op); O << ")"; break; } case Instruction::Add: O << "("; emitConstantValueOnly(CE->getOperand(0)); O << ") + ("; emitConstantValueOnly(CE->getOperand(1)); O << ")"; break; default: assert(0 && "Unsupported operator!"); } } else { assert(0 && "Unknown constant value!"); } }