target.scm 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. ;;; Compilation targets
  2. ;; Copyright (C) 2011-2014,2017-2018,2023 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. (define %native-word-size
  35. ((@ (system foreign) sizeof) '*))
  36. (define %target-type (make-fluid %host-type))
  37. (define %target-endianness (make-fluid (native-endianness)))
  38. (define %target-word-size (make-fluid %native-word-size))
  39. (define (validate-target target)
  40. (if (or (not (string? target))
  41. (let ((parts (string-split target #\-)))
  42. (or (< (length parts) 3)
  43. (let ((cpu (list-ref parts 0))
  44. (os (list-ref parts 2)))
  45. (or (string-null? cpu)
  46. ;; vendor (parts[1]) may be empty
  47. (string-null? os)
  48. ;; optional components (ABI) should be nonempty if
  49. ;; specified
  50. (or-map string-null? (list-tail parts 3)))))))
  51. (error "invalid target" target)))
  52. (define (with-target target thunk)
  53. (validate-target target)
  54. (let ((cpu (triplet-cpu target)))
  55. (with-fluids ((%target-type target)
  56. (%target-endianness (cpu-endianness cpu))
  57. (%target-word-size (triplet-pointer-size target)))
  58. (thunk))))
  59. (define (with-native-target thunk)
  60. (with-fluids ((%target-type %host-type)
  61. (%target-endianness (native-endianness))
  62. (%target-word-size %native-word-size))
  63. (thunk)))
  64. (define (cpu-endianness cpu)
  65. "Return the endianness for CPU."
  66. (if (string=? cpu (triplet-cpu %host-type))
  67. (native-endianness)
  68. (cond ((string-match "^i[0-9]86$" cpu)
  69. (endianness little))
  70. ((member cpu '("x86_64" "ia64"
  71. "powerpcle" "powerpc64le" "mipsel" "mips64el" "nios2"
  72. "sh3" "sh4" "alpha" "arc"
  73. "wasm32" "wasm64"))
  74. (endianness little))
  75. ((member cpu '("sparc" "sparc64" "powerpc" "powerpc64" "spu"
  76. "mips" "mips64" "m68k" "s390x"))
  77. (endianness big))
  78. ((string-match "^arm.*el" cpu)
  79. (endianness little))
  80. ((string-match "^arm.*eb" cpu)
  81. (endianness big))
  82. ((string-prefix? "arm" cpu) ;ARMs are LE by default
  83. (endianness little))
  84. ((string-match "^aarch64.*be" cpu)
  85. (endianness big))
  86. ((string=? "aarch64" cpu)
  87. (endianness little))
  88. ((string-match "riscv[1-9][0-9]*" cpu)
  89. (endianness little))
  90. ((string-match "loongarch[1-9][0-9]*" cpu)
  91. (endianness little))
  92. (else
  93. (error "unknown CPU endianness" cpu)))))
  94. (define (triplet-pointer-size triplet)
  95. "Return the size of pointers in bytes for TRIPLET."
  96. (let ((cpu (triplet-cpu triplet)))
  97. (cond ((and (string=? cpu (triplet-cpu %host-type))
  98. (string=? (triplet-os triplet) (triplet-os %host-type)))
  99. %native-word-size)
  100. ((string-match "^i[0-9]86$" cpu) 4)
  101. ;; Although GNU config.guess doesn't yet recognize them,
  102. ;; Debian (ab)uses the OS part to denote the specific ABI
  103. ;; being used: <http://wiki.debian.org/Multiarch/Tuples>.
  104. ;; See <http://www.linux-mips.org/wiki/WhatsWrongWithO32N32N64>
  105. ;; for details on the MIPS ABIs.
  106. ((string-match "^mips64.*-gnuabi64" triplet) 8) ; n64 ABI
  107. ((string-match "^mips64" cpu) 4) ; n32 or o32
  108. ((string-match "^x86_64-.*-gnux32" triplet) 4) ; x32
  109. ((string-match "32$" cpu) 4)
  110. ((string-match "64$" cpu) 8)
  111. ((string-match "64_?[lbe][lbe]$" cpu) 8)
  112. ((member cpu '("sparc" "powerpc" "mips" "mipsel" "nios2" "m68k" "sh3" "sh4" "arc")) 4)
  113. ((member cpu '("s390x" "alpha")) 8)
  114. ((string-match "^arm.*" cpu) 4)
  115. (else (error "unknown CPU word size" cpu)))))
  116. (define (triplet-cpu t)
  117. (substring t 0 (string-index t #\-)))
  118. (define (triplet-vendor t)
  119. (let ((start (1+ (string-index t #\-))))
  120. (substring t start (string-index t #\- start))))
  121. (define (triplet-os t)
  122. (let ((start (1+ (string-index t #\- (1+ (string-index t #\-))))))
  123. (substring t start)))
  124. (define (target-type)
  125. "Return the GNU configuration triplet of the target platform."
  126. (fluid-ref %target-type))
  127. (define (target-cpu)
  128. "Return the CPU name of the target platform."
  129. (triplet-cpu (target-type)))
  130. (define (target-vendor)
  131. "Return the vendor name of the target platform."
  132. (triplet-vendor (target-type)))
  133. (define target-runtime
  134. (make-parameter
  135. 'guile-vm
  136. (lambda (val)
  137. "Determine what kind of virtual machine we are targetting. Usually this
  138. is @code{guile-vm} when generating bytecode for Guile's virtual machine."
  139. val)))
  140. (define (target-os)
  141. "Return the operating system name of the target platform."
  142. (triplet-os (target-type)))
  143. (define (target-endianness)
  144. "Return the endianness object of the target platform."
  145. (fluid-ref %target-endianness))
  146. (define (target-word-size)
  147. "Return the word size, in bytes, of the target platform."
  148. (fluid-ref %target-word-size))
  149. (define (target-max-size-t)
  150. "Return the maximum size_t value of the target platform, in bytes."
  151. ;; Apply the currently-universal restriction of a maximum 48-bit
  152. ;; address space.
  153. (1- (ash 1 (min (* (target-word-size) 8) 48))))
  154. (define (target-max-size-t/scm)
  155. "Return the maximum size_t value of the target platform, in units of
  156. SCM words."
  157. (floor/ (target-max-size-t) (target-word-size)))
  158. (define (target-max-vector-length)
  159. "Return the maximum vector length of the target platform, in units of
  160. SCM words."
  161. ;; Vector size fits in first word; the low 8 bits are taken by the
  162. ;; type tag. Additionally, restrict to 48-bit address space.
  163. (1- (ash 1 (min (- (* (target-word-size) 8) 8) 48))))
  164. (define (target-most-negative-fixnum)
  165. "Return the most negative integer representable as a fixnum on the
  166. target platform."
  167. (- (ash 1 (- (* (target-word-size) 8) 3))))
  168. (define (target-most-positive-fixnum)
  169. "Return the most positive integer representable as a fixnum on the
  170. target platform."
  171. (1- (ash 1 (- (* (target-word-size) 8) 3))))
  172. (define (target-fixnum? n)
  173. (and (exact-integer? n)
  174. (<= (target-most-negative-fixnum)
  175. n
  176. (target-most-positive-fixnum))))