primop.scm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Marcus Crestani
  3. (define prescheme-primop-table (make-symbol-table))
  4. (walk-vector (lambda (primop)
  5. (if (primop? primop)
  6. (table-set! prescheme-primop-table
  7. (primop-id primop)
  8. primop)))
  9. all-primops)
  10. (define (get-prescheme-primop id)
  11. (cond ((table-ref prescheme-primop-table id)
  12. => identity)
  13. ((name->enumerand id primop)
  14. => get-primop)
  15. (else
  16. (bug "Scheme primop ~A not found" id))))
  17. (define (add-scheme-primop! id primop)
  18. (table-set! prescheme-primop-table id primop))
  19. (define-syntax define-scheme-primop
  20. (syntax-rules ()
  21. ((define-scheme-primop id type)
  22. (define-scheme-primop id #f type))
  23. ((define-scheme-primop id side-effects type)
  24. (define-scheme-primop id side-effects type default-simplifier))
  25. ((define-scheme-primop id side-effects type simplifier)
  26. (define-polymorphic-scheme-primop
  27. id side-effects (lambda (call) type) simplifier))))
  28. (define-syntax define-polymorphic-scheme-primop
  29. (syntax-rules ()
  30. ((define-polymorphic-scheme-primop id type)
  31. (define-polymorphic-scheme-primop id #f type))
  32. ((define-polymorphic-scheme-primop id side-effects type)
  33. (define-polymorphic-scheme-primop id side-effects type default-simplifier))
  34. ((define-scheme-primop id side-effects type simplifier)
  35. (add-scheme-primop! 'id
  36. (make-primop 'id #t 'side-effects simplifier
  37. (lambda (call) 1)
  38. type)))))
  39. (define-syntax define-nonsimple-scheme-primop
  40. (syntax-rules ()
  41. ((define-nonsimple-scheme-primop id)
  42. (define-nonsimple-scheme-primop id #f))
  43. ((define-nonsimple-scheme-primop id side-effects)
  44. (define-nonsimple-scheme-primop id side-effects default-simplifier))
  45. ((define-nonsimple-scheme-primop id side-effects simplifier)
  46. (add-scheme-primop! 'id
  47. (make-primop 'id #f 'side-effects simplifier
  48. (lambda (call) 1)
  49. 'nontrivial-primop)))))
  50. (define-syntax define-scheme-cond-primop
  51. (syntax-rules ()
  52. ((define-scheme-cond-primop id simplifier expand simplify?)
  53. (add-scheme-primop! 'id
  54. (make-conditional-primop 'id
  55. #f
  56. simplifier
  57. (lambda (call) 1)
  58. expand
  59. simplify?)))))
  60. ;(define-prescheme! 'error ; all four args must be present if used as value
  61. ; (lambda (exp env)
  62. ; (let ((string (expand (cadr exp) env #f))
  63. ; (args (map (lambda (arg)
  64. ; (expand arg env #f))
  65. ; (cddr exp))))
  66. ; (make-block-exp
  67. ; (list
  68. ; (make-call-exp (get-prescheme-primop 'error)
  69. ; 0
  70. ; type/unknown
  71. ; `(,string
  72. ; ,(make-quote-exp (length args) type/int32)
  73. ; . ,(case (length args)
  74. ; ((0)
  75. ; (list (make-quote-exp 0 type/int32)
  76. ; (make-quote-exp 0 type/int32)
  77. ; (make-quote-exp 0 type/int32)))
  78. ; ((1)
  79. ; (list (car args)
  80. ; (make-quote-exp 0 type/int32)
  81. ; (make-quote-exp 0 type/int32)))
  82. ; ((2)
  83. ; (list (car args)
  84. ; (cadr args)
  85. ; (make-quote-exp 0 type/int32)))
  86. ; ((3)
  87. ; args)
  88. ; (else
  89. ; (error "too many arguments to ERROR in ~S" exp))))
  90. ; exp)
  91. ; (make-quote-exp the-undefined-value type/unknown))))))
  92. ; For the moment VALUES is more or less a macro.
  93. ;(define-prescheme! 'values ; dies if used as a value
  94. ; (lambda (exp env)
  95. ; (make-call-exp (get-prescheme-primop 'pack)
  96. ; 0
  97. ; type/unknown
  98. ; (map (lambda (arg)
  99. ; (expand arg env #f))
  100. ; (cdr exp))
  101. ; exp)))
  102. ; Each arg spec is either #F = non-continuation argument or a list of
  103. ; variable (name . type)s for the continuation.
  104. ;(define (define-continuation-expander id primop-id arg-specs)
  105. ; (define-primitive-expander id (length arg-specs)
  106. ; (lambda (source args cenv)
  107. ; (receive (conts other)
  108. ; (expand-arguments args arg-specs cenv)
  109. ; (make-call-exp (get-prescheme-primop primop-id)
  110. ; (length conts)
  111. ; type/unknown
  112. ; (append conts other)
  113. ; source)))))
  114. ;(define (expand-arguments args specs cenv)
  115. ; (let loop ((args args) (specs specs) (conts '()) (other '()))
  116. ; (if (null? args)
  117. ; (values (reverse conts) (reverse other))
  118. ; (let ((arg (expand (car args) cenv #f)))
  119. ; (if (not (car specs))
  120. ; (loop (cdr args) (cdr specs) conts (cons arg other))
  121. ; (loop (cdr args) (cdr specs)
  122. ; (cons (expand-continuation-arg arg (car specs))
  123. ; conts)
  124. ; other))))))
  125. ;
  126. ;(define (expand-continuation-arg arg var-specs)
  127. ; (let* ((vars (map (lambda (p)
  128. ; (make-variable (car p) (cdr p)))
  129. ; var-specs)))
  130. ; (make-continuation-exp
  131. ; vars
  132. ; (make-call-exp (get-primop (enum primop unknown-call))
  133. ; 0
  134. ; type/unknown
  135. ; `(,arg
  136. ; ,(make-quote-exp (length vars) #f)
  137. ; . ,vars)
  138. ; #f)))) ; no source
  139. ; Randomness needed by both arith.scm and c-arith.scm.
  140. ; What we will get in C.
  141. (define int-mask (- (arithmetic-shift 1 pre-scheme-integer-size) 1))
  142. (define (lshr i n)
  143. (arithmetic-shift (bitwise-and i int-mask) (- 0 n)))