platform.nim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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, 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: "Any", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  175. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  176. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  177. props: {}),
  178. ]
  179. type
  180. TSystemCPU* = enum # Also add CPU for in initialization section and
  181. # alias conditionals to condsyms (end of module).
  182. cpuNone, cpuI386, cpuM68k, cpuAlpha, cpuPowerpc, cpuPowerpc64,
  183. cpuPowerpc64el, cpuSparc, cpuVm, cpuHppa, cpuIa64, cpuAmd64, cpuMips,
  184. cpuMipsel, cpuArm, cpuArm64, cpuJS, cpuNimVM, cpuAVR, cpuMSP430,
  185. cpuSparc64, cpuMips64, cpuMips64el, cpuRiscV64, cpuWasm32
  186. type
  187. TEndian* = enum
  188. littleEndian, bigEndian
  189. TInfoCPU* = tuple[name: string, intSize: int, endian: TEndian,
  190. floatSize, bit: int]
  191. const
  192. EndianToStr*: array[TEndian, string] = ["littleEndian", "bigEndian"]
  193. CPU*: array[succ(low(TSystemCPU))..high(TSystemCPU), TInfoCPU] = [
  194. (name: "i386", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  195. (name: "m68k", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  196. (name: "alpha", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  197. (name: "powerpc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  198. (name: "powerpc64", intSize: 64, endian: bigEndian, floatSize: 64,bit: 64),
  199. (name: "powerpc64el", intSize: 64, endian: littleEndian, floatSize: 64,bit: 64),
  200. (name: "sparc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  201. (name: "vm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  202. (name: "hppa", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  203. (name: "ia64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  204. (name: "amd64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  205. (name: "mips", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  206. (name: "mipsel", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  207. (name: "arm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  208. (name: "arm64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  209. (name: "js", intSize: 32, endian: bigEndian,floatSize: 64,bit: 32),
  210. (name: "nimvm", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  211. (name: "avr", intSize: 16, endian: littleEndian, floatSize: 32, bit: 16),
  212. (name: "msp430", intSize: 16, endian: littleEndian, floatSize: 32, bit: 16),
  213. (name: "sparc64", intSize: 64, endian: bigEndian, floatSize: 64, bit: 64),
  214. (name: "mips64", intSize: 64, endian: bigEndian, floatSize: 64, bit: 64),
  215. (name: "mips64el", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  216. (name: "riscv64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  217. (name: "wasm32", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32)]
  218. type
  219. Target* = object
  220. targetCPU*, hostCPU*: TSystemCPU
  221. targetOS*, hostOS*: TSystemOS
  222. intSize*: int
  223. floatSize*: int
  224. ptrSize*: int
  225. tnl*: string # target newline
  226. proc setTarget*(t: var Target; o: TSystemOS, c: TSystemCPU) =
  227. assert(c != cpuNone)
  228. assert(o != osNone)
  229. #echo "new Target: OS: ", o, " CPU: ", c
  230. t.targetCPU = c
  231. t.targetOS = o
  232. t.intSize = CPU[c].intSize div 8
  233. t.floatSize = CPU[c].floatSize div 8
  234. t.ptrSize = CPU[c].bit div 8
  235. t.tnl = OS[o].newLine
  236. proc nameToOS*(name: string): TSystemOS =
  237. for i in succ(osNone)..high(TSystemOS):
  238. if cmpIgnoreStyle(name, OS[i].name) == 0:
  239. return i
  240. result = osNone
  241. proc listOSnames*(): seq[string] =
  242. for i in succ(osNone)..high(TSystemOS):
  243. result.add OS[i].name
  244. proc nameToCPU*(name: string): TSystemCPU =
  245. for i in succ(cpuNone)..high(TSystemCPU):
  246. if cmpIgnoreStyle(name, CPU[i].name) == 0:
  247. return i
  248. result = cpuNone
  249. proc listCPUnames*(): seq[string] =
  250. for i in succ(cpuNone)..high(TSystemCPU):
  251. result.add CPU[i].name
  252. proc setTargetFromSystem*(t: var Target) =
  253. t.hostOS = nameToOS(system.hostOS)
  254. t.hostCPU = nameToCPU(system.hostCPU)
  255. t.setTarget(t.hostOS, t.hostCPU)