llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
global.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 <optional>
8 #include <string>
9 #include <string_view>
10 #include <llvm/ADT/StringRef.h>
11 #include <llvm/Support/Alignment.h>
12 
13 #include "core/user.h"
14 #include "core/visibility.h"
15 
16 class Prog;
17 
18 
19 
23 class Global : public User {
24 public:
26  static constexpr Value::Kind kValueKind = Value::Kind::GLOBAL;
27 
28 public:
30  enum class Kind {
31  EXTERN,
32  FUNC,
33  BLOCK,
34  ATOM
35  };
36 
37 public:
38  Global(
39  Kind kind,
40  const std::string_view name,
41  Visibility visibility = Visibility::LOCAL,
42  unsigned numOps = 0
43  );
44  virtual ~Global();
45 
47  Kind GetKind() const { return kind_; }
49  bool Is(Kind kind) const { return GetKind() == kind; }
50 
52  const std::string_view GetName() const { return name_; }
54  llvm::StringRef getName() const { return name_; }
55 
57  virtual std::optional<llvm::Align> GetAlignment() const = 0;
58 
60  void SetVisibility(Visibility visibility) { visibility_ = visibility; }
62  Visibility GetVisibility() const { return visibility_; }
63 
65  bool IsRoot() const;
67  bool IsLocal() const;
69  bool IsWeak() const;
70 
72  virtual void removeFromParent() = 0;
74  virtual void eraseFromParent() = 0;
75 
77  virtual Prog *getProg() = 0;
78 
80  virtual void dump(llvm::raw_ostream &os = llvm::errs()) const = 0;
81 
82 private:
83  friend class Prog;
85  Kind kind_;
87  std::string name_;
89  Visibility visibility_;
90 };
Global::removeFromParent
virtual void removeFromParent()=0
Removes the global from the parent container.
Global::getProg
virtual Prog * getProg()=0
Returns the program to which the global belongs.
Global::GetVisibility
Visibility GetVisibility() const
Returns the visibilty of a global.
Definition: global.h:62
Global::IsRoot
bool IsRoot() const
Checks if the symbol can be externally referenced.
Definition: global.cpp:30
Global::Kind
Kind
Enumeration of global kinds.
Definition: global.h:30
Global::IsWeak
bool IsWeak() const
Checks if a symbol is weak.
Definition: global.cpp:64
Global::dump
virtual void dump(llvm::raw_ostream &os=llvm::errs()) const =0
Dumps the representation of the function.
Global::GetAlignment
virtual std::optional< llvm::Align > GetAlignment() const =0
Externs have no known alignment.
Global::GetName
const std::string_view GetName() const
Returns the name of the global.
Definition: global.h:52
Global::Is
bool Is(Kind kind) const
Checks if the global is of a specific kind.
Definition: global.h:49
Value::Kind
Kind
Enumeration of value types.
Definition: value.h:133
Global::getName
llvm::StringRef getName() const
Returns the name of the basic block for LLVM.
Definition: global.h:54
Global::GetKind
Kind GetKind() const
Returns the kind of the global.
Definition: global.h:47
Prog
Definition: prog.h:33
Global::IsLocal
bool IsLocal() const
Checks if the global is hidden in the compilation unit.
Definition: global.cpp:47
Global::kValueKind
static constexpr Value::Kind kValueKind
Kind of the global.
Definition: global.h:26
Global::eraseFromParent
virtual void eraseFromParent()=0
Removes the global from the parent container, deleting it.
Global::SetVisibility
void SetVisibility(Visibility visibility)
Sets the visibilty of the global.
Definition: global.h:60
User
Definition: user.h:20
Global
Definition: global.h:23