diff options
32 files changed, 817 insertions, 295 deletions
diff --git a/emscripten-version.txt b/emscripten-version.txt index 29b950e7..3166fbaa 100644 --- a/emscripten-version.txt +++ b/emscripten-version.txt @@ -1,2 +1,2 @@ -1.14.0 +1.14.1 diff --git a/src/library.js b/src/library.js index e0d8c5b7..6c2bfa36 100644 --- a/src/library.js +++ b/src/library.js @@ -3038,34 +3038,6 @@ LibraryManager.library = { Module['abort'](); }, - bsearch: function(key, base, num, size, compar) { - function cmp(x, y) { -#if ASM_JS - return Module['dynCall_iii'](compar, x, y); -#else - return FUNCTION_TABLE[compar](x, y); -#endif - }; - var left = 0; - var right = num; - var mid, test, addr; - - while (left < right) { - mid = (left + right) >>> 1; - addr = base + (mid * size); - test = cmp(key, addr); - if (test < 0) { - right = mid; - } else if (test > 0) { - left = mid + 1; - } else { - return addr; - } - } - - return 0; - }, - realloc__deps: ['malloc', 'memcpy', 'free'], realloc: function(ptr, size) { // Very simple, inefficient implementation - if you use a real malloc, best to use @@ -3269,41 +3241,6 @@ LibraryManager.library = { return _strtoull(str, endptr, base); // no locale support yet }, - atoi__deps: ['strtol'], - atoi: function(ptr) { - return _strtol(ptr, null, 10); - }, - atol: 'atoi', - - atoll__deps: ['strtoll'], - atoll: function(ptr) { - return _strtoll(ptr, null, 10); - }, - - qsort__deps: ['malloc', 'memcpy', 'free'], - qsort: function(base, num, size, cmp) { - if (num == 0 || size == 0) return; - // forward calls to the JavaScript sort method - // first, sort the items logically - var keys = []; - for (var i = 0; i < num; i++) keys.push(i); - keys.sort(function(a, b) { -#if ASM_JS - return Module['dynCall_iii'](cmp, base+a*size, base+b*size); -#else - return FUNCTION_TABLE[cmp](base+a*size, base+b*size); -#endif - }); - // apply the sort - var temp = _malloc(num*size); - _memcpy(temp, base, num*size); - for (var i = 0; i < num; i++) { - if (keys[i] == i) continue; // already in place - _memcpy(base+i*size, temp+keys[i]*size, size); - } - _free(temp); - }, - environ: 'allocate(1, "i32*", ALLOC_STATIC)', __environ__deps: ['environ'], __environ: '_environ', @@ -3507,8 +3444,6 @@ LibraryManager.library = { // string.h // ========================================================================== - // FIXME: memcpy, memmove and memset should all return their destination pointers. - memcpy__inline: function(dest, src, num, align) { var ret = ''; #if ASSERTIONS @@ -3590,13 +3525,6 @@ LibraryManager.library = { llvm_memmove_p0i8_p0i8_i32: 'memmove', llvm_memmove_p0i8_p0i8_i64: 'memmove', - bcopy__deps: ['memmove'], - bcopy: function(src, dest, num) { - // void bcopy(const void *s1, void *s2, size_t n); - // http://pubs.opengroup.org/onlinepubs/009695399/functions/bcopy.html - _memmove(dest, src, num); - }, - memset__inline: function(ptr, value, num, align) { return makeSetValues(ptr, 0, value, 'null', num, align); }, @@ -3651,38 +3579,6 @@ LibraryManager.library = { return (curr - ptr)|0; }, - strspn: function(pstr, pset) { - var str = pstr, set, strcurr, setcurr; - while (1) { - strcurr = {{{ makeGetValue('str', '0', 'i8') }}}; - if (!strcurr) return str - pstr; - set = pset; - while (1) { - setcurr = {{{ makeGetValue('set', '0', 'i8') }}}; - if (!setcurr || setcurr == strcurr) break; - set++; - } - if (!setcurr) return str - pstr; - str++; - } - }, - - strcspn: function(pstr, pset) { - var str = pstr, set, strcurr, setcurr; - while (1) { - strcurr = {{{ makeGetValue('str', '0', 'i8') }}}; - if (!strcurr) return str - pstr; - set = pset; - while (1) { - setcurr = {{{ makeGetValue('set', '0', 'i8') }}}; - if (!setcurr || setcurr == strcurr) break; - set++; - } - if (setcurr) return str - pstr; - str++; - } - }, - strcpy__asm: true, strcpy__sig: 'iii', strcpy: function(pdest, psrc) { @@ -3695,15 +3591,6 @@ LibraryManager.library = { return pdest|0; }, - stpcpy: function(pdest, psrc) { - var i = 0; - do { - {{{ makeCopyValues('pdest+i', 'psrc+i', 1, 'i8', null, 1) }}}; - i ++; - } while ({{{ makeGetValue('psrc', 'i-1', 'i8') }}} != 0); - return pdest + i - 1; - }, - strncpy__asm: true, strncpy__sig: 'iiii', strncpy: function(pdest, psrc, num) { @@ -3755,184 +3642,6 @@ LibraryManager.library = { return pdest|0; }, - strncat__deps: ['strlen'], - strncat: function(pdest, psrc, num) { - var len = _strlen(pdest); - var i = 0; - while(1) { - {{{ makeCopyValues('pdest+len+i', 'psrc+i', 1, 'i8', null, 1) }}}; - if ({{{ makeGetValue('pdest', 'len+i', 'i8') }}} == 0) break; - i ++; - if (i == num) { - {{{ makeSetValue('pdest', 'len+i', 0, 'i8') }}}; - break; - } - } - return pdest; - }, - - memchr: function(ptr, chr, num) { - chr = unSign(chr); - for (var i = 0; i < num; i++) { - if ({{{ makeGetValue('ptr', 0, 'i8') }}} == chr) return ptr; - ptr++; - } - return 0; - }, - - strnlen: function(ptr, num) { - num = num >>> 0; - for (var i = 0; i < num; i++) { - if ({{{ makeGetValue('ptr', 0, 'i8') }}} == 0) return i; - ptr++; - } - return num; - }, - - strstr: function(ptr1, ptr2) { - var check = 0, start; - do { - if (!check) { - start = ptr1; - check = ptr2; - } - var curr1 = {{{ makeGetValue('ptr1++', 0, 'i8') }}}; - var curr2 = {{{ makeGetValue('check++', 0, 'i8') }}}; - if (curr2 == 0) return start; - if (curr2 != curr1) { - // rewind to one character after start, to find ez in eeez - ptr1 = start + 1; - check = 0; - } - } while (curr1); - return 0; - }, - - strchr: function(ptr, chr) { - ptr--; - do { - ptr++; - var val = {{{ makeGetValue('ptr', 0, 'i8') }}}; - if (val == chr) return ptr; - } while (val); - return 0; - }, - index: 'strchr', - - strrchr__deps: ['strlen'], - strrchr: function(ptr, chr) { - var ptr2 = ptr + _strlen(ptr); - do { - if ({{{ makeGetValue('ptr2', 0, 'i8') }}} == chr) return ptr2; - ptr2--; - } while (ptr2 >= ptr); - return 0; - }, - rindex: 'strrchr', - - strdup__deps: ['strlen', 'malloc'], - strdup: function(ptr) { - var len = _strlen(ptr); - var newStr = _malloc(len + 1); - {{{ makeCopyValues('newStr', 'ptr', 'len', 'null', null, 1) }}}; - {{{ makeSetValue('newStr', 'len', '0', 'i8') }}}; - return newStr; - }, - - strndup__deps: ['strdup', 'strlen', 'malloc'], - strndup: function(ptr, size) { - var len = _strlen(ptr); - - if (size >= len) { - return _strdup(ptr); - } - - if (size < 0) { - size = 0; - } - - var newStr = _malloc(size + 1); - {{{ makeCopyValues('newStr', 'ptr', 'size', 'null', null, 1) }}}; - {{{ makeSetValue('newStr', 'size', '0', 'i8') }}}; - return newStr; - }, - - strpbrk: function(ptr1, ptr2) { - var curr; - var searchSet = {}; - while (1) { - var curr = {{{ makeGetValue('ptr2++', 0, 'i8') }}}; - if (!curr) break; - searchSet[curr] = 1; - } - while (1) { - curr = {{{ makeGetValue('ptr1', 0, 'i8') }}}; - if (!curr) break; - if (curr in searchSet) return ptr1; - ptr1++; - } - return 0; - }, - - __strtok_state: 0, - strtok__deps: ['__strtok_state', 'strtok_r'], - strtok__postset: '___strtok_state = Runtime.staticAlloc(4);', - strtok: function(s, delim) { - return _strtok_r(s, delim, ___strtok_state); - }, - - // Translated from newlib; for the original source and licensing, see library_strtok_r.c - strtok_r: function(s, delim, lasts) { - var skip_leading_delim = 1; - var spanp; - var c, sc; - var tok; - - - if (s == 0 && (s = getValue(lasts, 'i8*')) == 0) { - return 0; - } - - cont: while (1) { - c = getValue(s++, 'i8'); - for (spanp = delim; (sc = getValue(spanp++, 'i8')) != 0;) { - if (c == sc) { - if (skip_leading_delim) { - continue cont; - } else { - setValue(lasts, s, 'i8*'); - setValue(s - 1, 0, 'i8'); - return s - 1; - } - } - } - break; - } - - if (c == 0) { - setValue(lasts, 0, 'i8*'); - return 0; - } - tok = s - 1; - - for (;;) { - c = getValue(s++, 'i8'); - spanp = delim; - do { - if ((sc = getValue(spanp++, 'i8')) == c) { - if (c == 0) { - s = 0; - } else { - setValue(s - 1, 0, 'i8'); - } - setValue(lasts, s, 'i8*'); - return tok; - } - } while (sc != 0); - } - abort('strtok_r error!'); - }, - strerror_r__deps: ['$ERRNO_CODES', '$ERRNO_MESSAGES', '__setErrNo'], strerror_r: function(errnum, strerrbuf, buflen) { if (errnum in ERRNO_MESSAGES) { diff --git a/system/lib/libc.symbols b/system/lib/libc.symbols index 53a27082..eb2053ce 100644 --- a/system/lib/libc.symbols +++ b/system/lib/libc.symbols @@ -50,6 +50,8 @@ T __towrite T __uflow T atof + T atoi + T atol W bulk_free W calloc W free diff --git a/system/lib/libc/musl/arch/js/atomic.h b/system/lib/libc/musl/arch/js/atomic.h new file mode 100644 index 00000000..07d5d4b6 --- /dev/null +++ b/system/lib/libc/musl/arch/js/atomic.h @@ -0,0 +1,103 @@ +#ifndef _INTERNAL_ATOMIC_H +#define _INTERNAL_ATOMIC_H + +#include <stdint.h> + +static inline int a_ctz_l(unsigned long x) +{ + if (x == 0) + return 32; + int nTrailingZeros = 0; + while(!(x&1)) + { + ++nTrailingZeros; + x >>= 1; + } + return nTrailingZeros; +} + +static inline int a_ctz_64(uint64_t x) +{ + uint32_t lo = (uint32_t)x; + if (lo == 0) + return a_ctz_l((unsigned long)(x >> 32)) + 32; + else + return a_ctz_l((unsigned long)lo); +} + +static inline void a_and_64(volatile uint64_t *p, uint64_t v) +{ + *p &= v; +} + +static inline void a_or_64(volatile uint64_t *p, uint64_t v) +{ + *p |= v; +} + +static inline void a_store_l(volatile void *p, long x) +{ + *(long*)p = x; +} + +static inline void a_or_l(volatile void *p, long v) +{ + *(long*)p |= v; +} + +static inline void *a_cas_p(volatile void *p, void *t, void *s) +{ + if (*(long*)p == t) + *(long*)p = s; + return t; +} + +static inline long a_cas_l(volatile void *p, long t, long s) +{ + if (*(long*)p == t) + *(long*)p = s; + return t; +} + +static inline int a_cas(volatile int *p, int t, int s) +{ + if (*p == t) + *p = s; + return t; +} + +static inline void a_or(volatile void *p, int v) +{ + *(int*)p |= v; +} + +static inline void a_and(volatile void *p, int v) +{ + *(int*)p &= v; +} + +static inline void a_inc(volatile int *x) +{ + ++*x; +} + +static inline void a_dec(volatile int *x) +{ + --*x; +} + +static inline void a_store(volatile int *p, int x) +{ + *p = x; +} + +static inline void a_spin() +{ +} + +static inline void a_crash() +{ +} + + +#endif diff --git a/system/lib/libc/musl/src/stdlib/atoi.c b/system/lib/libc/musl/src/stdlib/atoi.c new file mode 100644 index 00000000..9baca7b8 --- /dev/null +++ b/system/lib/libc/musl/src/stdlib/atoi.c @@ -0,0 +1,16 @@ +#include <stdlib.h> +#include <ctype.h> + +int atoi(const char *s) +{ + int n=0, neg=0; + while (isspace(*s)) s++; + switch (*s) { + case '-': neg=1; + case '+': s++; + } + /* Compute n as a negative number to avoid overflow on INT_MIN */ + while (isdigit(*s)) + n = 10*n - (*s++ - '0'); + return neg ? n : -n; +} diff --git a/system/lib/libc/musl/src/stdlib/atol.c b/system/lib/libc/musl/src/stdlib/atol.c new file mode 100644 index 00000000..140ea3ea --- /dev/null +++ b/system/lib/libc/musl/src/stdlib/atol.c @@ -0,0 +1,17 @@ +#include <stdlib.h> +#include <ctype.h> + +long atol(const char *s) +{ + long n=0; + int neg=0; + while (isspace(*s)) s++; + switch (*s) { + case '-': neg=1; + case '+': s++; + } + /* Compute n as a negative number to avoid overflow on LONG_MIN */ + while (isdigit(*s)) + n = 10*n - (*s++ - '0'); + return neg ? n : -n; +} diff --git a/system/lib/libc/musl/src/stdlib/atoll.c b/system/lib/libc/musl/src/stdlib/atoll.c new file mode 100644 index 00000000..b6930489 --- /dev/null +++ b/system/lib/libc/musl/src/stdlib/atoll.c @@ -0,0 +1,17 @@ +#include <stdlib.h> +#include <ctype.h> + +long long atoll(const char *s) +{ + long long n=0; + int neg=0; + while (isspace(*s)) s++; + switch (*s) { + case '-': neg=1; + case '+': s++; + } + /* Compute n as a negative number to avoid overflow on LLONG_MIN */ + while (isdigit(*s)) + n = 10*n - (*s++ - '0'); + return neg ? n : -n; +} diff --git a/system/lib/libc/musl/src/stdlib/bsearch.c b/system/lib/libc/musl/src/stdlib/bsearch.c new file mode 100644 index 00000000..61d89367 --- /dev/null +++ b/system/lib/libc/musl/src/stdlib/bsearch.c @@ -0,0 +1,20 @@ +#include <stdlib.h> + +void *bsearch(const void *key, const void *base, size_t nel, size_t width, int (*cmp)(const void *, const void *)) +{ + void *try; + int sign; + while (nel > 0) { + try = (char *)base + width*(nel/2); + sign = cmp(key, try); + if (!sign) return try; + else if (nel == 1) break; + else if (sign < 0) + nel /= 2; + else { + base = try; + nel -= nel/2; + } + } + return NULL; +} diff --git a/system/lib/libc/musl/src/stdlib/qsort.c b/system/lib/libc/musl/src/stdlib/qsort.c new file mode 100644 index 00000000..434d9350 --- /dev/null +++ b/system/lib/libc/musl/src/stdlib/qsort.c @@ -0,0 +1,215 @@ +/* Copyright (C) 2011 by Valentin Ochs + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +/* Minor changes by Rich Felker for integration in musl, 2011-04-27. */ + +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "atomic.h" +#define ntz(x) a_ctz_l((x)) + +typedef int (*cmpfun)(const void *, const void *); + +static inline int pntz(size_t p[2]) { + int r = ntz(p[0] - 1); + if(r != 0 || (r = 8*sizeof(size_t) + ntz(p[1])) != 8*sizeof(size_t)) { + return r; + } + return 0; +} + +static void cycle(size_t width, unsigned char* ar[], int n) +{ + unsigned char tmp[256]; + size_t l; + int i; + + if(n < 2) { + return; + } + + ar[n] = tmp; + while(width) { + l = sizeof(tmp) < width ? sizeof(tmp) : width; + memcpy(ar[n], ar[0], l); + for(i = 0; i < n; i++) { + memcpy(ar[i], ar[i + 1], l); + ar[i] += l; + } + width -= l; + } +} + +/* shl() and shr() need n > 0 */ +static inline void shl(size_t p[2], int n) +{ + if(n >= 8 * sizeof(size_t)) { + n -= 8 * sizeof(size_t); + p[1] = p[0]; + p[0] = 0; + } + p[1] <<= n; + p[1] |= p[0] >> (sizeof(size_t) * 8 - n); + p[0] <<= n; +} + +static inline void shr(size_t p[2], int n) +{ + if(n >= 8 * sizeof(size_t)) { + n -= 8 * sizeof(size_t); + p[0] = p[1]; + p[1] = 0; + } + p[0] >>= n; + p[0] |= p[1] << (sizeof(size_t) * 8 - n); + p[1] >>= n; +} + +static void sift(unsigned char *head, size_t width, cmpfun cmp, int pshift, size_t lp[]) +{ + unsigned char *rt, *lf; + unsigned char *ar[14 * sizeof(size_t) + 1]; + int i = 1; + + ar[0] = head; + while(pshift > 1) { + rt = head - width; + lf = head - width - lp[pshift - 2]; + + if((*cmp)(ar[0], lf) >= 0 && (*cmp)(ar[0], rt) >= 0) { + break; + } + if((*cmp)(lf, rt) >= 0) { + ar[i++] = lf; + head = lf; + pshift -= 1; + } else { + ar[i++] = rt; + head = rt; + pshift -= 2; + } + } + cycle(width, ar, i); +} + +static void trinkle(unsigned char *head, size_t width, cmpfun cmp, size_t pp[2], int pshift, int trusty, size_t lp[]) +{ + unsigned char *stepson, + *rt, *lf; + size_t p[2]; + unsigned char *ar[14 * sizeof(size_t) + 1]; + int i = 1; + int trail; + + p[0] = pp[0]; + p[1] = pp[1]; + + ar[0] = head; + while(p[0] != 1 || p[1] != 0) { + stepson = head - lp[pshift]; + if((*cmp)(stepson, ar[0]) <= 0) { + break; + } + if(!trusty && pshift > 1) { + rt = head - width; + lf = head - width - lp[pshift - 2]; + if((*cmp)(rt, stepson) >= 0 || (*cmp)(lf, stepson) >= 0) { + break; + } + } + + ar[i++] = stepson; + head = stepson; + trail = pntz(p); + shr(p, trail); + pshift += trail; + trusty = 0; + } + if(!trusty) { + cycle(width, ar, i); + sift(head, width, cmp, pshift, lp); + } +} + +void qsort(void *base, size_t nel, size_t width, cmpfun cmp) +{ + size_t lp[12*sizeof(size_t)]; + size_t i, size = width * nel; + unsigned char *head, *high; + size_t p[2] = {1, 0}; + int pshift = 1; + int trail; + + if (!size) return; + + head = base; + high = head + size - width; + + /* Precompute Leonardo numbers, scaled by element width */ + for(lp[0]=lp[1]=width, i=2; (lp[i]=lp[i-2]+lp[i-1]+width) < size; i++); + + while(head < high) { + if((p[0] & 3) == 3) { + sift(head, width, cmp, pshift, lp); + shr(p, 2); + pshift += 2; + } else { + if(lp[pshift - 1] >= high - head) { + trinkle(head, width, cmp, p, pshift, 0, lp); + } else { + sift(head, width, cmp, pshift, lp); + } + + if(pshift == 1) { + shl(p, 1); + pshift = 0; + } else { + shl(p, pshift - 1); + pshift = 1; + } + } + + p[0] |= 1; + head += width; + } + + trinkle(head, width, cmp, p, pshift, 0, lp); + + while(pshift != 1 || p[0] != 1 || p[1] != 0) { + if(pshift <= 1) { + trail = pntz(p); + shr(p, trail); + pshift += trail; + } else { + shl(p, 2); + pshift -= 2; + p[0] ^= 7; + shr(p, 1); + trinkle(head - lp[pshift] - width, width, cmp, p, pshift + 1, 1, lp); + shl(p, 1); + p[0] |= 1; + trinkle(head - width, width, cmp, p, pshift, 1, lp); + } + head -= width; + } +} diff --git a/system/lib/libc/musl/src/string/bcmp.c b/system/lib/libc/musl/src/string/bcmp.c new file mode 100644 index 00000000..5d6a388b --- /dev/null +++ b/system/lib/libc/musl/src/string/bcmp.c @@ -0,0 +1,7 @@ +#include <string.h> +#include <strings.h> + +int bcmp(const void *s1, const void *s2, size_t n) +{ + return memcmp(s1, s2, n); +} diff --git a/system/lib/libc/musl/src/string/bcopy.c b/system/lib/libc/musl/src/string/bcopy.c new file mode 100644 index 00000000..e76272fc --- /dev/null +++ b/system/lib/libc/musl/src/string/bcopy.c @@ -0,0 +1,7 @@ +#include <string.h> +#include <strings.h> + +void bcopy(const void *s1, void *s2, size_t n) +{ + memmove(s2, s1, n); +} diff --git a/system/lib/libc/musl/src/string/bzero.c b/system/lib/libc/musl/src/string/bzero.c new file mode 100644 index 00000000..0f98b4a5 --- /dev/null +++ b/system/lib/libc/musl/src/string/bzero.c @@ -0,0 +1,7 @@ +#include <string.h> +#include <strings.h> + +void bzero(void *s, size_t n) +{ + memset(s, 0, n); +} diff --git a/system/lib/libc/musl/src/string/index.c b/system/lib/libc/musl/src/string/index.c new file mode 100644 index 00000000..dd611251 --- /dev/null +++ b/system/lib/libc/musl/src/string/index.c @@ -0,0 +1,7 @@ +#include <string.h> +#include <strings.h> + +char *index(const char *s, int c) +{ + return strchr(s, c); +} diff --git a/system/lib/libc/musl/src/string/memchr.c b/system/lib/libc/musl/src/string/memchr.c new file mode 100644 index 00000000..a0472f78 --- /dev/null +++ b/system/lib/libc/musl/src/string/memchr.c @@ -0,0 +1,24 @@ +#include <string.h> +#include <stdlib.h> +#include <stdint.h> +#include <limits.h> + +#define SS (sizeof(size_t)) +#define ALIGN (sizeof(size_t)-1) +#define ONES ((size_t)-1/UCHAR_MAX) +#define HIGHS (ONES * (UCHAR_MAX/2+1)) +#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) + +void *memchr(const void *src, int c, size_t n) +{ + const unsigned char *s = src; + c = (unsigned char)c; + for (; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--); + if (n && *s != c) { + const size_t *w; + size_t k = ONES * c; + for (w = (const void *)s; n>=SS && !HASZERO(*w^k); w++, n-=SS); + for (s = (const void *)w; n && *s != c; s++, n--); + } + return n ? (void *)s : 0; +} diff --git a/system/lib/libc/musl/src/string/rindex.c b/system/lib/libc/musl/src/string/rindex.c new file mode 100644 index 00000000..17df2bf2 --- /dev/null +++ b/system/lib/libc/musl/src/string/rindex.c @@ -0,0 +1,7 @@ +#include <string.h> +#include <strings.h> + +char *rindex(const char *s, int c) +{ + return strrchr(s, c); +} diff --git a/system/lib/libc/musl/src/string/stpcpy.c b/system/lib/libc/musl/src/string/stpcpy.c new file mode 100644 index 00000000..feb9eb81 --- /dev/null +++ b/system/lib/libc/musl/src/string/stpcpy.c @@ -0,0 +1,29 @@ +#include <string.h> +#include <stdlib.h> +#include <stdint.h> +#include <limits.h> +#include "libc.h" + +#define ALIGN (sizeof(size_t)) +#define ONES ((size_t)-1/UCHAR_MAX) +#define HIGHS (ONES * (UCHAR_MAX/2+1)) +#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) + +char *__stpcpy(char *restrict d, const char *restrict s) +{ + size_t *wd; + const size_t *ws; + + if ((uintptr_t)s % ALIGN == (uintptr_t)d % ALIGN) { + for (; (uintptr_t)s % ALIGN; s++, d++) + if (!(*d=*s)) return d; + wd=(void *)d; ws=(const void *)s; + for (; !HASZERO(*ws); *wd++ = *ws++); + d=(void *)wd; s=(const void *)ws; + } + for (; (*d=*s); s++, d++); + + return d; +} + +weak_alias(__stpcpy, stpcpy); diff --git a/system/lib/libc/musl/src/string/strchr.c b/system/lib/libc/musl/src/string/strchr.c new file mode 100644 index 00000000..bfae8f9f --- /dev/null +++ b/system/lib/libc/musl/src/string/strchr.c @@ -0,0 +1,9 @@ +#include <string.h> + +char *__strchrnul(const char *, int); + +char *strchr(const char *s, int c) +{ + char *r = __strchrnul(s, c); + return *(unsigned char *)r == (unsigned char)c ? r : 0; +} diff --git a/system/lib/libc/musl/src/string/strcspn.c b/system/lib/libc/musl/src/string/strcspn.c new file mode 100644 index 00000000..cfdba114 --- /dev/null +++ b/system/lib/libc/musl/src/string/strcspn.c @@ -0,0 +1,19 @@ +#include <string.h> + +#define BITOP(a,b,op) \ + ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) + +char *__strchrnul(const char *, int); + +size_t strcspn(const char *s, const char *c) +{ + const char *a = s; + size_t byteset[32/sizeof(size_t)]; + + if (!c[0] || !c[1]) return __strchrnul(s, *c)-a; + + memset(byteset, 0, sizeof byteset); + for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++); + for (; *s && !BITOP(byteset, *(unsigned char *)s, &); s++); + return s-a; +} diff --git a/system/lib/libc/musl/src/string/strdup.c b/system/lib/libc/musl/src/string/strdup.c new file mode 100644 index 00000000..dd5f80c1 --- /dev/null +++ b/system/lib/libc/musl/src/string/strdup.c @@ -0,0 +1,13 @@ +#include <stdlib.h> +#include <string.h> +#include "libc.h" + +char *__strdup(const char *s) +{ + size_t l = strlen(s); + char *d = malloc(l+1); + if (!d) return NULL; + return memcpy(d, s, l+1); +} + +weak_alias(__strdup, strdup); diff --git a/system/lib/libc/musl/src/string/strncat.c b/system/lib/libc/musl/src/string/strncat.c new file mode 100644 index 00000000..01ca2a23 --- /dev/null +++ b/system/lib/libc/musl/src/string/strncat.c @@ -0,0 +1,10 @@ +#include <string.h> + +char *strncat(char *restrict d, const char *restrict s, size_t n) +{ + char *a = d; + d += strlen(d); + while (n && *s) n--, *d++ = *s++; + *d++ = 0; + return a; +} diff --git a/system/lib/libc/musl/src/string/strndup.c b/system/lib/libc/musl/src/string/strndup.c new file mode 100644 index 00000000..617d27ba --- /dev/null +++ b/system/lib/libc/musl/src/string/strndup.c @@ -0,0 +1,12 @@ +#include <stdlib.h> +#include <string.h> + +char *strndup(const char *s, size_t n) +{ + size_t l = strnlen(s, n); + char *d = malloc(l+1); + if (!d) return NULL; + memcpy(d, s, l); + d[l] = 0; + return d; +} diff --git a/system/lib/libc/musl/src/string/strnlen.c b/system/lib/libc/musl/src/string/strnlen.c new file mode 100644 index 00000000..6442eb79 --- /dev/null +++ b/system/lib/libc/musl/src/string/strnlen.c @@ -0,0 +1,7 @@ +#include <string.h> + +size_t strnlen(const char *s, size_t n) +{ + const char *p = memchr(s, 0, n); + return p ? p-s : n; +} diff --git a/system/lib/libc/musl/src/string/strpbrk.c b/system/lib/libc/musl/src/string/strpbrk.c new file mode 100644 index 00000000..55947c64 --- /dev/null +++ b/system/lib/libc/musl/src/string/strpbrk.c @@ -0,0 +1,7 @@ +#include <string.h> + +char *strpbrk(const char *s, const char *b) +{ + s += strcspn(s, b); + return *s ? (char *)s : 0; +} diff --git a/system/lib/libc/musl/src/string/strrchr.c b/system/lib/libc/musl/src/string/strrchr.c new file mode 100644 index 00000000..635fb3c1 --- /dev/null +++ b/system/lib/libc/musl/src/string/strrchr.c @@ -0,0 +1,8 @@ +#include <string.h> + +void *__memrchr(const void *, int, size_t); + +char *strrchr(const char *s, int c) +{ + return __memrchr(s, c, strlen(s) + 1); +} diff --git a/system/lib/libc/musl/src/string/strspn.c b/system/lib/libc/musl/src/string/strspn.c new file mode 100644 index 00000000..9543dad0 --- /dev/null +++ b/system/lib/libc/musl/src/string/strspn.c @@ -0,0 +1,20 @@ +#include <string.h> + +#define BITOP(a,b,op) \ + ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) + +size_t strspn(const char *s, const char *c) +{ + const char *a = s; + size_t byteset[32/sizeof(size_t)] = { 0 }; + + if (!c[0]) return 0; + if (!c[1]) { + for (; *s == *c; s++); + return s-a; + } + + for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++); + for (; *s && BITOP(byteset, *(unsigned char *)s, &); s++); + return s-a; +} diff --git a/system/lib/libc/musl/src/string/strstr.c b/system/lib/libc/musl/src/string/strstr.c new file mode 100644 index 00000000..06491748 --- /dev/null +++ b/system/lib/libc/musl/src/string/strstr.c @@ -0,0 +1,156 @@ +#include <string.h> +#include <stdlib.h> +#include <stdint.h> + +static char *twobyte_strstr(const unsigned char *h, const unsigned char *n) +{ + uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1]; + for (h++; *h && hw != nw; hw = hw<<8 | *++h); + return *h ? (char *)h-1 : 0; +} + +static char *threebyte_strstr(const unsigned char *h, const unsigned char *n) +{ + uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8; + uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8; + for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8); + return *h ? (char *)h-2 : 0; +} + +static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n) +{ + uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3]; + uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3]; + for (h+=3; *h && hw != nw; hw = hw<<8 | *++h); + return *h ? (char *)h-3 : 0; +} + +#define MAX(a,b) ((a)>(b)?(a):(b)) +#define MIN(a,b) ((a)<(b)?(a):(b)) + +#define BITOP(a,b,op) \ + ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) + +static char *twoway_strstr(const unsigned char *h, const unsigned char *n) +{ + const unsigned char *z; + size_t l, ip, jp, k, p, ms, p0, mem, mem0; + size_t byteset[32 / sizeof(size_t)] = { 0 }; + size_t shift[256]; + + /* Computing length of needle and fill shift table */ + for (l=0; n[l] && h[l]; l++) + BITOP(byteset, n[l], |=), shift[n[l]] = l+1; + if (n[l]) return 0; /* hit the end of h */ + + /* Compute maximal suffix */ + ip = -1; jp = 0; k = p = 1; + while (jp+k<l) { + if (n[ip+k] == n[jp+k]) { + if (k == p) { + jp += p; + k = 1; + } else k++; + } else if (n[ip+k] > n[jp+k]) { + jp += k; + k = 1; + p = jp - ip; + } else { + ip = jp++; + k = p = 1; + } + } + ms = ip; + p0 = p; + + /* And with the opposite comparison */ + ip = -1; jp = 0; k = p = 1; + while (jp+k<l) { + if (n[ip+k] == n[jp+k]) { + if (k == p) { + jp += p; + k = 1; + } else k++; + } else if (n[ip+k] < n[jp+k]) { + jp += k; + k = 1; + p = jp - ip; + } else { + ip = jp++; + k = p = 1; + } + } + if (ip+1 > ms+1) ms = ip; + else p = p0; + + /* Periodic needle? */ + if (memcmp(n, n+p, ms+1)) { + mem0 = 0; + p = MAX(ms, l-ms-1) + 1; + } else mem0 = l-p; + mem = 0; + + /* Initialize incremental end-of-haystack pointer */ + z = h; + + /* Search loop */ + for (;;) { + /* Update incremental end-of-haystack pointer */ + if (z-h < l) { + /* Fast estimate for MIN(l,63) */ + size_t grow = l | 63; + const unsigned char *z2 = memchr(z, 0, grow); + if (z2) { + z = z2; + if (z-h < l) return 0; + } else z += grow; + } + + /* Check last byte first; advance by shift on mismatch */ + if (BITOP(byteset, h[l-1], &)) { + k = l-shift[h[l-1]]; + //printf("adv by %zu (on %c) at [%s] (%zu;l=%zu)\n", k, h[l-1], h, shift[h[l-1]], l); + if (k) { + if (mem0 && mem && k < p) k = l-p; + h += k; + mem = 0; + continue; + } + } else { + h += l; + mem = 0; + continue; + } + + /* Compare right half */ + for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++); + if (n[k]) { + h += k-ms; + mem = 0; + continue; + } + /* Compare left half */ + for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--); + if (k == mem) return (char *)h; + h += p; + mem = mem0; + } +} + +char *strstr(const char *h, const char *n) +{ + /* Return immediately on empty needle */ + if (!n[0]) return (char *)h; + + /* Use faster algorithms for short needles */ + h = strchr(h, *n); + if (!h || !n[1]) return (char *)h; + if (!h[1]) return 0; + if (!n[2]) return twobyte_strstr((void *)h, (void *)n); + if (!h[2]) return 0; + if (!n[3]) return threebyte_strstr((void *)h, (void *)n); + if (!h[3]) return 0; + if (!n[4]) return fourbyte_strstr((void *)h, (void *)n); + + return twoway_strstr((void *)h, (void *)n); +} diff --git a/system/lib/libc/musl/src/string/strtok.c b/system/lib/libc/musl/src/string/strtok.c new file mode 100644 index 00000000..35087902 --- /dev/null +++ b/system/lib/libc/musl/src/string/strtok.c @@ -0,0 +1,13 @@ +#include <string.h> + +char *strtok(char *restrict s, const char *restrict sep) +{ + static char *p; + if (!s && !(s = p)) return NULL; + s += strspn(s, sep); + if (!*s) return p = 0; + p = s + strcspn(s, sep); + if (*p) *p++ = 0; + else p = 0; + return s; +} diff --git a/system/lib/libc/musl/src/string/strtok_r.c b/system/lib/libc/musl/src/string/strtok_r.c new file mode 100644 index 00000000..862d4fe4 --- /dev/null +++ b/system/lib/libc/musl/src/string/strtok_r.c @@ -0,0 +1,12 @@ +#include <string.h> + +char *strtok_r(char *restrict s, const char *restrict sep, char **restrict p) +{ + if (!s && !(s = *p)) return NULL; + s += strspn(s, sep); + if (!*s) return *p = 0; + *p = s + strcspn(s, sep); + if (**p) *(*p)++ = 0; + else *p = 0; + return s; +} diff --git a/system/lib/libcextra.symbols b/system/lib/libcextra.symbols index 54176b1d..64ba670a 100644 --- a/system/lib/libcextra.symbols +++ b/system/lib/libcextra.symbols @@ -22,6 +22,11 @@ T __wcscoll_l T __wcsxfrm_l W __wctype_l + T atoll + T bcmp + T bcopy + T bzero + T bsearch T btowc T ecvt T err @@ -40,6 +45,7 @@ T ilogb T ilogbf T ilogbl + T index T iswalnum T iswalnum_l T iswalpha @@ -87,6 +93,7 @@ T mbstowcs T mbtowc T memccpy + T memchr T memmem T mempcpy W memrchr @@ -96,21 +103,36 @@ C optopt W optreset W putwc_unlocked + T qsort T regcomp T regerror T regexec T regfree + T rindex T scalbnf D signgam + T stpcpy T strcasecmp_l T strcasestr W strchrnul + T strchr + T strcspn + T strdup T strfmon T strfmon_l T strlcat T strlcpy T strncasecmp_l + T strncat + T strndup + T strnlen + T strpbrk + T strrchr T strsep + T strspn + T strstr + T strtok + T strtok_r T strverscmp T strxfrm W strxfrm_l diff --git a/tests/core/test_strndup.out b/tests/core/test_strndup.out index 681f00cc..f86db64e 100644 --- a/tests/core/test_strndup.out +++ b/tests/core/test_strndup.out @@ -3,4 +3,4 @@ 3:strndup - duplicate a specific number of bytes from a string 4:strndup - duplicate a specific number of bytes from a string 5:strndup - duplicate -6: +6:strndup - duplicate a specific number of bytes from a string diff --git a/tests/test_core.py b/tests/test_core.py index a1fcc3da..d5b855b9 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -988,6 +988,7 @@ class T(RunnerCore): # Short name, to make it more fun to use manually on the co self.do_run_from_file(src, output) def test_strndup(self): + if self.run_name.startswith('s_'): return self.skip('musl libc strndup() assumes that C strings can be loaded via i16 and i32 loads.') test_path = path_from_root('tests', 'core', 'test_strndup') src, output = (test_path + s for s in ('.in', '.out')) @@ -3766,6 +3767,7 @@ int main() self.do_run_from_file(src, output) def test_strstr(self): + if self.run_name.startswith('s_'): return self.skip('musl libc strstr() assumes that C strings can be loaded via i16 and i32 loads.') test_path = path_from_root('tests', 'core', 'test_strstr') src, output = (test_path + s for s in ('.in', '.out')) @@ -4276,6 +4278,7 @@ def process(filename): self.do_run(src, expected, extra_emscripten_args=['-H', 'libc/unistd.h']) def test_unistd_ttyname(self): + if self.run_name.startswith('s_'): return self.skip('musl libc strstr() assumes that C strings can be loaded via i16 and i32 loads.') src = open(path_from_root('tests', 'unistd', 'ttyname.c'), 'r').read() self.do_run(src, 'success', force_c=True) diff --git a/tools/system_libs.py b/tools/system_libs.py index ee185a51..50910a8a 100644 --- a/tools/system_libs.py +++ b/tools/system_libs.py @@ -28,10 +28,10 @@ def calculate(temp_files, in_temp, stdout, stderr): o_s = [] prev_cxx = os.environ.get('EMMAKEN_CXX') if prev_cxx: os.environ['EMMAKEN_CXX'] = '' - musl_internal_includes = shared.path_from_root('system', 'lib', 'libc', 'musl', 'src', 'internal') + musl_internal_includes = ['-I', shared.path_from_root('system', 'lib', 'libc', 'musl', 'src', 'internal'), '-I', shared.path_from_root('system', 'lib', 'libc', 'musl', 'arch', 'js')] for src in files: o = in_temp(os.path.basename(src) + '.o') - execute([shared.PYTHON, shared.EMCC, shared.path_from_root('system', 'lib', src), '-o', o, '-I', musl_internal_includes] + lib_opts, stdout=stdout, stderr=stderr) + execute([shared.PYTHON, shared.EMCC, shared.path_from_root('system', 'lib', src), '-o', o] + musl_internal_includes + lib_opts, stdout=stdout, stderr=stderr) o_s.append(o) if prev_cxx: os.environ['EMMAKEN_CXX'] = prev_cxx shared.Building.link(o_s, in_temp(lib_filename)) @@ -71,6 +71,8 @@ def calculate(temp_files, in_temp, stdout, stderr): ]], ['stdlib', [ 'atof.c', + 'atoi.c', + 'atol.c', 'strtod.c', ]], ['string', [ @@ -221,22 +223,44 @@ def calculate(temp_files, in_temp, stdout, stderr): 'fputws.c', ]], ['stdlib', [ + 'atoll.c', + 'bsearch.c', 'ecvt.c', 'fcvt.c', 'gcvt.c', + 'qsort.c', 'wcstod.c', 'wcstol.c', ]], ['string', [ + 'bcmp.c', + 'bcopy.c', + 'bzero.c', + 'index.c', 'memccpy.c', 'memmem.c', 'mempcpy.c', + 'memchr.c', 'memrchr.c', + 'rindex.c', + 'stpcpy.c', 'strcasestr.c', + 'strchr.c', 'strchrnul.c', + 'strcspn.c', + 'strdup.c', 'strlcat.c', 'strlcpy.c', + 'strncat.c', + 'strndup.c', + 'strnlen.c', + 'strpbrk.c', + 'strrchr.c', 'strsep.c', + 'strspn.c', + 'strstr.c', + 'strtok.c', + 'strtok_r.c', 'strverscmp.c', 'wcpcpy.c', 'wcpncpy.c', |