blob: 4bb596fcaaeb420a8104d548685e02675d6a9f91 [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"
David Gross34e62052015-11-05 09:55:03 -080027#include "slang_pragma_list.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;
Pirama Arumuga Nainar8f093e02016-03-03 23:56:27 -080048 class HeaderSearchOptions;
49 class PreprocessorOptions;
zonr6315f762010-10-05 15:35:14 +080050 class TagDecl;
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070051 class TargetOptions;
zonr6315f762010-10-05 15:35:14 +080052 class VarDecl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070053}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070054
55namespace slang {
56
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070057class RSContext;
58
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070059class Backend : public clang::ASTConsumer {
60 private:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070061 const clang::TargetOptions &mTargetOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070062
Zonr Chang68fc02c2010-10-13 19:09:19 +080063 llvm::Module *mpModule;
64
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070065 // Output stream
66 llvm::raw_ostream *mpOS;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080067 Slang::OutputType mOT;
Kirk Stewart6b226742010-06-11 10:51:12 -070068
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070069 // This helps us translate Clang AST using into LLVM IR
70 clang::CodeGenerator *mGen;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070071
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070072 // Passes
Shih-wei Liao462aefd2010-06-04 15:32:04 -070073
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070074 // Passes apply on function scope in a translation unit
Stephen Hinesc7069072015-03-18 14:53:14 -070075 llvm::legacy::FunctionPassManager *mPerFunctionPasses;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070076 // Passes apply on module scope
Stephen Hinesc7069072015-03-18 14:53:14 -070077 llvm::legacy::PassManager *mPerModulePasses;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070078 // Passes for code emission
Stephen Hinesc7069072015-03-18 14:53:14 -070079 llvm::legacy::FunctionPassManager *mCodeGenPasses;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070080
Pirama Arumuga Nainar21cc0182015-05-06 11:17:16 -070081 llvm::buffer_ostream mBufferOutStream;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070082
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080083 void CreateFunctionPasses();
84 void CreateModulePasses();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070085 bool CreateCodeGenPasses();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070086
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070087 RSContext *mContext;
88
89 clang::SourceManager &mSourceMgr;
90
David Gross2770d0e2015-08-03 14:58:59 -070091 bool mASTPrint;
92
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070093 bool mAllowRSPrefix;
94
95 bool mIsFilterscript;
96
97 llvm::NamedMDNode *mExportVarMetadata;
98 llvm::NamedMDNode *mExportFuncMetadata;
99 llvm::NamedMDNode *mExportForEachNameMetadata;
100 llvm::NamedMDNode *mExportForEachSignatureMetadata;
David Gross8ee018b2016-06-02 14:46:55 -0700101 llvm::NamedMDNode *mExportReduceMetadata;
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700102 llvm::NamedMDNode *mExportTypeMetadata;
103 llvm::NamedMDNode *mRSObjectSlotsMetadata;
104
105 RSObjectRefCount mRefCount;
106
107 RSCheckAST mASTChecker;
108
Yang Nifb40ee22015-10-13 20:34:06 +0000109 RSForEachLowering mForEachHandler;
110
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700111 void AnnotateFunction(clang::FunctionDecl *FD);
112
113 void dumpExportVarInfo(llvm::Module *M);
114 void dumpExportFunctionInfo(llvm::Module *M);
115 void dumpExportForEachInfo(llvm::Module *M);
David Gross8ee018b2016-06-02 14:46:55 -0700116 void dumpExportReduceInfo(llvm::Module *M);
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700117 void dumpExportTypeInfo(llvm::Module *M);
118
Yang Ni9319dfc2016-02-25 15:14:37 -0800119 // 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);
Yang Nifb40ee22015-10-13 20:34:06 +0000125
David Gross37dbf5c2017-03-29 20:54:15 +0000126 // Insert explicit padding fields into struct to follow the current
127 // layout as defined by the RenderScript ABI (32-bit or 64-bit ARM).
128 //
129 // The padding does not change field offset or structure size -- it
130 // makes explicit any padding that was implicit due to the ABI.
131 // This ensures that if the frontend compiles for an ABI with
132 // stricter alignment requirements than the backend compiles for,
133 // the frontend and backend will still agree on structure layout
134 // (field offset and structure size). This is important for 32-bit
135 // x86: The frontend compiles for 32-bit ARM ABI, in which 64-bit
136 // scalars are 64-bit aligned; but the 32-bit x86 ABI says that
137 // 64-bit scalars are only 32-bit aligned.
138 void PadStruct(clang::RecordDecl* RD);
139
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700140 protected:
141 llvm::LLVMContext &mLLVMContext;
Logan Chien9207a2e2011-10-21 15:39:28 +0800142 clang::DiagnosticsEngine &mDiagEngine;
mkopec1c460b372012-01-09 11:21:50 -0500143 const clang::CodeGenOptions &mCodeGenOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700144
Stephen Hines3fd0a942011-01-18 12:27:39 -0800145 PragmaList *mPragmas;
Shih-wei Liaocecd11d2010-09-21 08:07:58 -0700146
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700147 unsigned int getTargetAPI() const { return mContext->getTargetAPI(); }
148
149 // TODO These are no longer virtual from base. Look into merging into caller.
Stephen Hines4cc499d2011-08-24 19:06:17 -0700150
Zonr Chang8785d052010-10-13 22:42:43 +0800151 // This handler will be invoked before Clang translates @Ctx to LLVM IR. This
Zonr Chang68fc02c2010-10-13 19:09:19 +0800152 // give you an opportunity to modified the IR in AST level (scope information,
153 // unoptimized IR, etc.). After the return from this method, slang will start
154 // translate @Ctx into LLVM IR. One should not operate on @Ctx afterwards
155 // since the changes applied on that never reflects to the LLVM module used
156 // in the final codegen.
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700157 void HandleTranslationUnitPre(clang::ASTContext &Ctx);
Zonr Chang68fc02c2010-10-13 19:09:19 +0800158
159 // This handler will be invoked when Clang have converted AST tree to LLVM IR.
160 // The @M contains the resulting LLVM IR tree. After the return from this
161 // method, slang will start doing optimization and code generation for @M.
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700162 void HandleTranslationUnitPost(llvm::Module *M);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700163
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700164 public:
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700165 Backend(RSContext *Context,
166 clang::DiagnosticsEngine *DiagEngine,
David Gross2770d0e2015-08-03 14:58:59 -0700167 const RSCCOptions &Opts,
Pirama Arumuga Nainar8f093e02016-03-03 23:56:27 -0800168 const clang::HeaderSearchOptions &HeaderSearchOpts,
169 const clang::PreprocessorOptions &PreprocessorOpts,
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700170 const clang::CodeGenOptions &CodeGenOpts,
171 const clang::TargetOptions &TargetOpts,
172 PragmaList *Pragmas,
173 llvm::raw_ostream *OS,
174 Slang::OutputType OT,
175 clang::SourceManager &SourceMgr,
176 bool AllowRSPrefix,
177 bool IsFilterscript);
178
179 virtual ~Backend();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700180
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700181 // Initialize - This is called to initialize the consumer, providing the
182 // ASTContext.
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700183 void Initialize(clang::ASTContext &Ctx) override;
184
185 // TODO Clean up what should be private, protected
186 // TODO Also clean up the include files
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700187
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700188 // HandleTopLevelDecl - Handle the specified top-level declaration. This is
189 // called by the parser to process every top-level Decl*. Note that D can be
190 // the head of a chain of Decls (e.g. for `int a, b` the chain will have two
191 // elements). Use Decl::getNextDeclarator() to walk the chain.
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700192 bool HandleTopLevelDecl(clang::DeclGroupRef D) override;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700193
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700194 // HandleTranslationUnit - This method is called when the ASTs for entire
195 // translation unit have been parsed.
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700196 void HandleTranslationUnit(clang::ASTContext &Ctx) override;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700197
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700198 // HandleTagDeclDefinition - This callback is invoked each time a TagDecl
199 // (e.g. struct, union, enum, class) is completed. This allows the client to
200 // hack on the type, which can occur at any point in the file (because these
201 // can be defined in declspecs).
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700202 void HandleTagDeclDefinition(clang::TagDecl *D) override;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700203
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700204 // CompleteTentativeDefinition - Callback invoked at the end of a translation
205 // unit to notify the consumer that the given tentative definition should be
206 // completed.
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700207 void CompleteTentativeDefinition(clang::VarDecl *D) override;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700208};
209
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700210} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700211
Stephen Hinese639eb52010-11-08 19:27:20 -0800212#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_BACKEND_H_ NOLINT