Merge remote branch 'origin/mesa_7_7_branch'
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_parse.c
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 "util/u_memory.h"
32
33 unsigned
34 tgsi_parse_init(
35 struct tgsi_parse_context *ctx,
36 const struct tgsi_token *tokens )
37 {
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];
41 }
42 else {
43 return TGSI_PARSE_ERROR;
44 }
45
46 ctx->Tokens = tokens;
47 ctx->Position = ctx->FullHeader.Header.HeaderSize;
48
49 return TGSI_PARSE_OK;
50 }
51
52 void
53 tgsi_parse_free(
54 struct tgsi_parse_context *ctx )
55 {
56 }
57
58 boolean
59 tgsi_parse_end_of_tokens(
60 struct tgsi_parse_context *ctx )
61 {
62 return ctx->Position >=
63 ctx->FullHeader.Header.HeaderSize + ctx->FullHeader.Header.BodySize;
64 }
65
66
67 /**
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
70 * real failures.
71 */
72 static INLINE void
73 copy_token(void *dst, const void *src)
74 {
75 memcpy(dst, src, 4);
76 }
77
78
79 /**
80 * Get next 4-byte token, return it at address specified by 'token'
81 */
82 static void
83 next_token(
84 struct tgsi_parse_context *ctx,
85 void *token )
86 {
87 assert( !tgsi_parse_end_of_tokens( ctx ) );
88 copy_token(token, &ctx->Tokens[ctx->Position]);
89 ctx->Position++;
90 }
91
92
93 void
94 tgsi_parse_token(
95 struct tgsi_parse_context *ctx )
96 {
97 struct tgsi_token token;
98 unsigned i;
99
100 next_token( ctx, &token );
101
102 switch( token.Type ) {
103 case TGSI_TOKEN_TYPE_DECLARATION:
104 {
105 struct tgsi_full_declaration *decl = &ctx->FullToken.FullDeclaration;
106
107 memset(decl, 0, sizeof *decl);
108 copy_token(&decl->Declaration, &token);
109
110 next_token( ctx, &decl->Range );
111
112 if( decl->Declaration.Semantic ) {
113 next_token( ctx, &decl->Semantic );
114 }
115
116 break;
117 }
118
119 case TGSI_TOKEN_TYPE_IMMEDIATE:
120 {
121 struct tgsi_full_immediate *imm = &ctx->FullToken.FullImmediate;
122 uint imm_count;
123
124 memset(imm, 0, sizeof *imm);
125 copy_token(&imm->Immediate, &token);
126
127 imm_count = imm->Immediate.NrTokens - 1;
128
129 switch (imm->Immediate.DataType) {
130 case TGSI_IMM_FLOAT32:
131 for (i = 0; i < imm_count; i++) {
132 next_token(ctx, &imm->u[i].Float);
133 }
134 break;
135
136 case TGSI_IMM_UINT32:
137 for (i = 0; i < imm_count; i++) {
138 next_token(ctx, &imm->u[i].Uint);
139 }
140 break;
141
142 case TGSI_IMM_INT32:
143 for (i = 0; i < imm_count; i++) {
144 next_token(ctx, &imm->u[i].Int);
145 }
146 break;
147
148 default:
149 assert( 0 );
150 }
151
152 break;
153 }
154
155 case TGSI_TOKEN_TYPE_INSTRUCTION:
156 {
157 struct tgsi_full_instruction *inst = &ctx->FullToken.FullInstruction;
158
159 memset(inst, 0, sizeof *inst);
160 copy_token(&inst->Instruction, &token);
161
162 if (inst->Instruction.Predicate) {
163 next_token(ctx, &inst->Predicate);
164 }
165
166 if (inst->Instruction.Label) {
167 next_token( ctx, &inst->Label);
168 }
169
170 if (inst->Instruction.Texture) {
171 next_token( ctx, &inst->Texture);
172 }
173
174 assert( inst->Instruction.NumDstRegs <= TGSI_FULL_MAX_DST_REGISTERS );
175
176 for( i = 0; i < inst->Instruction.NumDstRegs; i++ ) {
177
178 next_token( ctx, &inst->Dst[i].Register );
179
180 /*
181 * No support for indirect or multi-dimensional addressing.
182 */
183 assert( !inst->Dst[i].Register.Dimension );
184
185 if( inst->Dst[i].Register.Indirect ) {
186 next_token( ctx, &inst->Dst[i].Indirect );
187
188 /*
189 * No support for indirect or multi-dimensional addressing.
190 */
191 assert( !inst->Dst[i].Indirect.Dimension );
192 assert( !inst->Dst[i].Indirect.Indirect );
193 }
194 }
195
196 assert( inst->Instruction.NumSrcRegs <= TGSI_FULL_MAX_SRC_REGISTERS );
197
198 for( i = 0; i < inst->Instruction.NumSrcRegs; i++ ) {
199
200 next_token( ctx, &inst->Src[i].Register );
201
202 if( inst->Src[i].Register.Indirect ) {
203 next_token( ctx, &inst->Src[i].Indirect );
204
205 /*
206 * No support for indirect or multi-dimensional addressing.
207 */
208 assert( !inst->Src[i].Indirect.Indirect );
209 assert( !inst->Src[i].Indirect.Dimension );
210 }
211
212 if( inst->Src[i].Register.Dimension ) {
213 next_token( ctx, &inst->Src[i].Dimension );
214
215 /*
216 * No support for multi-dimensional addressing.
217 */
218 assert( !inst->Src[i].Dimension.Dimension );
219
220 if( inst->Src[i].Dimension.Indirect ) {
221 next_token( ctx, &inst->Src[i].DimIndirect );
222
223 /*
224 * No support for indirect or multi-dimensional addressing.
225 */
226 assert( !inst->Src[i].Indirect.Indirect );
227 assert( !inst->Src[i].Indirect.Dimension );
228 }
229 }
230 }
231
232 break;
233 }
234
235 case TGSI_TOKEN_TYPE_PROPERTY:
236 {
237 struct tgsi_full_property *prop = &ctx->FullToken.FullProperty;
238 uint prop_count;
239
240 memset(prop, 0, sizeof *prop);
241 copy_token(&prop->Property, &token);
242
243 prop_count = prop->Property.NrTokens - 1;
244 for (i = 0; i < prop_count; i++) {
245 next_token(ctx, &prop->u[i]);
246 }
247
248 break;
249 }
250
251 default:
252 assert( 0 );
253 }
254 }
255
256
257 unsigned
258 tgsi_num_tokens(const struct tgsi_token *tokens)
259 {
260 struct tgsi_parse_context ctx;
261 if (tgsi_parse_init(&ctx, tokens) == TGSI_PARSE_OK) {
262 unsigned len = (ctx.FullHeader.Header.HeaderSize +
263 ctx.FullHeader.Header.BodySize);
264 return len;
265 }
266 return 0;
267 }
268
269
270 /**
271 * Make a new copy of a token array.
272 */
273 struct tgsi_token *
274 tgsi_dup_tokens(const struct tgsi_token *tokens)
275 {
276 unsigned n = tgsi_num_tokens(tokens);
277 unsigned bytes = n * sizeof(struct tgsi_token);
278 struct tgsi_token *new_tokens = (struct tgsi_token *) MALLOC(bytes);
279 if (new_tokens)
280 memcpy(new_tokens, tokens, bytes);
281 return new_tokens;
282 }