source: vis_dev/vis-2.3/src/io/ioParse.c @ 17

Last change on this file since 17 was 14, checked in by cecile, 13 years ago

vis2.3

File size: 17.3 KB
RevLine 
[14]1/**CFile***********************************************************************
2
3  FileName    [ioParse.c]
4
5  PackageName [io]
6
7  Synopsis    [Functions used in parsing BLIF-MV files.]
8
9  Description []
10
11  SeeAlso     []
12
13  Author      [Yuji Kukimoto, Rajeev Ranjan, Huey-Yih Wang]
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 "ioInt.h"
37
38static char rcsid[] UNUSED = "$Id: ioParse.c,v 1.4 2002/08/16 17:35:01 fabio Exp $";
39
40/*---------------------------------------------------------------------------*/
41/* Constant declarations                                                     */
42/*---------------------------------------------------------------------------*/
43
44
45/*---------------------------------------------------------------------------*/
46/* Type declarations                                                         */
47/*---------------------------------------------------------------------------*/
48
49
50/*---------------------------------------------------------------------------*/
51/* Stucture 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 void _IoParserAddSubckt(array_t **subcktArray, char *modelName, char *instanceName, array_t *formalNameArray, array_t *actualNameArray);
72static int _IoIOProcess(Hrc_Node_t *hnode, char *name, int type);
73static IoPTable_t * _IoPTableCreate(array_t *inputArray, array_t *outputArray, array_t *defaultArray, array_t *symCubeArray);
74static void _IoPTableFree(IoPTable_t *pTable);
75
76/**AutomaticEnd***************************************************************/
77
78
79/*---------------------------------------------------------------------------*/
80/* Definition of exported functions                                          */
81/*---------------------------------------------------------------------------*/
82
83
84/*---------------------------------------------------------------------------*/
85/* Definition of internal functions                                          */
86/*---------------------------------------------------------------------------*/
87
88/**Function********************************************************************
89
90  Synopsis    [Finds or allocate a variable by name in a hnode.]
91
92  Description [Finds a variable by name in a hnode. If success, returns
93  a pointer to the variable. Otherwise allocates a new variable
94  and return a pointer to the variable..]
95
96  SideEffects []
97
98  SeeAlso     [Var_VariableFree]
99
100******************************************************************************/
101
102Var_Variable_t *
103IoVariableFindOrAllocByName(
104  Hrc_Node_t *hnode,
105  char *name)
106{
107  Var_Variable_t *var;
108
109  if ((var = Hrc_NodeFindVariableByName(hnode,name)) != NIL(Var_Variable_t)){
110    return var;
111  }
112  var = Var_VariableAlloc(hnode,name);
113  return var;
114}
115
116/**Function********************************************************************
117
118  Synopsis    [Converts a character string to the integer.]
119
120  Description [Converts a character string to the positive integer. Returns
121  -1 if the string does not represent an integer.]
122
123  SideEffects []
124
125  SeeAlso     []
126
127******************************************************************************/
128int
129IoAtoi(char *string)
130{
131  char *s;
132  for (s=string; *s!='\0'; s++) {
133    if (isdigit((int) *s)==0){
134      return -1;
135    }
136  } 
137  return atoi(string);
138}
139
140/**Function********************************************************************
141
142  Synopsis    [Frees an array of strings.]
143
144  Description [Frees an array of strings. Strings themselves are also
145  freed.]
146
147  SideEffects []
148
149  SeeAlso     []
150
151******************************************************************************/
152void
153IoStringArrayFree(array_t *array)
154{
155  int i;
156  char *symbol;
157
158  for (i=0; i < array_n(array); i++) {
159    symbol = array_fetch(char *, array, i);
160    FREE(symbol);
161  }
162  array_free(array);
163}
164
165
166/**Function********************************************************************
167
168  Synopsis    [Insert a character string to an array.]
169
170  Description [Insert a character string to an array. The string is not copied.
171  Note that the first argument is not a pointer to an array, but a pointer
172  to a pointer to an array.]
173
174  SideEffects []
175
176  SeeAlso     []
177
178******************************************************************************/
179void
180IoNameInsertInArray(array_t **arrayPtrPtr, char *name)
181{
182  if (*arrayPtrPtr == NIL(array_t)){
183    *arrayPtrPtr = array_alloc(char *, 0);
184  }
185  array_insert_last(char *, *arrayPtrPtr, name);
186}
187
188/**Function********************************************************************
189
190  Synopsis    [Inserts a IoSymValue_t structure to an array.]
191
192  Description [Inserts a IoSymValue_t structure to an array. Note that
193  the first argument is a pointer to a pointer to the array.]
194
195  SideEffects []
196
197  SeeAlso     []
198
199******************************************************************************/
200void
201IoSymValueInsertInArray(array_t **arrayPtrPtr, IoSymValue_t *value)
202{
203  if (*arrayPtrPtr == NIL(array_t)){
204    *arrayPtrPtr = array_alloc(IoSymValue_t *, 0);
205  }
206  array_insert_last(IoSymValue_t *, *arrayPtrPtr, value);
207}
208
209/**Function********************************************************************
210
211  Synopsis    [Inserts a SymCube(an array of IoSymValue_t) to an array.]
212
213  Description []
214
215  SideEffects []
216
217  SeeAlso     []
218
219******************************************************************************/
220void
221IoSymCubeInsertInArray(array_t **arrayPtrPtr, array_t *symCube)
222{
223  if (*arrayPtrPtr == NIL(array_t)){
224    *arrayPtrPtr = array_alloc(array_t *, 0);
225  }
226  array_insert_last(array_t *, *arrayPtrPtr, symCube);
227}
228
229/**Function********************************************************************
230
231  Synopsis    [Reads in .inputs constructs.]
232
233  Description [Reads in .inputs constructs. Returns 1 if success. 0 otherwise.]
234
235  SideEffects []
236
237  SeeAlso     []
238
239******************************************************************************/
240boolean
241IoInputProcess(
242  Hrc_Node_t *hnode, 
243  char *name)
244{
245  return _IoIOProcess(hnode, name, 0);
246}
247
248/**Function********************************************************************
249
250  Synopsis    [Reads in .outputs constructs.]
251
252  Description [Reads in .outputs constructs. Returns 1 if success. 0 otherwise.]
253
254  SideEffects []
255
256  SeeAlso     []
257
258******************************************************************************/
259boolean
260IoOutputProcess(
261  Hrc_Node_t *hnode, 
262  char *name)
263{
264  return _IoIOProcess(hnode, name, 1);
265}
266
267/**Function********************************************************************
268
269  Synopsis    [Reads in .mv constructs.]
270
271  Description [Reads in .mv constructs. Returns 1 if success. 0 otherwise.]
272
273  SideEffects []
274
275  SeeAlso     []
276
277******************************************************************************/
278boolean
279IoMvProcess(
280  Hrc_Model_t *model, 
281  Hrc_Node_t *hnode, 
282  array_t *varNames, 
283  int range, 
284  array_t *symValues)
285{
286  int i;
287  char *varName;
288  Var_Variable_t *var;
289
290  if (range == -1) {
291    error_append("Invalid .mv specification of ");
292    error_append(array_fetch(char *,varNames,0));
293    error_append(" in model ");
294    error_append(Hrc_ModelReadName(model));
295    error_append("\n- Range should be an integer.\n");
296    return 0;
297  }
298
299  for (i=0; i < array_n(varNames); i++){
300    varName = array_fetch(char *,varNames,i);
301    if ((var = Hrc_NodeFindVariableByName(hnode,varName)) != NIL(Var_Variable_t)) {
302      if (Var_VariableAddRangeInfo(var,range,symValues) == 0){
303        error_append("Invalid .mv specification of ");
304        error_append(varName);
305        error_append(" in model ");
306        error_append(Hrc_ModelReadName(model));
307        error_append("\n- You are trying to redefine the above variable.\n");
308        return 0;
309      }
310    }
311    else {
312      var = Var_VariableAlloc(hnode,varName);
313      (void)Var_VariableAddRangeInfo(var,range,symValues);
314    }
315  }
316  return 1;
317}
318
319/**Function********************************************************************
320
321  Synopsis    [Reads in .latch constructs.]
322
323  Description [Reads in .latch constructs. Returns 1 if success. 0 otherwise.]
324
325  SideEffects []
326
327  SeeAlso     []
328
329******************************************************************************/
330boolean
331IoLatchProcess(
332  Hrc_Model_t *model,
333  Hrc_Node_t *hnode, 
334  char *latchInput,
335  char *latchOutput)
336{
337  Var_Variable_t *varIn, *varOut;
338
339  varIn = IoVariableFindOrAllocByName(hnode,latchInput);
340  varOut = IoVariableFindOrAllocByName(hnode,latchOutput);
341  if (varIn == NIL(Var_Variable_t) || varOut == NIL(Var_Variable_t)){
342    return 0;
343  }
344  if ((Hrc_LatchCreate(model,varIn,varOut)) == NIL(Hrc_Latch_t)){
345    return 0; 
346  }
347  (void)Var_VariableSetNS(varIn);
348  /* a latch can have only one fanin */
349  if (Var_VariableSetPS(varOut) == 0){
350    error_append("Variable ");
351    error_append(latchOutput);
352    error_append(" are fed by more than one latch\n");
353    return 0;
354  }
355  return 1;
356}
357
358/**Function********************************************************************
359
360  Synopsis    [Reads in .table constructs.]
361
362  Description [Reads in .table constructs. Returns 1 if success. 0 otherwise.]
363
364  SideEffects []
365
366  SeeAlso     []
367
368******************************************************************************/
369boolean
370IoTableProcess(
371  Hrc_Model_t *model,
372  Hrc_Node_t *hnode,
373  array_t *inputArray,
374  array_t *outputArray,
375  array_t *defaultArray,
376  array_t *symCubeArray)
377{
378  IoPTable_t *pTable;
379  Tbl_Table_t *table;
380
381  pTable = _IoPTableCreate(inputArray,outputArray,defaultArray,symCubeArray);
382
383  if ((table = IoPTableTransformToTable(model, hnode, pTable)) == NIL(Tbl_Table_t)){
384    _IoPTableFree(pTable);
385    return 0;
386  }
387  _IoPTableFree(pTable);
388  Hrc_ModelAddNameTable(model,table);
389  return 1;
390}
391
392/**Function********************************************************************
393
394  Synopsis    [Reads in .reset constructs.]
395
396  Description [Reads in .reset constructs. Returns 1 if success. 0 otherwise.]
397
398  SideEffects []
399
400  SeeAlso     []
401
402******************************************************************************/
403boolean
404IoResetProcess(
405  array_t **resetArray,
406  Hrc_Model_t *model, 
407  Hrc_Node_t *hnode, 
408  array_t *inputArray, 
409  array_t *outputArray, 
410  array_t *defaultArray, 
411  array_t *symCubeArray)
412{
413  IoPTable_t *pTable;
414  Tbl_Table_t *table;
415
416  pTable = _IoPTableCreate(inputArray,outputArray,defaultArray,symCubeArray);
417  if ((table = IoPTableTransformToTable(model,hnode,pTable)) == NIL(Tbl_Table_t)){
418    _IoPTableFree(pTable);
419    return 0;
420  }
421  _IoPTableFree(pTable);
422
423  if (*resetArray == NIL(array_t)){
424    *resetArray = array_alloc(Tbl_Table_t *,0);
425  }
426  array_insert_last(Tbl_Table_t *,*resetArray,table);
427  return 1;
428}
429
430
431/**Function********************************************************************
432
433  Synopsis    [Reads in .subckt constructs.]
434
435  Description [Reads in .subckt constructs. Returns 1 if success. 0 otherwise.]
436
437  SideEffects []
438
439  SeeAlso     []
440
441******************************************************************************/
442boolean
443IoSubcktProcess(
444  array_t **subcktArray,
445  Hrc_Model_t *model, 
446  Hrc_Node_t *hnode, 
447  char *modelName, 
448  char *instanceName, 
449  array_t *formalNameArray, 
450  array_t *actualNameArray)
451{
452  int i;
453  char *actual;
454  Var_Variable_t *var;
455
456  for (i=0; i < array_n(actualNameArray); i++){
457    actual = array_fetch(char *,actualNameArray,i);
458    var = IoVariableFindOrAllocByName(hnode,actual);
459    if (var == NIL(Var_Variable_t)){
460      error_append("Problem in defining variable ");
461      error_append(actual);
462      error_append(" found in subckt ");
463      error_append(instanceName);
464      error_append(" in model ");
465      error_append(Hrc_ModelReadName(model));
466      error_append("\n");
467      return 0;
468    }
469  }
470
471  _IoParserAddSubckt(subcktArray,modelName,instanceName,formalNameArray,actualNameArray);
472  return 1;
473}
474
475
476/*---------------------------------------------------------------------------*/
477/* Definition of static functions                                            */
478/*---------------------------------------------------------------------------*/
479
480/**Function********************************************************************
481
482  Synopsis    [Add subckt information to the data structure where the parser
483  maintains all the subckt data.]
484
485  Description []
486
487  SideEffects []
488
489  SeeAlso     []
490
491******************************************************************************/
492static void
493_IoParserAddSubckt(
494  array_t **subcktArray,
495  char *modelName,
496  char *instanceName,
497  array_t *formalNameArray,
498  array_t *actualNameArray)
499{
500  IoSubckt_t *subckt;
501
502  subckt = ALLOC(IoSubckt_t,1);
503  subckt->modelName = modelName;
504  subckt->instanceName = instanceName;
505  subckt->formalNameArray = formalNameArray;
506  subckt->actualNameArray = actualNameArray;
507
508  if (*subcktArray == NIL(array_t)){
509    *subcktArray = array_alloc(IoSubckt_t *,0);
510  }
511  array_insert_last(IoSubckt_t *, *subcktArray, subckt);
512}
513
514/**Function********************************************************************
515
516  Synopsis    [Process .inputs and .outputs construct.]
517
518  Description [Process .inputs and .outputs construct. Returns 1 if success.
519  Otherwise returns 0.]
520
521  SideEffects []
522
523  SeeAlso     []
524
525******************************************************************************/
526
527static int
528_IoIOProcess(
529  Hrc_Node_t *hnode,
530  char *name,
531  int type)
532{
533  Var_Variable_t *var;
534
535  if (Hrc_NodeFindVariableByName(hnode, name) != NIL(Var_Variable_t)) {
536    error_append("Variable ");
537    error_append(name);
538    error_append(" is tried to be set to PI and PO in model ");
539    error_append(Hrc_NodeReadModelName(hnode));
540    error_append("\n");
541    return 0;
542  }
543  var = Var_VariableAlloc(hnode,name);
544  switch(type){
545    case 0:  /* PI */
546      Hrc_NodeAddFormalInput(hnode,var);
547      /* the following is now a part of Hrc_NodeAddFormalInput */
548      /*(void)Var_VariableSetPI(var);*/
549      break;
550    case 1:  /* PO */
551      Hrc_NodeAddFormalOutput(hnode,var);
552      /* the following is now a part of Hrc_NodeAddFormalOutput */
553      /*(void)Var_VariableSetPO(var);*/
554      break;
555    default:
556      fail("Strange argument is given to variable type in IOProcess\n");
557  }
558  return 1;
559}
560
561
562/**Function********************************************************************
563
564  Synopsis    [Allocates a temporary data structure for parsing tables.]
565
566  Description []
567
568  SideEffects []
569
570  SeeAlso     []
571
572******************************************************************************/
573static IoPTable_t *
574_IoPTableCreate(
575  array_t *inputArray,
576  array_t *outputArray,
577  array_t *defaultArray,
578  array_t *symCubeArray)
579{
580  IoPTable_t *pTable;
581
582  pTable = ALLOC(IoPTable_t,1);
583  if (inputArray == NIL(array_t)){
584    pTable->inputs = array_alloc(char *,0);
585  }
586  else {
587    pTable->inputs = inputArray;
588  }
589  assert(outputArray != NIL(array_t));
590  pTable->outputs = outputArray;
591  pTable->defaultOutput = defaultArray;
592  pTable->cubes = symCubeArray;
593
594  return pTable;
595}
596
597/**Function********************************************************************
598
599  Synopsis    [Frees a temporary data structure for parsing the tables.]
600
601  Description []
602
603  SideEffects []
604
605  SeeAlso     []
606
607******************************************************************************/
608
609static void
610_IoPTableFree(IoPTable_t *pTable)
611{
612  int i;
613  array_t *symValueArray;
614
615  IoStringArrayFree(pTable->inputs);
616  IoStringArrayFree(pTable->outputs);
617  if (pTable->defaultOutput != NIL(array_t)){
618    IoSymValueArrayFree(pTable->defaultOutput);
619  }
620  if (pTable->cubes != NIL(array_t)){
621    for (i=0; i < array_n(pTable->cubes); i++){
622      symValueArray = array_fetch(array_t *,pTable->cubes,i);
623      IoSymValueArrayFree(symValueArray);
624    }
625    array_free(pTable->cubes);
626  }
627  FREE(pTable);
628}
629
630
631
632
633
634
635
636
637
638
639
Note: See TracBrowser for help on using the repository browser.