Merge branch 'github/Blif', fast blif parser.

This commit is contained in:
Jean-Paul Chaput 2015-04-13 18:55:06 +02:00
commit 2cbb072e6c
1 changed files with 166 additions and 68 deletions

View File

@ -105,15 +105,38 @@ namespace {
string name; string name;
unordered_map<string, Net::Direction> pins; unordered_map<string, Net::Direction> pins;
vector<subckt> subcircuits; vector<subckt> subcircuits;
vector<pair<string, string> > aliases;
bool operator<(model const & o) const{ return name < o.name; } bool operator<(model const & o) const{ return name < o.name; }
}; };
std::vector<std::string> tokenize(std::string const & s){
std::vector<std::string> ret;
auto b_it = s.begin();
while(b_it != s.end()){
// Remove whitespace
while(b_it != s.end() && (*b_it == ' ' or *b_it == '\t'))
++b_it;
// Find the end of the token
auto e_it = b_it;
while(e_it != s.end() && *e_it != ' ' && *e_it != '\t' and *e_it != '#')
++e_it;
// Create the token
if(e_it != b_it)
ret.push_back(std::string(b_it, e_it));
// Handle comments
if(e_it != s.end() && *e_it == '#')
e_it = s.end();
b_it = e_it;
}
std::reverse(ret.begin(), ret.end());
return ret;
}
} // End of anonymous namespace. } // End of anonymous namespace.
namespace CRL { namespace CRL {
// //
// Can only parse simple, netlist BLIF files generated by Yosys // Can only parse simple, netlist BLIF files generated by Yosys
// Ignores all ".names" and uses only the .subckt, .model, .input and .output // Ignores all ".names" and uses only the .subckt, .model, .input and .output
@ -136,15 +159,35 @@ Cell * Blif::load ( string cellPath ) //, Cell *cell )
bool hasName; bool hasName;
string line; string line;
vector<string> current_names;
while(getline(ccell, line)){ while(getline(ccell, line)){
istringstream linestream(line); istringstream linestream(line);
string before_comment; vector<string> tokens = tokenize(line);
getline(linestream, before_comment, '#'); if(not tokens.empty()){
istringstream tokens(before_comment); string const token = tokens.back();
string token; tokens.pop_back();
while(tokens>>token){
assert(not token.empty()); assert(not token.empty());
if(token[0] == '.'){ if(token[0] == '.'){
// Process finished .names statement
if(not current_names.empty()){
if( current_names.size() != 4
or current_names[2] != "1" or current_names[3] != "1"
){
ostringstream mess;
mess << ".names statement is not an alias and will be ignored "
<< "(map to standard cells for functions and tie cells for constants)\n";
for(string const & S: current_names){
mess << S << " ";
}
mess << "\n";
cerr << Warning(mess.str());
}
else{
// Name statement is an alias: the second signal will map to the first
models.back().aliases.push_back(pair<string, string>(current_names[0], current_names[1]));
}
current_names.clear();
}
if(token == ".model"){ if(token == ".model"){
if(state != EXT) if(state != EXT)
throw Error("Nested model are not supported\n"); throw Error("Nested model are not supported\n");
@ -162,13 +205,11 @@ Cell * Blif::load ( string cellPath ) //, Cell *cell )
models.back().subcircuits.push_back(subckt()); models.back().subcircuits.push_back(subckt());
} }
else if(token == ".names"){ else if(token == ".names"){
cerr << Warning("BLIF names are ignored\n");
if(state == EXT) if(state == EXT)
throw Error("Names without an enclosing model are not supported\n"); throw Error("Names without an enclosing model are not supported\n");
if(state == MODEL and not hasName) if(state == MODEL and not hasName)
throw Error("Model has no name\n"); throw Error("Model has no name\n");
state = NAMES; state = NAMES;
hasName = false;
} }
else if(token == ".latch"){ else if(token == ".latch"){
throw Error("Latch constructs are not understood by the parser\n"); throw Error("Latch constructs are not understood by the parser\n");
@ -189,10 +230,28 @@ Cell * Blif::load ( string cellPath ) //, Cell *cell )
state = EXT; state = EXT;
} }
else{ else{
throw Error("Unexpected control token\n"); ostringstream mess;
mess << "Unknown control token <" << token << ">\n";
throw Error(mess.str());
}
}
else if(state == NAMES){
// Part of a cover for a logic function
current_names.push_back(token);
}
else{
ostringstream mess;
mess << "Encountered a non-control token at the beginning of a line: <" << token << ">\n";
throw Error(mess.str());
} }
} }
else{ // Either a pin or an input/output definition // Process all tokens after the control
while(not tokens.empty()){
string const token = tokens.back();
tokens.pop_back();
assert(not token.empty());
// Either a pin or an input/output definition
if(state == INPUTS or state == OUTPUTS){ if(state == INPUTS or state == OUTPUTS){
auto it = models.back().pins.find(token); auto it = models.back().pins.find(token);
Net::Direction D = (state == INPUTS)? Net::Direction::DirIn : Net::Direction::DirOut; Net::Direction D = (state == INPUTS)? Net::Direction::DirIn : Net::Direction::DirOut;
@ -206,28 +265,33 @@ Cell * Blif::load ( string cellPath ) //, Cell *cell )
else if(state == SUBCKT){ else if(state == SUBCKT){
if(hasName){ if(hasName){
// Encountered a pin: need to be processed // Encountered a pin: need to be processed
istringstream token_stream(token); auto equal_pos = token.find_first_of('=');
string before_space, after_space; if(equal_pos+1 < token.size()){
getline(token_stream, before_space, '='); string before_space = token.substr(0, equal_pos);
getline(token_stream, after_space, '='); string after_space = token.substr(equal_pos+1, string::npos);
if(token_stream){
Error("Encountered more than one '=' in token");
}
models.back().subcircuits.back().pins.push_back(pair<string, string>(before_space, after_space)); models.back().subcircuits.back().pins.push_back(pair<string, string>(before_space, after_space));
} }
else{
ostringstream mess;
mess << "Unable to parse the subckt pin specification <"
<< token << ">\n";
Error(mess.str());
}
}
else{ else{
models.back().subcircuits.back().cell = token; models.back().subcircuits.back().cell = token;
hasName = true; hasName = true;
} }
} }
else if(state == NAMES){ else if(state == NAMES){
// TODO; now just ignored current_names.push_back(token);
} }
else if(state == MODEL){ else if(state == MODEL){
if(hasName) if(hasName)
throw Error("Unexpected token after model name\n"); throw Error("Unexpected token after model name\n");
else{ else{
models.back().name = token; models.back().name = token;
cmess2 << "Processing model <" << token << ">" << endl;
hasName = true; hasName = true;
} }
} }
@ -235,7 +299,6 @@ Cell * Blif::load ( string cellPath ) //, Cell *cell )
throw Error("Unexpected token\n"); throw Error("Unexpected token\n");
} }
} }
}
line.clear(); line.clear();
} }
if(state != EXT){ if(state != EXT){
@ -255,13 +318,14 @@ Cell * Blif::load ( string cellPath ) //, Cell *cell )
} }
*/ */
if(models.size() > 1){ // Two passes: first create the cells and their nets, then create the internals
cerr << Warning("Several models in the file; only the last was open\n"); std::vector<Cell*> model_cells;
}
Cell * design = NULL;
for(auto M : models){ for(auto M : models){
design = framework->createCell(M.name); Cell * design = framework->createCell(M.name);
if(design == NULL){
throw Error("Model " + M.name + " is NULL\n");
}
model_cells.push_back(design);
addSupplyNets(design); addSupplyNets(design);
unordered_set<string> net_names; unordered_set<string> net_names;
@ -269,6 +333,10 @@ Cell * Blif::load ( string cellPath ) //, Cell *cell )
for(auto const & P : S.pins){ for(auto const & P : S.pins){
net_names.insert(P.second); net_names.insert(P.second);
} }
for(auto const & A : M.aliases){
net_names.insert(A.first);
net_names.insert(A.second);
}
} }
for(auto const & P : M.pins){ for(auto const & P : M.pins){
net_names.insert(P.first); net_names.insert(P.first);
@ -282,30 +350,60 @@ Cell * Blif::load ( string cellPath ) //, Cell *cell )
new_net->setDirection( it->second ); new_net->setDirection( it->second );
} }
} }
}
int i=0; // Second pass: every cell and its nets have already been created
for(auto & S : M.subcircuits){ for(int i=0; i<models.size(); ++i){
cmess2 << "Handling model " << models[i].name << endl;
auto const & M = models[i];
Cell * design = model_cells[i];
for(int j=0; j<M.subcircuits.size(); ++j){
auto & S = M.subcircuits[j];
ostringstream subckt_name; ostringstream subckt_name;
subckt_name << "subckt_" << i; subckt_name << "subckt_" << j;
Cell * cell = framework->getCell(S.cell, Catalog::State::Views, 0); Cell * cell = framework->getCell(S.cell, Catalog::State::Views, 0);
if(cell == NULL){ if(cell == NULL){
cerr << Warning("Cell " + S.cell + "to instanciate hasn't been found\n"); throw Error("Cell <" + S.cell + "> to instanciate hasn't been found\n");
continue;
} }
//cmess2 << "Creating instance <" << subckt_name.str() << "> of <" << S.cell << "> in <" << models[i].name << ">" << endl;
Instance* instance = Instance::create( design, subckt_name.str(), cell); Instance* instance = Instance::create( design, subckt_name.str(), cell);
for(auto & P : S.pins){ for(auto & P : S.pins){
Net* internalNet = cell->getNet( P.first ); Net* internalNet = cell->getNet( P.first );
Net* externalNet = design->getNet( P.second ); Net* externalNet = design->getNet( P.second );
assert(internalNet != NULL and externalNet != NULL); assert(internalNet != NULL and externalNet != NULL and instance != NULL);
instance->getPlug( internalNet )->setNet( externalNet ); instance->getPlug( internalNet )->setNet( externalNet );
} }
//connectSupplyNets(instance, design); //connectSupplyNets(instance, design);
++i; }
// Merge the aliased nets
for(auto alias : M.aliases){
//cmess2 << "Merging nets <" << alias.first << "> and <" << alias.second << ">" << endl;
Net * first_net = design->getNet( alias.first );
Net * second_net = design->getNet( alias.second );
if(first_net == NULL or second_net == NULL){
ostringstream mess;
mess << "Trying to create an alias for non-instantiated nets:";
if(first_net == NULL)
mess << " <" << alias.first << ">";
if(second_net == NULL)
mess << " <" << alias.second << ">";
mess << ", will be ignored\n";
cerr << Warning(mess.str());
continue;
}
if(!first_net->isExternal())
swap(first_net, second_net); // If only one net is external, it needs to be the first
first_net->merge(second_net);
} }
} }
cmess2 << "BLIF file loaded" << endl;
return design; if(model_cells.empty()){
throw Error("No model found in the file\n");
}
else if(model_cells.size() > 1){
cerr << Warning("Several models found: returned the first one\n");
}
return model_cells[0];
} }
} }