Import of the watch repository from Pebble

This commit is contained in:
Matthieu Jeanson 2024-12-12 16:43:03 -08:00 committed by Katharine Berry
commit 3b92768480
10334 changed files with 2564465 additions and 0 deletions

23
src/libc/alloca.c Normal file
View file

@ -0,0 +1,23 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <include/alloca.h>
# define __alloca(size) __builtin_alloca (size)
void *alloca(size_t size) {
return __alloca(size);
}

79
src/libc/ctype_ptr.c Normal file
View file

@ -0,0 +1,79 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements: (sorta)
// int isalpha(int c);
// int isupper(int c);
// int islower(int c);
// int isdigit(int c);
// int isxdigit(int c);
// int isspace(int c);
// int ispunct(int c);
// int isalnum(int c);
// int isprint(int c);
// int isgraph(int c);
// int iscntrl(int c);
// int isascii(int c);
#if UNITTEST
# include <include/ctype.h>
#else
# include <ctype.h>
#endif
// Ordered for unsigned char
#if !UNITTEST
__attribute__((__externally_visible__))
#endif
const unsigned char __ctype_data[256] = {
// ASCII set
// These values can't change. Look at ctype.h for information on these macros.
// +00 +01 +02 +03 +04 +05 +06 +07
_CCT, _CCT, _CCT, _CCT, _CCT, _CCT, _CCT, _CCT, // 00
_CCT, _CCT|_CSP, _CCT|_CSP, _CCT|_CSP, _CCT|_CSP, _CCT|_CSP, _CCT, _CCT, // 08
_CCT, _CCT, _CCT, _CCT, _CCT, _CCT, _CCT, _CCT, // 10
_CCT, _CCT, _CCT, _CCT, _CCT, _CCT, _CCT, _CCT, // 18
_CSP|_CPR, _CPU, _CPU, _CPU, _CPU, _CPU, _CPU, _CPU, // 20
_CPU, _CPU, _CPU, _CPU, _CPU, _CPU, _CPU, _CPU, // 28
_CNU, _CNU, _CNU, _CNU, _CNU, _CNU, _CNU, _CNU, // 30
_CNU, _CNU, _CPU, _CPU, _CPU, _CPU, _CPU, _CPU, // 38
_CPU, _CUP|_CHX, _CUP|_CHX, _CUP|_CHX, _CUP|_CHX, _CUP|_CHX, _CUP|_CHX, _CUP, // 40
_CUP, _CUP, _CUP, _CUP, _CUP, _CUP, _CUP, _CUP, // 48
_CUP, _CUP, _CUP, _CUP, _CUP, _CUP, _CUP, _CUP, // 50
_CUP, _CUP, _CUP, _CPU, _CPU, _CPU, _CPU, _CPU, // 58
_CPU, _CLO|_CHX, _CLO|_CHX, _CLO|_CHX, _CLO|_CHX, _CLO|_CHX, _CLO|_CHX, _CLO, // 60
_CLO, _CLO, _CLO, _CLO, _CLO, _CLO, _CLO, _CLO, // 68
_CLO, _CLO, _CLO, _CLO, _CLO, _CLO, _CLO, _CLO, // 70
_CLO, _CLO, _CLO, _CPU, _CPU, _CPU, _CPU, _CCT, // 78
// Non-ASCII set
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};

41
src/libc/include/_ansi.h Normal file
View file

@ -0,0 +1,41 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
// this is all magic necessary for pre-ansi compatible code
#define _PTR void *
#define _AND ,
#define _NOARGS void
#define _CONST const
#define _VOLATILE volatile
#define _SIGNED signed
#define _DOTS , ...
#define _VOID void
#define _EXFUN(name, proto) name proto
#define _EXPARM(name, proto) (* name) proto
#define _DEFUN(name, arglist, args) name(args)
#define _DEFUN_VOID(name) name(_NOARGS)
#define _CAST_VOID (void)
#ifndef _LONG_DOUBLE
#define _LONG_DOUBLE long double
#endif
#ifndef _PARAMS
#define _PARAMS(paramlist) paramlist
#endif

21
src/libc/include/alloca.h Normal file
View file

@ -0,0 +1,21 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <stddef.h>
void *alloca(size_t size);

68
src/libc/include/ctype.h Normal file
View file

