forked from ispc/ispc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sym.h
321 lines (261 loc) · 14.1 KB
/
sym.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
Copyright (c) 2010-2013, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @file sym.h
@brief header file with declarations for symbol and symbol table
classes.
*/
#ifndef ISPC_SYM_H
#define ISPC_SYM_H
#include "ispc.h"
#include "decl.h"
#include <map>
class StructType;
class ConstExpr;
/**
@brief Representation of a program symbol.
The Symbol class represents a symbol in an ispc program. Symbols can
include variables, functions, and named types. Note that all of the
members are publically accessible; other code throughout the system
accesses and modifies the members directly.
@todo Should we break function symbols into a separate FunctionSymbol
class and then not have these members that are not applicable for
function symbols (and vice versa, for non-function symbols)?
*/
class Symbol {
public:
/** The Symbol constructor takes the name of the symbol, its
position in a source file, and its type (if known). */
Symbol(const std::string &name, SourcePos pos, const Type *t = NULL,
StorageClass sc = SC_NONE);
SourcePos pos; /*!< Source file position where the symbol was defined */
std::string name; /*!< Symbol's name */
llvm::Value *storagePtr; /*!< For symbols with storage associated with
them (i.e. variables but not functions),
this member stores a pointer to its
location in memory.) */
llvm::Function *function; /*!< For symbols that represent functions,
this stores the LLVM Function value for
the symbol once it has been created. */
llvm::Function *exportedFunction;
/*!< For symbols that represent functions with
'export' qualifiers, this points to the LLVM
Function for the application-callable version
of the function. */
const Type *type; /*!< The type of the symbol; if not set by the
constructor, this is set after the
declaration around the symbol has been parsed. */
ConstExpr *constValue; /*!< For symbols with const-qualified types, this may store
the symbol's compile-time constant value. This value may
validly be NULL for a const-qualified type, however; for
example, the ConstExpr class can't currently represent
struct types. For cases like these, ConstExpr is NULL,
though for all const symbols, the value pointed to by the
storagePtr member will be its constant value. (This
messiness is due to needing an ispc ConstExpr for the early
constant folding optimizations). */
StorageClass storageClass;/*!< Records the storage class (if any) provided with the
symbol's declaration. */
int varyingCFDepth; /*!< This member records the number of levels of nested 'varying'
control flow within which the symbol was declared. Having
this value available makes it possible to avoid performing
masked stores when modifying the symbol's value when the
store is done at the same 'varying' control flow depth as
the one where the symbol was originally declared. */
const Function *parentFunction;
/*!< For symbols that are parameters to functions or are
variables declared inside functions, this gives the
function they're in. */
};
/** @brief Symbol table that holds all known symbols during parsing and compilation.
A single instance of a SymbolTable is stored in the Module class
(Module::symbolTable); it is created in the Module::Module()
constructor. It is then accessed via the global variable Module *\ref m
throughout the ispc implementation.
*/
class SymbolTable {
public:
SymbolTable();
~SymbolTable();
/** The parser calls this method when it enters a new scope in the
program; this allows us to track variables that shadows others in
outer scopes with same name as well as to efficiently discard all
of the variables declared in a particular scope when we exit that
scope. */
void PushScope();
/** For each scope started by a call to SymbolTable::PushScope(), there
must be a matching call to SymbolTable::PopScope() at the end of
that scope. */
void PopScope();
/** Adds the given variable symbol to the symbol table.
@param symbol The symbol to be added
@return true if successful; false if the provided symbol clashes
with a symbol defined at the same scope. (Symbols may shaodow
symbols in outer scopes; a warning is issued in this case, but this
method still returns true.) */
bool AddVariable(Symbol *symbol);
/** Looks for a variable with the given name in the symbol table. This
method searches outward from the innermost scope to the outermost,
returning the first match found.
@param name The name of the variable to be searched for.
@return A pointer to the Symbol, if a match is found. NULL if no
Symbol with the given name is in the symbol table. */
Symbol *LookupVariable(const char *name);
/** Adds the given function symbol to the symbol table.
@param symbol The function symbol to be added.
@return true if the symbol has been added. False if another
function symbol with the same name and function signature is
already present in the symbol table. */
bool AddFunction(Symbol *symbol);
/** Looks for the function or functions with the given name in the
symbol name. If a function has been overloaded and multiple
definitions are present for a given function name, all of them will
be returned in the provided vector and it's up the the caller to
resolve which one (if any) to use. Returns true if any matches
were found. */
bool LookupFunction(const char *name,
std::vector<Symbol *> *matches = NULL);
/** Looks for a function with the given name and type
in the symbol table.
@return pointer to matching Symbol; NULL if none is found. */
Symbol *LookupFunction(const char *name, const FunctionType *type);
/** Returns all of the functions in the symbol table that match the given
predicate.
@param pred A unary predicate that returns true or false, given a Symbol
pointer, based on whether the symbol should be included in the returned
set of matches. It can either be a function, with signature
<tt>bool pred(const Symbol *s)</tt>, or a unary predicate object with
an <tt>bool operator()(const Symbol *)</tt> method.
@param matches Pointer to a vector in which to return the matching
symbols.
*/
template <typename Predicate>
void GetMatchingFunctions(Predicate pred,
std::vector<Symbol *> *matches) const;
/** Returns all of the variable symbols in the symbol table that match
the given predicate. The predicate is defined as in the
GetMatchingFunctions() method.
*/
template <typename Predicate>
void GetMatchingVariables(Predicate pred,
std::vector<Symbol *> *matches) const;
/** Adds the named type to the symbol table. This is used for both
struct definitions (where <tt>struct Foo</tt> causes type \c Foo to
be added to the symbol table) as well as for <tt>typedef</tt>s.
For structs with forward declarations ("struct Foo;") and are thus
UndefinedStructTypes, this method replaces these with an actual
struct definition if one is provided.
@param name Name of the type to be added
@param type Type that \c name represents
@param pos Position in source file where the type was named
@return true if the named type was successfully added. False if a type
with the same name has already been defined.
*/
bool AddType(const char *name, const Type *type, SourcePos pos);
/** Looks for a type of the given name in the symbol table.
@return Pointer to the Type, if found; otherwise NULL is returned.
*/
const Type *LookupType(const char *name) const;
/** Look for a type given a pointer.
@return True if found, False otherwise.
*/
bool ContainsType(const Type * type) const;
/** This method returns zero or more strings with the names of symbols
in the symbol table that nearly (but not exactly) match the given
name. This is useful for issuing informative error methods when
misspelled identifiers are found a programs.
@param name String to compare variable and function symbol names against.
@return vector of zero or more strings that approximately match \c name.
*/
std::vector<std::string> ClosestVariableOrFunctionMatch(const char *name) const;
/** This method returns zero or more strings with the names of types
in the symbol table that nearly (but not exactly) match the given
name. */
std::vector<std::string> ClosestTypeMatch(const char *name) const;
std::vector<std::string> ClosestEnumTypeMatch(const char *name) const;
/** Prints out the entire contents of the symbol table to standard error.
(Debugging method). */
void Print();
/** Returns a random symbol from the symbol table. (It is not
guaranteed that it is equally likely to return all symbols). */
Symbol *RandomSymbol();
/** Returns a random type from the symbol table. */
const Type *RandomType();
/** Returns a Map with all global variables. */
const std::map<std::string, Symbol *> * getGlobals();
/** Returns a Map with all function symbols. */
const std::map<std::string, std::vector<Symbol *> > * getFunctions();
private:
std::vector<std::string> closestTypeMatch(const char *str,
bool structsVsEnums) const;
/** This member variable holds one SymbolMap for each of the current
active scopes as the program is being parsed. New maps are added
and removed from the end of the main vector, so searches for
symbols start looking at the end of \c variables and work
backwards.
*/
typedef std::map<std::string, Symbol *> SymbolMapType;
std::vector<SymbolMapType *> variables;
std::vector<SymbolMapType *> freeSymbolMaps;
/** Function declarations are *not* scoped. (C99, for example, allows
an implementation to maintain function declarations in a single
namespace.) A STL \c vector is used to store the function symbols
for a given name since, due to function overloading, a name can
have multiple function symbols associated with it. */
typedef std::map<std::string, std::vector<Symbol *> > FunctionMapType;
FunctionMapType functions;
/** Type definitions can't currently be scoped.
*/
typedef std::map<std::string, const Type *> TypeMapType;
TypeMapType types;
};
template <typename Predicate> void
SymbolTable::GetMatchingFunctions(Predicate pred,
std::vector<Symbol *> *matches) const {
// Iterate through all function symbols and apply the given predicate.
// If it returns true, add the Symbol * to the provided vector.
FunctionMapType::const_iterator iter;
for (iter = functions.begin(); iter != functions.end(); ++iter) {
const std::vector<Symbol *> &syms = iter->second;
for (unsigned int j = 0; j < syms.size(); ++j) {
if (pred(syms[j]))
matches->push_back(syms[j]);
}
}
}
template <typename Predicate> void
SymbolTable::GetMatchingVariables(Predicate pred,
std::vector<Symbol *> *matches) const {
for (unsigned int i = 0; i < variables.size(); ++i) {
SymbolMapType &sm = *(variables[i]);
SymbolMapType::const_iterator iter;
for (iter = sm.begin(); iter != sm.end(); ++iter) {
if (pred(iter->second))
matches->push_back(iter->second);
}
}
}
#endif // ISPC_SYM_H