ccgutils.nim 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. 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..<s.len:
  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..<s.len:
  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:
  66. let c = name[i]
  67. case c
  68. of 'a'..'z', '0'..'9', 'A'..'Z':
  69. result.add(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. result.add(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. result.add("X" & toHex(ord(c), 2))
  98. requiresUnderscore = true
  99. if requiresUnderscore:
  100. result.add "_"
  101. proc mapSetType(conf: ConfigRef; typ: PType): TCTypeKind =
  102. case int(getSize(conf, typ))
  103. of 1: result = ctInt8
  104. of 2: result = ctInt16
  105. of 4: result = ctInt32
  106. of 8: result = ctInt64
  107. else: result = ctArray
  108. proc ccgIntroducedPtr*(conf: ConfigRef; s: PSym, retType: PType): bool =
  109. var pt = skipTypes(s.typ, typedescInst)
  110. assert skResult != s.kind
  111. if tfByRef in pt.flags: return true
  112. elif tfByCopy in pt.flags: return false
  113. case pt.kind
  114. of tyObject:
  115. if s.typ.sym != nil and sfForward in s.typ.sym.flags:
  116. # forwarded objects are *always* passed by pointers for consistency!
  117. result = true
  118. elif (optByRef in s.options) or (getSize(conf, pt) > conf.target.floatSize * 3):
  119. result = true # requested anyway
  120. elif (tfFinal in pt.flags) and (pt[0] == nil):
  121. result = false # no need, because no subtyping possible
  122. else:
  123. result = true # ordinary objects are always passed by reference,
  124. # otherwise casting doesn't work
  125. of tyTuple:
  126. result = (getSize(conf, pt) > conf.target.floatSize*3) or (optByRef in s.options)
  127. else:
  128. result = false
  129. # first parameter and return type is 'lent T'? --> use pass by pointer
  130. if s.position == 0 and retType != nil and retType.kind == tyLent:
  131. result = not (pt.kind in {tyVar, tyArray, tyOpenArray, tyVarargs, tyRef, tyPtr, tyPointer} or
  132. pt.kind == tySet and mapSetType(conf, pt) == ctArray)