OpenASIP  2.0
OperationSerializer.cc
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  * @file OperationSerializer.cc
26  *
27  * Definition of OperationSerializer class.
28  *
29  * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30  * @author Pekka Jääskeläinen 2007 (pekka.jaaskelainen-no.spam-tut.fi)
31  * @author Mikael Lepistö 2007 (mikael.lepisto-no.spam-tut.fi)
32  * @note rating: yellow
33  * @note reviewed 7 September 2004 by pj, jn, jm, ao
34  */
35 
36 #include <string>
37 
38 #include "OperationSerializer.hh"
39 #include "Conversion.hh"
40 #include "Operation.hh"
41 #include "FileSystem.hh"
42 #include "Environment.hh"
43 #include "StringTools.hh"
44 #include "Operand.hh"
45 #include "ObjectState.hh"
46 
47 using std::string;
48 
49 /// Schema file name.
50 const string SCHEMA_FILE_NAME = "Operation_Schema.xsd";
51 /// Name of the library.
52 const string LIBRARY_NAME = "osal";
53 
54 /// The version number.
55 const double VERSION_NUMBER = 0.1;
56 
57 /// XML tag for the module.
58 const string OPSER_OSAL = "osal";
59 /// XML tag for version.
60 const string OPSER_VERSION = "version";
61 
62 /**
63  * Constructor.
64  *
65  * If schema file is found it is used.
66  */
68 
70  if (path != "") {
71  string schema = path + FileSystem::DIRECTORY_SEPARATOR +
73  if (FileSystem::fileExists(schema)) {
74  setSchemaFile(schema);
75  setUseSchema(true);
76  }
77  }
78 }
79 
80 /**
81  * Destructor.
82  */
84 }
85 
86 /**
87  * Writes the state of operations to XML file.
88  *
89  * The states of operations are first converted to the format understood
90  * by XMLSerializer and then written.
91  *
92  * @param state ObjectState to be written.
93  * @exception SerializerException If error occurs.
94  */
95 void
97  try {
98  ObjectState* converted = convertToXMLFormat(state);
99  serializer_.writeState(converted);
100  delete converted;
101  } catch (const Exception& e) {
102  string method = "OperationSerializer::writeState()";
103  string msg = "Problems writing the state: " + e.errorMessage();
104  SerializerException error(__FILE__, __LINE__, __func__, msg);
105  error.setCause(e);
106  throw error;
107  }
108 }
109 
110 /**
111  * Reads the Operation property file and returns a corresponding ObjectState.
112  *
113  * ObjectState is converted to more easily parseable format before returning.
114  *
115  * @return The read ObjectState tree.
116  */
119  ObjectState* operationState = NULL;
120  try {
121  ObjectState* xmlState = serializer_.readState();
122  operationState = convertToOperationFormat(xmlState);
123  delete xmlState;
124  } catch (const Exception& e) {
125  string method = "OperationSerializer::readState()";
126  string msg = "Problems reading the state: " + e.errorMessage();
127  SerializerException error(__FILE__, __LINE__, __func__, msg);
128  error.setCause(e);
129  throw error;
130  }
131 
132  return operationState;
133 }
134 
135 /**
136  * Converts ObjectState to the format understood by XMLSerializer.
137  *
138  * XMLSerializer needs an ObjectState tree in which each attribute
139  * is in its own node. ObjectState tree obtained form an operation
140  * is not in that format. This is why conversion is needed.
141  *
142  * @param state ObjectState to be converted.
143  * @exception ObjectStateLoadingException If ObjectState conversion fails.
144  * @return The converted ObjectState tree.
145  */
148  ObjectState* operation = new ObjectState(OPSER_OSAL);
150  for (int i = 0; i < state->childCount(); i++) {
151  ObjectState* child = state->child(i);
152  operation->addChild(toXMLFormat(child));
153  }
154  return operation;
155 }
156 
157 /**
158  * Converts an individual operation to xml format.
159  *
160  * @param state ObjectState to be converted.
161  * @exception ObjectStateLoadingException If ObjectState conversion fails.
162  * @return The converted ObjectState tree.
163  */
167  try {
168  ObjectState* nameChild = new ObjectState(Operation::OPRN_NAME);
169  nameChild->setValue(state->stringAttribute(Operation::OPRN_NAME));
170  oper->addChild(nameChild);
171 
172  ObjectState* descriptionChild = new ObjectState(Operation::OPRN_DESCRIPTION);
173  descriptionChild->setValue(state->stringAttribute(Operation::OPRN_DESCRIPTION));
174  oper->addChild(descriptionChild);
175 
177  inputChild->setValue(state->intAttribute(Operation::OPRN_INPUTS));
178  oper->addChild(inputChild);
179 
180  ObjectState* outputChild = new ObjectState(Operation::OPRN_OUTPUTS);
181  outputChild->setValue(state->intAttribute(Operation::OPRN_OUTPUTS));
182  oper->addChild(outputChild);
183 
186  }
187 
190  }
191 
192  if (state->boolAttribute(Operation::OPRN_TRAP)) {
194  }
195 
198  }
199 
202  }
203 
206  }
207 
210  }
211 
214  }
215 
216  for (int i = 0; i < state->childCount(); i++) {
217 
218  ObjectState* child = state->child(i);
219  if (child->name() == Operation::OPRN_IN ||
220  child->name() == Operation::OPRN_OUT) {
221 
222  ObjectState* operandChild = NULL;
223  if (child->name() == Operation::OPRN_IN) {
224  operandChild = new ObjectState(Operation::OPRN_IN);
225  } else {
226  operandChild = new ObjectState(Operation::OPRN_OUT);
227  }
228 
229  operandChild->setAttribute(
232 
233  operandChild->setAttribute(
236 
238  operandChild->setAttribute(
241  }
242 
244  operandChild->setAttribute(
247  }
248 
250  operandChild->
252  }
253 
255  operandChild->addChild(
257  }
258 
260  ObjectState* units = new ObjectState(
262  units->setValue(child->intAttribute(
264  operandChild->addChild(units);
265  }
266 
267  // can swap list
268  if (child->childCount() > 0) {
269  ObjectState* swap =
271  ObjectState* canSwap = child->child(0);
272 
273  for (int j = 0; j < canSwap->childCount(); j++) {
274  ObjectState* swapChild = canSwap->child(j);
275 
276  ObjectState* newSwap =
278 
279  newSwap->setAttribute(
281  swapChild->intAttribute(Operand::OPRND_ID));
282  swap->addChild(newSwap);
283 
284  }
285  operandChild->addChild(swap);
286  }
287 
288  oper->addChild(operandChild);
289  } else {
290 
291  ObjectState* newChild = new ObjectState(*child);
292  oper->addChild(newChild);
293  }
294  }
295 
296  } catch (const Exception& e) {
297  string msg = "Error while constructing ObjectState tree: " +
298  e.errorMessage();
299  ObjectStateLoadingException error(__FILE__, __LINE__, __func__, msg);
300  error.setCause(e);
301  throw error;
302  }
303  return oper;
304 }
305 
306 /**
307  * Converts ObjectState to more easily parseable format.
308  *
309  * More easily parseable format is such that there are less nodes.
310  * Only children of the main node are the operands, affects, and
311  * affected-by nodes.
312  *
313  * @param state ObjectState to be converted.
314  * @exception ObjectStateLoadingException If ObjectState conversion fails.
315  * @return The converted ObjectState tree.
316  */
319  ObjectState* operation = new ObjectState(OPSER_OSAL);
320  for (int i = 0; i < state->childCount(); i++) {
321  ObjectState* child = state->child(i);
322  operation->addChild(toOperation(child));
323  }
324  return operation;
325 }
326 
327 /**
328  * Converts Operation ObjectState tree to more easily parsed format.
329  *
330  * @param state ObjectState to be converted.
331  * @exception ObjectStateLoadingException If ObjectState conversion fails.
332  * @return The converted ObjectState tree.
333  */
337 
338  try {
339  for (int i = 0; i < state->childCount(); i++) {
340 
341  ObjectState* child = state->child(i);
342 
343  if (child->name() == Operation::OPRN_NAME) {
344  root->setAttribute(
346  } else if (child->name() == Operation::OPRN_DESCRIPTION) {
348  } else if (child->name() == Operation::OPRN_INPUTS) {
350  } else if (child->name() == Operation::OPRN_OUTPUTS) {
352  } else if (child->name() == Operation::OPRN_READS_MEMORY) {
354  } else if (child->name() == Operation::OPRN_WRITES_MEMORY) {
356  } else if (child->name() == Operation::OPRN_TRAP) {
357  root->setAttribute(Operation::OPRN_TRAP, true);
358  } else if (child->name() == Operation::OPRN_SIDE_EFFECTS) {
360  } else if (child->name() == Operation::OPRN_CLOCKED) {
362  } else if (child->name() == Operation::OPRN_CONTROL_FLOW) {
364  } else if (child->name() == Operation::OPRN_ISCALL) {
366  } else if (child->name() == Operation::OPRN_ISBRANCH) {
368 
369 // } else if (child->name() == Operation::OPRN_AFFECTED_BY) {
370 // ObjectState* affectedBy = new ObjectState(*child);
371 // root->addChild(affectedBy);
372 
373 // } else if (child->name() == Operation::OPRN_AFFECTS) {
374 // ObjectState* affects = new ObjectState(*child);
375 // root->addChild(affects);
376 
377  } else if (child->name() == Operation::OPRN_IN) {
378  ObjectState* inOperand = new ObjectState(Operation::OPRN_IN);
379 
380  inOperand->setAttribute(
383 
384  inOperand->setAttribute(
387 
389  inOperand->setAttribute(
392  }
393 
395  inOperand->setAttribute(
398  }
399 
400  root->addChild(inOperand);
401 
402  setOperandProperties(inOperand, child);
403 
404  if (!inOperand->hasAttribute(Operand::OPRND_MEM_ADDRESS)) {
405  inOperand->setAttribute(Operand::OPRND_MEM_ADDRESS, false);
406  }
407 
408  if (!inOperand->hasAttribute(Operand::OPRND_MEM_DATA)) {
409  inOperand->setAttribute(Operand::OPRND_MEM_DATA, false);
410  }
411 
412  } else if (child->name() == Operation::OPRN_OUT) {
413 
414  ObjectState* outOperand = new ObjectState(Operation::OPRN_OUT);
415 
416  outOperand->setAttribute(
419 
420  outOperand->setAttribute(
423 
425  outOperand->setAttribute(
428  }
429 
431  outOperand->setAttribute(
434  }
435 
436  root->addChild(outOperand);
437  setOperandProperties(outOperand, child);
438 
439  outOperand->setAttribute(Operand::OPRND_MEM_ADDRESS, false);
440 
441  if (!outOperand->hasAttribute(Operand::OPRND_MEM_DATA)) {
442  outOperand->setAttribute(Operand::OPRND_MEM_DATA, false);
443  }
444 
445  } else {
446  // just copy other elements
447  ObjectState* copyChild = new ObjectState(*child);
448  root->addChild(copyChild);
449  }
450  }
451 
452  root->setAttribute(
455 
457  root->setAttribute(Operation::OPRN_DESCRIPTION, std::string(""));
458  }
459 
462  }
463 
466  }
467 
468  if (!root->hasAttribute(Operation::OPRN_TRAP)) {
469  root->setAttribute(Operation::OPRN_TRAP, false);
470  }
471 
474  }
475 
478  }
479 
482  }
483  if (!root->hasAttribute(Operation::OPRN_ISCALL)) {
484  root->setAttribute(Operation::OPRN_ISCALL, false);
485  }
488  }
489 
490  } catch (const Exception& e) {
491  string msg = "Error while constructing ObjectState tree" +
492  e.errorMessage();
493  ObjectStateLoadingException error(__FILE__, __LINE__, __func__, msg);
494  error.setCause(e);
495  throw error;
496  }
497 
498  return root;
499 }
500 
501 /**
502  * Set operand properties to right values.
503  *
504  * @param operand Operand which properties are set.
505  * @param source Original ObjectState tree.
506  */
507 void
509  ObjectState* operand,
510  ObjectState* source) {
511 
512  for (int childIndex = 0; childIndex < source->childCount();
513  childIndex++) {
514 
515  ObjectState* child = source->child(childIndex);
516  if (child->name() == Operand::OPRND_MEM_ADDRESS) {
518  } else if (child->name() == Operand::OPRND_MEM_DATA) {
519  operand->setAttribute(Operand::OPRND_MEM_DATA, true);
520  } else if (child->name() == Operand::OPRND_CAN_SWAP) {
521  ObjectState* canSwap = new ObjectState(*child);
522  operand->addChild(canSwap);
523  }
524  if (child->name() == Operand::OPRND_MEM_UNITS) {
526  }
527  }
528 }
529 
530 /**
531  * Sets source file.
532  *
533  * @param filename The name of the file.
534  */
535 void
536 OperationSerializer::setSourceFile(const std::string& filename) {
537  serializer_.setSourceFile(filename);
538 }
539 
540 /**
541  * Sets destination file.
542  *
543  * @param filename The name of the file.
544  */
545 void
546 OperationSerializer::setDestinationFile(const std::string& filename) {
548 }
549 
550 /**
551  * Sets schema file.
552  *
553  * @param fileName The name of the file.
554  */
555 void
556 OperationSerializer::setSchemaFile(const std::string& filename) {
557  serializer_.setSchemaFile(filename);
558 }
559 
560 /**
561  * Sets whether schema is used or not.
562  *
563  * @param useSchema True or false, depending on if schema is to be used or not.
564  */
565 void
567  serializer_.setUseSchema(useSchema);
568 }
OPSER_VERSION
const string OPSER_VERSION
XML tag for version.
Definition: OperationSerializer.cc:60
Operand::OPRND_ELEM_COUNT
static const std::string OPRND_ELEM_COUNT
Object state name for element count.
Definition: Operand.hh:100
LIBRARY_NAME
const string LIBRARY_NAME
Name of the library.
Definition: OperationSerializer.cc:52
OperationSerializer::toOperation
ObjectState * toOperation(const ObjectState *state)
Definition: OperationSerializer.cc:335
ObjectState::hasAttribute
bool hasAttribute(const std::string &name) const
Definition: ObjectState.cc:205
Operand::OPRND_MEM_DATA
static const std::string OPRND_MEM_DATA
Object state name for memory data.
Definition: Operand.hh:90
FileSystem.hh
ObjectState::stringAttribute
std::string stringAttribute(const std::string &name) const
Definition: ObjectState.cc:249
OperationSerializer::serializer_
XMLSerializer serializer_
Constructs the ObjectState tree from the XML file.
Definition: OperationSerializer.hh:77
XMLSerializer::setSourceFile
void setSourceFile(const std::string &fileName)
Definition: XMLSerializer.cc:115
Operand::OPRND_MEM_ADDRESS
static const std::string OPRND_MEM_ADDRESS
Object state name for memory address.
Definition: Operand.hh:86
OperationSerializer::writeState
virtual void writeState(const ObjectState *state)
Definition: OperationSerializer.cc:96
Operand::OPRND_MEM_UNITS
static const std::string OPRND_MEM_UNITS
Object state name for memory unit count.
Definition: Operand.hh:88
ObjectStateLoadingException
Definition: Exception.hh:551
Operation::OPRN_IN
static const char * OPRN_IN
Object state name for input operand.
Definition: Operation.hh:91
ObjectState::intValue
int intValue() const
OperationSerializer::setDestinationFile
void setDestinationFile(const std::string &filename)
Definition: OperationSerializer.cc:546
OperationSerializer::convertToOperationFormat
ObjectState * convertToOperationFormat(const ObjectState *state)
Definition: OperationSerializer.cc:318
OperationSerializer::~OperationSerializer
virtual ~OperationSerializer()
Definition: OperationSerializer.cc:83
OperationSerializer::convertToXMLFormat
ObjectState * convertToXMLFormat(const ObjectState *state)
Definition: OperationSerializer.cc:147
Operation::OPRN_NAME
static const char * OPRN_NAME
Object state name for name.
Definition: Operation.hh:67
Operand::OPRND_CAN_SWAP
static const std::string OPRND_CAN_SWAP
Object state name for can swap.
Definition: Operand.hh:92
Exception::setCause
void setCause(const Exception &cause)
Definition: Exception.cc:75
VERSION_NUMBER
const double VERSION_NUMBER
The version number.
Definition: OperationSerializer.cc:55
ObjectState
Definition: ObjectState.hh:59
StringTools::stringToUpper
static std::string stringToUpper(const std::string &source)
Definition: StringTools.cc:143
StringTools.hh
OperationSerializer::setOperandProperties
void setOperandProperties(ObjectState *operand, ObjectState *source)
Definition: OperationSerializer.cc:508
Operation::OPRN_CONTROL_FLOW
static const char * OPRN_CONTROL_FLOW
Object state name for control flow property.
Definition: Operation.hh:81
Operation::OPRN_DESCRIPTION
static const char * OPRN_DESCRIPTION
Object state name for description.
Definition: Operation.hh:69
Operand::OPRND_ID
static const std::string OPRND_ID
Object state name for operand id.
Definition: Operand.hh:82
Operation::OPRN_TRAP
static const char * OPRN_TRAP
Object state name for trap.
Definition: Operation.hh:75
XMLSerializer::setSchemaFile
void setSchemaFile(const std::string &fileName)
Definition: XMLSerializer.cc:168
OPSER_OSAL
const string OPSER_OSAL
XML tag for the module.
Definition: OperationSerializer.cc:58
Conversion.hh
OperationSerializer::toXMLFormat
ObjectState * toXMLFormat(const ObjectState *state)
Definition: OperationSerializer.cc:165
XMLSerializer::readState
virtual ObjectState * readState()
Definition: XMLSerializer.cc:200
OperationSerializer::readState
virtual ObjectState * readState()
Definition: OperationSerializer.cc:118
Operation::OPRN_OPERATION
static const char * OPRN_OPERATION
Object state name for operation.
Definition: Operation.hh:65
Operation::OPRN_CLOCKED
static const char * OPRN_CLOCKED
Object state name for clockedness.
Definition: Operation.hh:79
XMLSerializer::setDestinationFile
void setDestinationFile(const std::string &fileName)
Definition: XMLSerializer.cc:142
__func__
#define __func__
Definition: Application.hh:67
ObjectState.hh
OperationSerializer.hh
Operation::OPRN_ISCALL
static const char * OPRN_ISCALL
Object state name for call property.
Definition: Operation.hh:97
TCETools::Serializer
Definition: Serializer.hh:45
Environment::schemaDirPath
static std::string schemaDirPath(const std::string &prog)
Definition: Environment.cc:151
ObjectState::child
ObjectState * child(int index) const
Definition: ObjectState.cc:471
ObjectState::addChild
void addChild(ObjectState *child)
Definition: ObjectState.cc:376
Operation.hh
Environment.hh
Operation::OPRN_WRITES_MEMORY
static const char * OPRN_WRITES_MEMORY
Object state name for writes memory.
Definition: Operation.hh:85
ObjectState::childCount
int childCount() const
OperationSerializer::setSchemaFile
void setSchemaFile(const std::string &filename)
Definition: OperationSerializer.cc:556
OperationSerializer::setUseSchema
void setUseSchema(bool useSchema)
Definition: OperationSerializer.cc:566
SerializerException
Definition: Exception.hh:675
OperationSerializer::setSourceFile
void setSourceFile(const std::string &filename)
Definition: OperationSerializer.cc:536
Operation::OPRN_READS_MEMORY
static const char * OPRN_READS_MEMORY
Object state name for reads memory.
Definition: Operation.hh:83
Exception
Definition: Exception.hh:54
Operand::OPRND_IN
static const std::string OPRND_IN
Object state name for input operand.
Definition: Operand.hh:94
OperationSerializer::OperationSerializer
OperationSerializer()
Definition: OperationSerializer.cc:67
ObjectState::name
std::string name() const
XMLSerializer::setUseSchema
void setUseSchema(bool useSchema)
Definition: XMLSerializer.cc:179
Operation::OPRN_OUT
static const char * OPRN_OUT
Object state name for output operand.
Definition: Operation.hh:93
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
Operation::OPRN_INPUTS
static const char * OPRN_INPUTS
Object state name for inputs.
Definition: Operation.hh:71
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
Operand.hh
Operand::OPRND_TYPE
static const std::string OPRND_TYPE
Object state name for operand type.
Definition: Operand.hh:84
Operation::OPRN_SIDE_EFFECTS
static const char * OPRN_SIDE_EFFECTS
Object state name for side effects.
Definition: Operation.hh:77
Operand::OPRND_ELEM_WIDTH
static const std::string OPRND_ELEM_WIDTH
Object state name for element width.
Definition: Operand.hh:98
FileSystem::fileExists
static bool fileExists(const std::string fileName)
ObjectState::boolAttribute
bool boolAttribute(const std::string &name) const
Definition: ObjectState.cc:338
SCHEMA_FILE_NAME
const string SCHEMA_FILE_NAME
Schema file name.
Definition: OperationSerializer.cc:50
ObjectState::stringValue
std::string stringValue() const
ObjectState::intAttribute
int intAttribute(const std::string &name) const
Definition: ObjectState.cc:276
ObjectState::setValue
void setValue(const std::string &value)
Operation::OPRN_ISBRANCH
static const char * OPRN_ISBRANCH
Object state name for branch property.
Definition: Operation.hh:99
Operation::OPRN_OUTPUTS
static const char * OPRN_OUTPUTS
Object state name for outputs.
Definition: Operation.hh:73
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100
XMLSerializer::writeState
virtual void writeState(const ObjectState *rootState)
Definition: XMLSerializer.cc:219