| | 408 | Index: src/gc/basic/ldc.mak |
|---|
| | 409 | =================================================================== |
|---|
| | 410 | --- src/gc/basic/ldc.mak (revision 0) |
|---|
| | 411 | +++ src/gc/basic/ldc.mak (revision 0) |
|---|
| | 412 | @@ -0,0 +1,100 @@ |
|---|
| | 413 | +# Makefile to build the garbage collector D library for Posix |
|---|
| | 414 | +# Designed to work with GNU make |
|---|
| | 415 | +# Targets: |
|---|
| | 416 | +# make |
|---|
| | 417 | +# Same as make all |
|---|
| | 418 | +# make lib |
|---|
| | 419 | +# Build the garbage collector library |
|---|
| | 420 | +# make doc |
|---|
| | 421 | +# Generate documentation |
|---|
| | 422 | +# make clean |
|---|
| | 423 | +# Delete unneeded files created by build process |
|---|
| | 424 | + |
|---|
| | 425 | +LIB_TARGET=libdruntime-gc-basic.a |
|---|
| | 426 | +LIB_MASK=libdruntime-gc-basic*.a |
|---|
| | 427 | + |
|---|
| | 428 | +CP=cp -f |
|---|
| | 429 | +RM=rm -f |
|---|
| | 430 | +MD=mkdir -p |
|---|
| | 431 | + |
|---|
| | 432 | +ADD_CFLAGS= |
|---|
| | 433 | +ADD_DFLAGS= |
|---|
| | 434 | + |
|---|
| | 435 | +#CFLAGS=-O $(ADD_CFLAGS) |
|---|
| | 436 | +CFLAGS=-g $(ADD_CFLAGS) |
|---|
| | 437 | + |
|---|
| | 438 | +#DFLAGS=-release -O -inline -w $(ADD_DFLAGS) |
|---|
| | 439 | +DFLAGS=-g -w -disable-invariants $(ADD_DFLAGS) |
|---|
| | 440 | + |
|---|
| | 441 | +#TFLAGS=-O -inline -w $(ADD_DFLAGS) |
|---|
| | 442 | +TFLAGS=-g -w $(ADD_DFLAGS) |
|---|
| | 443 | + |
|---|
| | 444 | +DOCFLAGS=-version=DDoc |
|---|
| | 445 | + |
|---|
| | 446 | +CC=gcc |
|---|
| | 447 | +LC=$(AR) -qsv |
|---|
| | 448 | +DC=ldc2 |
|---|
| | 449 | + |
|---|
| | 450 | +LIB_DEST=../../../lib |
|---|
| | 451 | + |
|---|
| | 452 | +.SUFFIXES: .s .S .c .cpp .d .html .o |
|---|
| | 453 | + |
|---|
| | 454 | +.s.o: |
|---|
| | 455 | + $(CC) -c $(CFLAGS) $< -o$@ |
|---|
| | 456 | + |
|---|
| | 457 | +.S.o: |
|---|
| | 458 | + $(CC) -c $(CFLAGS) $< -o$@ |
|---|
| | 459 | + |
|---|
| | 460 | +.c.o: |
|---|
| | 461 | + $(CC) -c $(CFLAGS) $< -o$@ |
|---|
| | 462 | + |
|---|
| | 463 | +.cpp.o: |
|---|
| | 464 | + g++ -c $(CFLAGS) $< -o$@ |
|---|
| | 465 | + |
|---|
| | 466 | +.d.o: |
|---|
| | 467 | + $(DC) -c $(DFLAGS) $< -of$@ |
|---|
| | 468 | + |
|---|
| | 469 | +.d.html: |
|---|
| | 470 | + $(DC) -c -o- $(DOCFLAGS) -Df$*.html $< |
|---|
| | 471 | +# $(DC) -c -o- $(DOCFLAGS) -Df$*.html dmd.ddoc $< |
|---|
| | 472 | + |
|---|
| | 473 | +targets : lib doc |
|---|
| | 474 | +all : lib doc |
|---|
| | 475 | +lib : basic.lib |
|---|
| | 476 | +doc : basic.doc |
|---|
| | 477 | + |
|---|
| | 478 | +###################################################### |
|---|
| | 479 | + |
|---|
| | 480 | +ALL_OBJS= \ |
|---|
| | 481 | + gc.o \ |
|---|
| | 482 | + gcalloc.o \ |
|---|
| | 483 | + gcbits.o \ |
|---|
| | 484 | + gcstats.o \ |
|---|
| | 485 | + gcx.o |
|---|
| | 486 | + |
|---|
| | 487 | +###################################################### |
|---|
| | 488 | + |
|---|
| | 489 | +ALL_DOCS= |
|---|
| | 490 | + |
|---|
| | 491 | +###################################################### |
|---|
| | 492 | + |
|---|
| | 493 | +basic.lib : $(LIB_TARGET) |
|---|
| | 494 | + |
|---|
| | 495 | +$(LIB_TARGET) : $(ALL_OBJS) |
|---|
| | 496 | + $(RM) $@ |
|---|
| | 497 | + $(LC) $@ $(ALL_OBJS) |
|---|
| | 498 | + |
|---|
| | 499 | +basic.doc : $(ALL_DOCS) |
|---|
| | 500 | + echo No documentation available. |
|---|
| | 501 | + |
|---|
| | 502 | +###################################################### |
|---|
| | 503 | + |
|---|
| | 504 | +clean : |
|---|
| | 505 | + find . -name "*.di" | xargs $(RM) |
|---|
| | 506 | + $(RM) $(ALL_OBJS) |
|---|
| | 507 | + $(RM) $(ALL_DOCS) |
|---|
| | 508 | + $(RM) $(LIB_MASK) |
|---|
| | 509 | + |
|---|
| | 510 | +install : |
|---|
| | 511 | + $(MD) $(LIB_DEST) |
|---|
| | 512 | + $(CP) $(LIB_MASK) $(LIB_DEST)/. |
|---|
| | 513 | Index: src/gc/stub/ldc.mak |
|---|
| | 514 | =================================================================== |
|---|
| | 515 | --- src/gc/stub/ldc.mak (revision 0) |
|---|
| | 516 | +++ src/gc/stub/ldc.mak (revision 0) |
|---|
| | 517 | @@ -0,0 +1,97 @@ |
|---|
| | 518 | +# Makefile to build the garbage collector D library for Posix |
|---|
| | 519 | +# Designed to work with GNU make |
|---|
| | 520 | +# Targets: |
|---|
| | 521 | +# make |
|---|
| | 522 | +# Same as make all |
|---|
| | 523 | +# make lib |
|---|
| | 524 | +# Build the garbage collector library |
|---|
| | 525 | +# make doc |
|---|
| | 526 | +# Generate documentation |
|---|
| | 527 | +# make clean |
|---|
| | 528 | +# Delete unneeded files created by build process |
|---|
| | 529 | + |
|---|
| | 530 | +LIB_TARGET=libdruntime-gc-stub.a |
|---|
| | 531 | +LIB_MASK=libdruntime-gc-stub*.a |
|---|
| | 532 | + |
|---|
| | 533 | +CP=cp -f |
|---|
| | 534 | +RM=rm -f |
|---|
| | 535 | +MD=mkdir -p |
|---|
| | 536 | + |
|---|
| | 537 | +ADD_CFLAGS= |
|---|
| | 538 | +ADD_DFLAGS= |
|---|
| | 539 | + |
|---|
| | 540 | +CFLAGS=-O $(ADD_CFLAGS) |
|---|
| | 541 | +#CFLAGS=-g $(ADD_CFLAGS) |
|---|
| | 542 | + |
|---|
| | 543 | +#DFLAGS=-release -O -inline -w $(ADD_DFLAGS) |
|---|
| | 544 | +DFLAGS=-g -w $(ADD_DFLAGS) |
|---|
| | 545 | + |
|---|
| | 546 | + |
|---|
| | 547 | +#TFLAGS=-O -inline -w $(ADD_DFLAGS) |
|---|
| | 548 | +TFLAGS=-g -w $(ADD_DFLAGS) |
|---|
| | 549 | + |
|---|
| | 550 | +DOCFLAGS=-version=DDoc |
|---|
| | 551 | + |
|---|
| | 552 | +CC=gcc |
|---|
| | 553 | +LC=$(AR) -qsv |
|---|
| | 554 | +DC=ldc2 |
|---|
| | 555 | + |
|---|
| | 556 | +LIB_DEST=../../../lib |
|---|
| | 557 | + |
|---|
| | 558 | +.SUFFIXES: .s .S .c .cpp .d .html .o |
|---|
| | 559 | + |
|---|
| | 560 | +.s.o: |
|---|
| | 561 | + $(CC) -c $(CFLAGS) $< -o$@ |
|---|
| | 562 | + |
|---|
| | 563 | +.S.o: |
|---|
| | 564 | + $(CC) -c $(CFLAGS) $< -o$@ |
|---|
| | 565 | + |
|---|
| | 566 | +.c.o: |
|---|
| | 567 | + $(CC) -c $(CFLAGS) $< -o$@ |
|---|
| | 568 | + |
|---|
| | 569 | +.cpp.o: |
|---|
| | 570 | + g++ -c $(CFLAGS) $< -o$@ |
|---|
| | 571 | + |
|---|
| | 572 | +.d.o: |
|---|
| | 573 | + $(DC) -c $(DFLAGS) $< -of$@ |
|---|
| | 574 | + |
|---|
| | 575 | +.d.html: |
|---|
| | 576 | + $(DC) -c -o- $(DOCFLAGS) -Df$*.html $< |
|---|
| | 577 | +# $(DC) -c -o- $(DOCFLAGS) -Df$*.html dmd.ddoc $< |
|---|
| | 578 | + |
|---|
| | 579 | +targets : lib doc |
|---|
| | 580 | +all : lib doc |
|---|
| | 581 | +lib : stub.lib |
|---|
| | 582 | +doc : stub.doc |
|---|
| | 583 | + |
|---|
| | 584 | +###################################################### |
|---|
| | 585 | + |
|---|
| | 586 | +ALL_OBJS= \ |
|---|
| | 587 | + gc.o |
|---|
| | 588 | + |
|---|
| | 589 | +###################################################### |
|---|
| | 590 | + |
|---|
| | 591 | +ALL_DOCS= |
|---|
| | 592 | + |
|---|
| | 593 | +###################################################### |
|---|
| | 594 | + |
|---|
| | 595 | +stub.lib : $(LIB_TARGET) |
|---|
| | 596 | + |
|---|
| | 597 | +$(LIB_TARGET) : $(ALL_OBJS) |
|---|
| | 598 | + $(RM) $@ |
|---|
| | 599 | + $(LC) $@ $(ALL_OBJS) |
|---|
| | 600 | + |
|---|
| | 601 | +stub.doc : $(ALL_DOCS) |
|---|
| | 602 | + echo No documentation available. |
|---|
| | 603 | + |
|---|
| | 604 | +###################################################### |
|---|
| | 605 | + |
|---|
| | 606 | +clean : |
|---|
| | 607 | + find . -name "*.di" | xargs $(RM) |
|---|
| | 608 | + $(RM) $(ALL_OBJS) |
|---|
| | 609 | + $(RM) $(ALL_DOCS) |
|---|
| | 610 | + $(RM) $(LIB_MASK) |
|---|
| | 611 | + |
|---|
| | 612 | +install : |
|---|
| | 613 | + $(MD) $(LIB_DEST) |
|---|
| | 614 | + $(CP) $(LIB_MASK) $(LIB_DEST)/. |
|---|
| | 615 | Index: src/compiler/ldc/adi.d |
|---|
| | 616 | =================================================================== |
|---|
| | 617 | --- src/compiler/ldc/adi.d (revision 0) |
|---|
| | 618 | +++ src/compiler/ldc/adi.d (revision 0) |
|---|
| | 619 | @@ -0,0 +1,602 @@ |
|---|
| | 620 | +//_ adi.d |
|---|
| | 621 | + |
|---|
| | 622 | +/** |
|---|
| | 623 | + * Part of the D programming language runtime library. |
|---|
| | 624 | + * Dynamic array property support routines |
|---|
| | 625 | + */ |
|---|
| | 626 | + |
|---|
| | 627 | +/* |
|---|
| | 628 | + * Copyright (C) 2000-2006 by Digital Mars, www.digitalmars.com |
|---|
| | 629 | + * Written by Walter Bright |
|---|
| | 630 | + * |
|---|
| | 631 | + * This software is provided 'as-is', without any express or implied |
|---|
| | 632 | + * warranty. In no event will the authors be held liable for any damages |
|---|
| | 633 | + * arising from the use of this software. |
|---|
| | 634 | + * |
|---|
| | 635 | + * Permission is granted to anyone to use this software for any purpose, |
|---|
| | 636 | + * including commercial applications, and to alter it and redistribute it |
|---|
| | 637 | + * freely, in both source and binary form, subject to the following |
|---|
| | 638 | + * restrictions: |
|---|
| | 639 | + * |
|---|
| | 640 | + * o The origin of this software must not be misrepresented; you must not |
|---|
| | 641 | + * claim that you wrote the original software. If you use this software |
|---|
| | 642 | + * in a product, an acknowledgment in the product documentation would be |
|---|
| | 643 | + * appreciated but is not required. |
|---|
| | 644 | + * o Altered source versions must be plainly marked as such, and must not |
|---|
| | 645 | + * be misrepresented as being the original software. |
|---|
| | 646 | + * o This notice may not be removed or altered from any source |
|---|
| | 647 | + * distribution. |
|---|
| | 648 | + */ |
|---|
| | 649 | + |
|---|
| | 650 | +/* |
|---|
| | 651 | + * Modified by Sean Kelly <sean@f4.ca> for use with Tango. |
|---|
| | 652 | + */ |
|---|
| | 653 | + |
|---|
| | 654 | + |
|---|
| | 655 | +//debug=adi; // uncomment to turn on debugging printf's |
|---|
| | 656 | + |
|---|
| | 657 | +private |
|---|
| | 658 | +{ |
|---|
| | 659 | + version( D_Version2 ) |
|---|
| | 660 | + { |
|---|
| | 661 | + import core.stdc.stdlib; |
|---|
| | 662 | + import core.stdc.string; |
|---|
| | 663 | + } |
|---|
| | 664 | + else |
|---|
| | 665 | + { |
|---|
| | 666 | + import tango.stdc.stdlib; |
|---|
| | 667 | + import tango.stdc.string; |
|---|
| | 668 | + } |
|---|
| | 669 | + import util.utf; |
|---|
| | 670 | + |
|---|
| | 671 | + enum BlkAttr : uint |
|---|
| | 672 | + { |
|---|
| | 673 | + FINALIZE = 0b0000_0001, |
|---|
| | 674 | + NO_SCAN = 0b0000_0010, |
|---|
| | 675 | + NO_MOVE = 0b0000_0100, |
|---|
| | 676 | + ALL_BITS = 0b1111_1111 |
|---|
| | 677 | + } |
|---|
| | 678 | + |
|---|
| | 679 | + extern (C) void* gc_malloc( size_t sz, uint ba = 0 ); |
|---|
| | 680 | + extern (C) void* gc_calloc( size_t sz, uint ba = 0 ); |
|---|
| | 681 | + extern (C) void gc_free( void* p ); |
|---|
| | 682 | +} |
|---|
| | 683 | + |
|---|
| | 684 | + |
|---|
| | 685 | +/********************************************** |
|---|
| | 686 | + * Reverse array of chars. |
|---|
| | 687 | + * Handled separately because embedded multibyte encodings should not be |
|---|
| | 688 | + * reversed. |
|---|
| | 689 | + */ |
|---|
| | 690 | + |
|---|
| | 691 | +extern (C) char[] _adReverseChar(char[] a) |
|---|
| | 692 | +{ |
|---|
| | 693 | + if (a.length > 1) |
|---|
| | 694 | + { |
|---|
| | 695 | + char[6] tmp; |
|---|
| | 696 | + char[6] tmplo; |
|---|
| | 697 | + char* lo = a.ptr; |
|---|
| | 698 | + char* hi = &a[length - 1]; |
|---|
| | 699 | + |
|---|
| | 700 | + while (lo < hi) |
|---|
| | 701 | + { auto clo = *lo; |
|---|
| | 702 | + auto chi = *hi; |
|---|
| | 703 | + |
|---|
| | 704 | + debug(adi) printf("lo = %d, hi = %d\n", lo, hi); |
|---|
| | 705 | + if (clo <= 0x7F && chi <= 0x7F) |
|---|
| | 706 | + { |
|---|
| | 707 | + debug(adi) printf("\tascii\n"); |
|---|
| | 708 | + *lo = chi; |
|---|
| | 709 | + *hi = clo; |
|---|
| | 710 | + lo++; |
|---|
| | 711 | + hi--; |
|---|
| | 712 | + continue; |
|---|
| | 713 | + } |
|---|
| | 714 | + |
|---|
| | 715 | + uint stridelo = UTF8stride[clo]; |
|---|
| | 716 | + // don't barf on invalid strides, just ignore it |
|---|
| | 717 | + if (stridelo == 0xFF) |
|---|
| | 718 | + stridelo = 1; |
|---|
| | 719 | + |
|---|
| | 720 | + uint stridehi = 1; |
|---|
| | 721 | + while ((chi & 0xC0) == 0x80) |
|---|
| | 722 | + { |
|---|
| | 723 | + chi = *--hi; |
|---|
| | 724 | + stridehi++; |
|---|
| | 725 | + assert(hi >= lo); |
|---|
| | 726 | + } |
|---|
| | 727 | + if (lo == hi) |
|---|
| | 728 | + break; |
|---|
| | 729 | + |
|---|
| | 730 | + debug(adi) printf("\tstridelo = %d, stridehi = %d\n", stridelo, stridehi); |
|---|
| | 731 | + if (stridelo == stridehi) |
|---|
| | 732 | + { |
|---|
| | 733 | + |
|---|
| | 734 | + memcpy(tmp.ptr, lo, stridelo); |
|---|
| | 735 | + memcpy(lo, hi, stridelo); |
|---|
| | 736 | + memcpy(hi, tmp.ptr, stridelo); |
|---|
| | 737 | + lo += stridelo; |
|---|
| | 738 | + hi--; |
|---|
| | 739 | + continue; |
|---|
| | 740 | + } |
|---|
| | 741 | + |
|---|
| | 742 | + /* Shift the whole array. This is woefully inefficient |
|---|
| | 743 | + */ |
|---|
| | 744 | + memcpy(tmp.ptr, hi, stridehi); |
|---|
| | 745 | + memcpy(tmplo.ptr, lo, stridelo); |
|---|
| | 746 | + memmove(lo + stridehi, lo + stridelo , cast(size_t)(hi - lo) - stridelo); |
|---|
| | 747 | + memcpy(lo, tmp.ptr, stridehi); |
|---|
| | 748 | + memcpy(hi + stridehi - stridelo, tmplo.ptr, stridelo); |
|---|
| | 749 | + |
|---|
| | 750 | + lo += stridehi; |
|---|
| | 751 | + hi = hi - 1 + (stridehi - stridelo); |
|---|
| | 752 | + } |
|---|
| | 753 | + } |
|---|
| | 754 | + return a; |
|---|
| | 755 | +} |
|---|
| | 756 | + |
|---|
| | 757 | +unittest |
|---|
| | 758 | +{ |
|---|
| | 759 | + char[] a = "abcd"c; |
|---|
| | 760 | + |
|---|
| | 761 | + char[] r = a.dup.reverse; |
|---|
| | 762 | + //writefln(r); |
|---|
| | 763 | + assert(r == "dcba"); |
|---|
| | 764 | + |
|---|
| | 765 | + a = "a\u1235\u1234c"; |
|---|
| | 766 | + //writefln(a); |
|---|
| | 767 | + r = a.dup.reverse; |
|---|
| | 768 | + //writefln(r); |
|---|
| | 769 | + assert(r == "c\u1234\u1235a"); |
|---|
| | 770 | + |
|---|
| | 771 | + a = "ab\u1234c"; |
|---|
| | 772 | + //writefln(a); |
|---|
| | 773 | + r = a.dup.reverse; |
|---|
| | 774 | + //writefln(r); |
|---|
| | 775 | + assert(r == "c\u1234ba"); |
|---|
| | 776 | + |
|---|
| | 777 | + a = "\u3026\u2021\u3061\n"; |
|---|
| | 778 | + r = a.dup.reverse; |
|---|
| | 779 | + assert(r == "\n\u3061\u2021\u3026"); |
|---|
| | 780 | +} |
|---|
| | 781 | + |
|---|
| | 782 | + |
|---|
| | 783 | +/********************************************** |
|---|
| | 784 | + * Reverse array of wchars. |
|---|
| | 785 | + * Handled separately because embedded multiword encodings should not be |
|---|
| | 786 | + * reversed. |
|---|
| | 787 | + */ |
|---|
| | 788 | + |
|---|
| | 789 | +extern (C) wchar[] _adReverseWchar(wchar[] a) |
|---|
| | 790 | +{ |
|---|
| | 791 | + if (a.length > 1) |
|---|
| | 792 | + { |
|---|
| | 793 | + wchar[2] tmp; |
|---|
| | 794 | + wchar* lo = a.ptr; |
|---|
| | 795 | + wchar* hi = &a[length - 1]; |
|---|
| | 796 | + |
|---|
| | 797 | + while (lo < hi) |
|---|
| | 798 | + { auto clo = *lo; |
|---|
| | 799 | + auto chi = *hi; |
|---|
| | 800 | + |
|---|
| | 801 | + if ((clo < 0xD800 || clo > 0xDFFF) && |
|---|
| | 802 | + (chi < 0xD800 || chi > 0xDFFF)) |
|---|
| | 803 | + { |
|---|
| | 804 | + *lo = chi; |
|---|
| | 805 | + *hi = clo; |
|---|
| | 806 | + lo++; |
|---|
| | 807 | + hi--; |
|---|
| | 808 | + continue; |
|---|
| | 809 | + } |
|---|
| | 810 | + |
|---|
| | 811 | + int stridelo = 1 + (clo >= 0xD800 && clo <= 0xDBFF); |
|---|
| | 812 | + |
|---|
| | 813 | + int stridehi = 1; |
|---|
| | 814 | + if (chi >= 0xDC00 && chi <= 0xDFFF) |
|---|
| | 815 | + { |
|---|
| | 816 | + chi = *--hi; |
|---|
| | 817 | + stridehi++; |
|---|
| | 818 | + assert(hi >= lo); |
|---|
| | 819 | + } |
|---|
| | 820 | + if (lo == hi) |
|---|
| | 821 | + break; |
|---|
| | 822 | + |
|---|
| | 823 | + if (stridelo == stridehi) |
|---|
| | 824 | + { int stmp; |
|---|
| | 825 | + |
|---|
| | 826 | + assert(stridelo == 2); |
|---|
| | 827 | + assert(stmp.sizeof == 2 * (*lo).sizeof); |
|---|
| | 828 | + stmp = *cast(int*)lo; |
|---|
| | 829 | + *cast(int*)lo = *cast(int*)hi; |
|---|
| | 830 | + *cast(int*)hi = stmp; |
|---|
| | 831 | + lo += stridelo; |
|---|
| | 832 | + hi--; |
|---|
| | 833 | + continue; |
|---|
| | 834 | + } |
|---|
| | 835 | + |
|---|
| | 836 | + /* Shift the whole array. This is woefully inefficient |
|---|
| | 837 | + */ |
|---|
| | 838 | + memcpy(tmp.ptr, hi, stridehi * wchar.sizeof); |
|---|
| | 839 | + memcpy(hi + stridehi - stridelo, lo, stridelo * wchar.sizeof); |
|---|
| | 840 | + memmove(lo + stridehi, lo + stridelo , (hi - (lo + stridelo)) * wchar.sizeof); |
|---|
| | 841 | + memcpy(lo, tmp.ptr, stridehi * wchar.sizeof); |
|---|
| | 842 | + |
|---|
| | 843 | + lo += stridehi; |
|---|
| | 844 | + hi = hi - 1 + (stridehi - stridelo); |
|---|
| | 845 | + } |
|---|
| | 846 | + } |
|---|
| | 847 | + return a; |
|---|
| | 848 | +} |
|---|
| | 849 | + |
|---|
| | 850 | +unittest |
|---|
| | 851 | +{ |
|---|
| | 852 | + wchar[] a = "abcd"; |
|---|
| | 853 | + wchar[] r; |
|---|
| | 854 | + |
|---|
| | 855 | + r = a.dup.reverse; |
|---|
| | 856 | + assert(r == "dcba"); |
|---|
| | 857 | + |
|---|
| | 858 | + a = "a\U00012356\U00012346c"; |
|---|
| | 859 | + r = a.dup.reverse; |
|---|
| | 860 | + assert(r == "c\U00012346\U00012356a"); |
|---|
| | 861 | + |
|---|
| | 862 | + a = "ab\U00012345c"; |
|---|
| | 863 | + r = a.dup.reverse; |
|---|
| | 864 | + assert(r == "c\U00012345ba"); |
|---|
| | 865 | +} |
|---|
| | 866 | + |
|---|
| | 867 | + |
|---|
| | 868 | +/********************************************** |
|---|
| | 869 | + * Support for array.reverse property. |
|---|
| | 870 | + * The actual type is painted on the return value by the frontend |
|---|
| | 871 | + * Given and returned length are number of elements |
|---|
| | 872 | + */ |
|---|
| | 873 | + |
|---|
| | 874 | +extern (C) void[] _adReverse(void[] a, size_t szelem) |
|---|
| | 875 | + out (result) |
|---|
| | 876 | + { |
|---|
| | 877 | + assert(result.ptr is a.ptr); |
|---|
| | 878 | + } |
|---|
| | 879 | + body |
|---|
| | 880 | + { |
|---|
| | 881 | + if (a.length >= 2) |
|---|
| | 882 | + { |
|---|
| | 883 | + byte* tmp; |
|---|
| | 884 | + byte[16] buffer; |
|---|
| | 885 | + |
|---|
| | 886 | + void* lo = a.ptr; |
|---|
| | 887 | + void* hi = a.ptr + (a.length - 1) * szelem; |
|---|
| | 888 | + |
|---|
| | 889 | + tmp = buffer.ptr; |
|---|
| | 890 | + if (szelem > 16) |
|---|
| | 891 | + { |
|---|
| | 892 | + //version (Win32) |
|---|
| | 893 | + //tmp = cast(byte*) alloca(szelem); |
|---|
| | 894 | + //else |
|---|
| | 895 | + tmp = cast(byte*) gc_malloc(szelem); |
|---|
| | 896 | + } |
|---|
| | 897 | + |
|---|
| | 898 | + for (; lo < hi; lo += szelem, hi -= szelem) |
|---|
| | 899 | + { |
|---|
| | 900 | + memcpy(tmp, lo, szelem); |
|---|
| | 901 | + memcpy(lo, hi, szelem); |
|---|
| | 902 | + memcpy(hi, tmp, szelem); |
|---|
| | 903 | + } |
|---|
| | 904 | + |
|---|
| | 905 | + version (Win32) |
|---|
| | 906 | + { |
|---|
| | 907 | + } |
|---|
| | 908 | + else |
|---|
| | 909 | + { |
|---|
| | 910 | + //if (szelem > 16) |
|---|
| | 911 | + // BUG: bad code is generate for delete pointer, tries |
|---|
| | 912 | + // to call delclass. |
|---|
| | 913 | + //gc_free(tmp); |
|---|
| | 914 | + } |
|---|
| | 915 | + } |
|---|
| | 916 | + return a.ptr[0 .. a.length]; |
|---|
| | 917 | + } |
|---|
| | 918 | + |
|---|
| | 919 | +unittest |
|---|
| | 920 | +{ |
|---|
| | 921 | + debug(adi) printf("array.reverse.unittest\n"); |
|---|
| | 922 | + |
|---|
| | 923 | + int[] a = new int[5]; |
|---|
| | 924 | + int[] b; |
|---|
| | 925 | + size_t i; |
|---|
| | 926 | + |
|---|
| | 927 | + for (i = 0; i < 5; i++) |
|---|
| | 928 | + a[i] = i; |
|---|
| | 929 | + b = a.reverse; |
|---|
| | 930 | + assert(b is a); |
|---|
| | 931 | + for (i = 0; i < 5; i++) |
|---|
| | 932 | + assert(a[i] == 4 - i); |
|---|
| | 933 | + |
|---|
| | 934 | + struct X20 |
|---|
| | 935 | + { // More than 16 bytes in size |
|---|
| | 936 | + int a; |
|---|
| | 937 | + int b, c, d, e; |
|---|
| | 938 | + } |
|---|
| | 939 | + |
|---|
| | 940 | + X20[] c = new X20[5]; |
|---|
| | 941 | + X20[] d; |
|---|
| | 942 | + |
|---|
| | 943 | + for (i = 0; i < 5; i++) |
|---|
| | 944 | + { c[i].a = i; |
|---|
| | 945 | + c[i].e = 10; |
|---|
| | 946 | + } |
|---|
| | 947 | + d = c.reverse; |
|---|
| | 948 | + assert(d is c); |
|---|
| | 949 | + for (i = 0; i < 5; i++) |
|---|
| | 950 | + { |
|---|
| | 951 | + assert(c[i].a == 4 - i); |
|---|
| | 952 | + assert(c[i].e == 10); |
|---|
| | 953 | + } |
|---|
| | 954 | +} |
|---|
| | 955 | + |
|---|
| | 956 | +/********************************************** |
|---|
| | 957 | + * Sort array of chars. |
|---|
| | 958 | + */ |
|---|
| | 959 | + |
|---|
| | 960 | +extern (C) char[] _adSortChar(char[] a) |
|---|
| | 961 | +{ |
|---|
| | 962 | + if (a.length > 1) |
|---|
| | 963 | + { |
|---|
| | 964 | + dstring da = toUTF32(a); |
|---|
| | 965 | + da.sort; |
|---|
| | 966 | + size_t i = 0; |
|---|
| | 967 | + foreach (dchar d; da) |
|---|
| | 968 | + { char[4] buf; |
|---|
| | 969 | + auto t = toUTF8(buf, d); |
|---|
| | 970 | + a[i .. i + t.length] = t[]; |
|---|
| | 971 | + i += t.length; |
|---|
| | 972 | + } |
|---|
| | 973 | + delete da; |
|---|
| | 974 | + } |
|---|
| | 975 | + return a; |
|---|
| | 976 | +} |
|---|
| | 977 | + |
|---|
| | 978 | +/********************************************** |
|---|
| | 979 | + * Sort array of wchars. |
|---|
| | 980 | + */ |
|---|
| | 981 | + |
|---|
| | 982 | +extern (C) wchar[] _adSortWchar(wchar[] a) |
|---|
| | 983 | +{ |
|---|
| | 984 | + if (a.length > 1) |
|---|
| | 985 | + { |
|---|
| | 986 | + dstring da = toUTF32(a); |
|---|
| | 987 | + da.sort; |
|---|
| | 988 | + size_t i = 0; |
|---|
| | 989 | + foreach (dchar d; da) |
|---|
| | 990 | + { wchar[2] buf; |
|---|
| | 991 | + auto t = toUTF16(buf, d); |
|---|
| | 992 | + a[i .. i + t.length] = t[]; |
|---|
| | 993 | + i += t.length; |
|---|
| | 994 | + } |
|---|
| | 995 | + delete da; |
|---|
| | 996 | + } |
|---|
| | 997 | + return a; |
|---|
| | 998 | +} |
|---|
| | 999 | + |
|---|
| | 1000 | +/*************************************** |
|---|
| | 1001 | + * Support for array equality test. |
|---|
| | 1002 | + * The actual type is painted on the return value by the frontend |
|---|
| | 1003 | + * Given lengths are number of elements |
|---|
| | 1004 | + */ |
|---|
| | 1005 | + |
|---|
| | 1006 | +extern (C) int _adEq(void[] a1, void[] a2, TypeInfo ti) |
|---|
| | 1007 | +{ |
|---|
| | 1008 | + debug(adi) printf("_adEq(a1.length = %d, a2.length = %d)\n", a1.length, a2.length); |
|---|
| | 1009 | + |
|---|
| | 1010 | + if (a1.length != a2.length) |
|---|
| | 1011 | + return 0; // not equal |
|---|
| | 1012 | + else if (a1.ptr == a2.ptr) |
|---|
| | 1013 | + return 1; // equal |
|---|
| | 1014 | + |
|---|
| | 1015 | + // let typeinfo decide |
|---|
| | 1016 | + return ti.equals(&a1, &a2); |
|---|
| | 1017 | +} |
|---|
| | 1018 | + |
|---|
| | 1019 | +unittest |
|---|
| | 1020 | +{ |
|---|
| | 1021 | + debug(adi) printf("array.Eq unittest\n"); |
|---|
| | 1022 | + |
|---|
| | 1023 | + char[] a = "hello"c; |
|---|
| | 1024 | + |
|---|
| | 1025 | + assert(a != "hel"); |
|---|
| | 1026 | + assert(a != "helloo"); |
|---|
| | 1027 | + assert(a != "betty"); |
|---|
| | 1028 | + assert(a == "hello"); |
|---|
| | 1029 | + assert(a != "hxxxx"); |
|---|
| | 1030 | +} |
|---|
| | 1031 | + |
|---|
| | 1032 | +/*************************************** |
|---|
| | 1033 | + * Support for array compare test. |
|---|
| | 1034 | + * The actual type is painted on the return value by the frontend |
|---|
| | 1035 | + * Given lengths are number of elements |
|---|
| | 1036 | + */ |
|---|
| | 1037 | + |
|---|
| | 1038 | +extern (C) int _adCmp(void[] a1, void[] a2, TypeInfo ti) |
|---|
| | 1039 | +{ |
|---|
| | 1040 | + debug(adi) printf("adCmp()\n"); |
|---|
| | 1041 | + |
|---|
| | 1042 | + if (a1.ptr == a2.ptr && |
|---|
| | 1043 | + a1.length == a2.length) |
|---|
| | 1044 | + return 0; |
|---|
| | 1045 | + |
|---|
| | 1046 | + auto len = a1.length; |
|---|
| | 1047 | + if (a2.length < len) |
|---|
| | 1048 | + len = a2.length; |
|---|
| | 1049 | + |
|---|
| | 1050 | + // let typeinfo decide |
|---|
| | 1051 | + return ti.compare(&a1, &a2); |
|---|
| | 1052 | +} |
|---|
| | 1053 | + |
|---|
| | 1054 | +unittest |
|---|
| | 1055 | +{ |
|---|
| | 1056 | + debug(adi) printf("array.Cmp unittest\n"); |
|---|
| | 1057 | + |
|---|
| | 1058 | + char[] a = "hello"c; |
|---|
| | 1059 | + |
|---|
| | 1060 | + assert(a > "hel"); |
|---|
| | 1061 | + assert(a >= "hel"); |
|---|
| | 1062 | + assert(a < "helloo"); |
|---|
| | 1063 | + assert(a <= "helloo"); |
|---|
| | 1064 | + assert(a > "betty"); |
|---|
| | 1065 | + assert(a >= "betty"); |
|---|
| | 1066 | + assert(a == "hello"); |
|---|
| | 1067 | + assert(a <= "hello"); |
|---|
| | 1068 | + assert(a >= "hello"); |
|---|
| | 1069 | +} |
|---|
| | 1070 | + |
|---|
| | 1071 | +/*************************************** |
|---|
| | 1072 | + * Support for array compare test. |
|---|
| | 1073 | + * The actual type is painted on the return value by the frontend |
|---|
| | 1074 | + * Given lengths are number of elements |
|---|
| | 1075 | + */ |
|---|
| | 1076 | + |
|---|
| | 1077 | +extern (C) int _adCmpChar(void[] a1, void[] a2) |
|---|
| | 1078 | +{ |
|---|
| | 1079 | + version(D_InlineAsm_X86) |
|---|
| | 1080 | + { |
|---|
| | 1081 | + //version = Asm86; |
|---|
| | 1082 | + } |
|---|
| | 1083 | + version (Asm86) |
|---|
| | 1084 | + { |
|---|
| | 1085 | + asm |
|---|
| | 1086 | + { naked ; |
|---|
| | 1087 | + |
|---|
| | 1088 | + push EDI ; |
|---|
| | 1089 | + push ESI ; |
|---|
| | 1090 | + |
|---|
| | 1091 | + mov ESI,a1+4[4+ESP] ; |
|---|
| | 1092 | + mov EDI,a2+4[4+ESP] ; |
|---|
| | 1093 | + |
|---|
| | 1094 | + mov ECX,a1[4+ESP] ; |
|---|
| | 1095 | + mov EDX,a2[4+ESP] ; |
|---|
| | 1096 | + |
|---|
| | 1097 | + cmp ECX,EDX ; |
|---|
| | 1098 | + jb GotLength ; |
|---|
| | 1099 | + |
|---|
| | 1100 | + mov ECX,EDX ; |
|---|
| | 1101 | + |
|---|
| | 1102 | +GotLength: |
|---|
| | 1103 | + cmp ECX,4 ; |
|---|
| | 1104 | + jb DoBytes ; |
|---|
| | 1105 | + |
|---|
| | 1106 | + // Do alignment if neither is dword aligned |
|---|
| | 1107 | + test ESI,3 ; |
|---|
| | 1108 | + jz Aligned ; |
|---|
| | 1109 | + |
|---|
| | 1110 | + test EDI,3 ; |
|---|
| | 1111 | + jz Aligned ; |
|---|
| | 1112 | +DoAlign: |
|---|
| | 1113 | + mov AL,[ESI] ; //align ESI to dword bounds |
|---|
| | 1114 | + mov DL,[EDI] ; |
|---|
| | 1115 | + |
|---|
| | 1116 | + cmp AL,DL ; |
|---|
| | 1117 | + jnz Unequal ; |
|---|
| | 1118 | + |
|---|
| | 1119 | + inc ESI ; |
|---|
| | 1120 | + inc EDI ; |
|---|
| | 1121 | + |
|---|
| | 1122 | + test ESI,3 ; |
|---|
| | 1123 | + |
|---|
| | 1124 | + lea ECX,[ECX-1] ; |
|---|
| | 1125 | + jnz DoAlign ; |
|---|
| | 1126 | +Aligned: |
|---|
| | 1127 | + mov EAX,ECX ; |
|---|
| | 1128 | + |
|---|
| | 1129 | + // do multiple of 4 bytes at a time |
|---|
| | 1130 | + |
|---|
| | 1131 | + shr ECX,2 ; |
|---|
| | 1132 | + jz TryOdd ; |
|---|
| | 1133 | + |
|---|
| | 1134 | + repe ; |
|---|
| | 1135 | + cmpsd ; |
|---|
| | 1136 | + |
|---|
| | 1137 | + jnz UnequalQuad ; |
|---|
| | 1138 | + |
|---|
| | 1139 | +TryOdd: |
|---|
| | 1140 | + mov ECX,EAX ; |
|---|
| | 1141 | +DoBytes: |
|---|
| | 1142 | + // if still equal and not end of string, do up to 3 bytes slightly |
|---|
| | 1143 | + // slower. |
|---|
| | 1144 | + |
|---|
| | 1145 | + and ECX,3 ; |
|---|
| | 1146 | + jz Equal ; |
|---|
| | 1147 | + |
|---|
| | 1148 | + repe ; |
|---|
| | 1149 | + cmpsb ; |
|---|
| | 1150 | + |
|---|
| | 1151 | + jnz Unequal ; |
|---|
| | 1152 | +Equal: |
|---|
| | 1153 | + mov EAX,a1[4+ESP] ; |
|---|
| | 1154 | + mov EDX,a2[4+ESP] ; |
|---|
| | 1155 | + |
|---|
| | 1156 | + sub EAX,EDX ; |
|---|
| | 1157 | + pop ESI ; |
|---|
| | 1158 | + |
|---|
| | 1159 | + pop EDI ; |
|---|
| | 1160 | + ret ; |
|---|
| | 1161 | + |
|---|
| | 1162 | +UnequalQuad: |
|---|
| | 1163 | + mov EDX,[EDI-4] ; |
|---|
| | 1164 | + mov EAX,[ESI-4] ; |
|---|
| | 1165 | + |
|---|
| | 1166 | + cmp AL,DL ; |
|---|
| | 1167 | + jnz Unequal ; |
|---|
| | 1168 | + |
|---|
| | 1169 | + cmp AH,DH ; |
|---|
| | 1170 | + jnz Unequal ; |
|---|
| | 1171 | + |
|---|
| | 1172 | + shr EAX,16 ; |
|---|
| | 1173 | + |
|---|
| | 1174 | + shr EDX,16 ; |
|---|
| | 1175 | + |
|---|
| | 1176 | + cmp AL,DL ; |
|---|
| | 1177 | + jnz Unequal ; |
|---|
| | 1178 | + |
|---|
| | 1179 | + cmp AH,DH ; |
|---|
| | 1180 | +Unequal: |
|---|
| | 1181 | + sbb EAX,EAX ; |
|---|
| | 1182 | + pop ESI ; |
|---|
| | 1183 | + |
|---|
| | 1184 | + or EAX,1 ; |
|---|
| | 1185 | + pop EDI ; |
|---|
| | 1186 | + |
|---|
| | 1187 | + ret ; |
|---|
| | 1188 | + } |
|---|
| | 1189 | + } |
|---|
| | 1190 | + else |
|---|
| | 1191 | + { |
|---|
| | 1192 | + int len; |
|---|
| | 1193 | + int c; |
|---|
| | 1194 | + |
|---|
| | 1195 | + debug(adi) printf("adCmpChar()\n"); |
|---|
| | 1196 | + len = cast(int)a1.length; |
|---|
| | 1197 | + if (a2.length < len) |
|---|
| | 1198 | + len = cast(int)a2.length; |
|---|
| | 1199 | + c = memcmp(cast(char *)a1.ptr, cast(char *)a2.ptr, len); |
|---|
| | 1200 | + if (!c) |
|---|
| | 1201 | + c = cast(int)a1.length - cast(int)a2.length; |
|---|
| | 1202 | + return c; |
|---|
| | 1203 | + } |
|---|
| | 1204 | +} |
|---|
| | 1205 | + |
|---|
| | 1206 | +unittest |
|---|
| | 1207 | +{ |
|---|
| | 1208 | + debug(adi) printf("array.CmpChar unittest\n"); |
|---|
| | 1209 | + |
|---|
| | 1210 | + char[] a = "hello"c; |
|---|
| | 1211 | + |
|---|
| | 1212 | + assert(a > "hel"); |
|---|
| | 1213 | + assert(a >= "hel"); |
|---|
| | 1214 | + assert(a < "helloo"); |
|---|
| | 1215 | + assert(a <= "helloo"); |
|---|
| | 1216 | + assert(a > "betty"); |
|---|
| | 1217 | + assert(a >= "betty"); |
|---|
| | 1218 | + assert(a == "hello"); |
|---|
| | 1219 | + assert(a <= "hello"); |
|---|
| | 1220 | + assert(a >= "hello"); |
|---|
| | 1221 | +} |
|---|
| | 1222 | Index: src/compiler/ldc/lifetime.d |
|---|
| | 1223 | =================================================================== |
|---|
| | 1224 | --- src/compiler/ldc/lifetime.d (revision 0) |
|---|
| | 1225 | +++ src/compiler/ldc/lifetime.d (revision 0) |
|---|
| | 1226 | @@ -0,0 +1,1147 @@ |
|---|
| | 1227 | +/** |
|---|
| | 1228 | + * This module contains all functions related to an object's lifetime: |
|---|
| | 1229 | + * allocation, resizing, deallocation, and finalization. |
|---|
| | 1230 | + * |
|---|
| | 1231 | + * Copyright: Copyright (C) 2004-2007 Digital Mars, www.digitalmars.com. |
|---|
| | 1232 | + * All rights reserved. |
|---|
| | 1233 | + * License: |
|---|
| | 1234 | + * This software is provided 'as-is', without any express or implied |
|---|
| | 1235 | + * warranty. In no event will the authors be held liable for any damages |
|---|
| | 1236 | + * arising from the use of this software. |
|---|
| | 1237 | + * |
|---|
| | 1238 | + * Permission is granted to anyone to use this software for any purpose, |
|---|
| | 1239 | + * including commercial applications, and to alter it and redistribute it |
|---|
| | 1240 | + * freely, in both source and binary form, subject to the following |
|---|
| | 1241 | + * restrictions: |
|---|
| | 1242 | + * |
|---|
| | 1243 | + * o The origin of this software must not be misrepresented; you must not |
|---|
| | 1244 | + * claim that you wrote the original software. If you use this software |
|---|
| | 1245 | + * in a product, an acknowledgment in the product documentation would be |
|---|
| | 1246 | + * appreciated but is not required. |
|---|
| | 1247 | + * o Altered source versions must be plainly marked as such, and must not |
|---|
| | 1248 | + * be misrepresented as being the original software. |
|---|
| | 1249 | + * o This notice may not be removed or altered from any source |
|---|
| | 1250 | + * distribution. |
|---|
| | 1251 | + * Authors: Walter Bright, Sean Kelly, Tomas Lindquist Olsen |
|---|
| | 1252 | + */ |
|---|
| | 1253 | +module lifetime; |
|---|
| | 1254 | + |
|---|
| | 1255 | +//debug=PRINTF; |
|---|
| | 1256 | +//debug=PRINTF2; |
|---|
| | 1257 | + |
|---|
| | 1258 | +private |
|---|
| | 1259 | +{ |
|---|
| | 1260 | + version( D_Version2 ) |
|---|
| | 1261 | + { |
|---|
| | 1262 | + import core.stdc.stdlib; |
|---|
| | 1263 | + import core.stdc.string; |
|---|
| | 1264 | + import core.stdc.stdarg; |
|---|
| | 1265 | + debug(PRINTF) import core.stdc.stdio; |
|---|
| | 1266 | + else debug(PRINTF2) import core.stdc.stdio; |
|---|
| | 1267 | + } |
|---|
| | 1268 | + else |
|---|
| | 1269 | + { |
|---|
| | 1270 | + import tango.stdc.stdlib; |
|---|
| | 1271 | + import tango.stdc.string; |
|---|
| | 1272 | + import tango.stdc.stdarg; |
|---|
| | 1273 | + debug(PRINTF) import tango.stdc.stdio; |
|---|
| | 1274 | + else debug(PRINTF2) import tango.stdc.stdio; |
|---|
| | 1275 | + } |
|---|
| | 1276 | +} |
|---|
| | 1277 | + |
|---|
| | 1278 | + |
|---|
| | 1279 | +private |
|---|
| | 1280 | +{ |
|---|
| | 1281 | + enum BlkAttr : uint |
|---|
| | 1282 | + { |
|---|
| | 1283 | + FINALIZE = 0b0000_0001, |
|---|
| | 1284 | + NO_SCAN = 0b0000_0010, |
|---|
| | 1285 | + NO_MOVE = 0b0000_0100, |
|---|
| | 1286 | + ALL_BITS = 0b1111_1111 |
|---|
| | 1287 | + } |
|---|
| | 1288 | + |
|---|
| | 1289 | + struct BlkInfo |
|---|
| | 1290 | + { |
|---|
| | 1291 | + void* base; |
|---|
| | 1292 | + size_t size; |
|---|
| | 1293 | + uint attr; |
|---|
| | 1294 | + } |
|---|
| | 1295 | + |
|---|
| | 1296 | + extern (C) uint gc_getAttr( void* p ); |
|---|
| | 1297 | + extern (C) uint gc_setAttr( void* p, uint a ); |
|---|
| | 1298 | + extern (C) uint gc_clrAttr( void* p, uint a ); |
|---|
| | 1299 | + |
|---|
| | 1300 | + extern (C) void* gc_malloc( size_t sz, uint ba = 0 ); |
|---|
| | 1301 | + extern (C) void* gc_calloc( size_t sz, uint ba = 0 ); |
|---|
| | 1302 | + extern (C) size_t gc_extend( void* p, size_t mx, size_t sz ); |
|---|
| | 1303 | + extern (C) void gc_free( void* p ); |
|---|
| | 1304 | + |
|---|
| | 1305 | + extern (C) void* gc_addrOf( void* p ); |
|---|
| | 1306 | + extern (C) size_t gc_sizeOf( void* p ); |
|---|
| | 1307 | + extern (C) BlkInfo gc_query( void* p ); |
|---|
| | 1308 | + |
|---|
| | 1309 | + extern (C) bool onCollectResource( Object o ); |
|---|
| | 1310 | + extern (C) void onFinalizeError( ClassInfo c, Exception e ); |
|---|
| | 1311 | + extern (C) void onOutOfMemoryError(); |
|---|
| | 1312 | + |
|---|
| | 1313 | + extern (C) void _d_monitordelete(Object h, bool det = true); |
|---|
| | 1314 | + |
|---|