OpenASIP 2.2
Loading...
Searching...
No Matches
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
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
47using std::string;
48
49/// Schema file name.
50const string SCHEMA_FILE_NAME = "Operation_Schema.xsd";
51/// Name of the library.
52const string LIBRARY_NAME = "osal";
53
54/// The version number.
55const double VERSION_NUMBER = 0.1;
56
57/// XML tag for the module.
58const string OPSER_OSAL = "osal";
59/// XML tag for version.
60const 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 */
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 */
95void
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 {
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
181 outputChild->setValue(state->intAttribute(Operation::OPRN_OUTPUTS));
182 oper->addChild(outputChild);
183
186 }
187
190 }
191
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) {
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) {
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
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
470 }
471
474 }
475
478 }
479
482 }
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 */
507void
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) {
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 */
535void
536OperationSerializer::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 */
545void
546OperationSerializer::setDestinationFile(const std::string& filename) {
548}
549
550/**
551 * Sets schema file.
552 *
553 * @param fileName The name of the file.
554 */
555void
556OperationSerializer::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 */
565void
567 serializer_.setUseSchema(useSchema);
568}
#define __func__
const string SCHEMA_FILE_NAME
const string OPSER_OSAL
XML tag for the module.
const string LIBRARY_NAME
Name of the library.
const string SCHEMA_FILE_NAME
Schema file name.
const double VERSION_NUMBER
The version number.
const string OPSER_VERSION
XML tag for version.
static std::string schemaDirPath(const std::string &prog)
std::string errorMessage() const
Definition Exception.cc:123
void setCause(const Exception &cause)
Definition Exception.cc:75
static const std::string DIRECTORY_SEPARATOR
static bool fileExists(const std::string fileName)
bool hasAttribute(const std::string &name) const
void setAttribute(const std::string &name, const std::string &value)
void setValue(const std::string &value)
ObjectState * child(int index) const
void addChild(ObjectState *child)
int intValue() const
std::string stringAttribute(const std::string &name) const
bool boolAttribute(const std::string &name) const
int intAttribute(const std::string &name) const
std::string stringValue() const
std::string name() const
int childCount() const
static const std::string OPRND_IN
Object state name for input operand.
Definition Operand.hh:94
static const std::string OPRND_CAN_SWAP
Object state name for can swap.
Definition Operand.hh:92
static const std::string OPRND_ELEM_WIDTH
Object state name for element width.
Definition Operand.hh:98
static const std::string OPRND_MEM_UNITS
Object state name for memory unit count.
Definition Operand.hh:88
static const std::string OPRND_TYPE
Object state name for operand type.
Definition Operand.hh:84
static const std::string OPRND_MEM_ADDRESS
Object state name for memory address.
Definition Operand.hh:86
static const std::string OPRND_ELEM_COUNT
Object state name for element count.
Definition Operand.hh:100
static const std::string OPRND_MEM_DATA
Object state name for memory data.
Definition Operand.hh:90
static const std::string OPRND_ID
Object state name for operand id.
Definition Operand.hh:82
ObjectState * toOperation(const ObjectState *state)
ObjectState * convertToOperationFormat(const ObjectState *state)
ObjectState * toXMLFormat(const ObjectState *state)
void setSourceFile(const std::string &filename)
XMLSerializer serializer_
Constructs the ObjectState tree from the XML file.
virtual void writeState(const ObjectState *state)
void setDestinationFile(const std::string &filename)
virtual ObjectState * readState()
void setUseSchema(bool useSchema)
ObjectState * convertToXMLFormat(const ObjectState *state)
void setSchemaFile(const std::string &filename)
void setOperandProperties(ObjectState *operand, ObjectState *source)
static const char * OPRN_CONTROL_FLOW
Object state name for control flow property.
Definition Operation.hh:81
static const char * OPRN_ISBRANCH
Object state name for branch property.
Definition Operation.hh:99
static const char * OPRN_DESCRIPTION
Object state name for description.
Definition Operation.hh:69
static const char * OPRN_IN
Object state name for input operand.
Definition Operation.hh:91
static const char * OPRN_CLOCKED
Object state name for clockedness.
Definition Operation.hh:79
static const char * OPRN_TRAP
Object state name for trap.
Definition Operation.hh:75
static const char * OPRN_OPERATION
Object state name for operation.
Definition Operation.hh:65
static const char * OPRN_ISCALL
Object state name for call property.
Definition Operation.hh:97
static const char * OPRN_SIDE_EFFECTS
Object state name for side effects.
Definition Operation.hh:77
static const char * OPRN_READS_MEMORY
Object state name for reads memory.
Definition Operation.hh:83
static const char * OPRN_OUT
Object state name for output operand.
Definition Operation.hh:93
static const char * OPRN_OUTPUTS
Object state name for outputs.
Definition Operation.hh:73
static const char * OPRN_NAME
Object state name for name.
Definition Operation.hh:67
static const char * OPRN_INPUTS
Object state name for inputs.
Definition Operation.hh:71
static const char * OPRN_WRITES_MEMORY
Object state name for writes memory.
Definition Operation.hh:85
static std::string stringToUpper(const std::string &source)
void setUseSchema(bool useSchema)
void setSchemaFile(const std::string &fileName)
void setSourceFile(const std::string &fileName)
virtual void writeState(const ObjectState *rootState)
void setDestinationFile(const std::string &fileName)
virtual ObjectState * readState()