ETISS 0.8.0
Extendable Translating Instruction Set Simulator (version 0.8.0)
GCCJIT.cpp
Go to the documentation of this file.
1 /*
2 
3  @copyright
4 
5  <pre>
6 
7  Copyright 2018 Infineon Technologies AG
8 
9  This file is part of ETISS tool, see <https://github.com/tum-ei-eda/etiss>.
10 
11  The initial version of this software has been created with the funding support by the German Federal
12  Ministry of Education and Research (BMBF) in the project EffektiV under grant 01IS13022.
13 
14  Redistribution and use in source and binary forms, with or without modification, are permitted
15  provided that the following conditions are met:
16 
17  1. Redistributions of source code must retain the above copyright notice, this list of conditions and
18  the following disclaimer.
19 
20  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
21  and the following disclaimer in the documentation and/or other materials provided with the distribution.
22 
23  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
24  or promote products derived from this software without specific prior written permission.
25 
26  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
27  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
28  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
29  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  POSSIBILITY OF SUCH DAMAGE.
34 
35  </pre>
36 
37  @author Chair of Electronic Design Automation, TUM
38 
39  @version 0.1
40 
41 */
42 
43 #include "GCCJIT.h"
44 
45 #include <cstdlib>
46 #include <cstring>
47 #include <dlfcn.h>
48 #include <fstream>
49 #include <iostream>
50 #include <sstream>
51 #include <stdlib.h> //mkdtemp
52 #include <unistd.h>
53 
54 GCCJIT::GCCJIT(bool cleanup) : etiss::JIT("gcc"), cleanup_(cleanup)
55 {
56 
57  id = 0;
58 
59  if (system(std::string("mkdir -p \"./tmp\"").c_str()))
60  std::cerr << "ERROR: GCCJIT failed to create ./tmp folder. this may lead to a failure to compile code."
61  << std::endl;
62 
63  path_ = "./tmp/XXXXXX";
64  // create unique folder (mkdtemp)
65  {
66  if (mkdtemp(&path_[0]) == 0)
67  {
68  std::cerr << "ERROR: GCCJIT failed to get unique working folder. Resulting compilations my be unrelyable"
69  << std::endl;
70  }
71 
72  if (system(std::string("mkdir -p \"" + path_ + "\"").c_str())) // make folder if not present
73  std::cerr << "ERROR: GCCJIT failed to create compilation path. this may lead to a failure to compile code."
74  << std::endl;
75  }
76  path_ = path_ + "/";
77 }
78 
80 {
81  if (cleanup_)
82  if (path_.substr(0, 6) == "./tmp/") // check path before recursive delete operation
83  if (system(std::string("rm -R \"" + path_ + "\"").c_str()))
84  std::cerr << "ERROR: GCCJIT failed to clean up compilation files located in " << path_ << std::endl;
85 }
86 
87 void *GCCJIT::translate(std::string code, std::set<std::string> headerpaths, std::set<std::string> librarypaths,
88  std::set<std::string> libraries, std::string &error, bool debug)
89 {
90 
91  if (system(NULL) == 0)
92  {
93  error = "system command execution not available";
94  return 0;
95  }
96 
97  unsigned lid = id++;
98 
99  std::string codefilename;
100  {
101  std::ofstream codeFile;
102  std::stringstream ss;
103  ss << "code_" << lid;
104  codefilename = ss.str();
105  codeFile.open((path_ + codefilename + std::string(".c")).c_str());
106  if (!codeFile.is_open())
107  {
108  error = "failed to create code file";
109  return 0;
110  }
111  codeFile << code;
112  codeFile.flush();
113  codeFile.close();
114  }
115  std::stringstream ss;
116  ss << "gcc -c -std=c99 -fPIC -march=native -mtune=native -pipe ";
117  if (debug)
118  ss << "-g -O0 -Wall -Wno-unused-label ";
119  else
120  ss << "-Ofast ";
121  for (std::set<std::string>::const_iterator iter = headerpaths.begin(); iter != headerpaths.end(); iter++)
122  {
123  ss << "-I\"" << *iter << "\" ";
124  }
125  ss << path_ << codefilename << ".c"
126  << " -o " << path_ << codefilename << ".o";
127 
128  // std::cout << "EXECUTING: " << ss.str() << std::endl;
129 
130  int eval = system(ss.str().c_str());
131  // std::cout << eval << std::endl;
132 
133  if (eval != 0)
134  {
135  std::cout << "compiler failed with code: " << eval << std::endl;
136  }
137 
138  ss.str("");
139 
140  ss << "gcc -shared ";
141  /*
142  if (debug)
143  ss <<"-g -dl ";
144  */
145 
146  for (std::set<std::string>::const_iterator iter = librarypaths.begin();iter != librarypaths.end();iter++){
147  ss << " -L" << *iter << " ";
148  }
149 
150  ss << " -o " << path_ << "lib" << codefilename << ".so " << path_ << codefilename << ".o ";
151 
152 
153  for (std::set<std::string>::const_iterator iter = libraries.begin();iter != libraries.end();iter++){
154  ss << " -l\"" << *iter << "\" ";
155  }
156 
157  if(!librarypaths.empty())
158  ss << "-Wl";
159 
160  for (std::set<std::string>::const_iterator iter = librarypaths.begin();iter != librarypaths.end();iter++){
161  ss << ",-rpath," << *iter;
162  }
163 
164  //std::cout << "EXECUTING: " << ss.str() << std::endl;
165  eval = system(ss.str().c_str());
166  //std::cout << eval << std::endl;
167 
168  if (eval != 0)
169  {
170  std::cout << "compiler failed with code: " << eval << std::endl;
171  }
172 
173  void *lib = dlopen((path_ + "lib" + codefilename + std::string(".so")).c_str(), RTLD_NOW | RTLD_LOCAL);
174 
175  if (lib == 0)
176  {
177  error = dlerror();
178  return 0;
179  }
180 
181  return lib;
182 }
183 void *GCCJIT::getFunction(void *handle, std::string name, std::string &error)
184 {
185  void *ret = dlsym(handle, name.c_str());
186  if (ret == 0)
187  {
188  error = dlerror();
189  return 0;
190  }
191  return ret;
192 }
193 void GCCJIT::free(void *handle)
194 {
195  dlclose(handle);
196 }
bool cleanup_
Definition: GCCJIT.h:63
virtual void free(void *handle)
clean up handled returned by etiss::JIT::translate
Definition: GCCJIT.cpp:193
virtual void * translate(std::string code, std::set< std::string > headerpaths, std::set< std::string > librarypaths, std::set< std::string > libraries, std::string &error, bool debug=false)
translate C code to executable code and return a handle/pointer that identifies the compilation resul...
Definition: GCCJIT.cpp:87
std::string path_
Definition: GCCJIT.h:62
GCCJIT(bool cleanup=true)
Definition: GCCJIT.cpp:54
virtual void * getFunction(void *handle, std::string name, std::string &error)
returns a function pointer to a compiled function from the handle returned by etiss::JIT::translate
Definition: GCCJIT.cpp:183
virtual ~GCCJIT()
Definition: GCCJIT.cpp:79
Page Table Entry (PTE) defines the composition of Page Frame Number (PFN) and relavant flags.
Definition: Benchmark.h:53
#define NULL