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