Intel SPMD Program Compiler  1.3.0
module.h
Go to the documentation of this file.
00001 /*
00002   Copyright (c) 2010-2012, Intel Corporation
00003   All rights reserved.
00004 
00005   Redistribution and use in source and binary forms, with or without
00006   modification, are permitted provided that the following conditions are
00007   met:
00008 
00009     * Redistributions of source code must retain the above copyright
00010       notice, this list of conditions and the following disclaimer.
00011 
00012     * Redistributions in binary form must reproduce the above copyright
00013       notice, this list of conditions and the following disclaimer in the
00014       documentation and/or other materials provided with the distribution.
00015 
00016     * Neither the name of Intel Corporation nor the names of its
00017       contributors may be used to endorse or promote products derived from
00018       this software without specific prior written permission.
00019 
00020 
00021    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00022    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00023    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00024    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00025    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00026    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00027    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
00032 */
00033 
00034 /** @file module.h
00035     @brief Declaration of the Module class, which is the ispc-side representation
00036     of the results of compiling a source file.
00037  */
00038 
00039 #ifndef ISPC_MODULE_H
00040 #define ISPC_MODULE_H 1
00041 
00042 #include "ispc.h"
00043 #include "ast.h"
00044 
00045 namespace llvm
00046 {
00047     class raw_string_ostream;
00048 }
00049 
00050 class Module {
00051 public:
00052     /** The name of the source file being compiled should be passed as the
00053         module name. */
00054     Module(const char *filename);
00055 
00056     /** Compiles the source file passed to the Module constructor, adding
00057         its global variables and functions to both the llvm::Module and
00058         SymbolTable.  Returns the number of errors during compilation.  */
00059     int CompileFile();
00060 
00061     /** Add a named type definition to the module. */
00062     void AddTypeDef(const std::string &name, const Type *type,
00063                     SourcePos pos);
00064 
00065     /** Add a new global variable corresponding to the given Symbol to the
00066         module.  If non-NULL, initExpr gives the initiailizer expression
00067         for the global's inital value. */ 
00068     void AddGlobalVariable(const std::string &name, const Type *type,
00069                            Expr *initExpr, bool isConst,
00070                            StorageClass storageClass, SourcePos pos);
00071 
00072     /** Add a declaration of the function defined by the given function
00073         symbol to the module. */
00074     void AddFunctionDeclaration(const std::string &name,
00075                                 const FunctionType *ftype, 
00076                                 StorageClass sc, bool isInline, SourcePos pos);
00077 
00078     /** Adds the function described by the declaration information and the
00079         provided statements to the module. */
00080     void AddFunctionDefinition(const std::string &name,
00081                                const FunctionType *ftype, Stmt *code);
00082 
00083     /** Adds the given type to the set of types that have their definitions
00084         included in automatically generated header files. */
00085     void AddExportedTypes(const std::vector<std::pair<const Type *, 
00086                                                       SourcePos> > &types);
00087 
00088     /** After a source file has been compiled, output can be generated in a
00089         number of different formats. */
00090     enum OutputType { Asm,      /** Generate text assembly language output */
00091                       Bitcode,  /** Generate LLVM IR bitcode output */
00092                       Object,   /** Generate a native object file */
00093                       CXX,      /** Generate a C++ file */
00094                       Header,   /** Generate a C/C++ header file with 
00095                                     declarations of 'export'ed functions, global
00096                                     variables, and the types used by them. */
00097                       Deps,     /** generate dependencies */
00098                       DevStub,  /** generate device-side offload stubs */
00099                       HostStub  /** generate host-side offload stubs */
00100     };
00101 
00102     /** Compile the given source file, generating assembly, object file, or
00103         LLVM bitcode output, as well as (optionally) a header file with
00104         declarations of functions and types used in the ispc/application
00105         interface.
00106         @param srcFile      Pathname to ispc source file to compile
00107         @param arch         Target architecture (e.g. "x86-64")
00108         @param cpu          Target CPU (e.g. "core-i7")
00109         @param targets      Target ISAs; this parameter may give a single target
00110                             ISA, or may give a comma-separated list of them in
00111                             case we are compiling to multiple ISAs.
00112         @param generatePIC  Indicates whether position-independent code should
00113                             be generated.
00114         @param outputType   Type of output to generate (object files, assembly,
00115                             LLVM bitcode.)
00116         @param outFileName  Base name of output filename for object files, etc.
00117                             If for example the multiple targets "sse2" and "avx"
00118                             are specified in the "targets" parameter and if this
00119                             parameter is "foo.o", then we'll generate multiple
00120                             output files, like "foo.o", "foo_sse2.o", "foo_avx.o".
00121         @param headerFileName If non-NULL, emit a header file suitable for
00122                               inclusion from C/C++ code with declarations of
00123                               types and functions exported from the given ispc
00124                               source file.
00125         @param includeFileName If non-NULL, gives the filename for the C++ 
00126                                backend to emit in an #include statement to
00127                                get definitions of the builtins for the generic
00128                                target.
00129         @return             Number of errors encountered when compiling
00130                             srcFile.
00131      */
00132     static int CompileAndOutput(const char *srcFile, const char *arch, 
00133                                 const char *cpu, const char *targets, 
00134                                 bool generatePIC, 
00135                                 OutputType outputType, 
00136                                 const char *outFileName, 
00137                                 const char *headerFileName,
00138                                 const char *includeFileName,
00139                                 const char *depsFileName,
00140                                 const char *hostStubFileName,
00141                                 const char *devStubFileName);
00142 
00143     /** Total number of errors encountered during compilation. */
00144     int errorCount;
00145 
00146     /** Symbol table to hold symbols visible in the current scope during
00147         compilation. */
00148     SymbolTable *symbolTable;
00149 
00150     /** llvm Module object into which globals and functions are added. */
00151     llvm::Module *module; 
00152 
00153     /** The diBuilder manages generating debugging information */
00154     llvm::DIBuilder *diBuilder;
00155 
00156 private:
00157     const char *filename;
00158     AST *ast;
00159 
00160     std::vector<std::pair<const Type *, SourcePos> > exportedTypes;
00161 
00162     /** Write the corresponding output type to the given file.  Returns
00163         true on success, false if there has been an error.  The given
00164         filename may be NULL, indicating that output should go to standard
00165         output. */
00166     bool writeOutput(OutputType ot, const char *filename,
00167                      const char *includeFileName = NULL);
00168     bool writeHeader(const char *filename);
00169     bool writeDeps(const char *filename);
00170     bool writeDevStub(const char *filename);
00171     bool writeHostStub(const char *filename);
00172     bool writeObjectFileOrAssembly(OutputType outputType, const char *filename);
00173     static bool writeObjectFileOrAssembly(llvm::TargetMachine *targetMachine,
00174                                           llvm::Module *module, OutputType outputType, 
00175                                           const char *outFileName);
00176     static bool writeBitcode(llvm::Module *module, const char *outFileName);
00177 
00178     void execPreprocessor(const char *infilename, llvm::raw_string_ostream* ostream) const;
00179 };
00180 
00181 #endif // ISPC_MODULE_H