1 /**************************************************************************
3 * Copyright 2007 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 "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"
36 union tgsi_full_token
*full_token
)
38 full_token
->Token
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
43 union tgsi_full_token
*full_token
)
49 struct tgsi_parse_context
*ctx
,
50 const struct tgsi_token
*tokens
)
52 ctx
->FullVersion
.Version
= *(struct tgsi_version
*) &tokens
[0];
53 if( ctx
->FullVersion
.Version
.MajorVersion
> 1 ) {
54 return TGSI_PARSE_ERROR
;
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];
62 ctx
->FullHeader
.Processor
= tgsi_default_processor();
66 ctx
->Position
= 1 + ctx
->FullHeader
.Header
.HeaderSize
;
68 tgsi_full_token_init( &ctx
->FullToken
);
75 struct tgsi_parse_context
*ctx
)
77 tgsi_full_token_free( &ctx
->FullToken
);
81 tgsi_parse_end_of_tokens(
82 struct tgsi_parse_context
*ctx
)
84 return ctx
->Position
>=
85 1 + ctx
->FullHeader
.Header
.HeaderSize
+ ctx
->FullHeader
.Header
.BodySize
;
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
95 copy_token(void *dst
, const void *src
)
102 * Get next 4-byte token, return it at address specified by 'token'
106 struct tgsi_parse_context
*ctx
,
109 assert( !tgsi_parse_end_of_tokens( ctx
) );
110 copy_token(token
, &ctx
->Tokens
[ctx
->Position
]);
117 struct tgsi_parse_context
*ctx
)
119 struct tgsi_token token
;
122 tgsi_full_token_free( &ctx
->FullToken
);
123 tgsi_full_token_init( &ctx
->FullToken
);
125 next_token( ctx
, &token
);
127 switch( token
.Type
) {
128 case TGSI_TOKEN_TYPE_DECLARATION
:
130 struct tgsi_full_declaration
*decl
= &ctx
->FullToken
.FullDeclaration
;
132 *decl
= tgsi_default_full_declaration();
133 copy_token(&decl
->Declaration
, &token
);
135 next_token( ctx
, &decl
->DeclarationRange
);
137 if( decl
->Declaration
.Semantic
) {
138 next_token( ctx
, &decl
->Semantic
);
144 case TGSI_TOKEN_TYPE_IMMEDIATE
:
146 struct tgsi_full_immediate
*imm
= &ctx
->FullToken
.FullImmediate
;
148 *imm
= tgsi_default_full_immediate();
149 copy_token(&imm
->Immediate
, &token
);
150 assert( !imm
->Immediate
.Extended
);
152 switch (imm
->Immediate
.DataType
) {
153 case TGSI_IMM_FLOAT32
:
155 uint imm_count
= imm
->Immediate
.NrTokens
- 1;
156 for (i
= 0; i
< imm_count
; i
++) {
157 next_token(ctx
, &imm
->u
[i
]);
169 case TGSI_TOKEN_TYPE_INSTRUCTION
:
171 struct tgsi_full_instruction
*inst
= &ctx
->FullToken
.FullInstruction
;
174 *inst
= tgsi_default_full_instruction();
175 copy_token(&inst
->Instruction
, &token
);
176 extended
= inst
->Instruction
.Extended
;
179 struct tgsi_src_register_ext token
;
181 next_token( ctx
, &token
);
183 switch( token
.Type
) {
184 case TGSI_INSTRUCTION_EXT_TYPE_NV
:
185 copy_token(&inst
->InstructionExtNv
, &token
);
188 case TGSI_INSTRUCTION_EXT_TYPE_LABEL
:
189 copy_token(&inst
->InstructionExtLabel
, &token
);
192 case TGSI_INSTRUCTION_EXT_TYPE_TEXTURE
:
193 copy_token(&inst
->InstructionExtTexture
, &token
);
200 extended
= token
.Extended
;
203 assert( inst
->Instruction
.NumDstRegs
<= TGSI_FULL_MAX_DST_REGISTERS
);
205 for( i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++ ) {
208 next_token( ctx
, &inst
->FullDstRegisters
[i
].DstRegister
);
211 * No support for indirect or multi-dimensional addressing.
213 assert( !inst
->FullDstRegisters
[i
].DstRegister
.Dimension
);
215 extended
= inst
->FullDstRegisters
[i
].DstRegister
.Extended
;
218 struct tgsi_src_register_ext token
;
220 next_token( ctx
, &token
);
222 switch( token
.Type
) {
223 case TGSI_DST_REGISTER_EXT_TYPE_CONDCODE
:
224 copy_token(&inst
->FullDstRegisters
[i
].DstRegisterExtConcode
,
228 case TGSI_DST_REGISTER_EXT_TYPE_MODULATE
:
229 copy_token(&inst
->FullDstRegisters
[i
].DstRegisterExtModulate
,
237 extended
= token
.Extended
;
240 if( inst
->FullDstRegisters
[i
].DstRegister
.Indirect
) {
241 next_token( ctx
, &inst
->FullDstRegisters
[i
].DstRegisterInd
);
244 * No support for indirect or multi-dimensional addressing.
246 assert( !inst
->FullDstRegisters
[i
].DstRegisterInd
.Indirect
);
247 assert( !inst
->FullDstRegisters
[i
].DstRegisterInd
.Dimension
);
248 assert( !inst
->FullDstRegisters
[i
].DstRegisterInd
.Extended
);
252 assert( inst
->Instruction
.NumSrcRegs
<= TGSI_FULL_MAX_SRC_REGISTERS
);
254 for( i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++ ) {
257 next_token( ctx
, &inst
->FullSrcRegisters
[i
].SrcRegister
);
259 extended
= inst
->FullSrcRegisters
[i
].SrcRegister
.Extended
;
262 struct tgsi_src_register_ext token
;
264 next_token( ctx
, &token
);
266 switch( token
.Type
) {
267 case TGSI_SRC_REGISTER_EXT_TYPE_SWZ
:
268 copy_token(&inst
->FullSrcRegisters
[i
].SrcRegisterExtSwz
,
272 case TGSI_SRC_REGISTER_EXT_TYPE_MOD
:
273 copy_token(&inst
->FullSrcRegisters
[i
].SrcRegisterExtMod
,
281 extended
= token
.Extended
;
284 if( inst
->FullSrcRegisters
[i
].SrcRegister
.Indirect
) {
285 next_token( ctx
, &inst
->FullSrcRegisters
[i
].SrcRegisterInd
);
288 * No support for indirect or multi-dimensional addressing.
290 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Indirect
);
291 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Dimension
);
292 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Extended
);
295 if( inst
->FullSrcRegisters
[i
].SrcRegister
.Dimension
) {
296 next_token( ctx
, &inst
->FullSrcRegisters
[i
].SrcRegisterDim
);
299 * No support for multi-dimensional addressing.
301 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterDim
.Dimension
);
302 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterDim
.Extended
);
304 if( inst
->FullSrcRegisters
[i
].SrcRegisterDim
.Indirect
) {
305 next_token( ctx
, &inst
->FullSrcRegisters
[i
].SrcRegisterDimInd
);
308 * No support for indirect or multi-dimensional addressing.
310 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Indirect
);
311 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Dimension
);
312 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Extended
);
327 tgsi_num_tokens(const struct tgsi_token
*tokens
)
329 struct tgsi_parse_context ctx
;
330 if (tgsi_parse_init(&ctx
, tokens
) == TGSI_PARSE_OK
) {
331 unsigned len
= (ctx
.FullHeader
.Header
.HeaderSize
+
332 ctx
.FullHeader
.Header
.BodySize
+
341 * Make a new copy of a token array.
344 tgsi_dup_tokens(const struct tgsi_token
*tokens
)
346 unsigned n
= tgsi_num_tokens(tokens
);
347 unsigned bytes
= n
* sizeof(struct tgsi_token
);
348 struct tgsi_token
*new_tokens
= (struct tgsi_token
*) MALLOC(bytes
);
350 memcpy(new_tokens
, tokens
, bytes
);