Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
jit.c
Go to the documentation of this file.
1/* -*- C -*-
2 * Serene programming language
3 * Copyright (C) 2019-2026 Sameer Rahmani <[email protected]>
4 *
5 * This library is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#include "serene/jit/jit.h"
20
21#include <llvm-c/Core.h>
22#include <llvm-c/Error.h>
23#include <llvm-c/LLJIT.h>
24#include <llvm-c/Support.h>
25#include <llvm-c/Target.h>
26
27#include <stdio.h>
28
29#include "serene/rt/context.h"
30#include "serene/rt/engine.h"
32#include "serene/utils.h"
33
34#include <string.h>
35
36static inline int handle_error(LLVMErrorRef err) {
37 // TODO(lxsameer): Use the runtime diagnostic system here.
38 char *err_msg = LLVMGetErrorMessage(err);
39 UNUSED(fprintf(stderr, "Error: %s\n", err_msg));
40 LLVMDisposeErrorMessage(err_msg);
41 return 1;
42}
43
45 PANIC_IF_NULL(mm);
47 PANIC_IF_NULL(jit);
48 jit->builder = nullptr;
49 jit->llvm_jit = nullptr;
50 return jit;
51}
52
54 PANIC_IF_NULL(result);
55
56 int error = 0;
57 // TODO(lxsameer): Check for the target platform and setup the codegen
58 // and printer accordingly. Only applies to when we want to
59 // link the jit library into the target binary.
60
61 // Initialize native target codegen and asm printer.
62 LLVMInitializeNativeTarget();
63 LLVMInitializeNativeAsmPrinter();
64
65 LLVMErrorRef err = nullptr;
66 err = LLVMOrcCreateLLJIT(&result->llvm_jit, result->builder);
67 if (err) {
68 error = handle_error(err);
69 LLVMShutdown();
70 return error;
71 }
72 return 0;
73}
74
76 LLVMErrorRef err = LLVMOrcDisposeLLJIT(jit->llvm_jit);
77 if (err) {
78 return handle_error(err);
79 }
80 return 0;
81}
82
84 PANIC_IF_NULL(ctx);
85 PANIC_IF_NULL(module);
86
87 LLVMOrcLLJITRef j = ctx->engine->jit->llvm_jit;
88
89 LLVMOrcJITDylibRef jd = LLVMOrcLLJITGetMainJITDylib(j);
90 LLVMErrorRef err = LLVMOrcLLJITAddLLVMIRModule(j, jd, module->tsm);
91 srn_error_t *srn_err = nullptr;
92
93 if (err != nullptr) {
94 // If adding the ThreadSafeModule fails then we need to clean it up
95 // ourselves. If adding it succeeds the JIT will manage the memory.
96 // LLVMOrcDisposeThreadSafeModule(module);
97
98 // LLVM owns the string message, but we want to own it
99 char *jit_err_msg = LLVMGetErrorMessage(err);
100 char *msg = srn_copy_bytes(ctx, jit_err_msg, strlen(jit_err_msg), alignof(char));
101 srn_err = srn_errors_make(ctx, FAILED_TO_ADD_MODULE, msg);
102 LLVMDisposeErrorMessage(jit_err_msg);
103 }
104 return srn_err;
105}
srn_error_t * srn_errors_make(const srn_context_t *ctx, srn_error_tag_t tag, const char *msg)
Definition errors.c:28
@ FAILED_TO_ADD_MODULE
Failed to add a module to the jit engine.
Definition errors.h:49
#define srn_mm_immortal_allocate(mm, T)
Definition interface.h:169
int srn_jit_shutdown(srn_jit_t *jit)
Definition jit.c:75
srn_jit_t * srn_jit_make(srn_mm_t *mm)
Definition jit.c:44
static int handle_error(LLVMErrorRef err)
Definition jit.c:36
srn_error_t * srn_jit_add_module(srn_context_t *ctx, srn_jit_module_t *module)
Add a module to the jit compiler.
Definition jit.c:83
int srn_jit_make_and_initialize(srn_jit_t *result)
Definition jit.c:53
srn_engine_t * engine
Long term state of the compiler.
Definition context.h:49
LLVMOrcThreadSafeModuleRef tsm
Definition jit.h:29
LLVMOrcLLJITRef llvm_jit
Definition jit.h:36
LLVMOrcLLJITBuilderRef builder
Definition jit.h:37
Main memory manager structure that will own all the allocated blocks and data.
Definition interface.h:112
#define PANIC_IF_NULL(ptr)
Definition utils.h:64
char * srn_copy_bytes(srn_context_t *ctx, const char *src, size_t len, size_t alignment)
Definition utils.c:42
#define UNUSED(x)
Definition utils.h:43