tmp-alloc.scm 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. ;; this is the most basic "dumb" allocator possible
  2. ;; it simply makes a new cell for each tmp,
  3. ;; it doesn't do any lifetime analysis
  4. (define (tmp-index tmp base tmps)
  5. (+ base (index tmp (queue:top tmps))))
  6. (define (tmp-alloc exp base tmps)
  7. (cond ((datum? exp) exp)
  8. ((var? exp)
  9. (if (eq? 'tmp (var-get-sort exp))
  10. (cond ((tmp-index (var-get-name exp) base tmps)
  11. => (lambda (i) `(var loc ,i)))
  12. (else (error 'tmp-alloc "unbound tmp variable" exp)))
  13. exp))
  14. ((if? exp)
  15. `(if ,(tmp-alloc (if-get-test exp) base tmps)
  16. ,(tmp-alloc (if-get-consequent exp) base tmps)
  17. ,(tmp-alloc (if-get-antecedent exp) base tmps)))
  18. ((allocate-closure? exp) exp)
  19. ((set-closure!? exp)
  20. (let ((clo (set-closure!-get-clo exp))
  21. (index (set-closure!-get-index exp))
  22. (value (set-closure!-get-value exp)))
  23. `(set-closure! ,(tmp-alloc clo base tmps)
  24. ,index
  25. ,(tmp-alloc value base tmps))))
  26. ((let? exp)
  27. (let ((tbl (mapply tmp-alloc-let-binding
  28. (let-get-bindings exp)
  29. base
  30. tmps))
  31. (body (let-get-body exp)))
  32. `(let ,tbl ,(tmp-alloc body base tmps))))
  33. ((application? exp)
  34. `(app . ,(mapply tmp-alloc (cdr exp) base tmps)))
  35. (else (error 'tmp-alloc "unknown data" exp))))
  36. (define (tmp-alloc-let-binding entry base tmps)
  37. (let ((result (tmp-alloc (cadr entry) base tmps)))
  38. (queue-push! tmps (car entry))
  39. (list (tmp-index (car entry) base tmps) result)))
  40. ;;
  41. (define (tmp-alloc-top top)
  42. (let* ((lbl (car top))
  43. (info (cadr top))
  44. (nm (caddr top))
  45. (num-args (cadddr top))
  46. (tmps (empty-queue))
  47. (res (tmp-alloc (cadddr (cdr top)) num-args tmps)))
  48. `(,lbl
  49. ,info
  50. ,nm
  51. ,(length (queue:top tmps))
  52. ,res)))