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 "pipe/p_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
)
45 if( full_token
->Token
.Type
== TGSI_TOKEN_TYPE_IMMEDIATE
) {
46 FREE( (void *) full_token
->FullImmediate
.u
.Pointer
);
52 struct tgsi_parse_context
*ctx
,
53 const struct tgsi_token
*tokens
)
55 ctx
->FullVersion
.Version
= *(struct tgsi_version
*) &tokens
[0];
56 if( ctx
->FullVersion
.Version
.MajorVersion
> 1 ) {
57 return TGSI_PARSE_ERROR
;
60 ctx
->FullHeader
.Header
= *(struct tgsi_header
*) &tokens
[1];
61 if( ctx
->FullHeader
.Header
.HeaderSize
>= 2 ) {
62 ctx
->FullHeader
.Processor
= *(struct tgsi_processor
*) &tokens
[2];
65 ctx
->FullHeader
.Processor
= tgsi_default_processor();
69 ctx
->Position
= 1 + ctx
->FullHeader
.Header
.HeaderSize
;
71 tgsi_full_token_init( &ctx
->FullToken
);
78 struct tgsi_parse_context
*ctx
)
80 tgsi_full_token_free( &ctx
->FullToken
);
84 tgsi_parse_end_of_tokens(
85 struct tgsi_parse_context
*ctx
)
87 return ctx
->Position
>=
88 1 + ctx
->FullHeader
.Header
.HeaderSize
+ ctx
->FullHeader
.Header
.BodySize
;
93 * This function is used to avoid and work-around type punning/aliasing
94 * warnings. The warnings seem harmless on x86 but on PPC they cause
98 copy_token(void *dst
, const void *src
)
105 * Get next 4-byte token, return it at address specified by 'token'
109 struct tgsi_parse_context
*ctx
,
112 assert( !tgsi_parse_end_of_tokens( ctx
) );
113 copy_token(token
, &ctx
->Tokens
[ctx
->Position
]);
120 struct tgsi_parse_context
*ctx
)
122 struct tgsi_token token
;
125 tgsi_full_token_free( &ctx
->FullToken
);
126 tgsi_full_token_init( &ctx
->FullToken
);
128 next_token( ctx
, &token
);
130 switch( token
.Type
) {
131 case TGSI_TOKEN_TYPE_DECLARATION
:
133 struct tgsi_full_declaration
*decl
= &ctx
->FullToken
.FullDeclaration
;
135 *decl
= tgsi_default_full_declaration();
136 copy_token(&decl
->Declaration
, &token
);
138 next_token( ctx
, &decl
->DeclarationRange
);
140 if( decl
->Declaration
.Semantic
) {
141 next_token( ctx
, &decl
->Semantic
);
147 case TGSI_TOKEN_TYPE_IMMEDIATE
:
149 struct tgsi_full_immediate
*imm
= &ctx
->FullToken
.FullImmediate
;
151 *imm
= tgsi_default_full_immediate();
152 copy_token(&imm
->Immediate
, &token
);
153 assert( !imm
->Immediate
.Extended
);
155 switch (imm
->Immediate
.DataType
) {
156 case TGSI_IMM_FLOAT32
:
157 imm
->u
.Pointer
= MALLOC(
158 sizeof( struct tgsi_immediate_float32
) * (imm
->Immediate
.Size
- 1) );
159 for( i
= 0; i
< imm
->Immediate
.Size
- 1; i
++ ) {
160 next_token( ctx
, (struct tgsi_immediate_float32
*) &imm
->u
.ImmediateFloat32
[i
] );
171 case TGSI_TOKEN_TYPE_INSTRUCTION
:
173 struct tgsi_full_instruction
*inst
= &ctx
->FullToken
.FullInstruction
;
176 *inst
= tgsi_default_full_instruction();
177 copy_token(&inst
->Instruction
, &token
);
178 extended
= inst
->Instruction
.Extended
;
181 struct tgsi_src_register_ext token
;
183 next_token( ctx
, &token
);
185 switch( token
.Type
) {
186 case TGSI_INSTRUCTION_EXT_TYPE_NV
:
187 copy_token(&inst
->InstructionExtNv
, &token
);
190 case TGSI_INSTRUCTION_EXT_TYPE_LABEL
:
191 copy_token(&inst
->InstructionExtLabel
, &token
);
194 case TGSI_INSTRUCTION_EXT_TYPE_TEXTURE
:
195 copy_token(&inst
->InstructionExtTexture
, &token
);
202 extended
= token
.Extended
;
205 assert( inst
->Instruction
.NumDstRegs
<= TGSI_FULL_MAX_DST_REGISTERS
);
207 for( i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++ ) {
210 next_token( ctx
, &inst
->FullDstRegisters
[i
].DstRegister
);
213 * No support for indirect or multi-dimensional addressing.
215 assert( !inst
->FullDstRegisters
[i
].DstRegister
.Indirect
);
216 assert( !inst
->FullDstRegisters
[i
].DstRegister
.Dimension
);
218 extended
= inst
->FullDstRegisters
[i
].DstRegister
.Extended
;
221 struct tgsi_src_register_ext token
;
223 next_token( ctx
, &token
);
225 switch( token
.Type
) {
226 case TGSI_DST_REGISTER_EXT_TYPE_CONDCODE
:
227 copy_token(&inst
->FullDstRegisters
[i
].DstRegisterExtConcode
,
231 case TGSI_DST_REGISTER_EXT_TYPE_MODULATE
:
232 copy_token(&inst
->FullDstRegisters
[i
].DstRegisterExtModulate
,
240 extended
= token
.Extended
;
244 assert( inst
->Instruction
.NumSrcRegs
<= TGSI_FULL_MAX_SRC_REGISTERS
);
246 for( i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++ ) {
249 next_token( ctx
, &inst
->FullSrcRegisters
[i
].SrcRegister
);
251 extended
= inst
->FullSrcRegisters
[i
].SrcRegister
.Extended
;
254 struct tgsi_src_register_ext token
;
256 next_token( ctx
, &token
);
258 switch( token
.Type
) {
259 case TGSI_SRC_REGISTER_EXT_TYPE_SWZ
:
260 copy_token(&inst
->FullSrcRegisters
[i
].SrcRegisterExtSwz
,
264 case TGSI_SRC_REGISTER_EXT_TYPE_MOD
:
265 copy_token(&inst
->FullSrcRegisters
[i
].SrcRegisterExtMod
,
273 extended
= token
.Extended
;
276 if( inst
->FullSrcRegisters
[i
].SrcRegister
.Indirect
) {
277 next_token( ctx
, &inst
->FullSrcRegisters
[i
].SrcRegisterInd
);
280 * No support for indirect or multi-dimensional addressing.
282 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Indirect
);
283 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Dimension
);
284 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Extended
);
287 if( inst
->FullSrcRegisters
[i
].SrcRegister
.Dimension
) {
288 next_token( ctx
, &inst
->FullSrcRegisters
[i
].SrcRegisterDim
);
291 * No support for multi-dimensional addressing.
293 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterDim
.Dimension
);
294 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterDim
.Extended
);
296 if( inst
->FullSrcRegisters
[i
].SrcRegisterDim
.Indirect
) {
297 next_token( ctx
, &inst
->FullSrcRegisters
[i
].SrcRegisterDimInd
);
300 * No support for indirect or multi-dimensional addressing.
302 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Indirect
);
303 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Dimension
);
304 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Extended
);
319 tgsi_num_tokens(const struct tgsi_token
*tokens
)
321 struct tgsi_parse_context ctx
;
322 if (tgsi_parse_init(&ctx
, tokens
) == TGSI_PARSE_OK
) {
323 unsigned len
= (ctx
.FullHeader
.Header
.HeaderSize
+
324 ctx
.FullHeader
.Header
.BodySize
+
333 * Make a new copy of a token array.
336 tgsi_dup_tokens(const struct tgsi_token
*tokens
)
338 unsigned n
= tgsi_num_tokens(tokens
);
339 unsigned bytes
= n
* sizeof(struct tgsi_token
);
340 struct tgsi_token
*new_tokens
= (struct tgsi_token
*) MALLOC(bytes
);
342 memcpy(new_tokens
, tokens
, bytes
);