torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ParsePrimitive.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
18 #include <stdarg.h>
19 
20 namespace torc {
21 namespace physical {
22 
23 
24  static const char delimiters[] = " \n\t\f"; /* token delimiters */
25 
27  static const unsigned initial_size = 1024;
28 
29  if (!line) {
30  lineSize = initial_size;
31  line = new char[lineSize];
32  }
33 
34  unsigned offset = 0;
35 
36  for (;;) {
37 
38  if (!fgets(line + offset, lineSize - offset, inFile)) { if
39  (ferror(inFile)) { perror("read error"); exit(1); } if (!offset)
40  strcpy(line, ".end\n"); break; }
41 
42  unsigned len = offset + strlen(line + offset);
43 
44  if (line[len-1] == '\n') {
45  ++lineNumber;
46  if (line[len-2] == '\\') len -= 2; else break;
47  }
48  else {
49  if (len != lineSize - 1) break;
50 
51  char *new_line = new char[lineSize << 1];
52  for (unsigned i = 0; i < lineSize; ++i)
53  new_line[i] = line[i];
54  delete [] line;
55  line = new_line;
56  lineSize <<= 1;
57  }
58 
59  offset = len;
60  }
61 
62 }
63 
64  void ParsePrimitive::printError(const char *format, ...) {
65  va_list args;
66 
67  if (lineNumber)
68  fprintf(stderr, "Parse printError in %s:%d: ", inFileName, lineNumber);
69  else fprintf(stderr, "Parse printError: ");
70 
71  va_start(args, format);
72  vfprintf(stderr, format, args);
73  va_end(args);
74 
75  exit(1);
76  }
77 
79 
80  inFileName = name;
81 
82  lineNumber = 0;
83  if (!(inFile = fopen(inFileName, "r")))
84  printError("Cannot open file '%s'.\n", inFileName);
85  lineNumber = 1;
86 
87  int site_no, pin_no, element_no;
88  string strTmp;
89  string typeTmp;
90  string compName;
91  string element1;
92  string element2;
93  string PrimitivePin1;
94  string PrimitivePin2;
95 
96 
98 
99  readCurrentLine();
100 
101  for (;;) {
102  string token = strtok(line, delimiters);
103  if (token[0] == '(') {
104  if (token == "(xdl_resource_report") {
105  string model = strtok(NULL, delimiters);
106  model = strtok(NULL, delimiters);
108  readCurrentLine();
109  continue;
110  }
111  else
112  if (token =="(tiles") {
113  readCurrentLine();
114  continue;
115  }
116  else
117  if (token =="(tile") {
118  for(int i=0; i<5; ++i)
119  strTmp = strtok(NULL, delimiters);
120  site_no = atoi(strTmp.c_str());
121  for(int i=0; i<site_no; ++i)
122  readCurrentLine();
123  readCurrentLine();
124  continue;
125  }
126  else
127  if (token == "(primitive_defs") {
128  readCurrentLine();
129  continue;
130  }
131  else
132  if (token == "(primitive_def") {
133  compName = strtok(NULL, delimiters);
134  strTmp = strtok(NULL, delimiters);
135  pin_no = atoi (strTmp.c_str());
136  strTmp = strtok(NULL, delimiters);
137  element_no=atoi(strTmp.c_str());
139  p->addPrimitive(pp);
140 
141  for(int i=0; i<pin_no; ++i){
142  readCurrentLine();
143  strTmp = strtok(line, delimiters);
144  strTmp = strtok(NULL, delimiters);
145  compName = strtok(NULL, delimiters);
146  typeTmp = strtok(NULL, delimiters);
147  typeTmp.resize(typeTmp.length() - 1);
148  PrimitivePinSharedPtr newPrimitivePin = RcFactory::newPrimitivePinPtr (strTmp, compName, ((typeTmp == "input") ? InputP : OutputP));
149  pp->addPrimitivePin(newPrimitivePin);
150  }
151 
152  for(int i=0; i<element_no; ++i){
153  readCurrentLine();
154  strTmp = strtok(line, delimiters);
155  compName = strtok(NULL, delimiters);
156  strTmp = strtok(NULL, delimiters);
157  pin_no = atoi(strTmp.c_str());
159  pp->addElement(ep);
160 
161  // read element pins
162  for(int i=0; i<pin_no; ++i){
163  readCurrentLine();
164  strTmp = strtok(line, delimiters);
165  compName = strtok(NULL, delimiters);
166  typeTmp = strtok(NULL, delimiters);
167  typeTmp.resize(typeTmp.length() - 1);
168  PrimitivePinSharedPtr newPrimitivePin = RcFactory::newPrimitivePinPtr (strTmp, compName, ((typeTmp == "input") ? InputP : OutputP));
169  ep->addPrimitivePin(newPrimitivePin);
170  }
171  readCurrentLine();
172  strTmp = strtok(line, delimiters);
173 
174  // read element configs
175  if (strTmp == "(cfg"){
176  for(;;){
177  string confTmp = strtok(NULL, delimiters);
178  if(confTmp[confTmp.size()-1]==')'){
179  confTmp.resize(confTmp.length() - 1);
180  ep->addConfig(confTmp);
181  break;
182  }
183  ep->addConfig(confTmp);
184  }
185  readCurrentLine();
186 
187  strTmp = strtok(line, delimiters);
188  }
189 
190  // read element connections
191  while(strTmp[0]!=')'){
192  element1 = strtok(NULL, delimiters);
193  PrimitivePin1 = strtok(NULL, delimiters);
194  strTmp = strtok(NULL, delimiters);
195  element2 = strtok(NULL, delimiters);
196  PrimitivePin2 = strtok(NULL, delimiters);
197  PrimitivePin2.resize(PrimitivePin2.length() - 1);
198  if(strTmp == "<=="){
199  swap(element1,element2);
200  swap(PrimitivePin1,PrimitivePin2);
201  }
202 
203  ConnectionPin pin1 = RcFactory::newConnectionPin(element1,PrimitivePin1);
204  ConnectionPin pin2 = RcFactory::newConnectionPin(element2,PrimitivePin2);
206  newConn->addConnectionPin(pin1);
207  newConn->addConnectionPin(pin2);
208  ep->addConnection(newConn);
209  readCurrentLine();
210  strTmp = strtok(line, delimiters);
211  }
212 
213  }
214  continue;
215  }
216  else
217  if ((token == "(end") || (token == "(summary")){
218  cout<<'(';
219  for(int i=0; i<3; ++i){
220  strTmp = strtok(NULL, delimiters);
221  cout<<strTmp<<" ";
222  }
223  cout<<endl;
224  break;
225  }
226  }
227  else
228  if (token[0] != '\n' && token[0] != '#' && token[0] != ')') printError("`.' expected.\n");
229 
230  readCurrentLine(); /* get next line */
231  }
232 
233  lineSize = 0;
234  if (line) delete [] line;
235  line = NULL;
236  fclose(inFile);
237 
238  return p;
239 }
240 
241 } // namespace physical
242 } // namespace torc
243 
static PrimitiveSetSharedPtr newPrimitiveSetPtr(const string &inName)
Create and return a new PrimitiveSet object.
Definition: RcFactory.hpp:42
Header for primitive parser.
Header for the RcFactory class.
void readCurrentLine()
Reads the current line.
boost::shared_ptr< Primitive > PrimitiveSharedPtr
Shared pointer encapsulation of a Primitive.
Definition: Primitive.hpp:171
boost::shared_ptr< Element > ElementSharedPtr
Shared pointer encapsulation of a element.
Definition: Element.hpp:119
void printError(const char *format,...)
Print printError.
Physical design connection-pin pair, suitable for specifying a net endpoint.
static const char delimiters[]
boost::shared_ptr< PrimitiveSet > PrimitiveSetSharedPtr
Shared pointer encapsulation of a PrimitiveSet.
static ElementSharedPtr newElementPtr(const string &inName)
Create and return a new Element object.
Definition: RcFactory.hpp:49
static ConnectionPin newConnectionPin(string inElementName, const string &inPinName)
Construct an ConnectionPin and return a constant reference.
Definition: RcFactory.hpp:77
boost::shared_ptr< Connection > ConnectionSharedPtr
Shared pointer encapsulation of a componenet.
Definition: Connection.hpp:82
static ConnectionSharedPtr newConnectionPtr(const string &inName)
Create and return a new Element object.
Definition: RcFactory.hpp:83
PrimitiveSetSharedPtr ParsePrimitiveFile(const char *inFileName)
parse an XDLRC file
boost::shared_ptr< PrimitivePin > PrimitivePinSharedPtr
Shared pointer encapsulation of a componenet.
static PrimitivePinSharedPtr newPrimitivePinPtr(string inElementName, string inPinName, const PinType inType)
Create and return a new PrimitivePin shared pointer.
Definition: RcFactory.hpp:92
static PrimitiveSharedPtr newPrimitivePtr(const string &inName)
Create and return a new Element object.
Definition: RcFactory.hpp:68