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