Merge remote branch 'origin/master' into glsl2
[mesa.git] / src / glsl / ir_validate.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file ir_validate.cpp
26 *
27 * Attempts to verify that various invariants of the IR tree are true.
28 *
29 * In particular, at the moment it makes sure that no single
30 * ir_instruction node except for ir_variable appears multiple times
31 * in the ir tree. ir_variable does appear multiple times: Once as a
32 * declaration in an exec_list, and multiple times as the endpoint of
33 * a dereference chain.
34 */
35
36 #include <inttypes.h>
37 #include "ir.h"
38 #include "ir_hierarchical_visitor.h"
39 #include "hash_table.h"
40 #include "glsl_types.h"
41
42 class ir_validate : public ir_hierarchical_visitor {
43 public:
44 ir_validate()
45 {
46 this->ht = hash_table_ctor(0, hash_table_pointer_hash,
47 hash_table_pointer_compare);
48
49 this->current_function = NULL;
50
51 this->callback = ir_validate::validate_ir;
52 this->data = ht;
53 }
54
55 ~ir_validate()
56 {
57 hash_table_dtor(this->ht);
58 }
59
60 virtual ir_visitor_status visit(ir_variable *v);
61 virtual ir_visitor_status visit(ir_dereference_variable *ir);
62 virtual ir_visitor_status visit(ir_if *ir);
63
64 virtual ir_visitor_status visit_enter(ir_function *ir);
65 virtual ir_visitor_status visit_leave(ir_function *ir);
66 virtual ir_visitor_status visit_enter(ir_function_signature *ir);
67
68 static void validate_ir(ir_instruction *ir, void *data);
69
70 ir_function *current_function;
71
72 struct hash_table *ht;
73 };
74
75
76 ir_visitor_status
77 ir_validate::visit(ir_dereference_variable *ir)
78 {
79 if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) {
80 printf("ir_dereference_variable @ %p does not specify a variable %p\n",
81 ir, ir->var);
82 abort();
83 }
84
85 if (hash_table_find(ht, ir->var) == NULL) {
86 printf("ir_dereference_variable @ %p specifies undeclared variable "
87 "`%s' @ %p\n",
88 ir, ir->var->name, ir->var);
89 abort();
90 }
91
92 this->validate_ir(ir, this->data);
93
94 return visit_continue;
95 }
96
97 ir_visitor_status
98 ir_validate::visit(ir_if *ir)
99 {
100 if (ir->condition->type != glsl_type::bool_type) {
101 printf("ir_if condition %s type instead of bool.\n",
102 ir->condition->type->name);
103 ir->print();
104 printf("\n");
105 abort();
106 }
107 }
108
109
110 ir_visitor_status
111 ir_validate::visit_enter(ir_function *ir)
112 {
113 /* Function definitions cannot be nested.
114 */
115 if (this->current_function != NULL) {
116 printf("Function definition nested inside another function "
117 "definition:\n");
118 printf("%s %p inside %s %p\n",
119 ir->name, ir,
120 this->current_function->name, this->current_function);
121 abort();
122 }
123
124 /* Store the current function hierarchy being traversed. This is used
125 * by the function signature visitor to ensure that the signatures are
126 * linked with the correct functions.
127 */
128 this->current_function = ir;
129
130 this->validate_ir(ir, this->data);
131
132 return visit_continue;
133 }
134
135 ir_visitor_status
136 ir_validate::visit_leave(ir_function *ir)
137 {
138 (void) ir;
139
140 this->current_function = NULL;
141 return visit_continue;
142 }
143
144 ir_visitor_status
145 ir_validate::visit_enter(ir_function_signature *ir)
146 {
147 if (this->current_function != ir->function()) {
148 printf("Function signature nested inside wrong function "
149 "definition:\n");
150 printf("%p inside %s %p instead of %s %p\n",
151 ir,
152 this->current_function->name, this->current_function,
153 ir->function_name(), ir->function());
154 abort();
155 }
156
157 this->validate_ir(ir, this->data);
158
159 return visit_continue;
160 }
161
162 ir_visitor_status
163 ir_validate::visit(ir_variable *ir)
164 {
165 /* An ir_variable is the one thing that can (and will) appear multiple times
166 * in an IR tree. It is added to the hashtable so that it can be used
167 * in the ir_dereference_variable handler to ensure that a variable is
168 * declared before it is dereferenced.
169 */
170 hash_table_insert(ht, ir, ir);
171 return visit_continue;
172 }
173
174 void
175 ir_validate::validate_ir(ir_instruction *ir, void *data)
176 {
177 struct hash_table *ht = (struct hash_table *) data;
178
179 if (hash_table_find(ht, ir)) {
180 printf("Instruction node present twice in ir tree:\n");
181 ir->print();
182 printf("\n");
183 abort();
184 }
185 hash_table_insert(ht, ir, ir);
186 }
187
188 void
189 check_node_type(ir_instruction *ir, void *data)
190 {
191 if (ir->ir_type <= ir_type_unset || ir->ir_type >= ir_type_max) {
192 printf("Instruction node with unset type\n");
193 ir->print(); printf("\n");
194 }
195 assert(ir->type != glsl_type::error_type);
196 }
197
198 void
199 validate_ir_tree(exec_list *instructions)
200 {
201 ir_validate v;
202
203 v.run(instructions);
204
205 foreach_iter(exec_list_iterator, iter, *instructions) {
206 ir_instruction *ir = (ir_instruction *)iter.get();
207
208 visit_tree(ir, check_node_type, NULL);
209 }
210 }