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, UsesUnderscorePrefix);
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!");