1/************************************************************************** 2 * 3 * Copyright 2007 VMware, Inc. 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 VMWARE 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 33unsigned 34tgsi_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 52void 53tgsi_parse_free( 54 UNUSED struct tgsi_parse_context *ctx ) 55{ 56} 57 58boolean 59tgsi_parse_end_of_tokens( 60 struct tgsi_parse_context *ctx ) 61{ 62 /* All values involved are unsigned, but the sum will be promoted to 63 * a signed value (at least on 64 bit). To capture a possible overflow 64 * make it a signed comparison. 65 */ 66 return (int)ctx->Position >= 67 ctx->FullHeader.Header.HeaderSize + ctx->FullHeader.Header.BodySize; 68} 69 70 71/** 72 * This function is used to avoid and work-around type punning/aliasing 73 * warnings. The warnings seem harmless on x86 but on PPC they cause 74 * real failures. 75 */ 76static inline void 77copy_token(void *dst, const void *src) 78{ 79 memcpy(dst, src, 4); 80} 81 82 83/** 84 * Get next 4-byte token, return it at address specified by 'token' 85 */ 86static void 87next_token( 88 struct tgsi_parse_context *ctx, 89 void *token ) 90{ 91 assert( !tgsi_parse_end_of_tokens( ctx ) ); 92 copy_token(token, &ctx->Tokens[ctx->Position]); 93 ctx->Position++; 94} 95 96 97void 98tgsi_parse_token( 99 struct tgsi_parse_context *ctx ) 100{ 101 struct tgsi_token token; 102 unsigned i; 103 104 next_token( ctx, &token ); 105 106 switch( token.Type ) { 107 case TGSI_TOKEN_TYPE_DECLARATION: 108 { 109 struct tgsi_full_declaration *decl = &ctx->FullToken.FullDeclaration; 110 111 memset(decl, 0, sizeof *decl); 112 copy_token(&decl->Declaration, &token); 113 114 next_token( ctx, &decl->Range ); 115 116 if (decl->Declaration.Dimension) { 117 next_token(ctx, &decl->Dim); 118 } 119 120 if (decl->Declaration.Interpolate) { 121 next_token( ctx, &decl->Interp ); 122 } 123 124 if (decl->Declaration.Semantic) { 125 next_token( ctx, &decl->Semantic ); 126 } 127 128 if (decl->Declaration.File == TGSI_FILE_IMAGE) { 129 next_token(ctx, &decl->Image); 130 } 131 132 if (decl->Declaration.File == TGSI_FILE_SAMPLER_VIEW) { 133 next_token(ctx, &decl->SamplerView); 134 } 135 136 if (decl->Declaration.Array) { 137 next_token(ctx, &decl->Array); 138 } 139 140 break; 141 } 142 143 case TGSI_TOKEN_TYPE_IMMEDIATE: 144 { 145 struct tgsi_full_immediate *imm = &ctx->FullToken.FullImmediate; 146 uint imm_count; 147 148 memset(imm, 0, sizeof *imm); 149 copy_token(&imm->Immediate, &token); 150 151 imm_count = imm->Immediate.NrTokens - 1; 152 153 switch (imm->Immediate.DataType) { 154 case TGSI_IMM_FLOAT32: 155 case TGSI_IMM_FLOAT64: 156 for (i = 0; i < imm_count; i++) { 157 next_token(ctx, &imm->u[i].Float); 158 } 159 break; 160 161 case TGSI_IMM_UINT32: 162 case TGSI_IMM_UINT64: 163 for (i = 0; i < imm_count; i++) { 164 next_token(ctx, &imm->u[i].Uint); 165 } 166 break; 167 168 case TGSI_IMM_INT32: 169 case TGSI_IMM_INT64: 170 for (i = 0; i < imm_count; i++) { 171 next_token(ctx, &imm->u[i].Int); 172 } 173 break; 174 175 default: 176 assert( 0 ); 177 } 178 179 break; 180 } 181 182 case TGSI_TOKEN_TYPE_INSTRUCTION: 183 { 184 struct tgsi_full_instruction *inst = &ctx->FullToken.FullInstruction; 185 186 memset(inst, 0, sizeof *inst); 187 copy_token(&inst->Instruction, &token); 188 189 if (inst->Instruction.Label) { 190 next_token( ctx, &inst->Label); 191 } 192 193 if (inst->Instruction.Texture) { 194 next_token( ctx, &inst->Texture); 195 for (i = 0; i < inst->Texture.NumOffsets; i++) { 196 next_token( ctx, &inst->TexOffsets[i] ); 197 } 198 } 199 200 if (inst->Instruction.Memory) { 201 next_token(ctx, &inst->Memory); 202 } 203 204 assert( inst->Instruction.NumDstRegs <= TGSI_FULL_MAX_DST_REGISTERS ); 205 206 for (i = 0; i < inst->Instruction.NumDstRegs; i++) { 207 208 next_token( ctx, &inst->Dst[i].Register ); 209 210 if (inst->Dst[i].Register.Indirect) 211 next_token( ctx, &inst->Dst[i].Indirect ); 212 213 if (inst->Dst[i].Register.Dimension) { 214 next_token( ctx, &inst->Dst[i].Dimension ); 215 216 /* 217 * No support for multi-dimensional addressing. 218 */ 219 assert( !inst->Dst[i].Dimension.Dimension ); 220 221 if (inst->Dst[i].Dimension.Indirect) 222 next_token( ctx, &inst->Dst[i].DimIndirect ); 223 } 224 } 225 226 assert( inst->Instruction.NumSrcRegs <= TGSI_FULL_MAX_SRC_REGISTERS ); 227 228 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) { 229 230 next_token( ctx, &inst->Src[i].Register ); 231 232 if (inst->Src[i].Register.Indirect) 233 next_token( ctx, &inst->Src[i].Indirect ); 234 235 if (inst->Src[i].Register.Dimension) { 236 next_token( ctx, &inst->Src[i].Dimension ); 237 238 /* 239 * No support for multi-dimensional addressing. 240 */ 241 assert( !inst->Src[i].Dimension.Dimension ); 242 243 if (inst->Src[i].Dimension.Indirect) 244 next_token( ctx, &inst->Src[i].DimIndirect ); 245 } 246 } 247 248 break; 249 } 250 251 case TGSI_TOKEN_TYPE_PROPERTY: 252 { 253 struct tgsi_full_property *prop = &ctx->FullToken.FullProperty; 254 uint prop_count; 255 256 memset(prop, 0, sizeof *prop); 257 copy_token(&prop->Property, &token); 258 259 prop_count = prop->Property.NrTokens - 1; 260 for (i = 0; i < prop_count; i++) { 261 next_token(ctx, &prop->u[i]); 262 } 263 264 break; 265 } 266 267 default: 268 assert( 0 ); 269 } 270} 271 272 273 274 275/** 276 * Make a new copy of a token array. 277 */ 278struct tgsi_token * 279tgsi_dup_tokens(const struct tgsi_token *tokens) 280{ 281 unsigned n = tgsi_num_tokens(tokens); 282 unsigned bytes = n * sizeof(struct tgsi_token); 283 struct tgsi_token *new_tokens = (struct tgsi_token *) MALLOC(bytes); 284 if (new_tokens) 285 memcpy(new_tokens, tokens, bytes); 286 return new_tokens; 287} 288 289 290/** 291 * Allocate memory for num_tokens tokens. 292 */ 293struct tgsi_token * 294tgsi_alloc_tokens(unsigned num_tokens) 295{ 296 unsigned bytes = num_tokens * sizeof(struct tgsi_token); 297 return (struct tgsi_token *) MALLOC(bytes); 298} 299 300 301/** 302 * Free tokens allocated by tgsi_alloc_tokens() or tgsi_dup_tokens() 303 */ 304void 305tgsi_free_tokens(const struct tgsi_token *tokens) 306{ 307 FREE((void *) tokens); 308} 309 310 311void 312tgsi_dump_tokens(const struct tgsi_token *tokens) 313{ 314 const unsigned *dwords = (const unsigned *)tokens; 315 int nr = tgsi_num_tokens(tokens); 316 int i; 317 318 STATIC_ASSERT(sizeof(*tokens) == sizeof(unsigned)); 319 320 debug_printf("const unsigned tokens[%d] = {\n", nr); 321 for (i = 0; i < nr; i++) 322 debug_printf("0x%08x,\n", dwords[i]); 323 debug_printf("};\n"); 324} 325 326unsigned 327tgsi_get_processor_type(const struct tgsi_token *tokens) 328{ 329 struct tgsi_parse_context parse; 330 331 if (tgsi_parse_init( &parse, tokens ) != TGSI_PARSE_OK) { 332 debug_printf("tgsi_parse_init() failed in %s:%i!\n", __func__, __LINE__); 333 return ~0; 334 } 335 return parse.FullHeader.Processor.Processor; 336} 337