riscv/
.clang-format
570 B
.gitignore
5 B
.gitsigners
112 B
LICENSE
1.1 KiB
Makefile
1.1 KiB
README
3.3 KiB
color.h
567 B
emulator.c
79.7 KiB
io.c
1.1 KiB
io.h
444 B
jit.c
32.6 KiB
jit.h
5.0 KiB
riscv.c
12.0 KiB
riscv.h
12.0 KiB
types.h
1.0 KiB
io.c
raw
| 1 | #include <stdarg.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | |
| 6 | #include "io.h" |
| 7 | #include "types.h" |
| 8 | |
| 9 | i32 readfile(const char *path, char **data) { |
| 10 | FILE *fp = NULL; |
| 11 | i32 size = -1; |
| 12 | |
| 13 | *data = NULL; |
| 14 | |
| 15 | if (!(fp = fopen(path, "r"))) { |
| 16 | goto cleanup; |
| 17 | } |
| 18 | if (fseek(fp, 0L, SEEK_END) != 0) { |
| 19 | goto cleanup; |
| 20 | } |
| 21 | if ((size = ftell(fp)) < 0) { |
| 22 | goto cleanup; |
| 23 | } |
| 24 | if (fseek(fp, 0L, SEEK_SET) != 0) { |
| 25 | goto cleanup; |
| 26 | } |
| 27 | if ((*data = malloc((size_t)size + 1)) == NULL) { |
| 28 | goto cleanup; |
| 29 | } |
| 30 | if (fread(*data, 1, (size_t)size, fp) != (size_t)size) { |
| 31 | size = -1; |
| 32 | goto cleanup; |
| 33 | } |
| 34 | (*data)[size] = '\0'; |
| 35 | |
| 36 | cleanup: |
| 37 | if (fp) { |
| 38 | fclose(fp); |
| 39 | } |
| 40 | if (size < 0 && *data) { |
| 41 | free(*data); |
| 42 | *data = NULL; |
| 43 | } |
| 44 | return size; |
| 45 | } |
| 46 | |
| 47 | void _bail(const char *file, i32 line, const char *restrict fmt, ...) { |
| 48 | va_list ap; |
| 49 | va_start(ap, fmt); |
| 50 | |
| 51 | fflush(stdout); |
| 52 | fprintf(stderr, "%s:%d: fatal: ", file, line); |
| 53 | vfprintf(stderr, fmt, ap); |
| 54 | fprintf(stderr, "\n"); |
| 55 | va_end(ap); |
| 56 | |
| 57 | exit(1); |
| 58 | } |