@@ -11,6 +11,9 @@ export JITTargetMachine
11
11
end
12
12
13
13
Base. unsafe_convert (:: Type{API.LLVMOrcJITStackRef} , orc:: OrcJIT ) = orc. ref
14
+ Base. unsafe_convert (:: Type{Ptr{Cvoid}} , orc:: OrcJIT ) = Base. unsafe_convert (Ptr{Cvoid}, orc. ref)
15
+
16
+ OrcJIT (ref:: Ptr{Cvoid} ) = OrcJIT (Base. unsafe_convert (API. LLVMOrcJITStackRef, ref))
14
17
15
18
"""
16
19
OrcJIT(::TargetMachine)
@@ -28,6 +31,15 @@ function dispose(orc::OrcJIT)
28
31
API. LLVMOrcDisposeInstance (orc)
29
32
end
30
33
34
+ function OrcJIT (f:: Core.Function , tm:: TargetMachine )
35
+ orc = OrcJIT (tm)
36
+ try
37
+ f (orc)
38
+ finally
39
+ dispose (orc)
40
+ end
41
+ end
42
+
31
43
function errormsg (orc:: OrcJIT )
32
44
# The error message is owned by `orc`, and will
33
45
# be disposed along-side the OrcJIT.
@@ -39,12 +51,52 @@ struct OrcModule
39
51
end
40
52
Base. convert (:: Type{API.LLVMOrcModuleHandle} , mod:: OrcModule ) = mod. handle
41
53
42
- function compile! (orc:: OrcJIT , mod:: Module , resolver = C_NULL , ctx = C_NULL ; lazy= false )
54
+ """
55
+ resolver(name, ctx)
56
+
57
+ Lookup the symbol `name`. Iff `ctx` is passed to this function it should be a
58
+ pointer to the OrcJIT we are compiling for.
59
+ """
60
+ function resolver (name, ctx)
61
+ name = unsafe_string (name)
62
+ # # Step 0: Should have already resolved it iff it was in the
63
+ # # same module
64
+ # # Step 1: See if it's something known to the execution engine
65
+ ptr = C_NULL
66
+ if ctx != C_NULL
67
+ orc = OrcJIT (ctx)
68
+ ptr = pointer (address (orc, name))
69
+ end
70
+
71
+ # # Step 2: Search the program symbols
72
+ if ptr == C_NULL
73
+ #
74
+ # SearchForAddressOfSymbol expects an unmangled 'C' symbol name.
75
+ # Iff we are on Darwin, strip the leading '_' off.
76
+ @static if Sys. isapple ()
77
+ if name[1 ] == ' _'
78
+ name = name[2 : end ]
79
+ end
80
+ end
81
+ ptr = LLVM. find_symbol (name)
82
+ end
83
+
84
+ # # Step 4: Lookup in libatomic
85
+ # TODO : Do we need to do this?
86
+
87
+ if ptr == C_NULL
88
+ error (" OrcJIT: Symbol `$name ` lookup failed. Aborting!" )
89
+ end
90
+
91
+ return UInt64 (reinterpret (UInt, ptr))
92
+ end
93
+
94
+ function compile! (orc:: OrcJIT , mod:: Module , resolver = @cfunction (resolver, UInt64, (Cstring, Ptr{Cvoid})), resolver_ctx = orc; lazy= false )
43
95
r_mod = Ref {API.LLVMOrcModuleHandle} ()
44
96
if lazy
45
- API. LLVMOrcAddLazilyCompiledIR (orc, r_mod, mod, resolver, ctx )
97
+ API. LLVMOrcAddLazilyCompiledIR (orc, r_mod, mod, resolver, resolver_ctx )
46
98
else
47
- API. LLVMOrcAddEagerlyCompiledIR (orc, r_mod, mod, resolver, ctx )
99
+ API. LLVMOrcAddEagerlyCompiledIR (orc, r_mod, mod, resolver, resolver_ctx )
48
100
end
49
101
OrcModule (r_mod[])
50
102
end
@@ -53,9 +105,9 @@ function Base.delete!(orc::OrcJIT, mod::OrcModule)
53
105
LLVM. API. LLVMOrcRemoveModule (orc, mod)
54
106
end
55
107
56
- function add! (orc:: OrcJIT , obj:: MemoryBuffer , resolver = C_NULL , ctx = C_NULL )
108
+ function add! (orc:: OrcJIT , obj:: MemoryBuffer , resolver = @cfunction (resolver, UInt64, (Cstring, Ptr{Cvoid})), resolver_ctx = orc )
57
109
r_mod = Ref {API.LLVMOrcModuleHandle} ()
58
- API. LLVMOrcAddObjectFile (orc, r_mod, obj, resolver, ctx )
110
+ API. LLVMOrcAddObjectFile (orc, r_mod, obj, resolver, resolver_ctx )
59
111
return OrcModule (r_mod[])
60
112
end
61
113
98
150
99
151
function callback! (orc:: OrcJIT , callback, ctx)
100
152
r_address = Ref {API.LLVMOrcTargetAddress} ()
101
- LLVM . API. LLVMOrcLazyCompileCallback (orc, r_address, callback, ctx)
153
+ API. LLVMOrcCreateLazyCompileCallback (orc, r_address, callback, ctx)
102
154
return OrcTargetAddress (r_address[])
103
155
end
104
156
0 commit comments