summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/compiler.h5
-rw-r--r--include/endian.h19
-rw-r--r--include/hexdump.h2
-rw-r--r--include/str.h118
-rw-r--r--include/system/alloca.h6
-rw-r--r--include/system/builtins_begin_.h94
-rw-r--r--include/system/builtins_end_.h28
-rw-r--r--include/system/ctype.h73
-rw-r--r--include/system/stdlib.h12
-rw-r--r--include/system/string.h51
-rw-r--r--include/system/strings.h9
11 files changed, 296 insertions, 121 deletions
diff --git a/include/compiler.h b/include/compiler.h
index 3cabd09c..40358a11 100644
--- a/include/compiler.h
+++ b/include/compiler.h
@@ -179,5 +179,10 @@
#define MIN(a, b) ({(a) < (b) ? (a) : (b);})
#define MAX(a, b) ({(a) <= (b) ? (b) : (a);})
+/**
+ * Builtins that don't go in string.h
+ */
+#define unreachable() __builtin_unreachable()
+
#endif /* !COMPILER_H_ */
// vim:fenc=utf-8:tw=75:et
diff --git a/include/endian.h b/include/endian.h
new file mode 100644
index 00000000..267fbf00
--- /dev/null
+++ b/include/endian.h
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+/*
+ * endian.h - bswap decls that can't go in compiler.h
+ * Copyright Peter Jones <pjones@redhat.com>
+ */
+
+#ifndef ENDIAN_H_
+#define ENDIAN_H_
+
+#include <stdint.h>
+
+#include "system/builtins_begin_.h"
+mkbi1_(uint16_t, bswap16, uint16_t, x)
+mkbi1_(uint32_t, bswap32, uint32_t, x)
+mkbi1_(uint64_t, bswap64, uint64_t, x)
+#include "system/builtins_end_.h"
+
+#endif /* !ENDIAN_H_ */
+// vim:fenc=utf-8:tw=75:noet
diff --git a/include/hexdump.h b/include/hexdump.h
index 36d77ec4..a6aa2bfa 100644
--- a/include/hexdump.h
+++ b/include/hexdump.h
@@ -48,8 +48,6 @@ prepare_hex(const void *data, size_t size, char *buf, unsigned int position)
return ret;
}
-#define isprint(c) ((c) >= 0x20 && (c) <= 0x7e)
-
static inline void UNUSED
prepare_text(const void *data, size_t size, char *buf, unsigned int position)
{
diff --git a/include/str.h b/include/str.h
index c4d12113..189aceff 100644
--- a/include/str.h
+++ b/include/str.h
@@ -9,122 +9,8 @@
#pragma GCC diagnostic ignored "-Wnonnull-compare"
#endif
-static inline UNUSED NONNULL(1) unsigned long
-strnlena(const CHAR8 *s, unsigned long n)
-{
- unsigned long i;
- for (i = 0; i < n; i++)
- if (s[i] == '\0')
- break;
- return i;
-}
-
-static inline UNUSED RETURNS_NONNULL NONNULL(1, 2) CHAR8 *
-strncpya(CHAR8 *dest, const CHAR8 *src, unsigned long n)
-{
- unsigned long i;
-
- for (i = 0; i < n && src[i] != '\0'; i++)
- dest[i] = src[i];
- for (; i < n; i++)
- dest[i] = '\0';
-
- return dest;
-}
-
-static inline UNUSED RETURNS_NONNULL NONNULL(1, 2) CHAR8 *
-strcata(CHAR8 *dest, const CHAR8 *src)
-{
- unsigned long dest_len = strlena(dest);
- unsigned long i;
-
- for (i = 0; src[i] != '\0'; i++)
- dest[dest_len + i] = src[i];
- dest[dest_len + i] = '\0';
-
- return dest;
-}
-
-static inline UNUSED NONNULL(1) CHAR8 *
-strdup(const CHAR8 * const src)
-{
- UINTN len;
- CHAR8 *news = NULL;
-
- len = strlena(src);
- news = AllocateZeroPool(len + 1);
- if (news)
- strncpya(news, src, len);
- return news;
-}
-
-static inline UNUSED NONNULL(1) CHAR8 *
-strndupa(const CHAR8 * const src, const UINTN srcmax)
-{
- UINTN len;
- CHAR8 *news = NULL;
-
- len = strnlena(src, srcmax);
- news = AllocateZeroPool(len + 1);
- if (news)
- strncpya(news, src, len);
- return news;
-}
-
-static inline UNUSED RETURNS_NONNULL NONNULL(1, 2) char *
-stpcpy(char *dest, const char * const src)
-{
- size_t i = 0;
- for (i = 0; src[i]; i++)
- dest[i] = src[i];
- dest[i] = '\000';
- return &dest[i];
-}
-
-static inline UNUSED CHAR8 *
-translate_slashes(CHAR8 *out, const char *str)
-{
- int i;
- int j;
- if (str == NULL || out == NULL)
- return NULL;
-
- for (i = 0, j = 0; str[i] != '\0'; i++, j++) {
- if (str[i] == '\\') {
- out[j] = '/';
- if (str[i+1] == '\\')
- i++;
- } else
- out[j] = str[i];
- }
- out[j] = '\0';
- return out;
-}
-
-static inline UNUSED RETURNS_NONNULL NONNULL(1) CHAR8 *
-strchrnula(const CHAR8 *s, int c)
-{
- unsigned int i;
-
- for (i = 0; s[i] != '\000' && s[i] != c; i++)
- ;
-
- return (CHAR8 *)&s[i];
-}
-
-static inline UNUSED NONNULL(1) CHAR8 *
-strchra(const CHAR8 *s, int c)
-{
- const CHAR8 *s1;
-
- s1 = strchrnula(s, c);
- if (!s1 || s1[0] == '\000')
- return NULL;
-
- return (CHAR8 *)s1;
-}
-
-static inline UNUSED RETURNS_NONNULL NONNULL(1) char *
+static inline UNUSED RETURNS_NONNULL NONNULL(1)
+char *
strnchrnul(const char *s, size_t max, int c)
{
unsigned int i;
diff --git a/include/system/alloca.h b/include/system/alloca.h
index dc11b60d..a9d1aab1 100644
--- a/include/system/alloca.h
+++ b/include/system/alloca.h
@@ -5,6 +5,12 @@
#ifndef _ALLOCA_H
#define _ALLOCA_H
+#include <builtins_begin_.h>
+mkbi1_(void *, alloca, size_t, size)
+#define alloca_with_align(size, alignment) __builtin_alloca_with_align(size, alignment)
+#define alloca_with_align_and_max(size, alignment, max) __builtin_alloca_with_align_and_max(size, alignment, max)
+#include <builtins_end_.h>
+
#endif /* !_ALLOCA_H */
#endif
// vim:fenc=utf-8:tw=75:noet
diff --git a/include/system/builtins_begin_.h b/include/system/builtins_begin_.h
new file mode 100644
index 00000000..92ea5e3a
--- /dev/null
+++ b/include/system/builtins_begin_.h
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+/**
+ * macros to build builtin wrappers
+ */
+#ifndef mkbi_cat_
+#define mkbi_cat_(a, b) a##b
+#endif
+#ifdef SHIM_STRING_C_
+
+#ifndef mkbi1_
+#define mkbi1_(rtype, x, typea, a) __typeof__(mkbi_cat_(__builtin_, x)) x;
+#endif
+#ifndef mkbi2_
+#define mkbi2_(rtype, x, typea, a, typeb, b) __typeof__(mkbi_cat_(__builtin_, x)) x;
+#endif
+
+#ifndef mkbi3_
+#define mkbi3_(rtype, x, typea, a, typeb, b, typec, c) __typeof__(mkbi_cat_(__builtin_, x)) x;
+#endif
+
+#ifndef mkdepbi1_
+#define mkdepbi1_(rtype, x, typea, a) __typeof__(mkbi_cat_(__builtin_, x)) x;
+#endif
+
+#ifndef mkdepbi2_
+#define mkdepbi2_(rtype, x, typea, a, typeb, b) __typeof__(mkbi_cat_(__builtin_, x)) x;
+#endif
+
+#else /* ! SHIM_STRING_C_ */
+
+#ifndef mkbi1_
+#define mkbi1_(rtype, x, typea, a) \
+ static inline __attribute__((__unused__)) \
+ rtype \
+ x(typea a) \
+ { \
+ return mkbi_cat_(__builtin_, x)(a); \
+ }
+#endif
+
+#ifndef mkbi2_
+#define mkbi2_(rtype, x, typea, a, typeb, b) \
+ static inline __attribute__((__unused__)) \
+ rtype \
+ x(typea a, typeb b) \
+ { \
+ return mkbi_cat_(__builtin_, x)(a, b); \
+ }
+#endif
+
+#ifndef mkbi3_
+#define mkbi3_(rtype, x, typea, a, typeb, b, typec, c) \
+ static inline __attribute__((__unused__)) \
+ rtype \
+ x(typea a, typeb b, typec c) \
+ { \
+ return mkbi_cat_(__builtin_, x)(a, b,c); \
+ }
+#endif
+
+#ifdef SHIM_DEPRECATE_STRLEN
+#ifndef mkdepbi_dep_
+#define mkdepbi_dep_ __attribute__((__deprecated__))
+#endif
+#else /* !SHIM_DEPRECATE_STRLEN */
+#ifndef mkdepbi_dep_
+#define mkdepbi_dep_
+#endif
+#endif /* SHIM_DEPRECATE_STRLEN */
+
+#ifndef mkdepbi1_
+#define mkdepbi1_(rtype, x, typea, a) \
+ static inline __attribute__((__unused__)) \
+ mkdepbi_dep_ \
+ rtype \
+ x(typea a) \
+ { \
+ return mkbi_cat_(__builtin_, x)(a); \
+ }
+#endif
+
+#ifndef mkdepbi2_
+#define mkdepbi2_(rtype, x, typea, a, typeb, b) \
+ static inline __attribute__((__unused__)) \
+ mkdepbi_dep_ \
+ rtype \
+ x(typea a, typeb b) \
+ { \
+ return mkbi_cat_(__builtin_, x)(a, b); \
+ }
+#endif
+#endif /* SHIM_STRING_C_ */
+
+// vim:fenc=utf-8:tw=75:noet
diff --git a/include/system/builtins_end_.h b/include/system/builtins_end_.h
new file mode 100644
index 00000000..0a6ad60a
--- /dev/null
+++ b/include/system/builtins_end_.h
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+
+#ifdef mkbi1_
+#undef mkbi1_
+#endif
+#ifdef mkbi2_
+#undef mkbi2_
+#endif
+#ifdef mkbi3_
+#undef mkbi3_
+#endif
+#ifdef mkdepbi1_
+#undef mkdepbi1_
+#endif
+#ifdef mkdepbi2_
+#undef mkdepbi2_
+#endif
+#ifdef mkdepbi_dep_
+#undef mkdepbi_dep_
+#endif
+#ifdef mkbi_cat_
+#undef mkbi_cat_
+#endif
+
+#ifdef _BUILTINS_BEGIN__H
+#undef _BUILTINS_BEGIN__H
+#endif
+// vim:fenc=utf-8:tw=75:noet
diff --git a/include/system/ctype.h b/include/system/ctype.h
index c771bb69..65e7348f 100644
--- a/include/system/ctype.h
+++ b/include/system/ctype.h
@@ -3,11 +3,82 @@
* ctype.h - standard ctype functions
*/
#ifdef SHIM_UNIT_TEST
-#include_next <string.h>
+#include_next <ctype.h>
#else
#ifndef _CTYPE_H
#define _CTYPE_H
+#define isprint(c) ((c) >= 0x20 && (c) <= 0x7e)
+
+/* Determines if a particular character is a decimal-digit character */
+static inline __attribute__((__unused__)) int
+isdigit(int c)
+{
+ //
+ // <digit> ::= [0-9]
+ //
+ return (('0' <= (c)) && ((c) <= '9'));
+}
+
+/* Determine if an integer represents character that is a hex digit */
+static inline __attribute__((__unused__)) int
+isxdigit(int c)
+{
+ //
+ // <hexdigit> ::= [0-9] | [a-f] | [A-F]
+ //
+ return ((('0' <= (c)) && ((c) <= '9')) ||
+ (('a' <= (c)) && ((c) <= 'f')) ||
+ (('A' <= (c)) && ((c) <= 'F')));
+}
+
+/* Determines if a particular character represents a space character */
+static inline __attribute__((__unused__)) int
+isspace(int c)
+{
+ //
+ // <space> ::= [ ]
+ //
+ return ((c) == ' ');
+}
+
+/* Determine if a particular character is an alphanumeric character */
+static inline __attribute__((__unused__)) int
+isalnum(int c)
+{
+ //
+ // <alnum> ::= [0-9] | [a-z] | [A-Z]
+ //
+ return ((('0' <= (c)) && ((c) <= '9')) ||
+ (('a' <= (c)) && ((c) <= 'z')) ||
+ (('A' <= (c)) && ((c) <= 'Z')));
+}
+
+/* Determines if a particular character is in upper case */
+static inline __attribute__((__unused__)) int
+isupper(int c)
+{
+ //
+ // <uppercase letter> := [A-Z]
+ //
+ return (('A' <= (c)) && ((c) <= 'Z'));
+}
+
+/* Convert character to lowercase */
+static inline __attribute__((__unused__)) int
+tolower(int c)
+{
+ if (('A' <= (c)) && ((c) <= 'Z')) {
+ return (c - ('A' - 'a'));
+ }
+ return (c);
+}
+
+static inline __attribute__((__unused__)) int
+toupper(int c)
+{
+ return ((c >= 'a' && c <= 'z') ? c - ('a' - 'A') : c);
+}
#endif /* !_CTYPE_H */
#endif /* !SHIM_UNIT_TEST */
diff --git a/include/system/stdlib.h b/include/system/stdlib.h
index f2660f63..da7d3af9 100644
--- a/include/system/stdlib.h
+++ b/include/system/stdlib.h
@@ -11,6 +11,18 @@
*/
#include <stddef.h>
+static inline void abort(void) { }
+
+#include <builtins_begin_.h>
+mkbi1_(int, abs, int, j)
+mkbi1_(long int, labs, long int, j)
+mkbi1_(long long int, llabs, long long int, j)
+
+#ifdef _INTTYPES_H
+mkbi1_(intmax_t, imaxabs, intmax_t, j)
+#endif /* _INTTYPES_H */
+#include <builtins_end_.h>
+
#endif /* !_STDLIB_H */
#endif
// vim:fenc=utf-8:tw=75:noet
diff --git a/include/system/string.h b/include/system/string.h
index 21e46c1d..822e28fa 100644
--- a/include/system/string.h
+++ b/include/system/string.h
@@ -7,8 +7,55 @@
#include <stddef.h>
-__typeof__(__builtin_memset) memset;
-__typeof__(__builtin_memcpy) memcpy;
+#include <builtins_begin_.h>
+
+mkbi1_(long int, ffsl, long int, x)
+mkbi1_(long int, clzl, long int, x)
+mkbi1_(long int, ctzl, long int, x)
+mkbi1_(long int, clrsbl, long int, x)
+mkbi1_(long int, popcountl, long int, x)
+mkbi1_(long int, parityl, long int, x)
+mkbi1_(long long int, ffsll, long long int, x)
+mkbi1_(long long int, clzll, long long int, x)
+mkbi1_(long long int, ctzll, long long int, x)
+mkbi1_(long long int, clrsbll, long long int, x)
+mkbi1_(long long int, popcountll, long long int, x)
+mkbi1_(long long int, parityll, long long int, x)
+
+mkbi3_(int, bcmp, const void *, s1, const void *, s2, size_t, n)
+mkbi3_(void, bcopy, const void *, src, void *, dest, size_t, n)
+mkbi2_(void, bzero, void *, s, size_t, n)
+mkdepbi2_(char *, index, const char *, s, int, c)
+mkbi3_(void *, memchr, const void *, s, int, c, size_t, n)
+mkbi3_(int, memcmp, const void *, s1, const void *, s2, size_t, n)
+mkbi3_(void *, memcpy, void *, dest, const void *, src, size_t, n)
+mkbi3_(void *, memmove, void *, dest, const void *, src, size_t, n)
+mkbi3_(void *, mempcpy, void *, dest, const void *, src, size_t, n)
+mkdepbi2_(char *, rindex, const char *, s, int, c)
+mkdepbi2_(char *, stpcpy, char *, dest, const char *, src)
+mkbi3_(char *, stpncpy, char *, dest, const char *, src, size_t, n)
+mkdepbi2_(int, strcasecmp, const char *, s1, const char *, s2)
+mkdepbi2_(char *, strcat, char *, dest, const char *, src)
+mkdepbi2_(char *, strchr, const char *, s, int, c)
+mkdepbi2_(int, strcmp, const char *, s1, const char *, s2)
+mkdepbi2_(char *, strcpy, char *, dest, const char *, src)
+mkdepbi2_(size_t, strcspn, const char *, s, const char *, reject)
+mkdepbi1_(char *, strdup, const char *, s)
+mkbi2_(char *, strndup, const char *, s, size_t, n)
+mkdepbi1_(size_t, strlen, const char *, s)
+mkbi3_(int, strncasecmp, const char *, s1, const char *, s2, size_t, n)
+mkbi3_(char *, strncat, char *, dest, const char *, src, size_t, n)
+mkbi3_(int, strncmp, const char *, s1, const char *, s2, size_t, n)
+mkbi3_(char *, strncpy, char *, dest, const char *, src, size_t, n)
+mkbi2_(int, strnlen, const char *, s1, size_t, n)
+mkdepbi2_(char *, strpbrk, const char *, s, const char *, accept)
+mkdepbi2_(char *, strrchr, const char *, s, int, c)
+mkdepbi2_(size_t, strspn, const char *, s, const char *, accept)
+mkdepbi2_(char *, strstr, const char *, haystack, const char *, needle)
+
+mkbi3_(void *, memset, void *, s, int, c, size_t, n);
+
+#include <builtins_end_.h>
#endif /* _STRING_H */
#endif
diff --git a/include/system/strings.h b/include/system/strings.h
index c82bd917..99bc05f2 100644
--- a/include/system/strings.h
+++ b/include/system/strings.h
@@ -5,6 +5,15 @@
#ifndef _STRINGS_H
#define _STRINGS_H
+#include <builtins_begin_.h>
+mkbi1_(int, ffs, int, x)
+mkbi1_(int, clz, int, x)
+mkbi1_(int, ctz, int, x)
+mkbi1_(int, clrsb, int, x)
+mkbi1_(int, popcount, int, x)
+mkbi1_(int, parity, int, x)
+#include <builtins_end_.h>
+
#endif /* !_STRINGS_H */
#endif
// vim:fenc=utf-8:tw=75:noet