tgsi: rename fields of tgsi_full_instruction to avoid excessive verbosity
[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->FullVersion.Version = *(struct tgsi_version *) &tokens[0];
39 if( ctx->FullVersion.Version.Major > 1 ) {
40 return TGSI_PARSE_ERROR;
41 }
42
43 ctx->FullHeader.Header = *(struct tgsi_header *) &tokens[1];
44 if( ctx->FullHeader.Header.HeaderSize >= 2 ) {
45 ctx->FullHeader.Processor = *(struct tgsi_processor *) &tokens[2];
46 }
47 else {
48 return TGSI_PARSE_ERROR;
49 }
50
51 ctx->Tokens = tokens;
52 ctx->Position = 1 + ctx->FullHeader.Header.HeaderSize;
53
54 return TGSI_PARSE_OK;
55 }
56
57 void
58 tgsi_parse_free(
59 struct tgsi_parse_context *ctx )
60 {
61 }
62
63 boolean
64 tgsi_parse_end_of_tokens(
65 struct tgsi_parse_context *ctx )
66 {
67 return ctx->Position >=
68 1 + ctx->FullHeader.Header.HeaderSize + ctx->FullHeader.Header.BodySize;
69 }
70
71
72 /**
73 * This function is used to avoid and work-around type punning/aliasing
74 * warnings. The warnings seem harmless on x86 but on PPC they cause
75 * real failures.
76 */
77 static INLINE void
78 copy_token(void *dst, const void *src)
79 {
80 memcpy(dst, src, 4);
81 }
82
83
84 /**
85 * Get next 4-byte token, return it at address specified by 'token'
86 */
87 static void
88 next_token(
89 struct tgsi_parse_context *ctx,
90 void *token )
91 {
92 assert( !tgsi_parse_end_of_tokens( ctx ) );
93 copy_token(token, &ctx->Tokens[ctx->Position]);
94 ctx->Position++;
95 }
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 next_token( ctx, &token );
106
107 switch( token.Type ) {
108 case TGSI_TOKEN_TYPE_DECLARATION:
109 {
110 struct tgsi_full_declaration *decl = &ctx->FullToken.FullDeclaration;
111
112 memset(decl, 0, sizeof *decl);
113 copy_token(&decl->Declaration, &token);
114
115 next_token( ctx, &decl->DeclarationRange );
116
117 if( decl->Declaration.Semantic ) {
118 next_token( ctx, &decl->Semantic );
119 }
120
121 break;
122 }
123
124 case TGSI_TOKEN_TYPE_IMMEDIATE:
125 {
126 struct tgsi_full_immediate *imm = &ctx->FullToken.FullImmediate;
127
128 memset(imm, 0, sizeof *imm);
129 copy_token(&imm->Immediate, &token);
130
131 switch (imm->Immediate.DataType) {
132 case TGSI_IMM_FLOAT32:
133 {
134 uint imm_count = imm->Immediate.NrTokens - 1;
135 for (i = 0; i < imm_count; i++) {
136 next_token(ctx, &imm->u[i]);
137 }
138 }
139 break;
140
141 default:
142 assert( 0 );
143 }
144
145 break;
146 }
147
148 case TGSI_TOKEN_TYPE_INSTRUCTION:
149 {
150 struct tgsi_full_instruction *inst = &ctx->FullToken.FullInstruction;
151
152 memset(inst, 0, sizeof *inst);
153 copy_token(&inst->Instruction, &token);
154
155 if (inst->Instruction.Predicate) {
156 next_token(ctx, &inst->Predicate);
157 }
158
159 if (inst->Instruction.Label) {
160 next_token( ctx, &inst->Label);
161 }
162
163 if (inst->Instruction.Texture) {
164 next_token( ctx, &inst->Texture);
165 }
166
167 assert( inst->Instruction.NumDstRegs <= TGSI_FULL_MAX_DST_REGISTERS );
168
169 for( i = 0; i < inst->Instruction.NumDstRegs; i++ ) {
170
171 next_token( ctx, &inst->Dst[i].DstRegister );
172
173 /*
174 * No support for indirect or multi-dimensional addressing.
175 */
176 assert( !inst->Dst[i].DstRegister.Dimension );
177
178 if( inst->Dst[i].DstRegister.Indirect ) {
179 next_token( ctx, &inst->Dst[i].DstRegisterInd );
180
181 /*
182 * No support for indirect or multi-dimensional addressing.
183 */
184 assert( !inst->Dst[i].DstRegisterInd.Dimension );
185 assert( !inst->Dst[i].DstRegisterInd.Indirect );
186 }
187 }
188
189 assert( inst->Instruction.NumSrcRegs <= TGSI_FULL_MAX_SRC_REGISTERS );
190
191 for( i = 0; i < inst->Instruction.NumSrcRegs; i++ ) {
192
193 next_token( ctx, &inst->Src[i].SrcRegister );
194
195 if( inst->Src[i].SrcRegister.Indirect ) {
196 next_token( ctx, &inst->Src[i].SrcRegisterInd );
197
198 /*
199 * No support for indirect or multi-dimensional addressing.
200 */
201 assert( !inst->Src[i].SrcRegisterInd.Indirect );
202 assert( !inst->Src[i].SrcRegisterInd.Dimension );
203 }
204
205 if( inst->Src[i].SrcRegister.Dimension ) {
206 next_token( ctx, &inst->Src[i].SrcRegisterDim );
207
208 /*
209 * No support for multi-dimensional addressing.
210 */
211 assert( !inst->Src[i].SrcRegisterDim.Dimension );
212
213 if( inst->Src[i].SrcRegisterDim.Indirect ) {
214 next_token( ctx, &inst->Src[i].SrcRegisterDimInd );
215
216 /*
217 * No support for indirect or multi-dimensional addressing.
218 */
219 assert( !inst->Src[i].SrcRegisterInd.Indirect );
220 assert( !inst->Src[i].SrcRegisterInd.Dimension );
221 }
222 }
223 }
224
225 break;
226 }
227
228 default:
229 assert( 0 );
230 }
231 }
232
233
234 unsigned
235 tgsi_num_tokens(const struct tgsi_token *tokens)
236 {
237 struct tgsi_parse_context ctx;
238 if (tgsi_parse_init(&ctx, tokens) == TGSI_PARSE_OK) {
239 unsigned len = (ctx.FullHeader.Header.HeaderSize +
240 ctx.FullHeader.Header.BodySize +
241 1);
242 return len;
243 }
244 return 0;
245 }
246
247
248 /**
249 * Make a new copy of a token array.
250 */
251 struct tgsi_token *
252 tgsi_dup_tokens(const struct tgsi_token *tokens)
253 {
254 unsigned n = tgsi_num_tokens(tokens);
255 unsigned bytes = n * sizeof(struct tgsi_token);
256 struct tgsi_token *new_tokens = (struct tgsi_token *) MALLOC(bytes);
257 if (new_tokens)
258 memcpy(new_tokens, tokens, bytes);
259 return new_tokens;
260 }