Intel SPMD Program Compiler  1.12.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 "ast.h"
43 #include "ispc.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 class raw_string_ostream;
52 }
53 
54 struct DispatchHeaderInfo;
55 
56 class Module {
57  public:
58  /** The name of the source file being compiled should be passed as the
59  module name. */
60  Module(const char *filename);
61 
62  /** Compiles the source file passed to the Module constructor, adding
63  its global variables and functions to both the llvm::Module and
64  SymbolTable. Returns the number of errors during compilation. */
65  int CompileFile();
66 
67  /** Add a named type definition to the module. */
68  void AddTypeDef(const std::string &name, const Type *type, SourcePos pos);
69 
70  /** Add a new global variable corresponding to the given Symbol to the
71  module. If non-NULL, initExpr gives the initiailizer expression
72  for the global's inital value. */
73  void AddGlobalVariable(const std::string &name, const Type *type, Expr *initExpr, bool isConst,
74  StorageClass storageClass, SourcePos pos);
75 
76  /** Add a declaration of the function defined by the given function
77  symbol to the module. */
78  void AddFunctionDeclaration(const std::string &name, const FunctionType *ftype, StorageClass sc, bool isInline,
79  bool isNoInline, SourcePos pos);
80 
81  /** Adds the function described by the declaration information and the
82  provided statements to the module. */
83  void AddFunctionDefinition(const std::string &name, const FunctionType *ftype, Stmt *code);
84 
85  /** Adds the given type to the set of types that have their definitions
86  included in automatically generated header files. */
87  void AddExportedTypes(const std::vector<std::pair<const Type *, SourcePos>> &types);
88 
89  /** After a source file has been compiled, output can be generated in a
90  number of different formats. */
91  enum OutputType {
92  Asm, /** Generate text assembly language output */
93  Bitcode, /** Generate LLVM IR bitcode output */
94  BitcodeText, /** Generate LLVM IR Text output */
95  Object, /** Generate a native object file */
96  CXX, /** Generate a C++ file */
97  Header, /** Generate a C/C++ header file with
98  declarations of 'export'ed functions, global
99  variables, and the types used by them. */
100  Deps, /** generate dependencies */
101  DevStub, /** generate device-side offload stubs */
102  HostStub /** generate host-side offload stubs */
103  };
104 
105  enum OutputFlags : int {
106  NoFlags = 0,
107  GeneratePIC = 0x1,
108  GenerateFlatDeps = 0x2, /** Dependencies will be output as a flat list. */
109  GenerateMakeRuleForDeps = 0x4, /** Dependencies will be output in a make rule format instead of a flat list. */
110  OutputDepsToStdout = 0x8, /** Dependency information will be output to stdout instead of file. */
111  };
112 
113  /** Compile the given source file, generating assembly, object file, or
114  LLVM bitcode output, as well as (optionally) a header file with
115  declarations of functions and types used in the ispc/application
116  interface.
117  @param srcFile Pathname to ispc source file to compile
118  @param arch %Target architecture (e.g. "x86-64")
119  @param cpu %Target CPU (e.g. "core-i7")
120  @param targets %Target ISAs; this parameter may give a single target
121  ISA, or may give a comma-separated list of them in
122  case we are compiling to multiple ISAs.
123  @param generatePIC Indicates whether position-independent code should
124  be generated.
125  @param outputType %Type of output to generate (object files, assembly,
126  LLVM bitcode.)
127  @param outFileName Base name of output filename for object files, etc.
128  If for example the multiple targets "sse2" and "avx"
129  are specified in the "targets" parameter and if this
130  parameter is "foo.o", then we'll generate multiple
131  output files, like "foo.o", "foo_sse2.o", "foo_avx.o".
132  @param headerFileName If non-NULL, emit a header file suitable for
133  inclusion from C/C++ code with declarations of
134  types and functions exported from the given ispc
135  source file.
136  @param includeFileName If non-NULL, gives the filename for the C++
137  backend to emit in an #include statement to
138  get definitions of the builtins for the generic
139  target.
140  @return Number of errors encountered when compiling
141  srcFile.
142  */
143  static int CompileAndOutput(const char *srcFile, const char *arch, const char *cpu, const char *targets,
144  OutputFlags outputFlags, OutputType outputType, const char *outFileName,
145  const char *headerFileName, const char *includeFileName, const char *depsFileName,
146  const char *depsTargetName, const char *hostStubFileName, const char *devStubFileName);
147 
148  /** Total number of errors encountered during compilation. */
150 
151  /** Symbol table to hold symbols visible in the current scope during
152  compilation. */
154 
155  /** llvm Module object into which globals and functions are added. */
156  llvm::Module *module;
157 
158  /** The diBuilder manages generating debugging information */
159  llvm::DIBuilder *diBuilder;
160 
161 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_4 && ISPC_LLVM_VERSION <= ISPC_LLVM_3_6
162  llvm::DICompileUnit diCompileUnit;
163 #elif ISPC_LLVM_VERSION >= ISPC_LLVM_3_7
164  llvm::DICompileUnit *diCompileUnit;
165 #endif // LLVM_3_4+
166 
167  private:
168  const char *filename;
170 
171  std::vector<std::pair<const Type *, SourcePos>> exportedTypes;
172 
173  /** Write the corresponding output type to the given file. Returns
174  true on success, false if there has been an error. The given
175  filename may be NULL, indicating that output should go to standard
176  output. */
177  bool writeOutput(OutputType ot, OutputFlags flags, const char *filename, const char *includeFileName = NULL,
178  const char *sourceFileName = NULL, DispatchHeaderInfo *DHI = 0);
179  bool writeHeader(const char *filename);
180  bool writeDispatchHeader(DispatchHeaderInfo *DHI);
181  bool writeDeps(const char *filename, bool generateMakeRule, const char *targetName = NULL,
182  const char *srcFilename = NULL);
183  bool writeDevStub(const char *filename);
184  bool writeHostStub(const char *filename);
185  bool writeObjectFileOrAssembly(OutputType outputType, const char *filename);
186  static bool writeObjectFileOrAssembly(llvm::TargetMachine *targetMachine, llvm::Module *module,
187  OutputType outputType, const char *outFileName);
188  static bool writeBitcode(llvm::Module *module, const char *outFileName, OutputType outputType);
189 
190  void execPreprocessor(const char *infilename, llvm::raw_string_ostream *ostream) const;
191 };
192 
193 inline Module::OutputFlags &operator|=(Module::OutputFlags &lhs, const __underlying_type(Module::OutputFlags) rhs) {
194  return lhs = (Module::OutputFlags)((__underlying_type(Module::OutputFlags))lhs | rhs);
195 }
196 inline Module::OutputFlags &operator&=(Module::OutputFlags &lhs, const __underlying_type(Module::OutputFlags) rhs) {
197  return lhs = (Module::OutputFlags)((__underlying_type(Module::OutputFlags))lhs & rhs);
198 }
200  return (Module::OutputFlags)((__underlying_type(Module::OutputFlags))lhs | rhs);
201 }
202 
203 #endif // ISPC_MODULE_H
Definition: ast.h:139
Definition: ispc.h:72
std::vector< std::pair< const Type *, SourcePos > > exportedTypes
Definition: module.h:171
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:117
Module::OutputFlags & operator &=(Module::OutputFlags &lhs, const __underlying_type(Module::OutputFlags) rhs)
Definition: module.h:196
OutputType
Definition: module.h:91
llvm::Module * module
Definition: module.h:156
Definition: module.h:56
Representation of a range of positions in a source file.
Definition: ispc.h:131
Module::OutputFlags & operator|=(Module::OutputFlags &lhs, const __underlying_type(Module::OutputFlags) rhs)
Definition: module.h:193
StorageClass
Definition: ispc.h:117
OutputFlags
Definition: module.h:105
Type representing a function (return type + argument types)
Definition: type.h:858
Interface class that defines the type abstraction.
Definition: type.h:95
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:199
int errorCount
Definition: module.h:149
const char * filename
Definition: module.h:168
llvm::DIBuilder * diBuilder
Definition: module.h:159
Main ispc.header file. Defines Target, Globals and Opt classes.
SymbolTable * symbolTable
Definition: module.h:153
AST * ast
Definition: module.h:169