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