diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-06-11 02:00:43 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-06-11 02:00:43 +0200 |
commit | d40fe3a6503586eb2ce93311cc4bf23febfbeb83 (patch) | |
tree | d0e39ac8727114cefe9a4215bbacb759dd70bde7 | |
parent | 5a78c6d8f0e30b7815729d9589e2252e439553e0 (diff) |
Get building & linking with LLVM working
-rw-r--r-- | cbs.h | 32 | ||||
-rw-r--r-- | make.c | 3 | ||||
-rw-r--r-- | src/codegen.c | 38 | ||||
-rw-r--r-- | src/codegen.h | 9 | ||||
-rw-r--r-- | src/main.c | 2 |
5 files changed, 67 insertions, 17 deletions
@@ -28,11 +28,10 @@ struct strs { size_t len, cap; }; -enum pkg_config_flags { - PC_CFLAGS = 1 << 0, - PC_LIBS = 1 << 1, - PC_SHARED = 1 << 2, - PC_STATIC = 1 << 3, +enum llvm_config_flags { + LLVM_CFLAGS = 1 << 0, + LLVM_LDFLAGS = 1 << 1, + LLVM_LIBS = 1 << 2, }; void cbsinit(int, char **); @@ -64,7 +63,7 @@ static void cmdput(struct strs); static void cmdfput(FILE *, struct strs); static char *swpext(const char *, const char *); -static bool pcquery(struct strs *, const char *, int); +static bool llvmquery(struct strs *, int); static bool binexists(const char *); static int nproc(void); @@ -376,20 +375,17 @@ cmdfput(FILE *fp, struct strs xs) } bool -pcquery(struct strs *xs, const char *lib, int flags) +llvmquery(struct strs *xs, int flags) { struct strs ys = {0}; - strspushl(&ys, "pkg-config", "--silence-errors"); - if (flags & PC_CFLAGS) + strspushl(&ys, "llvm-config"); + if (flags & LLVM_CFLAGS) strspushl(&ys, "--cflags"); - if (flags & PC_LIBS) + if (flags & LLVM_LDFLAGS) + strspushl(&ys, "--ldflags"); + if (flags & LLVM_LIBS) strspushl(&ys, "--libs"); - if (flags & PC_SHARED) - strspushl(&ys, "--shared"); - if (flags & PC_STATIC) - strspushl(&ys, "--static"); - strspushl(&ys, (char *)lib); char *buf; size_t bufsz; @@ -398,8 +394,10 @@ pcquery(struct strs *xs, const char *lib, int flags) if (ec != EXIT_SUCCESS) return false; - /* Remove trailing newline */ - buf[bufsz - 1] = 0; + for (size_t i = 0; i < bufsz; i++) { + if (buf[i] == '\n') + buf[i] = ' '; + } wordexp_t we; assert(wordexp(buf, &we, WRDE_NOCMD) == 0); @@ -153,6 +153,8 @@ cc(void *arg) strspushenv(&cmd, "CFLAGS", cflags_dbg, lengthof(cflags_dbg)); if (simd_flags != 0) strspushl(&cmd, "-DORYX_SIMD=1"); + if (strcmp(arg, "src/codegen.c") == 0) + llvmquery(&cmd, LLVM_CFLAGS); strspushl(&cmd, "-o", dst, "-c", src); cmdput(cmd); @@ -175,6 +177,7 @@ ld(void) strspushenv(&cmd, "CFLAGS", cflags_rls, lengthof(cflags_rls)); else strspushenv(&cmd, "CFLAGS", cflags_dbg, lengthof(cflags_dbg)); + llvmquery(&cmd, LLVM_LDFLAGS | LLVM_LIBS); strspushl(&cmd, "-o", TARGET); assert(glob("src/*.o", 0, globerr, &g) == 0); diff --git a/src/codegen.c b/src/codegen.c new file mode 100644 index 0000000..a9e5006 --- /dev/null +++ b/src/codegen.c @@ -0,0 +1,38 @@ +#include <llvm-c/Analysis.h> +#include <llvm-c/Core.h> + +#include "codegen.h" + +void +codegen(struct ast_soa ast, struct lexemes_soa toks) +{ + (void)ast; + (void)toks; + + /* Create a new module */ + LLVMModuleRef mod = LLVMModuleCreateWithName("oryx"); + + /* Declare the function ‘sum :: (int, int) int’ */ + LLVMTypeRef param_types[] = {LLVMInt32Type(), LLVMInt32Type()}; + LLVMTypeRef ret_type = LLVMFunctionType(LLVMInt32Type(), param_types, 2, 0); + LLVMValueRef sum = LLVMAddFunction(mod, "sum", ret_type); + + /* Create an entry block, and a builder */ + LLVMBasicBlockRef entry = LLVMAppendBasicBlock(sum, "entry"); + LLVMBuilderRef builder = LLVMCreateBuilder(); + LLVMPositionBuilderAtEnd(builder, entry); + + /* Generate an ADD instruction */ + LLVMValueRef tmp = LLVMBuildAdd(builder, LLVMGetParam(sum, 0), + LLVMGetParam(sum, 1), "tmpadd"); + LLVMBuildRet(builder, tmp); + + /* Verify that all went well and if not we abort */ + char *error = NULL; + LLVMVerifyModule(mod, LLVMAbortProcessAction, &error); + LLVMDisposeMessage(error); + + LLVMDumpModule(mod); + + LLVMDisposeBuilder(builder); +} diff --git a/src/codegen.h b/src/codegen.h new file mode 100644 index 0000000..79f3ad7 --- /dev/null +++ b/src/codegen.h @@ -0,0 +1,9 @@ +#ifndef ORYX_CODEGEN_H +#define ORYX_CODEGEN_H + +#include "lexer.h" +#include "parser.h" + +void codegen(struct ast_soa ast, struct lexemes_soa toks); + +#endif /* !ORYX_CODEGEN_H */ @@ -5,6 +5,7 @@ #include <stdlib.h> #include <unistd.h> +#include "codegen.h" #include "errors.h" #include "lexer.h" #include "parser.h" @@ -24,6 +25,7 @@ main(int argc, char **argv) struct lexemes_soa toks = lexstring(src, srclen); struct ast_soa ast = parsetoks(toks); + codegen(ast, toks); #if DEBUG free(src); |