@ -0,0 +1,68 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
// Casts to int ensure that the return value is int, as spec indicates
// Uppercase
#define _CUP (1 << 0)
// Lowercase
#define _CLO (1 << 1)
// Number
#define _CNU (1 << 2)
// Space
#define _CSP (1 << 3)
// Punctuation
#define _CPU (1 << 4)
// Control
#define _CCT (1 << 5)
// Printable (only for ' ')
#define _CPR (1 << 6)
// Hex character
#define _CHX (1 << 7)
// ctype flag table for all char values
extern const unsigned char __ctype_data[256];
// Cast to unsigned char to prevent overflow and to map signed char to our range.
// This lets us cut out 128 bytes of data from __ctype_data
#define __ctype_get(C) (__ctype_data[(unsigned char)(C)])
#define __ctype_check(C, FLG) ((int)(__ctype_get(C) & (FLG)))
#define isalpha(C) __ctype_check(C, _CUP|_CLO)
#define isupper(C) __ctype_check(C, _CUP)
#define islower(C) __ctype_check(C, _CLO)
#define isdigit(C) __ctype_check(C, _CNU)
#define isxdigit(C) __ctype_check(C, _CHX|_CNU)
#define isspace(C) __ctype_check(C, _CSP)
#define ispunct(C) __ctype_check(C, _CPU)
#define isalnum(C) __ctype_check(C, _CUP|_CLO|_CNU)
#define isprint(C) __ctype_check(C, _CUP|_CLO|_CNU|_CPU|_CPR)
#define isgraph(C) __ctype_check(C, _CUP|_CLO|_CNU|_CPU)
#define iscntrl(C) __ctype_check(C, _CCT)
#define isascii(C) ((int)(((unsigned char)(C)) < 0x80))
#define toascii(C) ((int)(((unsigned char)(C)) & 0x7F))
// __typeof__ use is to prevent double-evaluation
#define toupper(C) \
({ __typeof__(C) X = (C); \
islower(X) ? (int)X - 'a' + 'A' : (int)X; })
#define tolower(C) \
({ __typeof__(C) X = (C); \
isupper(X) ? (int)X - 'A' + 'a' : (int)X; })

314
src/libc/include/inttypes.h Normal file
View file

