swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 1 | #include "plat.h" |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 2 | #include "../error.h" |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 3 | #include <stdio.h> |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 6 | #include <sys/mman.h> |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 7 | #include <unistd.h> |
swissChili | 4691cf6 | 2021-08-05 21:34:57 -0700 | [diff] [blame] | 8 | #ifndef NO_READLINE |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 9 | #include <readline/readline.h> |
| 10 | #include <readline/history.h> |
swissChili | 4691cf6 | 2021-08-05 21:34:57 -0700 | [diff] [blame] | 11 | #endif |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 12 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 13 | void *malloc_aligned(size_t size) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 14 | { |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 15 | // https://www.gnu.org/software/libc/manual/html_node/Aligned-Memory-Blocks.html |
| 16 | // On glibc malloc() and realloc() return 8-byte aligned addresses. |
| 17 | return malloc(size); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 18 | } |
| 19 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 20 | void *realloc_aligned(void *addr, size_t size) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 21 | { |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 22 | return realloc(addr, size); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 23 | } |
| 24 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 25 | void free_aligned(void *addr) |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 26 | { |
swissChili | 708d4c4 | 2021-07-04 17:40:07 -0700 | [diff] [blame] | 27 | free(addr); |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 28 | } |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 29 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 30 | void *link_program(dasm_State **Dst) |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 31 | { |
| 32 | size_t size; |
| 33 | void *buf; |
| 34 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 35 | dasm_link(Dst, &size); |
| 36 | buf = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, |
| 37 | 0); |
| 38 | dasm_encode(Dst, buf); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 39 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 40 | mprotect(buf, size, PROT_READ | PROT_EXEC); |
swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 41 | return buf; |
| 42 | } |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 43 | |
| 44 | bool file_exists(const char *path) |
| 45 | { |
| 46 | return access(path, F_OK) == 0; |
| 47 | } |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 48 | |
| 49 | char *read_input_line(char *prompt) |
| 50 | { |
swissChili | 4691cf6 | 2021-08-05 21:34:57 -0700 | [diff] [blame] | 51 | #ifndef NO_READLINE |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 52 | return readline(prompt); |
swissChili | 4691cf6 | 2021-08-05 21:34:57 -0700 | [diff] [blame] | 53 | #else |
swissChili | a890aed | 2022-07-30 17:13:07 -0700 | [diff] [blame] | 54 | fprintf(stdout, "%s", prompt); |
| 55 | fflush(stdout); |
| 56 | char *buffer = malloc(512); |
| 57 | fgets(buffer, 512, stdin); |
| 58 | |
| 59 | return buffer; |
swissChili | 4691cf6 | 2021-08-05 21:34:57 -0700 | [diff] [blame] | 60 | #endif |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void add_line_to_history(char *line) |
| 64 | { |
swissChili | 4691cf6 | 2021-08-05 21:34:57 -0700 | [diff] [blame] | 65 | #ifndef NO_READLINE |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 66 | add_history(line); |
swissChili | a7568dc | 2021-08-08 16:52:52 -0700 | [diff] [blame] | 67 | #else |
| 68 | UNUSED(line); |
swissChili | 4691cf6 | 2021-08-05 21:34:57 -0700 | [diff] [blame] | 69 | #endif |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 70 | } |