3 * Dump/print a slang_operation tree
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
);
271 print_generic(op
, NULL
, indent
+3);
276 case SLANG_OPER_VARIABLE_DECL
:
277 assert(op
->num_children
== 0 || op
->num_children
== 1);
280 v
= _slang_locate_variable(op
->locals
, op
->a_id
, GL_TRUE
);
282 const slang_variable_scope
*scope
;
284 printf("DECL (locals=%p outer=%p) ", (void*)op
->locals
, (void*) op
->locals
->outer_scope
);
285 print_type(&v
->type
);
286 printf(" %s (%p)", (char *) op
->a_id
,
287 (void *) find_var(op
->locals
, op
->a_id
));
289 scope
= find_scope(op
->locals
, op
->a_id
);
290 printf(" (in scope %p) ", (void *) scope
);
292 if (op
->num_children
== 1) {
294 slang_print_tree(&op
->children
[0], indent
+ 3);
296 else if (v
->initializer
) {
297 printf(" := INITIALIZER\n");
298 slang_print_tree(v
->initializer
, indent
+ 3);
306 print_type(&v->type);
308 printf("ADDR: %d size: %d\n", v->address, v->size);
313 printf("DECL %s (anonymous variable!!!!)\n", (char *) op
->a_id
);
320 printf("ASM: %s at %p locals=%p outer=%p\n",
324 (void *) op
->locals
->outer_scope
);
325 print_generic(op
, "ASM", indent
+3);
328 case SLANG_OPER_BREAK
:
333 case SLANG_OPER_CONTINUE
:
335 printf("CONTINUE\n");
338 case SLANG_OPER_DISCARD
:
343 case SLANG_OPER_RETURN
:
346 if (op
->num_children
> 0)
347 slang_print_tree(&op
->children
[0], indent
+ 3);
350 case SLANG_OPER_LABEL
:
352 printf("LABEL %s\n", (char *) op
->a_id
);
355 case SLANG_OPER_EXPRESSION
:
357 printf("EXPR: locals=%p outer=%p\n",
359 (void *) op
->locals
->outer_scope
);
360 /*print_generic(op, "SLANG_OPER_EXPRESSION", indent);*/
361 slang_print_tree(&op
->children
[0], indent
+ 3);
367 slang_print_tree(&op
->children
[0], indent
+ 3);
370 slang_print_tree(&op
->children
[1], indent
+ 3);
373 slang_print_tree(&op
->children
[2], indent
+ 3);
378 case SLANG_OPER_WHILE
:
379 assert(op
->num_children
== 2);
381 printf("WHILE cond:\n");
382 slang_print_tree(&op
->children
[0], indent
+ 3);
384 printf("WHILE body:\n");
385 slang_print_tree(&op
->children
[1], indent
+ 3);
390 printf("DO body:\n");
391 slang_print_tree(&op
->children
[0], indent
+ 3);
393 printf("DO cond:\n");
394 slang_print_tree(&op
->children
[1], indent
+ 3);
399 printf("FOR init:\n");
400 slang_print_tree(&op
->children
[0], indent
+ 3);
402 printf("FOR while:\n");
403 slang_print_tree(&op
->children
[1], indent
+ 3);
405 printf("FOR step:\n");
406 slang_print_tree(&op
->children
[2], indent
+ 3);
408 printf("FOR body:\n");
409 slang_print_tree(&op
->children
[3], indent
+ 3);
413 print_generic(op, "FOR", indent + 3);
417 case SLANG_OPER_VOID
:
419 printf("(oper-void)\n");
422 case SLANG_OPER_LITERAL_BOOL
:
425 for (i
= 0; i
< op
->literal_size
; i
++)
426 printf("%s ", op
->literal
[0] ? "TRUE" : "FALSE");
431 case SLANG_OPER_LITERAL_INT
:
434 for (i
= 0; i
< op
->literal_size
; i
++)
435 printf("%d ", (int) op
->literal
[i
]);
439 case SLANG_OPER_LITERAL_FLOAT
:
442 for (i
= 0; i
< op
->literal_size
; i
++)
443 printf("%f ", op
->literal
[i
]);
447 case SLANG_OPER_IDENTIFIER
:
449 const slang_variable_scope
*scope
;
451 if (op
->var
&& op
->var
->a_name
) {
452 scope
= find_scope(op
->locals
, op
->var
->a_name
);
453 printf("VAR %s (in scope %p)\n", (char *) op
->var
->a_name
,
458 scope
= find_scope(op
->locals
, op
->a_id
);
459 printf("VAR' %s (in scope %p) locals=%p outer=%p\n",
463 (void *) op
->locals
->outer_scope
);
469 case SLANG_OPER_SEQUENCE
:
470 print_generic(op
, "COMMA-SEQ", indent
+3);
473 case SLANG_OPER_ASSIGN
:
475 printf("ASSIGNMENT locals=%p outer=%p\n",
477 (void *) op
->locals
->outer_scope
);
478 print_binary(op
, ":=", indent
);
481 case SLANG_OPER_ADDASSIGN
:
484 print_binary(op
, "+=", indent
);
487 case SLANG_OPER_SUBASSIGN
:
490 print_binary(op
, "-=", indent
);
493 case SLANG_OPER_MULASSIGN
:
496 print_binary(op
, "*=", indent
);
499 case SLANG_OPER_DIVASSIGN
:
502 print_binary(op
, "/=", indent
);
505 /*SLANG_OPER_MODASSIGN,*/
506 /*SLANG_OPER_LSHASSIGN,*/
507 /*SLANG_OPER_RSHASSIGN,*/
508 /*SLANG_OPER_ORASSIGN,*/
509 /*SLANG_OPER_XORASSIGN,*/
510 /*SLANG_OPER_ANDASSIGN,*/
511 case SLANG_OPER_SELECT
:
513 printf("SLANG_OPER_SELECT n=%d\n", op
->num_children
);
514 assert(op
->num_children
== 3);
515 slang_print_tree(&op
->children
[0], indent
+3);
518 slang_print_tree(&op
->children
[1], indent
+3);
521 slang_print_tree(&op
->children
[2], indent
+3);
524 case SLANG_OPER_LOGICALOR
:
525 print_binary(op
, "||", indent
);
528 case SLANG_OPER_LOGICALXOR
:
529 print_binary(op
, "^^", indent
);
532 case SLANG_OPER_LOGICALAND
:
533 print_binary(op
, "&&", indent
);
537 /*SLANG_OPER_BITXOR*/
538 /*SLANG_OPER_BITAND*/
539 case SLANG_OPER_EQUAL
:
540 print_binary(op
, "==", indent
);
543 case SLANG_OPER_NOTEQUAL
:
544 print_binary(op
, "!=", indent
);
547 case SLANG_OPER_LESS
:
548 print_binary(op
, "<", indent
);
551 case SLANG_OPER_GREATER
:
552 print_binary(op
, ">", indent
);
555 case SLANG_OPER_LESSEQUAL
:
556 print_binary(op
, "<=", indent
);
559 case SLANG_OPER_GREATEREQUAL
:
560 print_binary(op
, ">=", indent
);
563 /*SLANG_OPER_LSHIFT*/
564 /*SLANG_OPER_RSHIFT*/
566 print_binary(op
, "+", indent
);
569 case SLANG_OPER_SUBTRACT
:
570 print_binary(op
, "-", indent
);
573 case SLANG_OPER_MULTIPLY
:
574 print_binary(op
, "*", indent
);
577 case SLANG_OPER_DIVIDE
:
578 print_binary(op
, "/", indent
);
581 /*SLANG_OPER_MODULUS*/
582 case SLANG_OPER_PREINCREMENT
:
585 slang_print_tree(&op
->children
[0], indent
+3);
588 case SLANG_OPER_PREDECREMENT
:
591 slang_print_tree(&op
->children
[0], indent
+3);
594 case SLANG_OPER_PLUS
:
596 printf("SLANG_OPER_PLUS\n");
599 case SLANG_OPER_MINUS
:
601 printf("SLANG_OPER_MINUS\n");
604 /*SLANG_OPER_COMPLEMENT*/
608 slang_print_tree(&op
->children
[0], indent
+3);
611 case SLANG_OPER_SUBSCRIPT
:
613 printf("SLANG_OPER_SUBSCRIPT locals=%p outer=%p\n",
615 (void *) op
->locals
->outer_scope
);
616 print_generic(op
, NULL
, indent
+3);
619 case SLANG_OPER_CALL
:
622 = _slang_locate_function(A
->space
.funcs
, oper
->a_id
,
624 oper
->num_children
, &A
->space
, A
->atoms
);
627 printf("CALL %s(\n", (char *) op
->a_id
);
628 for (i
= 0; i
< op
->num_children
; i
++) {
629 slang_print_tree(&op
->children
[i
], indent
+3);
630 if (i
+ 1 < op
->num_children
) {
639 case SLANG_OPER_FIELD
:
641 printf("FIELD %s of\n", (char*) op
->a_id
);
642 slang_print_tree(&op
->children
[0], indent
+3);
645 case SLANG_OPER_POSTINCREMENT
:
648 slang_print_tree(&op
->children
[0], indent
+3);
651 case SLANG_OPER_POSTDECREMENT
:
654 slang_print_tree(&op
->children
[0], indent
+3);
658 printf("unknown op->type %d\n", (int) op
->type
);
666 slang_print_function(const slang_function
*f
, GLboolean body
)
671 if (_mesa_strcmp((char *) f
->header
.a_name
, "main") != 0)
675 printf("FUNCTION %s ( scope=%p\n",
676 (char *) f
->header
.a_name
, (void *) f
->parameters
);
678 for (i
= 0; i
< f
->param_count
; i
++) {
679 print_variable(f
->parameters
->variables
[i
], 3);
682 printf(") param scope = %p\n", (void *) f
->parameters
);
685 slang_print_tree(f
->body
, 0);
693 slang_type_qual_string(slang_type_qualifier q
)
696 case SLANG_QUAL_NONE
:
698 case SLANG_QUAL_CONST
:
700 case SLANG_QUAL_ATTRIBUTE
:
702 case SLANG_QUAL_VARYING
:
704 case SLANG_QUAL_UNIFORM
:
708 case SLANG_QUAL_INOUT
:
710 case SLANG_QUAL_FIXEDOUTPUT
:
711 return "fixedoutput";
712 case SLANG_QUAL_FIXEDINPUT
:
713 return "fixedinputk";
721 slang_type_string(slang_type_specifier_type t
)
724 case SLANG_SPEC_VOID
:
726 case SLANG_SPEC_BOOL
:
728 case SLANG_SPEC_BVEC2
:
730 case SLANG_SPEC_BVEC3
:
732 case SLANG_SPEC_BVEC4
:
736 case SLANG_SPEC_IVEC2
:
738 case SLANG_SPEC_IVEC3
:
740 case SLANG_SPEC_IVEC4
:
742 case SLANG_SPEC_FLOAT
:
744 case SLANG_SPEC_VEC2
:
746 case SLANG_SPEC_VEC3
:
748 case SLANG_SPEC_VEC4
:
750 case SLANG_SPEC_MAT2
:
752 case SLANG_SPEC_MAT3
:
754 case SLANG_SPEC_MAT4
:
756 case SLANG_SPEC_SAMPLER1D
:
758 case SLANG_SPEC_SAMPLER2D
:
760 case SLANG_SPEC_SAMPLER3D
:
762 case SLANG_SPEC_SAMPLERCUBE
:
763 return "samplerCube";
764 case SLANG_SPEC_SAMPLER1DSHADOW
:
765 return "sampler1DShadow";
766 case SLANG_SPEC_SAMPLER2DSHADOW
:
767 return "sampler2DShadow";
768 case SLANG_SPEC_SAMPLER2DRECT
:
769 return "sampler2DRect";
770 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
771 return "sampler2DRectShadow";
772 case SLANG_SPEC_STRUCT
:
774 case SLANG_SPEC_ARRAY
:
783 slang_fq_type_string(const slang_fully_specified_type
*t
)
785 static char str
[1000];
786 sprintf(str
, "%s %s", slang_type_qual_string(t
->qualifier
),
787 slang_type_string(t
->specifier
.type
));
793 slang_print_type(const slang_fully_specified_type
*t
)
795 printf("%s %s", slang_type_qual_string(t
->qualifier
),
796 slang_type_string(t
->specifier
.type
));
802 slang_var_string(const slang_variable
*v
)
804 static char str
[1000];
805 sprintf(str
, "%s : %s",
807 slang_fq_type_string(&v
->type
));
814 slang_print_variable(const slang_variable
*v
)
816 printf("Name: %s\n", (char *) v
->a_name
);
817 printf("Type: %s\n", slang_fq_type_string(&v
->type
));
822 _slang_print_var_scope(const slang_variable_scope
*vars
, int indent
)
827 printf("Var scope %p %d vars:\n", (void *) vars
, vars
->num_variables
);
828 for (i
= 0; i
< vars
->num_variables
; i
++) {
830 printf("%s (at %p)\n", (char *) vars
->variables
[i
]->a_name
, (void*) (vars
->variables
+ i
));
833 printf("outer_scope = %p\n", (void*) vars
->outer_scope
);
835 if (vars
->outer_scope
) {
836 /*spaces(indent + 3);*/
837 _slang_print_var_scope(vars
->outer_scope
, indent
+ 3);
844 slang_checksum_tree(const slang_operation
*op
)
846 int s
= op
->num_children
;
849 for (i
= 0; i
< op
->num_children
; i
++) {
850 s
+= slang_checksum_tree(&op
->children
[i
]);