From 4cc8e52052fb9e3eb4339ee555dd19daecab3f16 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Tue, 31 Mar 2020 12:04:53 +0100 Subject: [PATCH] Re-organisation + stdint.h and stddef.h --- CMakeLists.txt | 55 +++++- photon/libc/include/machine/_stdint.h | 243 ++++++++++++++++++++++++++ photon/libc/include/stddef.h | 21 +++ photon/libc/include/stdint.h | 53 ++++++ photon/libc/include/string.h | 16 ++ photon/libc/machine/i386/types.h | 0 photon/libc/machine/x86_64/types.h | 0 photon/libc/string/strlen.c | 11 ++ photon/temp.c | 3 - 9 files changed, 391 insertions(+), 11 deletions(-) create mode 100644 photon/libc/include/machine/_stdint.h create mode 100644 photon/libc/include/stddef.h create mode 100644 photon/libc/include/stdint.h create mode 100644 photon/libc/include/string.h create mode 100644 photon/libc/machine/i386/types.h create mode 100644 photon/libc/machine/x86_64/types.h create mode 100644 photon/libc/string/strlen.c delete mode 100644 photon/temp.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f490d8..ffb422e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,14 +1,53 @@ cmake_minimum_required(VERSION 3.5) project(photon) -file(GLOB_RECURSE photon_sources - ${CMAKE_SOURCE_DIR}/photon/*.c - ${CMAKE_SOURCE_DIR}/photon/*.h) -message(STATUS "Sources: " ${photon_sources}) +macro(subdirlist result curdir) + file(GLOB children RELATIVE ${curdir} ${curdir}/*) + set(dirlist "") + foreach (child ${children}) + if (IS_DIRECTORY ${curdir}/${child}) + list(APPEND dirlist ${child}) + endif() + endforeach() + set(${result} ${dirlist}) +endmacro() -add_library(photon_obj OBJECT ${photon_sources}) +subdirlist(machine_dirs ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/machine) -add_library(photon SHARED $) -add_library(photon_s STATIC $) +if (NOT PLATFORM) + set(PLATFORM "linux") +endif () -set_property(TARGET photon_obj PROPERTY POSITION_INDEPENDENT_CODE ON) +if (NOT MACHINE) + set(supported_machines "") + foreach (dir ${machine_dirs}) + set(supported_machines "${supported_machines}${dir} ") + endforeach (dir) + + message(FATAL_ERROR "No machine architecture specified.\n" + " Supported machines: ${supported_machines}\n" + " Ex. `cmake -DMACHINE=x86_64 `") +endif () + +if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${PLATFORM}) + message(FATAL_ERROR "Unsupported platform: ${PLATFORM}") +endif () + + +message(STATUS "Target platform: ${PLATFORM}") + +set(photon_libc_sources "") +set(generic_dirs string include) + +foreach (dir ${generic_dirs}) + file(GLOB_RECURSE dir_sources ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/${dir}/*.c) + set(photon_libc_sources ${photon_libc_sources} ${dir_sources}) +endforeach (dir) + +file(GLOB_RECURSE platform_sources ${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${PLATFORM}) +set(photon_libc_sources ${photon_libc_sources} ${platform_sources}) + +add_library(c STATIC ${photon_libc_sources}) +target_compile_options(c PRIVATE -ffreestanding -nostdlib -nostdinc) + +target_include_directories(c PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/include) diff --git a/photon/libc/include/machine/_stdint.h b/photon/libc/include/machine/_stdint.h new file mode 100644 index 0000000..7dcf5b1 --- /dev/null +++ b/photon/libc/include/machine/_stdint.h @@ -0,0 +1,243 @@ +#ifndef MACHINE__STDINT_H +#define MACHINE__STDINT_H + +#define __EXP(x) __##x##__ + +#if ( defined(__LONG_LONG_MAX__) && (__LONG_LONG_MAX__ > 0x7fffffff) ) \ + || ( defined(LLONG_MAX) && (LLONG_MAX > 0x7fffffff) ) +#define __have_longlong64 1 +#endif + +#if __EXP(LONG_MAX) > 0x7fffffff +#define __have_long64 1 +#elif __EXP(LONG_MAX) == 0x7fffffff && !defined(__SPU__) +#define __have_long32 1 +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __INT8_TYPE__ +typedef __INT8_TYPE__ __int8_t; +#ifdef __UINT8_TYPE__ +typedef __UINT8_TYPE__ __uint8_t; +#else +typedef unsigned __INT8_TYPE__ __uint8_t; +#endif +#define ___int8_t_defined 1 +#elif __EXP(SCHAR_MAX) == 0x7f +typedef signed char __int8_t ; +typedef unsigned char __uint8_t ; +#define ___int8_t_defined 1 +#endif + +#ifdef __INT16_TYPE__ +typedef __INT16_TYPE__ __int16_t; +#ifdef __UINT16_TYPE__ +typedef __UINT16_TYPE__ __uint16_t; +#else +typedef unsigned __INT16_TYPE__ __uint16_t; +#endif +#define ___int16_t_defined 1 +#elif __EXP(INT_MAX) == 0x7fff +typedef signed int __int16_t; +typedef unsigned int __uint16_t; +#define ___int16_t_defined 1 +#elif __EXP(SHRT_MAX) == 0x7fff +typedef signed short __int16_t; +typedef unsigned short __uint16_t; +#define ___int16_t_defined 1 +#elif __EXP(SCHAR_MAX) == 0x7fff +typedef signed char __int16_t; +typedef unsigned char __uint16_t; +#define ___int16_t_defined 1 +#endif + +#ifdef __INT32_TYPE__ +typedef __INT32_TYPE__ __int32_t; +#ifdef __UINT32_TYPE__ +typedef __UINT32_TYPE__ __uint32_t; +#else +typedef unsigned __INT32_TYPE__ __uint32_t; +#endif +#define ___int32_t_defined 1 +#elif __EXP(INT_MAX) == 0x7fffffffL +typedef signed int __int32_t; +typedef unsigned int __uint32_t; +#define ___int32_t_defined 1 +#elif __EXP(LONG_MAX) == 0x7fffffffL +typedef signed long __int32_t; +typedef unsigned long __uint32_t; +#define ___int32_t_defined 1 +#elif __EXP(SHRT_MAX) == 0x7fffffffL +typedef signed short __int32_t; +typedef unsigned short __uint32_t; +#define ___int32_t_defined 1 +#elif __EXP(SCHAR_MAX) == 0x7fffffffL +typedef signed char __int32_t; +typedef unsigned char __uint32_t; +#define ___int32_t_defined 1 +#endif + +#ifdef __INT64_TYPE__ +typedef __INT64_TYPE__ __int64_t; +#ifdef __UINT64_TYPE__ +typedef __UINT64_TYPE__ __uint64_t; +#else +typedef unsigned __INT64_TYPE__ __uint64_t; +#endif +#define ___int64_t_defined 1 +#elif __EXP(LONG_MAX) > 0x7fffffff +typedef signed long __int64_t; +typedef unsigned long __uint64_t; +#define ___int64_t_defined 1 + +/* GCC has __LONG_LONG_MAX__ */ +#elif defined(__LONG_LONG_MAX__) && (__LONG_LONG_MAX__ > 0x7fffffff) +typedef signed long long __int64_t; +typedef unsigned long long __uint64_t; +#define ___int64_t_defined 1 + +/* POSIX mandates LLONG_MAX in */ +#elif defined(LLONG_MAX) && (LLONG_MAX > 0x7fffffff) +typedef signed long long __int64_t; +typedef unsigned long long __uint64_t; +#define ___int64_t_defined 1 + +#elif __EXP(INT_MAX) > 0x7fffffff +typedef signed int __int64_t; +typedef unsigned int __uint64_t; +#define ___int64_t_defined 1 +#endif + +#ifdef __INT_LEAST8_TYPE__ +typedef __INT_LEAST8_TYPE__ __int_least8_t; +#ifdef __UINT_LEAST8_TYPE__ +typedef __UINT_LEAST8_TYPE__ __uint_least8_t; +#else +typedef unsigned __INT_LEAST8_TYPE__ __uint_least8_t; +#endif +#define ___int_least8_t_defined 1 +#elif defined(___int8_t_defined) +typedef __int8_t __int_least8_t; +typedef __uint8_t __uint_least8_t; +#define ___int_least8_t_defined 1 +#elif defined(___int16_t_defined) +typedef __int16_t __int_least8_t; +typedef __uint16_t __uint_least8_t; +#define ___int_least8_t_defined 1 +#elif defined(___int32_t_defined) +typedef __int32_t __int_least8_t; +typedef __uint32_t __uint_least8_t; +#define ___int_least8_t_defined 1 +#elif defined(___int64_t_defined) +typedef __int64_t __int_least8_t; +typedef __uint64_t __uint_least8_t; +#define ___int_least8_t_defined 1 +#endif + +#ifdef __INT_LEAST16_TYPE__ +typedef __INT_LEAST16_TYPE__ __int_least16_t; +#ifdef __UINT_LEAST16_TYPE__ +typedef __UINT_LEAST16_TYPE__ __uint_least16_t; +#else +typedef unsigned __INT_LEAST16_TYPE__ __uint_least16_t; +#endif +#define ___int_least16_t_defined 1 +#elif defined(___int16_t_defined) +typedef __int16_t __int_least16_t; +typedef __uint16_t __uint_least16_t; +#define ___int_least16_t_defined 1 +#elif defined(___int32_t_defined) +typedef __int32_t __int_least16_t; +typedef __uint32_t __uint_least16_t; +#define ___int_least16_t_defined 1 +#elif defined(___int64_t_defined) +typedef __int64_t __int_least16_t; +typedef __uint64_t __uint_least16_t; +#define ___int_least16_t_defined 1 +#endif + +#ifdef __INT_LEAST32_TYPE__ +typedef __INT_LEAST32_TYPE__ __int_least32_t; +#ifdef __UINT_LEAST32_TYPE__ +typedef __UINT_LEAST32_TYPE__ __uint_least32_t; +#else +typedef unsigned __INT_LEAST32_TYPE__ __uint_least32_t; +#endif +#define ___int_least32_t_defined 1 +#elif defined(___int32_t_defined) +typedef __int32_t __int_least32_t; +typedef __uint32_t __uint_least32_t; +#define ___int_least32_t_defined 1 +#elif defined(___int64_t_defined) +typedef __int64_t __int_least32_t; +typedef __uint64_t __uint_least32_t; +#define ___int_least32_t_defined 1 +#endif + +#ifdef __INT_LEAST64_TYPE__ +typedef __INT_LEAST64_TYPE__ __int_least64_t; +#ifdef __UINT_LEAST64_TYPE__ +typedef __UINT_LEAST64_TYPE__ __uint_least64_t; +#else +typedef unsigned __INT_LEAST64_TYPE__ __uint_least64_t; +#endif +#define ___int_least64_t_defined 1 +#elif defined(___int64_t_defined) +typedef __int64_t __int_least64_t; +typedef __uint64_t __uint_least64_t; +#define ___int_least64_t_defined 1 +#endif + +#if defined(__INTMAX_TYPE__) +typedef __INTMAX_TYPE__ __intmax_t; +#elif __have_longlong64 +typedef signed long long __intmax_t; +#else +typedef signed long __intmax_t; +#endif + +#if defined(__UINTMAX_TYPE__) +typedef __UINTMAX_TYPE__ __uintmax_t; +#elif __have_longlong64 +typedef unsigned long long __uintmax_t; +#else +typedef unsigned long __uintmax_t; +#endif + +#ifdef __INTPTR_TYPE__ +typedef __INTPTR_TYPE__ __intptr_t; +#ifdef __UINTPTR_TYPE__ +typedef __UINTPTR_TYPE__ __uintptr_t; +#else +typedef unsigned __INTPTR_TYPE__ __uintptr_t; +#endif +#elif defined(__PTRDIFF_TYPE__) +typedef __PTRDIFF_TYPE__ __intptr_t; +typedef unsigned __PTRDIFF_TYPE__ __uintptr_t; +#else +typedef long __intptr_t; +typedef unsigned long __uintptr_t; +#endif + +typedef __int8_t __int_fast8_t; +typedef __uint8_t __uint_fast8_t; + +typedef __int16_t __int_fast16_t; +typedef __uint16_t __uint_fast16_t; + +typedef __int32_t __int_fast32_t; +typedef __uint32_t __uint_fast32_t; + +typedef __int64_t __int_fast64_t; +typedef __uint64_t __uint_fast64_t; + +#undef __EXP + +#ifdef __cplusplus +} +#endif + +#endif /* _MACHINE__STDINT_H */ diff --git a/photon/libc/include/stddef.h b/photon/libc/include/stddef.h new file mode 100644 index 0000000..87509ea --- /dev/null +++ b/photon/libc/include/stddef.h @@ -0,0 +1,21 @@ +#ifndef STDDEF_H_ +#define STDDEF_H_ + +#include + +#define NULL ((void *)0) +#define offsetof(t, m) ((size_t)(&(t *)0)->m) + +#if defined(__cplusplus) +extern "C" { +#endif + +typedef __intptr_t ptrdiff_t; +typedef __uintptr_t size_t; +typedef int wchar_t; + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/photon/libc/include/stdint.h b/photon/libc/include/stdint.h new file mode 100644 index 0000000..361107c --- /dev/null +++ b/photon/libc/include/stdint.h @@ -0,0 +1,53 @@ +#ifndef STDINT_H_ +#define STDINT_H_ + +#include + +typedef __int8_t int8_t; +typedef __uint8_t uint8_t; + +typedef __int16_t int16_t; +typedef __uint16_t uint16_t; + +typedef __int32_t int32_t; +typedef __uint32_t uint32_t; + +typedef __int64_t int64_t; +typedef __uint64_t uint64_t; + +typedef __intptr_t intptr_t; +typedef __uintptr_t uintptr_t; + +typedef __int_least8_t int_least8_t; +typedef __uint_least8_t uint_least8_t; + +typedef __int_least16_t int_least16_t; +typedef __uint_least16_t uint_least16_t; + +typedef __int_least32_t int_least32_t; +typedef __uint_least32_t uint_least32_t; + +typedef __int_least64_t int_least64_t; +typedef __uint_least64_t uint_least64_t; + +typedef __int_fast8_t int_fast8_t; +typedef __uint_fast8_t uint_fast8_t; + +typedef __int_fast16_t int_fast16_t; +typedef __uint_fast16_t uint_fast16_t; + +typedef __int_fast32_t int_fast32_t; +typedef __uint_fast32_t uint_fast32_t; + +typedef __int_fast64_t int_fast64_t; +typedef __uint_fast64_t uint_fast64_t; + +typedef __intmax_t intmax_t; +typedef __uintmax_t uintmax_t; + +typedef __intptr_t intptr_t; +typedef __uintptr_t uintptr_t; + +typedef + +#endif diff --git a/photon/libc/include/string.h b/photon/libc/include/string.h new file mode 100644 index 0000000..631f22e --- /dev/null +++ b/photon/libc/include/string.h @@ -0,0 +1,16 @@ +#ifndef STRING_H_ +#define STRING_H_ + +#include + +#if defined(__cplusplus) +extern "C" { +#endif + +extern size_t strlen(const char *str); + +#if defined(__cplusplus) +} /* extern "C" */ +#endif + +#endif diff --git a/photon/libc/machine/i386/types.h b/photon/libc/machine/i386/types.h new file mode 100644 index 0000000..e69de29 diff --git a/photon/libc/machine/x86_64/types.h b/photon/libc/machine/x86_64/types.h new file mode 100644 index 0000000..e69de29 diff --git a/photon/libc/string/strlen.c b/photon/libc/string/strlen.c new file mode 100644 index 0000000..e03c5f4 --- /dev/null +++ b/photon/libc/string/strlen.c @@ -0,0 +1,11 @@ +#include + +size_t strlen(const char *str) +{ + const char *start = str; + while (*str) { + str++; + } + + return str - start; +} diff --git a/photon/temp.c b/photon/temp.c deleted file mode 100644 index 3bdd236..0000000 --- a/photon/temp.c +++ /dev/null @@ -1,3 +0,0 @@ -int temp_2() { - return 1; -}