2 * Copyright © 2016 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 #include <util/macros.h>
35 #define XML_BUFFER_SIZE 4096
37 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
43 struct gen_group
*commands
[256];
45 struct gen_group
*structs
[256];
47 struct gen_group
*registers
[256];
55 struct parser_context
{
61 struct gen_group
*group
;
64 struct gen_field
*fields
[128];
66 struct gen_spec
*spec
;
70 gen_group_get_name(struct gen_group
*group
)
76 gen_group_get_opcode(struct gen_group
*group
)
82 gen_spec_find_struct(struct gen_spec
*spec
, const char *name
)
84 for (int i
= 0; i
< spec
->nstructs
; i
++)
85 if (strcmp(spec
->structs
[i
]->name
, name
) == 0)
86 return spec
->structs
[i
];
92 gen_spec_get_gen(struct gen_spec
*spec
)
97 static void __attribute__((noreturn
))
98 fail(struct location
*loc
, const char *msg
, ...)
103 fprintf(stderr
, "%s:%d: error: ",
104 loc
->filename
, loc
->line_number
);
105 vfprintf(stderr
, msg
, ap
);
106 fprintf(stderr
, "\n");
112 fail_on_null(void *p
)
115 fprintf(stderr
, "aubinator: out of memory\n");
123 xstrdup(const char *s
)
125 return fail_on_null(strdup(s
));
137 return fail_on_null(zalloc(s
));
140 static struct gen_group
*
141 create_group(struct parser_context
*ctx
, const char *name
, const char **atts
)
143 struct gen_group
*group
;
145 group
= xzalloc(sizeof(*group
));
147 group
->name
= xstrdup(name
);
149 group
->group_offset
= 0;
150 group
->group_count
= 0;
156 get_group_offset_count(struct parser_context
*ctx
, const char *name
, const char **atts
, uint32_t *offset
, uint32_t *count
)
161 for (i
= 0; atts
[i
]; i
+= 2) {
162 if (strcmp(atts
[i
], "count") == 0)
163 *count
= strtoul(atts
[i
+ 1], &p
, 0);
164 else if (strcmp(atts
[i
], "start") == 0)
165 *offset
= strtoul(atts
[i
+ 1], &p
, 0);
170 static inline uint64_t
171 mask(int start
, int end
)
175 v
= ~0ULL >> (63 - end
+ start
);
180 static inline uint64_t
181 field(uint64_t value
, int start
, int end
)
183 /* The field values are obtained from the DWord,
184 * Hence, get the relative start and end positions
185 * by doing a %32 on the start and end positions
187 return (value
& mask(start
% 32, end
% 32)) >> (start
% 32);
190 static inline uint64_t
191 field_address(uint64_t value
, int start
, int end
)
193 /* no need to right shift for address/offset */
194 return (value
& mask(start
% 32, end
% 32));
197 static struct gen_type
198 string_to_type(struct parser_context
*ctx
, const char *s
)
203 if (strcmp(s
, "int") == 0)
204 return (struct gen_type
) { .kind
= GEN_TYPE_INT
};
205 else if (strcmp(s
, "uint") == 0)
206 return (struct gen_type
) { .kind
= GEN_TYPE_UINT
};
207 else if (strcmp(s
, "bool") == 0)
208 return (struct gen_type
) { .kind
= GEN_TYPE_BOOL
};
209 else if (strcmp(s
, "float") == 0)
210 return (struct gen_type
) { .kind
= GEN_TYPE_FLOAT
};
211 else if (strcmp(s
, "address") == 0)
212 return (struct gen_type
) { .kind
= GEN_TYPE_ADDRESS
};
213 else if (strcmp(s
, "offset") == 0)
214 return (struct gen_type
) { .kind
= GEN_TYPE_OFFSET
};
215 else if (sscanf(s
, "u%d.%d", &i
, &f
) == 2)
216 return (struct gen_type
) { .kind
= GEN_TYPE_UFIXED
, .i
= i
, .f
= f
};
217 else if (sscanf(s
, "s%d.%d", &i
, &f
) == 2)
218 return (struct gen_type
) { .kind
= GEN_TYPE_SFIXED
, .i
= i
, .f
= f
};
219 else if (g
= gen_spec_find_struct(ctx
->spec
, s
), g
!= NULL
)
220 return (struct gen_type
) { .kind
= GEN_TYPE_STRUCT
, .gen_struct
= g
};
221 else if (strcmp(s
, "mbo") == 0)
222 return (struct gen_type
) { .kind
= GEN_TYPE_MBO
};
224 fail(&ctx
->loc
, "invalid type: %s", s
);
227 static struct gen_field
*
228 create_field(struct parser_context
*ctx
, const char **atts
)
230 struct gen_field
*field
;
234 field
= xzalloc(sizeof(*field
));
236 for (i
= 0; atts
[i
]; i
+= 2) {
237 if (strcmp(atts
[i
], "name") == 0)
238 field
->name
= xstrdup(atts
[i
+ 1]);
239 else if (strcmp(atts
[i
], "start") == 0)
240 field
->start
= ctx
->group
->group_offset
+strtoul(atts
[i
+ 1], &p
, 0);
241 else if (strcmp(atts
[i
], "end") == 0) {
242 field
->end
= ctx
->group
->group_offset
+strtoul(atts
[i
+ 1], &p
, 0);
243 if(ctx
->group
->group_offset
)
244 ctx
->group
->group_offset
= field
->end
+1;
245 } else if (strcmp(atts
[i
], "type") == 0)
246 field
->type
= string_to_type(ctx
, atts
[i
+ 1]);
247 else if (strcmp(atts
[i
], "default") == 0 &&
248 field
->start
>= 16 && field
->end
<= 31) {
249 field
->has_default
= true;
250 field
->default_value
= strtoul(atts
[i
+ 1], &p
, 0);
258 start_element(void *data
, const char *element_name
, const char **atts
)
260 struct parser_context
*ctx
= data
;
262 const char *name
= NULL
;
263 const char *gen
= NULL
;
265 ctx
->loc
.line_number
= XML_GetCurrentLineNumber(ctx
->parser
);
267 for (i
= 0; atts
[i
]; i
+= 2) {
268 if (strcmp(atts
[i
], "name") == 0)
270 else if (strcmp(atts
[i
], "gen") == 0)
274 if (strcmp(element_name
, "genxml") == 0) {
276 fail(&ctx
->loc
, "no platform name given");
278 fail(&ctx
->loc
, "no gen given");
280 ctx
->platform
= xstrdup(name
);
282 int n
= sscanf(gen
, "%d.%d", &major
, &minor
);
284 fail(&ctx
->loc
, "invalid gen given: %s", gen
);
288 ctx
->spec
->gen
= MAKE_GEN(major
, minor
);
289 } else if (strcmp(element_name
, "instruction") == 0 ||
290 strcmp(element_name
, "struct") == 0 ||
291 strcmp(element_name
, "register") == 0) {
292 ctx
->group
= create_group(ctx
, name
, atts
);
293 } else if (strcmp(element_name
, "group") == 0) {
294 get_group_offset_count(ctx
, name
, atts
,&ctx
->group
->group_offset
,&ctx
->group
->group_count
);
295 } else if (strcmp(element_name
, "field") == 0) {
297 ctx
->fields
[ctx
->nfields
++] = create_field(ctx
, atts
);
298 if(ctx
->group
->group_count
)
299 ctx
->group
->group_count
--;
300 } while (ctx
->group
->group_count
> 0);
301 } else if (strcmp(element_name
, "enum") == 0) {
302 } else if (strcmp(element_name
, "value") == 0) {
307 end_element(void *data
, const char *name
)
309 struct parser_context
*ctx
= data
;
311 if (strcmp(name
, "instruction") == 0 ||
312 strcmp(name
, "struct") == 0 ||
313 strcmp(name
, "register") == 0) {
314 size_t size
= ctx
->nfields
* sizeof(ctx
->fields
[0]);
315 struct gen_group
*group
= ctx
->group
;
317 group
->fields
= xzalloc(size
);
318 group
->nfields
= ctx
->nfields
;
319 memcpy(group
->fields
, ctx
->fields
, size
);
323 for (int i
= 0; i
< group
->nfields
; i
++) {
324 if (group
->fields
[i
]->start
>= 16 &&
325 group
->fields
[i
]->end
<= 31 &&
326 group
->fields
[i
]->has_default
) {
327 group
->opcode_mask
|=
328 mask(group
->fields
[i
]->start
% 32, group
->fields
[i
]->end
% 32);
330 group
->fields
[i
]->default_value
<< group
->fields
[i
]->start
;
334 struct gen_spec
*spec
= ctx
->spec
;
335 if (strcmp(name
, "instruction") == 0)
336 spec
->commands
[spec
->ncommands
++] = group
;
337 else if (strcmp(name
, "struct") == 0)
338 spec
->structs
[spec
->nstructs
++] = group
;
339 else if (strcmp(name
, "register") == 0)
340 spec
->registers
[spec
->nregisters
++] = group
;
341 } else if (strcmp(name
, "group") == 0) {
342 ctx
->group
->group_offset
= 0;
343 ctx
->group
->group_count
= 0;
348 character_data(void *data
, const XML_Char
*s
, int len
)
353 gen_spec_load(const char *filename
)
355 struct parser_context ctx
;
360 input
= fopen(filename
, "r");
361 printf("xml filename = %s\n", filename
);
363 fprintf(stderr
, "failed to open xml description\n");
367 memset(&ctx
, 0, sizeof ctx
);
368 ctx
.parser
= XML_ParserCreate(NULL
);
369 XML_SetUserData(ctx
.parser
, &ctx
);
370 if (ctx
.parser
== NULL
) {
371 fprintf(stderr
, "failed to create parser\n");
376 XML_SetElementHandler(ctx
.parser
, start_element
, end_element
);
377 XML_SetCharacterDataHandler(ctx
.parser
, character_data
);
378 ctx
.loc
.filename
= filename
;
380 ctx
.spec
= xzalloc(sizeof(*ctx
.spec
));
383 buf
= XML_GetBuffer(ctx
.parser
, XML_BUFFER_SIZE
);
384 len
= fread(buf
, 1, XML_BUFFER_SIZE
, input
);
386 fprintf(stderr
, "fread: %m\n");
390 if (XML_ParseBuffer(ctx
.parser
, len
, len
== 0) == 0) {
392 "Error parsing XML at line %ld col %ld: %s\n",
393 XML_GetCurrentLineNumber(ctx
.parser
),
394 XML_GetCurrentColumnNumber(ctx
.parser
),
395 XML_ErrorString(XML_GetErrorCode(ctx
.parser
)));
401 XML_ParserFree(ctx
.parser
);
408 gen_spec_find_instruction(struct gen_spec
*spec
, const uint32_t *p
)
410 for (int i
= 0; i
< spec
->ncommands
; i
++) {
411 uint32_t opcode
= *p
& spec
->commands
[i
]->opcode_mask
;
412 if (opcode
== spec
->commands
[i
]->opcode
)
413 return spec
->commands
[i
];
420 gen_group_get_length(struct gen_group
*group
, const uint32_t *p
)
423 uint32_t type
= field(h
, 29, 31);
427 uint32_t opcode
= field(h
, 23, 28);
431 return field(h
, 0, 7) + 2;
435 case 3: /* Render */ {
436 uint32_t subtype
= field(h
, 27, 28);
439 return field(h
, 0, 7) + 2;
445 return field(h
, 0, 7) + 2;
450 unreachable("bad opcode");
454 gen_field_iterator_init(struct gen_field_iterator
*iter
,
455 struct gen_group
*group
, const uint32_t *p
)
463 gen_field_iterator_next(struct gen_field_iterator
*iter
)
471 if (iter
->i
== iter
->group
->nfields
)
474 f
= iter
->group
->fields
[iter
->i
++];
475 iter
->name
= f
->name
;
476 v
.dw
= iter
->p
[f
->start
/ 32];
477 switch (f
->type
.kind
) {
478 case GEN_TYPE_UNKNOWN
:
480 snprintf(iter
->value
, sizeof(iter
->value
),
481 "%ld", field(v
.dw
, f
->start
, f
->end
));
484 snprintf(iter
->value
, sizeof(iter
->value
),
485 "%lu", field(v
.dw
, f
->start
, f
->end
));
488 snprintf(iter
->value
, sizeof(iter
->value
),
489 "%s", field(v
.dw
, f
->start
, f
->end
) ? "true" : "false");
492 snprintf(iter
->value
, sizeof(iter
->value
), "%f", v
.f
);
494 case GEN_TYPE_ADDRESS
:
495 case GEN_TYPE_OFFSET
:
496 snprintf(iter
->value
, sizeof(iter
->value
),
497 "0x%08lx", field_address(v
.dw
, f
->start
, f
->end
));
499 case GEN_TYPE_STRUCT
:
500 snprintf(iter
->value
, sizeof(iter
->value
),
501 "<struct %s %d>", f
->type
.gen_struct
->name
, (f
->start
/ 32));
503 case GEN_TYPE_UFIXED
:
504 snprintf(iter
->value
, sizeof(iter
->value
),
505 "%f", (float) field(v
.dw
, f
->start
, f
->end
) / (1 << f
->type
.f
));
507 case GEN_TYPE_SFIXED
:
508 /* FIXME: Sign extend extracted field. */
509 snprintf(iter
->value
, sizeof(iter
->value
), "%s", "foo");