ccgutils.nim 4.2 KB

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