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 "tgsi_sanity.h"
30 #include "tgsi_info.h"
31 #include "tgsi_iterate.h"
33 typedef uint reg_flag
;
35 #define BITS_IN_REG_FLAG (sizeof( reg_flag ) * 8)
37 #define MAX_REGISTERS 256
38 #define MAX_REG_FLAGS ((MAX_REGISTERS + BITS_IN_REG_FLAG - 1) / BITS_IN_REG_FLAG)
40 struct sanity_check_ctx
42 struct tgsi_iterate_context iter
;
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
];
48 uint num_instructions
;
57 struct sanity_check_ctx
*ctx
,
63 debug_printf( "Error : " );
64 va_start( args
, format
);
65 _debug_vprintf( format
, args
);
73 struct sanity_check_ctx
*ctx
,
79 debug_printf( "Warning: " );
80 va_start( args
, format
);
81 _debug_vprintf( format
, args
);
89 struct sanity_check_ctx
*ctx
,
92 if (file
<= TGSI_FILE_NULL
|| file
>= TGSI_FILE_COUNT
) {
93 report_error( ctx
, "(%u): Invalid register file name", file
);
100 is_register_declared(
101 struct sanity_check_ctx
*ctx
,
105 assert( index
>= 0 && index
< MAX_REGISTERS
);
107 return (ctx
->regs_decl
[file
][index
/ BITS_IN_REG_FLAG
] & (1 << (index
% BITS_IN_REG_FLAG
))) ? TRUE
: FALSE
;
111 is_any_register_declared(
112 struct sanity_check_ctx
*ctx
,
117 for (i
= 0; i
< MAX_REG_FLAGS
; i
++)
118 if (ctx
->regs_decl
[file
][i
])
125 struct sanity_check_ctx
*ctx
,
129 assert( index
< MAX_REGISTERS
);
131 return (ctx
->regs_used
[file
][index
/ BITS_IN_REG_FLAG
] & (1 << (index
% BITS_IN_REG_FLAG
))) ? TRUE
: FALSE
;
134 static const char *file_names
[TGSI_FILE_COUNT
] =
148 check_register_usage(
149 struct sanity_check_ctx
*ctx
,
153 boolean indirect_access
)
155 if (!check_file_name( ctx
, file
))
158 if (indirect_access
) {
159 /* Note that 'index' is an offset relative to the value of the
160 * address register. No range checking done here.
162 if (!is_any_register_declared( ctx
, file
))
163 report_error( ctx
, "%s: Undeclared %s register", file_names
[file
], name
);
164 ctx
->regs_ind_used
[file
] = TRUE
;
167 if (index
< 0 || index
>= MAX_REGISTERS
) {
168 report_error( ctx
, "%s[%d]: Invalid %s index", file_names
[file
], index
, name
);
172 if (!is_register_declared( ctx
, file
, index
))
173 report_error( ctx
, "%s[%d]: Undeclared %s register", file_names
[file
], index
, name
);
174 ctx
->regs_used
[file
][index
/ BITS_IN_REG_FLAG
] |= (1 << (index
% BITS_IN_REG_FLAG
));
181 struct tgsi_iterate_context
*iter
,
182 struct tgsi_full_instruction
*inst
)
184 struct sanity_check_ctx
*ctx
= (struct sanity_check_ctx
*) iter
;
185 const struct tgsi_opcode_info
*info
;
188 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_END
) {
189 if (ctx
->index_of_END
!= ~0) {
190 report_error( ctx
, "Too many END instructions" );
192 ctx
->index_of_END
= ctx
->num_instructions
;
195 info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
197 report_error( ctx
, "(%u): Invalid instruction opcode", inst
->Instruction
.Opcode
);
201 if (info
->num_dst
!= inst
->Instruction
.NumDstRegs
) {
202 report_error( ctx
, "%s: Invalid number of destination operands, should be %u", info
->mnemonic
, info
->num_dst
);
204 if (info
->num_src
!= inst
->Instruction
.NumSrcRegs
) {
205 report_error( ctx
, "%s: Invalid number of source operands, should be %u", info
->mnemonic
, info
->num_src
);
208 /* Check destination and source registers' validity.
209 * Mark the registers as used.
211 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
212 check_register_usage(
214 inst
->FullDstRegisters
[i
].DstRegister
.File
,
215 inst
->FullDstRegisters
[i
].DstRegister
.Index
,
219 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
220 check_register_usage(
222 inst
->FullSrcRegisters
[i
].SrcRegister
.File
,
223 inst
->FullSrcRegisters
[i
].SrcRegister
.Index
,
225 (boolean
)inst
->FullSrcRegisters
[i
].SrcRegister
.Indirect
);
226 if (inst
->FullSrcRegisters
[i
].SrcRegister
.Indirect
) {
230 file
= inst
->FullSrcRegisters
[i
].SrcRegisterInd
.File
;
231 index
= inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Index
;
232 check_register_usage(
238 if (!(file
== TGSI_FILE_ADDRESS
|| file
== TGSI_FILE_LOOP
) || index
!= 0) {
239 report_warning(ctx
, "Indirect register neither ADDR[0] nor LOOP[0]");
244 switch (inst
->Instruction
.Opcode
) {
245 case TGSI_OPCODE_BGNFOR
:
246 case TGSI_OPCODE_ENDFOR
:
247 if (inst
->FullDstRegisters
[0].DstRegister
.File
!= TGSI_FILE_LOOP
||
248 inst
->FullDstRegisters
[0].DstRegister
.Index
!= 0) {
249 report_error(ctx
, "Destination register must be LOOP[0]");
254 switch (inst
->Instruction
.Opcode
) {
255 case TGSI_OPCODE_BGNFOR
:
256 if (inst
->FullSrcRegisters
[0].SrcRegister
.File
!= TGSI_FILE_CONSTANT
&&
257 inst
->FullSrcRegisters
[0].SrcRegister
.File
!= TGSI_FILE_IMMEDIATE
) {
258 report_error(ctx
, "Source register file must be either CONST or IMM");
263 ctx
->num_instructions
++;
270 struct tgsi_iterate_context
*iter
,
271 struct tgsi_full_declaration
*decl
)
273 struct sanity_check_ctx
*ctx
= (struct sanity_check_ctx
*) iter
;
277 /* No declarations allowed after the first instruction.
279 if (ctx
->num_instructions
> 0)
280 report_error( ctx
, "Instruction expected but declaration found" );
282 /* Check registers' validity.
283 * Mark the registers as declared.
285 file
= decl
->Declaration
.File
;
286 if (!check_file_name( ctx
, file
))
288 for (i
= decl
->DeclarationRange
.First
; i
<= decl
->DeclarationRange
.Last
; i
++) {
289 if (is_register_declared( ctx
, file
, i
))
290 report_error( ctx
, "%s[%u]: The same register declared more than once", file_names
[file
], i
);
291 ctx
->regs_decl
[file
][i
/ BITS_IN_REG_FLAG
] |= (1 << (i
% BITS_IN_REG_FLAG
));
299 struct tgsi_iterate_context
*iter
,
300 struct tgsi_full_immediate
*imm
)
302 struct sanity_check_ctx
*ctx
= (struct sanity_check_ctx
*) iter
;
304 assert( ctx
->num_imms
< MAX_REGISTERS
);
306 /* No immediates allowed after the first instruction.
308 if (ctx
->num_instructions
> 0)
309 report_error( ctx
, "Instruction expected but immediate found" );
311 /* Mark the register as declared.
313 ctx
->regs_decl
[TGSI_FILE_IMMEDIATE
][ctx
->num_imms
/ BITS_IN_REG_FLAG
] |= (1 << (ctx
->num_imms
% BITS_IN_REG_FLAG
));
316 /* Check data type validity.
318 if (imm
->Immediate
.DataType
!= TGSI_IMM_FLOAT32
) {
319 report_error( ctx
, "(%u): Invalid immediate data type", imm
->Immediate
.DataType
);
328 struct tgsi_iterate_context
*iter
)
330 struct sanity_check_ctx
*ctx
= (struct sanity_check_ctx
*) iter
;
333 /* There must be an END instruction somewhere.
335 if (ctx
->index_of_END
== ~0) {
336 report_error( ctx
, "Missing END instruction" );
339 /* Check if all declared registers were used.
341 for (file
= TGSI_FILE_NULL
; file
< TGSI_FILE_COUNT
; file
++) {
344 for (i
= 0; i
< MAX_REGISTERS
; i
++) {
345 if (is_register_declared( ctx
, file
, i
) && !is_register_used( ctx
, file
, i
) && !ctx
->regs_ind_used
[file
]) {
346 report_warning( ctx
, "%s[%u]: Register never used", file_names
[file
], i
);
351 /* Print totals, if any.
353 if (ctx
->errors
|| ctx
->warnings
)
354 debug_printf( "%u errors, %u warnings\n", ctx
->errors
, ctx
->warnings
);
361 struct tgsi_token
*tokens
)
363 struct sanity_check_ctx ctx
;
365 ctx
.iter
.prolog
= NULL
;
366 ctx
.iter
.iterate_instruction
= iter_instruction
;
367 ctx
.iter
.iterate_declaration
= iter_declaration
;
368 ctx
.iter
.iterate_immediate
= iter_immediate
;
369 ctx
.iter
.epilog
= epilog
;
371 memset( ctx
.regs_decl
, 0, sizeof( ctx
.regs_decl
) );
372 memset( ctx
.regs_used
, 0, sizeof( ctx
.regs_used
) );
373 memset( ctx
.regs_ind_used
, 0, sizeof( ctx
.regs_ind_used
) );
375 ctx
.num_instructions
= 0;
376 ctx
.index_of_END
= ~0;
381 if (!tgsi_iterate_shader( tokens
, &ctx
.iter
))
384 return ctx
.errors
== 0;