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