OpenASIP  2.0
CreateBEM.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 CreateBEM.cc
26  *
27  * Implements the main function of createbem application which creates a
28  * binary encoding map.
29  *
30  * @author Lasse Laasonen 2005 (lasse.laasonen-no.spam-tut.fi)
31  * @note rating: red
32  */
33 
34 #include <iostream>
35 #include <string>
36 
38 #include "BEMSerializer.hh"
39 #include "BEMGenerator.hh"
40 #include "BinaryEncoding.hh"
41 #include "Machine.hh"
42 #include "ADFSerializer.hh"
43 #include "FileSystem.hh"
44 
45 using std::cerr;
46 using std::endl;
47 using std::string;
48 
49 using namespace TTAMachine;
50 
51 
52 /**
53  * Loads the given ADF file and creates a Machine instance from it.
54  *
55  * @param adfFile The ADF file.
56  * @return The newly created Machine instance.
57  * @exception SerializerException If an error occurs while reading the file.
58  * @exception ObjectStateLoadingException If an error occurs while loading
59  * the state of Machine instance.
60  */
61 static Machine*
62 loadMachine(const std::string& adfFile) {
63  ADFSerializer serializer;
64  serializer.setSourceFile(adfFile);
65  return serializer.readMachine();
66 }
67 
68 /**
69  * Generates the name of the output file from the given ADF file name.
70  *
71  * The generated name has same body but .bem suffix.
72  *
73  * @param adfFile The name of the ADF file.
74  * @return The name of the output file.
75  */
76 static std::string
77 outputFileName(const std::string& adfFile) {
78  string file = FileSystem::fileNameBody(adfFile);
79  return file + ".bem";
80 }
81 
82 
83 /**
84  * The main function.
85  */
86 int main(int argc, char* argv[]) {
88  try {
89  options.parse(argv, argc);
90  } catch (ParserStopRequest) {
91  return EXIT_SUCCESS;
92  } catch (const IllegalCommandLine& exception) {
93  cerr << exception.errorMessage() << endl;
94  return EXIT_FAILURE;
95  }
96 
97  string adfFile = options.adfFile();
98  string outputFile = options.outputFile();
99  if (adfFile == "") {
100  options.printHelp();
101  return EXIT_FAILURE;
102  }
103 
104  Machine* mach = NULL;
105  try {
106  mach = loadMachine(adfFile);
107  } catch (const Exception& e) {
108  cerr << "Error while loading machine from adf:" << endl
109  << e.fileName() << ", line: " << e.lineNum() << endl
110  << e.errorMessage() << endl;
111  return EXIT_FAILURE;
112  }
113 
114  BEMGenerator generator(*mach);
115  BinaryEncoding* bem = generator.generate();
116 
117  BEMSerializer serializer;
118 
119  if (outputFile == "") {
120  outputFile = outputFileName(adfFile);
121  }
122 
123  serializer.setDestinationFile(outputFile);
124  serializer.writeBinaryEncoding(*bem);
125 
126  delete mach;
127  delete bem;
128 
129  return EXIT_SUCCESS;
130 }
BEMGeneratorCmdLineOptions
Definition: BEMGeneratorCmdLineOptions.hh:42
MachInfoCmdLineOptions::printHelp
virtual void printHelp() const
Definition: MachInfoCmdLineOptions.cc:89
BinaryEncoding
Definition: BinaryEncoding.hh:61
BEMGenerator.hh
FileSystem.hh
Exception::lineNum
int lineNum() const
loadMachine
static Machine * loadMachine(const std::string &adfFile)
Definition: CreateBEM.cc:62
ParserStopRequest
Definition: Exception.hh:491
XMLSerializer::setSourceFile
void setSourceFile(const std::string &fileName)
Definition: XMLSerializer.cc:115
outputFileName
static std::string outputFileName(const std::string &adfFile)
Definition: CreateBEM.cc:77
IllegalCommandLine
Definition: Exception.hh:438
BEMSerializer
Definition: BEMSerializer.hh:43
BEMGenerator
Definition: BEMGenerator.hh:61
Exception::fileName
std::string fileName() const
BEMGeneratorCmdLineOptions.hh
BinaryEncoding.hh
ADFSerializer
Definition: ADFSerializer.hh:49
XMLSerializer::setDestinationFile
void setDestinationFile(const std::string &fileName)
Definition: XMLSerializer.cc:142
main
int main(int argc, char *argv[])
Definition: CreateBEM.cc:86
Machine.hh
Exception
Definition: Exception.hh:54
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
CmdLineOptions::parse
void parse(char *argv[], int argc)
Definition: CmdLineOptions.cc:107
options
static MachInfoCmdLineOptions options
Definition: MachInfo.cc:46
ADFSerializer.hh
BEMSerializer::writeBinaryEncoding
void writeBinaryEncoding(const BinaryEncoding &bem)
Definition: BEMSerializer.cc:272
ADFSerializer::readMachine
TTAMachine::Machine * readMachine()
Definition: ADFSerializer.cc:275
BEMSerializer.hh
TTAMachine
Definition: Assembler.hh:48
FileSystem::fileNameBody
static std::string fileNameBody(const std::string &fileName)
Definition: FileSystem.cc:291
BEMGenerator::generate
BinaryEncoding * generate()
Definition: BEMGenerator.cc:104
TTAMachine::Machine
Definition: Machine.hh:73