magicsys.nim 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. # Built-in types and compilerprocs are registered here.
  10. import
  11. ast, astalgo, hashes, msgs, platform, nversion, times, idents,
  12. modulegraphs, lineinfos
  13. export createMagic
  14. proc nilOrSysInt*(g: ModuleGraph): PType = g.sysTypes[tyInt]
  15. proc registerSysType*(g: ModuleGraph; t: PType) =
  16. if g.sysTypes[t.kind] == nil: g.sysTypes[t.kind] = t
  17. proc newSysType(g: ModuleGraph; kind: TTypeKind, size: int): PType =
  18. result = newType(kind, g.systemModule)
  19. result.size = size
  20. result.align = size.int16
  21. proc getSysSym*(g: ModuleGraph; info: TLineInfo; name: string): PSym =
  22. result = strTableGet(g.systemModule.tab, getIdent(g.cache, name))
  23. if result == nil:
  24. localError(g.config, info, "system module needs: " & name)
  25. result = newSym(skError, getIdent(g.cache, name), g.systemModule, g.systemModule.info, {})
  26. result.typ = newType(tyError, g.systemModule)
  27. if result.kind == skAlias: result = result.owner
  28. proc getSysMagic*(g: ModuleGraph; info: TLineInfo; name: string, m: TMagic): PSym =
  29. var ti: TIdentIter
  30. let id = getIdent(g.cache, name)
  31. var r = initIdentIter(ti, g.systemModule.tab, id)
  32. while r != nil:
  33. if r.magic == m:
  34. # prefer the tyInt variant:
  35. if r.typ.sons[0] != nil and r.typ.sons[0].kind == tyInt: return r
  36. result = r
  37. r = nextIdentIter(ti, g.systemModule.tab)
  38. if result != nil: return result
  39. localError(g.config, info, "system module needs: " & name)
  40. result = newSym(skError, id, g.systemModule, g.systemModule.info, {})
  41. result.typ = newType(tyError, g.systemModule)
  42. proc sysTypeFromName*(g: ModuleGraph; info: TLineInfo; name: string): PType =
  43. result = getSysSym(g, info, name).typ
  44. proc getSysType*(g: ModuleGraph; info: TLineInfo; kind: TTypeKind): PType =
  45. template sysTypeFromName(s: string): untyped = sysTypeFromName(g, info, s)
  46. result = g.sysTypes[kind]
  47. if result == nil:
  48. case kind
  49. of tyInt: result = sysTypeFromName("int")
  50. of tyInt8: result = sysTypeFromName("int8")
  51. of tyInt16: result = sysTypeFromName("int16")
  52. of tyInt32: result = sysTypeFromName("int32")
  53. of tyInt64: result = sysTypeFromName("int64")
  54. of tyUInt: result = sysTypeFromName("uint")
  55. of tyUInt8: result = sysTypeFromName("uint8")
  56. of tyUInt16: result = sysTypeFromName("uint16")
  57. of tyUInt32: result = sysTypeFromName("uint32")
  58. of tyUInt64: result = sysTypeFromName("uint64")
  59. of tyFloat: result = sysTypeFromName("float")
  60. of tyFloat32: result = sysTypeFromName("float32")
  61. of tyFloat64: return sysTypeFromName("float64")
  62. of tyFloat128: result = sysTypeFromName("float128")
  63. of tyBool: result = sysTypeFromName("bool")
  64. of tyChar: result = sysTypeFromName("char")
  65. of tyString: result = sysTypeFromName("string")
  66. of tyCString: result = sysTypeFromName("cstring")
  67. of tyPointer: result = sysTypeFromName("pointer")
  68. of tyNil: result = newSysType(g, tyNil, g.config.target.ptrSize)
  69. else: internalError(g.config, "request for typekind: " & $kind)
  70. g.sysTypes[kind] = result
  71. if result.kind != kind:
  72. internalError(g.config, "wanted: " & $kind & " got: " & $result.kind)
  73. if result == nil: internalError(g.config, "type not found: " & $kind)
  74. proc resetSysTypes*(g: ModuleGraph) =
  75. g.systemModule = nil
  76. initStrTable(g.compilerprocs)
  77. initStrTable(g.exposed)
  78. for i in low(g.sysTypes)..high(g.sysTypes):
  79. g.sysTypes[i] = nil
  80. for i in low(g.intTypeCache)..high(g.intTypeCache):
  81. g.intTypeCache[i] = nil
  82. proc getIntLitType*(g: ModuleGraph; literal: PNode): PType =
  83. # we cache some common integer literal types for performance:
  84. let value = literal.intVal
  85. if value >= low(g.intTypeCache) and value <= high(g.intTypeCache):
  86. result = g.intTypeCache[value.int]
  87. if result == nil:
  88. let ti = getSysType(g, literal.info, tyInt)
  89. result = copyType(ti, ti.owner, false)
  90. result.n = literal
  91. g.intTypeCache[value.int] = result
  92. else:
  93. let ti = getSysType(g, literal.info, tyInt)
  94. result = copyType(ti, ti.owner, false)
  95. result.n = literal
  96. proc getFloatLitType*(g: ModuleGraph; literal: PNode): PType =
  97. # for now we do not cache these:
  98. result = newSysType(g, tyFloat, size=8)
  99. result.n = literal
  100. proc skipIntLit*(t: PType): PType {.inline.} =
  101. if t.n != nil and t.kind in {tyInt, tyFloat}:
  102. result = copyType(t, t.owner, false)
  103. result.n = nil
  104. else:
  105. result = t
  106. proc addSonSkipIntLit*(father, son: PType) =
  107. when not defined(nimNoNilSeqs):
  108. if isNil(father.sons): father.sons = @[]
  109. let s = son.skipIntLit
  110. add(father.sons, s)
  111. propagateToOwner(father, s)
  112. proc setIntLitType*(g: ModuleGraph; result: PNode) =
  113. let i = result.intVal
  114. case g.config.target.intSize
  115. of 8: result.typ = getIntLitType(g, result)
  116. of 4:
  117. if i >= low(int32) and i <= high(int32):
  118. result.typ = getIntLitType(g, result)
  119. else:
  120. result.typ = getSysType(g, result.info, tyInt64)
  121. of 2:
  122. if i >= low(int16) and i <= high(int16):
  123. result.typ = getIntLitType(g, result)
  124. elif i >= low(int32) and i <= high(int32):
  125. result.typ = getSysType(g, result.info, tyInt32)
  126. else:
  127. result.typ = getSysType(g, result.info, tyInt64)
  128. of 1:
  129. # 8 bit CPUs are insane ...
  130. if i >= low(int8) and i <= high(int8):
  131. result.typ = getIntLitType(g, result)
  132. elif i >= low(int16) and i <= high(int16):
  133. result.typ = getSysType(g, result.info, tyInt16)
  134. elif i >= low(int32) and i <= high(int32):
  135. result.typ = getSysType(g, result.info, tyInt32)
  136. else:
  137. result.typ = getSysType(g, result.info, tyInt64)
  138. else:
  139. internalError(g.config, result.info, "invalid int size")
  140. proc getCompilerProc*(g: ModuleGraph; name: string): PSym =
  141. let ident = getIdent(g.cache, name)
  142. result = strTableGet(g.compilerprocs, ident)
  143. proc registerCompilerProc*(g: ModuleGraph; s: PSym) =
  144. strTableAdd(g.compilerprocs, s)
  145. proc registerNimScriptSymbol*(g: ModuleGraph; s: PSym) =
  146. # Nimscript symbols must be al unique:
  147. let conflict = strTableGet(g.exposed, s.name)
  148. if conflict == nil:
  149. strTableAdd(g.exposed, s)
  150. else:
  151. localError(g.config, s.info,
  152. "symbol conflicts with other .exportNims symbol at: " & g.config$conflict.info)
  153. proc getNimScriptSymbol*(g: ModuleGraph; name: string): PSym =
  154. strTableGet(g.exposed, getIdent(g.cache, name))
  155. proc resetNimScriptSymbols*(g: ModuleGraph) = initStrTable(g.exposed)