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 17 #ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_BACKEND_H_ // NOLINT 18 #define _FRAMEWORKS_COMPILE_SLANG_SLANG_BACKEND_H_ 19 20 #include "clang/AST/ASTConsumer.h" 21 22 #include "llvm/PassManager.h" 23 24 #include "llvm/Support/FormattedStream.h" 25 26 #include "slang.h" 27 #include "slang_pragma_recorder.h" 28 #include "slang_version.h" 29 30 namespace llvm { 31 class formatted_raw_ostream; 32 class LLVMContext; 33 class NamedMDNode; 34 class Module; 35 } 36 37 namespace clang { 38 class CodeGenOptions; 39 class CodeGenerator; 40 class DeclGroupRef; 41 class TagDecl; 42 class VarDecl; 43 } 44 45 namespace slang { 46 47 class Backend : public clang::ASTConsumer { 48 private: 49 const clang::TargetOptions &mTargetOpts; 50 51 llvm::Module *mpModule; 52 53 // Output stream 54 llvm::raw_ostream *mpOS; 55 Slang::OutputType mOT; 56 57 // This helps us translate Clang AST using into LLVM IR 58 clang::CodeGenerator *mGen; 59 60 // Passes 61 62 // Passes apply on function scope in a translation unit 63 llvm::FunctionPassManager *mPerFunctionPasses; 64 // Passes apply on module scope 65 llvm::PassManager *mPerModulePasses; 66 // Passes for code emission 67 llvm::FunctionPassManager *mCodeGenPasses; 68 69 llvm::formatted_raw_ostream FormattedOutStream; 70 71 void CreateFunctionPasses(); 72 void CreateModulePasses(); 73 bool CreateCodeGenPasses(); 74 75 void WrapBitcode(llvm::raw_string_ostream &Bitcode); 76 77 protected: 78 llvm::LLVMContext &mLLVMContext; 79 clang::DiagnosticsEngine &mDiagEngine; 80 const clang::CodeGenOptions &mCodeGenOpts; 81 82 PragmaList *mPragmas; 83 getTargetAPI()84 virtual unsigned int getTargetAPI() const { 85 return SLANG_MAXIMUM_TARGET_API; 86 } 87 88 // This handler will be invoked before Clang translates @Ctx to LLVM IR. This 89 // give you an opportunity to modified the IR in AST level (scope information, 90 // unoptimized IR, etc.). After the return from this method, slang will start 91 // translate @Ctx into LLVM IR. One should not operate on @Ctx afterwards 92 // since the changes applied on that never reflects to the LLVM module used 93 // in the final codegen. HandleTranslationUnitPre(clang::ASTContext & Ctx)94 virtual void HandleTranslationUnitPre(clang::ASTContext &Ctx) { } 95 96 // This handler will be invoked when Clang have converted AST tree to LLVM IR. 97 // The @M contains the resulting LLVM IR tree. After the return from this 98 // method, slang will start doing optimization and code generation for @M. HandleTranslationUnitPost(llvm::Module * M)99 virtual void HandleTranslationUnitPost(llvm::Module *M) { } 100 101 public: 102 Backend(clang::DiagnosticsEngine *DiagEngine, 103 const clang::CodeGenOptions &CodeGenOpts, 104 const clang::TargetOptions &TargetOpts, 105 PragmaList *Pragmas, 106 llvm::raw_ostream *OS, 107 Slang::OutputType OT); 108 109 // Initialize - This is called to initialize the consumer, providing the 110 // ASTContext. 111 virtual void Initialize(clang::ASTContext &Ctx); 112 113 // HandleTopLevelDecl - Handle the specified top-level declaration. This is 114 // called by the parser to process every top-level Decl*. Note that D can be 115 // the head of a chain of Decls (e.g. for `int a, b` the chain will have two 116 // elements). Use Decl::getNextDeclarator() to walk the chain. 117 virtual bool HandleTopLevelDecl(clang::DeclGroupRef D); 118 119 // HandleTranslationUnit - This method is called when the ASTs for entire 120 // translation unit have been parsed. 121 virtual void HandleTranslationUnit(clang::ASTContext &Ctx); 122 123 // HandleTagDeclDefinition - This callback is invoked each time a TagDecl 124 // (e.g. struct, union, enum, class) is completed. This allows the client to 125 // hack on the type, which can occur at any point in the file (because these 126 // can be defined in declspecs). 127 virtual void HandleTagDeclDefinition(clang::TagDecl *D); 128 129 // CompleteTentativeDefinition - Callback invoked at the end of a translation 130 // unit to notify the consumer that the given tentative definition should be 131 // completed. 132 virtual void CompleteTentativeDefinition(clang::VarDecl *D); 133 134 virtual ~Backend(); 135 }; 136 137 } // namespace slang 138 139 #endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_BACKEND_H_ NOLINT 140