Underscore prefix TinyRT definitions

This commit is contained in:
flysand7 2023-07-28 00:04:24 +11:00
parent 06e1057ad2
commit dbd10db051
4 changed files with 74 additions and 75 deletions

View File

@ -97,7 +97,7 @@ try:
if is_defined: if is_defined:
tinyrt_apis.append(api_name) tinyrt_apis.append(api_name)
is_defined_int = 1 if is_defined else 0 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: except Exception as error:
print(f" -> [ERROR] writing to file '{tinyrt_header_path}'") print(f" -> [ERROR] writing to file '{tinyrt_header_path}'")
print(f" * {error}") print(f" * {error}")
@ -206,7 +206,6 @@ def archive(srcs, out):
sys.exit(code) sys.exit(code)
# Ciabatta build spec # Ciabatta build spec
if not os.path.exists('lib'): if not os.path.exists('lib'):
os.mkdir('lib') os.mkdir('lib')
@ -218,7 +217,8 @@ p = os.path.join
assemble(p('src', 'linux', 'crt-entry.asm'), p('bin', 'crt-entry.o')) 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', 'linux', 'crt-ctors.c')], p('bin', 'crt-ctors.o'), '-fpic -c')
compile([p('src', 'ciabatta.c')], p('bin', 'ciabatta.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: 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')

View File

@ -37,11 +37,11 @@ noreturn void exit(int code) {
} }
// TODO(bumbread): flush all the unflushed file streams // TODO(bumbread): flush all the unflushed file streams
// TODO(bumbread): close all file streams and delete temporary files // TODO(bumbread): close all file streams and delete temporary files
rt_program_exit(code); _rt_program_exit(code);
} }
noreturn void _Exit(int code) { noreturn void _Exit(int code) {
rt_program_exit(code); _rt_program_exit(code);
} }
noreturn void quick_exit(int 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]; void (*handler)(void) = at_quick_exit_handlers[i];
handler(); handler();
} }
rt_program_exit(code); _rt_program_exit(code);
} }

View File

@ -1,61 +1,60 @@
// See src/tinyrt.h file for the interface this file implements // 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) { static _RT_Status _rt_file_open(_RT_File *file, char *name, int _rt_flags) {
if((rt_flags & 0x3) == 0) { if((_rt_flags & 0x3) == 0) {
return RT_ERROR_BAD_PARAM; return _RT_ERROR_BAD_PARAM;
} }
int mode = 0; int mode = 0;
int flags = 0; int flags = 0;
if((rt_flags & 0x03) == 0x03) mode = O_RDWR; if((_rt_flags & 0x03) == 0x03) mode = O_RDWR;
else if(rt_flags & RT_FILE_READ) mode = O_RDONLY; else if(_rt_flags & _RT_FILE_READ) mode = O_RDONLY;
else if(rt_flags & RT_FILE_WRITE) mode = O_RDWR; else if(_rt_flags & _RT_FILE_WRITE) mode = O_RDWR;
if(rt_flags & RT_FILE_CREATE) flags |= O_CREAT; if(_rt_flags & _RT_FILE_CREATE) flags |= O_CREAT;
if(rt_flags & RT_FILE_EXCLUSIVE) flags |= O_EXCL; if(_rt_flags & _RT_FILE_EXCLUSIVE) flags |= O_EXCL;
if(rt_flags & RT_FILE_TRUNCATE) flags |= O_TRUNC; if(_rt_flags & _RT_FILE_TRUNCATE) flags |= O_TRUNC;
i64 fd = syscall_open(name, flags, mode); i64 fd = syscall_open(name, flags, mode);
if(-fd == EACCES) return RT_STATUS_FILE_ACCESS; if(-fd == EACCES) return _RT_STATUS_FILE_ACCESS;
if(-fd == EEXIST) return RT_STATUS_FILE_EXISTS; if(-fd == EEXIST) return _RT_STATUS_FILE_EXISTS;
if(-fd == ENOENT) return RT_STATUS_FILE_NOT_EXISTS; if(-fd == ENOENT) return _RT_STATUS_FILE_NOT_EXISTS;
if(-fd == EINVAL) return RT_ERROR_BAD_PARAM; if(-fd == EINVAL) return _RT_ERROR_BAD_PARAM;
if(-fd == EISDIR) return RT_STATUS_FILE_DIRECTORY; 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 // 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->fd = (u64)fd;
file->flags = rt_flags; file->flags = _rt_flags;
return RT_STATUS_OK; 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); i64 bytes_read = syscall_read(from->fd, buffer, size);
if(bytes_read == 0) { if(bytes_read == 0) {
return RT_STATUS_FILE_EOF; return _RT_STATUS_FILE_EOF;
} }
if(bytes_read < 0) { if(bytes_read < 0) {
return RT_STATUS_FILE_IO_ERROR; return _RT_STATUS_FILE_IO_ERROR;
} }
*out_bytes_read = bytes_read; *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) { static _RT_Status _rt_file_write(_RT_File *to, u64 size, void *buffer, u64 *out_bytes_written) {
// Call the syscall
i64 status = syscall_write(to->fd, buffer, size); i64 status = syscall_write(to->fd, buffer, size);
if(-status == EBADF) return RT_ERROR_BAD_PARAM; if(-status == EBADF) return _RT_ERROR_BAD_PARAM;
if(-status == EIO) return RT_STATUS_FILE_IO_ERROR; if(-status == EIO) return _RT_STATUS_FILE_IO_ERROR;
if(-status > 0) return RT_STATUS_FILE_IO_ERROR; if(-status > 0) return _RT_STATUS_FILE_IO_ERROR;
*out_bytes_written = status; *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); i64 result = syscall_close(file->fd);
if(result < 0) { 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); syscall_exit(code);
} }

