ccgutils.nim 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 s.typ.kind == tySink and conf.selectedGC notin {gcArc, gcAtomicArc, gcOrc, gcHooks}:
  82. # bug #23354:
  83. result = false
  84. elif (optByRef in s.options) or (getSize(conf, pt) > conf.target.floatSize * 3):
  85. result = true # requested anyway
  86. elif (tfFinal in pt.flags) and (pt[0] == nil):
  87. result = false # no need, because no subtyping possible
  88. else:
  89. result = true # ordinary objects are always passed by reference,
  90. # otherwise casting doesn't work
  91. of tyTuple:
  92. result = (getSize(conf, pt) > conf.target.floatSize*3) or (optByRef in s.options)
  93. else:
  94. result = false
  95. # first parameter and return type is 'lent T'? --> use pass by pointer
  96. if s.position == 0 and retType != nil and retType.kind == tyLent:
  97. result = not (pt.kind in {tyVar, tyArray, tyOpenArray, tyVarargs, tyRef, tyPtr, tyPointer} or
  98. pt.kind == tySet and mapSetType(conf, pt) == ctArray)
  99. proc encodeName*(name: string): string =
  100. result = mangle(name)
  101. result = $result.len & result
  102. proc makeUnique(m: BModule; s: PSym, name: string = ""): string =
  103. result = if name == "": s.name.s else: name
  104. result.add "__"
  105. result.add m.g.graph.ifaces[s.itemId.module].uniqueName
  106. result.add "_u"
  107. result.add $s.itemId.item
  108. proc encodeSym*(m: BModule; s: PSym; makeUnique: bool = false): string =
  109. #Module::Type
  110. var name = s.name.s
  111. if makeUnique:
  112. name = makeUnique(m, s, name)
  113. "N" & encodeName(s.skipGenericOwner.name.s) & encodeName(name) & "E"
  114. proc encodeType*(m: BModule; t: PType): string =
  115. result = ""
  116. var kindName = ($t.kind)[2..^1]
  117. kindName[0] = toLower($kindName[0])[0]
  118. case t.kind
  119. of tyObject, tyEnum, tyDistinct, tyUserTypeClass, tyGenericParam:
  120. result = encodeSym(m, t.sym)
  121. of tyGenericInst, tyUserTypeClassInst, tyGenericBody:
  122. result = encodeName(t[0].sym.name.s)
  123. result.add "I"
  124. for i in 1..<t.len - 1:
  125. result.add encodeType(m, t[i])
  126. result.add "E"
  127. of tySequence, tyOpenArray, tyArray, tyVarargs, tyTuple, tyProc, tySet, tyTypeDesc,
  128. tyPtr, tyRef, tyVar, tyLent, tySink, tyStatic, tyUncheckedArray, tyOr, tyAnd, tyBuiltInTypeClass:
  129. result =
  130. case t.kind:
  131. of tySequence: encodeName("seq")
  132. else: encodeName(kindName)
  133. result.add "I"
  134. for i in 0..<t.len:
  135. let s = t[i]
  136. if s.isNil: continue
  137. result.add encodeType(m, s)
  138. result.add "E"
  139. of tyRange:
  140. var val = "range_"
  141. if t.n[0].typ.kind in {tyFloat..tyFloat128}:
  142. val.addFloat t.n[0].floatVal
  143. val.add "_"
  144. val.addFloat t.n[1].floatVal
  145. else:
  146. val.add $t.n[0].intVal & "_" & $t.n[1].intVal
  147. result = encodeName(val)
  148. of tyString..tyUInt64, tyPointer, tyBool, tyChar, tyVoid, tyAnything, tyNil, tyEmpty:
  149. result = encodeName(kindName)
  150. of tyAlias, tyInferred, tyOwned:
  151. result = encodeType(m, t.elementType)
  152. else:
  153. assert false, "encodeType " & $t.kind