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 system.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'i64
  31. for i in countup(0, len(s) - 1):
  32. b = b +% ord(s[i])
  33. b = b +% `shl`(b, 10)
  34. b = b xor `shr`(b, 6)
  35. b = b +% `shl`(b, 3)
  36. b = b xor `shr`(b, 11)
  37. b = b +% `shl`(b, 15)
  38. result = b
  39. else:
  40. var a = 0'i32
  41. for i in countup(0, len(s) - 1):
  42. a = a +% ord(s[i]).int32
  43. a = a +% `shl`(a, 10'i32)
  44. a = a xor `shr`(a, 6'i32)
  45. a = a +% `shl`(a, 3'i32)
  46. a = a xor `shr`(a, 11'i32)
  47. a = a +% `shl`(a, 15'i32)
  48. result = 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 "_"