3 * Dump/print a slang_operation tree
7 #include "main/imports.h"
8 #include "slang_compile.h"
9 #include "slang_print.h"
21 print_type(const slang_fully_specified_type
*t
)
23 switch (t
->qualifier
) {
27 case SLANG_QUAL_CONST
:
30 case SLANG_QUAL_ATTRIBUTE
:
33 case SLANG_QUAL_VARYING
:
36 case SLANG_QUAL_UNIFORM
:
42 case SLANG_QUAL_INOUT
:
45 case SLANG_QUAL_FIXEDOUTPUT
:
46 printf("fixedoutput");
48 case SLANG_QUAL_FIXEDINPUT
:
52 printf("unknown qualifer!");
55 switch (t
->specifier
.type
) {
62 case SLANG_SPEC_BVEC2
:
65 case SLANG_SPEC_BVEC3
:
68 case SLANG_SPEC_BVEC4
:
74 case SLANG_SPEC_IVEC2
:
77 case SLANG_SPEC_IVEC3
:
80 case SLANG_SPEC_IVEC4
:
83 case SLANG_SPEC_FLOAT
:
101 case SLANG_SPEC_MAT4
:
104 case SLANG_SPEC_MAT23
:
107 case SLANG_SPEC_MAT32
:
110 case SLANG_SPEC_MAT24
:
113 case SLANG_SPEC_MAT42
:
116 case SLANG_SPEC_MAT34
:
119 case SLANG_SPEC_MAT43
:
122 case SLANG_SPEC_SAMPLER1D
:
125 case SLANG_SPEC_SAMPLER2D
:
128 case SLANG_SPEC_SAMPLER3D
:
131 case SLANG_SPEC_SAMPLERCUBE
:
132 printf("samplerCube");
134 case SLANG_SPEC_SAMPLER1DSHADOW
:
135 printf("sampler1DShadow");
137 case SLANG_SPEC_SAMPLER2DSHADOW
:
138 printf("sampler2DShadow");
140 case SLANG_SPEC_STRUCT
:
143 case SLANG_SPEC_ARRAY
:
147 printf("unknown type");
154 print_variable(const slang_variable
*v
, int indent
)
158 print_type(&v
->type
);
159 printf(" %s (at %p)", (char *) v
->a_name
, (void *) v
);
160 if (v
->initializer
) {
162 slang_print_tree(v
->initializer
, indent
+ 3);
171 print_binary(const slang_operation
*op
, const char *oper
, int indent
)
173 assert(op
->num_children
== 2);
175 printf("binary at %p locals=%p outer=%p\n",
178 (void *) op
->locals
->outer_scope
);
180 slang_print_tree(&op
->children
[0], indent
+ 3);
182 printf("%s at %p locals=%p outer=%p\n",
183 oper
, (void *) op
, (void *) op
->locals
,
184 (void *) op
->locals
->outer_scope
);
185 slang_print_tree(&op
->children
[1], indent
+ 3);
190 print_generic2(const slang_operation
*op
, const char *oper
,
191 const char *s
, int indent
)
196 printf("%s %s at %p locals=%p outer=%p\n",
197 oper
, s
, (void *) op
, (void *) op
->locals
,
198 (void *) op
->locals
->outer_scope
);
200 for (i
= 0; i
< op
->num_children
; i
++) {
202 printf("//child %u of %u:\n", i
, op
->num_children
);
203 slang_print_tree(&op
->children
[i
], indent
);
208 print_generic(const slang_operation
*op
, const char *oper
, int indent
)
210 print_generic2(op
, oper
, "", indent
);
214 static const slang_variable_scope
*
215 find_scope(const slang_variable_scope
*s
, slang_atom name
)
218 for (i
= 0; i
< s
->num_variables
; i
++) {
219 if (s
->variables
[i
]->a_name
== name
)
223 return find_scope(s
->outer_scope
, name
);
228 static const slang_variable
*
229 find_var(const slang_variable_scope
*s
, slang_atom name
)
232 for (i
= 0; i
< s
->num_variables
; i
++) {
233 if (s
->variables
[i
]->a_name
== name
)
234 return s
->variables
[i
];
237 return find_var(s
->outer_scope
, name
);
244 slang_print_tree(const slang_operation
*op
, int indent
)
250 case SLANG_OPER_NONE
:
252 printf("SLANG_OPER_NONE\n");
255 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
257 printf("{ locals=%p outer=%p\n", (void*)op
->locals
, (void*)op
->locals
->outer_scope
);
258 print_generic(op
, NULL
, indent
+3);
263 case SLANG_OPER_BLOCK_NEW_SCOPE
:
265 printf("{{ // new scope locals=%p outer=%p: ",
267 (void *) op
->locals
->outer_scope
);
268 for (i
= 0; i
< op
->locals
->num_variables
; i
++) {
269 printf("%s ", (char *) op
->locals
->variables
[i
]->a_name
);
272 print_generic(op
, NULL
, indent
+3);
277 case SLANG_OPER_VARIABLE_DECL
:
278 assert(op
->num_children
== 0 || op
->num_children
== 1);
281 v
= _slang_locate_variable(op
->locals
, op
->a_id
, GL_TRUE
);
283 const slang_variable_scope
*scope
;
285 printf("DECL (locals=%p outer=%p) ", (void*)op
->locals
, (void*) op
->locals
->outer_scope
);
286 print_type(&v
->type
);
287 printf(" %s (%p)", (char *) op
->a_id
,
288 (void *) find_var(op
->locals
, op
->a_id
));
290 scope
= find_scope(op
->locals
, op
->a_id
);
291 printf(" (in scope %p) ", (void *) scope
);
293 if (op
->num_children
== 1) {
295 slang_print_tree(&op
->children
[0], indent
+ 3);
297 else if (v
->initializer
) {
298 printf(" := INITIALIZER\n");
299 slang_print_tree(v
->initializer
, indent
+ 3);
307 print_type(&v->type);
309 printf("ADDR: %d size: %d\n", v->address, v->size);
314 printf("DECL %s (anonymous variable!!!!)\n", (char *) op
->a_id
);
321 printf("ASM: %s at %p locals=%p outer=%p\n",
325 (void *) op
->locals
->outer_scope
);
326 print_generic(op
, "ASM", indent
+3);
329 case SLANG_OPER_BREAK
:
334 case SLANG_OPER_CONTINUE
:
336 printf("CONTINUE\n");
339 case SLANG_OPER_DISCARD
:
344 case SLANG_OPER_RETURN
:
347 if (op
->num_children
> 0)
348 slang_print_tree(&op
->children
[0], indent
+ 3);
351 case SLANG_OPER_LABEL
:
353 printf("LABEL %s\n", (char *) op
->a_id
);
356 case SLANG_OPER_EXPRESSION
:
358 printf("EXPR: locals=%p outer=%p\n",
360 (void *) op
->locals
->outer_scope
);
361 /*print_generic(op, "SLANG_OPER_EXPRESSION", indent);*/
362 slang_print_tree(&op
->children
[0], indent
+ 3);
368 slang_print_tree(&op
->children
[0], indent
+ 3);
371 slang_print_tree(&op
->children
[1], indent
+ 3);
374 slang_print_tree(&op
->children
[2], indent
+ 3);
379 case SLANG_OPER_WHILE
:
380 assert(op
->num_children
== 2);
382 printf("WHILE LOOP: locals = %p\n", (void *) op
->locals
);
385 printf("WHILE cond:\n");
386 slang_print_tree(&op
->children
[0], indent
+ 3);
388 printf("WHILE body:\n");
389 slang_print_tree(&op
->children
[1], indent
+ 3);
392 printf("END WHILE LOOP\n");
397 printf("DO LOOP: locals = %p\n", (void *) op
->locals
);
400 printf("DO body:\n");
401 slang_print_tree(&op
->children
[0], indent
+ 3);
403 printf("DO cond:\n");
404 slang_print_tree(&op
->children
[1], indent
+ 3);
407 printf("END DO LOOP\n");
412 printf("FOR LOOP: locals = %p\n", (void *) op
->locals
);
415 printf("FOR init:\n");
416 slang_print_tree(&op
->children
[0], indent
+ 3);
418 printf("FOR condition:\n");
419 slang_print_tree(&op
->children
[1], indent
+ 3);
421 printf("FOR step:\n");
422 slang_print_tree(&op
->children
[2], indent
+ 3);
424 printf("FOR body:\n");
425 slang_print_tree(&op
->children
[3], indent
+ 3);
430 print_generic(op, "FOR", indent + 3);
434 case SLANG_OPER_VOID
:
436 printf("(oper-void)\n");
439 case SLANG_OPER_LITERAL_BOOL
:
442 for (i
= 0; i
< op
->literal_size
; i
++)
443 printf("%s ", op
->literal
[0] ? "TRUE" : "FALSE");
448 case SLANG_OPER_LITERAL_INT
:
451 for (i
= 0; i
< op
->literal_size
; i
++)
452 printf("%d ", (int) op
->literal
[i
]);
456 case SLANG_OPER_LITERAL_FLOAT
:
459 for (i
= 0; i
< op
->literal_size
; i
++)
460 printf("%f ", op
->literal
[i
]);
464 case SLANG_OPER_IDENTIFIER
:
466 const slang_variable_scope
*scope
;
468 if (op
->var
&& op
->var
->a_name
) {
469 scope
= find_scope(op
->locals
, op
->var
->a_name
);
470 printf("VAR %s (in scope %p)\n", (char *) op
->var
->a_name
,
475 scope
= find_scope(op
->locals
, op
->a_id
);
476 printf("VAR' %s (in scope %p) locals=%p outer=%p\n",
480 (void *) op
->locals
->outer_scope
);
486 case SLANG_OPER_SEQUENCE
:
487 print_generic(op
, "COMMA-SEQ", indent
+3);
490 case SLANG_OPER_ASSIGN
:
492 printf("ASSIGNMENT locals=%p outer=%p\n",
494 (void *) op
->locals
->outer_scope
);
495 print_binary(op
, ":=", indent
);
498 case SLANG_OPER_ADDASSIGN
:
501 print_binary(op
, "+=", indent
);
504 case SLANG_OPER_SUBASSIGN
:
507 print_binary(op
, "-=", indent
);
510 case SLANG_OPER_MULASSIGN
:
513 print_binary(op
, "*=", indent
);
516 case SLANG_OPER_DIVASSIGN
:
519 print_binary(op
, "/=", indent
);
522 /*SLANG_OPER_MODASSIGN,*/
523 /*SLANG_OPER_LSHASSIGN,*/
524 /*SLANG_OPER_RSHASSIGN,*/
525 /*SLANG_OPER_ORASSIGN,*/
526 /*SLANG_OPER_XORASSIGN,*/
527 /*SLANG_OPER_ANDASSIGN,*/
528 case SLANG_OPER_SELECT
:
530 printf("SLANG_OPER_SELECT n=%d\n", op
->num_children
);
531 assert(op
->num_children
== 3);
532 slang_print_tree(&op
->children
[0], indent
+3);
535 slang_print_tree(&op
->children
[1], indent
+3);
538 slang_print_tree(&op
->children
[2], indent
+3);
541 case SLANG_OPER_LOGICALOR
:
542 print_binary(op
, "||", indent
);
545 case SLANG_OPER_LOGICALXOR
:
546 print_binary(op
, "^^", indent
);
549 case SLANG_OPER_LOGICALAND
:
550 print_binary(op
, "&&", indent
);
554 /*SLANG_OPER_BITXOR*/
555 /*SLANG_OPER_BITAND*/
556 case SLANG_OPER_EQUAL
:
557 print_binary(op
, "==", indent
);
560 case SLANG_OPER_NOTEQUAL
:
561 print_binary(op
, "!=", indent
);
564 case SLANG_OPER_LESS
:
565 print_binary(op
, "<", indent
);
568 case SLANG_OPER_GREATER
:
569 print_binary(op
, ">", indent
);
572 case SLANG_OPER_LESSEQUAL
:
573 print_binary(op
, "<=", indent
);
576 case SLANG_OPER_GREATEREQUAL
:
577 print_binary(op
, ">=", indent
);
580 /*SLANG_OPER_LSHIFT*/
581 /*SLANG_OPER_RSHIFT*/
583 print_binary(op
, "+", indent
);
586 case SLANG_OPER_SUBTRACT
:
587 print_binary(op
, "-", indent
);
590 case SLANG_OPER_MULTIPLY
:
591 print_binary(op
, "*", indent
);
594 case SLANG_OPER_DIVIDE
:
595 print_binary(op
, "/", indent
);
598 /*SLANG_OPER_MODULUS*/
599 case SLANG_OPER_PREINCREMENT
:
602 slang_print_tree(&op
->children
[0], indent
+3);
605 case SLANG_OPER_PREDECREMENT
:
608 slang_print_tree(&op
->children
[0], indent
+3);
611 case SLANG_OPER_PLUS
:
613 printf("SLANG_OPER_PLUS\n");
616 case SLANG_OPER_MINUS
:
618 printf("SLANG_OPER_MINUS\n");
621 /*SLANG_OPER_COMPLEMENT*/
625 slang_print_tree(&op
->children
[0], indent
+3);
628 case SLANG_OPER_SUBSCRIPT
:
630 printf("SLANG_OPER_SUBSCRIPT locals=%p outer=%p\n",
632 (void *) op
->locals
->outer_scope
);
633 print_generic(op
, NULL
, indent
+3);
636 case SLANG_OPER_CALL
:
639 = _slang_locate_function(A
->space
.funcs
, oper
->a_id
,
641 oper
->num_children
, &A
->space
, A
->atoms
);
644 printf("CALL %s(\n", (char *) op
->a_id
);
645 for (i
= 0; i
< op
->num_children
; i
++) {
646 slang_print_tree(&op
->children
[i
], indent
+3);
647 if (i
+ 1 < op
->num_children
) {
656 case SLANG_OPER_METHOD
:
658 printf("METHOD CALL %s.%s\n", (char *) op
->a_obj
, (char *) op
->a_id
);
661 case SLANG_OPER_FIELD
:
663 printf("FIELD %s of\n", (char*) op
->a_id
);
664 slang_print_tree(&op
->children
[0], indent
+3);
667 case SLANG_OPER_POSTINCREMENT
:
670 slang_print_tree(&op
->children
[0], indent
+3);
673 case SLANG_OPER_POSTDECREMENT
:
676 slang_print_tree(&op
->children
[0], indent
+3);
680 printf("unknown op->type %d\n", (int) op
->type
);
688 slang_print_function(const slang_function
*f
, GLboolean body
)
693 if (_mesa_strcmp((char *) f
->header
.a_name
, "main") != 0)
697 printf("FUNCTION %s ( scope=%p\n",
698 (char *) f
->header
.a_name
, (void *) f
->parameters
);
700 for (i
= 0; i
< f
->param_count
; i
++) {
701 print_variable(f
->parameters
->variables
[i
], 3);
704 printf(") param scope = %p\n", (void *) f
->parameters
);
707 slang_print_tree(f
->body
, 0);
715 slang_type_qual_string(slang_type_qualifier q
)
718 case SLANG_QUAL_NONE
:
720 case SLANG_QUAL_CONST
:
722 case SLANG_QUAL_ATTRIBUTE
:
724 case SLANG_QUAL_VARYING
:
726 case SLANG_QUAL_UNIFORM
:
730 case SLANG_QUAL_INOUT
:
732 case SLANG_QUAL_FIXEDOUTPUT
:
733 return "fixedoutput";
734 case SLANG_QUAL_FIXEDINPUT
:
735 return "fixedinputk";
743 slang_type_string(slang_type_specifier_type t
)
746 case SLANG_SPEC_VOID
:
748 case SLANG_SPEC_BOOL
:
750 case SLANG_SPEC_BVEC2
:
752 case SLANG_SPEC_BVEC3
:
754 case SLANG_SPEC_BVEC4
:
758 case SLANG_SPEC_IVEC2
:
760 case SLANG_SPEC_IVEC3
:
762 case SLANG_SPEC_IVEC4
:
764 case SLANG_SPEC_FLOAT
:
766 case SLANG_SPEC_VEC2
:
768 case SLANG_SPEC_VEC3
:
770 case SLANG_SPEC_VEC4
:
772 case SLANG_SPEC_MAT2
:
774 case SLANG_SPEC_MAT3
:
776 case SLANG_SPEC_MAT4
:
778 case SLANG_SPEC_SAMPLER1D
:
780 case SLANG_SPEC_SAMPLER2D
:
782 case SLANG_SPEC_SAMPLER3D
:
784 case SLANG_SPEC_SAMPLERCUBE
:
785 return "samplerCube";
786 case SLANG_SPEC_SAMPLER1DSHADOW
:
787 return "sampler1DShadow";
788 case SLANG_SPEC_SAMPLER2DSHADOW
:
789 return "sampler2DShadow";
790 case SLANG_SPEC_SAMPLER2DRECT
:
791 return "sampler2DRect";
792 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
793 return "sampler2DRectShadow";
794 case SLANG_SPEC_STRUCT
:
796 case SLANG_SPEC_ARRAY
:
805 slang_fq_type_string(const slang_fully_specified_type
*t
)
807 static char str
[1000];
808 sprintf(str
, "%s %s", slang_type_qual_string(t
->qualifier
),
809 slang_type_string(t
->specifier
.type
));
815 slang_print_type(const slang_fully_specified_type
*t
)
817 printf("%s %s", slang_type_qual_string(t
->qualifier
),
818 slang_type_string(t
->specifier
.type
));
824 slang_var_string(const slang_variable
*v
)
826 static char str
[1000];
827 sprintf(str
, "%s : %s",
829 slang_fq_type_string(&v
->type
));
836 slang_print_variable(const slang_variable
*v
)
838 printf("Name: %s\n", (char *) v
->a_name
);
839 printf("Type: %s\n", slang_fq_type_string(&v
->type
));
844 _slang_print_var_scope(const slang_variable_scope
*vars
, int indent
)
849 printf("Var scope %p %d vars:\n", (void *) vars
, vars
->num_variables
);
850 for (i
= 0; i
< vars
->num_variables
; i
++) {
852 printf("%s (at %p)\n", (char *) vars
->variables
[i
]->a_name
, (void*) (vars
->variables
+ i
));
855 printf("outer_scope = %p\n", (void*) vars
->outer_scope
);
857 if (vars
->outer_scope
) {
858 /*spaces(indent + 3);*/
859 _slang_print_var_scope(vars
->outer_scope
, indent
+ 3);
866 slang_checksum_tree(const slang_operation
*op
)
868 int s
= op
->num_children
;
871 for (i
= 0; i
< op
->num_children
; i
++) {
872 s
+= slang_checksum_tree(&op
->children
[i
]);