ccgutils.nim 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 declares some helpers for the C code generator.
  10. import
  11. ast, types, msgs, wordrecg,
  12. platform, trees, options, cgendata, mangleutils
  13. import std/[hashes, strutils, formatfloat]
  14. when defined(nimPreviewSlimSystem):
  15. import std/assertions
  16. proc getPragmaStmt*(n: PNode, w: TSpecialWord): PNode =
  17. case n.kind
  18. of nkStmtList:
  19. result = nil
  20. for i in 0..<n.len:
  21. result = getPragmaStmt(n[i], w)
  22. if result != nil: break
  23. of nkPragma:
  24. result = nil
  25. for i in 0..<n.len:
  26. if whichPragma(n[i]) == w: return n[i]
  27. else:
  28. result = nil
  29. proc stmtsContainPragma*(n: PNode, w: TSpecialWord): bool =
  30. result = getPragmaStmt(n, w) != nil
  31. proc hashString*(conf: ConfigRef; s: string): BiggestInt =
  32. # has to be the same algorithm as strmantle.hashString!
  33. if CPU[conf.target.targetCPU].bit == 64:
  34. # we have to use the same bitwidth
  35. # as the target CPU
  36. var b = 0'u64
  37. for i in 0..<s.len:
  38. b = b + uint(s[i])
  39. b = b + (b shl 10)
  40. b = b xor (b shr 6)
  41. b = b + (b shl 3)
  42. b = b xor (b shr 11)
  43. b = b + (b shl 15)
  44. result = cast[Hash](b)
  45. else:
  46. var a = 0'u32
  47. for i in 0..<s.len:
  48. a = a + uint32(s[i])
  49. a = a + (a shl 10)
  50. a = a xor (a shr 6)
  51. a = a + (a shl 3)
  52. a = a xor (a shr 11)
  53. a = a + (a shl 15)
  54. result = cast[Hash](uint(a))
  55. template getUniqueType*(key: PType): PType = key
  56. proc makeSingleLineCString*(s: string): string =
  57. result = "\""
  58. for c in items(s):
  59. c.toCChar(result)
  60. result.add('\"')
  61. proc mapSetType(conf: ConfigRef; typ: PType): TCTypeKind =
  62. case int(getSize(conf, typ))
  63. of 1: result = ctInt8
  64. of 2: result = ctInt16
  65. of 4: result = ctInt32
  66. of 8: result = ctInt64
  67. else: result = ctArray
  68. proc ccgIntroducedPtr*(conf: ConfigRef; s: PSym, retType: PType): bool =
  69. var pt = skipTypes(s.typ, typedescInst)
  70. assert skResult != s.kind
  71. #note precedence: params override types
  72. if optByRef in s.options: return true
  73. elif sfByCopy in s.flags: return false
  74. elif tfByRef in pt.flags: return true
  75. elif tfByCopy in pt.flags: return false
  76. case pt.kind
  77. of tyObject:
  78. if s.typ.sym != nil and sfForward in s.typ.sym.flags:
  79. # forwarded objects are *always* passed by pointers for consistency!
  80. result = true
  81. elif (optByRef in s.options) or (getSize(conf, pt) > conf.target.floatSize * 3):
  82. result = true # requested anyway
  83. elif (tfFinal in pt.flags) and (pt[0] == nil):
  84. result = false # no need, because no subtyping possible
  85. else:
  86. result = true # ordinary objects are always passed by reference,
  87. # otherwise casting doesn't work
  88. of tyTuple:
  89. result = (getSize(conf, pt) > conf.target.floatSize*3) or (optByRef in s.options)
  90. else:
  91. result = false
  92. # first parameter and return type is 'lent T'? --> use pass by pointer
  93. if s.position == 0 and retType != nil and retType.kind == tyLent:
  94. result = not (pt.kind in {tyVar, tyArray, tyOpenArray, tyVarargs, tyRef, tyPtr, tyPointer} or
  95. pt.kind == tySet and mapSetType(conf, pt) == ctArray)
  96. proc encodeName*(name: string): string =
  97. result = mangle(name)
  98. result = $result.len & result
  99. proc makeUnique(m: BModule; s: PSym, name: string = ""): string =
  100. result = if name == "": s.name.s else: name
  101. result.add "__"
  102. result.add m.g.graph.ifaces[s.itemId.module].uniqueName
  103. result.add "_u"
  104. result.add $s.itemId.item
  105. proc encodeSym*(m: BModule; s: PSym; makeUnique: bool = false): string =
  106. #Module::Type
  107. var name = s.name.s
  108. if makeUnique:
  109. name = makeUnique(m, s, name)
  110. "N" & encodeName(s.skipGenericOwner.name.s) & encodeName(name) & "E"
  111. proc encodeType*(m: BModule; t: PType): string =
  112. result = ""
  113. var kindName = ($t.kind)[2..^1]
  114. kindName[0] = toLower($kindName[0])[0]
  115. case t.kind
  116. of tyObject, tyEnum, tyDistinct, tyUserTypeClass, tyGenericParam:
  117. result = encodeSym(m, t.sym)
  118. of tyGenericInst, tyUserTypeClassInst, tyGenericBody:
  119. result = encodeName(t[0].sym.name.s)
  120. result.add "I"
  121. for i in 1..<t.len - 1:
  122. result.add encodeType(m, t[i])
  123. result.add "E"
  124. of tySequence, tyOpenArray, tyArray, tyVarargs, tyTuple, tyProc, tySet, tyTypeDesc,
  125. tyPtr, tyRef, tyVar, tyLent, tySink, tyStatic, tyUncheckedArray, tyOr, tyAnd, tyBuiltInTypeClass:
  126. result =
  127. case t.kind:
  128. of tySequence: encodeName("seq")
  129. else: encodeName(kindName)
  130. result.add "I"
  131. for i in 0..<t.len:
  132. let s = t[i]
  133. if s.isNil: continue
  134. result.add encodeType(m, s)
  135. result.add "E"
  136. of tyRange:
  137. var val = "range_"
  138. if t.n[0].typ.kind in {tyFloat..tyFloat128}:
  139. val.addFloat t.n[0].floatVal
  140. val.add "_"
  141. val.addFloat t.n[1].floatVal
  142. else:
  143. val.add $t.n[0].intVal & "_" & $t.n[1].intVal
  144. result = encodeName(val)
  145. of tyString..tyUInt64, tyPointer, tyBool, tyChar, tyVoid, tyAnything, tyNil, tyEmpty:
  146. result = encodeName(kindName)
  147. of tyAlias, tyInferred, tyOwned:
  148. result = encodeType(m, t.elementType)
  149. else:
  150. assert false, "encodeType " & $t.kind