OpenVDB  7.2.1
SymbolTable.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
11 
12 #ifndef OPENVDB_AX_CODEGEN_SYMBOL_TABLE_HAS_BEEN_INCLUDED
13 #define OPENVDB_AX_CODEGEN_SYMBOL_TABLE_HAS_BEEN_INCLUDED
14 
15 #include <openvdb/version.h>
16 
17 #include <llvm/IR/Value.h>
18 
19 #include <string>
20 #include <unordered_map>
21 
22 namespace openvdb {
24 namespace OPENVDB_VERSION_NAME {
25 
26 namespace ax {
27 namespace codegen {
28 
35 {
36  using MapType = std::unordered_map<std::string, llvm::Value*>;
37 
38  SymbolTable() : mMap() {}
39  ~SymbolTable() = default;
40 
45  inline llvm::Value* get(const std::string& name) const
46  {
47  const auto iter = mMap.find(name);
48  if (iter == mMap.end()) return nullptr;
49  return iter->second;
50  }
51 
56  inline bool exists(const std::string& name) const
57  {
58  const auto iter = mMap.find(name);
59  return (iter != mMap.end());
60  }
61 
68  inline bool insert(const std::string& name, llvm::Value* value)
69  {
70  if (exists(name)) return false;
71  mMap[name] = value;
72  return true;
73  }
74 
81  inline bool replace(const std::string& name, llvm::Value* value)
82  {
83  const bool existed = exists(name);
84  mMap[name] = value;
85  return existed;
86  }
87 
90  inline void clear() { mMap.clear(); }
91 
94  inline const MapType& map() const { return mMap; }
95 
96 private:
97  MapType mMap;
98 };
99 
100 
112 {
113  using MapType = std::map<size_t, SymbolTable>;
114 
115  SymbolTableBlocks() : mTables({{0, SymbolTable()}}) {}
116  ~SymbolTableBlocks() = default;
117 
120  inline SymbolTable& globals() { return mTables.at(0); }
121  inline const SymbolTable& globals() const { return mTables.at(0); }
122 
129  inline bool erase(const size_t index)
130  {
131  if (index == 0) {
132  throw std::runtime_error("Attempted to erase global variables which is disallowed.");
133  }
134 
135  const bool existed = (mTables.find(index) != mTables.end());
136  mTables.erase(index);
137  return existed;
138  }
139 
144  inline SymbolTable* getOrInsert(const size_t index)
145  {
146  return &(mTables[index]);
147  }
148 
154  inline SymbolTable& get(const size_t index)
155  {
156  auto iter = mTables.find(index);
157  if (iter != mTables.end()) return iter->second;
158  throw std::runtime_error("Attempted to access invalid symbol table with index "
159  + std::to_string(index));
160  }
161 
170  inline llvm::Value* find(const std::string& name, const size_t startIndex) const
171  {
172  // Find the lower bound start index and if necessary, decrement into
173  // the first block where the search will be started. Note that this
174  // is safe as the global block 0 will always exist
175 
176  auto it = mTables.lower_bound(startIndex);
177  if (it == mTables.end() || it->first != startIndex) --it;
178 
179  // reverse the iterator (which also make it point to the preceding
180  // value, hence the crement)
181 
182  assert(it != mTables.end());
183  MapType::const_reverse_iterator iter(++it);
184 
185  for (; iter != mTables.crend(); ++iter) {
186  llvm::Value* value = iter->second.get(name);
187  if (value) return value;
188  }
189 
190  return nullptr;
191  }
192 
198  inline llvm::Value* find(const std::string& name) const
199  {
200  return this->find(name, mTables.crbegin()->first);
201  }
202 
209  inline bool replace(const std::string& name, llvm::Value* value)
210  {
211  for (auto it = mTables.rbegin(); it != mTables.rend(); ++it) {
212  if (it->second.get(name)) {
213  it->second.replace(name, value);
214  return true;
215  }
216  }
217 
218  return false;
219  }
220 
221 private:
222  MapType mTables;
223 };
224 
225 } // namespace codegen
226 } // namespace ax
227 } // namespace OPENVDB_VERSION_NAME
228 } // namespace openvdb
229 
230 #endif // OPENVDB_AX_CODEGEN_SYMBOL_TABLE_HAS_BEEN_INCLUDED
231 
openvdb::v7_2::ax::codegen::SymbolTableBlocks::getOrInsert
SymbolTable * getOrInsert(const size_t index)
Get or insert and get a SymbolTable with a unique index.
Definition: SymbolTable.h:144
openvdb::v7_2::ax::codegen::SymbolTableBlocks::~SymbolTableBlocks
~SymbolTableBlocks()=default
openvdb::v7_2::ax::codegen::SymbolTable::clear
void clear()
Clear all symbols in this table.
Definition: SymbolTable.h:90
openvdb::v7_2::ax::codegen::SymbolTable
A symbol table which can be used to represent a single scoped set of a programs variables....
Definition: SymbolTable.h:35
openvdb::v7_2::ax::codegen::SymbolTable::insert
bool insert(const std::string &name, llvm::Value *value)
Insert a variable to this symbol table if it does not exist. Returns true if successfully,...
Definition: SymbolTable.h:68
openvdb::v7_2::ax::codegen::SymbolTableBlocks::MapType
std::map< size_t, SymbolTable > MapType
Definition: SymbolTable.h:113
openvdb::v7_2::ax::codegen::SymbolTable::exists
bool exists(const std::string &name) const
Returns true if a variable exists in this symbol table with the given name.
Definition: SymbolTable.h:56
openvdb::v7_2::ax::codegen::SymbolTableBlocks::SymbolTableBlocks
SymbolTableBlocks()
Definition: SymbolTable.h:115
version.h
Library and file format version numbers.
openvdb::v7_2::ax::codegen::SymbolTable::get
llvm::Value * get(const std::string &name) const
Get a llvm::Value from this symbol table with the given name mapping. It it does not exist,...
Definition: SymbolTable.h:45
openvdb::v7_2::ax::codegen::SymbolTableBlocks::globals
SymbolTable & globals()
Access to the list of global variables which are always accessible.
Definition: SymbolTable.h:120
openvdb::v7_2::ax::codegen::SymbolTableBlocks::get
SymbolTable & get(const size_t index)
Get a SymbolTable with a unique index. If the symbol table does not exist, this function throws a run...
Definition: SymbolTable.h:154
openvdb::v7_2::ax::codegen::SymbolTable::MapType
std::unordered_map< std::string, llvm::Value * > MapType
Definition: SymbolTable.h:36
openvdb::v7_2::ax::codegen::SymbolTableBlocks::replace
bool replace(const std::string &name, llvm::Value *value)
Replace the first occurance of a variable with a given name with a replacement value....
Definition: SymbolTable.h:209
openvdb::v7_2::ax::codegen::SymbolTableBlocks::find
llvm::Value * find(const std::string &name, const size_t startIndex) const
Find a variable within the program starting at a given table index. If the given index does not exist...
Definition: SymbolTable.h:170
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:147
openvdb::v7_2::ax::codegen::SymbolTableBlocks::erase
bool erase(const size_t index)
Erase a given scoped indexed SymbolTable from the list of held SymbolTables. Returns true if the tabl...
Definition: SymbolTable.h:129
openvdb::v7_2::ax::codegen::SymbolTable::SymbolTable
SymbolTable()
Definition: SymbolTable.h:38
openvdb::v7_2::ax::codegen::SymbolTableBlocks
A map of unique ids to symbol tables which can be used to represent local variables within a program....
Definition: SymbolTable.h:112
openvdb::v7_2::ax::codegen::SymbolTable::map
const MapType & map() const
Access to the underlying map.
Definition: SymbolTable.h:94
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:95
openvdb::v7_2::ax::codegen::SymbolTableBlocks::globals
const SymbolTable & globals() const
Definition: SymbolTable.h:121
openvdb::v7_2::ax::codegen::SymbolTable::~SymbolTable
~SymbolTable()=default
openvdb
Definition: openvdb/Exceptions.h:13
openvdb::v7_2::ax::codegen::SymbolTableBlocks::find
llvm::Value * find(const std::string &name) const
Find a variable within the program starting at the lowest level SymbolTable.
Definition: SymbolTable.h:198
openvdb::v7_2::ax::codegen::SymbolTable::replace
bool replace(const std::string &name, llvm::Value *value)
Replace a variable in this symbol table. Returns true if the variable previously existed and false if...
Definition: SymbolTable.h:81