blob: 3ec3f30b235ba7e633eea1a5674fddf38a89fa5f [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/Target/TargetData.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070023
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070024#include "llvm/Support/StandardPasses.h"
25#include "llvm/Support/FormattedStream.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070026
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070027#include "clang/AST/ASTConsumer.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070028
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080029#include "slang.h"
zonr6315f762010-10-05 15:35:14 +080030#include "slang_pragma_recorder.h"
31
Shih-wei Liao462aefd2010-06-04 15:32:04 -070032namespace llvm {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080033 class formatted_raw_ostream;
zonr6315f762010-10-05 15:35:14 +080034 class LLVMContext;
35 class NamedMDNode;
zonr6315f762010-10-05 15:35:14 +080036 class Module;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080037 class PassManager;
38 class FunctionPassManager;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070039}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070040
41namespace clang {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080042 class CodeGenOptions;
zonr6315f762010-10-05 15:35:14 +080043 class CodeGenerator;
zonr6315f762010-10-05 15:35:14 +080044 class DeclGroupRef;
45 class TagDecl;
46 class VarDecl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070047}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070048
49namespace slang {
50
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070051class Backend : public clang::ASTConsumer {
52 private:
53 const clang::CodeGenOptions &mCodeGenOpts;
54 const clang::TargetOptions &mTargetOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070055
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 llvm::TargetData *mpTargetData;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070061
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070062 // This helps us translate Clang AST using into LLVM IR
63 clang::CodeGenerator *mGen;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070064
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070065 // Passes
Shih-wei Liao462aefd2010-06-04 15:32:04 -070066
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070067 // Passes apply on function scope in a translation unit
68 llvm::FunctionPassManager *mPerFunctionPasses;
69 // Passes apply on module scope
zonr6315f762010-10-05 15:35:14 +080070 llvm::PassManager *mPerModulePasses;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070071 // Passes for code emission
72 llvm::FunctionPassManager *mCodeGenPasses;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070073
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070074 llvm::formatted_raw_ostream FormattedOutStream;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070075
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080076 void CreateFunctionPasses();
77 void CreateModulePasses();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070078 bool CreateCodeGenPasses();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070079
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070080 protected:
81 llvm::LLVMContext &mLLVMContext;
82 clang::Diagnostic &mDiags;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070083
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070084 llvm::Module *mpModule;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070085
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070086 const PragmaList &mPragmas;
Shih-wei Liaocecd11d2010-09-21 08:07:58 -070087
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070088 // Extra handler for subclass to handle translation unit before emission
89 virtual void HandleTranslationUnitEx(clang::ASTContext &Ctx) { return; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070090
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070091 public:
92 Backend(clang::Diagnostic &Diags,
93 const clang::CodeGenOptions &CodeGenOpts,
94 const clang::TargetOptions &TargetOpts,
95 const PragmaList &Pragmas,
96 llvm::raw_ostream *OS,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080097 Slang::OutputType OT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070098
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070099 // Initialize - This is called to initialize the consumer, providing the
100 // ASTContext.
101 virtual void Initialize(clang::ASTContext &Ctx);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700102
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700103 // HandleTopLevelDecl - Handle the specified top-level declaration. This is
104 // called by the parser to process every top-level Decl*. Note that D can be
105 // the head of a chain of Decls (e.g. for `int a, b` the chain will have two
106 // elements). Use Decl::getNextDeclarator() to walk the chain.
107 virtual void HandleTopLevelDecl(clang::DeclGroupRef D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700108
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700109 // HandleTranslationUnit - This method is called when the ASTs for entire
110 // translation unit have been parsed.
111 virtual void HandleTranslationUnit(clang::ASTContext &Ctx);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700112
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700113 // HandleTagDeclDefinition - This callback is invoked each time a TagDecl
114 // (e.g. struct, union, enum, class) is completed. This allows the client to
115 // hack on the type, which can occur at any point in the file (because these
116 // can be defined in declspecs).
117 virtual void HandleTagDeclDefinition(clang::TagDecl *D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700118
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700119 // CompleteTentativeDefinition - Callback invoked at the end of a translation
120 // unit to notify the consumer that the given tentative definition should be
121 // completed.
122 virtual void CompleteTentativeDefinition(clang::VarDecl *D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700123
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700124 virtual ~Backend();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700125};
126
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700127} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700128
zonr6315f762010-10-05 15:35:14 +0800129#endif // _SLANG_COMPILER_BACKEND_H