From e7527a47ba9d6cba9c9e72800aa91817ec923616 Mon Sep 17 00:00:00 2001 From: Alan Mishchenko Date: Tue, 25 Sep 2012 16:37:25 -0700 Subject: Cleaned up interfaces of genlib/liberty/supergate reading/writing. --- src/map/mapper/mapperTree.c | 442 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 347 insertions(+), 95 deletions(-) (limited to 'src/map/mapper/mapperTree.c') diff --git a/src/map/mapper/mapperTree.c b/src/map/mapper/mapperTree.c index 6d240c56..62d11c53 100644 --- a/src/map/mapper/mapperTree.c +++ b/src/map/mapper/mapperTree.c @@ -29,12 +29,9 @@ ABC_NAMESPACE_IMPL_START /// DECLARATIONS /// //////////////////////////////////////////////////////////////////////// -static int Map_LibraryReadFileTree( Map_SuperLib_t * pLib, FILE * pFile, char *pFileName ); -static Map_Super_t * Map_LibraryReadGateTree( Map_SuperLib_t * pLib, char * pBuffer, int Number, int nVars ); -static int Map_LibraryDeriveGateInfo( Map_SuperLib_t * pLib, st_table * tExcludeGate ); -static void Map_LibraryAddFaninDelays( Map_SuperLib_t * pLib, Map_Super_t * pGate, Map_Super_t * pFanin, Mio_Pin_t * pPin ); -static int Map_LibraryGetMaxSuperPi_rec( Map_Super_t * pGate ); -static unsigned Map_LibraryGetGateSupp_rec( Map_Super_t * pGate ); +static void Map_LibraryAddFaninDelays( Map_SuperLib_t * pLib, Map_Super_t * pGate, Map_Super_t * pFanin, Mio_Pin_t * pPin ); +static int Map_LibraryGetMaxSuperPi_rec( Map_Super_t * pGate ); +static unsigned Map_LibraryGetGateSupp_rec( Map_Super_t * pGate ); // fanout limits static const int s_MapFanoutLimits[10] = { 1/*0*/, 10/*1*/, 5/*2*/, 2/*3*/, 1/*4*/, 1/*5*/, 1/*6*/ }; @@ -45,7 +42,7 @@ static const int s_MapFanoutLimits[10] = { 1/*0*/, 10/*1*/, 5/*2*/, 2/*3*/, 1/*4 /**Function************************************************************* - Synopsis [Reads the supergate library from file.] + Synopsis [Reads one gate.] Description [] @@ -54,50 +51,89 @@ static const int s_MapFanoutLimits[10] = { 1/*0*/, 10/*1*/, 5/*2*/, 2/*3*/, 1/*4 SeeAlso [] ***********************************************************************/ -int Map_LibraryReadTree( Map_SuperLib_t * pLib, char * pFileName, char * pExcludeFile ) +Map_Super_t * Map_LibraryReadGateTree( Map_SuperLib_t * pLib, char * pBuffer, int Number, int nVarsMax ) { - FILE * pFile; - int Status, num; - Abc_Frame_t * pAbc; - st_table * tExcludeGate = 0; + Map_Super_t * pGate; + char * pTemp; + int i, Num; - // read the beginning of the file - assert( pLib->pGenlib == NULL ); - pFile = Io_FileOpen( pFileName, "open_path", "r", 1 ); -// pFile = fopen( pFileName, "r" ); - if ( pFile == NULL ) + // start and clean the gate + pGate = (Map_Super_t *)Extra_MmFixedEntryFetch( pLib->mmSupers ); + memset( pGate, 0, sizeof(Map_Super_t) ); + + // set the gate number + pGate->Num = Number; + + // read the mark + pTemp = strtok( pBuffer, " " ); + if ( pTemp[0] == '*' ) { - printf( "Cannot open input file \"%s\".\n", pFileName ); - return 0; + pGate->fSuper = 1; + pTemp = strtok( NULL, " " ); } - if ( pExcludeFile ) + // read the root gate + pGate->pRoot = Mio_LibraryReadGateByName( pLib->pGenlib, pTemp, NULL ); + if ( pGate->pRoot == NULL ) { - pAbc = Abc_FrameGetGlobalFrame(); - - tExcludeGate = st_init_table(strcmp, st_strhash); - if ( (num = Mio_LibraryReadExclude( pExcludeFile, tExcludeGate )) == -1 ) + printf( "Cannot read the root gate names %s.\n", pTemp ); + return NULL; + } + // set the max number of fanouts + pGate->nFanLimit = s_MapFanoutLimits[ Mio_GateReadPinNum(pGate->pRoot) ]; + + // read the pin-to-pin delay + for ( i = 0; ( pTemp = strtok( NULL, " \n\0" ) ); i++ ) + { + if ( pTemp[0] == '#' ) + break; + if ( i == nVarsMax ) { - st_free_table( tExcludeGate ); - tExcludeGate = 0; - return 0; + printf( "There are too many entries on the line.\n" ); + return NULL; } + Num = atoi(pTemp); + if ( Num < 0 ) + { + printf( "The number of a child supergate is negative.\n" ); + return NULL; + } + if ( Num > pLib->nLines ) + { + printf( "The number of a child supergate (%d) exceeded the number of lines (%d).\n", + Num, pLib->nLines ); + return NULL; + } + pGate->pFanins[i] = pLib->ppSupers[Num]; + } + pGate->nFanins = i; + if ( pGate->nFanins != (unsigned)Mio_GateReadPinNum(pGate->pRoot) ) + { + printf( "The number of fanins of a root gate is wrong.\n" ); + return NULL; + } - fprintf ( Abc_FrameReadOut( pAbc ), "Read %d gates from exclude file\n", num ); + // save the gate name, just in case + if ( pTemp && pTemp[0] == '#' ) + { + if ( pTemp[1] == 0 ) + pTemp = strtok( NULL, " \n\0" ); + else // skip spaces + for ( pTemp++; *pTemp == ' '; pTemp++ ); + // save the formula + pGate->pFormula = Extra_MmFlexEntryFetch( pLib->mmForms, strlen(pTemp)+1 ); + strcpy( pGate->pFormula, pTemp ); } - - Status = Map_LibraryReadFileTree( pLib, pFile, pFileName ); - fclose( pFile ); - if ( Status == 0 ) - return 0; - // prepare the info about the library - return Map_LibraryDeriveGateInfo( pLib, tExcludeGate ); + // check the rest of the string + pTemp = strtok( NULL, " \n\0" ); + if ( pTemp != NULL ) + printf( "The following trailing symbols found \"%s\".\n", pTemp ); + return pGate; } - /**Function************************************************************* - Synopsis [Reads the library file.] + Synopsis [Reads the supergate library from file.] Description [] @@ -129,7 +165,7 @@ int Map_LibraryReadFileTree( Map_SuperLib_t * pLib, FILE * pFile, char *pFileNam pLib->pGenlib = Abc_FrameReadLibGen(); if ( pLib->pGenlib == NULL || strcmp( Mio_LibraryReadName(pLib->pGenlib), pLibName ) ) { - printf( "Supergate library \"%s\" requires the use of Genlib library \"%s\".\n", pFileName, pLibName ); + printf( "Supergate library \"%s\" requires the use of genlib library \"%s\".\n", pFileName, pLibName ); return 0; } @@ -224,10 +260,49 @@ int Map_LibraryReadFileTree( Map_SuperLib_t * pLib, FILE * pFile, char *pFileNam pLib->nSupersReal = nCounter; return 1; } +int Map_LibraryReadTree2( Map_SuperLib_t * pLib, char * pFileName, char * pExcludeFile ) +{ + FILE * pFile; + int Status, num; + Abc_Frame_t * pAbc; + st_table * tExcludeGate = 0; + + // read the beginning of the file + assert( pLib->pGenlib == NULL ); + pFile = Io_FileOpen( pFileName, "open_path", "r", 1 ); +// pFile = fopen( pFileName, "r" ); + if ( pFile == NULL ) + { + printf( "Cannot open input file \"%s\".\n", pFileName ); + return 0; + } + + if ( pExcludeFile ) + { + pAbc = Abc_FrameGetGlobalFrame(); + + tExcludeGate = st_init_table(strcmp, st_strhash); + if ( (num = Mio_LibraryReadExclude( pExcludeFile, tExcludeGate )) == -1 ) + { + st_free_table( tExcludeGate ); + tExcludeGate = 0; + return 0; + } + + fprintf ( Abc_FrameReadOut( pAbc ), "Read %d gates from exclude file\n", num ); + } + + Status = Map_LibraryReadFileTree( pLib, pFile, pFileName ); + fclose( pFile ); + if ( Status == 0 ) + return 0; + // prepare the info about the library + return Map_LibraryDeriveGateInfo( pLib, tExcludeGate ); +} /**Function************************************************************* - Synopsis [Reads one gate.] + Synopsis [Similar to fgets.] Description [] @@ -236,87 +311,264 @@ int Map_LibraryReadFileTree( Map_SuperLib_t * pLib, FILE * pFile, char *pFileNam SeeAlso [] ***********************************************************************/ -Map_Super_t * Map_LibraryReadGateTree( Map_SuperLib_t * pLib, char * pBuffer, int Number, int nVarsMax ) +int Vec_StrGets( char * pBuffer, int nBufferSize, Vec_Str_t * vStr, int * pPos ) { + char * pCur; + char * pBeg = Vec_StrArray(vStr) + *pPos; + char * pEnd = Vec_StrArray(vStr) + Vec_StrSize(vStr); + assert( nBufferSize > 1 ); + if ( pBeg == pEnd ) + { + *pBuffer = 0; + return 0; + } + assert( pBeg < pEnd ); + for ( pCur = pBeg; pCur < pEnd; pCur++ ) + { + *pBuffer++ = *pCur; + if ( *pCur == 0 ) + { + *pPos += pCur - pBeg; + return 0; + } + if ( *pCur == '\n' ) + { + *pPos += pCur - pBeg + 1; + *pBuffer = 0; + return 1; + } + if ( pCur - pBeg == nBufferSize-1 ) + { + *pPos += pCur - pBeg + 1; + *pBuffer = 0; + return 1; + } + } + return 0; +} + +/**Function************************************************************* + + Synopsis [] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +int Map_LibraryCompareLibNames( char * pName1, char * pName2 ) +{ + char * p1 = Abc_UtilStrsav( pName1 ); + char * p2 = Abc_UtilStrsav( pName2 ); + int i, RetValue; + for ( i = 0; p1[i]; i++ ) + if ( p1[i] == '>' || p1[i] == '\\' || p1[i] == '/' ) + p1[i] = '/'; + for ( i = 0; p2[i]; i++ ) + if ( p2[i] == '>' || p2[i] == '\\' || p2[i] == '/' ) + p2[i] = '/'; + RetValue = strcmp( p1, p2 ); + ABC_FREE( p1 ); + ABC_FREE( p2 ); + return RetValue; +} + +/**Function************************************************************* + + Synopsis [Reads the supergate library from file.] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +int Map_LibraryReadFileTreeStr( Map_SuperLib_t * pLib, Vec_Str_t * vStr, char * pFileName ) +{ + ProgressBar * pProgress; + char pBuffer[5000]; Map_Super_t * pGate; - char * pTemp; - int i, Num; + char * pTemp = 0, * pLibName; + int nCounter, k, i; + int RetValue, nPos = 0; - // start and clean the gate - pGate = (Map_Super_t *)Extra_MmFixedEntryFetch( pLib->mmSupers ); - memset( pGate, 0, sizeof(Map_Super_t) ); + // skip empty and comment lines +// while ( fgets( pBuffer, 5000, pFile ) != NULL ) + while ( 1 ) + { + RetValue = Vec_StrGets( pBuffer, 5000, vStr, &nPos ); + if ( RetValue == 0 ) + return 0; + // skip leading spaces + for ( pTemp = pBuffer; *pTemp == ' ' || *pTemp == '\r' || *pTemp == '\n'; pTemp++ ); + // skip comment lines and empty lines + if ( *pTemp != 0 && *pTemp != '#' ) + break; + } - // set the gate number - pGate->Num = Number; + pLibName = strtok( pTemp, " \t\r\n" ); + pLib->pGenlib = Abc_FrameReadLibGen(); +// if ( pLib->pGenlib == NULL || strcmp( , pLibName ) ) + if ( pLib->pGenlib == NULL || Map_LibraryCompareLibNames(Mio_LibraryReadName(pLib->pGenlib), pLibName) ) + { + printf( "Supergate library \"%s\" requires the use of genlib library \"%s\".\n", pFileName, pLibName ); + return 0; + } - // read the mark - pTemp = strtok( pBuffer, " " ); - if ( pTemp[0] == '*' ) + // read the number of variables + RetValue = Vec_StrGets( pBuffer, 5000, vStr, &nPos ); + if ( RetValue == 0 ) + return 0; + RetValue = sscanf( pBuffer, "%d\n", &pLib->nVarsMax ); + if ( pLib->nVarsMax < 2 || pLib->nVarsMax > 10 ) { - pGate->fSuper = 1; - pTemp = strtok( NULL, " " ); + printf( "Suspicious number of variables (%d).\n", pLib->nVarsMax ); + return 0; } - // read the root gate - pGate->pRoot = Mio_LibraryReadGateByName( pLib->pGenlib, pTemp, NULL ); - if ( pGate->pRoot == NULL ) + // read the number of gates + RetValue = Vec_StrGets( pBuffer, 5000, vStr, &nPos ); + if ( RetValue == 0 ) + return 0; + RetValue = sscanf( pBuffer, "%d\n", &pLib->nSupersReal ); + if ( pLib->nSupersReal < 1 || pLib->nSupersReal > 10000000 ) { - printf( "Cannot read the root gate names %s.\n", pTemp ); - return NULL; + printf( "Suspicious number of gates (%d).\n", pLib->nSupersReal ); + return 0; } - // set the max number of fanouts - pGate->nFanLimit = s_MapFanoutLimits[ Mio_GateReadPinNum(pGate->pRoot) ]; - // read the pin-to-pin delay - for ( i = 0; ( pTemp = strtok( NULL, " \n\0" ) ); i++ ) + // read the number of lines + RetValue = Vec_StrGets( pBuffer, 5000, vStr, &nPos ); + if ( RetValue == 0 ) + return 0; + RetValue = sscanf( pBuffer, "%d\n", &pLib->nLines ); + if ( pLib->nLines < 1 || pLib->nLines > 10000000 ) { - if ( pTemp[0] == '#' ) - break; - if ( i == nVarsMax ) - { - printf( "There are too many entries on the line.\n" ); - return NULL; - } - Num = atoi(pTemp); - if ( Num < 0 ) + printf( "Suspicious number of lines (%d).\n", pLib->nLines ); + return 0; + } + + // allocate room for supergate pointers + pLib->ppSupers = ABC_ALLOC( Map_Super_t *, pLib->nLines + 10000 ); + + // create the elementary supergates + for ( i = 0; i < pLib->nVarsMax; i++ ) + { + // get a new gate + pGate = (Map_Super_t *)Extra_MmFixedEntryFetch( pLib->mmSupers ); + memset( pGate, 0, sizeof(Map_Super_t) ); + // assign the elementary variable, the truth table, and the delays + pGate->Num = i; + // set the truth table + pGate->uTruth[0] = pLib->uTruths[i][0]; + pGate->uTruth[1] = pLib->uTruths[i][1]; + // set the arrival times of all input to non-existent delay + for ( k = 0; k < pLib->nVarsMax; k++ ) { - printf( "The number of a child supergate is negative.\n" ); - return NULL; + pGate->tDelaysR[k].Rise = pGate->tDelaysR[k].Fall = MAP_NO_VAR; + pGate->tDelaysF[k].Rise = pGate->tDelaysF[k].Fall = MAP_NO_VAR; } - if ( Num > pLib->nLines ) + // set an existent arrival time for rise and fall + pGate->tDelaysR[i].Rise = 0.0; + pGate->tDelaysF[i].Fall = 0.0; + // set the gate + pLib->ppSupers[i] = pGate; + } + + // read the lines + nCounter = pLib->nVarsMax; + pProgress = Extra_ProgressBarStart( stdout, pLib->nLines ); +// while ( fgets( pBuffer, 5000, pFile ) != NULL ) + while ( Vec_StrGets( pBuffer, 5000, vStr, &nPos ) ) + { + for ( pTemp = pBuffer; *pTemp == ' ' || *pTemp == '\r' || *pTemp == '\n'; pTemp++ ); + if ( pTemp[0] == '\0' ) + continue; +// if ( pTemp[0] == 'a' || pTemp[2] == 'a' ) +// { +// pLib->nLines--; +// continue; +// } + + // get the gate + pGate = Map_LibraryReadGateTree( pLib, pTemp, nCounter, pLib->nVarsMax ); + if ( pGate == NULL ) { - printf( "The number of a child supergate (%d) exceeded the number of lines (%d).\n", - Num, pLib->nLines ); - return NULL; + Extra_ProgressBarStop( pProgress ); + return 0; } - pGate->pFanins[i] = pLib->ppSupers[Num]; + pLib->ppSupers[nCounter++] = pGate; + // later we will derive: truth table, delays, area, number of component gates, etc + + // update the progress bar + Extra_ProgressBarUpdate( pProgress, nCounter, NULL ); } - pGate->nFanins = i; - if ( pGate->nFanins != (unsigned)Mio_GateReadPinNum(pGate->pRoot) ) + Extra_ProgressBarStop( pProgress ); + if ( nCounter != pLib->nLines ) + printf( "The number of lines read (%d) is different from what the file says (%d).\n", nCounter, pLib->nLines ); + pLib->nSupersAll = nCounter; + // count the number of real supergates + nCounter = 0; + for ( k = 0; k < pLib->nLines; k++ ) + nCounter += pLib->ppSupers[k]->fSuper; + if ( nCounter != pLib->nSupersReal ) + printf( "The number of gates read (%d) is different what the file says (%d).\n", nCounter, pLib->nSupersReal ); + pLib->nSupersReal = nCounter; + return 1; +} +int Map_LibraryReadTree( Map_SuperLib_t * pLib, char * pFileName, char * pExcludeFile ) +{ + char * pBuffer; + Vec_Str_t * vStr; + int Status, num; + Abc_Frame_t * pAbc; + st_table * tExcludeGate = 0; + + // read the beginning of the file + assert( pLib->pGenlib == NULL ); +// pFile = Io_FileOpen( pFileName, "open_path", "r", 1 ); + pBuffer = Mio_ReadFile( pFileName, 0 ); + if ( pBuffer == NULL ) { - printf( "The number of fanins of a root gate is wrong.\n" ); - return NULL; + printf( "Cannot open input file \"%s\".\n", pFileName ); + return 0; } + vStr = Vec_StrAllocArray( pBuffer, strlen(pBuffer) ); - // save the gate name, just in case - if ( pTemp && pTemp[0] == '#' ) + if ( pExcludeFile ) { - if ( pTemp[1] == 0 ) - pTemp = strtok( NULL, " \n\0" ); - else // skip spaces - for ( pTemp++; *pTemp == ' '; pTemp++ ); - // save the formula - pGate->pFormula = Extra_MmFlexEntryFetch( pLib->mmForms, strlen(pTemp)+1 ); - strcpy( pGate->pFormula, pTemp ); + pAbc = Abc_FrameGetGlobalFrame(); + + tExcludeGate = st_init_table(strcmp, st_strhash); + if ( (num = Mio_LibraryReadExclude( pExcludeFile, tExcludeGate )) == -1 ) + { + st_free_table( tExcludeGate ); + tExcludeGate = 0; + Vec_StrFree( vStr ); + return 0; + } + + fprintf ( Abc_FrameReadOut( pAbc ), "Read %d gates from exclude file\n", num ); } - // check the rest of the string - pTemp = strtok( NULL, " \n\0" ); - if ( pTemp != NULL ) - printf( "The following trailing symbols found \"%s\".\n", pTemp ); - return pGate; + + Status = Map_LibraryReadFileTreeStr( pLib, vStr, pFileName ); + Vec_StrFree( vStr ); + if ( Status == 0 ) + return 0; + // prepare the info about the library + return Map_LibraryDeriveGateInfo( pLib, tExcludeGate ); } + + + + + + /**Function************************************************************* Synopsis [Derives information about the library.] -- cgit v1.2.3