target.scm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. ;;; Compilation targets
  2. ;; Copyright (C) 2011-2014,2017-2018,2023-2024 Free Software Foundation, Inc.
  3. ;; This library is free software; you can redistribute it and/or
  4. ;; modify it under the terms of the GNU Lesser General Public
  5. ;; License as published by the Free Software Foundation; either
  6. ;; version 3 of the License, or (at your option) any later version.
  7. ;;
  8. ;; This library is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;; Lesser General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU Lesser General Public
  14. ;; License along with this library; if not, write to the Free Software
  15. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. ;; 02110-1301 USA
  17. ;;; Code:
  18. (define-module (system base target)
  19. #:use-module (rnrs bytevectors)
  20. #:use-module (ice-9 regex)
  21. #:export (target-type with-target with-native-target
  22. target-cpu target-vendor target-os
  23. target-runtime
  24. target-endianness target-word-size
  25. target-max-size-t
  26. target-max-size-t/scm
  27. target-max-vector-length
  28. target-most-negative-fixnum
  29. target-most-positive-fixnum
  30. target-fixnum?))
  31. ;;;
  32. ;;; Target types
  33. ;;;
  34. ;; Hacky way to get native pointer size without having to load (system
  35. ;; foreign).
  36. (define-syntax %native-word-size
  37. (lambda (stx)
  38. (syntax-case stx ()
  39. (id (identifier? #'id)
  40. (cond
  41. ((< most-positive-fixnum (ash 1 32)) 4)
  42. ((< most-positive-fixnum (ash 1 64)) 8)
  43. (else (error "unexpected!" most-positive-fixnum)))))))
  44. (define %target-type (make-fluid %host-type))
  45. (define %target-endianness (make-fluid (native-endianness)))
  46. (define %target-word-size (make-fluid %native-word-size))
  47. (define (validate-target target)
  48. (if (or (not (string? target))
  49. (let ((parts (string-split target #\-)))
  50. (or (< (length parts) 3)
  51. (let ((cpu (list-ref parts 0))
  52. (os (list-ref parts 2)))
  53. (or (string-null? cpu)
  54. ;; vendor (parts[1]) may be empty
  55. (string-null? os)
  56. ;; optional components (ABI) should be nonempty if
  57. ;; specified
  58. (or-map string-null? (list-tail parts 3)))))))
  59. (error "invalid target" target)))
  60. (define (with-target target thunk)
  61. (validate-target target)
  62. (let ((cpu (triplet-cpu target)))
  63. (with-fluids ((%target-type target)
  64. (%target-endianness (cpu-endianness cpu))
  65. (%target-word-size (triplet-pointer-size target)))
  66. (thunk))))
  67. (define (with-native-target thunk)
  68. (with-fluids ((%target-type %host-type)
  69. (%target-endianness (native-endianness))
  70. (%target-word-size %native-word-size))
  71. (thunk)))
  72. (define (cpu-endianness cpu)
  73. "Return the endianness for CPU."
  74. (if (string=? cpu (triplet-cpu %host-type))
  75. (native-endianness)
  76. (cond ((string-match "^i[0-9]86$" cpu)
  77. (endianness little))
  78. ((member cpu '("x86_64" "ia64"
  79. "powerpcle" "powerpc64le" "mipsel" "mips64el" "nios2"
  80. "sh3" "sh4" "alpha" "arc"
  81. "wasm32" "wasm64"))
  82. (endianness little))
  83. ((member cpu '("sparc" "sparc64" "powerpc" "powerpc64" "spu"
  84. "mips" "mips64" "m68k" "s390x"))
  85. (endianness big))
  86. ((string-match "^arm.*el" cpu)
  87. (endianness little))
  88. ((string-match "^arm.*eb" cpu)
  89. (endianness big))
  90. ((string-prefix? "arm" cpu) ;ARMs are LE by default
  91. (endianness little))
  92. ((string-match "^aarch64.*be" cpu)
  93. (endianness big))
  94. ((string=? "aarch64" cpu)
  95. (endianness little))
  96. ((string-match "riscv[1-9][0-9]*" cpu)
  97. (endianness little))
  98. ((string-match "loongarch[1-9][0-9]*" cpu)
  99. (endianness little))
  100. (else
  101. (error "unknown CPU endianness" cpu)))))
  102. (define (triplet-pointer-size triplet)
  103. "Return the size of pointers in bytes for TRIPLET."
  104. (let ((cpu (triplet-cpu triplet)))
  105. (cond ((and (string=? cpu (triplet-cpu %host-type))
  106. (string=? (triplet-os triplet) (triplet-os %host-type)))
  107. %native-word-size)
  108. ((string-match "^i[0-9]86$" cpu) 4)
  109. ;; Although GNU config.guess doesn't yet recognize them,
  110. ;; Debian (ab)uses the OS part to denote the specific ABI
  111. ;; being used: <http://wiki.debian.org/Multiarch/Tuples>.
  112. ;; See <http://www.linux-mips.org/wiki/WhatsWrongWithO32N32N64>
  113. ;; for details on the MIPS ABIs.
  114. ((string-match "^mips64.*-gnuabi64" triplet) 8) ; n64 ABI
  115. ((string-match "^mips64" cpu) 4) ; n32 or o32
  116. ((string-match "^x86_64-.*-gnux32" triplet) 4) ; x32
  117. ((string-match "32$" cpu) 4)
  118. ((string-match "64$" cpu) 8)
  119. ((string-match "64_?[lbe][lbe]$" cpu) 8)
  120. ((member cpu '("sparc" "powerpc" "mips" "mipsel" "nios2" "m68k" "sh3" "sh4" "arc")) 4)
  121. ((member cpu '("s390x" "alpha")) 8)
  122. ((string-match "^arm.*" cpu) 4)
  123. (else (error "unknown CPU word size" cpu)))))
  124. (define (triplet-cpu t)
  125. (substring t 0 (string-index t #\-)))
  126. (define (triplet-vendor t)
  127. (let ((start (1+ (string-index t #\-))))
  128. (substring t start (string-index t #\- start))))
  129. (define (triplet-os t)
  130. (let ((start (1+ (string-index t #\- (1+ (string-index t #\-))))))
  131. (substring t start)))
  132. (define (target-type)
  133. "Return the GNU configuration triplet of the target platform."
  134. (fluid-ref %target-type))
  135. (define (target-cpu)
  136. "Return the CPU name of the target platform."
  137. (triplet-cpu (target-type)))
  138. (define (target-vendor)
  139. "Return the vendor name of the target platform."
  140. (triplet-vendor (target-type)))
  141. (define target-runtime
  142. (make-parameter
  143. 'guile-vm
  144. (lambda (val)
  145. "Determine what kind of virtual machine we are targetting. Usually this
  146. is @code{guile-vm} when generating bytecode for Guile's virtual machine."
  147. val)))
  148. (define (target-os)
  149. "Return the operating system name of the target platform."
  150. (triplet-os (target-type)))
  151. (define (target-endianness)
  152. "Return the endianness object of the target platform."
  153. (fluid-ref %target-endianness))
  154. (define (target-word-size)
  155. "Return the word size, in bytes, of the target platform."
  156. (fluid-ref %target-word-size))
  157. (define (target-max-size-t)
  158. "Return the maximum size_t value of the target platform, in bytes."
  159. ;; Apply the currently-universal restriction of a maximum 48-bit
  160. ;; address space.
  161. (1- (ash 1 (min (* (target-word-size) 8) 48))))
  162. (define (target-max-size-t/scm)
  163. "Return the maximum size_t value of the target platform, in units of
  164. SCM words."
  165. (floor/ (target-max-size-t) (target-word-size)))
  166. (define (target-max-vector-length)
  167. "Return the maximum vector length of the target platform, in units of
  168. SCM words."
  169. ;; Vector size fits in first word; the low 8 bits are taken by the
  170. ;; type tag. Additionally, restrict to 48-bit address space.
  171. (1- (ash 1 (min (- (* (target-word-size) 8) 8) 48))))
  172. (define (target-most-negative-fixnum)
  173. "Return the most negative integer representable as a fixnum on the
  174. target platform."
  175. (- (ash 1 (- (* (target-word-size) 8) 3))))
  176. (define (target-most-positive-fixnum)
  177. "Return the most positive integer representable as a fixnum on the
  178. target platform."
  179. (1- (ash 1 (- (* (target-word-size) 8) 3))))
  180. (define (target-fixnum? n)
  181. (and (exact-integer? n)
  182. (<= (target-most-negative-fixnum)
  183. n
  184. (target-most-positive-fixnum))))