View File

@ -2,65 +2,65 @@
#pragma once #pragma once
// Common errors // Common errors
#define RT_STATUS_OK 0 // No errors #define _RT_STATUS_OK 0 // No errors
#define RT_ERROR_NOT_IMPLEMENTED -1 // Function not implemented #define _RT_ERROR_NOT_IMPLEMENTED -1 // Function not implemented
#define RT_ERROR_BAD_PARAM -2 // One of the function parameters was wrong #define _RT_ERROR_BAD_PARAM -2 // One of the function parameters was wrong
// File API errors // File API errors
#define RT_STATUS_FILE_ACCESS 1 // No access to the 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_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_EXISTS 3 // File exists when shouldn't
#define RT_STATUS_FILE_NOT_EXISTS 4 // File doesn't exist when should #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_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_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_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_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_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_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_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_IO_ERROR 12 // I/O error
#define RT_STATUS_FILE_BAD_FILE 13 // Bad file handle #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_EOF 14 // Read operation reached the end-of-file
// File API flags // File API flags
#define RT_FILE_READ 0x01 #define _RT_FILE_READ 0x01
#define RT_FILE_WRITE 0x02 #define _RT_FILE_WRITE 0x02
#define RT_FILE_CREATE 0x04 #define _RT_FILE_CREATE 0x04
#define RT_FILE_EXCLUSIVE 0x08 #define _RT_FILE_EXCLUSIVE 0x08
#define RT_FILE_TRUNCATE 0x10 #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) // API implementation flags (managed and used by the layers on top of tinyrt)
static bool _rt_api_file; static bool _rt_api_file;
static bool _rt_api_tmpfile; static bool _rt_api_tmpfile;
// Initialization & termination of minirt // Initialization & termination of minirt
static RT_Status rt_init(); static _RT_Status _rt_init();
static RT_Status rt_deinit(); static _RT_Status _rt_deinit();
// Program API // Program API
#if RT_API_PROGRAM == 1 #if _RT_API_PROGRAM == 1
static noreturn void rt_program_exit(int code); static noreturn void _rt_program_exit(int code);
#endif #endif
#if RT_API_ENVIRONMENT == 1 #if _RT_API_ENVIRONMENT == 1
static RT_Status rt_shell_exec(char const *cmd); static _RT_Status _rt_shell_exec(char const *cmd);
static RT_Status rt_env_get(char const *name); static _RT_Status _rt_env_get(char const *name);
#endif #endif
// File API // File API
#if RT_API_FILE == 1 #if _RT_API_FILE == 1
struct RT_File typedef RT_File; struct _RT_File typedef _RT_File;
struct RT_File { struct _RT_File {
union { union {
void *handle; void *handle;
u64 fd; u64 fd;
}; };
i32 flags; i32 flags;
}; };
static RT_Status rt_file_open(RT_File *file, char *name, int 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_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_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_close(_RT_File *file);
#endif #endif