2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
2008-01-29 00:34:34 -05:00
|
|
|
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
|
2007-10-25 16:27:34 -04:00
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2007-05-05 20:54:57 -04:00
|
|
|
|
2007-05-15 10:18:58 -04:00
|
|
|
(library (ikarus code-objects)
|
2007-05-05 20:54:57 -04:00
|
|
|
(export
|
2007-05-06 22:48:10 -04:00
|
|
|
make-code code-reloc-vector code-freevars
|
|
|
|
code-size code-ref code-set! set-code-reloc-vector!
|
2007-09-04 20:18:11 -04:00
|
|
|
set-code-annotation! procedure-annotation
|
2007-05-06 22:48:10 -04:00
|
|
|
code->thunk)
|
2007-05-05 20:54:57 -04:00
|
|
|
(import
|
2007-05-06 17:59:32 -04:00
|
|
|
(ikarus system $fx)
|
|
|
|
(ikarus system $codes)
|
2007-05-06 22:48:10 -04:00
|
|
|
(except (ikarus) make-code code-reloc-vector code-freevars
|
2007-09-04 19:59:14 -04:00
|
|
|
code-size code-ref code-set! set-code-reloc-vector!
|
2007-09-04 20:18:11 -04:00
|
|
|
procedure-annotation
|
2007-09-04 19:59:14 -04:00
|
|
|
set-code-annotation!))
|
2007-05-05 20:54:57 -04:00
|
|
|
|
|
|
|
(define make-code
|
|
|
|
(lambda (code-size freevars)
|
|
|
|
(unless (and (fixnum? code-size) ($fx>= code-size 0))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'make-code "not a valid code size" code-size))
|
2007-05-05 20:54:57 -04:00
|
|
|
(unless (and (fixnum? freevars) ($fx>= freevars 0))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'make-code "not a valid number of free vars" freevars))
|
2007-05-05 20:54:57 -04:00
|
|
|
(foreign-call "ikrt_make_code" code-size freevars '#())))
|
|
|
|
|
|
|
|
(define code-reloc-vector
|
|
|
|
(lambda (x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(unless (code? x) (die 'code-reloc-vector "not a code" x))
|
2007-05-05 20:54:57 -04:00
|
|
|
($code-reloc-vector x)))
|
|
|
|
|
|
|
|
(define code-freevars
|
|
|
|
(lambda (x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(unless (code? x) (die 'code-closure-size "not a code" x))
|
2007-05-05 20:54:57 -04:00
|
|
|
($code-freevars x)))
|
|
|
|
|
|
|
|
(define code-size
|
|
|
|
(lambda (x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(unless (code? x) (die 'code-size "not a code" x))
|
2007-05-05 20:54:57 -04:00
|
|
|
($code-size x)))
|
|
|
|
|
|
|
|
(define code-set!
|
|
|
|
(lambda (x i v)
|
2007-12-15 08:22:49 -05:00
|
|
|
(unless (code? x) (die 'code-set! "not a code" x))
|
2007-05-05 20:54:57 -04:00
|
|
|
(unless (and (fixnum? i)
|
|
|
|
($fx>= i 0)
|
|
|
|
($fx< i ($code-size x)))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'code-set! "not a valid index" i))
|
2007-05-05 20:54:57 -04:00
|
|
|
(unless (and (fixnum? v)
|
|
|
|
($fx>= v 0)
|
|
|
|
($fx< v 256))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'code-set! "not a valid byte" v))
|
2007-05-05 20:54:57 -04:00
|
|
|
($code-set! x i v)))
|
|
|
|
|
|
|
|
(define code-ref
|
|
|
|
(lambda (x i)
|
2007-12-15 08:22:49 -05:00
|
|
|
(unless (code? x) (die 'code-ref "not a code" x))
|
2007-05-05 20:54:57 -04:00
|
|
|
(unless (and (fixnum? i)
|
|
|
|
($fx>= i 0)
|
|
|
|
($fx< i ($code-size x)))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'code-ref "not a valid index" i))
|
2007-05-05 20:54:57 -04:00
|
|
|
($code-ref x i)))
|
|
|
|
|
|
|
|
(define set-code-reloc-vector!
|
|
|
|
(lambda (x v)
|
2007-05-06 22:49:33 -04:00
|
|
|
(unless (code? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'set-code-reloc-vector! "not a code" x))
|
2007-05-05 20:54:57 -04:00
|
|
|
(unless (vector? v)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'set-code-reloc-vector! "not a vector" v))
|
2007-05-05 20:54:57 -04:00
|
|
|
(foreign-call "ikrt_set_code_reloc_vector" x v)))
|
2007-05-06 22:48:10 -04:00
|
|
|
|
2007-09-04 19:59:14 -04:00
|
|
|
|
|
|
|
(define set-code-annotation!
|
|
|
|
(lambda (x v)
|
|
|
|
(unless (code? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'set-code-annotation! "not a code" x))
|
2007-09-04 19:59:14 -04:00
|
|
|
(foreign-call "ikrt_set_code_annotation" x v)))
|
|
|
|
|
2007-05-06 22:48:10 -04:00
|
|
|
(define code->thunk
|
|
|
|
(lambda (x)
|
2007-05-06 22:49:33 -04:00
|
|
|
(unless (code? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'code->thunk "not a a code object" x))
|
2007-05-06 22:48:10 -04:00
|
|
|
(unless ($fxzero? ($code-freevars x))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'code->thunk "has free variables" x))
|
2007-05-06 22:48:10 -04:00
|
|
|
($code->closure x)))
|
|
|
|
|
2007-09-04 20:18:11 -04:00
|
|
|
(define (procedure-annotation x)
|
|
|
|
(if (procedure? x)
|
|
|
|
($code-annotation ($closure-code x))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'procedure-annotation "not a procedure" x)))
|
2007-09-04 20:18:11 -04:00
|
|
|
|
2007-05-05 20:54:57 -04:00
|
|
|
)
|
|
|
|
|