OpenASIP  2.0
Static Public Member Functions | Private Member Functions | List of all members
CompileTools Class Reference

#include <CompileTools.hh>

Collaboration diagram for CompileTools:
Collaboration graph

Static Public Member Functions

static TTAProgram::ProgramcompileAsC (const TTAMachine::Machine &target, std::stringstream &code, const std::string &cflags="")
 
static TTAProgram::ProgramcompileAsC (const TTAMachine::Machine &target, const std::string &code, const std::string &cflags="")
 
static TTAProgram::ProgramcompileAsLLVM (const TTAMachine::Machine &target, const std::stringstream &code, const std::string compileOptions)
 

Private Member Functions

 CompileTools ()
 
virtual ~CompileTools ()
 

Detailed Description

Definition at line 51 of file CompileTools.hh.

Constructor & Destructor Documentation

◆ CompileTools()

CompileTools::CompileTools ( )
private

Definition at line 46 of file CompileTools.cc.

46  {
47 }

◆ ~CompileTools()

CompileTools::~CompileTools ( )
privatevirtual

Definition at line 49 of file CompileTools.cc.

49  {
50 }

Member Function Documentation

◆ compileAsC() [1/2]

TTAProgram::Program * CompileTools::compileAsC ( const TTAMachine::Machine target,
const std::string &  code,
const std::string &  cflags = "" 
)
static

Definition at line 53 of file CompileTools.cc.

56  {
57 
58  std::stringstream codeStream(code);
59  return compileAsC(target, codeStream, cflags);
60 }

References compileAsC().

Here is the call graph for this function:

◆ compileAsC() [2/2]

TTAProgram::Program * CompileTools::compileAsC ( const TTAMachine::Machine target,
std::stringstream &  code,
const std::string &  cflags = "" 
)
static

Interprets code as C code and tries to convert it to Program.

Returns
Pointer to new program if compilation is successful, otherwise NULL.

Definition at line 68 of file CompileTools.cc.

