Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 2084

Show
Ignore:
Timestamp:
04/19/07 20:21:06 (2 years ago)
Author:
sean
Message:

Merged in changes from DMD 1.013:
* An AA fix that was applied only to DMD because it involves the addition of new functions.
* A switch fix that was applied to both DMD and GDC because the changes should work for both.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/compiler/dmd/aaA.d

    r1617 r2084  
    3434private 
    3535{ 
     36    import tango.stdc.stdarg; 
    3637    import tango.stdc.string; 
    3738 
     
    9697{ 
    9798    BB* a; 
    98     version (X86_64) 
    99     { 
    100     } 
    101     else 
    102     { 
    103         // This is here only to retain binary compatibility with the 
    104         // old way we did AA's. Should eventually be removed. 
    105         //int reserved; 
    106     } 
    10799} 
    108100 
     
    761753    return result; 
    762754} 
     755 
     756 
     757/*********************************** 
     758 * Construct an associative array of type ti from 
     759 * length pairs of key/value pairs. 
     760 */ 
     761 
     762extern (C) 
     763BB* _d_assocarrayliteralT(TypeInfo_AssociativeArray ti, size_t length, ...) 
     764{ 
     765    auto valuesize = ti.next.tsize();           // value size 
     766    auto keyti = ti.key; 
     767    auto keysize = keyti.tsize();               // key size 
     768    BB* result; 
     769 
     770    //printf("_d_assocarrayliteralT(keysize = %d, valuesize = %d, length = %d)\n", keysize, valuesize, length); 
     771    //writefln("tivalue = %s", ti.next.classinfo.name); 
     772    if (length == 0 || valuesize == 0 || keysize == 0) 
     773    { 
     774        ; 
     775    } 
     776    else 
     777    { 
     778        va_list q; 
     779        va_start!(size_t)(q, length); 
     780 
     781        result = new BB(); 
     782        size_t i; 
     783 
     784        for (i = 0; i < prime_list.length - 1; i++) 
     785        { 
     786            if (length <= prime_list[i]) 
     787                break; 
     788        } 
     789        auto len = prime_list[i]; 
     790        result.b = new aaA*[len]; 
     791 
     792        size_t keystacksize   = (keysize   + int.sizeof - 1) & ~(int.sizeof - 1); 
     793        size_t valuestacksize = (valuesize + int.sizeof - 1) & ~(int.sizeof - 1); 
     794 
     795        size_t keytsize = aligntsize(keysize); 
     796 
     797        for (size_t j = 0; j < length; j++) 
     798        {   void* pkey = q; 
     799            q += keystacksize; 
     800            void* pvalue = q; 
     801            q += valuestacksize; 
     802            aaA* e; 
     803 
     804            auto key_hash = keyti.getHash(pkey); 
     805            //printf("hash = %d\n", key_hash); 
     806            i = key_hash % len; 
     807            auto pe = &result.b[i]; 
     808            while (1) 
     809            { 
     810                e = *pe; 
     811                if (!e) 
     812                { 
     813                    // Not found, create new elem 
     814                    //printf("create new one\n"); 
     815                    e = cast(aaA *) cast(void*) new void[aaA.sizeof + keytsize + valuesize]; 
     816                    memcpy(e + 1, pkey, keysize); 
     817                    e.hash = key_hash; 
     818                    *pe = e; 
     819                    result.nodes++; 
     820                    break; 
     821                } 
     822                if (key_hash == e.hash) 
     823                { 
     824                    auto c = keyti.compare(pkey, e + 1); 
     825                    if (c == 0) 
     826                        break; 
     827                    pe = (c < 0) ? &e.left : &e.right; 
     828                } 
     829                else 
     830                    pe = (key_hash < e.hash) ? &e.left : &e.right; 
     831            } 
     832            memcpy(cast(void *)(e + 1) + keytsize, pvalue, valuesize); 
     833        } 
     834 
     835        va_end(q); 
     836    } 
     837    return result; 
     838} 
  • trunk/lib/compiler/dmd/switch.d

    r1072 r2084  
    11/* 
    2  *  Copyright (C) 2004 by Digital Mars, www.digitalmars.com 
     2 *  Copyright (C) 2004-2007 by Digital Mars, www.digitalmars.com 
    33 *  Written by Walter Bright 
    44 * 
     
    102102body 
    103103{ 
    104     //printf("body _d_switch_string()\n"); 
     104    //printf("body _d_switch_string(%.*s)\n", ca); 
    105105    int low; 
    106106    int high; 
     
    112112    high = table.length; 
    113113 
    114 /* 
    115     // Print table 
    116     printf("ca[] = '%s'\n", (char *)ca); 
    117     for (mid = 0; mid < high; mid++) 
    118     { 
    119         pca = table[mid]; 
    120         printf("table[%d] = %d, '%s'\n", mid, pca.length, (char *)pca); 
    121     } 
    122 */ 
     114    version (none) 
     115    { 
     116        // Print table 
     117        printf("ca[] = '%s'\n", cast(char *)ca); 
     118        for (mid = 0; mid < high; mid++) 
     119        { 
     120            pca = table[mid]; 
     121            printf("table[%d] = %d, '%.*s'\n", mid, pca.length, pca); 
     122        } 
     123    } 
    123124    if (high && 
    124125        ca.length >= table[0].length && 
     
    139140            if (c == 0) 
    140141            { 
    141                 c = cast(byte)c1 - cast(byte)pca[0]; 
     142                c = cast(ubyte)c1 - cast(ubyte)pca[0]; 
    142143                if (c == 0) 
    143144                { 
  • trunk/lib/compiler/gdc/switch.d

    r1072 r2084  
    11/* 
    2  *  Copyright (C) 2004 by Digital Mars, www.digitalmars.com 
     2 *  Copyright (C) 2004-2007 by Digital Mars, www.digitalmars.com 
    33 *  Written by Walter Bright 
    44 * 
     
    4141 
    4242int _d_switch_string(char[][] table, char[] ca) 
    43     in 
    44     { 
    45         //printf("in _d_switch_string()\n"); 
    46         assert(table.length >= 0); 
    47         assert(ca.length >= 0); 
    48  
    49         // Make sure table[] is sorted correctly 
    50         int j; 
    51  
    52         for (j = 1; j < table.length; j++) 
    53         { 
    54             int len1 = table[j - 1].length; 
    55             int len2 = table[j].length; 
    56  
    57             assert(len1 <= len2); 
    58             if (len1 == len2) 
    59             { 
    60                 int ci; 
    61  
    62                 ci = memcmp(table[j - 1].ptr, table[j].ptr, len1); 
    63                 assert(ci < 0); // ci==0 means a duplicate 
    64             } 
    65         } 
    66     } 
    67     out (result) 
    68     { 
    69         int i; 
    70         int cj; 
    71  
    72         //printf("out _d_switch_string()\n"); 
    73         if (result == -1) 
    74         { 
    75             // Not found 
    76             for (i = 0; i < table.length; i++) 
    77             { 
    78                 if (table[i].length == ca.length) 
    79                 {   cj = memcmp(table[i].ptr, ca.ptr, ca.length); 
    80                     assert(cj != 0); 
    81                 } 
    82             } 
    83         } 
    84         else 
    85         { 
    86             assert(0 <= result && result < table.length); 
    87             for (i = 0; 1; i++) 
    88             { 
    89                 assert(i < table.length); 
    90                 if (table[i].length == ca.length) 
     43in 
     44
     45    //printf("in _d_switch_string()\n"); 
     46    assert(table.length >= 0); 
     47    assert(ca.length >= 0); 
     48 
     49    // Make sure table[] is sorted correctly 
     50    int j; 
     51 
     52    for (j = 1; j < table.length; j++) 
     53    { 
     54        int len1 = table[j - 1].length; 
     55        int len2 = table[j].length; 
     56 
     57        assert(len1 <= len2); 
     58        if (len1 == len2) 
     59        { 
     60            int ci; 
     61 
     62            ci = memcmp(table[j - 1].ptr, table[j].ptr, len1); 
     63            assert(ci < 0); // ci==0 means a duplicate 
     64        } 
     65    } 
     66
     67out (result) 
     68
     69    int i; 
     70    int cj; 
     71 
     72    //printf("out _d_switch_string()\n"); 
     73    if (result == -1) 
     74    { 
     75        // Not found 
     76        for (i = 0; i < table.length; i++) 
     77        { 
     78            if (table[i].length == ca.length) 
     79            {   cj = memcmp(table[i].ptr, ca.ptr, ca.length); 
     80                assert(cj != 0); 
     81            } 
     82        } 
     83    } 
     84    else 
     85    { 
     86        assert(0 <= result && result < table.length); 
     87        for (i = 0; 1; i++) 
     88        { 
     89            assert(i < table.length); 
     90            if (table[i].length == ca.length) 
     91            { 
     92                cj = memcmp(table[i].ptr, ca.ptr, ca.length); 
     93                if (cj == 0) 
    9194                { 
    92                     cj = memcmp(table[i].ptr, ca.ptr, ca.length); 
    93                     if (cj == 0) 
    94                     { 
    95                         assert(i == result); 
    96                         break; 
    97                     } 
    98                 } 
    99             } 
    100         } 
    101     } 
    102     body 
    103     { 
    104         //printf("body _d_switch_string()\n"); 
    105         int low; 
    106         int high; 
    107         int mid; 
    108         int c; 
    109         char[] pca; 
    110  
    111         low = 0; 
    112         high = table.length; 
    113  
    114     /* 
     95                    assert(i == result); 
     96                    break; 
     97                } 
     98            } 
     99        } 
     100    } 
     101
     102body 
     103
     104//printf("body _d_switch_string(%.*s)\n", ca); 
     105    int low; 
     106    int high; 
     107    int mid; 
     108    int c; 
     109    char[] pca; 
     110 
     111    low = 0; 
     112    high = table.length; 
     113 
     114    version (none) 
     115    { 
    115116        // Print table 
    116         printf("ca[] = '%s'\n", (char *)ca); 
     117        printf("ca[] = '%s'\n", cast(char *)ca); 
    117118        for (mid = 0; mid < high; mid++) 
    118119        { 
    119120            pca = table[mid]; 
    120             printf("table[%d] = %d, '%s'\n", mid, pca.length, (char *)pca); 
    121         } 
    122     */ 
    123         if (high && 
    124             ca.length >= table[0].length && 
    125             ca.length <= table[high - 1].length) 
    126         { 
    127             // Looking for 0 length string, which would only be at the beginning 
    128             if (ca.length == 0) 
    129                 return 0; 
    130  
    131             char c1 = ca[0]; 
    132  
    133             // Do binary search 
    134             while (low < high) 
    135             { 
    136                 mid = (low + high) >> 1; 
    137                 pca = table[mid]; 
    138                 c = ca.length - pca.length; 
     121            printf("table[%d] = %d, '%.*s'\n", mid, pca.length, pca); 
     122        } 
     123    } 
     124    if (high && 
     125        ca.length >= table[0].length && 
     126        ca.length <= table[high - 1].length) 
     127    { 
     128        // Looking for 0 length string, which would only be at the beginning 
     129        if (ca.length == 0) 
     130            return 0; 
     131 
     132        char c1 = ca[0]; 
     133 
     134        // Do binary search 
     135        while (low < high) 
     136        { 
     137            mid = (low + high) >> 1; 
     138            pca = table[mid]; 
     139            c = ca.length - pca.length; 
     140            if (c == 0) 
     141            { 
     142                c = cast(ubyte)c1 - cast(ubyte)pca[0]; 
    139143                if (c == 0) 
    140144                { 
    141                     c = cast(byte)c1 - cast(byte)pca[0]
     145                    c = memcmp(ca.ptr, pca.ptr, ca.length)
    142146                    if (c == 0) 
    143                     { 
    144                         c = memcmp(ca.ptr, pca.ptr, ca.length); 
    145                         if (c == 0) 
    146                         {   //printf("found %d\n", mid); 
    147                             return mid; 
    148                         } 
     147                    {   //printf("found %d\n", mid); 
     148                        return mid; 
    149149                    } 
    150150                } 
    151                 if (c < 0) 
    152                 { 
    153                     high = mid; 
    154                 } 
    155                 else 
    156                 { 
    157                     low = mid + 1; 
    158                 } 
    159             } 
    160         } 
    161  
    162         //printf("not found\n"); 
    163         return -1;              // not found 
    164     } 
     151            } 
     152            if (c < 0) 
     153            { 
     154                high = mid; 
     155            } 
     156            else 
     157            { 
     158                low = mid + 1; 
     159            } 
     160        } 
     161    } 
     162 
     163    //printf("not found\n"); 
     164    return -1;              // not found 
     165
    165166 
    166167unittest