nirfiles.nim 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2023 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. import ".." / ic / [bitabs, rodfiles]
  10. import nirinsts, nirtypes, nirlineinfos
  11. type
  12. NirModule* = object
  13. code*: Tree
  14. man*: LineInfoManager
  15. types*: TypeGraph
  16. lit*: Literals
  17. namespace*: LitId
  18. intbits*: uint32
  19. symnames*: SymNames
  20. proc load*(filename: string): NirModule =
  21. let lit = Literals()
  22. result = NirModule(lit: lit, types: initTypeGraph(lit))
  23. var r = rodfiles.open(filename)
  24. try:
  25. r.loadHeader(nirCookie)
  26. r.loadSection stringsSection
  27. r.load result.lit.strings
  28. r.loadSection numbersSection
  29. r.load result.lit.numbers
  30. r.loadSection bodiesSection
  31. r.load result.code
  32. r.loadSection typesSection
  33. r.load result.types
  34. r.loadSection sideChannelSection
  35. r.load result.man
  36. r.loadSection namespaceSection
  37. r.loadPrim result.namespace
  38. r.loadPrim result.intbits
  39. r.loadSection symnamesSection
  40. r.load result.symnames
  41. finally:
  42. r.close()
  43. proc store*(m: NirModule; outp: string) =
  44. var r = rodfiles.create(outp)
  45. try:
  46. r.storeHeader(nirCookie)
  47. r.storeSection stringsSection
  48. r.store m.lit.strings
  49. r.storeSection numbersSection
  50. r.store m.lit.numbers
  51. r.storeSection bodiesSection
  52. r.store m.code
  53. r.storeSection typesSection
  54. r.store m.types
  55. r.storeSection sideChannelSection
  56. r.store m.man
  57. r.storeSection namespaceSection
  58. r.storePrim m.namespace
  59. r.storePrim m.intbits
  60. r.storeSection symnamesSection
  61. r.store m.symnames
  62. finally:
  63. r.close()
  64. if r.err != ok:
  65. raise newException(IOError, "could store into: " & outp)