]> git.tdb.fi Git - libs/gl.git/blob - source/programcompiler.h
Add an intermediate node type for statements
[libs/gl.git] / source / programcompiler.h
1 #ifndef MSP_GL_PROGRAMCOMPILER_H_
2 #define MSP_GL_PROGRAMCOMPILER_H_
3
4 #include <set>
5 #include "programparser.h"
6 #include "programsyntax.h"
7
8 namespace Msp {
9 namespace GL {
10
11 class Program;
12 class Resources;
13
14 class ProgramCompiler
15 {
16 private:
17         struct Visitor: ProgramSyntax::TraversingVisitor
18         {
19                 typedef void ResultType;
20
21                 ProgramSyntax::Stage *stage;
22
23                 Visitor();
24
25                 virtual void apply(ProgramSyntax::Stage &);
26                 void get_result() const { }
27         };
28
29         struct BlockModifier: Visitor
30         {
31                 bool remove_node;
32                 std::vector<RefPtr<ProgramSyntax::Statement> > insert_nodes;
33
34                 BlockModifier();
35
36                 void flatten_block(ProgramSyntax::Block &);
37                 void apply_and_increment(ProgramSyntax::Block &, std::list<RefPtr<ProgramSyntax::Statement> >::iterator &);
38                 using Visitor::visit;
39                 virtual void visit(ProgramSyntax::Block &);
40         };
41
42         struct Formatter: Visitor
43         {
44                 typedef std::string ResultType;
45
46                 std::string formatted;
47                 unsigned indent;
48                 bool parameter_list;
49                 unsigned else_if;
50                 std::string block_interface;
51
52                 Formatter();
53
54                 virtual void apply(ProgramSyntax::Stage &);
55                 const std::string &get_result() const { return formatted; }
56                 using Visitor::visit;
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 &);
79         };
80
81         template<typename T>
82         struct NodeGatherer: Visitor
83         {
84                 typedef std::vector<T *> ResultType;
85
86                 std::vector<T *> nodes;
87
88                 const ResultType &get_result() const { return nodes; }
89                 using Visitor::visit;
90                 virtual void visit(T &n) { nodes.push_back(&n); }
91         };
92
93         struct DeclarationCombiner: BlockModifier
94         {
95                 bool toplevel;
96                 std::map<std::string, std::vector<ProgramSyntax::FunctionDeclaration *> > functions;
97                 std::map<std::string, ProgramSyntax::VariableDeclaration *> variables;
98
99                 DeclarationCombiner();
100
101                 using Visitor::visit;
102                 virtual void visit(ProgramSyntax::Block &);
103                 virtual void visit(ProgramSyntax::FunctionDeclaration &);
104                 virtual void visit(ProgramSyntax::VariableDeclaration &);
105         };
106
107         struct VariableResolver: Visitor
108         {
109                 std::vector<ProgramSyntax::Block *> blocks;
110                 ProgramSyntax::StructDeclaration *type;
111                 bool anonymous;
112                 std::string block_interface;
113                 bool record_target;
114                 ProgramSyntax::VariableDeclaration *assignment_target;
115                 bool self_referencing;
116
117                 VariableResolver();
118
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 &);
129         };
130
131         struct FunctionResolver: Visitor
132         {
133                 std::map<std::string, std::vector<ProgramSyntax::FunctionDeclaration *> > functions;
134
135                 using Visitor::visit;
136                 virtual void visit(ProgramSyntax::FunctionCall &);
137                 virtual void visit(ProgramSyntax::FunctionDeclaration &);
138         };
139
140         struct InterfaceGenerator: BlockModifier
141         {
142                 std::string in_prefix;
143                 std::string out_prefix;
144                 unsigned scope_level;
145                 std::map<std::string, RefPtr<ProgramSyntax::VariableDeclaration> > iface_declarations;
146
147                 InterfaceGenerator();
148
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 &);
159         };
160
161         struct DeclarationReorderer: Visitor
162         {
163                 enum DeclarationKind
164                 {
165                         NO_DECLARATION,
166                         LAYOUT,
167                         STRUCT,
168                         VARIABLE,
169                         FUNCTION
170                 };
171
172                 unsigned scope_level;
173                 DeclarationKind kind;
174                 std::set<ProgramSyntax::Node *> ordered_funcs;
175                 std::set<ProgramSyntax::Node *> needed_funcs;
176
177                 DeclarationReorderer();
178
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 &);
187         };
188
189         struct InlineableFunctionLocator: Visitor
190         {
191                 typedef std::set<ProgramSyntax::FunctionDeclaration *> ResultType;
192
193                 std::map<ProgramSyntax::FunctionDeclaration *, unsigned> refcounts;
194                 std::set<ProgramSyntax::FunctionDeclaration *> inlineable;
195                 ProgramSyntax::FunctionDeclaration *in_function;
196
197                 InlineableFunctionLocator();
198
199                 const ResultType &get_result() const { return inlineable; }
200                 using Visitor::visit;
201                 virtual void visit(ProgramSyntax::FunctionCall &);
202                 virtual void visit(ProgramSyntax::FunctionDeclaration &);
203         };
204
205         struct FunctionInliner: Visitor
206         {
207                 std::set<ProgramSyntax::FunctionDeclaration *> inlineable;
208                 unsigned extract_result;
209                 RefPtr<ProgramSyntax::Expression> inline_result;
210
211                 FunctionInliner();
212                 FunctionInliner(const std::set<ProgramSyntax::FunctionDeclaration *> &);
213
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 &);
223         };
224
225         struct ExpressionEvaluator: ProgramSyntax::NodeVisitor
226         {
227                 typedef std::map<ProgramSyntax::VariableDeclaration *, ProgramSyntax::Expression *> ValueMap;
228
229                 const ValueMap *variable_values;
230                 float result;
231                 bool result_valid;
232
233                 ExpressionEvaluator();
234                 ExpressionEvaluator(const ValueMap &);
235
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 &);
242         };
243
244         struct ConstantConditionEliminator: BlockModifier
245         {
246                 unsigned scope_level;
247                 bool record_only;
248                 ExpressionEvaluator::ValueMap variable_values;
249
250                 ConstantConditionEliminator();
251
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 &);
259         };
260
261         struct UnusedVariableLocator: Visitor
262         {
263                 struct VariableInfo
264                 {
265                         bool local;
266                         std::vector<ProgramSyntax::Node *> assignments;
267                         bool conditionally_assigned;
268                         bool referenced;
269
270                         VariableInfo();
271                 };
272
273                 typedef std::set<ProgramSyntax::Node *> ResultType;
274                 typedef std::map<ProgramSyntax::VariableDeclaration *, VariableInfo> BlockVariableMap;
275
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;
283                 bool global_scope;
284
285                 UnusedVariableLocator();
286
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 &);
304         };
305
306         struct UnusedFunctionLocator: Visitor
307         {
308                 typedef std::set<ProgramSyntax::Node *> ResultType;
309
310                 std::set<ProgramSyntax::Node *> unused_nodes;
311                 std::set<ProgramSyntax::FunctionDeclaration *> used_definitions;
312
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 &);
317         };
318
319         struct NodeRemover: Visitor
320         {
321                 std::set<ProgramSyntax::Node *> to_remove;
322
323                 NodeRemover() { }
324                 NodeRemover(const std::set<ProgramSyntax::Node *> &);
325
326                 using Visitor::visit;
327                 virtual void visit(ProgramSyntax::Block &);
328                 virtual void visit(ProgramSyntax::VariableDeclaration &);
329                 virtual void visit(ProgramSyntax::Iteration &);
330         };
331
332         struct PrecisionRemover: BlockModifier
333         {
334                 using Visitor::visit;
335                 virtual void visit(ProgramSyntax::Precision &);
336                 virtual void visit(ProgramSyntax::VariableDeclaration &);
337         };
338
339         struct DefaultPrecisionGenerator: BlockModifier
340         {
341                 bool toplevel;
342                 std::set<std::string> have_default;
343
344                 DefaultPrecisionGenerator();
345
346                 using Visitor::visit;
347                 virtual void visit(ProgramSyntax::Block &);
348                 virtual void visit(ProgramSyntax::Precision &);
349                 virtual void visit(ProgramSyntax::VariableDeclaration &);
350         };
351
352         struct LegacyConverter: BlockModifier
353         {
354                 GLApi target_api;
355                 Version target_version;
356                 std::string type;
357                 ProgramSyntax::VariableDeclaration *frag_out;
358
359                 LegacyConverter();
360                 LegacyConverter(const Version &);
361
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 &);
376         };
377
378         Resources *resources;
379         ProgramSyntax::Module *module;
380         std::set<std::string> imported_names;
381
382 public:
383         ProgramCompiler();
384         ~ProgramCompiler();
385
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 &);
390
391 private:
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 &);
397         void process();
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 &);
403         template<typename T>
404         static typename T::ResultType apply(ProgramSyntax::Stage &);
405         template<typename T, typename A>
406         static typename T::ResultType apply(ProgramSyntax::Stage &, const A &);
407 };
408
409 } // namespace GL
410 } // namespace Msp
411
412 #endif