[Mesa-dev] [PATCH 1/7] intel: decoder: build sorted linked lists of fields

Lionel Landwerlin lionel.g.landwerlin at intel.com
Thu Aug 3 14:37:39 UTC 2017


The xml files don't always have fields in order. This might confuse
our parsing of the commands. Let's have the fields in order. To do
this, the easiest way it to use a linked list. It also helps a bit
with the iterator.

Signed-off-by: Lionel Landwerlin <lionel.g.landwerlin at intel.com>
---
 src/intel/common/gen_decoder.c | 52 +++++++++++++++++++++++++-----------------
 src/intel/common/gen_decoder.h |  7 +++---
 2 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/src/intel/common/gen_decoder.c b/src/intel/common/gen_decoder.c
index 85880143f00..9b3588baed2 100644
--- a/src/intel/common/gen_decoder.c
+++ b/src/intel/common/gen_decoder.c
@@ -69,6 +69,9 @@ struct parser_context {
    int nvalues;
    struct gen_value *values[256];
 
+   struct gen_field *fields[256];
+   int nfields;
+
    struct gen_spec *spec;
 };
 
@@ -372,19 +375,25 @@ create_value(struct parser_context *ctx, const char **atts)
    return value;
 }
 
-static void
+static struct gen_field *
 create_and_append_field(struct parser_context *ctx,
                         const char **atts)
 {
-   if (ctx->group->nfields == ctx->group->fields_size) {
-      ctx->group->fields_size = MAX2(ctx->group->fields_size * 2, 2);
-      ctx->group->fields =
-         (struct gen_field **) realloc(ctx->group->fields,
-                                       sizeof(ctx->group->fields[0]) *
-                                       ctx->group->fields_size);
+   struct gen_field *field = create_field(ctx, atts);
+   struct gen_field *prev = NULL, *list = ctx->group->fields;
+
+   while (list && field->start > list->start) {
+      prev = list;
+      list = list->next;
    }
 
-   ctx->group->fields[ctx->group->nfields++] = create_field(ctx, atts);
+   field->next = list;
+   if (prev == NULL)
+      ctx->group->fields = field;
+   else
+      prev->next = field;
+
+   return field;
 }
 
 static void
@@ -434,7 +443,7 @@ start_element(void *data, const char *element_name, const char **atts)
       previous_group->next = group;
       ctx->group = group;
    } else if (strcmp(element_name, "field") == 0) {
-      create_and_append_field(ctx, atts);
+      ctx->fields[ctx->nfields++] = create_and_append_field(ctx, atts);
    } else if (strcmp(element_name, "enum") == 0) {
       ctx->enoom = create_enum(ctx, name, atts);
    } else if (strcmp(element_name, "value") == 0) {
@@ -454,18 +463,17 @@ end_element(void *data, const char *name)
        strcmp(name, "struct") == 0 ||
        strcmp(name, "register") == 0) {
       struct gen_group *group = ctx->group;
+      struct gen_field *list = group->fields;
 
       ctx->group = ctx->group->parent;
 
-      for (int i = 0; i < group->nfields; i++) {
-         if (group->fields[i]->start >= 16 &&
-             group->fields[i]->end <= 31 &&
-             group->fields[i]->has_default) {
+      while (list && list->end <= 31) {
+         if (list->start >= 16 && list->has_default) {
             group->opcode_mask |=
-               mask(group->fields[i]->start % 32, group->fields[i]->end % 32);
-            group->opcode |=
-               group->fields[i]->default_value << group->fields[i]->start;
+               mask(list->start % 32, list->end % 32);
+            group->opcode |= list->default_value << list->start;
          }
+         list = list->next;
       }
 
       if (strcmp(name, "instruction") == 0)
@@ -481,9 +489,10 @@ end_element(void *data, const char *name)
    } else if (strcmp(name, "group") == 0) {
       ctx->group = ctx->group->parent;
    } else if (strcmp(name, "field") == 0) {
-      assert(ctx->group->nfields > 0);
-      struct gen_field *field = ctx->group->fields[ctx->group->nfields - 1];
+      struct gen_field *field = ctx->fields[ctx->nfields - 1];
       size_t size = ctx->nvalues * sizeof(ctx->values[0]);
+      ctx->nfields--;
+      assert(ctx->nfields >= 0);
       field->inline_enum.values = xzalloc(size);
       field->inline_enum.nvalues = ctx->nvalues;
       memcpy(field->inline_enum.values, ctx->values, size);
@@ -771,6 +780,7 @@ gen_field_iterator_init(struct gen_field_iterator *iter,
    memset(iter, 0, sizeof(*iter));
 
    iter->group = group;
+   iter->field = group->fields;
    iter->p = p;
    iter->print_colors = print_colors;
 }
@@ -789,7 +799,7 @@ gen_get_enum_name(struct gen_enum *e, uint64_t value)
 static bool
 iter_more_fields(const struct gen_field_iterator *iter)
 {
-   return iter->field_iter < iter->group->nfields;
+   return iter->field != NULL && iter->field->next != NULL;
 }
 
 static uint32_t
@@ -825,7 +835,7 @@ iter_advance_group(struct gen_field_iterator *iter)
       }
    }
 
-   iter->field_iter = 0;
+   iter->field = iter->group->fields;
 }
 
 static bool
@@ -838,7 +848,7 @@ iter_advance_field(struct gen_field_iterator *iter)
       iter_advance_group(iter);
    }
 
-   iter->field = iter->group->fields[iter->field_iter++];
+   iter->field = iter->field->next;
    if (iter->field->name)
        strncpy(iter->name, iter->field->name, sizeof(iter->name));
    else
diff --git a/src/intel/common/gen_decoder.h b/src/intel/common/gen_decoder.h
index cfc9f2e3f15..2f05104178e 100644
--- a/src/intel/common/gen_decoder.h
+++ b/src/intel/common/gen_decoder.h
@@ -59,7 +59,6 @@ struct gen_field_iterator {
    const uint32_t *p;
    int dword; /**< current field starts at &p[dword] */
 
-   int field_iter;
    int group_iter;
 
    struct gen_field *field;
@@ -70,9 +69,7 @@ struct gen_group {
    struct gen_spec *spec;
    char *name;
 
-   struct gen_field **fields;
-   uint32_t nfields;
-   uint32_t fields_size;
+   struct gen_field *fields; /* linked list of fields */
 
    uint32_t group_offset, group_count;
    uint32_t group_size;
@@ -127,6 +124,8 @@ struct gen_type {
 };
 
 struct gen_field {
+   struct gen_field *next;
+
    char *name;
    int start, end;
    struct gen_type type;
-- 
2.13.3



More information about the mesa-dev mailing list