]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/debug.cpp
Rearrange operator metadata
[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->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_subtree(const vector<Branch> &branches)
51 {
52         begin_sub();
53         for(vector<Branch>::const_iterator i=branches.begin(); i!=branches.end(); )
54         {
55                 vector<Branch>::const_iterator j = increment(i, branches);
56                 if(!j->text.empty())
57                 {
58                         if(j->node)
59                                 annotated_branch(j->text, *j->node);
60                         else
61                                 append(j->text);
62                 }
63                 else
64                         j->node->visit(*this);
65         }
66         end_sub();
67 }
68
69 void DumpTree::begin_sub()
70 {
71         tree.back() = (tree.back()==BRANCH_LAST ? EMPTY : STRAIGHT);
72         tree.push_back(BRANCH);
73 }
74
75 void DumpTree::last_branch()
76 {
77         tree.back() = BRANCH_LAST;
78 }
79
80 void DumpTree::end_sub()
81 {
82         tree.pop_back();
83         if(tree.back()==STRAIGHT)
84                 tree.back() = BRANCH;
85 }
86
87 void DumpTree::annotated_branch(const string &annotation, Node &node)
88 {
89         append(annotation);
90         begin_sub();
91         last_branch();
92         node.visit(*this);
93         end_sub();
94 }
95
96 unsigned DumpTree::get_label(const Node &node)
97 {
98         unsigned &label = node_labels[&node];
99         if(!label)
100                 label = node_labels.size();
101         return label;
102 }
103
104 string DumpTree::format_type(TypeDeclaration *type)
105 {
106         return (type ? type->name : "?");
107 }
108
109 template<typename T>
110 typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
111 {
112         typename T::const_iterator ret = iter++;
113         if(iter==container.end())
114                 last_branch();
115         return ret;
116 }
117
118 void DumpTree::visit(Block &block)
119 {
120         append(format("Block %s", (block.use_braces ? "{}" : "(inline)")));
121         begin_sub();
122
123         for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
124                 append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
125
126         bool labeled_body = !block.variables.empty();
127         if(labeled_body)
128         {
129                 last_branch();
130                 append("Body");
131                 begin_sub();
132         }
133         for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
134         {
135                 NodeList<Statement>::const_iterator j = increment(i, block.body);
136                 (*j)->visit(*this);
137         }
138         if(labeled_body)
139                 end_sub();
140
141         end_sub();
142 }
143
144 void DumpTree::visit(Literal &literal)
145 {
146         append(format("Literal: %s -> %s", literal.token, format_type(literal.type)));
147 }
148
149 void DumpTree::visit(ParenthesizedExpression &parexpr)
150 {
151         annotated_branch(format("(expr) -> %s", format_type(parexpr.type)), *parexpr.expression);
152 }
153
154 void DumpTree::visit(VariableReference &var)
155 {
156         string text;
157         if(var.declaration)
158                 text += format("%%%d ", get_label(*var.declaration));
159         text += format("%s (var) -> %s", var.name, format_type(var.type));
160         append(text);
161 }
162
163 void DumpTree::visit(InterfaceBlockReference &iface)
164 {
165         string text;
166         if(iface.declaration)
167                 text += format("%%%d ", get_label(*iface.declaration));
168         text += format("%s (iface) -> %s", iface.name, format_type(iface.type));
169         append(text);
170 }
171
172 void DumpTree::visit(MemberAccess &memacc)
173 {
174         string text = "Member access:";
175         if(memacc.declaration)
176                 text += format(" %%%d", get_label(*memacc.declaration));
177         text += format(" .%s -> %s", memacc.member, format_type(memacc.type));
178         annotated_branch(text, *memacc.left);
179 }
180
181 void DumpTree::visit(Swizzle &swizzle)
182 {
183         static const char components[4] = { 'x', 'y', 'z', 'w' };
184         string text = "Swizzle: .";
185         for(unsigned i=0; i<swizzle.count; ++i)
186                 text += components[swizzle.components[i]];
187         text += format(" -> %s", format_type(swizzle.type));
188         annotated_branch(text, *swizzle.left);
189 }
190
191 void DumpTree::visit(UnaryExpression &unary)
192 {
193         string text = format("Unary: %s, %sfix -> %s", unary.oper->token, (unary.oper->type==Operator::PREFIX ? "pre" : "post"), format_type(unary.type));
194         annotated_branch(text, *unary.expression);
195 }
196
197 void DumpTree::visit(BinaryExpression &binary)
198 {
199         append(format("Binary: %s%s -> %s", binary.oper->token, binary.oper->token2, format_type(binary.type)));
200         begin_sub();
201         binary.left->visit(*this);
202         last_branch();
203         binary.right->visit(*this);
204         end_sub();
205 }
206
207 void DumpTree::visit(Assignment &assign)
208 {
209         append(format("Assignment: %s%s -> %s", assign.oper->token, (assign.self_referencing ? " (self-referencing)" : ""), format_type(assign.type)));
210         begin_sub();
211         if(assign.target.declaration)
212         {
213                 string text = format("Target: %%%d", get_label(*assign.target.declaration));
214
215                 static const char swizzle[4] = { 'x', 'y', 'z', 'w' };
216                 for(unsigned i=0; i<assign.target.chain_len; ++i)
217                 {
218                         unsigned component = assign.target.chain[i];
219                         switch(static_cast<Assignment::Target::ChainType>(component&0xC0))
220                         {
221                         case Assignment::Target::MEMBER:
222                                 text += format(" .%d", component&0x3F);
223                                 break;
224                         case Assignment::Target::SWIZZLE:
225                                 text += " .";
226                                 for(unsigned j=0; j<4; ++j)
227                                         if(component&(1<<j))
228                                                 text += swizzle[j];
229                                 break;
230                         case Assignment::Target::ARRAY:
231                                 text += format(" [%d]", component&0x3F);
232                                 break;
233                         }
234                 }
235                 append(text);
236         }
237         assign.left->visit(*this);
238         last_branch();
239         assign.right->visit(*this);
240         end_sub();
241 }
242
243 void DumpTree::visit(TernaryExpression &ternary)
244 {
245         append(format("Ternary: %s%s -> %s", ternary.oper->token, ternary.oper->token2, format_type(ternary.type)));
246         begin_sub();
247         ternary.condition->visit(*this);
248         ternary.true_expr->visit(*this);
249         last_branch();
250         ternary.false_expr->visit(*this);
251         end_sub();
252 }
253
254 void DumpTree::visit(FunctionCall &call)
255 {
256         string head = "Function call: ";
257         if(call.declaration)
258                 head += format("%%%d ", get_label(*call.declaration));
259         head += call.name;
260         if(call.constructor)
261                 head += " (constructor)";
262         head += format(" -> %s", format_type(call.type));
263         append(head);
264
265         begin_sub();
266         for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
267         {
268                 NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
269                 (*j)->visit(*this);
270         }
271         end_sub();
272 }
273
274 void DumpTree::visit(ExpressionStatement &expr)
275 {
276         annotated_branch("expr;", *expr.expression);
277 }
278
279 void DumpTree::visit(Import &import)
280 {
281         append(format("import %s", import.module));
282 }
283
284 void DumpTree::visit(Precision &prec)
285 {
286         append(format("precision %s %s", prec.precision, prec.type));
287 }
288
289 void DumpTree::visit(Layout &layout)
290 {
291         append("Layout");
292         begin_sub();
293         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
294         {
295                 vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
296                 string qualifier = j->name;
297                 if(j->has_value)
298                         qualifier += format("=%d", j->value);
299                 append(qualifier);
300         }
301         end_sub();
302 }
303
304 void DumpTree::visit(InterfaceLayout &layout)
305 {
306         annotated_branch(format("Layout: %s", layout.interface), layout.layout);
307 }
308
309 void DumpTree::visit(BasicTypeDeclaration &type)
310 {
311         append(format("%%%d typedef %s", get_label(type), type.name));
312
313         vector<Branch> branches;
314         if(type.base_type)
315                 branches.push_back(format("%s: %%%d %s", (type.kind==BasicTypeDeclaration::ALIAS ? "Alias of" : "Base"), get_label(*type.base_type), type.base_type->name));
316         if(type.kind==BasicTypeDeclaration::VECTOR)
317                 branches.push_back(format("Vector: %d", type.size));
318         else if(type.kind==BasicTypeDeclaration::MATRIX)
319                 branches.push_back(format("Matrix: %dx%d", type.size&0xFFFF, type.size>>16));
320         append_subtree(branches);
321 }
322
323 void DumpTree::visit(ImageTypeDeclaration &type)
324 {
325         static const char *dims[] = { "1D", "2D", "3D", "Cube" };
326
327         append(format("%%%d typedef %s", get_label(type), type.name));
328
329         vector<Branch> branches;
330         branches.push_back(format("Dimensions: %s%s", dims[type.dimensions-1], (type.array ? " array" : "")));
331         if(type.base_type)
332                 branches.push_back(format("Element type: %%%d %s", get_label(*type.base_type), type.base_type->name));
333         if(type.shadow)
334                 branches.push_back("Shadow");
335         append_subtree(branches);
336 }
337
338 void DumpTree::visit(StructDeclaration &strct)
339 {
340         annotated_branch(format("%%%d struct %s", get_label(strct), strct.name), 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(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.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(head);
395
396         begin_sub();
397         last_branch();
398         if(iface.type_declaration)
399                 append(format("Type: %%%d %s", get_label(*iface.type_declaration), iface.type_declaration->name));
400         else if(iface.members)
401                 iface.members->visit(*this);
402         end_sub();
403 }
404
405 void DumpTree::visit(FunctionDeclaration &func)
406 {
407         string text = format("%%%d %s %s%s", get_label(func), func.return_type, func.name, (func.signature.empty() ? "(?)" : func.signature));
408         if(func.source==BUILTIN_SOURCE)
409                 text += " (builtin)";
410         else if(!func.definition)
411                 text += " (undefined)";
412         append(text);
413
414         begin_sub();
415         if(func.return_type_declaration)
416                 append(format("Return type: %%%d %s", get_label(*func.return_type_declaration), func.return_type_declaration->name));
417         for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
418                 (*i)->visit(*this);
419         last_branch();
420         if(func.definition==&func)
421                 func.body.visit(*this);
422         else if(func.definition)
423                 append(format("Definition: %%%d", get_label(*func.definition)));
424         end_sub();
425 }
426
427 void DumpTree::visit(Conditional &cond)
428 {
429         append("if()");
430
431         vector<Branch> branches;
432         branches.push_back(cond.condition.get());
433         branches.push_back(&cond.body);
434         if(!cond.else_body.body.empty())
435                 branches.push_back(&cond.else_body);
436         append_subtree(branches);
437 }
438
439 void DumpTree::visit(Iteration &iter)
440 {
441         append("for()");
442
443         begin_sub();
444         if(iter.init_statement)
445                 annotated_branch("Initialization", *iter.init_statement);
446         if(iter.condition)
447                 annotated_branch("Condition", *iter.condition);
448         if(iter.loop_expression)
449                 annotated_branch("Loop", *iter.loop_expression);
450         last_branch();
451         annotated_branch("Body", iter.body);
452         end_sub();
453 }
454
455 void DumpTree::visit(Passthrough &pass)
456 {
457         if(pass.subscript)
458                 annotated_branch("passthrough[]", *pass.subscript);
459         else
460                 append("passthrough;");
461 }
462
463 void DumpTree::visit(Return &ret)
464 {
465         if(ret.expression)
466                 annotated_branch("return", *ret.expression);
467         else
468                 append("return;");
469 }
470
471 void DumpTree::visit(Jump &jump)
472 {
473         append(format("%s;", jump.keyword));
474 }
475
476 } // namespace SL
477 } // namespace GL
478 } // namespace Msp