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 "util/u_memory.h"
35 struct tgsi_parse_context
*ctx
,
36 const struct tgsi_token
*tokens
)
38 ctx
->FullHeader
.Header
= *(struct tgsi_header
*) &tokens
[0];
39 if( ctx
->FullHeader
.Header
.HeaderSize
>= 2 ) {
40 ctx
->FullHeader
.Processor
= *(struct tgsi_processor
*) &tokens
[1];
43 return TGSI_PARSE_ERROR
;
47 ctx
->Position
= ctx
->FullHeader
.Header
.HeaderSize
;
54 struct tgsi_parse_context
*ctx
)
59 tgsi_parse_end_of_tokens(
60 struct tgsi_parse_context
*ctx
)
62 return ctx
->Position
>=
63 ctx
->FullHeader
.Header
.HeaderSize
+ ctx
->FullHeader
.Header
.BodySize
;
68 * This function is used to avoid and work-around type punning/aliasing
69 * warnings. The warnings seem harmless on x86 but on PPC they cause
73 copy_token(void *dst
, const void *src
)
80 * Get next 4-byte token, return it at address specified by 'token'
84 struct tgsi_parse_context
*ctx
,
87 assert( !tgsi_parse_end_of_tokens( ctx
) );
88 copy_token(token
, &ctx
->Tokens
[ctx
->Position
]);
95 struct tgsi_parse_context
*ctx
)
97 struct tgsi_token token
;
100 next_token( ctx
, &token
);
102 switch( token
.Type
) {
103 case TGSI_TOKEN_TYPE_DECLARATION
:
105 struct tgsi_full_declaration
*decl
= &ctx
->FullToken
.FullDeclaration
;
107 memset(decl
, 0, sizeof *decl
);
108 copy_token(&decl
->Declaration
, &token
);
110 next_token( ctx
, &decl
->Range
);
112 if (decl
->Declaration
.Dimension
) {
113 next_token(ctx
, &decl
->Dim
);
116 if( decl
->Declaration
.Semantic
) {
117 next_token( ctx
, &decl
->Semantic
);
120 if (decl
->Declaration
.File
== TGSI_FILE_IMMEDIATE_ARRAY
) {
122 decl
->ImmediateData
.u
= (union tgsi_immediate_data
*)
123 &ctx
->Tokens
[ctx
->Position
];
124 for (i
= 0; i
<= decl
->Range
.Last
; ++i
) {
125 for (j
= 0; j
< 4; ++j
) {
134 case TGSI_TOKEN_TYPE_IMMEDIATE
:
136 struct tgsi_full_immediate
*imm
= &ctx
->FullToken
.FullImmediate
;
139 memset(imm
, 0, sizeof *imm
);
140 copy_token(&imm
->Immediate
, &token
);
142 imm_count
= imm
->Immediate
.NrTokens
- 1;
144 switch (imm
->Immediate
.DataType
) {
145 case TGSI_IMM_FLOAT32
:
146 for (i
= 0; i
< imm_count
; i
++) {
147 next_token(ctx
, &imm
->u
[i
].Float
);
151 case TGSI_IMM_UINT32
:
152 for (i
= 0; i
< imm_count
; i
++) {
153 next_token(ctx
, &imm
->u
[i
].Uint
);
158 for (i
= 0; i
< imm_count
; i
++) {
159 next_token(ctx
, &imm
->u
[i
].Int
);
170 case TGSI_TOKEN_TYPE_INSTRUCTION
:
172 struct tgsi_full_instruction
*inst
= &ctx
->FullToken
.FullInstruction
;
174 memset(inst
, 0, sizeof *inst
);
175 copy_token(&inst
->Instruction
, &token
);
177 if (inst
->Instruction
.Predicate
) {
178 next_token(ctx
, &inst
->Predicate
);
181 if (inst
->Instruction
.Label
) {
182 next_token( ctx
, &inst
->Label
);
185 if (inst
->Instruction
.Texture
) {
186 next_token( ctx
, &inst
->Texture
);
189 assert( inst
->Instruction
.NumDstRegs
<= TGSI_FULL_MAX_DST_REGISTERS
);
191 for( i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++ ) {
193 next_token( ctx
, &inst
->Dst
[i
].Register
);
195 if( inst
->Dst
[i
].Register
.Indirect
) {
196 next_token( ctx
, &inst
->Dst
[i
].Indirect
);
199 * No support for indirect or multi-dimensional addressing.
201 assert( !inst
->Dst
[i
].Indirect
.Dimension
);
202 assert( !inst
->Dst
[i
].Indirect
.Indirect
);
204 if( inst
->Dst
[i
].Register
.Dimension
) {
205 next_token( ctx
, &inst
->Dst
[i
].Dimension
);
208 * No support for multi-dimensional addressing.
210 assert( !inst
->Dst
[i
].Dimension
.Dimension
);
212 if( inst
->Dst
[i
].Dimension
.Indirect
) {
213 next_token( ctx
, &inst
->Dst
[i
].DimIndirect
);
216 * No support for indirect or multi-dimensional addressing.
218 assert( !inst
->Dst
[i
].Indirect
.Indirect
);
219 assert( !inst
->Dst
[i
].Indirect
.Dimension
);
224 assert( inst
->Instruction
.NumSrcRegs
<= TGSI_FULL_MAX_SRC_REGISTERS
);
226 for( i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++ ) {
228 next_token( ctx
, &inst
->Src
[i
].Register
);
230 if( inst
->Src
[i
].Register
.Indirect
) {
231 next_token( ctx
, &inst
->Src
[i
].Indirect
);
234 * No support for indirect or multi-dimensional addressing.
236 assert( !inst
->Src
[i
].Indirect
.Indirect
);
237 assert( !inst
->Src
[i
].Indirect
.Dimension
);
240 if( inst
->Src
[i
].Register
.Dimension
) {
241 next_token( ctx
, &inst
->Src
[i
].Dimension
);
244 * No support for multi-dimensional addressing.
246 assert( !inst
->Src
[i
].Dimension
.Dimension
);
248 if( inst
->Src
[i
].Dimension
.Indirect
) {
249 next_token( ctx
, &inst
->Src
[i
].DimIndirect
);
252 * No support for indirect or multi-dimensional addressing.
254 assert( !inst
->Src
[i
].Indirect
.Indirect
);
255 assert( !inst
->Src
[i
].Indirect
.Dimension
);
263 case TGSI_TOKEN_TYPE_PROPERTY
:
265 struct tgsi_full_property
*prop
= &ctx
->FullToken
.FullProperty
;
268 memset(prop
, 0, sizeof *prop
);
269 copy_token(&prop
->Property
, &token
);
271 prop_count
= prop
->Property
.NrTokens
- 1;
272 for (i
= 0; i
< prop_count
; i
++) {
273 next_token(ctx
, &prop
->u
[i
]);
288 * Make a new copy of a token array.
291 tgsi_dup_tokens(const struct tgsi_token
*tokens
)
293 unsigned n
= tgsi_num_tokens(tokens
);
294 unsigned bytes
= n
* sizeof(struct tgsi_token
);
295 struct tgsi_token
*new_tokens
= (struct tgsi_token
*) MALLOC(bytes
);
297 memcpy(new_tokens
, tokens
, bytes
);
303 * Allocate memory for num_tokens tokens.
306 tgsi_alloc_tokens(unsigned num_tokens
)
308 unsigned bytes
= num_tokens
* sizeof(struct tgsi_token
);
309 return (struct tgsi_token
*) MALLOC(bytes
);
314 tgsi_dump_tokens(const struct tgsi_token
*tokens
)
316 const unsigned *dwords
= (const unsigned *)tokens
;
317 int nr
= tgsi_num_tokens(tokens
);
320 assert(sizeof(*tokens
) == sizeof(unsigned));
322 debug_printf("const unsigned tokens[%d] = {\n", nr
);
323 for (i
= 0; i
< nr
; i
++)
324 debug_printf("0x%08x,\n", dwords
[i
]);
325 debug_printf("};\n");