blob: b434262e8d1498094070b26e611dec6b5ae255f3 [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"
12#include "clang/Frontend/CodeGenOptions.h"
13#include "clang/Basic/SourceManager.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070014
zonr6315f762010-10-05 15:35:14 +080015#include "libslang.h"
16#include "slang_pragma_recorder.h"
17
Shih-wei Liao462aefd2010-06-04 15:32:04 -070018namespace llvm {
zonr6315f762010-10-05 15:35:14 +080019 class LLVMContext;
20 class NamedMDNode;
21 class raw_ostream;
22 class Module;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070023}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070024
25namespace clang {
zonr6315f762010-10-05 15:35:14 +080026 class ASTConsumer;
27 class Diagnostic;
28 class TargetOptions;
29 class PragmaList;
30 class CodeGenerator;
31 class ASTContext;
32 class DeclGroupRef;
33 class TagDecl;
34 class VarDecl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070035}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070036
37namespace slang {
38
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070039class Backend : public clang::ASTConsumer {
40 private:
41 const clang::CodeGenOptions &mCodeGenOpts;
42 const clang::TargetOptions &mTargetOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070043
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070044 clang::SourceManager &mSourceMgr;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070045
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070046 // Output stream
47 llvm::raw_ostream *mpOS;
48 SlangCompilerOutputTy mOutputType;
Kirk Stewart6b226742010-06-11 10:51:12 -070049
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070050 llvm::TargetData *mpTargetData;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070051
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070052 // This helps us translate Clang AST using into LLVM IR
53 clang::CodeGenerator *mGen;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070054
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070055 // Passes
Shih-wei Liao462aefd2010-06-04 15:32:04 -070056
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070057 // Passes apply on function scope in a translation unit
58 llvm::FunctionPassManager *mPerFunctionPasses;
59 // Passes apply on module scope
zonr6315f762010-10-05 15:35:14 +080060 llvm::PassManager *mPerModulePasses;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070061 // Passes for code emission
62 llvm::FunctionPassManager *mCodeGenPasses;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070063
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070064 llvm::formatted_raw_ostream FormattedOutStream;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070065
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070066 bool mAllowRSPrefix;
Shih-wei Liaocecd11d2010-09-21 08:07:58 -070067
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070068 inline void CreateFunctionPasses() {
69 if (!mPerFunctionPasses) {
70 mPerFunctionPasses = new llvm::FunctionPassManager(mpModule);
71 mPerFunctionPasses->add(new llvm::TargetData(*mpTargetData));
Shih-wei Liao462aefd2010-06-04 15:32:04 -070072
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070073 llvm::createStandardFunctionPasses(mPerFunctionPasses,
74 mCodeGenOpts.OptimizationLevel);
75 }
76 return;
77 }
78
79 inline void CreateModulePasses() {
80 if (!mPerModulePasses) {
81 mPerModulePasses = new llvm::PassManager();
82 mPerModulePasses->add(new llvm::TargetData(*mpTargetData));
83
84 llvm::createStandardModulePasses(mPerModulePasses,
85 mCodeGenOpts.OptimizationLevel,
86 mCodeGenOpts.OptimizeSize,
87 mCodeGenOpts.UnitAtATime,
88 mCodeGenOpts.UnrollLoops,
89 // Some libc functions will be replaced
90 // by the LLVM built-in optimized
91 // function (e.g. strcmp)
92 /* SimplifyLibCalls */true,
93 /* HaveExceptions */false,
94 /* InliningPass */NULL);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070095 }
96
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070097 // llvm::createStandardFunctionPasses and llvm::createStandardModulePasses
98 // insert lots of optimization passes for the code generator. For the
99 // conventional desktop PC which memory resources and computation power is
100 // relatively large, doing lots optimization as possible is reasonible and
101 // feasible. However, on the mobile device or embedded system, this may
102 // cause some problem due to the hardware resources limitation. So they need
103 // to be further refined.
104 return;
105 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700106
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700107 bool CreateCodeGenPasses();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700108
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700109 protected:
110 llvm::LLVMContext &mLLVMContext;
111 clang::Diagnostic &mDiags;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700112
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700113 llvm::Module *mpModule;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700114
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700115 const PragmaList &mPragmas;
Shih-wei Liaocecd11d2010-09-21 08:07:58 -0700116
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700117 // Extra handler for subclass to handle translation unit before emission
118 virtual void HandleTranslationUnitEx(clang::ASTContext &Ctx) { return; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700119
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700120 public:
121 Backend(clang::Diagnostic &Diags,
122 const clang::CodeGenOptions &CodeGenOpts,
123 const clang::TargetOptions &TargetOpts,
124 const PragmaList &Pragmas,
125 llvm::raw_ostream *OS,
126 SlangCompilerOutputTy OutputType,
127 clang::SourceManager &SourceMgr,
128 bool AllowRSPrefix);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700129
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700130 // Initialize - This is called to initialize the consumer, providing the
131 // ASTContext.
132 virtual void Initialize(clang::ASTContext &Ctx);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700133
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700134 // HandleTopLevelDecl - Handle the specified top-level declaration. This is
135 // called by the parser to process every top-level Decl*. Note that D can be
136 // the head of a chain of Decls (e.g. for `int a, b` the chain will have two
137 // elements). Use Decl::getNextDeclarator() to walk the chain.
138 virtual void HandleTopLevelDecl(clang::DeclGroupRef D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700139
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700140 // HandleTranslationUnit - This method is called when the ASTs for entire
141 // translation unit have been parsed.
142 virtual void HandleTranslationUnit(clang::ASTContext &Ctx);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700143
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700144 // HandleTagDeclDefinition - This callback is invoked each time a TagDecl
145 // (e.g. struct, union, enum, class) is completed. This allows the client to
146 // hack on the type, which can occur at any point in the file (because these
147 // can be defined in declspecs).
148 virtual void HandleTagDeclDefinition(clang::TagDecl *D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700149
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700150 // CompleteTentativeDefinition - Callback invoked at the end of a translation
151 // unit to notify the consumer that the given tentative definition should be
152 // completed.
153 virtual void CompleteTentativeDefinition(clang::VarDecl *D);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700154
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700155 virtual ~Backend();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700156};
157
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700158} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700159
zonr6315f762010-10-05 15:35:14 +0800160#endif // _SLANG_COMPILER_BACKEND_H