1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "util/u_debug.h"
29 #include "util/u_memory.h"
30 #include "util/u_prim.h"
31 #include "cso_cache/cso_hash.h"
32 #include "tgsi_sanity.h"
33 #include "tgsi_info.h"
34 #include "tgsi_iterate.h"
38 /* max 2 dimensions */
43 struct sanity_check_ctx
45 struct tgsi_iterate_context iter
;
46 struct cso_hash
*regs_decl
;
47 struct cso_hash
*regs_used
;
48 struct cso_hash
*regs_ind_used
;
51 uint num_instructions
;
56 uint implied_array_size
;
59 static INLINE
unsigned
60 scan_register_key(const scan_register
*reg
)
62 unsigned key
= reg
->file
;
63 key
|= (reg
->indices
[0] << 4);
64 key
|= (reg
->indices
[1] << 18);
70 fill_scan_register1d(scan_register
*reg
,
71 uint file
, uint index
)
75 reg
->indices
[0] = index
;
80 fill_scan_register2d(scan_register
*reg
,
81 uint file
, uint index1
, uint index2
)
85 reg
->indices
[0] = index1
;
86 reg
->indices
[1] = index2
;
90 scan_register_dst(scan_register
*reg
,
91 struct tgsi_full_dst_register
*dst
)
93 fill_scan_register1d(reg
,
99 scan_register_src(scan_register
*reg
,
100 struct tgsi_full_src_register
*src
)
102 if (src
->Register
.Dimension
) {
103 /*FIXME: right now we don't support indirect
104 * multidimensional addressing */
105 fill_scan_register2d(reg
,
108 src
->Dimension
.Index
);
110 fill_scan_register1d(reg
,
112 src
->Register
.Index
);
116 static scan_register
*
117 create_scan_register_src(struct tgsi_full_src_register
*src
)
119 scan_register
*reg
= MALLOC(sizeof(scan_register
));
120 scan_register_src(reg
, src
);
125 static scan_register
*
126 create_scan_register_dst(struct tgsi_full_dst_register
*dst
)
128 scan_register
*reg
= MALLOC(sizeof(scan_register
));
129 scan_register_dst(reg
, dst
);
136 struct sanity_check_ctx
*ctx
,
142 debug_printf( "Error : " );
143 va_start( args
, format
);
144 _debug_vprintf( format
, args
);
146 debug_printf( "\n" );
152 struct sanity_check_ctx
*ctx
,
158 debug_printf( "Warning: " );
159 va_start( args
, format
);
160 _debug_vprintf( format
, args
);
162 debug_printf( "\n" );
168 struct sanity_check_ctx
*ctx
,
171 if (file
<= TGSI_FILE_NULL
|| file
>= TGSI_FILE_COUNT
) {
172 report_error( ctx
, "(%u): Invalid register file name", file
);
179 is_register_declared(
180 struct sanity_check_ctx
*ctx
,
181 const scan_register
*reg
)
183 void *data
= cso_hash_find_data_from_template(
184 ctx
->regs_decl
, scan_register_key(reg
),
185 (void*)reg
, sizeof(scan_register
));
186 return data
? TRUE
: FALSE
;
190 is_any_register_declared(
191 struct sanity_check_ctx
*ctx
,
194 struct cso_hash_iter iter
=
195 cso_hash_first_node(ctx
->regs_decl
);
197 while (!cso_hash_iter_is_null(iter
)) {
198 scan_register
*reg
= (scan_register
*)cso_hash_iter_data(iter
);
199 if (reg
->file
== file
)
201 iter
= cso_hash_iter_next(iter
);
209 struct sanity_check_ctx
*ctx
,
212 void *data
= cso_hash_find_data_from_template(
213 ctx
->regs_used
, scan_register_key(reg
),
214 reg
, sizeof(scan_register
));
215 return data
? TRUE
: FALSE
;
220 is_ind_register_used(
221 struct sanity_check_ctx
*ctx
,
224 return cso_hash_contains(ctx
->regs_ind_used
, reg
->file
);
227 static const char *file_names
[TGSI_FILE_COUNT
] =
243 check_register_usage(
244 struct sanity_check_ctx
*ctx
,
247 boolean indirect_access
)
249 if (!check_file_name( ctx
, reg
->file
)) {
254 if (indirect_access
) {
255 /* Note that 'index' is an offset relative to the value of the
256 * address register. No range checking done here.*/
259 if (!is_any_register_declared( ctx
, reg
->file
))
260 report_error( ctx
, "%s: Undeclared %s register", file_names
[reg
->file
], name
);
261 if (!is_ind_register_used(ctx
, reg
))
262 cso_hash_insert(ctx
->regs_ind_used
, reg
->file
, reg
);
267 if (!is_register_declared( ctx
, reg
)) {
268 if (reg
->dimensions
== 2) {
269 report_error( ctx
, "%s[%d][%d]: Undeclared %s register", file_names
[reg
->file
],
270 reg
->indices
[0], reg
->indices
[1], name
);
273 report_error( ctx
, "%s[%d]: Undeclared %s register", file_names
[reg
->file
],
274 reg
->indices
[0], name
);
277 if (!is_register_used( ctx
, reg
))
278 cso_hash_insert(ctx
->regs_used
, scan_register_key(reg
), reg
);
287 struct tgsi_iterate_context
*iter
,
288 struct tgsi_full_instruction
*inst
)
290 struct sanity_check_ctx
*ctx
= (struct sanity_check_ctx
*) iter
;
291 const struct tgsi_opcode_info
*info
;
294 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_END
) {
295 if (ctx
->index_of_END
!= ~0) {
296 report_error( ctx
, "Too many END instructions" );
298 ctx
->index_of_END
= ctx
->num_instructions
;
301 info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
303 report_error( ctx
, "(%u): Invalid instruction opcode", inst
->Instruction
.Opcode
);
307 if (info
->num_dst
!= inst
->Instruction
.NumDstRegs
) {
308 report_error( ctx
, "%s: Invalid number of destination operands, should be %u", info
->mnemonic
, info
->num_dst
);
310 if (info
->num_src
!= inst
->Instruction
.NumSrcRegs
) {
311 report_error( ctx
, "%s: Invalid number of source operands, should be %u", info
->mnemonic
, info
->num_src
);
314 /* Check destination and source registers' validity.
315 * Mark the registers as used.
317 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
318 scan_register
*reg
= create_scan_register_dst(&inst
->Dst
[i
]);
319 check_register_usage(
324 if (!inst
->Dst
[i
].Register
.WriteMask
) {
325 report_error(ctx
, "Destination register has empty writemask");
328 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
329 scan_register
*reg
= create_scan_register_src(&inst
->Src
[i
]);
330 check_register_usage(
334 (boolean
)inst
->Src
[i
].Register
.Indirect
);
335 if (inst
->Src
[i
].Register
.Indirect
) {
336 scan_register
*ind_reg
= MALLOC(sizeof(scan_register
));
338 fill_scan_register1d(ind_reg
,
339 inst
->Src
[i
].Indirect
.File
,
340 inst
->Src
[i
].Indirect
.Index
);
341 check_register_usage(
349 ctx
->num_instructions
++;
355 check_and_declare(struct sanity_check_ctx
*ctx
,
358 if (is_register_declared( ctx
, reg
))
359 report_error( ctx
, "%s[%u]: The same register declared more than once",
360 file_names
[reg
->file
], reg
->indices
[0] );
361 cso_hash_insert(ctx
->regs_decl
,
362 scan_register_key(reg
),
369 struct tgsi_iterate_context
*iter
,
370 struct tgsi_full_declaration
*decl
)
372 struct sanity_check_ctx
*ctx
= (struct sanity_check_ctx
*) iter
;
376 /* No declarations allowed after the first instruction.
378 if (ctx
->num_instructions
> 0)
379 report_error( ctx
, "Instruction expected but declaration found" );
381 /* Check registers' validity.
382 * Mark the registers as declared.
384 file
= decl
->Declaration
.File
;
385 if (!check_file_name( ctx
, file
))
387 for (i
= decl
->Range
.First
; i
<= decl
->Range
.Last
; i
++) {
388 /* declared TGSI_FILE_INPUT's for geometry processor
389 * have an implied second dimension */
390 if (file
== TGSI_FILE_INPUT
&&
391 ctx
->iter
.processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
) {
393 for (vert
= 0; vert
< ctx
->implied_array_size
; ++vert
) {
394 scan_register
*reg
= MALLOC(sizeof(scan_register
));
395 fill_scan_register2d(reg
, file
, i
, vert
);
396 check_and_declare(ctx
, reg
);
399 scan_register
*reg
= MALLOC(sizeof(scan_register
));
400 if (decl
->Declaration
.Dimension
) {
401 fill_scan_register2d(reg
, file
, i
, decl
->Dim
.Index2D
);
403 fill_scan_register1d(reg
, file
, i
);
405 check_and_declare(ctx
, reg
);
414 struct tgsi_iterate_context
*iter
,
415 struct tgsi_full_immediate
*imm
)
417 struct sanity_check_ctx
*ctx
= (struct sanity_check_ctx
*) iter
;
420 /* No immediates allowed after the first instruction.
422 if (ctx
->num_instructions
> 0)
423 report_error( ctx
, "Instruction expected but immediate found" );
425 /* Mark the register as declared.
427 reg
= MALLOC(sizeof(scan_register
));
428 fill_scan_register1d(reg
, TGSI_FILE_IMMEDIATE
, ctx
->num_imms
);
429 cso_hash_insert(ctx
->regs_decl
, scan_register_key(reg
), reg
);
432 /* Check data type validity.
434 if (imm
->Immediate
.DataType
!= TGSI_IMM_FLOAT32
&&
435 imm
->Immediate
.DataType
!= TGSI_IMM_UINT32
&&
436 imm
->Immediate
.DataType
!= TGSI_IMM_INT32
) {
437 report_error( ctx
, "(%u): Invalid immediate data type", imm
->Immediate
.DataType
);
447 struct tgsi_iterate_context
*iter
,
448 struct tgsi_full_property
*prop
)
450 struct sanity_check_ctx
*ctx
= (struct sanity_check_ctx
*) iter
;
452 if (iter
->processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
&&
453 prop
->Property
.PropertyName
== TGSI_PROPERTY_GS_INPUT_PRIM
) {
454 ctx
->implied_array_size
= u_vertices_per_prim(prop
->u
[0].Data
);
461 struct tgsi_iterate_context
*iter
)
463 struct sanity_check_ctx
*ctx
= (struct sanity_check_ctx
*) iter
;
465 /* There must be an END instruction somewhere.
467 if (ctx
->index_of_END
== ~0) {
468 report_error( ctx
, "Missing END instruction" );
471 /* Check if all declared registers were used.
474 struct cso_hash_iter iter
=
475 cso_hash_first_node(ctx
->regs_decl
);
477 while (!cso_hash_iter_is_null(iter
)) {
478 scan_register
*reg
= (scan_register
*)cso_hash_iter_data(iter
);
479 if (!is_register_used(ctx
, reg
) && !is_ind_register_used(ctx
, reg
)) {
480 report_warning( ctx
, "%s[%u]: Register never used",
481 file_names
[reg
->file
], reg
->indices
[0] );
483 iter
= cso_hash_iter_next(iter
);
487 /* Print totals, if any.
489 if (ctx
->errors
|| ctx
->warnings
)
490 debug_printf( "%u errors, %u warnings\n", ctx
->errors
, ctx
->warnings
);
496 regs_hash_destroy(struct cso_hash
*hash
)
498 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
499 while (!cso_hash_iter_is_null(iter
)) {
500 scan_register
*reg
= (scan_register
*)cso_hash_iter_data(iter
);
501 iter
= cso_hash_erase(hash
, iter
);
502 assert(reg
->file
< TGSI_FILE_COUNT
);
505 cso_hash_delete(hash
);
510 const struct tgsi_token
*tokens
)
512 struct sanity_check_ctx ctx
;
514 ctx
.iter
.prolog
= NULL
;
515 ctx
.iter
.iterate_instruction
= iter_instruction
;
516 ctx
.iter
.iterate_declaration
= iter_declaration
;
517 ctx
.iter
.iterate_immediate
= iter_immediate
;
518 ctx
.iter
.iterate_property
= iter_property
;
519 ctx
.iter
.epilog
= epilog
;
521 ctx
.regs_decl
= cso_hash_create();
522 ctx
.regs_used
= cso_hash_create();
523 ctx
.regs_ind_used
= cso_hash_create();
526 ctx
.num_instructions
= 0;
527 ctx
.index_of_END
= ~0;
531 ctx
.implied_array_size
= 0;
533 if (!tgsi_iterate_shader( tokens
, &ctx
.iter
))
536 regs_hash_destroy(ctx
.regs_decl
);
537 regs_hash_destroy(ctx
.regs_used
);
538 regs_hash_destroy(ctx
.regs_ind_used
);
539 return ctx
.errors
== 0;