summary refs log tree commit diff stats
path: root/nim/treetab.pas
blob: 31d7aa0cff3dce25adbb923cead806fe78230ac7 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
//
//           The Nimrod Compiler
//        (c) Copyright 2008 Andreas Rumpf
//
//    See the file "copying.txt", included in this
//    distribution, for details about the copyright.
//
unit treetab;

// Implements a table from trees to trees. Does structural equavilent checking.

interface

{$include 'config.inc'}

uses
  nsystem, nhashes, ast, astalgo, types;

function NodeTableGet(const t: TNodeTable; key: PNode): int;
procedure NodeTablePut(var t: TNodeTable; key: PNode; val: int);

function NodeTableTestOrSet(var t: TNodeTable; key: PNode; val: int): int;

implementation

function hashTree(n: PNode): THash;
var
  i: int;
begin
  result := 0;
  if n = nil then exit;
  result := ord(n.kind);
  case n.kind of
    nkEmpty, nkNilLit, nkType: begin end;
    nkIdent: result := concHash(result, n.ident.h);
    nkSym: result := concHash(result, n.sym.name.h);
    nkCharLit..nkInt64Lit: begin
      if (n.intVal >= low(int)) and (n.intVal <= high(int)) then
        result := concHash(result, int(n.intVal));
    end;
    nkFloatLit..nkFloat64Lit: begin
      if (n.floatVal >= -1000000.0) and (n.floatVal <= 1000000.0) then
        result := concHash(result, toInt(n.floatVal));
    end;
    nkStrLit..nkTripleStrLit:
      result := concHash(result, GetHashStr(n.strVal));
    else begin
      for i := 0 to sonsLen(n)-1 do
        result := concHash(result, hashTree(n.sons[i]));
    end
  end
end;

function TreesEquivalent(a, b: PNode): Boolean;
var
  i: int;
begin
  result := false;
  if a = b then begin
    result := true
  end
  else if (a <> nil) and (b <> nil) and (a.kind = b.kind) then begin
    case a.kind of
      nkEmpty, nkNilLit, nkType: result := true;
      nkSym:
        result := a.sym.id = b.sym.id;
      nkIdent:
        result := a.ident.id = b.ident.id;
      nkCharLit..nkInt64Lit:
        result := a.intVal = b.intVal;
      nkFloatLit..nkFloat64Lit:
        result := a.floatVal = b.floatVal;
      nkStrLit..nkTripleStrLit:
        result := a.strVal = b.strVal;
      else if sonsLen(a) = sonsLen(b) then begin
        for i := 0 to sonsLen(a)-1 do
          if not TreesEquivalent(a.sons[i], b.sons[i]) then exit;
        result := true
      end
    end;
    if result then result := sameTypeOrNil(a.typ, b.typ);
  end
end;

function NodeTableRawGet(const t: TNodeTable; k: THash; key: PNode): int;
var
  h: THash;
begin
  h := k and high(t.data);
  while t.data[h].key <> nil do begin
    if (t.data[h].h = k) and TreesEquivalent(t.data[h].key, key) then begin
      result := h; exit
    end;
    h := nextTry(h, high(t.data))
  end;
  result := -1
end;

function NodeTableGet(const t: TNodeTable; key: PNode): int;
var
  index: int;
begin
  index := NodeTableRawGet(t, hashTree(key), key);
  if index >= 0 then result := t.data[index].val
  else result := low(int)
end;

procedure NodeTableRawInsert(var data: TNodePairSeq; k: THash;
                             key: PNode; val: int);
var
  h: THash;
begin
  h := k and high(data);
  while data[h].key <> nil do h := nextTry(h, high(data));
  assert(data[h].key = nil);
  data[h].h := k;
  data[h].key := key;
  data[h].val := val;
end;

procedure NodeTablePut(var t: TNodeTable; key: PNode; val: int);
var
  index, i: int;
  n: TNodePairSeq;
  k: THash;
begin
  k := hashTree(key);
  index := NodeTableRawGet(t, k, key);
  if index >= 0 then begin
    assert(t.data[index].key <> nil);
    t.data[index].val := val
  end
  else begin
    if mustRehash(length(t.data), t.counter) then begin
    {@ignore}
      setLength(n, length(t.data) * growthFactor);
      fillChar(n[0], length(n)*sizeof(n[0]), 0);
    {@emit
      newSeq(n, length(t.data) * growthFactor); }
      for i := 0 to high(t.data) do
        if t.data[i].key <> nil then
          NodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val);
    {@ignore}
      t.data := n;
    {@emit
      swap(t.data, n);
    }
    end;
    NodeTableRawInsert(t.data, k, key, val);
    inc(t.counter)
  end;
end;

function NodeTableTestOrSet(var t: TNodeTable; key: PNode; val: int): int;
var
  index, i: int;
  n: TNodePairSeq;
  k: THash;
begin
  k := hashTree(key);
  index := NodeTableRawGet(t, k, key);
  if index >= 0 then begin
    assert(t.data[index].key <> nil);
    result := t.data[index].val
  end
  else begin
    if mustRehash(length(t.data), t.counter) then begin
    {@ignore}
      setLength(n, length(t.data) * growthFactor);
      fillChar(n[0], length(n)*sizeof(n[0]), 0);
    {@emit
      newSeq(n, length(t.data) * growthFactor); }
      for i := 0 to high(t.data) do
        if t.data[i].key <> nil then
          NodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val);
    {@ignore}
      t.data := n;
    {@emit
      swap(t.data, n);
    }
    end;
    NodeTableRawInsert(t.data, k, key, val);
    result := val;
    inc(t.counter)
  end;
end;

end.