From dbd10db051466bd78e62154afca68acd3b984fa9 Mon Sep 17 00:00:00 2001 From: flysand7 Date: Fri, 28 Jul 2023 00:04:24 +1100 Subject: [PATCH] Underscore prefix TinyRT definitions --- build.py | 8 ++-- src/impl/stdlib-program/program.c | 6 +-- src/linux/tinyrt.c | 61 +++++++++++++------------ src/tinyrt.h | 74 +++++++++++++++---------------- 4 files changed, 74 insertions(+), 75 deletions(-) diff --git a/build.py b/build.py index b860e5c..8f24b2d 100755 --- a/build.py +++ b/build.py @@ -97,7 +97,7 @@ try: if is_defined: tinyrt_apis.append(api_name) is_defined_int = 1 if is_defined else 0 - tinyrt_header_file.write(f'#define {api_name.upper()} {is_defined_int}\n') + tinyrt_header_file.write(f'#define _{api_name.upper()} {is_defined_int}\n') except Exception as error: print(f" -> [ERROR] writing to file '{tinyrt_header_path}'") print(f" * {error}") @@ -206,7 +206,6 @@ def archive(srcs, out): sys.exit(code) - # Ciabatta build spec if not os.path.exists('lib'): os.mkdir('lib') @@ -218,7 +217,8 @@ p = os.path.join assemble(p('src', 'linux', 'crt-entry.asm'), p('bin', 'crt-entry.o')) compile([p('src', 'linux', 'crt-ctors.c')], p('bin', 'crt-ctors.o'), '-fpic -c') compile([p('src', 'ciabatta.c')], p('bin', 'ciabatta.o'), '-fpic -c') -archive([p('bin', 'ciabatta.o'), p('bin', 'crt-ctors.o'), p('bin', 'crt-entry.o')], p('lib', lib_file)) +archive([p('bin', 'crt-ctors.o'), p('bin', 'crt-entry.o')], p('lib', crt_file)) +archive([p('bin', 'ciabatta.o'), ], p('lib', lib_file)) if args.test: - compile([args.test, p('lib', lib_file)], 'a', '-pie') + compile([args.test, p('lib', lib_file), p('lib', crt_file)], 'a', '-pie') diff --git a/src/impl/stdlib-program/program.c b/src/impl/stdlib-program/program.c index e510374..97aa470 100644 --- a/src/impl/stdlib-program/program.c +++ b/src/impl/stdlib-program/program.c @@ -37,11 +37,11 @@ noreturn void exit(int code) { } // TODO(bumbread): flush all the unflushed file streams // TODO(bumbread): close all file streams and delete temporary files - rt_program_exit(code); + _rt_program_exit(code); } noreturn void _Exit(int code) { - rt_program_exit(code); + _rt_program_exit(code); } noreturn void quick_exit(int code) { @@ -49,6 +49,6 @@ noreturn void quick_exit(int code) { void (*handler)(void) = at_quick_exit_handlers[i]; handler(); } - rt_program_exit(code); + _rt_program_exit(code); } diff --git a/src/linux/tinyrt.c b/src/linux/tinyrt.c index 2073aa5..23bdee1 100644 --- a/src/linux/tinyrt.c +++ b/src/linux/tinyrt.c @@ -1,61 +1,60 @@ // See src/tinyrt.h file for the interface this file implements -static RT_Status rt_file_open(RT_File *file, char *name, int rt_flags) { - if((rt_flags & 0x3) == 0) { - return RT_ERROR_BAD_PARAM; +static _RT_Status _rt_file_open(_RT_File *file, char *name, int _rt_flags) { + if((_rt_flags & 0x3) == 0) { + return _RT_ERROR_BAD_PARAM; } int mode = 0; int flags = 0; - if((rt_flags & 0x03) == 0x03) mode = O_RDWR; - else if(rt_flags & RT_FILE_READ) mode = O_RDONLY; - else if(rt_flags & RT_FILE_WRITE) mode = O_RDWR; - if(rt_flags & RT_FILE_CREATE) flags |= O_CREAT; - if(rt_flags & RT_FILE_EXCLUSIVE) flags |= O_EXCL; - if(rt_flags & RT_FILE_TRUNCATE) flags |= O_TRUNC; + if((_rt_flags & 0x03) == 0x03) mode = O_RDWR; + else if(_rt_flags & _RT_FILE_READ) mode = O_RDONLY; + else if(_rt_flags & _RT_FILE_WRITE) mode = O_RDWR; + if(_rt_flags & _RT_FILE_CREATE) flags |= O_CREAT; + if(_rt_flags & _RT_FILE_EXCLUSIVE) flags |= O_EXCL; + if(_rt_flags & _RT_FILE_TRUNCATE) flags |= O_TRUNC; i64 fd = syscall_open(name, flags, mode); - if(-fd == EACCES) return RT_STATUS_FILE_ACCESS; - if(-fd == EEXIST) return RT_STATUS_FILE_EXISTS; - if(-fd == ENOENT) return RT_STATUS_FILE_NOT_EXISTS; - if(-fd == EINVAL) return RT_ERROR_BAD_PARAM; - if(-fd == EISDIR) return RT_STATUS_FILE_DIRECTORY; + if(-fd == EACCES) return _RT_STATUS_FILE_ACCESS; + if(-fd == EEXIST) return _RT_STATUS_FILE_EXISTS; + if(-fd == ENOENT) return _RT_STATUS_FILE_NOT_EXISTS; + if(-fd == EINVAL) return _RT_ERROR_BAD_PARAM; + if(-fd == EISDIR) return _RT_STATUS_FILE_DIRECTORY; // I'm too lazy to fill in the rest so lets leave it at that for now - if(fd < 0) return RT_STATUS_FILE_IO_ERROR; + if(fd < 0) return _RT_STATUS_FILE_IO_ERROR; file->fd = (u64)fd; - file->flags = rt_flags; - return RT_STATUS_OK; + file->flags = _rt_flags; + return _RT_STATUS_OK; } -static RT_Status rt_file_read(u64 size, void *buffer, RT_File *from, u64 *out_bytes_read) { +static _RT_Status _rt_file_read(u64 size, void *buffer, _RT_File *from, u64 *out_bytes_read) { i64 bytes_read = syscall_read(from->fd, buffer, size); if(bytes_read == 0) { - return RT_STATUS_FILE_EOF; + return _RT_STATUS_FILE_EOF; } if(bytes_read < 0) { - return RT_STATUS_FILE_IO_ERROR; + return _RT_STATUS_FILE_IO_ERROR; } *out_bytes_read = bytes_read; - return RT_STATUS_OK; + return _RT_STATUS_OK; } -static RT_Status rt_file_write(RT_File *to, u64 size, void *buffer, u64 *out_bytes_written) { - // Call the syscall +static _RT_Status _rt_file_write(_RT_File *to, u64 size, void *buffer, u64 *out_bytes_written) { i64 status = syscall_write(to->fd, buffer, size); - if(-status == EBADF) return RT_ERROR_BAD_PARAM; - if(-status == EIO) return RT_STATUS_FILE_IO_ERROR; - if(-status > 0) return RT_STATUS_FILE_IO_ERROR; + if(-status == EBADF) return _RT_ERROR_BAD_PARAM; + if(-status == EIO) return _RT_STATUS_FILE_IO_ERROR; + if(-status > 0) return _RT_STATUS_FILE_IO_ERROR; *out_bytes_written = status; - return RT_STATUS_OK; + return _RT_STATUS_OK; } -static RT_Status rt_file_close(RT_File *file) { +static _RT_Status _rt_file_close(_RT_File *file) { i64 result = syscall_close(file->fd); if(result < 0) { - return RT_STATUS_FILE_IO_ERROR; + return _RT_STATUS_FILE_IO_ERROR; } - return RT_STATUS_OK; + return _RT_STATUS_OK; } -static noreturn void rt_program_exit(int code) { +static noreturn void _rt_program_exit(int code) { syscall_exit(code); } diff --git a/src/tinyrt.h b/src/tinyrt.h index 2df0990..1171b0c 100644 --- a/src/tinyrt.h +++ b/src/tinyrt.h @@ -2,65 +2,65 @@ #pragma once // Common errors -#define RT_STATUS_OK 0 // No errors -#define RT_ERROR_NOT_IMPLEMENTED -1 // Function not implemented -#define RT_ERROR_BAD_PARAM -2 // One of the function parameters was wrong +#define _RT_STATUS_OK 0 // No errors +#define _RT_ERROR_NOT_IMPLEMENTED -1 // Function not implemented +#define _RT_ERROR_BAD_PARAM -2 // One of the function parameters was wrong // File API errors -#define RT_STATUS_FILE_ACCESS 1 // No access to the file -#define RT_STATUS_FILE_NO_SPACE 2 // Storage device has no space for the file -#define RT_STATUS_FILE_EXISTS 3 // File exists when shouldn't -#define RT_STATUS_FILE_NOT_EXISTS 4 // File doesn't exist when should -#define RT_STATUS_FILE_DIRECTORY 5 // The file was a directory when shouldn't've been -#define RT_STATUS_FILE_NOT_DIRECTORY 6 // The file wasn't a directory when should've been -#define RT_STATUS_FILE_NAME_TOO_LONG 7 // The filename was too long for the Filesystem -#define RT_STATUS_FILE_LOOP 8 // Too many symlinks followed or a symlink encountered when expected none -#define RT_STATUS_FILE_BUSY 9 // The device is busy if exclusive access is requested -#define RT_STATUS_FILE_TOO_MANY_OPEN 10 // Too many open files in the process -#define RT_STATUS_FILE_NO_MEMORY 11 // No kernel memory or user limit on memory allocation exceeded -#define RT_STATUS_FILE_IO_ERROR 12 // I/O error -#define RT_STATUS_FILE_BAD_FILE 13 // Bad file handle -#define RT_STATUS_FILE_EOF 14 // Read operation reached the end-of-file +#define _RT_STATUS_FILE_ACCESS 1 // No access to the file +#define _RT_STATUS_FILE_NO_SPACE 2 // Storage device has no space for the file +#define _RT_STATUS_FILE_EXISTS 3 // File exists when shouldn't +#define _RT_STATUS_FILE_NOT_EXISTS 4 // File doesn't exist when should +#define _RT_STATUS_FILE_DIRECTORY 5 // The file was a directory when shouldn't've been +#define _RT_STATUS_FILE_NOT_DIRECTORY 6 // The file wasn't a directory when should've been +#define _RT_STATUS_FILE_NAME_TOO_LONG 7 // The filename was too long for the Filesystem +#define _RT_STATUS_FILE_LOOP 8 // Too many symlinks followed or a symlink encountered when expected none +#define _RT_STATUS_FILE_BUSY 9 // The device is busy if exclusive access is requested +#define _RT_STATUS_FILE_TOO_MANY_OPEN 10 // Too many open files in the process +#define _RT_STATUS_FILE_NO_MEMORY 11 // No kernel memory or user limit on memory allocation exceeded +#define _RT_STATUS_FILE_IO_ERROR 12 // I/O error +#define _RT_STATUS_FILE_BAD_FILE 13 // Bad file handle +#define _RT_STATUS_FILE_EOF 14 // Read operation reached the end-of-file // File API flags -#define RT_FILE_READ 0x01 -#define RT_FILE_WRITE 0x02 -#define RT_FILE_CREATE 0x04 -#define RT_FILE_EXCLUSIVE 0x08 -#define RT_FILE_TRUNCATE 0x10 +#define _RT_FILE_READ 0x01 +#define _RT_FILE_WRITE 0x02 +#define _RT_FILE_CREATE 0x04 +#define _RT_FILE_EXCLUSIVE 0x08 +#define _RT_FILE_TRUNCATE 0x10 -typedef i32 RT_Status; +typedef i32 _RT_Status; // API implementation flags (managed and used by the layers on top of tinyrt) static bool _rt_api_file; static bool _rt_api_tmpfile; // Initialization & termination of minirt -static RT_Status rt_init(); -static RT_Status rt_deinit(); +static _RT_Status _rt_init(); +static _RT_Status _rt_deinit(); // Program API -#if RT_API_PROGRAM == 1 - static noreturn void rt_program_exit(int code); +#if _RT_API_PROGRAM == 1 + static noreturn void _rt_program_exit(int code); #endif -#if RT_API_ENVIRONMENT == 1 - static RT_Status rt_shell_exec(char const *cmd); - static RT_Status rt_env_get(char const *name); +#if _RT_API_ENVIRONMENT == 1 + static _RT_Status _rt_shell_exec(char const *cmd); + static _RT_Status _rt_env_get(char const *name); #endif // File API -#if RT_API_FILE == 1 - struct RT_File typedef RT_File; - struct RT_File { +#if _RT_API_FILE == 1 + struct _RT_File typedef _RT_File; + struct _RT_File { union { void *handle; u64 fd; }; i32 flags; }; - static RT_Status rt_file_open(RT_File *file, char *name, int flags); - static RT_Status rt_file_read(u64 size, void *buffer, RT_File *from, u64 *out_bytes_read); - static RT_Status rt_file_write(RT_File *to, u64 size, void *buffer, u64 *out_bytes_written); - static RT_Status rt_file_close(RT_File *file); + static _RT_Status _rt_file_open(_RT_File *file, char *name, int flags); + static _RT_Status _rt_file_read(u64 size, void *buffer, _RT_File *from, u64 *out_bytes_read); + static _RT_Status _rt_file_write(_RT_File *to, u64 size, void *buffer, u64 *out_bytes_written); + static _RT_Status _rt_file_close(_RT_File *file); #endif