SeExpr
ExprEnv.h
Go to the documentation of this file.
1 /*
2  Copyright Disney Enterprises, Inc. All rights reserved.
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License
6  and the following modification to it: Section 6 Trademarks.
7  deleted and replaced with:
8 
9  6. Trademarks. This License does not grant permission to use the
10  trade names, trademarks, service marks, or product names of the
11  Licensor and its affiliates, except as required for reproducing
12  the content of the NOTICE file.
13 
14  You may obtain a copy of the License at
15  http://www.apache.org/licenses/LICENSE-2.0
16 */
17 #ifndef ExprEnv_h
18 #define ExprEnv_h
19 
20 #include <vector>
21 #include <map>
22 #include <cassert>
23 #include <memory>
24 
25 #include "ExprType.h"
26 #include "ExprLLVM.h"
27 #include <iostream>
28 
29 
30 namespace SeExpr2 {
31 class ExprVarRef;
32 class ExprLocalVar;
33 class ExprNode;
34 class ExprLocalFunctionNode;
35 class Interpreter;
36 
38 class ExprLocalVar {
39  protected:
43  public:
44  ExprLocalVar(const ExprType& type) : _type(type), _phi(0), _varPtr(0) {}
45 
46  virtual ~ExprLocalVar() {}
47 
49  const ExprLocalVar* getPhi() const { return _phi; }
51  ExprType type() const { return _type; }
53  virtual void setPhi(ExprLocalVar* phi) { _phi = phi; }
54 
56  virtual LLVM_VALUE codegen(LLVM_BUILDER,const std::string& name,LLVM_VALUE referenceType) LLVM_BODY;
57 
59  virtual LLVM_VALUE varPtr() {return _varPtr;}
60 
62  int buildInterpreter(Interpreter* interpreter) const;
63 
64 };
65 
67 // This is basically like single assignment form inspired. hence the phi node nomenclature.
68 class ExprLocalVarPhi : public ExprLocalVar {
69  public:
70  ExprLocalVarPhi(ExprType condLife, ExprLocalVar* thenVar, ExprLocalVar* elseVar)
71  : ExprLocalVar(ExprType()), _thenVar(thenVar), _elseVar(elseVar) {
72  // find the compatible common-denominator lifetime
73  ExprType firstType=_thenVar->type(),secondType=_elseVar->type();
75  _type=((firstType.isFP(1) ? secondType : firstType).setLifetime(firstType, secondType));
76  }
77  // lifetime should be the minimum (error=0,varying=1,uniform=2,constant=3).
78  // i.e. you can only guarantee something is constant if the condition, ifvar, and else var are the same
79  _type.setLifetime(firstType,secondType,condLife);
80  }
81 
82  bool valid() const{
83  return !_type.isError();
84  }
85 
86  void setPhi(ExprLocalVar* phi) {
87  _phi = phi;
88  _thenVar->setPhi(phi);
89  _elseVar->setPhi(phi);
90  }
91 
94 };
95 
97 class ExprVarEnv {
98  private:
99  typedef std::map<std::string, std::unique_ptr<ExprLocalVar>> VarDictType;
101  typedef std::map<std::string, ExprLocalFunctionNode*> FuncDictType;
103 
105  // i.e. a=3;a=[1,2,3];a=[2];a will yield 2 entries in shadowedVariables
106  std::vector<std::unique_ptr<ExprLocalVar>> shadowedVariables;
107 
109  std::vector<std::vector<std::pair<std::string,ExprLocalVarPhi*>>> _mergedVariables;
110 
113 
114  protected:
115  ExprVarEnv(ExprVarEnv& other);
117 
118  public:
119  // TODO: figure out when anotherOwns is needed
121  ExprVarEnv() : _parent(0) {};
122 
123  ~ExprVarEnv();
124 
126  void resetAndSetParent(ExprVarEnv* parent);
128  ExprLocalFunctionNode* findFunction(const std::string& name);
130  ExprLocalVar* find(const std::string& name);
132  ExprLocalVar const* lookup(const std::string& name) const;
134  void addFunction(const std::string& name, ExprLocalFunctionNode* prototype);
136  void add(const std::string& name, std::unique_ptr<ExprLocalVar> var);
138  // void add(ExprVarEnv & env,const ExprType & modifyingType);
140  // static bool branchesMatch(const ExprVarEnv & env1, const ExprVarEnv & env2);
141  size_t mergeBranches(const ExprType& type, ExprVarEnv& env1, ExprVarEnv& env2);
142  // Code generate merges.
143  LLVM_VALUE codegenMerges(LLVM_BUILDER builder,int mergeIndex) LLVM_BODY;
144  // Query merges
145  std::vector<std::pair<std::string,ExprLocalVarPhi*>>& merge(size_t index){
146  return _mergedVariables[index];
147  }
148 };
149 
151 // It is inspired by IRBuilder's notion of a basic block insertion point
153 public:
156  reset();
157  }
159  void reset(){
160  std::unique_ptr<ExprVarEnv> newEnv(new ExprVarEnv);
161  _currentEnv=newEnv.get();
162  all.emplace_back(std::move(newEnv));
163  }
170  std::unique_ptr<ExprVarEnv> newEnv(new ExprVarEnv);
171  newEnv->resetAndSetParent(parent);
172  all.emplace_back(std::move(newEnv));
173  return all.back().get();
174  }
175 private:
177  std::vector<std::unique_ptr<ExprVarEnv>> all;
180 };
181 
184  ExprEvalResult() : n(0), fp(0), str(0) {}
185  ExprEvalResult(int n, double* fp) : n(n), fp(fp), str(0) {}
186  ExprEvalResult(const char** c) : n(1), fp(0), str(c) {}
187  ExprEvalResult(int n, double* fp, const char** c) : n(n), fp(fp), str(c) {}
188 
189  int n;
190  double* fp;
191  const char** str;
192 };
193 }
194 #endif
ExprEvalResult(int n, double *fp, const char **c)
Definition: ExprEnv.h:187
FuncDictType _functions
Definition: ExprEnv.h:102
ExprLocalVar * _elseVar
Definition: ExprEnv.h:93
std::vector< std::vector< std::pair< std::string, ExprLocalVarPhi * > > > _mergedVariables
Keep track of all merged variables in.
Definition: ExprEnv.h:109
ExprLocalVar(const ExprType &type)
Definition: ExprEnv.h:44
bool valid() const
Definition: ExprEnv.h:82
ExprEvalResult(int n, double *fp)
Definition: ExprEnv.h:185
bool isFP() const
Direct is predicate checks.
Definition: ExprType.h:164
std::vector< std::unique_ptr< ExprVarEnv > > all
All owned symbol tables.
Definition: ExprEnv.h:177
ExprType type() const
returns type of the variable
Definition: ExprEnv.h:51
VarDictType _map
Definition: ExprEnv.h:100
Node that contains local function.
Definition: ExprNode.h:307
std::vector< std::unique_ptr< ExprLocalVar > > shadowedVariables
Variables that have been superceded (and thus are inaccessible)
Definition: ExprEnv.h:106
int buildInterpreter(Interpreter *interpreter) const
Allocates variable for interpreter.
void add(const std::string &name, std::unique_ptr< ExprLocalVar > var)
Add a variable refernece.
Definition: ExprEnv.cpp:73
std::map< std::string, std::unique_ptr< ExprLocalVar > > VarDictType
Definition: ExprEnv.h:99
ExprVarEnv * createDescendant(ExprVarEnv *parent)
Create a descendant scope from the provided parent, does not clobber current.
Definition: ExprEnv.h:169
ExprEvalResult(const char **c)
Definition: ExprEnv.h:186
#define LLVM_BODY
Definition: ExprLLVM.h:35
ExprVarEnv * _currentEnv
The current symbol table (should be a pointer owned by all)
Definition: ExprEnv.h:179
ExprVarEnv()
Create a scope with no parent.
Definition: ExprEnv.h:121
size_t mergeBranches(const ExprType &type, ExprVarEnv &env1, ExprVarEnv &env2)
Add all variables into scope by name, but modify their lifetimes to the given type&#39;s lifetime...
Definition: ExprEnv.cpp:83
virtual ~ExprLocalVar()
Definition: ExprEnv.h:46
ExprType & setLifetime(const ExprType &a)
Assign the lifetime from type a to be my type.
Definition: ExprType.h:136
ExprLocalVar reference, all local variables in seexpr are subclasses of this or this itself...
Definition: ExprEnv.h:38
Variable scope for tracking variable lookup.
Definition: ExprEnv.h:97
const ExprLocalVar * getPhi() const
get the primary representative phi node (i.e. the global parent of a dependent phi node) ...
Definition: ExprEnv.h:49
The result is computed int int< br >< divstyle="margin-left:40px;"> Picks values randomly between loRange and hiRange based on supplied index(which is automatically hashed).&nbsp
const char ** str
Definition: ExprEnv.h:191
ExprVarEnv & operator=(ExprVarEnv &other)
virtual LLVM_VALUE codegen(LLVM_BUILDER, const std::string &name, LLVM_VALUE referenceType) LLVM_BODY
LLVM value that has been allocated.
void reset()
Reset to factory state (one empty environment that is current)
Definition: ExprEnv.h:159
static bool valuesCompatible(const ExprType &a, const ExprType &b)
Checks if value types are compatible.
Definition: ExprType.h:173
ExprLocalVar * find(const std::string &name)
Find a variable name by name (recursive to parents)
Definition: ExprEnv.cpp:31
std::map< std::string, ExprLocalFunctionNode * > FuncDictType
Definition: ExprEnv.h:101
ExprLocalFunctionNode * findFunction(const std::string &name)
Find a function by name (recursive to parents)
Definition: ExprEnv.cpp:41
Evaluation result.
Definition: ExprEnv.h:183
ExprVarEnv * current()
Return the current variable scope.
Definition: ExprEnv.h:165
double LLVM_BUILDER
Definition: ExprLLVM.h:34
virtual LLVM_VALUE varPtr()
LLVM value that has been pre-done.
Definition: ExprEnv.h:59
bool isError() const
Definition: ExprType.h:168
ExprLocalVar const * lookup(const std::string &name) const
Find a const variable reference name by name (recursive to parents)
Definition: ExprEnv.cpp:51
ExprVarEnv * _parent
Parent variable environment has all variablesf rom previou scope (for lookup)
Definition: ExprEnv.h:112
void resetAndSetParent(ExprVarEnv *parent)
Resets the scope (deletes all variables) and sets parent.
Definition: ExprEnv.cpp:27
void setPhi(ExprLocalVar *phi)
sets the representative phi node (like a brute force set unioning operation) phi is the set represent...
Definition: ExprEnv.h:86
ExprLocalVar * _thenVar
Definition: ExprEnv.h:93
ExprLocalVar join (merge) references. Remembers which variables are possible assigners to this...
Definition: ExprEnv.h:68
ExprNode * _condNode
Definition: ExprEnv.h:92
LLVM_VALUE _varPtr
Definition: ExprEnv.h:42
LLVM_VALUE codegenMerges(LLVM_BUILDER builder, int mergeIndex) LLVM_BODY
ExprLocalVar * _phi
Definition: ExprEnv.h:41
double LLVM_VALUE
Definition: ExprLLVM.h:33
ExprLocalVarPhi(ExprType condLife, ExprLocalVar *thenVar, ExprLocalVar *elseVar)
Definition: ExprEnv.h:70
void addFunction(const std::string &name, ExprLocalFunctionNode *prototype)
Add a function.
Definition: ExprEnv.cpp:60
void setCurrent(ExprVarEnv *env)
Set a new current variable scope.
Definition: ExprEnv.h:167
ExprVarEnvBuilder()
Creates an empty builder with one current scope entry.
Definition: ExprEnv.h:155
std::vector< std::pair< std::string, ExprLocalVarPhi * > > & merge(size_t index)
Definition: ExprEnv.h:145
virtual void setPhi(ExprLocalVar *phi)
sets the representative phi node (like a brute force set unioning operation) phi is the set represent...
Definition: ExprEnv.h:53
Variable scope builder is used by the type checking and code gen to track visiblity of variables and ...
Definition: ExprEnv.h:152