@ -0,0 +1,314 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <stdint.h>
// There's no way to check this from the preprocessor, and this header isn't freestanding, so...
// If we ever upgrade GCC, we may need to re-evaluate these two.
// You can check the types by running `arm-none-eabi-gcc -dM -E - </dev/null` and looking for
// __UINT32_TYPE__ and __UINTPTR_TYPE__ respectively.
#if defined(__clang__)
#define __UINT32_IS_LONG 0
#else
#define __UINT32_IS_LONG 1
#endif
#define __UINTPTR_IS_LONG 0
#define __PRICHAR ""
#define __PRISHORT ""
#define __PRIINT ""
#define __PRILONG "l"
#define __PRILLONG "ll"
#define __PRI8(t) __PRICHAR#t
#define __PRI16(t) __PRISHORT#t
#if __UINT32_IS_LONG
# define __PRI32(t) __PRILONG#t
#else
# define __PRI32(t) __PRIINT#t
#endif
#define __PRI64(t) __PRILLONG#t
#if __UINTPTR_IS_LONG
#define __PRIPTR(t) __PRILONG#t
#else
#define __PRIPTR(t) __PRIINT#t
#endif
#ifndef PRId8
# define PRId8 __PRI8(d)
#endif
#ifndef PRIi8
# define PRIi8 __PRI8(i)
#endif
#ifndef PRIo8
# define PRIo8 __PRI8(o)
#endif
#ifndef PRIu8
# define PRIu8 __PRI8(u)
#endif
#ifndef PRIx8
# define PRIx8 __PRI8(x)
#endif
#ifndef PRIX8
# define PRIX8 __PRI8(X)
#endif
#ifndef PRIdLEAST8
# define PRIdLEAST8 PRId8
#endif
#ifndef PRIdFAST8
# define PRIdFAST8 PRId8
#endif
#ifndef PRIiLEAST8
# define PRIiLEAST8 PRIi8
#endif
#ifndef PRIiFAST8
# define PRIiFAST8 PRIi8
#endif
#ifndef PRIoLEAST8
# define PRIoLEAST8 PRIo8
#endif
#ifndef PRIoFAST8
# define PRIoFAST8 PRIo8
#endif
#ifndef PRIuLEAST8
# define PRIuLEAST8 PRIu8
#endif
#ifndef PRIuFAST8
# define PRIuFAST8 PRIu8
#endif
#ifndef PRIxLEAST8
# define PRIxLEAST8 PRIx8
#endif
#ifndef PRIxFAST8
# define PRIxFAST8 PRIx8
#endif
#ifndef PRIXLEAST8
# define PRIXLEAST8 PRIX8
#endif
#ifndef PRIXFAST8
# define PRIXFAST8 PRIX8
#endif
#ifndef PRId16
# define PRId16 __PRI16(d)
#endif
#ifndef PRIi16
# define PRIi16 __PRI16(i)
#endif
#ifndef PRIo16
# define PRIo16 __PRI16(o)
#endif
#ifndef PRIu16
# define PRIu16 __PRI16(u)
#endif
#ifndef PRIx16
# define PRIx16 __PRI16(x)
#endif
#ifndef PRIX16
# define PRIX16 __PRI16(X)
#endif
#ifndef PRIdLEAST16
# define PRIdLEAST16 PRId16
#endif
#ifndef PRIdFAST16
# define PRIdFAST16 PRId16
#endif
#ifndef PRIiLEAST16
# define PRIiLEAST16 PRIi16
#endif
#ifndef PRIiFAST16
# define PRIiFAST16 PRIi16
#endif
#ifndef PRIoLEAST16
# define PRIoLEAST16 PRIo16
#endif
#ifndef PRIoFAST16
# define PRIoFAST16 PRIo16
#endif
#ifndef PRIuLEAST16
# define PRIuLEAST16 PRIu16
#endif
#ifndef PRIuFAST16
# define PRIuFAST16 PRIu16
#endif
#ifndef PRIxLEAST16
# define PRIxLEAST16 PRIx16
#endif
#ifndef PRIxFAST16
# define PRIxFAST16 PRIx16
#endif
#ifndef PRIXLEAST16
# define PRIXLEAST16 PRIX16
#endif
#ifndef PRIXFAST16
# define PRIXFAST16 PRIX16
#endif
#ifndef PRId32
# define PRId32 __PRI32(d)
#endif
#ifndef PRIi32
# define PRIi32 __PRI32(i)
#endif
#ifndef PRIo32
# define PRIo32 __PRI32(o)
#endif
#ifndef PRIu32
# define PRIu32 __PRI32(u)
#endif
#ifndef PRIx32
# define PRIx32 __PRI32(x)
#endif
#ifndef PRIX32
# define PRIX32 __PRI32(X)
#endif
#ifndef PRIdLEAST32
# define PRIdLEAST32 PRId32
#endif
#ifndef PRIdFAST32
# define PRIdFAST32 PRId32
#endif
#ifndef PRIiLEAST32
# define PRIiLEAST32 PRIi32
#endif
#ifndef PRIiFAST32
# define PRIiFAST32 PRIi32
#endif
#ifndef PRIoLEAST32
# define PRIoLEAST32 PRIo32
#endif
#ifndef PRIoFAST32
# define PRIoFAST32 PRIo32
#endif
#ifndef PRIuLEAST32
# define PRIuLEAST32 PRIu32
#endif
#ifndef PRIuFAST32
# define PRIuFAST32 PRIu32
#endif
#ifndef PRIxLEAST32
# define PRIxLEAST32 PRIx32
#endif
#ifndef PRIxFAST32
# define PRIxFAST32 PRIx32
#endif
#ifndef PRIXLEAST32
# define PRIXLEAST32 PRIX32
#endif
#ifndef PRIXFAST32
# define PRIXFAST32 PRIX32
#endif
#ifndef PRId64
# define PRId64 __PRI64(d)
#endif
#ifndef PRIi64
# define PRIi64 __PRI64(i)
#endif
#ifndef PRIo64
# define PRIo64 __PRI64(o)
#endif
#ifndef PRIu64
# define PRIu64 __PRI64(u)
#endif
#ifndef PRIx64
# define PRIx64 __PRI64(x)
#endif
#ifndef PRIX64
# define PRIX64 __PRI64(X)
#endif
#ifndef PRIdLEAST64
# define PRIdLEAST64 PRId64
#endif
#ifndef PRIdFAST64
# define PRIdFAST64 PRId64
#endif
#ifndef PRIiLEAST64
# define PRIiLEAST64 PRIi64
#endif
#ifndef PRIiFAST64
# define PRIiFAST64 PRIi64
#endif
#ifndef PRIoLEAST64
# define PRIoLEAST64 PRIo64
#endif
#ifndef PRIoFAST64
# define PRIoFAST64 PRIo64
#endif
#ifndef PRIuLEAST64
# define PRIuLEAST64 PRIu64
#endif
#ifndef PRIuFAST64
# define PRIuFAST64 PRIu64
#endif
#ifndef PRIxLEAST64
# define PRIxLEAST64 PRIx64
#endif
#ifndef PRIxFAST64
# define PRIxFAST64 PRIx64
#endif
#ifndef PRIXLEAST64
# define PRIXLEAST64 PRIX64
#endif
#ifndef PRIXFAST64
# define PRIXFAST64 PRIX64
#endif
#ifndef PRIdPTR
# define PRIdPTR __PRIPTR(d)
#endif
#ifndef PRIiPTR
# define PRIiPTR __PRIPTR(i)
#endif
#ifndef PRIoPTR
# define PRIoPTR __PRIPTR(o)
#endif
#ifndef PRIuPTR
# define PRIuPTR __PRIPTR(u)
#endif
#ifndef PRIxPTR
# define PRIxPTR __PRIPTR(x)
#endif
#ifndef PRIXPTR
# define PRIXPTR __PRIPTR(X)
#endif
#ifndef PRIdMAX
# define PRIdMAX PRId64
#endif
#ifndef PRIiMAX
# define PRIiMAX PRIi64
#endif
#ifndef PRIoMAX
# define PRIoMAX PRIo64
#endif
#ifndef PRIuMAX
# define PRIuMAX PRIu64
#endif
#ifndef PRIxMAX
# define PRIxMAX PRIx64
#endif
#ifndef PRIXMAX
# define PRIXMAX PRIX64
#endif

72
src/libc/include/locale.h Normal file
View file

