summary refs log tree commit diff stats
path: root/tests/destructor/tcycle3.nim
blob: 8662136e7063a50055279cf1899c5d76b9e8ba34 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
discard """
  output: '''BEGIN
END
END 2
cpu.nes false
cpu step nes is nil? - false
0'''
  cmd: '''nim c --gc:orc $file'''
"""

# extracted from thavlak.nim

type
  BasicBlock = ref object
    inEdges: seq[BasicBlock]
    outEdges: seq[BasicBlock]
    name: int

proc newBasicBlock(name: int): BasicBlock =
  result = BasicBlock(
    inEdges: newSeq[BasicBlock](),
    outEdges: newSeq[BasicBlock](),
    name: name
  )

type
  Cfg = object
    basicBlockMap: seq[BasicBlock]
    startNode: BasicBlock

proc newCfg(): Cfg =
  result = Cfg(
    basicBlockMap: newSeq[BasicBlock](),
    startNode: nil)

proc createNode(cfg: var Cfg, name: int): BasicBlock =
  if name < cfg.basicBlockMap.len:
    result = cfg.basicBlockMap[name]
  else:
    result = newBasicBlock(name)
    cfg.basicBlockMap.setLen name+1
    cfg.basicBlockMap[name] = result

proc newBasicBlockEdge(cfg: var Cfg, fromName, toName: int) =
  echo "BEGIN"
  let fr = cfg.createNode(fromName)
  let to = cfg.createNode(toName)

  fr.outEdges.add(to)
  to.inEdges.add(fr)

proc run(cfg: var Cfg) =
  cfg.startNode = cfg.createNode(0) # RC = 2
  newBasicBlockEdge(cfg, 0, 1) #
  echo "END"

  discard cfg.createNode(1)

proc main =
  var c = newCfg()
  c.run
  echo "END 2"

# bug #14159
type
  NES = ref object
    cpu: CPU
    apu: APU

  CPU = ref object
    nes: NES

  APU = object
    nes: NES
    cpu: CPU

proc initAPU(nes: sink NES): APU {.nosinks.} =
  result.nes = nes
  result.cpu = nes.cpu

proc step(cpu: CPU): int =
  echo "cpu.nes ", cpu.isNil
  echo "cpu step nes is nil? - ", cpu.nes.isNil()

proc newNES(): NES =
  new result
  result.cpu = CPU(nes: result)
  result.apu = initAPU(result)

proc bug14159 =
  var nesConsole = newNES()
  discard nesConsole.cpu.step()

let mem = getOccupiedMem()
main()
bug14159()
GC_fullCollect()
echo getOccupiedMem() - mem