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" |
| 15 | #include "llvm/Support/raw_ostream.h" |
| 16 | using namespace clang; |
| 17 | |
| 18 | |
| 19 | const APValue &APValue::operator=(const APValue &RHS) { |
| 20 | if (Kind != RHS.Kind) { |
| 21 | MakeUninit(); |
| 22 | if (RHS.isInt()) |
| 23 | MakeInt(); |
| 24 | else if (RHS.isFloat()) |
| 25 | MakeFloat(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 26 | else if (RHS.isVector()) |
| 27 | MakeVector(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 28 | else if (RHS.isComplexInt()) |
| 29 | MakeComplexInt(); |
| 30 | else if (RHS.isComplexFloat()) |
| 31 | MakeComplexFloat(); |
| 32 | else if (RHS.isLValue()) |
| 33 | MakeLValue(); |
| 34 | } |
| 35 | if (isInt()) |
| 36 | setInt(RHS.getInt()); |
| 37 | else if (isFloat()) |
| 38 | setFloat(RHS.getFloat()); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 39 | else if (isVector()) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 40 | setVector(((Vec*)(char*)RHS.Data)->Elts, RHS.getVectorLength()); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 41 | else if (isComplexInt()) |
| 42 | setComplexInt(RHS.getComplexIntReal(), RHS.getComplexIntImag()); |
| 43 | else if (isComplexFloat()) |
| 44 | setComplexFloat(RHS.getComplexFloatReal(), RHS.getComplexFloatImag()); |
| 45 | else if (isLValue()) |
| 46 | setLValue(RHS.getLValueBase(), RHS.getLValueOffset()); |
| 47 | return *this; |
| 48 | } |
| 49 | |
| 50 | void APValue::MakeUninit() { |
| 51 | if (Kind == Int) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 52 | ((APSInt*)(char*)Data)->~APSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 53 | else if (Kind == Float) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 54 | ((APFloat*)(char*)Data)->~APFloat(); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 55 | else if (Kind == Vector) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 56 | ((Vec*)(char*)Data)->~Vec(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 57 | else if (Kind == ComplexInt) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 58 | ((ComplexAPSInt*)(char*)Data)->~ComplexAPSInt(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 59 | else if (Kind == ComplexFloat) |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 60 | ((ComplexAPFloat*)(char*)Data)->~ComplexAPFloat(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 61 | else if (Kind == LValue) { |
Douglas Gregor | 9830046 | 2009-09-08 19:57:33 +0000 | [diff] [blame] | 62 | ((LV*)(char*)Data)->~LV(); |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 63 | } |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 64 | Kind = Uninitialized; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void APValue::dump() const { |
| 68 | print(llvm::errs()); |
| 69 | llvm::errs() << '\n'; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static double GetApproxValue(const llvm::APFloat &F) { |
| 73 | llvm::APFloat V = F; |
| 74 | bool ignored; |
| 75 | V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, |
| 76 | &ignored); |
| 77 | return V.convertToDouble(); |
| 78 | } |
| 79 | |
| 80 | void APValue::print(llvm::raw_ostream &OS) const { |
| 81 | switch (getKind()) { |
| 82 | default: assert(0 && "Unknown APValue kind!"); |
| 83 | case Uninitialized: |
| 84 | OS << "Uninitialized"; |
| 85 | return; |
| 86 | case Int: |
| 87 | OS << "Int: " << getInt(); |
| 88 | return; |
| 89 | case Float: |
| 90 | OS << "Float: " << GetApproxValue(getFloat()); |
| 91 | return; |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 92 | case Vector: |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 93 | OS << "Vector: " << getVectorElt(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | for (unsigned i = 1; i != getVectorLength(); ++i) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 95 | OS << ", " << getVectorElt(i); |
Nate Begeman | 3d309f9 | 2009-01-18 01:01:34 +0000 | [diff] [blame] | 96 | return; |
Chris Lattner | 64c34f1 | 2008-11-16 07:46:48 +0000 | [diff] [blame] | 97 | case ComplexInt: |
| 98 | OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag(); |
| 99 | return; |
| 100 | case ComplexFloat: |
| 101 | OS << "ComplexFloat: " << GetApproxValue(getComplexFloatReal()) |
| 102 | << ", " << GetApproxValue(getComplexFloatImag()); |
| 103 | case LValue: |
| 104 | OS << "LValue: <todo>"; |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | |