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 | |
Stephen Hines | c706907 | 2015-03-18 14:53:14 -0700 | [diff] [blame] | 22 | #include "llvm/IR/LegacyPassManager.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" |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame^] | 28 | #include "slang_rs_check_ast.h" |
| 29 | #include "slang_rs_object_ref_count.h" |
Stephen Hines | 4cc499d | 2011-08-24 19:06:17 -0700 | [diff] [blame] | 30 | #include "slang_version.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 31 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 32 | namespace llvm { |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 33 | class formatted_raw_ostream; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 34 | class LLVMContext; |
| 35 | class NamedMDNode; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 36 | class Module; |
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 { |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame^] | 40 | class ASTConsumer; |
| 41 | class ASTContext; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 42 | class CodeGenOptions; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 43 | class CodeGenerator; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 44 | class DeclGroupRef; |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame^] | 45 | class DiagnosticsEngine; |
| 46 | class FunctionDecl; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 47 | class TagDecl; |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame^] | 48 | class TargetOptions; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 49 | class VarDecl; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 50 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 51 | |
| 52 | namespace slang { |
| 53 | |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame^] | 54 | class RSContext; |
| 55 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 56 | class Backend : public clang::ASTConsumer { |
| 57 | private: |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 58 | const clang::TargetOptions &mTargetOpts; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 59 | |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 60 | llvm::Module *mpModule; |
| 61 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 62 | // Output stream |
| 63 | llvm::raw_ostream *mpOS; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 64 | Slang::OutputType mOT; |
Kirk Stewart | 6b22674 | 2010-06-11 10:51:12 -0700 | [diff] [blame] | 65 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 66 | // This helps us translate Clang AST using into LLVM IR |
| 67 | clang::CodeGenerator *mGen; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 68 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 69 | // Passes |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 70 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 71 | // Passes apply on function scope in a translation unit |
Stephen Hines | c706907 | 2015-03-18 14:53:14 -0700 | [diff] [blame] | 72 | llvm::legacy::FunctionPassManager *mPerFunctionPasses; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 73 | // Passes apply on module scope |
Stephen Hines | c706907 | 2015-03-18 14:53:14 -0700 | [diff] [blame] | 74 | llvm::legacy::PassManager *mPerModulePasses; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 75 | // Passes for code emission |
Stephen Hines | c706907 | 2015-03-18 14:53:14 -0700 | [diff] [blame] | 76 | llvm::legacy::FunctionPassManager *mCodeGenPasses; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 77 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 78 | llvm::formatted_raw_ostream FormattedOutStream; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 79 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 80 | void CreateFunctionPasses(); |
| 81 | void CreateModulePasses(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 82 | bool CreateCodeGenPasses(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 83 | |
Stephen Hines | 5e6d0d5 | 2011-11-22 19:42:41 -0800 | [diff] [blame] | 84 | void WrapBitcode(llvm::raw_string_ostream &Bitcode); |
| 85 | |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame^] | 86 | RSContext *mContext; |
| 87 | |
| 88 | clang::SourceManager &mSourceMgr; |
| 89 | |
| 90 | bool mAllowRSPrefix; |
| 91 | |
| 92 | bool mIsFilterscript; |
| 93 | |
| 94 | llvm::NamedMDNode *mExportVarMetadata; |
| 95 | llvm::NamedMDNode *mExportFuncMetadata; |
| 96 | llvm::NamedMDNode *mExportForEachNameMetadata; |
| 97 | llvm::NamedMDNode *mExportForEachSignatureMetadata; |
| 98 | llvm::NamedMDNode *mExportTypeMetadata; |
| 99 | llvm::NamedMDNode *mRSObjectSlotsMetadata; |
| 100 | |
| 101 | RSObjectRefCount mRefCount; |
| 102 | |
| 103 | RSCheckAST mASTChecker; |
| 104 | |
| 105 | void AnnotateFunction(clang::FunctionDecl *FD); |
| 106 | |
| 107 | void dumpExportVarInfo(llvm::Module *M); |
| 108 | void dumpExportFunctionInfo(llvm::Module *M); |
| 109 | void dumpExportForEachInfo(llvm::Module *M); |
| 110 | void dumpExportTypeInfo(llvm::Module *M); |
| 111 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 112 | protected: |
| 113 | llvm::LLVMContext &mLLVMContext; |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 114 | clang::DiagnosticsEngine &mDiagEngine; |
mkopec1 | c460b37 | 2012-01-09 11:21:50 -0500 | [diff] [blame] | 115 | const clang::CodeGenOptions &mCodeGenOpts; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 116 | |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 117 | PragmaList *mPragmas; |
Shih-wei Liao | cecd11d | 2010-09-21 08:07:58 -0700 | [diff] [blame] | 118 | |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame^] | 119 | unsigned int getTargetAPI() const { return mContext->getTargetAPI(); } |
| 120 | |
| 121 | // TODO These are no longer virtual from base. Look into merging into caller. |
Stephen Hines | 4cc499d | 2011-08-24 19:06:17 -0700 | [diff] [blame] | 122 | |
Zonr Chang | 8785d05 | 2010-10-13 22:42:43 +0800 | [diff] [blame] | 123 | // 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] | 124 | // give you an opportunity to modified the IR in AST level (scope information, |
| 125 | // unoptimized IR, etc.). After the return from this method, slang will start |
| 126 | // translate @Ctx into LLVM IR. One should not operate on @Ctx afterwards |
| 127 | // since the changes applied on that never reflects to the LLVM module used |
| 128 | // in the final codegen. |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame^] | 129 | void HandleTranslationUnitPre(clang::ASTContext &Ctx); |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 130 | |
| 131 | // This handler will be invoked when Clang have converted AST tree to LLVM IR. |
| 132 | // The @M contains the resulting LLVM IR tree. After the return from this |
| 133 | // method, slang will start doing optimization and code generation for @M. |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame^] | 134 | void HandleTranslationUnitPost(llvm::Module *M); |
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 | public: |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame^] | 137 | Backend(RSContext *Context, |
| 138 | clang::DiagnosticsEngine *DiagEngine, |
| 139 | const clang::CodeGenOptions &CodeGenOpts, |
| 140 | const clang::TargetOptions &TargetOpts, |
| 141 | PragmaList *Pragmas, |
| 142 | llvm::raw_ostream *OS, |
| 143 | Slang::OutputType OT, |
| 144 | clang::SourceManager &SourceMgr, |
| 145 | bool AllowRSPrefix, |
| 146 | bool IsFilterscript); |
| 147 | |
| 148 | virtual ~Backend(); |
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 | // Initialize - This is called to initialize the consumer, providing the |
| 151 | // ASTContext. |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame^] | 152 | void Initialize(clang::ASTContext &Ctx) override; |
| 153 | |
| 154 | // TODO Clean up what should be private, protected |
| 155 | // TODO Also clean up the include files |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 156 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 157 | // HandleTopLevelDecl - Handle the specified top-level declaration. This is |
| 158 | // called by the parser to process every top-level Decl*. Note that D can be |
| 159 | // the head of a chain of Decls (e.g. for `int a, b` the chain will have two |
| 160 | // elements). Use Decl::getNextDeclarator() to walk the chain. |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame^] | 161 | bool HandleTopLevelDecl(clang::DeclGroupRef D) override; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 162 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 163 | // HandleTranslationUnit - This method is called when the ASTs for entire |
| 164 | // translation unit have been parsed. |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame^] | 165 | void HandleTranslationUnit(clang::ASTContext &Ctx) override; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 166 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 167 | // HandleTagDeclDefinition - This callback is invoked each time a TagDecl |
| 168 | // (e.g. struct, union, enum, class) is completed. This allows the client to |
| 169 | // hack on the type, which can occur at any point in the file (because these |
| 170 | // can be defined in declspecs). |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame^] | 171 | void HandleTagDeclDefinition(clang::TagDecl *D) override; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 172 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 173 | // CompleteTentativeDefinition - Callback invoked at the end of a translation |
| 174 | // unit to notify the consumer that the given tentative definition should be |
| 175 | // completed. |
Jean-Luc Brouillet | c10bc75 | 2015-05-04 23:02:25 -0700 | [diff] [blame^] | 176 | void CompleteTentativeDefinition(clang::VarDecl *D) override; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 179 | } // namespace slang |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 180 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 181 | #endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_BACKEND_H_ NOLINT |