#include #include #include #include #include #include "../sv.h" static FILE *src; static FILE *dst; void die(const char *name, const char *file, size_t line, const char *fmt, ...) { printf("ERROR %s(%s:%d): ", name, file, line); va_list args; va_start(args, fmt); vprintf(fmt, args); va_end(args); printf("\nCompilation failed!\n"); exit(1); } void debug(const char *name, const char *file, size_t line, const char *fmt, ...) { printf("LOG %s(%s:%d): ", name, file, line); va_list args; va_start(args, fmt); vprintf(fmt, args); va_end(args); } struct string_view backend_stream_init_src(const char *path) { assert(path != NULL); src = fopen(path, "r"); fseek(src, 0, SEEK_END); size_t size = ftell(src); fseek(src, 0, SEEK_SET); char *buf = malloc(size + 1); if(fread(buf, size, 1, src) == 0) DIE("Failed to read the file!\n"); buf[size] = '\0'; return (struct string_view){.buf = buf, .len = size}; } void backend_stream_init_dst(const char *path) { dst = fopen(path, "w"); } void backend_stream_close_src() { fclose(src); } void backend_stream_close_dst() { fclose(dst); } void emit8(uint8_t out) { fprintf(dst, "%02x", out); } void emit16(uint16_t out) { uint8_t test = 0; for(int i = 0; i < 2; i++) { test = (out >> (i * 8)) & 0xFF; fprintf(dst, "%02x", test); } } void emit32(uint32_t out) { uint8_t test = 0; for(int i = 0; i < 4; i++) { test = (out >> (i * 8)) & 0xFF; fprintf(dst, "%02x", test); } } void emit64(uint64_t out) { uint8_t test = 0; for(int i = 0; i < 8; i++) { test = (out >> (i * 8)) & 0xFF; fprintf(dst, "%02x", test); } }