source: vis_dev/vis-2.3/src/var/varVariable.c@ 30

Last change on this file since 30 was 14, checked in by cecile, 15 years ago

vis2.3

File size: 35.7 KB
Line 
1/**CFile***********************************************************************
2
3 FileName [varVariable.c]
4
5 PackageName [var]
6
7 Synopsis [Routines to access the MV-variable data structure.]
8
9 Description []
10
11 SeeAlso []
12
13 Author [Yuji Kukimoto]
14
15 Copyright [Copyright (c) 1994-1996 The Regents of the Univ. of California.
16 All rights reserved.
17
18 Permission is hereby granted, without written agreement and without license
19 or royalty fees, to use, copy, modify, and distribute this software and its
20 documentation for any purpose, provided that the above copyright notice and
21 the following two paragraphs appear in all copies of this software.
22
23 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
24 DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
25 OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
26 CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
29 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
30 FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
31 "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE
32 MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.]
33
34******************************************************************************/
35
36#include "varInt.h"
37
38static char rcsid[] UNUSED = "$Id: varVariable.c,v 1.6 2009/01/18 01:58:09 fabio Exp $";
39
40/*---------------------------------------------------------------------------*/
41/* Constant declarations */
42/*---------------------------------------------------------------------------*/
43
44
45/*---------------------------------------------------------------------------*/
46/* Stucture declarations */
47/*---------------------------------------------------------------------------*/
48
49
50/*---------------------------------------------------------------------------*/
51/* Type declarations */
52/*---------------------------------------------------------------------------*/
53
54
55/*---------------------------------------------------------------------------*/
56/* Variable declarations */
57/*---------------------------------------------------------------------------*/
58
59
60/*---------------------------------------------------------------------------*/
61/* Macro declarations */
62/*---------------------------------------------------------------------------*/
63
64
65/**AutomaticStart*************************************************************/
66
67/*---------------------------------------------------------------------------*/
68/* Static function prototypes */
69/*---------------------------------------------------------------------------*/
70
71static int _VarVariableSetType(Var_Variable_t *var, int type);
72static int _VarVariableResetType(Var_Variable_t *var, int type);
73static char * _VarTypeDecode(int type);
74static array_t * _VarStringArrayDup(array_t *array);
75static void _VarStringArrayFree(array_t *array);
76
77/**AutomaticEnd***************************************************************/
78
79
80/*---------------------------------------------------------------------------*/
81/* Definition of exported functions */
82/*---------------------------------------------------------------------------*/
83
84/**Function********************************************************************
85
86 Synopsis [Adds range/symbolic value information to a variable.]
87
88 Description [Adds range/symbolic value information to a variable. This
89 function is typically called when a .mv construct is encountered for
90 a variable which is already defined by either .inputs or .outputs.
91 The arguments of this function are a pointer to a variable, the number
92 of values the variable can take, and an array of symbolic value names
93 stored as character strings. If the variable is an enumerative variable,
94 then the last argument should be set to NIL(array_t). It is the user's
95 responsibility to free the array and its strings since the function
96 creates a copy of the strings internally. Returns 1 if success.
97 Otherwise returns 0.]
98
99 SideEffects []
100
101 SeeAlso [Var_VariableAlloc Var_VariableFree]
102
103******************************************************************************/
104
105boolean
106Var_VariableAddRangeInfo(
107 Var_Variable_t * var,
108 int numValues,
109 array_t * symValArray)
110{
111 int i;
112 char *symValue;
113 st_table *valueToIndex;
114
115 if (var->indexToValue != NIL(array_t) || var->numValues != 2){
116 error_append("Error: The range of variable ");
117 error_append(var->name);
118 error_append(" has been already set.\n");
119 return 0;
120 }
121
122 if (symValArray != NIL(array_t)){
123 if (numValues != array_n(symValArray)){
124 error_append("Error: Mismatch of range and # of symbolic values.\n");
125 return 0;
126 }
127 valueToIndex = st_init_table(strcmp,st_strhash);
128 var->indexToValue = _VarStringArrayDup(symValArray);
129 for (i=0; i < numValues; i++) {
130 symValue = array_fetch(char *,var->indexToValue,i);
131 if (st_insert(valueToIndex,symValue,(char *)((long)i))) {
132 error_append("Error: Invalid argument for Var_VariableAddRangeInfo().\n");
133 error_append("- Symbolic value ");
134 error_append(symValue);
135 error_append(" is redefined.\n");
136 st_free_table(valueToIndex);
137 return 0;
138 }
139 }
140 var->valueToIndex = valueToIndex;
141 }
142 var->numValues = numValues;
143 return 1;
144}
145
146
147/**Function********************************************************************
148
149 Synopsis [Expands the range of a variable.]
150
151 Description [Expands the range of a variable. Designed for the table
152 determinization routine.]
153
154 SideEffects []
155
156 SeeAlso [Var_VariableReduceRange]
157
158******************************************************************************/
159boolean
160Var_VariableExpandRange(
161 Var_Variable_t *var,
162 int numValues)
163{
164 if (numValues <= var->numValues){
165 error_append("Error: A range can only be expanded.\n");
166 return 0;
167 }
168 if (var->indexToValue != NIL(array_t)){
169 int i;
170 char *dummyName;
171
172 for (i=var->numValues; i < numValues; i++){
173 /* put empty strings for expanded range */
174 dummyName = ALLOC(char,1);
175 *dummyName = '\0';
176 array_insert_last(char *,var->indexToValue,dummyName);
177 }
178 }
179 var->numValues = numValues;
180 return 1;
181}
182
183/**Function********************************************************************
184
185 Synopsis [Reduces the range of a variable.]
186
187 Description [Reduces the range of a variable. Designed for Abstraction. If
188 numValues is greater than the current range of var, then
189 Var_VariableExpandRange will be called and returned.]
190
191 SideEffects []
192
193 SeeAlso [Var_VariableExpandRange]
194
195******************************************************************************/
196boolean
197Var_VariableReduceRange(
198 Var_Variable_t *var,
199 int numValues)
200{
201 if(numValues > var->numValues){
202 return Var_VariableExpandRange(var,numValues);
203 }
204 else if (var->indexToValue != NIL(array_t)){
205 error_append("Error: Cannot reduce the range of a symbolic variable.\n");
206 return 0;
207 }
208
209 var->numValues = numValues;
210 return 1;
211}
212
213/**Function********************************************************************
214
215 Synopsis [Allocates a new variable.]
216
217 Description [Allocates a new variable. This function is typically
218 invoked when a .inputs or a .outputs is encountered in the parser.
219 The arguments of this function are a pointer to the hnode to which
220 a new variable belongs to and a name of the variable. The name string
221 is copied for internal use. The user is responsible for freeing
222 the original string. The new variable is set to a binary variable
223 without any symbolic value definition, i.e. it is defined as a binary
224 enumerative variable. The user can update this range information
225 by calling Var_VariableAddRangeInfo(). Returns a pointer to
226 the variable if success. Otherwise, NIL(Var_Variable_t) is returned. ]
227
228 SideEffects []
229
230 SeeAlso [Var_VariableAddRangeInfo Var_VariableFree]
231
232******************************************************************************/
233
234Var_Variable_t *
235Var_VariableAlloc(
236 Hrc_Node_t *hnode,
237 char *name)
238{
239 Var_Variable_t *var;
240
241 var = ALLOC(Var_Variable_t,1);
242 var->name = util_strsav(name);
243 var->hnode = hnode;
244 if (hnode != NIL(Hrc_Node_t)){
245 if ( Hrc_NodeAddVariable(hnode,var) == 0 ){
246 Var_VariableFree(var);
247 return NIL(Var_Variable_t);
248 }
249 }
250 var->type = 0;
251 /* assume that this is a Boolean variable w/o symbolic value definition */
252 var->numValues = 2;
253 var->numFanoutTables = 0;
254 var->indexToValue = NIL(array_t);
255 var->valueToIndex = NIL(st_table);
256 var->typeIdentifier = NIL(char);
257 var->undef = NIL(void);
258 return var;
259}
260
261/**Function********************************************************************
262
263 Synopsis [Frees a variable.]
264
265 Description []
266
267 SideEffects []
268
269 SeeAlso [Var_VariableAlloc]
270
271******************************************************************************/
272void
273Var_VariableFree(Var_Variable_t *var)
274{
275 if (var->hnode != NIL(Hrc_Node_t)){
276 Hrc_NodeDeleteVariable(var->hnode,var);
277 }
278 FREE(var->name);
279 if (var->indexToValue != NIL(array_t)){
280 _VarStringArrayFree(var->indexToValue);
281 }
282 if (var->valueToIndex != NIL(st_table)){
283 st_free_table(var->valueToIndex);
284 }
285 if (var->typeIdentifier != NIL(char)){
286 FREE(var->typeIdentifier);
287 }
288 FREE(var);
289}
290
291/**Function********************************************************************
292
293 Synopsis [Duplicates a variable. ]
294
295 Description [Duplicates a variable. The second argument hnode is a new hnode
296 to which a duplicated variable belongs.]
297
298 SideEffects []
299
300 SeeAlso []
301
302******************************************************************************/
303Var_Variable_t *
304Var_VariableDup(
305 Var_Variable_t *var,
306 Hrc_Node_t *hnode)
307{
308 Var_Variable_t *varDup;
309
310 varDup = Var_VariableAlloc(hnode,var->name);
311 varDup->type = var->type;
312 varDup->hnode = hnode;
313 varDup->numFanoutTables = var->numFanoutTables;
314 if (Var_VariableAddRangeInfo(varDup,var->numValues,var->indexToValue) == 0){
315 return NIL(Var_Variable_t);
316 }
317 if (var->typeIdentifier != NIL(char)){
318 varDup->typeIdentifier = util_strsav(var->typeIdentifier);
319 }
320 return varDup;
321}
322
323/**Function********************************************************************
324
325 Synopsis [Returns a pointer to the name of a variable.]
326
327 Description []
328
329 SideEffects []
330
331 SeeAlso []
332
333******************************************************************************/
334char *
335Var_VariableReadName(Var_Variable_t *var)
336{
337 return var->name;
338}
339
340/**Function********************************************************************
341
342 Synopsis [Returns 1 if a variable is a primary input. Otherwise returns 0.]
343
344 Description []
345
346 SideEffects []
347
348 SeeAlso [Var_VariableSetPI]
349
350******************************************************************************/
351boolean
352Var_VariableTestIsPI(Var_Variable_t *var)
353{
354 return (((var->type) & VarPI) ? 1:0);
355}
356
357/**Function********************************************************************
358
359 Synopsis [Returns 1 if a variable is a primary output. Otherwise returns 0.]
360
361 Description []
362
363 SideEffects []
364
365 SeeAlso [Var_VariableSetPO]
366
367******************************************************************************/
368boolean
369Var_VariableTestIsPO(Var_Variable_t *var)
370{
371 return (((var->type) & VarPO) ? 1:0);
372}
373
374/**Function********************************************************************
375
376 Synopsis [Returns 1 if a variable is a present state variable. Otherwise returns 0.]
377
378 Description []
379
380 SideEffects []
381
382 SeeAlso [Var_VariableSetPS]
383
384******************************************************************************/
385boolean
386Var_VariableTestIsPS(Var_Variable_t *var)
387{
388 return (((var->type) & VarPS) ? 1:0);
389}
390
391/**Function********************************************************************
392
393 Synopsis [Returns 1 if a variable is a next state variable. Otherwise returns 0.]
394
395 Description []
396
397 SideEffects []
398
399 SeeAlso [Var_VariableSetNS]
400
401******************************************************************************/
402boolean
403Var_VariableTestIsNS(Var_Variable_t *var)
404{
405 return (((var->type) & VarNS) ? 1:0);
406}
407
408/**Function********************************************************************
409
410 Synopsis [Returns 1 if a variable is a subcircuit input. Otherwise returns 0.]
411
412 Description []
413
414 SideEffects []
415
416 SeeAlso [Var_VariableSetSI]
417
418******************************************************************************/
419boolean
420Var_VariableTestIsSI(Var_Variable_t *var)
421{
422 return (((var->type) & VarSI) ? 1:0);
423}
424
425/**Function********************************************************************
426
427 Synopsis [Returns 1 if a variable is a subcircuit output. Otherwise returns 0.]
428
429 Description []
430
431 SideEffects []
432
433 SeeAlso [Var_VariableSetSO]
434
435******************************************************************************/
436boolean
437Var_VariableTestIsSO(Var_Variable_t *var)
438{
439 return (((var->type) & VarSO) ? 1:0);
440}
441
442/**Function********************************************************************
443
444 Synopsis [Returns 1 if the value type of a variable is symbolic and 0
445 otherwise.]
446
447 Description []
448
449 SideEffects []
450
451 SeeAlso [Var_VariableTestIsEnumerative]
452
453******************************************************************************/
454boolean
455Var_VariableTestIsSymbolic(Var_Variable_t *var)
456{
457 if (var->indexToValue != NIL(array_t)){
458 return 1;
459 }
460 return 0;
461}
462
463/**Function********************************************************************
464
465 Synopsis [Returns 1 if the value type of a variable is enumerative and 0
466 otherwise.]
467
468 Description []
469
470 SideEffects []
471
472 SeeAlso [Var_VariableTestIsSymbolic]
473
474******************************************************************************/
475boolean
476Var_VariableTestIsEnumerative(Var_Variable_t *var)
477{
478 if (var->indexToValue == NIL(array_t)){
479 return 1;
480 }
481 return 0;
482}
483
484/**Function********************************************************************
485
486 Synopsis [Returns 1 if a variable is enumerative *and* a value is
487 in the range of the variable. Returns 0 otherwise.]
488
489 Description []
490
491 SideEffects []
492
493 SeeAlso []
494
495******************************************************************************/
496boolean
497Var_VariableTestIsValueInRange(
498 Var_Variable_t *var,
499 int value)
500{
501 if (var->indexToValue != NIL(array_t)){
502 return 0;
503 }
504 if (value >= 0 && value < var->numValues){
505 return 1;
506 }
507 return 0;
508}
509
510/**Function********************************************************************
511
512 Synopsis [Returns the number of values a variable can take.]
513
514 Description []
515
516 SideEffects []
517
518 SeeAlso []
519
520******************************************************************************/
521int
522Var_VariableReadNumValues(Var_Variable_t *var)
523{
524 return var->numValues;
525}
526
527/**Function********************************************************************
528
529 Synopsis [Returns the number of fanout tables.]
530
531 Description []
532
533 SideEffects []
534
535 SeeAlso []
536
537******************************************************************************/
538int
539Var_VariableReadNumFanoutTables(Var_Variable_t *var)
540{
541 return var->numFanoutTables;
542}
543
544/**Function********************************************************************
545
546 Synopsis [Increments the number of fanout tables.]
547
548 Description []
549
550 SideEffects []
551
552 SeeAlso []
553
554******************************************************************************/
555void
556Var_VariableIncrementNumFanoutTables(Var_Variable_t *var)
557{
558 (var->numFanoutTables)++;
559}
560
561/**Function********************************************************************
562
563 Synopsis [Resets the number of fanout tables.]
564
565 Description []
566
567 SideEffects []
568
569 SeeAlso []
570
571******************************************************************************/
572void
573Var_VariableResetNumFanoutTables(
574 Var_Variable_t* var)
575{
576 var->numFanoutTables = 0;
577}
578
579
580/**Function********************************************************************
581
582 Synopsis [Returns the name of the symbolic value associated with an integer index.]
583
584 Description []
585
586 SideEffects []
587
588 SeeAlso []
589
590******************************************************************************/
591char *
592Var_VariableReadSymbolicValueFromIndex(
593 Var_Variable_t *var,
594 int i)
595{
596 assert(var->indexToValue != NIL(array_t));
597 return array_fetch(char *,var->indexToValue,i);
598}
599
600/**Function********************************************************************
601
602 Synopsis [Returns the integer index associated with the name of a symbolic value.]
603
604 Description [Returns the integer index associated with a symbolic value. If
605 a given symbolic name is not valid, the routine returns -1.]
606
607 SideEffects []
608
609 SeeAlso []
610
611******************************************************************************/
612int
613Var_VariableReadIndexFromSymbolicValue(
614 Var_Variable_t *var,
615 char *symbolicValue)
616{
617 int isFound;
618 int index;
619
620 assert(var->valueToIndex != NIL(st_table));
621 isFound = st_lookup_int(var->valueToIndex,symbolicValue,&index);
622 return( isFound ? index : -1);
623}
624
625/**Function********************************************************************
626
627 Synopsis [Returns the hnode to which a variable belongs.]
628
629 Description []
630
631 SideEffects []
632
633 SeeAlso []
634
635******************************************************************************/
636Hrc_Node_t *
637Var_VariableReadHnode(Var_Variable_t *var)
638{
639 return var->hnode;
640}
641
642/**Function********************************************************************
643
644 Synopsis [Returns a pointer to the type identifier of a variable.]
645
646 Description [Reads the type identifier for the variable. Type identifiers are
647 defined in blif-mv using the .type statement.
648 Example: .type color red white blue
649 .mv flag_colors type=color]
650
651 SideEffects []
652
653 SeeAlso []
654
655******************************************************************************/
656char *
657Var_VariableReadTypeIdentifier(Var_Variable_t *var)
658{
659 return var->typeIdentifier;
660}
661
662
663/**Function********************************************************************
664
665 Synopsis [Returns a pointer to the user field of a variable.]
666
667 Description []
668
669 SideEffects []
670
671 SeeAlso []
672
673******************************************************************************/
674void *
675Var_VariableReadUndef(Var_Variable_t *var)
676{
677 return var->undef;
678}
679
680/**Function********************************************************************
681
682 Synopsis [Changes the name of a variable.]
683
684 Description [Changes the name of a variable. Returns 1 if success, otherwise
685 returns 0.]
686
687 SideEffects []
688
689 SeeAlso []
690
691******************************************************************************/
692boolean
693Var_VariableChangeName(
694 Var_Variable_t *var,
695 char *name)
696{
697 if (var->hnode != NIL(Hrc_Node_t)){
698 if (Hrc_NodeDeleteVariable(var->hnode,var) == 0){
699 /* this variable should have been in the hnode */
700 return 0;
701 }
702 }
703 FREE(var->name);
704 var->name = util_strsav(name);
705 if (var->hnode != NIL(Hrc_Node_t)){
706 if (Hrc_NodeAddVariable(var->hnode,var) == 0){
707 /* there is a node with the new name in the hnode already */
708 return 0;
709 }
710 }
711 return 1;
712}
713
714/**Function********************************************************************
715
716 Synopsis [Sets the PI-field of a variable.]
717
718 Description [Sets the PI-field of a variable. Returns 1 upon success.
719 Returns 0 if the variable is
720 already set as a PI. Returns -1 if a type error occurs. This function is used when
721 a variable is defined in .inputs *and* the variable is already declared
722 in .mv. ]
723
724 SideEffects []
725
726 SeeAlso [Var_VariableTestIsPI]
727
728******************************************************************************/
729int
730Var_VariableSetPI(Var_Variable_t *var)
731{
732 return _VarVariableSetType(var,VarPI);
733}
734
735/**Function********************************************************************
736
737 Synopsis [Sets the PO-field of a variable.]
738
739 Description [Sets the PO-field of a variable. Returns 1 upon success.
740 Returns 0 if the variable is
741 already set as a PO. Returns -1 if a type error occurs. This function is used when
742 a variable is defined in .outputs *and* the variable is already declared
743 in .mv. ]
744
745 SideEffects []
746
747 SeeAlso [Var_VariableTestIsPO]
748
749******************************************************************************/
750int
751Var_VariableSetPO(Var_Variable_t *var)
752{
753 return _VarVariableSetType(var,VarPO);
754}
755
756/**Function********************************************************************
757
758 Synopsis [Sets the PS-field of a variable.]
759
760 Description [Sets the PS-field of a variable. Returns 1 upon success.
761 Returns 0 if the variable is
762 already set as a PS. Returns -1 if a type error occurs. This function is used when
763 a variable is defined in .latch *and* the variable is already declared
764 in .mv. ]
765
766 SideEffects []
767
768 SeeAlso [Var_VariableTestIsPS]
769
770******************************************************************************/
771int
772Var_VariableSetPS(Var_Variable_t *var)
773{
774 return _VarVariableSetType(var,VarPS);
775}
776
777/**Function********************************************************************
778
779 Synopsis [Sets the NS-field of a variable.]
780
781 Description [Sets the NS-field of a variable. Returns 1 upon success.
782 Returns 0 if the variable is
783 already set as an NS. Returns -1 if a type error occurs. This function is used when
784 a variable is defined in .latch *and* the variable is already declared
785 in .mv. ]
786
787 SideEffects []
788
789 SeeAlso [Var_VariableTestIsNS]
790
791******************************************************************************/
792int
793Var_VariableSetNS(Var_Variable_t *var)
794{
795 return _VarVariableSetType(var,VarNS);
796}
797
798/**Function********************************************************************
799
800 Synopsis [Sets the SI-field of a variable.]
801
802 Description [Sets the SI-field of a variable. Returns 1 upon success.
803 Returns 0 if the variable is
804 already set as an SI. Returns -1 if a type error occurs.]
805
806 SideEffects []
807
808 SeeAlso [Var_VariableTestIsSI]
809
810******************************************************************************/
811int
812Var_VariableSetSI(Var_Variable_t *var)
813{
814 return _VarVariableSetType(var,VarSI);
815}
816
817/**Function********************************************************************
818
819 Synopsis [Sets the SO-field of a variable.]
820
821 Description [Sets the SO-field of a variable. Returns 1 upon success.
822 Returns 0 if the variable is
823 already set as an SO. Returns -1 if a type error occurs.]
824
825 SideEffects []
826
827 SeeAlso [Var_VariableTestIsSO]
828
829******************************************************************************/
830int
831Var_VariableSetSO(Var_Variable_t *var)
832{
833 return _VarVariableSetType(var,VarSO);
834}
835
836/**Function********************************************************************
837
838 Synopsis [Resets the PI-field of a variable.]
839
840 Description [Resets the PI-field of a variable. Returns 1 upon success.
841 Returns 0 if the variable is not set to PI.
842 ]
843
844 SideEffects []
845
846 SeeAlso [Var_VariableTestIsPI]
847
848******************************************************************************/
849int
850Var_VariableResetPI(Var_Variable_t *var)
851{
852 return _VarVariableResetType(var,VarPI);
853}
854
855/**Function********************************************************************
856
857 Synopsis [Resets the PO-field of a variable.]
858
859 Description [Resets the PO-field of a variable. Returns 1 upon success.
860 Returns 0 if the variable is not set to PO.
861 ]
862
863 SideEffects []
864
865 SeeAlso [Var_VariableTestIsPO]
866
867******************************************************************************/
868int
869Var_VariableResetPO(Var_Variable_t *var)
870{
871 return _VarVariableResetType(var,VarPO);
872}
873
874/**Function********************************************************************
875
876 Synopsis [Resets the PS-field of a variable.]
877
878 Description [Resets the PS-field of a variable. Returns 1 upon success.
879 Returns 0 if the variable is not set to PS.
880 ]
881
882 SideEffects []
883
884 SeeAlso [Var_VariableTestIsPS]
885
886******************************************************************************/
887int
888Var_VariableResetPS(Var_Variable_t *var)
889{
890 return _VarVariableResetType(var,VarPS);
891}
892
893/**Function********************************************************************
894
895 Synopsis [Resets the NS-field of a variable.]
896
897 Description [Resets the NS-field of a variable. Returns 1 upon success.
898 Returns 0 if the variable is not set to NS.
899 ]
900
901 SideEffects []
902
903 SeeAlso [Var_VariableTestIsNS]
904
905******************************************************************************/
906int
907Var_VariableResetNS(Var_Variable_t *var)
908{
909 return _VarVariableResetType(var,VarNS);
910}
911
912/**Function********************************************************************
913
914 Synopsis [Resets the SI-field of a variable.]
915
916 Description [Resets the SI-field of a variable. Returns 1 upon success.
917 Returns 0 if the variable is not set to SI.
918 ]
919
920 SideEffects []
921
922 SeeAlso [Var_VariableTestIsSI]
923
924******************************************************************************/
925int
926Var_VariableResetSI(Var_Variable_t *var)
927{
928 return _VarVariableResetType(var,VarSI);
929}
930
931/**Function********************************************************************
932
933 Synopsis [Resets the SO-field of a variable.]
934
935 Description [Resets the SO-field of a variable. Returns 1 upon success.
936 Returns 0 if the variable is not set to SO.
937 ]
938
939 SideEffects []
940
941 SeeAlso [Var_VariableTestIsSO]
942
943******************************************************************************/
944int
945Var_VariableResetSO(Var_Variable_t *var)
946{
947 return _VarVariableResetType(var,VarSO);
948}
949
950/**Function********************************************************************
951
952 Synopsis [Resets all the types of a variable.]
953
954 Description [Resets all the types of a variable.]
955
956 SideEffects []
957
958 SeeAlso []
959
960******************************************************************************/
961void
962Var_VariableResetAllTypes(Var_Variable_t *var)
963{
964 var->type = 0;
965}
966
967
968/**Function********************************************************************
969
970 Synopsis [Sets the type identifier for the variable.]
971
972 Description [Sets the type identifier for the variable. Type identifiers are
973 defined in blif-mv using the .type statement.
974 Example: .type color red white blue
975 .mv flag_colors type=color]
976
977 SideEffects []
978
979 SeeAlso []
980
981******************************************************************************/
982void
983Var_VariableSetTypeIdentifier(
984 Var_Variable_t *var,
985 char *typeIdentifier)
986{
987 if (var->typeIdentifier != NIL(char)){
988 FREE(var->typeIdentifier);
989 }
990 var->typeIdentifier = util_strsav(typeIdentifier);
991}
992
993/**Function********************************************************************
994
995 Synopsis [Given two variables, checks to see if the two variables refer
996 to the identical signal once a hierarchy is flattened out. Returns 1 if
997 they are identical. Otherwise returns 0. If the two variables do not
998 belong to the same hierarchy manager, it also returns 0 with an error
999 message sent to error_string.]
1000
1001 Description []
1002
1003 SideEffects []
1004
1005 SeeAlso []
1006
1007******************************************************************************/
1008boolean
1009Var_VariablesTestIdentical(
1010 Var_Variable_t *var1,
1011 Var_Variable_t *var2)
1012{
1013 Hrc_Manager_t *hmgr1, *hmgr2;
1014 Hrc_Node_t *hnode1, *hnode2, *rootNode;
1015 Var_Variable_t *varCanonical1, *varCanonical2;
1016
1017 hnode1 = Var_VariableReadHnode(var1);
1018 hnode2 = Var_VariableReadHnode(var2);
1019 hmgr1 = Hrc_NodeReadManager(hnode1);
1020 hmgr2 = Hrc_NodeReadManager(hnode2);
1021
1022 if (hmgr1 != hmgr2){
1023 error_append("Error: Variables ");
1024 error_append(Var_VariableReadName(var1));
1025 error_append(" and ");
1026 error_append(Var_VariableReadName(var2));
1027 error_append(" are not under the same manager.\n");
1028 return 0;
1029 }
1030 /* if two variables reside in the same hnode */
1031 if (hnode1 == hnode2){
1032 if (var1 == var2){
1033 return 1;
1034 }
1035 return 0;
1036 }
1037
1038 /* otherwise */
1039 rootNode = Hrc_ManagerReadRootNode(hmgr1);
1040
1041 varCanonical1 = Hrc_VariableFindActualFromFormal(hnode1,var1,rootNode);
1042 varCanonical2 = Hrc_VariableFindActualFromFormal(hnode2,var2,rootNode);
1043 if (varCanonical1 == varCanonical2){
1044 return 1;
1045 }
1046 return 0;
1047}
1048
1049
1050/**Function********************************************************************
1051
1052 Synopsis [Checks to see if two variables are defined over the same domain.]
1053
1054 Description []
1055
1056 SideEffects []
1057
1058 SeeAlso []
1059
1060******************************************************************************/
1061boolean
1062Var_VariablesTestHaveSameDomain(
1063 Var_Variable_t *var1,
1064 Var_Variable_t *var2)
1065{
1066 array_t *indexToValue1, *indexToValue2;
1067 char *value1, *value2;
1068 int i;
1069
1070 if (var1->numValues != var2->numValues)
1071 return 0;
1072 indexToValue1 = var1->indexToValue;
1073 indexToValue2 = var2->indexToValue;
1074 if (indexToValue1 == NIL(array_t) || indexToValue2 == NIL(array_t))
1075 return 1;
1076 for (i=0; i < array_n(indexToValue1); i++){
1077 value1 = array_fetch(char *,indexToValue1,i);
1078 value2 = array_fetch(char *,indexToValue2,i);
1079 if (strcmp(value1,value2)){
1080 return 0;
1081 }
1082 }
1083 return 1;
1084}
1085
1086/**Function********************************************************************
1087
1088 Synopsis [Checks to see if there is no type violation. Returns 1 if
1089 no violation exists. Otherwise returns 0.]
1090
1091 Description []
1092
1093 SideEffects []
1094
1095 SeeAlso []
1096
1097******************************************************************************/
1098boolean
1099Var_VariableTestTypeConsistency(Var_Variable_t *var)
1100{
1101 /* checking PI/PO */
1102 if ((var->type & 03) == 03){
1103 error_append("Error: Variable ");
1104 error_append(Var_VariableReadName(var));
1105 error_append(" is of type PI/PO.\n");
1106 return 0;
1107 }
1108 /* checking PI/PS */
1109 if ((var->type & 05) == 05){
1110 error_append("Error: Variable ");
1111 error_append(Var_VariableReadName(var));
1112 error_append(" is of type PI/PS.\n");
1113 return 0;
1114 }
1115 /* checking PI/SO */
1116 if ((var->type & 041) == 041){
1117 error_append("Error: Variable ");
1118 error_append(Var_VariableReadName(var));
1119 error_append(" is of type PI/SO.\n");
1120 return 0;
1121 }
1122 return 1;
1123}
1124
1125/**Function********************************************************************
1126
1127 Synopsis [Checks to see if two variables have the same type identifier]
1128
1129 Description [Returns 1 if var1 and var2 have the same type identifier,
1130 0 otherwise.]
1131
1132 SideEffects []
1133
1134 SeeAlso []
1135
1136******************************************************************************/
1137boolean
1138Var_VariablesTestHaveSameTypeIdentifier(
1139 Var_Variable_t *var1,
1140 Var_Variable_t *var2)
1141{
1142 if (var1->typeIdentifier == NIL(char) && var2->typeIdentifier == NIL(char)){
1143 return 1;
1144 }
1145 if (var1->typeIdentifier == NIL(char) || var2->typeIdentifier == NIL(char)){
1146 return 0;
1147 }
1148 if (strcmp(var1->typeIdentifier,var2->typeIdentifier) == 0 ){
1149 return 1;
1150 }
1151 return 0;
1152}
1153
1154
1155/*---------------------------------------------------------------------------*/
1156/* Definition of internal functions */
1157/*---------------------------------------------------------------------------*/
1158
1159/*---------------------------------------------------------------------------*/
1160/* Definition of static functions */
1161/*---------------------------------------------------------------------------*/
1162
1163/**Function********************************************************************
1164
1165 Synopsis [Sets the type field of a variable. Returns 0 if the variable is
1166 already set as a given type. If the variable is newly set to the type, it
1167 returns 1. If a type violation occurs after this setting, -1 is returned.
1168 Type can be set either VarPI, VarPO, VarPS, VarNS, VarSI and VarSO.]
1169
1170 Description []
1171
1172 SideEffects []
1173
1174 SeeAlso []
1175
1176******************************************************************************/
1177static int
1178_VarVariableSetType(Var_Variable_t *var,int type)
1179{
1180/*
1181 if (var->type & type){
1182 error_append("Warning: Variable ");
1183 error_append(Var_VariableReadName(var));
1184 error_append(" is already set to ");
1185 error_append(_VarTypeDecode(type));
1186 error_append(".\n");
1187 return 0;
1188 }
1189*/
1190 var->type |= type;
1191 if (Var_VariableTestTypeConsistency(var) == 0){
1192 return -1;
1193 }
1194 return 1;
1195}
1196
1197/**Function********************************************************************
1198
1199 Synopsis [Resets the type field of a variable. Returns 0 if the variable is
1200 not set as a given type, i.e. there is no need for resetting the type.
1201 Otherwise, returns 1.
1202 Type can be set either VarPI, VarPO, VarPS, VarNS, VarSI and VarSO.]
1203
1204 Description []
1205
1206 SideEffects []
1207
1208 SeeAlso []
1209
1210******************************************************************************/
1211static int
1212_VarVariableResetType(Var_Variable_t *var,int type)
1213{
1214/*
1215 if (var->type & type){
1216 error_append("Warning: Variable ");
1217 error_append(Var_VariableReadName(var));
1218 error_append(" is already set to ");
1219 error_append(_VarTypeDecode(type));
1220 error_append(".\n");
1221 return 0;
1222 }
1223*/
1224 if ((var->type & type) == 0){
1225 return 0;
1226 }
1227 var->type &= ~type;
1228 return 1;
1229}
1230
1231/**Function********************************************************************
1232
1233 Synopsis [Returns the character string corresponding to a type encoded
1234 in an integer. Note that SI and SO are filtered out.]
1235
1236 Description []
1237
1238 SideEffects []
1239
1240 SeeAlso []
1241
1242******************************************************************************/
1243static char *
1244_VarTypeDecode(int type)
1245{
1246 /* suppress information on SI/SO for simplicity */
1247 type &= 017;
1248 switch(type){
1249 case 00:
1250 return "Internal";
1251 case 01:
1252 return "PI";
1253 case 02:
1254 return "PO";
1255 case 03:
1256 error_append("Error: Strange type PI/PO.\n");
1257 return "PI/PO";
1258 case 04:
1259 return "PS";
1260 case 05:
1261 error_append("Error: Strange type PI/PS.\n");
1262 return "PI/PS";
1263 case 06:
1264 return "PO/PS";
1265 case 07:
1266 error_append("Error: Strange type PI/PO/PS.\n");
1267 return "PI/PO/PS";
1268 case 010:
1269 return "NS";
1270 case 011:
1271 return "PI/NS";
1272 case 012:
1273 return "PO/NS";
1274 case 013:
1275 error_append("Error: Strange type PI/PO/NS.\n");
1276 return "PI/PO/NS";
1277 case 014:
1278 return "PS/NS";
1279 case 015:
1280 error_append("Error: Strange type PI/PS/NS.\n");
1281 return "PI/PS/NS";
1282 case 016:
1283 return "PO/PS/NS";
1284 case 017:
1285 error_append("Error: Strange type PI/PO/PS/NS.\n");
1286 return "PI/PO/PS/NS";
1287 default:
1288 fail("Strange type in VarTypeDecode().\n");
1289 return NIL(char); /* not reached */
1290 }
1291}
1292
1293/**Function********************************************************************
1294
1295 Synopsis [Duplicates an array of strings.]
1296
1297 Description [Duplicates an array of strings. Strings themselves are also
1298 copied over to the new array.]
1299
1300 SideEffects []
1301
1302 SeeAlso []
1303
1304******************************************************************************/
1305static array_t *
1306_VarStringArrayDup(array_t *array)
1307{
1308 int i;
1309 char *symbol,*newSymbol;
1310 array_t *newArray;
1311
1312 newArray = array_alloc(char *,0);
1313 for (i=0; i < array_n(array); i++) {
1314 symbol = array_fetch(char *, array, i);
1315 newSymbol = util_strsav(symbol);
1316 array_insert_last(char *,newArray,newSymbol);
1317 }
1318 return newArray;
1319}
1320
1321/**Function********************************************************************
1322
1323 Synopsis [Frees an array of strings.]
1324
1325 Description [Frees an array of strings. Strings themselves are also
1326 freed.]
1327
1328 SideEffects []
1329
1330 SeeAlso []
1331
1332******************************************************************************/
1333static void
1334_VarStringArrayFree(array_t *array)
1335{
1336 int i;
1337 char *symbol;
1338
1339 for (i=0; i < array_n(array); i++) {
1340 symbol = array_fetch(char *, array, i);
1341 FREE(symbol);
1342 }
1343 array_free(array);
1344}
Note: See TracBrowser for help on using the repository browser.