| 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 |  | 
| zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 17 | #ifndef _SLANG_COMPILER_BACKEND_H | 
|  | 18 | #define _SLANG_COMPILER_BACKEND_H | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 19 |  | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 20 | #include "llvm/PassManager.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/Target/TargetData.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/StandardPasses.h" | 
|  | 25 | #include "llvm/Support/FormattedStream.h" | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 26 |  | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 27 | #include "clang/AST/ASTConsumer.h" | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 28 |  | 
| Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 29 | #include "slang.h" | 
| zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 30 | #include "slang_pragma_recorder.h" | 
|  | 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; | 
| Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 37 | class PassManager; | 
|  | 38 | class FunctionPassManager; | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 39 | } | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 40 |  | 
|  | 41 | namespace clang { | 
| 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; | 
|  | 45 | class TagDecl; | 
|  | 46 | class VarDecl; | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 47 | } | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 48 |  | 
|  | 49 | namespace slang { | 
|  | 50 |  | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 51 | class Backend : public clang::ASTConsumer { | 
|  | 52 | private: | 
|  | 53 | const clang::CodeGenOptions &mCodeGenOpts; | 
|  | 54 | const clang::TargetOptions &mTargetOpts; | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 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 | llvm::TargetData *mpTargetData; | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 61 |  | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 62 | // This helps us translate Clang AST using into LLVM IR | 
|  | 63 | clang::CodeGenerator *mGen; | 
| 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 | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 66 |  | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 67 | // Passes apply on function scope in a translation unit | 
|  | 68 | llvm::FunctionPassManager *mPerFunctionPasses; | 
|  | 69 | // Passes apply on module scope | 
| zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 70 | llvm::PassManager *mPerModulePasses; | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 71 | // Passes for code emission | 
|  | 72 | llvm::FunctionPassManager *mCodeGenPasses; | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 73 |  | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 74 | llvm::formatted_raw_ostream FormattedOutStream; | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 75 |  | 
| Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 76 | void CreateFunctionPasses(); | 
|  | 77 | void CreateModulePasses(); | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 78 | bool CreateCodeGenPasses(); | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 79 |  | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 80 | protected: | 
|  | 81 | llvm::LLVMContext &mLLVMContext; | 
|  | 82 | clang::Diagnostic &mDiags; | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 83 |  | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 84 | llvm::Module *mpModule; | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 85 |  | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 86 | const PragmaList &mPragmas; | 
| Shih-wei Liao | cecd11d | 2010-09-21 08:07:58 -0700 | [diff] [blame] | 87 |  | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 88 | // Extra handler for subclass to handle translation unit before emission | 
|  | 89 | virtual void HandleTranslationUnitEx(clang::ASTContext &Ctx) { return; } | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 90 |  | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 91 | public: | 
|  | 92 | Backend(clang::Diagnostic &Diags, | 
|  | 93 | const clang::CodeGenOptions &CodeGenOpts, | 
|  | 94 | const clang::TargetOptions &TargetOpts, | 
|  | 95 | const PragmaList &Pragmas, | 
|  | 96 | llvm::raw_ostream *OS, | 
| Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 97 | Slang::OutputType OT); | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 98 |  | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 99 | // Initialize - This is called to initialize the consumer, providing the | 
|  | 100 | // ASTContext. | 
|  | 101 | virtual void Initialize(clang::ASTContext &Ctx); | 
| 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 | // HandleTopLevelDecl - Handle the specified top-level declaration.  This is | 
|  | 104 | // called by the parser to process every top-level Decl*. Note that D can be | 
|  | 105 | // the head of a chain of Decls (e.g. for `int a, b` the chain will have two | 
|  | 106 | // elements). Use Decl::getNextDeclarator() to walk the chain. | 
|  | 107 | virtual void HandleTopLevelDecl(clang::DeclGroupRef D); | 
| 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 | // HandleTranslationUnit - This method is called when the ASTs for entire | 
|  | 110 | // translation unit have been parsed. | 
|  | 111 | virtual void HandleTranslationUnit(clang::ASTContext &Ctx); | 
| 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 | // HandleTagDeclDefinition - This callback is invoked each time a TagDecl | 
|  | 114 | // (e.g. struct, union, enum, class) is completed.  This allows the client to | 
|  | 115 | // hack on the type, which can occur at any point in the file (because these | 
|  | 116 | // can be defined in declspecs). | 
|  | 117 | virtual void HandleTagDeclDefinition(clang::TagDecl *D); | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 118 |  | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 119 | // CompleteTentativeDefinition - Callback invoked at the end of a translation | 
|  | 120 | // unit to notify the consumer that the given tentative definition should be | 
|  | 121 | // completed. | 
|  | 122 | virtual void CompleteTentativeDefinition(clang::VarDecl *D); | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 123 |  | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 124 | virtual ~Backend(); | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 125 | }; | 
|  | 126 |  | 
| Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 127 | }   // namespace slang | 
| Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 128 |  | 
| zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 129 | #endif  // _SLANG_COMPILER_BACKEND_H |