2013-08-24 15:02:52 +02:00
|
|
|
;;; Continuation-passing style (CPS) intermediate language (IL)
|
|
|
|
|
|
2014-02-14 10:49:45 +01:00
|
|
|
;; Copyright (C) 2013, 2014 Free Software Foundation, Inc.
|
2013-08-24 15:02:52 +02:00
|
|
|
|
|
|
|
|
;;;; This library is free software; you can redistribute it and/or
|
|
|
|
|
;;;; modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
;;;; License as published by the Free Software Foundation; either
|
|
|
|
|
;;;; version 3 of the License, or (at your option) any later version.
|
|
|
|
|
;;;;
|
|
|
|
|
;;;; This library 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
|
|
|
|
|
;;;; Lesser General Public License for more details.
|
|
|
|
|
;;;;
|
|
|
|
|
;;;; You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
;;;; License along with this library; if not, write to the Free Software
|
|
|
|
|
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
;;;
|
|
|
|
|
;;; A pass to reify lone $prim's that were never folded into a
|
|
|
|
|
;;; $primcall, and $primcall's to primitives that don't have a
|
|
|
|
|
;;; corresponding VM op.
|
|
|
|
|
;;;
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
|
|
(define-module (language cps reify-primitives)
|
|
|
|
|
#:use-module (ice-9 match)
|
|
|
|
|
#:use-module (language cps)
|
|
|
|
|
#:use-module (language cps dfg)
|
|
|
|
|
#:use-module (language cps primitives)
|
2013-12-02 21:31:47 +01:00
|
|
|
#:use-module (language bytecode)
|
2013-08-24 15:02:52 +02:00
|
|
|
#:export (reify-primitives))
|
|
|
|
|
|
|
|
|
|
(define (module-box src module name public? bound? val-proc)
|
2014-03-28 16:29:16 +01:00
|
|
|
(let-fresh (kbox) (module-sym name-sym public?-sym bound?-sym box)
|
2013-08-24 15:02:52 +02:00
|
|
|
(build-cps-term
|
|
|
|
|
($letconst (('module module-sym module)
|
|
|
|
|
('name name-sym name)
|
|
|
|
|
('public? public?-sym public?)
|
|
|
|
|
('bound? bound?-sym bound?))
|
2013-11-07 10:32:21 +01:00
|
|
|
($letk ((kbox ($kargs ('box) (box) ,(val-proc box))))
|
|
|
|
|
($continue kbox src
|
2013-08-24 15:02:52 +02:00
|
|
|
($primcall 'cached-module-box
|
|
|
|
|
(module-sym name-sym public?-sym bound?-sym))))))))
|
|
|
|
|
|
2013-10-30 21:08:38 +01:00
|
|
|
(define (primitive-module name)
|
|
|
|
|
(case name
|
2014-02-14 10:49:45 +01:00
|
|
|
((bytevector-length
|
|
|
|
|
|
|
|
|
|
bytevector-u8-ref bytevector-u8-set!
|
2013-10-30 21:08:38 +01:00
|
|
|
bytevector-s8-ref bytevector-s8-set!
|
|
|
|
|
|
|
|
|
|
bytevector-u16-ref bytevector-u16-set!
|
|
|
|
|
bytevector-u16-native-ref bytevector-u16-native-set!
|
|
|
|
|
bytevector-s16-ref bytevector-s16-set!
|
|
|
|
|
bytevector-s16-native-ref bytevector-s16-native-set!
|
|
|
|
|
|
|
|
|
|
bytevector-u32-ref bytevector-u32-set!
|
|
|
|
|
bytevector-u32-native-ref bytevector-u32-native-set!
|
|
|
|
|
bytevector-s32-ref bytevector-s32-set!
|
|
|
|
|
bytevector-s32-native-ref bytevector-s32-native-set!
|
|
|
|
|
|
|
|
|
|
bytevector-u64-ref bytevector-u64-set!
|
|
|
|
|
bytevector-u64-native-ref bytevector-u64-native-set!
|
|
|
|
|
bytevector-s64-ref bytevector-s64-set!
|
|
|
|
|
bytevector-s64-native-ref bytevector-s64-native-set!
|
|
|
|
|
|
|
|
|
|
bytevector-ieee-single-ref bytevector-ieee-single-set!
|
|
|
|
|
bytevector-ieee-single-native-ref bytevector-ieee-single-native-set!
|
|
|
|
|
bytevector-ieee-double-ref bytevector-ieee-double-set!
|
|
|
|
|
bytevector-ieee-double-native-ref bytevector-ieee-double-native-set!)
|
|
|
|
|
'(rnrs bytevectors))
|
2013-11-30 16:40:17 +01:00
|
|
|
((class-of) '(oop goops))
|
2013-10-30 21:08:38 +01:00
|
|
|
(else '(guile))))
|
|
|
|
|
|
2013-11-07 10:32:21 +01:00
|
|
|
(define (primitive-ref name k src)
|
2013-10-30 21:08:38 +01:00
|
|
|
(module-box #f (primitive-module name) name #f #t
|
2013-08-24 15:02:52 +02:00
|
|
|
(lambda (box)
|
|
|
|
|
(build-cps-term
|
2013-11-07 10:32:21 +01:00
|
|
|
($continue k src ($primcall 'box-ref (box)))))))
|
2013-08-24 15:02:52 +02:00
|
|
|
|
2013-11-07 10:32:21 +01:00
|
|
|
(define (builtin-ref idx k src)
|
2014-03-28 16:29:16 +01:00
|
|
|
(let-fresh () (idx-sym)
|
VM has "builtins": primitives addressable by emitted RTL code
* libguile/Makefile.am:
* libguile/vm-builtins.h: New header, declaring stubs needed by the
compiler like values, apply, and abort-to-prompt.
* libguile/vm.c: Adapt the apply and values stubs to conform to a
standard interface. Add an abort-to-prompt stub. Add call/cc and
call-with-values stubs.
(scm_vm_builtin_ref): New helper, for the builtin-ref opcode.
(scm_vm_builtin_name_to_index)
(scm_vm_builtin_index_to_name): New helpers, for the compiler and
disassembler, respectively.
(scm_init_vm_builtins, scm_bootstrap_vm): Allow the compiler helpers
to be loaded later into a module.
* module/language/rtl.scm: Export builtin-index->name and
builtin-name->index.
* libguile/vm-engine.c (RETURN_VALUE_LIST): Update to use new names of
"apply" and "values".
(tail-call/shuffle): New opcode.
(abort): Update to be a tail VM op, and reorder and renumber other
ops.
(builtin-ref): New opcode.
* libguile/continuations.h:
* libguile/continuations.c (scm_i_call_with_current_continuation):
Move this to vm.[ch], implemented as a builtin.
* module/language/tree-il/compile-cps.scm (convert): Convert to
'abort-to-prompt calls, possibly with 'apply, effectively undoing the
tree-il transformation.
* module/language/cps/reify-primitives.scm (builtin-ref): New helper.
(reify-primitives): Convert builtin primitives to builtin-ref.
* module/language/cps/dfg.scm (constant-needs-allocation?):
* module/language/cps/compile-rtl.scm (emit-rtl-sequence): Add support
for compiling builtin-ref.
* module/system/vm/disassembler.scm (code-annotation): Add annotation
for builtin-ref.
2013-10-20 15:49:22 +02:00
|
|
|
(build-cps-term
|
|
|
|
|
($letconst (('idx idx-sym idx))
|
2013-11-07 10:32:21 +01:00
|
|
|
($continue k src
|
VM has "builtins": primitives addressable by emitted RTL code
* libguile/Makefile.am:
* libguile/vm-builtins.h: New header, declaring stubs needed by the
compiler like values, apply, and abort-to-prompt.
* libguile/vm.c: Adapt the apply and values stubs to conform to a
standard interface. Add an abort-to-prompt stub. Add call/cc and
call-with-values stubs.
(scm_vm_builtin_ref): New helper, for the builtin-ref opcode.
(scm_vm_builtin_name_to_index)
(scm_vm_builtin_index_to_name): New helpers, for the compiler and
disassembler, respectively.
(scm_init_vm_builtins, scm_bootstrap_vm): Allow the compiler helpers
to be loaded later into a module.
* module/language/rtl.scm: Export builtin-index->name and
builtin-name->index.
* libguile/vm-engine.c (RETURN_VALUE_LIST): Update to use new names of
"apply" and "values".
(tail-call/shuffle): New opcode.
(abort): Update to be a tail VM op, and reorder and renumber other
ops.
(builtin-ref): New opcode.
* libguile/continuations.h:
* libguile/continuations.c (scm_i_call_with_current_continuation):
Move this to vm.[ch], implemented as a builtin.
* module/language/tree-il/compile-cps.scm (convert): Convert to
'abort-to-prompt calls, possibly with 'apply, effectively undoing the
tree-il transformation.
* module/language/cps/reify-primitives.scm (builtin-ref): New helper.
(reify-primitives): Convert builtin primitives to builtin-ref.
* module/language/cps/dfg.scm (constant-needs-allocation?):
* module/language/cps/compile-rtl.scm (emit-rtl-sequence): Add support
for compiling builtin-ref.
* module/system/vm/disassembler.scm (code-annotation): Add annotation
for builtin-ref.
2013-10-20 15:49:22 +02:00
|
|
|
($primcall 'builtin-ref (idx-sym)))))))
|
|
|
|
|
|
2013-08-24 15:02:52 +02:00
|
|
|
(define (reify-clause ktail)
|
2014-03-28 16:29:16 +01:00
|
|
|
(let-fresh (kclause kbody kthrow) (wna false str eol throw)
|
2013-08-24 15:02:52 +02:00
|
|
|
(build-cps-cont
|
2013-11-07 10:32:21 +01:00
|
|
|
(kclause ($kclause ('() '() #f '() #f)
|
|
|
|
|
(kbody
|
|
|
|
|
($kargs () ()
|
|
|
|
|
($letconst (('wna wna 'wrong-number-of-args)
|
|
|
|
|
('false false #f)
|
|
|
|
|
('str str "Wrong number of arguments")
|
|
|
|
|
('eol eol '()))
|
|
|
|
|
($letk ((kthrow
|
|
|
|
|
($kargs ('throw) (throw)
|
|
|
|
|
($continue ktail #f
|
|
|
|
|
($call throw
|
|
|
|
|
(wna false str eol false))))))
|
2014-03-31 16:38:53 +02:00
|
|
|
,(primitive-ref 'throw kthrow #f)))))
|
|
|
|
|
,#f)))))
|
2013-08-24 15:02:52 +02:00
|
|
|
|
|
|
|
|
;; FIXME: Operate on one function at a time, for efficiency.
|
|
|
|
|
(define (reify-primitives fun)
|
2014-03-28 16:29:16 +01:00
|
|
|
(with-fresh-name-state fun
|
|
|
|
|
(let ((conts (build-cont-table fun)))
|
|
|
|
|
(define (visit-fun term)
|
|
|
|
|
(rewrite-cps-exp term
|
2014-04-10 10:50:17 +02:00
|
|
|
(($ $fun free body)
|
|
|
|
|
($fun free ,(visit-cont body)))))
|
2014-03-28 16:29:16 +01:00
|
|
|
(define (visit-cont cont)
|
|
|
|
|
(rewrite-cps-cont cont
|
|
|
|
|
(($ $cont sym ($ $kargs names syms body))
|
|
|
|
|
(sym ($kargs names syms ,(visit-term body))))
|
2014-04-10 12:11:35 +02:00
|
|
|
(($ $cont sym ($ $kfun src meta self (and tail ($ $cont ktail)) #f))
|
2014-03-28 16:29:16 +01:00
|
|
|
;; A case-lambda with no clauses. Reify a clause.
|
2014-04-10 12:11:35 +02:00
|
|
|
(sym ($kfun src meta self ,tail ,(reify-clause ktail))))
|
|
|
|
|
(($ $cont sym ($ $kfun src meta self tail clause))
|
|
|
|
|
(sym ($kfun src meta self ,tail ,(visit-cont clause))))
|
2014-03-31 16:38:53 +02:00
|
|
|
(($ $cont sym ($ $kclause arity body alternate))
|
|
|
|
|
(sym ($kclause ,arity ,(visit-cont body)
|
|
|
|
|
,(and alternate (visit-cont alternate)))))
|
2014-03-28 16:29:16 +01:00
|
|
|
(($ $cont)
|
|
|
|
|
,cont)))
|
|
|
|
|
(define (visit-term term)
|
|
|
|
|
(rewrite-cps-term term
|
|
|
|
|
(($ $letk conts body)
|
|
|
|
|
($letk ,(map visit-cont conts) ,(visit-term body)))
|
|
|
|
|
(($ $continue k src exp)
|
|
|
|
|
,(match exp
|
|
|
|
|
(($ $prim name)
|
2014-03-30 10:41:31 +02:00
|
|
|
(match (vector-ref conts k)
|
2014-03-28 16:29:16 +01:00
|
|
|
(($ $kargs (_))
|
|
|
|
|
(cond
|
|
|
|
|
((builtin-name->index name)
|
|
|
|
|
=> (lambda (idx)
|
|
|
|
|
(builtin-ref idx k src)))
|
|
|
|
|
(else (primitive-ref name k src))))
|
|
|
|
|
(_ (build-cps-term ($continue k src ($void))))))
|
|
|
|
|
(($ $fun)
|
|
|
|
|
(build-cps-term ($continue k src ,(visit-fun exp))))
|
|
|
|
|
(($ $primcall 'call-thunk/no-inline (proc))
|
|
|
|
|
(build-cps-term
|
|
|
|
|
($continue k src ($call proc ()))))
|
|
|
|
|
(($ $primcall name args)
|
|
|
|
|
(cond
|
|
|
|
|
((or (prim-instruction name) (branching-primitive? name))
|
|
|
|
|
;; Assume arities are correct.
|
|
|
|
|
term)
|
|
|
|
|
(else
|
|
|
|
|
(let-fresh (k*) (v)
|
|
|
|
|
(build-cps-term
|
|
|
|
|
($letk ((k* ($kargs (v) (v)
|
|
|
|
|
($continue k src ($call v args)))))
|
|
|
|
|
,(cond
|
|
|
|
|
((builtin-name->index name)
|
|
|
|
|
=> (lambda (idx)
|
|
|
|
|
(builtin-ref idx k* src)))
|
|
|
|
|
(else (primitive-ref name k* src)))))))))
|
|
|
|
|
(_ term)))))
|
|
|
|
|
|
|
|
|
|
(visit-fun fun))))
|