debuginfo.nim 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2017 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## The compiler can generate debuginfo to help debuggers in translating back
  10. ## from C/C++/JS code to Nim. The data structure has been designed to produce
  11. ## something useful with Nim's marshal module.
  12. import sighashes
  13. type
  14. FilenameMapping* = object
  15. package*, file*: string
  16. mangled*: SigHash
  17. EnumDesc* = object
  18. size*: int
  19. owner*: SigHash
  20. id*: int
  21. name*: string
  22. values*: seq[(string, int)]
  23. DebugInfo* = object
  24. version*: int
  25. files*: seq[FilenameMapping]
  26. enums*: seq[EnumDesc]
  27. conflicts*: bool
  28. proc sdbmHash(package, file: string): SigHash =
  29. result = 0
  30. for i in 0..<package.len:
  31. result &= package[i]
  32. result &= '.'
  33. for i in 0..<file.len:
  34. result &= file[i]
  35. proc register*(self: var DebugInfo; package, file: string): SigHash =
  36. result = sdbmHash(package, file)
  37. for f in self.files:
  38. if f.mangled == result:
  39. if f.package == package and f.file == file: return
  40. self.conflicts = true
  41. break
  42. self.files.add(FilenameMapping(package: package, file: file, mangled: result))
  43. proc hasEnum*(self: DebugInfo; ename: string; id: int; owner: SigHash): bool =
  44. for en in self.enums:
  45. if en.owner == owner and en.name == ename and en.id == id: return true
  46. proc registerEnum*(self: var DebugInfo; ed: EnumDesc) =
  47. self.enums.add ed
  48. proc init*(self: var DebugInfo) =
  49. self.version = 1
  50. self.files = @[]
  51. self.enums = @[]
  52. var gDebugInfo*: DebugInfo
  53. debuginfo.init gDebugInfo
  54. import marshal, streams
  55. proc writeDebugInfo*(self: var DebugInfo; file: string) =
  56. let s = newFileStream(file, fmWrite)
  57. store(s, self)
  58. s.close
  59. proc writeDebugInfo*(file: string) = writeDebugInfo(gDebugInfo, file)
  60. proc loadDebugInfo*(self: var DebugInfo; file: string) =
  61. let s = newFileStream(file, fmRead)
  62. load(s, self)
  63. s.close
  64. proc loadDebugInfo*(file: string) = loadDebugInfo(gDebugInfo, file)