blob: 630be08fbfa24b5ddba06c0d8f29add719a65d3c [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;
Stephen Hinesb730e232013-01-09 15:31:36 -080023class DataLayout;
Zonr Changade92772012-04-13 15:58:24 +080024class TargetMachine;
25
Stephen Hinesdee928b2014-02-10 17:19:46 -080026namespace legacy {
27class PassManager;
28} // end namespace legacy
29
30using legacy::PassManager;
31
Zonr Changade92772012-04-13 15:58:24 +080032} // end namespace llvm
Logan1f028c02010-11-27 01:02:48 +080033
34namespace bcc {
35
Zonr Changade92772012-04-13 15:58:24 +080036class CompilerConfig;
37class OutputFile;
38class Script;
Logan1f028c02010-11-27 01:02:48 +080039
Zonr Changade92772012-04-13 15:58:24 +080040//===----------------------------------------------------------------------===//
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.
52class Compiler {
53public:
54 enum ErrorCode {
55 kSuccess,
Logan1f028c02010-11-27 01:02:48 +080056
Zonr Changade92772012-04-13 15:58:24 +080057 kInvalidConfigNoTarget,
58 kErrCreateTargetMachine,
59 kErrSwitchTargetMachine,
60 kErrNoTargetMachine,
Stephen Hinesb730e232013-01-09 15:31:36 -080061 kErrDataLayoutNoMemory,
Zonr Changade92772012-04-13 15:58:24 +080062 kErrMaterialization,
63 kErrInvalidOutputFileState,
64 kErrPrepareOutput,
65 kPrepareCodeGenPass,
Logan1f028c02010-11-27 01:02:48 +080066
Zonr Changade92772012-04-13 15:58:24 +080067 kErrHookBeforeAddLTOPasses,
68 kErrHookAfterAddLTOPasses,
Zonr Changade92772012-04-13 15:58:24 +080069 kErrHookAfterExecuteLTOPasses,
Logan1f028c02010-11-27 01:02:48 +080070
Zonr Changade92772012-04-13 15:58:24 +080071 kErrHookBeforeAddCodeGenPasses,
72 kErrHookAfterAddCodeGenPasses,
73 kErrHookBeforeExecuteCodeGenPasses,
74 kErrHookAfterExecuteCodeGenPasses,
Logan1f028c02010-11-27 01:02:48 +080075
Tobias Grosser5b7f52a2013-07-23 14:57:00 -070076 kErrInvalidSource
Zonr Changade92772012-04-13 15:58:24 +080077 };
Logan1f028c02010-11-27 01:02:48 +080078
Zonr Changade92772012-04-13 15:58:24 +080079 static const char *GetErrorString(enum ErrorCode pErrCode);
Logan1f028c02010-11-27 01:02:48 +080080
Zonr Changade92772012-04-13 15:58:24 +080081private:
82 llvm::TargetMachine *mTarget;
83 // LTO is enabled by default.
84 bool mEnableLTO;
Logan2a6dc822011-01-06 04:05:20 +080085
Zonr Changade92772012-04-13 15:58:24 +080086 enum ErrorCode runLTO(Script &pScript);
87 enum ErrorCode runCodeGen(Script &pScript, llvm::raw_ostream &pResult);
Logan1f028c02010-11-27 01:02:48 +080088
Zonr Changade92772012-04-13 15:58:24 +080089public:
90 Compiler();
91 Compiler(const CompilerConfig &pConfig);
Logan Chienda5e0c32011-06-13 03:47:21 +080092
Zonr Changade92772012-04-13 15:58:24 +080093 enum ErrorCode config(const CompilerConfig &pConfig);
Logan1f028c02010-11-27 01:02:48 +080094
Zonr Changade92772012-04-13 15:58:24 +080095 // Compile a script and output the result to a LLVM stream.
Tobias Grosser27fb7ed2013-06-21 18:34:56 -070096 //
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);
Logan1f028c02010-11-27 01:02:48 +0800101
Zonr Changade92772012-04-13 15:58:24 +0800102 // Compile a script and output the result to a file.
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700103 enum ErrorCode compile(Script &pScript, OutputFile &pResult,
104 llvm::raw_ostream *IRStream = 0);
Logan1f028c02010-11-27 01:02:48 +0800105
Shih-wei Liao0dbd4fb2012-07-27 03:09:47 -0700106 const llvm::TargetMachine& getTargetMachine() const
107 { return *mTarget; }
108
Zonr Changade92772012-04-13 15:58:24 +0800109 void enableLTO(bool pEnable = true)
110 { mEnableLTO = pEnable; }
Logan1f028c02010-11-27 01:02:48 +0800111
Zonr Changade92772012-04-13 15:58:24 +0800112 virtual ~Compiler();
Loganecf4cbd2011-01-06 05:34:11 +0800113
Zonr Changade92772012-04-13 15:58:24 +0800114protected:
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 Chien9347e0b2011-07-07 19:51:47 +0800121
Zonr Changade92772012-04-13 15:58:24 +0800122 // Called after adding last pass to code-generation passes.
123 virtual bool afterAddLTOPasses(Script &pScript, llvm::PassManager &pPM)
124 { return true; }
Shih-wei Liao4deffde2012-01-17 20:38:17 +0800125
Zonr Changade92772012-04-13 15:58:24 +0800126 // Called before executing code-generation passes.
127 virtual bool beforeExecuteLTOPasses(Script &pScript,
128 llvm::PassManager &pPM)
129 { return true; }
Logan1f028c02010-11-27 01:02:48 +0800130
Zonr Changade92772012-04-13 15:58:24 +0800131 // Called after executing code-generation passes.
132 virtual bool afterExecuteLTOPasses(Script &pScript)
133 { return true; }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700134
Zonr Changade92772012-04-13 15:58:24 +0800135 // Called before adding first pass to code-generation passes.
136 virtual bool beforeAddCodeGenPasses(Script &pScript, llvm::PassManager &pPM)
137 { return true; }
Logan Chienda5e0c32011-06-13 03:47:21 +0800138
Zonr Changade92772012-04-13 15:58:24 +0800139 // Called after adding last pass to code-generation passes.
140 virtual bool afterAddCodeGenPasses(Script &pScript, llvm::PassManager &pPM)
141 { return true; }
Logan1f028c02010-11-27 01:02:48 +0800142
Zonr Changade92772012-04-13 15:58:24 +0800143 // Called before executing code-generation passes.
144 virtual bool beforeExecuteCodeGenPasses(Script &pScript,
145 llvm::PassManager &pPM)
146 { return true; }
Logan1f028c02010-11-27 01:02:48 +0800147
Zonr Changade92772012-04-13 15:58:24 +0800148 // Called after executing code-generation passes.
149 virtual bool afterExecuteCodeGenPasses(Script &pScript)
150 { return true; }
151};
Logan1f028c02010-11-27 01:02:48 +0800152
Zonr Changade92772012-04-13 15:58:24 +0800153} // end namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800154
Zonr Changc72c4dd2012-04-12 15:38:53 +0800155#endif // BCC_COMPILER_H