github.com/golang-haiku/go-1.4.3@v0.0.0-20190609233734-1f5ae41cc308/src/cmd/gc/subr.c (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 #include <u.h> 6 #include <libc.h> 7 #include "go.h" 8 #include "md5.h" 9 #include "y.tab.h" 10 #include "yerr.h" 11 12 typedef struct Error Error; 13 struct Error 14 { 15 int lineno; 16 int seq; 17 char *msg; 18 }; 19 static Error *err; 20 static int nerr; 21 static int merr; 22 23 void 24 errorexit(void) 25 { 26 flusherrors(); 27 if(outfile) 28 remove(outfile); 29 exits("error"); 30 } 31 32 extern int yychar; 33 int 34 parserline(void) 35 { 36 if(yychar != 0 && yychar != -2) // parser has one symbol lookahead 37 return prevlineno; 38 return lineno; 39 } 40 41 static void 42 adderr(int line, char *fmt, va_list arg) 43 { 44 Fmt f; 45 Error *p; 46 47 fmtstrinit(&f); 48 fmtprint(&f, "%L: ", line); 49 fmtvprint(&f, fmt, arg); 50 fmtprint(&f, "\n"); 51 52 if(nerr >= merr) { 53 if(merr == 0) 54 merr = 16; 55 else 56 merr *= 2; 57 p = realloc(err, merr*sizeof err[0]); 58 if(p == nil) { 59 merr = nerr; 60 flusherrors(); 61 print("out of memory\n"); 62 errorexit(); 63 } 64 err = p; 65 } 66 err[nerr].seq = nerr; 67 err[nerr].lineno = line; 68 err[nerr].msg = fmtstrflush(&f); 69 nerr++; 70 } 71 72 static int 73 errcmp(const void *va, const void *vb) 74 { 75 Error *a, *b; 76 77 a = (Error*)va; 78 b = (Error*)vb; 79 if(a->lineno != b->lineno) 80 return a->lineno - b->lineno; 81 if(a->seq != b->seq) 82 return a->seq - b->seq; 83 return strcmp(a->msg, b->msg); 84 } 85 86 void 87 flusherrors(void) 88 { 89 int i; 90 91 Bflush(&bstdout); 92 if(nerr == 0) 93 return; 94 qsort(err, nerr, sizeof err[0], errcmp); 95 for(i=0; i<nerr; i++) 96 if(i==0 || strcmp(err[i].msg, err[i-1].msg) != 0) 97 print("%s", err[i].msg); 98 nerr = 0; 99 } 100 101 static void 102 hcrash(void) 103 { 104 if(debug['h']) { 105 flusherrors(); 106 if(outfile) 107 remove(outfile); 108 *(volatile int*)0 = 0; 109 } 110 } 111 112 void 113 yyerrorl(int line, char *fmt, ...) 114 { 115 va_list arg; 116 117 va_start(arg, fmt); 118 adderr(line, fmt, arg); 119 va_end(arg); 120 121 hcrash(); 122 nerrors++; 123 if(nsavederrors+nerrors >= 10 && !debug['e']) { 124 flusherrors(); 125 print("%L: too many errors\n", line); 126 errorexit(); 127 } 128 } 129 130 extern int yystate, yychar; 131 132 void 133 yyerror(char *fmt, ...) 134 { 135 int i; 136 static int lastsyntax; 137 va_list arg; 138 char buf[512], *p; 139 140 if(strncmp(fmt, "syntax error", 12) == 0) { 141 nsyntaxerrors++; 142 143 if(debug['x']) 144 print("yyerror: yystate=%d yychar=%d\n", yystate, yychar); 145 146 // An unexpected EOF caused a syntax error. Use the previous 147 // line number since getc generated a fake newline character. 148 if(curio.eofnl) 149 lexlineno = prevlineno; 150 151 // only one syntax error per line 152 if(lastsyntax == lexlineno) 153 return; 154 lastsyntax = lexlineno; 155 156 if(strstr(fmt, "{ or {") || strstr(fmt, " or ?") || strstr(fmt, " or @")) { 157 // The grammar has { and LBRACE but both show up as {. 158 // Rewrite syntax error referring to "{ or {" to say just "{". 159 strecpy(buf, buf+sizeof buf, fmt); 160 p = strstr(buf, "{ or {"); 161 if(p) 162 memmove(p+1, p+6, strlen(p+6)+1); 163 164 // The grammar has ? and @ but only for reading imports. 165 // Silence them in ordinary errors. 166 p = strstr(buf, " or ?"); 167 if(p) 168 memmove(p, p+5, strlen(p+5)+1); 169 p = strstr(buf, " or @"); 170 if(p) 171 memmove(p, p+5, strlen(p+5)+1); 172 fmt = buf; 173 } 174 175 // look for parse state-specific errors in list (see go.errors). 176 for(i=0; i<nelem(yymsg); i++) { 177 if(yymsg[i].yystate == yystate && yymsg[i].yychar == yychar) { 178 yyerrorl(lexlineno, "syntax error: %s", yymsg[i].msg); 179 return; 180 } 181 } 182 183 // plain "syntax error" gets "near foo" added 184 if(strcmp(fmt, "syntax error") == 0) { 185 yyerrorl(lexlineno, "syntax error near %s", lexbuf); 186 return; 187 } 188 189 // if bison says "syntax error, more info"; print "syntax error: more info". 190 if(fmt[12] == ',') { 191 yyerrorl(lexlineno, "syntax error:%s", fmt+13); 192 return; 193 } 194 195 yyerrorl(lexlineno, "%s", fmt); 196 return; 197 } 198 199 va_start(arg, fmt); 200 adderr(parserline(), fmt, arg); 201 va_end(arg); 202 203 hcrash(); 204 nerrors++; 205 if(nsavederrors+nerrors >= 10 && !debug['e']) { 206 flusherrors(); 207 print("%L: too many errors\n", parserline()); 208 errorexit(); 209 } 210 } 211 212 void 213 warn(char *fmt, ...) 214 { 215 va_list arg; 216 217 va_start(arg, fmt); 218 adderr(parserline(), fmt, arg); 219 va_end(arg); 220 221 hcrash(); 222 } 223 224 void 225 warnl(int line, char *fmt, ...) 226 { 227 va_list arg; 228 229 va_start(arg, fmt); 230 adderr(line, fmt, arg); 231 va_end(arg); 232 if(debug['m']) 233 flusherrors(); 234 } 235 236 void 237 fatal(char *fmt, ...) 238 { 239 va_list arg; 240 241 flusherrors(); 242 243 print("%L: internal compiler error: ", lineno); 244 va_start(arg, fmt); 245 vfprint(1, fmt, arg); 246 va_end(arg); 247 print("\n"); 248 249 // If this is a released compiler version, ask for a bug report. 250 if(strncmp(getgoversion(), "release", 7) == 0) { 251 print("\n"); 252 print("Please file a bug report including a short program that triggers the error.\n"); 253 print("http://code.google.com/p/go/issues/entry?template=compilerbug\n"); 254 } 255 hcrash(); 256 errorexit(); 257 } 258 259 void 260 linehist(char *file, int32 off, int relative) 261 { 262 if(debug['i']) { 263 if(file != nil) { 264 if(off < 0) 265 print("pragma %s", file); 266 else 267 if(off > 0) 268 print("line %s", file); 269 else 270 print("import %s", file); 271 } else 272 print("end of import"); 273 print(" at line %L\n", lexlineno); 274 } 275 276 if(off < 0 && file[0] != '/' && !relative) 277 file = smprint("%s/%s", ctxt->pathname, file); 278 linklinehist(ctxt, lexlineno, file, off); 279 } 280 281 int32 282 setlineno(Node *n) 283 { 284 int32 lno; 285 286 lno = lineno; 287 if(n != N) 288 switch(n->op) { 289 case ONAME: 290 case OTYPE: 291 case OPACK: 292 case OLITERAL: 293 break; 294 default: 295 lineno = n->lineno; 296 if(lineno == 0) { 297 if(debug['K']) 298 warn("setlineno: line 0"); 299 lineno = lno; 300 } 301 } 302 return lno; 303 } 304 305 uint32 306 stringhash(char *p) 307 { 308 uint32 h; 309 int c; 310 311 h = 0; 312 for(;;) { 313 c = *p++; 314 if(c == 0) 315 break; 316 h = h*PRIME1 + c; 317 } 318 319 if((int32)h < 0) { 320 h = -h; 321 if((int32)h < 0) 322 h = 0; 323 } 324 return h; 325 } 326 327 Sym* 328 lookup(char *name) 329 { 330 return pkglookup(name, localpkg); 331 } 332 333 Sym* 334 pkglookup(char *name, Pkg *pkg) 335 { 336 Sym *s; 337 uint32 h; 338 int c; 339 340 h = stringhash(name) % NHASH; 341 c = name[0]; 342 for(s = hash[h]; s != S; s = s->link) { 343 if(s->name[0] != c || s->pkg != pkg) 344 continue; 345 if(strcmp(s->name, name) == 0) 346 return s; 347 } 348 349 s = mal(sizeof(*s)); 350 s->name = mal(strlen(name)+1); 351 strcpy(s->name, name); 352 353 s->pkg = pkg; 354 355 s->link = hash[h]; 356 hash[h] = s; 357 s->lexical = LNAME; 358 359 return s; 360 } 361 362 Sym* 363 restrictlookup(char *name, Pkg *pkg) 364 { 365 if(!exportname(name) && pkg != localpkg) 366 yyerror("cannot refer to unexported name %s.%s", pkg->name, name); 367 return pkglookup(name, pkg); 368 } 369 370 371 // find all the exported symbols in package opkg 372 // and make them available in the current package 373 void 374 importdot(Pkg *opkg, Node *pack) 375 { 376 Sym *s, *s1; 377 uint32 h; 378 int n; 379 char *pkgerror; 380 381 n = 0; 382 for(h=0; h<NHASH; h++) { 383 for(s = hash[h]; s != S; s = s->link) { 384 if(s->pkg != opkg) 385 continue; 386 if(s->def == N) 387 continue; 388 if(!exportname(s->name) || utfrune(s->name, 0xb7)) // 0xb7 = center dot 389 continue; 390 s1 = lookup(s->name); 391 if(s1->def != N) { 392 pkgerror = smprint("during import \"%Z\"", opkg->path); 393 redeclare(s1, pkgerror); 394 continue; 395 } 396 s1->def = s->def; 397 s1->block = s->block; 398 s1->def->pack = pack; 399 s1->origpkg = opkg; 400 n++; 401 } 402 } 403 if(n == 0) { 404 // can't possibly be used - there were no symbols 405 yyerrorl(pack->lineno, "imported and not used: \"%Z\"", opkg->path); 406 } 407 } 408 409 static void 410 gethunk(void) 411 { 412 char *h; 413 int32 nh; 414 415 nh = NHUNK; 416 if(thunk >= 10L*NHUNK) 417 nh = 10L*NHUNK; 418 h = (char*)malloc(nh); 419 if(h == nil) { 420 flusherrors(); 421 yyerror("out of memory"); 422 errorexit(); 423 } 424 hunk = h; 425 nhunk = nh; 426 thunk += nh; 427 } 428 429 void* 430 mal(int32 n) 431 { 432 void *p; 433 434 if(n >= NHUNK) { 435 p = malloc(n); 436 if(p == nil) { 437 flusherrors(); 438 yyerror("out of memory"); 439 errorexit(); 440 } 441 memset(p, 0, n); 442 return p; 443 } 444 445 while((uintptr)hunk & MAXALIGN) { 446 hunk++; 447 nhunk--; 448 } 449 if(nhunk < n) 450 gethunk(); 451 452 p = hunk; 453 nhunk -= n; 454 hunk += n; 455 memset(p, 0, n); 456 return p; 457 } 458 459 void* 460 remal(void *p, int32 on, int32 n) 461 { 462 void *q; 463 464 q = (uchar*)p + on; 465 if(q != hunk || nhunk < n) { 466 if(on+n >= NHUNK) { 467 q = mal(on+n); 468 memmove(q, p, on); 469 return q; 470 } 471 if(nhunk < on+n) 472 gethunk(); 473 memmove(hunk, p, on); 474 p = hunk; 475 hunk += on; 476 nhunk -= on; 477 } 478 hunk += n; 479 nhunk -= n; 480 return p; 481 } 482 483 Node* 484 nod(int op, Node *nleft, Node *nright) 485 { 486 Node *n; 487 488 n = mal(sizeof(*n)); 489 n->op = op; 490 n->left = nleft; 491 n->right = nright; 492 n->lineno = parserline(); 493 n->xoffset = BADWIDTH; 494 n->orig = n; 495 n->curfn = curfn; 496 return n; 497 } 498 499 void 500 saveorignode(Node *n) 501 { 502 Node *norig; 503 504 if(n->orig != N) 505 return; 506 norig = nod(n->op, N, N); 507 *norig = *n; 508 n->orig = norig; 509 } 510 511 // ispaddedfield reports whether the given field 512 // is followed by padding. For the case where t is 513 // the last field, total gives the size of the enclosing struct. 514 static int 515 ispaddedfield(Type *t, vlong total) 516 { 517 if(t->etype != TFIELD) 518 fatal("ispaddedfield called non-field %T", t); 519 if(t->down == T) 520 return t->width + t->type->width != total; 521 return t->width + t->type->width != t->down->width; 522 } 523 524 int 525 algtype1(Type *t, Type **bad) 526 { 527 int a, ret; 528 Type *t1; 529 530 if(bad) 531 *bad = T; 532 if(t->broke) 533 return AMEM; 534 if(t->noalg) 535 return ANOEQ; 536 537 switch(t->etype) { 538 case TANY: 539 case TFORW: 540 // will be defined later. 541 *bad = t; 542 return -1; 543 544 case TINT8: 545 case TUINT8: 546 case TINT16: 547 case TUINT16: 548 case TINT32: 549 case TUINT32: 550 case TINT64: 551 case TUINT64: 552 case TINT: 553 case TUINT: 554 case TUINTPTR: 555 case TBOOL: 556 case TPTR32: 557 case TPTR64: 558 case TCHAN: 559 case TUNSAFEPTR: 560 return AMEM; 561 562 case TFUNC: 563 case TMAP: 564 if(bad) 565 *bad = t; 566 return ANOEQ; 567 568 case TFLOAT32: 569 return AFLOAT32; 570 571 case TFLOAT64: 572 return AFLOAT64; 573 574 case TCOMPLEX64: 575 return ACPLX64; 576 577 case TCOMPLEX128: 578 return ACPLX128; 579 580 case TSTRING: 581 return ASTRING; 582 583 case TINTER: 584 if(isnilinter(t)) 585 return ANILINTER; 586 return AINTER; 587 588 case TARRAY: 589 if(isslice(t)) { 590 if(bad) 591 *bad = t; 592 return ANOEQ; 593 } 594 a = algtype1(t->type, bad); 595 if(a == ANOEQ || a == AMEM) { 596 if(a == ANOEQ && bad) 597 *bad = t; 598 return a; 599 } 600 return -1; // needs special compare 601 602 case TSTRUCT: 603 if(t->type != T && t->type->down == T && !isblanksym(t->type->sym)) { 604 // One-field struct is same as that one field alone. 605 return algtype1(t->type->type, bad); 606 } 607 ret = AMEM; 608 for(t1=t->type; t1!=T; t1=t1->down) { 609 // All fields must be comparable. 610 a = algtype1(t1->type, bad); 611 if(a == ANOEQ) 612 return ANOEQ; 613 614 // Blank fields, padded fields, fields with non-memory 615 // equality need special compare. 616 if(a != AMEM || isblanksym(t1->sym) || ispaddedfield(t1, t->width)) { 617 ret = -1; 618 continue; 619 } 620 } 621 return ret; 622 } 623 624 fatal("algtype1: unexpected type %T", t); 625 return 0; 626 } 627 628 int 629 algtype(Type *t) 630 { 631 int a; 632 633 a = algtype1(t, nil); 634 if(a == AMEM || a == ANOEQ) { 635 if(isslice(t)) 636 return ASLICE; 637 switch(t->width) { 638 case 0: 639 return a + AMEM0 - AMEM; 640 case 1: 641 return a + AMEM8 - AMEM; 642 case 2: 643 return a + AMEM16 - AMEM; 644 case 4: 645 return a + AMEM32 - AMEM; 646 case 8: 647 return a + AMEM64 - AMEM; 648 case 16: 649 return a + AMEM128 - AMEM; 650 } 651 } 652 return a; 653 } 654 655 Type* 656 maptype(Type *key, Type *val) 657 { 658 Type *t; 659 Type *bad; 660 int atype, mtype; 661 662 if(key != nil) { 663 atype = algtype1(key, &bad); 664 if(bad == T) 665 mtype = key->etype; 666 else 667 mtype = bad->etype; 668 switch(mtype) { 669 default: 670 if(atype == ANOEQ) 671 yyerror("invalid map key type %T", key); 672 break; 673 case TANY: 674 // will be resolved later. 675 break; 676 case TFORW: 677 // map[key] used during definition of key. 678 // postpone check until key is fully defined. 679 // if there are multiple uses of map[key] 680 // before key is fully defined, the error 681 // will only be printed for the first one. 682 // good enough. 683 if(key->maplineno == 0) 684 key->maplineno = lineno; 685 break; 686 } 687 } 688 t = typ(TMAP); 689 t->down = key; 690 t->type = val; 691 return t; 692 } 693 694 Type* 695 typ(int et) 696 { 697 Type *t; 698 699 t = mal(sizeof(*t)); 700 t->etype = et; 701 t->width = BADWIDTH; 702 t->lineno = lineno; 703 t->orig = t; 704 return t; 705 } 706 707 static int 708 methcmp(const void *va, const void *vb) 709 { 710 Type *a, *b; 711 int i; 712 713 a = *(Type**)va; 714 b = *(Type**)vb; 715 if(a->sym == S && b->sym == S) 716 return 0; 717 if(a->sym == S) 718 return -1; 719 if(b->sym == S) 720 return 1; 721 i = strcmp(a->sym->name, b->sym->name); 722 if(i != 0) 723 return i; 724 if(!exportname(a->sym->name)) { 725 i = strcmp(a->sym->pkg->path->s, b->sym->pkg->path->s); 726 if(i != 0) 727 return i; 728 } 729 return 0; 730 } 731 732 Type* 733 sortinter(Type *t) 734 { 735 Type *f; 736 int i; 737 Type **a; 738 739 if(t->type == nil || t->type->down == nil) 740 return t; 741 742 i=0; 743 for(f=t->type; f; f=f->down) 744 i++; 745 a = mal(i*sizeof f); 746 i = 0; 747 for(f=t->type; f; f=f->down) 748 a[i++] = f; 749 qsort(a, i, sizeof a[0], methcmp); 750 while(i-- > 0) { 751 a[i]->down = f; 752 f = a[i]; 753 } 754 t->type = f; 755 return t; 756 } 757 758 Node* 759 nodintconst(int64 v) 760 { 761 Node *c; 762 763 c = nod(OLITERAL, N, N); 764 c->addable = 1; 765 c->val.u.xval = mal(sizeof(*c->val.u.xval)); 766 mpmovecfix(c->val.u.xval, v); 767 c->val.ctype = CTINT; 768 c->type = types[TIDEAL]; 769 ullmancalc(c); 770 return c; 771 } 772 773 Node* 774 nodfltconst(Mpflt* v) 775 { 776 Node *c; 777 778 c = nod(OLITERAL, N, N); 779 c->addable = 1; 780 c->val.u.fval = mal(sizeof(*c->val.u.fval)); 781 mpmovefltflt(c->val.u.fval, v); 782 c->val.ctype = CTFLT; 783 c->type = types[TIDEAL]; 784 ullmancalc(c); 785 return c; 786 } 787 788 void 789 nodconst(Node *n, Type *t, int64 v) 790 { 791 memset(n, 0, sizeof(*n)); 792 n->op = OLITERAL; 793 n->addable = 1; 794 ullmancalc(n); 795 n->val.u.xval = mal(sizeof(*n->val.u.xval)); 796 mpmovecfix(n->val.u.xval, v); 797 n->val.ctype = CTINT; 798 n->type = t; 799 800 if(isfloat[t->etype]) 801 fatal("nodconst: bad type %T", t); 802 } 803 804 Node* 805 nodnil(void) 806 { 807 Node *c; 808 809 c = nodintconst(0); 810 c->val.ctype = CTNIL; 811 c->type = types[TNIL]; 812 return c; 813 } 814 815 Node* 816 nodbool(int b) 817 { 818 Node *c; 819 820 c = nodintconst(0); 821 c->val.ctype = CTBOOL; 822 c->val.u.bval = b; 823 c->type = idealbool; 824 return c; 825 } 826 827 Type* 828 aindex(Node *b, Type *t) 829 { 830 Type *r; 831 int64 bound; 832 833 bound = -1; // open bound 834 typecheck(&b, Erv); 835 if(b != nil) { 836 switch(consttype(b)) { 837 default: 838 yyerror("array bound must be an integer expression"); 839 break; 840 case CTINT: 841 case CTRUNE: 842 bound = mpgetfix(b->val.u.xval); 843 if(bound < 0) 844 yyerror("array bound must be non negative"); 845 break; 846 } 847 } 848 849 // fixed array 850 r = typ(TARRAY); 851 r->type = t; 852 r->bound = bound; 853 return r; 854 } 855 856 Node* 857 treecopy(Node *n) 858 { 859 Node *m; 860 861 if(n == N) 862 return N; 863 864 switch(n->op) { 865 default: 866 m = nod(OXXX, N, N); 867 *m = *n; 868 m->orig = m; 869 m->left = treecopy(n->left); 870 m->right = treecopy(n->right); 871 m->list = listtreecopy(n->list); 872 if(m->defn) 873 abort(); 874 break; 875 876 case ONONAME: 877 if(n->sym == lookup("iota")) { 878 // Not sure yet whether this is the real iota, 879 // but make a copy of the Node* just in case, 880 // so that all the copies of this const definition 881 // don't have the same iota value. 882 m = nod(OXXX, N, N); 883 *m = *n; 884 m->iota = iota; 885 break; 886 } 887 // fall through 888 case ONAME: 889 case OLITERAL: 890 case OTYPE: 891 m = n; 892 break; 893 } 894 return m; 895 } 896 897 898 int 899 isnil(Node *n) 900 { 901 if(n == N) 902 return 0; 903 if(n->op != OLITERAL) 904 return 0; 905 if(n->val.ctype != CTNIL) 906 return 0; 907 return 1; 908 } 909 910 int 911 isptrto(Type *t, int et) 912 { 913 if(t == T) 914 return 0; 915 if(!isptr[t->etype]) 916 return 0; 917 t = t->type; 918 if(t == T) 919 return 0; 920 if(t->etype != et) 921 return 0; 922 return 1; 923 } 924 925 int 926 istype(Type *t, int et) 927 { 928 return t != T && t->etype == et; 929 } 930 931 int 932 isfixedarray(Type *t) 933 { 934 return t != T && t->etype == TARRAY && t->bound >= 0; 935 } 936 937 int 938 isslice(Type *t) 939 { 940 return t != T && t->etype == TARRAY && t->bound < 0; 941 } 942 943 int 944 isblank(Node *n) 945 { 946 if(n == N) 947 return 0; 948 return isblanksym(n->sym); 949 } 950 951 int 952 isblanksym(Sym *s) 953 { 954 char *p; 955 956 if(s == S) 957 return 0; 958 p = s->name; 959 if(p == nil) 960 return 0; 961 return p[0] == '_' && p[1] == '\0'; 962 } 963 964 int 965 isinter(Type *t) 966 { 967 return t != T && t->etype == TINTER; 968 } 969 970 int 971 isnilinter(Type *t) 972 { 973 if(!isinter(t)) 974 return 0; 975 if(t->type != T) 976 return 0; 977 return 1; 978 } 979 980 int 981 isideal(Type *t) 982 { 983 if(t == T) 984 return 0; 985 if(t == idealstring || t == idealbool) 986 return 1; 987 switch(t->etype) { 988 case TNIL: 989 case TIDEAL: 990 return 1; 991 } 992 return 0; 993 } 994 995 /* 996 * given receiver of type t (t == r or t == *r) 997 * return type to hang methods off (r). 998 */ 999 Type* 1000 methtype(Type *t, int mustname) 1001 { 1002 if(t == T) 1003 return T; 1004 1005 // strip away pointer if it's there 1006 if(isptr[t->etype]) { 1007 if(t->sym != S) 1008 return T; 1009 t = t->type; 1010 if(t == T) 1011 return T; 1012 } 1013 1014 // need a type name 1015 if(t->sym == S && (mustname || t->etype != TSTRUCT)) 1016 return T; 1017 1018 // check types 1019 if(!issimple[t->etype]) 1020 switch(t->etype) { 1021 default: 1022 return T; 1023 case TSTRUCT: 1024 case TARRAY: 1025 case TMAP: 1026 case TCHAN: 1027 case TSTRING: 1028 case TFUNC: 1029 break; 1030 } 1031 1032 return t; 1033 } 1034 1035 int 1036 cplxsubtype(int et) 1037 { 1038 switch(et) { 1039 case TCOMPLEX64: 1040 return TFLOAT32; 1041 case TCOMPLEX128: 1042 return TFLOAT64; 1043 } 1044 fatal("cplxsubtype: %E\n", et); 1045 return 0; 1046 } 1047 1048 static int 1049 eqnote(Strlit *a, Strlit *b) 1050 { 1051 if(a == b) 1052 return 1; 1053 if(a == nil || b == nil) 1054 return 0; 1055 if(a->len != b->len) 1056 return 0; 1057 return memcmp(a->s, b->s, a->len) == 0; 1058 } 1059 1060 typedef struct TypePairList TypePairList; 1061 struct TypePairList 1062 { 1063 Type *t1; 1064 Type *t2; 1065 TypePairList *next; 1066 }; 1067 1068 static int 1069 onlist(TypePairList *l, Type *t1, Type *t2) 1070 { 1071 for(; l; l=l->next) 1072 if((l->t1 == t1 && l->t2 == t2) || (l->t1 == t2 && l->t2 == t1)) 1073 return 1; 1074 return 0; 1075 } 1076 1077 static int eqtype1(Type*, Type*, TypePairList*); 1078 1079 // Return 1 if t1 and t2 are identical, following the spec rules. 1080 // 1081 // Any cyclic type must go through a named type, and if one is 1082 // named, it is only identical to the other if they are the same 1083 // pointer (t1 == t2), so there's no chance of chasing cycles 1084 // ad infinitum, so no need for a depth counter. 1085 int 1086 eqtype(Type *t1, Type *t2) 1087 { 1088 return eqtype1(t1, t2, nil); 1089 } 1090 1091 static int 1092 eqtype1(Type *t1, Type *t2, TypePairList *assumed_equal) 1093 { 1094 TypePairList l; 1095 1096 if(t1 == t2) 1097 return 1; 1098 if(t1 == T || t2 == T || t1->etype != t2->etype) 1099 return 0; 1100 if(t1->sym || t2->sym) { 1101 // Special case: we keep byte and uint8 separate 1102 // for error messages. Treat them as equal. 1103 switch(t1->etype) { 1104 case TUINT8: 1105 if((t1 == types[TUINT8] || t1 == bytetype) && (t2 == types[TUINT8] || t2 == bytetype)) 1106 return 1; 1107 break; 1108 case TINT: 1109 case TINT32: 1110 if((t1 == types[runetype->etype] || t1 == runetype) && (t2 == types[runetype->etype] || t2 == runetype)) 1111 return 1; 1112 break; 1113 } 1114 return 0; 1115 } 1116 1117 if(onlist(assumed_equal, t1, t2)) 1118 return 1; 1119 l.next = assumed_equal; 1120 l.t1 = t1; 1121 l.t2 = t2; 1122 1123 switch(t1->etype) { 1124 case TINTER: 1125 case TSTRUCT: 1126 for(t1=t1->type, t2=t2->type; t1 && t2; t1=t1->down, t2=t2->down) { 1127 if(t1->etype != TFIELD || t2->etype != TFIELD) 1128 fatal("struct/interface missing field: %T %T", t1, t2); 1129 if(t1->sym != t2->sym || t1->embedded != t2->embedded || !eqtype1(t1->type, t2->type, &l) || !eqnote(t1->note, t2->note)) 1130 goto no; 1131 } 1132 if(t1 == T && t2 == T) 1133 goto yes; 1134 goto no; 1135 1136 case TFUNC: 1137 // Loop over structs: receiver, in, out. 1138 for(t1=t1->type, t2=t2->type; t1 && t2; t1=t1->down, t2=t2->down) { 1139 Type *ta, *tb; 1140 1141 if(t1->etype != TSTRUCT || t2->etype != TSTRUCT) 1142 fatal("func missing struct: %T %T", t1, t2); 1143 1144 // Loop over fields in structs, ignoring argument names. 1145 for(ta=t1->type, tb=t2->type; ta && tb; ta=ta->down, tb=tb->down) { 1146 if(ta->etype != TFIELD || tb->etype != TFIELD) 1147 fatal("func struct missing field: %T %T", ta, tb); 1148 if(ta->isddd != tb->isddd || !eqtype1(ta->type, tb->type, &l)) 1149 goto no; 1150 } 1151 if(ta != T || tb != T) 1152 goto no; 1153 } 1154 if(t1 == T && t2 == T) 1155 goto yes; 1156 goto no; 1157 1158 case TARRAY: 1159 if(t1->bound != t2->bound) 1160 goto no; 1161 break; 1162 1163 case TCHAN: 1164 if(t1->chan != t2->chan) 1165 goto no; 1166 break; 1167 } 1168 1169 if(eqtype1(t1->down, t2->down, &l) && eqtype1(t1->type, t2->type, &l)) 1170 goto yes; 1171 goto no; 1172 1173 yes: 1174 return 1; 1175 1176 no: 1177 return 0; 1178 } 1179 1180 // Are t1 and t2 equal struct types when field names are ignored? 1181 // For deciding whether the result struct from g can be copied 1182 // directly when compiling f(g()). 1183 int 1184 eqtypenoname(Type *t1, Type *t2) 1185 { 1186 if(t1 == T || t2 == T || t1->etype != TSTRUCT || t2->etype != TSTRUCT) 1187 return 0; 1188 1189 t1 = t1->type; 1190 t2 = t2->type; 1191 for(;;) { 1192 if(!eqtype(t1, t2)) 1193 return 0; 1194 if(t1 == T) 1195 return 1; 1196 t1 = t1->down; 1197 t2 = t2->down; 1198 } 1199 } 1200 1201 // Is type src assignment compatible to type dst? 1202 // If so, return op code to use in conversion. 1203 // If not, return 0. 1204 int 1205 assignop(Type *src, Type *dst, char **why) 1206 { 1207 Type *missing, *have; 1208 int ptr; 1209 1210 if(why != nil) 1211 *why = ""; 1212 1213 // TODO(rsc,lvd): This behaves poorly in the presence of inlining. 1214 // https://code.google.com/p/go/issues/detail?id=2795 1215 if(safemode && importpkg == nil && src != T && src->etype == TUNSAFEPTR) { 1216 yyerror("cannot use unsafe.Pointer"); 1217 errorexit(); 1218 } 1219 1220 if(src == dst) 1221 return OCONVNOP; 1222 if(src == T || dst == T || src->etype == TFORW || dst->etype == TFORW || src->orig == T || dst->orig == T) 1223 return 0; 1224 1225 // 1. src type is identical to dst. 1226 if(eqtype(src, dst)) 1227 return OCONVNOP; 1228 1229 // 2. src and dst have identical underlying types 1230 // and either src or dst is not a named type or 1231 // both are empty interface types. 1232 // For assignable but different non-empty interface types, 1233 // we want to recompute the itab. 1234 if(eqtype(src->orig, dst->orig) && (src->sym == S || dst->sym == S || isnilinter(src))) 1235 return OCONVNOP; 1236 1237 // 3. dst is an interface type and src implements dst. 1238 if(dst->etype == TINTER && src->etype != TNIL) { 1239 if(implements(src, dst, &missing, &have, &ptr)) 1240 return OCONVIFACE; 1241 1242 // we'll have complained about this method anyway, suppress spurious messages. 1243 if(have && have->sym == missing->sym && (have->type->broke || missing->type->broke)) 1244 return OCONVIFACE; 1245 1246 if(why != nil) { 1247 if(isptrto(src, TINTER)) 1248 *why = smprint(":\n\t%T is pointer to interface, not interface", src); 1249 else if(have && have->sym == missing->sym && have->nointerface) 1250 *why = smprint(":\n\t%T does not implement %T (%S method is marked 'nointerface')", 1251 src, dst, missing->sym); 1252 else if(have && have->sym == missing->sym) 1253 *why = smprint(":\n\t%T does not implement %T (wrong type for %S method)\n" 1254 "\t\thave %S%hhT\n\t\twant %S%hhT", src, dst, missing->sym, 1255 have->sym, have->type, missing->sym, missing->type); 1256 else if(ptr) 1257 *why = smprint(":\n\t%T does not implement %T (%S method has pointer receiver)", 1258 src, dst, missing->sym); 1259 else if(have) 1260 *why = smprint(":\n\t%T does not implement %T (missing %S method)\n" 1261 "\t\thave %S%hhT\n\t\twant %S%hhT", src, dst, missing->sym, 1262 have->sym, have->type, missing->sym, missing->type); 1263 else 1264 *why = smprint(":\n\t%T does not implement %T (missing %S method)", 1265 src, dst, missing->sym); 1266 } 1267 return 0; 1268 } 1269 if(isptrto(dst, TINTER)) { 1270 if(why != nil) 1271 *why = smprint(":\n\t%T is pointer to interface, not interface", dst); 1272 return 0; 1273 } 1274 if(src->etype == TINTER && dst->etype != TBLANK) { 1275 if(why != nil && implements(dst, src, &missing, &have, &ptr)) 1276 *why = ": need type assertion"; 1277 return 0; 1278 } 1279 1280 // 4. src is a bidirectional channel value, dst is a channel type, 1281 // src and dst have identical element types, and 1282 // either src or dst is not a named type. 1283 if(src->etype == TCHAN && src->chan == Cboth && dst->etype == TCHAN) 1284 if(eqtype(src->type, dst->type) && (src->sym == S || dst->sym == S)) 1285 return OCONVNOP; 1286 1287 // 5. src is the predeclared identifier nil and dst is a nillable type. 1288 if(src->etype == TNIL) { 1289 switch(dst->etype) { 1290 case TARRAY: 1291 if(dst->bound != -100) // not slice 1292 break; 1293 case TPTR32: 1294 case TPTR64: 1295 case TFUNC: 1296 case TMAP: 1297 case TCHAN: 1298 case TINTER: 1299 return OCONVNOP; 1300 } 1301 } 1302 1303 // 6. rule about untyped constants - already converted by defaultlit. 1304 1305 // 7. Any typed value can be assigned to the blank identifier. 1306 if(dst->etype == TBLANK) 1307 return OCONVNOP; 1308 1309 return 0; 1310 } 1311 1312 // Can we convert a value of type src to a value of type dst? 1313 // If so, return op code to use in conversion (maybe OCONVNOP). 1314 // If not, return 0. 1315 int 1316 convertop(Type *src, Type *dst, char **why) 1317 { 1318 int op; 1319 1320 if(why != nil) 1321 *why = ""; 1322 1323 if(src == dst) 1324 return OCONVNOP; 1325 if(src == T || dst == T) 1326 return 0; 1327 1328 // 1. src can be assigned to dst. 1329 if((op = assignop(src, dst, why)) != 0) 1330 return op; 1331 1332 // The rules for interfaces are no different in conversions 1333 // than assignments. If interfaces are involved, stop now 1334 // with the good message from assignop. 1335 // Otherwise clear the error. 1336 if(src->etype == TINTER || dst->etype == TINTER) 1337 return 0; 1338 if(why != nil) 1339 *why = ""; 1340 1341 // 2. src and dst have identical underlying types. 1342 if(eqtype(src->orig, dst->orig)) 1343 return OCONVNOP; 1344 1345 // 3. src and dst are unnamed pointer types 1346 // and their base types have identical underlying types. 1347 if(isptr[src->etype] && isptr[dst->etype] && src->sym == S && dst->sym == S) 1348 if(eqtype(src->type->orig, dst->type->orig)) 1349 return OCONVNOP; 1350 1351 // 4. src and dst are both integer or floating point types. 1352 if((isint[src->etype] || isfloat[src->etype]) && (isint[dst->etype] || isfloat[dst->etype])) { 1353 if(simtype[src->etype] == simtype[dst->etype]) 1354 return OCONVNOP; 1355 return OCONV; 1356 } 1357 1358 // 5. src and dst are both complex types. 1359 if(iscomplex[src->etype] && iscomplex[dst->etype]) { 1360 if(simtype[src->etype] == simtype[dst->etype]) 1361 return OCONVNOP; 1362 return OCONV; 1363 } 1364 1365 // 6. src is an integer or has type []byte or []rune 1366 // and dst is a string type. 1367 if(isint[src->etype] && dst->etype == TSTRING) 1368 return ORUNESTR; 1369 1370 if(isslice(src) && dst->etype == TSTRING) { 1371 if(src->type->etype == bytetype->etype) 1372 return OARRAYBYTESTR; 1373 if(src->type->etype == runetype->etype) 1374 return OARRAYRUNESTR; 1375 } 1376 1377 // 7. src is a string and dst is []byte or []rune. 1378 // String to slice. 1379 if(src->etype == TSTRING && isslice(dst)) { 1380 if(dst->type->etype == bytetype->etype) 1381 return OSTRARRAYBYTE; 1382 if(dst->type->etype == runetype->etype) 1383 return OSTRARRAYRUNE; 1384 } 1385 1386 // 8. src is a pointer or uintptr and dst is unsafe.Pointer. 1387 if((isptr[src->etype] || src->etype == TUINTPTR) && dst->etype == TUNSAFEPTR) 1388 return OCONVNOP; 1389 1390 // 9. src is unsafe.Pointer and dst is a pointer or uintptr. 1391 if(src->etype == TUNSAFEPTR && (isptr[dst->etype] || dst->etype == TUINTPTR)) 1392 return OCONVNOP; 1393 1394 return 0; 1395 } 1396 1397 // Convert node n for assignment to type t. 1398 Node* 1399 assignconv(Node *n, Type *t, char *context) 1400 { 1401 int op; 1402 Node *r, *old; 1403 char *why; 1404 1405 if(n == N || n->type == T || n->type->broke) 1406 return n; 1407 1408 if(t->etype == TBLANK && n->type->etype == TNIL) 1409 yyerror("use of untyped nil"); 1410 1411 old = n; 1412 old->diag++; // silence errors about n; we'll issue one below 1413 defaultlit(&n, t); 1414 old->diag--; 1415 if(t->etype == TBLANK) 1416 return n; 1417 1418 // Convert ideal bool from comparison to plain bool 1419 // if the next step is non-bool (like interface{}). 1420 if(n->type == idealbool && t->etype != TBOOL) { 1421 if(n->op == ONAME || n->op == OLITERAL) { 1422 r = nod(OCONVNOP, n, N); 1423 r->type = types[TBOOL]; 1424 r->typecheck = 1; 1425 r->implicit = 1; 1426 n = r; 1427 } 1428 } 1429 1430 if(eqtype(n->type, t)) 1431 return n; 1432 1433 op = assignop(n->type, t, &why); 1434 if(op == 0) { 1435 yyerror("cannot use %lN as type %T in %s%s", n, t, context, why); 1436 op = OCONV; 1437 } 1438 1439 r = nod(op, n, N); 1440 r->type = t; 1441 r->typecheck = 1; 1442 r->implicit = 1; 1443 r->orig = n->orig; 1444 return r; 1445 } 1446 1447 static int 1448 subtype(Type **stp, Type *t, int d) 1449 { 1450 Type *st; 1451 1452 loop: 1453 st = *stp; 1454 if(st == T) 1455 return 0; 1456 1457 d++; 1458 if(d >= 10) 1459 return 0; 1460 1461 switch(st->etype) { 1462 default: 1463 return 0; 1464 1465 case TPTR32: 1466 case TPTR64: 1467 case TCHAN: 1468 case TARRAY: 1469 stp = &st->type; 1470 goto loop; 1471 1472 case TANY: 1473 if(!st->copyany) 1474 return 0; 1475 *stp = t; 1476 break; 1477 1478 case TMAP: 1479 if(subtype(&st->down, t, d)) 1480 break; 1481 stp = &st->type; 1482 goto loop; 1483 1484 case TFUNC: 1485 for(;;) { 1486 if(subtype(&st->type, t, d)) 1487 break; 1488 if(subtype(&st->type->down->down, t, d)) 1489 break; 1490 if(subtype(&st->type->down, t, d)) 1491 break; 1492 return 0; 1493 } 1494 break; 1495 1496 case TSTRUCT: 1497 for(st=st->type; st!=T; st=st->down) 1498 if(subtype(&st->type, t, d)) 1499 return 1; 1500 return 0; 1501 } 1502 return 1; 1503 } 1504 1505 /* 1506 * Is this a 64-bit type? 1507 */ 1508 int 1509 is64(Type *t) 1510 { 1511 if(t == T) 1512 return 0; 1513 switch(simtype[t->etype]) { 1514 case TINT64: 1515 case TUINT64: 1516 case TPTR64: 1517 return 1; 1518 } 1519 return 0; 1520 } 1521 1522 /* 1523 * Is a conversion between t1 and t2 a no-op? 1524 */ 1525 int 1526 noconv(Type *t1, Type *t2) 1527 { 1528 int e1, e2; 1529 1530 e1 = simtype[t1->etype]; 1531 e2 = simtype[t2->etype]; 1532 1533 switch(e1) { 1534 case TINT8: 1535 case TUINT8: 1536 return e2 == TINT8 || e2 == TUINT8; 1537 1538 case TINT16: 1539 case TUINT16: 1540 return e2 == TINT16 || e2 == TUINT16; 1541 1542 case TINT32: 1543 case TUINT32: 1544 case TPTR32: 1545 return e2 == TINT32 || e2 == TUINT32 || e2 == TPTR32; 1546 1547 case TINT64: 1548 case TUINT64: 1549 case TPTR64: 1550 return e2 == TINT64 || e2 == TUINT64 || e2 == TPTR64; 1551 1552 case TFLOAT32: 1553 return e2 == TFLOAT32; 1554 1555 case TFLOAT64: 1556 return e2 == TFLOAT64; 1557 } 1558 return 0; 1559 } 1560 1561 void 1562 argtype(Node *on, Type *t) 1563 { 1564 dowidth(t); 1565 if(!subtype(&on->type, t, 0)) 1566 fatal("argtype: failed %N %T\n", on, t); 1567 } 1568 1569 Type* 1570 shallow(Type *t) 1571 { 1572 Type *nt; 1573 1574 if(t == T) 1575 return T; 1576 nt = typ(0); 1577 *nt = *t; 1578 if(t->orig == t) 1579 nt->orig = nt; 1580 return nt; 1581 } 1582 1583 static Type* 1584 deep(Type *t) 1585 { 1586 Type *nt, *xt; 1587 1588 if(t == T) 1589 return T; 1590 1591 switch(t->etype) { 1592 default: 1593 nt = t; // share from here down 1594 break; 1595 1596 case TANY: 1597 nt = shallow(t); 1598 nt->copyany = 1; 1599 break; 1600 1601 case TPTR32: 1602 case TPTR64: 1603 case TCHAN: 1604 case TARRAY: 1605 nt = shallow(t); 1606 nt->type = deep(t->type); 1607 break; 1608 1609 case TMAP: 1610 nt = shallow(t); 1611 nt->down = deep(t->down); 1612 nt->type = deep(t->type); 1613 break; 1614 1615 case TFUNC: 1616 nt = shallow(t); 1617 nt->type = deep(t->type); 1618 nt->type->down = deep(t->type->down); 1619 nt->type->down->down = deep(t->type->down->down); 1620 break; 1621 1622 case TSTRUCT: 1623 nt = shallow(t); 1624 nt->type = shallow(t->type); 1625 xt = nt->type; 1626 1627 for(t=t->type; t!=T; t=t->down) { 1628 xt->type = deep(t->type); 1629 xt->down = shallow(t->down); 1630 xt = xt->down; 1631 } 1632 break; 1633 } 1634 return nt; 1635 } 1636 1637 Node* 1638 syslook(char *name, int copy) 1639 { 1640 Sym *s; 1641 Node *n; 1642 1643 s = pkglookup(name, runtimepkg); 1644 if(s == S || s->def == N) 1645 fatal("syslook: can't find runtime.%s", name); 1646 1647 if(!copy) 1648 return s->def; 1649 1650 n = nod(0, N, N); 1651 *n = *s->def; 1652 n->type = deep(s->def->type); 1653 1654 return n; 1655 } 1656 1657 /* 1658 * compute a hash value for type t. 1659 * if t is a method type, ignore the receiver 1660 * so that the hash can be used in interface checks. 1661 * %T already contains 1662 * all the necessary logic to generate a representation 1663 * of the type that completely describes it. 1664 * using smprint here avoids duplicating that code. 1665 * using md5 here is overkill, but i got tired of 1666 * accidental collisions making the runtime think 1667 * two types are equal when they really aren't. 1668 */ 1669 uint32 1670 typehash(Type *t) 1671 { 1672 char *p; 1673 MD5 d; 1674 1675 if(t->thistuple) { 1676 // hide method receiver from Tpretty 1677 t->thistuple = 0; 1678 p = smprint("%-uT", t); 1679 t->thistuple = 1; 1680 } else 1681 p = smprint("%-uT", t); 1682 //print("typehash: %s\n", p); 1683 md5reset(&d); 1684 md5write(&d, (uchar*)p, strlen(p)); 1685 free(p); 1686 return md5sum(&d, nil); 1687 } 1688 1689 Type* 1690 ptrto(Type *t) 1691 { 1692 Type *t1; 1693 1694 if(tptr == 0) 1695 fatal("ptrto: no tptr"); 1696 t1 = typ(tptr); 1697 t1->type = t; 1698 t1->width = widthptr; 1699 t1->align = widthptr; 1700 return t1; 1701 } 1702 1703 void 1704 frame(int context) 1705 { 1706 char *p; 1707 NodeList *l; 1708 Node *n; 1709 int flag; 1710 1711 p = "stack"; 1712 l = nil; 1713 if(curfn) 1714 l = curfn->dcl; 1715 if(context) { 1716 p = "external"; 1717 l = externdcl; 1718 } 1719 1720 flag = 1; 1721 for(; l; l=l->next) { 1722 n = l->n; 1723 switch(n->op) { 1724 case ONAME: 1725 if(flag) 1726 print("--- %s frame ---\n", p); 1727 print("%O %S G%d %T\n", n->op, n->sym, n->vargen, n->type); 1728 flag = 0; 1729 break; 1730 1731 case OTYPE: 1732 if(flag) 1733 print("--- %s frame ---\n", p); 1734 print("%O %T\n", n->op, n->type); 1735 flag = 0; 1736 break; 1737 } 1738 } 1739 } 1740 1741 /* 1742 * calculate sethi/ullman number 1743 * roughly how many registers needed to 1744 * compile a node. used to compile the 1745 * hardest side first to minimize registers. 1746 */ 1747 void 1748 ullmancalc(Node *n) 1749 { 1750 int ul, ur; 1751 1752 if(n == N) 1753 return; 1754 1755 if(n->ninit != nil) { 1756 ul = UINF; 1757 goto out; 1758 } 1759 1760 switch(n->op) { 1761 case OREGISTER: 1762 case OLITERAL: 1763 case ONAME: 1764 ul = 1; 1765 if(n->class == PPARAMREF || (n->class & PHEAP)) 1766 ul++; 1767 goto out; 1768 case OCALL: 1769 case OCALLFUNC: 1770 case OCALLMETH: 1771 case OCALLINTER: 1772 ul = UINF; 1773 goto out; 1774 case OANDAND: 1775 case OOROR: 1776 // hard with race detector 1777 if(flag_race) { 1778 ul = UINF; 1779 goto out; 1780 } 1781 } 1782 ul = 1; 1783 if(n->left != N) 1784 ul = n->left->ullman; 1785 ur = 1; 1786 if(n->right != N) 1787 ur = n->right->ullman; 1788 if(ul == ur) 1789 ul += 1; 1790 if(ur > ul) 1791 ul = ur; 1792 1793 out: 1794 if(ul > 200) 1795 ul = 200; // clamp to uchar with room to grow 1796 n->ullman = ul; 1797 } 1798 1799 void 1800 badtype(int o, Type *tl, Type *tr) 1801 { 1802 Fmt fmt; 1803 char *s; 1804 1805 fmtstrinit(&fmt); 1806 if(tl != T) 1807 fmtprint(&fmt, "\n %T", tl); 1808 if(tr != T) 1809 fmtprint(&fmt, "\n %T", tr); 1810 1811 // common mistake: *struct and *interface. 1812 if(tl && tr && isptr[tl->etype] && isptr[tr->etype]) { 1813 if(tl->type->etype == TSTRUCT && tr->type->etype == TINTER) 1814 fmtprint(&fmt, "\n (*struct vs *interface)"); 1815 else if(tl->type->etype == TINTER && tr->type->etype == TSTRUCT) 1816 fmtprint(&fmt, "\n (*interface vs *struct)"); 1817 } 1818 s = fmtstrflush(&fmt); 1819 yyerror("illegal types for operand: %O%s", o, s); 1820 } 1821 1822 /* 1823 * iterator to walk a structure declaration 1824 */ 1825 Type* 1826 structfirst(Iter *s, Type **nn) 1827 { 1828 Type *n, *t; 1829 1830 n = *nn; 1831 if(n == T) 1832 goto bad; 1833 1834 switch(n->etype) { 1835 default: 1836 goto bad; 1837 1838 case TSTRUCT: 1839 case TINTER: 1840 case TFUNC: 1841 break; 1842 } 1843 1844 t = n->type; 1845 if(t == T) 1846 goto rnil; 1847 1848 if(t->etype != TFIELD) 1849 fatal("structfirst: not field %T", t); 1850 1851 s->t = t; 1852 return t; 1853 1854 bad: 1855 fatal("structfirst: not struct %T", n); 1856 1857 rnil: 1858 return T; 1859 } 1860 1861 Type* 1862 structnext(Iter *s) 1863 { 1864 Type *n, *t; 1865 1866 n = s->t; 1867 t = n->down; 1868 if(t == T) 1869 goto rnil; 1870 1871 if(t->etype != TFIELD) 1872 goto bad; 1873 1874 s->t = t; 1875 return t; 1876 1877 bad: 1878 fatal("structnext: not struct %T", n); 1879 1880 rnil: 1881 return T; 1882 } 1883 1884 /* 1885 * iterator to this and inargs in a function 1886 */ 1887 Type* 1888 funcfirst(Iter *s, Type *t) 1889 { 1890 Type *fp; 1891 1892 if(t == T) 1893 goto bad; 1894 1895 if(t->etype != TFUNC) 1896 goto bad; 1897 1898 s->tfunc = t; 1899 s->done = 0; 1900 fp = structfirst(s, getthis(t)); 1901 if(fp == T) { 1902 s->done = 1; 1903 fp = structfirst(s, getinarg(t)); 1904 } 1905 return fp; 1906 1907 bad: 1908 fatal("funcfirst: not func %T", t); 1909 return T; 1910 } 1911 1912 Type* 1913 funcnext(Iter *s) 1914 { 1915 Type *fp; 1916 1917 fp = structnext(s); 1918 if(fp == T && !s->done) { 1919 s->done = 1; 1920 fp = structfirst(s, getinarg(s->tfunc)); 1921 } 1922 return fp; 1923 } 1924 1925 Type** 1926 getthis(Type *t) 1927 { 1928 if(t->etype != TFUNC) 1929 fatal("getthis: not a func %T", t); 1930 return &t->type; 1931 } 1932 1933 Type** 1934 getoutarg(Type *t) 1935 { 1936 if(t->etype != TFUNC) 1937 fatal("getoutarg: not a func %T", t); 1938 return &t->type->down; 1939 } 1940 1941 Type** 1942 getinarg(Type *t) 1943 { 1944 if(t->etype != TFUNC) 1945 fatal("getinarg: not a func %T", t); 1946 return &t->type->down->down; 1947 } 1948 1949 Type* 1950 getthisx(Type *t) 1951 { 1952 return *getthis(t); 1953 } 1954 1955 Type* 1956 getoutargx(Type *t) 1957 { 1958 return *getoutarg(t); 1959 } 1960 1961 Type* 1962 getinargx(Type *t) 1963 { 1964 return *getinarg(t); 1965 } 1966 1967 /* 1968 * return !(op) 1969 * eg == <=> != 1970 */ 1971 int 1972 brcom(int a) 1973 { 1974 switch(a) { 1975 case OEQ: return ONE; 1976 case ONE: return OEQ; 1977 case OLT: return OGE; 1978 case OGT: return OLE; 1979 case OLE: return OGT; 1980 case OGE: return OLT; 1981 } 1982 fatal("brcom: no com for %A\n", a); 1983 return a; 1984 } 1985 1986 /* 1987 * return reverse(op) 1988 * eg a op b <=> b r(op) a 1989 */ 1990 int 1991 brrev(int a) 1992 { 1993 switch(a) { 1994 case OEQ: return OEQ; 1995 case ONE: return ONE; 1996 case OLT: return OGT; 1997 case OGT: return OLT; 1998 case OLE: return OGE; 1999 case OGE: return OLE; 2000 } 2001 fatal("brcom: no rev for %A\n", a); 2002 return a; 2003 } 2004 2005 /* 2006 * return side effect-free n, appending side effects to init. 2007 * result is assignable if n is. 2008 */ 2009 Node* 2010 safeexpr(Node *n, NodeList **init) 2011 { 2012 Node *l; 2013 Node *r; 2014 Node *a; 2015 2016 if(n == N) 2017 return N; 2018 2019 if(n->ninit) { 2020 walkstmtlist(n->ninit); 2021 *init = concat(*init, n->ninit); 2022 n->ninit = nil; 2023 } 2024 2025 switch(n->op) { 2026 case ONAME: 2027 case OLITERAL: 2028 return n; 2029 2030 case ODOT: 2031 l = safeexpr(n->left, init); 2032 if(l == n->left) 2033 return n; 2034 r = nod(OXXX, N, N); 2035 *r = *n; 2036 r->left = l; 2037 typecheck(&r, Erv); 2038 walkexpr(&r, init); 2039 return r; 2040 2041 case ODOTPTR: 2042 case OIND: 2043 l = safeexpr(n->left, init); 2044 if(l == n->left) 2045 return n; 2046 a = nod(OXXX, N, N); 2047 *a = *n; 2048 a->left = l; 2049 walkexpr(&a, init); 2050 return a; 2051 2052 case OINDEX: 2053 case OINDEXMAP: 2054 l = safeexpr(n->left, init); 2055 r = safeexpr(n->right, init); 2056 if(l == n->left && r == n->right) 2057 return n; 2058 a = nod(OXXX, N, N); 2059 *a = *n; 2060 a->left = l; 2061 a->right = r; 2062 walkexpr(&a, init); 2063 return a; 2064 } 2065 2066 // make a copy; must not be used as an lvalue 2067 if(islvalue(n)) 2068 fatal("missing lvalue case in safeexpr: %N", n); 2069 return cheapexpr(n, init); 2070 } 2071 2072 Node* 2073 copyexpr(Node *n, Type *t, NodeList **init) 2074 { 2075 Node *a, *l; 2076 2077 l = temp(t); 2078 a = nod(OAS, l, n); 2079 typecheck(&a, Etop); 2080 walkexpr(&a, init); 2081 *init = list(*init, a); 2082 return l; 2083 } 2084 2085 /* 2086 * return side-effect free and cheap n, appending side effects to init. 2087 * result may not be assignable. 2088 */ 2089 Node* 2090 cheapexpr(Node *n, NodeList **init) 2091 { 2092 switch(n->op) { 2093 case ONAME: 2094 case OLITERAL: 2095 return n; 2096 } 2097 2098 return copyexpr(n, n->type, init); 2099 } 2100 2101 /* 2102 * return n in a local variable of type t if it is not already. 2103 * the value is guaranteed not to change except by direct 2104 * assignment to it. 2105 */ 2106 Node* 2107 localexpr(Node *n, Type *t, NodeList **init) 2108 { 2109 if(n->op == ONAME && (!n->addrtaken || strncmp(n->sym->name, "autotmp_", 8) == 0) && 2110 (n->class == PAUTO || n->class == PPARAM || n->class == PPARAMOUT) && 2111 convertop(n->type, t, nil) == OCONVNOP) 2112 return n; 2113 2114 return copyexpr(n, t, init); 2115 } 2116 2117 void 2118 setmaxarg(Type *t) 2119 { 2120 int64 w; 2121 2122 dowidth(t); 2123 w = t->argwid; 2124 if(t->argwid >= MAXWIDTH) 2125 fatal("bad argwid %T", t); 2126 if(w > maxarg) 2127 maxarg = w; 2128 } 2129 2130 /* 2131 * unicode-aware case-insensitive strcmp 2132 */ 2133 2134 static int 2135 ucistrcmp(char *p, char *q) 2136 { 2137 Rune rp, rq; 2138 2139 while(*p || *q) { 2140 if(*p == 0) 2141 return +1; 2142 if(*q == 0) 2143 return -1; 2144 p += chartorune(&rp, p); 2145 q += chartorune(&rq, q); 2146 rp = tolowerrune(rp); 2147 rq = tolowerrune(rq); 2148 if(rp < rq) 2149 return -1; 2150 if(rp > rq) 2151 return +1; 2152 } 2153 return 0; 2154 } 2155 2156 /* 2157 * code to resolve elided DOTs 2158 * in embedded types 2159 */ 2160 2161 // search depth 0 -- 2162 // return count of fields+methods 2163 // found with a given name 2164 static int 2165 lookdot0(Sym *s, Type *t, Type **save, int ignorecase) 2166 { 2167 Type *f, *u; 2168 int c; 2169 2170 u = t; 2171 if(isptr[u->etype]) 2172 u = u->type; 2173 2174 c = 0; 2175 if(u->etype == TSTRUCT || u->etype == TINTER) { 2176 for(f=u->type; f!=T; f=f->down) 2177 if(f->sym == s || (ignorecase && f->type->etype == TFUNC && f->type->thistuple > 0 && ucistrcmp(f->sym->name, s->name) == 0)) { 2178 if(save) 2179 *save = f; 2180 c++; 2181 } 2182 } 2183 u = methtype(t, 0); 2184 if(u != T) { 2185 for(f=u->method; f!=T; f=f->down) 2186 if(f->embedded == 0 && (f->sym == s || (ignorecase && ucistrcmp(f->sym->name, s->name) == 0))) { 2187 if(save) 2188 *save = f; 2189 c++; 2190 } 2191 } 2192 return c; 2193 } 2194 2195 // search depth d for field/method s -- 2196 // return count of fields+methods 2197 // found at search depth. 2198 // answer is in dotlist array and 2199 // count of number of ways is returned. 2200 int 2201 adddot1(Sym *s, Type *t, int d, Type **save, int ignorecase) 2202 { 2203 Type *f, *u; 2204 int c, a; 2205 2206 if(t->trecur) 2207 return 0; 2208 t->trecur = 1; 2209 2210 if(d == 0) { 2211 c = lookdot0(s, t, save, ignorecase); 2212 goto out; 2213 } 2214 2215 c = 0; 2216 u = t; 2217 if(isptr[u->etype]) 2218 u = u->type; 2219 if(u->etype != TSTRUCT && u->etype != TINTER) 2220 goto out; 2221 2222 d--; 2223 for(f=u->type; f!=T; f=f->down) { 2224 if(!f->embedded) 2225 continue; 2226 if(f->sym == S) 2227 continue; 2228 a = adddot1(s, f->type, d, save, ignorecase); 2229 if(a != 0 && c == 0) 2230 dotlist[d].field = f; 2231 c += a; 2232 } 2233 2234 out: 2235 t->trecur = 0; 2236 return c; 2237 } 2238 2239 // in T.field 2240 // find missing fields that 2241 // will give shortest unique addressing. 2242 // modify the tree with missing type names. 2243 Node* 2244 adddot(Node *n) 2245 { 2246 Type *t; 2247 Sym *s; 2248 int c, d; 2249 2250 typecheck(&n->left, Etype|Erv); 2251 n->diag |= n->left->diag; 2252 t = n->left->type; 2253 if(t == T) 2254 goto ret; 2255 2256 if(n->left->op == OTYPE) 2257 goto ret; 2258 2259 if(n->right->op != ONAME) 2260 goto ret; 2261 s = n->right->sym; 2262 if(s == S) 2263 goto ret; 2264 2265 for(d=0; d<nelem(dotlist); d++) { 2266 c = adddot1(s, t, d, nil, 0); 2267 if(c > 0) 2268 goto out; 2269 } 2270 goto ret; 2271 2272 out: 2273 if(c > 1) { 2274 yyerror("ambiguous selector %N", n); 2275 n->left = N; 2276 return n; 2277 } 2278 2279 // rebuild elided dots 2280 for(c=d-1; c>=0; c--) 2281 n->left = nod(ODOT, n->left, newname(dotlist[c].field->sym)); 2282 ret: 2283 return n; 2284 } 2285 2286 2287 /* 2288 * code to help generate trampoline 2289 * functions for methods on embedded 2290 * subtypes. 2291 * these are approx the same as 2292 * the corresponding adddot routines 2293 * except that they expect to be called 2294 * with unique tasks and they return 2295 * the actual methods. 2296 */ 2297 2298 typedef struct Symlink Symlink; 2299 struct Symlink 2300 { 2301 Type* field; 2302 uchar good; 2303 uchar followptr; 2304 Symlink* link; 2305 }; 2306 static Symlink* slist; 2307 2308 static void 2309 expand0(Type *t, int followptr) 2310 { 2311 Type *f, *u; 2312 Symlink *sl; 2313 2314 u = t; 2315 if(isptr[u->etype]) { 2316 followptr = 1; 2317 u = u->type; 2318 } 2319 2320 if(u->etype == TINTER) { 2321 for(f=u->type; f!=T; f=f->down) { 2322 if(f->sym->flags & SymUniq) 2323 continue; 2324 f->sym->flags |= SymUniq; 2325 sl = mal(sizeof(*sl)); 2326 sl->field = f; 2327 sl->link = slist; 2328 sl->followptr = followptr; 2329 slist = sl; 2330 } 2331 return; 2332 } 2333 2334 u = methtype(t, 0); 2335 if(u != T) { 2336 for(f=u->method; f!=T; f=f->down) { 2337 if(f->sym->flags & SymUniq) 2338 continue; 2339 f->sym->flags |= SymUniq; 2340 sl = mal(sizeof(*sl)); 2341 sl->field = f; 2342 sl->link = slist; 2343 sl->followptr = followptr; 2344 slist = sl; 2345 } 2346 } 2347 } 2348 2349 static void 2350 expand1(Type *t, int d, int followptr) 2351 { 2352 Type *f, *u; 2353 2354 if(t->trecur) 2355 return; 2356 if(d == 0) 2357 return; 2358 t->trecur = 1; 2359 2360 if(d != nelem(dotlist)-1) 2361 expand0(t, followptr); 2362 2363 u = t; 2364 if(isptr[u->etype]) { 2365 followptr = 1; 2366 u = u->type; 2367 } 2368 if(u->etype != TSTRUCT && u->etype != TINTER) 2369 goto out; 2370 2371 for(f=u->type; f!=T; f=f->down) { 2372 if(!f->embedded) 2373 continue; 2374 if(f->sym == S) 2375 continue; 2376 expand1(f->type, d-1, followptr); 2377 } 2378 2379 out: 2380 t->trecur = 0; 2381 } 2382 2383 void 2384 expandmeth(Type *t) 2385 { 2386 Symlink *sl; 2387 Type *f; 2388 int c, d; 2389 2390 if(t == T || t->xmethod != nil) 2391 return; 2392 2393 // mark top-level method symbols 2394 // so that expand1 doesn't consider them. 2395 for(f=t->method; f != nil; f=f->down) 2396 f->sym->flags |= SymUniq; 2397 2398 // generate all reachable methods 2399 slist = nil; 2400 expand1(t, nelem(dotlist)-1, 0); 2401 2402 // check each method to be uniquely reachable 2403 for(sl=slist; sl!=nil; sl=sl->link) { 2404 sl->field->sym->flags &= ~SymUniq; 2405 for(d=0; d<nelem(dotlist); d++) { 2406 c = adddot1(sl->field->sym, t, d, &f, 0); 2407 if(c == 0) 2408 continue; 2409 if(c == 1) { 2410 // addot1 may have dug out arbitrary fields, we only want methods. 2411 if(f->type->etype == TFUNC && f->type->thistuple > 0) { 2412 sl->good = 1; 2413 sl->field = f; 2414 } 2415 } 2416 break; 2417 } 2418 } 2419 2420 for(f=t->method; f != nil; f=f->down) 2421 f->sym->flags &= ~SymUniq; 2422 2423 t->xmethod = t->method; 2424 for(sl=slist; sl!=nil; sl=sl->link) { 2425 if(sl->good) { 2426 // add it to the base type method list 2427 f = typ(TFIELD); 2428 *f = *sl->field; 2429 f->embedded = 1; // needs a trampoline 2430 if(sl->followptr) 2431 f->embedded = 2; 2432 f->down = t->xmethod; 2433 t->xmethod = f; 2434 } 2435 } 2436 } 2437 2438 /* 2439 * Given funarg struct list, return list of ODCLFIELD Node fn args. 2440 */ 2441 static NodeList* 2442 structargs(Type **tl, int mustname) 2443 { 2444 Iter savet; 2445 Node *a, *n; 2446 NodeList *args; 2447 Type *t; 2448 char buf[100]; 2449 int gen; 2450 2451 args = nil; 2452 gen = 0; 2453 for(t = structfirst(&savet, tl); t != T; t = structnext(&savet)) { 2454 n = N; 2455 if(mustname && (t->sym == nil || strcmp(t->sym->name, "_") == 0)) { 2456 // invent a name so that we can refer to it in the trampoline 2457 snprint(buf, sizeof buf, ".anon%d", gen++); 2458 n = newname(lookup(buf)); 2459 } else if(t->sym) 2460 n = newname(t->sym); 2461 a = nod(ODCLFIELD, n, typenod(t->type)); 2462 a->isddd = t->isddd; 2463 if(n != N) 2464 n->isddd = t->isddd; 2465 args = list(args, a); 2466 } 2467 return args; 2468 } 2469 2470 /* 2471 * Generate a wrapper function to convert from 2472 * a receiver of type T to a receiver of type U. 2473 * That is, 2474 * 2475 * func (t T) M() { 2476 * ... 2477 * } 2478 * 2479 * already exists; this function generates 2480 * 2481 * func (u U) M() { 2482 * u.M() 2483 * } 2484 * 2485 * where the types T and U are such that u.M() is valid 2486 * and calls the T.M method. 2487 * The resulting function is for use in method tables. 2488 * 2489 * rcvr - U 2490 * method - M func (t T)(), a TFIELD type struct 2491 * newnam - the eventual mangled name of this function 2492 */ 2493 void 2494 genwrapper(Type *rcvr, Type *method, Sym *newnam, int iface) 2495 { 2496 Node *this, *fn, *call, *n, *t, *pad, *dot, *as; 2497 NodeList *l, *args, *in, *out; 2498 Type *tpad, *methodrcvr; 2499 int isddd; 2500 Val v; 2501 static int linehistdone = 0; 2502 2503 if(0 && debug['r']) 2504 print("genwrapper rcvrtype=%T method=%T newnam=%S\n", 2505 rcvr, method, newnam); 2506 2507 lexlineno++; 2508 lineno = lexlineno; 2509 if (linehistdone == 0) { 2510 // All the wrappers can share the same linehist entry. 2511 linehist("<autogenerated>", 0, 0); 2512 linehistdone = 1; 2513 } 2514 2515 dclcontext = PEXTERN; 2516 markdcl(); 2517 2518 this = nod(ODCLFIELD, newname(lookup(".this")), typenod(rcvr)); 2519 this->left->ntype = this->right; 2520 in = structargs(getinarg(method->type), 1); 2521 out = structargs(getoutarg(method->type), 0); 2522 2523 t = nod(OTFUNC, N, N); 2524 l = list1(this); 2525 if(iface && rcvr->width < types[tptr]->width) { 2526 // Building method for interface table and receiver 2527 // is smaller than the single pointer-sized word 2528 // that the interface call will pass in. 2529 // Add a dummy padding argument after the 2530 // receiver to make up the difference. 2531 tpad = typ(TARRAY); 2532 tpad->type = types[TUINT8]; 2533 tpad->bound = types[tptr]->width - rcvr->width; 2534 pad = nod(ODCLFIELD, newname(lookup(".pad")), typenod(tpad)); 2535 l = list(l, pad); 2536 } 2537 t->list = concat(l, in); 2538 t->rlist = out; 2539 2540 fn = nod(ODCLFUNC, N, N); 2541 fn->nname = newname(newnam); 2542 fn->nname->defn = fn; 2543 fn->nname->ntype = t; 2544 declare(fn->nname, PFUNC); 2545 funchdr(fn); 2546 2547 // arg list 2548 args = nil; 2549 isddd = 0; 2550 for(l=in; l; l=l->next) { 2551 args = list(args, l->n->left); 2552 isddd = l->n->left->isddd; 2553 } 2554 2555 methodrcvr = getthisx(method->type)->type->type; 2556 2557 // generate nil pointer check for better error 2558 if(isptr[rcvr->etype] && rcvr->type == methodrcvr) { 2559 // generating wrapper from *T to T. 2560 n = nod(OIF, N, N); 2561 n->ntest = nod(OEQ, this->left, nodnil()); 2562 // these strings are already in the reflect tables, 2563 // so no space cost to use them here. 2564 l = nil; 2565 v.ctype = CTSTR; 2566 v.u.sval = strlit(rcvr->type->sym->pkg->name); // package name 2567 l = list(l, nodlit(v)); 2568 v.u.sval = strlit(rcvr->type->sym->name); // type name 2569 l = list(l, nodlit(v)); 2570 v.u.sval = strlit(method->sym->name); 2571 l = list(l, nodlit(v)); // method name 2572 call = nod(OCALL, syslook("panicwrap", 0), N); 2573 call->list = l; 2574 n->nbody = list1(call); 2575 fn->nbody = list(fn->nbody, n); 2576 } 2577 2578 dot = adddot(nod(OXDOT, this->left, newname(method->sym))); 2579 2580 // generate call 2581 if(!flag_race && isptr[rcvr->etype] && isptr[methodrcvr->etype] && method->embedded && !isifacemethod(method->type)) { 2582 // generate tail call: adjust pointer receiver and jump to embedded method. 2583 dot = dot->left; // skip final .M 2584 if(!isptr[dotlist[0].field->type->etype]) 2585 dot = nod(OADDR, dot, N); 2586 as = nod(OAS, this->left, nod(OCONVNOP, dot, N)); 2587 as->right->type = rcvr; 2588 fn->nbody = list(fn->nbody, as); 2589 n = nod(ORETJMP, N, N); 2590 n->left = newname(methodsym(method->sym, methodrcvr, 0)); 2591 fn->nbody = list(fn->nbody, n); 2592 } else { 2593 fn->wrapper = 1; // ignore frame for panic+recover matching 2594 call = nod(OCALL, dot, N); 2595 call->list = args; 2596 call->isddd = isddd; 2597 if(method->type->outtuple > 0) { 2598 n = nod(ORETURN, N, N); 2599 n->list = list1(call); 2600 call = n; 2601 } 2602 fn->nbody = list(fn->nbody, call); 2603 } 2604 2605 if(0 && debug['r']) 2606 dumplist("genwrapper body", fn->nbody); 2607 2608 funcbody(fn); 2609 curfn = fn; 2610 // wrappers where T is anonymous (struct or interface) can be duplicated. 2611 if(rcvr->etype == TSTRUCT || 2612 rcvr->etype == TINTER || 2613 isptr[rcvr->etype] && rcvr->type->etype == TSTRUCT) 2614 fn->dupok = 1; 2615 typecheck(&fn, Etop); 2616 typechecklist(fn->nbody, Etop); 2617 2618 // Set inl_nonlocal to whether we are calling a method on a 2619 // type defined in a different package. Checked in inlvar. 2620 if(!methodrcvr->local) 2621 inl_nonlocal = 1; 2622 2623 inlcalls(fn); 2624 2625 inl_nonlocal = 0; 2626 2627 curfn = nil; 2628 funccompile(fn, 0); 2629 } 2630 2631 static Node* 2632 hashmem(Type *t) 2633 { 2634 Node *tfn, *n; 2635 Sym *sym; 2636 2637 sym = pkglookup("memhash", runtimepkg); 2638 2639 n = newname(sym); 2640 n->class = PFUNC; 2641 tfn = nod(OTFUNC, N, N); 2642 tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(ptrto(t)))); 2643 tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(types[TUINTPTR]))); 2644 tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(types[TUINTPTR]))); 2645 tfn->rlist = list(tfn->rlist, nod(ODCLFIELD, N, typenod(types[TUINTPTR]))); 2646 typecheck(&tfn, Etype); 2647 n->type = tfn->type; 2648 return n; 2649 } 2650 2651 static Node* 2652 hashfor(Type *t) 2653 { 2654 int a; 2655 Sym *sym; 2656 Node *tfn, *n; 2657 2658 a = algtype1(t, nil); 2659 switch(a) { 2660 case AMEM: 2661 return hashmem(t); 2662 case AINTER: 2663 sym = pkglookup("interhash", runtimepkg); 2664 break; 2665 case ANILINTER: 2666 sym = pkglookup("nilinterhash", runtimepkg); 2667 break; 2668 case ASTRING: 2669 sym = pkglookup("strhash", runtimepkg); 2670 break; 2671 case AFLOAT32: 2672 sym = pkglookup("f32hash", runtimepkg); 2673 break; 2674 case AFLOAT64: 2675 sym = pkglookup("f64hash", runtimepkg); 2676 break; 2677 case ACPLX64: 2678 sym = pkglookup("c64hash", runtimepkg); 2679 break; 2680 case ACPLX128: 2681 sym = pkglookup("c128hash", runtimepkg); 2682 break; 2683 default: 2684 sym = typesymprefix(".hash", t); 2685 break; 2686 } 2687 2688 n = newname(sym); 2689 n->class = PFUNC; 2690 tfn = nod(OTFUNC, N, N); 2691 tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(ptrto(t)))); 2692 tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(types[TUINTPTR]))); 2693 tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(types[TUINTPTR]))); 2694 tfn->rlist = list(tfn->rlist, nod(ODCLFIELD, N, typenod(types[TUINTPTR]))); 2695 typecheck(&tfn, Etype); 2696 n->type = tfn->type; 2697 return n; 2698 } 2699 2700 /* 2701 * Generate a helper function to compute the hash of a value of type t. 2702 */ 2703 void 2704 genhash(Sym *sym, Type *t) 2705 { 2706 Node *n, *fn, *np, *nh, *ni, *call, *nx, *na, *tfn, *r; 2707 Node *hashel; 2708 Type *first, *t1; 2709 int old_safemode; 2710 int64 size, mul, offend; 2711 2712 if(debug['r']) 2713 print("genhash %S %T\n", sym, t); 2714 2715 lineno = 1; // less confusing than end of input 2716 dclcontext = PEXTERN; 2717 markdcl(); 2718 2719 // func sym(p *T, s uintptr, h uintptr) uintptr 2720 fn = nod(ODCLFUNC, N, N); 2721 fn->nname = newname(sym); 2722 fn->nname->class = PFUNC; 2723 tfn = nod(OTFUNC, N, N); 2724 fn->nname->ntype = tfn; 2725 2726 n = nod(ODCLFIELD, newname(lookup("p")), typenod(ptrto(t))); 2727 tfn->list = list(tfn->list, n); 2728 np = n->left; 2729 n = nod(ODCLFIELD, newname(lookup("s")), typenod(types[TUINTPTR])); 2730 tfn->list = list(tfn->list, n); 2731 n = nod(ODCLFIELD, newname(lookup("h")), typenod(types[TUINTPTR])); 2732 tfn->list = list(tfn->list, n); 2733 nh = n->left; 2734 n = nod(ODCLFIELD, N, typenod(types[TUINTPTR])); // return value 2735 tfn->rlist = list(tfn->rlist, n); 2736 2737 funchdr(fn); 2738 typecheck(&fn->nname->ntype, Etype); 2739 2740 // genhash is only called for types that have equality but 2741 // cannot be handled by the standard algorithms, 2742 // so t must be either an array or a struct. 2743 switch(t->etype) { 2744 default: 2745 fatal("genhash %T", t); 2746 case TARRAY: 2747 if(isslice(t)) 2748 fatal("genhash %T", t); 2749 // An array of pure memory would be handled by the 2750 // standard algorithm, so the element type must not be 2751 // pure memory. 2752 hashel = hashfor(t->type); 2753 n = nod(ORANGE, N, nod(OIND, np, N)); 2754 ni = newname(lookup("i")); 2755 ni->type = types[TINT]; 2756 n->list = list1(ni); 2757 n->colas = 1; 2758 colasdefn(n->list, n); 2759 ni = n->list->n; 2760 2761 // TODO: with aeshash we don't need these shift/mul parts 2762 2763 // h = h<<3 | h>>61 2764 n->nbody = list(n->nbody, 2765 nod(OAS, 2766 nh, 2767 nod(OOR, 2768 nod(OLSH, nh, nodintconst(3)), 2769 nod(ORSH, nh, nodintconst(widthptr*8-3))))); 2770 2771 // h *= mul 2772 // Same multipliers as in runtime.memhash. 2773 if(widthptr == 4) 2774 mul = 3267000013LL; 2775 else 2776 mul = 23344194077549503LL; 2777 n->nbody = list(n->nbody, 2778 nod(OAS, 2779 nh, 2780 nod(OMUL, nh, nodintconst(mul)))); 2781 2782 // h = hashel(&p[i], sizeof(p[i]), h) 2783 call = nod(OCALL, hashel, N); 2784 nx = nod(OINDEX, np, ni); 2785 nx->bounded = 1; 2786 na = nod(OADDR, nx, N); 2787 na->etype = 1; // no escape to heap 2788 call->list = list(call->list, na); 2789 call->list = list(call->list, nodintconst(t->type->width)); 2790 call->list = list(call->list, nh); 2791 n->nbody = list(n->nbody, nod(OAS, nh, call)); 2792 2793 fn->nbody = list(fn->nbody, n); 2794 break; 2795 2796 case TSTRUCT: 2797 // Walk the struct using memhash for runs of AMEM 2798 // and calling specific hash functions for the others. 2799 first = T; 2800 offend = 0; 2801 for(t1=t->type;; t1=t1->down) { 2802 if(t1 != T && algtype1(t1->type, nil) == AMEM && !isblanksym(t1->sym)) { 2803 offend = t1->width + t1->type->width; 2804 if(first == T) 2805 first = t1; 2806 // If it's a memory field but it's padded, stop here. 2807 if(ispaddedfield(t1, t->width)) 2808 t1 = t1->down; 2809 else 2810 continue; 2811 } 2812 // Run memhash for fields up to this one. 2813 if(first != T) { 2814 size = offend - first->width; // first->width is offset 2815 hashel = hashmem(first->type); 2816 // h = hashel(&p.first, size, h) 2817 call = nod(OCALL, hashel, N); 2818 nx = nod(OXDOT, np, newname(first->sym)); // TODO: fields from other packages? 2819 na = nod(OADDR, nx, N); 2820 na->etype = 1; // no escape to heap 2821 call->list = list(call->list, na); 2822 call->list = list(call->list, nodintconst(size)); 2823 call->list = list(call->list, nh); 2824 fn->nbody = list(fn->nbody, nod(OAS, nh, call)); 2825 2826 first = T; 2827 } 2828 if(t1 == T) 2829 break; 2830 if(isblanksym(t1->sym)) 2831 continue; 2832 2833 // Run hash for this field. 2834 hashel = hashfor(t1->type); 2835 // h = hashel(&p.t1, size, h) 2836 call = nod(OCALL, hashel, N); 2837 nx = nod(OXDOT, np, newname(t1->sym)); // TODO: fields from other packages? 2838 na = nod(OADDR, nx, N); 2839 na->etype = 1; // no escape to heap 2840 call->list = list(call->list, na); 2841 call->list = list(call->list, nodintconst(t1->type->width)); 2842 call->list = list(call->list, nh); 2843 fn->nbody = list(fn->nbody, nod(OAS, nh, call)); 2844 } 2845 break; 2846 } 2847 r = nod(ORETURN, N, N); 2848 r->list = list(r->list, nh); 2849 fn->nbody = list(fn->nbody, r); 2850 2851 if(debug['r']) 2852 dumplist("genhash body", fn->nbody); 2853 2854 funcbody(fn); 2855 curfn = fn; 2856 fn->dupok = 1; 2857 typecheck(&fn, Etop); 2858 typechecklist(fn->nbody, Etop); 2859 curfn = nil; 2860 2861 // Disable safemode while compiling this code: the code we 2862 // generate internally can refer to unsafe.Pointer. 2863 // In this case it can happen if we need to generate an == 2864 // for a struct containing a reflect.Value, which itself has 2865 // an unexported field of type unsafe.Pointer. 2866 old_safemode = safemode; 2867 safemode = 0; 2868 funccompile(fn, 0); 2869 safemode = old_safemode; 2870 } 2871 2872 // Return node for 2873 // if p.field != q.field { return false } 2874 static Node* 2875 eqfield(Node *p, Node *q, Node *field) 2876 { 2877 Node *nif, *nx, *ny, *r; 2878 2879 nx = nod(OXDOT, p, field); 2880 ny = nod(OXDOT, q, field); 2881 nif = nod(OIF, N, N); 2882 nif->ntest = nod(ONE, nx, ny); 2883 r = nod(ORETURN, N, N); 2884 r->list = list(r->list, nodbool(0)); 2885 nif->nbody = list(nif->nbody, r); 2886 return nif; 2887 } 2888 2889 static Node* 2890 eqmemfunc(vlong size, Type *type) 2891 { 2892 char buf[30]; 2893 Node *fn; 2894 2895 switch(size) { 2896 default: 2897 fn = syslook("memequal", 1); 2898 break; 2899 case 1: 2900 case 2: 2901 case 4: 2902 case 8: 2903 case 16: 2904 snprint(buf, sizeof buf, "memequal%d", (int)size*8); 2905 fn = syslook(buf, 1); 2906 break; 2907 } 2908 argtype(fn, type); 2909 argtype(fn, type); 2910 return fn; 2911 } 2912 2913 // Return node for 2914 // if !memequal(&p.field, &q.field, size) { return false } 2915 static Node* 2916 eqmem(Node *p, Node *q, Node *field, vlong size) 2917 { 2918 Node *nif, *nx, *ny, *call, *r; 2919 2920 nx = nod(OADDR, nod(OXDOT, p, field), N); 2921 nx->etype = 1; // does not escape 2922 ny = nod(OADDR, nod(OXDOT, q, field), N); 2923 ny->etype = 1; // does not escape 2924 typecheck(&nx, Erv); 2925 typecheck(&ny, Erv); 2926 2927 call = nod(OCALL, eqmemfunc(size, nx->type->type), N); 2928 call->list = list(call->list, nx); 2929 call->list = list(call->list, ny); 2930 call->list = list(call->list, nodintconst(size)); 2931 2932 nif = nod(OIF, N, N); 2933 nif->ninit = list(nif->ninit, call); 2934 nif->ntest = nod(ONOT, call, N); 2935 r = nod(ORETURN, N, N); 2936 r->list = list(r->list, nodbool(0)); 2937 nif->nbody = list(nif->nbody, r); 2938 return nif; 2939 } 2940 2941 /* 2942 * Generate a helper function to check equality of two values of type t. 2943 */ 2944 void 2945 geneq(Sym *sym, Type *t) 2946 { 2947 Node *n, *fn, *np, *nq, *tfn, *nif, *ni, *nx, *ny, *nrange, *r; 2948 Type *t1, *first; 2949 int old_safemode; 2950 int64 size; 2951 int64 offend; 2952 2953 if(debug['r']) 2954 print("geneq %S %T\n", sym, t); 2955 2956 lineno = 1; // less confusing than end of input 2957 dclcontext = PEXTERN; 2958 markdcl(); 2959 2960 // func sym(p, q *T, s uintptr) bool 2961 fn = nod(ODCLFUNC, N, N); 2962 fn->nname = newname(sym); 2963 fn->nname->class = PFUNC; 2964 tfn = nod(OTFUNC, N, N); 2965 fn->nname->ntype = tfn; 2966 2967 n = nod(ODCLFIELD, newname(lookup("p")), typenod(ptrto(t))); 2968 tfn->list = list(tfn->list, n); 2969 np = n->left; 2970 n = nod(ODCLFIELD, newname(lookup("q")), typenod(ptrto(t))); 2971 tfn->list = list(tfn->list, n); 2972 nq = n->left; 2973 n = nod(ODCLFIELD, newname(lookup("s")), typenod(types[TUINTPTR])); 2974 tfn->list = list(tfn->list, n); 2975 n = nod(ODCLFIELD, N, typenod(types[TBOOL])); 2976 tfn->rlist = list(tfn->rlist, n); 2977 2978 funchdr(fn); 2979 2980 // geneq is only called for types that have equality but 2981 // cannot be handled by the standard algorithms, 2982 // so t must be either an array or a struct. 2983 switch(t->etype) { 2984 default: 2985 fatal("geneq %T", t); 2986 case TARRAY: 2987 if(isslice(t)) 2988 fatal("geneq %T", t); 2989 // An array of pure memory would be handled by the 2990 // standard memequal, so the element type must not be 2991 // pure memory. Even if we unrolled the range loop, 2992 // each iteration would be a function call, so don't bother 2993 // unrolling. 2994 nrange = nod(ORANGE, N, nod(OIND, np, N)); 2995 ni = newname(lookup("i")); 2996 ni->type = types[TINT]; 2997 nrange->list = list1(ni); 2998 nrange->colas = 1; 2999 colasdefn(nrange->list, nrange); 3000 ni = nrange->list->n; 3001 3002 // if p[i] != q[i] { return false } 3003 nx = nod(OINDEX, np, ni); 3004 nx->bounded = 1; 3005 ny = nod(OINDEX, nq, ni); 3006 ny->bounded = 1; 3007 3008 nif = nod(OIF, N, N); 3009 nif->ntest = nod(ONE, nx, ny); 3010 r = nod(ORETURN, N, N); 3011 r->list = list(r->list, nodbool(0)); 3012 nif->nbody = list(nif->nbody, r); 3013 nrange->nbody = list(nrange->nbody, nif); 3014 fn->nbody = list(fn->nbody, nrange); 3015 break; 3016 3017 case TSTRUCT: 3018 // Walk the struct using memequal for runs of AMEM 3019 // and calling specific equality tests for the others. 3020 // Skip blank-named fields. 3021 first = T; 3022 offend = 0; 3023 for(t1=t->type;; t1=t1->down) { 3024 if(t1 != T && algtype1(t1->type, nil) == AMEM && !isblanksym(t1->sym)) { 3025 offend = t1->width + t1->type->width; 3026 if(first == T) 3027 first = t1; 3028 // If it's a memory field but it's padded, stop here. 3029 if(ispaddedfield(t1, t->width)) 3030 t1 = t1->down; 3031 else 3032 continue; 3033 } 3034 // Run memequal for fields up to this one. 3035 // TODO(rsc): All the calls to newname are wrong for 3036 // cross-package unexported fields. 3037 if(first != T) { 3038 if(first->down == t1) { 3039 fn->nbody = list(fn->nbody, eqfield(np, nq, newname(first->sym))); 3040 } else if(first->down->down == t1) { 3041 fn->nbody = list(fn->nbody, eqfield(np, nq, newname(first->sym))); 3042 first = first->down; 3043 if(!isblanksym(first->sym)) 3044 fn->nbody = list(fn->nbody, eqfield(np, nq, newname(first->sym))); 3045 } else { 3046 // More than two fields: use memequal. 3047 size = offend - first->width; // first->width is offset 3048 fn->nbody = list(fn->nbody, eqmem(np, nq, newname(first->sym), size)); 3049 } 3050 first = T; 3051 } 3052 if(t1 == T) 3053 break; 3054 if(isblanksym(t1->sym)) 3055 continue; 3056 3057 // Check this field, which is not just memory. 3058 fn->nbody = list(fn->nbody, eqfield(np, nq, newname(t1->sym))); 3059 } 3060 3061 break; 3062 } 3063 3064 // return true 3065 r = nod(ORETURN, N, N); 3066 r->list = list(r->list, nodbool(1)); 3067 fn->nbody = list(fn->nbody, r); 3068 3069 if(debug['r']) 3070 dumplist("geneq body", fn->nbody); 3071 3072 funcbody(fn); 3073 curfn = fn; 3074 fn->dupok = 1; 3075 typecheck(&fn, Etop); 3076 typechecklist(fn->nbody, Etop); 3077 curfn = nil; 3078 3079 // Disable safemode while compiling this code: the code we 3080 // generate internally can refer to unsafe.Pointer. 3081 // In this case it can happen if we need to generate an == 3082 // for a struct containing a reflect.Value, which itself has 3083 // an unexported field of type unsafe.Pointer. 3084 old_safemode = safemode; 3085 safemode = 0; 3086 funccompile(fn, 0); 3087 safemode = old_safemode; 3088 } 3089 3090 static Type* 3091 ifacelookdot(Sym *s, Type *t, int *followptr, int ignorecase) 3092 { 3093 int i, c, d; 3094 Type *m; 3095 3096 *followptr = 0; 3097 3098 if(t == T) 3099 return T; 3100 3101 for(d=0; d<nelem(dotlist); d++) { 3102 c = adddot1(s, t, d, &m, ignorecase); 3103 if(c > 1) { 3104 yyerror("%T.%S is ambiguous", t, s); 3105 return T; 3106 } 3107 if(c == 1) { 3108 for(i=0; i<d; i++) { 3109 if(isptr[dotlist[i].field->type->etype]) { 3110 *followptr = 1; 3111 break; 3112 } 3113 } 3114 if(m->type->etype != TFUNC || m->type->thistuple == 0) { 3115 yyerror("%T.%S is a field, not a method", t, s); 3116 return T; 3117 } 3118 return m; 3119 } 3120 } 3121 return T; 3122 } 3123 3124 int 3125 implements(Type *t, Type *iface, Type **m, Type **samename, int *ptr) 3126 { 3127 Type *t0, *im, *tm, *rcvr, *imtype; 3128 int followptr; 3129 3130 t0 = t; 3131 if(t == T) 3132 return 0; 3133 3134 // if this is too slow, 3135 // could sort these first 3136 // and then do one loop. 3137 3138 if(t->etype == TINTER) { 3139 for(im=iface->type; im; im=im->down) { 3140 for(tm=t->type; tm; tm=tm->down) { 3141 if(tm->sym == im->sym) { 3142 if(eqtype(tm->type, im->type)) 3143 goto found; 3144 *m = im; 3145 *samename = tm; 3146 *ptr = 0; 3147 return 0; 3148 } 3149 } 3150 *m = im; 3151 *samename = nil; 3152 *ptr = 0; 3153 return 0; 3154 found:; 3155 } 3156 return 1; 3157 } 3158 3159 t = methtype(t, 0); 3160 if(t != T) 3161 expandmeth(t); 3162 for(im=iface->type; im; im=im->down) { 3163 imtype = methodfunc(im->type, 0); 3164 tm = ifacelookdot(im->sym, t, &followptr, 0); 3165 if(tm == T || tm->nointerface || !eqtype(methodfunc(tm->type, 0), imtype)) { 3166 if(tm == T) 3167 tm = ifacelookdot(im->sym, t, &followptr, 1); 3168 *m = im; 3169 *samename = tm; 3170 *ptr = 0; 3171 return 0; 3172 } 3173 // if pointer receiver in method, 3174 // the method does not exist for value types. 3175 rcvr = getthisx(tm->type)->type->type; 3176 if(isptr[rcvr->etype] && !isptr[t0->etype] && !followptr && !isifacemethod(tm->type)) { 3177 if(0 && debug['r']) 3178 yyerror("interface pointer mismatch"); 3179 3180 *m = im; 3181 *samename = nil; 3182 *ptr = 1; 3183 return 0; 3184 } 3185 } 3186 return 1; 3187 } 3188 3189 /* 3190 * even simpler simtype; get rid of ptr, bool. 3191 * assuming that the front end has rejected 3192 * all the invalid conversions (like ptr -> bool) 3193 */ 3194 int 3195 simsimtype(Type *t) 3196 { 3197 int et; 3198 3199 if(t == 0) 3200 return 0; 3201 3202 et = simtype[t->etype]; 3203 switch(et) { 3204 case TPTR32: 3205 et = TUINT32; 3206 break; 3207 case TPTR64: 3208 et = TUINT64; 3209 break; 3210 case TBOOL: 3211 et = TUINT8; 3212 break; 3213 } 3214 return et; 3215 } 3216 3217 NodeList* 3218 concat(NodeList *a, NodeList *b) 3219 { 3220 if(a == nil) 3221 return b; 3222 if(b == nil) 3223 return a; 3224 3225 a->end->next = b; 3226 a->end = b->end; 3227 b->end = nil; 3228 return a; 3229 } 3230 3231 NodeList* 3232 list1(Node *n) 3233 { 3234 NodeList *l; 3235 3236 if(n == nil) 3237 return nil; 3238 if(n->op == OBLOCK && n->ninit == nil) { 3239 // Flatten list and steal storage. 3240 // Poison pointer to catch errant uses. 3241 l = n->list; 3242 n->list = (NodeList*)1; 3243 return l; 3244 } 3245 l = mal(sizeof *l); 3246 l->n = n; 3247 l->end = l; 3248 return l; 3249 } 3250 3251 NodeList* 3252 list(NodeList *l, Node *n) 3253 { 3254 return concat(l, list1(n)); 3255 } 3256 3257 void 3258 listsort(NodeList** l, int(*f)(Node*, Node*)) 3259 { 3260 NodeList *l1, *l2, *le; 3261 3262 if(*l == nil || (*l)->next == nil) 3263 return; 3264 3265 l1 = *l; 3266 l2 = *l; 3267 for(;;) { 3268 l2 = l2->next; 3269 if(l2 == nil) 3270 break; 3271 l2 = l2->next; 3272 if(l2 == nil) 3273 break; 3274 l1 = l1->next; 3275 } 3276 3277 l2 = l1->next; 3278 l1->next = nil; 3279 l2->end = (*l)->end; 3280 (*l)->end = l1; 3281 3282 l1 = *l; 3283 listsort(&l1, f); 3284 listsort(&l2, f); 3285 3286 if((*f)(l1->n, l2->n) < 0) { 3287 *l = l1; 3288 } else { 3289 *l = l2; 3290 l2 = l1; 3291 l1 = *l; 3292 } 3293 3294 // now l1 == *l; and l1 < l2 3295 3296 while ((l1 != nil) && (l2 != nil)) { 3297 while ((l1->next != nil) && (*f)(l1->next->n, l2->n) < 0) 3298 l1 = l1->next; 3299 3300 // l1 is last one from l1 that is < l2 3301 le = l1->next; // le is the rest of l1, first one that is >= l2 3302 if(le != nil) 3303 le->end = (*l)->end; 3304 3305 (*l)->end = l1; // cut *l at l1 3306 *l = concat(*l, l2); // glue l2 to *l's tail 3307 3308 l1 = l2; // l1 is the first element of *l that is < the new l2 3309 l2 = le; // ... because l2 now is the old tail of l1 3310 } 3311 3312 *l = concat(*l, l2); // any remainder 3313 } 3314 3315 NodeList* 3316 listtreecopy(NodeList *l) 3317 { 3318 NodeList *out; 3319 3320 out = nil; 3321 for(; l; l=l->next) 3322 out = list(out, treecopy(l->n)); 3323 return out; 3324 } 3325 3326 Node* 3327 liststmt(NodeList *l) 3328 { 3329 Node *n; 3330 3331 n = nod(OBLOCK, N, N); 3332 n->list = l; 3333 if(l) 3334 n->lineno = l->n->lineno; 3335 return n; 3336 } 3337 3338 /* 3339 * return nelem of list 3340 */ 3341 int 3342 count(NodeList *l) 3343 { 3344 vlong n; 3345 3346 n = 0; 3347 for(; l; l=l->next) 3348 n++; 3349 if((int)n != n) { // Overflow. 3350 yyerror("too many elements in list"); 3351 } 3352 return n; 3353 } 3354 3355 /* 3356 * return nelem of list 3357 */ 3358 int 3359 structcount(Type *t) 3360 { 3361 int v; 3362 Iter s; 3363 3364 v = 0; 3365 for(t = structfirst(&s, &t); t != T; t = structnext(&s)) 3366 v++; 3367 return v; 3368 } 3369 3370 /* 3371 * return power of 2 of the constant 3372 * operand. -1 if it is not a power of 2. 3373 * 1000+ if it is a -(power of 2) 3374 */ 3375 int 3376 powtwo(Node *n) 3377 { 3378 uvlong v, b; 3379 int i; 3380 3381 if(n == N || n->op != OLITERAL || n->type == T) 3382 goto no; 3383 if(!isint[n->type->etype]) 3384 goto no; 3385 3386 v = mpgetfix(n->val.u.xval); 3387 b = 1ULL; 3388 for(i=0; i<64; i++) { 3389 if(b == v) 3390 return i; 3391 b = b<<1; 3392 } 3393 3394 if(!issigned[n->type->etype]) 3395 goto no; 3396 3397 v = -v; 3398 b = 1ULL; 3399 for(i=0; i<64; i++) { 3400 if(b == v) 3401 return i+1000; 3402 b = b<<1; 3403 } 3404 3405 no: 3406 return -1; 3407 } 3408 3409 /* 3410 * return the unsigned type for 3411 * a signed integer type. 3412 * returns T if input is not a 3413 * signed integer type. 3414 */ 3415 Type* 3416 tounsigned(Type *t) 3417 { 3418 3419 // this is types[et+1], but not sure 3420 // that this relation is immutable 3421 switch(t->etype) { 3422 default: 3423 print("tounsigned: unknown type %T\n", t); 3424 t = T; 3425 break; 3426 case TINT: 3427 t = types[TUINT]; 3428 break; 3429 case TINT8: 3430 t = types[TUINT8]; 3431 break; 3432 case TINT16: 3433 t = types[TUINT16]; 3434 break; 3435 case TINT32: 3436 t = types[TUINT32]; 3437 break; 3438 case TINT64: 3439 t = types[TUINT64]; 3440 break; 3441 } 3442 return t; 3443 } 3444 3445 /* 3446 * magic number for signed division 3447 * see hacker's delight chapter 10 3448 */ 3449 void 3450 smagic(Magic *m) 3451 { 3452 int p; 3453 uint64 ad, anc, delta, q1, r1, q2, r2, t; 3454 uint64 mask, two31; 3455 3456 m->bad = 0; 3457 switch(m->w) { 3458 default: 3459 m->bad = 1; 3460 return; 3461 case 8: 3462 mask = 0xffLL; 3463 break; 3464 case 16: 3465 mask = 0xffffLL; 3466 break; 3467 case 32: 3468 mask = 0xffffffffLL; 3469 break; 3470 case 64: 3471 mask = 0xffffffffffffffffULL; 3472 break; 3473 } 3474 two31 = mask ^ (mask>>1); 3475 3476 p = m->w-1; 3477 ad = m->sd; 3478 if(m->sd < 0) 3479 ad = -(uvlong)m->sd; 3480 3481 // bad denominators 3482 if(ad == 0 || ad == 1 || ad == two31) { 3483 m->bad = 1; 3484 return; 3485 } 3486 3487 t = two31; 3488 ad &= mask; 3489 3490 anc = t - 1 - t%ad; 3491 anc &= mask; 3492 3493 q1 = two31/anc; 3494 r1 = two31 - q1*anc; 3495 q1 &= mask; 3496 r1 &= mask; 3497 3498 q2 = two31/ad; 3499 r2 = two31 - q2*ad; 3500 q2 &= mask; 3501 r2 &= mask; 3502 3503 for(;;) { 3504 p++; 3505 q1 <<= 1; 3506 r1 <<= 1; 3507 q1 &= mask; 3508 r1 &= mask; 3509 if(r1 >= anc) { 3510 q1++; 3511 r1 -= anc; 3512 q1 &= mask; 3513 r1 &= mask; 3514 } 3515 3516 q2 <<= 1; 3517 r2 <<= 1; 3518 q2 &= mask; 3519 r2 &= mask; 3520 if(r2 >= ad) { 3521 q2++; 3522 r2 -= ad; 3523 q2 &= mask; 3524 r2 &= mask; 3525 } 3526 3527 delta = ad - r2; 3528 delta &= mask; 3529 if(q1 < delta || (q1 == delta && r1 == 0)) { 3530 continue; 3531 } 3532 break; 3533 } 3534 3535 m->sm = q2+1; 3536 if(m->sm & two31) 3537 m->sm |= ~mask; 3538 m->s = p-m->w; 3539 } 3540 3541 /* 3542 * magic number for unsigned division 3543 * see hacker's delight chapter 10 3544 */ 3545 void 3546 umagic(Magic *m) 3547 { 3548 int p; 3549 uint64 nc, delta, q1, r1, q2, r2; 3550 uint64 mask, two31; 3551 3552 m->bad = 0; 3553 m->ua = 0; 3554 3555 switch(m->w) { 3556 default: 3557 m->bad = 1; 3558 return; 3559 case 8: 3560 mask = 0xffLL; 3561 break; 3562 case 16: 3563 mask = 0xffffLL; 3564 break; 3565 case 32: 3566 mask = 0xffffffffLL; 3567 break; 3568 case 64: 3569 mask = 0xffffffffffffffffULL; 3570 break; 3571 } 3572 two31 = mask ^ (mask>>1); 3573 3574 m->ud &= mask; 3575 if(m->ud == 0 || m->ud == two31) { 3576 m->bad = 1; 3577 return; 3578 } 3579 nc = mask - (-m->ud&mask)%m->ud; 3580 p = m->w-1; 3581 3582 q1 = two31/nc; 3583 r1 = two31 - q1*nc; 3584 q1 &= mask; 3585 r1 &= mask; 3586 3587 q2 = (two31-1) / m->ud; 3588 r2 = (two31-1) - q2*m->ud; 3589 q2 &= mask; 3590 r2 &= mask; 3591 3592 for(;;) { 3593 p++; 3594 if(r1 >= nc-r1) { 3595 q1 <<= 1; 3596 q1++; 3597 r1 <<= 1; 3598 r1 -= nc; 3599 } else { 3600 q1 <<= 1; 3601 r1 <<= 1; 3602 } 3603 q1 &= mask; 3604 r1 &= mask; 3605 if(r2+1 >= m->ud-r2) { 3606 if(q2 >= two31-1) { 3607 m->ua = 1; 3608 } 3609 q2 <<= 1; 3610 q2++; 3611 r2 <<= 1; 3612 r2++; 3613 r2 -= m->ud; 3614 } else { 3615 if(q2 >= two31) { 3616 m->ua = 1; 3617 } 3618 q2 <<= 1; 3619 r2 <<= 1; 3620 r2++; 3621 } 3622 q2 &= mask; 3623 r2 &= mask; 3624 3625 delta = m->ud - 1 - r2; 3626 delta &= mask; 3627 3628 if(p < m->w+m->w) 3629 if(q1 < delta || (q1 == delta && r1 == 0)) { 3630 continue; 3631 } 3632 break; 3633 } 3634 m->um = q2+1; 3635 m->s = p-m->w; 3636 } 3637 3638 Sym* 3639 ngotype(Node *n) 3640 { 3641 if(n->type != T) 3642 return typenamesym(n->type); 3643 return S; 3644 } 3645 3646 /* 3647 * Convert raw string to the prefix that will be used in the symbol 3648 * table. All control characters, space, '%' and '"', as well as 3649 * non-7-bit clean bytes turn into %xx. The period needs escaping 3650 * only in the last segment of the path, and it makes for happier 3651 * users if we escape that as little as possible. 3652 * 3653 * If you edit this, edit ../ld/lib.c:/^pathtoprefix too. 3654 * If you edit this, edit ../../debug/goobj/read.go:/importPathToPrefix too. 3655 */ 3656 static char* 3657 pathtoprefix(char *s) 3658 { 3659 static char hex[] = "0123456789abcdef"; 3660 char *p, *r, *w, *l; 3661 int n; 3662 3663 // find first character past the last slash, if any. 3664 l = s; 3665 for(r=s; *r; r++) 3666 if(*r == '/') 3667 l = r+1; 3668 3669 // check for chars that need escaping 3670 n = 0; 3671 for(r=s; *r; r++) 3672 if(*r <= ' ' || (*r == '.' && r >= l) || *r == '%' || *r == '"' || *r >= 0x7f) 3673 n++; 3674 3675 // quick exit 3676 if(n == 0) 3677 return s; 3678 3679 // escape 3680 p = mal((r-s)+1+2*n); 3681 for(r=s, w=p; *r; r++) { 3682 if(*r <= ' ' || (*r == '.' && r >= l) || *r == '%' || *r == '"' || *r >= 0x7f) { 3683 *w++ = '%'; 3684 *w++ = hex[(*r>>4)&0xF]; 3685 *w++ = hex[*r&0xF]; 3686 } else 3687 *w++ = *r; 3688 } 3689 *w = '\0'; 3690 return p; 3691 } 3692 3693 Pkg* 3694 mkpkg(Strlit *path) 3695 { 3696 Pkg *p; 3697 int h; 3698 3699 h = stringhash(path->s) & (nelem(phash)-1); 3700 for(p=phash[h]; p; p=p->link) 3701 if(p->path->len == path->len && memcmp(path->s, p->path->s, path->len) == 0) 3702 return p; 3703 3704 p = mal(sizeof *p); 3705 p->path = path; 3706 p->prefix = pathtoprefix(path->s); 3707 p->link = phash[h]; 3708 phash[h] = p; 3709 return p; 3710 } 3711 3712 Strlit* 3713 strlit(char *s) 3714 { 3715 Strlit *t; 3716 3717 t = mal(sizeof *t + strlen(s)); 3718 strcpy(t->s, s); 3719 t->len = strlen(s); 3720 return t; 3721 } 3722 3723 void 3724 addinit(Node **np, NodeList *init) 3725 { 3726 Node *n; 3727 3728 if(init == nil) 3729 return; 3730 3731 n = *np; 3732 switch(n->op) { 3733 case ONAME: 3734 case OLITERAL: 3735 // There may be multiple refs to this node; 3736 // introduce OCONVNOP to hold init list. 3737 n = nod(OCONVNOP, n, N); 3738 n->type = n->left->type; 3739 n->typecheck = 1; 3740 *np = n; 3741 break; 3742 } 3743 n->ninit = concat(init, n->ninit); 3744 n->ullman = UINF; 3745 } 3746 3747 static char* reservedimports[] = { 3748 "go", 3749 "type", 3750 }; 3751 3752 int 3753 isbadimport(Strlit *path) 3754 { 3755 int i; 3756 char *s; 3757 Rune r; 3758 3759 if(strlen(path->s) != path->len) { 3760 yyerror("import path contains NUL"); 3761 return 1; 3762 } 3763 3764 for(i=0; i<nelem(reservedimports); i++) { 3765 if(strcmp(path->s, reservedimports[i]) == 0) { 3766 yyerror("import path \"%s\" is reserved and cannot be used", path->s); 3767 return 1; 3768 } 3769 } 3770 3771 s = path->s; 3772 while(*s) { 3773 s += chartorune(&r, s); 3774 if(r == Runeerror) { 3775 yyerror("import path contains invalid UTF-8 sequence: \"%Z\"", path); 3776 return 1; 3777 } 3778 if(r < 0x20 || r == 0x7f) { 3779 yyerror("import path contains control character: \"%Z\"", path); 3780 return 1; 3781 } 3782 if(r == '\\') { 3783 yyerror("import path contains backslash; use slash: \"%Z\"", path); 3784 return 1; 3785 } 3786 if(isspacerune(r)) { 3787 yyerror("import path contains space character: \"%Z\"", path); 3788 return 1; 3789 } 3790 if(utfrune("!\"#$%&'()*,:;<=>?[]^`{|}", r)) { 3791 yyerror("import path contains invalid character '%C': \"%Z\"", r, path); 3792 return 1; 3793 } 3794 } 3795 return 0; 3796 } 3797 3798 void 3799 checknil(Node *x, NodeList **init) 3800 { 3801 Node *n; 3802 3803 if(isinter(x->type)) { 3804 x = nod(OITAB, x, N); 3805 typecheck(&x, Erv); 3806 } 3807 n = nod(OCHECKNIL, x, N); 3808 n->typecheck = 1; 3809 *init = list(*init, n); 3810 } 3811 3812 /* 3813 * Can this type be stored directly in an interface word? 3814 */ 3815 int 3816 isdirectiface(Type *t) 3817 { 3818 // Setting IfacePointerOnly = 1 changes the 3819 // interface representation so that the data word 3820 // in an interface value must always be a pointer. 3821 // Setting it to 0 uses the original representation, 3822 // where the data word can hold a pointer or any 3823 // non-pointer value no bigger than a pointer. 3824 enum { 3825 IfacePointerOnly = 1, 3826 }; 3827 3828 if(IfacePointerOnly) { 3829 switch(t->etype) { 3830 case TPTR32: 3831 case TPTR64: 3832 case TCHAN: 3833 case TMAP: 3834 case TFUNC: 3835 case TUNSAFEPTR: 3836 return 1; 3837 case TARRAY: 3838 // Array of 1 direct iface type can be direct. 3839 return t->bound == 1 && isdirectiface(t->type); 3840 case TSTRUCT: 3841 // Struct with 1 field of direct iface type can be direct. 3842 return t->type != T && t->type->down == T && isdirectiface(t->type->type); 3843 } 3844 return 0; 3845 } 3846 3847 dowidth(t); 3848 return t->width <= widthptr; 3849 }