diff options
Diffstat (limited to 'source')
| -rw-r--r-- | source/allocate.c | 6 | ||||
| -rw-r--r-- | source/config.c | 162 | ||||
| -rw-r--r-- | source/env.c | 3 | ||||
| -rw-r--r-- | source/io.c | 2 | ||||
| -rw-r--r-- | source/kpl.c | 9 | ||||
| -rw-r--r-- | source/memory.c | 4 |
6 files changed, 179 insertions, 7 deletions
diff --git a/source/allocate.c b/source/allocate.c new file mode 100644 index 0000000..e242562 --- /dev/null +++ b/source/allocate.c @@ -0,0 +1,6 @@ + +#include "kpl.h" + +extern inline void *kpl_object_allocate(kpl_thread *thread, int32_t size); + +extern inline void kpl_object_return(kpl_env *env, void *object); diff --git a/source/config.c b/source/config.c new file mode 100644 index 0000000..f1aabf8 --- /dev/null +++ b/source/config.c @@ -0,0 +1,162 @@ + +#include "kpl.h" + +typedef kpl_exit_code kpl_config_option_parse_fn(kpl_env *env, int32_t arg_index, char *argv[]); + +typedef struct { + int32_t flags; + char match, *help_option_argument, *help_message; + kpl_config_option_parse_fn *parse_fn; +} kpl_config_option; + +#define KPL_CONFIG_MATCH(CHAR) (CHAR - 'a') + +static kpl_exit_code kpl_config_option_parse_eval(kpl_env *env, int32_t arg_index, char *argv[]) { + env->kpl_entry = argv[arg_index]; + env->flags |= KPL_ENV_FLAG_RUN_EVAL; + return KPL_EXIT_CODE_SUCCESS; +} + +static const kpl_config_option config_options[]; + +static kpl_exit_code kpl_config_option_parse_help(kpl_env *env, int32_t arg_index, char *argv[]) { + (void) env; + (void) arg_index; + printf(KPL_COLOR(LIGHT_GREEN) "Kevin's Programming Language" KPL_COLOR(RESET) " (" KPL_COLOR(LIGHT_CYAN) + "KPL" KPL_COLOR(RESET) ") Version: " KPL_COLOR(LIGHT_MAGENTA) KPL_VERSION KPL_COLOR(RESET) + " Website: " KPL_COLOR(LIGHT_BLUE) KPL_WEBSITE KPL_COLOR(RESET) "\n"); + printf("Usage:" KPL_COLOR(BOLD) " %s " KPL_COLOR(RESET) KPL_COLOR(LIGHT_YELLOW) "OPTIONS" KPL_COLOR(RESET) + KPL_COLOR(BOLD) " [-r|-e STRING|FILE" KPL_FILE_EXTENSION KPL_COLOR(RESET) "]\n", argv[0]); + for (int32_t option_index = KPL_CONFIG_MATCH('a'); option_index < KPL_CONFIG_MATCH('z'); option_index++) { + const kpl_config_option *option = &config_options[option_index]; + if (!option->match) + continue; + printf("\t" KPL_COLOR(LIGHT_YELLOW) "-%c" KPL_COLOR(RESET), option->match); + if (option->help_option_argument) + printf(KPL_COLOR(GREEN) " [%s]" KPL_COLOR(RESET), option->help_option_argument); + printf(", %s\n", option->help_message); + } + return KPL_EXIT_CODE_HELP; +} + +static kpl_exit_code kpl_config_option_parse_io_queue(kpl_env *env, int32_t arg_index, char *argv[]) { + int32_t io_uring_queue_size_pow2 = atoi(argv[arg_index]); + if (io_uring_queue_size_pow2 < KPL_IO_URING_QUEUE_SIZE_POW2_MIN || + io_uring_queue_size_pow2 > KPL_IO_URING_QUEUE_SIZE_POW2_MAX) { + printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid power of two for io_uring, must be between %d and %d\n" + KPL_COLOR(RESET), KPL_IO_URING_QUEUE_SIZE_POW2_MIN, KPL_IO_URING_QUEUE_SIZE_POW2_MAX); + return KPL_EXIT_CODE_ERROR; + } + env->io_uring_queue_size_pow2 = io_uring_queue_size_pow2; + return KPL_EXIT_CODE_SUCCESS; +} + +static kpl_exit_code kpl_config_option_parse_repl(kpl_env *env, int32_t arg_index, char *argv[]) { + (void) arg_index; + (void) argv; + env->flags |= KPL_ENV_FLAG_RUN_REPL; + return KPL_EXIT_CODE_SUCCESS; +} + +static kpl_exit_code kpl_config_option_parse_thread_count(kpl_env *env, int32_t arg_index, char *argv[]) { + int32_t thread_count = atoi(argv[arg_index]); + if (thread_count < KPL_THEAD_THREAD_COUNT_MIN || thread_count > KPL_THEAD_THREAD_COUNT_MAX) { + printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid thread count, must be between %d and %d\n" KPL_COLOR(RESET), + KPL_THEAD_THREAD_COUNT_MIN, KPL_THEAD_THREAD_COUNT_MAX); + return KPL_EXIT_CODE_ERROR; + } + env->thread_count = thread_count; + return KPL_EXIT_CODE_SUCCESS; +} + +static kpl_exit_code kpl_config_option_parse_version(kpl_env *env, int32_t arg_index, char *argv[]) { + (void) env; + (void) arg_index; + (void) argv; + printf("kpl version " KPL_VERSION "\n"); + return KPL_EXIT_CODE_HELP; +} + +static const kpl_config_option config_options[] = { + [KPL_CONFIG_MATCH('a')] = {}, + [KPL_CONFIG_MATCH('e')] = { + .match = 'e', + .help_option_argument = "STRING", + .help_message = "Evaluate KPL String", + .parse_fn = kpl_config_option_parse_eval + }, + [KPL_CONFIG_MATCH('h')] = { + .match = 'h', + .help_message = "Show Help", + .parse_fn = kpl_config_option_parse_help + }, + [KPL_CONFIG_MATCH('i')] = { + .match = 'i', + .help_option_argument = "NUMBER", + .help_message = "Power Of Two For io_uring Queue Size", + .parse_fn = kpl_config_option_parse_io_queue + }, + [KPL_CONFIG_MATCH('r')] = { + .match = 'r', + .help_message = "Start KPL REPL", + .parse_fn = kpl_config_option_parse_repl + }, + [KPL_CONFIG_MATCH('t')] = { + .match = 't', + .help_option_argument = "NUMBER", + .help_message = "Number Of Threads To Use", + .parse_fn = kpl_config_option_parse_thread_count + }, + [KPL_CONFIG_MATCH('v')] = { + .match = 'v', + .help_message = "Show Version", + .parse_fn = kpl_config_option_parse_version + }, + [KPL_CONFIG_MATCH('z')] = {} +}; + +static void kpl_env_set_defaults(kpl_env *env) { + env->io_uring_queue_size_pow2 = KPL_IO_URING_QUEUE_SIZE_POW2_DEFAULT; + env->thread_count = KPL_THREAD_DETERIMINE_COUNT; +} + +kpl_exit_code kpl_env_config(kpl_env *env, int32_t argc, char *argv[]) { + kpl_env_set_defaults(env); + if (argc == KPL_ENV_CONFIG_NO_ARGS) + return KPL_EXIT_CODE_SUCCESS; + int32_t arg_index = 1, exit_code = KPL_EXIT_CODE_SUCCESS; + while (arg_index < argc) { + if (argv[arg_index][0] != '-') { + env->kpl_entry = argv[arg_index]; + return KPL_EXIT_CODE_SUCCESS; + } + if (argv[arg_index][1] && argv[arg_index][2]) { + printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid option %s\n" KPL_COLOR(RESET), argv[arg_index]); + return KPL_EXIT_CODE_ERROR; + } + if (argv[arg_index][1] == '-') { + arg_index++; + if (arg_index >= argc) { + printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid, no arguments after --\n" KPL_COLOR(RESET)); + return KPL_EXIT_CODE_ERROR; + } + env->kpl_entry = argv[arg_index]; + return KPL_EXIT_CODE_SUCCESS; + } + const kpl_config_option *option = &config_options[KPL_CONFIG_MATCH(argv[arg_index][1])]; + if (option->help_option_argument) { + arg_index++; + if (arg_index >= argc) { + printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid, option -%c requires argument\n" KPL_COLOR(RESET), + option->match); + return KPL_EXIT_CODE_ERROR; + } + } + if ((exit_code = option->parse_fn(env, arg_index, argv)) != KPL_EXIT_CODE_SUCCESS) + return exit_code; + if (env->flags & (KPL_ENV_FLAG_RUN_EVAL | KPL_ENV_FLAG_RUN_REPL)) + return KPL_EXIT_CODE_SUCCESS; + arg_index++; + } + return kpl_config_option_parse_help(env, 0, argv); +} diff --git a/source/env.c b/source/env.c index 4e4f463..050f95b 100644 --- a/source/env.c +++ b/source/env.c @@ -1,6 +1,3 @@ #include "kpl.h" -extern inline void *kpl_thread_object_allocate(kpl_thread *thread, int32_t size); - -extern inline void kpl_env_object_return(kpl_env *env, void *object); diff --git a/source/io.c b/source/io.c index 0cd8480..8f3db55 100644 --- a/source/io.c +++ b/source/io.c @@ -52,6 +52,7 @@ kpl_object_task *kpl_io_uring_get_completed(kpl_io_uring *io_uring) { cring_head++; __atomic_store_n(io_uring->cring_head, cring_head, __ATOMIC_RELEASE); io_uring->submitted_to_kernel--; + task->object_flags &= ~KPL_OBJECT_FLAGS_IO_PENDING; return task; } @@ -77,6 +78,7 @@ struct io_uring_sqe *kpl_io_uring_op(kpl_io_uring *io_uring, kpl_object_task *ta __atomic_store_n(io_uring->sring_tail, sring_tail, __ATOMIC_RELEASE); io_uring->added_to_queue++; memset(sqe, 0, sizeof(struct io_uring_sqe)); + task->object_flags |= KPL_OBJECT_FLAGS_IO_PENDING; sqe->user_data = (uint64_t) task; sqe->opcode = op; sqe->fd = fd; diff --git a/source/kpl.c b/source/kpl.c index 55ca994..d970eb3 100644 --- a/source/kpl.c +++ b/source/kpl.c @@ -5,7 +5,12 @@ #include "kpl.h" #ifdef KPL_MAIN -int32_t main(void) { - return 0; +int32_t main(int32_t argc, char *argv[]) { + int32_t exit_code = KPL_EXIT_CODE_SUCCESS; + kpl_env env = {}; + if ((exit_code = kpl_env_config(&env, argc, argv)) != KPL_EXIT_CODE_SUCCESS) + return exit_code; + // TODO + return exit_code; } #endif diff --git a/source/memory.c b/source/memory.c index a9bcc30..a9c3321 100644 --- a/source/memory.c +++ b/source/memory.c @@ -73,7 +73,7 @@ static void *kpl_memory_allocator_slab_bump(kpl_memory_allocator_slab *slab, int } static kpl_object *kpl_memory_allocator_slab_allocate(kpl_memory_allocator_slab_group *slab_group, - int32_t thread_id) { + uint16_t thread_id) { kpl_memory_allocator_slab *slab = slab_group->slab_tail; int32_t size = 1 << slab_group->slab_size_pow2; if (!slab || slab->buffer_used + size > slab->buffer_size) { @@ -93,7 +93,7 @@ static kpl_object *kpl_memory_allocator_slab_allocate(kpl_memory_allocator_slab_ return object; } -void *kpl_memory_allocator_allocate(kpl_memory_allocator *memory_allocator, int32_t size, int32_t thread_id) { +void *kpl_memory_allocator_allocate(kpl_memory_allocator *memory_allocator, int32_t size, uint16_t thread_id) { if (size < 1 << KPL_MEMORY_ALLOCATOR_MIN_ROUND_SIZE_POW2) return NULL; int32_t size_pow2 = kpl_memory_allocator_get_size_pow2(size); |
