blob: 71cd1c753ac19f7526b4f8fcf0dad347f0cc92cf [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
Stephen Hines5e6d0d52011-11-22 19:42:41 -080078 void WrapBitcode(llvm::raw_string_ostream &Bitcode);
79
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070080 protected:
81 llvm::LLVMContext &mLLVMContext;
Logan Chien9207a2e2011-10-21 15:39:28 +080082 clang::DiagnosticsEngine &mDiagEngine;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070083
Stephen Hines3fd0a942011-01-18 12:27:39 -080084 PragmaList *mPragmas;
Shih-wei Liaocecd11d2010-09-21 08:07:58 -070085
Stephen Hines4cc499d2011-08-24 19:06:17 -070086 virtual unsigned int getTargetAPI() const {
87 return SLANG_MAXIMUM_TARGET_API;
88 }
89
Zonr Chang8785d052010-10-13 22:42:43 +080090 // This handler will be invoked before Clang translates @Ctx to LLVM IR. This
Zonr Chang68fc02c2010-10-13 19:09:19 +080091 // give you an opportunity to modified the IR in AST level (scope information,
92 // unoptimized IR, etc.). After the return from this method, slang will start
93 // translate @Ctx into LLVM IR. One should not operate on @Ctx afterwards
94 // since the changes applied on that never reflects to the LLVM module used
95 // in the final codegen.
96 virtual void HandleTranslationUnitPre(clang::ASTContext &Ctx) { return; }
97
98 // This handler will be invoked when Clang have converted AST tree to LLVM IR.
99 // The @M contains the resulting LLVM IR tree. After the return from this
100 // method, slang will start doing optimization and code generation for @M.
101 virtual void HandleTranslationUnitPost(llvm::Module *M) { return; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700102
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700103 public:
Logan Chien9207a2e2011-10-21 15:39:28 +0800104 Backend(clang::DiagnosticsEngine *DiagEngine,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700105 const clang::CodeGenOptions &CodeGenOpts,
106 const clang::TargetOptions &TargetOpts,
Stephen Hines3fd0a942011-01-18 12:27:39 -0800107 PragmaList *Pragmas,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700108 llvm::raw_ostream *OS,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800109 Slang::OutputType OT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700110
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700111 // Initialize - This is called to initialize the consumer, providing the
112 // ASTContext.
113 virtual void Initialize(clang::ASTContext &Ctx);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700114
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700115 // HandleTopLevelDecl - Handle the specified top-level declaration. This is
116 // called by the parser to process every top-level Decl*. Note that D can be
117 // the head of a chain of Decls (e.g. for `int a, b` the chain will have two
118 // elements). Use Decl::getNextDeclarator() to walk the chain.
Logan Chienfa6ef562011-11-25 13:50:02 +0800119 virtual bool HandleTopLevelDecl(clang::DeclGroupRef D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700120
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700121 // HandleTranslationUnit - This method is called when the ASTs for entire
122 // translation unit have been parsed.
123 virtual void HandleTranslationUnit(clang::ASTContext &Ctx);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700124
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700125 // HandleTagDeclDefinition - This callback is invoked each time a TagDecl
126 // (e.g. struct, union, enum, class) is completed. This allows the client to
127 // hack on the type, which can occur at any point in the file (because these
128 // can be defined in declspecs).
129 virtual void HandleTagDeclDefinition(clang::TagDecl *D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700130
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700131 // CompleteTentativeDefinition - Callback invoked at the end of a translation
132 // unit to notify the consumer that the given tentative definition should be
133 // completed.
134 virtual void CompleteTentativeDefinition(clang::VarDecl *D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700135
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700136 virtual ~Backend();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700137};
138
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700139} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700140
Stephen Hinese639eb52010-11-08 19:27:20 -0800141#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_BACKEND_H_ NOLINT