diff options
author | Alex Lorenz <arphaman@gmail.com> | 2015-06-23 18:22:10 +0000 |
---|---|---|
committer | Alex Lorenz <arphaman@gmail.com> | 2015-06-23 18:22:10 +0000 |
commit | 6f8a6be145875d96ed4e4ee78099805ddabfaaa0 (patch) | |
tree | 407145368320891ca76dab8f73faa0795e2e3f55 /llvm/lib/Support/APSInt.cpp | |
parent | a5d9c1d32fe0573b31cbbe5cc6d892fcebb92e9c (diff) | |
download | llvm-6f8a6be145875d96ed4e4ee78099805ddabfaaa0.zip llvm-6f8a6be145875d96ed4e4ee78099805ddabfaaa0.tar.gz llvm-6f8a6be145875d96ed4e4ee78099805ddabfaaa0.tar.bz2 |
ADT: Add a string APSInt constructor.
This commit moves the APSInt initialization code that's used by
the LLLexer class into a new APSInt constructor that constructs
APSInts from strings.
This change is useful for MIR Serialization, as it would allow
the MILexer class to use the same APSInt initialization as
LLexer when parsing immediate machine operands.
llvm-svn: 240436
Diffstat (limited to 'llvm/lib/Support/APSInt.cpp')
-rw-r--r-- | llvm/lib/Support/APSInt.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/llvm/lib/Support/APSInt.cpp b/llvm/lib/Support/APSInt.cpp index 73acafa..975457c 100644 --- a/llvm/lib/Support/APSInt.cpp +++ b/llvm/lib/Support/APSInt.cpp @@ -17,6 +17,25 @@ using namespace llvm; +APSInt::APSInt(StringRef Str) { + assert(!Str.empty() && "Invalid string length"); + + // (Over-)estimate the required number of bits. + unsigned NumBits = ((Str.size() * 64) / 19) + 2; + APInt Tmp(NumBits, Str, /*Radix=*/10); + if (Str[0] == '-') { + unsigned MinBits = Tmp.getMinSignedBits(); + if (MinBits > 0 && MinBits < NumBits) + Tmp = Tmp.trunc(MinBits); + *this = APSInt(Tmp, /*IsUnsigned=*/false); + return; + } + unsigned ActiveBits = Tmp.getActiveBits(); + if (ActiveBits > 0 && ActiveBits < NumBits) + Tmp = Tmp.trunc(ActiveBits); + *this = APSInt(Tmp, /*IsUnsigned=*/true); +} + void APSInt::Profile(FoldingSetNodeID& ID) const { ID.AddInteger((unsigned) (IsUnsigned ? 1 : 0)); APInt::Profile(ID); |