@ -0,0 +1,72 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#define __need_NULL
#include <stddef.h>
// We use newlib's order because why not and it'll make us more compatible.
#ifndef LC_ALL
# define LC_ALL 0
#endif
#ifndef LC_COLLATE
# define LC_COLLATE 1
#endif
#ifndef LC_CTYPE
# define LC_CTYPE 2
#endif
#ifndef LC_MONETARY
# define LC_MONETARY 3
#endif
#ifndef LC_NUMERIC
# define LC_NUMERIC 4
#endif
#ifndef LC_TIME
# define LC_TIME 5
#endif
#ifndef LC_MESSAGES
# define LC_MESSAGES 6
#endif
struct lconv {
char *decimal_point;
char *thousands_sep;
char *grouping;
char *int_curr_symbol;
char *currency_symbol;
char *mon_decimal_point;
char *mon_thousands_sep;
char *mon_grouping;
char *positive_sign;
char *negative_sign;
char int_frac_digits;
char frac_digits;
char p_cs_precedes;
char p_sep_by_space;
char n_cs_precedes;
char n_sep_by_space;
char p_sign_posn;
char n_sign_posn;
char int_n_cs_precedes;
char int_n_sep_by_space;
char int_n_sign_posn;
char int_p_cs_precedes;
char int_p_sep_by_space;
char int_p_sign_posn;
};
char *setlocale(int category, const char *locale);

23
src/libc/include/math.h Normal file
View file

@ -0,0 +1,23 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
typedef float float_t;
typedef double double_t;
double round(double d);

44
src/libc/include/setjmp.h Normal file
View file

@ -0,0 +1,44 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Notes:
// This is entirely non-portable. It will need to be rewritten if we stop using ARM.
// Unfortunately, there is no portable way to define these.
// This means unit tests can't use our setjmp/longjmp either.
#pragma once
#if __arm__
// On ARM at least, GPRs are longs
// This still holds on A64.
struct __jmp_buf_struct {
long r4, r5, r6, r7, r8, r9, sl, fp, sp, lr;
// Using real FPU
#if defined(__VFP_FP__) && !defined(__SOFTFP__)
// FPU registers are still 32bit on A64 though, so they're ints
int s[16]; /* s16~s31 */
int fpscr;
#endif
};
typedef struct __jmp_buf_struct jmp_buf[1];
int setjmp(jmp_buf env);
void longjmp(jmp_buf buf, int value);
#else
// other implementations either use system setjmp or don't have it.
# include_next <setjmp.h>
#endif

47
src/libc/include/stdio.h Normal file
View file

@ -0,0 +1,47 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#define __need_size_t
#define __need_NULL
#include <stddef.h>
#define __need___va_list
#include <stdarg.h>
typedef struct {
} FILE;
// stdio.h isn't supposed to define va_list, so _need___va_list gives us __gnuc_va_list
// Let's define that to something less compiler-specific
#define __VA_LIST __gnuc_va_list
int printf(const char *__restrict format, ...)
__attribute__((format (printf, 1, 2)));
int sprintf(char * restrict str, const char * restrict format, ...)
__attribute__((__format__(__printf__, 2, 3)));
int snprintf(char * restrict str, size_t size, const char * restrict format, ...)
__attribute__((__format__(__printf__, 3, 4)));
int vsprintf(char * restrict str, const char * restrict format, __VA_LIST ap)
__attribute__((__format__(__printf__, 2, 0)));
int vsnprintf(char * restrict str, size_t size, const char * restrict format, __VA_LIST ap)
__attribute__((__format__(__printf__, 3, 0)));
#if !UNITTEST
#define sniprintf snprintf
#define vsniprintf vsnprintf
#endif

59
src/libc/include/stdlib.h Normal file
View file

@ -0,0 +1,59 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#ifndef EXIT_FAILURE
# define EXIT_FAILURE 1
#endif
#ifndef EXIT_SUCCESS
# define EXIT_SUCCESS 0
#endif
// It's an int.
#ifndef RAND_MAX
# define RAND_MAX (0x7FFFFFFFL)
#endif
#define __need_size_t
#define __need_NULL
#include <stddef.h>
int abs(int j) __attribute__((__const__));
long labs(long j) __attribute__((__const__));
#if __clang__
// Default builtins break the clang build for some reason,
// and we don't have a real implementation of these functions
#define abs(x) __builtin_abs(x)
#define labs(x) __builtin_labs(x)
#endif
int atoi(const char *nptr) __attribute__((__pure__));
long int atol(const char *nptr) __attribute__((__pure__));
long int strtol(const char * restrict nptr, char ** restrict endptr, int base);
// Implemented in src/fw/util/rand/rand.c
int rand(void);
int rand_r(unsigned *seed);
void srand(unsigned seed);
void exit(int status) __attribute__((noreturn));
// Not implemented, but included in the header to build the default platform.c of libs.
void free(void *ptr);
void *malloc(size_t bytes);

45
src/libc/include/string.h Normal file
View file

