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" |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
| 17 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
David Blaikie | 9fe8c74 | 2011-09-23 05:35:21 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | struct LV { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 24 | const Expr* Base; |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 25 | CharUnits Offset; |
| 26 | }; |
| 27 | } |
| 28 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 29 | APValue::APValue(const Expr* B) : Kind(Uninitialized) { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 30 | MakeLValue(); setLValue(B, CharUnits::Zero()); |
| 31 | } |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 32 | |
| 33 | const APValue &APValue::operator=(const APValue &RHS) { |
| 34 | if (Kind != RHS.Kind) { |
| 35 | MakeUninit(); |
| 36 | if (RHS.isInt()) |
| 37 | MakeInt(); |
| 38 | else if (RHS.isFloat()) |
| 39 | MakeFloat(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 40 | else if (RHS.isVector()) |
| 41 | MakeVector(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 42 | else if (RHS.isComplexInt()) |
| 43 | MakeComplexInt(); |
| 44 | else if (RHS.isComplexFloat()) |
| 45 | MakeComplexFloat(); |
| 46 | else if (RHS.isLValue()) |
| 47 | MakeLValue(); |
| 48 | } |
| 49 | if (isInt()) |
| 50 | setInt(RHS.getInt()); |
| 51 | else if (isFloat()) |
| 52 | setFloat(RHS.getFloat()); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 53 | else if (isVector()) |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 54 | setVector(((const Vec *)(const char *)RHS.Data)->Elts, |
| 55 | RHS.getVectorLength()); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 56 | else if (isComplexInt()) |
| 57 | setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag()); |
| 58 | else if (isComplexFloat()) |
| 59 | setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag()); |
| 60 | else if (isLValue()) |
| 61 | setLValue(RHS.getLValueBase(), RHS.getLValueOffset()); |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | void APValue::MakeUninit() { |
| 66 | if (Kind == Int) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 67 | ((APSInt*)(char*)Data)->~APSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 68 | else if (Kind == Float) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 69 | ((APFloat*)(char*)Data)->~APFloat(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 70 | else if (Kind == Vector) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 71 | ((Vec*)(char*)Data)->~Vec(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 72 | else if (Kind == ComplexInt) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 73 | ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 74 | else if (Kind == ComplexFloat) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 75 | ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 76 | else if (Kind == LValue) { |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 77 | ((LV*)(char*)Data)->~LV(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 78 | } |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 79 | Kind = Uninitialized; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void APValue::dump() const { |
| 83 | print(llvm::errs()); |
| 84 | llvm::errs() << '\n'; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static double GetApproxValue(const llvm::APFloat &F) { |
| 88 | llvm::APFloat V = F; |
| 89 | bool ignored; |
| 90 | V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, |
| 91 | &ignored); |
| 92 | return V.convertToDouble(); |
| 93 | } |
| 94 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 95 | void APValue::print(raw_ostream &OS) const { |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 96 | switch (getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 97 | default: llvm_unreachable("Unknown APValue kind!"); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 98 | case Uninitialized: |
| 99 | OS << "Uninitialized"; |
| 100 | return; |
| 101 | case Int: |
| 102 | OS << "Int: " << getInt(); |
| 103 | return; |
| 104 | case Float: |
| 105 | OS << "Float: " << GetApproxValue(getFloat()); |
| 106 | return; |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 107 | case Vector: |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 108 | OS << "Vector: " << getVectorElt(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | for (unsigned i = 1; i != getVectorLength(); ++i) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 110 | OS << ", " << getVectorElt(i); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 111 | return; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 112 | case ComplexInt: |
| 113 | OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag(); |
| 114 | return; |
| 115 | case ComplexFloat: |
| 116 | OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal()) |
| 117 | << ", " << GetApproxValue(getComplexFloatImag()); |
| 118 | case LValue: |
| 119 | OS << "LValue: <todo>"; |
| 120 | return; |
| 121 | } |
| 122 | } |
| 123 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 124 | static void WriteShortAPValueToStream(raw_ostream& Out, |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 125 | const APValue& V) { |
| 126 | switch (V.getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 127 | default: llvm_unreachable("Unknown APValue kind!"); |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 128 | case APValue::Uninitialized: |
| 129 | Out << "Uninitialized"; |
| 130 | break; |
| 131 | case APValue::Int: |
| 132 | Out << V.getInt(); |
| 133 | break; |
| 134 | case APValue::Float: |
| 135 | Out << GetApproxValue(V.getFloat()); |
| 136 | break; |
| 137 | case APValue::Vector: |
| 138 | Out << '['; |
| 139 | WriteShortAPValueToStream(Out, V.getVectorElt(0)); |
| 140 | for (unsigned i = 1; i != V.getVectorLength(); ++i) { |
| 141 | Out << ", "; |
| 142 | WriteShortAPValueToStream(Out, V.getVectorElt(i)); |
| 143 | } |
| 144 | Out << ']'; |
| 145 | break; |
| 146 | case APValue::ComplexInt: |
| 147 | Out << V.getComplexIntReal() << "+" << V.getComplexIntImag() << "i"; |
| 148 | break; |
| 149 | case APValue::ComplexFloat: |
| 150 | Out << GetApproxValue(V.getComplexFloatReal()) << "+" |
| 151 | << GetApproxValue(V.getComplexFloatImag()) << "i"; |
| 152 | break; |
| 153 | case APValue::LValue: |
| 154 | Out << "LValue: <todo>"; |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 160 | const APValue &V) { |
| 161 | llvm::SmallString<64> Buffer; |
| 162 | llvm::raw_svector_ostream Out(Buffer); |
| 163 | WriteShortAPValueToStream(Out, V); |
| 164 | return DB << Out.str(); |
| 165 | } |
| 166 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 167 | const Expr* APValue::getLValueBase() const { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 168 | assert(isLValue() && "Invalid accessor"); |
| 169 | return ((const LV*)(const void*)Data)->Base; |
| 170 | } |
| 171 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame^] | 172 | CharUnits &APValue::getLValueOffset() { |
| 173 | assert(isLValue() && "Invalid accessor"); |
| 174 | return ((LV*)(void*)Data)->Offset; |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 177 | void APValue::setLValue(const Expr *B, const CharUnits &O) { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 178 | assert(isLValue() && "Invalid accessor"); |
| 179 | ((LV*)(char*)Data)->Base = B; |
| 180 | ((LV*)(char*)Data)->Offset = O; |
| 181 | } |
| 182 | |
| 183 | void APValue::MakeLValue() { |
| 184 | assert(isUninit() && "Bad state change"); |
| 185 | new ((void*)(char*)Data) LV(); |
| 186 | Kind = LValue; |
| 187 | } |