summary refs log tree commit diff stats
path: root/compiler/debuginfo.nim
blob: e408a932b364da6e4257077107c52e7079e062e9 (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
#
#
#           The Nim Compiler
#        (c) Copyright 2017 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## The compiler can generate debuginfo to help debuggers in translating back
## from C/C++/JS code to Nim. The data structure has been designed to produce
## something useful with Nim's marshal module.

import sighashes

type
  FilenameMapping* = object
    package*, file*: string
    mangled*: SigHash
  EnumDesc* = object
    size*: int
    owner*: SigHash
    id*: int
    name*: string
    values*: seq[(string, int)]
  DebugInfo* = object
    version*: int
    files*: seq[FilenameMapping]
    enums*: seq[EnumDesc]
    conflicts*: bool

proc sdbmHash(package, file: string): SigHash =
  result = 0
  for i in 0..<package.len:
    result &= package[i]
  result &= '.'
  for i in 0..<file.len:
    result &= file[i]

proc register*(self: var DebugInfo; package, file: string): SigHash =
  result = sdbmHash(package, file)
  for f in self.files:
    if f.mangled == result:
      if f.package == package and f.file == file: return
      self.conflicts = true
      break
  self.files.add(FilenameMapping(package: package, file: file, mangled: result))

proc hasEnum*(self: DebugInfo; ename: string; id: int; owner: SigHash): bool =
  for en in self.enums:
    if en.owner == owner and en.name == ename and en.id == id: return true

proc registerEnum*(self: var DebugInfo; ed: EnumDesc) =
  self.enums.add ed

proc init*(self: var DebugInfo) =
  self.version = 1
  self.files = @[]
  self.enums = @[]

var gDebugInfo*: DebugInfo
debuginfo.init gDebugInfo

import marshal, streams

proc writeDebugInfo*(self: var DebugInfo; file: string) =
  let s = newFileStream(file, fmWrite)
  store(s, self)
  s.close

proc writeDebugInfo*(file: string) = writeDebugInfo(gDebugInfo, file)

proc loadDebugInfo*(self: var DebugInfo; file: string) =
  let s = newFileStream(file, fmRead)
  load(s, self)
  s.close

proc loadDebugInfo*(file: string) = loadDebugInfo(gDebugInfo, file)
pan class="n">s.a0 s1 = s1 xor s.a1 discard next(s) s.a0 = s0 s.a1 = s1 proc random*(max: int): int {.benign, deprecated.} = ## Returns a random number in the range 0..max-1. The sequence of ## random number is always the same, unless `randomize` is called ## which initializes the random number generator with a "random" ## number, i.e. a tickcount. **Deprecated since version 0.18.0**. ## Use ``rand`` instead. while true: let x = next(state) if x < randMax - (randMax mod ui(max)): return int(x mod uint64(max)) proc random*(max: float): float {.benign, deprecated.} = ## Returns a random number in the range 0..<max. The sequence of ## random number is always the same, unless `randomize` is called ## which initializes the random number generator with a "random" ## number, i.e. a tickcount. **Deprecated since version 0.18.0**. ## Use ``rand`` instead. let x = next(state) when defined(JS): result = (float(x) / float(high(uint32))) * max else: let u = (0x3FFu64 shl 52u64) or (x shr 12u64) result = (cast[float](u) - 1.0) * max proc random*[T](x: HSlice[T, T]): T {.deprecated.} = ## For a slice `a .. b` returns a value in the range `a .. b-1`. ## **Deprecated since version 0.18.0**. ## Use ``rand`` instead. result = T(random(x.b - x.a)) + x.a proc random*[T](a: openArray[T]): T {.deprecated.} = ## returns a random element from the openarray `a`. ## **Deprecated since version 0.18.0**. ## Use ``rand`` instead. result = a[random(a.low..a.len)] proc rand*(r: var Rand; max: int): int {.benign.} = ## Returns a random number in the range 0..max. The sequence of ## random number is always the same, unless `randomize` is called ## which initializes the random number generator with a "random" ## number, i.e. a tickcount. while true: let x = next(r) if x <= randMax - (randMax mod ui(max)): return int(x mod (uint64(max)+1u64)) proc rand*(max: int): int {.benign.} = ## Returns a random number in the range 0..max. The sequence of ## random number is always the same, unless `randomize` is called ## which initializes the random number generator with a "random" ## number, i.e. a tickcount. rand(state, max) proc rand*(r: var Rand; max: float): float {.benign.} = ## Returns a random number in the range 0..max. The sequence of ## random number is always the same, unless `randomize` is called ## which initializes the random number generator with a "random" ## number, i.e. a tickcount. let x = next(r) when defined(JS): result = (float(x) / float(high(uint32))) * max else: let u = (0x3FFu64 shl 52u64) or (x shr 12u64) result = (cast[float](u) - 1.0) * max proc rand*(max: float): float {.benign.} = ## Returns a random number in the range 0..max. The sequence of ## random number is always the same, unless `randomize` is called ## which initializes the random number generator with a "random" ## number, i.e. a tickcount. rand(state, max) proc rand*[T](r: var Rand; x: HSlice[T, T]): T = ## For a slice `a .. b` returns a value in the range `a .. b`. result = T(rand(r, x.b - x.a)) + x.a proc rand*[T](x: HSlice[T, T]): T = ## For a slice `a .. b` returns a value in the range `a .. b`. result = rand(state, x) proc rand*[T](r: var Rand; a: openArray[T]): T = ## returns a random element from the openarray `a`. result = a[rand(r, a.low..a.high)] proc rand*[T](a: openArray[T]): T = ## returns a random element from the openarray `a`. result = a[rand(a.low..a.high)] proc initRand*(seed: int64): Rand = ## Creates a new ``Rand`` state from ``seed``. result.a0 = ui(seed shr 16) result.a1 = ui(seed and 0xffff) discard next(result) proc randomize*(seed: int64) {.benign.} = ## Initializes the default random number generator ## with a specific seed. state = initRand(seed) proc shuffle*[T](r: var Rand; x: var openArray[T]) = ## Swaps the positions of elements in a sequence randomly. for i in countdown(x.high, 1): let j = r.rand(i) swap(x[i], x[j]) proc shuffle*[T](x: var openArray[T]) = ## Swaps the positions of elements in a sequence randomly. shuffle(state, x) when not defined(nimscript): import times proc randomize*() {.benign.} = ## Initializes the random number generator with a "random" ## number, i.e. a tickcount. Note: Does not work for NimScript. let time = int64(times.epochTime() * 1_000_000_000) randomize(time) {.pop.} when isMainModule: proc main = var occur: array[1000, int] var x = 8234 for i in 0..100_000: x = rand(high(occur)) inc occur[x] for i, oc in occur: if oc < 69: doAssert false, "too few occurrences of " & $i elif oc > 150: doAssert false, "too many occurrences of " & $i var a = [0, 1] shuffle(a) doAssert a[0] == 1 doAssert a[1] == 0 main()