tcycle3.nim 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. discard """
  2. output: '''BEGIN
  3. END
  4. END 2
  5. cpu.nes false
  6. cpu step nes is nil? - false
  7. 0'''
  8. cmd: '''nim c --gc:orc $file'''
  9. """
  10. # extracted from thavlak.nim
  11. type
  12. BasicBlock = ref object
  13. inEdges: seq[BasicBlock]
  14. outEdges: seq[BasicBlock]
  15. name: int
  16. proc newBasicBlock(name: int): BasicBlock =
  17. result = BasicBlock(
  18. inEdges: newSeq[BasicBlock](),
  19. outEdges: newSeq[BasicBlock](),
  20. name: name
  21. )
  22. type
  23. Cfg = object
  24. basicBlockMap: seq[BasicBlock]
  25. startNode: BasicBlock
  26. proc newCfg(): Cfg =
  27. result = Cfg(
  28. basicBlockMap: newSeq[BasicBlock](),
  29. startNode: nil)
  30. proc createNode(cfg: var Cfg, name: int): BasicBlock =
  31. if name < cfg.basicBlockMap.len:
  32. result = cfg.basicBlockMap[name]
  33. else:
  34. result = newBasicBlock(name)
  35. cfg.basicBlockMap.setLen name+1
  36. cfg.basicBlockMap[name] = result
  37. proc newBasicBlockEdge(cfg: var Cfg, fromName, toName: int) =
  38. echo "BEGIN"
  39. let fr = cfg.createNode(fromName)
  40. let to = cfg.createNode(toName)
  41. fr.outEdges.add(to)
  42. to.inEdges.add(fr)
  43. proc run(cfg: var Cfg) =
  44. cfg.startNode = cfg.createNode(0) # RC = 2
  45. newBasicBlockEdge(cfg, 0, 1) #
  46. echo "END"
  47. discard cfg.createNode(1)
  48. proc main =
  49. var c = newCfg()
  50. c.run
  51. echo "END 2"
  52. # bug #14159
  53. type
  54. NES = ref object
  55. cpu: CPU
  56. apu: APU
  57. CPU = ref object
  58. nes: NES
  59. APU = object
  60. nes: NES
  61. cpu: CPU
  62. proc initAPU(nes: sink NES): APU {.nosinks.} =
  63. result.nes = nes
  64. result.cpu = nes.cpu
  65. proc step(cpu: CPU): int =
  66. echo "cpu.nes ", cpu.isNil
  67. echo "cpu step nes is nil? - ", cpu.nes.isNil()
  68. proc newNES(): NES =
  69. new result
  70. result.cpu = CPU(nes: result)
  71. result.apu = initAPU(result)
  72. proc bug14159 =
  73. var nesConsole = newNES()
  74. discard nesConsole.cpu.step()
  75. let mem = getOccupiedMem()
  76. main()
  77. bug14159()
  78. GC_fullCollect()
  79. echo getOccupiedMem() - mem