tgsi_parse.c revision 4a49301e
1/**************************************************************************
2 *
3 * Copyright 2007 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 "util/u_debug.h"
29#include "pipe/p_shader_tokens.h"
30#include "tgsi_parse.h"
31#include "tgsi_build.h"
32#include "util/u_memory.h"
33
34void
35tgsi_full_token_init(
36   union tgsi_full_token *full_token )
37{
38   full_token->Token.Type = TGSI_TOKEN_TYPE_DECLARATION;
39}
40
41void
42tgsi_full_token_free(
43   union tgsi_full_token *full_token )
44{
45}
46
47unsigned
48tgsi_parse_init(
49   struct tgsi_parse_context *ctx,
50   const struct tgsi_token *tokens )
51{
52   ctx->FullVersion.Version = *(struct tgsi_version *) &tokens[0];
53   if( ctx->FullVersion.Version.MajorVersion > 1 ) {
54      return TGSI_PARSE_ERROR;
55   }
56
57   ctx->FullHeader.Header = *(struct tgsi_header *) &tokens[1];
58   if( ctx->FullHeader.Header.HeaderSize >= 2 ) {
59      ctx->FullHeader.Processor = *(struct tgsi_processor *) &tokens[2];
60   }
61   else {
62      ctx->FullHeader.Processor = tgsi_default_processor();
63   }
64
65   ctx->Tokens = tokens;
66   ctx->Position = 1 + ctx->FullHeader.Header.HeaderSize;
67
68   tgsi_full_token_init( &ctx->FullToken );
69
70   return TGSI_PARSE_OK;
71}
72
73void
74tgsi_parse_free(
75   struct tgsi_parse_context *ctx )
76{
77   tgsi_full_token_free( &ctx->FullToken );
78}
79
80boolean
81tgsi_parse_end_of_tokens(
82   struct tgsi_parse_context *ctx )
83{
84   return ctx->Position >=
85      1 + ctx->FullHeader.Header.HeaderSize + ctx->FullHeader.Header.BodySize;
86}
87
88
89/**
90 * This function is used to avoid and work-around type punning/aliasing
91 * warnings.  The warnings seem harmless on x86 but on PPC they cause
92 * real failures.
93 */
94static INLINE void
95copy_token(void *dst, const void *src)
96{
97   memcpy(dst, src, 4);
98}
99
100
101/**
102 * Get next 4-byte token, return it at address specified by 'token'
103 */
104static void
105next_token(
106   struct tgsi_parse_context *ctx,
107   void *token )
108{
109   assert( !tgsi_parse_end_of_tokens( ctx ) );
110   copy_token(token, &ctx->Tokens[ctx->Position]);
111   ctx->Position++;
112}
113
114
115void
116tgsi_parse_token(
117   struct tgsi_parse_context *ctx )
118{
119   struct tgsi_token token;
120   unsigned i;
121
122   tgsi_full_token_free( &ctx->FullToken );
123   tgsi_full_token_init( &ctx->FullToken );
124
125   next_token( ctx, &token );
126
127   switch( token.Type ) {
128   case TGSI_TOKEN_TYPE_DECLARATION:
129   {
130      struct tgsi_full_declaration *decl = &ctx->FullToken.FullDeclaration;
131
132      *decl = tgsi_default_full_declaration();
133      copy_token(&decl->Declaration, &token);
134
135      next_token( ctx, &decl->DeclarationRange );
136
137      if( decl->Declaration.Semantic ) {
138         next_token( ctx, &decl->Semantic );
139      }
140
141      break;
142   }
143
144   case TGSI_TOKEN_TYPE_IMMEDIATE:
145   {
146      struct tgsi_full_immediate *imm = &ctx->FullToken.FullImmediate;
147
148      *imm = tgsi_default_full_immediate();
149      copy_token(&imm->Immediate, &token);
150      assert( !imm->Immediate.Extended );
151
152      switch (imm->Immediate.DataType) {
153      case TGSI_IMM_FLOAT32:
154         {
155            uint imm_count = imm->Immediate.NrTokens - 1;
156            for (i = 0; i < imm_count; i++) {
157               next_token(ctx, &imm->u[i]);
158            }
159         }
160         break;
161
162      default:
163         assert( 0 );
164      }
165
166      break;
167   }
168
169   case TGSI_TOKEN_TYPE_INSTRUCTION:
170   {
171      struct tgsi_full_instruction *inst = &ctx->FullToken.FullInstruction;
172      unsigned extended;
173
174      *inst = tgsi_default_full_instruction();
175      copy_token(&inst->Instruction, &token);
176      extended = inst->Instruction.Extended;
177
178      while( extended ) {
179         struct tgsi_src_register_ext token;
180
181         next_token( ctx, &token );
182
183         switch( token.Type ) {
184         case TGSI_INSTRUCTION_EXT_TYPE_LABEL:
185            copy_token(&inst->InstructionExtLabel, &token);
186            break;
187
188         case TGSI_INSTRUCTION_EXT_TYPE_TEXTURE:
189            copy_token(&inst->InstructionExtTexture, &token);
190            break;
191
192         case TGSI_INSTRUCTION_EXT_TYPE_PREDICATE:
193            copy_token(&inst->InstructionExtPredicate, &token);
194            break;
195
196         default:
197            assert( 0 );
198         }
199
200         extended = token.Extended;
201      }
202
203      assert( inst->Instruction.NumDstRegs <= TGSI_FULL_MAX_DST_REGISTERS );
204
205      for(  i = 0; i < inst->Instruction.NumDstRegs; i++ ) {
206         unsigned extended;
207
208         next_token( ctx, &inst->FullDstRegisters[i].DstRegister );
209
210         /*
211          * No support for indirect or multi-dimensional addressing.
212          */
213         assert( !inst->FullDstRegisters[i].DstRegister.Dimension );
214
215         extended = inst->FullDstRegisters[i].DstRegister.Extended;
216
217         while( extended ) {
218            struct tgsi_src_register_ext token;
219
220            next_token( ctx, &token );
221
222            switch( token.Type ) {
223            case TGSI_DST_REGISTER_EXT_TYPE_MODULATE:
224               copy_token(&inst->FullDstRegisters[i].DstRegisterExtModulate,
225                          &token);
226               break;
227
228            default:
229               assert( 0 );
230            }
231
232            extended = token.Extended;
233         }
234
235         if( inst->FullDstRegisters[i].DstRegister.Indirect ) {
236            next_token( ctx, &inst->FullDstRegisters[i].DstRegisterInd );
237
238            /*
239             * No support for indirect or multi-dimensional addressing.
240             */
241            assert( !inst->FullDstRegisters[i].DstRegisterInd.Indirect );
242            assert( !inst->FullDstRegisters[i].DstRegisterInd.Dimension );
243            assert( !inst->FullDstRegisters[i].DstRegisterInd.Extended );
244         }
245      }
246
247      assert( inst->Instruction.NumSrcRegs <= TGSI_FULL_MAX_SRC_REGISTERS );
248
249      for( i = 0; i < inst->Instruction.NumSrcRegs; i++ ) {
250         unsigned extended;
251
252         next_token( ctx, &inst->FullSrcRegisters[i].SrcRegister );
253
254         extended = inst->FullSrcRegisters[i].SrcRegister.Extended;
255
256         while( extended ) {
257            struct tgsi_src_register_ext token;
258
259            next_token( ctx, &token );
260
261            switch( token.Type ) {
262            case TGSI_SRC_REGISTER_EXT_TYPE_MOD:
263               copy_token(&inst->FullSrcRegisters[i].SrcRegisterExtMod,
264                          &token);
265               break;
266
267            default:
268               assert( 0 );
269            }
270
271            extended = token.Extended;
272         }
273
274         if( inst->FullSrcRegisters[i].SrcRegister.Indirect ) {
275            next_token( ctx, &inst->FullSrcRegisters[i].SrcRegisterInd );
276
277            /*
278             * No support for indirect or multi-dimensional addressing.
279             */
280            assert( !inst->FullSrcRegisters[i].SrcRegisterInd.Indirect );
281            assert( !inst->FullSrcRegisters[i].SrcRegisterInd.Dimension );
282            assert( !inst->FullSrcRegisters[i].SrcRegisterInd.Extended );
283         }
284
285         if( inst->FullSrcRegisters[i].SrcRegister.Dimension ) {
286            next_token( ctx, &inst->FullSrcRegisters[i].SrcRegisterDim );
287
288            /*
289             * No support for multi-dimensional addressing.
290             */
291            assert( !inst->FullSrcRegisters[i].SrcRegisterDim.Dimension );
292            assert( !inst->FullSrcRegisters[i].SrcRegisterDim.Extended );
293
294            if( inst->FullSrcRegisters[i].SrcRegisterDim.Indirect ) {
295               next_token( ctx, &inst->FullSrcRegisters[i].SrcRegisterDimInd );
296
297               /*
298               * No support for indirect or multi-dimensional addressing.
299               */
300               assert( !inst->FullSrcRegisters[i].SrcRegisterInd.Indirect );
301               assert( !inst->FullSrcRegisters[i].SrcRegisterInd.Dimension );
302               assert( !inst->FullSrcRegisters[i].SrcRegisterInd.Extended );
303            }
304         }
305      }
306
307      break;
308   }
309
310   default:
311      assert( 0 );
312   }
313}
314
315
316unsigned
317tgsi_num_tokens(const struct tgsi_token *tokens)
318{
319   struct tgsi_parse_context ctx;
320   if (tgsi_parse_init(&ctx, tokens) == TGSI_PARSE_OK) {
321      unsigned len = (ctx.FullHeader.Header.HeaderSize +
322                      ctx.FullHeader.Header.BodySize +
323                      1);
324      return len;
325   }
326   return 0;
327}
328
329
330/**
331 * Make a new copy of a token array.
332 */
333struct tgsi_token *
334tgsi_dup_tokens(const struct tgsi_token *tokens)
335{
336   unsigned n = tgsi_num_tokens(tokens);
337   unsigned bytes = n * sizeof(struct tgsi_token);
338   struct tgsi_token *new_tokens = (struct tgsi_token *) MALLOC(bytes);
339   if (new_tokens)
340      memcpy(new_tokens, tokens, bytes);
341   return new_tokens;
342}
343