OpenASIP 2.2
Loading...
Searching...
No Matches
LLVMTCE.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 LLVMTCE.cc
26 *
27 * LLVM/TCE compiler command line interface.
28 *
29 * @author Veli-Pekka Jääskeläinen 2008 (vjaaskel-no.spam-cs.tut.fi)
30 * @note rating: red
31 */
32#include <iostream>
33#include "Application.hh"
34#include "LLVMBackend.hh"
36#include "Program.hh"
37#include "ADFSerializer.hh"
38#include "FileSystem.hh"
39#include "InterPassData.hh"
40#include "Machine.hh"
41
42#include "CompilerWarnings.hh"
43IGNORE_COMPILER_WARNING("-Wunused-parameter")
44
45#ifndef __STDC_LIMIT_MACROS
46#define __STDC_LIMIT_MACROS
47#endif
48#include <llvm/Support/CommandLine.h>
49
51
52const std::string DEFAULT_OUTPUT_FILENAME = "out.tpef";
53const int DEFAULT_OPT_LEVEL = 2;
54
55/**
56 * Main function of the CLI.
57 *
58 * Checks command line options and runs compiler accordingly.
59 *
60 * @param argc Argument count.
61 * @param argv Command line arguments.
62 * @return Exit status code for the OS.
63 */
64int
65main(int argc, char* argv[]) {
66
67 Application::initialize(argc, argv);
68
70
71 // Parse command line options.
72 try {
73 options->parse(argv, argc);
74 } catch (ParserStopRequest const&) {
75 return EXIT_SUCCESS;
76 } catch (const IllegalCommandLine& e) {
77 std::cerr << e.errorMessageStack() << std::endl;
78 return EXIT_FAILURE;
79 }
80
81 if (options->numberOfArguments() != 1) {
83 return EXIT_FAILURE;
84 }
85
86 if (options->tempDir() == "") {
88 << "Temporary directory required." << std::endl;
90 return EXIT_FAILURE;
91 }
92
94
97 }
98
99 // --- Target ADF ---
100 if (!options->isMachineFileDefined()) {
101 std::cerr << "ERROR: No target architecture (.adf) defined."
102 << std::endl;
103
104 return EXIT_FAILURE;
105 }
106
107 TTAMachine::Machine* mach = NULL;
108 std::string targetADF = options->machineFile();
109
110 if (!FileSystem::fileExists(targetADF) ||
111 !FileSystem::fileIsReadable(targetADF) ||
112 FileSystem::fileIsDirectory(targetADF)) {
113
114 std::cerr << "ERROR: Target architecture file '"
115 << targetADF << "' doesn't exist or isn't readable."
116 << std::endl;
117
118 return EXIT_FAILURE;
119 }
120
121 try {
122 ADFSerializer serializer;
123 serializer.setSourceFile(targetADF);
124 mach = serializer.readMachine();
125 } catch (Exception& e) {
126 std::cerr << "Error loading target architecture file '"
127 << targetADF << "':" << std::endl
128 << e.errorMessageStack() << std::endl;
129
130 return EXIT_FAILURE;
131 }
132
133 // --- Output file name ---
135 if (options->isOutputFileDefined()) {
136 // Test if output file can be opened
137 outputFileName = options->outputFile();
138 }
141 std::cerr << "Output file '"
142 << outputFileName << "' can not be opened for writing!"
143 << std::endl;
144 return EXIT_FAILURE;
145 }
146
147 // --- optimization level ---
148 int optLevel = DEFAULT_OPT_LEVEL;
149 if (options->isOptLevelDefined()) {
150 optLevel = options->optLevel();
151 }
152
153 std::string bytecodeFile = options->argument(1);
154
155 bool debug = options->debugFlag();
156
157 //--- check if program was ran in src dir or from install dir ---
158 std::string runPath = std::string(argv[0]);
159
160 bool useInstalledVersion = Application::isInstalled();
161
162 // All emulation code which cannot be linked in before last global dce is
163 // executed, for emulation of instructions which are generated during
164 // lowering. Unnecessary functions are removed by the MachineDCE pass.
165 std::string emulationCode;
166 if (options->isStandardEmulationLibDefined()) {
167 emulationCode = options->standardEmulationLib();
168 }
169
170 // ---- Run compiler ----
171 try {
172 InterPassData* ipData = new InterPassData;
173 int argc = 0;
174 std::string argv = options->getLLVMargv();
176 std::cout << "llvm args = " << argv << std::endl;
177 }
178 // Convert to char**
179 std::vector<char*> argv_array;
180 std::istringstream iss(argv);
181 std::string token;
182 while (iss >> token) {
183 char* arg = new char[token.size() + 1];
184 copy(token.begin(), token.end(), arg);
185 arg[token.size()] = '\0';
186 argv_array.push_back(arg);
187 argc++;
188 }
189 argv_array.push_back(0);
190 llvm::cl::ParseCommandLineOptions(
191 argc, argv_array.data(), "llvm flags\n");
192
193 LLVMBackend compiler(useInstalledVersion, options->tempDir());
194 compiler.setMachine(*mach);
195 TTAProgram::Program* seqProg =
196 compiler.compile(
197 bytecodeFile, emulationCode, optLevel, debug, ipData);
198
200 delete seqProg;
201 seqProg = NULL;
202
203 delete ipData;
204 ipData = NULL;
205
206 } catch (const CompileError& e) {
207 // CompilerErrors are related to the user program input and
208 // should be communicated to the programmer in a clean fashion.
209 Application::errorStream() << e.errorMessage() << std::endl;
210 } catch (const Exception& e) {
211 // Assume other Exceptions are due to like lack of more
212 // user friendly CompilerError or actual internal compiler errors.
213 std::cerr << "Error compiling '" << bytecodeFile << "':" << std::endl
214 << e.errorMessageStack() << std::endl;
215
216 return EXIT_FAILURE;
217 }
218
219 delete mach;
220 mach = NULL;
221
222 return EXIT_SUCCESS;
223}
#define IGNORE_COMPILER_WARNING(X)
#define POP_COMPILER_DIAGS
static std::string outputFileName(const std::string &adfFile)
Definition CreateBEM.cc:77
int main(int argc, char *argv[])
Definition LLVMTCE.cc:65
const int DEFAULT_OPT_LEVEL
Definition LLVMTCE.cc:53
POP_COMPILER_DIAGS const std::string DEFAULT_OUTPUT_FILENAME
Definition LLVMTCE.cc:52
static MachInfoCmdLineOptions options
Definition MachInfo.cc:46
TTAMachine::Machine * readMachine()
static void setCmdLineOptions(CmdLineOptions *options_)
static bool isInstalled()
static const int VERBOSE_LEVEL_INCREASED
Increased verbose level - print information about modules etc.
static void setVerboseLevel(const int level=VERBOSE_LEVEL_DEFAULT)
static std::ostream & errorStream()
static void initialize()
void parse(char *argv[], int argc)
virtual bool isVerboseSwitchDefined() const
virtual bool isVerboseSpamSwitchDefined() const
virtual std::string argument(int index) const
virtual int numberOfArguments() const
std::string errorMessageStack(bool messagesOnly=false) const
Definition Exception.cc:138
std::string errorMessage() const
Definition Exception.cc:123
static bool fileIsReadable(const std::string fileName)
static bool fileIsDirectory(const std::string fileName)
static bool fileIsWritable(const std::string fileName)
static bool fileIsCreatable(const std::string fileName)
static bool fileExists(const std::string fileName)
TTAProgram::Program * compile(const std::string &bytecodeFile, const std::string &emulationBytecodeFile, int optLevel, bool debug=false, InterPassData *ipData=NULL)
void setMachine(TTAMachine::Machine &target)
virtual void printHelp() const
static void writeToTPEF(const TTAProgram::Program &program, const std::string &tpefFileName)
Definition Program.cc:1169
void setSourceFile(const std::string &fileName)