Intel SPMD Program Compiler  1.9.1
module.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010-2015, Intel Corporation
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11 
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15 
16  * Neither the name of Intel Corporation nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 /** @file module.h
35  @brief %Declaration of the Module class, which is the ispc-side representation
36  of the results of compiling a source file.
37  */
38 
39 #ifndef ISPC_MODULE_H
40 #define ISPC_MODULE_H 1
41 
42 #include "ispc.h"
43 #include "ast.h"
44 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_4
45  #include <llvm/DebugInfo.h>
46 #elif ISPC_LLVM_VERSION >= ISPC_LLVM_3_5
47  #include <llvm/IR/DebugInfo.h>
48 #endif
49 
50 namespace llvm
51 {
52  class raw_string_ostream;
53 }
54 
55 struct DispatchHeaderInfo;
56 
57 class Module {
58 public:
59  /** The name of the source file being compiled should be passed as the
60  module name. */
61  Module(const char *filename);
62 
63  /** Compiles the source file passed to the Module constructor, adding
64  its global variables and functions to both the llvm::Module and
65  SymbolTable. Returns the number of errors during compilation. */
66  int CompileFile();
67 
68  /** Add a named type definition to the module. */
69  void AddTypeDef(const std::string &name, const Type *type,
70  SourcePos pos);
71 
72  /** Add a new global variable corresponding to the given Symbol to the
73  module. If non-NULL, initExpr gives the initiailizer expression
74  for the global's inital value. */
75  void AddGlobalVariable(const std::string &name, const Type *type,
76  Expr *initExpr, bool isConst,
77  StorageClass storageClass, SourcePos pos);
78 
79  /** Add a declaration of the function defined by the given function
80  symbol to the module. */
81  void AddFunctionDeclaration(const std::string &name,
82  const FunctionType *ftype,
83  StorageClass sc, bool isInline, SourcePos pos);
84 
85  /** Adds the function described by the declaration information and the
86  provided statements to the module. */
87  void AddFunctionDefinition(const std::string &name,
88  const FunctionType *ftype, Stmt *code);
89 
90  /** Adds the given type to the set of types that have their definitions
91  included in automatically generated header files. */
92  void AddExportedTypes(const std::vector<std::pair<const Type *,
93  SourcePos> > &types);
94 
95  /** After a source file has been compiled, output can be generated in a
96  number of different formats. */
97  enum OutputType { Asm, /** Generate text assembly language output */
98  Bitcode, /** Generate LLVM IR bitcode output */
99  Object, /** Generate a native object file */
100  CXX, /** Generate a C++ file */
101  Header, /** Generate a C/C++ header file with
102  declarations of 'export'ed functions, global
103  variables, and the types used by them. */
104  Deps, /** generate dependencies */
105  DevStub, /** generate device-side offload stubs */
106  HostStub /** generate host-side offload stubs */
107  };
108 
109  /** Compile the given source file, generating assembly, object file, or
110  LLVM bitcode output, as well as (optionally) a header file with
111  declarations of functions and types used in the ispc/application
112  interface.
113  @param srcFile Pathname to ispc source file to compile
114  @param arch %Target architecture (e.g. "x86-64")
115  @param cpu %Target CPU (e.g. "core-i7")
116  @param targets %Target ISAs; this parameter may give a single target
117  ISA, or may give a comma-separated list of them in
118  case we are compiling to multiple ISAs.
119  @param generatePIC Indicates whether position-independent code should
120  be generated.
121  @param outputType %Type of output to generate (object files, assembly,
122  LLVM bitcode.)
123  @param outFileName Base name of output filename for object files, etc.
124  If for example the multiple targets "sse2" and "avx"
125  are specified in the "targets" parameter and if this
126  parameter is "foo.o", then we'll generate multiple
127  output files, like "foo.o", "foo_sse2.o", "foo_avx.o".
128  @param headerFileName If non-NULL, emit a header file suitable for
129  inclusion from C/C++ code with declarations of
130  types and functions exported from the given ispc
131  source file.
132  @param includeFileName If non-NULL, gives the filename for the C++
133  backend to emit in an #include statement to
134  get definitions of the builtins for the generic
135  target.
136  @return Number of errors encountered when compiling
137  srcFile.
138  */
139  static int CompileAndOutput(const char *srcFile, const char *arch,
140  const char *cpu, const char *targets,
141  bool generatePIC,
142  OutputType outputType,
143  const char *outFileName,
144  const char *headerFileName,
145  const char *includeFileName,
146  const char *depsFileName,
147  const char *hostStubFileName,
148  const char *devStubFileName);
149 
150  /** Total number of errors encountered during compilation. */
152 
153  /** Symbol table to hold symbols visible in the current scope during
154  compilation. */
156 
157  /** llvm Module object into which globals and functions are added. */
158  llvm::Module *module;
159 
160  /** The diBuilder manages generating debugging information */
161  llvm::DIBuilder *diBuilder;
162 
163 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_4 && ISPC_LLVM_VERSION <= ISPC_LLVM_3_6
164  llvm::DICompileUnit diCompileUnit;
165 #elif ISPC_LLVM_VERSION >= ISPC_LLVM_3_7
166  llvm::DICompileUnit* diCompileUnit;
167 #endif // LLVM_3_4+
168 
169 private:
170  const char *filename;
172 
173  std::vector<std::pair<const Type *, SourcePos> > exportedTypes;
174 
175  /** Write the corresponding output type to the given file. Returns
176  true on success, false if there has been an error. The given
177  filename may be NULL, indicating that output should go to standard
178  output. */
179  bool writeOutput(OutputType ot, const char *filename,
180  const char *includeFileName = NULL,
181  DispatchHeaderInfo *DHI = 0);
182  bool writeHeader(const char *filename);
184  bool writeDeps(const char *filename);
185  bool writeDevStub(const char *filename);
186  bool writeHostStub(const char *filename);
187  bool writeObjectFileOrAssembly(OutputType outputType, const char *filename);
188  static bool writeObjectFileOrAssembly(llvm::TargetMachine *targetMachine,
189  llvm::Module *module, OutputType outputType,
190  const char *outFileName);
191  static bool writeBitcode(llvm::Module *module, const char *outFileName);
192 
193  void execPreprocessor(const char *infilename, llvm::raw_string_ostream* ostream) const;
194 };
195 
196 #endif // ISPC_MODULE_H
Definition: ast.h:144
Definition: ispc.h:74
void AddTypeDef(const std::string &name, const Type *type, SourcePos pos)
Definition: module.cpp:522
std::vector< std::pair< const Type *, SourcePos > > exportedTypes
Definition: module.h:173
bool writeDevStub(const char *filename)
Definition: module.cpp:1941
Interface class for statements in the ispc language.
Definition: stmt.h:49
bool writeOutput(OutputType ot, const char *filename, const char *includeFileName=NULL, DispatchHeaderInfo *DHI=0)
Definition: module.cpp:1172
Symbol table that holds all known symbols during parsing and compilation.
Definition: sym.h:119
OutputType
Definition: module.h:97
bool writeHeader(const char *filename)
Definition: module.cpp:2201
bool writeObjectFileOrAssembly(OutputType outputType, const char *filename)
Definition: module.cpp:1444
bool writeHostStub(const char *filename)
Definition: module.cpp:2079
bool writeDeps(const char *filename)
Definition: module.cpp:1888
llvm::Module * module
Definition: module.h:158
void AddExportedTypes(const std::vector< std::pair< const Type *, SourcePos > > &types)
Definition: module.cpp:1156
static bool writeBitcode(llvm::Module *module, const char *outFileName)
Definition: module.cpp:1376
void AddFunctionDeclaration(const std::string &name, const FunctionType *ftype, StorageClass sc, bool isInline, SourcePos pos)
Definition: module.cpp:876
Definition: module.h:57
int CompileFile()
Definition: module.cpp:455
Module(const char *filename)
Definition: module.cpp:383
Representation of a range of positions in a source file.
Definition: ispc.h:134
StorageClass
Definition: ispc.h:118
void execPreprocessor(const char *infilename, llvm::raw_string_ostream *ostream) const
Definition: module.cpp:2434
void AddFunctionDefinition(const std::string &name, const FunctionType *ftype, Stmt *code)
Definition: module.cpp:1134
Type representing a function (return type + argument types)
Definition: type.h:883
Interface class that defines the type abstraction.
Definition: type.h:101
Expr is the abstract base class that defines the interface that all expression types must implement...
Definition: expr.h:48
void AddGlobalVariable(const std::string &name, const Type *type, Expr *initExpr, bool isConst, StorageClass storageClass, SourcePos pos)
Definition: module.cpp:531
int errorCount
Definition: module.h:151
const char * filename
Definition: module.h:170
llvm::DIBuilder * diBuilder
Definition: module.h:161
Main ispc.header file. Defines Target, Globals and Opt classes.
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)
Definition: module.cpp:3046
SymbolTable * symbolTable
Definition: module.h:155
AST * ast
Definition: module.h:171
bool writeDispatchHeader(DispatchHeaderInfo *DHI)
Definition: module.cpp:2314