1 /*-----------------------------------------------------------------------
2
3 File : cte_termvars.c
4
5 Author: Stephan Schulz
6
7 Contents
8
9 Functions for the management of shared variables.
10
11 Copyright 1998, 1999 by the author.
12 This code is released under the GNU General Public Licence and
13 the GNU Lesser General Public License.
14 See the file COPYING in the main E directory for details..
15 Run "eprover -h" for contact information.
16
17 Changes
18
19 <1> Wed Feb 25 00:48:17 MET 1998
20 new
21
22 -----------------------------------------------------------------------*/
23
24 #include "cte_termvars.h"
25
26
27
28 /*---------------------------------------------------------------------*/
29 /* Global Variables */
30 /*---------------------------------------------------------------------*/
31
32
33 /*---------------------------------------------------------------------*/
34 /* Forward Declarations */
35 /*---------------------------------------------------------------------*/
36
37
38 /*---------------------------------------------------------------------*/
39 /* Internal Functions */
40 /*---------------------------------------------------------------------*/
41
42
43 /*-----------------------------------------------------------------------
44 //
45 // Function: var_named_new
46 // create a new VarBankNamed_p
47 //
48 //
49 // Global Variables: -
50 //
51 // Side Effects : memory
52 //
53 /----------------------------------------------------------------------*/
var_named_new(Term_p var,char * name)54 VarBankNamed_p var_named_new(Term_p var, char* name)
55 {
56 VarBankNamed_p res;
57
58 res = VarBankNamedCellAlloc();
59 res->name = SecureStrdup(name);
60 res->var = var;
61
62 return res;
63 }
64
65 /*-----------------------------------------------------------------------
66 //
67 // Function: var_named_free
68 // free a VarBankNamed_p
69 //
70 //
71 // Global Variables: -
72 //
73 // Side Effects : memory
74 //
75 /----------------------------------------------------------------------*/
var_named_free(VarBankNamed_p junk)76 void var_named_free(VarBankNamed_p junk)
77 {
78 FREE(junk->name);
79 VarBankNamedCellFree(junk);
80 }
81
82
83 /*-----------------------------------------------------------------------
84 //
85 // Function: clear_env_stack
86 // clear the env stack, removing all named cells
87 //
88 //
89 // Global Variables: -
90 //
91 // Side Effects : Memory operations
92 //
93 /----------------------------------------------------------------------*/
clear_env_stack(VarBank_p bank)94 void clear_env_stack(VarBank_p bank)
95 {
96 VarBankNamed_p named;
97
98 while(!PStackEmpty(bank->env))
99 {
100 named = PStackPopP(bank->env);
101 if(named)
102 {
103 var_named_free(named);
104 }
105 }
106 }
107
108 /*---------------------------------------------------------------------*/
109 /* Exported Functions */
110 /*---------------------------------------------------------------------*/
111
112 /*-----------------------------------------------------------------------
113 //
114 // Function: VarBankAlloc()
115 //
116 // Allocate an empty, initialized VarBank-Structure, return pointer
117 // to it.
118 //
119 // Global Variables: -
120 //
121 // Side Effects : Memory operations
122 //
123 /----------------------------------------------------------------------*/
124
VarBankAlloc(SortTable_p sort_table)125 VarBank_p VarBankAlloc(SortTable_p sort_table)
126 {
127 VarBank_p handle;
128
129 handle = VarBankCellAlloc();
130 handle->v_count = 0;
131 handle->sort_table = sort_table;
132 handle->max_var = 0;
133 handle->stacks = PDArrayAlloc(INITIAL_SORT_STACK_SIZE, 5);
134 handle->ext_index = NULL;
135 handle->env = PStackAlloc();
136 return handle;
137 }
138
139 /*-----------------------------------------------------------------------
140 //
141 // Function: VarBankFree()
142 //
143 // Deallocate a VarBankCell.
144 //
145 // Global Variables: -
146 //
147 // Side Effects : Memory operations
148 //
149 /----------------------------------------------------------------------*/
150
VarBankFree(VarBank_p junk)151 void VarBankFree(VarBank_p junk)
152 {
153 int i, j;
154 VarBankStack_p stack;
155
156 assert(junk);
157 StrTreeFree(junk->ext_index);
158 PStackFree(junk->env);
159
160 for(i=0; i<PDArraySize(junk->stacks); i++)
161 {
162 stack = (VarBankStack_p) PDArrayElementP(junk->stacks, i);
163 if (! stack)
164 {
165 continue;
166 }
167
168 // free stack
169 for(j=0; j<PDArraySize(stack); j++)
170 {
171 if(PDArrayElementP(stack, j))
172 {
173 TermTopFree(PDArrayElementP(stack, j));
174 }
175 }
176 PDArrayFree(stack);
177 }
178 PDArrayFree(junk->stacks);
179
180 VarBankCellFree(junk);
181 }
182
183
184 /*-----------------------------------------------------------------------
185 //
186 // Function: VarBankCreateStack
187 // Create a stack for the given sort
188 //
189 //
190 // Global Variables: -
191 //
192 // Side Effects : memory operations
193 //
194 /----------------------------------------------------------------------*/
VarBankCreateStack(VarBank_p bank,SortType sort)195 VarBankStack_p VarBankCreateStack(VarBank_p bank, SortType sort)
196 {
197 VarBankStack_p res;
198
199 if (sort >= PDArraySize(bank->stacks))
200 {
201 PDArrayEnlarge(bank->stacks, sort);
202 }
203
204 res = PDArrayAlloc(DEFAULT_VARBANK_SIZE, GROW_EXPONENTIAL);
205 PDArrayAssignP(bank->stacks, sort, res);
206
207 return res;
208 }
209
210
211 /*-----------------------------------------------------------------------
212 //
213 // Function: VarBankClearExtNames()
214 //
215 // Reset the External name -> FunCode association state
216 //
217 // Global Variables: -
218 //
219 // Side Effects : Memory operations
220 //
221 /----------------------------------------------------------------------*/
222
VarBankClearExtNames(VarBank_p vars)223 void VarBankClearExtNames(VarBank_p vars)
224 {
225 VarBankClearExtNamesNoReset(vars);
226 VarBankResetVCount(vars);
227 clear_env_stack(vars);
228 }
229
230
231 /*-----------------------------------------------------------------------
232 //
233 // Function: VarBankClearExtNamesNoReset()
234 //
235 // Reset the External name -> FunCode association state, but do not
236 // reset the variable counter
237 //
238 // Global Variables: -
239 //
240 // Side Effects : Memory operations
241 //
242 /----------------------------------------------------------------------*/
243
VarBankClearExtNamesNoReset(VarBank_p vars)244 void VarBankClearExtNamesNoReset(VarBank_p vars)
245 {
246 StrTreeFree(vars->ext_index);
247 vars->ext_index = NULL;
248 clear_env_stack(vars);
249 }
250
251 /*-----------------------------------------------------------------------
252 //
253 // Function: VarBankVarsSetProp()
254 //
255 // Set the given properties in all variables.
256 //
257 // Global Variables: -
258 //
259 // Side Effects : -
260 //
261 /----------------------------------------------------------------------*/
262
VarBankVarsSetProp(VarBank_p bank,TermProperties prop)263 void VarBankVarsSetProp(VarBank_p bank, TermProperties prop)
264 {
265 VarBankStack_p stack;
266 Term_p handle;
267 int i, j;
268
269 for(i=0; i<PDArraySize(bank->stacks); i++)
270 {
271 stack = PDArrayElementP(bank->stacks, i);
272 if (!stack)
273 {
274 continue;
275 }
276
277 for(j=0; j < stack->size; j++)
278 {
279 handle = PDArrayElementP(stack, j);
280 if(handle)
281 {
282 TermCellSetProp(handle, prop);
283 }
284 }
285 }
286 }
287
288
289 /*-----------------------------------------------------------------------
290 //
291 // Function: VarBankVarsDelProp()
292 //
293 // Delete the given properties in all variables.
294 //
295 // Global Variables:
296 //
297 // Side Effects :
298 //
299 /----------------------------------------------------------------------*/
300
VarBankVarsDelProp(VarBank_p bank,TermProperties prop)301 void VarBankVarsDelProp(VarBank_p bank, TermProperties prop)
302 {
303 VarBankStack_p stack;
304 Term_p handle;
305 int i, j;
306
307 for(i=0; i<PDArraySize(bank->stacks); i++)
308 {
309 stack = PDArrayElementP(bank->stacks, i);
310 if (!stack)
311 {
312 continue;
313 }
314
315 for(j=0; j < stack->size; j++)
316 {
317 handle = PDArrayElementP(stack, j);
318 if(handle)
319 {
320 TermCellDelProp(handle, prop);
321 }
322 }
323 }
324 }
325
326
327 /*-----------------------------------------------------------------------
328 //
329 // Function: VarBankFCodeFind()
330 //
331 // Return the pointer to the variable associated with given f_code
332 // if it exists in the VarBank, NULL otherwise.
333 //
334 // Global Variables: -
335 //
336 // Side Effects : -
337 //
338 /----------------------------------------------------------------------*/
339
VarBankFCodeFind(VarBank_p bank,FunCode f_code,SortType sort)340 Term_p VarBankFCodeFind(VarBank_p bank, FunCode f_code, SortType sort)
341 {
342 VarBankStack_p stack;
343
344 assert(f_code<0);
345 stack = VarBankGetStack(bank, sort);
346 return PDArrayElementP(stack, -f_code);
347 }
348
349
350 /*-----------------------------------------------------------------------
351 //
352 // Function: VarBankExtNameFind()
353 //
354 // Return the pointer to the variable associated with given external
355 // name if it exists in the VarBank, NULL otherwise.
356 //
357 // Global Variables: -
358 //
359 // Side Effects : -
360 //
361 /----------------------------------------------------------------------*/
362
VarBankExtNameFind(VarBank_p bank,char * name)363 Term_p VarBankExtNameFind(VarBank_p bank, char* name)
364 {
365 StrTree_p entry;
366
367 entry = StrTreeFind(&(bank->ext_index), name);
368
369 if(entry)
370 {
371 return entry->val1.p_val;
372 }
373 return NULL;
374 }
375
376
377 /*-----------------------------------------------------------------------
378 //
379 // Function: VarBankVarAlloc()
380 //
381 // Return a pointer to the newly created variable
382 // with the given f_code and sort in the variable bank.
383 //
384 // Global Variables: -
385 //
386 // Side Effects : May change variable bank
387 //
388 /----------------------------------------------------------------------*/
389
VarBankVarAlloc(VarBank_p bank,FunCode f_code,SortType sort)390 Term_p VarBankVarAlloc(VarBank_p bank, FunCode f_code, SortType sort)
391 {
392 Term_p var;
393
394 var = TermDefaultCellAlloc();
395 TermCellSetProp(var, TPIsShared);
396
397 var->weight = DEFAULT_VWEIGHT;
398 var->v_count = 1;
399 var->f_count = 0;
400 var->entry_no = f_code;
401 var->f_code = f_code;
402 var->sort = sort;
403
404 PDArrayAssignP(VarBankGetStack(bank, sort), -f_code, var);
405 bank->max_var = MAX(-f_code, bank->max_var);
406
407 assert(var->sort != STNoSort);
408
409 return var;
410 }
411
412 /*-----------------------------------------------------------------------
413 //
414 // Function: VarBankGetFreshVar()
415 //
416 // Return a pointer to the next "fresh" variable. Freshness is
417 // controlled by the v_count entry in the variable bank, which is
418 // increased by this function. The variable is only guaranteed to be
419 // fresh if VarBankVarAssertAlloc() calls are not mixed with
420 // VarBankGetFreshVar() calls.
421 //
422 // As of 2010-02-10 this will only return even numbered variables -
423 // odd ones are reserved to create clause copies that are
424 // guaranteed to be variable-disjoint.
425 //
426 // Global Variables:
427 //
428 // Side Effects :
429 //
430 /----------------------------------------------------------------------*/
431
VarBankGetFreshVar(VarBank_p bank,SortType sort)432 Term_p VarBankGetFreshVar(VarBank_p bank, SortType sort)
433 {
434 bank->v_count+=2;
435
436 return VarBankVarAssertAlloc(bank, -(bank->v_count), sort);
437 }
438
439
440 /*-----------------------------------------------------------------------
441 //
442 // Function: VarBankExtNameAssertAlloc()
443 //
444 // Return a pointer to the variable with the given external name in
445 // the variable bank. Create a new variable if none with the given
446 // name exists and assign it the next unused FunCode.
447 //
448 // Global Variables: -
449 //
450 // Side Effects : May change variable bank
451 //
452 /----------------------------------------------------------------------*/
453
VarBankExtNameAssertAlloc(VarBank_p bank,char * name)454 Term_p VarBankExtNameAssertAlloc(VarBank_p bank, char* name)
455 {
456 Term_p var;
457 StrTree_p handle, test;
458
459 if(Verbose>=5)
460 {
461 fprintf(stderr, "alloc no sort %s\n", name);
462 }
463
464 var = VarBankExtNameFind(bank, name);
465
466 if(!var)
467 {
468 var = VarBankGetFreshVar(bank, bank->sort_table->default_type);
469 handle = StrTreeCellAlloc();
470 handle->key = SecureStrdup(name);
471 handle->val1.p_val = var;
472 handle->val2.i_val = var->f_code;
473 test = StrTreeInsert(&(bank->ext_index), handle);
474 UNUSED(test); assert(test == NULL);
475 }
476
477 return var;
478 }
479
480 /*-----------------------------------------------------------------------
481 //
482 // Function: VarBankExtNameAssertAllocSort()
483 //
484 // Return a pointer to the variable with the given external name
485 // and sort in the variable bank. Create a new variable if none with
486 // the given name exists and assign it the next unused FunCode.
487 //
488 // Global Variables: -
489 //
490 // Side Effects : May change variable bank
491 //
492 /----------------------------------------------------------------------*/
493
VarBankExtNameAssertAllocSort(VarBank_p bank,char * name,SortType sort)494 Term_p VarBankExtNameAssertAllocSort(VarBank_p bank, char* name, SortType sort)
495 {
496 Term_p var;
497 StrTree_p handle, test;
498 VarBankNamed_p named;
499
500 if(Verbose>=5)
501 {
502 fprintf(stderr, "alloc sort %s with sort ", name);
503 SortPrintTSTP(stderr, bank->sort_table, sort);
504 fputc('\n', stderr);
505 }
506
507 handle = StrTreeFind(&(bank->ext_index), name);
508 if(!handle)
509 {
510 var = VarBankGetFreshVar(bank, sort);
511 handle = StrTreeCellAlloc();
512 handle->key = SecureStrdup(name);
513 handle->val1.p_val = var;
514 handle->val2.i_val = var->f_code;
515 test = StrTreeInsert(&(bank->ext_index), handle);
516 UNUSED(test); assert(test == NULL);
517 }
518 else
519 {
520 var = handle->val1.p_val;
521 if(var->sort != sort)
522 {
523 /* save old var name */
524 named = var_named_new(var, name);
525 PStackPushP(bank->env, named);
526
527 /* replace by new variable (of given sort) */
528 var = VarBankGetFreshVar(bank, sort);
529 handle->val1.p_val = var;
530 handle->val2.i_val = var->f_code;
531 }
532 }
533
534 return var;
535 }
536
537 /*-----------------------------------------------------------------------
538 //
539 // Function: VarBankPushEnv
540 // enter a new environment for variables. The next VarBankPopEnv will
541 // forget about all ext variables defined in between. This is useful
542 // when parsing, if several variables share the same name but
543 // not the same type
544 //
545 //
546 // Global Variables: -
547 //
548 // Side Effects : modifies ext_index and env
549 //
550 /----------------------------------------------------------------------*/
VarBankPushEnv(VarBank_p bank)551 void VarBankPushEnv(VarBank_p bank)
552 {
553 PStackPushP(bank->env, NULL);
554 }
555
556
557 /*-----------------------------------------------------------------------
558 //
559 // Function: VarBankPopEnv
560 // pops env frames until the env stack is empty, or NULL is obtained;
561 // for each such frame, associate the corresponding name with its variable
562 //
563 //
564 // Global Variables: -
565 //
566 // Side Effects : Modifies bank->env
567 //
568 /----------------------------------------------------------------------*/
VarBankPopEnv(VarBank_p bank)569 void VarBankPopEnv(VarBank_p bank)
570 {
571 StrTree_p handle, test;
572 VarBankNamed_p named;
573
574 while(!PStackEmpty(bank->env) && (named = PStackPopP(bank->env)))
575 {
576 handle = StrTreeCellAlloc();
577 handle->key = SecureStrdup(named->name);
578 handle->val1.p_val = named->var;
579 handle->val2.i_val = named->var->f_code;
580 test = StrTreeInsert(&(bank->ext_index), handle);
581
582 if(test)
583 {
584 StrTreeCellFree(handle); /* already present */
585 }
586 }
587 }
588
589 /*-----------------------------------------------------------------------
590 //
591 // Function: VarBankCardinal
592 // Returns the number of variables in the whole var bank
593 //
594 //
595 // Global Variables: -
596 //
597 // Side Effects : -
598 //
599 /----------------------------------------------------------------------*/
VarBankCardinal(VarBank_p bank)600 long VarBankCardinal(VarBank_p bank)
601 {
602 VarBankStack_p stack;
603 long res = 0;
604 int i;
605
606 for (i=0; i < PDArraySize(bank->stacks); i++)
607 {
608 stack = PDArrayElementP(bank->stacks, i);
609 if (stack)
610 {
611 res += PDArrayMembers(stack);
612 }
613 }
614
615 return res;
616 }
617
618
619 /*-----------------------------------------------------------------------
620 //
621 // Function: VarBankCollectVars
622 // Collect all the variables of the bank into the given stack. Returns
623 // the number of variables pushed in the bank.
624 //
625 //
626 // Global Variables: -
627 //
628 // Side Effects : Modifies stack
629 //
630 /----------------------------------------------------------------------*/
VarBankCollectVars(VarBank_p bank,PStack_p into)631 long VarBankCollectVars(VarBank_p bank, PStack_p into)
632 {
633 long res = 0;
634 VarBankStack_p stack;
635 int i;
636 FunCode j;
637 Term_p current_term;
638
639 for (i=0; i < PDArraySize(bank->stacks); i++)
640 {
641 stack = PDArrayElementP(bank->stacks, i);
642 if (stack)
643 {
644 for(j=0; j<PDArraySize(stack); j++)
645 {
646 current_term = PDArrayElementP(stack, j);
647 PStackPushP(into, current_term);
648 res ++;
649 }
650 }
651 }
652
653 return res;
654 }
655
656 /*---------------------------------------------------------------------*/
657 /* End of File */
658 /*---------------------------------------------------------------------*/
659
660
661