scm-scheme.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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
  6. ;;;
  7. ;;; scheme48-1.9.2/ps-compiler/prescheme/primop/scm-scheme.scm
  8. ;;;
  9. ;;; Primitives that directly correspond to primops.
  10. ;;;
  11. ;;; (define-primitive (id (arg-pred arg-type) ...) result-type . maybe-primop-id)
  12. ;;;
  13. ;;; Primitives that are n-ary or have other weirdness.
  14. ;;;
  15. ;;; (define-complex-primitive (id . argument-predicates)
  16. ;;; eval-fn inference-rule source . maybe-expander)
  17. ;;;
  18. ;;; Primitives that have only source but not a primop.
  19. ;;;
  20. ;;; (define-semi-primitive (id . argument-predicates)
  21. ;;; eval-fn inference-rule source maybe-expander)
  22. ;;;
  23. ;;; Primitives available only at load time.
  24. ;;;
  25. ;;; (define-load-time-primitive (id . argument-predicates) eval-fn)
  26. ;;;
  27. ;;; (really-define-primitive (id . argument-predicates)
  28. ;;; eval-fn inference-rule source expander expands-in-place?)
  29. (define-module (ps-compiler prescheme primop scm-scheme)
  30. #:export (define-primitive
  31. define-complex-primitive))
  32. (define-syntax really-define-primitive
  33. (lambda (exp r c)
  34. (let* ((spec (cadr exp))
  35. (id (car spec))
  36. (arg-predicates (cdr spec))
  37. (eval (caddr exp))
  38. (rest (cdddr exp))
  39. (inference-rule (car rest))
  40. (source (cadr rest))
  41. (expander (caddr rest))
  42. (expands-in-place? (cadddr rest)))
  43. `(let ((,(r 'predicates) ,(let recur ((preds arg-predicates))
  44. (cond ((pair? preds)
  45. `(cons ,(car preds)
  46. ,(recur (cdr preds))))
  47. ((null? preds)
  48. '(quote ()))
  49. (else
  50. preds)))))
  51. (define-prescheme! ',id
  52. #f ;; location
  53. (make-primitive ',id
  54. ,(r 'predicates)
  55. ,eval
  56. ',source
  57. ,expander
  58. ,expands-in-place?
  59. ,inference-rule))))))
  60. (define-syntax define-complex-primitive
  61. (lambda (exp r c)
  62. `(really-define-primitive ,@(cdr exp) #t)))
  63. (define-syntax define-primitive
  64. (lambda (exp r c)
  65. (let* ((id (cadr exp))
  66. (args (caddr exp))
  67. (result (cadddr exp))
  68. (primop (if (null? (cddddr exp)) (cadr exp) (car (cddddr exp))))
  69. (names (map (lambda (a b) b)
  70. args
  71. '(x1 x2 x3 x4 x5 x6 x7 x8 x9))))
  72. `(define-complex-primitive (,id . ,(map car args)) ,id
  73. (lambda (args node depth return?)
  74. (if (not (= (length args)
  75. ,(length args)))
  76. (user-error "wrong number of arguments in ~S" (schemify node)))
  77. ,@(do ((i 0 (+ i 1))
  78. (args args (cdr args))
  79. (res '() (cons `(check-arg-type args ,i ,(cadar args) depth node)
  80. res)))
  81. ((null? args)
  82. (reverse res)))
  83. ,result)
  84. (lambda ,names (,id . ,names))
  85. (lambda (args type)
  86. (make-primop-call-node (get-prescheme-primop ',primop) args type))))))
  87. (define-syntax define-semi-primitive
  88. (lambda (exp r c)
  89. `(really-define-primitive ,@(cdr exp) #f #f)))
  90. (define-syntax define-load-time-primitive
  91. (lambda (exp r c)
  92. `(define-semi-primitive ,(cadr exp)
  93. ,(caddr exp)
  94. (make-load-time-only-rule ',(caadr exp))
  95. #f)))
  96. (define (make-load-time-only-rule id)
  97. (lambda (args node depth return?)
  98. (user-error "~S is only available at load time ~S" id (schemify node))))
  99. ;;----------------------------------------------------------------
  100. ;; Boolean stuff
  101. (define-semi-primitive (not #f) not
  102. (lambda (args node depth return?)
  103. (check-arg-type args 0 type/boolean depth node)
  104. type/boolean)
  105. (lambda (x) (if x #f #t)))
  106. (define-load-time-primitive (boolean? #f) boolean?)
  107. (define-complex-primitive (eq? #f #f) eq?
  108. (lambda (args node depth return?)
  109. (unify! (infer-type (car args) depth)
  110. (infer-type (cadr args) depth)
  111. node)
  112. type/boolean)
  113. (lambda (x y) (eq? x y))
  114. (lambda (args type)
  115. (make-primop-call-node (get-prescheme-primop 'eq?) args type)))
  116. (define-load-time-primitive (eqv? #f) eqv?)
  117. (define-load-time-primitive (equal? #f) equal?)
  118. ;;----------------------------------------------------------------
  119. ;; Characters
  120. (define (ascii-value? n)
  121. (and (integer? n)
  122. (>= n 0)
  123. (< n ascii-limit)))
  124. (define-primitive ascii->char ((ascii-value? type/integer)) type/char)
  125. (define-primitive char->ascii ((char? type/char)) type/integer)
  126. (define (char-comparison-rule args node depth return?)
  127. (check-arg-type args 0 type/char depth node)
  128. (check-arg-type args 1 type/char depth node)
  129. type/boolean)
  130. (define-syntax define-char-comparison
  131. (lambda (exp r c)
  132. (let ((id (cadr exp))
  133. (op (caddr exp)))
  134. `(define-complex-primitive (,id char? char?) ,id
  135. char-comparison-rule
  136. (lambda (x y) (,op x y))
  137. (lambda (args type)
  138. (make-primop-call-node (get-prescheme-primop ',op) args type))))))
  139. (define-char-comparison char=? =)
  140. (define-char-comparison char<? <)
  141. (define-char-comparison char>? >)
  142. (define-char-comparison char<=? <=)
  143. (define-char-comparison char>=? >=)
  144. ;; Plus lots more...
  145. ;;----------------------------------------------------------------
  146. ;; Data manipulation
  147. (define (any? x) #t)
  148. (define (positive-integer? x)
  149. (and (integer? x)
  150. (<= 0 x)))
  151. (define (unsigned-byte? x)
  152. (and (positive-integer? x)
  153. (<= x 256)))
  154. (define-complex-primitive (make-vector positive-integer? any?) make-vector
  155. (lambda (args node depth return?)
  156. (let ((uvar (make-uvar 'v depth)))
  157. (make-nonpolymorphic! uvar)
  158. (check-arg-type args 0 type/integer depth node)
  159. (check-arg-type args 1 uvar depth node)
  160. (make-pointer-type uvar)))
  161. (lambda (size init)
  162. (make-vector size init))
  163. (lambda (args type)
  164. (make-primop-call-node (get-prescheme-primop 'make-vector) args type)))
  165. (define-load-time-primitive (vector-length vector?) vector-length)
  166. (define-complex-primitive (vector-ref vector? positive-integer?) vector-ref
  167. (lambda (args node depth return?)
  168. (let ((elt-type (make-uvar 'v depth)))
  169. (check-arg-type args 0 (make-pointer-type elt-type) depth node)
  170. (check-arg-type args 1 type/integer depth node)
  171. elt-type))
  172. (lambda (vector index)
  173. (vector-ref vector index))
  174. (lambda (args type)
  175. (make-primop-call-node (get-prescheme-primop 'vector-ref) args type)))
  176. (define-complex-primitive (vector-set! vector? positive-integer? any?)
  177. vector-set!
  178. (lambda (args node depth return?)
  179. (let ((elt-type (make-uvar 'v depth)))
  180. (check-arg-type args 0 (make-pointer-type elt-type) depth node)
  181. (check-arg-type args 1 type/integer depth node)
  182. (check-arg-type args 2 elt-type depth node)
  183. type/unit))
  184. (lambda (vector index value)
  185. (vector-set! vector index value))
  186. (lambda (args type)
  187. (make-primop-call-node (get-prescheme-primop 'vector-set!) args type)))
  188. (define-primitive make-string ((integer? type/integer)) type/string)
  189. (define-primitive string-length ((string? type/string)) type/integer)
  190. (define-primitive string-ref
  191. ((string? type/string) (integer? type/integer))
  192. type/char)
  193. (define-primitive string-set!
  194. ((string? type/string) (integer? type/integer) (char? type/char))
  195. type/unit)
  196. (define-complex-primitive (deallocate any?) (lambda (x) (values))
  197. (lambda (args node depth return?)
  198. (let ((type (make-pointer-type (make-uvar 'p depth))))
  199. (check-arg-type args 0 type depth node)
  200. type/unit))
  201. (lambda (thing)
  202. (deallocate thing))
  203. (lambda (args type)
  204. (make-primop-call-node (get-prescheme-primop 'deallocate) args type)))
  205. (define-complex-primitive (null-pointer? any?) (lambda (x) #f)
  206. (lambda (args node depth return?)
  207. (let ((type (make-pointer-type (make-uvar 'p depth))))
  208. (check-arg-type args 0 type depth node)
  209. type/boolean))
  210. (lambda (thing)
  211. (null-pointer? thing))
  212. (lambda (args type)
  213. (make-primop-call-node (get-prescheme-primop 'null-pointer?) args type)))
  214. (define-complex-primitive (null-pointer) (lambda () #f)
  215. (lambda (args node depth return?)
  216. (make-pointer-type (make-uvar 'null depth)))
  217. (lambda (type)
  218. (null-pointer type))
  219. (lambda (args type)
  220. (make-primop-call-node (get-prescheme-primop 'null-pointer)
  221. (list (make-literal-node type))
  222. type)))
  223. ;;----------------------------------------------------------------
  224. ;; I/O
  225. (define-primitive current-input-port () type/input-port stdin)
  226. (define-primitive current-output-port () type/output-port stdout)
  227. (define-primitive current-error-port () type/output-port stderr)
  228. (define type/status type/integer)
  229. (let ((return (make-tuple-type (list type/input-port type/status))))
  230. (define-primitive open-input-file ((string? type/string)) return))
  231. (let ((return (make-tuple-type (list type/output-port type/status))))
  232. (define-primitive open-output-file ((string? type/string)) return))
  233. (define-primitive close-input-port ((input-port? type/input-port)) type/status)
  234. (define-primitive close-output-port ((output-port? type/output-port)) type/status)
  235. (define char-return-type
  236. (make-tuple-type (list type/char type/boolean type/status)))
  237. (define-primitive read-char ((input-port? type/input-port)) char-return-type)
  238. (define-primitive peek-char ((input-port? type/input-port)) char-return-type)
  239. (define integer-return-type
  240. (make-tuple-type (list type/integer type/boolean type/status)))
  241. (define-primitive read-integer ((input-port? type/input-port)) integer-return-type)
  242. (define-primitive write-char
  243. ((char? type/char) (output-port? type/output-port))
  244. type/status)
  245. (define-primitive write-string
  246. ((string? type/string) (output-port? type/output-port))
  247. type/status)
  248. (define-primitive write-integer
  249. ((integer? type/integer) (output-port? type/output-port))
  250. type/status)
  251. (define-complex-primitive (newline output-port?) newline
  252. (lambda (args node depth return?)
  253. (check-arg-type args 0 type/output-port depth node)
  254. type/status)
  255. (lambda (out)
  256. (write-char #\newline out))
  257. (lambda (args type)
  258. (make-primop-call-node (get-prescheme-primop 'write-char)
  259. (cons (make-literal-node #\newline) args)
  260. type)))
  261. (define-primitive force-output ((output-port? type/output-port)) type/status)
  262. (define-primitive error-string
  263. ((positive-integer? type/status))
  264. type/string)
  265. ;;----------------------------------------------------------------
  266. (define-complex-primitive (values . any?) values
  267. (lambda (args node depth return?)
  268. (make-tuple-type (infer-types args depth)))
  269. #f
  270. (lambda (args type)
  271. (let ((node (make-node values-operator (cons 'values args))))
  272. (node-set! node 'type type)
  273. node)))
  274. (define values-operator (get-operator 'values))
  275. ;; CALL-WITH-VALUES that uses closures instead of procedures.
  276. (define (ps-call-with-values producer consumer)
  277. (call-with-values
  278. (lambda ()
  279. (apply-closure producer '()))
  280. (lambda args
  281. (apply-closure consumer args))))
  282. (define-complex-primitive (call-with-values closure? closure?)
  283. ps-call-with-values
  284. (lambda (args node depth return?)
  285. (if (not (lambda-node? (cadr args)))
  286. (user-error
  287. "second argument to CALL-WITH-VALUES must be a lambda node~% ~S"
  288. (schemify node)))
  289. (let* ((consumer-type (infer-type (cadr args) depth))
  290. (arg-types (arrow-type-args consumer-type))
  291. (result-type (arrow-type-result consumer-type)))
  292. (unify! (infer-type (car args) depth)
  293. (make-arrow-type '() (make-tuple-type arg-types))
  294. node)
  295. (if (not return?) ;; so we cause a check for illegal tuples
  296. (unify! result-type (make-uvar 'temp depth) node))
  297. result-type))
  298. #f
  299. (lambda (args type)
  300. (let* ((tuple-type (arrow-type-result
  301. (maybe-follow-uvar (node-ref (car args) 'type))))
  302. (node (make-node call-with-values-operator
  303. (list 'call-with-values
  304. (make-call-node (car args) '() tuple-type)
  305. (cadr args)))))
  306. (node-set! node 'type type)
  307. node)))
  308. (define lambda-node? (node-predicate 'lambda))
  309. (define call-with-values-operator (get-operator 'call-with-values))
  310. (define-primitive unspecific () type/unit)
  311. (define-complex-primitive (error string? . integer?) error
  312. (lambda (args node depth return?)
  313. (check-arg-type args 0 type/string depth node)
  314. (do ((args (cdr args) (cdr args)))
  315. ((null? args))
  316. (check-arg-type args 0 type/integer depth node))
  317. type/null)
  318. (lambda (error string)
  319. (error string))
  320. (lambda (args type)
  321. (make-primop-call-node (get-prescheme-primop 'error) args type)))
  322. ;; For enumerated types that are shared with C
  323. (define-load-time-primitive (make-external-constant symbol? symbol? string?)
  324. make-external-constant)
  325. ;;----------------------------------------------------------------
  326. ;; Utilities for making nodes
  327. (define call-operator (get-operator 'call))
  328. (define literal-operator (get-operator 'literal))
  329. (define name-operator (get-operator 'name))
  330. (define primitive-operator (get-operator 'primitive))
  331. (define (make-call-node proc args type)
  332. (let ((node (make-node call-operator (cons proc args))))
  333. (node-set! node 'type type)
  334. node))
  335. (define (make-literal-node value)
  336. (make-node literal-operator value))
  337. (define (make-primop-call-node primop args type)
  338. (make-call-node (make-literal-node primop) args type))
  339. (define (make-reference-node id binding)
  340. (let ((node (make-node name-operator id)))
  341. (node-set! node 'binding binding)
  342. node))
  343. (define (var->name-node var)
  344. (make-reference-node ((structure-ref variable variable-name) var)
  345. (make-binding #f var #f)))