summaryrefslogtreecommitdiff
path: root/src/compiler/compiler.cpp
blob: 38e552dd29f24abc2ae85c4b563cbfa9de84cdf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "compiler.h"

#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Driver/Compilation.h>
#include <clang/Driver/Driver.h>
#include <clang/Driver/Tool.h>
#include <clang/Frontend/CompilerInvocation.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/DiagnosticOptions.h>
#include <clang/Frontend/FrontendDiagnostic.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>

#include <llvm/LLVMContext.h>
#include <llvm/Module.h>
#include <llvm/Config/config.h>
#include <llvm/ADT/OwningPtr.h>
#include <llvm/ADT/SmallString.h>
#include <llvm/Config/config.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/Support/ManagedStatic.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/System/Host.h>
#include <llvm/System/Path.h>
#include <llvm/Target/TargetSelect.h>

using namespace Coal;
using namespace clang;


Compiler::Compiler()
{
   init();
}

Compiler::~Compiler()
{

}

void Compiler::init()
{
#if 0
   // Initialize a compiler invocation object from the clang (-cc1) arguments.
   const driver::ArgStringList &CCArgs = Cmd->getArguments();
   llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
   CompilerInvocation::CreateFromArgs(*CI,
                                      const_cast<const char **>(CCArgs.data()),
                                      const_cast<const char **>(CCArgs.data()) +
                                      CCArgs.size(),
                                      Diags);

   // Show the invocation, with -v.
   if (CI->getHeaderSearchOpts().Verbose) {
      llvm::errs() << "clang invocation:\n";
      C->PrintJob(llvm::errs(), C->getJobs(), "\n", true);
      llvm::errs() << "\n";
   }

   // FIXME: This is copied from cc1_main.cpp; simplify and eliminate.

   // Create a compiler instance to handle the actual work.
   CompilerInstance Clang;
   Clang.setLLVMContext(new llvm::LLVMContext);
   Clang.setInvocation(CI.take());

   // Create the compilers actual diagnostics engine.
   Clang.createDiagnostics(int(CCArgs.size()),const_cast<char**>(
                              CCArgs.data()));
   if (!Clang.hasDiagnostics())
      return 1;

   // Infer the builtin include path if unspecified.
   if (Clang.getHeaderSearchOpts().UseBuiltinIncludes &&
       Clang.getHeaderSearchOpts().ResourceDir.empty())
      Clang.getHeaderSearchOpts().ResourceDir =
         CompilerInvocation::GetResourcesPath(argv[0], MainAddr);
#endif
}

llvm::Module * Compiler::compile(const std::string &text)
{
   // Create and execute the frontend to generate an LLVM bitcode module.
   llvm::OwningPtr<CodeGenAction> act(new EmitLLVMOnlyAction());
   if (!m_clang.ExecuteAction(*act))
      return 0;

   return act->takeModule();
}