ccgutils.nim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, astalgo, ropes, hashes, strutils, types, msgs, wordrecg,
  12. platform, trees, options
  13. proc getPragmaStmt*(n: PNode, w: TSpecialWord): PNode =
  14. case n.kind
  15. of nkStmtList:
  16. for i in 0 ..< n.len:
  17. result = getPragmaStmt(n[i], w)
  18. if result != nil: break
  19. of nkPragma:
  20. for i in 0 ..< n.len:
  21. if whichPragma(n[i]) == w: return n[i]
  22. else: discard
  23. proc stmtsContainPragma*(n: PNode, w: TSpecialWord): bool =
  24. result = getPragmaStmt(n, w) != nil
  25. proc hashString*(conf: ConfigRef; s: string): BiggestInt =
  26. # has to be the same algorithm as strmantle.hashString!
  27. if CPU[conf.target.targetCPU].bit == 64:
  28. # we have to use the same bitwidth
  29. # as the target CPU
  30. var b = 0'u64
  31. for i in 0 ..< len(s):
  32. b = b + uint(s[i])
  33. b = b + (b shl 10)
  34. b = b xor (b shr 6)
  35. b = b + (b shl 3)
  36. b = b xor (b shr 11)
  37. b = b + (b shl 15)
  38. result = cast[Hash](b)
  39. else:
  40. var a = 0'u32
  41. for i in 0 ..< len(s):
  42. a = a + uint32(s[i])
  43. a = a + (a shl 10)
  44. a = a xor (a shr 6)
  45. a = a + (a shl 3)
  46. a = a xor (a shr 11)
  47. a = a + (a shl 15)
  48. result = cast[Hash](a)
  49. template getUniqueType*(key: PType): PType = key
  50. proc makeSingleLineCString*(s: string): string =
  51. result = "\""
  52. for c in items(s):
  53. c.toCChar(result)
  54. result.add('\"')
  55. proc mangle*(name: string): string =
  56. result = newStringOfCap(name.len)
  57. var start = 0
  58. if name[0] in Digits:
  59. result.add("X" & name[0])
  60. start = 1
  61. var requiresUnderscore = false
  62. template special(x) =
  63. result.add x
  64. requiresUnderscore = true
  65. for i in start..(name.len-1):
  66. let c = name[i]
  67. case c
  68. of 'a'..'z', '0'..'9', 'A'..'Z':
  69. add(result, c)
  70. of '_':
  71. # we generate names like 'foo_9' for scope disambiguations and so
  72. # disallow this here:
  73. if i > 0 and i < name.len-1 and name[i+1] in Digits:
  74. discard
  75. else:
  76. add(result, c)
  77. of '$': special "dollar"
  78. of '%': special "percent"
  79. of '&': special "amp"
  80. of '^': special "roof"
  81. of '!': special "emark"
  82. of '?': special "qmark"
  83. of '*': special "star"
  84. of '+': special "plus"
  85. of '-': special "minus"
  86. of '/': special "slash"
  87. of '\\': special "backslash"
  88. of '=': special "eq"
  89. of '<': special "lt"
  90. of '>': special "gt"
  91. of '~': special "tilde"
  92. of ':': special "colon"
  93. of '.': special "dot"
  94. of '@': special "at"
  95. of '|': special "bar"
  96. else:
  97. add(result, "X" & toHex(ord(c), 2))
  98. requiresUnderscore = true
  99. if requiresUnderscore:
  100. result.add "_"