OpenASIP  2.0
ParserStructs.hh
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * @file ParsetStructs.hh
27  *
28  * Structures that assembler parser uses for storing parsed information.
29  *
30  * @author Mikael Lepistö 2005 (tmlepist-no.spam-cs.tut.fi)
31  * @note rating: yellow
32  */
33 
34 #ifndef TCEASM_PARSER_STRUCTS_HH
35 #define TCEASM_PARSER_STRUCTS_HH
36 
37 #include <map>
38 #include <string>
39 #include <vector>
40 #include <sstream>
41 
42 #include "Conversion.hh"
43 
44 typedef unsigned long UValue;
45 typedef long SValue;
46 
47 /// Buffer size.
48 const UValue MAX_VALUE_LENGTH_IN_BYTES = 16; // oversized buffer...
49 
50 /**
51  * Parsed data of one BridgeTerm.
52  */
53 class BusTerm {
54 public:
55  /// Previous or next bus register.
56  bool prev;
57 
58  /**
59  * String representation of term for error message generation.
60  */
61  std::string toString() const {
62  if (prev) {
63  return "{prev}";
64  } else {
65  return "{next}";
66  }
67  }
68 };
69 
70 /**
71  * Parsed data of function unit operand port or
72  * special register reference.
73  *
74  * Form: unit.port[.operation]
75  */
76 class FUTerm {
77 public:
78  /// Is operation part of the term used.
79  bool part3Used;
80  /// Unit name
81  std::string part1;
82  /// Port name.
83  std::string part2;
84  /// Operation name.
85  std::string part3;
86 
87  /**
88  * String representation of term for error message generation.
89  */
90  std::string toString() const {
91  std::string retVal = part1 + "." + part2;
92  if (part3Used) {
93  retVal += "." + part3;
94  }
95  return retVal;
96  }
97 };
98 
99 /**
100  * Parsed data of registerfile index or
101  * function unit operation operand reference.
102  *
103  * Form: rf[.port].index and fu.operation.index
104  */
105 class IndexTerm {
106 public:
107  /// Is port name used.
108  bool part2Used;
109  /// Unit name.
110  std::string part1;
111  /// Port or operation name.
112  std::string part2;
113  /// Register or operand index.
115 
116  /**
117  * String representation of term for error message generation.
118  */
119  std::string toString() const {
120  std::string retVal = part1;
121  if (part2Used) {
122  retVal += "." + part2;
123  }
124  retVal += "." + Conversion::toString(index);
125  return retVal;
126  }
127 };
128 
129 /**
130  * Parsed data of register term
131  *
132  * This is used for presenting any type of port or register reference.
133  *
134  * Register term can be BusTerm, FUTerm or IndexTerm.
135  */
137 public:
138 
139  /**
140  * Register term types.
141  */
142  enum TermType {
143  BUS, ///< Bus term.
144  FUNCTION_UNIT, ///< FU term.
145  INDEX ///< Index term.
146  };
147 
148  /// Type of terminal that is represented by this object.
150 
151  /// The bus term, if type field is BUS. Otherwise not used.
153  /// The fu term, if type field is FUNCTION_UNIT. Otherwise not used.
155  /// The index term, if type field is INDEX. Otherwise not used.
157 
158  /**
159  * String representation of term for error message generation.
160  */
161  std::string toString() const {
162 
163  switch (type) {
164  case BUS:
165  return busTerm.toString();
166  break;
167 
168  case FUNCTION_UNIT:
169  return fuTerm.toString();
170  break;
171 
172  case INDEX:
173  return indexTerm.toString();
174  break;
175 
176  default:
177  return "Unknown register term type";
178  }
179  }
180 };
181 
182 /**
183  * Parsed expression.
184  *
185  * Expression is label with possibly some additional information.
186  *
187  * Form: name[(+|-)offset][=literal]
188  * e.g. myDataStructLabel+8=16, myCodeLabel=3, myDataStructLabel+8 or
189  * myDataStructLabel
190  *
191  * Part after '=' sign is just value of label, if value is always
192  * resolved by compiler and it is also given by user values will
193  * be compared to match.
194  *
195  * Offset can be used for example with structures,
196  * if one want's to refer different datafields.
197  */
198 class Expression {
199 public:
200  /// Name of the label.
201  std::string label;
202 
203  /// Is resolved value defined in struct.
204  bool hasValue;
205  /// Resolved value.
207 
208  /// Is offset defined.
209  bool hasOffset;
210  /// Is offset minus.
211  bool isMinus;
212  /// Value of offset.
214 
215  /**
216  * String representation of term for error message generation.
217  */
218  std::string toString() const {
219 
220  std::stringstream retVal;
221 
222  retVal << label;
223 
224  if (hasOffset) {
225  if (isMinus) {
226  retVal << '-';
227  } else {
228  retVal << '+';
229  }
230 
231  retVal << std::dec << offset;
232  }
233 
234  if (hasValue) {
235  retVal << "=" << std::hex << value;
236  }
237 
238  return retVal.str();
239  }
240 };
241 
242 /**
243  * Parsed literal or expression.
244  */
246 public:
247 
249 
250  /// Does object contain expression or literal.
252  /// If expression the expression, Otherwise not used.
254 
255  /// If literal, the literal. Otherwise not used.
257  /// Sign of the value.
258  bool isSigned;
259 
260  /**
261  * String representation of term for error message generation.
262  */
263  std::string toString() const {
264  std::stringstream retVal;
265  if (isExpression) {
266  retVal << expression.toString();
267  } else {
268  retVal << std::dec << static_cast<int>(value);
269  }
270  return retVal.str();
271  }
272 };
273 
274 /**
275  * Source field of parsed move.
276  *
277  * Source can be either RegisterTerm or Immediate.
278  */
280 public:
281  /// Is source register or immediate reference.
283  /// If register, the register. Otherwise not used.
285  /// If immediate value, the literal or expression. Otherwise not used.
287 
288  /**
289  * String representation of term for error message generation.
290  */
291  std::string toString() const {
292  std::stringstream retVal;
293  if (isRegister) {
294  retVal << regTerm.toString();
295  } else {
296  retVal << immTerm.toString();
297  }
298  return retVal.str();
299  }
300 };
301 
302 /**
303  * Guard field of parsed move.
304  */
305 class ParserGuard {
306 public:
307  /// Is guard used.
308  bool isGuarded;
309  /// Is guard inverted.
311  /// Guard port or register.
313 
314  /**
315  * String representation of term for error message generation.
316  */
317  std::string toString() const {
318  std::stringstream retVal;
319  if (isGuarded) {
320  if (isInverted) {
321  retVal << '!';
322  } else {
323  retVal << '?';
324  }
325 
326  retVal << regTerm.toString();
327  }
328  return retVal.str();
329  }
330 };
331 
332 /**
333  * One init data field of data line.
334  *
335  * Form: [width:]literalOrExpression
336  *
337  * e.g.these are valid 4:codeLabel, 0b1010010, 0x139da 4:0x1abcd -4
338  */
340 public:
341  /// Number of MAUs that are initialized by the init field.
343  /// Initialisation value.
345 
346  /**
347  * String representation of term for error message generation.
348  */
349  std::string toString() const {
350  std::stringstream retVal;
351  retVal << std::dec << (int)width << ":";
352  if (litOrExpr.isExpression) {
353  retVal << std::dec << (litOrExpr.expression.label);
354  } else {
355  retVal << std::dec << (int)(litOrExpr.value);
356  }
357  return retVal.str();
358  }
359 };
360 
361 /**
362  * One annotation.
363  */
364 class Annotation {
365 public:
366  // Id of annotation
368  // Payload
369  std::vector<InitDataField> payload;
370 
371  std::string toString() const {
372  std::stringstream retVal;
373  retVal << "[0x" << std::hex << id;
374 
375  for (unsigned int i = 0; i < payload.size(); i++) {
376  retVal << " " << payload[i].toString();
377  }
378 
379  retVal << "]";
380 
381  return retVal.str();
382  }
383 };
384 
385 /**
386  * All info of one parsed instruction slot.
387  *
388  * Instruction slot defines a move or long immediate. An instruction slot can
389  * also be empty (unused).
390  */
391 class ParserMove {
392 public:
393  /// Types of instruction slots.
394  enum MoveType {
395  EMPTY, ///< Empty move slot.
396  LONG_IMMEDIATE, ///< Encoding of one long immediate slot.
397  TRANSPORT ///< Data transport (move).
398  };
399 
400  /// Type of move.
402 
403  /// Tells whether the slot is the first of the instruction.
404  bool isBegin;
405 
406  /// Guard field.
408  /// Source field.
410  /// Destination field.
412  /// Line number of source code for errors.
414 
415  std::vector<Annotation> annotationes;
416 
417  /**
418  * Empty constructor.
419  *
420  * All values are set by hand.
421  */
423  }
424 
425  /**
426  * Constructor.
427  *
428  * @param aType Type of created move.
429  * @param begin Is first move of an instruction.
430  */
431  ParserMove(MoveType aType, bool begin) :
432  type(aType), isBegin(begin) {
433  }
434 
435  /**
436  * String representation of term for error message generation.
437  */
438  std::string toString() const {
439  std::stringstream retVal;
440  switch (type) {
441  case EMPTY:
442  retVal << "...";
443  break;
444 
445  case LONG_IMMEDIATE:
446  retVal << "[" << destination.toString()
447  << "=" << source.toString()
448  << "]";
449 
450  for (unsigned int i = 0;i < annotationes.size();i++) {
451  retVal << annotationes[i].toString();
452  }
453 
454  break;
455 
456  case TRANSPORT:
457  retVal << guard.toString() << " "
458  << source.toString() << " -> "
459  << destination.toString();
460 
461  for (unsigned int i = 0;i < annotationes.size();i++) {
462  retVal << annotationes[i].toString();
463  }
464 
465  break;
466 
467  default:
468  retVal << "Unknown move type!\n";
469  }
470  return retVal.str();
471  }
472 };
473 
474 /**
475  * Parsed data area definition.
476  */
477 class DataLine {
478 public:
479  /// Number of MAUs initialized by this data line.
481  /// Address space whose MAUs are initialized.
482  std::string dataSpace;
483 
484  /// Init data fields of data line. Uninitilized data line, if empty.
485  std::vector<InitDataField> initData;
486  /// Labels of this data line.
487  std::vector<std::string> labels;
488 
489  /// Line number where in source code this DA line is found.
491 
492  /**
493  * String representation of term for error message generation.
494  */
495  std::string toString() const {
496 
497  std::stringstream retVal;
498 
499  retVal << "DataLine:\n"
500  << "address space:\t" << dataSpace << std::endl
501  << "data maus: \t" << width << std::endl;
502 
503  retVal << "Labels:" << std::endl;
504  for (unsigned int i = 0; i < labels.size(); i++) {
505  retVal << "\t" << labels[i] << std::endl;
506  }
507 
508  retVal << "Init data:" << std::endl;
509  for (unsigned int i = 0; i < initData.size(); i++) {
510  retVal << (int)initData[i].width << ":";
511 
512  if (initData[i].litOrExpr.isExpression) {
513  retVal << initData[i].litOrExpr.expression.label;
514  } else {
515  retVal << (int)initData[i].litOrExpr.value;
516  }
517 
518  retVal << "\t";
519 
520  if ((i+1)%16 == 0) retVal << std::endl;
521  }
522 
523  retVal << "\n";
524 
525  return retVal.str();
526  }
527 };
528 
529 #endif
DataLine::toString
std::string toString() const
Definition: ParserStructs.hh:495
ParserSource::toString
std::string toString() const
Definition: ParserStructs.hh:291
SValue
long SValue
Definition: ParserStructs.hh:45
Expression::isMinus
bool isMinus
Is offset minus.
Definition: ParserStructs.hh:211
IndexTerm::toString
std::string toString() const
Definition: ParserStructs.hh:119
FUTerm::part2
std::string part2
Port name.
Definition: ParserStructs.hh:83
FUTerm::part3Used
bool part3Used
Is operation part of the term used.
Definition: ParserStructs.hh:79
ParserMove::isBegin
bool isBegin
Tells whether the slot is the first of the instruction.
Definition: ParserStructs.hh:404
Expression::hasValue
bool hasValue
Is resolved value defined in struct.
Definition: ParserStructs.hh:204
BusTerm::prev
bool prev
Previous or next bus register.
Definition: ParserStructs.hh:56
ParserGuard::isGuarded
bool isGuarded
Is guard used.
Definition: ParserStructs.hh:308
Expression
Definition: ParserStructs.hh:198
ParserMove::type
MoveType type
Type of move.
Definition: ParserStructs.hh:401
RegisterTerm::busTerm
BusTerm busTerm
The bus term, if type field is BUS. Otherwise not used.
Definition: ParserStructs.hh:152
Expression::label
std::string label
Name of the label.
Definition: ParserStructs.hh:201
DataLine::dataSpace
std::string dataSpace
Address space whose MAUs are initialized.
Definition: ParserStructs.hh:482
RegisterTerm::INDEX
@ INDEX
Index term.
Definition: ParserStructs.hh:145
Conversion::toString
static std::string toString(const T &source)
ParserGuard::regTerm
RegisterTerm regTerm
Guard port or register.
Definition: ParserStructs.hh:312
MAX_VALUE_LENGTH_IN_BYTES
const UValue MAX_VALUE_LENGTH_IN_BYTES
Buffer size.
Definition: ParserStructs.hh:48
IndexTerm::part2
std::string part2
Port or operation name.
Definition: ParserStructs.hh:112
IndexTerm
Definition: ParserStructs.hh:105
Expression::value
UValue value
Resolved value.
Definition: ParserStructs.hh:206
Expression::toString
std::string toString() const
Definition: ParserStructs.hh:218
InitDataField::width
UValue width
Number of MAUs that are initialized by the init field.
Definition: ParserStructs.hh:342
LiteralOrExpression::isExpression
bool isExpression
Does object contain expression or literal.
Definition: ParserStructs.hh:251
ParserMove::toString
std::string toString() const
Definition: ParserStructs.hh:438
ParserMove::LONG_IMMEDIATE
@ LONG_IMMEDIATE
Encoding of one long immediate slot.
Definition: ParserStructs.hh:396
IndexTerm::part1
std::string part1
Unit name.
Definition: ParserStructs.hh:110
ParserGuard::isInverted
bool isInverted
Is guard inverted.
Definition: ParserStructs.hh:310
IndexTerm::index
UValue index
Register or operand index.
Definition: ParserStructs.hh:114
UValue
unsigned long UValue
Definition: ParserStructs.hh:44
IndexTerm::part2Used
bool part2Used
Is port name used.
Definition: ParserStructs.hh:108
RegisterTerm::toString
std::string toString() const
Definition: ParserStructs.hh:161
LiteralOrExpression::LiteralOrExpression
LiteralOrExpression()
Definition: ParserStructs.hh:248
Conversion.hh
RegisterTerm::type
TermType type
Type of terminal that is represented by this object.
Definition: ParserStructs.hh:149
InitDataField
Definition: ParserStructs.hh:339
RegisterTerm::BUS
@ BUS
Bus term.
Definition: ParserStructs.hh:143
InitDataField::toString
std::string toString() const
Definition: ParserStructs.hh:349
LiteralOrExpression
Definition: ParserStructs.hh:245
ParserMove::EMPTY
@ EMPTY
Empty move slot.
Definition: ParserStructs.hh:395
LiteralOrExpression::toString
std::string toString() const
Definition: ParserStructs.hh:263
RegisterTerm::FUNCTION_UNIT
@ FUNCTION_UNIT
FU term.
Definition: ParserStructs.hh:144
InitDataField::litOrExpr
LiteralOrExpression litOrExpr
Initialisation value.
Definition: ParserStructs.hh:344
DataLine::initData
std::vector< InitDataField > initData
Init data fields of data line. Uninitilized data line, if empty.
Definition: ParserStructs.hh:485
BusTerm
Definition: ParserStructs.hh:53
ParserMove::ParserMove
ParserMove()
Definition: ParserStructs.hh:422
ParserMove
Definition: ParserStructs.hh:391
DataLine
Definition: ParserStructs.hh:477
ParserSource::regTerm
RegisterTerm regTerm
If register, the register. Otherwise not used.
Definition: ParserStructs.hh:284
FUTerm::toString
std::string toString() const
Definition: ParserStructs.hh:90
LiteralOrExpression::value
UValue value
If literal, the literal. Otherwise not used.
Definition: ParserStructs.hh:256
ParserMove::annotationes
std::vector< Annotation > annotationes
Definition: ParserStructs.hh:415
ParserSource::immTerm
LiteralOrExpression immTerm
If immediate value, the literal or expression. Otherwise not used.
Definition: ParserStructs.hh:286
LiteralOrExpression::isSigned
bool isSigned
Sign of the value.
Definition: ParserStructs.hh:258
ParserMove::ParserMove
ParserMove(MoveType aType, bool begin)
Definition: ParserStructs.hh:431
BusTerm::toString
std::string toString() const
Definition: ParserStructs.hh:61
RegisterTerm::indexTerm
IndexTerm indexTerm
The index term, if type field is INDEX. Otherwise not used.
Definition: ParserStructs.hh:156
DataLine::asmLineNumber
UValue asmLineNumber
Line number where in source code this DA line is found.
Definition: ParserStructs.hh:490
ParserMove::TRANSPORT
@ TRANSPORT
Data transport (move).
Definition: ParserStructs.hh:397
ParserGuard
Definition: ParserStructs.hh:305
ParserMove::destination
RegisterTerm destination
Destination field.
Definition: ParserStructs.hh:411
RegisterTerm::fuTerm
FUTerm fuTerm
The fu term, if type field is FUNCTION_UNIT. Otherwise not used.
Definition: ParserStructs.hh:154
Annotation::id
UValue id
Definition: ParserStructs.hh:367
ParserMove::MoveType
MoveType
Types of instruction slots.
Definition: ParserStructs.hh:394
ParserMove::source
ParserSource source
Source field.
Definition: ParserStructs.hh:409
DataLine::labels
std::vector< std::string > labels
Labels of this data line.
Definition: ParserStructs.hh:487
ParserSource
Definition: ParserStructs.hh:279
Expression::offset
UValue offset
Value of offset.
Definition: ParserStructs.hh:213
Annotation::toString
std::string toString() const
Definition: ParserStructs.hh:371
Annotation::payload
std::vector< InitDataField > payload
Definition: ParserStructs.hh:369
RegisterTerm
Definition: ParserStructs.hh:136
LiteralOrExpression::expression
Expression expression
If expression the expression, Otherwise not used.
Definition: ParserStructs.hh:253
Expression::hasOffset
bool hasOffset
Is offset defined.
Definition: ParserStructs.hh:209
ParserMove::guard
ParserGuard guard
Guard field.
Definition: ParserStructs.hh:407
Annotation
Definition: ParserStructs.hh:364
RegisterTerm::TermType
TermType
Definition: ParserStructs.hh:142
FUTerm
Definition: ParserStructs.hh:76
ParserGuard::toString
std::string toString() const
Definition: ParserStructs.hh:317
FUTerm::part1
std::string part1
Unit name.
Definition: ParserStructs.hh:81
FUTerm::part3
std::string part3
Operation name.
Definition: ParserStructs.hh:85
DataLine::width
UValue width
Number of MAUs initialized by this data line.
Definition: ParserStructs.hh:480
ParserSource::isRegister
bool isRegister
Is source register or immediate reference.
Definition: ParserStructs.hh:282
ParserMove::asmLineNumber
UValue asmLineNumber
Line number of source code for errors.
Definition: ParserStructs.hh:413