summary refs log tree commit diff stats
path: root/compiler/nir/nirfiles.nim
blob: cd5a79f06e444a4ea48c49177ca8a5eb291d1e9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#
#
#           The Nim Compiler
#        (c) Copyright 2023 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

import ".." / ic / [bitabs, rodfiles]
import nirinsts, nirtypes, nirlineinfos

type
  NirModule* = object
    code*: Tree
    man*: LineInfoManager
    types*: TypeGraph
    lit*: Literals
    namespace*: LitId
    intbits*: uint32
    symnames*: SymNames

proc load*(filename: string): NirModule =
  let lit = Literals()
  result = NirModule(lit: lit, types: initTypeGraph(lit))
  var r = rodfiles.open(filename)
  try:
    r.loadHeader(nirCookie)
    r.loadSection stringsSection
    r.load result.lit.strings

    r.loadSection numbersSection
    r.load result.lit.numbers

    r.loadSection bodiesSection
    r.load result.code

    r.loadSection typesSection
    r.load result.types

    r.loadSection sideChannelSection
    r.load result.man

    r.loadSection namespaceSection
    r.loadPrim result.namespace
    r.loadPrim result.intbits

    r.loadSection symnamesSection
    r.load result.symnames

  finally:
    r.close()

proc store*(m: NirModule; outp: string) =
  var r = rodfiles.create(outp)
  try:
    r.storeHeader(nirCookie)
    r.storeSection stringsSection
    r.store m.lit.strings

    r.storeSection numbersSection
    r.store m.lit.numbers

    r.storeSection bodiesSection
    r.store m.code

    r.storeSection typesSection
    r.store m.types

    r.storeSection sideChannelSection
    r.store m.man

    r.storeSection namespaceSection
    r.storePrim m.namespace
    r.storePrim m.intbits

    r.storeSection symnamesSection
    r.store m.symnames

  finally:
    r.close()
  if r.err != ok:
    raise newException(IOError, "could store into: " & outp)