File: makeguide.cpp

package info (click to toggle)
mona 1.4-7-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,996 kB
  • ctags: 3,939
  • sloc: ansic: 14,363; cpp: 12,610; sh: 1,076; yacc: 493; lex: 358; makefile: 150; lisp: 53
file content (377 lines) | stat: -rw-r--r-- 11,813 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * 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 "untyped.h"

extern "C" {
#include "../Mem/mem.h"
}

using std::cout;

extern SymbolTable symbolTable;
extern int numTypes;

Guide_Declaration *guide_declaration = 0;

// GTA GUIDE CONSTRUCTION

void
MonaUntypedAST::makeGTAGuide()
{
  IdentList *univs = symbolTable.allUnivs();

  if (numTypes > 0) { 
    // convert types to guide
    if (!univs || univs->empty()) {
      cout << "Error: Types declared but no universes (or trees)\n"
	   << "Execution aborted\n";
      exit(-1);
    }
    if (univs->size() == 1) {
      // boolean and universe state space must be different
      Name name = Name("<dummy>", dummyPos);
      symbolTable.insertUniv(&name, NULL, true); 
      delete univs;
      univs = symbolTable.allUnivs();
    }
    guide_declaration = new Guide_Declaration(new GuideFuncList, dummyPos);
    typedUnivs2guide(univs->size(), univs, 0, "");
  }

  if (guide_declaration) { // guide is declared
    /*#warning NOT REQUIRING UNIVERSES*/
    /*
    if (!univs || univs->empty()) {
      cout << "Error: Guide declared but no universes\n"
	   << "Execution aborted\n";
      exit(-1);
    }
    */

    // fill tables
    unsigned numUnivs = univs->size();
    char **univPos = (char **) mem_alloc(sizeof(char *)*numUnivs);
    char **univName = (char **) mem_alloc(sizeof(char *)*numUnivs);
    IdentList::iterator id;
    int i;
    for (id = univs->begin(), i = 0; id != univs->end(); id++, i++) {
      univName[i] = symbolTable.lookupSymbol(*id);
      univPos[i] = symbolTable.lookupPos(*id);
    }
    unsigned numSs = guide_declaration->funcList->size();
    SsId *muLeft = (SsId *) mem_alloc(sizeof(SsId)*numSs);
    SsId *muRight = (SsId *) mem_alloc(sizeof(SsId)*numSs);
    char **ssName = (char **) mem_alloc(sizeof(char *)*numSs);
    SsKind *ssKind = 0;
    int *ssType = 0;
    if (numTypes > 0) {
      ssKind = (SsKind *) mem_alloc(sizeof(SsKind)*numSs);
      ssType = (int *) mem_alloc(sizeof(int)*numSs);
    }
    GuideFuncList::iterator g;
    for (g = guide_declaration->funcList->begin(), i = 0;
	 g != guide_declaration->funcList->end(); g++, i++) {
      muLeft[i] = symbolTable.lookupNumber((*g)->name2);
      muRight[i] = symbolTable.lookupNumber((*g)->name3);
      ssName[i] = (*g)->name1->str;
      if (numTypes > 0) {
	switch((*g)->kind) {
	case SS_UNIVHAT:
	  ssKind[i] = gtaSSUNIVHAT;
	  break;
	case SS_ORHAT: 
	  ssKind[i] = gtaSSORHAT;
	  break;
	case SS_ORLEAF: 
	  ssKind[i] = gtaSSORLEAF;
	  break;
	case SS_AND: 
	  ssKind[i] = gtaSSAND;
	  break;
	case SS_DUMMY:
	  ssKind[i] = gtaSSDUMMY;
	  break;
	default: ;
	}
	Ident sstype = 
	  symbolTable.lookupSSType(symbolTable.lookupIdent((*g)->name1));
	if (sstype != -1) {
	  char *sstypename = symbolTable.lookupSymbol(sstype);
	  int j;
	  for (j = 0; j < numTypes; j++)
	    if (treetypes[j].name == sstypename) {
	      ssType[i] = j;
	      break;
	    }
	  invariant(j < numTypes);
	}
	else
	  ssType[i] = -1;
      }
    }
    
    makeGuide(numSs, muLeft, muRight, ssName, numUnivs, 
	      univPos, univName, ssType, ssKind);
    
    if (!checkDisjoint()) {
      cout << "Illegal guide and universe declarations:\n"
	   << "Universes must have disjoint state spaces\n"
	   << "Execution aborted\n";
      exit(-1);
    }
    if (!checkAllUsed()) {
      cout << "Illegal guide and universe declarations:\n"
	   << "All state spaces must be reachable\n"
	   << "Execution aborted\n";
      exit(-1);
    }
    if (!checkAllCovered()) {
      cout << "Illegal guide and universe declarations:\n"
	   << "Every infinite path in the guide must belong to a universe\n"
	   << "Execution aborted\n";
      exit(-1);
    }
  }

  else { // no guide declared, make default
    if (!univs || univs->empty()) {
      // make one universe
      Name name = Name("<univ>", dummyPos);
      symbolTable.insertUniv(&name, (char *) NULL); 
      delete univs;
      univs = symbolTable.allUnivs();
    }
      
    if (univs->size() == 1) {
      // boolean and universe state space must be different
      Name name = Name("<dummy>", dummyPos);
      symbolTable.insertUniv(&name, NULL, true); 
      delete univs;
      univs = symbolTable.allUnivs();
    }
      
    // fill name table
    unsigned numUnivs = univs->size();
    char **univName = (char **) mem_alloc(sizeof(char *)*numUnivs);
    Ident *id;
    int u;
    for (id = univs->begin(), u = 0; id != univs->end(); id++, u++)
      univName[u] = symbolTable.lookupSymbol(*id);
      
    makeDefaultGuide(numUnivs, univName);
  }
  delete univs;
}