@ -0,0 +1,45 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#define __need_size_t
#define __need_NULL
#include <stddef.h>
int memcmp(const void *s1, const void *s2, size_t n);
void *memcpy(void * restrict s1, const void * restrict s2, size_t n);
void *memmove(void *s1, const void *s2, size_t n);
void *memset(void *s, int c, size_t n);
void *memchr(const void *s, int c, size_t n);
char *strcat(char * restrict s1, const char * restrict s2);
char *strncat(char * restrict s1, const char * restrict s2, size_t n);
size_t strlen(const char *s);
size_t strnlen(const char *s, size_t maxlen);
char *strcpy(char * restrict s1, const char * restrict s2);
char *strncpy(char * restrict s1, const char * restrict s2, size_t n);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
size_t strcspn(const char *s1, const char *s2);
size_t strspn(const char *s1, const char *s2);
char *strstr(const char *s1, const char *s2);
int atoi(const char *nptr) __attribute__((__pure__));
long int atol(const char *nptr) __attribute__((__pure__));
long int strtol(const char * restrict nptr, char ** restrict endptr, int base);
unsigned long int strtoul(const char * restrict nptr, char ** restrict endptr, int base);

View file

@ -0,0 +1,29 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#if defined(__cplusplus)
#define __BEGIN_DECLS extern "C" {
#define __END_DECLS };
#else
#define __BEGIN_DECLS
#define __END_DECLS
#endif
#define __P(protos) protos
#define __CONCAT(x, y) x ## y
#define __STRING(x) #x

View file

@ -0,0 +1,19 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
// stub

View file

@ -0,0 +1,29 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#define __need_size_t
#include <stddef.h>
// Cheap trick to make __SIZE_TYPE__ give an unsigned version
#define unsigned signed
typedef __SIZE_TYPE__ ssize_t;
#undef unsigned
typedef signed long int time_t;
typedef ssize_t off_t;

19
src/libc/include/unistd.h Normal file
View file

@ -0,0 +1,19 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
// stub

19
src/libc/include/wchar.h Normal file
View file

@ -0,0 +1,19 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
// stub

19
src/libc/include/wctype.h Normal file
View file

@ -0,0 +1,19 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
// stub

57
src/libc/math/floor.c Normal file
View file

@ -0,0 +1,57 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// double floor(double x);
#include <pblibc_private.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
// TODO: PBL-36144 replace this naive implementation with __builtin_floor()
#ifndef NAN
#define NAN (0.0/0.0)
#endif
#ifndef INFINITY
#define INFINITY (1.0/0.0)
#endif
#ifndef isinf
#define isinf(y) (__builtin_isinf(y))
#endif
#ifndef isnan
#define isnan(y) (__builtin_isnan(y))
#endif
double floor(double x) {
if (isnan(x)) {
return NAN;
}
if (isinf(x)) {
return INFINITY;
}
const int64_t x_int = (int64_t)x;
const bool has_no_fraction = (double)x_int == x;
return (double)((x >= 0 || has_no_fraction) ? x_int : x_int - 1);
}

33
src/libc/math/round.c Normal file
View file

@ -0,0 +1,33 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Naive implementation, probably room for optimization...
double round(double d) {
if (__builtin_floor(d) == d) {
return d;
}
double half;
if (d < 0.0) {
if (d > -0.5) {
return -0.0;
} else {
half = -0.5;
}
} else {
half = 0.5;
}
return __builtin_floor(d + half);
}

View file

@ -0,0 +1,25 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#if __ARM_ARCH_ISA_THUMB >= 2
# define MEMCPY_IMPLEMENTED_IN_ASSEMBLY 1
# define MEMSET_IMPLEMENTED_IN_ASSEMBLY 1
#else
# define MEMCPY_IMPLEMENTED_IN_ASSEMBLY 0
# define MEMSET_IMPLEMENTED_IN_ASSEMBLY 0
#endif

149
src/libc/pblibc_private.h Normal file
View file

