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/IR/LegacyPassManager.h"
23
24#include "llvm/Support/raw_ostream.h"
25
26#include "slang.h"
27#include "slang_pragma_list.h"
28#include "slang_rs_check_ast.h"
29#include "slang_rs_foreach_lowering.h"
30#include "slang_rs_object_ref_count.h"
31#include "slang_version.h"
32
33namespace llvm {
34  class buffer_ostream;
35  class LLVMContext;
36  class NamedMDNode;
37  class Module;
38}
39
40namespace clang {
41  class ASTConsumer;
42  class ASTContext;
43  class CodeGenOptions;
44  class CodeGenerator;
45  class DeclGroupRef;
46  class DiagnosticsEngine;
47  class FunctionDecl;
48  class HeaderSearchOptions;
49  class PreprocessorOptions;
50  class TagDecl;
51  class TargetOptions;
52  class VarDecl;
53}
54
55namespace slang {
56
57class RSContext;
58
59class Backend : public clang::ASTConsumer {
60 private:
61  const clang::TargetOptions &mTargetOpts;
62
63  llvm::Module *mpModule;
64
65  // Output stream
66  llvm::raw_ostream *mpOS;
67  Slang::OutputType mOT;
68
69  // This helps us translate Clang AST using into LLVM IR
70  clang::CodeGenerator *mGen;
71
72  // Passes
73
74  // Passes apply on function scope in a translation unit
75  llvm::legacy::FunctionPassManager *mPerFunctionPasses;
76  // Passes apply on module scope
77  llvm::legacy::PassManager *mPerModulePasses;
78  // Passes for code emission
79  llvm::legacy::FunctionPassManager *mCodeGenPasses;
80
81  llvm::buffer_ostream mBufferOutStream;
82
83  void CreateFunctionPasses();
84  void CreateModulePasses();
85  bool CreateCodeGenPasses();
86
87  RSContext *mContext;
88
89  clang::SourceManager &mSourceMgr;
90
91  bool mASTPrint;
92
93  bool mAllowRSPrefix;
94
95  bool mIsFilterscript;
96
97  llvm::NamedMDNode *mExportVarMetadata;
98  llvm::NamedMDNode *mExportFuncMetadata;
99  llvm::NamedMDNode *mExportForEachNameMetadata;
100  llvm::NamedMDNode *mExportForEachSignatureMetadata;
101  llvm::NamedMDNode *mExportReduceMetadata;
102  llvm::NamedMDNode *mExportTypeMetadata;
103  llvm::NamedMDNode *mRSObjectSlotsMetadata;
104
105  RSObjectRefCount mRefCount;
106
107  RSCheckAST mASTChecker;
108
109  RSForEachLowering mForEachHandler;
110
111  void AnnotateFunction(clang::FunctionDecl *FD);
112
113  void dumpExportVarInfo(llvm::Module *M);
114  void dumpExportFunctionInfo(llvm::Module *M);
115  void dumpExportForEachInfo(llvm::Module *M);
116  void dumpExportReduceInfo(llvm::Module *M);
117  void dumpExportTypeInfo(llvm::Module *M);
118
119  // Translates any rsForEach() or rsForEachWithOptions() calls inside the body
120  // of FD to lower-level runtime calls to rsForEachInternal(), if FD is not a
121  // kernel function itself, as indicated by isKernel being false. If isKernel
122  // is true, reports an error on any calls to rsForEach() or
123  // rsForEachWithOptions().
124  void LowerRSForEachCall(clang::FunctionDecl* FD, bool isKernel);
125
126 protected:
127  llvm::LLVMContext &mLLVMContext;
128  clang::DiagnosticsEngine &mDiagEngine;
129  const clang::CodeGenOptions &mCodeGenOpts;
130
131  PragmaList *mPragmas;
132
133  unsigned int getTargetAPI() const { return mContext->getTargetAPI(); }
134
135  // TODO These are no longer virtual from base.  Look into merging into caller.
136
137  // This handler will be invoked before Clang translates @Ctx to LLVM IR. This
138  // give you an opportunity to modified the IR in AST level (scope information,
139  // unoptimized IR, etc.). After the return from this method, slang will start
140  // translate @Ctx into LLVM IR. One should not operate on @Ctx afterwards
141  // since the changes applied on that never reflects to the LLVM module used
142  // in the final codegen.
143  void HandleTranslationUnitPre(clang::ASTContext &Ctx);
144
145  // This handler will be invoked when Clang have converted AST tree to LLVM IR.
146  // The @M contains the resulting LLVM IR tree. After the return from this
147  // method, slang will start doing optimization and code generation for @M.
148  void HandleTranslationUnitPost(llvm::Module *M);
149
150 public:
151  Backend(RSContext *Context,
152            clang::DiagnosticsEngine *DiagEngine,
153            const RSCCOptions &Opts,
154            const clang::HeaderSearchOptions &HeaderSearchOpts,
155            const clang::PreprocessorOptions &PreprocessorOpts,
156            const clang::CodeGenOptions &CodeGenOpts,
157            const clang::TargetOptions &TargetOpts,
158            PragmaList *Pragmas,
159            llvm::raw_ostream *OS,
160            Slang::OutputType OT,
161            clang::SourceManager &SourceMgr,
162            bool AllowRSPrefix,
163            bool IsFilterscript);
164
165  virtual ~Backend();
166
167  // Initialize - This is called to initialize the consumer, providing the
168  // ASTContext.
169  void Initialize(clang::ASTContext &Ctx) override;
170
171  // TODO Clean up what should be private, protected
172  // TODO Also clean up the include files
173
174  // HandleTopLevelDecl - Handle the specified top-level declaration.  This is
175  // called by the parser to process every top-level Decl*. Note that D can be
176  // the head of a chain of Decls (e.g. for `int a, b` the chain will have two
177  // elements). Use Decl::getNextDeclarator() to walk the chain.
178  bool HandleTopLevelDecl(clang::DeclGroupRef D) override;
179
180  // HandleTranslationUnit - This method is called when the ASTs for entire
181  // translation unit have been parsed.
182  void HandleTranslationUnit(clang::ASTContext &Ctx) override;
183
184  // HandleTagDeclDefinition - This callback is invoked each time a TagDecl
185  // (e.g. struct, union, enum, class) is completed.  This allows the client to
186  // hack on the type, which can occur at any point in the file (because these
187  // can be defined in declspecs).
188  void HandleTagDeclDefinition(clang::TagDecl *D) override;
189
190  // CompleteTentativeDefinition - Callback invoked at the end of a translation
191  // unit to notify the consumer that the given tentative definition should be
192  // completed.
193  void CompleteTentativeDefinition(clang::VarDecl *D) override;
194};
195
196}   // namespace slang
197
198#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_BACKEND_H_  NOLINT
199