Index: libc/config/CMakeLists.txt =================================================================== --- libc/config/CMakeLists.txt +++ libc/config/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(linux) +add_subdirectory(darwin) Index: libc/config/darwin/CMakeLists.txt =================================================================== --- /dev/null +++ libc/config/darwin/CMakeLists.txt @@ -0,0 +1 @@ + Index: libc/config/darwin/api.td =================================================================== --- /dev/null +++ libc/config/darwin/api.td @@ -0,0 +1,156 @@ +include "config/public_api.td" + +include "spec/llvm_libc_ext.td" +include "spec/stdc.td" + +def SizeT : TypeDecl<"size_t"> { + let Decl = [{ + #define __need_size_t + #include + }]; +} + +def NullMacro : MacroDef<"NULL"> { + let Defn = [{ + #define __need_NULL + #include + }]; +} + +def ErrnoMacro : MacroDef<"errno"> { + let Defn = [{ + #ifdef __cplusplus + extern "C" + #endif + int *__errno_location(); + #define errno (*__errno_location()) + }]; +} + +def MathErrHandlingMacro : MacroDef<"math_errhandling"> { + let Defn = [{ + #ifndef math_errhandling + #ifdef __FAST_MATH__ + #define math_errhandling 0 + #elif defined __NO_MATH_ERRNO__ + #define math_errhandling (MATH_ERREXCEPT) + #else + #define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT) + #endif + #endif // math_errhandling not defined + }]; +} + +def IsFiniteMacro : MacroDef<"isfinite"> { + let Defn = [{ + #define isfinite(x) __builtin_isfinite(x) + }]; +} + +def IsInfMacro : MacroDef<"isinf"> { + let Defn = [{ + #define isinf(x) __builtin_isinf(x) + }]; +} + +def IsNanMacro : MacroDef<"isnan"> { + let Defn = [{ + #define isnan(x) __builtin_isnan(x) + }]; +} + +def FloatT : TypeDecl<"float_t"> { + let Decl = [{ + #define __need_float_t + #include <__llvm-libc-stdc-types.h> + }]; +} + +def DoubleT : TypeDecl<"double_t"> { + let Decl = [{ + #define __need_double_t + #include <__llvm-libc-stdc-types.h> + }]; +} + +def MathAPI : PublicAPI<"math.h"> { + let Macros = [ + SimpleMacroDef<"MATH_ERRNO", "1">, + SimpleMacroDef<"MATH_ERREXCEPT", "2">, + MathErrHandlingMacro, + + SimpleMacroDef<"INFINITY", "__builtin_inff()">, + SimpleMacroDef<"NAN", "__builtin_nanf(\"\")">, + + IsFiniteMacro, + IsInfMacro, + IsNanMacro, + ]; + let TypeDeclarations = [ + DoubleT, + FloatT, + ]; + let Functions = [ + "copysign", + "copysignf", + "ceil", + "ceilf", + "cosf", + "fabs", + "fabsf", + "fabsl", + "floor", + "floorf", + "frexp", + "frexpf", + "logb", + "logbf", + "modf", + "modff", + "expf", + "exp2f", + "round", + "roundf", + "sincosf", + "sinf", + "trunc", + "truncf", + "truncl", + ]; +} + +def StringAPI : PublicAPI<"string.h"> { + let Functions = [ + "bzero", + "memcpy", + "memmove", + "memcmp", + "memchr", + "memset", + "strcpy", + "strncpy", + "strcat", + "strncat", + "strcmp", + "strcoll", + "strncmp", + "strxfrm", + "strchr", + "strcspn", + "strpbrk", + "strrchr", + "strspn", + "strstr", + "strtok", + "strerror", + "strlen", + ]; + + let TypeDeclarations = [ + SizeT, + ]; + + let Macros = [ + NullMacro, + ]; +} Index: libc/config/darwin/platfrom_defs.h.inc =================================================================== --- /dev/null +++ libc/config/darwin/platfrom_defs.h.inc @@ -0,0 +1,15 @@ +//===----- Definition of platform specific macros for Mach-O platforms -------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +%%begin() + +#define ENTRYPOINT_SECTION_ATTRIBUTE(name) \ + __attribute__((section(".libc.entrypoint,"#name))) +#define LLVM_LIBC_ENTRYPOINT(name) ENTRYPOINT_SECTION_ATTRIBUTE(name) name + +// Note the comma in the section name!!! Index: libc/config/darwin/x86_64/entrypoints.txt =================================================================== --- /dev/null +++ libc/config/darwin/x86_64/entrypoints.txt @@ -0,0 +1,41 @@ +set(TARGET_LIBC_ENTRYPOINTS + # errno.h entrypoints + libc.src.errno.__errno_location + + # string.h entrypoints + libc.src.string.bzero + libc.src.string.memcpy + libc.src.string.memset + libc.src.string.strcpy + libc.src.string.strcat + libc.src.string.strlen +) + +set(TARGET_LIBM_ENTRYPOINTS + # math.h entrypoints + libc.src.math.copysign + libc.src.math.copysignf + libc.src.math.ceil + libc.src.math.ceilf + libc.src.math.cosf + libc.src.math.expf + libc.src.math.exp2f + libc.src.math.fabs + libc.src.math.fabsf + libc.src.math.fabsl + libc.src.math.floor + libc.src.math.floorf + libc.src.math.frexp + libc.src.math.frexpf + libc.src.math.logb + libc.src.math.logbf + libc.src.math.modf + libc.src.math.modff + libc.src.math.round + libc.src.math.roundf + libc.src.math.sincosf + libc.src.math.sinf + libc.src.math.trunc + libc.src.math.truncf + libc.src.math.truncl +) Index: libc/config/darwin/x86_64/headers.txt =================================================================== --- /dev/null +++ libc/config/darwin/x86_64/headers.txt @@ -0,0 +1,6 @@ +set(PUBLIC_HEADERS + libc.include.errno + libc.include.math + libc.include.sys_syscall +) + Index: libc/config/linux/platfrom_defs.h.inc =================================================================== --- libc/config/linux/platfrom_defs.h.inc +++ libc/config/linux/platfrom_defs.h.inc @@ -1,4 +1,4 @@ -//===----- Definition of platform specific macros for ELF paltforms -------===// +//===----- Definition of platform specific macros for ELF platforms -------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. Index: libc/src/string/bzero.h =================================================================== --- libc/src/string/bzero.h +++ libc/src/string/bzero.h @@ -10,6 +10,7 @@ #define LLVM_LIBC_SRC_STRING_BZERO_H #include "include/string.h" +#include // size_t namespace __llvm_libc { Index: libc/src/string/memset.h =================================================================== --- libc/src/string/memset.h +++ libc/src/string/memset.h @@ -10,6 +10,7 @@ #define LLVM_LIBC_SRC_STRING_MEMSET_H #include "include/string.h" +#include // size_t namespace __llvm_libc { Index: libc/src/string/strlen.h =================================================================== --- libc/src/string/strlen.h +++ libc/src/string/strlen.h @@ -10,6 +10,7 @@ #define LLVM_LIBC_SRC_STRING_STRLEN_H #include "include/string.h" +#include // size_t namespace __llvm_libc { Index: libc/src/string/strlen.cpp =================================================================== --- libc/src/string/strlen.cpp +++ libc/src/string/strlen.cpp @@ -9,6 +9,7 @@ #include "src/string/strlen.h" #include "src/__support/common.h" +#include // size_t namespace __llvm_libc { Index: libc/test/config/darwin/CMakeLists.txt =================================================================== --- /dev/null +++ libc/test/config/darwin/CMakeLists.txt @@ -0,0 +1,5 @@ +add_libc_testsuite(libc_darwin_tests) + +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_MACHINE}) + add_subdirectory(${LIBC_TARGET_MACHINE}) +endif() Index: libc/test/config/darwin/x86_64/CMakeLists.txt =================================================================== --- /dev/null +++ libc/test/config/darwin/x86_64/CMakeLists.txt @@ -0,0 +1,9 @@ +add_libc_unittest( + libc_darwin_x86_64_strlen_test + SUITE + libc_darwin_tests + SRCS + darwin.cpp + DEPENDS + libc.src.string.strlen +) Index: libc/test/config/darwin/x86_64/darwin.cpp =================================================================== --- /dev/null +++ libc/test/config/darwin/x86_64/darwin.cpp @@ -0,0 +1,24 @@ +//===-- Unittests for strlen -------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/string/strlen.h" +#include "utils/UnitTest/Test.h" + +TEST(StrLenTest, EmptyString) { + const char *empty = ""; + + size_t result = __llvm_libc::strlen(empty); + ASSERT_EQ((size_t)0, result); +} + +TEST(StrLenTest, AnyString) { + const char *any = "Hello World!"; + + size_t result = __llvm_libc::strlen(any); + ASSERT_EQ((size_t)12, result); +}