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