blob: 7135ffdf5073e2d7a2b803597707760ca767e6e1 [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
zonr6315f762010-10-05 15:35:14 +080017#ifndef _SLANG_COMPILER_BACKEND_H
18#define _SLANG_COMPILER_BACKEND_H
Shih-wei Liao462aefd2010-06-04 15:32:04 -070019
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070020#include "llvm/PassManager.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070021
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070022#include "llvm/Support/StandardPasses.h"
23#include "llvm/Support/FormattedStream.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070024
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070025#include "clang/AST/ASTConsumer.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070026
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080027#include "slang.h"
zonr6315f762010-10-05 15:35:14 +080028#include "slang_pragma_recorder.h"
29
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;
80 clang::Diagnostic &mDiags;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070081
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070082 const PragmaList &mPragmas;
Shih-wei Liaocecd11d2010-09-21 08:07:58 -070083
Zonr Chang68fc02c2010-10-13 19:09:19 +080084 // This handler will be invoked before Clang translate @Ctx to LLVM IR. This
85 // give you an opportunity to modified the IR in AST level (scope information,
86 // unoptimized IR, etc.). After the return from this method, slang will start
87 // translate @Ctx into LLVM IR. One should not operate on @Ctx afterwards
88 // since the changes applied on that never reflects to the LLVM module used
89 // in the final codegen.
90 virtual void HandleTranslationUnitPre(clang::ASTContext &Ctx) { return; }
91
92 // This handler will be invoked when Clang have converted AST tree to LLVM IR.
93 // The @M contains the resulting LLVM IR tree. After the return from this
94 // method, slang will start doing optimization and code generation for @M.
95 virtual void HandleTranslationUnitPost(llvm::Module *M) { return; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070096
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070097 public:
98 Backend(clang::Diagnostic &Diags,
99 const clang::CodeGenOptions &CodeGenOpts,
100 const clang::TargetOptions &TargetOpts,
101 const PragmaList &Pragmas,
102 llvm::raw_ostream *OS,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800103 Slang::OutputType OT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700104
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700105 // Initialize - This is called to initialize the consumer, providing the
106 // ASTContext.
107 virtual void Initialize(clang::ASTContext &Ctx);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700108
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700109 // HandleTopLevelDecl - Handle the specified top-level declaration. This is
110 // called by the parser to process every top-level Decl*. Note that D can be
111 // the head of a chain of Decls (e.g. for `int a, b` the chain will have two
112 // elements). Use Decl::getNextDeclarator() to walk the chain.
113 virtual void HandleTopLevelDecl(clang::DeclGroupRef D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700114
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700115 // HandleTranslationUnit - This method is called when the ASTs for entire
116 // translation unit have been parsed.
117 virtual void HandleTranslationUnit(clang::ASTContext &Ctx);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700118
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700119 // HandleTagDeclDefinition - This callback is invoked each time a TagDecl
120 // (e.g. struct, union, enum, class) is completed. This allows the client to
121 // hack on the type, which can occur at any point in the file (because these
122 // can be defined in declspecs).
123 virtual void HandleTagDeclDefinition(clang::TagDecl *D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700124
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700125 // CompleteTentativeDefinition - Callback invoked at the end of a translation
126 // unit to notify the consumer that the given tentative definition should be
127 // completed.
128 virtual void CompleteTentativeDefinition(clang::VarDecl *D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700129
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700130 virtual ~Backend();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700131};
132
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700133} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700134
zonr6315f762010-10-05 15:35:14 +0800135#endif // _SLANG_COMPILER_BACKEND_H