]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/debug.cpp
Fix a misformatted branch in GLSL compiler debug output
[libs/gl.git] / source / glsl / debug.cpp
1 #include <msp/stringcodec/utf8.h>
2 #include <msp/strings/format.h>
3 #include "debug.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9 namespace SL {
10
11 const std::string &DumpTree::apply(Stage &stage)
12 {
13         formatted = format("Stage: %s\n", Stage::get_stage_name(stage.type));
14         tree.push_back(BRANCH);
15         append(format("Version: %d.%02d", stage.required_features.glsl_version.major, stage.required_features.glsl_version.minor));
16
17         for(std::map<string, TypeDeclaration *>::const_iterator i=stage.types.begin(); i!=stage.types.end(); ++i)
18                 append(format("Type: %%%d %s", get_label(*i->second), i->first));
19
20         set<InterfaceBlock *> seen_interfaces;
21         for(std::map<string, InterfaceBlock *>::const_iterator i=stage.interface_blocks.begin(); i!=stage.interface_blocks.end(); ++i)
22                 if(seen_interfaces.insert(i->second).second)
23                 {
24                         string text = format("Interface block: %%%d %s %s", get_label(*i->second), i->second->interface, i->second->block_name);
25                         if(!i->second->instance_name.empty())
26                                 text += format(" %s", i->second->instance_name);
27                         append(text);
28                 }
29
30         for(std::map<string, FunctionDeclaration *>::const_iterator i=stage.functions.begin(); i!=stage.functions.end(); ++i)
31                 append(format("Function: %%%d %s", get_label(*i->second), i->first));
32
33         last_branch();
34         stage.content.visit(*this);
35         return formatted;
36 }
37
38 void DumpTree::append(const string &line)
39 {
40         StringCodec::Utf8::Encoder enc;
41         for(vector<TreeChars>::const_iterator i=tree.begin(); i!=tree.end(); )
42         {
43                 enc.encode_char(*i++, formatted);
44                 enc.encode_char((i==tree.end() ? REACH : EMPTY), formatted);
45         }
46         formatted += line;
47         formatted += '\n';
48 }
49
50 void DumpTree::append(const Node &node, const string &line)
51 {
52         append(format("<%d:%d> %s", node.source, node.line, line));
53 }
54
55 void DumpTree::append_subtree(const vector<Branch> &branches)
56 {
57         begin_sub();
58         for(vector<Branch>::const_iterator i=branches.begin(); i!=branches.end(); )
59         {
60                 vector<Branch>::const_iterator j = increment(i, branches);
61                 if(!j->text.empty())
62                 {
63                         append(j->text);
64                         if(j->node)
65                         {
66                                 begin_sub();
67                                 last_branch();
68                                 j->node->visit(*this);
69                                 end_sub();
70                         }
71                 }
72                 else
73                         j->node->visit(*this);
74         }
75         end_sub();
76 }
77
78 void DumpTree::append_subtree(Node &node)
79 {
80         begin_sub();
81         last_branch();
82         node.visit(*this);
83         end_sub();
84 }
85
86 void DumpTree::begin_sub()
87 {
88         tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
89         tree.push_back(BRANCH);
90 }
91
92 void DumpTree::last_branch()
93 {
94         tree.back() = BRANCH_LAST;
95 }
96
97 void DumpTree::end_sub()
98 {
99         tree.pop_back();
100         if(tree.back()==STRAIGHT)
101                 tree.back() = BRANCH;
102 }
103
104 unsigned DumpTree::get_label(const Node &node)
105 {
106         unsigned &label = node_labels[&node];
107         if(!label)
108                 label = node_labels.size();
109         return label;
110 }
111
112 string DumpTree::format_type(TypeDeclaration *type)
113 {
114         return (type ? type->name : "?");
115 }
116
117 template<typename T>
118 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
119 {
120         typename T::const_iterator ret = iter++;
121         if(iter==container.end())
122                 last_branch();
123         return ret;
124 }
125
126 void DumpTree::visit(Block &block)
127 {
128         append(block, format("Block %s", (block.use_braces ? "{}" : "(inline)")));
129         begin_sub();
130
131         for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
132                 append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
133
134         for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
135         {
136                 NodeList<Statement>::const_iterator j = increment(i, block.body);
137                 (*j)->visit(*this);
138         }
139
140         end_sub();
141 }
142
143 void DumpTree::visit(Literal &literal)
144 {
145         append(literal, format("Literal: %s -> %s", literal.token, format_type(literal.type)));
146 }
147
148 void DumpTree::visit(VariableReference &var)
149 {
150         string text;
151         if(var.declaration)
152                 text += format("%%%d ", get_label(*var.declaration));
153         text += format("%s (var) -> %s", var.name, format_type(var.type));
154         append(var, text);
155 }
156
157 void DumpTree::visit(InterfaceBlockReference &iface)
158 {
159         string text;
160         if(iface.declaration)
161                 text += format("%%%d ", get_label(*iface.declaration));
162         text += format("%s (iface) -> %s", iface.name, format_type(iface.type));
163         append(iface, text);
164 }
165
166 void DumpTree::visit(MemberAccess &memacc)
167 {
168         string text = "Member access:";
169         if(memacc.declaration)
170                 text += format(" %%%d", get_label(*memacc.declaration));
171         text += format(" .%s -> %s", memacc.member, format_type(memacc.type));
172         append(memacc, text);
173         append_subtree(*memacc.left);
174 }
175
176 void DumpTree::visit(Swizzle &swizzle)
177 {
178         static const char components[4] = { 'x', 'y', 'z', 'w' };
179         string text = "Swizzle: .";
180         for(unsigned i=0; i<swizzle.count; ++i)
181                 text += components[swizzle.components[i]];
182         text += format(" -> %s", format_type(swizzle.type));
183         append(swizzle, text);
184         append_subtree(*swizzle.left);
185 }
186
187 void DumpTree::visit(UnaryExpression &unary)
188 {
189         string text = format("Unary: %s, %sfix -> %s", unary.oper->token, (unary.oper->type==Operator::PREFIX ? "pre" : "post"), format_type(unary.type));
190         append(unary, text);
191         append_subtree(*unary.expression);
192 }
193
194 void DumpTree::visit(BinaryExpression &binary)
195 {
196         append(binary, format("Binary: %s%s -> %s", binary.oper->token, binary.oper->token2, format_type(binary.type)));
197         begin_sub();
198         binary.left->visit(*this);
199         last_branch();
200         binary.right->visit(*this);
201         end_sub();
202 }
203
204 void DumpTree::visit(Assignment &assign)
205 {
206         append(assign, format("Assignment: %s%s -> %s", assign.oper->token, (assign.self_referencing ? " (self-referencing)" : ""), format_type(assign.type)));
207         begin_sub();
208         if(assign.target.declaration)
209         {
210                 string text = format("Target: %%%d", get_label(*assign.target.declaration));
211
212                 static const char swizzle[4] = { 'x', 'y', 'z', 'w' };
213                 for(unsigned i=0; i<assign.target.chain_len; ++i)
214                 {
215                         unsigned component = assign.target.chain[i];
216                         switch(static_cast<Assignment::Target::ChainType>(component&0xC0))
217                         {
218                         case Assignment::Target::MEMBER:
219                                 text += format(" .%d", component&0x3F);
220                                 break;
221                         case Assignment::Target::SWIZZLE:
222                                 text += " .";
223                                 for(unsigned j=0; j<4; ++j)
224                                         if(component&(1<<j))
225                                                 text += swizzle[j];
226                                 break;
227                         case Assignment::Target::ARRAY:
228                                 text += format(" [%d]", component&0x3F);
229                                 break;
230                         }
231                 }
232                 append(text);
233         }
234         assign.left->visit(*this);
235         last_branch();
236         assign.right->visit(*this);
237         end_sub();
238 }
239
240 void DumpTree::visit(TernaryExpression &ternary)
241 {
242         append(ternary, format("Ternary: %s%s -> %s", ternary.oper->token, ternary.oper->token2, format_type(ternary.type)));
243         begin_sub();
244         ternary.condition->visit(*this);
245         ternary.true_expr->visit(*this);
246         last_branch();
247         ternary.false_expr->visit(*this);
248         end_sub();
249 }
250
251 void DumpTree::visit(FunctionCall &call)
252 {
253         string head = "Function call: ";
254         if(call.declaration)
255                 head += format("%%%d ", get_label(*call.declaration));
256         head += call.name;
257         if(call.constructor)
258                 head += " (constructor)";
259         head += format(" -> %s", format_type(call.type));
260         append(call, head);
261
262         begin_sub();
263         for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
264         {
265                 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
266                 (*j)->visit(*this);
267         }
268         end_sub();
269 }
270
271 void DumpTree::visit(ExpressionStatement &expr)
272 {
273         append(expr, "expr;");
274         append_subtree(*expr.expression);
275 }
276
277 void DumpTree::visit(Import &import)
278 {
279         append(import, format("import %s", import.module));
280 }
281
282 void DumpTree::visit(Precision &prec)
283 {
284         append(prec, format("precision %s %s", prec.precision, prec.type));
285 }
286
287 void DumpTree::visit(Layout &layout)
288 {
289         append(layout, "Layout");
290         begin_sub();
291         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
292         {
293                 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
294                 string qualifier = j->name;
295                 if(j->has_value)
296                         qualifier += format("=%d", j->value);
297                 append(qualifier);
298         }
299         end_sub();
300 }
301
302 void DumpTree::visit(InterfaceLayout &layout)
303 {
304         append(layout, format("Layout: %s", layout.interface));
305         append_subtree(layout.layout);
306 }
307
308 void DumpTree::visit(BasicTypeDeclaration &type)
309 {
310         append(type, format("%%%d typedef %s", get_label(type), type.name));
311
312         vector<Branch> branches;
313         if(type.base_type)
314                 branches.push_back(format("%s: %%%d %s", (type.kind==BasicTypeDeclaration::ALIAS ? "Alias of" : "Base"), get_label(*type.base_type), type.base_type->name));
315         if(type.kind==BasicTypeDeclaration::VECTOR)
316                 branches.push_back(format("Vector: %d", type.size));
317         else if(type.kind==BasicTypeDeclaration::MATRIX)
318                 branches.push_back(format("Matrix: %dx%d", type.size&0xFFFF, type.size>>16));
319         append_subtree(branches);
320 }
321
322 void DumpTree::visit(ImageTypeDeclaration &type)
323 {
324         static const char *dims[] = { "1D", "2D", "3D", "Cube" };
325
326         append(type, format("%%%d typedef %s", get_label(type), type.name));
327
328         vector<Branch> branches;
329         branches.push_back(format("Dimensions: %s%s", dims[type.dimensions-1], (type.array ? " array" : "")));
330         if(type.base_type)
331                 branches.push_back(format("Element type: %%%d %s", get_label(*type.base_type), type.base_type->name));
332         if(type.shadow)
333                 branches.push_back("Shadow");
334         append_subtree(branches);
335 }
336
337 void DumpTree::visit(StructDeclaration &strct)
338 {
339         append(strct, format("%%%d struct %s", get_label(strct), strct.name));
340         append_subtree(strct.members);
341 }
342
343 void DumpTree::visit(VariableDeclaration &var)
344 {
345         string decl = format("%%%d ", get_label(var));
346         if(var.constant)
347                 decl += "const ";
348         if(!var.interpolation.empty())
349                 decl += format("%s ", var.interpolation);
350         if(!var.sampling.empty())
351                 decl += format("%s ", var.sampling);
352         if(!var.interface.empty())
353                 decl += format("%s ", var.interface);
354         if(!var.precision.empty())
355                 decl += format("%s ", var.precision);
356         decl += format("%s %s", var.type, var.name);
357         if(var.source==BUILTIN_SOURCE)
358                 decl += " (builtin)";
359         else if(var.linked_declaration)
360                 decl += " (linked)";
361         append(var, decl);
362
363         vector<Branch> branches;
364         if(var.type_declaration)
365                 branches.push_back(format("Type: %%%d %s", get_label(*var.type_declaration), var.type_declaration->name));
366         if(var.layout)
367                 branches.push_back(var.layout.get());
368         if(var.array)
369         {
370                 if(var.array_size)
371                         branches.push_back(Branch("Array []", var.array_size.get()));
372                 else
373                         branches.push_back("Array []");
374         }
375         if(var.init_expression)
376                 branches.push_back(var.init_expression.get());
377         append_subtree(branches);
378 }
379
380 void DumpTree::visit(InterfaceBlock &iface)
381 {
382         string head;
383         if(!iface.instance_name.empty())
384                 head += format("%%%d ", get_label(iface));
385         head += format("%s %s", iface.interface, iface.block_name);
386         if(!iface.instance_name.empty())
387                 head += format(" %s", iface.instance_name);
388         if(iface.array)
389                 head += "[]";
390         if(iface.source==BUILTIN_SOURCE)
391                 head += " (builtin)";
392         else if(iface.linked_block)
393                 head += " (linked)";
394         append(iface, head);
395
396         vector<Branch> branches;
397         if(iface.type_declaration)
398                 branches.push_back(format("Type: %%%d %s", get_label(*iface.type_declaration), iface.type_declaration->name));
399         if(iface.layout)
400                 branches.push_back(iface.layout.get());
401         if(iface.members)
402                 branches.push_back(iface.members.get());
403         append_subtree(branches);
404 }
405
406 void DumpTree::visit(FunctionDeclaration &func)
407 {
408         string text = format("%%%d %s %s%s", get_label(func), func.return_type, func.name, (func.signature.empty() ? "(?)" : func.signature));
409         if(func.source==BUILTIN_SOURCE)
410                 text += " (builtin)";
411         else if(!func.definition)
412                 text += " (undefined)";
413         append(func, text);
414
415         begin_sub();
416         if(func.return_type_declaration)
417                 append(format("Return type: %%%d %s", get_label(*func.return_type_declaration), func.return_type_declaration->name));
418         for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
419                 (*i)->visit(*this);
420         last_branch();
421         if(func.definition==&func)
422                 func.body.visit(*this);
423         else if(func.definition)
424                 append(format("Definition: %%%d", get_label(*func.definition)));
425         end_sub();
426 }
427
428 void DumpTree::visit(Conditional &cond)
429 {
430         append(cond, "if()");
431
432         vector<Branch> branches;
433         branches.push_back(cond.condition.get());
434         branches.push_back(Branch("then", &cond.body));
435         if(!cond.else_body.body.empty())
436                 branches.push_back(Branch("else", &cond.else_body));
437         append_subtree(branches);
438 }
439
440 void DumpTree::visit(Iteration &iter)
441 {
442         append(iter, "for()");
443
444         vector<Branch> branches;
445         if(iter.init_statement)
446                 branches.push_back(Branch("Initialization", iter.init_statement.get()));
447         if(iter.condition)
448                 branches.push_back(Branch("Condition", iter.condition.get()));
449         if(iter.loop_expression)
450                 branches.push_back(Branch("Loop", iter.loop_expression.get()));
451         branches.push_back(&iter.body);
452         append_subtree(branches);
453 }
454
455 void DumpTree::visit(Passthrough &pass)
456 {
457         append(pass, (pass.subscript ? "passthrough[]" : "passthrough"));
458         if(pass.subscript)
459                 append_subtree(*pass.subscript);
460 }
461
462 void DumpTree::visit(Return &ret)
463 {
464         append(ret, (ret.expression ? "return" : "return;"));
465         if(ret.expression)
466                 append_subtree(*ret.expression);
467 }
468
469 void DumpTree::visit(Jump &jump)
470 {
471         append(jump, format("%s;", jump.keyword));
472 }
473
474 } // namespace SL
475 } // namespace GL
476 } // namespace Msp