1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
#| modules.jl -- module handling for the compiler
$Id: modules.jl,v 1.44 2002/09/22 00:10:50 jsh Exp $
Copyright (C) 2000 John Harper <john@dcs.warwick.ac.uk>
This file is part of librep.
librep is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
librep 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 librep; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|#
(declare (unsafe-for-call/cc))
(define-structure rep.vm.compiler.modules
(export current-module
macro-env
variable-ref-p
locate-variable
compiler-symbol-value
compiler-boundp
compiler-binding-from-rep-p
compiler-binding-immutable-p
get-procedure-handler
get-language-property
compiler-macroexpand
compiler-macroexpand-1
compile-module-body
note-require
note-macro-def
compile-structure
compile-define-structure
compile-top-level-structure
compile-top-level-define-structure
compile-structure-ref
compile-function
compile-module)
(open rep
rep.structures
rep.vm.compiler.basic
rep.vm.compiler.bindings
rep.vm.compiler.utils
rep.vm.compiler.lap)
(define macro-env (make-fluid '())) ;alist of (NAME . MACRO-DEF)
(define default-macro-env (make-fluid '()))
;;; module environment of form being compiled
;; the name of the module being compiled in
(define current-module (make-fluid *user-structure*))
;; if true, the namespace of the module being compiled in; only
;; set when compiling code outside a module definition
(define current-structure (make-fluid
(get-structure (fluid current-module))))
(define current-language (make-fluid 'rep))
;; the names of the currently open and accessed modules
(define open-modules (make-fluid (and (fluid current-structure)
(structure-imports
(fluid current-structure)))))
(define accessed-modules (make-fluid (and (fluid current-structure)
(structure-accessible
(fluid current-structure)))))
;;; functions
(define (find-structure name)
(condition-case nil
(intern-structure name)
(file-error nil)))
;; return t if the module called STRUCT exports a variable called VAR
(defun module-exports-p (struct var)
(and (symbolp var)
(cond ((symbolp struct)
(let ((tem (find-structure struct)))
(and tem (structure-exports-p tem var))))
((structurep struct)
(structure-exports-p struct var)))))
;; return t if ARG is a structure reference form
(defun structure-ref-p (arg)
(and (eq (car arg) 'structure-ref)
(memq (locate-variable 'structure-ref) '(rep rep.module-system))))
;; return t if ARG refers to a variable
(defun variable-ref-p (arg)
(or (symbolp arg) (structure-ref-p arg)))
;; return the name of the structure exporting VAR to the current
;; structure, or nil
(defun locate-variable (var)
(if (structure-ref-p var)
(nth 1 var)
(let loop ((rest (fluid open-modules)))
(if rest
(if (module-exports-p (car rest) var)
(car rest)
(loop (cdr rest)))
;; it's not exported by any opened modules, if we have a handle
;; on the current module (i.e. we're compiling code not in
;; a module definition) try looking in that
(if (and (symbolp var) (fluid current-structure)
(structure-bound-p (fluid current-structure) var))
(fluid current-module)
nil)))))
(defun variable-stem (var)
(if (consp var)
(nth 2 var) ;structure-ref
var))
(defun symbol-value-1 (var)
(cond ((and (symbolp var) (special-variable-p var))
(symbol-value var))
((and (symbolp var) (fluid current-structure)
(structure-bound-p (fluid current-structure) var))
(%structure-ref (fluid current-structure) var))
((has-local-binding-p var) nil)
(t
(let* ((struct (locate-variable var))
(module (and struct (find-structure struct))))
(and module
(structure-bound-p module (variable-stem var))
(%structure-ref module (variable-stem var)))))))
;; if possible, return the value of variable VAR, else return nil
(defun compiler-symbol-value (var)
(let ((value (symbol-value-1 var)))
;; if the value is an autoload, try to load it
(if (and (closurep value)
(eq (car (closure-function value)) 'autoload))
(load-autoload value)
value)))
(defun compiler-boundp (var)
(and (symbolp var)
(or (locate-variable var)
(and (special-variable-p var) (boundp var)))))
;; return t if the binding of VAR comes from the rep (built-ins) module
(defun compiler-binding-from-rep-p (var)
(if (structure-ref-p var)
(eq (nth 1 var) 'rep)
(and (not (has-local-binding-p var))
(eq (locate-variable var) 'rep))))
;; return t if the binding of VAR is a known constant
;; (not including those in comp-constant-env)
(defun compiler-binding-immutable-p (var)
(and (not (has-local-binding-p var))
(let ((struct (locate-variable var)))
(and struct (binding-immutable-p (variable-stem var)
(find-structure struct))))))
(defun get-language-property (prop)
(and (fluid current-language) (get (fluid current-language) prop)))
(defun get-procedure-handler (name prop-name)
(unless (has-local-binding-p name)
(let*
((struct (locate-variable name))
(prop (and struct (get struct prop-name))))
(if (and prop (symbolp prop))
(get (variable-stem name) prop)
prop))))
(defun compiler-macroexpand-1 (form)
(when (and (consp form)
(symbolp (car form))
(not (has-local-binding-p (car form))))
(let* ((def (assq (car form) (fluid macro-env)))
;; make #<subr macroexpand> pass us any inner expansions
(macro-environment compiler-macroexpand-1))
(if def
(setq form (apply (cdr def) (cdr form)))
(setq def (compiler-symbol-value (car form)))
(when (and (eq (car def) 'macro) (functionp (cdr def)))
(when (and (closurep (cdr def))
(eq (car (closure-function (cdr def))) 'autoload))
(setq def (load-autoload (cdr def))))
(setq form (apply (cdr def) (cdr form)))))))
form)
(defun compiler-macroexpand (form #!optional pred)
(let loop ((in form))
(let
((out (compiler-macroexpand-1 in)))
;;(format standard-error "in: %S, out: %S\n" in out)
(if ((or pred eq) in out)
out
(loop out)))))
;; if OPENED or ACCESSED are `t', the current values are used
(defun call-with-module-env (thunk opened accessed)
(let-fluids ((macro-env (fluid default-macro-env))
(current-module (fluid current-module))
(current-structure (fluid current-structure))
(current-language (fluid current-language))
(open-modules (if (eq opened t)
(fluid open-modules)
opened))
(accessed-modules (if (eq accessed t)
(fluid accessed-modules)
accessed))
(const-env nil)
(inline-env nil)
(defuns nil)
(defvars (fluid defvars))
(defines nil)
(lexically-pure t)
(output-stream nil))
(thunk)))
(defun compile-module-body-1 (body)
(find-language-module)
(let
;; find language pass-1 and pass-2 compilers
((pass-1 (get-language-property 'compiler-pass-1))
(pass-2 (get-language-property 'compiler-pass-2)))
;; pass 1. remember definitions in the body for pass 2
(when pass-1
(setq body (pass-1 body)))
;; pass 2. the actual compilation
(when pass-2
(setq body (pass-2 body)))
;; return the compiled representation of the body
body))
(defun compile-module-body (body opened accessed)
(call-with-module-env
(lambda () (compile-module-body-1 body))
opened accessed))
(defun note-require (feature)
(unless (or (memq feature (fluid open-modules))
(and (fluid current-structure)
(eval `(featurep ',feature) (fluid current-structure))))
;; XXX this is broken; there's no way to tell if we're trying
;; XXX to load a module, or a bare file.
(cond ((get-structure feature)
;; structure already loaded..
(fluid-set open-modules (cons feature (fluid open-modules))))
((fluid current-structure)
;; try to require it..
(eval `(require ',feature) (fluid current-structure))
(when (get-structure feature)
(fluid-set open-modules (cons feature (fluid open-modules)))))
;; no current structure, try to load the file
;; as a module..
((intern-structure feature)
(fluid-set open-modules (cons feature (fluid open-modules))))
(t (compiler-warning "unable to require `%s'" feature)))))
;; XXX enclose macro defs in the *user-structure*, this is different
;; to with interpreted code
(defun note-macro-def (name body)
(fluid-set macro-env
(cons (cons name
(let ((closure (make-closure body)))
(set-closure-structure
closure (get-structure *user-structure*))
closure))
(fluid macro-env))))
(defun call-with-structure (thunk struct)
(let-fluids ((current-module (structure-name struct))
(current-structure struct)
(current-language nil))
(let-fluids ((open-modules (and (fluid current-structure)
(structure-imports
(fluid current-structure))))
(accessed-modules (and (fluid current-structure)
(structure-accessible
(fluid current-structure)))))
(find-language-module)
(thunk))))
(defun find-language-module ()
;; scan all opened modules for a known language
(catch 'out
(mapc (lambda (struct)
(if (get struct 'compiler-module)
(progn
(or (intern-structure (get struct 'compiler-module))
(compiler-error "unable to load module `%s'"
(get struct 'compiler-module)))
(fluid-set current-language struct)
(throw 'out))))
(fluid open-modules))
(fluid-set current-language 'no-lang)))
;;; declarations
;; (declare (in-module STRUCT))
(defun declare-in-module (form)
(fluid-set current-module (cadr form))
(fluid-set current-structure (intern-structure (fluid current-module))))
(put 'in-module 'compiler-decl-fun declare-in-module)
;; (declare (language LANG))
(defun declare-language (form)
(fluid-set current-language (cadr form)))
(put 'language 'compiler-decl-fun declare-language)
;;; module compilers
(defun compile-structure (form)
(compile-structure-def nil (cadr form) (cddr form)))
(defun compile-define-structure (form)
(compile-structure-def (cadr form) (caddr form) (cdddr form)))
(defun compile-top-level-structure (form)
(compile-structure-def nil (cadr form) (cddr form) t))
(defun compile-top-level-define-structure (form)
(compile-structure-def (cadr form) (caddr form) (cdddr form) t))
(defun compile-structure-def (name sig body #!optional top-level)
(let
((opened '(rep.module-system))
(accessed '())
(config (car body))
header)
(setq body (cdr body))
(unless (listp (car config))
(setq config (list config)))
(mapc (lambda (clause)
(case (car clause)
((open)
(setq opened (nconc (reverse (cdr clause)) opened))
(setq header (cons clause header)))
((access)
(setq accessed (nconc (reverse (cdr clause)) accessed))
(setq header (cons clause header)))
(t (setq header (cons clause header)))))
config)
(setq header (cons '(open rep.module-system) (nreverse header)))
(let-fluids ((current-structure nil)
(current-module name))
(call-with-module-env
(lambda ()
(setq body (compile-module-body-1 body))
(if top-level
(if name
`(define-structure ,name ,sig ,config ,@body)
`(structure ,sig ,config ,@body))
(compile-form-1 '%make-structure)
(compile-form-1 `(%parse-interface ',sig))
(if header
(progn
(compile-constant `(lambda () ,@header))
(emit-insn '(enclose)))
(compile-constant nil))
(if body
;; compile non-top-level structure bodies, so that
;; they can access the active bindings
(compile-lambda-constant `(lambda () ,@body))
(compile-constant nil))
(when name
(compile-constant name))
(emit-insn `(call ,(if name 4 3)))
(note-function-call-made)
(decrement-stack (if name 4 3))))
opened accessed))))
(defun compile-structure-ref (form)
(let
((struct (nth 1 form))
(var (nth 2 form)))
(or (memq struct (fluid accessed-modules))
(memq struct (fluid open-modules))
(compiler-error
"referencing non-accessible structure `%s'" struct))
(or (module-exports-p struct var)
(compiler-error
"referencing private variable `%s#%s'" struct var))
(compile-constant struct)
(compile-constant var)
(emit-insn '(structure-ref))
(decrement-stack)))
;;; exported top-level functions
(defun compile-function (function #!optional name)
"Compiles the body of the function FUNCTION."
(interactive "aFunction to compile:")
(let-fluids ((defuns nil)
(defvars nil)
(defines nil)
(current-fun function)
(output-stream nil))
(let ((body (closure-function function)))
(unless (bytecodep body)
(call-with-structure
(lambda ()
(set-closure-function function (compile-lambda body name)))
(closure-structure function)))
function)))
(defun compile-module (struct)
"Compiles all function bindings in the module named STRUCT."
(interactive "SModule name:")
(let ((struct (intern-structure struct)))
(when struct
(structure-walk (lambda (var value)
(when (closurep value)
(compile-function value var))) struct)))))
|