llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
item.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/ilist_node.h>
8 #include <llvm/ADT/ilist.h>
9 #include <llvm/ADT/StringRef.h>
10 
11 #include "core/use.h"
12 
13 class Item;
14 class Expr;
15 class Atom;
16 
17 
18 
22 template <> struct llvm::ilist_traits<Item> {
23 private:
24  using instr_iterator = simple_ilist<Item>::iterator;
25 
26 public:
27  void deleteNode(Item *inst);
28  void addNodeToList(Item *inst);
29  void removeNodeFromList(Item *inst);
30  void transferNodesFromList(
31  ilist_traits &from,
32  instr_iterator first,
33  instr_iterator last
34  );
35 
36  Atom *getParent();
37 };
38 
42 class Item final : public llvm::ilist_node_with_parent<Item, Atom> {
43 public:
45  enum class Kind : uint8_t {
47  INT8,
49  INT16,
51  INT32,
53  INT64,
55  FLOAT64,
57  EXPR32,
59  EXPR64,
61  SPACE,
63  STRING
64  };
65 
66 public:
68  Item(Item &that);
70  ~Item();
71 
72  // Helpers to create items.
73  static Item *CreateInt8(int8_t val);
74  static Item *CreateInt16(int16_t val);
75  static Item *CreateInt32(int32_t val);
76  static Item *CreateInt64(int64_t val);
77  static Item *CreateFloat64(double val);
78  static Item *CreateSpace(unsigned val);
79  static Item *CreateExpr32(Expr *val);
80  static Item *CreateExpr64(Expr *val);
81  static Item *CreateString(const std::string_view str);
82 
84  void removeFromParent();
86  void eraseFromParent();
87 
89  Atom *getParent() const { return parent_; }
90 
92  Kind GetKind() const { return kind_; }
93 
95  bool IsExpr() const
96  {
97  return kind_ == Kind::EXPR32 || kind_ == Kind::EXPR64;
98  }
99 
101  bool IsSpace() const { return GetKind() == Item::Kind::SPACE; }
102 
104  size_t GetSize() const;
105 
106  // Returns integer values.
107  int8_t GetInt8() const { assert(kind_ == Kind::INT8); return int8val_; }
108  int16_t GetInt16() const { assert(kind_ == Kind::INT16); return int16val_; }
109  int32_t GetInt32() const { assert(kind_ == Kind::INT32); return int32val_; }
110  int64_t GetInt64() const { assert(kind_ == Kind::INT64); return int64val_; }
112  unsigned GetSpace() const { assert(IsSpace()); return int32val_; }
113 
114  // Returns the real values.
115  double GetFloat64() const
116  {
117  assert(kind_ == Kind::FLOAT64);
118  return float64val_;
119  }
120 
122  llvm::StringRef getString() const
123  {
124  assert(kind_ == Kind::STRING);
125  return stringVal_;
126  }
127 
129  std::string_view GetString() const
130  {
131  assert(kind_ == Kind::STRING);
132  return stringVal_;
133  }
134 
136  Expr *GetExpr();
138  const Expr *GetExpr() const;
140  Expr *AsExpr();
142  const Expr *AsExpr() const;
143 
144 private:
146  Item(Kind kind) : kind_(kind), parent_(nullptr) {}
147 
148 private:
149  friend struct llvm::ilist_traits<Item>;
150 
151  void setParent(Atom *parent) { parent_ = parent; }
152 
153 private:
155  Kind kind_;
157  Atom *parent_;
159  union {
160  int8_t int8val_;
161  int16_t int16val_;
162  int32_t int32val_;
163  int64_t int64val_;
164  double float64val_;
165  Use useVal_;
166  std::string stringVal_;
167  };
168 };
Use
Definition: use.h:18
Item::getString
llvm::StringRef getString() const
Returns the string value.
Definition: item.h:122
Item::Kind::EXPR32
@ EXPR32
32-bit pointer.
Item::GetKind
Kind GetKind() const
Returns the item kind.
Definition: item.h:92
Atom
Definition: atom.h:23
Item::Kind::INT16
@ INT16
16-bit integer.
Expr
Definition: expr.h:19
Item::Kind::INT8
@ INT8
8-bit integer.
Item::IsSpace
bool IsSpace() const
Checks whether the item is space.
Definition: item.h:101
Item::Kind
Kind
Enumeration of item kinds.
Definition: item.h:45
Item::getParent
Atom * getParent() const
Returns a pointer to the parent section.
Definition: item.h:89
Item::Kind::FLOAT64
@ FLOAT64
IEEE double.
Item::Item
Item(Item &that)
Copy constructor.
Definition: item.cpp:14
Item::Kind::INT64
@ INT64
64-bit integer.
Item::Kind::INT32
@ INT32
32-bit integer.
Item::Kind::SPACE
@ SPACE
Unallocated space.
Item::GetSpace
unsigned GetSpace() const
Returns the spacing.
Definition: item.h:112
Item::Kind::EXPR64
@ EXPR64
64-bit pointer.
Item::Kind::STRING
@ STRING
Raw string.
Item::GetString
std::string_view GetString() const
Returns the string value.
Definition: item.h:129
Item::IsExpr
bool IsExpr() const
Checks whether the item is an expression.
Definition: item.h:95