iclineinfos.nim 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2024 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # For the line information we use 32 bits. They are used as follows:
  10. # Bit 0 (AsideBit): If we have inline line information or not. If not, the
  11. # remaining 31 bits are used as an index into a seq[(LitId, int, int)].
  12. #
  13. # We use 10 bits for the "file ID", this means a program can consist of as much
  14. # as 1024 different files. (If it uses more files than that, the overflow bit
  15. # would be set.)
  16. # This means we have 21 bits left to encode the (line, col) pair. We use 7 bits for the column
  17. # so 128 is the limit and 14 bits for the line number.
  18. # The packed representation supports files with up to 16384 lines.
  19. # Keep in mind that whenever any limit is reached the AsideBit is set and the real line
  20. # information is kept in a side channel.
  21. import std / assertions
  22. const
  23. AsideBit = 1
  24. FileBits = 10
  25. LineBits = 14
  26. ColBits = 7
  27. FileMax = (1 shl FileBits) - 1
  28. LineMax = (1 shl LineBits) - 1
  29. ColMax = (1 shl ColBits) - 1
  30. static:
  31. assert AsideBit + FileBits + LineBits + ColBits == 32
  32. import .. / ic / [bitabs, rodfiles] # for LitId
  33. type
  34. PackedLineInfo* = distinct uint32
  35. LineInfoManager* = object
  36. aside: seq[(LitId, int32, int32)]
  37. const
  38. NoLineInfo* = PackedLineInfo(0'u32)
  39. proc pack*(m: var LineInfoManager; file: LitId; line, col: int32): PackedLineInfo =
  40. if file.uint32 <= FileMax.uint32 and line <= LineMax and col <= ColMax:
  41. let col = if col < 0'i32: 0'u32 else: col.uint32
  42. let line = if line < 0'i32: 0'u32 else: line.uint32
  43. # use inline representation:
  44. result = PackedLineInfo((file.uint32 shl 1'u32) or (line shl uint32(AsideBit + FileBits)) or
  45. (col shl uint32(AsideBit + FileBits + LineBits)))
  46. else:
  47. result = PackedLineInfo((m.aside.len shl 1) or AsideBit)
  48. m.aside.add (file, line, col)
  49. proc unpack*(m: LineInfoManager; i: PackedLineInfo): (LitId, int32, int32) =
  50. let i = i.uint32
  51. if (i and 1'u32) == 0'u32:
  52. # inline representation:
  53. result = (LitId((i shr 1'u32) and FileMax.uint32),
  54. int32((i shr uint32(AsideBit + FileBits)) and LineMax.uint32),
  55. int32((i shr uint32(AsideBit + FileBits + LineBits)) and ColMax.uint32))
  56. else:
  57. result = m.aside[int(i shr 1'u32)]
  58. proc getFileId*(m: LineInfoManager; i: PackedLineInfo): LitId =
  59. result = unpack(m, i)[0]
  60. proc store*(r: var RodFile; m: LineInfoManager) = storeSeq(r, m.aside)
  61. proc load*(r: var RodFile; m: var LineInfoManager) = loadSeq(r, m.aside)
  62. when isMainModule:
  63. var m = LineInfoManager(aside: @[])
  64. for i in 0'i32..<16388'i32:
  65. for col in 0'i32..<100'i32:
  66. let packed = pack(m, LitId(1023), i, col)
  67. let u = unpack(m, packed)
  68. assert u[0] == LitId(1023)
  69. assert u[1] == i
  70. assert u[2] == col
  71. echo m.aside.len