10 static TypeList *thash[1021];
11 static TypeList *namehash[1021];
12 static TypeList *alltypes;
15 hash(uint num, uint num1)
17 return (num*1009 + num1*1013) % nelem(thash);
28 return h%nelem(namehash);
38 namehash[h] = mktl(t, namehash[h]);
48 h = hash(t->n1, t->n2);
49 thash[h] = mktl(t, thash[h]);
56 typebysue(char sue, char *name)
61 for(tl=namehash[shash(name)]; tl; tl=tl->tl){
63 if(t->sue==sue && t->suename && strcmp(name, t->suename)==0)
80 typebynum(uint n1, uint n2)
86 for(tl=thash[hash(n1, n2)]; tl; tl=tl->tl){
88 if(t->n1==n1 && t->n2==n2)
106 t = emalloc(sizeof *t);
108 alltypes = mktl(t, alltypes);
115 } fixes[] = { /* Font Tab 4 */
117 "builtin", "$builtin",
118 "complex", "$complex",
143 for(i=0; i<nelem(fixes); i++)
144 if(name[0]==fixes[i].old[0] && strcmp(name, fixes[i].old)==0)
152 memset(thash, 0, sizeof thash);
153 memset(namehash, 0, sizeof namehash);
157 renumber(TypeList *tl, uint n1)
161 for(; tl; tl=tl->tl){
177 while(t && (t->ty == Defer || t->ty == Typedef)){
181 if(t == u) /* cycle */
187 fprint(2, "cycle\n");
190 while(t && (t->ty==Defer || t->ty==Typedef)){
191 fprint(2, "t %p/%d %s\n", t, t->ty, t->name);
192 if(t == u && n++ == 2)
202 if(t->ty != Typedef && t->ty != Defer)
212 /* push names downward to remove anonymity */
213 if(t->name && t->sub->name == nil)
214 t->sub->name = t->name;
243 if(t != tt && tt->ty != Base)
246 t->xsizeof = tt->xsizeof;
252 t->xsizeof = countbytes(t->hi);
257 nameof(Type *t, int doanon)
259 static char buf[1024];
263 strcpy(buf, fixname(t->name));
265 snprint(buf, sizeof buf, "%s_%s", t->isunion ? "union" : "struct", t->suename);
267 snprint(buf, sizeof buf, "%s_%lud_", prefix, t->gen);
277 basecharof(Type *t) //XXX
292 else if(t->printfmt=='f')
299 else if(t->printfmt=='f')
307 nilstrcmp(char *a, char *b)
319 typecmp(Type *t, Type *u)
335 if(t->isunion != u->isunion)
336 return t->isunion - u->isunion;
338 i = nilstrcmp(t->name, u->name);
342 i = nilstrcmp(t->suename, u->suename);
346 if(t->name || t->suename)
356 i = strcmp(t->tname[0], u->tname[0]);
367 qtypecmp(const void *va, const void *vb)
373 return typecmp(t, u);
377 printtype(Biobuf *b, Type *t)
389 Bprint(b, "sizeof%s = %lud;\n", name, t->xsizeof);
390 Bprint(b, "aggr %s {\n", name);
392 for(j=0; j<t->n; j++){
397 Bprint(b, "// oops: nil type\n");
402 Bprint(b, "// oops: unknown type %d for %p/%s (%d,%d; %c,%s; %p)\n",
403 tt->ty, tt, fixname(t->tname[j]),
404 tt->n1, tt->n2, tt->sue ? tt->sue : '.', tt->suename, tt->sub);
405 Bprint(b, "// t->t[j] = %p\n", ttt=t->t[j]);
407 Bprint(b, "// %s %d (%d,%d) sub %p\n", ttt->name, ttt->ty, ttt->n1, ttt->n2, ttt->sub);
416 Bprint(b, "\t'%c' %lud %s;\n", basecharof(tt), t->val[j], fixname(t->tname[j]));
420 Bprint(b, "\t%s %lud %s;\n", nameof(tt, 1), t->val[j], fixname(t->tname[j]));
425 Bprint(b, "\t'X' 0 __dummy;\n");
428 name = nameof(t, 1); /* might have smashed it */
429 Bprint(b, "defn %s(addr) { indent_%s(addr, \"\"); }\n", name, name);
430 Bprint(b, "defn\nindent_%s(addr, indent) {\n", name);
431 Bprint(b, "\tcomplex %s addr;\n", name);
432 for(j=0; j<t->n; j++){
433 name = fixname(t->tname[j]);
436 Bprint(b, "// oops nil %s\n", name);
442 Bprint(b, "\tprint(indent, \"%s\t\", addr.%s, \"\\n\");\n",
446 ttt = defer(tt->sub);
447 if(ttt && ttt->ty == Aggr)
448 Bprint(b, "\tprint(indent, \"%s\t%s(\", addr.%s, \")\\n\");\n",
449 name, nameof(ttt, 1), name);
454 Bprint(b, "\tprint(indent, \"%s\t\", addr.%s\\X, \"\\n\");\n",
458 Bprint(b, "\tprint(indent, \"%s\t\", addr.%s, \" \", %s(addr.%s), \"\\n\");\n",
459 name, name, nameof(tt, 1), name);
462 Bprint(b, "\tprint(indent, \"%s\t%s{\\n\");\n",
463 name, nameof(tt, 0));
464 Bprint(b, "\tindent_%s(addr+%lud, indent+\" \");\n",
465 nameof(tt, 1), t->val[j]);
466 Bprint(b, "\tprint(indent, \"}\\n\");\n");
475 Bprint(b, "// enum %s\n", name);
476 for(j=0; j<t->n; j++)
477 Bprint(b, "%s = %ld;\n", fixname(t->tname[j]), t->val[j]);
479 Bprint(b, "vals_%s = {\n", name);
480 for(j=0; j<t->n; j++)
481 Bprint(b, "\t%lud,\n", t->val[j]);
483 Bprint(b, "names_%s = {\n", name);
484 for(j=0; j<t->n; j++)
485 Bprint(b, "\t\"%s\",\n", fixname(t->tname[j]));
487 Bprint(b, "defn\n%s(val) {\n", name);
488 Bprint(b, "\tlocal i;\n");
489 Bprint(b, "\ti = match(val, vals_%s);\n", name);
490 Bprint(b, "\tif i >= 0 then return names_%s[i];\n", name);
491 Bprint(b, "\treturn \"???\";\n");
498 printtypes(Biobuf *b)
504 /* check that pointer resolved */
505 for(tl=alltypes; tl; tl=tl->tl){
509 warn("type %d,%d referenced but not defined", t->n1, t->n2);
510 else if(t->sue && t->suename)
511 warn("%s %s referenced but not defined",
512 t->sue=='s' ? "struct" :
513 t->sue=='u' ? "union" :
514 t->sue=='e' ? "enum" : "???", t->suename);
518 /* push typedefs down, base types up */
520 for(tl=alltypes; tl; tl=tl->tl){
523 if(t->ty == Typedef || t->ty == Defer)
527 /* push ranges around */
528 for(tl=alltypes; tl; tl=tl->tl)
532 * only take one type of a given name; acid is going to do this anyway,
533 * and this will reduce the amount of code we output considerably.
534 * we could run a DFA equivalence relaxation sort of algorithm
535 * to find the actual equivalence classes, and then rename types
536 * appropriately, but this will do for now.
538 all = emalloc(n*sizeof(all[0]));
540 for(tl=alltypes; tl; tl=tl->tl)
543 qsort(all, n, sizeof(all[0]), qtypecmp);
547 if(i==0 || typecmp(all[i-1], all[i]) != 0)
550 all[i]->equiv = all[nn-1];
553 for(tl=alltypes; tl; tl=tl->tl){
557 t->equiv = tt->equiv;
561 printtype(b, all[i]);
569 memset(thash, 0, sizeof(thash));
570 memset(namehash, 0, sizeof namehash);