tgsi_sanity.c revision 4b929b32d03a58d80cacbd63c172dbd7221c8a8f
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 "pipe/p_debug.h" 29#include "tgsi_sanity.h" 30#include "tgsi_info.h" 31#include "tgsi_iterate.h" 32 33#define MAX_REGISTERS 256 34 35typedef uint reg_flag; 36 37#define BITS_IN_REG_FLAG (sizeof( reg_flag ) * 8) 38 39struct sanity_check_ctx 40{ 41 struct tgsi_iterate_context iter; 42 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]; 46 uint num_imms; 47 uint num_instructions; 48 uint index_of_END; 49 50 uint errors; 51 uint warnings; 52}; 53 54static void 55report_error( 56 struct sanity_check_ctx *ctx, 57 const char *format, 58 ... ) 59{ 60 va_list args; 61 62 debug_printf( "Error : " ); 63 va_start( args, format ); 64 _debug_vprintf( format, args ); 65 va_end( args ); 66 debug_printf( "\n" ); 67 ctx->errors++; 68} 69 70static void 71report_warning( 72 struct sanity_check_ctx *ctx, 73 const char *format, 74 ... ) 75{ 76 va_list args; 77 78 debug_printf( "Warning: " ); 79 va_start( args, format ); 80 _debug_vprintf( format, args ); 81 va_end( args ); 82 debug_printf( "\n" ); 83 ctx->warnings++; 84} 85 86static boolean 87check_file_name( 88 struct sanity_check_ctx *ctx, 89 uint file ) 90{ 91 if (file <= TGSI_FILE_NULL || file >= TGSI_FILE_COUNT) { 92 report_error( ctx, "Invalid register file name" ); 93 return FALSE; 94 } 95 return TRUE; 96} 97 98static boolean 99is_register_declared( 100 struct sanity_check_ctx *ctx, 101 uint file, 102 int index ) 103{ 104 assert( index >= 0 && index < MAX_REGISTERS ); 105 106 return (ctx->regs_decl[file][index / BITS_IN_REG_FLAG] & (1 << (index % BITS_IN_REG_FLAG))) ? TRUE : FALSE; 107} 108 109static boolean 110is_any_register_declared( 111 struct sanity_check_ctx *ctx, 112 uint file ) 113{ 114 uint i; 115 116 for (i = 0; i < MAX_REGISTERS / BITS_IN_REG_FLAG; i++) 117 if (ctx->regs_decl[file][i]) 118 return TRUE; 119 return FALSE; 120} 121 122static boolean 123is_register_used( 124 struct sanity_check_ctx *ctx, 125 uint file, 126 int index ) 127{ 128 assert( index < MAX_REGISTERS ); 129 130 return (ctx->regs_used[file][index / BITS_IN_REG_FLAG] & (1 << (index % BITS_IN_REG_FLAG))) ? TRUE : FALSE; 131} 132 133static const char *file_names[] = 134{ 135 "NULL", 136 "CONST", 137 "IN", 138 "OUT", 139 "TEMP", 140 "SAMP", 141 "ADDR", 142 "IMM" 143}; 144 145static boolean 146check_register_usage( 147 struct sanity_check_ctx *ctx, 148 uint file, 149 int index, 150 const char *name, 151 boolean indirect_access ) 152{ 153 if (!check_file_name( ctx, file )) 154 return FALSE; 155 if (indirect_access) { 156 if (!is_any_register_declared( ctx, file )) 157 report_error( ctx, "%s: Undeclared %s register", file_names[file], name ); 158 ctx->regs_ind_used[file] = TRUE; 159 } 160 else { 161 if (!is_register_declared( ctx, file, index )) 162 report_error( ctx, "%s[%d]: Undeclared %s register", file_names[file], index, name ); 163 ctx->regs_used[file][index / BITS_IN_REG_FLAG] |= (1 << (index % BITS_IN_REG_FLAG)); 164 } 165 return TRUE; 166} 167 168static boolean 169iter_instruction( 170 struct tgsi_iterate_context *iter, 171 struct tgsi_full_instruction *inst ) 172{ 173 struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter; 174 const struct tgsi_opcode_info *info; 175 uint i; 176 177 /* There must be no other instructions after END. 178 */ 179 if (ctx->index_of_END != ~0) { 180 report_error( ctx, "Unexpected instruction after END" ); 181 } 182 else if (inst->Instruction.Opcode == TGSI_OPCODE_END) { 183 ctx->index_of_END = ctx->num_instructions; 184 } 185 186 info = tgsi_get_opcode_info( inst->Instruction.Opcode ); 187 if (info == NULL) { 188 report_error( ctx, "Invalid instruction opcode" ); 189 return TRUE; 190 } 191 192 if (info->num_dst != inst->Instruction.NumDstRegs) { 193 report_error( ctx, "Invalid number of destination operands" ); 194 } 195 if (info->num_src != inst->Instruction.NumSrcRegs) { 196 report_error( ctx, "Invalid number of source operands" ); 197 } 198 199 /* Check destination and source registers' validity. 200 * Mark the registers as used. 201 */ 202 for (i = 0; i < inst->Instruction.NumDstRegs; i++) { 203 check_register_usage( 204 ctx, 205 inst->FullDstRegisters[i].DstRegister.File, 206 inst->FullDstRegisters[i].DstRegister.Index, 207 "destination", 208 FALSE ); 209 } 210 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) { 211 check_register_usage( 212 ctx, 213 inst->FullSrcRegisters[i].SrcRegister.File, 214 inst->FullSrcRegisters[i].SrcRegister.Index, 215 "source", 216 (boolean)inst->FullSrcRegisters[i].SrcRegister.Indirect ); 217 if (inst->FullSrcRegisters[i].SrcRegister.Indirect) { 218 uint file; 219 int index; 220 221 file = inst->FullSrcRegisters[i].SrcRegisterInd.File; 222 index = inst->FullSrcRegisters[i].SrcRegisterInd.Index; 223 check_register_usage( 224 ctx, 225 file, 226 index, 227 "indirect", 228 FALSE ); 229 if (file != TGSI_FILE_ADDRESS || index != 0) 230 report_warning( ctx, "Indirect register not ADDR[0]" ); 231 } 232 } 233 234 ctx->num_instructions++; 235 236 return TRUE; 237} 238 239static boolean 240iter_declaration( 241 struct tgsi_iterate_context *iter, 242 struct tgsi_full_declaration *decl ) 243{ 244 struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter; 245 uint file; 246 uint i; 247 248 /* No declarations allowed after the first instruction. 249 */ 250 if (ctx->num_instructions > 0) 251 report_error( ctx, "Instruction expected but declaration found" ); 252 253 /* Check registers' validity. 254 * Mark the registers as declared. 255 */ 256 file = decl->Declaration.File; 257 if (!check_file_name( ctx, file )) 258 return TRUE; 259 for (i = decl->DeclarationRange.First; i <= decl->DeclarationRange.Last; i++) { 260 if (is_register_declared( ctx, file, i )) 261 report_error( ctx, "The same register declared twice" ); 262 ctx->regs_decl[file][i / BITS_IN_REG_FLAG] |= (1 << (i % BITS_IN_REG_FLAG)); 263 } 264 265 return TRUE; 266} 267 268static boolean 269iter_immediate( 270 struct tgsi_iterate_context *iter, 271 struct tgsi_full_immediate *imm ) 272{ 273 struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter; 274 275 assert( ctx->num_imms < MAX_REGISTERS ); 276 277 /* No immediates allowed after the first instruction. 278 */ 279 if (ctx->num_instructions > 0) 280 report_error( ctx, "Instruction expected but immediate found" ); 281 282 /* Mark the register as declared. 283 */ 284 ctx->regs_decl[TGSI_FILE_IMMEDIATE][ctx->num_imms / BITS_IN_REG_FLAG] |= (1 << (ctx->num_imms % BITS_IN_REG_FLAG)); 285 ctx->num_imms++; 286 287 /* Check data type validity. 288 */ 289 if (imm->Immediate.DataType != TGSI_IMM_FLOAT32) { 290 report_error( ctx, "Invalid immediate data type" ); 291 return TRUE; 292 } 293 294 return TRUE; 295} 296 297static boolean 298epilog( 299 struct tgsi_iterate_context *iter ) 300{ 301 struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter; 302 uint file; 303 304 /* There must be an END instruction at the end. 305 */ 306 if (ctx->index_of_END == ~0 || ctx->index_of_END != ctx->num_instructions - 1) { 307 report_error( ctx, "Expected END at end of instruction sequence" ); 308 } 309 310 /* Check if all declared registers were used. 311 */ 312 for (file = TGSI_FILE_NULL; file < TGSI_FILE_COUNT; file++) { 313 uint i; 314 315 for (i = 0; i < MAX_REGISTERS; i++) { 316 if (is_register_declared( ctx, file, i ) && !is_register_used( ctx, file, i ) && !ctx->regs_ind_used[file]) { 317 report_warning( ctx, "Register never used" ); 318 } 319 } 320 } 321 322 /* Print totals, if any. 323 */ 324 if (ctx->errors || ctx->warnings) 325 debug_printf( "\n%u errors, %u warnings", ctx->errors, ctx->warnings ); 326 327 return TRUE; 328} 329 330boolean 331tgsi_sanity_check( 332 struct tgsi_token *tokens ) 333{ 334 struct sanity_check_ctx ctx; 335 336 ctx.iter.prolog = NULL; 337 ctx.iter.iterate_instruction = iter_instruction; 338 ctx.iter.iterate_declaration = iter_declaration; 339 ctx.iter.iterate_immediate = iter_immediate; 340 ctx.iter.epilog = epilog; 341 342 memset( ctx.regs_decl, 0, sizeof( ctx.regs_decl ) ); 343 memset( ctx.regs_used, 0, sizeof( ctx.regs_used ) ); 344 memset( ctx.regs_ind_used, 0, sizeof( ctx.regs_ind_used ) ); 345 ctx.num_imms = 0; 346 ctx.num_instructions = 0; 347 ctx.index_of_END = ~0; 348 349 ctx.errors = 0; 350 ctx.warnings = 0; 351 352 if (!tgsi_iterate_shader( tokens, &ctx.iter )) 353 return FALSE; 354 355 return ctx.errors == 0; 356} 357