//  RECURSIVE TYPES -> GUIDE

char*
MonaUntypedAST::typedUnivs2guide(unsigned num, IdentList *univs, unsigned idx, 
				 char *pos)
{
  invariant(num>0);
  char *ssname;
  if (num > 1) { // branch
    ssname = new char[strlen(pos)+4];
    sprintf(ssname, "ss-%s", pos); // ss-01101
    ssname = symbolTable.insertString(ssname);

    char *leftpos, *rightpos;
    GuideFunc *g = makeStateSpace(ssname, pos, &leftpos, &rightpos, 
				  SS_UNIVHAT);
    g->name2 = new Name(typedUnivs2guide((num+1)/2, univs, idx, 
					 leftpos), 
			dummyPos);
    g->name3 = new Name(typedUnivs2guide(num/2, univs, idx+(num+1)/2, 
					 rightpos), 
			dummyPos);
  }
  else { // make type
    Ident univId = univs->get(idx);
    char *univ = symbolTable.lookupSymbol(univId);
    symbolTable.updateUnivPos(univId, pos);
    if (strcmp(univ, "<dummy>") == 0)
      ssname = makeDummySS(univ);
    else {
      Ident typeId = symbolTable.lookupUnivType(univId);
      ASTVariantList *variants = symbolTable.lookupTypeVariants(typeId);
      IdentList typeSet(typeId);
      ssname = variants2guide(variants->size(), variants, 0, &typeSet, typeId,
			      univ, symbolTable.lookupSymbol(typeId), "");
      symbolTable.addTypeStatespace(typeId, ssname);
      Name n(ssname, dummyPos);
      symbolTable.setSSType(symbolTable.lookupIdent(&n), typeId);
    }
  }
  return ssname;
}

char*
MonaUntypedAST::variants2guide(unsigned num, ASTVariantList *variants, 
			       unsigned idx, IdentList *typeSet,
			       Ident typeId,
			       char *univ, char *type, char *pos)
{
  char *ssname;
  invariant(num>0);
  if (num > 1) { // branch
    ssname = new char[strlen(univ)+strlen(type)+strlen(pos)+6];
    sprintf(ssname, "ss-%s-%s-%s", univ, type, pos); // ss-U-A-01101
    ssname = symbolTable.insertString(ssname);

    char *leftpos, *rightpos;
    GuideFunc *g = makeStateSpace(ssname, pos, &leftpos, &rightpos, SS_ORHAT);
    g->name2 = new Name(variants2guide((num+1)/2, variants, idx, typeSet, 
				       typeId, univ, type, leftpos), 
			dummyPos);
    g->name3 = new Name(variants2guide(num/2, variants, idx+(num+1)/2, typeSet,
				       typeId, univ, type, rightpos), 
			dummyPos);
  }
  else { // make variant leaf and start component tree
    ASTVariant *v = variants->get(idx);
    if (!v->path) {
      v->path = new BitList(pos);
      setVariantPos(symbolTable.lookupTypeNumber(typeId), idx, pos);
      symbolTable.setTypeReachable(typeId);
    }

    ssname = new char[strlen(univ)+strlen(type)+strlen(v->name)+7];
    sprintf(ssname, "ss-%s-%s-%s-", univ, type, v->name);// ss-U-A-a1-
    ssname = symbolTable.insertString(ssname);

    char *leftpos, *rightpos;
    GuideFunc *g = makeStateSpace(ssname, "", &leftpos, &rightpos, SS_ORLEAF);
    unsigned size = v->components ? v->components->size() : 0;
    g->name2 = new Name(components2guide((size+1)/2, v->components, 0, 
					 typeSet, typeId, idx,
					 univ, type, v->name, leftpos), 
			dummyPos);
    g->name3 = new Name(components2guide(size/2, v->components, (size+1)/2, 
					 typeSet, typeId, idx,
					 univ, type, v->name, rightpos), 
			dummyPos);
  }
  return ssname;
}

