github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/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 inlcalls(fn); 2618 curfn = nil; 2619 funccompile(fn, 0); 2620 } 2621 2622 static Node* 2623 hashmem(Type *t) 2624 { 2625 Node *tfn, *n; 2626 Sym *sym; 2627 2628 sym = pkglookup("memhash", runtimepkg); 2629 2630 n = newname(sym); 2631 n->class = PFUNC; 2632 tfn = nod(OTFUNC, N, N); 2633 tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(ptrto(t)))); 2634 tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(types[TUINTPTR]))); 2635 tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(types[TUINTPTR]))); 2636 tfn->rlist = list(tfn->rlist, nod(ODCLFIELD, N, typenod(types[TUINTPTR]))); 2637 typecheck(&tfn, Etype); 2638 n->type = tfn->type; 2639 return n; 2640 } 2641 2642 static Node* 2643 hashfor(Type *t) 2644 { 2645 int a; 2646 Sym *sym; 2647 Node *tfn, *n; 2648 2649 a = algtype1(t, nil); 2650 switch(a) { 2651 case AMEM: 2652 return hashmem(t); 2653 case AINTER: 2654 sym = pkglookup("interhash", runtimepkg); 2655 break; 2656 case ANILINTER: 2657 sym = pkglookup("nilinterhash", runtimepkg); 2658 break; 2659 case ASTRING: 2660 sym = pkglookup("strhash", runtimepkg); 2661 break; 2662 case AFLOAT32: 2663 sym = pkglookup("f32hash", runtimepkg); 2664 break; 2665 case AFLOAT64: 2666 sym = pkglookup("f64hash", runtimepkg); 2667 break; 2668 case ACPLX64: 2669 sym = pkglookup("c64hash", runtimepkg); 2670 break; 2671 case ACPLX128: 2672 sym = pkglookup("c128hash", runtimepkg); 2673 break; 2674 default: 2675 sym = typesymprefix(".hash", t); 2676 break; 2677 } 2678 2679 n = newname(sym); 2680 n->class = PFUNC; 2681 tfn = nod(OTFUNC, N, N); 2682 tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(ptrto(t)))); 2683 tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(types[TUINTPTR]))); 2684 tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(types[TUINTPTR]))); 2685 tfn->rlist = list(tfn->rlist, nod(ODCLFIELD, N, typenod(types[TUINTPTR]))); 2686 typecheck(&tfn, Etype); 2687 n->type = tfn->type; 2688 return n; 2689 } 2690 2691 /* 2692 * Generate a helper function to compute the hash of a value of type t. 2693 */ 2694 void 2695 genhash(Sym *sym, Type *t) 2696 { 2697 Node *n, *fn, *np, *nh, *ni, *call, *nx, *na, *tfn, *r; 2698 Node *hashel; 2699 Type *first, *t1; 2700 int old_safemode; 2701 int64 size, mul, offend; 2702 2703 if(debug['r']) 2704 print("genhash %S %T\n", sym, t); 2705 2706 lineno = 1; // less confusing than end of input 2707 dclcontext = PEXTERN; 2708 markdcl(); 2709 2710 // func sym(p *T, s uintptr, h uintptr) uintptr 2711 fn = nod(ODCLFUNC, N, N); 2712 fn->nname = newname(sym); 2713 fn->nname->class = PFUNC; 2714 tfn = nod(OTFUNC, N, N); 2715 fn->nname->ntype = tfn; 2716 2717 n = nod(ODCLFIELD, newname(lookup("p")), typenod(ptrto(t))); 2718 tfn->list = list(tfn->list, n); 2719 np = n->left; 2720 n = nod(ODCLFIELD, newname(lookup("s")), typenod(types[TUINTPTR])); 2721 tfn->list = list(tfn->list, n); 2722 n = nod(ODCLFIELD, newname(lookup("h")), typenod(types[TUINTPTR])); 2723 tfn->list = list(tfn->list, n); 2724 nh = n->left; 2725 n = nod(ODCLFIELD, N, typenod(types[TUINTPTR])); // return value 2726 tfn->rlist = list(tfn->rlist, n); 2727 2728 funchdr(fn); 2729 typecheck(&fn->nname->ntype, Etype); 2730 2731 // genhash is only called for types that have equality but 2732 // cannot be handled by the standard algorithms, 2733 // so t must be either an array or a struct. 2734 switch(t->etype) { 2735 default: 2736 fatal("genhash %T", t); 2737 case TARRAY: 2738 if(isslice(t)) 2739 fatal("genhash %T", t); 2740 // An array of pure memory would be handled by the 2741 // standard algorithm, so the element type must not be 2742 // pure memory. 2743 hashel = hashfor(t->type); 2744 n = nod(ORANGE, N, nod(OIND, np, N)); 2745 ni = newname(lookup("i")); 2746 ni->type = types[TINT]; 2747 n->list = list1(ni); 2748 n->colas = 1; 2749 colasdefn(n->list, n); 2750 ni = n->list->n; 2751 2752 // TODO: with aeshash we don't need these shift/mul parts 2753 2754 // h = h<<3 | h>>61 2755 n->nbody = list(n->nbody, 2756 nod(OAS, 2757 nh, 2758 nod(OOR, 2759 nod(OLSH, nh, nodintconst(3)), 2760 nod(ORSH, nh, nodintconst(widthptr*8-3))))); 2761 2762 // h *= mul 2763 // Same multipliers as in runtime.memhash. 2764 if(widthptr == 4) 2765 mul = 3267000013LL; 2766 else 2767 mul = 23344194077549503LL; 2768 n->nbody = list(n->nbody, 2769 nod(OAS, 2770 nh, 2771 nod(OMUL, nh, nodintconst(mul)))); 2772 2773 // h = hashel(&p[i], sizeof(p[i]), h) 2774 call = nod(OCALL, hashel, N); 2775 nx = nod(OINDEX, np, ni); 2776 nx->bounded = 1; 2777 na = nod(OADDR, nx, N); 2778 na->etype = 1; // no escape to heap 2779 call->list = list(call->list, na); 2780 call->list = list(call->list, nodintconst(t->type->width)); 2781 call->list = list(call->list, nh); 2782 n->nbody = list(n->nbody, nod(OAS, nh, call)); 2783 2784 fn->nbody = list(fn->nbody, n); 2785 break; 2786 2787 case TSTRUCT: 2788 // Walk the struct using memhash for runs of AMEM 2789 // and calling specific hash functions for the others. 2790 first = T; 2791 offend = 0; 2792 for(t1=t->type;; t1=t1->down) { 2793 if(t1 != T && algtype1(t1->type, nil) == AMEM && !isblanksym(t1->sym)) { 2794 offend = t1->width + t1->type->width; 2795 if(first == T) 2796 first = t1; 2797 // If it's a memory field but it's padded, stop here. 2798 if(ispaddedfield(t1, t->width)) 2799 t1 = t1->down; 2800 else 2801 continue; 2802 } 2803 // Run memhash for fields up to this one. 2804 if(first != T) { 2805 size = offend - first->width; // first->width is offset 2806 hashel = hashmem(first->type); 2807 // h = hashel(&p.first, size, h) 2808 call = nod(OCALL, hashel, N); 2809 nx = nod(OXDOT, np, newname(first->sym)); // TODO: fields from other packages? 2810 na = nod(OADDR, nx, N); 2811 na->etype = 1; // no escape to heap 2812 call->list = list(call->list, na); 2813 call->list = list(call->list, nodintconst(size)); 2814 call->list = list(call->list, nh); 2815 fn->nbody = list(fn->nbody, nod(OAS, nh, call)); 2816 2817 first = T; 2818 } 2819 if(t1 == T) 2820 break; 2821 if(isblanksym(t1->sym)) 2822 continue; 2823 2824 // Run hash for this field. 2825 hashel = hashfor(t1->type); 2826 // h = hashel(&p.t1, size, h) 2827 call = nod(OCALL, hashel, N); 2828 nx = nod(OXDOT, np, newname(t1->sym)); // TODO: fields from other packages? 2829 na = nod(OADDR, nx, N); 2830 na->etype = 1; // no escape to heap 2831 call->list = list(call->list, na); 2832 call->list = list(call->list, nodintconst(t1->type->width)); 2833 call->list = list(call->list, nh); 2834 fn->nbody = list(fn->nbody, nod(OAS, nh, call)); 2835 } 2836 break; 2837 } 2838 r = nod(ORETURN, N, N); 2839 r->list = list(r->list, nh); 2840 fn->nbody = list(fn->nbody, r); 2841 2842 if(debug['r']) 2843 dumplist("genhash body", fn->nbody); 2844 2845 funcbody(fn); 2846 curfn = fn; 2847 fn->dupok = 1; 2848 typecheck(&fn, Etop); 2849 typechecklist(fn->nbody, Etop); 2850 curfn = nil; 2851 2852 // Disable safemode while compiling this code: the code we 2853 // generate internally can refer to unsafe.Pointer. 2854 // In this case it can happen if we need to generate an == 2855 // for a struct containing a reflect.Value, which itself has 2856 // an unexported field of type unsafe.Pointer. 2857 old_safemode = safemode; 2858 safemode = 0; 2859 funccompile(fn, 0); 2860 safemode = old_safemode; 2861 } 2862 2863 // Return node for 2864 // if p.field != q.field { return false } 2865 static Node* 2866 eqfield(Node *p, Node *q, Node *field) 2867 { 2868 Node *nif, *nx, *ny, *r; 2869 2870 nx = nod(OXDOT, p, field); 2871 ny = nod(OXDOT, q, field); 2872 nif = nod(OIF, N, N); 2873 nif->ntest = nod(ONE, nx, ny); 2874 r = nod(ORETURN, N, N); 2875 r->list = list(r->list, nodbool(0)); 2876 nif->nbody = list(nif->nbody, r); 2877 return nif; 2878 } 2879 2880 static Node* 2881 eqmemfunc(vlong size, Type *type) 2882 { 2883 char buf[30]; 2884 Node *fn; 2885 2886 switch(size) { 2887 default: 2888 fn = syslook("memequal", 1); 2889 break; 2890 case 1: 2891 case 2: 2892 case 4: 2893 case 8: 2894 case 16: 2895 snprint(buf, sizeof buf, "memequal%d", (int)size*8); 2896 fn = syslook(buf, 1); 2897 break; 2898 } 2899 argtype(fn, type); 2900 argtype(fn, type); 2901 return fn; 2902 } 2903 2904 // Return node for 2905 // if !memequal(&p.field, &q.field, size) { return false } 2906 static Node* 2907 eqmem(Node *p, Node *q, Node *field, vlong size) 2908 { 2909 Node *nif, *nx, *ny, *call, *r; 2910 2911 nx = nod(OADDR, nod(OXDOT, p, field), N); 2912 nx->etype = 1; // does not escape 2913 ny = nod(OADDR, nod(OXDOT, q, field), N); 2914 ny->etype = 1; // does not escape 2915 typecheck(&nx, Erv); 2916 typecheck(&ny, Erv); 2917 2918 call = nod(OCALL, eqmemfunc(size, nx->type->type), N); 2919 call->list = list(call->list, nx); 2920 call->list = list(call->list, ny); 2921 call->list = list(call->list, nodintconst(size)); 2922 2923 nif = nod(OIF, N, N); 2924 nif->ninit = list(nif->ninit, call); 2925 nif->ntest = nod(ONOT, call, N); 2926 r = nod(ORETURN, N, N); 2927 r->list = list(r->list, nodbool(0)); 2928 nif->nbody = list(nif->nbody, r); 2929 return nif; 2930 } 2931 2932 /* 2933 * Generate a helper function to check equality of two values of type t. 2934 */ 2935 void 2936 geneq(Sym *sym, Type *t) 2937 { 2938 Node *n, *fn, *np, *nq, *tfn, *nif, *ni, *nx, *ny, *nrange, *r; 2939 Type *t1, *first; 2940 int old_safemode; 2941 int64 size; 2942 int64 offend; 2943 2944 if(debug['r']) 2945 print("geneq %S %T\n", sym, t); 2946 2947 lineno = 1; // less confusing than end of input 2948 dclcontext = PEXTERN; 2949 markdcl(); 2950 2951 // func sym(p, q *T, s uintptr) bool 2952 fn = nod(ODCLFUNC, N, N); 2953 fn->nname = newname(sym); 2954 fn->nname->class = PFUNC; 2955 tfn = nod(OTFUNC, N, N); 2956 fn->nname->ntype = tfn; 2957 2958 n = nod(ODCLFIELD, newname(lookup("p")), typenod(ptrto(t))); 2959 tfn->list = list(tfn->list, n); 2960 np = n->left; 2961 n = nod(ODCLFIELD, newname(lookup("q")), typenod(ptrto(t))); 2962 tfn->list = list(tfn->list, n); 2963 nq = n->left; 2964 n = nod(ODCLFIELD, newname(lookup("s")), typenod(types[TUINTPTR])); 2965 tfn->list = list(tfn->list, n); 2966 n = nod(ODCLFIELD, N, typenod(types[TBOOL])); 2967 tfn->rlist = list(tfn->rlist, n); 2968 2969 funchdr(fn); 2970 2971 // geneq is only called for types that have equality but 2972 // cannot be handled by the standard algorithms, 2973 // so t must be either an array or a struct. 2974 switch(t->etype) { 2975 default: 2976 fatal("geneq %T", t); 2977 case TARRAY: 2978 if(isslice(t)) 2979 fatal("geneq %T", t); 2980 // An array of pure memory would be handled by the 2981 // standard memequal, so the element type must not be 2982 // pure memory. Even if we unrolled the range loop, 2983 // each iteration would be a function call, so don't bother 2984 // unrolling. 2985 nrange = nod(ORANGE, N, nod(OIND, np, N)); 2986 ni = newname(lookup("i")); 2987 ni->type = types[TINT]; 2988 nrange->list = list1(ni); 2989 nrange->colas = 1; 2990 colasdefn(nrange->list, nrange); 2991 ni = nrange->list->n; 2992 2993 // if p[i] != q[i] { return false } 2994 nx = nod(OINDEX, np, ni); 2995 nx->bounded = 1; 2996 ny = nod(OINDEX, nq, ni); 2997 ny->bounded = 1; 2998 2999 nif = nod(OIF, N, N); 3000 nif->ntest = nod(ONE, nx, ny); 3001 r = nod(ORETURN, N, N); 3002 r->list = list(r->list, nodbool(0)); 3003 nif->nbody = list(nif->nbody, r); 3004 nrange->nbody = list(nrange->nbody, nif); 3005 fn->nbody = list(fn->nbody, nrange); 3006 break; 3007 3008 case TSTRUCT: 3009 // Walk the struct using memequal for runs of AMEM 3010 // and calling specific equality tests for the others. 3011 // Skip blank-named fields. 3012 first = T; 3013 offend = 0; 3014 for(t1=t->type;; t1=t1->down) { 3015 if(t1 != T && algtype1(t1->type, nil) == AMEM && !isblanksym(t1->sym)) { 3016 offend = t1->width + t1->type->width; 3017 if(first == T) 3018 first = t1; 3019 // If it's a memory field but it's padded, stop here. 3020 if(ispaddedfield(t1, t->width)) 3021 t1 = t1->down; 3022 else 3023 continue; 3024 } 3025 // Run memequal for fields up to this one. 3026 // TODO(rsc): All the calls to newname are wrong for 3027 // cross-package unexported fields. 3028 if(first != T) { 3029 if(first->down == t1) { 3030 fn->nbody = list(fn->nbody, eqfield(np, nq, newname(first->sym))); 3031 } else if(first->down->down == t1) { 3032 fn->nbody = list(fn->nbody, eqfield(np, nq, newname(first->sym))); 3033 first = first->down; 3034 if(!isblanksym(first->sym)) 3035 fn->nbody = list(fn->nbody, eqfield(np, nq, newname(first->sym))); 3036 } else { 3037 // More than two fields: use memequal. 3038 size = offend - first->width; // first->width is offset 3039 fn->nbody = list(fn->nbody, eqmem(np, nq, newname(first->sym), size)); 3040 } 3041 first = T; 3042 } 3043 if(t1 == T) 3044 break; 3045 if(isblanksym(t1->sym)) 3046 continue; 3047 3048 // Check this field, which is not just memory. 3049 fn->nbody = list(fn->nbody, eqfield(np, nq, newname(t1->sym))); 3050 } 3051 3052 break; 3053 } 3054 3055 // return true 3056 r = nod(ORETURN, N, N); 3057 r->list = list(r->list, nodbool(1)); 3058 fn->nbody = list(fn->nbody, r); 3059 3060 if(debug['r']) 3061 dumplist("geneq body", fn->nbody); 3062 3063 funcbody(fn); 3064 curfn = fn; 3065 fn->dupok = 1; 3066 typecheck(&fn, Etop); 3067 typechecklist(fn->nbody, Etop); 3068 curfn = nil; 3069 3070 // Disable safemode while compiling this code: the code we 3071 // generate internally can refer to unsafe.Pointer. 3072 // In this case it can happen if we need to generate an == 3073 // for a struct containing a reflect.Value, which itself has 3074 // an unexported field of type unsafe.Pointer. 3075 old_safemode = safemode; 3076 safemode = 0; 3077 funccompile(fn, 0); 3078 safemode = old_safemode; 3079 } 3080 3081 static Type* 3082 ifacelookdot(Sym *s, Type *t, int *followptr, int ignorecase) 3083 { 3084 int i, c, d; 3085 Type *m; 3086 3087 *followptr = 0; 3088 3089 if(t == T) 3090 return T; 3091 3092 for(d=0; d<nelem(dotlist); d++) { 3093 c = adddot1(s, t, d, &m, ignorecase); 3094 if(c > 1) { 3095 yyerror("%T.%S is ambiguous", t, s); 3096 return T; 3097 } 3098 if(c == 1) { 3099 for(i=0; i<d; i++) { 3100 if(isptr[dotlist[i].field->type->etype]) { 3101 *followptr = 1; 3102 break; 3103 } 3104 } 3105 if(m->type->etype != TFUNC || m->type->thistuple == 0) { 3106 yyerror("%T.%S is a field, not a method", t, s); 3107 return T; 3108 } 3109 return m; 3110 } 3111 } 3112 return T; 3113 } 3114 3115 int 3116 implements(Type *t, Type *iface, Type **m, Type **samename, int *ptr) 3117 { 3118 Type *t0, *im, *tm, *rcvr, *imtype; 3119 int followptr; 3120 3121 t0 = t; 3122 if(t == T) 3123 return 0; 3124 3125 // if this is too slow, 3126 // could sort these first 3127 // and then do one loop. 3128 3129 if(t->etype == TINTER) { 3130 for(im=iface->type; im; im=im->down) { 3131 for(tm=t->type; tm; tm=tm->down) { 3132 if(tm->sym == im->sym) { 3133 if(eqtype(tm->type, im->type)) 3134 goto found; 3135 *m = im; 3136 *samename = tm; 3137 *ptr = 0; 3138 return 0; 3139 } 3140 } 3141 *m = im; 3142 *samename = nil; 3143 *ptr = 0; 3144 return 0; 3145 found:; 3146 } 3147 return 1; 3148 } 3149 3150 t = methtype(t, 0); 3151 if(t != T) 3152 expandmeth(t); 3153 for(im=iface->type; im; im=im->down) { 3154 imtype = methodfunc(im->type, 0); 3155 tm = ifacelookdot(im->sym, t, &followptr, 0); 3156 if(tm == T || tm->nointerface || !eqtype(methodfunc(tm->type, 0), imtype)) { 3157 if(tm == T) 3158 tm = ifacelookdot(im->sym, t, &followptr, 1); 3159 *m = im; 3160 *samename = tm; 3161 *ptr = 0; 3162 return 0; 3163 } 3164 // if pointer receiver in method, 3165 // the method does not exist for value types. 3166 rcvr = getthisx(tm->type)->type->type; 3167 if(isptr[rcvr->etype] && !isptr[t0->etype] && !followptr && !isifacemethod(tm->type)) { 3168 if(0 && debug['r']) 3169 yyerror("interface pointer mismatch"); 3170 3171 *m = im; 3172 *samename = nil; 3173 *ptr = 1; 3174 return 0; 3175 } 3176 } 3177 return 1; 3178 } 3179 3180 /* 3181 * even simpler simtype; get rid of ptr, bool. 3182 * assuming that the front end has rejected 3183 * all the invalid conversions (like ptr -> bool) 3184 */ 3185 int 3186 simsimtype(Type *t) 3187 { 3188 int et; 3189 3190 if(t == 0) 3191 return 0; 3192 3193 et = simtype[t->etype]; 3194 switch(et) { 3195 case TPTR32: 3196 et = TUINT32; 3197 break; 3198 case TPTR64: 3199 et = TUINT64; 3200 break; 3201 case TBOOL: 3202 et = TUINT8; 3203 break; 3204 } 3205 return et; 3206 } 3207 3208 NodeList* 3209 concat(NodeList *a, NodeList *b) 3210 { 3211 if(a == nil) 3212 return b; 3213 if(b == nil) 3214 return a; 3215 3216 a->end->next = b; 3217 a->end = b->end; 3218 b->end = nil; 3219 return a; 3220 } 3221 3222 NodeList* 3223 list1(Node *n) 3224 { 3225 NodeList *l; 3226 3227 if(n == nil) 3228 return nil; 3229 if(n->op == OBLOCK && n->ninit == nil) { 3230 // Flatten list and steal storage. 3231 // Poison pointer to catch errant uses. 3232 l = n->list; 3233 n->list = (NodeList*)1; 3234 return l; 3235 } 3236 l = mal(sizeof *l); 3237 l->n = n; 3238 l->end = l; 3239 return l; 3240 } 3241 3242 NodeList* 3243 list(NodeList *l, Node *n) 3244 { 3245 return concat(l, list1(n)); 3246 } 3247 3248 void 3249 listsort(NodeList** l, int(*f)(Node*, Node*)) 3250 { 3251 NodeList *l1, *l2, *le; 3252 3253 if(*l == nil || (*l)->next == nil) 3254 return; 3255 3256 l1 = *l; 3257 l2 = *l; 3258 for(;;) { 3259 l2 = l2->next; 3260 if(l2 == nil) 3261 break; 3262 l2 = l2->next; 3263 if(l2 == nil) 3264 break; 3265 l1 = l1->next; 3266 } 3267 3268 l2 = l1->next; 3269 l1->next = nil; 3270 l2->end = (*l)->end; 3271 (*l)->end = l1; 3272 3273 l1 = *l; 3274 listsort(&l1, f); 3275 listsort(&l2, f); 3276 3277 if((*f)(l1->n, l2->n) < 0) { 3278 *l = l1; 3279 } else { 3280 *l = l2; 3281 l2 = l1; 3282 l1 = *l; 3283 } 3284 3285 // now l1 == *l; and l1 < l2 3286 3287 while ((l1 != nil) && (l2 != nil)) { 3288 while ((l1->next != nil) && (*f)(l1->next->n, l2->n) < 0) 3289 l1 = l1->next; 3290 3291 // l1 is last one from l1 that is < l2 3292 le = l1->next; // le is the rest of l1, first one that is >= l2 3293 if(le != nil) 3294 le->end = (*l)->end; 3295 3296 (*l)->end = l1; // cut *l at l1 3297 *l = concat(*l, l2); // glue l2 to *l's tail 3298 3299 l1 = l2; // l1 is the first element of *l that is < the new l2 3300 l2 = le; // ... because l2 now is the old tail of l1 3301 } 3302 3303 *l = concat(*l, l2); // any remainder 3304 } 3305 3306 NodeList* 3307 listtreecopy(NodeList *l) 3308 { 3309 NodeList *out; 3310 3311 out = nil; 3312 for(; l; l=l->next) 3313 out = list(out, treecopy(l->n)); 3314 return out; 3315 } 3316 3317 Node* 3318 liststmt(NodeList *l) 3319 { 3320 Node *n; 3321 3322 n = nod(OBLOCK, N, N); 3323 n->list = l; 3324 if(l) 3325 n->lineno = l->n->lineno; 3326 return n; 3327 } 3328 3329 /* 3330 * return nelem of list 3331 */ 3332 int 3333 count(NodeList *l) 3334 { 3335 vlong n; 3336 3337 n = 0; 3338 for(; l; l=l->next) 3339 n++; 3340 if((int)n != n) { // Overflow. 3341 yyerror("too many elements in list"); 3342 } 3343 return n; 3344 } 3345 3346 /* 3347 * return nelem of list 3348 */ 3349 int 3350 structcount(Type *t) 3351 { 3352 int v; 3353 Iter s; 3354 3355 v = 0; 3356 for(t = structfirst(&s, &t); t != T; t = structnext(&s)) 3357 v++; 3358 return v; 3359 } 3360 3361 /* 3362 * return power of 2 of the constant 3363 * operand. -1 if it is not a power of 2. 3364 * 1000+ if it is a -(power of 2) 3365 */ 3366 int 3367 powtwo(Node *n) 3368 { 3369 uvlong v, b; 3370 int i; 3371 3372 if(n == N || n->op != OLITERAL || n->type == T) 3373 goto no; 3374 if(!isint[n->type->etype]) 3375 goto no; 3376 3377 v = mpgetfix(n->val.u.xval); 3378 b = 1ULL; 3379 for(i=0; i<64; i++) { 3380 if(b == v) 3381 return i; 3382 b = b<<1; 3383 } 3384 3385 if(!issigned[n->type->etype]) 3386 goto no; 3387 3388 v = -v; 3389 b = 1ULL; 3390 for(i=0; i<64; i++) { 3391 if(b == v) 3392 return i+1000; 3393 b = b<<1; 3394 } 3395 3396 no: 3397 return -1; 3398 } 3399 3400 /* 3401 * return the unsigned type for 3402 * a signed integer type. 3403 * returns T if input is not a 3404 * signed integer type. 3405 */ 3406 Type* 3407 tounsigned(Type *t) 3408 { 3409 3410 // this is types[et+1], but not sure 3411 // that this relation is immutable 3412 switch(t->etype) { 3413 default: 3414 print("tounsigned: unknown type %T\n", t); 3415 t = T; 3416 break; 3417 case TINT: 3418 t = types[TUINT]; 3419 break; 3420 case TINT8: 3421 t = types[TUINT8]; 3422 break; 3423 case TINT16: 3424 t = types[TUINT16]; 3425 break; 3426 case TINT32: 3427 t = types[TUINT32]; 3428 break; 3429 case TINT64: 3430 t = types[TUINT64]; 3431 break; 3432 } 3433 return t; 3434 } 3435 3436 /* 3437 * magic number for signed division 3438 * see hacker's delight chapter 10 3439 */ 3440 void 3441 smagic(Magic *m) 3442 { 3443 int p; 3444 uint64 ad, anc, delta, q1, r1, q2, r2, t; 3445 uint64 mask, two31; 3446 3447 m->bad = 0; 3448 switch(m->w) { 3449 default: 3450 m->bad = 1; 3451 return; 3452 case 8: 3453 mask = 0xffLL; 3454 break; 3455 case 16: 3456 mask = 0xffffLL; 3457 break; 3458 case 32: 3459 mask = 0xffffffffLL; 3460 break; 3461 case 64: 3462 mask = 0xffffffffffffffffULL; 3463 break; 3464 } 3465 two31 = mask ^ (mask>>1); 3466 3467 p = m->w-1; 3468 ad = m->sd; 3469 if(m->sd < 0) 3470 ad = -(uvlong)m->sd; 3471 3472 // bad denominators 3473 if(ad == 0 || ad == 1 || ad == two31) { 3474 m->bad = 1; 3475 return; 3476 } 3477 3478 t = two31; 3479 ad &= mask; 3480 3481 anc = t - 1 - t%ad; 3482 anc &= mask; 3483 3484 q1 = two31/anc; 3485 r1 = two31 - q1*anc; 3486 q1 &= mask; 3487 r1 &= mask; 3488 3489 q2 = two31/ad; 3490 r2 = two31 - q2*ad; 3491 q2 &= mask; 3492 r2 &= mask; 3493 3494 for(;;) { 3495 p++; 3496 q1 <<= 1; 3497 r1 <<= 1; 3498 q1 &= mask; 3499 r1 &= mask; 3500 if(r1 >= anc) { 3501 q1++; 3502 r1 -= anc; 3503 q1 &= mask; 3504 r1 &= mask; 3505 } 3506 3507 q2 <<= 1; 3508 r2 <<= 1; 3509 q2 &= mask; 3510 r2 &= mask; 3511 if(r2 >= ad) { 3512 q2++; 3513 r2 -= ad; 3514 q2 &= mask; 3515 r2 &= mask; 3516 } 3517 3518 delta = ad - r2; 3519 delta &= mask; 3520 if(q1 < delta || (q1 == delta && r1 == 0)) { 3521 continue; 3522 } 3523 break; 3524 } 3525 3526 m->sm = q2+1; 3527 if(m->sm & two31) 3528 m->sm |= ~mask; 3529 m->s = p-m->w; 3530 } 3531 3532 /* 3533 * magic number for unsigned division 3534 * see hacker's delight chapter 10 3535 */ 3536 void 3537 umagic(Magic *m) 3538 { 3539 int p; 3540 uint64 nc, delta, q1, r1, q2, r2; 3541 uint64 mask, two31; 3542 3543 m->bad = 0; 3544 m->ua = 0; 3545 3546 switch(m->w) { 3547 default: 3548 m->bad = 1; 3549 return; 3550 case 8: 3551 mask = 0xffLL; 3552 break; 3553 case 16: 3554 mask = 0xffffLL; 3555 break; 3556 case 32: 3557 mask = 0xffffffffLL; 3558 break; 3559 case 64: 3560 mask = 0xffffffffffffffffULL; 3561 break; 3562 } 3563 two31 = mask ^ (mask>>1); 3564 3565 m->ud &= mask; 3566 if(m->ud == 0 || m->ud == two31) { 3567 m->bad = 1; 3568 return; 3569 } 3570 nc = mask - (-m->ud&mask)%m->ud; 3571 p = m->w-1; 3572 3573 q1 = two31/nc; 3574 r1 = two31 - q1*nc; 3575 q1 &= mask; 3576 r1 &= mask; 3577 3578 q2 = (two31-1) / m->ud; 3579 r2 = (two31-1) - q2*m->ud; 3580 q2 &= mask; 3581 r2 &= mask; 3582 3583 for(;;) { 3584 p++; 3585 if(r1 >= nc-r1) { 3586 q1 <<= 1; 3587 q1++; 3588 r1 <<= 1; 3589 r1 -= nc; 3590 } else { 3591 q1 <<= 1; 3592 r1 <<= 1; 3593 } 3594 q1 &= mask; 3595 r1 &= mask; 3596 if(r2+1 >= m->ud-r2) { 3597 if(q2 >= two31-1) { 3598 m->ua = 1; 3599 } 3600 q2 <<= 1; 3601 q2++; 3602 r2 <<= 1; 3603 r2++; 3604 r2 -= m->ud; 3605 } else { 3606 if(q2 >= two31) { 3607 m->ua = 1; 3608 } 3609 q2 <<= 1; 3610 r2 <<= 1; 3611 r2++; 3612 } 3613 q2 &= mask; 3614 r2 &= mask; 3615 3616 delta = m->ud - 1 - r2; 3617 delta &= mask; 3618 3619 if(p < m->w+m->w) 3620 if(q1 < delta || (q1 == delta && r1 == 0)) { 3621 continue; 3622 } 3623 break; 3624 } 3625 m->um = q2+1; 3626 m->s = p-m->w; 3627 } 3628 3629 Sym* 3630 ngotype(Node *n) 3631 { 3632 if(n->type != T) 3633 return typenamesym(n->type); 3634 return S; 3635 } 3636 3637 /* 3638 * Convert raw string to the prefix that will be used in the symbol 3639 * table. All control characters, space, '%' and '"', as well as 3640 * non-7-bit clean bytes turn into %xx. The period needs escaping 3641 * only in the last segment of the path, and it makes for happier 3642 * users if we escape that as little as possible. 3643 * 3644 * If you edit this, edit ../ld/lib.c:/^pathtoprefix too. 3645 * If you edit this, edit ../../debug/goobj/read.go:/importPathToPrefix too. 3646 */ 3647 static char* 3648 pathtoprefix(char *s) 3649 { 3650 static char hex[] = "0123456789abcdef"; 3651 char *p, *r, *w, *l; 3652 int n; 3653 3654 // find first character past the last slash, if any. 3655 l = s; 3656 for(r=s; *r; r++) 3657 if(*r == '/') 3658 l = r+1; 3659 3660 // check for chars that need escaping 3661 n = 0; 3662 for(r=s; *r; r++) 3663 if(*r <= ' ' || (*r == '.' && r >= l) || *r == '%' || *r == '"' || *r >= 0x7f) 3664 n++; 3665 3666 // quick exit 3667 if(n == 0) 3668 return s; 3669 3670 // escape 3671 p = mal((r-s)+1+2*n); 3672 for(r=s, w=p; *r; r++) { 3673 if(*r <= ' ' || (*r == '.' && r >= l) || *r == '%' || *r == '"' || *r >= 0x7f) { 3674 *w++ = '%'; 3675 *w++ = hex[(*r>>4)&0xF]; 3676 *w++ = hex[*r&0xF]; 3677 } else 3678 *w++ = *r; 3679 } 3680 *w = '\0'; 3681 return p; 3682 } 3683 3684 Pkg* 3685 mkpkg(Strlit *path) 3686 { 3687 Pkg *p; 3688 int h; 3689 3690 h = stringhash(path->s) & (nelem(phash)-1); 3691 for(p=phash[h]; p; p=p->link) 3692 if(p->path->len == path->len && memcmp(path->s, p->path->s, path->len) == 0) 3693 return p; 3694 3695 p = mal(sizeof *p); 3696 p->path = path; 3697 p->prefix = pathtoprefix(path->s); 3698 p->link = phash[h]; 3699 phash[h] = p; 3700 return p; 3701 } 3702 3703 Strlit* 3704 strlit(char *s) 3705 { 3706 Strlit *t; 3707 3708 t = mal(sizeof *t + strlen(s)); 3709 strcpy(t->s, s); 3710 t->len = strlen(s); 3711 return t; 3712 } 3713 3714 void 3715 addinit(Node **np, NodeList *init) 3716 { 3717 Node *n; 3718 3719 if(init == nil) 3720 return; 3721 3722 n = *np; 3723 switch(n->op) { 3724 case ONAME: 3725 case OLITERAL: 3726 // There may be multiple refs to this node; 3727 // introduce OCONVNOP to hold init list. 3728 n = nod(OCONVNOP, n, N); 3729 n->type = n->left->type; 3730 n->typecheck = 1; 3731 *np = n; 3732 break; 3733 } 3734 n->ninit = concat(init, n->ninit); 3735 n->ullman = UINF; 3736 } 3737 3738 static char* reservedimports[] = { 3739 "go", 3740 "type", 3741 }; 3742 3743 int 3744 isbadimport(Strlit *path) 3745 { 3746 int i; 3747 char *s; 3748 Rune r; 3749 3750 if(strlen(path->s) != path->len) { 3751 yyerror("import path contains NUL"); 3752 return 1; 3753 } 3754 3755 for(i=0; i<nelem(reservedimports); i++) { 3756 if(strcmp(path->s, reservedimports[i]) == 0) { 3757 yyerror("import path \"%s\" is reserved and cannot be used", path->s); 3758 return 1; 3759 } 3760 } 3761 3762 s = path->s; 3763 while(*s) { 3764 s += chartorune(&r, s); 3765 if(r == Runeerror) { 3766 yyerror("import path contains invalid UTF-8 sequence: \"%Z\"", path); 3767 return 1; 3768 } 3769 if(r < 0x20 || r == 0x7f) { 3770 yyerror("import path contains control character: \"%Z\"", path); 3771 return 1; 3772 } 3773 if(r == '\\') { 3774 yyerror("import path contains backslash; use slash: \"%Z\"", path); 3775 return 1; 3776 } 3777 if(isspacerune(r)) { 3778 yyerror("import path contains space character: \"%Z\"", path); 3779 return 1; 3780 } 3781 if(utfrune("!\"#$%&'()*,:;<=>?[]^`{|}", r)) { 3782 yyerror("import path contains invalid character '%C': \"%Z\"", r, path); 3783 return 1; 3784 } 3785 } 3786 return 0; 3787 } 3788 3789 void 3790 checknil(Node *x, NodeList **init) 3791 { 3792 Node *n; 3793 3794 if(isinter(x->type)) { 3795 x = nod(OITAB, x, N); 3796 typecheck(&x, Erv); 3797 } 3798 n = nod(OCHECKNIL, x, N); 3799 n->typecheck = 1; 3800 *init = list(*init, n); 3801 } 3802 3803 /* 3804 * Can this type be stored directly in an interface word? 3805 * Yes, if the representation is a single pointer. 3806 */ 3807 int 3808 isdirectiface(Type *t) 3809 { 3810 switch(t->etype) { 3811 case TPTR32: 3812 case TPTR64: 3813 case TCHAN: 3814 case TMAP: 3815 case TFUNC: 3816 case TUNSAFEPTR: 3817 return 1; 3818 case TARRAY: 3819 // Array of 1 direct iface type can be direct. 3820 return t->bound == 1 && isdirectiface(t->type); 3821 case TSTRUCT: 3822 // Struct with 1 field of direct iface type can be direct. 3823 return t->type != T && t->type->down == T && isdirectiface(t->type->type); 3824 } 3825 return 0; 3826 }