From 3a84c8b58d7c87785bf1ae48eb41c91ecde9fc1c Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 5 Apr 2015 21:44:20 +0300 Subject: string0.c: Move from stmhal/ to lib/. --- cc3200/application.mk | 2 +- cc3200/bootmgr/bootloader.mk | 8 +- esp8266/Makefile | 2 +- lib/libc/string0.c | 205 +++++++++++++++++++++++++++++++++++++++++++ minimal/Makefile | 2 +- stmhal/Makefile | 2 +- stmhal/string0.c | 205 ------------------------------------------- teensy/Makefile | 2 +- 8 files changed, 216 insertions(+), 212 deletions(-) create mode 100644 lib/libc/string0.c delete mode 100644 stmhal/string0.c diff --git a/cc3200/application.mk b/cc3200/application.mk index 70d072e8f..58f377eae 100644 --- a/cc3200/application.mk +++ b/cc3200/application.mk @@ -139,6 +139,7 @@ APP_MAIN_SRC_C = \ serverstask.c APP_LIB_SRC_C = $(addprefix lib/,\ + libc/string0.c \ fatfs/ff.c \ mp-readline/readline.c \ ) @@ -154,7 +155,6 @@ APP_STM_SRC_C = $(addprefix stmhal/,\ printf.c \ pyexec.c \ pybstdio.c \ - string0.c \ ) OBJ = $(PY_O) $(addprefix $(BUILD)/, $(APP_FATFS_SRC_C:.c=.o) $(APP_RTOS_SRC_C:.c=.o) $(APP_FTP_SRC_C:.c=.o) $(APP_HAL_SRC_C:.c=.o) $(APP_MISC_SRC_C:.c=.o)) diff --git a/cc3200/bootmgr/bootloader.mk b/cc3200/bootmgr/bootloader.mk index c7c7b05ef..877ffa878 100644 --- a/cc3200/bootmgr/bootloader.mk +++ b/cc3200/bootmgr/bootloader.mk @@ -66,11 +66,15 @@ BOOT_PY_SRC_C = $(addprefix py/,\ BOOT_STM_SRC_C = $(addprefix stmhal/,\ printf.c \ - string0.c \ ) - + +BOOT_LIB_SRC_C = $(addprefix lib/,\ + libc/string0.c \ + ) + OBJ = $(addprefix $(BUILD)/, $(BOOT_HAL_SRC_C:.c=.o) $(BOOT_SL_SRC_C:.c=.o) $(BOOT_CC3100_SRC_C:.c=.o) $(BOOT_UTIL_SRC_C:.c=.o) $(BOOT_MISC_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(BOOT_MAIN_SRC_C:.c=.o) $(BOOT_MAIN_SRC_S:.s=.o) $(BOOT_PY_SRC_C:.c=.o) $(BOOT_STM_SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(BOOT_LIB_SRC_C:.c=.o) # Add the linker script LINKER_SCRIPT = bootmgr/bootmgr.lds diff --git a/esp8266/Makefile b/esp8266/Makefile index 0728e8a56..1909e0d76 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -52,12 +52,12 @@ SRC_C = \ STM_SRC_C = $(addprefix stmhal/,\ printf.c \ - string0.c \ pyexec.c \ pybstdio.c \ ) LIB_SRC_C = $(addprefix lib/,\ + libc/string0.c \ mp-readline/readline.c \ ) diff --git a/lib/libc/string0.c b/lib/libc/string0.c new file mode 100644 index 000000000..b73a5e9ea --- /dev/null +++ b/lib/libc/string0.c @@ -0,0 +1,205 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * + * 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. + */ + +#include +#include "std.h" + +#define likely(x) __builtin_expect((x), 1) + +void *memcpy(void *dst, const void *src, size_t n) { + if (likely(!(((uint32_t)dst) & 3) && !(((uint32_t)src) & 3))) { + // pointers aligned + uint32_t *d = dst; + const uint32_t *s = src; + + // copy words first + for (size_t i = (n >> 2); i; i--) { + *d++ = *s++; + } + + if (n & 2) { + // copy half-word + *(uint16_t*)d = *(const uint16_t*)s; + d = (uint32_t*)((uint16_t*)d + 1); + s = (const uint32_t*)((const uint16_t*)s + 1); + } + + if (n & 1) { + // copy byte + *((uint8_t*)d) = *((const uint8_t*)s); + } + } else { + // unaligned access, copy bytes + uint8_t *d = dst; + const uint8_t *s = src; + + for (; n; n--) { + *d++ = *s++; + } + } + + return dst; +} + +void *memmove(void *dest, const void *src, size_t n) { + if (src < dest && (uint8_t*)dest < (const uint8_t*)src + n) { + // need to copy backwards + uint8_t *d = (uint8_t*)dest + n - 1; + const uint8_t *s = (const uint8_t*)src + n - 1; + for (; n > 0; n--) { + *d-- = *s--; + } + return dest; + } else { + // can use normal memcpy + return memcpy(dest, src, n); + } +} + +void *memset(void *s, int c, size_t n) { + if (c == 0 && ((uint32_t)s & 3) == 0) { + // aligned store of 0 + uint32_t *s32 = s; + for (size_t i = n >> 2; i > 0; i--) { + *s32++ = 0; + } + if (n & 2) { + *((uint16_t*)s32) = 0; + s32 = (uint32_t*)((uint16_t*)s32 + 1); + } + if (n & 1) { + *((uint8_t*)s32) = 0; + } + } else { + uint8_t *s2 = s; + for (; n > 0; n--) { + *s2++ = c; + } + } + return s; +} + +int memcmp(const char *s1, const char *s2, size_t n) { + while (n--) { + char c1 = *s1++; + char c2 = *s2++; + if (c1 < c2) return -1; + else if (c1 > c2) return 1; + } + return 0; +} + +size_t strlen(const char *str) { + int len = 0; + for (const char *s = str; *s; s++) { + len += 1; + } + return len; +} + +int strcmp(const char *s1, const char *s2) { + while (*s1 && *s2) { + char c1 = *s1++; // XXX UTF8 get char, next char + char c2 = *s2++; // XXX UTF8 get char, next char + if (c1 < c2) return -1; + else if (c1 > c2) return 1; + } + if (*s2) return -1; + else if (*s1) return 1; + else return 0; +} + +int strncmp(const char *s1, const char *s2, size_t n) { + while (*s1 && *s2 && n > 0) { + char c1 = *s1++; // XXX UTF8 get char, next char + char c2 = *s2++; // XXX UTF8 get char, next char + n--; + if (c1 < c2) return -1; + else if (c1 > c2) return 1; + } + if (n == 0) return 0; + else if (*s2) return -1; + else if (*s1) return 1; + else return 0; +} + +char *strcpy(char *dest, const char *src) { + char *d = dest; + while (*src) { + *d++ = *src++; + } + *d = '\0'; + return dest; +} + +// needed because gcc optimises strcpy + strcat to this +char *stpcpy(char *dest, const char *src) { + while (*src) { + *dest++ = *src++; + } + *dest = '\0'; + return dest; +} + +char *strcat(char *dest, const char *src) { + char *d = dest; + while (*d) { + d++; + } + while (*src) { + *d++ = *src++; + } + *d = '\0'; + return dest; +} + +// Public Domain implementation of strchr from: +// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strchr_function +char *strchr(const char *s, int c) +{ + /* Scan s for the character. When this loop is finished, + s will either point to the end of the string or the + character we were looking for. */ + while (*s != '\0' && *s != (char)c) + s++; + return ((*s == c) ? (char *) s : 0); +} + + +// Public Domain implementation of strstr from: +// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strstr_function +char *strstr(const char *haystack, const char *needle) +{ + size_t needlelen; + /* Check for the null needle case. */ + if (*needle == '\0') + return (char *) haystack; + needlelen = strlen(needle); + for (; (haystack = strchr(haystack, *needle)) != 0; haystack++) + if (strncmp(haystack, needle, needlelen) == 0) + return (char *) haystack; + return 0; +} diff --git a/minimal/Makefile b/minimal/Makefile index 592911516..09adfd4fd 100644 --- a/minimal/Makefile +++ b/minimal/Makefile @@ -45,8 +45,8 @@ SRC_C = \ uart_core.c \ uart_extra.c \ stmhal/printf.c \ - stmhal/string0.c \ stmhal/pyexec.c \ + lib/libc/string0.c \ lib/mp-readline/readline.c \ SRC_S = \ diff --git a/stmhal/Makefile b/stmhal/Makefile index e9235a744..0ec0cf32b 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -62,6 +62,7 @@ endif #LIBS += $(shell $(CC) -print-libgcc-file-name) SRC_LIB = $(addprefix lib/,\ + libc/string0.c \ libm/math.c \ libm/asinfacosf.c \ libm/atanf.c \ @@ -94,7 +95,6 @@ SRC_LIB = $(addprefix lib/,\ SRC_C = \ main.c \ - string0.c \ system_stm32f4xx.c \ stm32f4xx_it.c \ usbd_conf.c \ diff --git a/stmhal/string0.c b/stmhal/string0.c deleted file mode 100644 index b73a5e9ea..000000000 --- a/stmhal/string0.c +++ /dev/null @@ -1,205 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 Damien P. George - * - * 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. - */ - -#include -#include "std.h" - -#define likely(x) __builtin_expect((x), 1) - -void *memcpy(void *dst, const void *src, size_t n) { - if (likely(!(((uint32_t)dst) & 3) && !(((uint32_t)src) & 3))) { - // pointers aligned - uint32_t *d = dst; - const uint32_t *s = src; - - // copy words first - for (size_t i = (n >> 2); i; i--) { - *d++ = *s++; - } - - if (n & 2) { - // copy half-word - *(uint16_t*)d = *(const uint16_t*)s; - d = (uint32_t*)((uint16_t*)d + 1); - s = (const uint32_t*)((const uint16_t*)s + 1); - } - - if (n & 1) { - // copy byte - *((uint8_t*)d) = *((const uint8_t*)s); - } - } else { - // unaligned access, copy bytes - uint8_t *d = dst; - const uint8_t *s = src; - - for (; n; n--) { - *d++ = *s++; - } - } - - return dst; -} - -void *memmove(void *dest, const void *src, size_t n) { - if (src < dest && (uint8_t*)dest < (const uint8_t*)src + n) { - // need to copy backwards - uint8_t *d = (uint8_t*)dest + n - 1; - const uint8_t *s = (const uint8_t*)src + n - 1; - for (; n > 0; n--) { - *d-- = *s--; - } - return dest; - } else { - // can use normal memcpy - return memcpy(dest, src, n); - } -} - -void *memset(void *s, int c, size_t n) { - if (c == 0 && ((uint32_t)s & 3) == 0) { - // aligned store of 0 - uint32_t *s32 = s; - for (size_t i = n >> 2; i > 0; i--) { - *s32++ = 0; - } - if (n & 2) { - *((uint16_t*)s32) = 0; - s32 = (uint32_t*)((uint16_t*)s32 + 1); - } - if (n & 1) { - *((uint8_t*)s32) = 0; - } - } else { - uint8_t *s2 = s; - for (; n > 0; n--) { - *s2++ = c; - } - } - return s; -} - -int memcmp(const char *s1, const char *s2, size_t n) { - while (n--) { - char c1 = *s1++; - char c2 = *s2++; - if (c1 < c2) return -1; - else if (c1 > c2) return 1; - } - return 0; -} - -size_t strlen(const char *str) { - int len = 0; - for (const char *s = str; *s; s++) { - len += 1; - } - return len; -} - -int strcmp(const char *s1, const char *s2) { - while (*s1 && *s2) { - char c1 = *s1++; // XXX UTF8 get char, next char - char c2 = *s2++; // XXX UTF8 get char, next char - if (c1 < c2) return -1; - else if (c1 > c2) return 1; - } - if (*s2) return -1; - else if (*s1) return 1; - else return 0; -} - -int strncmp(const char *s1, const char *s2, size_t n) { - while (*s1 && *s2 && n > 0) { - char c1 = *s1++; // XXX UTF8 get char, next char - char c2 = *s2++; // XXX UTF8 get char, next char - n--; - if (c1 < c2) return -1; - else if (c1 > c2) return 1; - } - if (n == 0) return 0; - else if (*s2) return -1; - else if (*s1) return 1; - else return 0; -} - -char *strcpy(char *dest, const char *src) { - char *d = dest; - while (*src) { - *d++ = *src++; - } - *d = '\0'; - return dest; -} - -// needed because gcc optimises strcpy + strcat to this -char *stpcpy(char *dest, const char *src) { - while (*src) { - *dest++ = *src++; - } - *dest = '\0'; - return dest; -} - -char *strcat(char *dest, const char *src) { - char *d = dest; - while (*d) { - d++; - } - while (*src) { - *d++ = *src++; - } - *d = '\0'; - return dest; -} - -// Public Domain implementation of strchr from: -// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strchr_function -char *strchr(const char *s, int c) -{ - /* Scan s for the character. When this loop is finished, - s will either point to the end of the string or the - character we were looking for. */ - while (*s != '\0' && *s != (char)c) - s++; - return ((*s == c) ? (char *) s : 0); -} - - -// Public Domain implementation of strstr from: -// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strstr_function -char *strstr(const char *haystack, const char *needle) -{ - size_t needlelen; - /* Check for the null needle case. */ - if (*needle == '\0') - return (char *) haystack; - needlelen = strlen(needle); - for (; (haystack = strchr(haystack, *needle)) != 0; haystack++) - if (strncmp(haystack, needle, needlelen) == 0) - return (char *) haystack; - return 0; -} diff --git a/teensy/Makefile b/teensy/Makefile index 1c896c390..4e97cbc6a 100644 --- a/teensy/Makefile +++ b/teensy/Makefile @@ -101,7 +101,6 @@ STM_SRC_C = $(addprefix stmhal/,\ printf.c \ pyexec.c \ pybstdio.c \ - string0.c \ ) STM_SRC_S = $(addprefix stmhal/,\ @@ -109,6 +108,7 @@ STM_SRC_S = $(addprefix stmhal/,\ ) LIB_SRC_C = $(addprefix lib/,\ + libc/string0.c \ mp-readline/readline.c \ ) -- cgit v1.2.3