~ubuntu-branches/ubuntu/trusty/librep/trusty

« back to all changes in this revision

Viewing changes to lisp/rep/vm/compiler/src.jl

  • Committer: Bazaar Package Importer
  • Author(s): Christian Marillat
  • Date: 2001-11-13 15:06:22 UTC
  • Revision ID: james.westby@ubuntu.com-20011113150622-vgmgmk6srj3kldr3
Tags: upstream-0.15.2
ImportĀ upstreamĀ versionĀ 0.15.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#| src.jl -- source code program transforms
 
2
 
 
3
   $Id: src.jl,v 1.8 2000/08/13 19:18:24 john Exp $
 
4
 
 
5
   Copyright (C) 2000 John Harper <john@dcs.warwick.ac.uk>
 
6
 
 
7
   This file is part of librep.
 
8
 
 
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)
 
12
   any later version.
 
13
 
 
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.
 
18
 
 
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.
 
22
|#
 
23
 
 
24
(declare (unsafe-for-call/cc))
 
25
 
 
26
(define-structure rep.vm.compiler.src
 
27
 
 
28
    (export coalesce-constants
 
29
            mash-constants
 
30
            source-code-transform)
 
31
 
 
32
    (open rep
 
33
          rep.vm.compiler.utils
 
34
          rep.vm.compiler.modules
 
35
          rep.vm.compiler.lap
 
36
          rep.vm.compiler.bindings
 
37
          rep.vm.bytecodes)
 
38
 
 
39
;;; Constant folding
 
40
 
 
41
  (defun foldablep (name)
 
42
    (unless (has-local-binding-p name)
 
43
      (let ((fun (get-procedure-handler name 'compiler-foldablep)))
 
44
        (and fun (fun name)))))
 
45
 
 
46
  (defun quote-constant (value)
 
47
    (if (or (symbolp value) (consp value))
 
48
        (list 'quote value)
 
49
      value))
 
50
 
 
51
  ;; This assumes that FORM is a list, and its car is one of the functions
 
52
  ;; in the comp-constant-functions list
 
53
  (defun fold-constants (form)
 
54
    (catch 'exit
 
55
      (let
 
56
          ((args (mapcar (lambda (arg)
 
57
                           (when (consp arg)
 
58
                             (setq arg (compiler-macroexpand arg)))
 
59
                           (when (and (consp arg) (foldablep (car arg)))
 
60
                             (setq arg (fold-constants arg)))
 
61
                           (if (compiler-constant-p arg)
 
62
                               (compiler-constant-value arg)
 
63
                             ;; Not a constant, abort, abort
 
64
                             (throw 'exit form)))
 
65
                         (cdr form))))
 
66
        ;; Now we have ARGS, the constant [folded] arguments from FORM
 
67
        (quote-constant (apply (compiler-symbol-value (car form)) args)))))
 
68
 
 
69
  (defun coalesce-constants (folder forms)
 
70
    (when forms
 
71
      (let loop ((result '())
 
72
                 (first (car forms))
 
73
                 (rest (cdr forms)))
 
74
        (cond ((null rest) (nreverse (cons first result)))
 
75
              ((and (compiler-constant-p first)
 
76
                    rest (compiler-constant-p (car rest)))
 
77
               (loop result
 
78
                     (quote-constant
 
79
                      (folder (compiler-constant-value first)
 
80
                              (compiler-constant-value (car rest))))
 
81
                     (cdr rest)))
 
82
              (t (loop (cons first result) (car rest) (cdr rest)))))))
 
83
 
 
84
  (defun mash-constants (folder forms)
 
85
    (let ((consts (filter compiler-constant-p forms))
 
86
          (non-consts (filter (lambda (x)
 
87
                                (not (compiler-constant-p x))) forms)))
 
88
      (if consts
 
89
          (cons (quote-constant
 
90
                 (apply folder (mapcar compiler-constant-value consts)))
 
91
                non-consts)
 
92
        non-consts)))
 
93
 
 
94
;;; Entry point
 
95
 
 
96
  (defun source-code-transform (form)
 
97
    (let (tem)
 
98
      ;; first try constant folding
 
99
      (when (and (consp form) (foldablep (car form)))
 
100
        (setq form (fold-constants form)))
 
101
 
 
102
      ;; then look for a specific tranformer
 
103
      (when (and (symbolp (car form))
 
104
                 (setq tem (get-procedure-handler
 
105
                            (car form) 'compiler-transform-property)))
 
106
        (setq form (tem form)))
 
107
 
 
108
      form)))