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 namespace SeExpr2 {
30 class ExprVarRef;
31 class ExprLocalVar;
32 class ExprNode;
33 class ExprLocalFunctionNode;
34 class Interpreter;
35
37 class ExprLocalVar {
38 protected:
39 ExprType _type;
40 ExprLocalVar* _phi;
41 mutable LLVM_VALUE _varPtr;
42
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
66 // This is basically like single assignment form inspired. hence the phi node nomenclature.
67 class ExprLocalVarPhi : public ExprLocalVar {
68 public:
69 ExprLocalVarPhi(ExprType condLife, ExprLocalVar* thenVar, ExprLocalVar* elseVar)
70 : ExprLocalVar(ExprType()), _thenVar(thenVar), _elseVar(elseVar) {
71 // find the compatible common-denominator lifetime
72 ExprType firstType = _thenVar->type(), secondType = _elseVar->type();
73 if (ExprType::valuesCompatible(_thenVar->type(), _elseVar->type())) {
74 _type = ((firstType.isFP(1) ? secondType : firstType).setLifetime(firstType, secondType));
75 }
76 // lifetime should be the minimum (error=0,varying=1,uniform=2,constant=3).
77 // i.e. you can only guarantee something is constant if the condition, ifvar, and else var are the same
78 _type.setLifetime(firstType, secondType, condLife);
79 }
80
81 bool valid() const { return !_type.isError(); }
82
83 void setPhi(ExprLocalVar* phi) {
84 _phi = phi;
85 _thenVar->setPhi(phi);
86 _elseVar->setPhi(phi);
87 }
88
89 ExprNode* _condNode;
90 ExprLocalVar* _thenVar, *_elseVar;
91 };
92
94 class ExprVarEnv {
95 private:
96 typedef std::map<std::string, std::unique_ptr<ExprLocalVar>> VarDictType;
97 VarDictType _map;
98 typedef std::map<std::string, ExprLocalFunctionNode*> FuncDictType;
99 FuncDictType _functions;
100
102 // i.e. a=3;a=[1,2,3];a=[2];a will yield 2 entries in shadowedVariables
103 std::vector<std::unique_ptr<ExprLocalVar>> shadowedVariables;
104
106 std::vector<std::vector<std::pair<std::string, ExprLocalVarPhi*>>> _mergedVariables;
107
109 ExprVarEnv* _parent;
110
111 protected:
112 ExprVarEnv(ExprVarEnv& other);
113 ExprVarEnv& operator=(ExprVarEnv& other);
114
115 public:
116 // TODO: figure out when anotherOwns is needed
118 ExprVarEnv() : _parent(0) {};
119
120 ~ExprVarEnv();
121
123 void resetAndSetParent(ExprVarEnv* parent);
125 ExprLocalFunctionNode* findFunction(const std::string& name);
127 ExprLocalVar* find(const std::string& name);
129 ExprLocalVar const* lookup(const std::string& name) const;
131 void addFunction(const std::string& name, ExprLocalFunctionNode* prototype);
133 void add(const std::string& name, std::unique_ptr<ExprLocalVar> var);
135 // void add(ExprVarEnv & env,const ExprType & modifyingType);
137 // static bool branchesMatch(const ExprVarEnv & env1, const ExprVarEnv & env2);
138 size_t mergeBranches(const ExprType& type, ExprVarEnv& env1, ExprVarEnv& env2);
139 // Code generate merges.
140 LLVM_VALUE codegenMerges(LLVM_BUILDER builder, int mergeIndex) LLVM_BODY;
141 // Query merges
142 std::vector<std::pair<std::string, ExprLocalVarPhi*>>& merge(size_t index) { return _mergedVariables[index]; }
143 };
144
146 //scopes
147 // It is inspired by IRBuilder's notion of a basic block insertion point
148 class ExprVarEnvBuilder {
149 public:
151 ExprVarEnvBuilder() { reset(); }
153 void reset() {
154 std::unique_ptr<ExprVarEnv> newEnv(new ExprVarEnv);
155 _currentEnv = newEnv.get();
156 all.emplace_back(std::move(newEnv));
157 }
159 ExprVarEnv* current() { return _currentEnv; }
161 void setCurrent(ExprVarEnv* env) { _currentEnv = env; }
163 ExprVarEnv* createDescendant(ExprVarEnv* parent) {
164 std::unique_ptr<ExprVarEnv> newEnv(new ExprVarEnv);
165 newEnv->resetAndSetParent(parent);
166 all.emplace_back(std::move(newEnv));
167 return all.back().get();
168 }
169
170 private:
172 std::vector<std::unique_ptr<ExprVarEnv>> all;
174 ExprVarEnv* _currentEnv;
175 };
176
178 struct ExprEvalResult {
179 ExprEvalResult() : n(0), fp(0), str(0) {}
180 ExprEvalResult(int n, double* fp) : n(n), fp(fp), str(0) {}
181 ExprEvalResult(const char** c) : n(1), fp(0), str(c) {}
182 ExprEvalResult(int n, double* fp, const char** c) : n(n), fp(fp), str(c) {}
183
184 int n;
185 double* fp;
186 const char** str;
187 };
188 }
189 #endif
SeExpr2::ExprLocalVar::_phi
ExprLocalVar * _phi
Definition: ExprEnv.h:40
index
The result is computed int int< br >< div style="margin-left: 40px;"> Picks values randomly between loRange and hiRange based on supplied index(which is automatically hashed). &nbsp
SeExpr2::ExprType::isError
bool isError() const
Definition: ExprType.h:168
SeExpr2::ExprVarEnv::find
ExprLocalVar * find(const std::string &name)
Find a variable name by name (recursive to parents)
Definition: ExprEnv.cpp:29
SeExpr2::ExprVarEnv::findFunction
ExprLocalFunctionNode * findFunction(const std::string &name)
Find a function by name (recursive to parents)
Definition: ExprEnv.cpp:39
SeExpr2::ExprVarEnv::shadowedVariables
std::vector< std::unique_ptr< ExprLocalVar > > shadowedVariables
Variables that have been superceded (and thus are inaccessible)
Definition: ExprEnv.h:103
SeExpr2::ExprVarEnvBuilder::createDescendant
ExprVarEnv * createDescendant(ExprVarEnv *parent)
Create a descendant scope from the provided parent, does not clobber current.
Definition: ExprEnv.h:163
SeExpr2::ExprNode
Definition: ExprNode.h:72
SeExpr2::ExprLocalVar::ExprLocalVar
ExprLocalVar(const ExprType &type)
Definition: ExprEnv.h:44
SeExpr2::ExprEvalResult
Evaluation result.
Definition: ExprEnv.h:178
LLVM_BUILDER
double LLVM_BUILDER
Definition: ExprLLVM.h:34
SeExpr2::ExprVarEnv::operator=
ExprVarEnv & operator=(ExprVarEnv &other)
SeExpr2::ExprEvalResult::ExprEvalResult
ExprEvalResult(int n, double *fp)
Definition: ExprEnv.h:180
SeExpr2::ExprLocalVar::~ExprLocalVar
virtual ~ExprLocalVar()
Definition: ExprEnv.h:46
SeExpr2::ExprVarEnvBuilder::all
std::vector< std::unique_ptr< ExprVarEnv > > all
All owned symbol tables.
Definition: ExprEnv.h:172
SeExpr2::ExprLocalVarPhi
ExprLocalVar join (merge) references. Remembers which variables are possible assigners to this.
Definition: ExprEnv.h:67
SeExpr2::ExprVarEnv::lookup
ExprLocalVar const * lookup(const std::string &name) const
Find a const variable reference name by name (recursive to parents)
Definition: ExprEnv.cpp:49
SeExpr2::ExprType
Definition: ExprType.h:39
SeExpr2::ExprVarEnv::codegenMerges
LLVM_VALUE codegenMerges(LLVM_BUILDER builder, int mergeIndex) LLVM_BODY
SeExpr2::ExprLocalVar::type
ExprType type() const
returns type of the variable
Definition: ExprEnv.h:51
SeExpr2::ExprLocalVar::codegen
virtual LLVM_VALUE codegen(LLVM_BUILDER, const std::string &name, LLVM_VALUE referenceType) LLVM_BODY
LLVM value that has been allocated.
SeExpr2::ExprVarEnv
Variable scope for tracking variable lookup.
Definition: ExprEnv.h:94
SeExpr2::ExprLocalVar::_type
ExprType _type
Definition: ExprEnv.h:39
SeExpr2::ExprVarEnv::resetAndSetParent
void resetAndSetParent(ExprVarEnv *parent)
Resets the scope (deletes all variables) and sets parent.
Definition: ExprEnv.cpp:27
LLVM_VALUE
double LLVM_VALUE
Definition: ExprLLVM.h:33
SeExpr2
Definition: Context.h:22
SeExpr2::ExprVarEnv::_mergedVariables
std::vector< std::vector< std::pair< std::string, ExprLocalVarPhi * > > > _mergedVariables
Keep track of all merged variables in.
Definition: ExprEnv.h:106
SeExpr2::ExprVarEnvBuilder::reset
void reset()
Reset to factory state (one empty environment that is current)
Definition: ExprEnv.h:153
SeExpr2::ExprLocalVar::buildInterpreter
int buildInterpreter(Interpreter *interpreter) const
Allocates variable for interpreter.
Definition: Interpreter.cpp:738
ExprType.h
SeExpr2::ExprEvalResult::ExprEvalResult
ExprEvalResult(const char **c)
Definition: ExprEnv.h:181
SeExpr2::ExprVarEnv::_map
VarDictType _map
Definition: ExprEnv.h:97
ExprLLVM.h
SeExpr2::ExprVarEnvBuilder::current
ExprVarEnv * current()
Return the current variable scope.
Definition: ExprEnv.h:159
SeExpr2::ExprLocalVar::varPtr
virtual LLVM_VALUE varPtr()
LLVM value that has been pre-done.
Definition: ExprEnv.h:59
SeExpr2::ExprVarEnv::_functions
FuncDictType _functions
Definition: ExprEnv.h:99
SeExpr2::ExprVarEnvBuilder
Variable scope builder is used by the type checking and code gen to track visiblity of variables and ...
Definition: ExprEnv.h:148
SeExpr2::ExprEvalResult::ExprEvalResult
ExprEvalResult()
Definition: ExprEnv.h:179
SeExpr2::ExprLocalVarPhi::valid
bool valid() const
Definition: ExprEnv.h:81
SeExpr2::ExprLocalVarPhi::_condNode
ExprNode * _condNode
Definition: ExprEnv.h:89
SeExpr2::ExprType::isFP
bool isFP() const
Direct is predicate checks.
Definition: ExprType.h:164
SeExpr2::ExprVarEnv::_parent
ExprVarEnv * _parent
Parent variable environment has all variablesf rom previou scope (for lookup)
Definition: ExprEnv.h:109
SeExpr2::ExprVarEnv::addFunction
void addFunction(const std::string &name, ExprLocalFunctionNode *prototype)
Add a function.
Definition: ExprEnv.cpp:58
SeExpr2::ExprVarEnvBuilder::_currentEnv
ExprVarEnv * _currentEnv
The current symbol table (should be a pointer owned by all)
Definition: ExprEnv.h:174
SeExpr2::ExprVarEnv::add
void add(const std::string &name, std::unique_ptr< ExprLocalVar > var)
Add a variable refernece.
Definition: ExprEnv.cpp:71
SeExpr2::ExprVarEnv::VarDictType
std::map< std::string, std::unique_ptr< ExprLocalVar > > VarDictType
Definition: ExprEnv.h:96
SeExpr2::ExprVarEnv::mergeBranches
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's lifetime.
Definition: ExprEnv.cpp:81
SeExpr2::ExprLocalVarPhi::setPhi
void setPhi(ExprLocalVar *phi)
sets the representative phi node (like a brute force set unioning operation) phi is the set represent...
Definition: ExprEnv.h:83
SeExpr2::ExprType::setLifetime
ExprType & setLifetime(const ExprType &a)
Assign the lifetime from type a to be my type.
Definition: ExprType.h:136
SeExpr2::ExprEvalResult::fp
double * fp
Definition: ExprEnv.h:185
SeExpr2::ExprLocalFunctionNode
Node that contains local function.
Definition: ExprNode.h:307
SeExpr2::ExprLocalVarPhi::_thenVar
ExprLocalVar * _thenVar
Definition: ExprEnv.h:90
SeExpr2::ExprLocalVarPhi::ExprLocalVarPhi
ExprLocalVarPhi(ExprType condLife, ExprLocalVar *thenVar, ExprLocalVar *elseVar)
Definition: ExprEnv.h:69
SeExpr2::ExprVarEnvBuilder::ExprVarEnvBuilder
ExprVarEnvBuilder()
Creates an empty builder with one current scope entry.
Definition: ExprEnv.h:151
SeExpr2::ExprVarEnv::ExprVarEnv
ExprVarEnv()
Create a scope with no parent.
Definition: ExprEnv.h:118
SeExpr2::ExprEvalResult::str
const char ** str
Definition: ExprEnv.h:186
SeExpr2::ExprType::valuesCompatible
static bool valuesCompatible(const ExprType &a, const ExprType &b)
Checks if value types are compatible.
Definition: ExprType.h:173
LLVM_BODY
#define LLVM_BODY
Definition: ExprLLVM.h:35
SeExpr2::ExprEvalResult::n
int n
Definition: ExprEnv.h:184
SeExpr2::ExprVarEnv::~ExprVarEnv
~ExprVarEnv()
Definition: ExprEnv.cpp:25
SeExpr2::ExprVarEnv::merge
std::vector< std::pair< std::string, ExprLocalVarPhi * > > & merge(size_t index)
Definition: ExprEnv.h:142
SeExpr2::ExprVarEnv::FuncDictType
std::map< std::string, ExprLocalFunctionNode * > FuncDictType
Definition: ExprEnv.h:98
SeExpr2::ExprVarEnvBuilder::setCurrent
void setCurrent(ExprVarEnv *env)
Set a new current variable scope.
Definition: ExprEnv.h:161
SeExpr2::ExprEvalResult::ExprEvalResult
ExprEvalResult(int n, double *fp, const char **c)
Definition: ExprEnv.h:182
SeExpr2::Interpreter
Definition: Interpreter.h:40
SeExpr2::ExprLocalVar::getPhi
const ExprLocalVar * getPhi() const
get the primary representative phi node (i.e. the global parent of a dependent phi node)
Definition: ExprEnv.h:49
SeExpr2::ExprLocalVar
ExprLocalVar reference, all local variables in seexpr are subclasses of this or this itself.
Definition: ExprEnv.h:37
SeExpr2::ExprLocalVar::setPhi
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
SeExpr2::ExprLocalVar::_varPtr
LLVM_VALUE _varPtr
Definition: ExprEnv.h:41
SeExpr2::ExprLocalVarPhi::_elseVar
ExprLocalVar * _elseVar
Definition: ExprEnv.h:90