home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / t3_1 / risc_src.lha / risc_sources / comp / front_end / inf_files.t < prev    next >
Encoding:
Text File  |  1989-06-30  |  12.4 KB  |  327 lines

  1. (herald (front_end inf_files)
  2.   (env t (orbit_top defs)))
  3.  
  4. ;;; Copyright (c) 1985 Yale University
  5. ;;;     Authors: N Adams, R Kelsey, D Kranz, J Philbin, J Rees.
  6. ;;; This material was developed by the T Project at the Yale University Computer 
  7. ;;; Science Department.  Permission to copy this software, to redistribute it, 
  8. ;;; and to use it for any purpose is granted, subject to the following restric-
  9. ;;; tions and understandings.
  10. ;;; 1. Any copy made of this software must include this copyright notice in full.
  11. ;;; 2. Users of this software agree to make their best efforts (a) to return
  12. ;;;    to the T Project at Yale any improvements or extensions that they make,
  13. ;;;    so that these may be included in future releases; and (b) to inform
  14. ;;;    the T Project of noteworthy uses of this software.
  15. ;;; 3. All materials developed as a consequence of the use of this software
  16. ;;;    shall duly acknowledge such use, in accordance with the usual standards
  17. ;;;    of acknowledging credit in academic research.
  18. ;;; 4. Yale has made no warrantee or representation that the operation of
  19. ;;;    this software will be error-free, and Yale is under no obligation to
  20. ;;;    provide any services, by way of maintenance, update, or otherwise.
  21. ;;; 5. In conjunction with products arising from the use of this material,
  22. ;;;    there shall be no use of the name of the Yale University nor of any
  23. ;;;    adaptation thereof in any advertising, promotional, or sales literature
  24. ;;;    without prior written consent from Yale in each case.
  25. ;;;
  26.  
  27. ;;; An INF file contains declaration information that has been inferred from
  28. ;;; a source file.
  29.  
  30. ;;;                     SHAPE => FILE
  31. ;;;========================================================================
  32.  
  33. ;;; Write out the declaration information from SHAPE into a file.  The
  34. ;;; following things are written out:
  35. ;;;   a) Comexes for primops defined in the file.
  36. ;;;   b) Parents and arguments for parameterized primops defined in the file.
  37. ;;;   c) A list of environment records.
  38. ;;;   d) The source code for the comexes in part a) in case the file is
  39. ;;;      used on a different machine than the one it was produced on.
  40.  
  41. (define (write-support-file shape filename)
  42.   (with-open-ports ((output (open (information-filename filename) 'dump)))
  43.     (set-encoder output shape-encoder)
  44.     (receive (made new)
  45.              (partition-list primop.constructed? (shape-primops shape))
  46.       (write output (if (null? new)
  47.                         '() 
  48.                         (cons (if *compile-primops?*
  49.                                   (compile-support-primops new)
  50.                                   nil)
  51.                               (map primop.id new))))
  52.       (write output (map (lambda (p)
  53.                            `(,(primop.id p) . ,(primop.arglist p)))
  54.                          made))
  55.       (write output (new-environment-table->list (shape-new-env shape)))
  56.       (write output (map primop->executable new))
  57.       t)))
  58.  
  59. (define (compile-support-primops primops)
  60.   (orbit-primop-compile (primops->source-code primops) orbit-env))
  61.  
  62. ;;; Put all non-local definitions from TABLE into a list.
  63.  
  64. (define (new-environment-table->list table)
  65.   (let ((exp '()))
  66.     (table-walk table
  67.                 (lambda (name variable)
  68.                   (let ((def (variable-definition variable)))
  69.                     (if (not def)
  70.                         (bug '"new environment variable ~S has no def" variable))
  71.                     (if (not (memq? 'local (definition-data def)))
  72.                         (push exp (cons name def))))))
  73.     exp))
  74.  
  75. ;;;                FILE => TABLE
  76. ;;;========================================================================
  77.  
  78. ;;; Reading definition tables back from files.  The files are cached in
  79. ;;; DEFINITION-TABLES to aviod unnecessary work.
  80.  
  81. (define definition-tables (make-tree-table 'definition-tables))
  82.  
  83. ;;; Read the information file in if it hasn't already been done.
  84.  
  85. (define (get-definition-table name)
  86.   (let* ((file (module-name->filename name))
  87.          (time (file-write-date (information-filename file)))
  88.          (old (table-entry definition-tables name)))
  89.     (cond ((and old (= (car old) time))
  90.            (cdr old))
  91.           (else
  92.            (let ((defs (read-definitions-from-file name file)))
  93.              (set (table-entry definition-tables name) (cons time defs))
  94.              defs)))))
  95.  
  96. ;;; Retrieve the definitions.  This requires two readings of the file if
  97. ;;; the primop comexes in it are not suitable for the current system.
  98.  
  99. (define (read-definitions-from-file name filename)
  100.   (noise "Getting early bindings from ~S~%" (filename-name filename))
  101.   (cond ((maybe-read-definitions name filename nil)
  102.          => identity)
  103.         (else
  104.          (orbit-warning "primops in ~A are not compiled for this system.~%"
  105.                         (filename->string filename))
  106.          (let ((primops (load-and-evaluate-primops filename)))
  107.            (maybe-read-definitions name filename primops)))))
  108.  
  109. (define (maybe-read-definitions name filename primops)
  110.   (with-open-ports ((input (open (information-filename filename) 'retrieve)))
  111.     (set-decoder input shape-decoder)
  112.     (let ((comex+ids (read input)))
  113.       (cond ((null? comex+ids)
  114.              (resurrect-constructed-primops (read input))
  115.              (resurrect-definition-table (read input) name '()))
  116.             ((or primops
  117.                  (install-primop-comex comex+ids))
  118.              => (lambda (primops)
  119.                   (resurrect-constructed-primops (read input))
  120.                   (resurrect-definition-table (read input) name primops)))
  121.             (else nil)))))
  122.  
  123. (define (resurrect-constructed-primops data)
  124.   (walk (lambda (stuff)
  125.           (let ((primop (remake-primop (car stuff) (cdr stuff))))
  126.             (set (table-entry primop-table (primop.variant-id primop)) primop)))
  127.         data))
  128.  
  129. ;;; Actually put the definitions and primops into a table.
  130.  
  131. (define (resurrect-definition-table defs name primops)
  132.   (let ((table (make-definition-table name)))
  133.     (walk (lambda (p)
  134.             (add-primop table p))
  135.           primops)
  136.     (walk (lambda (p)
  137.             (set (table (car p)) (cdr p)))
  138.           defs)
  139.     table))
  140.  
  141. (define (install-primop-comex comex+ids)
  142.   (let ((comex (car comex+ids))
  143.         (ids (cdr comex+ids)))
  144.     (cond ((not (and comex (installable-comex? comex)))
  145.            nil)
  146.           (else
  147.            (instantiate-comex comex orbit-env)
  148.            (map (lambda (id)
  149.                    (table-entry primop-table id))
  150.                 ids)))))
  151.  
  152. ;;; Comexes are not yet labeled with their target system.
  153.  
  154. (define (installable-comex? c)
  155.   c)
  156.  
  157. ;;; Load the primop source code and compile and install them.
  158.  
  159. (define (load-and-evaluate-primops filename)
  160.   (with-open-ports ((input (open (information-filename filename) 'retrieve)))
  161.     (set-decoder input shape-no-primops-decoder)
  162.     (read input)   ; Ignore comex
  163.     (read input)   ; Ignore constructed primops
  164.     (read input)   ; Ignore definitions
  165.     (map (lambda (code)
  166.            (let ((primop (eval code orbit-env)))
  167.              (set (table-entry primop-table (primop.id primop)) primop)
  168.              primop))
  169.          (read input))))
  170.  
  171. ;;; Compiling the primop source code in an INF file.
  172. ;;; This needs to be given a complete file-spec including the extension.
  173.  
  174. (define (compile-primop-source file-spec)
  175.   (let ((filename (->filename file-spec)))
  176.     (receive (names constructed definitions sources)
  177.              (load-for-primop-compilation filename)
  178.       (cond ((null? sources)
  179.              (orbit-warning "No primops to compile in ~A"
  180.                             (filename->string filename)))
  181.             (else
  182.              (let* ((source (primop-executables->source-code names sources))
  183.                     (comex (orbit-primop-compile source orbit-env)))
  184.                (dump-with-primop-comex filename
  185.                                        (cons comex names)
  186.                                        constructed
  187.                                        definitions
  188.                                        source))))
  189.       (return))))
  190.  
  191. (define (load-for-primop-compilation filename)
  192.   (with-open-ports ((input (open filename 'retrieve)))
  193.     (set-decoder input shape-re-decoder)
  194.     (let* ((names       (cdr (read input)))
  195.            (constructed (read input))
  196.            (definitions (read input))
  197.            (source      (read input)))
  198.       (return names constructed definitions source))))
  199.  
  200. (define (dump-with-primop-comex filename comex constructed definitions source)
  201.   (with-open-ports ((output (open filename 'dump)))
  202.     (set-encoder output shape-re-encoder)
  203.     (write output comex)
  204.     (write output constructed)
  205.     (write output definitions)
  206.     (write output source)
  207.     (return)))
  208.  
  209. ;;;           Dumping and Retrieving Definition Records
  210. ;;;============================================================================
  211.  
  212. ;;; Definitions, primops, and comexes need to be dismantled and rebuilt.
  213.  
  214. ;;; Dumping
  215.  
  216. (define (shape-encoder x)
  217.   (cond ((primop?     x)
  218.          (if (and (primop.constructed? x)
  219.                   (not (primop.variant-id x)))
  220.              (return 'constructed-primop constructed-primop-accessors '())
  221.              (return 'primop primop-accessors '())))
  222.         ((definition? x) (return 'definition '() definition-accessors))
  223.         ((comex?      x) (return 'comex      '() comex-accessors))
  224.         (else
  225.          (return nil nil nil))))
  226.  
  227. (define definition-accessors
  228.   (list definition-data
  229.         definition-variant
  230.         definition-type
  231.         definition-value))
  232.  
  233. (define comex-accessors (stype-selectors comex-stype))
  234.  
  235. (define primop-accessors
  236.   (list any-primop-id))
  237.  
  238. (define constructed-primop-accessors
  239.   (list primop.id primop.arglist))
  240.  
  241. ;;; Retreiving
  242.  
  243. (define (shape-decoder x)
  244.   (case x
  245.     ((definition)         (return make-definition definition-accessors))
  246.     ((primop)             (return lookup-primop   '()))
  247.     ((constructed-primop) (return remake-primop   '()))
  248.     ((comex)              (return make-comex      comex-accessors))
  249.     (else                 (return nil             nil))))
  250.  
  251. (define (shape-no-primops-decoder x)
  252.   (case x
  253.     ((definition)         (return make-definition definition-accessors))
  254.     ((primop)             (return false           '()))
  255.     ((constructed-primop) (return false           '()))
  256.     ((comex)              (return make-comex      comex-accessors))
  257.     (else                 (return nil             nil))))
  258.  
  259. ;;; Primops are stored in a single global table.  Ugh.
  260.  
  261. (define (lookup-primop name)
  262.   (let ((primop (table-entry primop-table name)))
  263.     (cond ((not primop)
  264.            (bug '"cannot find primop ~S" name))
  265.           (else
  266.            primop))))
  267.  
  268. (define (remake-primop name args)
  269.   (let ((primop (table-entry primop-table name)))
  270.     (cond ((not primop)
  271.            (bug '"cannot find primop ~S" name))
  272.           ((null? args)
  273.            primop)
  274.           (else
  275.            (construct-primop primop args)))))
  276.  
  277. ;;; Reading and writing inf-files to compile their primops.
  278. ;;; Primops are retrieved as PSEUDO-PRIMOP structures so that they are not
  279. ;;; introduced into the global primop table.
  280.  
  281. (define (shape-re-decoder x)
  282.   (case x
  283.     ((definition)         (return make-definition      definition-accessors))
  284.     ((primop)             (return pseudo-lookup-primop '()))
  285.     ((constructed-primop) (return pseudo-remake-primop '()))
  286.     ((comex)              (return make-comex           comex-accessors))
  287.     (else                 (return nil                  nil))))
  288.  
  289. (define (pseudo-lookup-primop name)
  290.   (let ((primop (make-pseudo-primop)))
  291.     (set (pseudo-primop-constructed? primop) nil)
  292.     (set (pseudo-primop-id           primop) name)
  293.     (set (pseudo-primop-arglist      primop) nil)
  294.     primop))
  295.  
  296. (define (pseudo-remake-primop name args)
  297.   (let ((primop (make-pseudo-primop)))
  298.     (set (pseudo-primop-constructed? primop) t)
  299.     (set (pseudo-primop-id           primop) name)
  300.     (set (pseudo-primop-arglist      primop) args)
  301.     primop))
  302.  
  303. (define (shape-re-encoder x)
  304.   (cond ((pseudo-primop? x)
  305.          (if (pseudo-primop-constructed? x)
  306.              (return 'constructed-primop pseudo-constructed-primop-accessors '())
  307.              (return 'primop pseudo-primop-accessors '())))
  308.         ((definition? x) (return 'definition '() definition-accessors))
  309.         ((comex?      x) (return 'comex      '() comex-accessors))
  310.         (else
  311.          (return nil nil nil))))
  312.  
  313. (define-structure-type pseudo-primop
  314.   constructed?
  315.   id
  316.   arglist
  317.   )
  318.  
  319. (define pseudo-primop-accessors
  320.   (list pseudo-primop-id))
  321.  
  322. (define pseudo-constructed-primop-accessors
  323.   (list pseudo-primop-id pseudo-primop-arglist))
  324.  
  325.  
  326.  
  327.