home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / src-16f.lha / compiler / bcgen.lisp < prev    next >
Encoding:
Text File  |  1991-11-06  |  5.4 KB  |  183 lines

  1. ;;; -*- Package: C; Log: C.Log -*-
  2. ;;;
  3. ;;; **********************************************************************
  4. ;;; This code was written as part of the CMU Common Lisp project at
  5. ;;; Carnegie Mellon University, and has been placed in the public domain.
  6. ;;; If you want to use this code or any part of CMU Common Lisp, please contact
  7. ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
  8. ;;;
  9. (ext:file-comment
  10.   "$Header: bcgen.lisp,v 1.2 91/02/20 14:56:40 ram Exp $")
  11. ;;;
  12. ;;; **********************************************************************
  13. ;;;
  14. ;;;    A back end for the compiler that generates an interpreted byte code
  15. ;;; instead of native code.
  16. ;;;
  17. ;;; Written by Rob MacLachlan
  18. ;;;
  19. (in-package 'c)
  20.  
  21. ;;;; Data structures:
  22.  
  23. ;;; The Continuation-Info is a representation of the number of values which
  24. ;;; should be pushed when something is evaluated with that continuation:
  25. ;;; -- A positive integer specifies a fixed number of values
  26. ;;; -- NIL specifies no values
  27. ;;; -- :Multiple specifies an arbitrary number of values, with a values count
  28. ;;;    on top. 
  29. ;;;
  30. ;;; ### Also need an MV-call variant that increments the values count already
  31. ;;; on TOS (???)  I guess we could have a coalesce-values Xop that takes the
  32. ;;; top N values globs, squeezes out the values counts and pushes the total
  33. ;;; count.
  34.  
  35. ;;; The Leaf-Loc has various interpretations depending on the kind of leaf:
  36. ;;; -- In a lambda-var, it is something or other that specifies the argument or
  37. ;;;    local that the value is found in for non-closure variables in their home
  38. ;;;    environment.
  39. ;;; -- In a functional, it is the closure that represents that functional. (???)
  40.  
  41. ;;; The Environment-Info is the number of local variables allocated in the
  42. ;;; environment.
  43.  
  44. ;;; The BC-Block structure is used to annotate blocks with information that we
  45. ;;; need to generate byte code.  This structure is stored in the Block-Info.
  46. ;;;
  47. (defstruct bc-block
  48.   ;;
  49.   ;; The label for the start of this block. 
  50.   label
  51.   ;;
  52.   ;; Lists of continuations representing the values on the stack at the
  53.   ;; beginning and end of this block.  The first continuation is on top, second
  54.   ;; underneath, etc.
  55.   (start-conts () :type list)
  56.   (end-conts () :type list))
  57.  
  58.  
  59. ;;; Generate-Byte-Code  --  Interface
  60. ;;;
  61. ;;;    Generate byte code to implement the functions in Component.
  62. ;;;
  63. (proclaim '(function generate-byte-code (component) void))
  64. (defun generate-byte-code (component)
  65.   (allocate-variables component)
  66.   (stack-analyze component))
  67.  
  68.  
  69. ;;; Default-Values  --  Internal
  70. ;;;
  71. ;;;    Push any extra values expected by Cont, given that Count values have
  72. ;;; already been pushed.
  73. ;;;
  74. (proclaim '(function default-values (continuation (integer 1)) void))
  75. (defun default-values (cont count)
  76.  
  77.   (when (eq for-value :multiple)
  78.     (inst push-ic-0 1))
  79.   )
  80.  
  81.  
  82. ;;; Call-Sys-Function  --  Internal
  83. ;;;
  84. ;;;    Call a system constant function.
  85.  
  86.  
  87.   
  88. (defun byte-code-generate-block (block)
  89.   (let ((last (block-last block)))
  90.     (do ((node (continuation-next (block-start block))
  91.            (continuation-next (node-cont node))))
  92.     (())
  93.       (etypecase node
  94.     (ref
  95.      (when for-value
  96.        (let* ((leaf (ref-leaf ref))
  97.           (name (leaf-name leaf)))
  98.          (etypecase leaf
  99.            (constant
  100.         (let ((value (constant-value leaf)))
  101.           (cond ((or (not name) (numberp value) (characterp value)
  102.                  (and (symbolp value) (symbol-package value)))
  103.              (push-constant value))
  104.             (t
  105.              (push-constant name)
  106.              (call-sys-function 'symbol-value)))))
  107.            (global-var
  108.         (push-constant name)
  109.         (ecase (global-var-kind leaf)
  110.           (:global-function
  111.            (call-sys-function 'symbol-function))
  112.           ((:constant :special :global)
  113.            (call-sys-function 'symbol-value))))
  114.            (lambda-var
  115.         (let ((closure (lambda-var-closure leaf)))
  116.           (cond (closure
  117.              (push-closure closure current-env)
  118.              (push-closure-slot leaf closure))
  119.             ((eq (lambda-environment (lambda-var-home leaf))
  120.                  current-env)
  121.              (push-al (leaf-loc leaf)))
  122.             (t
  123.              (push-closure-slot leaf (environment-closure current-env))))))
  124.            (functional
  125.         (push-closure (leaf-loc leaf) current-env))))
  126.  
  127.        (default-values cont 1)))
  128.     (if
  129.      (let* ((next-block (block-next block))
  130.         (consequent (continuation-block (if-consequent node)))
  131.         (c-label (bc-block-label (block-info consequent)))
  132.         (alternative (continuation-block (if-alternative node)))
  133.         (a-label (bc-block-label (block-info alternative))))
  134.        (cond ((eq consequent next-block)
  135.           (inst branch-false a-label))
  136.          ((eq alternative next-block)
  137.           (inst branch-true c-label))
  138.          (t
  139.           (inst branch-true c-label)
  140.           (inst branch a-label)))))
  141.     (set
  142.      ;;
  143.      ;; Similar to Ref:
  144.      ;;    Local lexical
  145.      ;;    Special
  146.      ;;    Closure
  147.      )
  148.     (combination
  149.      ;;
  150.      ;; Cases:
  151.      ;;    Funny function:
  152.      ;;        Catch, UWP, Specbind.  Use Xop.
  153.      ;;    If a Let, just pop the args into the appropriate locals and jump.
  154.      ;;    System constant function.
  155.      ;;    Other...
  156.      )
  157.     (mv-combination
  158.      ;;
  159.      ;; Cases:
  160.      ;;    Funny function: Throw.
  161.      ;;    Local call:
  162.      ;;        MV-Bind
  163.      ;;    Full call
  164.      )    
  165.     (bind
  166.      ;;
  167.      ;; Allocate closure stuff that is allocated at this node.  Allocate
  168.      ;; locals.  Move any set arguments into the local we keep them in.
  169.      )
  170.     (return
  171.      (inst return)))
  172.       
  173.       (when (eq node last) (return)))))
  174.  
  175. #| Need to inhibit generation of:
  176. Controlled by setting of Continuation-Info (a.k.a. for-value).
  177.  
  178. Arguments to funny functions
  179.  
  180. Functions for calls where we don't want the value to be pushed because it is a
  181. local call or a call to a system constant function.
  182. |#
  183.