platform.nim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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, osHaiku, osAndroid, osVxworks
  22. osGenode, osJS, osNimVM, osStandalone, osNintendoSwitch
  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: "Haiku", parDir: "..", dllFrmt: "lib$1.so", altDirSep: ":",
  138. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  139. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  140. props: {ospNeedsPIC, ospPosix, ospLacksThreadVars}),
  141. (name: "Android", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  142. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  143. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  144. props: {ospNeedsPIC, ospPosix}),
  145. (name: "VxWorks", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  146. objExt: ".o", newLine: "\x0A", pathSep: ";", dirSep: "\\",
  147. scriptExt: ".sh", curDir: ".", exeExt: ".vxe", extSep: ".",
  148. props: {ospNeedsPIC, ospPosix, ospLacksThreadVars}),
  149. (name: "Genode", pardir: "..", dllFrmt: "$1.lib.so", altDirSep: "/",
  150. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  151. scriptExt: "", curDir: "/", exeExt: "", extSep: ".",
  152. props: {ospNeedsPIC, ospLacksThreadVars}),
  153. (name: "JS", parDir: "..",
  154. dllFrmt: "lib$1.so", altDirSep: "/",
  155. objExt: ".o", newLine: "\x0A",
  156. pathSep: ":", dirSep: "/",
  157. scriptExt: ".sh", curDir: ".",
  158. exeExt: "", extSep: ".", props: {}),
  159. (name: "NimVM", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  160. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  161. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {}),
  162. (name: "Standalone", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  163. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  164. scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
  165. props: {}),
  166. (name: "NintendoSwitch", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
  167. objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
  168. scriptExt: ".sh", curDir: ".", exeExt: ".elf", extSep: ".",
  169. props: {ospNeedsPIC, ospPosix}),
  170. ]
  171. type
  172. TSystemCPU* = enum # Also add CPU for in initialization section and
  173. # alias conditionals to condsyms (end of module).
  174. cpuNone, cpuI386, cpuM68k, cpuAlpha, cpuPowerpc, cpuPowerpc64,
  175. cpuPowerpc64el, cpuSparc, cpuVm, cpuIa64, cpuAmd64, cpuMips, cpuMipsel,
  176. cpuArm, cpuArm64, cpuJS, cpuNimVM, cpuAVR, cpuMSP430, cpuSparc64,
  177. cpuMips64, cpuMips64el, cpuRiscV64
  178. type
  179. TEndian* = enum
  180. littleEndian, bigEndian
  181. TInfoCPU* = tuple[name: string, intSize: int, endian: TEndian,
  182. floatSize, bit: int]
  183. const
  184. EndianToStr*: array[TEndian, string] = ["littleEndian", "bigEndian"]
  185. CPU*: array[succ(low(TSystemCPU))..high(TSystemCPU), TInfoCPU] = [
  186. (name: "i386", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  187. (name: "m68k", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  188. (name: "alpha", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  189. (name: "powerpc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  190. (name: "powerpc64", intSize: 64, endian: bigEndian, floatSize: 64,bit: 64),
  191. (name: "powerpc64el", intSize: 64, endian: littleEndian, floatSize: 64,bit: 64),
  192. (name: "sparc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  193. (name: "vm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  194. (name: "ia64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  195. (name: "amd64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  196. (name: "mips", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  197. (name: "mipsel", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  198. (name: "arm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  199. (name: "arm64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  200. (name: "js", intSize: 32, endian: bigEndian,floatSize: 64,bit: 32),
  201. (name: "nimvm", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  202. (name: "avr", intSize: 16, endian: littleEndian, floatSize: 32, bit: 16),
  203. (name: "msp430", intSize: 16, endian: littleEndian, floatSize: 32, bit: 16),
  204. (name: "sparc64", intSize: 64, endian: bigEndian, floatSize: 64, bit: 64),
  205. (name: "mips64", intSize: 64, endian: bigEndian, floatSize: 64, bit: 64),
  206. (name: "mips64el", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  207. (name: "riscv64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64)]
  208. type
  209. Target* = object
  210. targetCPU*, hostCPU*: TSystemCPU
  211. targetOS*, hostOS*: TSystemOS
  212. intSize*: int
  213. floatSize*: int
  214. ptrSize*: int
  215. tnl*: string # target newline
  216. proc setTarget*(t: var Target; o: TSystemOS, c: TSystemCPU) =
  217. assert(c != cpuNone)
  218. assert(o != osNone)
  219. #echo "new Target: OS: ", o, " CPU: ", c
  220. t.targetCPU = c
  221. t.targetOS = o
  222. t.intSize = CPU[c].intSize div 8
  223. t.floatSize = CPU[c].floatSize div 8
  224. t.ptrSize = CPU[c].bit div 8
  225. t.tnl = OS[o].newLine
  226. proc nameToOS*(name: string): TSystemOS =
  227. for i in countup(succ(osNone), high(TSystemOS)):
  228. if cmpIgnoreStyle(name, OS[i].name) == 0:
  229. return i
  230. result = osNone
  231. proc nameToCPU*(name: string): TSystemCPU =
  232. for i in countup(succ(cpuNone), high(TSystemCPU)):
  233. if cmpIgnoreStyle(name, CPU[i].name) == 0:
  234. return i
  235. result = cpuNone
  236. proc setTargetFromSystem*(t: var Target) =
  237. t.hostOS = nameToOS(system.hostOS)
  238. t.hostCPU = nameToCPU(system.hostCPU)
  239. t.setTarget(t.hostOS, t.hostCPU)