aubinator: Style fixes.
[mesa.git] / src / intel / tools / decoder.c
1 /*
2 * Copyright © 2016 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdio.h>
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <expat.h>
30
31 #include <util/macros.h>
32
33 #include "decoder.h"
34
35 #define XML_BUFFER_SIZE 4096
36
37 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
38
39 struct gen_spec {
40 uint32_t gen;
41
42 int ncommands;
43 struct gen_group *commands[256];
44 int nstructs;
45 struct gen_group *structs[256];
46 int nregisters;
47 struct gen_group *registers[256];
48 };
49
50 struct location {
51 const char *filename;
52 int line_number;
53 };
54
55 struct parser_context {
56 XML_Parser parser;
57 int foo;
58 struct location loc;
59 const char *platform;
60
61 struct gen_group *group;
62
63 int nfields;
64 struct gen_field *fields[128];
65
66 struct gen_spec *spec;
67 };
68
69 const char *
70 gen_group_get_name(struct gen_group *group)
71 {
72 return group->name;
73 }
74
75 uint32_t
76 gen_group_get_opcode(struct gen_group *group)
77 {
78 return group->opcode;
79 }
80
81 struct gen_group *
82 gen_spec_find_struct(struct gen_spec *spec, const char *name)
83 {
84 for (int i = 0; i < spec->nstructs; i++)
85 if (strcmp(spec->structs[i]->name, name) == 0)
86 return spec->structs[i];
87
88 return NULL;
89 }
90
91 uint32_t
92 gen_spec_get_gen(struct gen_spec *spec)
93 {
94 return spec->gen;
95 }
96
97 static void __attribute__((noreturn))
98 fail(struct location *loc, const char *msg, ...)
99 {
100 va_list ap;
101
102 va_start(ap, msg);
103 fprintf(stderr, "%s:%d: error: ",
104 loc->filename, loc->line_number);
105 vfprintf(stderr, msg, ap);
106 fprintf(stderr, "\n");
107 va_end(ap);
108 exit(EXIT_FAILURE);
109 }
110
111 static void *
112 fail_on_null(void *p)
113 {
114 if (p == NULL) {
115 fprintf(stderr, "aubinator: out of memory\n");
116 exit(EXIT_FAILURE);
117 }
118
119 return p;
120 }
121
122 static char *
123 xstrdup(const char *s)
124 {
125 return fail_on_null(strdup(s));
126 }
127
128 static void *
129 zalloc(size_t s)
130 {
131 return calloc(s, 1);
132 }
133
134 static void *
135 xzalloc(size_t s)
136 {
137 return fail_on_null(zalloc(s));
138 }
139
140 static struct gen_group *
141 create_group(struct parser_context *ctx, const char *name, const char **atts)
142 {
143 struct gen_group *group;
144
145 group = xzalloc(sizeof(*group));
146 if (name)
147 group->name = xstrdup(name);
148
149 group->group_offset = 0;
150 group->group_count = 0;
151
152 return group;
153 }
154
155 static void
156 get_group_offset_count(struct parser_context *ctx, const char *name,
157 const char **atts, uint32_t *offset, uint32_t *count)
158 {
159 char *p;
160 int i;
161
162 for (i = 0; atts[i]; i += 2) {
163 if (strcmp(atts[i], "count") == 0)
164 *count = strtoul(atts[i + 1], &p, 0);
165 else if (strcmp(atts[i], "start") == 0)
166 *offset = strtoul(atts[i + 1], &p, 0);
167 }
168 return;
169 }
170
171 static inline uint64_t
172 mask(int start, int end)
173 {
174 uint64_t v;
175
176 v = ~0ULL >> (63 - end + start);
177
178 return v << start;
179 }
180
181 static inline uint64_t
182 field(uint64_t value, int start, int end)
183 {
184 /* The field values are obtained from the DWord,
185 * Hence, get the relative start and end positions
186 * by doing a %32 on the start and end positions
187 */
188 return (value & mask(start % 32, end % 32)) >> (start % 32);
189 }
190
191 static inline uint64_t
192 field_address(uint64_t value, int start, int end)
193 {
194 /* no need to right shift for address/offset */
195 return (value & mask(start % 32, end % 32));
196 }
197
198 static struct gen_type
199 string_to_type(struct parser_context *ctx, const char *s)
200 {
201 int i, f;
202 struct gen_group *g;
203
204 if (strcmp(s, "int") == 0)
205 return (struct gen_type) { .kind = GEN_TYPE_INT };
206 else if (strcmp(s, "uint") == 0)
207 return (struct gen_type) { .kind = GEN_TYPE_UINT };
208 else if (strcmp(s, "bool") == 0)
209 return (struct gen_type) { .kind = GEN_TYPE_BOOL };
210 else if (strcmp(s, "float") == 0)
211 return (struct gen_type) { .kind = GEN_TYPE_FLOAT };
212 else if (strcmp(s, "address") == 0)
213 return (struct gen_type) { .kind = GEN_TYPE_ADDRESS };
214 else if (strcmp(s, "offset") == 0)
215 return (struct gen_type) { .kind = GEN_TYPE_OFFSET };
216 else if (sscanf(s, "u%d.%d", &i, &f) == 2)
217 return (struct gen_type) { .kind = GEN_TYPE_UFIXED, .i = i, .f = f };
218 else if (sscanf(s, "s%d.%d", &i, &f) == 2)
219 return (struct gen_type) { .kind = GEN_TYPE_SFIXED, .i = i, .f = f };
220 else if (g = gen_spec_find_struct(ctx->spec, s), g != NULL)
221 return (struct gen_type) { .kind = GEN_TYPE_STRUCT, .gen_struct = g };
222 else if (strcmp(s, "mbo") == 0)
223 return (struct gen_type) { .kind = GEN_TYPE_MBO };
224 else
225 fail(&ctx->loc, "invalid type: %s", s);
226 }
227
228 static struct gen_field *
229 create_field(struct parser_context *ctx, const char **atts)
230 {
231 struct gen_field *field;
232 char *p;
233 int i;
234
235 field = xzalloc(sizeof(*field));
236
237 for (i = 0; atts[i]; i += 2) {
238 if (strcmp(atts[i], "name") == 0)
239 field->name = xstrdup(atts[i + 1]);
240 else if (strcmp(atts[i], "start") == 0)
241 field->start = ctx->group->group_offset+strtoul(atts[i + 1], &p, 0);
242 else if (strcmp(atts[i], "end") == 0) {
243 field->end = ctx->group->group_offset+strtoul(atts[i + 1], &p, 0);
244 if (ctx->group->group_offset)
245 ctx->group->group_offset = field->end+1;
246 } else if (strcmp(atts[i], "type") == 0)
247 field->type = string_to_type(ctx, atts[i + 1]);
248 else if (strcmp(atts[i], "default") == 0 &&
249 field->start >= 16 && field->end <= 31) {
250 field->has_default = true;
251 field->default_value = strtoul(atts[i + 1], &p, 0);
252 }
253 }
254
255 return field;
256 }
257
258 static void
259 start_element(void *data, const char *element_name, const char **atts)
260 {
261 struct parser_context *ctx = data;
262 int i;
263 const char *name = NULL;
264 const char *gen = NULL;
265
266 ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);
267
268 for (i = 0; atts[i]; i += 2) {
269 if (strcmp(atts[i], "name") == 0)
270 name = atts[i + 1];
271 else if (strcmp(atts[i], "gen") == 0)
272 gen = atts[i + 1];
273 }
274
275 if (strcmp(element_name, "genxml") == 0) {
276 if (name == NULL)
277 fail(&ctx->loc, "no platform name given");
278 if (gen == NULL)
279 fail(&ctx->loc, "no gen given");
280
281 ctx->platform = xstrdup(name);
282 int major, minor;
283 int n = sscanf(gen, "%d.%d", &major, &minor);
284 if (n == 0)
285 fail(&ctx->loc, "invalid gen given: %s", gen);
286 if (n == 1)
287 minor = 0;
288
289 ctx->spec->gen = MAKE_GEN(major, minor);
290 } else if (strcmp(element_name, "instruction") == 0 ||
291 strcmp(element_name, "struct") == 0 ||
292 strcmp(element_name, "register") == 0) {
293 ctx->group = create_group(ctx, name, atts);
294 } else if (strcmp(element_name, "group") == 0) {
295 get_group_offset_count(ctx, name, atts, &ctx->group->group_offset,
296 &ctx->group->group_count);
297 } else if (strcmp(element_name, "field") == 0) {
298 do {
299 ctx->fields[ctx->nfields++] = create_field(ctx, atts);
300 if (ctx->group->group_count)
301 ctx->group->group_count--;
302 } while (ctx->group->group_count > 0);
303 } else if (strcmp(element_name, "enum") == 0) {
304 } else if (strcmp(element_name, "value") == 0) {
305 }
306 }
307
308 static void
309 end_element(void *data, const char *name)
310 {
311 struct parser_context *ctx = data;
312
313 if (strcmp(name, "instruction") == 0 ||
314 strcmp(name, "struct") == 0 ||
315 strcmp(name, "register") == 0) {
316 size_t size = ctx->nfields * sizeof(ctx->fields[0]);
317 struct gen_group *group = ctx->group;
318
319 group->fields = xzalloc(size);
320 group->nfields = ctx->nfields;
321 memcpy(group->fields, ctx->fields, size);
322 ctx->nfields = 0;
323 ctx->group = NULL;
324
325 for (int i = 0; i < group->nfields; i++) {
326 if (group->fields[i]->start >= 16 &&
327 group->fields[i]->end <= 31 &&
328 group->fields[i]->has_default) {
329 group->opcode_mask |=
330 mask(group->fields[i]->start % 32, group->fields[i]->end % 32);
331 group->opcode |=
332 group->fields[i]->default_value << group->fields[i]->start;
333 }
334 }
335
336 struct gen_spec *spec = ctx->spec;
337 if (strcmp(name, "instruction") == 0)
338 spec->commands[spec->ncommands++] = group;
339 else if (strcmp(name, "struct") == 0)
340 spec->structs[spec->nstructs++] = group;
341 else if (strcmp(name, "register") == 0)
342 spec->registers[spec->nregisters++] = group;
343 } else if (strcmp(name, "group") == 0) {
344 ctx->group->group_offset = 0;
345 ctx->group->group_count = 0;
346 }
347 }
348
349 static void
350 character_data(void *data, const XML_Char *s, int len)
351 {
352 }
353
354 struct gen_spec *
355 gen_spec_load(const char *filename)
356 {
357 struct parser_context ctx;
358 void *buf;
359 int len;
360 FILE *input;
361
362 input = fopen(filename, "r");
363 printf("xml filename = %s\n", filename);
364 if (input == NULL) {
365 fprintf(stderr, "failed to open xml description\n");
366 exit(EXIT_FAILURE);
367 }
368
369 memset(&ctx, 0, sizeof ctx);
370 ctx.parser = XML_ParserCreate(NULL);
371 XML_SetUserData(ctx.parser, &ctx);
372 if (ctx.parser == NULL) {
373 fprintf(stderr, "failed to create parser\n");
374 fclose(input);
375 return NULL;
376 }
377
378 XML_SetElementHandler(ctx.parser, start_element, end_element);
379 XML_SetCharacterDataHandler(ctx.parser, character_data);
380 ctx.loc.filename = filename;
381
382 ctx.spec = xzalloc(sizeof(*ctx.spec));
383
384 do {
385 buf = XML_GetBuffer(ctx.parser, XML_BUFFER_SIZE);
386 len = fread(buf, 1, XML_BUFFER_SIZE, input);
387 if (len < 0) {
388 fprintf(stderr, "fread: %m\n");
389 fclose(input);
390 return NULL;
391 }
392 if (XML_ParseBuffer(ctx.parser, len, len == 0) == 0) {
393 fprintf(stderr,
394 "Error parsing XML at line %ld col %ld: %s\n",
395 XML_GetCurrentLineNumber(ctx.parser),
396 XML_GetCurrentColumnNumber(ctx.parser),
397 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
398 fclose(input);
399 return NULL;
400 }
401 } while (len > 0);
402
403 XML_ParserFree(ctx.parser);
404 fclose(input);
405
406 return ctx.spec;
407 }
408
409 struct gen_group *
410 gen_spec_find_instruction(struct gen_spec *spec, const uint32_t *p)
411 {
412 for (int i = 0; i < spec->ncommands; i++) {
413 uint32_t opcode = *p & spec->commands[i]->opcode_mask;
414 if (opcode == spec->commands[i]->opcode)
415 return spec->commands[i];
416 }
417
418 return NULL;
419 }
420
421 int
422 gen_group_get_length(struct gen_group *group, const uint32_t *p)
423 {
424 uint32_t h = p[0];
425 uint32_t type = field(h, 29, 31);
426
427 switch (type) {
428 case 0: /* MI */ {
429 uint32_t opcode = field(h, 23, 28);
430 if (opcode < 16)
431 return 1;
432 else
433 return field(h, 0, 7) + 2;
434 break;
435 }
436
437 case 3: /* Render */ {
438 uint32_t subtype = field(h, 27, 28);
439 switch (subtype) {
440 case 0:
441 return field(h, 0, 7) + 2;
442 case 1:
443 return 1;
444 case 2:
445 return 2;
446 case 3:
447 return field(h, 0, 7) + 2;
448 }
449 }
450 }
451
452 unreachable("bad opcode");
453 }
454
455 void
456 gen_field_iterator_init(struct gen_field_iterator *iter,
457 struct gen_group *group, const uint32_t *p)
458 {
459 iter->group = group;
460 iter->p = p;
461 iter->i = 0;
462 }
463
464 bool
465 gen_field_iterator_next(struct gen_field_iterator *iter)
466 {
467 struct gen_field *f;
468 union {
469 uint32_t dw;
470 float f;
471 } v;
472
473 if (iter->i == iter->group->nfields)
474 return false;
475
476 f = iter->group->fields[iter->i++];
477 iter->name = f->name;
478 v.dw = iter->p[f->start / 32];
479 switch (f->type.kind) {
480 case GEN_TYPE_UNKNOWN:
481 case GEN_TYPE_INT:
482 snprintf(iter->value, sizeof(iter->value),
483 "%ld", field(v.dw, f->start, f->end));
484 break;
485 case GEN_TYPE_UINT:
486 snprintf(iter->value, sizeof(iter->value),
487 "%lu", field(v.dw, f->start, f->end));
488 break;
489 case GEN_TYPE_BOOL:
490 snprintf(iter->value, sizeof(iter->value),
491 "%s", field(v.dw, f->start, f->end) ? "true" : "false");
492 break;
493 case GEN_TYPE_FLOAT:
494 snprintf(iter->value, sizeof(iter->value), "%f", v.f);
495 break;
496 case GEN_TYPE_ADDRESS:
497 case GEN_TYPE_OFFSET:
498 snprintf(iter->value, sizeof(iter->value),
499 "0x%08lx", field_address(v.dw, f->start, f->end));
500 break;
501 case GEN_TYPE_STRUCT:
502 snprintf(iter->value, sizeof(iter->value),
503 "<struct %s %d>", f->type.gen_struct->name, (f->start / 32));
504 break;
505 case GEN_TYPE_UFIXED:
506 snprintf(iter->value, sizeof(iter->value),
507 "%f", (float) field(v.dw, f->start, f->end) / (1 << f->type.f));
508 break;
509 case GEN_TYPE_SFIXED:
510 /* FIXME: Sign extend extracted field. */
511 snprintf(iter->value, sizeof(iter->value), "%s", "foo");
512 break;
513 case GEN_TYPE_MBO:
514 break;
515 }
516
517 return true;
518 }