14a49301eSmrg/**************************************************************************
24a49301eSmrg *
3af69d88dSmrg * Copyright 2008 VMware, Inc.
44a49301eSmrg * All Rights Reserved.
54a49301eSmrg *
64a49301eSmrg * Permission is hereby granted, free of charge, to any person obtaining a
74a49301eSmrg * copy of this software and associated documentation files (the
84a49301eSmrg * "Software"), to deal in the Software without restriction, including
94a49301eSmrg * without limitation the rights to use, copy, modify, merge, publish,
104a49301eSmrg * distribute, sub license, and/or sell copies of the Software, and to
114a49301eSmrg * permit persons to whom the Software is furnished to do so, subject to
124a49301eSmrg * the following conditions:
134a49301eSmrg *
144a49301eSmrg * The above copyright notice and this permission notice (including the
154a49301eSmrg * next paragraph) shall be included in all copies or substantial portions
164a49301eSmrg * of the Software.
174a49301eSmrg *
184a49301eSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
194a49301eSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
204a49301eSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21af69d88dSmrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
224a49301eSmrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
234a49301eSmrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
244a49301eSmrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
254a49301eSmrg *
264a49301eSmrg **************************************************************************/
274a49301eSmrg
284a49301eSmrg#include "util/u_debug.h"
29cdc920a0Smrg#include "util/u_memory.h"
30cdc920a0Smrg#include "util/u_prim.h"
31cdc920a0Smrg#include "cso_cache/cso_hash.h"
324a49301eSmrg#include "tgsi_sanity.h"
334a49301eSmrg#include "tgsi_info.h"
344a49301eSmrg#include "tgsi_iterate.h"
354a49301eSmrg
363464ebd5Sriastradh
373464ebd5SriastradhDEBUG_GET_ONCE_BOOL_OPTION(print_sanity, "TGSI_PRINT_SANITY", FALSE)
383464ebd5Sriastradh
393464ebd5Sriastradh
40cdc920a0Smrgtypedef struct {
41cdc920a0Smrg   uint file : 28;
42cdc920a0Smrg   /* max 2 dimensions */
43cdc920a0Smrg   uint dimensions : 4;
44cdc920a0Smrg   uint indices[2];
45cdc920a0Smrg} scan_register;
464a49301eSmrg
474a49301eSmrgstruct sanity_check_ctx
484a49301eSmrg{
494a49301eSmrg   struct tgsi_iterate_context iter;
507ec681f3Smrg   struct cso_hash regs_decl;
517ec681f3Smrg   struct cso_hash regs_used;
527ec681f3Smrg   struct cso_hash regs_ind_used;
534a49301eSmrg
544a49301eSmrg   uint num_imms;
554a49301eSmrg   uint num_instructions;
564a49301eSmrg   uint index_of_END;
574a49301eSmrg
584a49301eSmrg   uint errors;
594a49301eSmrg   uint warnings;
60cdc920a0Smrg   uint implied_array_size;
6101e04c3fSmrg   uint implied_out_array_size;
623464ebd5Sriastradh
633464ebd5Sriastradh   boolean print;
644a49301eSmrg};
654a49301eSmrg
6601e04c3fSmrgstatic inline unsigned
67cdc920a0Smrgscan_register_key(const scan_register *reg)
68cdc920a0Smrg{
69cdc920a0Smrg   unsigned key = reg->file;
70cdc920a0Smrg   key |= (reg->indices[0] << 4);
71cdc920a0Smrg   key |= (reg->indices[1] << 18);
72cdc920a0Smrg
73cdc920a0Smrg   return key;
74cdc920a0Smrg}
75cdc920a0Smrg
76cdc920a0Smrgstatic void
77cdc920a0Smrgfill_scan_register1d(scan_register *reg,
78cdc920a0Smrg                     uint file, uint index)
79cdc920a0Smrg{
80cdc920a0Smrg   reg->file = file;
81cdc920a0Smrg   reg->dimensions = 1;
82cdc920a0Smrg   reg->indices[0] = index;
83cdc920a0Smrg   reg->indices[1] = 0;
84cdc920a0Smrg}
85cdc920a0Smrg
86cdc920a0Smrgstatic void
87cdc920a0Smrgfill_scan_register2d(scan_register *reg,
88cdc920a0Smrg                     uint file, uint index1, uint index2)
89cdc920a0Smrg{
90cdc920a0Smrg   reg->file = file;
91cdc920a0Smrg   reg->dimensions = 2;
92cdc920a0Smrg   reg->indices[0] = index1;
93cdc920a0Smrg   reg->indices[1] = index2;
94cdc920a0Smrg}
95cdc920a0Smrg
96cdc920a0Smrgstatic void
97cdc920a0Smrgscan_register_dst(scan_register *reg,
98cdc920a0Smrg                  struct tgsi_full_dst_register *dst)
99cdc920a0Smrg{
1003464ebd5Sriastradh   if (dst->Register.Dimension) {
1013464ebd5Sriastradh      /*FIXME: right now we don't support indirect
1023464ebd5Sriastradh       * multidimensional addressing */
1033464ebd5Sriastradh      fill_scan_register2d(reg,
1043464ebd5Sriastradh                           dst->Register.File,
1053464ebd5Sriastradh                           dst->Register.Index,
1063464ebd5Sriastradh                           dst->Dimension.Index);
1073464ebd5Sriastradh   } else {
1083464ebd5Sriastradh      fill_scan_register1d(reg,
1093464ebd5Sriastradh                           dst->Register.File,
1103464ebd5Sriastradh                           dst->Register.Index);
1113464ebd5Sriastradh   }
112cdc920a0Smrg}
113cdc920a0Smrg
114cdc920a0Smrgstatic void
115cdc920a0Smrgscan_register_src(scan_register *reg,
116cdc920a0Smrg                  struct tgsi_full_src_register *src)
117cdc920a0Smrg{
118cdc920a0Smrg   if (src->Register.Dimension) {
119cdc920a0Smrg      /*FIXME: right now we don't support indirect
120cdc920a0Smrg       * multidimensional addressing */
121cdc920a0Smrg      fill_scan_register2d(reg,
122cdc920a0Smrg                           src->Register.File,
123cdc920a0Smrg                           src->Register.Index,
124cdc920a0Smrg                           src->Dimension.Index);
125cdc920a0Smrg   } else {
126cdc920a0Smrg      fill_scan_register1d(reg,
127cdc920a0Smrg                           src->Register.File,
128cdc920a0Smrg                           src->Register.Index);
129cdc920a0Smrg   }
130cdc920a0Smrg}
131cdc920a0Smrg
132cdc920a0Smrgstatic scan_register *
133cdc920a0Smrgcreate_scan_register_src(struct tgsi_full_src_register *src)
134cdc920a0Smrg{
135cdc920a0Smrg   scan_register *reg = MALLOC(sizeof(scan_register));
136cdc920a0Smrg   scan_register_src(reg, src);
137cdc920a0Smrg
138cdc920a0Smrg   return reg;
139cdc920a0Smrg}
140cdc920a0Smrg
141cdc920a0Smrgstatic scan_register *
142cdc920a0Smrgcreate_scan_register_dst(struct tgsi_full_dst_register *dst)
143cdc920a0Smrg{
144cdc920a0Smrg   scan_register *reg = MALLOC(sizeof(scan_register));
145cdc920a0Smrg   scan_register_dst(reg, dst);
146cdc920a0Smrg
147cdc920a0Smrg   return reg;
148cdc920a0Smrg}
149cdc920a0Smrg
1504a49301eSmrgstatic void
1514a49301eSmrgreport_error(
1524a49301eSmrg   struct sanity_check_ctx *ctx,
1534a49301eSmrg   const char *format,
1544a49301eSmrg   ... )
1554a49301eSmrg{
1564a49301eSmrg   va_list args;
1574a49301eSmrg
1583464ebd5Sriastradh   if (!ctx->print)
1593464ebd5Sriastradh      return;
1603464ebd5Sriastradh
1614a49301eSmrg   debug_printf( "Error  : " );
1624a49301eSmrg   va_start( args, format );
1634a49301eSmrg   _debug_vprintf( format, args );
1644a49301eSmrg   va_end( args );
1654a49301eSmrg   debug_printf( "\n" );
1664a49301eSmrg   ctx->errors++;
1674a49301eSmrg}
1684a49301eSmrg
1694a49301eSmrgstatic void
1704a49301eSmrgreport_warning(
1714a49301eSmrg   struct sanity_check_ctx *ctx,
1724a49301eSmrg   const char *format,
1734a49301eSmrg   ... )
1744a49301eSmrg{
1754a49301eSmrg   va_list args;
1764a49301eSmrg
1773464ebd5Sriastradh   if (!ctx->print)
1783464ebd5Sriastradh      return;
1793464ebd5Sriastradh
1804a49301eSmrg   debug_printf( "Warning: " );
1814a49301eSmrg   va_start( args, format );
1824a49301eSmrg   _debug_vprintf( format, args );
1834a49301eSmrg   va_end( args );
1844a49301eSmrg   debug_printf( "\n" );
1854a49301eSmrg   ctx->warnings++;
1864a49301eSmrg}
1874a49301eSmrg
1884a49301eSmrgstatic boolean
1894a49301eSmrgcheck_file_name(
1904a49301eSmrg   struct sanity_check_ctx *ctx,
1914a49301eSmrg   uint file )
1924a49301eSmrg{
1934a49301eSmrg   if (file <= TGSI_FILE_NULL || file >= TGSI_FILE_COUNT) {
1944a49301eSmrg      report_error( ctx, "(%u): Invalid register file name", file );
1954a49301eSmrg      return FALSE;
1964a49301eSmrg   }
1974a49301eSmrg   return TRUE;
1984a49301eSmrg}
1994a49301eSmrg
2004a49301eSmrgstatic boolean
2014a49301eSmrgis_register_declared(
2024a49301eSmrg   struct sanity_check_ctx *ctx,
203cdc920a0Smrg   const scan_register *reg)
2044a49301eSmrg{
205cdc920a0Smrg   void *data = cso_hash_find_data_from_template(
2067ec681f3Smrg      &ctx->regs_decl, scan_register_key(reg),
207cdc920a0Smrg      (void*)reg, sizeof(scan_register));
208cdc920a0Smrg   return  data ? TRUE : FALSE;
2094a49301eSmrg}
2104a49301eSmrg
2114a49301eSmrgstatic boolean
2124a49301eSmrgis_any_register_declared(
2134a49301eSmrg   struct sanity_check_ctx *ctx,
2144a49301eSmrg   uint file )
2154a49301eSmrg{
216cdc920a0Smrg   struct cso_hash_iter iter =
2177ec681f3Smrg      cso_hash_first_node(&ctx->regs_decl);
2184a49301eSmrg
219cdc920a0Smrg   while (!cso_hash_iter_is_null(iter)) {
220cdc920a0Smrg      scan_register *reg = (scan_register *)cso_hash_iter_data(iter);
221cdc920a0Smrg      if (reg->file == file)
2224a49301eSmrg         return TRUE;
223cdc920a0Smrg      iter = cso_hash_iter_next(iter);
224cdc920a0Smrg   }
225cdc920a0Smrg
2264a49301eSmrg   return FALSE;
2274a49301eSmrg}
2284a49301eSmrg
2294a49301eSmrgstatic boolean
2304a49301eSmrgis_register_used(
2314a49301eSmrg   struct sanity_check_ctx *ctx,
232cdc920a0Smrg   scan_register *reg)
2334a49301eSmrg{
234cdc920a0Smrg   void *data = cso_hash_find_data_from_template(
2357ec681f3Smrg      &ctx->regs_used, scan_register_key(reg),
236cdc920a0Smrg      reg, sizeof(scan_register));
237cdc920a0Smrg   return  data ? TRUE : FALSE;
238cdc920a0Smrg}
239cdc920a0Smrg
2404a49301eSmrg
241cdc920a0Smrgstatic boolean
242cdc920a0Smrgis_ind_register_used(
243cdc920a0Smrg   struct sanity_check_ctx *ctx,
244cdc920a0Smrg   scan_register *reg)
245cdc920a0Smrg{
2467ec681f3Smrg   return cso_hash_contains(&ctx->regs_ind_used, reg->file);
2474a49301eSmrg}
2484a49301eSmrg
2494a49301eSmrgstatic const char *file_names[TGSI_FILE_COUNT] =
2504a49301eSmrg{
2514a49301eSmrg   "NULL",
2524a49301eSmrg   "CONST",
2534a49301eSmrg   "IN",
2544a49301eSmrg   "OUT",
2554a49301eSmrg   "TEMP",
2564a49301eSmrg   "SAMP",
2574a49301eSmrg   "ADDR",
2584a49301eSmrg   "IMM",
2593464ebd5Sriastradh   "SV",
2603464ebd5Sriastradh   "RES"
2614a49301eSmrg};
2624a49301eSmrg
2634a49301eSmrgstatic boolean
2644a49301eSmrgcheck_register_usage(
2654a49301eSmrg   struct sanity_check_ctx *ctx,
266cdc920a0Smrg   scan_register *reg,
2674a49301eSmrg   const char *name,
2684a49301eSmrg   boolean indirect_access )
2694a49301eSmrg{
270cdc920a0Smrg   if (!check_file_name( ctx, reg->file )) {
271cdc920a0Smrg      FREE(reg);
2724a49301eSmrg      return FALSE;
273cdc920a0Smrg   }
2744a49301eSmrg
2754a49301eSmrg   if (indirect_access) {
2764a49301eSmrg      /* Note that 'index' is an offset relative to the value of the
277cdc920a0Smrg       * address register.  No range checking done here.*/
278cdc920a0Smrg      reg->indices[0] = 0;
279cdc920a0Smrg      reg->indices[1] = 0;
280cdc920a0Smrg      if (!is_any_register_declared( ctx, reg->file ))
281cdc920a0Smrg         report_error( ctx, "%s: Undeclared %s register", file_names[reg->file], name );
282cdc920a0Smrg      if (!is_ind_register_used(ctx, reg))
2837ec681f3Smrg         cso_hash_insert(&ctx->regs_ind_used, reg->file, reg);
284cdc920a0Smrg      else
285cdc920a0Smrg         FREE(reg);
2864a49301eSmrg   }
2874a49301eSmrg   else {
288cdc920a0Smrg      if (!is_register_declared( ctx, reg )) {
289cdc920a0Smrg         if (reg->dimensions == 2) {
290cdc920a0Smrg            report_error( ctx, "%s[%d][%d]: Undeclared %s register", file_names[reg->file],
291cdc920a0Smrg                          reg->indices[0], reg->indices[1], name );
292cdc920a0Smrg         }
293cdc920a0Smrg         else {
294cdc920a0Smrg            report_error( ctx, "%s[%d]: Undeclared %s register", file_names[reg->file],
295cdc920a0Smrg                          reg->indices[0], name );
296cdc920a0Smrg         }
2974a49301eSmrg      }
298cdc920a0Smrg      if (!is_register_used( ctx, reg ))
2997ec681f3Smrg         cso_hash_insert(&ctx->regs_used, scan_register_key(reg), reg);
300cdc920a0Smrg      else
301cdc920a0Smrg         FREE(reg);
3024a49301eSmrg   }
3034a49301eSmrg   return TRUE;
3044a49301eSmrg}
3054a49301eSmrg
3064a49301eSmrgstatic boolean
3074a49301eSmrgiter_instruction(
3084a49301eSmrg   struct tgsi_iterate_context *iter,
3094a49301eSmrg   struct tgsi_full_instruction *inst )
3104a49301eSmrg{
3114a49301eSmrg   struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
3124a49301eSmrg   const struct tgsi_opcode_info *info;
3134a49301eSmrg   uint i;
3144a49301eSmrg
3154a49301eSmrg   if (inst->Instruction.Opcode == TGSI_OPCODE_END) {
31601e04c3fSmrg      if (ctx->index_of_END != ~0u) {
3174a49301eSmrg         report_error( ctx, "Too many END instructions" );
3184a49301eSmrg      }
3194a49301eSmrg      ctx->index_of_END = ctx->num_instructions;
3204a49301eSmrg   }
3214a49301eSmrg
3224a49301eSmrg   info = tgsi_get_opcode_info( inst->Instruction.Opcode );
32301e04c3fSmrg   if (!info) {
3244a49301eSmrg      report_error( ctx, "(%u): Invalid instruction opcode", inst->Instruction.Opcode );
3254a49301eSmrg      return TRUE;
3264a49301eSmrg   }
3274a49301eSmrg
3284a49301eSmrg   if (info->num_dst != inst->Instruction.NumDstRegs) {
32901e04c3fSmrg      report_error( ctx, "%s: Invalid number of destination operands, should be %u",
33001e04c3fSmrg                    tgsi_get_opcode_name(inst->Instruction.Opcode), info->num_dst );
3314a49301eSmrg   }
3324a49301eSmrg   if (info->num_src != inst->Instruction.NumSrcRegs) {
33301e04c3fSmrg      report_error( ctx, "%s: Invalid number of source operands, should be %u",
33401e04c3fSmrg                    tgsi_get_opcode_name(inst->Instruction.Opcode), info->num_src );
3354a49301eSmrg   }
3364a49301eSmrg
3374a49301eSmrg   /* Check destination and source registers' validity.
3384a49301eSmrg    * Mark the registers as used.
3394a49301eSmrg    */
3404a49301eSmrg   for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
341cdc920a0Smrg      scan_register *reg = create_scan_register_dst(&inst->Dst[i]);
3424a49301eSmrg      check_register_usage(
3434a49301eSmrg         ctx,
344cdc920a0Smrg         reg,
3454a49301eSmrg         "destination",
3464a49301eSmrg         FALSE );
347cdc920a0Smrg      if (!inst->Dst[i].Register.WriteMask) {
3484a49301eSmrg         report_error(ctx, "Destination register has empty writemask");
3494a49301eSmrg      }
3504a49301eSmrg   }
3514a49301eSmrg   for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
352cdc920a0Smrg      scan_register *reg = create_scan_register_src(&inst->Src[i]);
3534a49301eSmrg      check_register_usage(
3544a49301eSmrg         ctx,
355cdc920a0Smrg         reg,
3564a49301eSmrg         "source",
357cdc920a0Smrg         (boolean)inst->Src[i].Register.Indirect );
358cdc920a0Smrg      if (inst->Src[i].Register.Indirect) {
359cdc920a0Smrg         scan_register *ind_reg = MALLOC(sizeof(scan_register));
3604a49301eSmrg
361cdc920a0Smrg         fill_scan_register1d(ind_reg,
362cdc920a0Smrg                              inst->Src[i].Indirect.File,
363cdc920a0Smrg                              inst->Src[i].Indirect.Index);
3644a49301eSmrg         check_register_usage(
3654a49301eSmrg            ctx,
366cdc920a0Smrg            ind_reg,
3674a49301eSmrg            "indirect",
3684a49301eSmrg            FALSE );
3694a49301eSmrg      }
3704a49301eSmrg   }
3714a49301eSmrg
3724a49301eSmrg   ctx->num_instructions++;
3734a49301eSmrg
3744a49301eSmrg   return TRUE;
3754a49301eSmrg}
3764a49301eSmrg
377cdc920a0Smrgstatic void
378cdc920a0Smrgcheck_and_declare(struct sanity_check_ctx *ctx,
379cdc920a0Smrg                  scan_register *reg)
380cdc920a0Smrg{
381cdc920a0Smrg   if (is_register_declared( ctx, reg))
382cdc920a0Smrg      report_error( ctx, "%s[%u]: The same register declared more than once",
383cdc920a0Smrg                    file_names[reg->file], reg->indices[0] );
3847ec681f3Smrg   cso_hash_insert(&ctx->regs_decl,
385cdc920a0Smrg                   scan_register_key(reg),
386cdc920a0Smrg                   reg);
387cdc920a0Smrg}
388cdc920a0Smrg
389cdc920a0Smrg
3904a49301eSmrgstatic boolean
3914a49301eSmrgiter_declaration(
3924a49301eSmrg   struct tgsi_iterate_context *iter,
3934a49301eSmrg   struct tgsi_full_declaration *decl )
3944a49301eSmrg{
3954a49301eSmrg   struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
3964a49301eSmrg   uint file;
3974a49301eSmrg   uint i;
3984a49301eSmrg
3994a49301eSmrg   /* No declarations allowed after the first instruction.
4004a49301eSmrg    */
4014a49301eSmrg   if (ctx->num_instructions > 0)
4024a49301eSmrg      report_error( ctx, "Instruction expected but declaration found" );
4034a49301eSmrg
4044a49301eSmrg   /* Check registers' validity.
4054a49301eSmrg    * Mark the registers as declared.
4064a49301eSmrg    */
4074a49301eSmrg   file = decl->Declaration.File;
4084a49301eSmrg   if (!check_file_name( ctx, file ))
4094a49301eSmrg      return TRUE;
410cdc920a0Smrg   for (i = decl->Range.First; i <= decl->Range.Last; i++) {
41101e04c3fSmrg      /* declared TGSI_FILE_INPUT's for geometry and tessellation
412cdc920a0Smrg       * have an implied second dimension */
41301e04c3fSmrg      uint processor = ctx->iter.processor.Processor;
41401e04c3fSmrg      uint patch = decl->Semantic.Name == TGSI_SEMANTIC_PATCH ||
41501e04c3fSmrg         decl->Semantic.Name == TGSI_SEMANTIC_TESSOUTER ||
41601e04c3fSmrg         decl->Semantic.Name == TGSI_SEMANTIC_TESSINNER;
41701e04c3fSmrg      if (file == TGSI_FILE_INPUT && !patch && (
41801e04c3fSmrg                processor == PIPE_SHADER_GEOMETRY ||
41901e04c3fSmrg                processor == PIPE_SHADER_TESS_CTRL ||
42001e04c3fSmrg                processor == PIPE_SHADER_TESS_EVAL)) {
421cdc920a0Smrg         uint vert;
422cdc920a0Smrg         for (vert = 0; vert < ctx->implied_array_size; ++vert) {
423cdc920a0Smrg            scan_register *reg = MALLOC(sizeof(scan_register));
424cdc920a0Smrg            fill_scan_register2d(reg, file, i, vert);
425cdc920a0Smrg            check_and_declare(ctx, reg);
426cdc920a0Smrg         }
42701e04c3fSmrg      } else if (file == TGSI_FILE_OUTPUT && !patch &&
42801e04c3fSmrg                 processor == PIPE_SHADER_TESS_CTRL) {
42901e04c3fSmrg         uint vert;
43001e04c3fSmrg         for (vert = 0; vert < ctx->implied_out_array_size; ++vert) {
43101e04c3fSmrg            scan_register *reg = MALLOC(sizeof(scan_register));
43201e04c3fSmrg            fill_scan_register2d(reg, file, i, vert);
43301e04c3fSmrg            check_and_declare(ctx, reg);
43401e04c3fSmrg         }
435cdc920a0Smrg      } else {
436cdc920a0Smrg         scan_register *reg = MALLOC(sizeof(scan_register));
437cdc920a0Smrg         if (decl->Declaration.Dimension) {
438cdc920a0Smrg            fill_scan_register2d(reg, file, i, decl->Dim.Index2D);
439cdc920a0Smrg         } else {
440cdc920a0Smrg            fill_scan_register1d(reg, file, i);
441cdc920a0Smrg         }
442cdc920a0Smrg         check_and_declare(ctx, reg);
443cdc920a0Smrg      }
4444a49301eSmrg   }
4454a49301eSmrg
4464a49301eSmrg   return TRUE;
4474a49301eSmrg}
4484a49301eSmrg
4494a49301eSmrgstatic boolean
4504a49301eSmrgiter_immediate(
4514a49301eSmrg   struct tgsi_iterate_context *iter,
4524a49301eSmrg   struct tgsi_full_immediate *imm )
4534a49301eSmrg{
4544a49301eSmrg   struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
455cdc920a0Smrg   scan_register *reg;
4564a49301eSmrg
4574a49301eSmrg   /* No immediates allowed after the first instruction.
4584a49301eSmrg    */
4594a49301eSmrg   if (ctx->num_instructions > 0)
4604a49301eSmrg      report_error( ctx, "Instruction expected but immediate found" );
4614a49301eSmrg
4624a49301eSmrg   /* Mark the register as declared.
4634a49301eSmrg    */
464cdc920a0Smrg   reg = MALLOC(sizeof(scan_register));
465cdc920a0Smrg   fill_scan_register1d(reg, TGSI_FILE_IMMEDIATE, ctx->num_imms);
4667ec681f3Smrg   cso_hash_insert(&ctx->regs_decl, scan_register_key(reg), reg);
4674a49301eSmrg   ctx->num_imms++;
4684a49301eSmrg
4694a49301eSmrg   /* Check data type validity.
4704a49301eSmrg    */
471cdc920a0Smrg   if (imm->Immediate.DataType != TGSI_IMM_FLOAT32 &&
472cdc920a0Smrg       imm->Immediate.DataType != TGSI_IMM_UINT32 &&
473cdc920a0Smrg       imm->Immediate.DataType != TGSI_IMM_INT32) {
4744a49301eSmrg      report_error( ctx, "(%u): Invalid immediate data type", imm->Immediate.DataType );
4754a49301eSmrg      return TRUE;
4764a49301eSmrg   }
4774a49301eSmrg
4784a49301eSmrg   return TRUE;
4794a49301eSmrg}
4804a49301eSmrg
481cdc920a0Smrg
482cdc920a0Smrgstatic boolean
483cdc920a0Smrgiter_property(
484cdc920a0Smrg   struct tgsi_iterate_context *iter,
485cdc920a0Smrg   struct tgsi_full_property *prop )
486cdc920a0Smrg{
487cdc920a0Smrg   struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
488cdc920a0Smrg
48901e04c3fSmrg   if (iter->processor.Processor == PIPE_SHADER_GEOMETRY &&
490cdc920a0Smrg       prop->Property.PropertyName == TGSI_PROPERTY_GS_INPUT_PRIM) {
491cdc920a0Smrg      ctx->implied_array_size = u_vertices_per_prim(prop->u[0].Data);
492cdc920a0Smrg   }
49301e04c3fSmrg   if (iter->processor.Processor == PIPE_SHADER_TESS_CTRL &&
49401e04c3fSmrg       prop->Property.PropertyName == TGSI_PROPERTY_TCS_VERTICES_OUT)
49501e04c3fSmrg      ctx->implied_out_array_size = prop->u[0].Data;
49601e04c3fSmrg   return TRUE;
49701e04c3fSmrg}
49801e04c3fSmrg
49901e04c3fSmrgstatic boolean
50001e04c3fSmrgprolog(struct tgsi_iterate_context *iter)
50101e04c3fSmrg{
50201e04c3fSmrg   struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
50301e04c3fSmrg   if (iter->processor.Processor == PIPE_SHADER_TESS_CTRL ||
50401e04c3fSmrg       iter->processor.Processor == PIPE_SHADER_TESS_EVAL)
50501e04c3fSmrg      ctx->implied_array_size = 32;
506cdc920a0Smrg   return TRUE;
507cdc920a0Smrg}
508cdc920a0Smrg
5094a49301eSmrgstatic boolean
5104a49301eSmrgepilog(
5114a49301eSmrg   struct tgsi_iterate_context *iter )
5124a49301eSmrg{
5134a49301eSmrg   struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
5144a49301eSmrg
5154a49301eSmrg   /* There must be an END instruction somewhere.
5164a49301eSmrg    */
51701e04c3fSmrg   if (ctx->index_of_END == ~0u) {
5184a49301eSmrg      report_error( ctx, "Missing END instruction" );
5194a49301eSmrg   }
5204a49301eSmrg
5214a49301eSmrg   /* Check if all declared registers were used.
5224a49301eSmrg    */
523cdc920a0Smrg   {
524cdc920a0Smrg      struct cso_hash_iter iter =
5257ec681f3Smrg         cso_hash_first_node(&ctx->regs_decl);
526cdc920a0Smrg
527cdc920a0Smrg      while (!cso_hash_iter_is_null(iter)) {
528cdc920a0Smrg         scan_register *reg = (scan_register *)cso_hash_iter_data(iter);
529cdc920a0Smrg         if (!is_register_used(ctx, reg) && !is_ind_register_used(ctx, reg)) {
530cdc920a0Smrg            report_warning( ctx, "%s[%u]: Register never used",
531cdc920a0Smrg                            file_names[reg->file], reg->indices[0] );
5324a49301eSmrg         }
533cdc920a0Smrg         iter = cso_hash_iter_next(iter);
5344a49301eSmrg      }
5354a49301eSmrg   }
5364a49301eSmrg
5374a49301eSmrg   /* Print totals, if any.
5384a49301eSmrg    */
5394a49301eSmrg   if (ctx->errors || ctx->warnings)
5404a49301eSmrg      debug_printf( "%u errors, %u warnings\n", ctx->errors, ctx->warnings );
5414a49301eSmrg
5424a49301eSmrg   return TRUE;
5434a49301eSmrg}
5444a49301eSmrg
545cdc920a0Smrgstatic void
546cdc920a0Smrgregs_hash_destroy(struct cso_hash *hash)
547cdc920a0Smrg{
548cdc920a0Smrg   struct cso_hash_iter iter = cso_hash_first_node(hash);
549cdc920a0Smrg   while (!cso_hash_iter_is_null(iter)) {
550cdc920a0Smrg      scan_register *reg = (scan_register *)cso_hash_iter_data(iter);
551cdc920a0Smrg      iter = cso_hash_erase(hash, iter);
552cdc920a0Smrg      assert(reg->file < TGSI_FILE_COUNT);
553cdc920a0Smrg      FREE(reg);
554cdc920a0Smrg   }
5557ec681f3Smrg   cso_hash_deinit(hash);
556cdc920a0Smrg}
557cdc920a0Smrg
5584a49301eSmrgboolean
5594a49301eSmrgtgsi_sanity_check(
5604a49301eSmrg   const struct tgsi_token *tokens )
5614a49301eSmrg{
5624a49301eSmrg   struct sanity_check_ctx ctx;
56301e04c3fSmrg   boolean retval;
5644a49301eSmrg
56501e04c3fSmrg   ctx.iter.prolog = prolog;
5664a49301eSmrg   ctx.iter.iterate_instruction = iter_instruction;
5674a49301eSmrg   ctx.iter.iterate_declaration = iter_declaration;
5684a49301eSmrg   ctx.iter.iterate_immediate = iter_immediate;
569cdc920a0Smrg   ctx.iter.iterate_property = iter_property;
5704a49301eSmrg   ctx.iter.epilog = epilog;
5714a49301eSmrg
5727ec681f3Smrg   cso_hash_init(&ctx.regs_decl);
5737ec681f3Smrg   cso_hash_init(&ctx.regs_used);
5747ec681f3Smrg   cso_hash_init(&ctx.regs_ind_used);
575cdc920a0Smrg
5764a49301eSmrg   ctx.num_imms = 0;
5774a49301eSmrg   ctx.num_instructions = 0;
5784a49301eSmrg   ctx.index_of_END = ~0;
5794a49301eSmrg
5804a49301eSmrg   ctx.errors = 0;
5814a49301eSmrg   ctx.warnings = 0;
582cdc920a0Smrg   ctx.implied_array_size = 0;
5833464ebd5Sriastradh   ctx.print = debug_get_option_print_sanity();
5844a49301eSmrg
58501e04c3fSmrg   retval = tgsi_iterate_shader( tokens, &ctx.iter );
5867ec681f3Smrg   regs_hash_destroy(&ctx.regs_decl);
5877ec681f3Smrg   regs_hash_destroy(&ctx.regs_used);
5887ec681f3Smrg   regs_hash_destroy(&ctx.regs_ind_used);
58901e04c3fSmrg   if (retval == FALSE)
59001e04c3fSmrg      return FALSE;
59101e04c3fSmrg
5924a49301eSmrg   return ctx.errors == 0;
5934a49301eSmrg}
594