Commit ffd77ffe by Alan Mishchenko

Improvements to word-level Verilog parser.

parent 43ee0cff
......@@ -42,34 +42,37 @@ static char * Wlc_Names[WLC_OBJ_NUMBER+1] = {
">>>", // 10: shift right (arithmetic)
"<<", // 11: shift left
"<<<", // 12: shift left (arithmetic)
"~", // 13: bitwise NOT
"&", // 14: bitwise AND
"|", // 15: bitwise OR
"^", // 16: bitwise XOR
"[:]", // 17: bit selection
"{,}", // 18: bit concatenation
"BitPad", // 19: zero padding
"SgnExt", // 20: sign extension
"!", // 21: logic NOT
"&&", // 22: logic AND
"||", // 23: logic OR
"==", // 24: compare equal
"!=", // 25: compare not equal
"<", // 26: compare less
">", // 27: compare more
"<=", // 28: compare less or equal
">=", // 29: compare more or equal
"&", // 30: reduction AND
"|", // 31: reduction OR
"^", // 32: reduction XOR
"+", // 33: arithmetic addition
"-", // 34: arithmetic subtraction
"*", // 35: arithmetic multiplier
"//", // 36: arithmetic division
"%%", // 37: arithmetic modulus
"**", // 38: arithmetic power
"table", // 39: lookup table
NULL // 40: unused
"rotateR", // 13: shift left (arithmetic)
"rotateL", // 14: shift left (arithmetic)
"~", // 15: bitwise NOT
"&", // 16: bitwise AND
"|", // 17: bitwise OR
"^", // 18: bitwise XOR
"[:]", // 19: bit selection
"{,}", // 20: bit concatenation
"bitPad", // 21: zero padding
"signExtend", // 22: sign extension
"!", // 23: logic NOT
"&&", // 24: logic AND
"||", // 25: logic OR
"==", // 26: compare equal
"!=", // 27: compare not equal
"<", // 28: compare less
">", // 29: compare more
"<=", // 30: compare less or equal
">=", // 31: compare more or equal
"&", // 32: reduction AND
"|", // 33: reduction OR
"^", // 34: reduction XOR
"+", // 35: arithmetic addition
"-", // 36: arithmetic subtraction
"*", // 37: arithmetic multiplier
"//", // 38: arithmetic division
"%%", // 39: arithmetic modulus
"**", // 40: arithmetic power
"-", // 41: arithmetic minus
"table", // 42: bit table
NULL // 43: unused
};
////////////////////////////////////////////////////////////////////////
......
......@@ -409,6 +409,7 @@ static inline char * Wlc_PrsReadConstant( Wlc_Prs_t * p, char * pStr, Vec_Int_t
*pRange = Abc_Base2Log( Number );
while ( Wlc_PrsIsDigit(pStr) )
pStr++;
Vec_IntFill( vFanins, 1, Number );
return pStr;
}
pStr = Wlc_PrsFindSymbol( pStr, '\'' );
......@@ -424,7 +425,7 @@ static inline char * Wlc_PrsReadConstant( Wlc_Prs_t * p, char * pStr, Vec_Int_t
if ( nDigits != (nBits + 3)/4 )
{
// return (char *)(ABC_PTRINT_T)Wlc_PrsWriteErrorMessage( p, pStr, "The length of a constant does not match." );
printf( "Warning: The length of a constant (%d hex digits) does not match the number of bits (%d).\n", nDigits, nBits );
// printf( "Warning: The length of a constant (%d hex digits) does not match the number of bits (%d).\n", nDigits, nBits );
}
*pRange = nBits;
pStr += 2;
......@@ -452,7 +453,7 @@ static inline char * Wlc_PrsReadName( Wlc_Prs_t * p, char * pStr, Vec_Int_t * vF
Wlc_ObjAddFanins( p->pNtk, Wlc_NtkObj(p->pNtk, iObj), vFanins );
Vec_IntFree( vFanins );
// add node's name
sprintf( Buffer, "const%d", p->nConsts++ );
sprintf( Buffer, "_c%d_", p->nConsts++ );
NameId = Abc_NamStrFindOrAdd( p->pNtk->pManName, Buffer, &fFound );
if ( fFound )
return (char *)(ABC_PTRINT_T)Wlc_PrsWriteErrorMessage( p, pStr, "Name %s is already used.", Buffer );
......@@ -530,7 +531,7 @@ static inline int Wlc_PrsFindDefinition( Wlc_Prs_t * p, char * pStr, Vec_Int_t *
if ( !(pStr = Wlc_PrsReadName(p, pStr, vFanins)) )
return Wlc_PrsWriteErrorMessage( p, pStr, "Cannot read name after !." );
}
else if ( pStr[0] == '&' || pStr[0] == '|' || pStr[0] == '^' )
else if ( pStr[0] == '&' || pStr[0] == '|' || pStr[0] == '^' || pStr[0] == '-' )
{
if ( pStr[0] == '&' )
Type = WLC_OBJ_REDUCT_AND;
......@@ -538,9 +539,11 @@ static inline int Wlc_PrsFindDefinition( Wlc_Prs_t * p, char * pStr, Vec_Int_t *
Type = WLC_OBJ_REDUCT_OR;
else if ( pStr[0] == '^' )
Type = WLC_OBJ_REDUCT_XOR;
else if ( pStr[0] == '-' )
Type = WLC_OBJ_ARI_MINUS;
else assert( 0 );
if ( !(pStr = Wlc_PrsReadName(p, pStr+1, vFanins)) )
return Wlc_PrsWriteErrorMessage( p, pStr, "Cannot read name after reduction operator." );
return Wlc_PrsWriteErrorMessage( p, pStr, "Cannot read name after a unary operator." );
}
else if ( pStr[0] == '{' )
{
......@@ -603,9 +606,9 @@ static inline int Wlc_PrsFindDefinition( Wlc_Prs_t * p, char * pStr, Vec_Int_t *
}
else
{
if ( pStr[0] == '>' && pStr[1] == '>' && pStr[2] != '>' ) pStr += 2, Type = WLC_OBJ_SHIFT_R;
if ( pStr[0] == '>' && pStr[1] == '>' && pStr[2] != '>' ) pStr += 2, Type = fRotating ? WLC_OBJ_ROTATE_R : WLC_OBJ_SHIFT_R;
else if ( pStr[0] == '>' && pStr[1] == '>' && pStr[2] == '>' ) pStr += 3, Type = WLC_OBJ_SHIFT_RA;
else if ( pStr[0] == '<' && pStr[1] == '<' && pStr[2] != '<' ) pStr += 2, Type = WLC_OBJ_SHIFT_L;
else if ( pStr[0] == '<' && pStr[1] == '<' && pStr[2] != '<' ) pStr += 2, Type = fRotating ? WLC_OBJ_ROTATE_L : WLC_OBJ_SHIFT_L;
else if ( pStr[0] == '<' && pStr[1] == '<' && pStr[2] == '<' ) pStr += 3, Type = WLC_OBJ_SHIFT_LA;
else if ( pStr[0] == '&' && pStr[1] != '&' ) pStr += 1, Type = WLC_OBJ_BIT_AND;
else if ( pStr[0] == '|' && pStr[1] != '|' ) pStr += 1, Type = WLC_OBJ_BIT_OR;
......@@ -613,7 +616,7 @@ static inline int Wlc_PrsFindDefinition( Wlc_Prs_t * p, char * pStr, Vec_Int_t *
else if ( pStr[0] == '&' && pStr[1] == '&' ) pStr += 2, Type = WLC_OBJ_LOGIC_AND;
else if ( pStr[0] == '|' && pStr[1] == '|' ) pStr += 2, Type = WLC_OBJ_LOGIC_OR;
else if ( pStr[0] == '=' && pStr[1] == '=' ) pStr += 2, Type = WLC_OBJ_COMP_EQU;
else if ( pStr[0] == '!' && pStr[1] == '=' ) pStr += 2, Type = WLC_OBJ_COMP_NOT;
else if ( pStr[0] == '!' && pStr[1] == '=' ) pStr += 2, Type = WLC_OBJ_COMP_NOTEQU;
else if ( pStr[0] == '<' && pStr[1] != '=' ) pStr += 1, Type = WLC_OBJ_COMP_LESS;
else if ( pStr[0] == '>' && pStr[1] != '=' ) pStr += 1, Type = WLC_OBJ_COMP_MORE;
else if ( pStr[0] == '<' && pStr[1] == '=' ) pStr += 2, Type = WLC_OBJ_COMP_LESSEQU;
......@@ -702,6 +705,7 @@ int Wlc_PrsDerive( Wlc_Prs_t * p )
if ( Wlc_PrsStrCmp( pName, "table" ) )
{
// THIS IS A HACK TO DETECT tables
int Width1, Width2;
int v, b, Value, nBits, nInts, * pTable;
Vec_Int_t * vValues = Vec_IntAlloc( 256 );
Wlc_PrsForEachLineStart( p, pStart, i, i+1 )
......@@ -711,9 +715,11 @@ int Wlc_PrsDerive( Wlc_Prs_t * p )
pStart = Wlc_PrsFindSymbol( pStart, '\'' );
if ( pStart == NULL )
continue;
Width1 = atoi(pStart-1);
pStart = Wlc_PrsFindSymbol( pStart+2, '\'' );
if ( pStart == NULL )
continue;
Width2 = atoi(pStart-1);
Value = 0;
Abc_TtReadHexNumber( (word *)&Value, pStart+2 );
Vec_IntPush( vValues, Value );
......@@ -725,14 +731,15 @@ int Wlc_PrsDerive( Wlc_Prs_t * p )
Vec_IntFree( vValues );
return Wlc_PrsWriteErrorMessage( p, pStart, "Cannot read module \"%s\".", pName );
}
assert( Width1 == nBits );
// create bitmap
nInts = Abc_BitWordNum( nBits * Vec_IntSize(vValues) );
nInts = Abc_BitWordNum( Width2 * Vec_IntSize(vValues) );
pTable = (unsigned *)Mem_FlexEntryFetch( p->pMemTable, nInts * sizeof(unsigned) );
memset( pTable, 0, nInts * sizeof(unsigned) );
Vec_IntForEachEntry( vValues, Value, v )
for ( b = 0; b < nBits; b++ )
for ( b = 0; b < Width2; b++ )
if ( (Value >> b) & 1 )
Abc_InfoSetBit( pTable, v * nBits + b );
Abc_InfoSetBit( pTable, v * Width2 + b );
Vec_PtrPush( p->vTables, pTable );
Vec_IntFree( vValues );
continue;
......
......@@ -42,6 +42,60 @@ ABC_NAMESPACE_IMPL_START
SeeAlso []
***********************************************************************/
void Wlc_WriteTableOne( FILE * pFile, int nFans, int nOuts, word * pTable, int Id )
{
int m, nMints = (1<<nFans);
// Abc_TtPrintHexArrayRev( stdout, pTable, nMints ); printf( "\n" );
assert( nOuts > 0 && nOuts <= 64 && (64 % nOuts) == 0 );
fprintf( pFile, "module table%d(ind, val);\n", Id );
fprintf( pFile, " input [%d:0] ind;\n", nFans-1 );
fprintf( pFile, " output [%d:0] val;\n", nOuts-1 );
fprintf( pFile, " reg [%d:0] val;\n", nOuts-1 );
fprintf( pFile, " always @(ind)\n" );
fprintf( pFile, " begin\n" );
fprintf( pFile, " case (ind)\n" );
for ( m = 0; m < nMints; m++ )
fprintf( pFile, " %d\'h%x: val = %d\'h%x;\n", nFans, m, nOuts, (pTable[(nOuts * m) >> 6] >> ((nOuts * m) & 63)) & Abc_Tt6Mask(nOuts) );
fprintf( pFile, " endcase\n" );
fprintf( pFile, " end\n" );
fprintf( pFile, "endmodule\n" );
fprintf( pFile, "\n" );
}
void Wlc_WriteTables( FILE * pFile, Wlc_Ntk_t * p )
{
Vec_Int_t * vNodes;
Wlc_Obj_t * pObj, * pFanin;
word * pTable;
int i;
if ( p->vTables == NULL || Vec_PtrSize(p->vTables) == 0 )
return;
// map tables into their nodes
vNodes = Vec_IntStart( Vec_PtrSize(p->vTables) );
Wlc_NtkForEachObj( p, pObj, i )
if ( pObj->Type == WLC_OBJ_TABLE )
Vec_IntWriteEntry( vNodes, Wlc_ObjTableId(pObj), i );
// write tables
Vec_PtrForEachEntry( word *, p->vTables, pTable, i )
{
pObj = Wlc_NtkObj( p, Vec_IntEntry(vNodes, i) );
assert( pObj->Type == WLC_OBJ_TABLE );
pFanin = Wlc_ObjFanin0( p, pObj );
Wlc_WriteTableOne( pFile, Wlc_ObjRange(pFanin), Wlc_ObjRange(pObj), pTable, i );
}
Vec_IntFree( vNodes );
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Wlc_WriteVerIntVec( FILE * pFile, Wlc_Ntk_t * p, Vec_Int_t * vVec, int Start )
{
char * pName;
......@@ -80,23 +134,50 @@ void Wlc_WriteVerInt( FILE * pFile, Wlc_Ntk_t * p )
if ( Wlc_NtkPoNum(p) > 0 )
Wlc_WriteVerIntVec( pFile, p, &p->vPos, 3 );
fprintf( pFile, " );\n" );
// mark fanins of rotation shifts
Wlc_NtkForEachObj( p, pObj, i )
if ( pObj->Type == WLC_OBJ_ROTATE_R || pObj->Type == WLC_OBJ_ROTATE_L )
Wlc_ObjFanin1(p, pObj)->Mark = 1;
Wlc_NtkForEachObj( p, pObj, i )
{
char * pName = Wlc_ObjName(p, i);
char * pName0 = Wlc_ObjFaninNum(pObj) ? Wlc_ObjName(p, Wlc_ObjFaninId0(pObj)) : NULL;
int nDigits = Abc_Base10Log(pObj->End+1) + Abc_Base10Log(pObj->Beg+1);
if ( pObj->Mark )
{
pObj->Mark = 0;
continue;
}
sprintf( Range, "%s[%d:%d]%*s", pObj->Signed ? "signed ":" ", pObj->End, pObj->Beg, 8-nDigits, "" );
fprintf( pFile, " " );
assert( pObj->Type != WLC_OBJ_TABLE );
if ( pObj->Type == WLC_OBJ_PI )
fprintf( pFile, "input wire %s %-16s", Range, pName );
fprintf( pFile, "input wire %s %s", Range, pName );
else if ( pObj->Type == WLC_OBJ_PO )
fprintf( pFile, "output wire %s %-16s = %s", Range, pName, pName0 );
else if ( pObj->Type == WLC_OBJ_TABLE )
{
// wire [3:0] s4972; table0 s4972_Index(s4971, s4972);
fprintf( pFile, " wire %s %s ; table%d s%d_Index(%s, %s)", Range, pName, Wlc_ObjTableId(pObj), i, pName0, pName );
}
else if ( pObj->Type == WLC_OBJ_CONST )
{
fprintf( pFile, " wire %s %-16s = %d\'%sh", Range, pName, Wlc_ObjRange(pObj), pObj->Signed ? "s":"" );
Abc_TtPrintHexArrayRev( pFile, (word *)Wlc_ObjConstValue(pObj), (Wlc_ObjRange(pObj) + 3) / 4 );
}
else if ( pObj->Type == WLC_OBJ_ROTATE_R || pObj->Type == WLC_OBJ_ROTATE_L )
{
// wire [27:0] s4960 = (s57 >> 17) | (s57 << 11);
Wlc_Obj_t * pShift = Wlc_ObjFanin1(p, pObj);
int Num0 = *Wlc_ObjConstValue(pShift);
int Num1 = Wlc_ObjRange(pObj) - Num0;
assert( pShift->Type == WLC_OBJ_CONST );
assert( Num0 > 0 && Num0 < Wlc_ObjRange(pObj) );
fprintf( pFile, " wire %s %-16s = ", Range, Wlc_ObjName(p, i) );
if ( pObj->Type == WLC_OBJ_ROTATE_R )
fprintf( pFile, "(%s >> %d) | (%s << %d)", pName0, Num0, pName0, Num1 );
else
fprintf( pFile, "(%s << %d) | (%s >> %d)", pName0, Num0, pName0, Num1 );
}
else
{
fprintf( pFile, " wire %s %-16s = ", Range, Wlc_ObjName(p, i) );
......@@ -127,7 +208,7 @@ void Wlc_WriteVerInt( FILE * pFile, Wlc_Ntk_t * p )
fprintf( pFile, " %s%s", Wlc_ObjName(p, Wlc_ObjFaninId(pObj, k)), k == Wlc_ObjFaninNum(pObj)-1 ? "":"," );
fprintf( pFile, " }" );
}
else
else
{
fprintf( pFile, "%s ", Wlc_ObjName(p, Wlc_ObjFaninId(pObj, 0)) );
if ( pObj->Type == WLC_OBJ_SHIFT_R )
......@@ -150,7 +231,7 @@ void Wlc_WriteVerInt( FILE * pFile, Wlc_Ntk_t * p )
fprintf( pFile, "||" );
else if ( pObj->Type == WLC_OBJ_COMP_EQU )
fprintf( pFile, "==" );
else if ( pObj->Type == WLC_OBJ_COMP_NOT )
else if ( pObj->Type == WLC_OBJ_COMP_NOTEQU )
fprintf( pFile, "!=" );
else if ( pObj->Type == WLC_OBJ_COMP_LESS )
fprintf( pFile, "<" );
......@@ -191,6 +272,7 @@ void Wlc_WriteVer( Wlc_Ntk_t * p, char * pFileName )
}
fprintf( pFile, "// Benchmark \"%s\" written by ABC on %s\n", p->pName, Extra_TimeStamp() );
fprintf( pFile, "\n" );
Wlc_WriteTables( pFile, p );
Wlc_WriteVerInt( pFile, p );
fprintf( pFile, "\n" );
fclose( pFile );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment