llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
atom.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.h>
8 
9 #include "core/value.h"
10 #include "core/global.h"
11 #include "core/symbol_table.h"
12 #include "core/item.h"
13 
14 class Data;
15 class Expr;
16 
17 
18 
19 
23 class Atom
24  : public llvm::ilist_node_with_parent<Atom, Object>
25  , public Global
26 {
27 public:
29  static constexpr Global::Kind kGlobalKind = Global::Kind::ATOM;
30 
32  using ItemListType = llvm::ilist<Item>;
33  // Iterators over items.
34  typedef ItemListType::iterator iterator;
35  typedef ItemListType::const_iterator const_iterator;
36 
37 public:
40  const std::string_view name,
41  Visibility visibility = Visibility::LOCAL,
42  std::optional<llvm::Align> align = std::nullopt)
43  : Global(Global::Kind::ATOM, name, visibility, 0)
44  , parent_(nullptr)
45  , align_(align)
46  {
47  }
48 
50  ~Atom() override;
51 
53  void removeFromParent() override;
55  void eraseFromParent() override;
56 
58  Object *getParent() const { return parent_; }
59 
61  void remove(iterator it) { items_.remove(it); }
63  void erase(iterator it) { items_.erase(it); }
65  void AddItem(Item *atom, Item *before = nullptr);
66 
67  // Iterators over items.
68  bool empty() const { return items_.empty(); }
69  size_t size() const { return items_.size(); }
70  iterator begin() { return items_.begin(); }
71  iterator end() { return items_.end(); }
72  const_iterator begin() const { return items_.begin(); }
73  const_iterator end() const { return items_.end(); }
75  void clear() { items_.clear(); }
76 
78  size_t GetByteSize() const;
80  void SetAlignment(llvm::Align align) { align_ = align; }
82  std::optional<llvm::Align> GetAlignment() const override { return align_; }
83 
85  Prog *getProg() override;
86 
88  void dump(llvm::raw_ostream &os = llvm::errs()) const;
89 
90 private:
91  friend struct SymbolTableListTraits<Atom>;
92  friend struct llvm::ilist_traits<Item>;
93  static ItemListType Atom::*getSublistAccess(Item *) {
94  return &Atom::items_;
95  }
96 
98  void setParent(Object *parent) { parent_ = parent; }
99 
100 private:
102  Object *parent_;
104  ItemListType items_;
106  std::optional<llvm::Align> align_;
107 };
108 
110 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Atom &atom)
111 {
112  atom.dump(os);
113  return os;
114 }
115 
Atom::getProg
Prog * getProg() override
Returns the program to which the atom belongs.
Definition: atom.cpp:33
Atom::Atom
Atom(const std::string_view name, Visibility visibility=Visibility::LOCAL, std::optional< llvm::Align > align=std::nullopt)
Creates a new parent.
Definition: atom.h:39
Atom::getParent
Object * getParent() const
Returns a pointer to the parent section.
Definition: atom.h:58
Atom::GetAlignment
std::optional< llvm::Align > GetAlignment() const override
Returns the parent alignment.
Definition: atom.h:82
Atom
Definition: atom.h:23
Atom::kGlobalKind
static constexpr Global::Kind kGlobalKind
Kind of the global.
Definition: atom.h:29
Atom::ItemListType
llvm::ilist< Item > ItemListType
Type of the item list.
Definition: atom.h:32
Atom::dump
void dump(llvm::raw_ostream &os=llvm::errs()) const
Dump the object.
Definition: atom.cpp:59
Atom::eraseFromParent
void eraseFromParent() override
Removes an parent from the data section, erasing it.
Definition: atom.cpp:27
Atom::remove
void remove(iterator it)
Removes an atom.
Definition: atom.h:61
Atom::~Atom
~Atom() override
Deletes the parent.
Definition: atom.cpp:16
Global::Kind
Kind
Enumeration of global kinds.
Definition: global.h:30
Expr
Definition: expr.h:19
Data
Definition: data.h:47
Atom::erase
void erase(iterator it)
Erases an atom.
Definition: atom.h:63
Object
Definition: object.h:43
SymbolTableListTraits
Definition: symbol_table.h:33
Atom::SetAlignment
void SetAlignment(llvm::Align align)
Changes the parent alignment.
Definition: atom.h:80
Prog
Definition: prog.h:33
Atom::GetByteSize
size_t GetByteSize() const
Returns the size of the atom in bytes.
Definition: atom.cpp:49
Atom::AddItem
void AddItem(Item *atom, Item *before=nullptr)
Adds an atom to the atom.
Definition: atom.cpp:39
Atom::removeFromParent
void removeFromParent() override
Removes an atom from the data section.
Definition: atom.cpp:21
Global
Definition: global.h:23
Atom::clear
void clear()
Clears all items.
Definition: atom.h:75