chigraph  master
Systems programming language written for beginners in LLVM
CCompiler.cpp
Go to the documentation of this file.
1 
3 #include "chi/CCompiler.hpp"
4 #include "chi/BitcodeParser.hpp"
5 #include "chi/LLVMVersion.hpp"
7 #include "chi/Support/Result.hpp"
9 
10 #include <llvm/IR/Module.h>
11 #include <llvm/Support/MemoryBuffer.h>
12 
13 namespace fs = boost::filesystem;
14 
15 namespace chi {
16 
17 Result compileCToLLVM(const boost::filesystem::path& ctollvmPath, llvm::LLVMContext& llvmContext,
18  std::vector<std::string> arguments, boost::string_view inputCCode,
19  std::unique_ptr<llvm::Module>* toFill) {
20  assert(toFill != nullptr && "null toFill passed to compileCToLLVM");
21  assert(fs::is_regular_file(ctollvmPath) &&
22  "invalid path passed to compileCToLLVM for ctollvmPath");
23 
24  Result res;
25 
26  arguments.push_back("-nostdlib");
27 
28  // gather std include paths
29  std::vector<fs::path> stdIncludePaths;
30  res += stdCIncludePaths(stdIncludePaths);
31  if (!res) { return res; }
32 
33  for (const auto& p : stdIncludePaths) {
34  arguments.push_back("-I");
35  arguments.push_back(p.string());
36  }
37 
38  std::string errors;
39 
40  // call chi-ctollvm
41  std::unique_ptr<llvm::Module> mod;
42  {
43  // chi-ctollvm accepts args in the style of -c <arg1> -c <arg2> ...
44  std::vector<std::string> argsToChiCtoLLVM;
45  for (const auto& arg : arguments) {
46  argsToChiCtoLLVM.push_back("-c");
47  argsToChiCtoLLVM.push_back(arg);
48  }
49 
50  std::string generatedBitcode;
51  Subprocess ctollvmExe(ctollvmPath);
52  ctollvmExe.setArguments(argsToChiCtoLLVM);
53 
54  ctollvmExe.attachStringToStdOut(generatedBitcode);
55  ctollvmExe.attachStringToStdErr(errors);
56 
57  res += ctollvmExe.start();
58 
59  // push it the code and close the stream
60  res += ctollvmExe.pushToStdIn(inputCCode.data(), inputCCode.size());
61  res += ctollvmExe.closeStdIn();
62 
63  if (!res) { return res; }
64 
65  // wait for the exit
66  auto errCode = ctollvmExe.exitCode();
67 
68  if (errCode != 0) {
69  res.addEntry("EUKN", "Failed to Generate IR with clang", {{"Error", errors}});
70  return res;
71  }
72  if (!errors.empty()) {
73  res.addEntry("WUKN", "Warnings emitted while generating IR with clang",
74  {{"Warning", errors}});
75  }
76 
77  auto readCtx = res.addScopedContext(
78  {{"Error parsing bitcode file generated from clang", &inputCCode[0]}});
79  res += parseBitcodeString(generatedBitcode, llvmContext, toFill);
80  }
81 
82  if (*toFill == nullptr) {
83  res.addEntry("EUKN", "Failed to generate IR with clang", {{"Error", errors}});
84  } else if (!errors.empty()) {
85  res.addEntry("WUKN", "Warnings encountered while generating IR with clang",
86  {{"Error", errors}});
87  }
88 
89  return res;
90 }
91 
92 } // namepsace chi
void attachStringToStdOut(std::string &str)
Attach a string to stdout.
Definition: Subprocess.hpp:135
void addEntry(const char *ec, const char *overview, nlohmann::json data)
Add a entry to the result, either a warning or an error.
Definition: Result.cpp:52
Defines the Result class and related functions.
Result stdCIncludePaths(std::vector< boost::filesystem::path > &toFill)
Gets the location of the standard C library to include.
int exitCode()
Wait and gets the exit code.
Definition: Subprocess.cpp:534
void attachStringToStdErr(std::string &str)
Attach a string to stderr.
Definition: Subprocess.hpp:146
Result closeStdIn()
Close the STDIN stream (send an EOF)
Definition: Subprocess.cpp:388
Result pushToStdIn(const char *data, size_t size)
Pushes data to the stdin stream of the child process.
Definition: Subprocess.cpp:374
Provides an platform-independent abstraction for creating subprocesses.
Definition: Subprocess.hpp:52
ScopedContext addScopedContext(const nlohmann::json &data)
Add a context with a scope Example usage: chi::Result res; res.contextJson(); // returns {} { aut...
Definition: Result.hpp:123
Result start()
Start the process.
Definition: Subprocess.cpp:405
void setArguments(const ForwardIterator &begin, const ForwardIterator &end)
Set the arguments for the program with a begin and end iterator.
Definition: Subprocess.hpp:75
Result compileCToLLVM(const boost::filesystem::path &ctollvmPath, llvm::LLVMContext &llvmContext, std::vector< std::string > arguments, boost::string_view inputCCode, std::unique_ptr< llvm::Module > *toFill)
Use chi-ctollvm to compile C source code to a llvm module It also uses stdCIncludePaths to find basic...
Definition: CCompiler.cpp:17
The namespace where chigraph lives.
The result object, used for identifiying errors with good diagnostics.
Definition: Result.hpp:72