platform.nim 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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, osNimrodVM, osStandalone
  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: "NimrodVM", 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. type
  167. TSystemCPU* = enum # Also add CPU for in initialization section and
  168. # alias conditionals to condsyms (end of module).
  169. cpuNone, cpuI386, cpuM68k, cpuAlpha, cpuPowerpc, cpuPowerpc64,
  170. cpuPowerpc64el, cpuSparc, cpuVm, cpuIa64, cpuAmd64, cpuMips, cpuMipsel,
  171. cpuArm, cpuArm64, cpuJS, cpuNimrodVM, cpuAVR, cpuMSP430, cpuSparc64,
  172. cpuMips64, cpuMips64el
  173. type
  174. TEndian* = enum
  175. littleEndian, bigEndian
  176. TInfoCPU* = tuple[name: string, intSize: int, endian: TEndian,
  177. floatSize, bit: int]
  178. const
  179. EndianToStr*: array[TEndian, string] = ["littleEndian", "bigEndian"]
  180. CPU*: array[succ(low(TSystemCPU))..high(TSystemCPU), TInfoCPU] = [
  181. (name: "i386", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  182. (name: "m68k", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  183. (name: "alpha", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  184. (name: "powerpc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  185. (name: "powerpc64", intSize: 64, endian: bigEndian, floatSize: 64,bit: 64),
  186. (name: "powerpc64el", intSize: 64, endian: littleEndian, floatSize: 64,bit: 64),
  187. (name: "sparc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  188. (name: "vm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  189. (name: "ia64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  190. (name: "amd64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  191. (name: "mips", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  192. (name: "mipsel", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  193. (name: "arm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
  194. (name: "arm64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
  195. (name: "js", intSize: 32, endian: bigEndian,floatSize: 64,bit: 32),
  196. (name: "nimrodvm", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
  197. (name: "avr", intSize: 16, endian: littleEndian, floatSize: 32, bit: 16),
  198. (name: "msp430", intSize: 16, endian: littleEndian, floatSize: 32, bit: 16),
  199. (name: "sparc64", intSize: 64, endian: bigEndian, floatSize: 64, bit: 64),
  200. (name: "mips64", intSize: 64, endian: bigEndian, floatSize: 64, bit: 64),
  201. (name: "mips64el", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64)]
  202. var
  203. targetCPU*, hostCPU*: TSystemCPU
  204. targetOS*, hostOS*: TSystemOS
  205. proc nameToOS*(name: string): TSystemOS
  206. proc nameToCPU*(name: string): TSystemCPU
  207. var
  208. intSize*: int
  209. floatSize*: int
  210. ptrSize*: int
  211. tnl*: string # target newline
  212. proc setTarget*(o: TSystemOS, c: TSystemCPU) =
  213. assert(c != cpuNone)
  214. assert(o != osNone)
  215. #echo "new Target: OS: ", o, " CPU: ", c
  216. targetCPU = c
  217. targetOS = o
  218. intSize = CPU[c].intSize div 8
  219. floatSize = CPU[c].floatSize div 8
  220. ptrSize = CPU[c].bit div 8
  221. tnl = OS[o].newLine
  222. proc nameToOS(name: string): TSystemOS =
  223. for i in countup(succ(osNone), high(TSystemOS)):
  224. if cmpIgnoreStyle(name, OS[i].name) == 0:
  225. return i
  226. result = osNone
  227. proc nameToCPU(name: string): TSystemCPU =
  228. for i in countup(succ(cpuNone), high(TSystemCPU)):
  229. if cmpIgnoreStyle(name, CPU[i].name) == 0:
  230. return i
  231. result = cpuNone
  232. hostCPU = nameToCPU(system.hostCPU)
  233. hostOS = nameToOS(system.hostOS)
  234. setTarget(hostOS, hostCPU) # assume no cross-compiling