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 struct tgsi_parse_context
*ctx
,
96 assert( !tgsi_parse_end_of_tokens( ctx
) );
98 *(struct tgsi_token
*) token
= ctx
->Tokens
[ctx
->Position
++];
103 struct tgsi_parse_context
*ctx
)
105 struct tgsi_token token
;
108 tgsi_full_token_free( &ctx
->FullToken
);
109 tgsi_full_token_init( &ctx
->FullToken
);
111 next_token( ctx
, &token
);
113 switch( token
.Type
) {
114 case TGSI_TOKEN_TYPE_DECLARATION
:
116 struct tgsi_full_declaration
*decl
= &ctx
->FullToken
.FullDeclaration
;
118 *decl
= tgsi_default_full_declaration();
119 decl
->Declaration
= *(struct tgsi_declaration
*) &token
;
121 next_token( ctx
, &decl
->DeclarationRange
);
123 if( decl
->Declaration
.Semantic
) {
124 next_token( ctx
, &decl
->Semantic
);
130 case TGSI_TOKEN_TYPE_IMMEDIATE
:
132 struct tgsi_full_immediate
*imm
= &ctx
->FullToken
.FullImmediate
;
134 *imm
= tgsi_default_full_immediate();
135 imm
->Immediate
= *(struct tgsi_immediate
*) &token
;
137 assert( !imm
->Immediate
.Extended
);
139 switch (imm
->Immediate
.DataType
) {
140 case TGSI_IMM_FLOAT32
:
141 imm
->u
.Pointer
= MALLOC(
142 sizeof( struct tgsi_immediate_float32
) * (imm
->Immediate
.Size
- 1) );
143 for( i
= 0; i
< imm
->Immediate
.Size
- 1; i
++ ) {
144 next_token( ctx
, (struct tgsi_immediate_float32
*) &imm
->u
.ImmediateFloat32
[i
] );
155 case TGSI_TOKEN_TYPE_INSTRUCTION
:
157 struct tgsi_full_instruction
*inst
= &ctx
->FullToken
.FullInstruction
;
160 *inst
= tgsi_default_full_instruction();
161 inst
->Instruction
= *(struct tgsi_instruction
*) &token
;
163 extended
= inst
->Instruction
.Extended
;
166 struct tgsi_src_register_ext token
;
168 next_token( ctx
, &token
);
170 switch( token
.Type
) {
171 case TGSI_INSTRUCTION_EXT_TYPE_NV
:
172 inst
->InstructionExtNv
=
173 *(struct tgsi_instruction_ext_nv
*) &token
;
176 case TGSI_INSTRUCTION_EXT_TYPE_LABEL
:
177 inst
->InstructionExtLabel
=
178 *(struct tgsi_instruction_ext_label
*) &token
;
181 case TGSI_INSTRUCTION_EXT_TYPE_TEXTURE
:
182 inst
->InstructionExtTexture
=
183 *(struct tgsi_instruction_ext_texture
*) &token
;
190 extended
= token
.Extended
;
193 assert( inst
->Instruction
.NumDstRegs
<= TGSI_FULL_MAX_DST_REGISTERS
);
195 for( i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++ ) {
198 next_token( ctx
, &inst
->FullDstRegisters
[i
].DstRegister
);
201 * No support for indirect or multi-dimensional addressing.
203 assert( !inst
->FullDstRegisters
[i
].DstRegister
.Indirect
);
204 assert( !inst
->FullDstRegisters
[i
].DstRegister
.Dimension
);
206 extended
= inst
->FullDstRegisters
[i
].DstRegister
.Extended
;
209 struct tgsi_src_register_ext token
;
211 next_token( ctx
, &token
);
213 switch( token
.Type
) {
214 case TGSI_DST_REGISTER_EXT_TYPE_CONDCODE
:
215 inst
->FullDstRegisters
[i
].DstRegisterExtConcode
=
216 *(struct tgsi_dst_register_ext_concode
*) &token
;
219 case TGSI_DST_REGISTER_EXT_TYPE_MODULATE
:
220 inst
->FullDstRegisters
[i
].DstRegisterExtModulate
=
221 *(struct tgsi_dst_register_ext_modulate
*) &token
;
228 extended
= token
.Extended
;
232 assert( inst
->Instruction
.NumSrcRegs
<= TGSI_FULL_MAX_SRC_REGISTERS
);
234 for( i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++ ) {
237 next_token( ctx
, &inst
->FullSrcRegisters
[i
].SrcRegister
);
239 extended
= inst
->FullSrcRegisters
[i
].SrcRegister
.Extended
;
242 struct tgsi_src_register_ext token
;
244 next_token( ctx
, &token
);
246 switch( token
.Type
) {
247 case TGSI_SRC_REGISTER_EXT_TYPE_SWZ
:
248 inst
->FullSrcRegisters
[i
].SrcRegisterExtSwz
=
249 *(struct tgsi_src_register_ext_swz
*) &token
;
252 case TGSI_SRC_REGISTER_EXT_TYPE_MOD
:
253 inst
->FullSrcRegisters
[i
].SrcRegisterExtMod
=
254 *(struct tgsi_src_register_ext_mod
*) &token
;
261 extended
= token
.Extended
;
264 if( inst
->FullSrcRegisters
[i
].SrcRegister
.Indirect
) {
265 next_token( ctx
, &inst
->FullSrcRegisters
[i
].SrcRegisterInd
);
268 * No support for indirect or multi-dimensional addressing.
270 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Indirect
);
271 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Dimension
);
272 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterInd
.Extended
);
275 if( inst
->FullSrcRegisters
[i
].SrcRegister
.Dimension
) {
276 next_token( ctx
, &inst
->FullSrcRegisters
[i
].SrcRegisterDim
);
279 * No support for multi-dimensional addressing.
281 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterDim
.Dimension
);
282 assert( !inst
->FullSrcRegisters
[i
].SrcRegisterDim
.Extended
);
284 if( inst
->FullSrcRegisters
[i
].SrcRegisterDim
.Indirect
) {
285 next_token( ctx
, &inst
->FullSrcRegisters
[i
].SrcRegisterDimInd
);
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
);
307 tgsi_num_tokens(const struct tgsi_token
*tokens
)
309 struct tgsi_parse_context ctx
;
310 if (tgsi_parse_init(&ctx
, tokens
) == TGSI_PARSE_OK
) {
311 unsigned len
= (ctx
.FullHeader
.Header
.HeaderSize
+
312 ctx
.FullHeader
.Header
.BodySize
+
321 * Make a new copy of a token array.
324 tgsi_dup_tokens(const struct tgsi_token
*tokens
)
326 unsigned n
= tgsi_num_tokens(tokens
);
327 unsigned bytes
= n
* sizeof(struct tgsi_token
);
328 struct tgsi_token
*new_tokens
= (struct tgsi_token
*) MALLOC(bytes
);
330 memcpy(new_tokens
, tokens
, bytes
);