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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
|
/*-----------------------------------------------------------------------
File : cte_termvars.c
Author: Stephan Schulz
Contents
Functions for the management of shared variables.
Copyright 1998, 1999, 2008 by the author.
This code is released under the GNU General Public Licence and
the GNU Lesser General Public License.
See the file COPYING in the main E directory for details..
Run "eprover -h" for contact information.
Created: Wed Feb 25 00:48:17 MET 1998
-----------------------------------------------------------------------*/
#include "cte_termvars.h"
/*---------------------------------------------------------------------*/
/* Global Variables */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Internal Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: var_named_new()
//
// Create a new VarBankNamedCell associating name and variable.
//
// Global Variables: -
//
// Side Effects : memory
//
/----------------------------------------------------------------------*/
VarBankNamed_p var_named_new(Term_p var, char* name)
{
VarBankNamed_p res;
//printf("var_named_new()\n");
res = VarBankNamedCellAlloc();
res->name = SecureStrdup(name);
res->var = var;
return res;
}
/*-----------------------------------------------------------------------
//
// Function: var_named_free()
//
// free a VarBankNamed structure.
//
// Global Variables: -
//
// Side Effects : memory
//
/----------------------------------------------------------------------*/
void var_named_free(VarBankNamed_p junk)
{
FREE(junk->name);
VarBankNamedCellFree(junk);
}
/*-----------------------------------------------------------------------
//
// Function: clear_env_stack()
//
// clear the env stack, removing all named cells
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
void clear_env_stack(VarBank_p bank)
{
VarBankNamed_p named;
while(!PStackEmpty(bank->env))
{
named = PStackPopP(bank->env);
if(named)
{
var_named_free(named);
}
}
}
/*---------------------------------------------------------------------*/
/* Exported Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: VarBankAlloc()
//
// Allocate an empty, initialized VarBank-Structure, return pointer
// to it.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
VarBank_p VarBankAlloc(TypeBank_p type_bank)
{
VarBank_p handle;
handle = VarBankCellAlloc();
handle->id = "Unpaired";
handle->var_count = 0;
handle->fresh_count = 0;
handle->sort_table = type_bank;
handle->max_var = 0;
handle->varstacks = PDArrayAlloc(INITIAL_SORT_STACK_SIZE, 5);
handle->v_counts = PDIntArrayAlloc(INITIAL_SORT_STACK_SIZE, 5);
handle->variables = PDArrayAlloc(DEFAULT_VARBANK_SIZE, GROW_EXPONENTIAL);
handle->ext_index = NULL;
handle->env = PStackAlloc();
handle->shadow = NULL;
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: VarBankFree()
//
// Deallocate a VarBankCell.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
void VarBankFree(VarBank_p junk)
{
int i;
VarBankStack_p stack;
Term_p var;
assert(junk);
StrTreeFree(junk->ext_index);
PStackFree(junk->env);
for(i=0; i<PDArraySize(junk->varstacks); i++)
{
stack = (VarBankStack_p)PDArrayElementP(junk->varstacks, i);
if(stack)
{
PStackFree(stack);
}
}
PDArrayFree(junk->varstacks);
PDArrayFree(junk->v_counts);
for(i=0; i<=junk->max_var; i++)
{
if((var = PDArrayElementP(junk->variables, i)))
{
TermTopFree(var);
}
}
PDArrayFree(junk->variables);
if(junk->shadow)
{
junk->shadow->shadow = NULL;
}
VarBankCellFree(junk);
}
/*-----------------------------------------------------------------------
//
// Function: VarBankPairShadow()
//
// Pair two variable banks to ensure that they ave consistent
// id/variable mappings. Primary may contain variables, secondary
// should be empty.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void VarBankPairShadow(VarBank_p primary, VarBank_p secondary)
{
long sort;
PStackPointer i;
PStack_p varstack;
Term_p var;
assert(primary);
assert(secondary);
assert(secondary->var_count == 0);
primary->shadow = secondary;
secondary->shadow = primary;
primary->id = "Primary";
secondary->id = "Secondary";
// Create primary vars in secondary
for(sort=0; sort < PDArraySize(primary->varstacks); sort++)
{
varstack = PDArrayElementP(primary->varstacks, sort);
if(varstack)
{
for(i=0; i<PStackGetSP(varstack); i++)
{
var = PStackElementP(varstack, i);
assert(!VarIsAltVar(var));
assert(var->type->type_uid == sort);
VarBankVarAlloc(secondary, var->f_code, var->type);
}
}
}
secondary->fresh_count = primary->fresh_count;
}
/*-----------------------------------------------------------------------
//
// Function: VarBankCreateStack()
//
// Create a stack for variables of the given sort.
//
// Global Variables: -
//
// Side Effects : memory operations
//
/----------------------------------------------------------------------*/
VarBankStack_p VarBankCreateStack(VarBank_p bank, TypeUniqueID sort)
{
VarBankStack_p res;
res = PStackAlloc();
PDArrayAssignP(bank->varstacks, sort, res);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: VarBankResetVCounts()
//
// Reset all the fresh variable counters for the different sorts.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void VarBankResetVCounts(VarBank_p bank)
{
int i;
for(i=0; i<PDArraySize(bank->v_counts); i++)
{
PDArrayAssignInt(bank->v_counts, i, 0);
}
}
/*-----------------------------------------------------------------------
//
// Function: VarBankSetVCountsToUsed()
//
// Set all the fresh variable counters for the different sorts to
// the maximum number of variables allocated for that sort.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void VarBankSetVCountsToUsed(VarBank_p bank)
{
int i;
VarBankStack_p stack;
for(i=0; i<PDArraySize(bank->v_counts); i++)
{
stack = VarBankGetStack(bank,i);
if(stack)
{
PDArrayAssignInt(bank->v_counts, i, PStackGetSP(stack));
}
else
{
PDArrayAssignInt(bank->v_counts, i, 0);
}
}
}
/*-----------------------------------------------------------------------
//
// Function: VarBankClearExtNames()
//
// Reset the External name -> FunCode association state
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
void VarBankClearExtNames(VarBank_p vars)
{
VarBankClearExtNamesNoReset(vars);
VarBankResetVCounts(vars);
clear_env_stack(vars);
}
/*-----------------------------------------------------------------------
//
// Function: VarBankClearExtNamesNoReset()
//
// Reset the External name -> FunCode association state, but do not
// reset the variable counter
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
void VarBankClearExtNamesNoReset(VarBank_p vars)
{
StrTreeFree(vars->ext_index);
vars->ext_index = NULL;
clear_env_stack(vars);
}
/*-----------------------------------------------------------------------
//
// Function: VarBankVarsSetProp()
//
// Set the given properties in all variables.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void VarBankVarsSetProp(VarBank_p bank, TermProperties prop)
{
Term_p handle;
int i;
for(i=0; i<PDArraySize(bank->variables); i++)
{
handle = PDArrayElementP(bank->variables, i);
if(handle)
{
TermCellSetProp(handle, prop);
}
}
}
/*-----------------------------------------------------------------------
//
// Function: VarBankVarsDelProp()
//
// Delete the given properties in all variables.
//
// Global Variables:
//
// Side Effects :
//
/----------------------------------------------------------------------*/
void VarBankVarsDelProp(VarBank_p bank, TermProperties prop)
{
Term_p handle;
int i;
for(i=0; i<PDArraySize(bank->variables); i++)
{
handle = PDArrayElementP(bank->variables, i);
if(handle)
{
TermCellDelProp(handle, prop);
}
}
}
/*-----------------------------------------------------------------------
//
// Function: VarBankFCodeFind()
//
// Return the pointer to the variable associated with given f_code
// if it exists in the VarBank, NULL otherwise.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
Term_p VarBankFCodeFind(VarBank_p bank, FunCode f_code)
{
assert(f_code<0);
return PDArrayElementP(bank->variables, -f_code);
}
/*-----------------------------------------------------------------------
//
// Function: VarBankExtNameFind()
//
// Return the pointer to the variable associated with given external
// name if it exists in the VarBank, NULL otherwise.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
Term_p VarBankExtNameFind(VarBank_p bank, char* name)
{
StrTree_p entry;
entry = StrTreeFind(&(bank->ext_index), name);
if(entry)
{
return entry->val1.p_val;
}
return NULL;
}
/*-----------------------------------------------------------------------
//
// Function:var_bank_var_alloc()
//
// Return a pointer to the newly created variable
// with the given f_code and sort in the variable bank.
//
// Global Variables: -
//
// Side Effects : May change variable bank
//
/----------------------------------------------------------------------*/
Term_p var_bank_var_alloc(VarBank_p bank, FunCode f_code, Type_p type)
{
Term_p var;
VarBankStack_p stack = VarBankGetStack(bank, type->type_uid);
assert(!PDArrayElementP(bank->variables, -f_code));
var = TermDefaultCellAlloc();
TermCellSetProp(var, TPIsShared);
var->weight = DEFAULT_VWEIGHT;
var->v_count = 1;
var->f_count = 0;
var->entry_no = f_code;
var->f_code = f_code;
var->type = type;
PDArrayAssignP(bank->variables, -f_code, var);
if(!VarIsAltVar(var))
{
PStackPushP(stack, var);
}
bank->max_var = MAX(-f_code, bank->max_var);
bank->var_count++;
assert(var->type);
return var;
}
/*-----------------------------------------------------------------------
//
// Function: VarBankVarAlloc()
//
// Return a pointer to the newly created variable
// with the given f_code and sort in the variable bank.
//
// Global Variables: -
//
// Side Effects : May change variable bank and its shadow
//
/----------------------------------------------------------------------*/
Term_p VarBankVarAlloc(VarBank_p bank, FunCode f_code, Type_p type)
{
Term_p var = var_bank_var_alloc(bank, f_code, type);
if(bank->shadow)
{
var_bank_var_alloc(bank->shadow, f_code, type);
}
return var;
}
/*-----------------------------------------------------------------------
//
// Function: VarBankGetFreshVar()
//
// Return a pointer to the next "fresh" variable. Freshness is
// controlled by the v_count entry in the variable bank, which is
// increased by this function. The variable is only guaranteed to be
// fresh if VarBankVarAssertAlloc() calls are not mixed with
// VarBankGetFreshVar() calls.
//
// As of 2010-02-10 this will only return even numbered variables -
// odd ones are reserved to create clause copies that are
// guaranteed to be variable-disjoint.
//
// Global Variables:
//
// Side Effects :
//
/----------------------------------------------------------------------*/
Term_p VarBankGetFreshVar(VarBank_p bank, Type_p t)
{
Term_p res;
int v_count = PDArrayElementInt(bank->v_counts, t->type_uid);
VarBankStack_p stack = VarBankGetStack(bank, t->type_uid);
assert(stack);
if(UNLIKELY(PStackGetSP(stack)<=v_count))
{
//printf("# XXX %s %ld %d ", bank->id, PStackGetSP(stack), v_count);
bank->fresh_count+=2;
res = VarBankVarAssertAlloc(bank, -(bank->fresh_count), t);
//printf("=> %ld %d\n", PStackGetSP(stack), v_count);
assert(PStackGetSP(stack)>v_count);
if(bank->shadow)
{
bank->shadow->fresh_count = bank->fresh_count;
}
}
else
{
res = PStackElementP(stack, v_count);
}
v_count++;
PDArrayAssignInt(bank->v_counts, t->type_uid, v_count);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: VarBankExtNameAssertAlloc()
//
// Return a pointer to the variable with the given external name in
// the variable bank. Create a new variable if none with the given
// name exists and assign it the next unused FunCode.
//
// Global Variables: -
//
// Side Effects : May change variable bank
//
/----------------------------------------------------------------------*/
Term_p VarBankExtNameAssertAlloc(VarBank_p bank, char* name)
{
Term_p var;
StrTree_p handle, test;
if(Verbose>=5)
{
fprintf(stderr, "Alloc no sort %s\n", name);
}
var = VarBankExtNameFind(bank, name);
if(!var)
{
var = VarBankGetFreshVar(bank, bank->sort_table->default_type);
handle = StrTreeCellAlloc();
handle->key = SecureStrdup(name);
handle->val1.p_val = var;
handle->val2.i_val = var->f_code;
test = StrTreeInsert(&(bank->ext_index), handle);
UNUSED(test); assert(test == NULL);
}
return var;
}
/*-----------------------------------------------------------------------
//
// Function: VarBankExtNameAssertAllocSort()
//
// Return a pointer to the variable with the given external name
// and sort in the variable bank. Create a new variable if none with
// the given name exists and assign it the next unused FunCode.
//
// Global Variables: -
//
// Side Effects : May change variable bank
//
/----------------------------------------------------------------------*/
Term_p VarBankExtNameAssertAllocSort(VarBank_p bank, char* name, Type_p type)
{
Term_p var;
StrTree_p handle, test;
VarBankNamed_p named;
if(Verbose>=5)
{
fprintf(stderr, "# Alloc variable %s with sort ", name);
TypePrintTSTP(stderr, bank->sort_table, type);
fputc('\n', stderr);
}
handle = StrTreeFind(&(bank->ext_index), name);
if(!handle)
{
var = VarBankGetFreshVar(bank, type);
handle = StrTreeCellAlloc();
handle->key = SecureStrdup(name);
handle->val1.p_val = var;
handle->val2.i_val = var->f_code;
test = StrTreeInsert(&(bank->ext_index), handle);
UNUSED(test); assert(test == NULL);
}
else
{
var = handle->val1.p_val;
if(var->type != type)
{
/* save old var name */
named = var_named_new(var, name);
PStackPushP(bank->env, named);
/* replace by new variable (of given sort) */
var = VarBankGetFreshVar(bank, type);
handle->val1.p_val = var;
handle->val2.i_val = var->f_code;
}
}
return var;
}
/*-----------------------------------------------------------------------
//
// Function: VarBankPushEnv
// enter a new environment for variables. The next VarBankPopEnv will
// forget about all ext variables defined in between. This is useful
// when parsing, if several variables share the same name but
// not the same type
//
//
// Global Variables: -
//
// Side Effects : modifies ext_index and env
//
/----------------------------------------------------------------------*/
void VarBankPushEnv(VarBank_p bank)
{
PStackPushP(bank->env, NULL);
}
/*-----------------------------------------------------------------------
//
// Function: VarBankPopEnv
// pops env frames until the env stack is empty, or NULL is obtained;
// for each such frame, associate the corresponding name with its variable
//
//
// Global Variables: -
//
// Side Effects : Modifies bank->env
//
/----------------------------------------------------------------------*/
void VarBankPopEnv(VarBank_p bank)
{
StrTree_p handle, test;
VarBankNamed_p named;
while(!PStackEmpty(bank->env) && (named = PStackPopP(bank->env)))
{
handle = StrTreeCellAlloc();
handle->key = SecureStrdup(named->name);
handle->val1.p_val = named->var;
handle->val2.i_val = named->var->f_code;
test = StrTreeInsert(&(bank->ext_index), handle);
var_named_free(named);
if(test)
{
// StrTree insert compares only by the key:
// if varable with the same name but with the different
// type exists, then type will not be overwritten.
test->val1.p_val = handle->val1.p_val;
test->val2.i_val = handle->val2.i_val;
FREE(handle->key);
StrTreeCellFree(handle);
}
}
}
/*-----------------------------------------------------------------------
//
// Function: VarBankCardinality()
//
// Returns the number of variables in the whole var bank
//
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
long VarBankCardinality(VarBank_p bank)
{
return bank->var_count;
}
/*-----------------------------------------------------------------------
//
// Function: VarBankCollectVars()
//
// Collect all the variables of the bank onto the given
// stack. Returns the total number of variables pushed onto the stack.
//
// Global Variables: -
//
// Side Effects : Modifies stack
//
/----------------------------------------------------------------------*/
long VarBankCollectVars(VarBank_p bank, PStack_p into)
{
long res = 0;
FunCode i;
Term_p t;
printf("VarBankCollectVars()...\n");
for (i=0; i < bank->max_var; i++)
{
t = PDArrayElementP(bank->variables, i);
if(t)
{
PStackPushP(into, t);
res ++;
}
}
printf("...VarBankCollectVars()\n");
return res;
}
/*---------------------------------------------------------------------*/
/* End of File */
/*---------------------------------------------------------------------*/
|