Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | db16918 | 2012-01-05 18:46:36 -0800 | [diff] [blame] | 2 | * Copyright 2010-2012, The Android Open Source Project |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 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 | |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 17 | #ifndef BCC_COMPILER_H |
| 18 | #define BCC_COMPILER_H |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 19 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 20 | namespace llvm { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 21 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 22 | class raw_ostream; |
Stephen Hines | b730e23 | 2013-01-09 15:31:36 -0800 | [diff] [blame] | 23 | class DataLayout; |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 24 | class TargetMachine; |
| 25 | |
Stephen Hines | dee928b | 2014-02-10 17:19:46 -0800 | [diff] [blame] | 26 | namespace legacy { |
| 27 | class PassManager; |
| 28 | } // end namespace legacy |
| 29 | |
| 30 | using legacy::PassManager; |
| 31 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 32 | } // end namespace llvm |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 33 | |
| 34 | namespace bcc { |
| 35 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 36 | class CompilerConfig; |
| 37 | class OutputFile; |
| 38 | class Script; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 39 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | // Design of Compiler |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | // 1. A compiler instance can be constructed provided an "initial config." |
| 44 | // 2. A compiler can later be re-configured using config(). |
| 45 | // 3. Once config() is invoked, it'll re-create TargetMachine instance (i.e., |
| 46 | // mTarget) according to the configuration supplied. TargetMachine instance |
| 47 | // is *shared* across the different calls to compile() before the next call |
| 48 | // to config(). |
| 49 | // 4. Once a compiler instance is created, you can use the compile() service |
| 50 | // to compile the file over and over again. Each call uses TargetMachine |
| 51 | // instance to construct the compilation passes. |
| 52 | class Compiler { |
| 53 | public: |
| 54 | enum ErrorCode { |
| 55 | kSuccess, |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 56 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 57 | kInvalidConfigNoTarget, |
| 58 | kErrCreateTargetMachine, |
| 59 | kErrSwitchTargetMachine, |
| 60 | kErrNoTargetMachine, |
Stephen Hines | b730e23 | 2013-01-09 15:31:36 -0800 | [diff] [blame] | 61 | kErrDataLayoutNoMemory, |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 62 | kErrMaterialization, |
| 63 | kErrInvalidOutputFileState, |
| 64 | kErrPrepareOutput, |
| 65 | kPrepareCodeGenPass, |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 66 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 67 | kErrHookBeforeAddLTOPasses, |
| 68 | kErrHookAfterAddLTOPasses, |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 69 | kErrHookAfterExecuteLTOPasses, |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 70 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 71 | kErrHookBeforeAddCodeGenPasses, |
| 72 | kErrHookAfterAddCodeGenPasses, |
| 73 | kErrHookBeforeExecuteCodeGenPasses, |
| 74 | kErrHookAfterExecuteCodeGenPasses, |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 75 | |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 76 | kErrInvalidSource |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 77 | }; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 78 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 79 | static const char *GetErrorString(enum ErrorCode pErrCode); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 80 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 81 | private: |
| 82 | llvm::TargetMachine *mTarget; |
| 83 | // LTO is enabled by default. |
| 84 | bool mEnableLTO; |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 85 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 86 | enum ErrorCode runLTO(Script &pScript); |
| 87 | enum ErrorCode runCodeGen(Script &pScript, llvm::raw_ostream &pResult); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 88 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 89 | public: |
| 90 | Compiler(); |
| 91 | Compiler(const CompilerConfig &pConfig); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 92 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 93 | enum ErrorCode config(const CompilerConfig &pConfig); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 94 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 95 | // Compile a script and output the result to a LLVM stream. |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 96 | // |
| 97 | // @param IRStream If not NULL, the LLVM-IR that is fed to code generation |
| 98 | // will be written to IRStream. |
| 99 | enum ErrorCode compile(Script &pScript, llvm::raw_ostream &pResult, |
| 100 | llvm::raw_ostream *IRStream); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 101 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 102 | // Compile a script and output the result to a file. |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 103 | enum ErrorCode compile(Script &pScript, OutputFile &pResult, |
| 104 | llvm::raw_ostream *IRStream = 0); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 105 | |
Shih-wei Liao | 0dbd4fb | 2012-07-27 03:09:47 -0700 | [diff] [blame] | 106 | const llvm::TargetMachine& getTargetMachine() const |
| 107 | { return *mTarget; } |
| 108 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 109 | void enableLTO(bool pEnable = true) |
| 110 | { mEnableLTO = pEnable; } |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 111 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 112 | virtual ~Compiler(); |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 113 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 114 | protected: |
| 115 | //===--------------------------------------------------------------------===// |
| 116 | // Plugin callbacks for sub-class. |
| 117 | //===--------------------------------------------------------------------===// |
| 118 | // Called before adding first pass to code-generation passes. |
| 119 | virtual bool beforeAddLTOPasses(Script &pScript, llvm::PassManager &pPM) |
| 120 | { return true; } |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 121 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 122 | // Called after adding last pass to code-generation passes. |
| 123 | virtual bool afterAddLTOPasses(Script &pScript, llvm::PassManager &pPM) |
| 124 | { return true; } |
Shih-wei Liao | 4deffde | 2012-01-17 20:38:17 +0800 | [diff] [blame] | 125 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 126 | // Called before executing code-generation passes. |
| 127 | virtual bool beforeExecuteLTOPasses(Script &pScript, |
| 128 | llvm::PassManager &pPM) |
| 129 | { return true; } |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 130 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 131 | // Called after executing code-generation passes. |
| 132 | virtual bool afterExecuteLTOPasses(Script &pScript) |
| 133 | { return true; } |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 134 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 135 | // Called before adding first pass to code-generation passes. |
| 136 | virtual bool beforeAddCodeGenPasses(Script &pScript, llvm::PassManager &pPM) |
| 137 | { return true; } |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 138 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 139 | // Called after adding last pass to code-generation passes. |
| 140 | virtual bool afterAddCodeGenPasses(Script &pScript, llvm::PassManager &pPM) |
| 141 | { return true; } |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 142 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 143 | // Called before executing code-generation passes. |
| 144 | virtual bool beforeExecuteCodeGenPasses(Script &pScript, |
| 145 | llvm::PassManager &pPM) |
| 146 | { return true; } |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 147 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 148 | // Called after executing code-generation passes. |
| 149 | virtual bool afterExecuteCodeGenPasses(Script &pScript) |
| 150 | { return true; } |
| 151 | }; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 152 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 153 | } // end namespace bcc |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 154 | |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 155 | #endif // BCC_COMPILER_H |