ccgutils.nim 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, hashes, strutils, msgs, wordrecg,
  12. platform, trees, options, cgendata
  13. when defined(nimPreviewSlimSystem):
  14. import std/assertions
  15. proc getPragmaStmt*(n: PNode, w: TSpecialWord): PNode =
  16. case n.kind
  17. of nkStmtList:
  18. result = nil
  19. for i in 0..<n.len:
  20. result = getPragmaStmt(n[i], w)
  21. if result != nil: break
  22. of nkPragma:
  23. result = nil
  24. for i in 0..<n.len:
  25. if whichPragma(n[i]) == w: return n[i]
  26. else:
  27. result = nil
  28. proc stmtsContainPragma*(n: PNode, w: TSpecialWord): bool =
  29. result = getPragmaStmt(n, w) != nil
  30. proc hashString*(conf: ConfigRef; s: string): BiggestInt =
  31. # has to be the same algorithm as strmantle.hashString!
  32. if CPU[conf.target.targetCPU].bit == 64:
  33. # we have to use the same bitwidth
  34. # as the target CPU
  35. var b = 0'u64
  36. for i in 0..<s.len:
  37. b = b + uint(s[i])
  38. b = b + (b shl 10)
  39. b = b xor (b shr 6)
  40. b = b + (b shl 3)
  41. b = b xor (b shr 11)
  42. b = b + (b shl 15)
  43. result = cast[Hash](b)
  44. else:
  45. var a = 0'u32
  46. for i in 0..<s.len:
  47. a = a + uint32(s[i])
  48. a = a + (a shl 10)
  49. a = a xor (a shr 6)
  50. a = a + (a shl 3)
  51. a = a xor (a shr 11)
  52. a = a + (a shl 15)
  53. result = cast[Hash](uint(a))
  54. template getUniqueType*(key: PType): PType = key
  55. proc makeSingleLineCString*(s: string): string =
  56. result = "\""
  57. for c in items(s):
  58. c.toCChar(result)
  59. result.add('\"')
  60. proc mangle*(name: string): string =
  61. result = newStringOfCap(name.len)
  62. var start = 0
  63. if name[0] in Digits:
  64. result.add("X" & name[0])
  65. start = 1
  66. var requiresUnderscore = false
  67. template special(x) =
  68. result.add x
  69. requiresUnderscore = true
  70. for i in start..<name.len:
  71. let c = name[i]
  72. case c
  73. of 'a'..'z', '0'..'9', 'A'..'Z':
  74. result.add(c)
  75. of '_':
  76. # we generate names like 'foo_9' for scope disambiguations and so
  77. # disallow this here:
  78. if i > 0 and i < name.len-1 and name[i+1] in Digits:
  79. discard
  80. else:
  81. result.add(c)
  82. of '$': special "dollar"
  83. of '%': special "percent"
  84. of '&': special "amp"
  85. of '^': special "roof"
  86. of '!': special "emark"
  87. of '?': special "qmark"
  88. of '*': special "star"
  89. of '+': special "plus"
  90. of '-': special "minus"
  91. of '/': special "slash"
  92. of '\\': special "backslash"
  93. of '=': special "eq"
  94. of '<': special "lt"
  95. of '>': special "gt"
  96. of '~': special "tilde"
  97. of ':': special "colon"
  98. of '.': special "dot"
  99. of '@': special "at"
  100. of '|': special "bar"
  101. else:
  102. result.add("X" & toHex(ord(c), 2))
  103. requiresUnderscore = true
  104. if requiresUnderscore:
  105. result.add "_"
  106. proc mapSetType(conf: ConfigRef; typ: PType): TCTypeKind =
  107. case int(getSize(conf, typ))
  108. of 1: result = ctInt8
  109. of 2: result = ctInt16
  110. of 4: result = ctInt32
  111. of 8: result = ctInt64
  112. else: result = ctArray
  113. proc ccgIntroducedPtr*(conf: ConfigRef; s: PSym, retType: PType): bool =
  114. var pt = skipTypes(s.typ, typedescInst)
  115. assert skResult != s.kind
  116. #note precedence: params override types
  117. if optByRef in s.options: return true
  118. elif sfByCopy in s.flags: return false
  119. elif tfByRef in pt.flags: return true
  120. elif tfByCopy in pt.flags: return false
  121. case pt.kind
  122. of tyObject:
  123. if s.typ.sym != nil and sfForward in s.typ.sym.flags:
  124. # forwarded objects are *always* passed by pointers for consistency!
  125. result = true
  126. elif (optByRef in s.options) or (getSize(conf, pt) > conf.target.floatSize * 3):
  127. result = true # requested anyway
  128. elif (tfFinal in pt.flags) and (pt[0] == nil):
  129. result = false # no need, because no subtyping possible
  130. else:
  131. result = true # ordinary objects are always passed by reference,
  132. # otherwise casting doesn't work
  133. of tyTuple:
  134. result = (getSize(conf, pt) > conf.target.floatSize*3) or (optByRef in s.options)
  135. else:
  136. result = false
  137. # first parameter and return type is 'lent T'? --> use pass by pointer
  138. if s.position == 0 and retType != nil and retType.kind == tyLent:
  139. result = not (pt.kind in {tyVar, tyArray, tyOpenArray, tyVarargs, tyRef, tyPtr, tyPointer} or
  140. pt.kind == tySet and mapSetType(conf, pt) == ctArray)