adding port parsers

This commit is contained in:
tangxifan 2019-08-09 17:48:55 -06:00
parent f80e58c753
commit 2c7d6e3de4
6 changed files with 278 additions and 11 deletions

View File

@ -63,6 +63,7 @@ class Point {
public: //Mutators
void set_x(T x_val);
void set_y(T y_val);
void swap();
private:
T x_;
T y_;

View File

@ -52,6 +52,12 @@ namespace vtr {
y_ = y_val;
}
template<class T>
void Point<T>::swap() {
std::swap(x_, y_);
}
/*
* Rect
*/

View File

@ -0,0 +1,142 @@
/**********************************************************
* MIT License
*
* Copyright (c) 2018 LNIS - The University of Utah
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
***********************************************************************/
/************************************************************************
* Filename: port_parser.cpp
* Created by: Xifan Tang
* Change history:
* +-------------------------------------+
* | Date | Author | Notes
* +-------------------------------------+
* | 2019/08/09 | Xifan Tang | Created
* +-------------------------------------+
***********************************************************************/
/************************************************************************
* Member functions for PortParser class
***********************************************************************/
#include "string.h"
#include "vtr_assert.h"
#include "vtr_geometry.h"
#include "string_token.h"
#include "port_parser.h"
/************************************************************************
* Constructors
***********************************************************************/
PortParser::PortParser (const std::string& data) {
set_data(data);
set_default_bracket();
set_default_delim();
parse();
}
/************************************************************************
* Public Accessors
***********************************************************************/
/* Get the data string */
std::string PortParser::data() const {
return data_;
}
/************************************************************************
* Public Mutators
***********************************************************************/
void PortParser::set_data(const std::string& data) {
data_ = data;
parse();
return;
}
/************************************************************************
* Internal Mutators
***********************************************************************/
/* Parse the data */
void PortParser::parse() {
/* Create a tokenizer */
StringToken tokenizer(data_);
/* Split the data into <port_name> and <pin_string> */
std::vector<std::string> port_tokens = tokenizer.split(bracket_.x());
/* Make sure we have a port name! */
VTR_ASSERT_SAFE ((1 == port_tokens.size()) || (2 == port_tokens.size()));
/* Store the port name! */
port_name_ = port_tokens[0];
/* If we only have one token */
if (1 == port_tokens.size()) {
pin_range_.set_x(-1);
pin_range_.set_y(-1);
return; /* We can finish here */
}
/* Chomp the ']' */
tokenizer.set_data(port_tokens[1]);
std::vector<std::string> pin_tokens = tokenizer.split(bracket_.y());
/* Make sure we have a valid string! */
VTR_ASSERT_SAFE (1 == port_tokens.size());
/* Split the pin string now */
tokenizer.set_data(port_tokens[0]);
pin_tokens = tokenizer.split(delim_);
/* Check if we have LSB and MSB or just one */
if ( 1 == pin_tokens.size() ) {
/* Single pin */
pin_range_.set_x(stoi(pin_tokens[0]));
pin_range_.set_y(stoi(pin_tokens[0]));
} else if ( 2 == pin_tokens.size() ) {
/* A number of pin */
pin_range_.set_x(stoi(pin_tokens[0]));
pin_range_.set_y(stoi(pin_tokens[1]));
}
/* Reorder to ensure LSB <= MSB */
if (pin_range_.x() > pin_range_.y()) {
pin_range_.swap();
}
return;
}
void PortParser::set_default_bracket() {
bracket_.set_x('[');
bracket_.set_y(']');
return;
}
void PortParser::set_default_delim() {
delim_ = ':';
return;
}
/************************************************************************
* End of file : port_parser.cpp
***********************************************************************/

View File

@ -0,0 +1,93 @@
/**********************************************************
* MIT License
*
* Copyright (c) 2018 LNIS - The University of Utah
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
***********************************************************************/
/************************************************************************
* Filename: port_parser.h
* Created by: Xifan Tang
* Change history:
* +-------------------------------------+
* | Date | Author | Notes
* +-------------------------------------+
* | 2019/08/09 | Xifan Tang | Created
* +-------------------------------------+
***********************************************************************/
/* IMPORTANT:
* The following preprocessing flags are added to
* avoid compilation error when this headers are included in more than 1 times
*/
#ifndef PORT_PARSER_H
#define PORT_PARSER_H
/*
* Notes in include header files in a head file
* Only include the neccessary header files
* that is required by the data types in the function/class declarations!
*/
/* Header files should be included in a sequence */
/* Standard header files required go first */
#include <string>
#include <vector>
#include "string_token.h"
/************************************************************************
* This file includes parsers for port definition in the architecture XML
* language. Note that it is also compatiable to Verilog syntax.
* It means we may reuse this for constructing a structural Verilog parser
* Supported port definition:
* 1. <port_name>[<LSB>:<MSB>]
* 2. <port_name>[<MSB>:<LSB>]
* 3. <port_name>[<single_pin_index>]
* 4. <port_name>[]
* 5. <port_name>
* In case 4 and 5, we will assign (-1,-1) for LSB and MSB
***********************************************************************/
class PortParser{
public : /* Constructors*/
PortParser (const std::string& data);
public : /* Public Accessors */
std::string data() const;
std::vector<std::string> split();
public : /* Public Mutators */
void set_data(const std::string& data);
private : /* Private Mutators */
void parse();
void set_default_bracket();
void set_default_delim();
private: /* Internal data */
std::string data_; /* Lines to be splited */
vtr::Point<char> bracket_;
char delim_;
std::string port_name_;
vtr::Point<size_t> pin_range_;
};
#endif
/************************************************************************
* End of file : port_parser.h
***********************************************************************/

View File

@ -58,20 +58,11 @@ std::string StringToken::data() const {
return data_;
}
/* Split the string */
std::vector<std::string> StringToken::split() {
/* Split the string using a given delim */
std::vector<std::string> StringToken::split(const std::string& delims) const {
/* Return vector */
std::vector<std::string> ret;
/* Add a default delim */
if (true == delims_.empty()) {
add_default_delim();
}
/* Create delims */
std::string delims;
for (const auto& delim : delims_) {
delims.push_back(delim);
}
/* Get a writable char array */
char* tmp = new char[data_.size() + 1];
std::copy(data_.begin(), data_.end(), tmp);
@ -92,6 +83,37 @@ std::vector<std::string> StringToken::split() {
return ret;
}
/* Split the string using a given delim */
std::vector<std::string> StringToken::split(const char& delim) const {
/* Create delims */
std::string delims(1, delim);
return split(delims);
}
/* Split the string using a given delim */
std::vector<std::string> StringToken::split(const char* delim) const {
/* Create delims */
std::string delims(delim);
return split(delims);
}
/* Split the string */
std::vector<std::string> StringToken::split() {
/* Add a default delim */
if (true == delims_.empty()) {
add_default_delim();
}
/* Create delims */
std::string delims;
for (const auto& delim : delims_) {
delims.push_back(delim);
}
return split(delims);
}
/************************************************************************
* Public Mutators
***********************************************************************/

View File

@ -62,6 +62,9 @@ class StringToken {
StringToken (const std::string& data);
public : /* Public Accessors */
std::string data() const;
std::vector<std::string> split(const std::string& delims) const;
std::vector<std::string> split(const char& delim) const;
std::vector<std::string> split(const char* delim) const;
std::vector<std::string> split();
public : /* Public Mutators */
void set_data(const std::string& data);