ccgutils.nim 4.3 KB

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