Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def create_spec_compiler
compiler
end

def run(code, filename = nil, inject_primitives = true, debug = Crystal::Debug::None, flags = nil, *, file = __FILE__)
def run(code, filename : String? = nil, inject_primitives = true, debug = Crystal::Debug::None, flags = nil, *, file = __FILE__) : LLVM::GenericValue | SpecRunOutput
if inject_primitives
code = %(require "primitives"\n#{code})
end
Expand Down
41 changes: 33 additions & 8 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Crystal
ONCE = "__crystal_once"

class Program
def run(code, filename = nil, debug = Debug::Default)
def run(code, filename : String? = nil, debug = Debug::Default)
parser = new_parser(code)
parser.filename = filename
node = parser.parse
Expand Down Expand Up @@ -79,7 +79,17 @@ module Crystal
end

def evaluate(node, return_type : T.class, debug = Debug::Default) : T forall T
visitor = CodeGenVisitor.new self, node, single_module: true, debug: debug
llvm_context =
{% if LibLLVM::IS_LT_110 %}
LLVM::Context.new
{% else %}
begin
ts_ctx = LLVM::Orc::ThreadSafeContext.new
ts_ctx.context
end
{% end %}

visitor = CodeGenVisitor.new self, node, single_module: true, debug: debug, llvm_context: llvm_context
visitor.accept node
visitor.process_finished_hooks
visitor.finish
Expand All @@ -88,7 +98,6 @@ module Crystal
llvm_mod.target = target_machine.triple

main = visitor.typed_fun?(llvm_mod, MAIN_NAME).not_nil!
llvm_context = llvm_mod.context

# void (*__evaluate_wrapper)(void*)
wrapper_type = LLVM::Type.function([llvm_context.void_pointer], llvm_context.void)
Expand All @@ -111,11 +120,27 @@ module Crystal
llvm_mod.verify

result = uninitialized T
LLVM::JITCompiler.new(llvm_mod) do |jit|
func_ptr = jit.function_address("__evaluate_wrapper")

{% if LibLLVM::IS_LT_110 %}
LLVM::JITCompiler.new(llvm_mod) do |jit|
func_ptr = jit.function_address("__evaluate_wrapper")
func = Proc(T*, Nil).new(func_ptr, Pointer(Void).null)
func.call(pointerof(result))
end
{% else %}
lljit_builder = LLVM::Orc::LLJITBuilder.new
lljit = LLVM::Orc::LLJIT.new(lljit_builder)

dylib = lljit.main_jit_dylib
dylib.link_symbols_from_current_process(lljit.global_prefix)
tsm = LLVM::Orc::ThreadSafeModule.new(llvm_mod, ts_ctx)
lljit.add_llvm_ir_module(dylib, tsm)

func_ptr = lljit.lookup("__evaluate_wrapper")
func = Proc(T*, Nil).new(func_ptr, Pointer(Void).null)
func.call(pointerof(result))
end
{% end %}

result
end

Expand Down Expand Up @@ -245,9 +270,9 @@ module Crystal
def initialize(@program : Program, @node : ASTNode,
@single_module : Bool = false,
@debug = Debug::Default,
@frame_pointers : FramePointers = :auto)
@frame_pointers : FramePointers = :auto,
@llvm_context : LLVM::Context = LLVM::Context.new)
@abi = @program.target_machine.abi
@llvm_context = LLVM::Context.new
# LLVM::Context.register(@llvm_context, "main")
@llvm_mod = @llvm_context.new_module("main_module")
@main_mod = @llvm_mod
Expand Down
1 change: 1 addition & 0 deletions src/llvm/lib_llvm/lljit.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ lib LibLLVM
fun orc_dispose_lljit = LLVMOrcDisposeLLJIT(j : OrcLLJITRef) : ErrorRef

fun orc_lljit_get_main_jit_dylib = LLVMOrcLLJITGetMainJITDylib(j : OrcLLJITRef) : OrcJITDylibRef
fun orc_lljit_get_global_prefix = LLVMOrcLLJITGetGlobalPrefix(j : OrcLLJITRef) : Char
fun orc_lljit_add_llvm_ir_module = LLVMOrcLLJITAddLLVMIRModule(j : OrcLLJITRef, jd : OrcJITDylibRef, tsm : OrcThreadSafeModuleRef) : ErrorRef
fun orc_lljit_lookup = LLVMOrcLLJITLookup(j : OrcLLJITRef, result : OrcExecutorAddress*, name : Char*) : ErrorRef
end
4 changes: 2 additions & 2 deletions src/llvm/orc/jit_dylib.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class LLVM::Orc::JITDylib
@unwrap
end

def link_symbols_from_current_process : Nil
LLVM.assert LibLLVM.orc_create_dynamic_library_search_generator_for_process(out dg, 0, nil, nil)
def link_symbols_from_current_process(global_prefix : Char) : Nil
LLVM.assert LibLLVM.orc_create_dynamic_library_search_generator_for_process(out dg, global_prefix.ord.to_u8, nil, nil)
LibLLVM.orc_jit_dylib_add_generator(self, dg)
end
end
4 changes: 4 additions & 0 deletions src/llvm/orc/lljit.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class LLVM::Orc::LLJIT
JITDylib.new(LibLLVM.orc_lljit_get_main_jit_dylib(self))
end

def global_prefix : Char
LibLLVM.orc_lljit_get_global_prefix(self).unsafe_chr
end

def add_llvm_ir_module(dylib : JITDylib, tsm : ThreadSafeModule) : Nil
tsm.take_ownership { raise "Failed to take ownership of LLVM::Orc::ThreadSafeModule" }
LLVM.assert LibLLVM.orc_lljit_add_llvm_ir_module(self, dylib, tsm)
Expand Down