Add support for casting operators
[oota-llvm.git] / lib / VMCore / ConstPoolVals.cpp
1 //===-- llvm/ConstPoolVals.cpp - Implement Constant Value nodes -*- C++ -*--=//
2 //
3 // This file implements the ConstPoolVal class and associated functions.
4 //
5 //===---------------------------------------------------------------------===//
6
7 #include "llvm/Type.h"
8 #include "llvm/Value.h"
9 #include "llvm/ConstPoolVals.h"
10
11
12 //===--------------------------------------------------------------------------
13 // External functions
14 //
15
16 // Convenience functions to get the value of an integer constant, for an
17 // appropriate integer or non-integer type that can be held in an integer.
18 // The type of the argument must be the following:
19 //   GetSignedIntConstantValue:   signed integer or bool
20 //   GetUnsignedIntConstantValue: unsigned integer, bool, or pointer
21 //   GetConstantValueAsSignedInt: any of the above, but the value
22 //                                must fit into a int64_t.
23 // 
24 // isValidConstant is set to true if a valid constant was found.
25 // 
26
27 int64_t
28 GetSignedIntConstantValue(const Value* val, bool& isValidConstant)
29 {
30   int64_t intValue = 0;
31   isValidConstant = false;
32   
33   if (val->getValueType() == Value::ConstantVal)
34     {
35       switch(val->getType()->getPrimitiveID())
36         {
37         case Type::BoolTyID:
38           intValue = ((ConstPoolBool*) val)->getValue()? 1 : 0;
39           isValidConstant = true;
40           break;
41         case Type::SByteTyID:
42         case Type::ShortTyID:
43         case Type::IntTyID:
44         case Type::LongTyID:
45           intValue = ((ConstPoolSInt*) val)->getValue();
46           isValidConstant = true;
47           break;
48         default:
49           break;
50         }
51     }
52   
53   return intValue;
54 }
55
56 uint64_t
57 GetUnsignedIntConstantValue(const Value* val, bool& isValidConstant)
58 {
59   uint64_t intValue = 0;
60   isValidConstant = false;
61   
62   if (val->getValueType() == Value::ConstantVal)
63     {
64       switch(val->getType()->getPrimitiveID())
65         {
66         case Type::BoolTyID:
67           intValue = ((ConstPoolBool*) val)->getValue()? 1 : 0;
68           isValidConstant = true;
69           break;
70         case Type::UByteTyID:
71         case Type::UShortTyID:
72         case Type::UIntTyID:
73         case Type::ULongTyID:
74         case Type::PointerTyID:
75           intValue = ((ConstPoolUInt*) val)->getValue();
76           isValidConstant = true;
77           break;
78         default:
79           break;
80         }
81     }
82   
83   return intValue;
84 }
85
86
87 int64_t
88 GetConstantValueAsSignedInt(const Value* val, bool& isValidConstant)
89 {
90   int64_t intValue = 0;
91   
92   if (val->getType()->isSigned())
93     {
94       intValue = GetSignedIntConstantValue(val, isValidConstant);
95     }
96   else                          // non-numeric types will fall here
97     {
98       uint64_t uintValue = GetUnsignedIntConstantValue(val, isValidConstant);
99       if (isValidConstant && uintValue < INT64_MAX)     // then safe to cast to signed
100         intValue = (int64_t) uintValue;
101       else 
102         isValidConstant = false;
103     }
104   
105   return intValue;
106 }
107
108 //===--------------------------------------------------------------------------