Intel® Implicit SPMD Program Compiler (Intel® ISPC)  1.13.0
sym.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010-2019, Intel Corporation
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11 
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15 
16  * Neither the name of Intel Corporation nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 /** @file sym.cpp
35  @brief file with definitions for symbol and symbol table classes.
36 */
37 
38 #include "sym.h"
39 #include "type.h"
40 #include "util.h"
41 #include <stdio.h>
42 
43 ///////////////////////////////////////////////////////////////////////////
44 // Symbol
45 
46 Symbol::Symbol(const std::string &n, SourcePos p, const Type *t, StorageClass sc) : pos(p), name(n) {
47  storagePtr = NULL;
48  function = exportedFunction = NULL;
49  type = t;
50  constValue = NULL;
51  storageClass = sc;
52  varyingCFDepth = 0;
53  parentFunction = NULL;
54 }
55 
56 ///////////////////////////////////////////////////////////////////////////
57 // SymbolTable
58 
59 SymbolTable::SymbolTable() { PushScope(); }
60 
62  // Otherwise we have mismatched push/pop scopes
63  Assert(variables.size() == 1);
64  PopScope();
65 }
66 
68  SymbolMapType *sm;
69  if (freeSymbolMaps.size() > 0) {
70  sm = freeSymbolMaps.back();
71  freeSymbolMaps.pop_back();
72  sm->erase(sm->begin(), sm->end());
73  } else
74  sm = new SymbolMapType;
75 
76  variables.push_back(sm);
77 }
78 
80  Assert(variables.size() > 1);
81  freeSymbolMaps.push_back(variables.back());
82  variables.pop_back();
83 }
84 
86  Assert(symbol != NULL);
87 
88  // Check to see if a symbol of the same name has already been declared.
89  for (int i = (int)variables.size() - 1; i >= 0; --i) {
90  SymbolMapType &sm = *(variables[i]);
91  if (sm.find(symbol->name) != sm.end()) {
92  if (i == (int)variables.size() - 1) {
93  // If a symbol of the same name was declared in the
94  // same scope, it's an error.
95  Error(symbol->pos, "Ignoring redeclaration of symbol \"%s\".", symbol->name.c_str());
96  return false;
97  } else {
98  // Otherwise it's just shadowing something else, which
99  // is legal but dangerous..
100  Warning(symbol->pos, "Symbol \"%s\" shadows symbol declared in outer scope.", symbol->name.c_str());
101  (*variables.back())[symbol->name] = symbol;
102  return true;
103  }
104  }
105  }
106 
107  // No matches, so go ahead and add it...
108  (*variables.back())[symbol->name] = symbol;
109  return true;
110 }
111 
113  // Note that we iterate through the variables vectors backwards, since
114  // we want to search from the innermost scope to the outermost, so that
115  // we get the right symbol if we have multiple variables in different
116  // scopes that shadow each other.
117  for (int i = (int)variables.size() - 1; i >= 0; --i) {
118  SymbolMapType &sm = *(variables[i]);
119  SymbolMapType::iterator iter = sm.find(name);
120  if (iter != sm.end())
121  return iter->second;
122  }
123  return NULL;
124 }
125 
127  const FunctionType *ft = CastType<FunctionType>(symbol->type);
128  Assert(ft != NULL);
129  if (LookupFunction(symbol->name.c_str(), ft) != NULL)
130  // A function of the same name and type has already been added to
131  // the symbol table
132  return false;
133 
134  std::vector<Symbol *> &funOverloads = functions[symbol->name];
135  funOverloads.push_back(symbol);
136  return true;
137 }
138 
139 bool SymbolTable::LookupFunction(const char *name, std::vector<Symbol *> *matches) {
140  FunctionMapType::iterator iter = functions.find(name);
141  if (iter != functions.end()) {
142  if (matches == NULL)
143  return true;
144  else {
145  const std::vector<Symbol *> &funcs = iter->second;
146  for (int j = 0; j < (int)funcs.size(); ++j)
147  matches->push_back(funcs[j]);
148  }
149  }
150  return matches ? (matches->size() > 0) : false;
151 }
152 
154  FunctionMapType::iterator iter = functions.find(name);
155  if (iter != functions.end()) {
156  std::vector<Symbol *> funcs = iter->second;
157  for (int j = 0; j < (int)funcs.size(); ++j) {
158  if (Type::Equal(funcs[j]->type, type))
159  return funcs[j];
160  }
161  }
162  return NULL;
163 }
164 
165 bool SymbolTable::AddType(const char *name, const Type *type, SourcePos pos) {
166  const Type *t = LookupType(name);
167  if (t != NULL && CastType<UndefinedStructType>(t) == NULL) {
168  // If we have a previous declaration of anything other than an
169  // UndefinedStructType with this struct name, issue an error. If
170  // we have an UndefinedStructType, then we'll fall through to the
171  // code below that adds the definition to the type map.
172  Error(pos, "Ignoring redefinition of type \"%s\".", name);
173  return false;
174  }
175 
176  types[name] = type;
177  return true;
178 }
179 
180 const Type *SymbolTable::LookupType(const char *name) const {
181  // Again, search through the type maps backward to get scoping right.
182  TypeMapType::const_iterator iter = types.find(name);
183  if (iter != types.end())
184  return iter->second;
185  return NULL;
186 }
187 
188 bool SymbolTable::ContainsType(const Type *type) const {
189  TypeMapType::const_iterator iter = types.begin();
190  while (iter != types.end()) {
191  if (iter->second == type) {
192  return true;
193  }
194  iter++;
195  }
196  return false;
197 }
198 
199 std::vector<std::string> SymbolTable::ClosestVariableOrFunctionMatch(const char *str) const {
200  // This is a little wasteful, but we'll look through all of the
201  // variable and function symbols and compute the edit distance from the
202  // given string to them. If the edit distance is under maxDelta, then
203  // it goes in the entry of the matches[] array corresponding to its
204  // edit distance.
205  const int maxDelta = 2;
206  std::vector<std::string> matches[maxDelta + 1];
207 
208  for (int i = 0; i < (int)variables.size(); ++i) {
209  const SymbolMapType &sv = *(variables[i]);
210  SymbolMapType::const_iterator iter;
211  for (iter = sv.begin(); iter != sv.end(); ++iter) {
212  const Symbol *sym = iter->second;
213  int dist = StringEditDistance(str, sym->name, maxDelta + 1);
214  if (dist <= maxDelta)
215  matches[dist].push_back(sym->name);
216  }
217  }
218 
219  FunctionMapType::const_iterator iter;
220  for (iter = functions.begin(); iter != functions.end(); ++iter) {
221  int dist = StringEditDistance(str, iter->first, maxDelta + 1);
222  if (dist <= maxDelta)
223  matches[dist].push_back(iter->first);
224  }
225 
226  // Now, return the first entry of matches[] that is non-empty, if any.
227  for (int i = 0; i <= maxDelta; ++i) {
228  if (matches[i].size())
229  return matches[i];
230  }
231 
232  // Otherwise, no joy.
233  return std::vector<std::string>();
234 }
235 
236 std::vector<std::string> SymbolTable::ClosestTypeMatch(const char *str) const { return closestTypeMatch(str, true); }
237 
238 std::vector<std::string> SymbolTable::ClosestEnumTypeMatch(const char *str) const {
239  return closestTypeMatch(str, false);
240 }
241 
242 std::vector<std::string> SymbolTable::closestTypeMatch(const char *str, bool structsVsEnums) const {
243  // This follows the same approach as ClosestVariableOrFunctionMatch()
244  // above; compute all edit distances, keep the ones shorter than
245  // maxDelta, return the first non-empty vector of one or more sets of
246  // alternatives with minimal edit distance.
247  const int maxDelta = 2;
248  std::vector<std::string> matches[maxDelta + 1];
249 
250  TypeMapType::const_iterator iter;
251  for (iter = types.begin(); iter != types.end(); ++iter) {
252  // Skip over either StructTypes or EnumTypes, depending on the
253  // value of the structsVsEnums parameter
254  bool isEnum = (CastType<EnumType>(iter->second) != NULL);
255  if (isEnum && structsVsEnums)
256  continue;
257  else if (!isEnum && !structsVsEnums)
258  continue;
259 
260  int dist = StringEditDistance(str, iter->first, maxDelta + 1);
261  if (dist <= maxDelta)
262  matches[dist].push_back(iter->first);
263  }
264 
265  for (int i = 0; i <= maxDelta; ++i) {
266  if (matches[i].size())
267  return matches[i];
268  }
269  return std::vector<std::string>();
270 }
271 
273  int depth = 0;
274  fprintf(stderr, "Variables:\n----------------\n");
275  for (int i = 0; i < (int)variables.size(); ++i) {
276  SymbolMapType &sm = *(variables[i]);
277  SymbolMapType::iterator iter;
278  for (iter = sm.begin(); iter != sm.end(); ++iter) {
279  fprintf(stderr, "%*c", depth, ' ');
280  Symbol *sym = iter->second;
281  fprintf(stderr, "%s [%s]", sym->name.c_str(), sym->type->GetString().c_str());
282  }
283  fprintf(stderr, "\n");
284  depth += 4;
285  }
286 
287  fprintf(stderr, "Functions:\n----------------\n");
288  FunctionMapType::iterator fiter = functions.begin();
289  while (fiter != functions.end()) {
290  fprintf(stderr, "%s\n", fiter->first.c_str());
291  std::vector<Symbol *> &syms = fiter->second;
292  for (unsigned int j = 0; j < syms.size(); ++j)
293  fprintf(stderr, " %s\n", syms[j]->type->GetString().c_str());
294  ++fiter;
295  }
296 
297  depth = 0;
298  fprintf(stderr, "Named types:\n---------------\n");
299  TypeMapType::iterator siter = types.begin();
300  while (siter != types.end()) {
301  fprintf(stderr, "%*c", depth, ' ');
302  fprintf(stderr, "%s -> %s\n", siter->first.c_str(), siter->second->GetString().c_str());
303  ++siter;
304  }
305 }
306 
307 inline int ispcRand() {
308 #ifdef ISPC_HOST_IS_WINDOWS
309  return rand();
310 #else
311  return lrand48();
312 #endif
313 }
314 
316  int v = ispcRand() % variables.size();
317  if (variables[v]->size() == 0)
318  return NULL;
319  int count = ispcRand() % variables[v]->size();
320  SymbolMapType::iterator iter = variables[v]->begin();
321  while (count-- > 0) {
322  ++iter;
323  Assert(iter != variables[v]->end());
324  }
325  return iter->second;
326 }
327 
329  int count = types.size();
330  TypeMapType::iterator iter = types.begin();
331  while (count-- > 0) {
332  ++iter;
333  Assert(iter != types.end());
334  }
335  return iter->second;
336 }
llvm::Value * storagePtr
Definition: sym.h:70
void Print()
Definition: sym.cpp:272
bool AddFunction(Symbol *symbol)
Definition: sym.cpp:126
void PushScope()
Definition: sym.cpp:67
const Type * LookupType(const char *name) const
Definition: sym.cpp:180
bool ContainsType(const Type *type) const
Definition: sym.cpp:188
bool AddVariable(Symbol *symbol)
Definition: sym.cpp:85
std::vector< std::string > ClosestEnumTypeMatch(const char *name) const
Definition: sym.cpp:238
std::string name
Definition: sym.h:69
Symbol * LookupVariable(const char *name)
Definition: sym.cpp:112
~SymbolTable()
Definition: sym.cpp:61
ConstExpr * constValue
Definition: sym.h:85
header file with declarations for symbol and symbol table classes.
std::vector< std::string > closestTypeMatch(const char *str, bool structsVsEnums) const
Definition: sym.cpp:242
Symbol(const std::string &name, SourcePos pos, const Type *t=NULL, StorageClass sc=SC_NONE)
Definition: sym.cpp:46
bool AddType(const char *name, const Type *type, SourcePos pos)
Definition: sym.cpp:165
bool LookupFunction(const char *name, std::vector< Symbol *> *matches=NULL)
Definition: sym.cpp:139
const Type * RandomType()
Definition: sym.cpp:328
SourcePos pos
Definition: sym.h:68
StorageClass storageClass
Definition: sym.h:94
Representation of a range of positions in a source file.
Definition: ispc.h:123
virtual std::string GetString() const =0
StorageClass
Definition: ispc.h:114
void Error(SourcePos p, const char *fmt,...)
Definition: util.cpp:351
std::vector< std::string > ClosestTypeMatch(const char *name) const
Definition: sym.cpp:236
SymbolTable()
Definition: sym.cpp:59
int StringEditDistance(const std::string &str1, const std::string &str2, int maxDist)
Definition: util.cpp:455
#define Assert(expr)
Definition: util.h:128
static bool Equal(const Type *a, const Type *b)
Definition: type.cpp:2853
std::map< std::string, Symbol * > SymbolMapType
Definition: sym.h:257
Type representing a function (return type + argument types)
Definition: type.h:829
Representation of a program symbol.
Definition: sym.h:62
int varyingCFDepth
Definition: sym.h:96
Interface class that defines the type abstraction.
Definition: type.h:90
const Function * parentFunction
Definition: sym.h:102
void PopScope()
Definition: sym.cpp:79
Symbol * RandomSymbol()
Definition: sym.cpp:315
const Type * type
Definition: sym.h:82
void Warning(SourcePos p, const char *fmt,...)
Definition: util.cpp:378
int ispcRand()
Definition: sym.cpp:307
llvm::Function * exportedFunction
Definition: sym.h:77
File with declarations for classes related to type representation.
std::vector< std::string > ClosestVariableOrFunctionMatch(const char *name) const
Definition: sym.cpp:199