char*
MonaUntypedAST::components2guide(unsigned num, ASTComponentList *components,
				 unsigned idx, IdentList *typeSet, 
				 Ident typeId, unsigned variantidx,
				 char *univ, char *type, 
				 char *variant, char *pos)
{
  char *ssname;
  if (num > 1) { // branch
    ssname = new char[strlen(univ)+strlen(type)+strlen(variant)+strlen(pos)+7];
    sprintf(ssname, "ss-%s-%s-%s-%s", univ, type, variant, pos);// ss-U-A-a1-01
    ssname = symbolTable.insertString(ssname);

    char *leftpos, *rightpos;
    GuideFunc *g = makeStateSpace(ssname, pos, &leftpos, &rightpos, SS_AND);
    g->name2 = new Name(components2guide((num+1)/2, components, idx, 
					 typeSet, typeId, variantidx, 
					 univ, type, variant, leftpos), 
			dummyPos);
    g->name3 = new Name(components2guide(num/2, components, idx+(num+1)/2, 
					 typeSet, typeId, variantidx, 
					 univ, type, variant, rightpos), 
			dummyPos);
  }
  else if (num == 1) { // make new or reuse type
    ASTComponent *c = components->get(idx);
    if (!c->path) {
      c->path = new BitList(pos);
      setComponentPos(symbolTable.lookupTypeNumber(typeId), variantidx, idx, 
		      pos);
    }
    Name t = Name(c->type, dummyPos);
    Ident typeId = symbolTable.lookupIdent(&t);
    if (symbolTable.lookupType(typeId) != Typename)
      TypeError("Type name expected", c->pos);

    ASTVariantList *variants = symbolTable.lookupTypeVariants(typeId);
    if (!typeSet->exists(typeId)) { // make new
      typeSet->insert(typeId);
      ssname = variants2guide(variants->size(), variants, 0, typeSet, typeId,
			      univ, c->type, "");
      symbolTable.addTypeStatespace(typeId, ssname);
      Name n(ssname, dummyPos);
      symbolTable.setSSType(symbolTable.lookupIdent(&n), typeId);
    }
    else { // type already in current universe
      if (variants->size() > 1) {
	ssname = new char[strlen(univ)+strlen(c->type)+6];
	sprintf(ssname, "ss-%s-%s-", univ, c->type); // ss-U-A-
      }
      else {
	ASTVariant *v = variants->get(0);
	ssname = new char[strlen(univ)+strlen(c->type)+strlen(v->name)+7];
	sprintf(ssname, "ss-%s-%s-%s-", univ, c->type, v->name);// ss-U-A-a1-
      }
      ssname = symbolTable.insertString(ssname);
    }
  } 
  else  // need dummy
    ssname = makeDummySS(univ);
  return ssname;
}

GuideFunc*
MonaUntypedAST::makeStateSpace(char *ssname, char *pos,
			       char **leftpos, char **rightpos,
			       SSKind kind)
{
  Name n = Name(ssname, dummyPos);
  symbolTable.insertStatespace(&n);
  
  *leftpos = new char[strlen(pos)+2];
  sprintf(*leftpos, "%s0", pos);
  *leftpos = symbolTable.insertString(*leftpos);
  *rightpos = new char[strlen(pos)+2];
  sprintf(*rightpos, "%s1", pos);
  *rightpos = symbolTable.insertString(*rightpos);
  
  GuideFunc *g = new GuideFunc(new Name(ssname, dummyPos), NULL, NULL, kind);
  guide_declaration->funcList->push_back(g);
  return g;
}

char*
MonaUntypedAST::makeDummySS(char *univ)
{
  char *ssname = new char[strlen(univ)+7];
  sprintf(ssname, "dummy-%s", univ);
  ssname = symbolTable.insertString(ssname);

  if (!symbolTable.exists(ssname)) {
    Name n = Name(ssname, dummyPos);
    symbolTable.insertStatespace(&n);
    guide_declaration->funcList->push_back
      (new GuideFunc(new Name(ssname, dummyPos),
		     new Name(ssname, dummyPos),
		     new Name(ssname, dummyPos),
		     SS_DUMMY));
  }

  return ssname;
}