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
29namespace SeExpr2 {
30class ExprVarRef;
31class ExprLocalVar;
32class ExprNode;
33class ExprLocalFunctionNode;
34class Interpreter;
35
38 protected:
42
43 public:
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.
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();
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
91};
92
95 private:
96 typedef std::map<std::string, std::unique_ptr<ExprLocalVar>> VarDictType;
98 typedef std::map<std::string, ExprLocalFunctionNode*> FuncDictType;
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
110
111 protected:
114
115 public:
116 // TODO: figure out when anotherOwns is needed
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.
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
149 public:
153 void reset() {
154 std::unique_ptr<ExprVarEnv> newEnv(new ExprVarEnv);
155 _currentEnv = newEnv.get();
156 all.emplace_back(std::move(newEnv));
157 }
161 void setCurrent(ExprVarEnv* env) { _currentEnv = env; }
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;
175};
176
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
double LLVM_BUILDER
Definition: ExprLLVM.h:34
#define LLVM_BODY
Definition: ExprLLVM.h:35
double LLVM_VALUE
Definition: ExprLLVM.h:33
Node that contains local function.
Definition: ExprNode.h:307
ExprLocalVar join (merge) references. Remembers which variables are possible assigners to this.
Definition: ExprEnv.h:67
ExprLocalVar * _elseVar
Definition: ExprEnv.h:90
bool valid() const
Definition: ExprEnv.h:81
ExprNode * _condNode
Definition: ExprEnv.h:89
ExprLocalVar * _thenVar
Definition: ExprEnv.h:90
ExprLocalVarPhi(ExprType condLife, ExprLocalVar *thenVar, ExprLocalVar *elseVar)
Definition: ExprEnv.h:69
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
ExprLocalVar reference, all local variables in seexpr are subclasses of this or this itself.
Definition: ExprEnv.h:37
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
const ExprLocalVar * getPhi() const
get the primary representative phi node (i.e. the global parent of a dependent phi node)
Definition: ExprEnv.h:49
virtual LLVM_VALUE varPtr()
LLVM value that has been pre-done.
Definition: ExprEnv.h:59
LLVM_VALUE _varPtr
Definition: ExprEnv.h:41
ExprLocalVar(const ExprType &type)
Definition: ExprEnv.h:44
ExprType type() const
returns type of the variable
Definition: ExprEnv.h:51
virtual ~ExprLocalVar()
Definition: ExprEnv.h:46
virtual LLVM_VALUE codegen(LLVM_BUILDER, const std::string &name, LLVM_VALUE referenceType) LLVM_BODY
LLVM value that has been allocated.
int buildInterpreter(Interpreter *interpreter) const
Allocates variable for interpreter.
ExprLocalVar * _phi
Definition: ExprEnv.h:40
static bool valuesCompatible(const ExprType &a, const ExprType &b)
Checks if value types are compatible.
Definition: ExprType.h:173
bool isFP() const
Direct is predicate checks.
Definition: ExprType.h:164
ExprType & setLifetime(const ExprType &a)
Assign the lifetime from type a to be my type.
Definition: ExprType.h:136
bool isError() const
Definition: ExprType.h:168
Variable scope builder is used by the type checking and code gen to track visiblity of variables and ...
Definition: ExprEnv.h:148
std::vector< std::unique_ptr< ExprVarEnv > > all
All owned symbol tables.
Definition: ExprEnv.h:172
void setCurrent(ExprVarEnv *env)
Set a new current variable scope.
Definition: ExprEnv.h:161
ExprVarEnvBuilder()
Creates an empty builder with one current scope entry.
Definition: ExprEnv.h:151
ExprVarEnv * current()
Return the current variable scope.
Definition: ExprEnv.h:159
ExprVarEnv * createDescendant(ExprVarEnv *parent)
Create a descendant scope from the provided parent, does not clobber current.
Definition: ExprEnv.h:163
void reset()
Reset to factory state (one empty environment that is current)
Definition: ExprEnv.h:153
ExprVarEnv * _currentEnv
The current symbol table (should be a pointer owned by all)
Definition: ExprEnv.h:174
Variable scope for tracking variable lookup.
Definition: ExprEnv.h:94
ExprVarEnv()
Create a scope with no parent.
Definition: ExprEnv.h:118
void add(const std::string &name, std::unique_ptr< ExprLocalVar > var)
Add a variable refernece.
Definition: ExprEnv.cpp:71
std::vector< std::vector< std::pair< std::string, ExprLocalVarPhi * > > > _mergedVariables
Keep track of all merged variables in.
Definition: ExprEnv.h:106
VarDictType _map
Definition: ExprEnv.h:97
ExprLocalVar * find(const std::string &name)
Find a variable name by name (recursive to parents)
Definition: ExprEnv.cpp:29
ExprVarEnv * _parent
Parent variable environment has all variablesf rom previou scope (for lookup)
Definition: ExprEnv.h:109
LLVM_VALUE codegenMerges(LLVM_BUILDER builder, int mergeIndex) LLVM_BODY
FuncDictType _functions
Definition: ExprEnv.h:99
std::map< std::string, ExprLocalFunctionNode * > FuncDictType
Definition: ExprEnv.h:98
std::vector< std::unique_ptr< ExprLocalVar > > shadowedVariables
Variables that have been superceded (and thus are inaccessible)
Definition: ExprEnv.h:103
ExprVarEnv(ExprVarEnv &other)
ExprLocalVar const * lookup(const std::string &name) const
Find a const variable reference name by name (recursive to parents)
Definition: ExprEnv.cpp:49
void resetAndSetParent(ExprVarEnv *parent)
Resets the scope (deletes all variables) and sets parent.
Definition: ExprEnv.cpp:27
void addFunction(const std::string &name, ExprLocalFunctionNode *prototype)
Add a function.
Definition: ExprEnv.cpp:58
ExprLocalFunctionNode * findFunction(const std::string &name)
Find a function by name (recursive to parents)
Definition: ExprEnv.cpp:39
std::map< std::string, std::unique_ptr< ExprLocalVar > > VarDictType
Definition: ExprEnv.h:96
std::vector< std::pair< std::string, ExprLocalVarPhi * > > & merge(size_t index)
Definition: ExprEnv.h:142
ExprVarEnv & operator=(ExprVarEnv &other)
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
Evaluation result.
Definition: ExprEnv.h:178
ExprEvalResult(int n, double *fp, const char **c)
Definition: ExprEnv.h:182
ExprEvalResult(const char **c)
Definition: ExprEnv.h:181
const char ** str
Definition: ExprEnv.h:186
ExprEvalResult(int n, double *fp)
Definition: ExprEnv.h:180
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