aboutsummaryrefslogtreecommitdiff
path: root/src/codegen.c
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-06-24 05:39:58 +0200
committerThomas Voss <mail@thomasvoss.com> 2024-06-24 05:39:58 +0200
commit80ef5fb48853052d75184e1fe2fa8735d3b37576 (patch)
tree7d45fe81cef8b475f8356096a461624fbfb7d3a7 /src/codegen.c
parentda314b492202a347df9ec10649b33d0d7d2fe7e3 (diff)
Set the target data layout
Diffstat (limited to 'src/codegen.c')
-rw-r--r--src/codegen.c38
1 files changed, 32 insertions, 6 deletions
diff --git a/src/codegen.c b/src/codegen.c
index fe8e63b..d432478 100644
--- a/src/codegen.c
+++ b/src/codegen.c
@@ -17,6 +17,18 @@
#define lengthof(xs) (sizeof(xs) / sizeof(*(xs)))
+/* Cheers to Lernö for this */
+#define LLVM_TARGET_INIT(x) \
+ do { \
+ LLVMInitialize##x##AsmParser(); \
+ LLVMInitialize##x##AsmPrinter(); \
+ LLVMInitialize##x##TargetInfo(); \
+ LLVMInitialize##x##Target(); \
+ LLVMInitialize##x##Disassembler(); \
+ LLVMInitialize##x##TargetMC(); \
+ } while (false)
+
+
/* A context structure we can pass to all the codegen functions just so they
have easy access to everything */
struct cgctx {
@@ -45,12 +57,21 @@ static LLVMTypeRef type2llvm(struct cgctx, type_t);
static void codegenast(struct cgctx);
void
-codegen(const char *file, mpq_t *folds, scope_t *scps, type_t *types,
- ast_t ast, aux_t aux, lexemes_t toks)
+codegen(const char *file, mpq_t *folds, scope_t *scps, type_t *types, ast_t ast,
+ aux_t aux, lexemes_t toks)
{
+ LLVM_TARGET_INIT(X86);
+
+ char *error = NULL;
char *triple = LLVMGetDefaultTargetTriple();
+ LLVMTargetRef lltarget;
LLVMContextRef llctx = LLVMContextCreate();
- LLVMModuleRef llmod = LLVMModuleCreateWithNameInContext("oryx", llctx);
+ LLVMModuleRef llmod = LLVMModuleCreateWithNameInContext("oryx", llctx);
+ if (LLVMGetTargetFromTriple(triple, &lltarget, &error) != 0)
+ err("codegen: %s", error);
+ LLVMTargetMachineRef llmach = LLVMCreateTargetMachine(
+ lltarget, triple, "", "", LLVMCodeGenLevelNone, LLVMRelocDefault,
+ LLVMCodeModelDefault);
struct cgctx ctx = {
.a = &(arena_t){0},
@@ -66,7 +87,7 @@ codegen(const char *file, mpq_t *folds, scope_t *scps, type_t *types,
.ctx = llctx,
.mod = llmod,
.bob = LLVMCreateBuilderInContext(llctx),
- .td = LLVMGetModuleDataLayout(llmod),
+ .td = LLVMCreateTargetDataLayout(llmach),
};
LLVMSetTarget(ctx.mod, triple);
@@ -76,17 +97,22 @@ codegen(const char *file, mpq_t *folds, scope_t *scps, type_t *types,
codegenast(ctx);
- char *error = NULL;
+ error = NULL;
if (LLVMVerifyModule(ctx.mod, LLVMReturnStatusAction, &error) == 1)
err("codegen: %s", error);
+ LLVMDumpModule(ctx.mod);
+
+#if DEBUG
tmpfree(ctx.s);
arena_free(ctx.a);
LLVMDisposeBuilder(ctx.bob);
LLVMDisposeMessage(error);
- LLVMDumpModule(ctx.mod);
LLVMDisposeModule(ctx.mod);
+ LLVMDisposeTargetData(ctx.td);
+ LLVMDisposeTargetMachine(llmach);
LLVMContextDispose(ctx.ctx);
+#endif
}
static idx_t codegendecl(struct cgctx ctx, idx_t);