diff options
| author | Cliff Click <cliffc@acm.org> | 2026-08-12 11:20:14 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-08-12 11:20:14 -0700 |
| commit | 1cb8279a64f4f79615ac8358927b11237df29bf2 (patch) | |
| tree | d057b4ee723a9d29e16f2c71959c0ae2773cdb63 /chapter25/src/main/c | |
| parent | 628452bb9f6137d433395fae5a9f5d780fb0f1e1 (diff) | |
| download | Simple-1cb8279a64f4f79615ac8358927b11237df29bf2.tar.gz Simple-1cb8279a64f4f79615ac8358927b11237df29bf2.zip | |
Real argc/argv args
Beef up BubbleSort error handling
Diffstat (limited to 'chapter25/src/main/c')
| -rw-r--r-- | chapter25/src/main/c/simple_crt.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/chapter25/src/main/c/simple_crt.c b/chapter25/src/main/c/simple_crt.c new file mode 100644 index 00000000..b79e6f88 --- /dev/null +++ b/chapter25/src/main/c/simple_crt.c @@ -0,0 +1,57 @@ +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +// Native process arguments converted to ordinary Simple array layouts. +// The outer array is mutable; every contained byte array is published as +// immutable and non-null to Simple code. +typedef struct SimpleBytes { + uint32_t len; + unsigned char bytes[]; +} SimpleBytes; + +typedef struct SimpleArgs { + uint32_t len; + uint32_t pad; + SimpleBytes *args[]; +} SimpleArgs; + +__attribute__((CALL_CONV)) +extern int64_t simple_main(SimpleArgs *args); + +static SimpleArgs *simple_args(int argc, char **argv) { + if (argc < 0 || (uint64_t)argc > UINT32_MAX) + return NULL; + + SimpleArgs *args = calloc(1, sizeof(*args) + (size_t)argc * sizeof(args->args[0])); + if (args == NULL) + return NULL; + args->len = (uint32_t)argc; + + for (int i = 0; i < argc; i++) { + size_t len = strlen(argv[i]); + if (len > UINT32_MAX) { + free(args); + return NULL; + } + SimpleBytes *str = malloc(sizeof(*str) + len); + if (str == NULL) { + for (int j = 0; j < i; j++) free(args->args[j]); + free(args); + return NULL; + } + str->len = (uint32_t)len; + memcpy(str->bytes, argv[i], len); + args->args[i] = str; + } + return args; +} + +int main(int argc, char **argv) { + SimpleArgs *args = simple_args(argc, argv); + if (args == NULL) + return 1; + // The language-level hidden receiver is not part of the emitted ABI for + // free functions; source argument zero occupies native argument zero. + return (int)simple_main(args); +} |
