Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 17 | #ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_BACKEND_H_ // NOLINT |
| 18 | #define _FRAMEWORKS_COMPILE_SLANG_SLANG_BACKEND_H_ |
| 19 | |
| 20 | #include "clang/AST/ASTConsumer.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 21 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 22 | #include "llvm/PassManager.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 23 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 24 | #include "llvm/Support/FormattedStream.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 25 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 26 | #include "slang.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 27 | #include "slang_pragma_recorder.h" |
Stephen Hines | 4cc499d | 2011-08-24 19:06:17 -0700 | [diff] [blame] | 28 | #include "slang_version.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 29 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 30 | namespace llvm { |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 31 | class formatted_raw_ostream; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 32 | class LLVMContext; |
| 33 | class NamedMDNode; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 34 | class Module; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 35 | class PassManager; |
| 36 | class FunctionPassManager; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 37 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 38 | |
| 39 | namespace clang { |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 40 | class CodeGenOptions; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 41 | class CodeGenerator; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 42 | class DeclGroupRef; |
| 43 | class TagDecl; |
| 44 | class VarDecl; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 45 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 46 | |
| 47 | namespace slang { |
| 48 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 49 | class Backend : public clang::ASTConsumer { |
| 50 | private: |
| 51 | const clang::CodeGenOptions &mCodeGenOpts; |
| 52 | const clang::TargetOptions &mTargetOpts; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 53 | |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 54 | llvm::Module *mpModule; |
| 55 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 56 | // Output stream |
| 57 | llvm::raw_ostream *mpOS; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 58 | Slang::OutputType mOT; |
Kirk Stewart | 6b22674 | 2010-06-11 10:51:12 -0700 | [diff] [blame] | 59 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 60 | // This helps us translate Clang AST using into LLVM IR |
| 61 | clang::CodeGenerator *mGen; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 62 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 63 | // Passes |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 64 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 65 | // Passes apply on function scope in a translation unit |
| 66 | llvm::FunctionPassManager *mPerFunctionPasses; |
| 67 | // Passes apply on module scope |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 68 | llvm::PassManager *mPerModulePasses; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 69 | // Passes for code emission |
| 70 | llvm::FunctionPassManager *mCodeGenPasses; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 71 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 72 | llvm::formatted_raw_ostream FormattedOutStream; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 73 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 74 | void CreateFunctionPasses(); |
| 75 | void CreateModulePasses(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 76 | bool CreateCodeGenPasses(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 77 | |
Stephen Hines | 5e6d0d5 | 2011-11-22 19:42:41 -0800 | [diff] [blame] | 78 | void WrapBitcode(llvm::raw_string_ostream &Bitcode); |
| 79 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 80 | protected: |
| 81 | llvm::LLVMContext &mLLVMContext; |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 82 | clang::DiagnosticsEngine &mDiagEngine; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 83 | |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 84 | PragmaList *mPragmas; |
Shih-wei Liao | cecd11d | 2010-09-21 08:07:58 -0700 | [diff] [blame] | 85 | |
Stephen Hines | 4cc499d | 2011-08-24 19:06:17 -0700 | [diff] [blame] | 86 | virtual unsigned int getTargetAPI() const { |
| 87 | return SLANG_MAXIMUM_TARGET_API; |
| 88 | } |
| 89 | |
Zonr Chang | 8785d05 | 2010-10-13 22:42:43 +0800 | [diff] [blame] | 90 | // This handler will be invoked before Clang translates @Ctx to LLVM IR. This |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 91 | // give you an opportunity to modified the IR in AST level (scope information, |
| 92 | // unoptimized IR, etc.). After the return from this method, slang will start |
| 93 | // translate @Ctx into LLVM IR. One should not operate on @Ctx afterwards |
| 94 | // since the changes applied on that never reflects to the LLVM module used |
| 95 | // in the final codegen. |
| 96 | virtual void HandleTranslationUnitPre(clang::ASTContext &Ctx) { return; } |
| 97 | |
| 98 | // This handler will be invoked when Clang have converted AST tree to LLVM IR. |
| 99 | // The @M contains the resulting LLVM IR tree. After the return from this |
| 100 | // method, slang will start doing optimization and code generation for @M. |
| 101 | virtual void HandleTranslationUnitPost(llvm::Module *M) { return; } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 102 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 103 | public: |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 104 | Backend(clang::DiagnosticsEngine *DiagEngine, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 105 | const clang::CodeGenOptions &CodeGenOpts, |
| 106 | const clang::TargetOptions &TargetOpts, |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 107 | PragmaList *Pragmas, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 108 | llvm::raw_ostream *OS, |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 109 | Slang::OutputType OT); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 110 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 111 | // Initialize - This is called to initialize the consumer, providing the |
| 112 | // ASTContext. |
| 113 | virtual void Initialize(clang::ASTContext &Ctx); |
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 | // HandleTopLevelDecl - Handle the specified top-level declaration. This is |
| 116 | // called by the parser to process every top-level Decl*. Note that D can be |
| 117 | // the head of a chain of Decls (e.g. for `int a, b` the chain will have two |
| 118 | // elements). Use Decl::getNextDeclarator() to walk the chain. |
Logan Chien | fa6ef56 | 2011-11-25 13:50:02 +0800 | [diff] [blame] | 119 | virtual bool HandleTopLevelDecl(clang::DeclGroupRef D); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 120 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 121 | // HandleTranslationUnit - This method is called when the ASTs for entire |
| 122 | // translation unit have been parsed. |
| 123 | virtual void HandleTranslationUnit(clang::ASTContext &Ctx); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 124 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 125 | // HandleTagDeclDefinition - This callback is invoked each time a TagDecl |
| 126 | // (e.g. struct, union, enum, class) is completed. This allows the client to |
| 127 | // hack on the type, which can occur at any point in the file (because these |
| 128 | // can be defined in declspecs). |
| 129 | virtual void HandleTagDeclDefinition(clang::TagDecl *D); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 130 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 131 | // CompleteTentativeDefinition - Callback invoked at the end of a translation |
| 132 | // unit to notify the consumer that the given tentative definition should be |
| 133 | // completed. |
| 134 | virtual void CompleteTentativeDefinition(clang::VarDecl *D); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 135 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 136 | virtual ~Backend(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 139 | } // namespace slang |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 140 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 141 | #endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_BACKEND_H_ NOLINT |