OpenASIP 2.2
Loading...
Searching...
No Matches
IDFSerializer.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 IDFSerializer.cc
26 *
27 * Implementation of IDFSerializer class.
28 *
29 * @author Lasse Laasonen 2005 (lasse.laasonen-no.spam-tut.fi)
30 * @note rating: red
31 */
32
33#include <string>
34#include <set>
35
36#include "IDFSerializer.hh"
39#include "Environment.hh"
40#include "Application.hh"
41#include "ObjectState.hh"
42
43using std::string;
44
45const string ADF_IMPLEMENTATION = "adf-implementation";
46const string IC_DECODER_PLUGIN = "ic-decoder-plugin";
47const string IC_DECODER_PLUGIN_NAME = "name";
48const string IC_DECODER_PLUGIN_FILE = "file";
49const string IC_DECODER_PARAMETER = "parameter";
50const string IC_DECODER_PARAMETER_NAME = "name";
51const string IC_DECODER_PARAMETER_VALUE = "value";
52
53const string DECOMPRESSOR_FILE = "decompressor-file";
54
55const string HDB_FILE = "hdb-file";
56
57const string FU_GENERATE = "fu-generate";
58const string FU_GENERATE_NAME = "name";
59const string FU_GENERATE_OPERATION = "operation";
60const string FU_GENERATE_OPERATION_NAME = "name";
61const string FU_GENERATE_OPERATION_ID = "operation-id";
62const string FU_GENERATE_OPTION = "option";
63
64const string FU = "fu";
65const string FU_NAME = "name";
66const string FU_ID = "fu-id";
67
68const string RF = "rf";
69const string RF_NAME = "name";
70const string RF_ID = "rf-id";
71
72const string IU = "iu";
73const string IU_NAME = "name";
74
75const string BUS = "bus";
76const string BUS_NAME = "name";
77const string BUS_ID = "bus-id";
78
79const string SOCKET = "socket";
80const string SOCKET_NAME = "name";
81const string SOCKET_ID = "socket-id";
82
83const string IDF_SCHEMA_FILE = "idf/IDF_Schema.xsd";
84
85namespace IDF {
86
87/**
88 * The constructor.
89 */
94
95
96/**
97 * The destructor.
98 */
101
102
103/**
104 * Reads the source file that is set and creates an ObjectState tree that
105 * can be loaded by MachineImplementation instance.
106 *
107 * @return The newly created ObjectState tree.
108 * @exception SerializerException If an error occurs while reading the file.
109 */
113 ObjectState* omState = convertToOMFormat(fileState);
114
115 // set the source IDF
116 omState->setAttribute(
118 delete fileState;
119 return omState;
120}
121
122/**
123 * Writes the given ObjectState tree created by
124 * MachineImplementation::saveState to an IDF file.
125 *
126 * @param state ObjectState tree that represents the MachineImplementation
127 * instance.
128 * @exception SerializerException If an error occurs while writing the file.
129 */
130void
132 ObjectState* fileState = convertToFileFormat(state);
133 XMLSerializer::writeState(fileState);
134 delete fileState;
135}
136
137/**
138 * Reads the source IDF file that is set and creates a MachineImplementation
139 * instance from it.
140 *
141 * @return The newly created MachineImplementation instance.
142 * @exception SerializerException If an error occurs while reading the file.
143 * @exception ObjectStateLoadingException If an error occurs while loading
144 * the state of MachineImplementation
145 * instance.
146 */
149 ObjectState* omState = readState();
151 omState);
152 delete omState;
153 return implementation;
154}
155
156/**
157 * Writes the given machine implementation to the destination IDF file.
158 *
159 * @param implementation The implementation to be written.
160 * @exception SerializerException If an error occurs while writing file.
161 */
162void
169
170/**
171 * Converts the given ObjectState tree that represents an IDF file to format
172 * that can be loaded by MachineImplementation instance.
173 *
174 * @param fileState ObjectState tree that represents an IDF file.
175 * @return The newly created ObjectState tree.
176 */
179
180 ObjectState* omState = new ObjectState(
182
183 // add unit implementations
184 ObjectState* fuGenerated =
186 ObjectState* fuImpls = new ObjectState(
188 ObjectState* rfImpls = new ObjectState(
190 ObjectState* iuImpls = new ObjectState(
192 ObjectState* busImpls = new ObjectState(
194 ObjectState* socketImpls = new ObjectState(
196
197 omState->addChild(fuImpls);
198 omState->addChild(rfImpls);
199 omState->addChild(iuImpls);
200 omState->addChild(busImpls);
201 omState->addChild(socketImpls);
202 omState->addChild(fuGenerated);
203
204 // the IC&decoder plugin data
205 if (fileState->hasChild(IC_DECODER_PLUGIN)) {
206 ObjectState* child = fileState->childByName(IC_DECODER_PLUGIN);
207 ObjectState* icdecState = new ObjectState(
209
210 std::string name = "";
211 std::string fileName = "";
212 std::string hdbFile = "";
213
214 for (int i = 0; i < child->childCount(); i++) {
215 ObjectState* attr = child->child(i);
216 if (attr->name() == IC_DECODER_PLUGIN_NAME) {
217 name = attr->stringValue();
218 } else if (attr->name() == IC_DECODER_PLUGIN_FILE) {
219 fileName = attr->stringValue();
220 } else if (attr->name() == HDB_FILE) {
221 hdbFile = attr->stringValue();
222 } else if (attr->name() == IC_DECODER_PARAMETER) {
223
224 // IC&decoder parameter.
225 ObjectState* parameterState = new ObjectState(
227
228 std::string parameterName = attr->stringAttribute(
230
231 parameterState->setAttribute(
233 parameterName);
234
235 std::string parameterValue = "";
236
238 parameterValue = attr->childByName(
240 }
241
242 parameterState->setAttribute(
244 parameterValue);
245
246 icdecState->addChild(parameterState);
247 }
248 }
249
250
251 icdecState->setAttribute(
253 icdecState->setAttribute(
255 icdecState->setAttribute(
257 omState->addChild(icdecState);
258 }
259
260 // decompressor file
261 if (fileState->hasChild(DECOMPRESSOR_FILE)) {
262 ObjectState* child = fileState->childByName(DECOMPRESSOR_FILE);
263 omState->setAttribute(
265 child->stringValue());
266 }
267
268 // Generated FUs.
269 for (int i = 0; i < fileState->childCount(); i++) {
270 ObjectState* child = fileState->child(i);
271
272 if (child->name() != FU_GENERATE) {
273 continue;
274 }
275
276 ObjectState* impl = new ObjectState(*child);
277 fuGenerated->addChild(impl);
278 }
279
280 const std::set<std::string> handledElements{FU, RF, IU, SOCKET, BUS};
281 for (int i = 0; i < fileState->childCount(); i++) {
282 ObjectState* child = fileState->child(i);
283
284 if (!handledElements.count(child->name())) {
285 continue;
286 }
287
288 ObjectState* impl = new ObjectState(
290 impl->setAttribute(
292 child->childByName(HDB_FILE)->stringValue());
293
294 if (child->name() == FU) {
295 fuImpls->addChild(impl);
296 impl->setAttribute(
298 child->stringAttribute(FU_NAME));
299 impl->setAttribute(
301 child->childByName(FU_ID)->stringValue());
302 } else if (child->name() == RF) {
303 rfImpls->addChild(impl);
304 impl->setAttribute(
306 child->stringAttribute(RF_NAME));
307 impl->setAttribute(
309 child->childByName(RF_ID)->stringValue());
310 } else if (child->name() == IU) {
311 iuImpls->addChild(impl);
312 impl->setAttribute(
314 child->stringAttribute(IU_NAME));
315 impl->setAttribute(
317 child->childByName(RF_ID)->stringValue());
318 } else if (child->name() == BUS) {
319 busImpls->addChild(impl);
320 impl->setAttribute(
322 child->stringAttribute(BUS_NAME));
323 impl->setAttribute(
325 child->childByName(BUS_ID)->stringValue());
326 } else if (child->name() == SOCKET) {
327 socketImpls->addChild(impl);
328 impl->setAttribute(
331 impl->setAttribute(
334 } else {
335 assert(false);
336 }
337 }
338
339 return omState;
340}
341
342
343/**
344 * Converts the given ObjectState tree that represents state of a
345 * MachineImplementation instance to IDF file format.
346 *
347 * @param omState ObjectState tree that represents state of a
348 * MachineImplementation instance.
349 * @return The newly created ObjectState tree.
350 */
353
355
356 // the IC&decoder plugin data
358 ObjectState* child = omState->childByName(
360 ObjectState* icdecState = new ObjectState(IC_DECODER_PLUGIN);
361
362 if (child->hasAttribute(
365 newChild->setValue(
366 child->stringAttribute(
368 icdecState->addChild(newChild);
369 }
370
371 if (child->hasAttribute(
373 ObjectState* newChild = new ObjectState(
375 newChild->setValue(
376 child->stringAttribute(
378 icdecState->addChild(newChild);
379 }
380
381 if (child->hasAttribute(
383 ObjectState* newChild = new ObjectState(HDB_FILE);
384 newChild->setValue(
385 child->stringAttribute(
387 icdecState->addChild(newChild);
388 }
389
390
391 // Parameters
392 for (int i = 0; i < child->childCount(); i++) {
393 ObjectState* parameter = child->child(i);
394 assert(parameter->name() ==
396
398 icdecState->addChild(fileParam);
399 fileParam->setAttribute(
401 parameter->stringAttribute(
403
404 if (parameter->hasAttribute(
406
407 ObjectState* value =
409
410 value->setValue(parameter->stringAttribute(
412
413 fileParam->addChild(value);
414 }
415 }
416
417 fileState->addChild(icdecState);
418 }
419
420 // decompressor file
421 if (omState->hasAttribute(
424 fileState->addChild(newChild);
425 newChild->setValue(
426 omState->stringAttribute(
428
429 }
430
431 // add generated FUs.
432 ObjectState* fuGens =
434 for (int i = fuGens->childCount() - 1; i >= 0; --i) {
435 ObjectState* child = fuGens->child(i);
436 fileState->addChild(new ObjectState(*child));
437 }
438
439 // add FU implementations
440 ObjectState* fuImpls = omState->childByName(
442 for (int i = 0; i < fuImpls->childCount(); i++) {
443 ObjectState* child = fuImpls->child(i);
444 ObjectState* fu = new ObjectState(FU);
445 fileState->addChild(fu);
446 fu->setAttribute(
447 FU_NAME,
448 child->stringAttribute(
450 ObjectState* hdbFile = new ObjectState(HDB_FILE);
451 fu->addChild(hdbFile);
452 hdbFile->setValue(
453 child->stringAttribute(
455 ObjectState* id = new ObjectState(FU_ID);
456 fu->addChild(id);
457 id->setValue(
459 }
460
461 // add RF implementations
462 ObjectState* rfImpls = omState->childByName(
464 for (int i = 0; i < rfImpls->childCount(); i++) {
465 ObjectState* child = rfImpls->child(i);
466 ObjectState* rf = new ObjectState(RF);
467 fileState->addChild(rf);
468 rf->setAttribute(
469 RF_NAME,
470 child->stringAttribute(
472 ObjectState* hdbFile = new ObjectState(HDB_FILE);
473 rf->addChild(hdbFile);
474 hdbFile->setValue(
475 child->stringAttribute(
477 ObjectState* id = new ObjectState(RF_ID);
478 rf->addChild(id);
479 id->setValue(
481 }
482
483 // add IU implementations
484 ObjectState* iuImpls = omState->childByName(
486 for (int i = 0; i < iuImpls->childCount(); i++) {
487 ObjectState* child = iuImpls->child(i);
488 ObjectState* iu = new ObjectState(IU);
489 fileState->addChild(iu);
490 iu->setAttribute(
491 IU_NAME,
492 child->stringAttribute(
494 ObjectState* hdbFile = new ObjectState(HDB_FILE);
495 iu->addChild(hdbFile);
496 hdbFile->setValue(
497 child->stringAttribute(
499 ObjectState* id = new ObjectState(RF_ID);
500 iu->addChild(id);
501 id->setValue(
503 }
504
505 // add bus implementations
506 ObjectState* busImpls = omState->childByName(
508 for (int i = 0; i < busImpls->childCount(); i++) {
509 ObjectState* child = busImpls->child(i);
510 ObjectState* bus = new ObjectState(BUS);
511 fileState->addChild(bus);
512 bus->setAttribute(
513 BUS_NAME,
514 child->stringAttribute(
516 ObjectState* hdbFile = new ObjectState(HDB_FILE);
517 bus->addChild(hdbFile);
518 hdbFile->setValue(
519 child->stringAttribute(
521 ObjectState* id = new ObjectState(BUS_ID);
522 bus->addChild(id);
523 id->setValue(
525 }
526
527 // add socket implementations
528 ObjectState* socketImpls = omState->childByName(
530 for (int i = 0; i < socketImpls->childCount(); i++) {
531 ObjectState* child = socketImpls->child(i);
532 ObjectState* socket = new ObjectState(SOCKET);
533 fileState->addChild(socket);
534 socket->setAttribute(
535 IU_NAME,
536 child->stringAttribute(
538 ObjectState* hdbFile = new ObjectState(HDB_FILE);
539 socket->addChild(hdbFile);
540 hdbFile->setValue(
541 child->stringAttribute(
544 socket->addChild(id);
545 id->setValue(
547 }
548
549
550 return fileState;
551}
552
553}
#define assert(condition)
const string SOCKET
const string SOCKET_NAME
IDF::MachineImplementation * implementation
the implementation definition of the estimated processor
const string FU
const string RF_NAME
const string FU_ID
const string RF_ID
const string FU_GENERATE_OPERATION_ID
const string DECOMPRESSOR_FILE
const string FU_GENERATE
const string SOCKET_ID
const string SOCKET
const string IC_DECODER_PARAMETER
const string BUS
const string SOCKET_NAME
const string HDB_FILE
const string IC_DECODER_PLUGIN
const string ADF_IMPLEMENTATION
const string BUS_ID
const string FU_GENERATE_OPERATION_NAME
const string IU
const string FU_GENERATE_NAME
const string BUS_NAME
const string IDF_SCHEMA_FILE
const string FU_GENERATE_OPERATION
const string IC_DECODER_PARAMETER_VALUE
const string IC_DECODER_PLUGIN_NAME
const string FU_GENERATE_OPTION
const string IU_NAME
const string RF
const string FU_NAME
const string IC_DECODER_PLUGIN_FILE
const string IC_DECODER_PARAMETER_NAME
#define FU_NAME
Definition OSAL.hh:494
static std::string schemaDirPath(const std::string &prog)
virtual void writeState(const ObjectState *state)
virtual ObjectState * readState()
MachineImplementation * readMachineImplementation()
static ObjectState * convertToOMFormat(const ObjectState *fileState)
void writeMachineImplementation(const MachineImplementation &implementation)
static ObjectState * convertToFileFormat(const ObjectState *omState)
static const std::string OSKEY_DECOMPRESSOR_FILE
ObjectState attribute key for the name of the decompressor file.
static const std::string OSNAME_IC_DECODER_PLUGIN
ObjectState name for the name of the IC/decoder plugin file.
static const std::string OSKEY_IC_DECODER_HDB
ObjectState attribute name for ic&decoder HDB.
static const std::string OSKEY_SOURCE_IDF
ObjectState attribute name for the source IDF.
static const std::string OSNAME_IU_IMPLEMENTATIONS
ObjectState name for IU implementation container.
static const std::string OSKEY_IC_DECODER_NAME
ObjectState attribute name for ic&decoder name.
static const std::string OSNAME_MACHINE_IMPLEMENTATION
ObjectState name for machine implementation.
static const std::string OSNAME_BUS_IMPLEMENTATIONS
ObjectState name for bus implementation container.
static const std::string OSNAME_FU_GENERATED
ObjectState name for FU generations container.
static const std::string OSKEY_IC_DECODER_FILE
ObjectState attribute name for ic&decoder file.
virtual ObjectState * saveState() const
static const std::string OSNAME_IC_DECODER_PARAMETER
ObjectState attribute name for ic&decoder parameter.
static const std::string OSNAME_SOCKET_IMPLEMENTATIONS
ObjectState name for socket implementation container.
static const std::string OSKEY_IC_DECODER_PARAMETER_NAME
ObjectState attribute name for ic&decoder parameter name.
static const std::string OSNAME_RF_IMPLEMENTATIONS
ObjectState name for RF implementation container.
static const std::string OSNAME_FU_IMPLEMENTATIONS
ObjectState name for FU implementation container.
static const std::string OSKEY_IC_DECODER_PARAMETER_VALUE
ObjectState attribute name for ic&decoder parameter value.
static const std::string OSNAME_UNIT_IMPLEMENTATION
ObjectState name for unit implementation.
static const std::string OSKEY_ID
ObjectState attribute key for the entry ID.
static const std::string OSKEY_UNIT_NAME
Objectstate attribute key for the name of the unit.
static const std::string OSKEY_HDB_FILE
ObjectState attribute key for the name of the HDB file.
bool hasAttribute(const std::string &name) const
ObjectState * childByName(const std::string &name) const
void setAttribute(const std::string &name, const std::string &value)
void setValue(const std::string &value)
bool hasChild(const std::string &name) const
ObjectState * child(int index) const
void addChild(ObjectState *child)
std::string stringAttribute(const std::string &name) const
std::string stringValue() const
std::string name() const
int childCount() const
void setUseSchema(bool useSchema)
void setSchemaFile(const std::string &fileName)
std::string sourceFile() const
virtual void writeState(const ObjectState *rootState)
virtual ObjectState * readState()