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

# This module implements a dependency file generator.

import 
  os, options, ast, astalgo, msgs, ropes, idents, passes, importer

proc genDependPass*(): TPass
proc generateDot*(project: string)

type 
  TGen = object of TPassContext
    module*: PSym
    filename*: string
  PGen = ref TGen

var gDotGraph: PRope # the generated DOT file; we need a global variable

proc addDependencyAux(importing, imported: string) = 
  appf(gDotGraph, "$1 -> $2;$n", [toRope(importing), toRope(imported)]) 
  # s1 -> s2_4[label="[0-9]"];
  
proc addDotDependency(c: PPassContext, n: PNode): PNode = 
  result = n
  var g = PGen(c)
  case n.kind
  of nkImportStmt: 
    for i in countup(0, sonsLen(n) - 1): 
      var imported = getModuleName(n.sons[i])
      addDependencyAux(g.module.name.s, imported)
  of nkFromStmt, nkImportExceptStmt: 
    var imported = getModuleName(n.sons[0])
    addDependencyAux(g.module.name.s, imported)
  of nkStmtList, nkBlockStmt, nkStmtListExpr, nkBlockExpr: 
    for i in countup(0, sonsLen(n) - 1): discard addDotDependency(c, n.sons[i])
  else: 
    nil

proc generateDot(project: string) = 
  writeRope(ropef("digraph $1 {$n$2}$n", [
      toRope(changeFileExt(extractFileName(project), "")), gDotGraph]), 
            changeFileExt(project, "dot"))

proc myOpen(module: PSym, filename: string): PPassContext = 
  var g: PGen
  new(g)
  g.module = module
  g.filename = filename
  result = g

proc gendependPass(): TPass = 
  initPass(result)
  result.open = myOpen
  result.process = addDotDependency
w"> not(One shl elem.modElemSize) proc bitSetInit(b: var TBitSet, length: int) = newSeq(b, length) proc bitSetUnion(x: var TBitSet, y: TBitSet) = for i in 0 .. high(x): x[i] = x[i] or y[i] proc bitSetDiff(x: var TBitSet, y: TBitSet) = for i in 0 .. high(x): x[i] = x[i] and not y[i] proc bitSetSymDiff(x: var TBitSet, y: TBitSet) = for i in 0 .. high(x): x[i] = x[i] xor y[i] proc bitSetIntersect(x: var TBitSet, y: TBitSet) = for i in 0 .. high(x): x[i] = x[i] and y[i] proc bitSetEquals(x, y: TBitSet): bool = for i in 0 .. high(x): if x[i] != y[i]: return false result = true proc bitSetContains(x, y: TBitSet): bool = for i in 0 .. high(x): if (x[i] and not y[i]) != Zero: return false result = true # Number of set bits for all values of int8 const populationCount: array[uint8, uint8] = block: var arr: array[uint8, uint8] proc countSetBits(x: uint8): uint8 = return ( x and 0b00000001'u8) + ((x and 0b00000010'u8) shr 1) + ((x and 0b00000100'u8) shr 2) + ((x and 0b00001000'u8) shr 3) + ((x and 0b00010000'u8) shr 4) + ((x and 0b00100000'u8) shr 5) + ((x and 0b01000000'u8) shr 6) + ((x and 0b10000000'u8) shr 7) for it in low(uint8)..high(uint8): arr[it] = countSetBits(cast[uint8](it)) arr proc bitSetCard(x: TBitSet): BiggestInt = for it in x: result.inc int(populationCount[it])