1 #ifndef MSP_GL_PROGRAMCOMPILER_H_
2 #define MSP_GL_PROGRAMCOMPILER_H_
5 #include "programparser.h"
6 #include "programsyntax.h"
17 struct Visitor: ProgramSyntax::TraversingVisitor
19 typedef void ResultType;
21 ProgramSyntax::Stage *stage;
25 virtual void apply(ProgramSyntax::Stage &);
26 void get_result() const { }
29 struct BlockModifier: Visitor
32 std::vector<RefPtr<ProgramSyntax::Statement> > insert_nodes;
36 void flatten_block(ProgramSyntax::Block &);
37 void apply_and_increment(ProgramSyntax::Block &, std::list<RefPtr<ProgramSyntax::Statement> >::iterator &);
39 virtual void visit(ProgramSyntax::Block &);
42 struct Formatter: Visitor
44 typedef std::string ResultType;
46 std::string formatted;
50 std::string block_interface;
54 virtual void apply(ProgramSyntax::Stage &);
55 const std::string &get_result() const { return formatted; }
57 virtual void visit(ProgramSyntax::Block &);
58 virtual void visit(ProgramSyntax::Literal &);
59 virtual void visit(ProgramSyntax::ParenthesizedExpression &);
60 virtual void visit(ProgramSyntax::VariableReference &);
61 virtual void visit(ProgramSyntax::MemberAccess &);
62 virtual void visit(ProgramSyntax::UnaryExpression &);
63 virtual void visit(ProgramSyntax::BinaryExpression &);
64 virtual void visit(ProgramSyntax::Assignment &);
65 virtual void visit(ProgramSyntax::FunctionCall &);
66 virtual void visit(ProgramSyntax::ExpressionStatement &);
67 virtual void visit(ProgramSyntax::Import &);
68 virtual void visit(ProgramSyntax::Precision &);
69 virtual void visit(ProgramSyntax::Layout &);
70 virtual void visit(ProgramSyntax::InterfaceLayout &);
71 virtual void visit(ProgramSyntax::StructDeclaration &);
72 virtual void visit(ProgramSyntax::VariableDeclaration &);
73 virtual void visit(ProgramSyntax::InterfaceBlock &);
74 virtual void visit(ProgramSyntax::FunctionDeclaration &);
75 virtual void visit(ProgramSyntax::Conditional &);
76 virtual void visit(ProgramSyntax::Iteration &);
77 virtual void visit(ProgramSyntax::Return &);
78 virtual void visit(ProgramSyntax::Jump &);
82 struct NodeGatherer: Visitor
84 typedef std::vector<T *> ResultType;
86 std::vector<T *> nodes;
88 const ResultType &get_result() const { return nodes; }
90 virtual void visit(T &n) { nodes.push_back(&n); }
93 struct DeclarationCombiner: BlockModifier
96 std::map<std::string, std::vector<ProgramSyntax::FunctionDeclaration *> > functions;
97 std::map<std::string, ProgramSyntax::VariableDeclaration *> variables;
99 DeclarationCombiner();
101 using Visitor::visit;
102 virtual void visit(ProgramSyntax::Block &);
103 virtual void visit(ProgramSyntax::FunctionDeclaration &);
104 virtual void visit(ProgramSyntax::VariableDeclaration &);
107 struct VariableResolver: Visitor
109 std::vector<ProgramSyntax::Block *> blocks;
110 ProgramSyntax::StructDeclaration *type;
112 std::string block_interface;
114 ProgramSyntax::VariableDeclaration *assignment_target;
115 bool self_referencing;
119 virtual void apply(ProgramSyntax::Stage &);
120 using Visitor::visit;
121 virtual void visit(ProgramSyntax::Block &);
122 virtual void visit(ProgramSyntax::VariableReference &);
123 virtual void visit(ProgramSyntax::MemberAccess &);
124 virtual void visit(ProgramSyntax::BinaryExpression &);
125 virtual void visit(ProgramSyntax::Assignment &);
126 virtual void visit(ProgramSyntax::StructDeclaration &);
127 virtual void visit(ProgramSyntax::VariableDeclaration &);
128 virtual void visit(ProgramSyntax::InterfaceBlock &);
131 struct FunctionResolver: Visitor
133 std::map<std::string, std::vector<ProgramSyntax::FunctionDeclaration *> > functions;
135 using Visitor::visit;
136 virtual void visit(ProgramSyntax::FunctionCall &);
137 virtual void visit(ProgramSyntax::FunctionDeclaration &);
140 struct InterfaceGenerator: BlockModifier
142 std::string in_prefix;
143 std::string out_prefix;
144 unsigned scope_level;
145 std::map<std::string, RefPtr<ProgramSyntax::VariableDeclaration> > iface_declarations;
147 InterfaceGenerator();
149 static std::string get_out_prefix(ProgramSyntax::StageType);
150 virtual void apply(ProgramSyntax::Stage &);
151 using Visitor::visit;
152 virtual void visit(ProgramSyntax::Block &);
153 std::string change_prefix(const std::string &, const std::string &) const;
154 bool generate_interface(ProgramSyntax::VariableDeclaration &, const std::string &, const std::string &);
155 void insert_assignment(const std::string &, ProgramSyntax::Expression *);
156 virtual void visit(ProgramSyntax::VariableReference &);
157 virtual void visit(ProgramSyntax::VariableDeclaration &);
158 virtual void visit(ProgramSyntax::Passthrough &);
161 struct DeclarationReorderer: Visitor
172 unsigned scope_level;
173 DeclarationKind kind;
174 std::set<ProgramSyntax::Node *> ordered_funcs;
175 std::set<ProgramSyntax::Node *> needed_funcs;
177 DeclarationReorderer();
179 using Visitor::visit;
180 virtual void visit(ProgramSyntax::Block &);
181 virtual void visit(ProgramSyntax::FunctionCall &);
182 virtual void visit(ProgramSyntax::InterfaceLayout &) { kind = LAYOUT; }
183 virtual void visit(ProgramSyntax::StructDeclaration &) { kind = STRUCT; }
184 virtual void visit(ProgramSyntax::VariableDeclaration &);
185 virtual void visit(ProgramSyntax::InterfaceBlock &) { kind = VARIABLE; }
186 virtual void visit(ProgramSyntax::FunctionDeclaration &);
189 struct InlineableFunctionLocator: Visitor
191 typedef std::set<ProgramSyntax::FunctionDeclaration *> ResultType;
193 std::map<ProgramSyntax::FunctionDeclaration *, unsigned> refcounts;
194 std::set<ProgramSyntax::FunctionDeclaration *> inlineable;
195 ProgramSyntax::FunctionDeclaration *in_function;
197 InlineableFunctionLocator();
199 const ResultType &get_result() const { return inlineable; }
200 using Visitor::visit;
201 virtual void visit(ProgramSyntax::FunctionCall &);
202 virtual void visit(ProgramSyntax::FunctionDeclaration &);
205 struct FunctionInliner: Visitor
207 std::set<ProgramSyntax::FunctionDeclaration *> inlineable;
208 unsigned extract_result;
209 RefPtr<ProgramSyntax::Expression> inline_result;
212 FunctionInliner(const std::set<ProgramSyntax::FunctionDeclaration *> &);
214 void visit_and_inline(RefPtr<ProgramSyntax::Expression> &);
215 using Visitor::visit;
216 virtual void visit(ProgramSyntax::Block &);
217 virtual void visit(ProgramSyntax::UnaryExpression &);
218 virtual void visit(ProgramSyntax::BinaryExpression &);
219 virtual void visit(ProgramSyntax::MemberAccess &);
220 virtual void visit(ProgramSyntax::FunctionCall &);
221 virtual void visit(ProgramSyntax::VariableDeclaration &);
222 virtual void visit(ProgramSyntax::Return &);
225 struct ExpressionEvaluator: ProgramSyntax::NodeVisitor
227 typedef std::map<ProgramSyntax::VariableDeclaration *, ProgramSyntax::Expression *> ValueMap;
229 const ValueMap *variable_values;
233 ExpressionEvaluator();
234 ExpressionEvaluator(const ValueMap &);
236 using ProgramSyntax::NodeVisitor::visit;
237 virtual void visit(ProgramSyntax::Literal &);
238 virtual void visit(ProgramSyntax::ParenthesizedExpression &);
239 virtual void visit(ProgramSyntax::VariableReference &);
240 virtual void visit(ProgramSyntax::UnaryExpression &);
241 virtual void visit(ProgramSyntax::BinaryExpression &);
244 struct ConstantConditionEliminator: BlockModifier
246 unsigned scope_level;
248 ExpressionEvaluator::ValueMap variable_values;
250 ConstantConditionEliminator();
252 using Visitor::visit;
253 virtual void visit(ProgramSyntax::Block &);
254 virtual void visit(ProgramSyntax::UnaryExpression &);
255 virtual void visit(ProgramSyntax::Assignment &);
256 virtual void visit(ProgramSyntax::VariableDeclaration &);
257 virtual void visit(ProgramSyntax::Conditional &);
258 virtual void visit(ProgramSyntax::Iteration &);
261 struct UnusedVariableLocator: Visitor
266 std::vector<ProgramSyntax::Node *> assignments;
267 bool conditionally_assigned;
273 typedef std::set<ProgramSyntax::Node *> ResultType;
274 typedef std::map<ProgramSyntax::VariableDeclaration *, VariableInfo> BlockVariableMap;
276 std::set<ProgramSyntax::Node *> unused_nodes;
277 std::map<ProgramSyntax::VariableDeclaration *, ProgramSyntax::Node *> aggregates;
278 ProgramSyntax::Node *aggregate;
279 std::vector<BlockVariableMap> variables;
280 ProgramSyntax::Assignment *assignment;
281 bool assignment_target;
282 bool assign_to_subscript;
285 UnusedVariableLocator();
287 virtual void apply(ProgramSyntax::Stage &);
288 const ResultType &get_result() const { return unused_nodes; }
289 using Visitor::visit;
290 virtual void visit(ProgramSyntax::VariableReference &);
291 virtual void visit(ProgramSyntax::MemberAccess &);
292 virtual void visit(ProgramSyntax::BinaryExpression &);
293 virtual void visit(ProgramSyntax::Assignment &);
294 void record_assignment(ProgramSyntax::VariableDeclaration &, ProgramSyntax::Node &, bool);
295 void clear_assignments(VariableInfo &, bool);
296 virtual void visit(ProgramSyntax::ExpressionStatement &);
297 virtual void visit(ProgramSyntax::StructDeclaration &);
298 virtual void visit(ProgramSyntax::VariableDeclaration &);
299 virtual void visit(ProgramSyntax::InterfaceBlock &);
300 virtual void visit(ProgramSyntax::FunctionDeclaration &);
301 void merge_down_variables();
302 virtual void visit(ProgramSyntax::Conditional &);
303 virtual void visit(ProgramSyntax::Iteration &);
306 struct UnusedFunctionLocator: Visitor
308 typedef std::set<ProgramSyntax::Node *> ResultType;
310 std::set<ProgramSyntax::Node *> unused_nodes;
311 std::set<ProgramSyntax::FunctionDeclaration *> used_definitions;
313 const ResultType &get_result() const { return unused_nodes; }
314 using Visitor::visit;
315 virtual void visit(ProgramSyntax::FunctionCall &);
316 virtual void visit(ProgramSyntax::FunctionDeclaration &);
319 struct NodeRemover: Visitor
321 std::set<ProgramSyntax::Node *> to_remove;
324 NodeRemover(const std::set<ProgramSyntax::Node *> &);
326 using Visitor::visit;
327 virtual void visit(ProgramSyntax::Block &);
328 virtual void visit(ProgramSyntax::VariableDeclaration &);
329 virtual void visit(ProgramSyntax::Iteration &);
332 struct PrecisionRemover: BlockModifier
334 using Visitor::visit;
335 virtual void visit(ProgramSyntax::Precision &);
336 virtual void visit(ProgramSyntax::VariableDeclaration &);
339 struct DefaultPrecisionGenerator: BlockModifier
342 std::set<std::string> have_default;
344 DefaultPrecisionGenerator();
346 using Visitor::visit;
347 virtual void visit(ProgramSyntax::Block &);
348 virtual void visit(ProgramSyntax::Precision &);
349 virtual void visit(ProgramSyntax::VariableDeclaration &);
352 struct LegacyConverter: BlockModifier
355 Version target_version;
357 ProgramSyntax::VariableDeclaration *frag_out;
360 LegacyConverter(const Version &);
362 bool check_version(const Version &) const;
363 bool check_extension(const Extension &) const;
364 using Visitor::visit;
365 bool supports_unified_interface_syntax() const;
366 virtual void visit(ProgramSyntax::VariableReference &);
367 virtual void visit(ProgramSyntax::Assignment &);
368 bool supports_unified_sampling_functions() const;
369 virtual void visit(ProgramSyntax::FunctionCall &);
370 bool supports_interface_layouts() const;
371 bool supports_centroid_sampling() const;
372 bool supports_sample_sampling() const;
373 virtual void visit(ProgramSyntax::VariableDeclaration &);
374 bool supports_interface_blocks(const std::string &) const;
375 virtual void visit(ProgramSyntax::InterfaceBlock &);
378 Resources *resources;
379 ProgramSyntax::Module *module;
380 std::set<std::string> imported_names;
386 void compile(const std::string &, const std::string & = "<string>");
387 void compile(IO::Base &, Resources * = 0, const std::string & = "<file>");
388 void compile(IO::Base &, const std::string &);
389 void add_shaders(Program &);
392 static ProgramSyntax::Module *create_builtins_module();
393 static ProgramSyntax::Module &get_builtins_module();
394 static ProgramSyntax::Stage *get_builtins(ProgramSyntax::StageType);
395 void append_module(ProgramSyntax::Module &);
396 void append_stage(ProgramSyntax::Stage &);
398 void import(const std::string &);
399 void generate(ProgramSyntax::Stage &);
400 bool optimize(ProgramSyntax::Stage &);
401 void finalize(ProgramSyntax::Stage &);
402 static void inject_block(ProgramSyntax::Block &, const ProgramSyntax::Block &);
404 static typename T::ResultType apply(ProgramSyntax::Stage &);
405 template<typename T, typename A>
406 static typename T::ResultType apply(ProgramSyntax::Stage &, const A &);