71  {
72  using namespace TTAProgram;
73 
74  Program* resultProgram = NULL;
75 
76  std::string tempDir = FileSystem::createTempDirectory(
78  if (tempDir.empty()) {
79  return NULL;
80  }
81  std::string tempAdfFile =
82  tempDir + FileSystem::DIRECTORY_SEPARATOR + "temp.adf";
83  std::string sourceCFile =
84  tempDir + FileSystem::DIRECTORY_SEPARATOR + "temp.c";
85  std::string resultFile =
86  tempDir + FileSystem::DIRECTORY_SEPARATOR + "temp.tpef";
87 
88  ADFSerializer serializer;
89  serializer.setDestinationFile(tempAdfFile);
90  try {
91  serializer.writeMachine(target);
92  } catch (const SerializerException& exception) {
94  return NULL;
95  }
96 
97  std::ofstream cStream(sourceCFile);
98  if (cStream.fail()) {
100  return NULL;
101  }
102  cStream << code.rdbuf();
103  cStream.close();
104 
105  std::string command = Environment::tceCompiler() + " "
106  "-a " + tempAdfFile + " " +
107  "-o " + resultFile + " " +
108  cflags + " " +
109  sourceCFile;
110  std::vector<std::string> output;
111 
112  if (Application::runShellCommandAndGetOutput(command, output) == 0 &&
113  FileSystem::fileExists(resultFile)) {
114  try {
115  resultProgram = Program::loadFromTPEF(resultFile, target);
116  } catch (Exception& e) {
118  return NULL;
119  }
120  }
121 
123  return resultProgram;
124 }

References FileSystem::createTempDirectory(), FileSystem::currentWorkingDir(), FileSystem::DIRECTORY_SEPARATOR, FileSystem::fileExists(), FileSystem::removeFileOrDirectory(), Application::runShellCommandAndGetOutput(), XMLSerializer::setDestinationFile(), Environment::tceCompiler(), and ADFSerializer::writeMachine().

Referenced by compileAsC().

Here is the call graph for this function:

◆ compileAsLLVM()

TTAProgram::Program * CompileTools::compileAsLLVM ( const TTAMachine::Machine target,
const std::stringstream &  code,
const std::string  compileOptions 
)
static

Interprets string stream as LLVM (.ll) assembly code and tries to convert it to Program.

Returns
Pointer to new program if compilation is successful, otherwise NULL.

Definition at line 133 of file CompileTools.cc.

136  {
137  using namespace TTAProgram;
138 
139  Program* resultProgram = NULL;
140 
141  std::string tempDir = FileSystem::createTempDirectory(
143  if (tempDir.empty()) {
144  return NULL;
145  }
146  std::string tempAdfFile =
147  tempDir + FileSystem::DIRECTORY_SEPARATOR + "temp.adf";
148  std::string sourceLLFile =
149  tempDir + FileSystem::DIRECTORY_SEPARATOR + "temp.ll";
150  std::string resultFile =
151  tempDir + FileSystem::DIRECTORY_SEPARATOR + "temp.tpef";
152 
153  ADFSerializer serializer;
154  serializer.setDestinationFile(tempAdfFile);
155  try {
156  serializer.writeMachine(target);
157  } catch (const SerializerException& exception) {
159  return NULL;
160  }
161 
162  std::ofstream llStream(sourceLLFile);
163  if (llStream.fail()) {
165  return NULL;
166  }
167  llStream << code.rdbuf();
168  llStream.close();
169 
170  std::string compileLLFilecmd = Environment::tceCompiler() + " "
171  + "-a " + tempAdfFile + " "
172  + "-o " + resultFile + " " + " "
173  + compileOptions + " "
174  + sourceLLFile;
175 
176  if (Application::runShellCommandSilently(compileLLFilecmd) == 0) {
177  try {
178  resultProgram = Program::loadFromTPEF(resultFile, target);
179  } catch (Exception& e) {
181  return NULL;
182  }
183  }
184 
186  return resultProgram;
187 }

References FileSystem::createTempDirectory(), FileSystem::currentWorkingDir(), FileSystem::DIRECTORY_SEPARATOR, FileSystem::removeFileOrDirectory(), Application::runShellCommandSilently(), XMLSerializer::setDestinationFile(), Environment::tceCompiler(), and ADFSerializer::writeMachine().

Here is the call graph for this function:

The documentation for this class was generated from the following files:
TTAProgram
Definition: Estimator.hh:65
TTAProgram::Program
Definition: Program.hh:63
FileSystem::removeFileOrDirectory
static bool removeFileOrDirectory(const std::string &path)
Definition: FileSystem.cc:493
Application::runShellCommandAndGetOutput
static int runShellCommandAndGetOutput(const std::string &command, std::vector< std::string > &outputLines, std::size_t maxOutputLines=DEFAULT_MAX_OUTPUT_LINES, bool includeStdErr=false)
Definition: Application.cc:338
CompileTools::compileAsC
static TTAProgram::Program * compileAsC(const TTAMachine::Machine &target, std::stringstream &code, const std::string &cflags="")
Definition: CompileTools.cc:68
Environment::tceCompiler
static std::string tceCompiler()
Definition: Environment.cc:928
ADFSerializer
Definition: ADFSerializer.hh:49
XMLSerializer::setDestinationFile
void setDestinationFile(const std::string &fileName)
Definition: XMLSerializer.cc:142
SerializerException
Definition: Exception.hh:675
Exception
Definition: Exception.hh:54
Application::runShellCommandSilently
static int runShellCommandSilently(const std::string &command)
Definition: Application.cc:301
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
FileSystem::fileExists
static bool fileExists(const std::string fileName)
ADFSerializer::writeMachine
void writeMachine(const TTAMachine::Machine &machine)
Definition: ADFSerializer.cc:259
FileSystem::currentWorkingDir
static std::string currentWorkingDir()
Definition: FileSystem.cc:142
FileSystem::createTempDirectory
static std::string createTempDirectory(const std::string &path="/tmp", const std::string &tempDirPrefix="tmp_tce_")
Definition: FileSystem.cc:441