forked from ispc/ispc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.h
201 lines (168 loc) · 8.62 KB
/
module.h
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
Copyright (c) 2010-2015, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @file module.h
@brief %Declaration of the Module class, which is the ispc-side representation
of the results of compiling a source file.
*/
#ifndef ISPC_MODULE_H
#define ISPC_MODULE_H 1
#include "ispc.h"
#include "ast.h"
#if ISPC_LLVM_VERSION == ISPC_LLVM_3_4
#include <llvm/DebugInfo.h>
#elif ISPC_LLVM_VERSION >= ISPC_LLVM_3_5
#include <llvm/IR/DebugInfo.h>
#endif
namespace llvm
{
class raw_string_ostream;
class MemoryBuffer;
#ifdef ISPC_LIBISPC_ENABLED
class ExecutionEngine;
#endif
}
struct DispatchHeaderInfo;
class Module {
public:
/** The name of the source file being compiled should be passed as the
module name. */
Module(const char *filename);
/** Compiles the source file passed to the Module constructor, adding
its global variables and functions to both the llvm::Module and
SymbolTable. Returns the number of errors during compilation. */
int CompileFile();
#ifdef ISPC_LIBISPC_ENABLED
/** Compile JIT the src string. */
static int CompileAndJIT(const char* src);
/** Get the address of JIT function by name. */
static uint64_t GetFunctionAddress(const std::string& name);
#endif
/** Add a named type definition to the module. */
void AddTypeDef(const std::string &name, const Type *type,
SourcePos pos);
/** Add a new global variable corresponding to the given Symbol to the
module. If non-NULL, initExpr gives the initiailizer expression
for the global's inital value. */
void AddGlobalVariable(const std::string &name, const Type *type,
Expr *initExpr, bool isConst,
StorageClass storageClass, SourcePos pos);
/** Add a declaration of the function defined by the given function
symbol to the module. */
void AddFunctionDeclaration(const std::string &name,
const FunctionType *ftype,
StorageClass sc, bool isInline, SourcePos pos);
/** Adds the function described by the declaration information and the
provided statements to the module. */
void AddFunctionDefinition(const std::string &name,
const FunctionType *ftype, Stmt *code);
/** Adds the given type to the set of types that have their definitions
included in automatically generated header files. */
void AddExportedTypes(const std::vector<std::pair<const Type *,
SourcePos> > &types);
/** Compile the given source file, generating assembly, object file, or
LLVM bitcode output, as well as (optionally) a header file with
declarations of functions and types used in the ispc/application
interface.
@param srcFile Pathname to ispc source file to compile
@param arch %Target architecture (e.g. "x86-64")
@param cpu %Target CPU (e.g. "core-i7")
@param targets %Target ISAs; this parameter may give a single target
ISA, or may give a comma-separated list of them in
case we are compiling to multiple ISAs.
@param generatePIC Indicates whether position-independent code should
be generated.
@param outputType %Type of output to generate (object files, assembly,
LLVM bitcode.)
@param outFileName Base name of output filename for object files, etc.
If for example the multiple targets "sse2" and "avx"
are specified in the "targets" parameter and if this
parameter is "foo.o", then we'll generate multiple
output files, like "foo.o", "foo_sse2.o", "foo_avx.o".
@param headerFileName If non-NULL, emit a header file suitable for
inclusion from C/C++ code with declarations of
types and functions exported from the given ispc
source file.
@param includeFileName If non-NULL, gives the filename for the C++
backend to emit in an #include statement to
get definitions of the builtins for the generic
target.
@return Number of errors encountered when compiling
srcFile.
*/
static int CompileAndOutput(const char *srcFile, const char *arch,
const char *cpu, const char *targets,
bool generatePIC,
OutputType outputType,
const char *outFileName,
const char *headerFileName,
const char *includeFileName,
const char *depsFileName,
const char *hostStubFileName,
const char *devStubFileName);
/** Total number of errors encountered during compilation. */
int errorCount;
/** Symbol table to hold symbols visible in the current scope during
compilation. */
SymbolTable *symbolTable;
/** llvm Module object into which globals and functions are added. */
std::unique_ptr<llvm::Module> module;
/** The diBuilder manages generating debugging information */
llvm::DIBuilder *diBuilder;
#if ISPC_LLVM_VERSION >= ISPC_LLVM_3_4 && ISPC_LLVM_VERSION <= ISPC_LLVM_3_6
llvm::DICompileUnit diCompileUnit;
#elif ISPC_LLVM_VERSION >= ISPC_LLVM_3_7
llvm::DICompileUnit* diCompileUnit;
#endif // LLVM_3_4+
private:
const char *filename;
AST *ast;
std::vector<std::pair<const Type *, SourcePos> > exportedTypes;
#ifdef ISPC_LIBISPC_ENABLED
std::unique_ptr<llvm::ExecutionEngine> executionEngine;
#endif
/** Write the corresponding output type to the given file. Returns
true on success, false if there has been an error. The given
filename may be NULL, indicating that output should go to standard
output. */
bool writeOutput(OutputType ot, const char *filename,
const char *includeFileName = NULL,
DispatchHeaderInfo *DHI = 0);
bool writeHeader(const char *filename);
bool writeDispatchHeader(DispatchHeaderInfo *DHI);
bool writeDeps(const char *filename);
bool writeDevStub(const char *filename);
bool writeHostStub(const char *filename);
bool writeObjectFileOrAssembly(OutputType outputType, const char *filename);
static bool writeObjectFileOrAssembly(llvm::TargetMachine *targetMachine,
llvm::Module *module, OutputType outputType,
const char *outFileName);
static bool writeBitcode(llvm::Module *module, const char *outFileName);
void execPreprocessor(llvm::MemoryBuffer* srcbuf,
llvm::raw_string_ostream* ostream) const;
int compile(std::unique_ptr<llvm::MemoryBuffer> srcbuf);
};
#endif // ISPC_MODULE_H