blob: 24ec8d43c9a65cd9d05e0e65d9129f259805092a [file] [log] [blame]
zonr6315f762010-10-05 15:35:14 +08001#ifndef _SLANG_COMPILER_BACKEND_H
2#define _SLANG_COMPILER_BACKEND_H
Shih-wei Liao462aefd2010-06-04 15:32:04 -07003
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07004#include "llvm/PassManager.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -07005
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07006#include "llvm/Target/TargetData.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -07007
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07008#include "llvm/Support/StandardPasses.h"
9#include "llvm/Support/FormattedStream.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070010
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070011#include "clang/AST/ASTConsumer.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070012
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080013#include "slang.h"
zonr6315f762010-10-05 15:35:14 +080014#include "slang_pragma_recorder.h"
15
Shih-wei Liao462aefd2010-06-04 15:32:04 -070016namespace llvm {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080017 class formatted_raw_ostream;
zonr6315f762010-10-05 15:35:14 +080018 class LLVMContext;
19 class NamedMDNode;
zonr6315f762010-10-05 15:35:14 +080020 class Module;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080021 class PassManager;
22 class FunctionPassManager;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070023}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070024
25namespace clang {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080026 class CodeGenOptions;
zonr6315f762010-10-05 15:35:14 +080027 class CodeGenerator;
zonr6315f762010-10-05 15:35:14 +080028 class DeclGroupRef;
29 class TagDecl;
30 class VarDecl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070031}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070032
33namespace slang {
34
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070035class Backend : public clang::ASTConsumer {
36 private:
37 const clang::CodeGenOptions &mCodeGenOpts;
38 const clang::TargetOptions &mTargetOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070039
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070040 // Output stream
41 llvm::raw_ostream *mpOS;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080042 Slang::OutputType mOT;
Kirk Stewart6b226742010-06-11 10:51:12 -070043
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070044 llvm::TargetData *mpTargetData;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070045
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070046 // This helps us translate Clang AST using into LLVM IR
47 clang::CodeGenerator *mGen;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070048
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070049 // Passes
Shih-wei Liao462aefd2010-06-04 15:32:04 -070050
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070051 // Passes apply on function scope in a translation unit
52 llvm::FunctionPassManager *mPerFunctionPasses;
53 // Passes apply on module scope
zonr6315f762010-10-05 15:35:14 +080054 llvm::PassManager *mPerModulePasses;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070055 // Passes for code emission
56 llvm::FunctionPassManager *mCodeGenPasses;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070057
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070058 llvm::formatted_raw_ostream FormattedOutStream;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070059
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080060 void CreateFunctionPasses();
61 void CreateModulePasses();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070062 bool CreateCodeGenPasses();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070063
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070064 protected:
65 llvm::LLVMContext &mLLVMContext;
66 clang::Diagnostic &mDiags;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070067
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070068 llvm::Module *mpModule;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070069
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070070 const PragmaList &mPragmas;
Shih-wei Liaocecd11d2010-09-21 08:07:58 -070071
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070072 // Extra handler for subclass to handle translation unit before emission
73 virtual void HandleTranslationUnitEx(clang::ASTContext &Ctx) { return; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070074
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070075 public:
76 Backend(clang::Diagnostic &Diags,
77 const clang::CodeGenOptions &CodeGenOpts,
78 const clang::TargetOptions &TargetOpts,
79 const PragmaList &Pragmas,
80 llvm::raw_ostream *OS,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080081 Slang::OutputType OT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070082
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070083 // Initialize - This is called to initialize the consumer, providing the
84 // ASTContext.
85 virtual void Initialize(clang::ASTContext &Ctx);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070086
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070087 // HandleTopLevelDecl - Handle the specified top-level declaration. This is
88 // called by the parser to process every top-level Decl*. Note that D can be
89 // the head of a chain of Decls (e.g. for `int a, b` the chain will have two
90 // elements). Use Decl::getNextDeclarator() to walk the chain.
91 virtual void HandleTopLevelDecl(clang::DeclGroupRef D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070092
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070093 // HandleTranslationUnit - This method is called when the ASTs for entire
94 // translation unit have been parsed.
95 virtual void HandleTranslationUnit(clang::ASTContext &Ctx);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070096
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070097 // HandleTagDeclDefinition - This callback is invoked each time a TagDecl
98 // (e.g. struct, union, enum, class) is completed. This allows the client to
99 // hack on the type, which can occur at any point in the file (because these
100 // can be defined in declspecs).
101 virtual void HandleTagDeclDefinition(clang::TagDecl *D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700102
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700103 // CompleteTentativeDefinition - Callback invoked at the end of a translation
104 // unit to notify the consumer that the given tentative definition should be
105 // completed.
106 virtual void CompleteTentativeDefinition(clang::VarDecl *D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700107
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700108 virtual ~Backend();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700109};
110
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700111} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700112
zonr6315f762010-10-05 15:35:14 +0800113#endif // _SLANG_COMPILER_BACKEND_H