gen/
.clang-format
570 B
.gitignore
30 B
.gitsigners
112 B
LICENSE
1.1 KiB
Makefile
911 B
README
1.8 KiB
ast.c
5.0 KiB
ast.h
15.1 KiB
desugar.c
23.1 KiB
desugar.h
286 B
gen.c
108.5 KiB
gen.h
4.9 KiB
io.c
1.1 KiB
io.h
444 B
limits.h
1.3 KiB
module.c
10.0 KiB
module.h
2.2 KiB
options.c
1.4 KiB
options.h
472 B
parser.c
68.3 KiB
parser.h
942 B
radiance.c
3.7 KiB
ralloc.c
2.0 KiB
ralloc.h
1.1 KiB
resolver.c
109.7 KiB
resolver.h
5.6 KiB
riscv.c
12.0 KiB
riscv.h
12.0 KiB
scanner.c
10.2 KiB
scanner.h
3.2 KiB
strings.c
2.6 KiB
strings.h
407 B
symtab.c
5.7 KiB
symtab.h
4.6 KiB
types.h
1.0 KiB
util.h
1.5 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 | } |