@ -0,0 +1,149 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
// Rename (and re-define) the libc functions
#if UNITTEST
#include <stddef.h>
#include <stdarg.h>
# define PBLIBC(fn) pblibc_##fn
// String functions
# undef memcmp
# define memcmp PBLIBC(memcmp)
# undef memcpy
# define memcpy PBLIBC(memcpy)
# undef memmove
# define memmove PBLIBC(memmove)
# undef memset
# define memset PBLIBC(memset)
# undef memchr
# define memchr PBLIBC(memchr)
# undef atoi
# define atoi PBLIBC(atoi)
# undef atol
# define atol PBLIBC(atol)
# undef strtol
# define strtol PBLIBC(strtol)
# undef strcat
# define strcat PBLIBC(strcat)
# undef strncat
# define strncat PBLIBC(strncat)
# undef strlen
# define strlen PBLIBC(strlen)
# undef strnlen
# define strnlen PBLIBC(strnlen)
# undef strcpy
# define strcpy PBLIBC(strcpy)
# undef strncpy
# define strncpy PBLIBC(strncpy)
# undef strcmp
# define strcmp PBLIBC(strcmp)
# undef strncmp
# define strncmp PBLIBC(strncmp)
# undef strchr
# define strchr PBLIBC(strchr)
# undef strrchr
# define strrchr PBLIBC(strrchr)
# undef strcspn
# define strcspn PBLIBC(strcspn)
# undef strspn
# define strspn PBLIBC(strspn)
# undef strstr
# define strstr PBLIBC(strstr)
int memcmp(const void *s1, const void *s2, size_t n);
void *memcpy(void *s1, const void *s2, size_t n);
void *memmove(void *s1, const void *s2, size_t n);
void *memset(void *s, int c, size_t n);
void *memchr(const void *s, int c, size_t n);
int atoi(const char *nptr);
long int atol(const char *nptr);
long int strtol(const char *nptr, char **endptr, int base);
char *strcat(char *s1, const char *s2);
char *strncat(char *s1, const char *s2, size_t n);
size_t strlen(const char *s);
size_t strnlen(const char *s, size_t maxlen);
char *strcpy(char *s1, const char *s2);
char *strncpy(char *s1, const char *s2, size_t n);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
size_t strcspn(const char *s1, const char *s2);
size_t strspn(const char *s1, const char *s2);
char *strstr(const char *s1, const char *s2);
// Math functions
# undef floor
# define floor PBLIBC(floor)
# undef log
# define log PBLIBC(log)
# undef pow
# define pow PBLIBC(pow)
# undef scalbn
# define scalbn PBLIBC(scalbn)
# undef sqrt
# define sqrt PBLIBC(sqrt)
double floor(double x);
double log(double x);
double pow(double x, double y);
double round(double d);
double scalbn(double x, int n);
double sqrt(double x);
// ctype
# undef __ctype_lookup
# undef isalpha
# undef isupper
# undef islower
# undef isdigit
# undef isxdigit
# undef isspace
# undef ispunct
# undef isalnum
# undef isprint
# undef isgraph
# undef iscntrl
# undef isascii
# undef toascii
# undef toupper
# undef tolower
// printf
# undef vsprintf
# define vsprintf PBLIBC(vsprintf)
# undef vsnprintf
# define vsnprintf PBLIBC(vsnprintf)
# undef sprintf
# define sprintf PBLIBC(sprintf)
# undef snprintf
# define snprintf PBLIBC(snprintf)
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
#endif

77
src/libc/setjmp.c Normal file
View file

@ -0,0 +1,77 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// int setjmp(jmp_buf env);
// void longjmp(jmp_buf buf, int value);
///////////////////////////////////////
// Notes:
// This is entirely non-portable. It will need to be rewritten if we stop using ARM.
// Unfortunately, there is no portable way to define these.
// This means unit tests can't use our setjmp/longjmp either.
#include <include/setjmp.h>
#if __ARM_ARCH_ISA_THUMB >= 2
// Valid for anything with THUMB2
int __attribute__((__naked__)) setjmp(jmp_buf env) {
__asm volatile (
// move SP to a register we can store from and don't need to save
"mov %ip, %sp\n"
// store all the registers
"stmia %r0!, {%r4-%r9, %sl, %fp, %ip, %lr}\n"
// using real FPU
#if defined(__VFP_FP__) && !defined(__SOFTFP__)
// store FP registers
"vstmia %r0!, {%s16-%s31}\n"
// store FPSCR
"vmrs %r1, fpscr\n"
"str %r1, [%r0], #4\n"
#endif
// return 0
"mov %r0, #0\n"
"bx lr\n"
);
}
void __attribute__((__naked__)) longjmp(jmp_buf buf, int value) {
__asm volatile (
// load all the registers
"ldmia %r0!, {%r4-%r9, %sl, %fp, %ip, %lr}\n"
// load SP from a register we could load to and don't need to restore
"mov %sp, %ip\n"
// using real FPU
#if defined(__VFP_FP__) && !defined(__SOFTFP__)
// load FP registers
"vldmia %r0!, {%s16-%s31}\n"
// load FPSCR
"ldr %r2, [%r0], #4\n"
"vmsr fpscr, %r2\n"
#endif
// return value
"movs %r0, %r1\n"
// unless it's 0, in which case, return 1.
"it eq\n"
"moveq %r0, #1\n"
"bx lr\n"
);
}
#else
// Undefined implementations, don't implement!
// That way we get a link-time error.
#endif

40
src/libc/string/atoi.c Normal file
View file

@ -0,0 +1,40 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// int atoi(const char *nptr);
// long int atol(const char *nptr);
///////////////////////////////////////
// Exports to apps:
// atoi, atol
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
#include <pblibc_private.h>
intmax_t strtoX_core(const char * restrict nptr, char ** restrict endptr, int base, bool do_errors,
intmax_t max, intmax_t min);
int atoi(const char *nptr) {
return strtoX_core(nptr, NULL, 10, false, INT_MAX, INT_MIN);
}
long int atol(const char *nptr) {
return strtoX_core(nptr, NULL, 10, false, INT_MAX, INT_MIN);
}

33
src/libc/string/memchr.c Normal file
View file

@ -0,0 +1,33 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// void *memchr(const void *s, int c, size_t n);
#include <stddef.h>
#include <pblibc_private.h>
void *memchr(const void *s, int c, size_t n) {
const unsigned char *p = (const unsigned char*)s;
unsigned char ch = (unsigned char)c;
for (size_t i = 0; i < n; i++) {
if (p[i] == ch) {
return (void*)&p[i];
}
}
return NULL;
}

36
src/libc/string/memcmp.c Normal file
View file

