cps.scm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ; Copyright (c) 1993-2008 by Richard Kelsey. See file COPYING.
  2. ; (cps-call <primop> <exits> <first-arg-index> <args> <cps>) ->
  3. ; <call-node> + <top-call-node> + <bottom-lambda-node>
  4. ;
  5. ; (cps-sequence <nodes> <cps>) -> <last-node> + <top-call> + <bottom-lambda>
  6. ;
  7. ; (<cps> <node>) -> <value-node> + <top-call-node> + <bottom-lambda-node>
  8. (define (cps-call primop exits first-arg-index args cps)
  9. (let ((call (make-call-node primop
  10. (+ (length args) first-arg-index)
  11. exits))
  12. (arguments (make-arg-nodes args first-arg-index cps)))
  13. (let loop ((args arguments) (first #f) (last #f))
  14. (if (null? args)
  15. (values call first last)
  16. (let ((arg (car args)))
  17. (attach call (arg-index arg) (arg-value arg))
  18. (if (and last (arg-first arg))
  19. (attach-body last (arg-first arg)))
  20. (loop (cdr args)
  21. (or first (arg-first arg))
  22. (or (arg-last arg) last)))))))
  23. ; Record to hold information about arguments to calls.
  24. (define-record-type arg :arg
  25. (make-arg index rank value first last)
  26. arg?
  27. (index arg-index) ; The index of this argument in the call.
  28. (rank arg-rank) ; The estimated cost of executing this node at run time.
  29. (value arg-value) ; What CPS returned for this argument.
  30. (first arg-first)
  31. (last arg-last))
  32. ; Convert the elements of EXP into nodes (if they aren't already) and put
  33. ; them into an ARG record. Returns the list of ARG records sorted
  34. ; by ARG-RANK.
  35. (define (make-arg-nodes exp start cps)
  36. (do ((index start (+ index 1))
  37. (args exp (cdr args))
  38. (vals '() (cons (receive (value first last)
  39. (cps (car args))
  40. (make-arg index (node-rank first) value first last))
  41. vals)))
  42. ((null? args)
  43. (sort-list vals
  44. (lambda (v1 v2)
  45. (> (arg-rank v1) (arg-rank v2)))))))
  46. ; Complexity analysis used to order argument evaluation. More complex
  47. ; arguments are to be evaluated first. This just counts reference nodes.
  48. ; It is almost certainly a waste of time.
  49. (define (node-rank first)
  50. (if (not first)
  51. 0
  52. (complexity-analyze-vector (call-args first))))
  53. (define (complexity-analyze node)
  54. (cond ((empty? node)
  55. 0)
  56. ((reference-node? node)
  57. 1)
  58. ((lambda-node? node)
  59. (if (not (empty? (lambda-body node)))
  60. (complexity-analyze-vector (call-args (lambda-body node)))
  61. 0))
  62. ((call-node? node)
  63. (complexity-analyze-vector (call-args node)))
  64. (else
  65. 0)))
  66. (define (complexity-analyze-vector vec)
  67. (do ((i 0 (+ i 1))
  68. (q 0 (+ q (complexity-analyze (vector-ref vec i)))))
  69. ((>= i (vector-length vec))
  70. q)))
  71. ;----------------------------------------------------------------
  72. ; (cps-sequence <nodes> <values-cps>) ->
  73. ; <last-node> + <top-call> + <bottom-lambda>
  74. ; <values-cps> is the same as the <cps> used above, except that it returns
  75. ; a list of value nodes instead of exactly one.
  76. (define (cps-sequence nodes values-cps)
  77. (if (null? nodes)
  78. (bug "CPS: empty sequence"))
  79. (let loop ((nodes nodes) (first #f) (last #f))
  80. (if (null? (cdr nodes))
  81. (values (car nodes) first last)
  82. (receive (exp-first exp-last)
  83. (cps-sequent (car nodes) values-cps)
  84. (if (and last exp-first)
  85. (attach-body last exp-first))
  86. (loop (cdr nodes) (or first exp-first) (or exp-last last))))))
  87. (define (cps-sequent node values-cps)
  88. (receive (vals exp-first exp-last)
  89. (values-cps node)
  90. (receive (calls other)
  91. (partition-list call-node? vals)
  92. (map erase other)
  93. (if (null? calls)
  94. (values exp-first exp-last)
  95. (insert-let calls exp-first exp-last)))))
  96. (define (insert-let calls exp-first exp-last)
  97. (let* ((vars (map (lambda (call)
  98. (make-variable 'v (trivial-call-return-type call)))
  99. calls))
  100. (cont (make-lambda-node 'c 'cont vars))
  101. (call (make-call-node (get-primop (enum primop let))
  102. (+ 1 (length calls))
  103. 1)))
  104. (attach-call-args call (cons cont calls))
  105. (cond (exp-first
  106. (attach-body exp-last call)
  107. (values exp-first cont))
  108. (else
  109. (values call cont)))))