retdec
const_int.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_LLVMIR2HLL_IR_CONST_INT_H
8 #define RETDEC_LLVMIR2HLL_IR_CONST_INT_H
9 
10 #include <cstddef>
11 #include <cstdint>
12 #include <string>
13 
14 #include <llvm/ADT/APInt.h>
15 #include <llvm/ADT/APSInt.h>
16 
19 
20 namespace retdec {
21 namespace llvmir2hll {
22 
23 class Expression;
24 class IntType;
25 class Visitor;
26 
33 class ConstInt final: public Constant {
34 public:
35  static ShPtr<ConstInt> create(std::int64_t value, unsigned bitWidth,
36  bool isSigned = true);
37  static ShPtr<ConstInt> create(const llvm::APInt &value,
38  bool isSigned = true);
39  static ShPtr<ConstInt> create(const llvm::APSInt &value);
41 
42  virtual ShPtr<Value> clone() override;
43  virtual bool isEqualTo(ShPtr<Value> otherValue) const override;
44  virtual ShPtr<retdec::llvmir2hll::Type> getType() const override;
45  virtual void replace(ShPtr<Expression> oldExpr,
46  ShPtr<Expression> newExpr) override;
47 
48  void flipSign();
49 
50  bool isMinSigned() const;
51  bool isSigned() const;
52  bool isUnsigned() const;
53  llvm::APSInt getValue() const;
54  std::string toString(unsigned radix = 10,
55  const std::string &prefix = "") const;
56  std::string toHexString(const std::string &prefix = "0x") const;
57  bool isNegative() const;
58  bool isNegativeOne() const;
59  bool isPositive() const;
60  bool isZero() const;
61  bool isOne() const;
62  bool isMoreReadableInHexa() const;
63 
66  virtual void accept(Visitor *v) override;
68 
69 private:
71  llvm::APSInt value;
72 
75 
76 private:
77  // Since instances are created by calling the static function create(), the
78  // constructor can be private.
79  explicit ConstInt(const llvm::APSInt &value);
80 };
81 
82 } // namespace llvmir2hll
83 } // namespace retdec
84 
85 #endif
An integer constant.
Definition: const_int.h:33
static ShPtr< ConstInt > getTwoToPositivePower(ShPtr< ConstInt > x)
Computes 2^x, where x >= 0, and returns the result.
Definition: const_int.cpp:335
bool isZero() const
Determines whether the constant is zero.
Definition: const_int.cpp:192
bool isNegativeOne() const
Determines whether the constant is negative one.
Definition: const_int.cpp:176
static ShPtr< ConstInt > create(std::int64_t value, unsigned bitWidth, bool isSigned=true)
Constructs an integer constant initialized to the given value of the given bit width.
Definition: const_int.cpp:300
virtual bool isEqualTo(ShPtr< Value > otherValue) const override
Returns true if this value is equal to otherValue, false otherwise.
Definition: const_int.cpp:36
bool isMoreReadableInHexa() const
Returns true if the constant is more readable in the hexadecimal radix than in the decimal radix,...
Definition: const_int.cpp:224
virtual void accept(Visitor *v) override
Visitor pattern implementation.
Definition: const_int.cpp:344
bool isSigned() const
Returns true if the integer is signed, false otherwise.
Definition: const_int.cpp:72
ConstInt(const llvm::APSInt &value)
Constructs an integer constant initialized to the given value.
Definition: const_int.cpp:26
bool isPositive() const
Determines whether the constant is positive (> 0).
Definition: const_int.cpp:185
std::string toString(unsigned radix=10, const std::string &prefix="") const
Converts the constant into a string in the given radix and optionally the given prefix.
Definition: const_int.cpp:111
virtual ShPtr< retdec::llvmir2hll::Type > getType() const override
Returns the type of the expression.
Definition: const_int.cpp:47
ShPtr< IntType > type
Type of the constant.
Definition: const_int.h:74
bool isMinSigned() const
Determines whether the constant has minimum signed value on it's bitwidth.
Definition: const_int.cpp:63
std::string toHexString(const std::string &prefix="0x") const
Converts the constant into a string in the hexadecimal format and prepends the given prefix.
Definition: const_int.cpp:141
bool isUnsigned() const
Returns true if the integer is unsigned, false otherwise.
Definition: const_int.cpp:79
llvm::APSInt value
Value of the constant.
Definition: const_int.h:71
virtual ShPtr< Value > clone() override
Returns a clone of the value.
Definition: const_int.cpp:30
llvm::APSInt getValue() const
Returns the constant's value.
Definition: const_int.cpp:86
bool isOne() const
Determines whether the constant is one.
Definition: const_int.cpp:201
bool isNegative() const
Determines whether the constant is negative (< 0).
Definition: const_int.cpp:162
void flipSign()
Flip the sign of value.
Definition: const_int.cpp:152
virtual void replace(ShPtr< Expression > oldExpr, ShPtr< Expression > newExpr) override
Replaces all occurrences of oldExpr with newExpr in the current expression.
Definition: const_int.cpp:51
A base class for all constants.
Definition: constant.h:20
A base class of all visitors.
Definition: visitor.h:95
A base class for all constants.
A library providing API for working with back-end IR.
std::shared_ptr< T > ShPtr
An alias for a shared pointer.
Definition: smart_ptr.h:18
Definition: archive_wrapper.h:19
Declarations, aliases, macros, etc. for the use of smart pointers.