summaryrefslogtreecommitdiff
path: root/src/backend/linux.c
diff options
context:
space:
mode:
author0x221E <0x221E@0xinfinity.dev>2026-04-12 16:24:06 +0200
committer0x221E <0x221E@0xinfinity.dev>2026-04-12 16:24:06 +0200
commit4946ca67cf04845737f0f7f70b5ed27bcfe9a18b (patch)
treee0ce4c11f5b81828da7680143ea444003dd355b3 /src/backend/linux.c
Initial commitHEADmaster
Diffstat (limited to 'src/backend/linux.c')
-rw-r--r--src/backend/linux.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/backend/linux.c b/src/backend/linux.c
new file mode 100644
index 0000000..dfa5a80
--- /dev/null
+++ b/src/backend/linux.c
@@ -0,0 +1,92 @@
+#include <backend.h>
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#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);
+ }
+}