blob: 4254acafe877d6ad38c3de5b8eb0bd79c8a5a1e0 [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
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070022#include "llvm/PassManager.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070023
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070024#include "llvm/Support/FormattedStream.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"
Stephen Hines4cc499d2011-08-24 19:06:17 -070028#include "slang_version.h"
zonr6315f762010-10-05 15:35:14 +080029
Shih-wei Liao462aefd2010-06-04 15:32:04 -070030namespace llvm {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080031 class formatted_raw_ostream;
zonr6315f762010-10-05 15:35:14 +080032 class LLVMContext;
33 class NamedMDNode;
zonr6315f762010-10-05 15:35:14 +080034 class Module;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070035}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070036
37namespace clang {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080038 class CodeGenOptions;
zonr6315f762010-10-05 15:35:14 +080039 class CodeGenerator;
zonr6315f762010-10-05 15:35:14 +080040 class DeclGroupRef;
41 class TagDecl;
42 class VarDecl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070043}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070044
45namespace slang {
46
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070047class Backend : public clang::ASTConsumer {
48 private:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070049 const clang::TargetOptions &mTargetOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070050
Zonr Chang68fc02c2010-10-13 19:09:19 +080051 llvm::Module *mpModule;
52
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070053 // Output stream
54 llvm::raw_ostream *mpOS;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080055 Slang::OutputType mOT;
Kirk Stewart6b226742010-06-11 10:51:12 -070056
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070057 // This helps us translate Clang AST using into LLVM IR
58 clang::CodeGenerator *mGen;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070059
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070060 // Passes
Shih-wei Liao462aefd2010-06-04 15:32:04 -070061
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070062 // Passes apply on function scope in a translation unit
63 llvm::FunctionPassManager *mPerFunctionPasses;
64 // Passes apply on module scope
zonr6315f762010-10-05 15:35:14 +080065 llvm::PassManager *mPerModulePasses;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070066 // Passes for code emission
67 llvm::FunctionPassManager *mCodeGenPasses;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070068
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070069 llvm::formatted_raw_ostream FormattedOutStream;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070070
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080071 void CreateFunctionPasses();
72 void CreateModulePasses();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070073 bool CreateCodeGenPasses();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070074
Stephen Hines5e6d0d52011-11-22 19:42:41 -080075 void WrapBitcode(llvm::raw_string_ostream &Bitcode);
76
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070077 protected:
78 llvm::LLVMContext &mLLVMContext;
Logan Chien9207a2e2011-10-21 15:39:28 +080079 clang::DiagnosticsEngine &mDiagEngine;
mkopec1c460b372012-01-09 11:21:50 -050080 const clang::CodeGenOptions &mCodeGenOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070081
Stephen Hines3fd0a942011-01-18 12:27:39 -080082 PragmaList *mPragmas;
Shih-wei Liaocecd11d2010-09-21 08:07:58 -070083
Chris Wailesc9454af2014-06-13 17:25:40 -070084 virtual unsigned int getTargetAPI() const {
Stephen Hines4cc499d2011-08-24 19:06:17 -070085 return SLANG_MAXIMUM_TARGET_API;
86 }
87
Zonr Chang8785d052010-10-13 22:42:43 +080088 // This handler will be invoked before Clang translates @Ctx to LLVM IR. This
Zonr Chang68fc02c2010-10-13 19:09:19 +080089 // give you an opportunity to modified the IR in AST level (scope information,
90 // unoptimized IR, etc.). After the return from this method, slang will start
91 // translate @Ctx into LLVM IR. One should not operate on @Ctx afterwards
92 // since the changes applied on that never reflects to the LLVM module used
93 // in the final codegen.
Jean-Luc Brouillet796e7b12014-05-27 11:35:17 -070094 virtual void HandleTranslationUnitPre(clang::ASTContext &Ctx) { }
Zonr Chang68fc02c2010-10-13 19:09:19 +080095
96 // This handler will be invoked when Clang have converted AST tree to LLVM IR.
97 // The @M contains the resulting LLVM IR tree. After the return from this
98 // method, slang will start doing optimization and code generation for @M.
Jean-Luc Brouillet796e7b12014-05-27 11:35:17 -070099 virtual void HandleTranslationUnitPost(llvm::Module *M) { }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700100
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700101 public:
Logan Chien9207a2e2011-10-21 15:39:28 +0800102 Backend(clang::DiagnosticsEngine *DiagEngine,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700103 const clang::CodeGenOptions &CodeGenOpts,
104 const clang::TargetOptions &TargetOpts,
Stephen Hines3fd0a942011-01-18 12:27:39 -0800105 PragmaList *Pragmas,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700106 llvm::raw_ostream *OS,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800107 Slang::OutputType OT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700108
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700109 // Initialize - This is called to initialize the consumer, providing the
110 // ASTContext.
111 virtual void Initialize(clang::ASTContext &Ctx);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700112
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700113 // HandleTopLevelDecl - Handle the specified top-level declaration. This is
114 // called by the parser to process every top-level Decl*. Note that D can be
115 // the head of a chain of Decls (e.g. for `int a, b` the chain will have two
116 // elements). Use Decl::getNextDeclarator() to walk the chain.
Logan Chienfa6ef562011-11-25 13:50:02 +0800117 virtual bool HandleTopLevelDecl(clang::DeclGroupRef D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700118
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700119 // HandleTranslationUnit - This method is called when the ASTs for entire
120 // translation unit have been parsed.
121 virtual void HandleTranslationUnit(clang::ASTContext &Ctx);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700122
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700123 // HandleTagDeclDefinition - This callback is invoked each time a TagDecl
124 // (e.g. struct, union, enum, class) is completed. This allows the client to
125 // hack on the type, which can occur at any point in the file (because these
126 // can be defined in declspecs).
127 virtual void HandleTagDeclDefinition(clang::TagDecl *D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700128
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700129 // CompleteTentativeDefinition - Callback invoked at the end of a translation
130 // unit to notify the consumer that the given tentative definition should be
131 // completed.
132 virtual void CompleteTentativeDefinition(clang::VarDecl *D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700133
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700134 virtual ~Backend();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700135};
136
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700137} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700138
Stephen Hinese639eb52010-11-08 19:27:20 -0800139#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_BACKEND_H_ NOLINT