platform.nim 13 KB

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