@ -0,0 +1,36 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// int memcmp(const void *s1, const void *s2, size_t n);
#include <stddef.h>
#include <pblibc_private.h>
int memcmp(const void *s1, const void *s2, size_t n) {
const unsigned char *p1 = (const unsigned char*)s1;
const unsigned char *p2 = (const unsigned char*)s2;
while (n--) {
int diff = *p1 - *p2;
if (diff) {
return diff;
}
p1++;
p2++;
}
return 0;
}

54
src/libc/string/memcpy.c Normal file
View file

@ -0,0 +1,54 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// void *memcpy(void *s1, const void *s2, size_t n);
// void *memmove(void *s1, const void *s2, size_t n);
///////////////////////////////////////
// Exports to apps:
// memcpy, memmove
#include <stddef.h>
#include <stdint.h>
#include <pblibc_assembly.h>
#include <pblibc_private.h>
#if !MEMCPY_IMPLEMENTED_IN_ASSEMBLY
void *memcpy(void * restrict s1, const void * restrict s2, size_t n) {
char *dest = (char*)s1;
const char *src = (const char*)s2;
while (n--) {
*dest++ = *src++;
}
return s1;
}
#endif
void *memmove(void * restrict s1, const void * restrict s2, size_t n) {
char *dest = (char*)s1;
const char *src = (const char*)s2;
if (dest <= src) {
while (n--) {
*dest++ = *src++;
}
} else {
while (n--) {
dest[n] = src[n];
}
}
return s1;
}

36
src/libc/string/memset.c Normal file
View file

@ -0,0 +1,36 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// void *memset(void *s, int c, size_t n);
///////////////////////////////////////
// Exports to apps:
// memset
#include <stddef.h>
#include <pblibc_assembly.h>
#include <pblibc_private.h>
#if !MEMSET_IMPLEMENTED_IN_ASSEMBLY
void *memset(void *s, int c, size_t n) {
unsigned char *p = (unsigned char*)s;
while (n--) {
*p++ = (unsigned char)c;
}
return s;
}
#endif

49
src/libc/string/strcat.c Normal file
View file

@ -0,0 +1,49 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// char *strcat(char *s1, const char *s2);
// char *strncat(char *s1, const char *s2, size_t n);
///////////////////////////////////////
// Exports to apps:
// strcat, strncat
///////////////////////////////////////
// Notes:
// Tuned for code size.
#include <string.h>
#include <pblibc_private.h>
char *strcat(char * restrict s1, const char * restrict s2) {
char *rc = s1;
s1 += strlen(s1);
strcpy(s1, s2);
return rc;
}
char *strncat(char * restrict s1, const char * restrict s2, size_t n) {
char * rc = s1;
s1 += strlen(s1);
size_t strn = strlen(s2);
if (strn > n) {
strn = n;
}
memcpy(s1, s2, strn);
s1[strn] = '\0';
return rc;
}

43
src/libc/string/strchr.c Normal file
View file

@ -0,0 +1,43 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// char *strchr(const char *s1, int c);
// char *strrchr(const char *s1, int c);
///////////////////////////////////////
// Notes:
// Tuned for code size.
#include <stddef.h>
#include <string.h>
#include <pblibc_private.h>
char *strchr(const char *s, int c) {
size_t n = strlen(s) + 1;
return memchr(s, c, n);
}
char *strrchr(const char *s, int c) {
char ch = (char)c;
size_t i = strlen(s);
do {
if (s[i] == ch) {
return (char*)&s[i];
}
} while (i--);
return NULL;
}

43
src/libc/string/strcmp.c Normal file
View file

@ -0,0 +1,43 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// int strcmp(const char *s1, const char *s2);
// int strncmp(const char *s1, const char *s2, size_t n);
///////////////////////////////////////
// Exports to apps:
// strcmp, strncmp
///////////////////////////////////////
// Notes:
// Tuned for code size.
#include <stddef.h>
#include <string.h>
#include <pblibc_private.h>
int strcmp(const char *s1, const char *s2) {
size_t n = strlen(s1) + 1;
return memcmp(s1, s2, n);
}
int strncmp(const char *s1, const char *s2, size_t n) {
size_t strn = strlen(s1) + 1;
if (strn < n) {
n = strn;
}
return memcmp(s1, s2, n);
}

48
src/libc/string/strcpy.c Normal file
View file

@ -0,0 +1,48 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// char *strcpy(char *s1, const char *s2);
// char *strncpy(char *s1, const char *s2, size_t n);
///////////////////////////////////////
// Exports to apps:
// strcpy, strncpy
///////////////////////////////////////
// Notes:
// Tuned for code size.
#include <stddef.h>
#include <string.h>
#include <pblibc_private.h>
char *strcpy(char * restrict s1, const char * restrict s2) {
size_t n = strlen(s2) + 1;
return memcpy(s1, s2, n);
}
char *strncpy(char * restrict s1, const char * restrict s2, size_t n) {
char *rc = s1;
size_t strn = strlen(s2) + 1;
if (strn > n) {
strn = n;
}
memcpy(s1, s2, strn);
if (n > strn) {
memset(s1 + strn, '\0', n - strn);
}
return rc;
}

