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)
318 int careaboutaggrcount;
321 typecmp(Type *t, Type *u)
337 if(t->isunion != u->isunion)
338 return t->isunion - u->isunion;
340 i = nilstrcmp(t->name, u->name);
344 i = nilstrcmp(t->suename, u->suename);
348 if(careaboutaggrcount && t->ty == Aggr){
355 if(t->name || t->suename)
365 i = strcmp(t->tname[0], u->tname[0]);
376 qtypecmp(const void *va, const void *vb)
382 return typecmp(t, u);
386 printtype(Biobuf *b, Type *t)
398 Bprint(b, "sizeof%s = %lud;\n", name, t->xsizeof);
399 Bprint(b, "aggr %s {\n", name);
401 for(j=0; j<t->n; j++){
406 Bprint(b, "// oops: nil type\n");
411 Bprint(b, "// oops: unknown type %d for %p/%s (%d,%d; %c,%s; %p)\n",
412 tt->ty, tt, fixname(t->tname[j]),
413 tt->n1, tt->n2, tt->sue ? tt->sue : '.', tt->suename, tt->sub);
414 Bprint(b, "// t->t[j] = %p\n", ttt=t->t[j]);
416 Bprint(b, "// %s %d (%d,%d) sub %p\n", ttt->name, ttt->ty, ttt->n1, ttt->n2, ttt->sub);
425 Bprint(b, "\t'%c' %lud %s;\n", basecharof(tt), t->val[j], fixname(t->tname[j]));
429 Bprint(b, "\t%s %lud %s;\n", nameof(tt, 1), t->val[j], fixname(t->tname[j]));
434 Bprint(b, "\t'X' 0 __dummy;\n");
437 name = nameof(t, 1); /* might have smashed it */
438 Bprint(b, "defn %s(addr) { indent_%s(addr, \"\"); }\n", name, name);
439 Bprint(b, "defn\nindent_%s(addr, indent) {\n", name);
440 Bprint(b, "\tcomplex %s addr;\n", name);
441 for(j=0; j<t->n; j++){
442 name = fixname(t->tname[j]);
445 Bprint(b, "// oops nil %s\n", name);
451 Bprint(b, "\tprint(indent, \"%s\t\", addr.%s, \"\\n\");\n",
455 ttt = defer(tt->sub);
456 if(ttt && ttt->ty == Aggr)
457 Bprint(b, "\tprint(indent, \"%s\t(%s)\", addr.%s, \"\\n\");\n",
458 name, nameof(ttt, 1), name);
463 Bprint(b, "\tprint(indent, \"%s\t\", addr.%s\\X, \"\\n\");\n",
467 Bprint(b, "\tprint(indent, \"%s\t\", addr.%s, \" \", %s(addr.%s), \"\\n\");\n",
468 name, name, nameof(tt, 1), name);
471 Bprint(b, "\tprint(indent, \"%s\t%s{\\n\");\n",
472 name, nameof(tt, 0));
473 Bprint(b, "\tindent_%s(addr+%lud, indent+\" \");\n",
474 nameof(tt, 1), t->val[j]);
475 Bprint(b, "\tprint(indent, \"}\\n\");\n");
484 Bprint(b, "// enum %s\n", name);
485 for(j=0; j<t->n; j++)
486 Bprint(b, "%s = %ld;\n", fixname(t->tname[j]), t->val[j]);
488 Bprint(b, "vals_%s = {\n", name);
489 for(j=0; j<t->n; j++)
490 Bprint(b, "\t%lud,\n", t->val[j]);
492 Bprint(b, "names_%s = {\n", name);
493 for(j=0; j<t->n; j++)
494 Bprint(b, "\t\"%s\",\n", fixname(t->tname[j]));
496 Bprint(b, "defn\n%s(val) {\n", name);
497 Bprint(b, "\tlocal i;\n");
498 Bprint(b, "\ti = match(val, vals_%s);\n", name);
499 Bprint(b, "\tif i >= 0 then return names_%s[i];\n", name);
500 Bprint(b, "\treturn \"???\";\n");
507 printtypes(Biobuf *b)
513 /* check that pointer resolved */
514 for(tl=alltypes; tl; tl=tl->tl){
518 warn("type %d,%d referenced but not defined", t->n1, t->n2);
519 else if(t->sue && t->suename)
520 warn("%s %s referenced but not defined",
521 t->sue=='s' ? "struct" :
522 t->sue=='u' ? "union" :
523 t->sue=='e' ? "enum" : "???", t->suename);
527 /* push typedefs down, base types up */
529 for(tl=alltypes; tl; tl=tl->tl){
532 if(t->ty == Typedef || t->ty == Defer)
536 /* push ranges around */
537 for(tl=alltypes; tl; tl=tl->tl)
541 * only take one type of a given name; acid is going to do this anyway,
542 * and this will reduce the amount of code we output considerably.
543 * we could run a DFA equivalence relaxation sort of algorithm
544 * to find the actual equivalence classes, and then rename types
545 * appropriately, but this will do for now.
547 all = emalloc(n*sizeof(all[0]));
549 for(tl=alltypes; tl; tl=tl->tl)
552 careaboutaggrcount = 1;
553 qsort(all, n, sizeof(all[0]), qtypecmp);
554 careaboutaggrcount = 0;
558 if(i==0 || typecmp(all[i-1], all[i]) != 0)
561 all[i]->equiv = all[nn-1];
564 for(tl=alltypes; tl; tl=tl->tl){
568 t->equiv = tt->equiv;
572 printtype(b, all[i]);
580 memset(thash, 0, sizeof(thash));
581 memset(namehash, 0, sizeof namehash);