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