blob: a22c329e8574150932dd16573e25b506477076fb [file] [log] [blame]
Logan1f028c02010-11-27 01:02:48 +08001/*
2 * Copyright 2010, The Android Open Source Project
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
17#ifndef BCC_COMPILER_H
18#define BCC_COMPILER_H
19
20#include <bcc/bcc.h>
21
Loganc4395232010-11-27 18:54:17 +080022#include "CodeEmitter.h"
23#include "CodeMemoryManager.h"
Logan1f028c02010-11-27 01:02:48 +080024
25#include "llvm/ADT/OwningPtr.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/Target/TargetMachine.h"
28
29#include <stddef.h>
30
31#include <list>
32#include <string>
33#include <vector>
34#include <utility>
35
36
Logan1f028c02010-11-27 01:02:48 +080037namespace llvm {
38 class LLVMContext;
39 class Module;
Logan474cbd22011-01-31 01:47:44 +080040 class MemoryBuffer;
Logan1f028c02010-11-27 01:02:48 +080041}
42
43
44namespace bcc {
Logan2a6dc822011-01-06 04:05:20 +080045 class ScriptCompiled;
Logan1f028c02010-11-27 01:02:48 +080046
47 class Compiler {
Logande2ca792010-11-27 22:15:39 +080048 private:
Logan1f028c02010-11-27 01:02:48 +080049 //////////////////////////////////////////////////////////////////////////
50 // The variable section below (e.g., Triple, CodeGenOptLevel)
51 // is initialized in GlobalInitialization()
52 //
53 static bool GlobalInitialized;
54
Logan1f028c02010-11-27 01:02:48 +080055 // If given, this will be the name of the target triple to compile for.
56 // If not given, the initial values defined in this file will be used.
57 static std::string Triple;
58
59 static llvm::CodeGenOpt::Level CodeGenOptLevel;
60
61 // End of section of GlobalInitializing variables
62 /////////////////////////////////////////////////////////////////////////
63 // If given, the name of the target CPU to generate code for.
64 static std::string CPU;
65
66 // The list of target specific features to enable or disable -- this should
67 // be a list of strings starting with '+' (enable) or '-' (disable).
68 static std::vector<std::string> Features;
69
Logan1f028c02010-11-27 01:02:48 +080070 static void LLVMErrorHandler(void *UserData, const std::string &Message);
71
72 static const llvm::StringRef PragmaMetadataName;
73 static const llvm::StringRef ExportVarMetadataName;
74 static const llvm::StringRef ExportFuncMetadataName;
Stephen Hines071288a2011-01-27 14:38:26 -080075 static const llvm::StringRef ObjectSlotMetadataName;
Logan1f028c02010-11-27 01:02:48 +080076
77 friend class CodeEmitter;
78 friend class CodeMemoryManager;
79
Logande2ca792010-11-27 22:15:39 +080080
Logan1f028c02010-11-27 01:02:48 +080081 private:
Logan2a6dc822011-01-06 04:05:20 +080082 ScriptCompiled *mpResult;
83
Logan1f028c02010-11-27 01:02:48 +080084 std::string mError;
85
Logan1f028c02010-11-27 01:02:48 +080086 // The memory manager for code emitter
87 llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr;
Logan1f028c02010-11-27 01:02:48 +080088
89 // The CodeEmitter
90 llvm::OwningPtr<CodeEmitter> mCodeEmitter;
Logan1f028c02010-11-27 01:02:48 +080091
92 BCCSymbolLookupFn mpSymbolLookupFn;
93 void *mpSymbolLookupContext;
94
95 llvm::LLVMContext *mContext;
96 llvm::Module *mModule;
97
98 bool mHasLinked;
99
100 public:
Logan2a6dc822011-01-06 04:05:20 +0800101 Compiler(ScriptCompiled *result);
Logan1f028c02010-11-27 01:02:48 +0800102
Loganecf4cbd2011-01-06 05:34:11 +0800103 static void GlobalInitialization();
104
Loganf340bf72011-01-14 17:51:40 +0800105 void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
Logan1f028c02010-11-27 01:02:48 +0800106 mpSymbolLookupFn = pFn;
107 mpSymbolLookupContext = pContext;
108 }
109
Logande2ca792010-11-27 22:15:39 +0800110 CodeMemoryManager *createCodeMemoryManager();
111
112 CodeEmitter *createCodeEmitter();
113
Logan474cbd22011-01-31 01:47:44 +0800114 llvm::Module *parseBitcodeFile(llvm::MemoryBuffer *MEM);
115
Logan1f028c02010-11-27 01:02:48 +0800116 int readModule(llvm::Module *module) {
Logan1f028c02010-11-27 01:02:48 +0800117 mModule = module;
118 return hasError();
119 }
120
Logan474cbd22011-01-31 01:47:44 +0800121 int linkModule(llvm::Module *module);
Logan1f028c02010-11-27 01:02:48 +0800122
Logan1f028c02010-11-27 01:02:48 +0800123 int compile();
124
Logan38d06072010-12-29 00:16:39 +0800125 char const *getErrorMessage() {
126 return mError.c_str();
Logan1f028c02010-11-27 01:02:48 +0800127 }
128
Logan1f028c02010-11-27 01:02:48 +0800129 const llvm::Module *getModule() const {
130 return mModule;
131 }
132
133 ~Compiler();
134
135 private:
Logande2ca792010-11-27 22:15:39 +0800136
137 bool hasError() const {
138 return !mError.empty();
139 }
140
141 void setError(const char *Error) {
142 mError.assign(Error); // Copying
143 }
144
145 void setError(const std::string &Error) {
146 mError = Error;
147 }
148
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800149 }; // End of class Compiler
Logan1f028c02010-11-27 01:02:48 +0800150
Logan9b504eb2010-11-27 18:19:35 +0800151} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800152
153#endif // BCC_COMPILER_H