| 1 |
;;; |
| 2 |
;;; Interface to VM code generator (code.c) |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2000-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 |
(declare) ;; a dummy form to suppress generation of "sci" file |
| 35 |
|
| 36 |
;; This module is for internal use, shared by the runtime compiler, |
| 37 |
;; AOT-compiler, and other module that needs to deal with Gauche VM |
| 38 |
;; code generation. |
| 39 |
(define-module gauche.vm.code |
| 40 |
(export vm-dump-code vm-code->list vm-insn-build |
| 41 |
vm-insn-code->name vm-insn-name->code |
| 42 |
|
| 43 |
make-compiled-code-builder |
| 44 |
compiled-code-emit0! compiled-code-emit0o! |
| 45 |
compiled-code-emit0i! compiled-code-emit0oi! |
| 46 |
compiled-code-emit-PUSH! compiled-code-emit-RET! |
| 47 |
compiled-code-emit1! compiled-code-emit1o! |
| 48 |
compiled-code-emit1i! compiled-code-emit1oi! |
| 49 |
compiled-code-emit2! compiled-code-emit2o! |
| 50 |
compiled-code-emit2i! compiled-code-emit2oi! |
| 51 |
compiled-code-new-label compiled-code-set-label! |
| 52 |
compiled-code-push-info! |
| 53 |
compiled-code-finish-builder |
| 54 |
compiled-code-copy!)) |
| 55 |
(select-module gauche.vm.code) |
| 56 |
|
| 57 |
;;============================================================ |
| 58 |
;; Compiled code builder interface |
| 59 |
;; |
| 60 |
|
| 61 |
(inline-stub |
| 62 |
(.include "gauche/priv/configP.h" |
| 63 |
"gauche/code.h" |
| 64 |
"gauche/priv/codeP.h" |
| 65 |
"gauche/vminsn.h") |
| 66 |
|
| 67 |
(define-cproc vm-dump-code (code::<compiled-code>) ::<void> |
| 68 |
Scm_CompiledCodeDump) |
| 69 |
(define-cproc vm-code->list (code::<compiled-code>) |
| 70 |
Scm_CompiledCodeToList) |
| 71 |
(define-cproc vm-insn-build (insn) ::<ulong> |
| 72 |
(return (cast u_long (Scm_VMInsnBuild insn)))) |
| 73 |
(define-cproc vm-insn-code->name (opcode::<uint>) |
| 74 |
(return (SCM_INTERN (Scm_VMInsnName opcode)))) |
| 75 |
(define-cproc vm-insn-name->code (insn-name) ::<int> |
| 76 |
Scm_VMInsnNameToCode) |
| 77 |
|
| 78 |
(define-cproc make-compiled-code-builder (reqargs::<uint16> optargs::<uint16> |
| 79 |
name parent intform) |
| 80 |
Scm_MakeCompiledCodeBuilder) |
| 81 |
|
| 82 |
;; CompiledCodeEmit is performance critical. To reduce the overhead of |
| 83 |
;; argument passing, we prepare variations for specific code patterns. |
| 84 |
(define-cproc compiled-code-emit0! |
| 85 |
(cc::<compiled-code> code::<int>) ::<void> |
| 86 |
(Scm_CompiledCodeEmit cc code 0 0 '#f '#f)) |
| 87 |
|
| 88 |
(define-cproc compiled-code-emit-PUSH! |
| 89 |
(cc::<compiled-code>) ::<void> |
| 90 |
(Scm_CompiledCodeEmit cc SCM_VM_PUSH 0 0 '#f '#f)) |
| 91 |
|
| 92 |
(define-cproc compiled-code-emit-RET! |
| 93 |
(cc::<compiled-code>) ::<void> |
| 94 |
(Scm_CompiledCodeEmit cc SCM_VM_RET 0 0 '#f '#f)) |
| 95 |
|
| 96 |
(define-cproc compiled-code-emit0o! |
| 97 |
(cc::<compiled-code> code::<int> operand) ::<void> |
| 98 |
(Scm_CompiledCodeEmit cc code 0 0 operand '#f)) |
| 99 |
|
| 100 |
(define-cproc compiled-code-emit0i! |
| 101 |
(cc::<compiled-code> code::<int> info) ::<void> |
| 102 |
(Scm_CompiledCodeEmit cc code 0 0 '#f info)) |
| 103 |
|
| 104 |
(define-cproc compiled-code-emit0oi! |
| 105 |
(cc::<compiled-code> code::<int> operand info) ::<void> |
| 106 |
(Scm_CompiledCodeEmit cc code 0 0 operand info)) |
| 107 |
|
| 108 |
(define-cproc compiled-code-emit1! |
| 109 |
(cc::<compiled-code> code::<int> arg0::<int>) ::<void> |
| 110 |
(Scm_CompiledCodeEmit cc code arg0 0 '#f '#f)) |
| 111 |
|
| 112 |
(define-cproc compiled-code-emit1o! |
| 113 |
(cc::<compiled-code> code::<int> arg0::<int> operand) ::<void> |
| 114 |
(Scm_CompiledCodeEmit cc code arg0 0 operand '#f)) |
| 115 |
|
| 116 |
(define-cproc compiled-code-emit1i! |
| 117 |
(cc::<compiled-code> code::<int> arg0::<int> info) ::<void> |
| 118 |
(Scm_CompiledCodeEmit cc code arg0 0 '#f info)) |
| 119 |
|
| 120 |
(define-cproc compiled-code-emit1oi! |
| 121 |
(cc::<compiled-code> code::<int> arg0::<int> operand info) ::<void> |
| 122 |
(Scm_CompiledCodeEmit cc code arg0 0 operand info)) |
| 123 |
|
| 124 |
(define-cproc compiled-code-emit2! |
| 125 |
(cc::<compiled-code> code::<int> arg0::<int> arg1::<int>) ::<void> |
| 126 |
(Scm_CompiledCodeEmit cc code arg0 arg1 '#f '#f)) |
| 127 |
|
| 128 |
(define-cproc compiled-code-emit2o! |
| 129 |
(cc::<compiled-code> code::<int> arg0::<int> arg1::<int> operand) ::<void> |
| 130 |
(Scm_CompiledCodeEmit cc code arg0 arg1 operand '#f)) |
| 131 |
|
| 132 |
(define-cproc compiled-code-emit2i! |
| 133 |
(cc::<compiled-code> code::<int> arg0::<int> arg1::<int> info) ::<void> |
| 134 |
(Scm_CompiledCodeEmit cc code arg0 arg1 '#f info)) |
| 135 |
|
| 136 |
(define-cproc compiled-code-emit2oi! |
| 137 |
(cc::<compiled-code> code::<int> arg0::<int> arg1::<int> operand info)::<void> |
| 138 |
(Scm_CompiledCodeEmit cc code arg0 arg1 operand info)) |
| 139 |
|
| 140 |
(define-cproc compiled-code-new-label (cc::<compiled-code>) |
| 141 |
Scm_CompiledCodeNewLabel) |
| 142 |
|
| 143 |
(define-cproc compiled-code-set-label! (cc::<compiled-code> label) |
| 144 |
::<void> Scm_CompiledCodeSetLabel) |
| 145 |
|
| 146 |
(define-cproc compiled-code-finish-builder (cc::<compiled-code> |
| 147 |
maxstack::<int>) |
| 148 |
::<void> Scm_CompiledCodeFinishBuilder) |
| 149 |
|
| 150 |
(define-cproc compiled-code-copy! (dest::<compiled-code> |
| 151 |
src::<compiled-code>) |
| 152 |
::<void> Scm_CompiledCodeCopyX) |
| 153 |
|
| 154 |
;; Push source info to (-> compiled-code info). |
| 155 |
;; Usually the source info is pushed as the instructions are emitted. |
| 156 |
;; This is mainly used to add info that are not attached to a particular |
| 157 |
;; instruction (e.g. the definition of the entire closure). |
| 158 |
(define-cproc compiled-code-push-info! (cc::<compiled-code> info) |
| 159 |
::<void> Scm_CompiledCodePushInfo) |
| 160 |
|
| 161 |
;; Kludge: Let gauche.internal import me. It must be done before the |
| 162 |
;; compiler runs. This should eventually be done in the gauche.internal side. |
| 163 |
(initcode |
| 164 |
(Scm_ImportModule (Scm_GaucheInternalModule) 'gauche.vm.code SCM_FALSE 0)) |
| 165 |
) |
| 166 |
|
| 167 |
;; Extract the original definition (source) of the code. |
| 168 |
;; If the source is the result of macro expansion, the 'definition |
| 169 |
;; attr of debug-info contains the final expansion, but its 'original |
| 170 |
;; pair attribute keeps one macro step before. %original-source traces |
| 171 |
;; back to the root of the source. |
| 172 |
(define (%original-source src) ; internal |
| 173 |
(and (pair? src) |
| 174 |
(let loop ([src src]) |
| 175 |
(if-let1 orig (pair-attribute-get src 'original #f) |
| 176 |
(loop orig) |
| 177 |
src)))) |
| 178 |
|
| 179 |
(select-module gauche.internal) |
| 180 |
;; Standard way to extract source code from <compiled-code> |
| 181 |
(define (compiled-code-definition code) |
| 182 |
(and-let1 def (assq-ref (~ code'debug-info) 'definition) |
| 183 |
((with-module gauche.vm.code %original-source) |
| 184 |
(assq-ref def 'source-info)))) |
| 185 |
|
| 186 |
(define (compiled-code-type code) |
| 187 |
(and-let* ([sig (~ code'signature-info)] |
| 188 |
[ (pair? sig) ]) |
| 189 |
(alist-ref (cdr sig) 'type))) |
| 190 |
|
| 191 |
(define-cproc %decode-packed-debug-info (code-vector::<u8vector> |
| 192 |
const-vector::<vector>) |
| 193 |
(return (Scm_DecodePackedDebugInfo (SCM_U8VECTOR_ELEMENTS code-vector) |
| 194 |
(SCM_U8VECTOR_SIZE code-vector) |
| 195 |
const-vector))) |
| 196 |
|
| 197 |
;; Called from pass5 of the compiler. SRC is $lambda-src of the iform |
| 198 |
;; (could be #f) Does two things: |
| 199 |
;; - Push the entire source into debug-info |
| 200 |
;; - Create extended pair of signature, with source location info attached |
| 201 |
;; as 'source-info pair attribute, and store it into signature-info. |
| 202 |
(define (compiled-code-attach-source-info! code src type) |
| 203 |
(if src |
| 204 |
(let ([orig ((with-module gauche.vm.code %original-source) src)] |
| 205 |
[sig (extended-cons (slot-ref code'full-name) |
| 206 |
(pair-attribute-get src 'arg-info '_))]) |
| 207 |
(if-let1 si (pair-attribute-get orig 'source-info #f) |
| 208 |
(pair-attribute-set! sig 'source-info si)) |
| 209 |
(compiled-code-push-info! code `(definition (source-info . ,src))) |
| 210 |
(slot-set! code 'signature-info |
| 211 |
(cons sig |
| 212 |
(cond-list [type `(type . ,type)])))) |
| 213 |
(slot-set! code 'signature-info (list (cons (slot-ref code'full-name) '_))))) |
| 214 |
|
| 215 |
(select-module gauche) |
| 216 |
(inline-stub |
| 217 |
;; <compiled-code> class is visible |
| 218 |
(define-cclass <compiled-code> |
| 219 |
"ScmCompiledCode*" "Scm_CompiledCodeClass" |
| 220 |
(c "SCM_CLASS_DEFAULT_CPL") |
| 221 |
((parent :setter #f) |
| 222 |
(signature-info :c-name "signatureInfo") |
| 223 |
(debug-info :c-spec "Scm_CodeDebugInfo(obj)" :setter #f) |
| 224 |
(required-args :type <fixnum> :c-name "requiredArgs" :setter #f) |
| 225 |
(optional-args :type <fixnum> :c-name "optionalArgs" :setter #f) |
| 226 |
(name :setter #f) |
| 227 |
(full-name :c-spec "Scm_CompiledCodeFullName(obj)" :setter #f) |
| 228 |
(size :type <fixnum> :c-name "codeSize" :setter #f) |
| 229 |
(max-stack :type <fixnum> :c-name "maxstack" :setter #f) |
| 230 |
(intermediate-form :c-name "intermediateForm" :setter #f)) |
| 231 |
(printer (Scm_Printf port "#<compiled-code %S@%p>" |
| 232 |
(Scm_CompiledCodeFullName (SCM_COMPILED_CODE obj)) |
| 233 |
obj))) |
| 234 |
) |
| 235 |
|
| 236 |
(select-module gauche.internal) |
| 237 |
(inline-stub |
| 238 |
|
| 239 |
(define-cfn get_packed_vector (s::ScmPackedDebugInfo*) :static |
| 240 |
;; This is called only during precomp, or once during decoding, |
| 241 |
;; so we don't need to worry about allocation. |
| 242 |
(return (Scm_MakeU8VectorFromArray (-> s codeSize) (-> s codeVector)))) |
| 243 |
|
| 244 |
;; <packed-debug-info> is internal |
| 245 |
(define-cclass <packed-debug-info> |
| 246 |
"ScmPackedDebugInfo*" "Scm_PackedDebugInfoClass" |
| 247 |
(c "SCM_CLASS_DEFAULT_CPL") |
| 248 |
((packed-vector :setter #f |
| 249 |
:getter (c "get_packed_vector")) |
| 250 |
(const-vector :setter #f |
| 251 |
:c-name "constVector")) |
| 252 |
) |
| 253 |
|
| 254 |
(define-cproc decode-packed-debug-info (p::<packed-debug-info>) |
| 255 |
(when (SCM_FALSEP (-> p decoded)) |
| 256 |
(let* ([decode-debug-info SCM_UNDEFINED] |
| 257 |
[_ (SCM_BIND_PROC decode-debug-info "decode-debug-info" |
| 258 |
(Scm_GaucheInternalModule))] |
| 259 |
[decoded (Scm_ApplyRec2 decode-debug-info |
| 260 |
(get_packed_vector p) |
| 261 |
(-> p constVector))]) |
| 262 |
(set! (-> p decoded) decoded))) |
| 263 |
(return (-> p decoded))) |
| 264 |
) |