Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 1 | //===--- APValue.cpp - Union class for APFloat/APSInt/Complex -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the APValue class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/APValue.h" |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 15 | #include "clang/AST/CharUnits.h" |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | using namespace clang; |
| 18 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 19 | namespace { |
| 20 | struct LV { |
| 21 | Expr* Base; |
| 22 | CharUnits Offset; |
| 23 | }; |
| 24 | } |
| 25 | |
| 26 | APValue::APValue(Expr* B) : Kind(Uninitialized) { |
| 27 | MakeLValue(); setLValue(B, CharUnits::Zero()); |
| 28 | } |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 29 | |
| 30 | const APValue &APValue::operator=(const APValue &RHS) { |
| 31 | if (Kind != RHS.Kind) { |
| 32 | MakeUninit(); |
| 33 | if (RHS.isInt()) |
| 34 | MakeInt(); |
| 35 | else if (RHS.isFloat()) |
| 36 | MakeFloat(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 37 | else if (RHS.isVector()) |
| 38 | MakeVector(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 39 | else if (RHS.isComplexInt()) |
| 40 | MakeComplexInt(); |
| 41 | else if (RHS.isComplexFloat()) |
| 42 | MakeComplexFloat(); |
| 43 | else if (RHS.isLValue()) |
| 44 | MakeLValue(); |
| 45 | } |
| 46 | if (isInt()) |
| 47 | setInt(RHS.getInt()); |
| 48 | else if (isFloat()) |
| 49 | setFloat(RHS.getFloat()); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 50 | else if (isVector()) |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 51 | setVector(((const Vec *)(const char *)RHS.Data)->Elts, |
| 52 | RHS.getVectorLength()); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 53 | else if (isComplexInt()) |
| 54 | setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag()); |
| 55 | else if (isComplexFloat()) |
| 56 | setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag()); |
| 57 | else if (isLValue()) |
| 58 | setLValue(RHS.getLValueBase(), RHS.getLValueOffset()); |
| 59 | return *this; |
| 60 | } |
| 61 | |
| 62 | void APValue::MakeUninit() { |
| 63 | if (Kind == Int) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 64 | ((APSInt*)(char*)Data)->~APSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 65 | else if (Kind == Float) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 66 | ((APFloat*)(char*)Data)->~APFloat(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 67 | else if (Kind == Vector) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 68 | ((Vec*)(char*)Data)->~Vec(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 69 | else if (Kind == ComplexInt) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 70 | ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 71 | else if (Kind == ComplexFloat) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 72 | ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 73 | else if (Kind == LValue) { |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 74 | ((LV*)(char*)Data)->~LV(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 75 | } |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 76 | Kind = Uninitialized; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void APValue::dump() const { |
| 80 | print(llvm::errs()); |
| 81 | llvm::errs() << '\n'; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static double GetApproxValue(const llvm::APFloat &F) { |
| 85 | llvm::APFloat V = F; |
| 86 | bool ignored; |
| 87 | V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, |
| 88 | &ignored); |
| 89 | return V.convertToDouble(); |
| 90 | } |
| 91 | |
| 92 | void APValue::print(llvm::raw_ostream &OS) const { |
| 93 | switch (getKind()) { |
| 94 | default: assert(0 && "Unknown APValue kind!"); |
| 95 | case Uninitialized: |
| 96 | OS << "Uninitialized"; |
| 97 | return; |
| 98 | case Int: |
| 99 | OS << "Int: " << getInt(); |
| 100 | return; |
| 101 | case Float: |
| 102 | OS << "Float: " << GetApproxValue(getFloat()); |
| 103 | return; |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 104 | case Vector: |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 105 | OS << "Vector: " << getVectorElt(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | for (unsigned i = 1; i != getVectorLength(); ++i) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 107 | OS << ", " << getVectorElt(i); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 108 | return; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 109 | case ComplexInt: |
| 110 | OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag(); |
| 111 | return; |
| 112 | case ComplexFloat: |
| 113 | OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal()) |
| 114 | << ", " << GetApproxValue(getComplexFloatImag()); |
| 115 | case LValue: |
| 116 | OS << "LValue: <todo>"; |
| 117 | return; |
| 118 | } |
| 119 | } |
| 120 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 121 | Expr* APValue::getLValueBase() const { |
| 122 | assert(isLValue() && "Invalid accessor"); |
| 123 | return ((const LV*)(const void*)Data)->Base; |
| 124 | } |
| 125 | |
| 126 | CharUnits APValue::getLValueOffset() const { |
| 127 | assert(isLValue() && "Invalid accessor"); |
| 128 | return ((const LV*)(const void*)Data)->Offset; |
| 129 | } |
| 130 | |
| 131 | void APValue::setLValue(Expr *B, const CharUnits &O) { |
| 132 | assert(isLValue() && "Invalid accessor"); |
| 133 | ((LV*)(char*)Data)->Base = B; |
| 134 | ((LV*)(char*)Data)->Offset = O; |
| 135 | } |
| 136 | |
| 137 | void APValue::MakeLValue() { |
| 138 | assert(isUninit() && "Bad state change"); |
| 139 | new ((void*)(char*)Data) LV(); |
| 140 | Kind = LValue; |
| 141 | } |
| 142 | |