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()) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 51 | setVector(((Vec*)(char*)RHS.Data)->Elts, RHS.getVectorLength()); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 52 | else if (isComplexInt()) |
| 53 | setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag()); |
| 54 | else if (isComplexFloat()) |
| 55 | setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag()); |
| 56 | else if (isLValue()) |
| 57 | setLValue(RHS.getLValueBase(), RHS.getLValueOffset()); |
| 58 | return *this; |
| 59 | } |
| 60 | |
| 61 | void APValue::MakeUninit() { |
| 62 | if (Kind == Int) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 63 | ((APSInt*)(char*)Data)->~APSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 64 | else if (Kind == Float) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 65 | ((APFloat*)(char*)Data)->~APFloat(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 66 | else if (Kind == Vector) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 67 | ((Vec*)(char*)Data)->~Vec(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 68 | else if (Kind == ComplexInt) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 69 | ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 70 | else if (Kind == ComplexFloat) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 71 | ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 72 | else if (Kind == LValue) { |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 73 | ((LV*)(char*)Data)->~LV(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 74 | } |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 75 | Kind = Uninitialized; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void APValue::dump() const { |
| 79 | print(llvm::errs()); |
| 80 | llvm::errs() << '\n'; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static double GetApproxValue(const llvm::APFloat &F) { |
| 84 | llvm::APFloat V = F; |
| 85 | bool ignored; |
| 86 | V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, |
| 87 | &ignored); |
| 88 | return V.convertToDouble(); |
| 89 | } |
| 90 | |
| 91 | void APValue::print(llvm::raw_ostream &OS) const { |
| 92 | switch (getKind()) { |
| 93 | default: assert(0 && "Unknown APValue kind!"); |
| 94 | case Uninitialized: |
| 95 | OS << "Uninitialized"; |
| 96 | return; |
| 97 | case Int: |
| 98 | OS << "Int: " << getInt(); |
| 99 | return; |
| 100 | case Float: |
| 101 | OS << "Float: " << GetApproxValue(getFloat()); |
| 102 | return; |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 103 | case Vector: |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 104 | OS << "Vector: " << getVectorElt(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | for (unsigned i = 1; i != getVectorLength(); ++i) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 106 | OS << ", " << getVectorElt(i); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 107 | return; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 108 | case ComplexInt: |
| 109 | OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag(); |
| 110 | return; |
| 111 | case ComplexFloat: |
| 112 | OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal()) |
| 113 | << ", " << GetApproxValue(getComplexFloatImag()); |
| 114 | case LValue: |
| 115 | OS << "LValue: <todo>"; |
| 116 | return; |
| 117 | } |
| 118 | } |
| 119 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 120 | Expr* APValue::getLValueBase() const { |
| 121 | assert(isLValue() && "Invalid accessor"); |
| 122 | return ((const LV*)(const void*)Data)->Base; |
| 123 | } |
| 124 | |
| 125 | CharUnits APValue::getLValueOffset() const { |
| 126 | assert(isLValue() && "Invalid accessor"); |
| 127 | return ((const LV*)(const void*)Data)->Offset; |
| 128 | } |
| 129 | |
| 130 | void APValue::setLValue(Expr *B, const CharUnits &O) { |
| 131 | assert(isLValue() && "Invalid accessor"); |
| 132 | ((LV*)(char*)Data)->Base = B; |
| 133 | ((LV*)(char*)Data)->Offset = O; |
| 134 | } |
| 135 | |
| 136 | void APValue::MakeLValue() { |
| 137 | assert(isUninit() && "Bad state change"); |
| 138 | new ((void*)(char*)Data) LV(); |
| 139 | Kind = LValue; |
| 140 | } |
| 141 | |