]> gitweb.factorcode.org Git - factor.git/blob - extra/llvm/jit/jit.factor
5a8574261964e6e8eac3a1276f43c1713b80f067
[factor.git] / extra / llvm / jit / jit.factor
1 USING: accessors alien.c-types alien.syntax assocs destructors
2 kernel llvm.core llvm.engine llvm.wrappers namespaces ;
3
4 IN: llvm.jit
5
6 SYMBOL: thejit
7
8 TUPLE: jit ee mps ;
9
10 : empty-engine ( -- engine )
11     "initial-module" <module> [
12         <provider>
13     ] with-disposal [
14         <engine>
15     ] with-disposal ;
16
17 : <jit> ( -- jit )
18     jit new empty-engine >>ee H{ } clone >>mps ;
19
20 : (remove-functions) ( function -- )
21     thejit get ee>> value>> over LLVMFreeMachineCodeForFunction
22     LLVMGetNextFunction dup ALIEN: 0 = [ drop ] [ (remove-functions) ] if ;
23
24 : remove-functions ( module -- )
25     ! free machine code for each function in module
26     LLVMGetFirstFunction dup ALIEN: 0 = [ drop ] [ (remove-functions) ] if ;
27
28 : (remove-provider) ( provider -- )
29     thejit get ee>> value>> swap value>> f <void*> f <void*>
30     [ LLVMRemoveModuleProvider drop ] 2keep *void* [ llvm-throw ] when*
31     *void* module new swap >>value
32     [ value>> remove-functions ] with-disposal ;
33
34 : remove-provider ( name -- )
35     dup thejit get mps>> at [
36         (remove-provider)
37         thejit get mps>> delete-at
38     ] [ drop ] if* ;
39
40 : add-provider ( provider name -- )
41     dup remove-provider
42     thejit get ee>> value>>  pick value>> LLVMAddModuleProvider
43     [ t >>disposed ] dip thejit get mps>> set-at ;
44
45 thejit [ <jit> ] initialize