platform.nim 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This module contains data about the different processors
  10. # and operating systems.
  11. # Note: Unfortunately if an OS or CPU is listed here this does not mean that
  12. # Nim has been tested on this platform or that the RTL has been ported.
  13. # Feel free to test for your excentric platform!
  14. import
  15. strutils
  16. when defined(nimPreviewSlimSystem):
  17. import std/assertions
  18. type
  19. TSystemOS* = enum # Also add OS in initialization section and alias
  20. # conditionals to condsyms (end of module).
  21. osNone, osDos, osWindows, osOs2, osLinux, osMorphos, osSkyos, osSolaris,
  22. osIrix, osNetbsd, osFreebsd, osOpenbsd, osDragonfly, osCrossos, osAix, osPalmos, osQnx,
  23. osAmiga, osAtari, osNetware, osMacos, osMacosx, osIos, osHaiku, osAndroid, osVxWorks
  24. osGenode, osJS, osNimVM, osStandalone, osNintendoSwitch, osFreeRTOS, osZephyr, osAny
  25. type
  26. TInfoOSProp* = enum
  27. ospNeedsPIC, # OS needs PIC for libraries
  28. ospCaseInsensitive, # OS filesystem is case insensitive
  29. ospPosix, # OS is posix-like
  30. ospLacksThreadVars # OS lacks proper __threadvar support
  31. TInfoOSProps* = set[TInfoOSProp]
  32. TInfoOS* = tuple[name: string, parDir: string, dllFrmt: string,
  33. altDirSep: string, objExt: string, newLine: string,
  34. pathSep: string, dirSep: string, scriptExt: string,
  35. curDir: string, exeExt: string, extSep: string,
  36. props: TInfoOSProps]
  37. const
  38. OS*: array[succ(low(TSystemOS))..high(TSystemOS), TInfoOS] = [
  39. (name: "DOS",
  40. parDir: "..", dllFrmt: "$1.dll", altDirSep: "/", objExt: ".obj",
  41. newLine: "\x0D\x0A", pathSep: ";", dirSep: "\\", scriptExt: ".bat",
  42. curDir: ".", exeExt: ".exe", extSep: ".", props: {ospCaseInsensitive}),
  43. (name: "Windows", parDir: "..", dllFrmt: "$1.dll", altDirSep: "/",
  44. objExt: ".obj", newLine: "\x0D\x0A", pathSep: ";", dirSep: "\\",
  45. scriptExt: ".bat", curDir: ".", exeExt: ".exe", extSep: ".",
  46. props: {ospCaseInsensitive}),
  47. (name: "OS2", parDir: "..",
  48. dllFrmt: "$1.dll", altDirSep: "/",
  49. objExt: ".obj", newLine: "\x0D\x0A",
  50. pathSep: ";", dirSep: "\\",
  51. scriptExt: ".bat", curDir: ".",
  52. exeExt: ".exe", extSep: ".",
  53. props: {ospCaseInsensitive}),
  54. (name: "Linux", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  55. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  56. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  57. props: {ospNeedsPIC, ospPosix}),
  58. (name: "MorphOS", parDir: "..",
  59. dllFrmt: "lib$1.so", altDirSep: "/",
  60. objExt: ".o", newLine: "\x0A",
  61. pathSep: ":", dirSep: "/",
  62. scriptExt: ".sh", curDir: ".",
  63. exeExt: "", extSep: ".",
  64. props: {ospNeedsPIC, ospPosix}),
  65. (name: "SkyOS", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  66. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  67. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  68. props: {ospNeedsPIC, ospPosix}),
  69. (name: "Solaris", parDir: "..",
  70. dllFrmt: "lib$1.so", altDirSep: "/",
  71. objExt: ".o", newLine: "\x0A",
  72. pathSep: ":", dirSep: "/",
  73. scriptExt: ".sh", curDir: ".",
  74. exeExt: "", extSep: ".",
  75. props: {ospNeedsPIC, ospPosix}),
  76. (name: "Irix", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  77. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  78. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  79. props: {ospNeedsPIC, ospPosix}),
  80. (name: "NetBSD", parDir: "..",
  81. dllFrmt: "lib$1.so", altDirSep: "/",
  82. objExt: ".o", newLine: "\x0A",
  83. pathSep: ":", dirSep: "/",
  84. scriptExt: ".sh", curDir: ".",
  85. exeExt: "", extSep: ".",
  86. props: {ospNeedsPIC, ospPosix}),
  87. (name: "FreeBSD", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  88. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  89. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  90. props: {ospNeedsPIC, ospPosix}),
  91. (name: "OpenBSD", parDir: "..",
  92. dllFrmt: "lib$1.so", altDirSep: "/",
  93. objExt: ".o", newLine: "\x0A",
  94. pathSep: ":", dirSep: "/",
  95. scriptExt: ".sh", curDir: ".",
  96. exeExt: "", extSep: ".",
  97. props: {ospNeedsPIC, ospPosix}),
  98. (name: "DragonFly", parDir: "..",
  99. dllFrmt: "lib$1.so", altDirSep: "/",
  100. objExt: ".o", newLine: "\x0A",
  101. pathSep: ":", dirSep: "/",
  102. scriptExt: ".sh", curDir: ".",
  103. exeExt: "", extSep: ".",
  104. props: {ospNeedsPIC, ospPosix}),
  105. (name: "CROSSOS", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  106. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  107. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  108. props: {ospNeedsPIC, ospPosix}),
  109. (name: "AIX", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  110. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  111. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  112. props: {ospNeedsPIC, ospPosix}),
  113. (name: "PalmOS", parDir: "..",
  114. dllFrmt: "lib$1.so", altDirSep: "/",
  115. objExt: ".o", newLine: "\x0A",
  116. pathSep: ":", dirSep: "/",
  117. scriptExt: ".sh", curDir: ".",
  118. exeExt: "", extSep: ".",
  119. props: {ospNeedsPIC}),
  120. (name: "QNX",
  121. parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o",
  122. newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".",
  123. exeExt: "", extSep: ".", props: {ospNeedsPIC, ospPosix}),
  124. (name: "Amiga",
  125. parDir: "..", dllFrmt: "$1.library", altDirSep: "/", objExt: ".o",
  126. newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".",
  127. exeExt: "", extSep: ".", props: {ospNeedsPIC}),
  128. (name: "Atari",
  129. parDir: "..", dllFrmt: "$1.dll", altDirSep: "/", objExt: ".o",
  130. newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: "", curDir: ".",
  131. exeExt: ".tpp", extSep: ".", props: {ospNeedsPIC}),
  132. (name: "Netware",
  133. parDir: "..", dllFrmt: "$1.nlm", altDirSep: "/", objExt: "",
  134. newLine: "\x0D\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh",
  135. curDir: ".", exeExt: ".nlm", extSep: ".", props: {ospCaseInsensitive}),
  136. (name: "MacOS", parDir: "::", dllFrmt: "$1Lib", altDirSep: ":",
  137. objExt: ".o", newLine: "\x0D", pathSep: ",", dirSep: ":", scriptExt: "",
  138. curDir: ":", exeExt: "", extSep: ".", props: {ospCaseInsensitive}),
  139. (name: "MacOSX", parDir: "..", dllFrmt: "lib$1.dylib", altDirSep: ":",
  140. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  141. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  142. props: {ospNeedsPIC, ospPosix, ospLacksThreadVars}),
  143. (name: "iOS", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  144. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  145. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  146. props: {ospNeedsPIC, ospPosix}),
  147. (name: "Haiku", parDir: "..", dllFrmt: "lib$1.so", altDirSep: ":",
  148. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  149. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  150. props: {ospNeedsPIC, ospPosix, ospLacksThreadVars}),
  151. (name: "Android", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  152. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  153. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  154. props: {ospNeedsPIC, ospPosix}),
  155. (name: "VxWorks", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  156. objExt: ".o", newLine: "\x0A", pathSep: ";", dirSep: "\\",
  157. scriptExt: ".sh", curDir: ".", exeExt: ".vxe", extSep: ".",
  158. props: {ospNeedsPIC, ospPosix, ospLacksThreadVars}),
  159. (name: "Genode", pardir: "..", dllFrmt: "$1.lib.so", altDirSep: "/",
  160. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  161. scriptExt: "", curDir: "/", exeExt: "", extSep: ".",
  162. props: {ospNeedsPIC, ospLacksThreadVars}),
  163. (name: "JS", parDir: "..",
  164. dllFrmt: "lib$1.so", altDirSep: "/",
  165. objExt: ".o", newLine: "\x0A",
  166. pathSep: ":", dirSep: "/",
  167. scriptExt: ".sh", curDir: ".",
  168. exeExt: "", extSep: ".", props: {}),
  169. (name: "NimVM", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  170. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  171. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {}),
  172. (name: "Standalone", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  173. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  174. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  175. props: {}),
  176. (name: "NintendoSwitch", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  177. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  178. scriptExt: ".sh", curDir: ".", exeExt: ".elf", extSep: ".",
  179. props: {ospNeedsPIC, ospPosix}),
  180. (name: "FreeRTOS", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  181. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  182. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  183. props: {ospPosix}),
  184. (name: "Zephyr", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  185. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  186. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  187. props: {ospPosix}),
  188. (name: "Any", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  189. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  190. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  191. props: {}),
  192. ]
  193. type
  194. TSystemCPU* = enum # Also add CPU for in initialization section and
  195. # alias conditionals to condsyms (end of module).
  196. cpuNone, cpuI386, cpuM68k, cpuAlpha, cpuPowerpc, cpuPowerpc64,
  197. cpuPowerpc64el, cpuSparc, cpuVm, cpuHppa, cpuIa64, cpuAmd64, cpuMips,
  198. cpuMipsel, cpuArm, cpuArm64, cpuJS, cpuNimVM, cpuAVR, cpuMSP430,
  199. cpuSparc64, cpuMips64, cpuMips64el, cpuRiscV32, cpuRiscV64, cpuEsp, cpuWasm32,
  200. cpuE2k, cpuLoongArch64
  201. type
  202. TInfoCPU* = tuple[name: string, intSize: int, endian: Endianness,
  203. floatSize, bit: int]
  204. const
  205. EndianToStr*: array[Endianness, string] = ["littleEndian", "bigEndian"]
  206. CPU*: array[succ(low(TSystemCPU))..high(TSystemCPU), TInfoCPU] = [
  207. (name: "i386", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  208. (name: "m68k", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  209. (name: "alpha", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  210. (name: "powerpc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  211. (name: "powerpc64", intSize: 64, endian: bigEndian, floatSize: 64,bit: 64),
  212. (name: "powerpc64el", intSize: 64, endian: littleEndian, floatSize: 64,bit: 64),
  213. (name: "sparc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  214. (name: "vm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  215. (name: "hppa", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  216. (name: "ia64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  217. (name: "amd64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64), # a.k.a. x86_64, covers both amd and intel
  218. (name: "mips", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  219. (name: "mipsel", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  220. (name: "arm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  221. (name: "arm64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  222. (name: "js", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  223. (name: "nimvm", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  224. # xxx this seems buggy; on a 64bit machine, sizeof(int) is 64 in nimvm.
  225. (name: "avr", intSize: 16, endian: littleEndian, floatSize: 32, bit: 16),
  226. (name: "msp430", intSize: 16, endian: littleEndian, floatSize: 32, bit: 16),
  227. (name: "sparc64", intSize: 64, endian: bigEndian, floatSize: 64, bit: 64),
  228. (name: "mips64", intSize: 64, endian: bigEndian, floatSize: 64, bit: 64),
  229. (name: "mips64el", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  230. (name: "riscv32", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  231. (name: "riscv64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  232. (name: "esp", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  233. (name: "wasm32", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  234. (name: "e2k", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  235. (name: "loongarch64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64)]
  236. type
  237. Target* = object
  238. targetCPU*, hostCPU*: TSystemCPU
  239. targetOS*, hostOS*: TSystemOS
  240. intSize*: int
  241. floatSize*: int
  242. ptrSize*: int
  243. tnl*: string # target newline
  244. proc setTarget*(t: var Target; o: TSystemOS, c: TSystemCPU) =
  245. assert(c != cpuNone)
  246. assert(o != osNone)
  247. #echo "new Target: OS: ", o, " CPU: ", c
  248. t.targetCPU = c
  249. t.targetOS = o
  250. t.intSize = CPU[c].intSize div 8
  251. t.floatSize = CPU[c].floatSize div 8
  252. t.ptrSize = CPU[c].bit div 8
  253. t.tnl = OS[o].newLine
  254. proc nameToOS*(name: string): TSystemOS =
  255. for i in succ(osNone)..high(TSystemOS):
  256. if cmpIgnoreStyle(name, OS[i].name) == 0:
  257. return i
  258. result = osNone
  259. proc listOSnames*(): seq[string] =
  260. for i in succ(osNone)..high(TSystemOS):
  261. result.add OS[i].name
  262. proc nameToCPU*(name: string): TSystemCPU =
  263. for i in succ(cpuNone)..high(TSystemCPU):
  264. if cmpIgnoreStyle(name, CPU[i].name) == 0:
  265. return i
  266. result = cpuNone
  267. proc listCPUnames*(): seq[string] =
  268. for i in succ(cpuNone)..high(TSystemCPU):
  269. result.add CPU[i].name
  270. proc setTargetFromSystem*(t: var Target) =
  271. t.hostOS = nameToOS(system.hostOS)
  272. t.hostCPU = nameToCPU(system.hostCPU)
  273. t.setTarget(t.hostOS, t.hostCPU)