tgsi: add properties and system value register
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_sanity.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "util/u_debug.h"
29 #include "tgsi_sanity.h"
30 #include "tgsi_info.h"
31 #include "tgsi_iterate.h"
32
33 typedef uint reg_flag;
34
35 #define BITS_IN_REG_FLAG (sizeof( reg_flag ) * 8)
36
37 #define MAX_REGISTERS 1024
38 #define MAX_REG_FLAGS ((MAX_REGISTERS + BITS_IN_REG_FLAG - 1) / BITS_IN_REG_FLAG)
39
40 struct sanity_check_ctx
41 {
42 struct tgsi_iterate_context iter;
43
44 reg_flag regs_decl[TGSI_FILE_COUNT][MAX_REG_FLAGS];
45 reg_flag regs_used[TGSI_FILE_COUNT][MAX_REG_FLAGS];
46 boolean regs_ind_used[TGSI_FILE_COUNT];
47 uint num_imms;
48 uint num_instructions;
49 uint index_of_END;
50
51 uint errors;
52 uint warnings;
53 };
54
55 static void
56 report_error(
57 struct sanity_check_ctx *ctx,
58 const char *format,
59 ... )
60 {
61 va_list args;
62
63 debug_printf( "Error : " );
64 va_start( args, format );
65 _debug_vprintf( format, args );
66 va_end( args );
67 debug_printf( "\n" );
68 ctx->errors++;
69 }
70
71 static void
72 report_warning(
73 struct sanity_check_ctx *ctx,
74 const char *format,
75 ... )
76 {
77 va_list args;
78
79 debug_printf( "Warning: " );
80 va_start( args, format );
81 _debug_vprintf( format, args );
82 va_end( args );
83 debug_printf( "\n" );
84 ctx->warnings++;
85 }
86
87 static boolean
88 check_file_name(
89 struct sanity_check_ctx *ctx,
90 uint file )
91 {
92 if (file <= TGSI_FILE_NULL || file >= TGSI_FILE_COUNT) {
93 report_error( ctx, "(%u): Invalid register file name", file );
94 return FALSE;
95 }
96 return TRUE;
97 }
98
99 static boolean
100 is_register_declared(
101 struct sanity_check_ctx *ctx,
102 uint file,
103 int index )
104 {
105 assert( index >= 0 && index < MAX_REGISTERS );
106
107 return (ctx->regs_decl[file][index / BITS_IN_REG_FLAG] & (1 << (index % BITS_IN_REG_FLAG))) ? TRUE : FALSE;
108 }
109
110 static boolean
111 is_any_register_declared(
112 struct sanity_check_ctx *ctx,
113 uint file )
114 {
115 uint i;
116
117 for (i = 0; i < MAX_REG_FLAGS; i++)
118 if (ctx->regs_decl[file][i])
119 return TRUE;
120 return FALSE;
121 }
122
123 static boolean
124 is_register_used(
125 struct sanity_check_ctx *ctx,
126 uint file,
127 int index )
128 {
129 assert( index < MAX_REGISTERS );
130
131 return (ctx->regs_used[file][index / BITS_IN_REG_FLAG] & (1 << (index % BITS_IN_REG_FLAG))) ? TRUE : FALSE;
132 }
133
134 static const char *file_names[TGSI_FILE_COUNT] =
135 {
136 "NULL",
137 "CONST",
138 "IN",
139 "OUT",
140 "TEMP",
141 "SAMP",
142 "ADDR",
143 "IMM",
144 "LOOP",
145 "PRED"
146 };
147
148 static boolean
149 check_register_usage(
150 struct sanity_check_ctx *ctx,
151 uint file,
152 int index,
153 const char *name,
154 boolean indirect_access )
155 {
156 if (!check_file_name( ctx, file ))
157 return FALSE;
158
159 if (indirect_access) {
160 /* Note that 'index' is an offset relative to the value of the
161 * address register. No range checking done here.
162 */
163 if (!is_any_register_declared( ctx, file ))
164 report_error( ctx, "%s: Undeclared %s register", file_names[file], name );
165 ctx->regs_ind_used[file] = TRUE;
166 }
167 else {
168 if (index < 0 || index >= MAX_REGISTERS) {
169 report_error( ctx, "%s[%d]: Invalid %s index", file_names[file], index, name );
170 return FALSE;
171 }
172
173 if (!is_register_declared( ctx, file, index ))
174 report_error( ctx, "%s[%d]: Undeclared %s register", file_names[file], index, name );
175 ctx->regs_used[file][index / BITS_IN_REG_FLAG] |= (1 << (index % BITS_IN_REG_FLAG));
176 }
177 return TRUE;
178 }
179
180 static boolean
181 iter_instruction(
182 struct tgsi_iterate_context *iter,
183 struct tgsi_full_instruction *inst )
184 {
185 struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
186 const struct tgsi_opcode_info *info;
187 uint i;
188
189 if (inst->Instruction.Opcode == TGSI_OPCODE_END) {
190 if (ctx->index_of_END != ~0) {
191 report_error( ctx, "Too many END instructions" );
192 }
193 ctx->index_of_END = ctx->num_instructions;
194 }
195
196 info = tgsi_get_opcode_info( inst->Instruction.Opcode );
197 if (info == NULL) {
198 report_error( ctx, "(%u): Invalid instruction opcode", inst->Instruction.Opcode );
199 return TRUE;
200 }
201
202 if (info->num_dst != inst->Instruction.NumDstRegs) {
203 report_error( ctx, "%s: Invalid number of destination operands, should be %u", info->mnemonic, info->num_dst );
204 }
205 if (info->num_src != inst->Instruction.NumSrcRegs) {
206 report_error( ctx, "%s: Invalid number of source operands, should be %u", info->mnemonic, info->num_src );
207 }
208
209 /* Check destination and source registers' validity.
210 * Mark the registers as used.
211 */
212 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
213 check_register_usage(
214 ctx,
215 inst->Dst[i].Register.File,
216 inst->Dst[i].Register.Index,
217 "destination",
218 FALSE );
219 }
220 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
221 check_register_usage(
222 ctx,
223 inst->Src[i].Register.File,
224 inst->Src[i].Register.Index,
225 "source",
226 (boolean)inst->Src[i].Register.Indirect );
227 if (inst->Src[i].Register.Indirect) {
228 uint file;
229 int index;
230
231 file = inst->Src[i].Indirect.File;
232 index = inst->Src[i].Indirect.Index;
233 check_register_usage(
234 ctx,
235 file,
236 index,
237 "indirect",
238 FALSE );
239 if (!(file == TGSI_FILE_ADDRESS || file == TGSI_FILE_LOOP) || index != 0) {
240 report_warning(ctx, "Indirect register neither ADDR[0] nor LOOP[0]");
241 }
242 }
243 }
244
245 switch (inst->Instruction.Opcode) {
246 case TGSI_OPCODE_BGNFOR:
247 case TGSI_OPCODE_ENDFOR:
248 if (inst->Dst[0].Register.File != TGSI_FILE_LOOP ||
249 inst->Dst[0].Register.Index != 0) {
250 report_error(ctx, "Destination register must be LOOP[0]");
251 }
252 break;
253 }
254
255 switch (inst->Instruction.Opcode) {
256 case TGSI_OPCODE_BGNFOR:
257 if (inst->Src[0].Register.File != TGSI_FILE_CONSTANT &&
258 inst->Src[0].Register.File != TGSI_FILE_IMMEDIATE) {
259 report_error(ctx, "Source register file must be either CONST or IMM");
260 }
261 break;
262 }
263
264 ctx->num_instructions++;
265
266 return TRUE;
267 }
268
269 static boolean
270 iter_declaration(
271 struct tgsi_iterate_context *iter,
272 struct tgsi_full_declaration *decl )
273 {
274 struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
275 uint file;
276 uint i;
277
278 /* No declarations allowed after the first instruction.
279 */
280 if (ctx->num_instructions > 0)
281 report_error( ctx, "Instruction expected but declaration found" );
282
283 /* Check registers' validity.
284 * Mark the registers as declared.
285 */
286 file = decl->Declaration.File;
287 if (!check_file_name( ctx, file ))
288 return TRUE;
289 for (i = decl->Range.First; i <= decl->Range.Last; i++) {
290 if (is_register_declared( ctx, file, i ))
291 report_error( ctx, "%s[%u]: The same register declared more than once", file_names[file], i );
292 ctx->regs_decl[file][i / BITS_IN_REG_FLAG] |= (1 << (i % BITS_IN_REG_FLAG));
293 }
294
295 return TRUE;
296 }
297
298 static boolean
299 iter_immediate(
300 struct tgsi_iterate_context *iter,
301 struct tgsi_full_immediate *imm )
302 {
303 struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
304
305 assert( ctx->num_imms < MAX_REGISTERS );
306
307 /* No immediates allowed after the first instruction.
308 */
309 if (ctx->num_instructions > 0)
310 report_error( ctx, "Instruction expected but immediate found" );
311
312 /* Mark the register as declared.
313 */
314 ctx->regs_decl[TGSI_FILE_IMMEDIATE][ctx->num_imms / BITS_IN_REG_FLAG] |= (1 << (ctx->num_imms % BITS_IN_REG_FLAG));
315 ctx->num_imms++;
316
317 /* Check data type validity.
318 */
319 if (imm->Immediate.DataType != TGSI_IMM_FLOAT32) {
320 report_error( ctx, "(%u): Invalid immediate data type", imm->Immediate.DataType );
321 return TRUE;
322 }
323
324 return TRUE;
325 }
326
327
328 static boolean
329 iter_property(
330 struct tgsi_iterate_context *iter,
331 struct tgsi_full_property *prop )
332 {
333 /*struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;*/
334
335 return TRUE;
336 }
337
338 static boolean
339 epilog(
340 struct tgsi_iterate_context *iter )
341 {
342 struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
343 uint file;
344
345 /* There must be an END instruction somewhere.
346 */
347 if (ctx->index_of_END == ~0) {
348 report_error( ctx, "Missing END instruction" );
349 }
350
351 /* Check if all declared registers were used.
352 */
353 for (file = TGSI_FILE_NULL; file < TGSI_FILE_COUNT; file++) {
354 uint i;
355
356 for (i = 0; i < MAX_REGISTERS; i++) {
357 if (is_register_declared( ctx, file, i ) && !is_register_used( ctx, file, i ) && !ctx->regs_ind_used[file]) {
358 report_warning( ctx, "%s[%u]: Register never used", file_names[file], i );
359 }
360 }
361 }
362
363 /* Print totals, if any.
364 */
365 if (ctx->errors || ctx->warnings)
366 debug_printf( "%u errors, %u warnings\n", ctx->errors, ctx->warnings );
367
368 return TRUE;
369 }
370
371 boolean
372 tgsi_sanity_check(
373 const struct tgsi_token *tokens )
374 {
375 struct sanity_check_ctx ctx;
376
377 ctx.iter.prolog = NULL;
378 ctx.iter.iterate_instruction = iter_instruction;
379 ctx.iter.iterate_declaration = iter_declaration;
380 ctx.iter.iterate_immediate = iter_immediate;
381 ctx.iter.iterate_property = iter_property;
382 ctx.iter.epilog = epilog;
383
384 memset( ctx.regs_decl, 0, sizeof( ctx.regs_decl ) );
385 memset( ctx.regs_used, 0, sizeof( ctx.regs_used ) );
386 memset( ctx.regs_ind_used, 0, sizeof( ctx.regs_ind_used ) );
387 ctx.num_imms = 0;
388 ctx.num_instructions = 0;
389 ctx.index_of_END = ~0;
390
391 ctx.errors = 0;
392 ctx.warnings = 0;
393
394 if (!tgsi_iterate_shader( tokens, &ctx.iter ))
395 return FALSE;
396
397 return ctx.errors == 0;
398 }