c-arith.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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, Mike Sperber, Marcus Crestani
  6. ;;;
  7. ;;; scheme48-1.9.2/ps-compiler/prescheme/primop/c-arith.scm
  8. (define-module (ps-compiler prescheme primop c-arith)
  9. #:use-module (prescheme scheme48)
  10. #:use-module (prescheme platform)
  11. #:use-module (ps-compiler node node)
  12. #:use-module (ps-compiler prescheme c-call)
  13. #:use-module (ps-compiler prescheme primop c-primop))
  14. (define-syntax define-c-arith-binop-generator
  15. (syntax-rules ()
  16. ((_ id c-op)
  17. (define-c-generator id #t
  18. (lambda (call port indent)
  19. (simple-c-primop c-op call port))))))
  20. (define-c-arith-binop-generator + "+")
  21. (define-c-arith-binop-generator - "-")
  22. (define-c-arith-binop-generator * "*")
  23. (define-c-arith-binop-generator quotient "/")
  24. (define-c-arith-binop-generator un+ "+")
  25. (define-c-arith-binop-generator un- "-")
  26. (define-c-arith-binop-generator un* "*")
  27. (define-c-arith-binop-generator unquotient "/")
  28. (define-c-arith-binop-generator fl+ "+")
  29. (define-c-arith-binop-generator fl- "-")
  30. (define-c-arith-binop-generator fl* "*")
  31. (define-c-arith-binop-generator fl/ "/")
  32. (define-c-generator small* #t
  33. (lambda (call port indent)
  34. (format port "PS_SMALL_MULTIPLY(")
  35. (c-value (call-arg call 0) port)
  36. (format port ", ")
  37. (c-value (call-arg call 1) port)
  38. (format port ")")))
  39. (define-c-arith-binop-generator remainder "%")
  40. (define-c-arith-binop-generator unremainder "%")
  41. (define-c-arith-binop-generator bitwise-and "&")
  42. (define-c-arith-binop-generator bitwise-ior "|")
  43. (define-c-arith-binop-generator bitwise-xor "^")
  44. (define-c-generator ashl #t
  45. (lambda (call port indent)
  46. (generate-shift call port indent "LEFT" #f)))
  47. (define-c-generator ashr #t
  48. (lambda (call port indent)
  49. (generate-shift call port indent "RIGHT" #f)))
  50. (define-c-generator lshr #t
  51. (lambda (call port indent)
  52. (generate-shift call port indent "RIGHT_LOGICAL" #t)))
  53. (define (generate-shift call port indent macro logical?)
  54. (cond ((= 1 (call-exits call))
  55. ;; PS_SHIFT_??? is a C macro that handles overshifting even if C doesn't
  56. (indent-to port indent)
  57. (format port "PS_SHIFT_~A(" macro)
  58. (c-value (call-arg call 1) port)
  59. (format port ", ")
  60. (c-value (call-arg call 2) port)
  61. (format port ", ")
  62. (c-variable (car (lambda-variables (call-arg call 0))) port)
  63. (format port ")"))
  64. ((and (literal-node? (call-arg call 1))
  65. (>= (literal-value (call-arg call 1)) pre-scheme-integer-size))
  66. (format port "0L"))
  67. (else
  68. (format port "PS_SHIFT_~A_INLINE(" macro)
  69. (c-value (call-arg call 0) port)
  70. (format port ", ")
  71. (c-value (call-arg call 1) port)
  72. (format port ")"))))
  73. (define-c-generator bitwise-not #t
  74. (lambda (call port indent)
  75. (simple-c-primop "~" call port)))
  76. (define-syntax define-c-comp-binop-generator
  77. (syntax-rules ()
  78. ((_ id c-op)
  79. (define-c-generator id #t
  80. (lambda (call port indent)
  81. (simple-c-primop c-op call port))))))
  82. (define-c-comp-binop-generator = "==")
  83. (define-c-comp-binop-generator < "<" )
  84. (define-c-comp-binop-generator fl= "==")
  85. (define-c-comp-binop-generator fl< "<" )
  86. (define-c-comp-binop-generator un= "==")
  87. (define-c-comp-binop-generator un< "<" )
  88. (define-c-comp-binop-generator char=? "==")
  89. (define-c-comp-binop-generator char<? "<" )
  90. (define-c-generator ascii->char #t
  91. (lambda (call port indent)
  92. (display "((char) " port)
  93. (c-value (call-arg call 0) port)
  94. (display ")" port)))
  95. (define-c-generator char->ascii #t
  96. (lambda (call port indent)
  97. (display "((unsigned char) " port)
  98. (c-value (call-arg call 0) port)
  99. (display ")" port)))
  100. (define-c-generator unsigned->integer #t
  101. (lambda (call port indent)
  102. (display "((long) " port)
  103. (c-value (call-arg call 0) port)
  104. (display ")" port)))
  105. (define-c-generator integer->unsigned #t
  106. (lambda (call port indent)
  107. (display "((unsigned long) " port)
  108. (c-value (call-arg call 0) port)
  109. (display ")" port)))
  110. ;;(define-c-generator sign-extend #t
  111. ;; (lambda (call port indent)
  112. ;; (display "((long) " port)
  113. ;; (c-value (call-arg call 0) port)
  114. ;; (display ")" port)))
  115. ;;
  116. ;;(define-c-generator zero-extend #t
  117. ;; (lambda (call port indent)
  118. ;; (display "((unsigned long) " port)
  119. ;; (c-value (call-arg call 0) port)
  120. ;; (display ")" port)))