Intel SPMD Program Compiler  1.10.0
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  enum OutputFlags : int { NoFlags = 0,
110  GeneratePIC = 0x1,
111  GenerateFlatDeps = 0x2, /** Dependencies will be output as a flat list. */
112  GenerateMakeRuleForDeps = 0x4, /** Dependencies will be output in a make rule format instead of a flat list. */
113  OutputDepsToStdout = 0x8, /** Dependency information will be output to stdout instead of file. */
114  };
115 
116  /** Compile the given source file, generating assembly, object file, or
117  LLVM bitcode output, as well as (optionally) a header file with
118  declarations of functions and types used in the ispc/application
119  interface.
120  @param srcFile Pathname to ispc source file to compile
121  @param arch %Target architecture (e.g. "x86-64")
122  @param cpu %Target CPU (e.g. "core-i7")
123  @param targets %Target ISAs; this parameter may give a single target
124  ISA, or may give a comma-separated list of them in
125  case we are compiling to multiple ISAs.
126  @param generatePIC Indicates whether position-independent code should
127  be generated.
128  @param outputType %Type of output to generate (object files, assembly,
129  LLVM bitcode.)
130  @param outFileName Base name of output filename for object files, etc.
131  If for example the multiple targets "sse2" and "avx"
132  are specified in the "targets" parameter and if this
133  parameter is "foo.o", then we'll generate multiple
134  output files, like "foo.o", "foo_sse2.o", "foo_avx.o".
135  @param headerFileName If non-NULL, emit a header file suitable for
136  inclusion from C/C++ code with declarations of
137  types and functions exported from the given ispc
138  source file.
139  @param includeFileName If non-NULL, gives the filename for the C++
140  backend to emit in an #include statement to
141  get definitions of the builtins for the generic
142  target.
143  @return Number of errors encountered when compiling
144  srcFile.
145  */
146  static int CompileAndOutput(const char *srcFile, const char *arch,
147  const char *cpu, const char *targets,
148  OutputFlags outputFlags,
149  OutputType outputType,
150  const char *outFileName,
151  const char *headerFileName,
152  const char *includeFileName,
153  const char *depsFileName,
154  const char *depsTargetName,
155  const char *hostStubFileName,
156  const char *devStubFileName);
157 
158  /** Total number of errors encountered during compilation. */
160 
161  /** Symbol table to hold symbols visible in the current scope during
162  compilation. */
164 
165  /** llvm Module object into which globals and functions are added. */
166  llvm::Module *module;
167 
168  /** The diBuilder manages generating debugging information */
169  llvm::DIBuilder *diBuilder;
170 
171 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_4 && ISPC_LLVM_VERSION <= ISPC_LLVM_3_6
172  llvm::DICompileUnit diCompileUnit;
173 #elif ISPC_LLVM_VERSION >= ISPC_LLVM_3_7
174  llvm::DICompileUnit* diCompileUnit;
175 #endif // LLVM_3_4+
176 
177 private:
178  const char *filename;
180 
181  std::vector<std::pair<const Type *, SourcePos> > exportedTypes;
182 
183  /** Write the corresponding output type to the given file. Returns
184  true on success, false if there has been an error. The given
185  filename may be NULL, indicating that output should go to standard
186  output. */
187  bool writeOutput(OutputType ot, OutputFlags flags, const char *filename,
188  const char *includeFileName = NULL,
189  const char *sourceFileName = NULL,
190  DispatchHeaderInfo *DHI = 0);
191  bool writeHeader(const char *filename);
192  bool writeDispatchHeader(DispatchHeaderInfo *DHI);
193  bool writeDeps(const char *filename, bool generateMakeRule, const char *targetName = NULL, const char *srcFilename = NULL);
194  bool writeDevStub(const char *filename);
195  bool writeHostStub(const char *filename);
196  bool writeObjectFileOrAssembly(OutputType outputType, const char *filename);
197  static bool writeObjectFileOrAssembly(llvm::TargetMachine *targetMachine,
198  llvm::Module *module, OutputType outputType,
199  const char *outFileName);
200  static bool writeBitcode(llvm::Module *module, const char *outFileName);
201 
202  void execPreprocessor(const char *infilename, llvm::raw_string_ostream* ostream) const;
203 };
204 
205 inline Module::OutputFlags& operator|=(Module::OutputFlags& lhs, const __underlying_type(Module::OutputFlags) rhs) {
206  return lhs = (Module::OutputFlags)((__underlying_type(Module::OutputFlags))lhs | rhs);
207 }
208 inline Module::OutputFlags& operator&=(Module::OutputFlags& lhs, const __underlying_type(Module::OutputFlags) rhs) {
209  return lhs = (Module::OutputFlags)((__underlying_type(Module::OutputFlags))lhs & rhs);
210 }
212  return (Module::OutputFlags)((__underlying_type(Module::OutputFlags))lhs | rhs);
213 }
214 
215 #endif // ISPC_MODULE_H
Definition: ast.h:144
Definition: ispc.h:75
std::vector< std::pair< const Type *, SourcePos > > exportedTypes
Definition: module.h:181
Interface class for statements in the ispc language.
Definition: stmt.h:49
Symbol table that holds all known symbols during parsing and compilation.
Definition: sym.h:119
OutputType
Definition: module.h:97
Module::OutputFlags & operator&=(Module::OutputFlags &lhs, const __underlying_type(Module::OutputFlags) rhs)
Definition: module.h:208
llvm::Module * module
Definition: module.h:166
Definition: module.h:57
Representation of a range of positions in a source file.
Definition: ispc.h:137
Module::OutputFlags & operator|=(Module::OutputFlags &lhs, const __underlying_type(Module::OutputFlags) rhs)
Definition: module.h:205
StorageClass
Definition: ispc.h:121
OutputFlags
Definition: module.h:109
Type representing a function (return type + argument types)
Definition: type.h:885
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
Module::OutputFlags operator|(const Module::OutputFlags lhs, const Module::OutputFlags rhs)
Definition: module.h:211
int errorCount
Definition: module.h:159
const char * filename
Definition: module.h:178
llvm::DIBuilder * diBuilder
Definition: module.h:169
Main ispc.header file. Defines Target, Globals and Opt classes.
SymbolTable * symbolTable
Definition: module.h:163
AST * ast
Definition: module.h:179