blob: 2c3b9b903d0648dc843b365879751b830747fd01 [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
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
Stephen Hinese639eb52010-11-08 19:27:20 -080017#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_BACKEND_H_ // NOLINT
18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_BACKEND_H_
19
20#include "clang/AST/ASTConsumer.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070021
Stephen Hinesc7069072015-03-18 14:53:14 -070022#include "llvm/IR/LegacyPassManager.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070023
Pirama Arumuga Nainar21cc0182015-05-06 11:17:16 -070024#include "llvm/Support/raw_ostream.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070025
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080026#include "slang.h"
zonr6315f762010-10-05 15:35:14 +080027#include "slang_pragma_recorder.h"
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070028#include "slang_rs_check_ast.h"
Yang Nifb40ee22015-10-13 20:34:06 +000029#include "slang_rs_foreach_lowering.h"
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070030#include "slang_rs_object_ref_count.h"
Stephen Hines4cc499d2011-08-24 19:06:17 -070031#include "slang_version.h"
zonr6315f762010-10-05 15:35:14 +080032
Shih-wei Liao462aefd2010-06-04 15:32:04 -070033namespace llvm {
Pirama Arumuga Nainar21cc0182015-05-06 11:17:16 -070034 class buffer_ostream;
zonr6315f762010-10-05 15:35:14 +080035 class LLVMContext;
36 class NamedMDNode;
zonr6315f762010-10-05 15:35:14 +080037 class Module;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070038}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070039
40namespace clang {
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070041 class ASTConsumer;
42 class ASTContext;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080043 class CodeGenOptions;
zonr6315f762010-10-05 15:35:14 +080044 class CodeGenerator;
zonr6315f762010-10-05 15:35:14 +080045 class DeclGroupRef;
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070046 class DiagnosticsEngine;
47 class FunctionDecl;
zonr6315f762010-10-05 15:35:14 +080048 class TagDecl;
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070049 class TargetOptions;
zonr6315f762010-10-05 15:35:14 +080050 class VarDecl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070051}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070052
53namespace slang {
54
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070055class RSContext;
56
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070057class Backend : public clang::ASTConsumer {
58 private:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070059 const clang::TargetOptions &mTargetOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070060
Zonr Chang68fc02c2010-10-13 19:09:19 +080061 llvm::Module *mpModule;
62
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070063 // Output stream
64 llvm::raw_ostream *mpOS;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080065 Slang::OutputType mOT;
Kirk Stewart6b226742010-06-11 10:51:12 -070066
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070067 // This helps us translate Clang AST using into LLVM IR
68 clang::CodeGenerator *mGen;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070069
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070070 // Passes
Shih-wei Liao462aefd2010-06-04 15:32:04 -070071
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070072 // Passes apply on function scope in a translation unit
Stephen Hinesc7069072015-03-18 14:53:14 -070073 llvm::legacy::FunctionPassManager *mPerFunctionPasses;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070074 // Passes apply on module scope
Stephen Hinesc7069072015-03-18 14:53:14 -070075 llvm::legacy::PassManager *mPerModulePasses;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070076 // Passes for code emission
Stephen Hinesc7069072015-03-18 14:53:14 -070077 llvm::legacy::FunctionPassManager *mCodeGenPasses;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070078
Pirama Arumuga Nainar21cc0182015-05-06 11:17:16 -070079 llvm::buffer_ostream mBufferOutStream;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070080
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080081 void CreateFunctionPasses();
82 void CreateModulePasses();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070083 bool CreateCodeGenPasses();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070084
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070085 RSContext *mContext;
86
87 clang::SourceManager &mSourceMgr;
88
David Gross2770d0e2015-08-03 14:58:59 -070089 bool mASTPrint;
90
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070091 bool mAllowRSPrefix;
92
93 bool mIsFilterscript;
94
95 llvm::NamedMDNode *mExportVarMetadata;
96 llvm::NamedMDNode *mExportFuncMetadata;
97 llvm::NamedMDNode *mExportForEachNameMetadata;
98 llvm::NamedMDNode *mExportForEachSignatureMetadata;
Matt Walac0c5dd82015-07-23 17:29:37 -070099 llvm::NamedMDNode *mExportReduceMetadata;
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700100 llvm::NamedMDNode *mExportTypeMetadata;
101 llvm::NamedMDNode *mRSObjectSlotsMetadata;
102
103 RSObjectRefCount mRefCount;
104
105 RSCheckAST mASTChecker;
106
Yang Nifb40ee22015-10-13 20:34:06 +0000107 RSForEachLowering mForEachHandler;
108
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700109 void AnnotateFunction(clang::FunctionDecl *FD);
110
111 void dumpExportVarInfo(llvm::Module *M);
112 void dumpExportFunctionInfo(llvm::Module *M);
113 void dumpExportForEachInfo(llvm::Module *M);
Matt Walac0c5dd82015-07-23 17:29:37 -0700114 void dumpExportReduceInfo(llvm::Module *M);
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700115 void dumpExportTypeInfo(llvm::Module *M);
116
Yang Nifb40ee22015-10-13 20:34:06 +0000117 void LowerRSForEachCall(clang::FunctionDecl* FD);
118
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700119 protected:
120 llvm::LLVMContext &mLLVMContext;
Logan Chien9207a2e2011-10-21 15:39:28 +0800121 clang::DiagnosticsEngine &mDiagEngine;
mkopec1c460b372012-01-09 11:21:50 -0500122 const clang::CodeGenOptions &mCodeGenOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700123
Stephen Hines3fd0a942011-01-18 12:27:39 -0800124 PragmaList *mPragmas;
Shih-wei Liaocecd11d2010-09-21 08:07:58 -0700125
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700126 unsigned int getTargetAPI() const { return mContext->getTargetAPI(); }
127
128 // TODO These are no longer virtual from base. Look into merging into caller.
Stephen Hines4cc499d2011-08-24 19:06:17 -0700129
Zonr Chang8785d052010-10-13 22:42:43 +0800130 // This handler will be invoked before Clang translates @Ctx to LLVM IR. This
Zonr Chang68fc02c2010-10-13 19:09:19 +0800131 // give you an opportunity to modified the IR in AST level (scope information,
132 // unoptimized IR, etc.). After the return from this method, slang will start
133 // translate @Ctx into LLVM IR. One should not operate on @Ctx afterwards
134 // since the changes applied on that never reflects to the LLVM module used
135 // in the final codegen.
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700136 void HandleTranslationUnitPre(clang::ASTContext &Ctx);
Zonr Chang68fc02c2010-10-13 19:09:19 +0800137
138 // This handler will be invoked when Clang have converted AST tree to LLVM IR.
139 // The @M contains the resulting LLVM IR tree. After the return from this
140 // method, slang will start doing optimization and code generation for @M.
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700141 void HandleTranslationUnitPost(llvm::Module *M);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700142
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700143 public:
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700144 Backend(RSContext *Context,
145 clang::DiagnosticsEngine *DiagEngine,
David Gross2770d0e2015-08-03 14:58:59 -0700146 const RSCCOptions &Opts,
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700147 const clang::CodeGenOptions &CodeGenOpts,
148 const clang::TargetOptions &TargetOpts,
149 PragmaList *Pragmas,
150 llvm::raw_ostream *OS,
151 Slang::OutputType OT,
152 clang::SourceManager &SourceMgr,
153 bool AllowRSPrefix,
154 bool IsFilterscript);
155
156 virtual ~Backend();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700157
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700158 // Initialize - This is called to initialize the consumer, providing the
159 // ASTContext.
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700160 void Initialize(clang::ASTContext &Ctx) override;
161
162 // TODO Clean up what should be private, protected
163 // TODO Also clean up the include files
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700164
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700165 // HandleTopLevelDecl - Handle the specified top-level declaration. This is
166 // called by the parser to process every top-level Decl*. Note that D can be
167 // the head of a chain of Decls (e.g. for `int a, b` the chain will have two
168 // elements). Use Decl::getNextDeclarator() to walk the chain.
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700169 bool HandleTopLevelDecl(clang::DeclGroupRef D) override;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700170
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700171 // HandleTranslationUnit - This method is called when the ASTs for entire
172 // translation unit have been parsed.
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700173 void HandleTranslationUnit(clang::ASTContext &Ctx) override;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700174
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700175 // HandleTagDeclDefinition - This callback is invoked each time a TagDecl
176 // (e.g. struct, union, enum, class) is completed. This allows the client to
177 // hack on the type, which can occur at any point in the file (because these
178 // can be defined in declspecs).
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700179 void HandleTagDeclDefinition(clang::TagDecl *D) override;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700180
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700181 // CompleteTentativeDefinition - Callback invoked at the end of a translation
182 // unit to notify the consumer that the given tentative definition should be
183 // completed.
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700184 void CompleteTentativeDefinition(clang::VarDecl *D) override;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700185};
186
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700187} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700188
Stephen Hinese639eb52010-11-08 19:27:20 -0800189#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_BACKEND_H_ NOLINT