llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
constant.h
1 // This file if part of the llir-opt project.
2 // Licensing information can be found in the LICENSE file.
3 // (C) 2018 Nandor Licker. All rights reserved.
4 
5 #pragma once
6 
7 #include <llvm/ADT/APFloat.h>
8 #include <llvm/ADT/APInt.h>
9 
10 #include "core/value.h"
11 #include "core/register.h"
12 
13 using APInt = llvm::APInt;
14 using APFloat = llvm::APFloat;
15 
16 
17 
21 class Constant : public Value {
22 public:
24  static constexpr Value::Kind kValueKind = Value::Kind::CONST;
25 
26 public:
30  enum Kind {
31  INT,
32  FLOAT
33  };
34 
35  Constant(Kind kind) : Value(Value::Kind::CONST), kind_(kind) {}
36 
37  virtual ~Constant();
38 
39  Kind GetKind() const { return kind_; }
40 
41  bool Is(Kind kind) const { return GetKind() == kind; }
42 
43 private:
45  Kind kind_;
46 };
47 
48 
52 class ConstantInt final : public Constant {
53 public:
55  static constexpr Constant::Kind kConstKind = Constant::Kind::INT;
56 
57 public:
58  ConstantInt(int64_t v);
59  ConstantInt(const APInt &v) : Constant(Constant::Kind::INT), v_(v) {}
60 
61  APInt GetValue() const { return v_; }
62  int64_t GetInt() const { return v_.getSExtValue(); }
63 
64 private:
65  APInt v_;
66 };
67 
68 
72 class ConstantFloat final : public Constant {
73 public:
75  static constexpr Constant::Kind kConstKind = Constant::Kind::FLOAT;
76 
77 public:
78  ConstantFloat(double d) : Constant(Constant::Kind::FLOAT), v_(APFloat(d)) {}
79  ConstantFloat(const APFloat &v) : Constant(Constant::Kind::FLOAT), v_(v) {}
80 
81  APFloat GetValue() const { return v_; }
82  double GetDouble() const;
83 
84 private:
85  APFloat v_;
86 };
ConstantFloat::kConstKind
static constexpr Constant::Kind kConstKind
Kind of the constant.
Definition: constant.h:75
ConstantInt::kConstKind
static constexpr Constant::Kind kConstKind
Kind of the constant.
Definition: constant.h:55
Value
Definition: value.h:22
Constant
Definition: constant.h:21
Constant::Kind
Kind
Definition: constant.h:30
Value::Kind
Kind
Enumeration of value types.
Definition: value.h:133
ConstantInt
Definition: constant.h:52
ConstantFloat
Definition: constant.h:72
Constant::kValueKind
static constexpr Value::Kind kValueKind
Kind of the global.
Definition: constant.h:24