OpenVDB: SymbolTable.h Source File

OpenVDB 8.1.0

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 <map>
21 #include <unordered_map>
22
23 namespace openvdb {
24 OPENVDB_USE_VERSION_NAMESPACE
25 namespace OPENVDB_VERSION_NAME {
26
27 namespace ax {
28 namespace codegen {
29
35 struct SymbolTable
36 {
37 using MapType = std::unordered_map<std::string, llvm::Value*>;
38
39 SymbolTable() : mMap() {}
40 ~SymbolTable() = default;
41
46 inline llvm::Value* get(const std::string& name) const
47 {
48 const auto iter = mMap.find(name);
49 if (iter == mMap.end()) return nullptr;
50 return iter->second;
51 }
52
57 inline bool exists(const std::string& name) const
58 {
59 const auto iter = mMap.find(name);
60 return (iter != mMap.end());
61 }
62
69 inline bool insert(const std::string& name, llvm::Value* value)
70 {
71 if (exists(name)) return false;
72 mMap[name] = value;
73 return true;
74 }
75
82 inline bool replace(const std::string& name, llvm::Value* value)
83 {
84 const bool existed = exists(name);
85 mMap[name] = value;
86 return existed;
87 }
88
91 inline void clear() { mMap.clear(); }
92
95 inline const MapType& map() const { return mMap; }
96
97 private:
98 MapType mMap;
99 };
100
101
112 struct SymbolTableBlocks
113 {
114 using MapType = std::map<size_t, SymbolTable>;
115
116 SymbolTableBlocks() : mTables({{0, SymbolTable()}}) {}
117 ~SymbolTableBlocks() = default;
118
121 inline SymbolTable& globals() { return mTables.at(0); }
122 inline const SymbolTable& globals() const { return mTables.at(0); }
123
130 inline bool erase(const size_t index)
131 {
132 if (index == 0) {
133 throw std::runtime_error("Attempted to erase global variables which is disallowed.");
134 }
135
136 const bool existed = (mTables.find(index) != mTables.end());
137 mTables.erase(index);
138 return existed;
139 }
140
145 inline SymbolTable* getOrInsert(const size_t index)
146 {
147 return &(mTables[index]);
148 }
149
154 inline SymbolTable* get(const size_t index)
155 {
156 auto iter = mTables.find(index);
157 if (iter == mTables.end()) return nullptr;
158 return &(iter->second);
159 }
160
169 inline llvm::Value* find(const std::string& name, const size_t startIndex) const
170 {
171 // Find the lower bound start index and if necessary, decrement into
172 // the first block where the search will be started. Note that this
173 // is safe as the global block 0 will always exist
174
175 auto it = mTables.lower_bound(startIndex);
176 if (it == mTables.end() || it->first != startIndex) --it;
177
178 // reverse the iterator (which also make it point to the preceding
179 // value, hence the crement)
180
181 assert(it != mTables.end());
182 MapType::const_reverse_iterator iter(++it);
183
184 for (; iter != mTables.crend(); ++iter) {
185 llvm::Value* value = iter->second.get(name);
186 if (value) return value;
187 }
188
189 return nullptr;
190 }
191
197 inline llvm::Value* find(const std::string& name) const
198 {
199 return this->find(name, mTables.crbegin()->first);
200 }
201
208 inline bool replace(const std::string& name, llvm::Value* value)
209 {
210 for (auto it = mTables.rbegin(); it != mTables.rend(); ++it) {
211 if (it->second.get(name)) {
212 it->second.replace(name, value);
213 return true;
214 }
215 }
216
217 return false;
218 }
219
220 private:
221 MapType mTables;
222 };
223
224 } // namespace codegen
225 } // namespace ax
226 } // namespace OPENVDB_VERSION_NAME
227 } // namespace openvdb
228
229 #endif // OPENVDB_AX_CODEGEN_SYMBOL_TABLE_HAS_BEEN_INCLUDED
230
openvdb
Definition: openvdb/Exceptions.h:13
openvdb::v8_1::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:113
openvdb::v8_1::ax::codegen::SymbolTableBlocks::SymbolTableBlocks
SymbolTableBlocks()
Definition: SymbolTable.h:116
openvdb::v8_1::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:197
openvdb::v8_1::ax::codegen::SymbolTableBlocks::~SymbolTableBlocks
~SymbolTableBlocks()=default
openvdb::v8_1::ax::codegen::SymbolTableBlocks::MapType
std::map< size_t, SymbolTable > MapType
Definition: SymbolTable.h:114
openvdb::v8_1::ax::codegen::SymbolTableBlocks::globals
SymbolTable & globals()
Access to the list of global variables which are always accessible.
Definition: SymbolTable.h:121
openvdb::v8_1::ax::codegen::SymbolTableBlocks::globals
const SymbolTable & globals() const
Definition: SymbolTable.h:122
openvdb::v8_1::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:130
openvdb::v8_1::ax::codegen::SymbolTableBlocks::getOrInsert
SymbolTable * getOrInsert(const size_t index)
Get or insert and get a SymbolTable with a unique index.
Definition: SymbolTable.h:145
openvdb::v8_1::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:169
openvdb::v8_1::ax::codegen::SymbolTableBlocks::get
SymbolTable * get(const size_t index)
Get a SymbolTable with a unique index. If it doesn't exist, nullptr is returned.
Definition: SymbolTable.h:154
openvdb::v8_1::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:208
openvdb::v8_1::ax::codegen::SymbolTable
A symbol table which can be used to represent a single scoped set of a programs variables....
Definition: SymbolTable.h:36
openvdb::v8_1::ax::codegen::SymbolTable::~SymbolTable
~SymbolTable()=default
openvdb::v8_1::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:69
openvdb::v8_1::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:57
openvdb::v8_1::ax::codegen::SymbolTable::map
const MapType & map() const
Access to the underlying map.
Definition: SymbolTable.h:95
openvdb::v8_1::ax::codegen::SymbolTable::MapType
std::unordered_map< std::string, llvm::Value * > MapType
Definition: SymbolTable.h:37
openvdb::v8_1::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:46
openvdb::v8_1::ax::codegen::SymbolTable::SymbolTable
SymbolTable()
Definition: SymbolTable.h:39
openvdb::v8_1::ax::codegen::SymbolTable::clear
void clear()
Clear all symbols in this table.
Definition: SymbolTable.h:91
openvdb::v8_1::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:82
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:178