1
#| lap.jl -- intermediate code management
3
$Id: lap.jl,v 1.6 2000/08/13 19:18:24 john Exp $
5
Copyright (C) 2000 John Harper <john@dcs.warwick.ac.uk>
7
This file is part of librep.
9
librep is free software; you can redistribute it and/or modify it
10
under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2, or (at your option)
14
librep is distributed in the hope that it will be useful, but
15
WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
GNU General Public License for more details.
19
You should have received a copy of the GNU General Public License
20
along with librep; see the file COPYING. If not, write to
21
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
(declare (unsafe-for-call/cc))
26
(define-structure rep.vm.compiler.lap
28
(export intermediate-code
41
rep.vm.compiler.bindings)
43
(define saved-state (make-fluid))
45
;; list of (INSN . [ARG]), (TAG . REFS)
46
(define intermediate-code (make-fluid '()))
48
;; Output one opcode and its optional argument
49
(define (emit-insn insn)
51
;; so the peepholer can safely modify code
52
(setq insn (copy-sequence insn)))
53
(fluid-set intermediate-code (cons insn (fluid intermediate-code))))
56
(define make-label gensym)
58
;; Arrange for the address of LABEL to be pushed onto the stack
59
(define (push-label-addr label)
60
(emit-insn `(push-label ,label))
63
;; Set the address of the label LABEL to the current pc
64
(define fix-label emit-insn)
66
(define (prefix-label label)
67
(fluid-set intermediate-code (nconc (list label)
68
(fluid intermediate-code))))
71
(fluid-set saved-state
72
(cons (list (cons intermediate-code (fluid intermediate-code))
73
(cons spec-bindings (fluid spec-bindings))
77
(fluid lex-bindings)))
78
(cons lexically-pure (fluid lexically-pure))
79
(cons current-stack (fluid current-stack))
80
(cons max-stack (fluid max-stack))
81
(cons current-b-stack (fluid current-b-stack))
82
(cons max-b-stack (fluid max-b-stack)))
83
(fluid saved-state))))
86
(fluid-set saved-state (cdr (fluid saved-state))))
88
;; reload lex-bindings value, preserving eq-ness of cells
89
(define (reload-lex-bindings saved)
90
(let loop ((rest (fluid lex-bindings)))
91
(if (eq (caar rest) (caar saved))
93
(fluid-set lex-bindings rest)
94
(do ((old rest (cdr old))
95
(new saved (cdr new)))
97
(rplacd (car old) (cdr (car new)))))
100
(define (reload-state)
102
(if (eq (car cell) lex-bindings)
103
(reload-lex-bindings (cdr cell))
104
(fluid-set (car cell) (cdr cell))))
105
(car (fluid saved-state)))))