blob: fb519408442d65eee1690c22e81fd32a40fe946d [file] [log] [blame]
Logan1f028c02010-11-27 01:02:48 +08001/*
Stephen Hinesdb169182012-01-05 18:46:36 -08002 * Copyright 2010-2012, The Android Open Source Project
Logan1f028c02010-11-27 01:02:48 +08003 *
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 Changc72c4dd2012-04-12 15:38:53 +080017#ifndef BCC_COMPILER_H
18#define BCC_COMPILER_H
Logan1f028c02010-11-27 01:02:48 +080019
Logan1f028c02010-11-27 01:02:48 +080020namespace llvm {
Logan1f028c02010-11-27 01:02:48 +080021
Zonr Changade92772012-04-13 15:58:24 +080022class raw_ostream;
23class PassManager;
Stephen Hinesb730e232013-01-09 15:31:36 -080024class DataLayout;
Zonr Changade92772012-04-13 15:58:24 +080025class TargetMachine;
26
27} // end namespace llvm
Logan1f028c02010-11-27 01:02:48 +080028
29namespace bcc {
30
Zonr Changade92772012-04-13 15:58:24 +080031class CompilerConfig;
32class OutputFile;
33class Script;
Logan1f028c02010-11-27 01:02:48 +080034
Zonr Changade92772012-04-13 15:58:24 +080035//===----------------------------------------------------------------------===//
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.
47class Compiler {
48public:
49 enum ErrorCode {
50 kSuccess,
Logan1f028c02010-11-27 01:02:48 +080051
Zonr Changade92772012-04-13 15:58:24 +080052 kInvalidConfigNoTarget,
53 kErrCreateTargetMachine,
54 kErrSwitchTargetMachine,
55 kErrNoTargetMachine,
Stephen Hinesb730e232013-01-09 15:31:36 -080056 kErrDataLayoutNoMemory,
Zonr Changade92772012-04-13 15:58:24 +080057 kErrMaterialization,
58 kErrInvalidOutputFileState,
59 kErrPrepareOutput,
60 kPrepareCodeGenPass,
Logan1f028c02010-11-27 01:02:48 +080061
Zonr Changade92772012-04-13 15:58:24 +080062 kErrHookBeforeAddLTOPasses,
63 kErrHookAfterAddLTOPasses,
64 kErrHookBeforeExecuteLTOPasses,
65 kErrHookAfterExecuteLTOPasses,
Logan1f028c02010-11-27 01:02:48 +080066
Zonr Changade92772012-04-13 15:58:24 +080067 kErrHookBeforeAddCodeGenPasses,
68 kErrHookAfterAddCodeGenPasses,
69 kErrHookBeforeExecuteCodeGenPasses,
70 kErrHookAfterExecuteCodeGenPasses,
Logan1f028c02010-11-27 01:02:48 +080071
Stephen Hines47f0d5a2013-06-05 00:27:38 -070072 kErrInvalidSource,
73
74 kMaxErrorCode
Zonr Changade92772012-04-13 15:58:24 +080075 };
Logan1f028c02010-11-27 01:02:48 +080076
Zonr Changade92772012-04-13 15:58:24 +080077 static const char *GetErrorString(enum ErrorCode pErrCode);
Logan1f028c02010-11-27 01:02:48 +080078
Zonr Changade92772012-04-13 15:58:24 +080079private:
80 llvm::TargetMachine *mTarget;
81 // LTO is enabled by default.
82 bool mEnableLTO;
Logan2a6dc822011-01-06 04:05:20 +080083
Zonr Changade92772012-04-13 15:58:24 +080084 enum ErrorCode runLTO(Script &pScript);
85 enum ErrorCode runCodeGen(Script &pScript, llvm::raw_ostream &pResult);
Logan1f028c02010-11-27 01:02:48 +080086
Zonr Changade92772012-04-13 15:58:24 +080087public:
88 Compiler();
89 Compiler(const CompilerConfig &pConfig);
Logan Chienda5e0c32011-06-13 03:47:21 +080090
Zonr Changade92772012-04-13 15:58:24 +080091 enum ErrorCode config(const CompilerConfig &pConfig);
Logan1f028c02010-11-27 01:02:48 +080092
Zonr Changade92772012-04-13 15:58:24 +080093 // Compile a script and output the result to a LLVM stream.
Tobias Grosser27fb7ed2013-06-21 18:34:56 -070094 //
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);
Logan1f028c02010-11-27 01:02:48 +080099
Zonr Changade92772012-04-13 15:58:24 +0800100 // Compile a script and output the result to a file.
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700101 enum ErrorCode compile(Script &pScript, OutputFile &pResult,
102 llvm::raw_ostream *IRStream = 0);
Logan1f028c02010-11-27 01:02:48 +0800103
Shih-wei Liao0dbd4fb2012-07-27 03:09:47 -0700104 const llvm::TargetMachine& getTargetMachine() const
105 { return *mTarget; }
106
Zonr Changade92772012-04-13 15:58:24 +0800107 void enableLTO(bool pEnable = true)
108 { mEnableLTO = pEnable; }
Logan1f028c02010-11-27 01:02:48 +0800109
Zonr Changade92772012-04-13 15:58:24 +0800110 virtual ~Compiler();
Loganecf4cbd2011-01-06 05:34:11 +0800111
Zonr Changade92772012-04-13 15:58:24 +0800112protected:
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 Chien9347e0b2011-07-07 19:51:47 +0800119
Zonr Changade92772012-04-13 15:58:24 +0800120 // Called after adding last pass to code-generation passes.
121 virtual bool afterAddLTOPasses(Script &pScript, llvm::PassManager &pPM)
122 { return true; }
Shih-wei Liao4deffde2012-01-17 20:38:17 +0800123
Zonr Changade92772012-04-13 15:58:24 +0800124 // Called before executing code-generation passes.
125 virtual bool beforeExecuteLTOPasses(Script &pScript,
126 llvm::PassManager &pPM)
127 { return true; }
Logan1f028c02010-11-27 01:02:48 +0800128
Zonr Changade92772012-04-13 15:58:24 +0800129 // Called after executing code-generation passes.
130 virtual bool afterExecuteLTOPasses(Script &pScript)
131 { return true; }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700132
Zonr Changade92772012-04-13 15:58:24 +0800133 // Called before adding first pass to code-generation passes.
134 virtual bool beforeAddCodeGenPasses(Script &pScript, llvm::PassManager &pPM)
135 { return true; }
Logan Chienda5e0c32011-06-13 03:47:21 +0800136
Zonr Changade92772012-04-13 15:58:24 +0800137 // Called after adding last pass to code-generation passes.
138 virtual bool afterAddCodeGenPasses(Script &pScript, llvm::PassManager &pPM)
139 { return true; }
Logan1f028c02010-11-27 01:02:48 +0800140
Zonr Changade92772012-04-13 15:58:24 +0800141 // Called before executing code-generation passes.
142 virtual bool beforeExecuteCodeGenPasses(Script &pScript,
143 llvm::PassManager &pPM)
144 { return true; }
Logan1f028c02010-11-27 01:02:48 +0800145
Zonr Changade92772012-04-13 15:58:24 +0800146 // Called after executing code-generation passes.
147 virtual bool afterExecuteCodeGenPasses(Script &pScript)
148 { return true; }
149};
Logan1f028c02010-11-27 01:02:48 +0800150
Zonr Changade92772012-04-13 15:58:24 +0800151} // end namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800152
Zonr Changc72c4dd2012-04-12 15:38:53 +0800153#endif // BCC_COMPILER_H