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