File: ident.cpp

package info (click to toggle)
mona 1.4-7-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,976 kB
  • ctags: 3,713
  • sloc: ansic: 14,363; cpp: 12,610; sh: 1,076; yacc: 493; lex: 358; makefile: 154; lisp: 53
file content (306 lines) | stat: -rw-r--r-- 5,851 bytes parent folder | download | duplicates (2)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * MONA
 * Copyright (C) 1997-2004 BRICS.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the  Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 * USA.
 */

#include <iostream>
#include "ident.h"
#include "offsets.h"
#include "symboltable.h"

using std::cout;

extern SymbolTable symbolTable;
extern Offsets offsets;

int 
varcmp(const void *i1, const void *i2)
{
  if (*((Ident *) i1) < *((Ident *) i2))
    return -1;
  else if (*((Ident *) i1) > *((Ident *) i2))
    return 1;
  else
    return 0;
}

int 
varcmp_offsets(const void *i1, const void *i2)
{
  if (offsets.off(*((Ident *) i1)) < offsets.off(*((Ident *) i2)))
    return -1;
  else if (offsets.off(*((Ident *) i1)) > offsets.off(*((Ident *) i2)))
    return 1;
  else
    return 0;
}

void 
IdentList::insert(Ident id)
{
  for (iterator i = begin(); i != end(); i++)
    if (*i == id)
      return;
  push_back(id);
}

void 
IdentList::insert(IdentList *d)
{
  for (iterator i = d->begin(); i != d->end(); i++)
    insert(*i);
}

void
IdentList::remove(Ident id)
{
  for (iterator i = begin(); i != end(); i++)
    if (*i == id) {
      Ident f = pop_front();
      if (id != f)
	set(i, f);
      return;
    }
}

void 
IdentList::sort()
{
  Deque<Ident>::sort(varcmp);
}

void 
IdentList::dump()
{
  for (iterator i = begin(); i != end();) {
    cout << symbolTable.lookupSymbol(*i);
    if (++i != end())
      cout << ",";
  }
}

void 
IdentList::dumplist()
{
  cout << "[";
  iterator i;
  for (i = begin(); i != end(); i++) {
    if (i != begin())
      cout << ",";
    cout << *i;
  }
  cout << "]";
}

unsigned
IdentList::hash()
{
  unsigned h = 1;
  for (iterator i = begin(); i != end(); i++) 
    h = (h << 1) + *i;
  return h;
}

void
IdentList::compress()
{ // sort (by offsets!) and remove duplicates
  Deque<Ident>::sort(varcmp_offsets);
  unsigned n = 0;
  for (iterator i = begin(); i != end(); i++)
    if (i+1 == end() || *i != *(i+1))
      set(n++, *i);
  while (n < size())
    pop_back();
}

IdentList *
IdentList::copy()
{
  return (IdentList *) Deque<Ident>::copy();
}

bool
equal(IdentList *l1, IdentList *l2)
{
  if (l1 == NULL && l2 == NULL)
    return true;

  if (l1 == NULL || l2 == NULL)
    return false;

  if (l1->size() != l2->size())
    return false;

  for (IdentList::iterator i = l1->begin(), j = l2->begin();
       i != l1->end(); i++, j++)
    if (*i != *j)
      return false;

  return true;
}

IdentList*
ident_union(IdentList *i1, IdentList *i2) 
  // merge sorted identlists into union
{
  if ((!i1 || i1->empty()) && (!i2 || i2->empty()))
    return NULL;
  if (!i1)
    return new IdentList(*i2);
  if (!i2)
    return new IdentList(*i1);

  IdentList *r = new IdentList;

  Ident *id1 = i1->begin();
  Ident *id2 = i2->begin();
  
  while (id1 != i1->end() && id2 != i2->end()) {
    if (*id1 < *id2) {
      r->push_back(*id1);
      id1++;
    }
    else if (*id1 > *id2) {
      r->push_back(*id2);
      id2++;
    }
    else {
      r->push_back(*id1);
      id1++;
      id2++;
    }
  }
  while (id1 != i1->end()) {
    r->push_back(*id1);
    id1++;
  }
  while (id2 != i2->end()) {
    r->push_back(*id2);
    id2++;
  }

  return r;
}

IdentList*
intersection(IdentList *i1, IdentList *i2)
  // make sorted intersection of sorted identlists
{
  if (!i1 || !i2)
    return NULL;

  IdentList *r = new IdentList;

  Ident *id1 = i1->begin();
  Ident *id2 = i2->begin();
  
  while (id1 != i1->end() && id2 != i2->end()) {
    if (*id1 < *id2) 
      id1++;
    else if (*id1 > *id2) 
      id2++;
    else {
      r->push_back(*id1);
      id1++;
      id2++;
    }
  }

  if (r->empty()) {
    delete r;
    r = NULL;
  }
  return r;
}

IdentList*
copy(IdentList *l)
{
  if (l)
    return new IdentList(*l);
  else
    return NULL;
}

Ident
subst(Ident id, IdentList *i1, IdentList *i2)
{
  invariant(i1 && i2 && i1->size() == i2->size());

  IdentList::iterator p1, p2;
  for (p1 = i1->begin(), p2 = i2->begin(); p1!=i1->end(); p1++, p2++)
    if (*p1==id)
      return *p2;

  /*
  unsigned a = 0, b, c = i1->size();
  while (a < c) {
    b = (c+a)/2;
    Ident t = i1->get(b);
    if (offsets.off(t) > offsets.off(id))
      c = b;
    else if (offsets.off(t) < offsets.off(id))
      a = b+1;
    else
      return i2->get(b);
  }
  */

  return id;
}

Ident
subst(Ident id1, Ident id2, Ident id3)
{
  if (id1 == id2)
    return id3;
  return id1;
}

IdentList *
subst(IdentList *i1, IdentList *i2, IdentList *i3, Ident except)
{ // map i1 using i2->i3 (but leave 'except' unchanged)
  invariant(i1 && i2 && i3 && i2->size() == i3->size());

  IdentList *res = new IdentList;
  for (IdentList::iterator j1 = i1->begin(); j1 != i1->end(); j1++) 
    if (*j1 == except)
      res->push_back(*j1);
    else
      res->push_back(subst(*j1, i2, i3));
  return res;
}

bool
sameUnivs(Ident id1, Ident id2) // test id1's universes same as id2's
{
  return equal(symbolTable.lookupUnivs(id1),
               symbolTable.lookupUnivs(id2));
}

bool
sameListUnivs(IdentList *i1, IdentList *i2)
{
  invariant(i1 && i2 && i1->size() == i2->size());
  
  IdentList::iterator j1,j2;
  for (j1 = i1->begin(), j2 = i2->begin(); j1 != i1->end(); j1++, j2++)
    if (!sameUnivs(*j1, *j2))
      return false;
  return true;
}