42
src/libc/string/strlen.c Normal file
View file

@ -0,0 +1,42 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// size_t strlen(const char *s);
// size_t strnlen(const char *s, size_t maxlen);
///////////////////////////////////////
// Exports to apps:
// strlen
#include <stddef.h>
#include <pblibc_private.h>
size_t strlen(const char *s) {
size_t len = 0;
while (*s++) {
len++;
}
return len;
}
size_t strnlen(const char *s, size_t maxlen) {
size_t len = 0;
while (*s++ && maxlen--) {
len++;
}
return len;
}

58
src/libc/string/strspn.c Normal file
View file

@ -0,0 +1,58 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// size_t strcspn(const char *s1, const char *s2);
// size_t strspn(const char *s1, const char *s2);
#include <stddef.h>
#include <string.h>
#include <pblibc_private.h>
size_t strcspn(const char *s1, const char *s2) {
size_t len = 0;
const char *p;
while (s1[len]) {
p = s2;
while (*p) {
if (s1[len] == *p++) {
return len;
}
}
len++;
}
return len;
}
size_t strspn(const char *s1, const char *s2) {
size_t len = 0;
const char *p;
while (s1[len]) {
p = s2;
while (*p) {
if (s1[len] == *p) {
break;
}
p++;
}
if (!*p) {
return len;
}
len++;
}
return len;
}

37
src/libc/string/strstr.c Normal file
View file

@ -0,0 +1,37 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// char *strstr(const char *s1, const char *s2);
///////////////////////////////////////
// Notes:
// Tuned for code size.
#include <stddef.h>
#include <string.h>
#include <pblibc_private.h>
char *strstr(const char *s1, const char *s2) {
size_t len = strlen(s2);
while (*s1) {
if (strncmp(s1, s2, len) == 0) {
return (char*)s1;
}
s1++;
}
return NULL;
}

88
src/libc/string/strtol.c Normal file
View file

@ -0,0 +1,88 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// long int strtol(const char *nptr, char **endptr, int base);
#include <ctype.h>
#include <string.h>
#include <stdint.h>
#include <limits.h>
#include <stdbool.h>
#include <pblibc_private.h>
intmax_t strtoX_core(const char * restrict nptr, char ** restrict endptr, int base, bool do_errors,
intmax_t max, intmax_t min) {
intmax_t value = 0;
int sign = 1;
while (isspace((int)*nptr)) {
nptr++;
}
switch (*nptr) {
case '+': sign = 1; nptr++; break;
case '-': sign = -1; nptr++; break;
}
if (nptr[0] == '0' && nptr[1] == 'x' && (base == 0 || base == 16)) {
base = 16;
nptr += 2;
} else if (nptr[0] == '0' && (base == 0 || base == 8)) {
base = 8;
nptr++;
} else if (base == 0) {
base = 10;
}
// We break on '\0' anyways
for (;;) {
char ch = *nptr;
if (ch >= '0' && ch <= '9') {
ch = ch - '0';
} else if (ch >= 'A' && ch <= 'Z') {
ch = ch - 'A' + 10;
} else if (ch >= 'a' && ch <= 'z') {
ch = ch - 'a' + 10;
} else { // This will catch '\0'
break;
}
if (ch >= base) {
break;
}
value *= base;
value += ch;
if (do_errors) {
if ((sign > 0) && (value > max)) {
value = max;
} else if ((sign < 0) && (-value < min)) {
value = -min;
}
}
nptr++;
}
if (endptr) {
*endptr = (char*)nptr;
}
return value * sign;
}
long int strtol(const char * restrict nptr, char ** restrict endptr, int base) {
// INT_* because unit tests
return strtoX_core(nptr, endptr, base, true, INT_MAX, INT_MIN);
}
unsigned long int strtoul(const char * restrict nptr, char ** restrict endptr, int base) {
// UINT_* because unit tests
return strtoX_core(nptr, endptr, base, true, UINT_MAX, 0);
}

27
src/libc/stub/setlocale.c Normal file
View file

@ -0,0 +1,27 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////
// Implements:
// char *setlocale(int category, const char *locale);
///////////////////////////////////////
// Notes:
// Simplicity references this, but we don't want to implement it.
#include <stddef.h>
char *setlocale(int category, const char *locale) {
return NULL;
}

24
src/libc/wscript Normal file
View file

@ -0,0 +1,24 @@
import waftools.gitinfo
def build(bld):
# Collect all the files in the libc directory
source_dirs = ['string', 'math', 'stub']
sources = sum([bld.path.ant_glob('%s/*.[csS]' % d) for d in source_dirs], [])
sources.extend(bld.path.ant_glob('*.[csS]'))
def build_libc(target, env):
# Build the libc directory using firmware environment
bld.stlib(source=sources,
target=target,
includes=['include', '.'],
export_includes='include',
cflags=['-fno-lto'],
use='',
env=env.derive())
bld(export_includes=['include'], name='pblibc_includes')
build_libc('pblibc', bld.env)
build_libc('pblibc-cm0', bld.all_envs['cortex-m0'])
# vim:filetype=python