freedreno/rnn: add relaxed boolean type
[mesa.git] / src / freedreno / rnn / rnn.c
1 /*
2 * Copyright (C) 2010-2011 Marcin Koƛcielnicki <koriakin@0x04.net>
3 * Copyright (C) 2010 Luca Barbieri <luca@luca-barbieri.com>
4 * Copyright (C) 2010 Francisco Jerez <currojerez@riseup.net>
5 * Copyright (C) 2010 Martin Peres <martin.peres@ensi-bourges.fr>
6 * Copyright (C) 2010 Marcin Slusarz <marcin.slusarz@gmail.com>
7 * All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
27 */
28
29 /* workaround libxml2 silliness: */
30 #pragma GCC diagnostic ignored "-Wpointer-sign"
31
32 #include <libxml/xmlversion.h>
33 #include <libxml/parser.h>
34 #include <libxml/xpath.h>
35 #include <libxml/xmlreader.h>
36 #include <stdint.h>
37 #include <string.h>
38 #include <limits.h>
39 #include <ctype.h>
40 #include <stdio.h>
41 #include "rnn.h"
42 #include "rnn_path.h"
43 #include "util.h"
44
45 #include "util/u_debug.h"
46
47 static char *catstr (char *a, char *b) {
48 if (!a)
49 return b;
50 return aprintf("%s_%s", a, b);
51 }
52
53 static int strdiff (const char *a, const char *b) {
54 if (!a && !b)
55 return 0;
56 if (!a || !b)
57 return 1;
58 return strcmp (a, b);
59 }
60
61 static void rnn_err(struct rnndb *db, const char *format, ...) _util_printf_format(2, 3);
62
63 static void rnn_err(struct rnndb *db, const char *format, ...)
64 {
65 va_list ap;
66 va_start(ap, format);
67 vfprintf(stderr, format, ap);
68 va_end(ap);
69 db->estatus = 1;
70 }
71
72 void rnn_init(void) {
73 LIBXML_TEST_VERSION
74 xmlInitParser();
75 }
76
77 struct rnndb *rnn_newdb(void) {
78 struct rnndb *db = calloc(sizeof *db, 1);
79 return db;
80 }
81
82 static char *getcontent (xmlNode *attr) {
83 xmlNode *chain = attr->children;
84 size_t size = 0;
85 char *content, *p;
86 while (chain) {
87 if (chain->type == XML_TEXT_NODE)
88 size += strlen(chain->content);
89 chain = chain->next;
90 }
91 p = content = malloc(size + 1);
92 chain = attr->children;
93 while (chain) {
94 if (chain->type == XML_TEXT_NODE) {
95 char* sp = chain->content;
96 if(p == content) {
97 while(isspace(*sp))
98 ++sp;
99 }
100 size_t len = strlen(sp);
101 memcpy(p, sp, len);
102 p += len;
103 }
104 chain = chain->next;
105 }
106 while(p != content && isspace(p[-1]))
107 --p;
108 *p = 0;
109 return content;
110 }
111
112 static char *getattrib (struct rnndb *db, char *file, int line, xmlAttr *attr) {
113 xmlNode *chain = attr->children;
114 while (chain) {
115 if (chain->type != XML_TEXT_NODE) {
116 rnn_err(db, "%s:%d: unknown attribute child \"%s\" in attribute \"%s\"\n", file, line, chain->name, attr->name);
117 } else {
118 return chain->content;
119 }
120 chain = chain->next;
121 }
122 return "";
123 }
124
125 static int getboolattrib (struct rnndb *db, char *file, int line, xmlAttr *attr) {
126 char *c = getattrib(db, file, line, attr);
127 if (!strcmp(c, "yes") || !strcmp(c, "1") || !strcmp(c, "true"))
128 return 1;
129 if (!strcmp(c, "no") || !strcmp(c, "0") || !strcmp(c, "false"))
130 return 0;
131 rnn_err(db, "%s:%d: invalid boolean value \"%s\" in attribute \"%s\"\n", file, line, c, attr->name);
132 return 0;
133 }
134
135 static uint64_t getnum(struct rnndb *db, char *file, int line, xmlAttr *attr, char *c)
136 {
137 char *cc;
138 uint64_t res;
139 if (strchr(c, 'x') || strchr(c, 'X'))
140 res = strtoull(c, &cc, 16);
141 else
142 res = strtoull(c, &cc, 10);
143 if (*cc) {
144 rnn_err(db, "%s:%d: invalid numeric value \"%s\" in attribute \"%s\"\n", file, line, c, attr->name);
145 }
146 return res;
147 }
148
149 static uint64_t getnumattrib (struct rnndb *db, char *file, int line, xmlAttr *attr) {
150 char *c = getattrib(db, file, line, attr);
151 return getnum(db, file, line, attr, c);
152 }
153
154 static int trytop (struct rnndb *db, char *file, xmlNode *node);
155
156 static int trydoc (struct rnndb *db, char *file, xmlNode *node) {
157 if (!strcmp(node->name, "brief")) {
158 return 1;
159 } else if (!strcmp(node->name, "doc")) {
160 return 1;
161 }
162 return 0;
163 }
164
165 static struct rnnvalue *parsevalue(struct rnndb *db, char *file, xmlNode *node);
166 static struct rnnbitfield *parsebitfield(struct rnndb *db, char *file, xmlNode *node);
167
168 static int trytypetag (struct rnndb *db, char *file, xmlNode *node, struct rnntypeinfo *ti) {
169 if (!strcmp(node->name, "value")) {
170 struct rnnvalue *val = parsevalue(db, file, node);
171 if (val)
172 ADDARRAY(ti->vals, val);
173 return 1;
174 } else if (!strcmp(node->name, "bitfield")) {
175 struct rnnbitfield *bf = parsebitfield(db, file, node);
176 if (bf)
177 ADDARRAY(ti->bitfields, bf);
178 return 1;
179 }
180 return 0;
181 }
182 static int trytypeattr (struct rnndb *db, char *file, xmlNode *node, xmlAttr *attr, struct rnntypeinfo *ti) {
183 if (!strcmp(attr->name, "shr")) {
184 ti->shr = getnumattrib(db, file, node->line, attr);
185 return 1;
186 } else if (!strcmp(attr->name, "min")) {
187 ti->min = getnumattrib(db, file, node->line, attr);
188 ti->minvalid = 1;
189 return 1;
190 } else if (!strcmp(attr->name, "max")) {
191 ti->max = getnumattrib(db, file, node->line, attr);
192 ti->maxvalid = 1;
193 return 1;
194 } else if (!strcmp(attr->name, "align")) {
195 ti->align = getnumattrib(db, file, node->line, attr);
196 ti->alignvalid = 1;
197 return 1;
198 } else if (!strcmp(attr->name, "type")) {
199 ti->name = strdup(getattrib(db, file, node->line, attr));;
200 return 1;
201 } else if (!strcmp(attr->name, "radix")) {
202 ti->radix = getnumattrib(db, file, node->line, attr);
203 ti->radixvalid = 1;
204 return 1;
205 } else if (!strcmp(attr->name, "pos")) {
206 ti->high = ti->low = getnumattrib(db, file, node->line, attr);
207 return 1;
208 } else if (!strcmp(attr->name, "low")) {
209 ti->low = getnumattrib(db, file, node->line, attr);
210 return 1;
211 } else if (!strcmp(attr->name, "high")) {
212 ti->high = getnumattrib(db, file, node->line, attr);
213 return 1;
214 } else if (!strcmp(attr->name, "addvariant")) {
215 ti->addvariant = getboolattrib(db, file, node->line, attr);
216 return 1;
217 }
218 return 0;
219 }
220
221 static struct rnnvalue *parsevalue(struct rnndb *db, char *file, xmlNode *node) {
222 struct rnnvalue *val = calloc(sizeof *val, 1);
223 val->file = file;
224 xmlAttr *attr = node->properties;
225 while (attr) {
226 if (!strcmp(attr->name, "name")) {
227 val->name = strdup(getattrib(db, file, node->line, attr));
228 } else if (!strcmp(attr->name, "value")) {
229 val->value = getnumattrib(db, file, node->line, attr);
230 val->valvalid = 1;
231 } else if (!strcmp(attr->name, "varset")) {
232 val->varinfo.varsetstr = strdup(getattrib(db, file, node->line, attr));
233 } else if (!strcmp(attr->name, "variants")) {
234 val->varinfo.variantsstr = strdup(getattrib(db, file, node->line, attr));
235 } else {
236 rnn_err(db, "%s:%d: wrong attribute \"%s\" for value\n", file, node->line, attr->name);
237 }
238 attr = attr->next;
239 }
240 xmlNode *chain = node->children;
241 while (chain) {
242 if (chain->type != XML_ELEMENT_NODE) {
243 } else if (!trytop(db, file, chain) && !trydoc(db, file, chain)) {
244 rnn_err(db, "%s:%d: wrong tag in %s: <%s>\n", file, chain->line, node->name, chain->name);
245 }
246 chain = chain->next;
247 }
248 if (!val->name) {
249 rnn_err(db, "%s:%d: nameless value\n", file, node->line);
250 return 0;
251 } else {
252 return val;
253 }
254 }
255
256 static void parsespectype(struct rnndb *db, char *file, xmlNode *node) {
257 struct rnnspectype *res = calloc (sizeof *res, 1);
258 res->file = file;
259 xmlAttr *attr = node->properties;
260 int i;
261 while (attr) {
262 if (!strcmp(attr->name, "name")) {
263 res->name = strdup(getattrib(db, file, node->line, attr));
264 } else if (!trytypeattr(db, file, node, attr, &res->typeinfo)) {
265 rnn_err(db, "%s:%d: wrong attribute \"%s\" for spectype\n", file, node->line, attr->name);
266 }
267 attr = attr->next;
268 }
269 if (!res->name) {
270 rnn_err(db, "%s:%d: nameless spectype\n", file, node->line);
271 return;
272 }
273 for (i = 0; i < db->spectypesnum; i++)
274 if (!strcmp(db->spectypes[i]->name, res->name)) {
275 rnn_err(db, "%s:%d: duplicated spectype name %s\n", file, node->line, res->name);
276 return;
277 }
278 ADDARRAY(db->spectypes, res);
279 xmlNode *chain = node->children;
280 while (chain) {
281 if (chain->type != XML_ELEMENT_NODE) {
282 } else if (!trytypetag(db, file, chain, &res->typeinfo) && !trytop(db, file, chain) && !trydoc(db, file, chain)) {
283 rnn_err(db, "%s:%d: wrong tag in spectype: <%s>\n", file, chain->line, chain->name);
284 }
285 chain = chain->next;
286 }
287 }
288
289 static void parseenum(struct rnndb *db, char *file, xmlNode *node) {
290 xmlAttr *attr = node->properties;
291 char *name = 0;
292 int isinline = 0;
293 int bare = 0;
294 char *prefixstr = 0;
295 char *varsetstr = 0;
296 char *variantsstr = 0;
297 int i;
298 while (attr) {
299 if (!strcmp(attr->name, "name")) {
300 name = getattrib(db, file, node->line, attr);
301 } else if (!strcmp(attr->name, "bare")) {
302 bare = getboolattrib(db, file, node->line, attr);
303 } else if (!strcmp(attr->name, "inline")) {
304 isinline = getboolattrib(db, file, node->line, attr);
305 } else if (!strcmp(attr->name, "prefix")) {
306 prefixstr = strdup(getattrib(db, file, node->line, attr));
307 } else if (!strcmp(attr->name, "varset")) {
308 varsetstr = strdup(getattrib(db, file, node->line, attr));
309 } else if (!strcmp(attr->name, "variants")) {
310 variantsstr = strdup(getattrib(db, file, node->line, attr));
311 } else {
312 rnn_err(db, "%s:%d: wrong attribute \"%s\" for enum\n", file, node->line, attr->name);
313 }
314 attr = attr->next;
315 }
316 if (!name) {
317 rnn_err(db, "%s:%d: nameless enum\n", file, node->line);
318 return;
319 }
320 struct rnnenum *cur = 0;
321 for (i = 0; i < db->enumsnum; i++)
322 if (!strcmp(db->enums[i]->name, name)) {
323 cur = db->enums[i];
324 break;
325 }
326 if (cur) {
327 if (strdiff(cur->varinfo.prefixstr, prefixstr) ||
328 strdiff(cur->varinfo.varsetstr, varsetstr) ||
329 strdiff(cur->varinfo.variantsstr, variantsstr) ||
330 cur->isinline != isinline || cur->bare != bare) {
331 rnn_err(db, "%s:%d: merge fail for enum %s\n", file, node->line, node->name);
332 }
333 } else {
334 cur = calloc(sizeof *cur, 1);
335 cur->name = strdup(name);
336 cur->isinline = isinline;
337 cur->bare = bare;
338 cur->varinfo.prefixstr = prefixstr;
339 cur->varinfo.varsetstr = varsetstr;
340 cur->varinfo.variantsstr = variantsstr;
341 cur->file = file;
342 ADDARRAY(db->enums, cur);
343 }
344 xmlNode *chain = node->children;
345 while (chain) {
346 if (chain->type != XML_ELEMENT_NODE) {
347 } else if (!strcmp(chain->name, "value")) {
348 struct rnnvalue *val = parsevalue(db, file, chain);
349 if (val)
350 ADDARRAY(cur->vals, val);
351 } else if (!trytop(db, file, chain) && !trydoc(db, file, chain)) {
352 rnn_err(db, "%s:%d: wrong tag in enum: <%s>\n", file, chain->line, chain->name);
353 }
354 chain = chain->next;
355 }
356 }
357
358 static struct rnnbitfield *parsebitfield(struct rnndb *db, char *file, xmlNode *node) {
359 struct rnnbitfield *bf = calloc(sizeof *bf, 1);
360 bf->file = file;
361 xmlAttr *attr = node->properties;
362 bf->typeinfo.low = bf->typeinfo.high = -1;
363 while (attr) {
364 if (!strcmp(attr->name, "name")) {
365 bf->name = strdup(getattrib(db, file, node->line, attr));
366 } else if (!strcmp(attr->name, "varset")) {
367 bf->varinfo.varsetstr = strdup(getattrib(db, file, node->line, attr));
368 } else if (!strcmp(attr->name, "variants")) {
369 bf->varinfo.variantsstr = strdup(getattrib(db, file, node->line, attr));
370 } else if (!trytypeattr(db, file, node, attr, &bf->typeinfo)) {
371 rnn_err(db, "%s:%d: wrong attribute \"%s\" for bitfield\n", file, node->line, attr->name);
372 }
373 attr = attr->next;
374 }
375 xmlNode *chain = node->children;
376 while (chain) {
377 if (chain->type != XML_ELEMENT_NODE) {
378 } else if (!trytypetag(db, file, chain, &bf->typeinfo) && !trytop(db, file, chain) && !trydoc(db, file, chain)) {
379 rnn_err(db, "%s:%d: wrong tag in %s: <%s>\n", file, chain->line, node->name, chain->name);
380 }
381 chain = chain->next;
382 }
383 if (!bf->name) {
384 rnn_err(db, "%s:%d: nameless bitfield\n", file, node->line);
385 return 0;
386 } else if (bf->typeinfo.low < 0|| bf->typeinfo.high < 0 || bf->typeinfo.high < bf->typeinfo.low) {
387 rnn_err(db, "%s:%d: bitfield has wrong placement\n", file, node->line);
388 return 0;
389 } else {
390 return bf;
391 }
392 }
393
394 static void parsebitset(struct rnndb *db, char *file, xmlNode *node) {
395 xmlAttr *attr = node->properties;
396 char *name = 0;
397 int isinline = 0;
398 int bare = 0;
399 char *prefixstr = 0;
400 char *varsetstr = 0;
401 char *variantsstr = 0;
402 int i;
403 while (attr) {
404 if (!strcmp(attr->name, "name")) {
405 name = getattrib(db, file, node->line, attr);
406 } else if (!strcmp(attr->name, "bare")) {
407 bare = getboolattrib(db, file, node->line, attr);
408 } else if (!strcmp(attr->name, "inline")) {
409 isinline = getboolattrib(db, file, node->line, attr);
410 } else if (!strcmp(attr->name, "prefix")) {
411 prefixstr = strdup(getattrib(db, file, node->line, attr));
412 } else if (!strcmp(attr->name, "varset")) {
413 varsetstr = strdup(getattrib(db, file, node->line, attr));
414 } else if (!strcmp(attr->name, "variants")) {
415 variantsstr = strdup(getattrib(db, file, node->line, attr));
416 } else {
417 rnn_err(db, "%s:%d: wrong attribute \"%s\" for bitset\n", file, node->line, attr->name);
418 }
419 attr = attr->next;
420 }
421 if (!name) {
422 rnn_err(db, "%s:%d: nameless bitset\n", file, node->line);
423 return;
424 }
425 struct rnnbitset *cur = 0;
426 for (i = 0; i < db->bitsetsnum; i++)
427 if (!strcmp(db->bitsets[i]->name, name)) {
428 cur = db->bitsets[i];
429 break;
430 }
431 if (cur) {
432 if (strdiff(cur->varinfo.prefixstr, prefixstr) ||
433 strdiff(cur->varinfo.varsetstr, varsetstr) ||
434 strdiff(cur->varinfo.variantsstr, variantsstr) ||
435 cur->isinline != isinline || cur->bare != bare) {
436 rnn_err(db, "%s:%d: merge fail for bitset %s\n", file, node->line, node->name);
437 }
438 } else {
439 cur = calloc(sizeof *cur, 1);
440 cur->name = strdup(name);
441 cur->isinline = isinline;
442 cur->bare = bare;
443 cur->varinfo.prefixstr = prefixstr;
444 cur->varinfo.varsetstr = varsetstr;
445 cur->varinfo.variantsstr = variantsstr;
446 cur->file = file;
447 ADDARRAY(db->bitsets, cur);
448 }
449 xmlNode *chain = node->children;
450 while (chain) {
451 if (chain->type != XML_ELEMENT_NODE) {
452 } else if (!strcmp(chain->name, "bitfield")) {
453 struct rnnbitfield *bf = parsebitfield(db, file, chain);
454 if (bf)
455 ADDARRAY(cur->bitfields, bf);
456 } else if (!trytop(db, file, chain) && !trydoc(db, file, chain)) {
457 rnn_err(db, "%s:%d: wrong tag in bitset: <%s>\n", file, chain->line, chain->name);
458 }
459 chain = chain->next;
460 }
461 }
462
463 static struct rnndelem *trydelem(struct rnndb *db, char *file, xmlNode *node) {
464 if (!strcmp(node->name, "use-group")) {
465 struct rnndelem *res = calloc(sizeof *res, 1);
466 res->file = file;
467 res->type = RNN_ETYPE_USE_GROUP;
468 xmlAttr *attr = node->properties;
469 while (attr) {
470 if (!strcmp(attr->name, "name")) {
471 res->name = strdup(getattrib(db, file, node->line, attr));
472 } else {
473 rnn_err(db, "%s:%d: wrong attribute \"%s\" for %s\n", file, node->line, attr->name, node->name);
474 }
475 attr = attr->next;
476 }
477 if (!res->name) {
478 rnn_err(db, "%s:%d: nameless use-group\n", file, node->line);
479 return 0;
480 }
481 return res;
482 } else if (!strcmp(node->name, "stripe") || !strcmp(node->name, "array")) {
483 struct rnndelem *res = calloc(sizeof *res, 1);
484 res->type = (strcmp(node->name, "stripe")?RNN_ETYPE_ARRAY:RNN_ETYPE_STRIPE);
485 res->length = 1;
486 res->file = file;
487 xmlAttr *attr = node->properties;
488 while (attr) {
489 if (!strcmp(attr->name, "name")) {
490 res->name = strdup(getattrib(db, file, node->line, attr));
491 } else if (!strcmp(attr->name, "offset")) {
492 res->offset = getnumattrib(db, file, node->line, attr);
493 } else if (!strcmp(attr->name, "offsets")) {
494 char *str = strdup(getattrib(db, file, node->line, attr));
495 char *tok, *save, *tmp = str;
496 while ((tok = strtok_r(str, ",", &save))) {
497 uint64_t offset = getnum(db, file, node->line, attr, tok);
498 ADDARRAY(res->offsets, offset);
499 str = NULL;
500 }
501 if (str)
502 fprintf(stderr, "%s:%d: invalid offsets: %s\n", file, node->line, str);
503 free(tmp);
504 } else if (!strcmp(attr->name, "doffset")) {
505 /* dynamic runtime determined offset: */
506 res->doffset = strdup(getattrib(db, file, node->line, attr));
507 } else if (!strcmp(attr->name, "doffsets")) {
508 /* dynamic runtime determined offsets: */
509 char *str = strdup(getattrib(db, file, node->line, attr));
510 char *tok, *save, *tmp = str;
511 while ((tok = strtok_r(str, ",", &save))) {
512 char *doffset = strdup(tok);
513 ADDARRAY(res->doffsets, doffset);
514 str = NULL;
515 }
516 if (str)
517 fprintf(stderr, "%s:%d: invalid offsets: %s\n", file, node->line, str);
518 free(tmp);
519 } else if (!strcmp(attr->name, "length")) {
520 res->length = getnumattrib(db, file, node->line, attr);
521 } else if (!strcmp(attr->name, "stride")) {
522 res->stride = getnumattrib(db, file, node->line, attr);
523 } else if (!strcmp(attr->name, "prefix")) {
524 res->varinfo.prefixstr = strdup(getattrib(db, file, node->line, attr));
525 } else if (!strcmp(attr->name, "varset")) {
526 res->varinfo.varsetstr = strdup(getattrib(db, file, node->line, attr));
527 } else if (!strcmp(attr->name, "variants")) {
528 res->varinfo.variantsstr = strdup(getattrib(db, file, node->line, attr));
529 } else if (!strcmp(attr->name, "index")) {
530 const char *enumname = getattrib(db, file, node->line, attr);
531 res->index = rnn_findenum(db, enumname);
532 if (!res->index) {
533 rnn_err(db, "%s:%d: invalid enum name \"%s\"\n", file, node->line, enumname);
534 }
535 } else {
536 rnn_err(db, "%s:%d: wrong attribute \"%s\" for %s\n", file, node->line, attr->name, node->name);
537 }
538 attr = attr->next;
539 }
540 xmlNode *chain = node->children;
541 while (chain) {
542 struct rnndelem *delem;
543 if (chain->type != XML_ELEMENT_NODE) {
544 } else if ((delem = trydelem(db, file, chain))) {
545 ADDARRAY(res->subelems, delem);
546 } else if (!trytop(db, file, chain) && !trydoc(db, file, chain)) {
547 rnn_err(db, "%s:%d: wrong tag in %s: <%s>\n", file, chain->line, node->name, chain->name);
548 }
549 chain = chain->next;
550 }
551
552 /* Sanity checking */
553 if (res->type == RNN_ETYPE_ARRAY && res->stride == 0) {
554 fprintf(stderr, "%s: Array %s's stride is undefined. Aborting.\n", file, res->name);
555 exit(-1);
556 }
557 return res;
558
559 }
560 int width;
561 if (!strcmp(node->name, "reg8"))
562 width = 8;
563 else if (!strcmp(node->name, "reg16"))
564 width = 16;
565 else if (!strcmp(node->name, "reg32"))
566 width = 32;
567 else if (!strcmp(node->name, "reg64"))
568 width = 64;
569 else
570 return 0;
571 struct rnndelem *res = calloc(sizeof *res, 1);
572 res->file = file;
573 res->type = RNN_ETYPE_REG;
574 res->width = width;
575 res->length = 1;
576 res->access = RNN_ACCESS_RW;
577 xmlAttr *attr = node->properties;
578 res->typeinfo.low = 0;
579 res->typeinfo.high = width - 1;
580 while (attr) {
581 if (!strcmp(attr->name, "name")) {
582 res->name = strdup(getattrib(db, file, node->line, attr));
583 } else if (!strcmp(attr->name, "offset")) {
584 res->offset = getnumattrib(db, file, node->line, attr);
585 } else if (!strcmp(attr->name, "length")) {
586 res->length = getnumattrib(db, file, node->line, attr);
587 } else if (!strcmp(attr->name, "stride")) {
588 res->stride = getnumattrib(db, file, node->line, attr);
589 } else if (!strcmp(attr->name, "varset")) {
590 res->varinfo.varsetstr = strdup(getattrib(db, file, node->line, attr));
591 } else if (!strcmp(attr->name, "variants")) {
592 res->varinfo.variantsstr = strdup(getattrib(db, file, node->line, attr));
593 } else if (!strcmp(attr->name, "access")) {
594 char *str = getattrib(db, file, node->line, attr);
595 if (!strcmp(str, "r"))
596 res->access = RNN_ACCESS_R;
597 else if (!strcmp(str, "w"))
598 res->access = RNN_ACCESS_W;
599 else if (!strcmp(str, "rw"))
600 res->access = RNN_ACCESS_RW;
601 else
602 fprintf (stderr, "%s:%d: wrong access type \"%s\" for register\n", file, node->line, str);
603 } else if (!trytypeattr(db, file, node, attr, &res->typeinfo)) {
604 rnn_err(db, "%s:%d: wrong attribute \"%s\" for register\n", file, node->line, attr->name);
605 }
606 attr = attr->next;
607 }
608 xmlNode *chain = node->children;
609 while (chain) {
610 if (chain->type != XML_ELEMENT_NODE) {
611 } else if (!trytypetag(db, file, chain, &res->typeinfo) && !trytop(db, file, chain) && !trydoc(db, file, chain)) {
612 rnn_err(db, "%s:%d: wrong tag in %s: <%s>\n", file, chain->line, node->name, chain->name);
613 }
614 chain = chain->next;
615 }
616 if (!res->name) {
617 rnn_err(db, "%s:%d: nameless register\n", file, node->line);
618 return 0;
619 } else {
620 }
621 return res;
622 }
623
624 static void parsegroup(struct rnndb *db, char *file, xmlNode *node) {
625 xmlAttr *attr = node->properties;
626 char *name = 0;
627 int i;
628 while (attr) {
629 if (!strcmp(attr->name, "name")) {
630 name = getattrib(db, file, node->line, attr);
631 } else {
632 rnn_err(db, "%s:%d: wrong attribute \"%s\" for group\n", file, node->line, attr->name);
633 }
634 attr = attr->next;
635 }
636 if (!name) {
637 rnn_err(db, "%s:%d: nameless group\n", file, node->line);
638 return;
639 }
640 struct rnngroup *cur = 0;
641 for (i = 0; i < db->groupsnum; i++)
642 if (!strcmp(db->groups[i]->name, name)) {
643 cur = db->groups[i];
644 break;
645 }
646 if (!cur) {
647 cur = calloc(sizeof *cur, 1);
648 cur->name = strdup(name);
649 ADDARRAY(db->groups, cur);
650 }
651 xmlNode *chain = node->children;
652 while (chain) {
653 struct rnndelem *delem;
654 if (chain->type != XML_ELEMENT_NODE) {
655 } else if ((delem = trydelem(db, file, chain))) {
656 ADDARRAY(cur->subelems, delem);
657 } else if (!trytop(db, file, chain) && !trydoc(db, file, chain)) {
658 rnn_err(db, "%s:%d: wrong tag in group: <%s>\n", file, chain->line, chain->name);
659 }
660 chain = chain->next;
661 }
662 }
663
664 static void parsedomain(struct rnndb *db, char *file, xmlNode *node) {
665 xmlAttr *attr = node->properties;
666 char *name = 0;
667 uint64_t size = 0; int width = 8;
668 int bare = 0;
669 char *prefixstr = 0;
670 char *varsetstr = 0;
671 char *variantsstr = 0;
672 int i;
673 while (attr) {
674 if (!strcmp(attr->name, "name")) {
675 name = getattrib(db, file, node->line, attr);
676 } else if (!strcmp(attr->name, "bare")) {
677 bare = getboolattrib(db, file, node->line, attr);
678 } else if (!strcmp(attr->name, "size")) {
679 size = getnumattrib(db, file, node->line, attr);
680 } else if (!strcmp(attr->name, "width")) {
681 width = getnumattrib(db, file, node->line, attr);
682 } else if (!strcmp(attr->name, "prefix")) {
683 prefixstr = strdup(getattrib(db, file, node->line, attr));
684 } else if (!strcmp(attr->name, "varset")) {
685 varsetstr = strdup(getattrib(db, file, node->line, attr));
686 } else if (!strcmp(attr->name, "variants")) {
687 variantsstr = strdup(getattrib(db, file, node->line, attr));
688 } else {
689 rnn_err(db, "%s:%d: wrong attribute \"%s\" for domain\n", file, node->line, attr->name);
690 }
691 attr = attr->next;
692 }
693 if (!name) {
694 rnn_err(db, "%s:%d: nameless domain\n", file, node->line);
695 return;
696 }
697 struct rnndomain *cur = 0;
698 for (i = 0; i < db->domainsnum; i++)
699 if (!strcmp(db->domains[i]->name, name)) {
700 cur = db->domains[i];
701 break;
702 }
703 if (cur) {
704 if (strdiff(cur->varinfo.prefixstr, prefixstr) ||
705 strdiff(cur->varinfo.varsetstr, varsetstr) ||
706 strdiff(cur->varinfo.variantsstr, variantsstr) ||
707 cur->width != width ||
708 cur->bare != bare ||
709 (size && cur->size && size != cur->size)) {
710 rnn_err(db, "%s:%d: merge fail for domain %s\n", file, node->line, node->name);
711 } else {
712 if (size)
713 cur->size = size;
714 }
715 } else {
716 cur = calloc(sizeof *cur, 1);
717 cur->name = strdup(name);
718 cur->bare = bare;
719 cur->width = width;
720 cur->size = size;
721 cur->varinfo.prefixstr = prefixstr;
722 cur->varinfo.varsetstr = varsetstr;
723 cur->varinfo.variantsstr = variantsstr;
724 cur->file = file;
725 ADDARRAY(db->domains, cur);
726 }
727 xmlNode *chain = node->children;
728 while (chain) {
729 struct rnndelem *delem;
730 if (chain->type != XML_ELEMENT_NODE) {
731 } else if ((delem = trydelem(db, file, chain))) {
732 ADDARRAY(cur->subelems, delem);
733 } else if (!trytop(db, file, chain) && !trydoc(db, file, chain)) {
734 rnn_err(db, "%s:%d: wrong tag in domain: <%s>\n", file, chain->line, chain->name);
735 }
736 chain = chain->next;
737 }
738 }
739
740 static void parsecopyright(struct rnndb *db, char *file, xmlNode *node) {
741 struct rnncopyright* copyright = &db->copyright;
742 xmlAttr *attr = node->properties;
743 while (attr) {
744 if (!strcmp(attr->name, "year")) {
745 unsigned firstyear = getnumattrib(db, file, node->line, attr);
746 if(!copyright->firstyear || firstyear < copyright->firstyear)
747 copyright->firstyear = firstyear;
748 } else {
749 rnn_err(db, "%s:%d: wrong attribute \"%s\" for copyright\n", file, node->line, attr->name);
750 }
751 attr = attr->next;
752 }
753 xmlNode *chain = node->children;
754 while (chain) {
755 if (chain->type != XML_ELEMENT_NODE) {
756 } else if (!strcmp(chain->name, "license"))
757 if(copyright->license) {
758 if(strcmp(copyright->license, node->content)) {
759 fprintf(stderr, "fatal error: multiple different licenses specified!\n");
760 abort(); /* TODO: do something better here, but headergen, xml2html, etc. should not produce anything in this case */
761 }
762 } else
763 copyright->license = getcontent(chain);
764 else if (!strcmp(chain->name, "author")) {
765 struct rnnauthor* author = calloc(sizeof *author, 1);
766 xmlAttr* authorattr = chain->properties;
767 xmlNode *authorchild = chain->children;
768 author->contributions = getcontent(chain);
769 while (authorattr) {
770 if (!strcmp(authorattr->name, "name"))
771 author->name = strdup(getattrib(db, file, chain->line, authorattr));
772 else if (!strcmp(authorattr->name, "email"))
773 author->email = strdup(getattrib(db, file, chain->line, authorattr));
774 else {
775 rnn_err(db, "%s:%d: wrong attribute \"%s\" for author\n", file, chain->line, authorattr->name);
776 }
777 authorattr = authorattr->next;
778 }
779 while(authorchild) {
780 if (authorchild->type != XML_ELEMENT_NODE) {
781 } else if (!strcmp(authorchild->name, "nick")) {
782 xmlAttr* nickattr = authorchild->properties;
783 char* nickname = 0;
784 while(nickattr) {
785 if (!strcmp(nickattr->name, "name"))
786 nickname = strdup(getattrib(db, file, authorchild->line, nickattr));
787 else {
788 rnn_err(db, "%s:%d: wrong attribute \"%s\" for nick\n", file, authorchild->line, nickattr->name);
789 }
790 nickattr = nickattr->next;
791 }
792 if(!nickname) {
793 rnn_err(db, "%s:%d: missing \"name\" attribute for nick\n", file, authorchild->line);
794 } else
795 ADDARRAY(author->nicknames, nickname);
796 } else {
797 rnn_err(db, "%s:%d: wrong tag in author: <%s>\n", file, authorchild->line, authorchild->name);
798 }
799 authorchild = authorchild->next;
800 }
801 ADDARRAY(copyright->authors, author);
802 } else {
803 rnn_err(db, "%s:%d: wrong tag in copyright: <%s>\n", file, chain->line, chain->name);
804 }
805 chain = chain->next;
806 }
807 }
808
809 static int trytop (struct rnndb *db, char *file, xmlNode *node) {
810 if (!strcmp(node->name, "enum")) {
811 parseenum(db, file, node);
812 return 1;
813 } else if (!strcmp(node->name, "bitset")) {
814 parsebitset(db, file, node);
815 return 1;
816 } else if (!strcmp(node->name, "group")) {
817 parsegroup(db, file, node);
818 return 1;
819 } else if (!strcmp(node->name, "domain")) {
820 parsedomain(db, file, node);
821 return 1;
822 } else if (!strcmp(node->name, "spectype")) {
823 parsespectype(db, file, node);
824 return 1;
825 } else if (!strcmp(node->name, "import")) {
826 xmlAttr *attr = node->properties;
827 char *subfile = 0;
828 while (attr) {
829 if (!strcmp(attr->name, "file")) {
830 subfile = getattrib(db, file, node->line, attr);
831 } else {
832 rnn_err(db, "%s:%d: wrong attribute \"%s\" for import\n", file, node->line, attr->name);
833 }
834 attr = attr->next;
835 }
836 if (!subfile) {
837 rnn_err(db, "%s:%d: missing \"file\" attribute for import\n", file, node->line);
838 } else {
839 rnn_parsefile(db, subfile);
840 }
841 return 1;
842 } else if (!strcmp(node->name, "copyright")) {
843 parsecopyright(db, file, node);
844 return 1;
845 }
846 return 0;
847 }
848
849 static char * find_file(const char *file_orig)
850 {
851 const char *rnn_path = getenv("RNN_PATH");
852 char *fname;
853
854 if (!rnn_path)
855 rnn_path = RNN_DEF_PATH;
856
857 FILE *file = find_in_path(file_orig, rnn_path, &fname);
858 if (!file) {
859 fprintf (stderr, "%s: couldn't find database file. Please set the env var RNN_PATH.\n", file_orig);
860 return NULL;
861 }
862 fclose(file);
863
864 return fname;
865 }
866
867 void rnn_parsefile (struct rnndb *db, char *file_orig) {
868 int i;
869 char *fname;
870
871 fname = find_file(file_orig);
872 if (!fname) {
873 db->estatus = 1;
874 return;
875 }
876
877 for (i = 0; i < db->filesnum; i++)
878 if (!strcmp(db->files[i], fname))
879 return;
880
881 ADDARRAY(db->files, fname);
882 xmlDocPtr doc = xmlParseFile(fname);
883 if (!doc) {
884 rnn_err(db, "%s: couldn't open database file. Please set the env var RNN_PATH.\n", fname);
885 return;
886 }
887 xmlNode *root = doc->children;
888 while (root) {
889 if (root->type != XML_ELEMENT_NODE) {
890 } else if (strcmp(root->name, "database")) {
891 rnn_err(db, "%s:%d: wrong top-level tag <%s>\n", fname, root->line, root->name);
892 } else {
893 xmlNode *chain = root->children;
894 while (chain) {
895 if (chain->type != XML_ELEMENT_NODE) {
896 } else if (!trytop(db, fname, chain) && !trydoc(db, fname, chain)) {
897 rnn_err(db, "%s:%d: wrong tag in database: <%s>\n", fname, chain->line, chain->name);
898 }
899 chain = chain->next;
900 }
901 }
902 root = root->next;
903 }
904 xmlFreeDoc(doc);
905 }
906
907 static struct rnnvalue *copyvalue (struct rnnvalue *val, char *file) {
908 struct rnnvalue *res = calloc (sizeof *res, 1);
909 res->name = val->name;
910 res->valvalid = val->valvalid;
911 res->value = val->value;
912 res->varinfo = val->varinfo;
913 res->file = file;
914 return res;
915 }
916
917 static struct rnnbitfield *copybitfield (struct rnnbitfield *bf, char *file);
918
919
920 static void copytypeinfo (struct rnntypeinfo *dst, struct rnntypeinfo *src, char *file) {
921 int i;
922 dst->name = src->name;
923 dst->shr = src->shr;
924 dst->low = src->low;
925 dst->high = src->high;
926 dst->min = src->min;
927 dst->max = src->max;
928 dst->align = src->align;
929 dst->addvariant = src->addvariant;
930 for (i = 0; i < src->valsnum; i++)
931 ADDARRAY(dst->vals, copyvalue(src->vals[i], file));
932 for (i = 0; i < src->bitfieldsnum; i++)
933 ADDARRAY(dst->bitfields, copybitfield(src->bitfields[i], file));
934 }
935
936 static struct rnnbitfield *copybitfield (struct rnnbitfield *bf, char *file) {
937 struct rnnbitfield *res = calloc (sizeof *res, 1);
938 res->name = bf->name;
939 res->varinfo = bf->varinfo;
940 res->file = file;
941 copytypeinfo(&res->typeinfo, &bf->typeinfo, file);
942 return res;
943 }
944
945 static struct rnndelem *copydelem (struct rnndelem *elem, char *file) {
946 struct rnndelem *res = calloc (sizeof *res, 1);
947 res->type = elem->type;
948 res->name = elem->name;
949 res->width = elem->width;
950 res->access = elem->access;
951 res->offset = elem->offset;
952 res->length = elem->length;
953 res->stride = elem->stride;
954 res->varinfo = elem->varinfo;
955 res->file = file;
956 copytypeinfo(&res->typeinfo, &elem->typeinfo, file);
957 int i;
958 for (i = 0; i < elem->subelemsnum; i++)
959 ADDARRAY(res->subelems, copydelem(elem->subelems[i], file));
960 for (i = 0; i < elem->offsetsnum; i++)
961 ADDARRAY(res->offsets, elem->offsets[i]);
962 return res;
963 }
964
965 static struct rnnvarset *copyvarset (struct rnnvarset *varset) {
966 struct rnnvarset *res = calloc(sizeof *res, 1);
967 res->venum = varset->venum;
968 res->variants = calloc(sizeof *res->variants, res->venum->valsnum);
969 int i;
970 for (i = 0; i < res->venum->valsnum; i++)
971 res->variants[i] = varset->variants[i];
972 return res;
973 }
974
975 static void prepenum(struct rnndb *db, struct rnnenum *en);
976
977 static int findvidx (struct rnndb *db, struct rnnenum *en, char *name) {
978 int i;
979 for (i = 0; i < en->valsnum; i++)
980 if (!strcmp(en->vals[i]->name, name))
981 return i;
982 rnn_err(db, "Cannot find variant %s in enum %s!\n", name, en->name);
983 return -1;
984 }
985
986 static void prepvarinfo (struct rnndb *db, char *what, struct rnnvarinfo *vi, struct rnnvarinfo *parent) {
987 if (parent)
988 vi->prefenum = parent->prefenum;
989 if (vi->prefixstr) {
990 if (!strcmp(vi->prefixstr, "none"))
991 vi->prefenum = 0;
992 else
993 vi->prefenum = rnn_findenum(db, vi->prefixstr); // XXX
994 }
995 int i;
996 if (parent)
997 for (i = 0; i < parent->varsetsnum; i++)
998 ADDARRAY(vi->varsets, copyvarset(parent->varsets[i]));
999 struct rnnenum *varset = vi->prefenum;
1000 if (!varset && !vi->varsetstr && parent)
1001 vi->varsetstr = parent->varsetstr;
1002 if (vi->varsetstr)
1003 varset = rnn_findenum(db, vi->varsetstr);
1004 if (vi->variantsstr) {
1005 char *vars = vi->variantsstr;
1006 if (!varset) {
1007 rnn_err(db, "%s: tried to use variants without active varset!\n", what);
1008 return;
1009 }
1010 struct rnnvarset *vs = 0;
1011 int nvars = varset->valsnum;
1012 for (i = 0; i < vi->varsetsnum; i++)
1013 if (vi->varsets[i]->venum == varset) {
1014 vs = vi->varsets[i];
1015 break;
1016 }
1017 if (!vs) {
1018 vs = calloc (sizeof *vs, 1);
1019 vs->venum = varset;
1020 vs->variants = calloc(sizeof *vs->variants, nvars);
1021 for (i = 0; i < nvars; i++)
1022 vs->variants[i] = 1;
1023 ADDARRAY(vi->varsets, vs);
1024 }
1025 while (1) {
1026 while (*vars == ' ') vars++;
1027 if (*vars == 0)
1028 break;
1029 char *split = vars;
1030 while (*split != ':' && *split != '-' && *split != ' ' && *split != 0)
1031 split++;
1032 char *first = 0;
1033 if (split != vars)
1034 first = strndup(vars, split-vars);
1035 if (*split == ' ' || *split == 0) {
1036 int idx = findvidx(db, varset, first);
1037 if (idx != -1)
1038 vs->variants[idx] |= 2;
1039 vars = split;
1040 } else {
1041 char *end = split+1;
1042 while (*end != ' ' && *end != 0)
1043 end++;
1044 char *second = 0;
1045 if (end != split+1)
1046 second = strndup(split+1, end-split-1);
1047 int idx1 = 0;
1048 if (first)
1049 idx1 = findvidx(db, varset, first);
1050 int idx2 = nvars;
1051 if (second) {
1052 idx2 = findvidx(db, varset, second);
1053 if (*split == '-')
1054 idx2++;
1055 }
1056 if (idx1 != -1 && idx2 != -1)
1057 for (i = idx1; i < idx2; i++)
1058 vs->variants[i] |= 2;
1059 vars = end;
1060 free(second);
1061 }
1062 free(first);
1063 }
1064 vi->dead = 1;
1065 for (i = 0; i < nvars; i++) {
1066 vs->variants[i] = (vs->variants[i] == 3);
1067 if (vs->variants[i])
1068 vi->dead = 0;
1069 }
1070 }
1071 if (vi->dead)
1072 return;
1073 if (vi->prefenum) {
1074 struct rnnvarset *vs = 0;
1075 for (i = 0; i < vi->varsetsnum; i++)
1076 if (vi->varsets[i]->venum == vi->prefenum) {
1077 vs = vi->varsets[i];
1078 break;
1079 }
1080 if (vs) {
1081 for (i = 0; i < vi->prefenum->valsnum; i++)
1082 if (vs->variants[i]) {
1083 vi->prefix = vi->prefenum->vals[i]->name;
1084 return;
1085 }
1086 } else {
1087 vi->prefix = vi->prefenum->vals[0]->name;
1088 }
1089 }
1090 }
1091
1092 static void prepvalue(struct rnndb *db, struct rnnvalue *val, char *prefix, struct rnnvarinfo *parvi) {
1093 val->fullname = catstr(prefix, val->name);
1094 prepvarinfo (db, val->fullname, &val->varinfo, parvi);
1095 if (val->varinfo.dead)
1096 return;
1097 if (val->varinfo.prefix)
1098 val->fullname = catstr(val->varinfo.prefix, val->fullname);
1099 }
1100
1101 static void prepbitfield(struct rnndb *db, struct rnnbitfield *bf, char *prefix, struct rnnvarinfo *parvi);
1102
1103 static void preptypeinfo(struct rnndb *db, struct rnntypeinfo *ti, char *prefix, struct rnnvarinfo *vi, char *file) {
1104 int i;
1105 if (ti->name) {
1106 struct rnnenum *en = rnn_findenum (db, ti->name);
1107 struct rnnbitset *bs = rnn_findbitset (db, ti->name);
1108 struct rnnspectype *st = rnn_findspectype (db, ti->name);
1109 if (en) {
1110 if (en->isinline) {
1111 ti->type = RNN_TTYPE_INLINE_ENUM;
1112 int j;
1113 for (j = 0; j < en->valsnum; j++)
1114 ADDARRAY(ti->vals, copyvalue(en->vals[j], file));
1115 } else {
1116 ti->type = RNN_TTYPE_ENUM;
1117 ti->eenum = en;
1118 }
1119 } else if (bs) {
1120 if (bs->isinline) {
1121 ti->type = RNN_TTYPE_INLINE_BITSET;
1122 int j;
1123 for (j = 0; j < bs->bitfieldsnum; j++)
1124 ADDARRAY(ti->bitfields, copybitfield(bs->bitfields[j], file));
1125 } else {
1126 ti->type = RNN_TTYPE_BITSET;
1127 ti->ebitset = bs;
1128 }
1129 } else if (st) {
1130 ti->type = RNN_TTYPE_SPECTYPE;
1131 ti->spectype = st;
1132 } else if (!strcmp(ti->name, "hex")) {
1133 ti->type = RNN_TTYPE_HEX;
1134 } else if (!strcmp(ti->name, "float")) {
1135 ti->type = RNN_TTYPE_FLOAT;
1136 } else if (!strcmp(ti->name, "uint")) {
1137 ti->type = RNN_TTYPE_UINT;
1138 } else if (!strcmp(ti->name, "int")) {
1139 ti->type = RNN_TTYPE_INT;
1140 } else if (!strcmp(ti->name, "boolean")) {
1141 ti->type = RNN_TTYPE_BOOLEAN;
1142 } else if (!strcmp(ti->name, "bitfield")) {
1143 ti->type = RNN_TTYPE_INLINE_BITSET;
1144 } else if (!strcmp(ti->name, "enum")) {
1145 ti->type = RNN_TTYPE_INLINE_ENUM;
1146 } else if (!strcmp(ti->name, "fixed")) {
1147 ti->type = RNN_TTYPE_FIXED;
1148 } else if (!strcmp(ti->name, "ufixed")) {
1149 ti->type = RNN_TTYPE_UFIXED;
1150 } else if (!strcmp(ti->name, "a3xx_regid")) {
1151 ti->type = RNN_TTYPE_A3XX_REGID;
1152 } else if (!strcmp(ti->name, "waddress") || !strcmp(ti->name, "address")) {
1153 ti->type = RNN_TTYPE_HEX;
1154 } else {
1155 ti->type = RNN_TTYPE_HEX;
1156 rnn_err(db, "%s: unknown type %s\n", prefix, ti->name);
1157 }
1158 } else if (ti->bitfieldsnum) {
1159 ti->name = "bitfield";
1160 ti->type = RNN_TTYPE_INLINE_BITSET;
1161 } else if (ti->valsnum) {
1162 ti->name = "enum";
1163 ti->type = RNN_TTYPE_INLINE_ENUM;
1164 } else if (ti->low == 0 && ti->high == 0) {
1165 ti->name = "boolean";
1166 ti->type = RNN_TTYPE_BOOLEAN;
1167 } else {
1168 ti->name = "hex";
1169 ti->type = RNN_TTYPE_HEX;
1170 }
1171 if (ti->addvariant && ti->type != RNN_TTYPE_ENUM) {
1172 rnn_err(db, "%s: addvariant specified on non-enum type %s\n", prefix, ti->name);
1173 }
1174 for (i = 0; i < ti->bitfieldsnum; i++)
1175 prepbitfield(db, ti->bitfields[i], prefix, vi);
1176 for (i = 0; i < ti->valsnum; i++)
1177 prepvalue(db, ti->vals[i], prefix, vi);
1178 }
1179
1180 static void prepbitfield(struct rnndb *db, struct rnnbitfield *bf, char *prefix, struct rnnvarinfo *parvi) {
1181 bf->fullname = catstr(prefix, bf->name);
1182 prepvarinfo (db, bf->fullname, &bf->varinfo, parvi);
1183 if (bf->varinfo.dead)
1184 return;
1185 preptypeinfo(db, &bf->typeinfo, bf->fullname, &bf->varinfo, bf->file);
1186 if (bf->varinfo.prefix)
1187 bf->fullname = catstr(bf->varinfo.prefix, bf->fullname);
1188 }
1189
1190 static void prepdelem(struct rnndb *db, struct rnndelem *elem, char *prefix, struct rnnvarinfo *parvi, int width) {
1191 if (elem->type == RNN_ETYPE_USE_GROUP) {
1192 int i;
1193 struct rnngroup *gr = 0;
1194 for (i = 0; i < db->groupsnum; i++)
1195 if (!strcmp(db->groups[i]->name, elem->name)) {
1196 gr = db->groups[i];
1197 break;
1198 }
1199 if (gr) {
1200 for (i = 0; i < gr->subelemsnum; i++)
1201 ADDARRAY(elem->subelems, copydelem(gr->subelems[i], elem->file));
1202 } else {
1203 rnn_err(db, "group %s not found!\n", elem->name);
1204 }
1205 elem->type = RNN_ETYPE_STRIPE;
1206 elem->length = 1;
1207 elem->name = 0;
1208 }
1209 if (elem->name)
1210 elem->fullname = catstr(prefix, elem->name);
1211 prepvarinfo (db, elem->fullname?elem->fullname:prefix, &elem->varinfo, parvi);
1212 if (elem->varinfo.dead)
1213 return;
1214 if (elem->length != 1 && !elem->stride) {
1215 if (elem->type != RNN_ETYPE_REG) {
1216 rnn_err(db, "%s has non-1 length, but no stride!\n", elem->fullname);
1217 } else {
1218 elem->stride = elem->width/width;
1219 }
1220 }
1221 preptypeinfo(db, &elem->typeinfo, elem->name?elem->fullname:prefix, &elem->varinfo, elem->file);
1222
1223 int i;
1224 for (i = 0; i < elem->subelemsnum; i++)
1225 prepdelem(db, elem->subelems[i], elem->name?elem->fullname:prefix, &elem->varinfo, width);
1226 if (elem->varinfo.prefix && elem->name)
1227 elem->fullname = catstr(elem->varinfo.prefix, elem->fullname);
1228 }
1229
1230 static void prepdomain(struct rnndb *db, struct rnndomain *dom) {
1231 prepvarinfo (db, dom->name, &dom->varinfo, 0);
1232 int i;
1233 for (i = 0; i < dom->subelemsnum; i++)
1234 prepdelem(db, dom->subelems[i], dom->bare?0:dom->name, &dom->varinfo, dom->width);
1235 dom->fullname = catstr(dom->varinfo.prefix, dom->name);
1236 }
1237
1238 static void prepenum(struct rnndb *db, struct rnnenum *en) {
1239 if (en->prepared)
1240 return;
1241 prepvarinfo (db, en->name, &en->varinfo, 0);
1242 int i;
1243 if (en->isinline)
1244 return;
1245 for (i = 0; i < en->valsnum; i++)
1246 prepvalue(db, en->vals[i], en->bare?0:en->name, &en->varinfo);
1247 en->fullname = catstr(en->varinfo.prefix, en->name);
1248 en->prepared = 1;
1249 }
1250
1251 static void prepbitset(struct rnndb *db, struct rnnbitset *bs) {
1252 prepvarinfo (db, bs->name, &bs->varinfo, 0);
1253 int i;
1254 if (bs->isinline)
1255 return;
1256 for (i = 0; i < bs->bitfieldsnum; i++)
1257 prepbitfield(db, bs->bitfields[i], bs->bare?0:bs->name, &bs->varinfo);
1258 bs->fullname = catstr(bs->varinfo.prefix, bs->name);
1259 }
1260
1261 static void prepspectype(struct rnndb *db, struct rnnspectype *st) {
1262 preptypeinfo(db, &st->typeinfo, st->name, 0, st->file); // XXX doesn't exactly make sense...
1263 }
1264
1265 void rnn_prepdb (struct rnndb *db) {
1266 int i;
1267 for (i = 0; i < db->enumsnum; i++)
1268 prepenum(db, db->enums[i]);
1269 for (i = 0; i < db->bitsetsnum; i++)
1270 prepbitset(db, db->bitsets[i]);
1271 for (i = 0; i < db->domainsnum; i++)
1272 prepdomain(db, db->domains[i]);
1273 for (i = 0; i < db->spectypesnum; i++)
1274 prepspectype(db, db->spectypes[i]);
1275 }
1276
1277 struct rnnenum *rnn_findenum (struct rnndb *db, const char *name) {
1278 int i;
1279 for (i = 0; i < db->enumsnum; i++)
1280 if (!strcmp(db->enums[i]->name, name))
1281 return db->enums[i];
1282 return 0;
1283 }
1284
1285 struct rnnbitset *rnn_findbitset (struct rnndb *db, const char *name) {
1286 int i;
1287 for (i = 0; i < db->bitsetsnum; i++)
1288 if (!strcmp(db->bitsets[i]->name, name))
1289 return db->bitsets[i];
1290 return 0;
1291 }
1292
1293 struct rnndomain *rnn_finddomain (struct rnndb *db, const char *name) {
1294 int i;
1295 for (i = 0; i < db->domainsnum; i++)
1296 if (!strcmp(db->domains[i]->name, name))
1297 return db->domains[i];
1298 return 0;
1299 }
1300
1301 struct rnnspectype *rnn_findspectype (struct rnndb *db, const char *name) {
1302 int i;
1303 for (i = 0; i < db->spectypesnum; i++)
1304 if (!strcmp(db->spectypes[i]->name, name))
1305 return db->spectypes[i];
1306 return 0;
1307 }