primop.scm 7.0 KB

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