| 1 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 2 |
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; This program is free software: you can redistribute it and/or modify |
| 5 |
;;; it under the terms of the GNU General Public License version 3 as |
| 6 |
;;; published by the Free Software Foundation. |
| 7 |
;;; |
| 8 |
;;; This program is distributed in the hope that it will be useful, but |
| 9 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;;; General Public License for more details. |
| 12 |
;;; |
| 13 |
;;; You should have received a copy of the GNU General Public License |
| 14 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
|
| 17 |
(library (ikarus.code-objects) |
| 18 |
(export |
| 19 |
make-code code-reloc-vector code-freevars |
| 20 |
code-size code-ref code-set! set-code-reloc-vector! |
| 21 |
set-code-annotation! procedure-annotation |
| 22 |
make-annotation-indirect annotation-indirect? |
| 23 |
code->thunk) |
| 24 |
(import |
| 25 |
(ikarus system $fx) |
| 26 |
(ikarus system $codes) |
| 27 |
(except (ikarus) make-code code-reloc-vector code-freevars |
| 28 |
code-size code-ref code-set! set-code-reloc-vector! |
| 29 |
procedure-annotation |
| 30 |
set-code-annotation!)) |
| 31 |
|
| 32 |
(define make-code |
| 33 |
(lambda (code-size freevars) |
| 34 |
(unless (and (fixnum? code-size) ($fx>= code-size 0)) |
| 35 |
(die 'make-code "not a valid code size" code-size)) |
| 36 |
(unless (and (fixnum? freevars) ($fx>= freevars 0)) |
| 37 |
(die 'make-code "not a valid number of free vars" freevars)) |
| 38 |
(foreign-call "ikrt_make_code" code-size freevars '#()))) |
| 39 |
|
| 40 |
(define code-reloc-vector |
| 41 |
(lambda (x) |
| 42 |
(unless (code? x) (die 'code-reloc-vector "not a code" x)) |
| 43 |
($code-reloc-vector x))) |
| 44 |
|
| 45 |
(define code-freevars |
| 46 |
(lambda (x) |
| 47 |
(unless (code? x) (die 'code-closure-size "not a code" x)) |
| 48 |
($code-freevars x))) |
| 49 |
|
| 50 |
(define code-size |
| 51 |
(lambda (x) |
| 52 |
(unless (code? x) (die 'code-size "not a code" x)) |
| 53 |
($code-size x))) |
| 54 |
|
| 55 |
(define code-set! |
| 56 |
(lambda (x i v) |
| 57 |
(unless (code? x) (die 'code-set! "not a code" x)) |
| 58 |
(unless (and (fixnum? i) |
| 59 |
($fx>= i 0) |
| 60 |
($fx< i ($code-size x))) |
| 61 |
(die 'code-set! "not a valid index" i)) |
| 62 |
(unless (and (fixnum? v) |
| 63 |
($fx>= v 0) |
| 64 |
($fx< v 256)) |
| 65 |
(die 'code-set! "not a valid byte" v)) |
| 66 |
($code-set! x i v))) |
| 67 |
|
| 68 |
(define code-ref |
| 69 |
(lambda (x i) |
| 70 |
(unless (code? x) (die 'code-ref "not a code" x)) |
| 71 |
(unless (and (fixnum? i) |
| 72 |
($fx>= i 0) |
| 73 |
($fx< i ($code-size x))) |
| 74 |
(die 'code-ref "not a valid index" i)) |
| 75 |
($code-ref x i))) |
| 76 |
|
| 77 |
(define set-code-reloc-vector! |
| 78 |
(lambda (x v) |
| 79 |
(unless (code? x) |
| 80 |
(die 'set-code-reloc-vector! "not a code" x)) |
| 81 |
(unless (vector? v) |
| 82 |
(die 'set-code-reloc-vector! "not a vector" v)) |
| 83 |
(foreign-call "ikrt_set_code_reloc_vector" x v))) |
| 84 |
|
| 85 |
|
| 86 |
(define set-code-annotation! |
| 87 |
(lambda (x v) |
| 88 |
(unless (code? x) |
| 89 |
(die 'set-code-annotation! "not a code" x)) |
| 90 |
(foreign-call "ikrt_set_code_annotation" x v))) |
| 91 |
|
| 92 |
(define code->thunk |
| 93 |
(lambda (x) |
| 94 |
(unless (code? x) |
| 95 |
(die 'code->thunk "not a a code object" x)) |
| 96 |
(unless ($fxzero? ($code-freevars x)) |
| 97 |
(die 'code->thunk "has free variables" x)) |
| 98 |
($code->closure x))) |
| 99 |
|
| 100 |
(define-struct annotation-indirect ()) |
| 101 |
(define (procedure-annotation x) |
| 102 |
(if (procedure? x) |
| 103 |
(let ([ae ($code-annotation ($closure-code x))]) |
| 104 |
(if (annotation-indirect? ae) |
| 105 |
($annotated-procedure-annotation x) |
| 106 |
ae)) |
| 107 |
(die 'procedure-annotation "not a procedure" x))) |
| 108 |
|
| 109 |
) |
| 110 |
|