summaryrefslogtreecommitdiff
path: root/src/compiler/compiler.cpp
blob: c41a6cdcfa45e94d1c21fc8011ca3a5dfbdf2287 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "compiler.h"

#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Frontend/CompilerInvocation.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/DiagnosticOptions.h>
#include <clang/Frontend/HeaderSearchOptions.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>

#include <iostream>
#include <fstream>

using namespace Coal;
using namespace clang;

static void
setupCodeGenOpts(CodeGenOptions &opts)
{
   opts.DebugInfo = true;
   //opts.OptimizationLevel = 2;
   //opts.MainFileName = "main.cl";
   //opts.UnrollLoops = true;
   opts.AsmVerbose = true;
}

static void
setupDiagnosticOpts(DiagnosticOptions &opts)
{
   opts.Pedantic = true;
   opts.ShowColumn = true;
   opts.ShowLocation = true;
   opts.ShowCarets = true;
   opts.ShowFixits = true;
   opts.ShowColors = true;
   opts.ErrorLimit = 19;
   opts.MessageLength = 80;
   //opts.Warnings[0] = "all";
}

static void
setupFrontendOpts(FrontendOptions &opts, const std::string &fileLoc)
{
   opts.ProgramAction = frontend::EmitLLVMOnly;
   opts.DisableFree = true;

   /* XXX HACK
    * just matches whatever garbage Compiler::prepareInput
    * creates */
   opts.Inputs.push_back(
      std::make_pair(IK_OpenCL, fileLoc));
}

static void
setupHeaderSearchOpts(HeaderSearchOptions &opts)
{
   opts.Verbose = true;
   opts.UseBuiltinIncludes = false;
   opts.UseStandardIncludes = false;
   opts.UseStandardCXXIncludes = false;

   std::string buildPath(COAL_BUILD_DIR);
   buildPath += "/src/builtins";
   opts.AddPath(buildPath,
                frontend::Angled, true,
                /*IsFramework=*/ false, true);
   std::string installPath(COAL_INSTALL_DIR);
   installPath += "/include/coal";
   opts.AddPath(installPath,
                frontend::Angled, true,
                /*IsFramework=*/ false, true);
}

static void
setupLangOpts(LangOptions &opts)
{
   opts.NoBuiltin = true;
}

static void
setupPreprocessorOpts(PreprocessorOptions &opts)
{
   opts.Includes.push_back("coal-internal.h");
}

static void
setupTargetOpts(TargetOptions &opts)
{
   opts.Triple = llvm::sys::getHostTriple();
   //opts.CPU =
}


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

Compiler::~Compiler()
{

}

bool Compiler::init()
{
   std::string tempDir = llvm::sys::Path::GetTemporaryDirectory().str();
   m_tempFileLocation = tempDir + "/main.cl";

   m_clang.setLLVMContext(new llvm::LLVMContext);

   // Create the compilers actual diagnostics engine.
   m_clang.createDiagnostics(0 ,NULL);
   if (!m_clang.hasDiagnostics())
      return 0;

   setupHeaderSearchOpts(m_clang.getHeaderSearchOpts());
   setupPreprocessorOpts(m_clang.getPreprocessorOpts());
   setupCodeGenOpts(m_clang.getCodeGenOpts());
   setupDiagnosticOpts(m_clang.getDiagnosticOpts());
   setupFrontendOpts(m_clang.getFrontendOpts(), m_tempFileLocation);
   setupLangOpts(m_clang.getLangOpts());
   setupTargetOpts(m_clang.getTargetOpts());
}

llvm::Module * Compiler::compile(const std::string &text)
{
   llvm::Module *module;
   /* setup the input file */
   prepareInput(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;

   module = act->takeModule();
   module->dump();
   return module;
}


/** XXX
 *  this needs to go. it's a nasty, nasty hack.
 *  It's also a serious security threat!
 */
void Compiler::prepareInput(const std::string &text)
{
   std::string error;

   std::cerr << "Output path = " << m_tempFileLocation.c_str();
   std::cerr << "Prepare input = "<<text << std::endl;

   std::fstream stream(m_tempFileLocation.c_str(), std::ios::out | std::ios::trunc);
   stream << text;
   stream.close();
}