OpenASIP  2.0
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"
35 #include "LLVMTCECmdLineOptions.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"
43 IGNORE_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 
52 const std::string DEFAULT_OUTPUT_FILENAME = "out.tpef";
53 const 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  */
64 int
65 main(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) {
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) {
82  options->printHelp();
83  return EXIT_FAILURE;
84  }
85 
86  if (options->tempDir() == "") {
88  << "Temporary directory required." << std::endl;
89  options->printHelp();
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  TTAProgram::Program* seqProg =
195  compiler.compile(
196  bytecodeFile, emulationCode, *mach, optLevel, debug, ipData);
197 
199  delete seqProg;
200  seqProg = NULL;
201 
202  delete ipData;
203  ipData = NULL;
204 
205  } catch (const CompileError& e) {
206  // CompilerErrors are related to the user program input and
207  // should be communicated to the programmer in a clean fashion.
208  Application::errorStream() << e.errorMessage() << std::endl;
209  } catch (const Exception& e) {
210  // Assume other Exceptions are due to like lack of more
211  // user friendly CompilerError or actual internal compiler errors.
212  std::cerr << "Error compiling '" << bytecodeFile << "':" << std::endl
213  << e.errorMessageStack() << std::endl;
214 
215  return EXIT_FAILURE;
216  }
217 
218  delete mach;
219  mach = NULL;
220 
221  return EXIT_SUCCESS;
222 }
TTAProgram::Program
Definition: Program.hh:63
MachInfoCmdLineOptions::printHelp
virtual void printHelp() const
Definition: MachInfoCmdLineOptions.cc:89
FileSystem.hh
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
CmdLineParser::numberOfArguments
virtual int numberOfArguments() const
LLVMBackend::compile
TTAProgram::Program * compile(const std::string &bytecodeFile, const std::string &emulationBytecodeFile, TTAMachine::Machine &target, int optLevel, bool debug=false, InterPassData *ipData=NULL)
Definition: LLVMBackend.cc:360
Application::setVerboseLevel
static void setVerboseLevel(const int level=VERBOSE_LEVEL_DEFAULT)
Definition: Application.hh:186
Application::setCmdLineOptions
static void setCmdLineOptions(CmdLineOptions *options_)
Definition: Application.cc:381
CompileError
Definition: Exception.hh:1019
IllegalCommandLine
Definition: Exception.hh:438
FileSystem::fileIsCreatable
static bool fileIsCreatable(const std::string fileName)
Definition: FileSystem.cc:123
FileSystem::fileIsDirectory
static bool fileIsDirectory(const std::string fileName)
FileSystem::fileIsWritable
static bool fileIsWritable(const std::string fileName)
LLVMTCECmdLineOptions.hh
ADFSerializer
Definition: ADFSerializer.hh:49
Application.hh
InterPassData
Definition: InterPassData.hh:48
Exception::errorMessageStack
std::string errorMessageStack(bool messagesOnly=false) const
Definition: Exception.cc:138
main
int main(int argc, char *argv[])
Definition: LLVMTCE.cc:65
Machine.hh
CmdLineOptions::isVerboseSwitchDefined
virtual bool isVerboseSwitchDefined() const
Definition: CmdLineOptions.cc:300
Exception
Definition: Exception.hh:54
Application::VERBOSE_LEVEL_INCREASED
static const int VERBOSE_LEVEL_INCREASED
Increased verbose level - print information about modules etc.
Definition: Application.hh:224
LLVMTCECmdLineOptions
Definition: LLVMTCECmdLineOptions.hh:48
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
DEFAULT_OPT_LEVEL
const int DEFAULT_OPT_LEVEL
Definition: LLVMTCE.cc:53
Application::isInstalled
static bool isInstalled()
Definition: Application.cc:522
IGNORE_COMPILER_WARNING
#define IGNORE_COMPILER_WARNING(X)
Definition: CompilerWarnings.hh:51
ADFSerializer.hh
Program.hh
Application::errorStream
static std::ostream & errorStream()
Definition: Application.cc:171
InterPassData.hh
FileSystem::fileExists
static bool fileExists(const std::string fileName)
Application::initialize
static void initialize()
Definition: Application.cc:99
TTAProgram::Program::writeToTPEF
static void writeToTPEF(const TTAProgram::Program &program, const std::string &tpefFileName)
Definition: Program.cc:1169
POP_COMPILER_DIAGS
#define POP_COMPILER_DIAGS
Definition: CompilerWarnings.hh:68
ADFSerializer::readMachine
TTAMachine::Machine * readMachine()
Definition: ADFSerializer.cc:275
LLVMBackend
Definition: LLVMBackend.hh:63
FileSystem::fileIsReadable
static bool fileIsReadable(const std::string fileName)
CmdLineParser::argument
virtual std::string argument(int index) const
DEFAULT_OUTPUT_FILENAME
const POP_COMPILER_DIAGS std::string DEFAULT_OUTPUT_FILENAME
Definition: LLVMTCE.cc:52
CompilerWarnings.hh
TTAMachine::Machine
Definition: Machine.hh:73
LLVMBackend.hh
CmdLineOptions::isVerboseSpamSwitchDefined
virtual bool isVerboseSpamSwitchDefined() const
Definition: CmdLineOptions.cc:310