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