📄 slang_print.c
字号:
/** * Dump/print a slang_operation tree */#include "main/imports.h"#include "slang_compile.h"#include "slang_print.h"static voidspaces(int n){ while (n--) printf(" ");}static voidprint_type(const slang_fully_specified_type *t){ switch (t->qualifier) { case SLANG_QUAL_NONE: /*printf("");*/ break; case SLANG_QUAL_CONST: printf("const "); break; case SLANG_QUAL_ATTRIBUTE: printf("attrib "); break; case SLANG_QUAL_VARYING: printf("varying "); break; case SLANG_QUAL_UNIFORM: printf("uniform "); break; case SLANG_QUAL_OUT: printf("output "); break; case SLANG_QUAL_INOUT: printf("inout "); break; case SLANG_QUAL_FIXEDOUTPUT: printf("fixedoutput"); break; case SLANG_QUAL_FIXEDINPUT: printf("fixedinput"); break; default: printf("unknown qualifer!"); } switch (t->specifier.type) { case SLANG_SPEC_VOID: printf("void"); break; case SLANG_SPEC_BOOL: printf("bool"); break; case SLANG_SPEC_BVEC2: printf("bvec2"); break; case SLANG_SPEC_BVEC3: printf("bvec3"); break; case SLANG_SPEC_BVEC4: printf("bvec4"); break; case SLANG_SPEC_INT: printf("int"); break; case SLANG_SPEC_IVEC2: printf("ivec2"); break; case SLANG_SPEC_IVEC3: printf("ivec3"); break; case SLANG_SPEC_IVEC4: printf("ivec4"); break; case SLANG_SPEC_FLOAT: printf("float"); break; case SLANG_SPEC_VEC2: printf("vec2"); break; case SLANG_SPEC_VEC3: printf("vec3"); break; case SLANG_SPEC_VEC4: printf("vec4"); break; case SLANG_SPEC_MAT2: printf("mat2"); break; case SLANG_SPEC_MAT3: printf("mat3"); break; case SLANG_SPEC_MAT4: printf("mat4"); break; case SLANG_SPEC_MAT23: printf("mat2x3"); break; case SLANG_SPEC_MAT32: printf("mat3x2"); break; case SLANG_SPEC_MAT24: printf("mat2x4"); break; case SLANG_SPEC_MAT42: printf("mat4x2"); break; case SLANG_SPEC_MAT34: printf("mat3x4"); break; case SLANG_SPEC_MAT43: printf("mat4x3"); break; case SLANG_SPEC_SAMPLER1D: printf("sampler1D"); break; case SLANG_SPEC_SAMPLER2D: printf("sampler2D"); break; case SLANG_SPEC_SAMPLER3D: printf("sampler3D"); break; case SLANG_SPEC_SAMPLERCUBE: printf("samplerCube"); break; case SLANG_SPEC_SAMPLER1DSHADOW: printf("sampler1DShadow"); break; case SLANG_SPEC_SAMPLER2DSHADOW: printf("sampler2DShadow"); break; case SLANG_SPEC_STRUCT: printf("struct"); break; case SLANG_SPEC_ARRAY: printf("array"); break; default: printf("unknown type"); } /*printf("\n");*/}static voidprint_variable(const slang_variable *v, int indent){ spaces(indent); printf("VAR "); print_type(&v->type); printf(" %s (at %p)", (char *) v->a_name, (void *) v); if (v->initializer) { printf(" :=\n"); slang_print_tree(v->initializer, indent + 3); } else { printf(";\n"); }}static voidprint_binary(const slang_operation *op, const char *oper, int indent){ assert(op->num_children == 2);#if 0 printf("binary at %p locals=%p outer=%p\n", (void *) op, (void *) op->locals, (void *) op->locals->outer_scope);#endif slang_print_tree(&op->children[0], indent + 3); spaces(indent); printf("%s at %p locals=%p outer=%p\n", oper, (void *) op, (void *) op->locals, (void *) op->locals->outer_scope); slang_print_tree(&op->children[1], indent + 3);}static voidprint_generic2(const slang_operation *op, const char *oper, const char *s, int indent){ GLuint i; if (oper) { spaces(indent); printf("%s %s at %p locals=%p outer=%p\n", oper, s, (void *) op, (void *) op->locals, (void *) op->locals->outer_scope); } for (i = 0; i < op->num_children; i++) { spaces(indent); printf("//child %u of %u:\n", i, op->num_children); slang_print_tree(&op->children[i], indent); }}static voidprint_generic(const slang_operation *op, const char *oper, int indent){ print_generic2(op, oper, "", indent);}static const slang_variable_scope *find_scope(const slang_variable_scope *s, slang_atom name){ GLuint i; for (i = 0; i < s->num_variables; i++) { if (s->variables[i]->a_name == name) return s; } if (s->outer_scope) return find_scope(s->outer_scope, name); else return NULL;}static const slang_variable *find_var(const slang_variable_scope *s, slang_atom name){ GLuint i; for (i = 0; i < s->num_variables; i++) { if (s->variables[i]->a_name == name) return s->variables[i]; } if (s->outer_scope) return find_var(s->outer_scope, name); else return NULL;}voidslang_print_tree(const slang_operation *op, int indent){ GLuint i; switch (op->type) { case SLANG_OPER_NONE: spaces(indent); printf("SLANG_OPER_NONE\n"); break; case SLANG_OPER_BLOCK_NO_NEW_SCOPE: spaces(indent); printf("{ locals=%p outer=%p\n", (void*)op->locals, (void*)op->locals->outer_scope); print_generic(op, NULL, indent+3); spaces(indent); printf("}\n"); break; case SLANG_OPER_BLOCK_NEW_SCOPE: spaces(indent); printf("{{ // new scope locals=%p outer=%p: ", (void *) op->locals, (void *) op->locals->outer_scope); for (i = 0; i < op->locals->num_variables; i++) { printf("%s ", (char *) op->locals->variables[i]->a_name); } printf("\n"); print_generic(op, NULL, indent+3); spaces(indent); printf("}}\n"); break; case SLANG_OPER_VARIABLE_DECL: assert(op->num_children == 0 || op->num_children == 1); { slang_variable *v; v = _slang_locate_variable(op->locals, op->a_id, GL_TRUE); if (v) { const slang_variable_scope *scope; spaces(indent); printf("DECL (locals=%p outer=%p) ", (void*)op->locals, (void*) op->locals->outer_scope); print_type(&v->type); printf(" %s (%p)", (char *) op->a_id, (void *) find_var(op->locals, op->a_id)); scope = find_scope(op->locals, op->a_id); printf(" (in scope %p) ", (void *) scope); assert(scope); if (op->num_children == 1) { printf(" :=\n"); slang_print_tree(&op->children[0], indent + 3); } else if (v->initializer) { printf(" := INITIALIZER\n"); slang_print_tree(v->initializer, indent + 3); } else { printf(";\n"); } /* spaces(indent); printf("TYPE: "); print_type(&v->type); spaces(indent); printf("ADDR: %d size: %d\n", v->address, v->size); */ } else { spaces(indent); printf("DECL %s (anonymous variable!!!!)\n", (char *) op->a_id); } } break; case SLANG_OPER_ASM: spaces(indent); printf("ASM: %s at %p locals=%p outer=%p\n", (char *) op->a_id, (void *) op, (void *) op->locals, (void *) op->locals->outer_scope); print_generic(op, "ASM", indent+3); break; case SLANG_OPER_BREAK: spaces(indent); printf("BREAK\n"); break; case SLANG_OPER_CONTINUE: spaces(indent); printf("CONTINUE\n"); break; case SLANG_OPER_DISCARD: spaces(indent); printf("DISCARD\n"); break; case SLANG_OPER_RETURN: spaces(indent); printf("RETURN\n"); if (op->num_children > 0) slang_print_tree(&op->children[0], indent + 3); break; case SLANG_OPER_LABEL: spaces(indent); printf("LABEL %s\n", (char *) op->a_id); break; case SLANG_OPER_EXPRESSION: spaces(indent); printf("EXPR: locals=%p outer=%p\n", (void *) op->locals, (void *) op->locals->outer_scope); /*print_generic(op, "SLANG_OPER_EXPRESSION", indent);*/ slang_print_tree(&op->children[0], indent + 3); break; case SLANG_OPER_IF: spaces(indent); printf("IF\n"); slang_print_tree(&op->children[0], indent + 3); spaces(indent); printf("THEN\n"); slang_print_tree(&op->children[1], indent + 3); spaces(indent); printf("ELSE\n"); slang_print_tree(&op->children[2], indent + 3); spaces(indent); printf("ENDIF\n"); break; case SLANG_OPER_WHILE: assert(op->num_children == 2); spaces(indent); printf("WHILE cond:\n"); slang_print_tree(&op->children[0], indent + 3); spaces(indent); printf("WHILE body:\n"); slang_print_tree(&op->children[1], indent + 3); break; case SLANG_OPER_DO: spaces(indent); printf("DO body:\n"); slang_print_tree(&op->children[0], indent + 3); spaces(indent); printf("DO cond:\n"); slang_print_tree(&op->children[1], indent + 3); break; case SLANG_OPER_FOR: spaces(indent); printf("FOR init:\n"); slang_print_tree(&op->children[0], indent + 3); spaces(indent); printf("FOR while:\n"); slang_print_tree(&op->children[1], indent + 3); spaces(indent); printf("FOR step:\n"); slang_print_tree(&op->children[2], indent + 3); spaces(indent); printf("FOR body:\n"); slang_print_tree(&op->children[3], indent + 3); spaces(indent); printf("ENDFOR\n"); /* print_generic(op, "FOR", indent + 3); */ break; case SLANG_OPER_VOID: spaces(indent); printf("(oper-void)\n"); break; case SLANG_OPER_LITERAL_BOOL: spaces(indent); printf("LITERAL ("); for (i = 0; i < op->literal_size; i++) printf("%s ", op->literal[0] ? "TRUE" : "FALSE"); printf(")\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -