Debian Codegraph @

src/libnative.scm

raw
1 ;;;
2 ;;; libnative.scm - Playing with native code
3 ;;;
4 ;;; Copyright (c) 2021-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; This is EXPERIMENTAL and UNDER DEVELOPMENT.
35
36 ;; Nasty stuff.
37 ;; These bindings are available only during the bootstrap process,
38 ;; for we don't want ordinary code to call those internal routines.
39 ;; For now, it is just a stub for more experiment. Eventually ffi and jit
40 ;; will use those routines.
41
42 (select-module gauche.bootstrap)
43
44 (inline-stub
45 (.include "gauche/priv/configP.h"
46 "gauche/priv/mmapP.h"
47 "gauche/priv/nativeP.h"
48 "gauche/priv/codeP.h"
49 "gauche/vminsn.h")
50
51 ;; For FFI
52 (define-cproc %%call-native (tstart::<fixnum>
53 tend::<fixnum>
54 code::<uvector>
55 start::<fixnum>
56 end::<fixnum>
57 entry::<fixnum>
58 patcher rettype)
59 (return (Scm__VMCallNative (Scm_VM) tstart tend code start end entry
60 patcher rettype)))
61
62 ;; For JIT
63 (define-cproc %%jit-compile-procedure (proc compiler)
64 (unless (SCM_CLOSUREP proc)
65 (Scm_Error "Closure required, but got: %S" proc))
66 (let* ([orig-code (SCM_CLOSURE_CODE proc)]
67 [orig-env::ScmEnvFrame* (SCM_CLOSURE_ENV proc)]
68 [builder::ScmCompiledCode*
69 (SCM_COMPILED_CODE
70 (Scm_MakeCompiledCodeBuilder (SCM_PROCEDURE_REQUIRED proc)
71 (SCM_PROCEDURE_OPTIONAL proc)
72 (SCM_PROCEDURE_INFO proc)
73 proc
74 SCM_FALSE))]
75 [r (Scm_ApplyRec1 compiler orig-code)]
76 [_ (SCM_ASSERT (SCM_PAIRP r))]
77 [maxdepth (SCM_CAR r)]
78 [_ (SCM_ASSERT (SCM_PAIRP (SCM_CDR r)))]
79 [codevec (SCM_CADR r)]
80 [offsets (SCM_CDDR r)])
81 (SCM_ASSERT (SCM_INTP maxdepth))
82 (SCM_ASSERT (SCM_U8VECTORP codevec))
83 (let* ([codepage (Scm__AllocateCodePage (SCM_U8VECTOR codevec))])
84 (for-each (lambda (offset)
85 (Scm_CompiledCodeEmit builder
86 SCM_VM_XINSN 0 0
87 (SCM_LIST2 codepage offset)
88 SCM_FALSE))
89 offsets))
90 (Scm_CompiledCodeEmit builder SCM_VM_RET 0 0 SCM_FALSE SCM_FALSE)
91 (Scm_CompiledCodeFinishBuilder builder (SCM_INT_VALUE maxdepth))
92 (return (Scm_MakeClosure (SCM_OBJ builder) orig-env))))
93 )
94
95 (select-module gauche.internal)
96
97 ;; Stub for FFI
98 (include "native-supp.scm")
99
100 ;; Stub for JIT
101 (define-cproc %unsafe-jit-enabled? () ::<boolean>
102 (.if GAUCHE_ENABLE_UNSAFE_JIT_API
103 (return TRUE)
104 (return FALSE)))
105
106 (define %jit-compile
107 (if (%unsafe-jit-enabled?)
108 (let ([jcp (module-binding-ref 'gauche.bootstrap '%%jit-compile-procedure)])
109 (^[proc compiler] (jcp proc compiler)))
110 (^ _ (error "Operation not allowed"))))
111
112 (inline-stub
113 (define-cise-stmt with-static-table
114 [(_ (var ((key intval) ...)) body ...)
115 (let ([tmp (gensym)]
116 [initsize (length key)])
117 `(let* ([,var :: (static ScmHashTable*) NULL])
118 (when (== ,var NULL)
119 (let* ([,tmp :: ScmHashTable*
120 (SCM_HASH_TABLE (Scm_MakeHashTableSimple SCM_HASH_EQ ,initsize))])
121 ,@(map (^[k v]
122 `(Scm_HashTableSet ,tmp ',k
123 (Scm_IntptrToInteger (cast intptr_t ,v))
124 0))
125 key intval)
126 (set! ,var ,tmp)))
127 ,@body))]))
128
129 ;; Returns offset of VM field
130 (define-cproc vm-field-offset (field-name::<symbol>)
131 (with-static-table
132 (tab ((env (offsetof ScmVM env))
133 (denv (offsetof ScmVM denv))
134 (cont (offsetof ScmVM cont))
135 (argp (offsetof ScmVM argp))
136 (val0 (offsetof ScmVM val0))
137 (vals (offsetof ScmVM vals))
138 (numVals (offsetof ScmVM numVals))
139 (sp (offsetof ScmVM sp))
140 (stackEnd (offsetof ScmVM stackEnd))))
141 (let* ([off (Scm_HashTableRef tab (SCM_OBJ field-name) SCM_FALSE)])
142 (unless (SCM_INTP off)
143 (Scm_Error "Unknown VM field: %S" field-name))
144 (return off))))
145
146 ;; Returns address of the named function to be called from JIT code.
147 ;; We want to allow only selected function to be callable, so we create
148 ;; table manually.
149 (define-cproc vm-function-address (name::<symbol>)
150 (with-static-table
151 (tab ((Cons Scm_Cons)
152 (NumEq Scm_NumEq)
153 (NumLT Scm_NumLT)
154 (NumGT Scm_NumGT)
155 (Add Scm_Add)
156 (Sub Scm_Sub)
157 (Mul Scm_Mul)
158 (Div Scm_Div)))
159 (let* ([addr (Scm_HashTableRef tab (SCM_OBJ name) SCM_FALSE)])
160 (unless (SCM_INTEGERP addr)
161 (Scm_Error "Unknown function address: %S" name))
162 (return addr))))
163
164 ;; TEMPORARY - Returns raw representation of ScmObj.
165 (define-cproc raw-value (obj) ::<integer>
166 (.unless GAUCHE_ENABLE_UNSAFE_JIT_API
167 (Scm_Error "Operation not allowed"))
168 (return (Scm_IntptrToInteger (cast intptr_t obj))))