blob: 1e104fccc8d72beff023e9f696c78d3e4b29b1e5 [file] [log] [blame]
Chris Lattner6a8e0f52004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
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"
19using namespace llvm;
20
21bool AsmPrinter::doInitialization(Module &M) {
22 Mang = new Mangler(M);
23 return false;
24}
25
26bool AsmPrinter::doFinalization(Module &M) {
27 delete Mang; Mang = 0;
28 return false;
29}
30
31void AsmPrinter::setupMachineFunction(MachineFunction &MF) {
32 // What's my mangled name?
33 CurrentFnName = Mang->getValueName((Value*)MF.getFunction());
34
35}
36
37
38
39// Print out the specified constant, without a storage class. Only the
40// constants valid in constant expressions can occur here.
41void AsmPrinter::emitConstantValueOnly(const Constant *CV) {
42 if (CV->isNullValue())
43 O << "0";
44 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
45 assert(CB == ConstantBool::True);
46 O << "1";
47 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
48 if (((CI->getValue() << 32) >> 32) == CI->getValue())
49 O << CI->getValue();
50 else
51 O << (unsigned long long)CI->getValue();
52 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
53 O << CI->getValue();
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)) {
66 O << "(";
67 emitConstantValueOnly(ptrVal);
68 O << ") + " << Offset;
69 } else {
70 emitConstantValueOnly(ptrVal);
71 }
72 break;
73 }
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();
80
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");
92 O << "(";
93 emitConstantValueOnly(Op);
94 O << ")";
95 break;
96 }
97 case Instruction::Add:
98 O << "(";
99 emitConstantValueOnly(CE->getOperand(0));
100 O << ") + (";
101 emitConstantValueOnly(CE->getOperand(1));
102 O << ")";
103 break;
104 default:
105 assert(0 && "Unsupported operator!");
106 }
107 } else {
108 assert(0 && "Unknown constant value!");
109 }
110}