zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 1 | #ifndef _SLANG_COMPILER_BACKEND_H |
| 2 | #define _SLANG_COMPILER_BACKEND_H |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 3 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 4 | #include "llvm/PassManager.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 5 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 6 | #include "llvm/Target/TargetData.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 7 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 8 | #include "llvm/Support/StandardPasses.h" |
| 9 | #include "llvm/Support/FormattedStream.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 10 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 11 | #include "clang/AST/ASTConsumer.h" |
| 12 | #include "clang/Frontend/CodeGenOptions.h" |
| 13 | #include "clang/Basic/SourceManager.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 14 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 15 | #include "libslang.h" |
| 16 | #include "slang_pragma_recorder.h" |
| 17 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 18 | namespace llvm { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 19 | class LLVMContext; |
| 20 | class NamedMDNode; |
| 21 | class raw_ostream; |
| 22 | class Module; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 23 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 24 | |
| 25 | namespace clang { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 26 | class ASTConsumer; |
| 27 | class Diagnostic; |
| 28 | class TargetOptions; |
| 29 | class PragmaList; |
| 30 | class CodeGenerator; |
| 31 | class ASTContext; |
| 32 | class DeclGroupRef; |
| 33 | class TagDecl; |
| 34 | class VarDecl; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 35 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 36 | |
| 37 | namespace slang { |
| 38 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 39 | class Backend : public clang::ASTConsumer { |
| 40 | private: |
| 41 | const clang::CodeGenOptions &mCodeGenOpts; |
| 42 | const clang::TargetOptions &mTargetOpts; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 43 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 44 | clang::SourceManager &mSourceMgr; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 45 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 46 | // Output stream |
| 47 | llvm::raw_ostream *mpOS; |
| 48 | SlangCompilerOutputTy mOutputType; |
Kirk Stewart | 6b22674 | 2010-06-11 10:51:12 -0700 | [diff] [blame] | 49 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 50 | llvm::TargetData *mpTargetData; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 51 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 52 | // This helps us translate Clang AST using into LLVM IR |
| 53 | clang::CodeGenerator *mGen; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 54 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 55 | // Passes |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 56 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 57 | // Passes apply on function scope in a translation unit |
| 58 | llvm::FunctionPassManager *mPerFunctionPasses; |
| 59 | // Passes apply on module scope |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 60 | llvm::PassManager *mPerModulePasses; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 61 | // Passes for code emission |
| 62 | llvm::FunctionPassManager *mCodeGenPasses; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 63 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 64 | llvm::formatted_raw_ostream FormattedOutStream; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 65 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 66 | bool mAllowRSPrefix; |
Shih-wei Liao | cecd11d | 2010-09-21 08:07:58 -0700 | [diff] [blame] | 67 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 68 | inline void CreateFunctionPasses() { |
| 69 | if (!mPerFunctionPasses) { |
| 70 | mPerFunctionPasses = new llvm::FunctionPassManager(mpModule); |
| 71 | mPerFunctionPasses->add(new llvm::TargetData(*mpTargetData)); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 72 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 73 | llvm::createStandardFunctionPasses(mPerFunctionPasses, |
| 74 | mCodeGenOpts.OptimizationLevel); |
| 75 | } |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | inline void CreateModulePasses() { |
| 80 | if (!mPerModulePasses) { |
| 81 | mPerModulePasses = new llvm::PassManager(); |
| 82 | mPerModulePasses->add(new llvm::TargetData(*mpTargetData)); |
| 83 | |
| 84 | llvm::createStandardModulePasses(mPerModulePasses, |
| 85 | mCodeGenOpts.OptimizationLevel, |
| 86 | mCodeGenOpts.OptimizeSize, |
| 87 | mCodeGenOpts.UnitAtATime, |
| 88 | mCodeGenOpts.UnrollLoops, |
| 89 | // Some libc functions will be replaced |
| 90 | // by the LLVM built-in optimized |
| 91 | // function (e.g. strcmp) |
| 92 | /* SimplifyLibCalls */true, |
| 93 | /* HaveExceptions */false, |
| 94 | /* InliningPass */NULL); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 97 | // llvm::createStandardFunctionPasses and llvm::createStandardModulePasses |
| 98 | // insert lots of optimization passes for the code generator. For the |
| 99 | // conventional desktop PC which memory resources and computation power is |
| 100 | // relatively large, doing lots optimization as possible is reasonible and |
| 101 | // feasible. However, on the mobile device or embedded system, this may |
| 102 | // cause some problem due to the hardware resources limitation. So they need |
| 103 | // to be further refined. |
| 104 | return; |
| 105 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 106 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 107 | bool CreateCodeGenPasses(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 108 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 109 | protected: |
| 110 | llvm::LLVMContext &mLLVMContext; |
| 111 | clang::Diagnostic &mDiags; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 112 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 113 | llvm::Module *mpModule; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 114 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 115 | const PragmaList &mPragmas; |
Shih-wei Liao | cecd11d | 2010-09-21 08:07:58 -0700 | [diff] [blame] | 116 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 117 | // Extra handler for subclass to handle translation unit before emission |
| 118 | virtual void HandleTranslationUnitEx(clang::ASTContext &Ctx) { return; } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 119 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 120 | public: |
| 121 | Backend(clang::Diagnostic &Diags, |
| 122 | const clang::CodeGenOptions &CodeGenOpts, |
| 123 | const clang::TargetOptions &TargetOpts, |
| 124 | const PragmaList &Pragmas, |
| 125 | llvm::raw_ostream *OS, |
| 126 | SlangCompilerOutputTy OutputType, |
| 127 | clang::SourceManager &SourceMgr, |
| 128 | bool AllowRSPrefix); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 129 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 130 | // Initialize - This is called to initialize the consumer, providing the |
| 131 | // ASTContext. |
| 132 | virtual void Initialize(clang::ASTContext &Ctx); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 133 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 134 | // HandleTopLevelDecl - Handle the specified top-level declaration. This is |
| 135 | // called by the parser to process every top-level Decl*. Note that D can be |
| 136 | // the head of a chain of Decls (e.g. for `int a, b` the chain will have two |
| 137 | // elements). Use Decl::getNextDeclarator() to walk the chain. |
| 138 | virtual void HandleTopLevelDecl(clang::DeclGroupRef D); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 139 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 140 | // HandleTranslationUnit - This method is called when the ASTs for entire |
| 141 | // translation unit have been parsed. |
| 142 | virtual void HandleTranslationUnit(clang::ASTContext &Ctx); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 143 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 144 | // HandleTagDeclDefinition - This callback is invoked each time a TagDecl |
| 145 | // (e.g. struct, union, enum, class) is completed. This allows the client to |
| 146 | // hack on the type, which can occur at any point in the file (because these |
| 147 | // can be defined in declspecs). |
| 148 | virtual void HandleTagDeclDefinition(clang::TagDecl *D); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 149 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 150 | // CompleteTentativeDefinition - Callback invoked at the end of a translation |
| 151 | // unit to notify the consumer that the given tentative definition should be |
| 152 | // completed. |
| 153 | virtual void CompleteTentativeDefinition(clang::VarDecl *D); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 154 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 155 | virtual ~Backend(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 158 | } // namespace slang |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 159 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 160 | #endif // _SLANG_COMPILER_BACKEND_H |