1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2007 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "util/u_debug.h"
29bf215546Sopenharmony_ci#include "pipe/p_shader_tokens.h"
30bf215546Sopenharmony_ci#include "tgsi_parse.h"
31bf215546Sopenharmony_ci#include "util/u_memory.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ciunsigned
34bf215546Sopenharmony_citgsi_parse_init(
35bf215546Sopenharmony_ci   struct tgsi_parse_context *ctx,
36bf215546Sopenharmony_ci   const struct tgsi_token *tokens )
37bf215546Sopenharmony_ci{
38bf215546Sopenharmony_ci   ctx->FullHeader.Header = *(struct tgsi_header *) &tokens[0];
39bf215546Sopenharmony_ci   if (ctx->FullHeader.Header.HeaderSize >= 2) {
40bf215546Sopenharmony_ci      ctx->FullHeader.Processor = *(struct tgsi_processor *) &tokens[1];
41bf215546Sopenharmony_ci   }
42bf215546Sopenharmony_ci   else {
43bf215546Sopenharmony_ci      return TGSI_PARSE_ERROR;
44bf215546Sopenharmony_ci   }
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci   ctx->Tokens = tokens;
47bf215546Sopenharmony_ci   ctx->Position = ctx->FullHeader.Header.HeaderSize;
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   return TGSI_PARSE_OK;
50bf215546Sopenharmony_ci}
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_civoid
53bf215546Sopenharmony_citgsi_parse_free(
54bf215546Sopenharmony_ci   UNUSED struct tgsi_parse_context *ctx )
55bf215546Sopenharmony_ci{
56bf215546Sopenharmony_ci}
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ciboolean
59bf215546Sopenharmony_citgsi_parse_end_of_tokens(
60bf215546Sopenharmony_ci   struct tgsi_parse_context *ctx )
61bf215546Sopenharmony_ci{
62bf215546Sopenharmony_ci   /* All values involved are unsigned, but the sum will be promoted to
63bf215546Sopenharmony_ci    * a signed value (at least on 64 bit). To capture a possible overflow
64bf215546Sopenharmony_ci    * make it a signed comparison.
65bf215546Sopenharmony_ci    */
66bf215546Sopenharmony_ci   return (int)ctx->Position >=
67bf215546Sopenharmony_ci	 ctx->FullHeader.Header.HeaderSize + ctx->FullHeader.Header.BodySize;
68bf215546Sopenharmony_ci}
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci/**
72bf215546Sopenharmony_ci * This function is used to avoid and work-around type punning/aliasing
73bf215546Sopenharmony_ci * warnings.  The warnings seem harmless on x86 but on PPC they cause
74bf215546Sopenharmony_ci * real failures.
75bf215546Sopenharmony_ci */
76bf215546Sopenharmony_cistatic inline void
77bf215546Sopenharmony_cicopy_token(void *dst, const void *src)
78bf215546Sopenharmony_ci{
79bf215546Sopenharmony_ci   memcpy(dst, src, 4);
80bf215546Sopenharmony_ci}
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci/**
84bf215546Sopenharmony_ci * Get next 4-byte token, return it at address specified by 'token'
85bf215546Sopenharmony_ci */
86bf215546Sopenharmony_cistatic void
87bf215546Sopenharmony_cinext_token(
88bf215546Sopenharmony_ci   struct tgsi_parse_context *ctx,
89bf215546Sopenharmony_ci   void *token )
90bf215546Sopenharmony_ci{
91bf215546Sopenharmony_ci   assert( !tgsi_parse_end_of_tokens( ctx ) );
92bf215546Sopenharmony_ci   copy_token(token, &ctx->Tokens[ctx->Position]);
93bf215546Sopenharmony_ci   ctx->Position++;
94bf215546Sopenharmony_ci}
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_civoid
98bf215546Sopenharmony_citgsi_parse_token(
99bf215546Sopenharmony_ci   struct tgsi_parse_context *ctx )
100bf215546Sopenharmony_ci{
101bf215546Sopenharmony_ci   struct tgsi_token token;
102bf215546Sopenharmony_ci   unsigned i;
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   next_token( ctx, &token );
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   switch( token.Type ) {
107bf215546Sopenharmony_ci   case TGSI_TOKEN_TYPE_DECLARATION:
108bf215546Sopenharmony_ci   {
109bf215546Sopenharmony_ci      struct tgsi_full_declaration *decl = &ctx->FullToken.FullDeclaration;
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci      memset(decl, 0, sizeof *decl);
112bf215546Sopenharmony_ci      copy_token(&decl->Declaration, &token);
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci      next_token( ctx, &decl->Range );
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci      if (decl->Declaration.Dimension) {
117bf215546Sopenharmony_ci         next_token(ctx, &decl->Dim);
118bf215546Sopenharmony_ci      }
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci      if (decl->Declaration.Interpolate) {
121bf215546Sopenharmony_ci         next_token( ctx, &decl->Interp );
122bf215546Sopenharmony_ci      }
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci      if (decl->Declaration.Semantic) {
125bf215546Sopenharmony_ci         next_token( ctx, &decl->Semantic );
126bf215546Sopenharmony_ci      }
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci      if (decl->Declaration.File == TGSI_FILE_IMAGE) {
129bf215546Sopenharmony_ci         next_token(ctx, &decl->Image);
130bf215546Sopenharmony_ci      }
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci      if (decl->Declaration.File == TGSI_FILE_SAMPLER_VIEW) {
133bf215546Sopenharmony_ci         next_token(ctx, &decl->SamplerView);
134bf215546Sopenharmony_ci      }
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci      if (decl->Declaration.Array) {
137bf215546Sopenharmony_ci         next_token(ctx, &decl->Array);
138bf215546Sopenharmony_ci      }
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci      break;
141bf215546Sopenharmony_ci   }
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci   case TGSI_TOKEN_TYPE_IMMEDIATE:
144bf215546Sopenharmony_ci   {
145bf215546Sopenharmony_ci      struct tgsi_full_immediate *imm = &ctx->FullToken.FullImmediate;
146bf215546Sopenharmony_ci      uint imm_count;
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci      memset(imm, 0, sizeof *imm);
149bf215546Sopenharmony_ci      copy_token(&imm->Immediate, &token);
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci      imm_count = imm->Immediate.NrTokens - 1;
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci      switch (imm->Immediate.DataType) {
154bf215546Sopenharmony_ci      case TGSI_IMM_FLOAT32:
155bf215546Sopenharmony_ci      case TGSI_IMM_FLOAT64:
156bf215546Sopenharmony_ci         for (i = 0; i < imm_count; i++) {
157bf215546Sopenharmony_ci            next_token(ctx, &imm->u[i].Float);
158bf215546Sopenharmony_ci         }
159bf215546Sopenharmony_ci         break;
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_ci      case TGSI_IMM_UINT32:
162bf215546Sopenharmony_ci      case TGSI_IMM_UINT64:
163bf215546Sopenharmony_ci         for (i = 0; i < imm_count; i++) {
164bf215546Sopenharmony_ci            next_token(ctx, &imm->u[i].Uint);
165bf215546Sopenharmony_ci         }
166bf215546Sopenharmony_ci         break;
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci      case TGSI_IMM_INT32:
169bf215546Sopenharmony_ci      case TGSI_IMM_INT64:
170bf215546Sopenharmony_ci         for (i = 0; i < imm_count; i++) {
171bf215546Sopenharmony_ci            next_token(ctx, &imm->u[i].Int);
172bf215546Sopenharmony_ci         }
173bf215546Sopenharmony_ci         break;
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci      default:
176bf215546Sopenharmony_ci         assert( 0 );
177bf215546Sopenharmony_ci      }
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci      break;
180bf215546Sopenharmony_ci   }
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci   case TGSI_TOKEN_TYPE_INSTRUCTION:
183bf215546Sopenharmony_ci   {
184bf215546Sopenharmony_ci      struct tgsi_full_instruction *inst = &ctx->FullToken.FullInstruction;
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci      memset(inst, 0, sizeof *inst);
187bf215546Sopenharmony_ci      copy_token(&inst->Instruction, &token);
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci      if (inst->Instruction.Label) {
190bf215546Sopenharmony_ci         next_token( ctx, &inst->Label);
191bf215546Sopenharmony_ci      }
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci      if (inst->Instruction.Texture) {
194bf215546Sopenharmony_ci         next_token( ctx, &inst->Texture);
195bf215546Sopenharmony_ci         for (i = 0; i < inst->Texture.NumOffsets; i++) {
196bf215546Sopenharmony_ci            next_token( ctx, &inst->TexOffsets[i] );
197bf215546Sopenharmony_ci         }
198bf215546Sopenharmony_ci      }
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci      if (inst->Instruction.Memory) {
201bf215546Sopenharmony_ci         next_token(ctx, &inst->Memory);
202bf215546Sopenharmony_ci      }
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci      assert( inst->Instruction.NumDstRegs <= TGSI_FULL_MAX_DST_REGISTERS );
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_ci      for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ci         next_token( ctx, &inst->Dst[i].Register );
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci         if (inst->Dst[i].Register.Indirect)
211bf215546Sopenharmony_ci            next_token( ctx, &inst->Dst[i].Indirect );
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_ci         if (inst->Dst[i].Register.Dimension) {
214bf215546Sopenharmony_ci            next_token( ctx, &inst->Dst[i].Dimension );
215bf215546Sopenharmony_ci
216bf215546Sopenharmony_ci            /*
217bf215546Sopenharmony_ci             * No support for multi-dimensional addressing.
218bf215546Sopenharmony_ci             */
219bf215546Sopenharmony_ci            assert( !inst->Dst[i].Dimension.Dimension );
220bf215546Sopenharmony_ci
221bf215546Sopenharmony_ci            if (inst->Dst[i].Dimension.Indirect)
222bf215546Sopenharmony_ci               next_token( ctx, &inst->Dst[i].DimIndirect );
223bf215546Sopenharmony_ci         }
224bf215546Sopenharmony_ci      }
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_ci      assert( inst->Instruction.NumSrcRegs <= TGSI_FULL_MAX_SRC_REGISTERS );
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_ci      for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci         next_token( ctx, &inst->Src[i].Register );
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ci         if (inst->Src[i].Register.Indirect)
233bf215546Sopenharmony_ci            next_token( ctx, &inst->Src[i].Indirect );
234bf215546Sopenharmony_ci
235bf215546Sopenharmony_ci         if (inst->Src[i].Register.Dimension) {
236bf215546Sopenharmony_ci            next_token( ctx, &inst->Src[i].Dimension );
237bf215546Sopenharmony_ci
238bf215546Sopenharmony_ci            /*
239bf215546Sopenharmony_ci             * No support for multi-dimensional addressing.
240bf215546Sopenharmony_ci             */
241bf215546Sopenharmony_ci            assert( !inst->Src[i].Dimension.Dimension );
242bf215546Sopenharmony_ci
243bf215546Sopenharmony_ci            if (inst->Src[i].Dimension.Indirect)
244bf215546Sopenharmony_ci               next_token( ctx, &inst->Src[i].DimIndirect );
245bf215546Sopenharmony_ci         }
246bf215546Sopenharmony_ci      }
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_ci      break;
249bf215546Sopenharmony_ci   }
250bf215546Sopenharmony_ci
251bf215546Sopenharmony_ci   case TGSI_TOKEN_TYPE_PROPERTY:
252bf215546Sopenharmony_ci   {
253bf215546Sopenharmony_ci      struct tgsi_full_property *prop = &ctx->FullToken.FullProperty;
254bf215546Sopenharmony_ci      uint prop_count;
255bf215546Sopenharmony_ci
256bf215546Sopenharmony_ci      memset(prop, 0, sizeof *prop);
257bf215546Sopenharmony_ci      copy_token(&prop->Property, &token);
258bf215546Sopenharmony_ci
259bf215546Sopenharmony_ci      prop_count = prop->Property.NrTokens - 1;
260bf215546Sopenharmony_ci      for (i = 0; i < prop_count; i++) {
261bf215546Sopenharmony_ci         next_token(ctx, &prop->u[i]);
262bf215546Sopenharmony_ci      }
263bf215546Sopenharmony_ci
264bf215546Sopenharmony_ci      break;
265bf215546Sopenharmony_ci   }
266bf215546Sopenharmony_ci
267bf215546Sopenharmony_ci   default:
268bf215546Sopenharmony_ci      assert( 0 );
269bf215546Sopenharmony_ci   }
270bf215546Sopenharmony_ci}
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_ci
273bf215546Sopenharmony_ci
274bf215546Sopenharmony_ci
275bf215546Sopenharmony_ci/**
276bf215546Sopenharmony_ci * Make a new copy of a token array.
277bf215546Sopenharmony_ci */
278bf215546Sopenharmony_cistruct tgsi_token *
279bf215546Sopenharmony_citgsi_dup_tokens(const struct tgsi_token *tokens)
280bf215546Sopenharmony_ci{
281bf215546Sopenharmony_ci   unsigned n = tgsi_num_tokens(tokens);
282bf215546Sopenharmony_ci   unsigned bytes = n * sizeof(struct tgsi_token);
283bf215546Sopenharmony_ci   struct tgsi_token *new_tokens = (struct tgsi_token *) MALLOC(bytes);
284bf215546Sopenharmony_ci   if (new_tokens)
285bf215546Sopenharmony_ci      memcpy(new_tokens, tokens, bytes);
286bf215546Sopenharmony_ci   return new_tokens;
287bf215546Sopenharmony_ci}
288bf215546Sopenharmony_ci
289bf215546Sopenharmony_ci
290bf215546Sopenharmony_ci/**
291bf215546Sopenharmony_ci * Allocate memory for num_tokens tokens.
292bf215546Sopenharmony_ci */
293bf215546Sopenharmony_cistruct tgsi_token *
294bf215546Sopenharmony_citgsi_alloc_tokens(unsigned num_tokens)
295bf215546Sopenharmony_ci{
296bf215546Sopenharmony_ci   unsigned bytes = num_tokens * sizeof(struct tgsi_token);
297bf215546Sopenharmony_ci   return (struct tgsi_token *) MALLOC(bytes);
298bf215546Sopenharmony_ci}
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci
301bf215546Sopenharmony_ci/**
302bf215546Sopenharmony_ci * Free tokens allocated by tgsi_alloc_tokens() or tgsi_dup_tokens()
303bf215546Sopenharmony_ci */
304bf215546Sopenharmony_civoid
305bf215546Sopenharmony_citgsi_free_tokens(const struct tgsi_token *tokens)
306bf215546Sopenharmony_ci{
307bf215546Sopenharmony_ci   FREE((void *) tokens);
308bf215546Sopenharmony_ci}
309bf215546Sopenharmony_ci
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_civoid
312bf215546Sopenharmony_citgsi_dump_tokens(const struct tgsi_token *tokens)
313bf215546Sopenharmony_ci{
314bf215546Sopenharmony_ci   const unsigned *dwords = (const unsigned *)tokens;
315bf215546Sopenharmony_ci   int nr = tgsi_num_tokens(tokens);
316bf215546Sopenharmony_ci   int i;
317bf215546Sopenharmony_ci
318bf215546Sopenharmony_ci   STATIC_ASSERT(sizeof(*tokens) == sizeof(unsigned));
319bf215546Sopenharmony_ci
320bf215546Sopenharmony_ci   debug_printf("const unsigned tokens[%d] = {\n", nr);
321bf215546Sopenharmony_ci   for (i = 0; i < nr; i++)
322bf215546Sopenharmony_ci      debug_printf("0x%08x,\n", dwords[i]);
323bf215546Sopenharmony_ci   debug_printf("};\n");
324bf215546Sopenharmony_ci}
325bf215546Sopenharmony_ci
326bf215546Sopenharmony_ciunsigned
327bf215546Sopenharmony_citgsi_get_processor_type(const struct tgsi_token *tokens)
328bf215546Sopenharmony_ci{
329bf215546Sopenharmony_ci   struct tgsi_parse_context parse;
330bf215546Sopenharmony_ci
331bf215546Sopenharmony_ci   if (tgsi_parse_init( &parse, tokens ) != TGSI_PARSE_OK) {
332bf215546Sopenharmony_ci      debug_printf("tgsi_parse_init() failed in %s:%i!\n", __func__, __LINE__);
333bf215546Sopenharmony_ci      return ~0;
334bf215546Sopenharmony_ci   }
335bf215546Sopenharmony_ci   return parse.FullHeader.Processor.Processor;
336bf215546Sopenharmony_ci}
337