Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / src / gallium / auxiliary / tgsi / util / 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 "pipe/p_debug.h"
29 #include "pipe/p_util.h"
30 #include "pipe/p_shader_tokens.h"
31 #include "tgsi_parse.h"
32 #include "tgsi_build.h"
33
34 void
35 tgsi_full_token_init(
36 union tgsi_full_token *full_token )
37 {
38 full_token->Token.Type = TGSI_TOKEN_TYPE_DECLARATION;
39 }
40
41 void
42 tgsi_full_token_free(
43 union tgsi_full_token *full_token )
44 {
45 if( full_token->Token.Type == TGSI_TOKEN_TYPE_IMMEDIATE ) {
46 FREE( (void *) full_token->FullImmediate.u.Pointer );
47 }
48 }
49
50 unsigned
51 tgsi_parse_init(
52 struct tgsi_parse_context *ctx,
53 const struct tgsi_token *tokens )
54 {
55 ctx->FullVersion.Version = *(struct tgsi_version *) &tokens[0];
56 if( ctx->FullVersion.Version.MajorVersion > 1 ) {
57 return TGSI_PARSE_ERROR;
58 }
59
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];
63 }
64 else {
65 ctx->FullHeader.Processor = tgsi_default_processor();
66 }
67
68 ctx->Tokens = tokens;
69 ctx->Position = 1 + ctx->FullHeader.Header.HeaderSize;
70
71 tgsi_full_token_init( &ctx->FullToken );
72
73 return TGSI_PARSE_OK;
74 }
75
76 void
77 tgsi_parse_free(
78 struct tgsi_parse_context *ctx )
79 {
80 tgsi_full_token_free( &ctx->FullToken );
81 }
82
83 boolean
84 tgsi_parse_end_of_tokens(
85 struct tgsi_parse_context *ctx )
86 {
87 return ctx->Position >=
88 1 + ctx->FullHeader.Header.HeaderSize + ctx->FullHeader.Header.BodySize;
89 }
90
91 static void
92 next_token(
93 struct tgsi_parse_context *ctx,
94 void *token )
95 {
96 assert( !tgsi_parse_end_of_tokens( ctx ) );
97
98 *(struct tgsi_token *) token = ctx->Tokens[ctx->Position++];
99 }
100
101 void
102 tgsi_parse_token(
103 struct tgsi_parse_context *ctx )
104 {
105 struct tgsi_token token;
106 unsigned i;
107
108 tgsi_full_token_free( &ctx->FullToken );
109 tgsi_full_token_init( &ctx->FullToken );
110
111 next_token( ctx, &token );
112
113 switch( token.Type ) {
114 case TGSI_TOKEN_TYPE_DECLARATION:
115 {
116 struct tgsi_full_declaration *decl = &ctx->FullToken.FullDeclaration;
117
118 *decl = tgsi_default_full_declaration();
119 decl->Declaration = *(struct tgsi_declaration *) &token;
120
121 switch( decl->Declaration.Type ) {
122 case TGSI_DECLARE_RANGE:
123 next_token( ctx, &decl->u.DeclarationRange );
124 break;
125
126 case TGSI_DECLARE_MASK:
127 next_token( ctx, &decl->u.DeclarationMask );
128 break;
129
130 default:
131 assert (0);
132 }
133
134 if( decl->Declaration.Interpolate ) {
135 next_token( ctx, &decl->Interpolation );
136 }
137
138 if( decl->Declaration.Semantic ) {
139 next_token( ctx, &decl->Semantic );
140 }
141
142 break;
143 }
144
145 case TGSI_TOKEN_TYPE_IMMEDIATE:
146 {
147 struct tgsi_full_immediate *imm = &ctx->FullToken.FullImmediate;
148
149 *imm = tgsi_default_full_immediate();
150 imm->Immediate = *(struct tgsi_immediate *) &token;
151
152 assert( !imm->Immediate.Extended );
153
154 switch (imm->Immediate.DataType) {
155 case TGSI_IMM_FLOAT32:
156 imm->u.Pointer = MALLOC(
157 sizeof( struct tgsi_immediate_float32 ) * (imm->Immediate.Size - 1) );
158 for( i = 0; i < imm->Immediate.Size - 1; i++ ) {
159 next_token( ctx, (struct tgsi_immediate_float32 *) &imm->u.ImmediateFloat32[i] );
160 }
161 break;
162
163 default:
164 assert( 0 );
165 }
166
167 break;
168 }
169
170 case TGSI_TOKEN_TYPE_INSTRUCTION:
171 {
172 struct tgsi_full_instruction *inst = &ctx->FullToken.FullInstruction;
173 unsigned extended;
174
175 *inst = tgsi_default_full_instruction();
176 inst->Instruction = *(struct tgsi_instruction *) &token;
177
178 extended = inst->Instruction.Extended;
179
180 while( extended ) {
181 struct tgsi_src_register_ext token;
182
183 next_token( ctx, &token );
184
185 switch( token.Type ) {
186 case TGSI_INSTRUCTION_EXT_TYPE_NV:
187 inst->InstructionExtNv =
188 *(struct tgsi_instruction_ext_nv *) &token;
189 break;
190
191 case TGSI_INSTRUCTION_EXT_TYPE_LABEL:
192 inst->InstructionExtLabel =
193 *(struct tgsi_instruction_ext_label *) &token;
194 break;
195
196 case TGSI_INSTRUCTION_EXT_TYPE_TEXTURE:
197 inst->InstructionExtTexture =
198 *(struct tgsi_instruction_ext_texture *) &token;
199 break;
200
201 default:
202 assert( 0 );
203 }
204
205 extended = token.Extended;
206 }
207
208 assert( inst->Instruction.NumDstRegs <= TGSI_FULL_MAX_DST_REGISTERS );
209
210 for( i = 0; i < inst->Instruction.NumDstRegs; i++ ) {
211 unsigned extended;
212
213 next_token( ctx, &inst->FullDstRegisters[i].DstRegister );
214
215 /*
216 * No support for indirect or multi-dimensional addressing.
217 */
218 assert( !inst->FullDstRegisters[i].DstRegister.Indirect );
219 assert( !inst->FullDstRegisters[i].DstRegister.Dimension );
220
221 extended = inst->FullDstRegisters[i].DstRegister.Extended;
222
223 while( extended ) {
224 struct tgsi_src_register_ext token;
225
226 next_token( ctx, &token );
227
228 switch( token.Type ) {
229 case TGSI_DST_REGISTER_EXT_TYPE_CONDCODE:
230 inst->FullDstRegisters[i].DstRegisterExtConcode =
231 *(struct tgsi_dst_register_ext_concode *) &token;
232 break;
233
234 case TGSI_DST_REGISTER_EXT_TYPE_MODULATE:
235 inst->FullDstRegisters[i].DstRegisterExtModulate =
236 *(struct tgsi_dst_register_ext_modulate *) &token;
237 break;
238
239 default:
240 assert( 0 );
241 }
242
243 extended = token.Extended;
244 }
245 }
246
247 assert( inst->Instruction.NumSrcRegs <= TGSI_FULL_MAX_SRC_REGISTERS );
248
249 for( i = 0; i < inst->Instruction.NumSrcRegs; i++ ) {
250 unsigned extended;
251
252 next_token( ctx, &inst->FullSrcRegisters[i].SrcRegister );
253
254 extended = inst->FullSrcRegisters[i].SrcRegister.Extended;
255
256 while( extended ) {
257 struct tgsi_src_register_ext token;
258
259 next_token( ctx, &token );
260
261 switch( token.Type ) {
262 case TGSI_SRC_REGISTER_EXT_TYPE_SWZ:
263 inst->FullSrcRegisters[i].SrcRegisterExtSwz =
264 *(struct tgsi_src_register_ext_swz *) &token;
265 break;
266
267 case TGSI_SRC_REGISTER_EXT_TYPE_MOD:
268 inst->FullSrcRegisters[i].SrcRegisterExtMod =
269 *(struct tgsi_src_register_ext_mod *) &token;
270 break;
271
272 default:
273 assert( 0 );
274 }
275
276 extended = token.Extended;
277 }
278
279 if( inst->FullSrcRegisters[i].SrcRegister.Indirect ) {
280 next_token( ctx, &inst->FullSrcRegisters[i].SrcRegisterInd );
281
282 /*
283 * No support for indirect or multi-dimensional addressing.
284 */
285 assert( !inst->FullSrcRegisters[i].SrcRegisterInd.Indirect );
286 assert( !inst->FullSrcRegisters[i].SrcRegisterInd.Dimension );
287 assert( !inst->FullSrcRegisters[i].SrcRegisterInd.Extended );
288 }
289
290 if( inst->FullSrcRegisters[i].SrcRegister.Dimension ) {
291 next_token( ctx, &inst->FullSrcRegisters[i].SrcRegisterDim );
292
293 /*
294 * No support for multi-dimensional addressing.
295 */
296 assert( !inst->FullSrcRegisters[i].SrcRegisterDim.Dimension );
297 assert( !inst->FullSrcRegisters[i].SrcRegisterDim.Extended );
298
299 if( inst->FullSrcRegisters[i].SrcRegisterDim.Indirect ) {
300 next_token( ctx, &inst->FullSrcRegisters[i].SrcRegisterDimInd );
301
302 /*
303 * No support for indirect or multi-dimensional addressing.
304 */
305 assert( !inst->FullSrcRegisters[i].SrcRegisterInd.Indirect );
306 assert( !inst->FullSrcRegisters[i].SrcRegisterInd.Dimension );
307 assert( !inst->FullSrcRegisters[i].SrcRegisterInd.Extended );
308 }
309 }
310 }
311
312 break;
313 }
314
315 default:
316 assert( 0 );
317 }
318 }
319
320
321 unsigned
322 tgsi_num_tokens(const struct tgsi_token *tokens)
323 {
324 struct tgsi_parse_context ctx;
325 if (tgsi_parse_init(&ctx, tokens) == TGSI_PARSE_OK) {
326 unsigned len = (ctx.FullHeader.Header.HeaderSize +
327 ctx.FullHeader.Header.BodySize +
328 1);
329 return len;
330 }
331 return 0;
332 }
333
334
335 /**
336 * Make a new copy of a token array.
337 */
338 struct tgsi_token *
339 tgsi_dup_tokens(const struct tgsi_token *tokens)
340 {
341 unsigned n = tgsi_num_tokens(tokens);
342 unsigned bytes = n * sizeof(struct tgsi_token);
343 struct tgsi_token *new_tokens = (struct tgsi_token *) MALLOC(bytes);
344 if (new_tokens)
345 memcpy(new_tokens, tokens, bytes);
346 return new_tokens;
347 }