blob: c6785749ed697439c89e2e7e4c0a12bb507f81ec [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;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080035 class PassManager;
36 class FunctionPassManager;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070037}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070038
39namespace clang {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080040 class CodeGenOptions;
zonr6315f762010-10-05 15:35:14 +080041 class CodeGenerator;
zonr6315f762010-10-05 15:35:14 +080042 class DeclGroupRef;
43 class TagDecl;
44 class VarDecl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070045}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070046
47namespace slang {
48
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070049class Backend : public clang::ASTConsumer {
50 private:
51 const clang::CodeGenOptions &mCodeGenOpts;
52 const clang::TargetOptions &mTargetOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070053
Zonr Chang68fc02c2010-10-13 19:09:19 +080054 llvm::Module *mpModule;
55
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070056 // Output stream
57 llvm::raw_ostream *mpOS;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080058 Slang::OutputType mOT;
Kirk Stewart6b226742010-06-11 10:51:12 -070059
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070060 // This helps us translate Clang AST using into LLVM IR
61 clang::CodeGenerator *mGen;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070062
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070063 // Passes
Shih-wei Liao462aefd2010-06-04 15:32:04 -070064
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070065 // Passes apply on function scope in a translation unit
66 llvm::FunctionPassManager *mPerFunctionPasses;
67 // Passes apply on module scope
zonr6315f762010-10-05 15:35:14 +080068 llvm::PassManager *mPerModulePasses;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070069 // Passes for code emission
70 llvm::FunctionPassManager *mCodeGenPasses;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070071
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070072 llvm::formatted_raw_ostream FormattedOutStream;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070073
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080074 void CreateFunctionPasses();
75 void CreateModulePasses();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070076 bool CreateCodeGenPasses();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070077
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070078 protected:
79 llvm::LLVMContext &mLLVMContext;
Logan Chien9207a2e2011-10-21 15:39:28 +080080 clang::DiagnosticsEngine &mDiagEngine;
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
Stephen Hines4cc499d2011-08-24 19:06:17 -070084 virtual unsigned int getTargetAPI() const {
85 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.
94 virtual void HandleTranslationUnitPre(clang::ASTContext &Ctx) { return; }
95
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.
99 virtual void HandleTranslationUnitPost(llvm::Module *M) { return; }
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.
117 virtual void 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