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 "pipe/p_debug.h"
29 #include "tgsi_sanity.h"
30 #include "tgsi_info.h"
31 #include "tgsi_iterate.h"
33 #define MAX_REGISTERS 256
35 typedef uint reg_flag
;
37 #define BITS_IN_REG_FLAG (sizeof( reg_flag ) * 8)
39 struct sanity_check_ctx
41 struct tgsi_iterate_context iter
;
43 reg_flag regs_decl
[TGSI_FILE_COUNT
][MAX_REGISTERS
/ BITS_IN_REG_FLAG
];
44 reg_flag regs_used
[TGSI_FILE_COUNT
][MAX_REGISTERS
/ BITS_IN_REG_FLAG
];
45 boolean regs_ind_used
[TGSI_FILE_COUNT
];
47 uint num_instructions
;
56 struct sanity_check_ctx
*ctx
,
62 debug_printf( "Error : " );
63 va_start( args
, format
);
64 _debug_vprintf( format
, args
);
72 struct sanity_check_ctx
*ctx
,
78 debug_printf( "Warning: " );
79 va_start( args
, format
);
80 _debug_vprintf( format
, args
);
88 struct sanity_check_ctx
*ctx
,
91 if (file
<= TGSI_FILE_NULL
|| file
>= TGSI_FILE_COUNT
) {
92 report_error( ctx
, "Invalid register file name" );
100 struct sanity_check_ctx
*ctx
,
104 assert( index
>= 0 && index
< MAX_REGISTERS
);
106 return (ctx
->regs_decl
[file
][index
/ BITS_IN_REG_FLAG
] & (1 << (index
% BITS_IN_REG_FLAG
))) ? TRUE
: FALSE
;
110 is_any_register_declared(
111 struct sanity_check_ctx
*ctx
,
116 for (i
= 0; i
< MAX_REGISTERS
/ BITS_IN_REG_FLAG
; i
++)
117 if (ctx
->regs_decl
[file
][i
])
124 struct sanity_check_ctx
*ctx
,
128 assert( index
< MAX_REGISTERS
);
130 return (ctx
->regs_used
[file
][index
/ BITS_IN_REG_FLAG
] & (1 << (index
% BITS_IN_REG_FLAG
))) ? TRUE
: FALSE
;
133 static const char *file_names
[] =
146 check_register_usage(
147 struct sanity_check_ctx
*ctx
,
151 boolean indirect_access
)
153 if (!check_file_name( ctx
, file
))
156 if (index
< 0 || index
> MAX_REGISTERS
) {
157 report_error( ctx
, "%s[%i]: Invalid index %s", file_names
[file
], index
, name
);
161 if (indirect_access
) {
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 (!is_register_declared( ctx
, file
, index
))
168 report_error( ctx
, "%s[%d]: Undeclared %s register", file_names
[file
], index
, name
);
169 ctx
->regs_used
[file
][index
/ BITS_IN_REG_FLAG
] |= (1 << (index
% BITS_IN_REG_FLAG
));
176 struct tgsi_iterate_context
*iter
,
177 struct tgsi_full_instruction
*inst
)
179 struct sanity_check_ctx
*ctx
= (struct sanity_check_ctx
*) iter
;
180 const struct tgsi_opcode_info
*info
;
183 /* There must be no other instructions after END.
185 if (ctx
->index_of_END
!= ~0) {
186 report_error( ctx
, "Unexpected instruction after END" );
188 else if (inst
->Instruction
.Opcode
== TGSI_OPCODE_END
) {
189 ctx
->index_of_END
= ctx
->num_instructions
;
192 info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
194 report_error( ctx
, "Invalid instruction opcode" );
198 if (info
->num_dst
!= inst
->Instruction
.NumDstRegs
) {
199 report_error( ctx
, "Invalid number of destination operands" );
201 if (info
->num_src
!= inst
->Instruction
.NumSrcRegs
) {
202 report_error( ctx
, "Invalid number of source operands" );
205 /* Check destination and source registers' validity.
206 * Mark the registers as used.
208 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
209 check_register_usage(
211 inst
->FullDstRegisters
[i
].DstRegister
.File
,
212 inst
->FullDstRegisters
[i
].DstRegister
.Index
,
216 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
217 check_register_usage(
219 inst
->FullSrcRegisters
[i
].SrcRegister
.File
,
220 inst
->FullSrcRegisters
[i
].SrcRegister
.Index
,
222 (boolean
)inst
->FullSrcRegisters
[i
].SrcRegister
.Indirect
);
223 if (inst
->FullSrcRegisters
[i
].SrcRegister
.Indirect
) {
227 file
= inst
->FullSrcRegisters
[i
].SrcRegisterInd
.File
;
228 index
= inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Index
;
229 check_register_usage(
235 if (file
!= TGSI_FILE_ADDRESS
|| index
!= 0)
236 report_warning( ctx
, "Indirect register not ADDR[0]" );
240 ctx
->num_instructions
++;
247 struct tgsi_iterate_context
*iter
,
248 struct tgsi_full_declaration
*decl
)
250 struct sanity_check_ctx
*ctx
= (struct sanity_check_ctx
*) iter
;
254 /* No declarations allowed after the first instruction.
256 if (ctx
->num_instructions
> 0)
257 report_error( ctx
, "Instruction expected but declaration found" );
259 /* Check registers' validity.
260 * Mark the registers as declared.
262 file
= decl
->Declaration
.File
;
263 if (!check_file_name( ctx
, file
))
265 for (i
= decl
->DeclarationRange
.First
; i
<= decl
->DeclarationRange
.Last
; i
++) {
266 if (is_register_declared( ctx
, file
, i
))
267 report_error( ctx
, "The same register declared twice" );
268 ctx
->regs_decl
[file
][i
/ BITS_IN_REG_FLAG
] |= (1 << (i
% BITS_IN_REG_FLAG
));
276 struct tgsi_iterate_context
*iter
,
277 struct tgsi_full_immediate
*imm
)
279 struct sanity_check_ctx
*ctx
= (struct sanity_check_ctx
*) iter
;
281 assert( ctx
->num_imms
< MAX_REGISTERS
);
283 /* No immediates allowed after the first instruction.
285 if (ctx
->num_instructions
> 0)
286 report_error( ctx
, "Instruction expected but immediate found" );
288 /* Mark the register as declared.
290 ctx
->regs_decl
[TGSI_FILE_IMMEDIATE
][ctx
->num_imms
/ BITS_IN_REG_FLAG
] |= (1 << (ctx
->num_imms
% BITS_IN_REG_FLAG
));
293 /* Check data type validity.
295 if (imm
->Immediate
.DataType
!= TGSI_IMM_FLOAT32
) {
296 report_error( ctx
, "Invalid immediate data type" );
305 struct tgsi_iterate_context
*iter
)
307 struct sanity_check_ctx
*ctx
= (struct sanity_check_ctx
*) iter
;
310 /* There must be an END instruction at the end.
312 if (ctx
->index_of_END
== ~0 || ctx
->index_of_END
!= ctx
->num_instructions
- 1) {
313 report_error( ctx
, "Expected END at end of instruction sequence" );
316 /* Check if all declared registers were used.
318 for (file
= TGSI_FILE_NULL
; file
< TGSI_FILE_COUNT
; file
++) {
321 for (i
= 0; i
< MAX_REGISTERS
; i
++) {
322 if (is_register_declared( ctx
, file
, i
) && !is_register_used( ctx
, file
, i
) && !ctx
->regs_ind_used
[file
]) {
323 report_warning( ctx
, "Register never used" );
328 /* Print totals, if any.
330 if (ctx
->errors
|| ctx
->warnings
)
331 debug_printf( "%u errors, %u warnings\n", ctx
->errors
, ctx
->warnings
);
338 struct tgsi_token
*tokens
)
340 struct sanity_check_ctx ctx
;
342 ctx
.iter
.prolog
= NULL
;
343 ctx
.iter
.iterate_instruction
= iter_instruction
;
344 ctx
.iter
.iterate_declaration
= iter_declaration
;
345 ctx
.iter
.iterate_immediate
= iter_immediate
;
346 ctx
.iter
.epilog
= epilog
;
348 memset( ctx
.regs_decl
, 0, sizeof( ctx
.regs_decl
) );
349 memset( ctx
.regs_used
, 0, sizeof( ctx
.regs_used
) );
350 memset( ctx
.regs_ind_used
, 0, sizeof( ctx
.regs_ind_used
) );
352 ctx
.num_instructions
= 0;
353 ctx
.index_of_END
= ~0;
358 if (!tgsi_iterate_shader( tokens
, &ctx
.iter
))
361 return ctx
.errors
== 0;