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 { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 23 | struct LVBase { |
| 24 | const Expr *Base; |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 25 | CharUnits Offset; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 26 | unsigned PathLength; |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 27 | }; |
| 28 | } |
| 29 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 30 | struct APValue::LV : LVBase { |
| 31 | static const unsigned InlinePathSpace = |
| 32 | (MaxSize - sizeof(LVBase)) / sizeof(LValuePathEntry); |
| 33 | |
| 34 | /// Path - The sequence of base classes, fields and array indices to follow to |
| 35 | /// walk from Base to the subobject. When performing GCC-style folding, there |
| 36 | /// may not be such a path. |
| 37 | union { |
| 38 | LValuePathEntry Path[InlinePathSpace]; |
| 39 | LValuePathEntry *PathPtr; |
| 40 | }; |
| 41 | |
| 42 | LV() { PathLength = (unsigned)-1; } |
| 43 | ~LV() { if (hasPathPtr()) delete [] PathPtr; } |
| 44 | |
| 45 | void allocPath() { |
| 46 | if (hasPathPtr()) PathPtr = new LValuePathEntry[PathLength]; |
| 47 | } |
| 48 | |
| 49 | bool hasPath() const { return PathLength != (unsigned)-1; } |
| 50 | bool hasPathPtr() const { return hasPath() && PathLength > InlinePathSpace; } |
| 51 | |
| 52 | LValuePathEntry *getPath() { return hasPathPtr() ? PathPtr : Path; } |
Richard Smith | 38dce9b | 2011-11-07 07:31:09 +0000 | [diff] [blame^] | 53 | const LValuePathEntry *getPath() const { |
| 54 | return hasPathPtr() ? PathPtr : Path; |
| 55 | } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 58 | APValue::APValue(const Expr* B) : Kind(Uninitialized) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 59 | MakeLValue(); |
| 60 | setLValue(B, CharUnits::Zero(), ArrayRef<LValuePathEntry>()); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 61 | } |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 62 | |
| 63 | const APValue &APValue::operator=(const APValue &RHS) { |
| 64 | if (Kind != RHS.Kind) { |
| 65 | MakeUninit(); |
| 66 | if (RHS.isInt()) |
| 67 | MakeInt(); |
| 68 | else if (RHS.isFloat()) |
| 69 | MakeFloat(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 70 | else if (RHS.isVector()) |
| 71 | MakeVector(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 72 | else if (RHS.isComplexInt()) |
| 73 | MakeComplexInt(); |
| 74 | else if (RHS.isComplexFloat()) |
| 75 | MakeComplexFloat(); |
| 76 | else if (RHS.isLValue()) |
| 77 | MakeLValue(); |
| 78 | } |
| 79 | if (isInt()) |
| 80 | setInt(RHS.getInt()); |
| 81 | else if (isFloat()) |
| 82 | setFloat(RHS.getFloat()); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 83 | else if (isVector()) |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 84 | setVector(((const Vec *)(const char *)RHS.Data)->Elts, |
| 85 | RHS.getVectorLength()); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 86 | else if (isComplexInt()) |
| 87 | setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag()); |
| 88 | else if (isComplexFloat()) |
| 89 | setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag()); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 90 | else if (isLValue()) { |
| 91 | if (RHS.hasLValuePath()) |
| 92 | setLValue(RHS.getLValueBase(), RHS.getLValueOffset(),RHS.getLValuePath()); |
| 93 | else |
| 94 | setLValue(RHS.getLValueBase(), RHS.getLValueOffset(), NoLValuePath()); |
| 95 | } |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 96 | return *this; |
| 97 | } |
| 98 | |
| 99 | void APValue::MakeUninit() { |
| 100 | if (Kind == Int) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 101 | ((APSInt*)(char*)Data)->~APSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 102 | else if (Kind == Float) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 103 | ((APFloat*)(char*)Data)->~APFloat(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 104 | else if (Kind == Vector) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 105 | ((Vec*)(char*)Data)->~Vec(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 106 | else if (Kind == ComplexInt) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 107 | ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 108 | else if (Kind == ComplexFloat) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 109 | ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 110 | else if (Kind == LValue) { |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 111 | ((LV*)(char*)Data)->~LV(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 112 | } |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 113 | Kind = Uninitialized; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void APValue::dump() const { |
| 117 | print(llvm::errs()); |
| 118 | llvm::errs() << '\n'; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static double GetApproxValue(const llvm::APFloat &F) { |
| 122 | llvm::APFloat V = F; |
| 123 | bool ignored; |
| 124 | V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, |
| 125 | &ignored); |
| 126 | return V.convertToDouble(); |
| 127 | } |
| 128 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 129 | void APValue::print(raw_ostream &OS) const { |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 130 | switch (getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 131 | default: llvm_unreachable("Unknown APValue kind!"); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 132 | case Uninitialized: |
| 133 | OS << "Uninitialized"; |
| 134 | return; |
| 135 | case Int: |
| 136 | OS << "Int: " << getInt(); |
| 137 | return; |
| 138 | case Float: |
| 139 | OS << "Float: " << GetApproxValue(getFloat()); |
| 140 | return; |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 141 | case Vector: |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 142 | OS << "Vector: " << getVectorElt(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | for (unsigned i = 1; i != getVectorLength(); ++i) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 144 | OS << ", " << getVectorElt(i); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 145 | return; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 146 | case ComplexInt: |
| 147 | OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag(); |
| 148 | return; |
| 149 | case ComplexFloat: |
| 150 | OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal()) |
| 151 | << ", " << GetApproxValue(getComplexFloatImag()); |
| 152 | case LValue: |
| 153 | OS << "LValue: <todo>"; |
| 154 | return; |
| 155 | } |
| 156 | } |
| 157 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 158 | static void WriteShortAPValueToStream(raw_ostream& Out, |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 159 | const APValue& V) { |
| 160 | switch (V.getKind()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 161 | default: llvm_unreachable("Unknown APValue kind!"); |
Jeffrey Yasskin | 5b106a8 | 2011-07-18 16:43:53 +0000 | [diff] [blame] | 162 | case APValue::Uninitialized: |
| 163 | Out << "Uninitialized"; |
| 164 | break; |
| 165 | case APValue::Int: |
| 166 | Out << V.getInt(); |
| 167 | break; |
| 168 | case APValue::Float: |
| 169 | Out << GetApproxValue(V.getFloat()); |
| 170 | break; |
| 171 | case APValue::Vector: |
| 172 | Out << '['; |
| 173 | WriteShortAPValueToStream(Out, V.getVectorElt(0)); |
| 174 | for (unsigned i = 1; i != V.getVectorLength(); ++i) { |
| 175 | Out << ", "; |
| 176 | WriteShortAPValueToStream(Out, V.getVectorElt(i)); |
| 177 | } |
| 178 | Out << ']'; |
| 179 | break; |
| 180 | case APValue::ComplexInt: |
| 181 | Out << V.getComplexIntReal() << "+" << V.getComplexIntImag() << "i"; |
| 182 | break; |
| 183 | case APValue::ComplexFloat: |
| 184 | Out << GetApproxValue(V.getComplexFloatReal()) << "+" |
| 185 | << GetApproxValue(V.getComplexFloatImag()) << "i"; |
| 186 | break; |
| 187 | case APValue::LValue: |
| 188 | Out << "LValue: <todo>"; |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 194 | const APValue &V) { |
| 195 | llvm::SmallString<64> Buffer; |
| 196 | llvm::raw_svector_ostream Out(Buffer); |
| 197 | WriteShortAPValueToStream(Out, V); |
| 198 | return DB << Out.str(); |
| 199 | } |
| 200 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 201 | const Expr* APValue::getLValueBase() const { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 202 | assert(isLValue() && "Invalid accessor"); |
| 203 | return ((const LV*)(const void*)Data)->Base; |
| 204 | } |
| 205 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 206 | CharUnits &APValue::getLValueOffset() { |
| 207 | assert(isLValue() && "Invalid accessor"); |
| 208 | return ((LV*)(void*)Data)->Offset; |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 211 | bool APValue::hasLValuePath() const { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 212 | assert(isLValue() && "Invalid accessor"); |
Richard Smith | 38dce9b | 2011-11-07 07:31:09 +0000 | [diff] [blame^] | 213 | return ((const LV*)(const char*)Data)->hasPath(); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | ArrayRef<APValue::LValuePathEntry> APValue::getLValuePath() const { |
| 217 | assert(isLValue() && hasLValuePath() && "Invalid accessor"); |
Richard Smith | 38dce9b | 2011-11-07 07:31:09 +0000 | [diff] [blame^] | 218 | const LV &LVal = *((const LV*)(const char*)Data); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 219 | return ArrayRef<LValuePathEntry>(LVal.getPath(), LVal.PathLength); |
| 220 | } |
| 221 | |
| 222 | void APValue::setLValue(const Expr *B, const CharUnits &O, NoLValuePath) { |
| 223 | assert(isLValue() && "Invalid accessor"); |
| 224 | LV &LVal = *((LV*)(char*)Data); |
| 225 | LVal.Base = B; |
| 226 | LVal.Offset = O; |
| 227 | LVal.PathLength = (unsigned)-1; |
| 228 | } |
| 229 | |
| 230 | void APValue::setLValue(const Expr *B, const CharUnits &O, |
| 231 | ArrayRef<LValuePathEntry> Path) { |
| 232 | assert(isLValue() && "Invalid accessor"); |
| 233 | LV &LVal = *((LV*)(char*)Data); |
| 234 | LVal.Base = B; |
| 235 | LVal.Offset = O; |
| 236 | LVal.PathLength = Path.size(); |
Richard Smith | 38dce9b | 2011-11-07 07:31:09 +0000 | [diff] [blame^] | 237 | LVal.allocPath(); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 238 | memcpy(LVal.getPath(), Path.data(), Path.size() * sizeof(LValuePathEntry)); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void APValue::MakeLValue() { |
| 242 | assert(isUninit() && "Bad state change"); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 243 | assert(sizeof(LV) <= MaxSize && "LV too big"); |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 244 | new ((void*)(char*)Data) LV(); |
| 245 | Kind = LValue; |
| 246 | } |