llvm.org GIT mirror llvm / a3f99f9
First patch in the direction of splitting MachineCodeEmitter in two subclasses: JITCodeEmitter and ObjectCodeEmitter. No functional changes yet. Patch by Aaron Gray git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@72631 91177308-0d34-0410-b5e6-96231b3b80d8 Bruno Cardoso Lopes 11 years ago
33 changed file(s) with 1130 addition(s) and 393 deletion(s). Raw diff Collapse all Expand all
0 //===-- llvm/CodeGen/JITCodeEmitter.h - Code emission ----------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines an abstract interface that is used by the machine code
10 // emission framework to output the code. This allows machine code emission to
11 // be separated from concerns such as resolution of call targets, and where the
12 // machine code will be written (memory or disk, f.e.).
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_CODEGEN_JITCODEEMITTER_H
17 #define LLVM_CODEGEN_JITCODEEMITTER_H
18
19 #include
20 #include "llvm/Support/DataTypes.h"
21 #include "llvm/Support/Streams.h"
22 #include "llvm/CodeGen/MachineCodeEmitter.h"
23
24 using namespace std;
25
26 namespace llvm {
27
28 class MachineBasicBlock;
29 class MachineConstantPool;
30 class MachineJumpTableInfo;
31 class MachineFunction;
32 class MachineModuleInfo;
33 class MachineRelocation;
34 class Value;
35 class GlobalValue;
36 class Function;
37
38 /// JITCodeEmitter - This class defines two sorts of methods: those for
39 /// emitting the actual bytes of machine code, and those for emitting auxillary
40 /// structures, such as jump tables, relocations, etc.
41 ///
42 /// Emission of machine code is complicated by the fact that we don't (in
43 /// general) know the size of the machine code that we're about to emit before
44 /// we emit it. As such, we preallocate a certain amount of memory, and set the
45 /// BufferBegin/BufferEnd pointers to the start and end of the buffer. As we
46 /// emit machine instructions, we advance the CurBufferPtr to indicate the
47 /// location of the next byte to emit. In the case of a buffer overflow (we
48 /// need to emit more machine code than we have allocated space for), the
49 /// CurBufferPtr will saturate to BufferEnd and ignore stores. Once the entire
50 /// function has been emitted, the overflow condition is checked, and if it has
51 /// occurred, more memory is allocated, and we reemit the code into it.
52 ///
53 class JITCodeEmitter : public MachineCodeEmitter {
54 public:
55 virtual ~JITCodeEmitter() {}
56
57 /// startFunction - This callback is invoked when the specified function is
58 /// about to be code generated. This initializes the BufferBegin/End/Ptr
59 /// fields.
60 ///
61 virtual void startFunction(MachineFunction &F) = 0;
62
63 /// finishFunction - This callback is invoked when the specified function has
64 /// finished code generation. If a buffer overflow has occurred, this method
65 /// returns true (the callee is required to try again), otherwise it returns
66 /// false.
67 ///
68 virtual bool finishFunction(MachineFunction &F) = 0;
69
70 /// startGVStub - This callback is invoked when the JIT needs the
71 /// address of a GV (e.g. function) that has not been code generated yet.
72 /// The StubSize specifies the total size required by the stub.
73 ///
74 virtual void startGVStub(const GlobalValue* GV, unsigned StubSize,
75 unsigned Alignment = 1) = 0;
76
77 /// startGVStub - This callback is invoked when the JIT needs the address of a
78 /// GV (e.g. function) that has not been code generated yet. Buffer points to
79 /// memory already allocated for this stub.
80 ///
81 virtual void startGVStub(const GlobalValue* GV, void *Buffer,
82 unsigned StubSize) = 0;
83
84 /// finishGVStub - This callback is invoked to terminate a GV stub.
85 ///
86 virtual void *finishGVStub(const GlobalValue* F) = 0;
87
88 /// emitByte - This callback is invoked when a byte needs to be written to the
89 /// output stream.
90 ///
91 void emitByte(unsigned char B) {
92 if (CurBufferPtr != BufferEnd)
93 *CurBufferPtr++ = B;
94 }
95
96 /// emitWordLE - This callback is invoked when a 32-bit word needs to be
97 /// written to the output stream in little-endian format.
98 ///
99 void emitWordLE(unsigned W) {
100 if (4 <= BufferEnd-CurBufferPtr) {
101 *CurBufferPtr++ = (unsigned char)(W >> 0);
102 *CurBufferPtr++ = (unsigned char)(W >> 8);
103 *CurBufferPtr++ = (unsigned char)(W >> 16);
104 *CurBufferPtr++ = (unsigned char)(W >> 24);
105 } else {
106 CurBufferPtr = BufferEnd;
107 }
108 }
109
110 /// emitWordBE - This callback is invoked when a 32-bit word needs to be
111 /// written to the output stream in big-endian format.
112 ///
113 void emitWordBE(unsigned W) {
114 if (4 <= BufferEnd-CurBufferPtr) {
115 *CurBufferPtr++ = (unsigned char)(W >> 24);
116 *CurBufferPtr++ = (unsigned char)(W >> 16);
117 *CurBufferPtr++ = (unsigned char)(W >> 8);
118 *CurBufferPtr++ = (unsigned char)(W >> 0);
119 } else {
120 CurBufferPtr = BufferEnd;
121 }
122 }
123
124 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
125 /// written to the output stream in little-endian format.
126 ///
127 void emitDWordLE(uint64_t W) {
128 if (8 <= BufferEnd-CurBufferPtr) {
129 *CurBufferPtr++ = (unsigned char)(W >> 0);
130 *CurBufferPtr++ = (unsigned char)(W >> 8);
131 *CurBufferPtr++ = (unsigned char)(W >> 16);
132 *CurBufferPtr++ = (unsigned char)(W >> 24);
133 *CurBufferPtr++ = (unsigned char)(W >> 32);
134 *CurBufferPtr++ = (unsigned char)(W >> 40);
135 *CurBufferPtr++ = (unsigned char)(W >> 48);
136 *CurBufferPtr++ = (unsigned char)(W >> 56);
137 } else {
138 CurBufferPtr = BufferEnd;
139 }
140 }
141
142 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
143 /// written to the output stream in big-endian format.
144 ///
145 void emitDWordBE(uint64_t W) {
146 if (8 <= BufferEnd-CurBufferPtr) {
147 *CurBufferPtr++ = (unsigned char)(W >> 56);
148 *CurBufferPtr++ = (unsigned char)(W >> 48);
149 *CurBufferPtr++ = (unsigned char)(W >> 40);
150 *CurBufferPtr++ = (unsigned char)(W >> 32);
151 *CurBufferPtr++ = (unsigned char)(W >> 24);
152 *CurBufferPtr++ = (unsigned char)(W >> 16);
153 *CurBufferPtr++ = (unsigned char)(W >> 8);
154 *CurBufferPtr++ = (unsigned char)(W >> 0);
155 } else {
156 CurBufferPtr = BufferEnd;
157 }
158 }
159
160 /// emitAlignment - Move the CurBufferPtr pointer up the the specified
161 /// alignment (saturated to BufferEnd of course).
162 void emitAlignment(unsigned Alignment) {
163 if (Alignment == 0) Alignment = 1;
164
165 if(Alignment <= (uintptr_t)(BufferEnd-CurBufferPtr)) {
166 // Move the current buffer ptr up to the specified alignment.
167 CurBufferPtr =
168 (unsigned char*)(((uintptr_t)CurBufferPtr+Alignment-1) &
169 ~(uintptr_t)(Alignment-1));
170 } else {
171 CurBufferPtr = BufferEnd;
172 }
173 }
174
175
176 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
177 /// written to the output stream.
178 void emitULEB128Bytes(unsigned Value) {
179 do {
180 unsigned char Byte = Value & 0x7f;
181 Value >>= 7;
182 if (Value) Byte |= 0x80;
183 emitByte(Byte);
184 } while (Value);
185 }
186
187 /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
188 /// written to the output stream.
189 void emitSLEB128Bytes(int Value) {
190 int Sign = Value >> (8 * sizeof(Value) - 1);
191 bool IsMore;
192
193 do {
194 unsigned char Byte = Value & 0x7f;
195 Value >>= 7;
196 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
197 if (IsMore) Byte |= 0x80;
198 emitByte(Byte);
199 } while (IsMore);
200 }
201
202 /// emitString - This callback is invoked when a String needs to be
203 /// written to the output stream.
204 void emitString(const std::string &String) {
205 for (unsigned i = 0, N = static_cast(String.size());
206 i < N; ++i) {
207 unsigned char C = String[i];
208 emitByte(C);
209 }
210 emitByte(0);
211 }
212
213 /// emitInt32 - Emit a int32 directive.
214 void emitInt32(int Value) {
215 if (4 <= BufferEnd-CurBufferPtr) {
216 *((uint32_t*)CurBufferPtr) = Value;
217 CurBufferPtr += 4;
218 } else {
219 CurBufferPtr = BufferEnd;
220 }
221 }
222
223 /// emitInt64 - Emit a int64 directive.
224 void emitInt64(uint64_t Value) {
225 if (8 <= BufferEnd-CurBufferPtr) {
226 *((uint64_t*)CurBufferPtr) = Value;
227 CurBufferPtr += 8;
228 } else {
229 CurBufferPtr = BufferEnd;
230 }
231 }
232
233 /// emitInt32At - Emit the Int32 Value in Addr.
234 void emitInt32At(uintptr_t *Addr, uintptr_t Value) {
235 if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
236 (*(uint32_t*)Addr) = (uint32_t)Value;
237 }
238
239 /// emitInt64At - Emit the Int64 Value in Addr.
240 void emitInt64At(uintptr_t *Addr, uintptr_t Value) {
241 if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
242 (*(uint64_t*)Addr) = (uint64_t)Value;
243 }
244
245
246 /// emitLabel - Emits a label
247 virtual void emitLabel(uint64_t LabelID) = 0;
248
249 /// allocateSpace - Allocate a block of space in the current output buffer,
250 /// returning null (and setting conditions to indicate buffer overflow) on
251 /// failure. Alignment is the alignment in bytes of the buffer desired.
252 virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
253 emitAlignment(Alignment);
254 void *Result;
255
256 // Check for buffer overflow.
257 if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
258 CurBufferPtr = BufferEnd;
259 Result = 0;
260 } else {
261 // Allocate the space.
262 Result = CurBufferPtr;
263 CurBufferPtr += Size;
264 }
265
266 return Result;
267 }
268
269 /// StartMachineBasicBlock - This should be called by the target when a new
270 /// basic block is about to be emitted. This way the MCE knows where the
271 /// start of the block is, and can implement getMachineBasicBlockAddress.
272 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
273
274 /// getCurrentPCValue - This returns the address that the next emitted byte
275 /// will be output to.
276 ///
277 virtual uintptr_t getCurrentPCValue() const {
278 return (uintptr_t)CurBufferPtr;
279 }
280
281 /// getCurrentPCOffset - Return the offset from the start of the emitted
282 /// buffer that we are currently writing to.
283 uintptr_t getCurrentPCOffset() const {
284 return CurBufferPtr-BufferBegin;
285 }
286
287 /// addRelocation - Whenever a relocatable address is needed, it should be
288 /// noted with this interface.
289 virtual void addRelocation(const MachineRelocation &MR) = 0;
290
291 /// FIXME: These should all be handled with relocations!
292
293 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
294 /// the constant pool that was last emitted with the emitConstantPool method.
295 ///
296 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
297
298 /// getJumpTableEntryAddress - Return the address of the jump table with index
299 /// 'Index' in the function that last called initJumpTableInfo.
300 ///
301 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
302
303 /// getMachineBasicBlockAddress - Return the address of the specified
304 /// MachineBasicBlock, only usable after the label for the MBB has been
305 /// emitted.
306 ///
307 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
308
309 /// getLabelAddress - Return the address of the specified LabelID, only usable
310 /// after the LabelID has been emitted.
311 ///
312 virtual uintptr_t getLabelAddress(uint64_t LabelID) const = 0;
313
314 /// Specifies the MachineModuleInfo object. This is used for exception handling
315 /// purposes.
316 virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
317 };
318
319 } // End llvm namespace
320
321 #endif
2222 namespace llvm {
2323 class Function;
2424 class GlobalValue;
25 class MachineCodeEmitter;
25 class JITCodeEmitter;
2626 class MachineRelocation;
2727
2828 /// TargetJITInfo - Target specific information required by the Just-In-Time
3838 ///
3939 virtual void replaceMachineCodeForFunction(void *Old, void *New) = 0;
4040
41 /// emitGlobalValueIndirectSym - Use the specified MachineCodeEmitter object
41 /// emitGlobalValueIndirectSym - Use the specified JITCodeEmitter object
4242 /// to emit an indirect symbol which contains the address of the specified
4343 /// ptr.
4444 virtual void *emitGlobalValueIndirectSym(const GlobalValue* GV, void *ptr,
45 MachineCodeEmitter &MCE) {
45 JITCodeEmitter &JCE) {
4646 assert(0 && "This target doesn't implement emitGlobalValueIndirectSym!");
4747 return 0;
4848 }
4949
50 /// emitFunctionStub - Use the specified MachineCodeEmitter object to emit a
50 /// emitFunctionStub - Use the specified JITCodeEmitter object to emit a
5151 /// small native function that simply calls the function at the specified
5252 /// address. Return the address of the resultant function.
5353 virtual void *emitFunctionStub(const Function* F, void *Fn,
54 MachineCodeEmitter &MCE) {
54 JITCodeEmitter &JCE) {
5555 assert(0 && "This target doesn't implement emitFunctionStub!");
5656 return 0;
5757 }
5858
59 /// emitFunctionStubAtAddr - Use the specified MachineCodeEmitter object to
59 /// emitFunctionStubAtAddr - Use the specified JITCodeEmitter object to
6060 /// emit a small native function that simply calls Fn. Emit the stub into
6161 /// the supplied buffer.
6262 virtual void emitFunctionStubAtAddr(const Function* F, void *Fn,
63 void *Buffer, MachineCodeEmitter &MCE) {
63 void *Buffer, JITCodeEmitter &JCE) {
6464 assert(0 && "This target doesn't implement emitFunctionStubAtAddr!");
6565 }
6666
124124
125125 /// allocateSeparateGVMemory - If true, globals should be placed in
126126 /// separately allocated heap memory rather than in the same
127 /// code memory allocated by MachineCodeEmitter.
127 /// code memory allocated by JITCodeEmitter.
128128 virtual bool allocateSeparateGVMemory() const { return false; }
129129 protected:
130130 bool useGOT;
2727 class TargetLowering;
2828 class TargetFrameInfo;
2929 class MachineCodeEmitter;
30 class JITCodeEmitter;
3031 class TargetRegisterInfo;
3132 class Module;
3233 class PassManagerBase;
235236 return true;
236237 }
237238
239 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
240 /// to be split up (e.g., to add an object writer pass), this method can be
241 /// used to finish up adding passes to emit the file, if necessary.
242 ///
243 virtual bool addPassesToEmitFileFinish(PassManagerBase &,
244 JITCodeEmitter *,
245 CodeGenOpt::Level) {
246 return true;
247 }
248
238249 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
239250 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
240251 /// actually outputting the machine code and resolving things like the address
243254 ///
244255 virtual bool addPassesToEmitMachineCode(PassManagerBase &,
245256 MachineCodeEmitter &,
257 CodeGenOpt::Level) {
258 return true;
259 }
260
261 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
262 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
263 /// actually outputting the machine code and resolving things like the address
264 /// of functions. This method returns true if machine code emission is
265 /// not supported.
266 ///
267 virtual bool addPassesToEmitMachineCode(PassManagerBase &,
268 JITCodeEmitter &,
246269 CodeGenOpt::Level) {
247270 return true;
248271 }
296319 MachineCodeEmitter *MCE,
297320 CodeGenOpt::Level);
298321
322 /// addPassesToEmitFileFinish - If the passes to emit the specified file had
323 /// to be split up (e.g., to add an object writer pass), this method can be
324 /// used to finish up adding passes to emit the file, if necessary.
325 ///
326 virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
327 JITCodeEmitter *MCE,
328 CodeGenOpt::Level);
329
299330 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
300331 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
301332 /// actually outputting the machine code and resolving things like the address
304335 ///
305336 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
306337 MachineCodeEmitter &MCE,
338 CodeGenOpt::Level);
339
340 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
341 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
342 /// actually outputting the machine code and resolving things like the address
343 /// of functions. This method returns true if machine code emission is
344 /// not supported.
345 ///
346 virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
347 JITCodeEmitter &MCE,
307348 CodeGenOpt::Level);
308349
309350 /// Target-Independent Code Generator Pass Configuration Options.
354395 return true;
355396 }
356397
398 /// addCodeEmitter - This pass should be overridden by the target to add a
399 /// code emitter, if supported. If this is not supported, 'true' should be
400 /// returned. If DumpAsm is true, the generated assembly is printed to cerr.
401 virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
402 bool /*DumpAsm*/, JITCodeEmitter &) {
403 return true;
404 }
405
357406 /// addSimpleCodeEmitter - This pass should be overridden by the target to add
358407 /// a code emitter (without setting flags), if supported. If this is not
359408 /// supported, 'true' should be returned. If DumpAsm is true, the generated
363412 return true;
364413 }
365414
415 /// addSimpleCodeEmitter - This pass should be overridden by the target to add
416 /// a code emitter (without setting flags), if supported. If this is not
417 /// supported, 'true' should be returned. If DumpAsm is true, the generated
418 /// assembly is printed to cerr.
419 virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
420 bool /*DumpAsm*/, JITCodeEmitter &) {
421 return true;
422 }
423
366424 /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
367425 /// on this target. User flag overrides.
368426 virtual bool getEnableTailMergeDefault() const { return true; }
108108 return false; // success!
109109 }
110110
111 /// addPassesToEmitFileFinish - If the passes to emit the specified file had to
112 /// be split up (e.g., to add an object writer pass), this method can be used to
113 /// finish up adding passes to emit the file, if necessary.
114 bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
115 JITCodeEmitter *JCE,
116 CodeGenOpt::Level OptLevel) {
117 if (JCE)
118 addSimpleCodeEmitter(PM, OptLevel, PrintEmittedAsm, *JCE);
119
120 PM.add(createGCInfoDeleter());
121
122 // Delete machine code for this function
123 PM.add(createMachineCodeDeleter());
124
125 return false; // success!
126 }
127
111128 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
112129 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
113130 /// actually outputting the machine code and resolving things like the address
134151 return false; // success!
135152 }
136153
154 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
155 /// get machine code emitted. This uses a MachineCodeEmitter object to handle
156 /// actually outputting the machine code and resolving things like the address
157 /// of functions. This method should returns true if machine code emission is
158 /// not supported.
159 ///
160 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
161 JITCodeEmitter &JCE,
162 CodeGenOpt::Level OptLevel) {
163 // Add common CodeGen passes.
164 if (addCommonCodeGenPasses(PM, OptLevel))
165 return true;
166
167 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
168 PM.add(createMachineFunctionPrinterPass(cerr));
169
170 addCodeEmitter(PM, OptLevel, PrintEmittedAsm, JCE);
171
172 PM.add(createGCInfoDeleter());
173
174 // Delete machine code for this function
175 PM.add(createMachineCodeDeleter());
176
177 return false; // success!
178 }
179
137180 static void printAndVerify(PassManagerBase &PM,
138181 bool allowDoubleDefs = false) {
139182 if (PrintMachineCode)
1818 #include "llvm/GlobalVariable.h"
1919 #include "llvm/Instructions.h"
2020 #include "llvm/ModuleProvider.h"
21 #include "llvm/CodeGen/MachineCodeEmitter.h"
21 #include "llvm/CodeGen/JITCodeEmitter.h"
2222 #include "llvm/ExecutionEngine/GenericValue.h"
2323 #include "llvm/CodeGen/MachineCodeInfo.h"
2424 #include "llvm/Target/TargetData.h"
213213
214214 jitstate = new JITState(MP);
215215
216 // Initialize MCE
217 MCE = createEmitter(*this, JMM);
216 // Initialize JCE
217 JCE = createEmitter(*this, JMM);
218218
219219 // Add target data
220220 MutexGuard locked(lock);
223223
224224 // Turn the machine code intermediate representation into bytes in memory that
225225 // may be executed.
226 if (TM.addPassesToEmitMachineCode(PM, *MCE, OptLevel)) {
226 if (TM.addPassesToEmitMachineCode(PM, *JCE, OptLevel)) {
227227 cerr << "Target does not support machine code emission!\n";
228228 abort();
229229 }
252252
253253 JIT::~JIT() {
254254 delete jitstate;
255 delete MCE;
255 delete JCE;
256256 delete &TM;
257257 }
258258
272272
273273 // Turn the machine code intermediate representation into bytes in memory
274274 // that may be executed.
275 if (TM.addPassesToEmitMachineCode(PM, *MCE, CodeGenOpt::Default)) {
275 if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) {
276276 cerr << "Target does not support machine code emission!\n";
277277 abort();
278278 }
305305
306306 // Turn the machine code intermediate representation into bytes in memory
307307 // that may be executed.
308 if (TM.addPassesToEmitMachineCode(PM, *MCE, CodeGenOpt::Default)) {
308 if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) {
309309 cerr << "Target does not support machine code emission!\n";
310310 abort();
311311 }
337337
338338 // Turn the machine code intermediate representation into bytes in memory
339339 // that may be executed.
340 if (TM.addPassesToEmitMachineCode(PM, *MCE, CodeGenOpt::Default)) {
340 if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) {
341341 cerr << "Target does not support machine code emission!\n";
342342 abort();
343343 }
653653 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
654654 }
655655 } else {
656 Ptr = MCE->allocateSpace(S, A);
656 Ptr = JCE->allocateSpace(S, A);
657657 }
658658 addGlobalMapping(GV, Ptr);
659659 EmitGlobalVariable(GV);
5050 class JIT : public ExecutionEngine {
5151 TargetMachine &TM; // The current target we are compiling to
5252 TargetJITInfo &TJI; // The JITInfo for the target we are compiling to
53 MachineCodeEmitter *MCE; // MCE object
53 JITCodeEmitter *JCE; // JCE object
5454
5555 JITState *jitstate;
5656
146146 void addPendingFunction(Function *F);
147147
148148 /// getCodeEmitter - Return the code emitter this JIT is emitting into.
149 MachineCodeEmitter *getCodeEmitter() const { return MCE; }
149 JITCodeEmitter *getCodeEmitter() const { return JCE; }
150150
151151 static ExecutionEngine *createJIT(ModuleProvider *MP, std::string *Err,
152152 JITMemoryManager *JMM,
157157 void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0);
158158
159159 private:
160 static MachineCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM);
160 static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM);
161161 void registerMachineCodeInfo(MachineCodeInfo *MCI);
162162 void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked);
163163 void updateFunctionStub(Function *F);
1515 #include "JITDwarfEmitter.h"
1616 #include "llvm/Function.h"
1717 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/CodeGen/MachineCodeEmitter.h"
18 #include "llvm/CodeGen/JITCodeEmitter.h"
1919 #include "llvm/CodeGen/MachineFunction.h"
2020 #include "llvm/CodeGen/MachineLocation.h"
2121 #include "llvm/CodeGen/MachineModuleInfo.h"
3333
3434
3535 unsigned char* JITDwarfEmitter::EmitDwarfTable(MachineFunction& F,
36 MachineCodeEmitter& mce,
36 JITCodeEmitter& jce,
3737 unsigned char* StartFunction,
3838 unsigned char* EndFunction) {
3939 const TargetMachine& TM = F.getTarget();
4141 needsIndirectEncoding = TM.getTargetAsmInfo()->getNeedsIndirectEncoding();
4242 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
4343 RI = TM.getRegisterInfo();
44 MCE = &mce;
44 JCE = &jce;
4545
4646 unsigned char* ExceptionTable = EmitExceptionTable(&F, StartFunction,
4747 EndFunction);
8080 }
8181
8282 intptr_t LabelPtr = 0;
83 if (LabelID) LabelPtr = MCE->getLabelAddress(LabelID);
83 if (LabelID) LabelPtr = JCE->getLabelAddress(LabelID);
8484
8585 const MachineLocation &Dst = Move.getDestination();
8686 const MachineLocation &Src = Move.getSource();
8787
8888 // Advance row if new location.
8989 if (BaseLabelPtr && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
90 MCE->emitByte(dwarf::DW_CFA_advance_loc4);
91 MCE->emitInt32(LabelPtr - BaseLabelPtr);
90 JCE->emitByte(dwarf::DW_CFA_advance_loc4);
91 JCE->emitInt32(LabelPtr - BaseLabelPtr);
9292
9393 BaseLabelID = LabelID;
9494 BaseLabelPtr = LabelPtr;
9999 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
100100 if (!Src.isReg()) {
101101 if (Src.getReg() == MachineLocation::VirtualFP) {
102 MCE->emitByte(dwarf::DW_CFA_def_cfa_offset);
102 JCE->emitByte(dwarf::DW_CFA_def_cfa_offset);
103103 } else {
104 MCE->emitByte(dwarf::DW_CFA_def_cfa);
105 MCE->emitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), true));
104 JCE->emitByte(dwarf::DW_CFA_def_cfa);
105 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), true));
106106 }
107107
108108 int Offset = -Src.getOffset();
109109
110 MCE->emitULEB128Bytes(Offset);
110 JCE->emitULEB128Bytes(Offset);
111111 } else {
112112 assert(0 && "Machine move no supported yet.");
113113 }
114114 } else if (Src.isReg() &&
115115 Src.getReg() == MachineLocation::VirtualFP) {
116116 if (Dst.isReg()) {
117 MCE->emitByte(dwarf::DW_CFA_def_cfa_register);
118 MCE->emitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), true));
117 JCE->emitByte(dwarf::DW_CFA_def_cfa_register);
118 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), true));
119119 } else {
120120 assert(0 && "Machine move no supported yet.");
121121 }
124124 int Offset = Dst.getOffset() / stackGrowth;
125125
126126 if (Offset < 0) {
127 MCE->emitByte(dwarf::DW_CFA_offset_extended_sf);
128 MCE->emitULEB128Bytes(Reg);
129 MCE->emitSLEB128Bytes(Offset);
127 JCE->emitByte(dwarf::DW_CFA_offset_extended_sf);
128 JCE->emitULEB128Bytes(Reg);
129 JCE->emitSLEB128Bytes(Offset);
130130 } else if (Reg < 64) {
131 MCE->emitByte(dwarf::DW_CFA_offset + Reg);
132 MCE->emitULEB128Bytes(Offset);
131 JCE->emitByte(dwarf::DW_CFA_offset + Reg);
132 JCE->emitULEB128Bytes(Offset);
133133 } else {
134 MCE->emitByte(dwarf::DW_CFA_offset_extended);
135 MCE->emitULEB128Bytes(Reg);
136 MCE->emitULEB128Bytes(Offset);
134 JCE->emitByte(dwarf::DW_CFA_offset_extended);
135 JCE->emitULEB128Bytes(Reg);
136 JCE->emitULEB128Bytes(Offset);
137137 }
138138 }
139139 }
402402 unsigned SizeAlign = (4 - TotalSize) & 3;
403403
404404 // Begin the exception table.
405 MCE->emitAlignment(4);
405 JCE->emitAlignment(4);
406406 for (unsigned i = 0; i != SizeAlign; ++i) {
407 MCE->emitByte(0);
407 JCE->emitByte(0);
408408 // Asm->EOL("Padding");
409409 }
410410
411 unsigned char* DwarfExceptionTable = (unsigned char*)MCE->getCurrentPCValue();
411 unsigned char* DwarfExceptionTable = (unsigned char*)JCE->getCurrentPCValue();
412412
413413 // Emit the header.
414 MCE->emitByte(dwarf::DW_EH_PE_omit);
414 JCE->emitByte(dwarf::DW_EH_PE_omit);
415415 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
416 MCE->emitByte(dwarf::DW_EH_PE_absptr);
416 JCE->emitByte(dwarf::DW_EH_PE_absptr);
417417 // Asm->EOL("TType format (DW_EH_PE_absptr)");
418 MCE->emitULEB128Bytes(TypeOffset);
418 JCE->emitULEB128Bytes(TypeOffset);
419419 // Asm->EOL("TType base offset");
420 MCE->emitByte(dwarf::DW_EH_PE_udata4);
420 JCE->emitByte(dwarf::DW_EH_PE_udata4);
421421 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
422 MCE->emitULEB128Bytes(SizeSites);
422 JCE->emitULEB128Bytes(SizeSites);
423423 // Asm->EOL("Call-site table length");
424424
425425 // Emit the landing pad site information.
430430
431431 if (!S.BeginLabel) {
432432 BeginLabelPtr = (intptr_t)StartFunction;
433 MCE->emitInt32(0);
433 JCE->emitInt32(0);
434434 } else {
435 BeginLabelPtr = MCE->getLabelAddress(S.BeginLabel);
436 MCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction);
435 BeginLabelPtr = JCE->getLabelAddress(S.BeginLabel);
436 JCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction);
437437 }
438438
439439 // Asm->EOL("Region start");
440440
441441 if (!S.EndLabel) {
442442 EndLabelPtr = (intptr_t)EndFunction;
443 MCE->emitInt32((intptr_t)EndFunction - BeginLabelPtr);
443 JCE->emitInt32((intptr_t)EndFunction - BeginLabelPtr);
444444 } else {
445 EndLabelPtr = MCE->getLabelAddress(S.EndLabel);
446 MCE->emitInt32(EndLabelPtr - BeginLabelPtr);
445 EndLabelPtr = JCE->getLabelAddress(S.EndLabel);
446 JCE->emitInt32(EndLabelPtr - BeginLabelPtr);
447447 }
448448 //Asm->EOL("Region length");
449449
450450 if (!S.PadLabel) {
451 MCE->emitInt32(0);
451 JCE->emitInt32(0);
452452 } else {
453 unsigned PadLabelPtr = MCE->getLabelAddress(S.PadLabel);
454 MCE->emitInt32(PadLabelPtr - (intptr_t)StartFunction);
453 unsigned PadLabelPtr = JCE->getLabelAddress(S.PadLabel);
454 JCE->emitInt32(PadLabelPtr - (intptr_t)StartFunction);
455455 }
456456 // Asm->EOL("Landing pad");
457457
458 MCE->emitULEB128Bytes(S.Action);
458 JCE->emitULEB128Bytes(S.Action);
459459 // Asm->EOL("Action");
460460 }
461461
463463 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
464464 ActionEntry &Action = Actions[I];
465465
466 MCE->emitSLEB128Bytes(Action.ValueForTypeID);
466 JCE->emitSLEB128Bytes(Action.ValueForTypeID);
467467 //Asm->EOL("TypeInfo index");
468 MCE->emitSLEB128Bytes(Action.NextAction);
468 JCE->emitSLEB128Bytes(Action.NextAction);
469469 //Asm->EOL("Next action");
470470 }
471471
475475
476476 if (GV) {
477477 if (TD->getPointerSize() == sizeof(int32_t)) {
478 MCE->emitInt32((intptr_t)Jit.getOrEmitGlobalVariable(GV));
478 JCE->emitInt32((intptr_t)Jit.getOrEmitGlobalVariable(GV));
479479 } else {
480 MCE->emitInt64((intptr_t)Jit.getOrEmitGlobalVariable(GV));
480 JCE->emitInt64((intptr_t)Jit.getOrEmitGlobalVariable(GV));
481481 }
482482 } else {
483483 if (TD->getPointerSize() == sizeof(int32_t))
484 MCE->emitInt32(0);
484 JCE->emitInt32(0);
485485 else
486 MCE->emitInt64(0);
486 JCE->emitInt64(0);
487487 }
488488 // Asm->EOL("TypeInfo");
489489 }
491491 // Emit the filter typeids.
492492 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
493493 unsigned TypeID = FilterIds[j];
494 MCE->emitULEB128Bytes(TypeID);
494 JCE->emitULEB128Bytes(TypeID);
495495 //Asm->EOL("Filter TypeInfo index");
496496 }
497497
498 MCE->emitAlignment(4);
498 JCE->emitAlignment(4);
499499
500500 return DwarfExceptionTable;
501501 }
506506 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
507507 PointerSize : -PointerSize;
508508
509 unsigned char* StartCommonPtr = (unsigned char*)MCE->getCurrentPCValue();
509 unsigned char* StartCommonPtr = (unsigned char*)JCE->getCurrentPCValue();
510510 // EH Common Frame header
511 MCE->allocateSpace(4, 0);
512 unsigned char* FrameCommonBeginPtr = (unsigned char*)MCE->getCurrentPCValue();
513 MCE->emitInt32((int)0);
514 MCE->emitByte(dwarf::DW_CIE_VERSION);
515 MCE->emitString(Personality ? "zPLR" : "zR");
516 MCE->emitULEB128Bytes(1);
517 MCE->emitSLEB128Bytes(stackGrowth);
518 MCE->emitByte(RI->getDwarfRegNum(RI->getRARegister(), true));
511 JCE->allocateSpace(4, 0);
512 unsigned char* FrameCommonBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
513 JCE->emitInt32((int)0);
514 JCE->emitByte(dwarf::DW_CIE_VERSION);
515 JCE->emitString(Personality ? "zPLR" : "zR");
516 JCE->emitULEB128Bytes(1);
517 JCE->emitSLEB128Bytes(stackGrowth);
518 JCE->emitByte(RI->getDwarfRegNum(RI->getRARegister(), true));
519519
520520 if (Personality) {
521521 // Augmentation Size: 3 small ULEBs of one byte each, and the personality
522522 // function which size is PointerSize.
523 MCE->emitULEB128Bytes(3 + PointerSize);
523 JCE->emitULEB128Bytes(3 + PointerSize);
524524
525525 // We set the encoding of the personality as direct encoding because we use
526526 // the function pointer. The encoding is not relative because the current
527527 // PC value may be bigger than the personality function pointer.
528528 if (PointerSize == 4) {
529 MCE->emitByte(dwarf::DW_EH_PE_sdata4);
530 MCE->emitInt32(((intptr_t)Jit.getPointerToGlobal(Personality)));
529 JCE->emitByte(dwarf::DW_EH_PE_sdata4);
530 JCE->emitInt32(((intptr_t)Jit.getPointerToGlobal(Personality)));
531531 } else {
532 MCE->emitByte(dwarf::DW_EH_PE_sdata8);
533 MCE->emitInt64(((intptr_t)Jit.getPointerToGlobal(Personality)));
534 }
535
536 MCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
537 MCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
532 JCE->emitByte(dwarf::DW_EH_PE_sdata8);
533 JCE->emitInt64(((intptr_t)Jit.getPointerToGlobal(Personality)));
534 }
535
536 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
537 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
538538
539539 } else {
540 MCE->emitULEB128Bytes(1);
541 MCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
540 JCE->emitULEB128Bytes(1);
541 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
542542 }
543543
544544 std::vector Moves;
545545 RI->getInitialFrameState(Moves);
546546 EmitFrameMoves(0, Moves);
547 MCE->emitAlignment(PointerSize);
548
549 MCE->emitInt32At((uintptr_t*)StartCommonPtr,
550 (uintptr_t)((unsigned char*)MCE->getCurrentPCValue() -
547 JCE->emitAlignment(PointerSize);
548
549 JCE->emitInt32At((uintptr_t*)StartCommonPtr,
550 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
551551 FrameCommonBeginPtr));
552552
553553 return StartCommonPtr;
563563 unsigned PointerSize = TD->getPointerSize();
564564
565565 // EH frame header.
566 unsigned char* StartEHPtr = (unsigned char*)MCE->getCurrentPCValue();
567 MCE->allocateSpace(4, 0);
568 unsigned char* FrameBeginPtr = (unsigned char*)MCE->getCurrentPCValue();
566 unsigned char* StartEHPtr = (unsigned char*)JCE->getCurrentPCValue();
567 JCE->allocateSpace(4, 0);
568 unsigned char* FrameBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
569569 // FDE CIE Offset
570 MCE->emitInt32(FrameBeginPtr - StartCommonPtr);
571 MCE->emitInt32(StartFunction - (unsigned char*)MCE->getCurrentPCValue());
572 MCE->emitInt32(EndFunction - StartFunction);
570 JCE->emitInt32(FrameBeginPtr - StartCommonPtr);
571 JCE->emitInt32(StartFunction - (unsigned char*)JCE->getCurrentPCValue());
572 JCE->emitInt32(EndFunction - StartFunction);
573573
574574 // If there is a personality and landing pads then point to the language
575575 // specific data area in the exception table.
576576 if (MMI->getPersonalityIndex()) {
577 MCE->emitULEB128Bytes(4);
577 JCE->emitULEB128Bytes(4);
578578
579579 if (!MMI->getLandingPads().empty()) {
580 MCE->emitInt32(ExceptionTable - (unsigned char*)MCE->getCurrentPCValue());
580 JCE->emitInt32(ExceptionTable - (unsigned char*)JCE->getCurrentPCValue());
581581 } else {
582 MCE->emitInt32((int)0);
582 JCE->emitInt32((int)0);
583583 }
584584 } else {
585 MCE->emitULEB128Bytes(0);
585 JCE->emitULEB128Bytes(0);
586586 }
587587
588588 // Indicate locations of function specific callee saved registers in
589589 // frame.
590590 EmitFrameMoves((intptr_t)StartFunction, MMI->getFrameMoves());
591591
592 MCE->emitAlignment(PointerSize);
592 JCE->emitAlignment(PointerSize);
593593
594594 // Indicate the size of the table
595 MCE->emitInt32At((uintptr_t*)StartEHPtr,
596 (uintptr_t)((unsigned char*)MCE->getCurrentPCValue() -
595 JCE->emitInt32At((uintptr_t*)StartEHPtr,
596 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
597597 StartEHPtr));
598598
599599 // Double zeroes for the unwind runtime
600600 if (PointerSize == 8) {
601 MCE->emitInt64(0);
602 MCE->emitInt64(0);
601 JCE->emitInt64(0);
602 JCE->emitInt64(0);
603603 } else {
604 MCE->emitInt32(0);
605 MCE->emitInt32(0);
604 JCE->emitInt32(0);
605 JCE->emitInt32(0);
606606 }
607607
608608
610610 }
611611
612612 unsigned JITDwarfEmitter::GetDwarfTableSizeInBytes(MachineFunction& F,
613 MachineCodeEmitter& mce,
613 JITCodeEmitter& jce,
614614 unsigned char* StartFunction,
615615 unsigned char* EndFunction) {
616616 const TargetMachine& TM = F.getTarget();
618618 needsIndirectEncoding = TM.getTargetAsmInfo()->getNeedsIndirectEncoding();
619619 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
620620 RI = TM.getRegisterInfo();
621 MCE = &mce;
621 JCE = &jce;
622622 unsigned FinalSize = 0;
623623
624624 FinalSize += GetExceptionTableSizeInBytes(&F);
732732 }
733733
734734 intptr_t LabelPtr = 0;
735 if (LabelID) LabelPtr = MCE->getLabelAddress(LabelID);
735 if (LabelID) LabelPtr = JCE->getLabelAddress(LabelID);
736736
737737 const MachineLocation &Dst = Move.getDestination();
738738 const MachineLocation &Src = Move.getSource();
1717 namespace llvm {
1818
1919 class Function;
20 class MachineCodeEmitter;
20 class JITCodeEmitter;
2121 class MachineFunction;
2222 class MachineModuleInfo;
2323 class MachineMove;
2727
2828 class JITDwarfEmitter {
2929 const TargetData* TD;
30 MachineCodeEmitter* MCE;
30 JITCodeEmitter* JCE;
3131 const TargetRegisterInfo* RI;
3232 MachineModuleInfo* MMI;
3333 JIT& Jit;
6565 JITDwarfEmitter(JIT& jit);
6666
6767 unsigned char* EmitDwarfTable(MachineFunction& F,
68 MachineCodeEmitter& MCE,
68 JITCodeEmitter& JCE,
6969 unsigned char* StartFunction,
7070 unsigned char* EndFunction);
7171
7272
7373 unsigned GetDwarfTableSizeInBytes(MachineFunction& F,
74 MachineCodeEmitter& MCE,
74 JITCodeEmitter& JCE,
7575 unsigned char* StartFunction,
7676 unsigned char* EndFunction);
7777
1717 #include "llvm/Constants.h"
1818 #include "llvm/Module.h"
1919 #include "llvm/DerivedTypes.h"
20 #include "llvm/CodeGen/MachineCodeEmitter.h"
20 #include "llvm/CodeGen/JITCodeEmitter.h"
2121 #include "llvm/CodeGen/MachineFunction.h"
2222 #include "llvm/CodeGen/MachineConstantPool.h"
2323 #include "llvm/CodeGen/MachineJumpTableInfo.h"
545545 namespace {
546546 /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is
547547 /// used to output functions to memory for execution.
548 class JITEmitter : public MachineCodeEmitter {
548 class JITEmitter : public JITCodeEmitter {
549549 JITMemoryManager *MemMgr;
550550
551551 // When outputting a function stub in the context of some other function, we
12881288
12891289 void* JITEmitter::allocateSpace(uintptr_t Size, unsigned Alignment) {
12901290 if (BufferBegin)
1291 return MachineCodeEmitter::allocateSpace(Size, Alignment);
1291 return JITCodeEmitter::allocateSpace(Size, Alignment);
12921292
12931293 // create a new memory block if there is no active one.
12941294 // care must be taken so that BufferBegin is invalidated when a
14591459 // Public interface to this file
14601460 //===----------------------------------------------------------------------===//
14611461
1462 MachineCodeEmitter *JIT::createEmitter(JIT &jit, JITMemoryManager *JMM) {
1462 JITCodeEmitter *JIT::createEmitter(JIT &jit, JITMemoryManager *JMM) {
14631463 return new JITEmitter(jit, JMM);
14641464 }
14651465
14861486 return Addr;
14871487
14881488 // Get a stub if the target supports it.
1489 assert(isa(MCE) && "Unexpected MCE?");
1489 assert(isa(JCE) && "Unexpected MCE?");
14901490 JITEmitter *JE = cast(getCodeEmitter());
14911491 return JE->getJITResolver().getFunctionStub(F);
14921492 }
14931493
14941494 void JIT::registerMachineCodeInfo(MachineCodeInfo *mc) {
1495 assert(isa(MCE) && "Unexpected MCE?");
1495 assert(isa(JCE) && "Unexpected MCE?");
14961496 JITEmitter *JE = cast(getCodeEmitter());
14971497
14981498 JE->setMachineCodeInfo(mc);
15001500
15011501 void JIT::updateFunctionStub(Function *F) {
15021502 // Get the empty stub we generated earlier.
1503 assert(isa(MCE) && "Unexpected MCE?");
1503 assert(isa(JCE) && "Unexpected MCE?");
15041504 JITEmitter *JE = cast(getCodeEmitter());
15051505 void *Stub = JE->getJITResolver().getFunctionStub(F);
15061506
15141514 /// that were emitted during code generation.
15151515 ///
15161516 void JIT::updateDlsymStubTable() {
1517 assert(isa(MCE) && "Unexpected MCE?");
1517 assert(isa(JCE) && "Unexpected MCE?");
15181518 JITEmitter *JE = cast(getCodeEmitter());
15191519
15201520 SmallVector GVs;
15521552 JE->startGVStub(0, offset, 4);
15531553
15541554 // Emit the number of records
1555 MCE->emitInt32(nStubs);
1555 JE->emitInt32(nStubs);
15561556
15571557 // Emit the string offsets
15581558 for (unsigned i = 0; i != nStubs; ++i)
1559 MCE->emitInt32(Offsets[i]);
1559 JE->emitInt32(Offsets[i]);
15601560
15611561 // Emit the pointers. Verify that they are at least 2-byte aligned, and set
15621562 // the low bit to 0 == GV, 1 == Function, so that the client code doing the
15701570 Ptr |= (intptr_t)1;
15711571
15721572 if (sizeof(Ptr) == 8)
1573 MCE->emitInt64(Ptr);
1573 JE->emitInt64(Ptr);
15741574 else
1575 MCE->emitInt32(Ptr);
1575 JE->emitInt32(Ptr);
15761576 }
15771577 for (StringMapConstIterator i = ExtFns.begin(), e = ExtFns.end();
15781578 i != e; ++i) {
15791579 intptr_t Ptr = (intptr_t)i->second | 1;
15801580
15811581 if (sizeof(Ptr) == 8)
1582 MCE->emitInt64(Ptr);
1582 JE->emitInt64(Ptr);
15831583 else
1584 MCE->emitInt32(Ptr);
1584 JE->emitInt32(Ptr);
15851585 }
15861586
15871587 // Emit the strings.
15881588 for (unsigned i = 0; i != GVs.size(); ++i)
1589 MCE->emitString(GVs[i]->getName());
1589 JE->emitString(GVs[i]->getName());
15901590 for (StringMapConstIterator i = ExtFns.begin(), e = ExtFns.end();
15911591 i != e; ++i)
1592 MCE->emitString(i->first());
1592 JE->emitString(i->first());
15931593
15941594 // Tell the JIT memory manager where it is. The JIT Memory Manager will
15951595 // deallocate space for the old one, if one existed.
16081608 RemoveFunctionFromSymbolTable(OldPtr);
16091609
16101610 // Free the actual memory for the function body and related stuff.
1611 assert(isa(MCE) && "Unexpected MCE?");
1612 cast(MCE)->deallocateMemForFunction(F);
1613 }
1614
1611 assert(isa(JCE) && "Unexpected MCE?");
1612 cast(JCE)->deallocateMemForFunction(F);
1613 }
1614
2222 class ARMTargetMachine;
2323 class FunctionPass;
2424 class MachineCodeEmitter;
25 class JITCodeEmitter;
2526 class raw_ostream;
2627
2728 // Enums corresponding to ARM condition codes
9596 bool Verbose);
9697 FunctionPass *createARMCodeEmitterPass(ARMTargetMachine &TM,
9798 MachineCodeEmitter &MCE);
99
100 FunctionPass *createARMCodeEmitterPass(
101 ARMTargetMachine &TM, MachineCodeEmitter &MCE);
102 /*
103 template< class machineCodeEmitter>
104 FunctionPass *createARMCodeEmitterPass(
105 ARMTargetMachine &TM, machineCodeEmitter &MCE);
106 */
107 FunctionPass *createARMJITCodeEmitterPass(
108 ARMTargetMachine &TM, JITCodeEmitter &JCE);
109
98110 FunctionPass *createARMLoadStoreOptimizationPass();
99111 FunctionPass *createARMConstantIslandPass();
100112
2424 #include "llvm/Function.h"
2525 #include "llvm/PassManager.h"
2626 #include "llvm/CodeGen/MachineCodeEmitter.h"
27 #include "llvm/CodeGen/JITCodeEmitter.h"
2728 #include "llvm/CodeGen/MachineConstantPool.h"
2829 #include "llvm/CodeGen/MachineFunctionPass.h"
2930 #include "llvm/CodeGen/MachineInstr.h"
4041 STATISTIC(NumEmitted, "Number of machine instructions emitted");
4142
4243 namespace {
43 class VISIBILITY_HIDDEN ARMCodeEmitter : public MachineFunctionPass {
44
45 class ARMCodeEmitter {
46 public:
47
48 /// getBinaryCodeForInstr - This function, generated by the
49 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
50 /// machine instructions.
51
52 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
53 };
54
55 template< class machineCodeEmitter>
56 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass,
57 public ARMCodeEmitter
58 {
4459 ARMJITInfo *JTI;
4560 const ARMInstrInfo *II;
4661 const TargetData *TD;
4762 TargetMachine &TM;
48 MachineCodeEmitter &MCE;
63 machineCodeEmitter &MCE;
4964 const std::vector *MCPEs;
5065 const std::vector *MJTEs;
5166 bool IsPIC;
5267
5368 public:
5469 static char ID;
55 explicit ARMCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce)
70 explicit Emitter(TargetMachine &tm, machineCodeEmitter &mce)
5671 : MachineFunctionPass(&ID), JTI(0), II(0), TD(0), TM(tm),
5772 MCE(mce), MCPEs(0), MJTEs(0),
5873 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
59 ARMCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce,
74 Emitter(TargetMachine &tm, machineCodeEmitter &mce,
6075 const ARMInstrInfo &ii, const TargetData &td)
6176 : MachineFunctionPass(&ID), JTI(0), II(&ii), TD(&td), TM(tm),
6277 MCE(mce), MCPEs(0), MJTEs(0),
132147 void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI);
133148
134149 void emitMiscInstruction(const MachineInstr &MI);
135
136 /// getBinaryCodeForInstr - This function, generated by the
137 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
138 /// machine instructions.
139 ///
140 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
141150
142151 /// getMachineOpValue - Return binary encoding of operand. If the machine
143152 /// operand requires relocation, record the relocation and return zero.
160169 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc,
161170 intptr_t JTBase = 0);
162171 };
163 char ARMCodeEmitter::ID = 0;
172 template
173 char Emitter::ID = 0;
164174 }
165175
166176 /// createARMCodeEmitterPass - Return a pass that emits the collected ARM code
167177 /// to the specified MCE object.
168 FunctionPass *llvm::createARMCodeEmitterPass(ARMTargetMachine &TM,
169 MachineCodeEmitter &MCE) {
170 return new ARMCodeEmitter(TM, MCE);
171 }
172
173 bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
178
179 namespace llvm {
180
181 FunctionPass *createARMCodeEmitterPass(
182 ARMTargetMachine &TM, MachineCodeEmitter &MCE)
183 {
184 return new Emitter(TM, MCE);
185 }
186 FunctionPass *createARMJITCodeEmitterPass(
187 ARMTargetMachine &TM, JITCodeEmitter &JCE)
188 {
189 return new Emitter(TM, JCE);
190 }
191
192 } // end namespace llvm
193
194 template< class machineCodeEmitter>
195 bool Emitter< machineCodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
174196 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
175197 MF.getTarget().getRelocationModel() != Reloc::Static) &&
176198 "JIT relocation model must be set to static or default!");
199221
200222 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
201223 ///
202 unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const {
224 template< class machineCodeEmitter>
225 unsigned Emitter< machineCodeEmitter>::getShiftOp(unsigned Imm) const {
203226 switch (ARM_AM::getAM2ShiftOpc(Imm)) {
204227 default: assert(0 && "Unknown shift opc!");
205228 case ARM_AM::asr: return 2;
213236
214237 /// getMachineOpValue - Return binary encoding of operand. If the machine
215238 /// operand requires relocation, record the relocation and return zero.
216 unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI,
239 template< class machineCodeEmitter>
240 unsigned Emitter< machineCodeEmitter>::getMachineOpValue(const MachineInstr &MI,
217241 const MachineOperand &MO) {
218242 if (MO.isReg())
219243 return ARMRegisterInfo::getRegisterNumbering(MO.getReg());
242266
243267 /// emitGlobalAddress - Emit the specified address to the code stream.
244268 ///
245 void ARMCodeEmitter::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
269 template< class machineCodeEmitter>
270 void Emitter< machineCodeEmitter>::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
246271 bool NeedStub, intptr_t ACPV) {
247272 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
248273 Reloc, GV, ACPV, NeedStub));
251276 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
252277 /// be emitted to the current location in the function, and allow it to be PC
253278 /// relative.
254 void ARMCodeEmitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
279 template< class machineCodeEmitter>
280 void Emitter< machineCodeEmitter>::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
255281 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
256282 Reloc, ES));
257283 }
259285 /// emitConstPoolAddress - Arrange for the address of an constant pool
260286 /// to be emitted to the current location in the function, and allow it to be PC
261287 /// relative.
262 void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) {
288 template< class machineCodeEmitter>
289 void Emitter< machineCodeEmitter>::emitConstPoolAddress(unsigned CPI, unsigned Reloc) {
263290 // Tell JIT emitter we'll resolve the address.
264291 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
265292 Reloc, CPI, 0, true));
268295 /// emitJumpTableAddress - Arrange for the address of a jump table to
269296 /// be emitted to the current location in the function, and allow it to be PC
270297 /// relative.
271 void ARMCodeEmitter::emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) {
298 template< class machineCodeEmitter>
299 void Emitter< machineCodeEmitter>::emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) {
272300 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
273301 Reloc, JTIndex, 0, true));
274302 }
275303
276304 /// emitMachineBasicBlock - Emit the specified address basic block.
277 void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
305 template< class machineCodeEmitter>
306 void Emitter< machineCodeEmitter>::emitMachineBasicBlock(MachineBasicBlock *BB,
278307 unsigned Reloc, intptr_t JTBase) {
279308 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
280309 Reloc, BB, JTBase));
281310 }
282311
283 void ARMCodeEmitter::emitWordLE(unsigned Binary) {
312 template< class machineCodeEmitter>
313 void Emitter< machineCodeEmitter>::emitWordLE(unsigned Binary) {
284314 #ifndef NDEBUG
285315 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0')
286316 << Binary << std::dec << "\n";
288318 MCE.emitWordLE(Binary);
289319 }
290320
291 void ARMCodeEmitter::emitDWordLE(uint64_t Binary) {
321 template< class machineCodeEmitter>
322 void Emitter< machineCodeEmitter>::emitDWordLE(uint64_t Binary) {
292323 #ifndef NDEBUG
293324 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0')
294325 << (unsigned)Binary << std::dec << "\n";
298329 MCE.emitDWordLE(Binary);
299330 }
300331
301 void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) {
332 template< class machineCodeEmitter>
333 void Emitter< machineCodeEmitter>::emitInstruction(const MachineInstr &MI) {
302334 DOUT << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI;
303335
304336 NumEmitted++; // Keep track of the # of mi's emitted
364396 }
365397 }
366398
367 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) {
399 template< class machineCodeEmitter>
400 void Emitter< machineCodeEmitter>::emitConstPoolInstruction(const MachineInstr &MI) {
368401 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index.
369402 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index.
370403 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex];
431464 }
432465 }
433466
434 void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) {
467 template< class machineCodeEmitter>
468 void Emitter< machineCodeEmitter>::emitMOVi2piecesInstruction(const MachineInstr &MI) {
435469 const MachineOperand &MO0 = MI.getOperand(0);
436470 const MachineOperand &MO1 = MI.getOperand(1);
437471 assert(MO1.isImm() && "Not a valid so_imm value!");
472506 emitWordLE(Binary);
473507 }
474508
475 void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) {
509 template< class machineCodeEmitter>
510 void Emitter< machineCodeEmitter>::emitLEApcrelJTInstruction(const MachineInstr &MI) {
476511 // It's basically add r, pc, (LJTI - $+8)
477512
478513 const TargetInstrDesc &TID = MI.getDesc();
500535 emitWordLE(Binary);
501536 }
502537
503 void ARMCodeEmitter::emitPseudoMoveInstruction(const MachineInstr &MI) {
538 template< class machineCodeEmitter>
539 void Emitter< machineCodeEmitter>::emitPseudoMoveInstruction(const MachineInstr &MI) {
504540 unsigned Opcode = MI.getDesc().Opcode;
505541
506542 // Part of binary is determined by TableGn.
539575 emitWordLE(Binary);
540576 }
541577
542 void ARMCodeEmitter::addPCLabel(unsigned LabelID) {
578 template< class machineCodeEmitter>
579 void Emitter< machineCodeEmitter>::addPCLabel(unsigned LabelID) {
543580 DOUT << " ** LPC" << LabelID << " @ "
544581 << (void*)MCE.getCurrentPCValue() << '\n';
545582 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue());
546583 }
547584
548 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) {
585 template< class machineCodeEmitter>
586 void Emitter< machineCodeEmitter>::emitPseudoInstruction(const MachineInstr &MI) {
549587 unsigned Opcode = MI.getDesc().Opcode;
550588 switch (Opcode) {
551589 default:
614652 }
615653 }
616654
617
618 unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI,
655 template< class machineCodeEmitter>
656 unsigned Emitter< machineCodeEmitter>::getMachineSoRegOpValue(const MachineInstr &MI,
619657 const TargetInstrDesc &TID,
620658 const MachineOperand &MO,
621659 unsigned OpIdx) {
673711 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
674712 }
675713
676 unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) {
714 template< class machineCodeEmitter>
715 unsigned Emitter< machineCodeEmitter>::getMachineSoImmOpValue(unsigned SoImm) {
677716 // Encode rotate_imm.
678717 unsigned Binary = (ARM_AM::getSOImmValRot(SoImm) >> 1)
679718 << ARMII::SoRotImmShift;
683722 return Binary;
684723 }
685724
686 unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI,
725 template< class machineCodeEmitter>
726 unsigned Emitter< machineCodeEmitter>::getAddrModeSBit(const MachineInstr &MI,
687727 const TargetInstrDesc &TID) const {
688728 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){
689729 const MachineOperand &MO = MI.getOperand(i-1);
693733 return 0;
694734 }
695735
696 void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI,
736 template< class machineCodeEmitter>
737 void Emitter< machineCodeEmitter>::emitDataProcessingInstruction(const MachineInstr &MI,
697738 unsigned ImplicitRd,
698739 unsigned ImplicitRn) {
699740 const TargetInstrDesc &TID = MI.getDesc();
756797 emitWordLE(Binary);
757798 }
758799
759 void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI,
800 template< class machineCodeEmitter>
801 void Emitter< machineCodeEmitter>::emitLoadStoreInstruction(const MachineInstr &MI,
760802 unsigned ImplicitRd,
761803 unsigned ImplicitRn) {
762804 const TargetInstrDesc &TID = MI.getDesc();
830872 emitWordLE(Binary);
831873 }
832874
833 void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI,
875 template< class machineCodeEmitter>
876 void Emitter< machineCodeEmitter>::emitMiscLoadStoreInstruction(const MachineInstr &MI,
834877 unsigned ImplicitRn) {
835878 const TargetInstrDesc &TID = MI.getDesc();
836879 unsigned Form = TID.TSFlags & ARMII::FormMask;
913956 return Binary;
914957 }
915958
916 void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
959 template< class machineCodeEmitter>
960 void Emitter< machineCodeEmitter>::emitLoadStoreMultipleInstruction(const MachineInstr &MI) {
917961 // Part of binary is determined by TableGn.
918962 unsigned Binary = getBinaryCodeForInstr(MI);
919963
945989 emitWordLE(Binary);
946990 }
947991
948 void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) {
992 template< class machineCodeEmitter>
993 void Emitter< machineCodeEmitter>::emitMulFrmInstruction(const MachineInstr &MI) {
949994 const TargetInstrDesc &TID = MI.getDesc();
950995
951996 // Part of binary is determined by TableGn.
9821027 emitWordLE(Binary);
9831028 }
9841029
985 void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) {
1030 template< class machineCodeEmitter>
1031 void Emitter< machineCodeEmitter>::emitExtendInstruction(const MachineInstr &MI) {
9861032 const TargetInstrDesc &TID = MI.getDesc();
9871033
9881034 // Part of binary is determined by TableGn.
10191065 emitWordLE(Binary);
10201066 }
10211067
1022 void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) {
1068 template< class machineCodeEmitter>
1069 void Emitter< machineCodeEmitter>::emitMiscArithInstruction(const MachineInstr &MI) {
10231070 const TargetInstrDesc &TID = MI.getDesc();
10241071
10251072 // Part of binary is determined by TableGn.
10571104 emitWordLE(Binary);
10581105 }
10591106
1060 void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) {
1107 template< class machineCodeEmitter>
1108 void Emitter< machineCodeEmitter>::emitBranchInstruction(const MachineInstr &MI) {
10611109 const TargetInstrDesc &TID = MI.getDesc();
10621110
10631111 if (TID.Opcode == ARM::TPsoft)
10751123 emitWordLE(Binary);
10761124 }
10771125
1078 void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) {
1126 template< class machineCodeEmitter>
1127 void Emitter< machineCodeEmitter>::emitInlineJumpTable(unsigned JTIndex) {
10791128 // Remember the base address of the inline jump table.
10801129 uintptr_t JTBase = MCE.getCurrentPCValue();
10811130 JTI->addJumpTableBaseAddr(JTIndex, JTBase);
10941143 }
10951144 }
10961145
1097 void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) {
1146 template< class machineCodeEmitter>
1147 void Emitter< machineCodeEmitter>::emitMiscBranchInstruction(const MachineInstr &MI) {
10981148 const TargetInstrDesc &TID = MI.getDesc();
10991149
11001150 // Handle jump tables.
11741224 return Binary;
11751225 }
11761226
1177 void ARMCodeEmitter::emitVFPArithInstruction(const MachineInstr &MI) {
1227 template< class machineCodeEmitter>
1228 void Emitter< machineCodeEmitter>::emitVFPArithInstruction(const MachineInstr &MI) {
11781229 const TargetInstrDesc &TID = MI.getDesc();
11791230
11801231 // Part of binary is determined by TableGn.
12131264 emitWordLE(Binary);
12141265 }
12151266
1216 void ARMCodeEmitter::emitVFPConversionInstruction(const MachineInstr &MI) {
1267 template< class machineCodeEmitter>
1268 void Emitter< machineCodeEmitter>::emitVFPConversionInstruction(const MachineInstr &MI) {
12171269 const TargetInstrDesc &TID = MI.getDesc();
12181270 unsigned Form = TID.TSFlags & ARMII::FormMask;
12191271
12691321 emitWordLE(Binary);
12701322 }
12711323
1272 void ARMCodeEmitter::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
1324 template< class machineCodeEmitter>
1325 void Emitter< machineCodeEmitter>::emitVFPLoadStoreInstruction(const MachineInstr &MI) {
12731326 // Part of binary is determined by TableGn.
12741327 unsigned Binary = getBinaryCodeForInstr(MI);
12751328
13031356 emitWordLE(Binary);
13041357 }
13051358
1306 void
1307 ARMCodeEmitter::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) {
1359 template< class machineCodeEmitter>
1360 void Emitter< machineCodeEmitter>::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) {
13081361 // Part of binary is determined by TableGn.
13091362 unsigned Binary = getBinaryCodeForInstr(MI);
13101363
13381391 emitWordLE(Binary);
13391392 }
13401393
1341 void ARMCodeEmitter::emitMiscInstruction(const MachineInstr &MI) {
1394 template< class machineCodeEmitter>
1395 void Emitter< machineCodeEmitter>::emitMiscInstruction(const MachineInstr &MI) {
13421396 // Part of binary is determined by TableGn.
13431397 unsigned Binary = getBinaryCodeForInstr(MI);
13441398
13491403 }
13501404
13511405 #include "ARMGenCodeEmitter.inc"
1406
1717 #include "ARMRelocations.h"
1818 #include "ARMSubtarget.h"
1919 #include "llvm/Function.h"
20 #include "llvm/CodeGen/MachineCodeEmitter.h"
20 #include "llvm/CodeGen/JITCodeEmitter.h"
2121 #include "llvm/Config/alloca.h"
2222 #include "llvm/Support/Debug.h"
2323 #include "llvm/Support/Streams.h"
140140 }
141141
142142 void *ARMJITInfo::emitGlobalValueIndirectSym(const GlobalValue *GV, void *Ptr,
143 MachineCodeEmitter &MCE) {
144 MCE.startGVStub(GV, 4, 4);
145 MCE.emitWordLE((intptr_t)Ptr);
146 void *PtrAddr = MCE.finishGVStub(GV);
143 JITCodeEmitter &JCE) {
144 JCE.startGVStub(GV, 4, 4);
145 JCE.emitWordLE((intptr_t)Ptr);
146 void *PtrAddr = JCE.finishGVStub(GV);
147147 addIndirectSymAddr(Ptr, (intptr_t)PtrAddr);
148148 return PtrAddr;
149149 }
150150
151151 void *ARMJITInfo::emitFunctionStub(const Function* F, void *Fn,
152 MachineCodeEmitter &MCE) {
152 JITCodeEmitter &JCE) {
153153 // If this is just a call to an external function, emit a branch instead of a
154154 // call. The code is the same except for one bit of the last instruction.
155155 if (Fn != (void*)(intptr_t)ARMCompilationCallback) {
159159 intptr_t LazyPtr = getIndirectSymAddr(Fn);
160160 if (!LazyPtr) {
161161 // In PIC mode, the function stub is loading a lazy-ptr.
162 LazyPtr= (intptr_t)emitGlobalValueIndirectSym((GlobalValue*)F, Fn, MCE);
162 LazyPtr= (intptr_t)emitGlobalValueIndirectSym((GlobalValue*)F, Fn, JCE);
163163 if (F)
164164 DOUT << "JIT: Indirect symbol emitted at [" << LazyPtr << "] for GV '"
165165 << F->getName() << "'\n";
167167 DOUT << "JIT: Stub emitted at [" << LazyPtr
168168 << "] for external function at '" << Fn << "'\n";
169169 }
170 MCE.startGVStub(F, 16, 4);
171 intptr_t Addr = (intptr_t)MCE.getCurrentPCValue();
172 MCE.emitWordLE(0xe59fc004); // ldr pc, [pc, #+4]
173 MCE.emitWordLE(0xe08fc00c); // L_func$scv: add ip, pc, ip
174 MCE.emitWordLE(0xe59cf000); // ldr pc, [ip]
175 MCE.emitWordLE(LazyPtr - (Addr+4+8)); // func - (L_func$scv+8)
170 JCE.startGVStub(F, 16, 4);
171 intptr_t Addr = (intptr_t)JCE.getCurrentPCValue();
172 JCE.emitWordLE(0xe59fc004); // ldr pc, [pc, #+4]
173 JCE.emitWordLE(0xe08fc00c); // L_func$scv: add ip, pc, ip
174 JCE.emitWordLE(0xe59cf000); // ldr pc, [ip]
175 JCE.emitWordLE(LazyPtr - (Addr+4+8)); // func - (L_func$scv+8)
176176 sys::Memory::InvalidateInstructionCache((void*)Addr, 16);
177177 } else {
178178 // The stub is 8-byte size and 4-aligned.
179 MCE.startGVStub(F, 8, 4);
180 intptr_t Addr = (intptr_t)MCE.getCurrentPCValue();
181 MCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4]
182 MCE.emitWordLE((intptr_t)Fn); // addr of function
179 JCE.startGVStub(F, 8, 4);
180 intptr_t Addr = (intptr_t)JCE.getCurrentPCValue();
181 JCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4]
182 JCE.emitWordLE((intptr_t)Fn); // addr of function
183183 sys::Memory::InvalidateInstructionCache((void*)Addr, 8);
184184 }
185185 } else {
190190 //
191191 // Branch and link to the compilation callback.
192192 // The stub is 16-byte size and 4-byte aligned.
193 MCE.startGVStub(F, 16, 4);
194 intptr_t Addr = (intptr_t)MCE.getCurrentPCValue();
193 JCE.startGVStub(F, 16, 4);
194 intptr_t Addr = (intptr_t)JCE.getCurrentPCValue();
195195 // Save LR so the callback can determine which stub called it.
196196 // The compilation callback is responsible for popping this prior
197197 // to returning.
198 MCE.emitWordLE(0xe92d4000); // push {lr}
198 JCE.emitWordLE(0xe92d4000); // push {lr}
199199 // Set the return address to go back to the start of this stub.
200 MCE.emitWordLE(0xe24fe00c); // sub lr, pc, #12
200 JCE.emitWordLE(0xe24fe00c); // sub lr, pc, #12
201201 // Invoke the compilation callback.
202 MCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4]
202 JCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4]
203203 // The address of the compilation callback.
204 MCE.emitWordLE((intptr_t)ARMCompilationCallback);
204 JCE.emitWordLE((intptr_t)ARMCompilationCallback);
205205 sys::Memory::InvalidateInstructionCache((void*)Addr, 16);
206206 }
207207
208 return MCE.finishGVStub(F);
208 return JCE.finishGVStub(F);
209209 }
210210
211211 intptr_t ARMJITInfo::resolveRelocDestAddr(MachineRelocation *MR) const {
5454 ///
5555 virtual void replaceMachineCodeForFunction(void *Old, void *New);
5656
57 /// emitGlobalValueIndirectSym - Use the specified MachineCodeEmitter object
57 /// emitGlobalValueIndirectSym - Use the specified JITCodeEmitter object
5858 /// to emit an indirect symbol which contains the address of the specified
5959 /// ptr.
6060 virtual void *emitGlobalValueIndirectSym(const GlobalValue* GV, void *ptr,
61 MachineCodeEmitter &MCE);
61 JITCodeEmitter &JCE);
6262
63 /// emitFunctionStub - Use the specified MachineCodeEmitter object to emit a
63 /// emitFunctionStub - Use the specified JITCodeEmitter object to emit a
6464 /// small native function that simply calls the function at the specified
6565 /// address.
6666 virtual void *emitFunctionStub(const Function* F, void *Fn,
67 MachineCodeEmitter &MCE);
67 JITCodeEmitter &JCE);
6868
6969 /// getLazyResolverFunction - Expose the lazy resolver to the JIT.
7070 virtual LazyResolverFn getLazyResolverFunction(JITCompilerFn);
8585
8686 /// allocateSeparateGVMemory - If true, globals should be placed in
8787 /// separately allocated heap memory rather than in the same
88 /// code memory allocated by MachineCodeEmitter.
88 /// code memory allocated by JITCodeEmitter.
8989 virtual bool allocateSeparateGVMemory() const {
9090 #ifdef __APPLE__
9191 return true;
189189 return false;
190190 }
191191
192 bool ARMTargetMachine::addCodeEmitter(PassManagerBase &PM,
193 CodeGenOpt::Level OptLevel,
194 bool DumpAsm,
195 JITCodeEmitter &JCE) {
196 // FIXME: Move this to TargetJITInfo!
197 if (DefRelocModel == Reloc::Default)
198 setRelocationModel(Reloc::Static);
199
200 // Machine code emitter pass for ARM.
201 PM.add(createARMJITCodeEmitterPass(*this, JCE));
202 if (DumpAsm) {
203 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
204 if (AsmPrinterCtor)
205 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
206 }
207
208 return false;
209 }
210
192211 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
193212 CodeGenOpt::Level OptLevel,
194213 bool DumpAsm,
203222
204223 return false;
205224 }
225
226 bool ARMTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
227 CodeGenOpt::Level OptLevel,
228 bool DumpAsm,
229 JITCodeEmitter &JCE) {
230 // Machine code emitter pass for ARM.
231 PM.add(createARMJITCodeEmitterPass(*this, JCE));
232 if (DumpAsm) {
233 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
234 if (AsmPrinterCtor)
235 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
236 }
237
238 return false;
239 }
240
241
7676 bool Verbose, raw_ostream &Out);
7777 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
7878 bool DumpAsm, MachineCodeEmitter &MCE);
79 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
80 bool DumpAsm, JITCodeEmitter &MCE);
7981 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
8082 CodeGenOpt::Level OptLevel,
8183 bool DumpAsm,
8284 MachineCodeEmitter &MCE);
85 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
86 CodeGenOpt::Level OptLevel,
87 bool DumpAsm,
88 JITCodeEmitter &MCE);
8389 };
8490
8591 /// ThumbTargetMachine - Thumb target machine.
3131 FunctionPass *createAlphaPatternInstructionSelector(TargetMachine &TM);
3232 FunctionPass *createAlphaCodeEmitterPass(AlphaTargetMachine &TM,
3333 MachineCodeEmitter &MCE);
34 FunctionPass *createAlphaJITCodeEmitterPass(AlphaTargetMachine &TM,
35 JITCodeEmitter &JCE);
3436 FunctionPass *createAlphaLLRPPass(AlphaTargetMachine &tm);
3537 FunctionPass *createAlphaBranchSelectionPass();
3638
1717 #include "Alpha.h"
1818 #include "llvm/PassManager.h"
1919 #include "llvm/CodeGen/MachineCodeEmitter.h"
20 #include "llvm/CodeGen/JITCodeEmitter.h"
2021 #include "llvm/CodeGen/MachineFunctionPass.h"
2122 #include "llvm/CodeGen/MachineInstr.h"
2223 #include "llvm/CodeGen/Passes.h"
2324 #include "llvm/Function.h"
25 #include "llvm/Support/Compiler.h"
2426 #include "llvm/Support/Debug.h"
2527 using namespace llvm;
2628
2729 namespace {
28 class AlphaCodeEmitter : public MachineFunctionPass {
30
31 class AlphaCodeEmitter {
32 MachineCodeEmitter &MCE;
33 public:
34 AlphaCodeEmitter( MachineCodeEmitter &mce) : MCE(mce) {}
35
36 /// getBinaryCodeForInstr - This function, generated by the
37 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
38 /// machine instructions.
39
40 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
41
42 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
43
44 unsigned getMachineOpValue(const MachineInstr &MI, const MachineOperand &MO);
45 };
46
47 template
48 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass,
49 public AlphaCodeEmitter
50 {
2951 const AlphaInstrInfo *II;
3052 TargetMachine &TM;
31 MachineCodeEmitter &MCE;
32
33 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
34 ///
35 unsigned getMachineOpValue(const MachineInstr &MI,
36 const MachineOperand &MO);
53 machineCodeEmitter &MCE;
3754
3855 public:
3956 static char ID;
40 explicit AlphaCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce)
41 : MachineFunctionPass(&ID), II(0), TM(tm), MCE(mce) {}
42 AlphaCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce,
57 explicit Emitter(TargetMachine &tm, machineCodeEmitter &mce)
58 : MachineFunctionPass(&ID), AlphaCodeEmitter( mce),
59 II(0), TM(tm), MCE(mce) {}
60 Emitter(TargetMachine &tm, machineCodeEmitter &mce,
4361 const AlphaInstrInfo& ii)
44 : MachineFunctionPass(&ID), II(&ii), TM(tm), MCE(mce) {}
62 : MachineFunctionPass(&ID), AlphaCodeEmitter( mce),
63 II(&ii), TM(tm), MCE(mce) {}
4564
4665 bool runOnMachineFunction(MachineFunction &MF);
4766
5170
5271 void emitInstruction(const MachineInstr &MI);
5372
54 /// getBinaryCodeForInstr - This function, generated by the
55 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
56 /// machine instructions.
57 ///
58 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
59
6073 private:
6174 void emitBasicBlock(MachineBasicBlock &MBB);
62
6375 };
64 char AlphaCodeEmitter::ID = 0;
76
77 template
78 char Emitter::ID = 0;
6579 }
6680
6781 /// createAlphaCodeEmitterPass - Return a pass that emits the collected Alpha code
6882 /// to the specified MCE object.
69 FunctionPass *llvm::createAlphaCodeEmitterPass(AlphaTargetMachine &TM,
83
84 FunctionPass *llvm::createAlphaCodeEmitterPass( AlphaTargetMachine &TM,
7085 MachineCodeEmitter &MCE) {
71 return new AlphaCodeEmitter(TM, MCE);
72 }
73
74 bool AlphaCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
86 return new Emitter(TM, MCE);
87 }
88
89 FunctionPass *llvm::createAlphaJITCodeEmitterPass( AlphaTargetMachine &TM,
90 JITCodeEmitter &JCE) {
91 return new Emitter(TM, JCE);
92 }
93
94 template
95 bool Emitter::runOnMachineFunction(MachineFunction &MF) {
7596 II = ((AlphaTargetMachine&)MF.getTarget()).getInstrInfo();
7697
7798 do {
83104 return false;
84105 }
85106
86 void AlphaCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
107 template
108 void Emitter::emitBasicBlock(MachineBasicBlock &MBB) {
87109 MCE.StartMachineBasicBlock(&MBB);
88110 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
89111 I != E; ++I) {
142164 }
143165
144166 unsigned AlphaCodeEmitter::getMachineOpValue(const MachineInstr &MI,
145 const MachineOperand &MO) {
167 const MachineOperand &MO) {
146168
147169 unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
148170 // or things that get fixed up later by the JIT.
214236 return rv;
215237 }
216238
217
218239 #include "AlphaGenCodeEmitter.inc"
219240
241
1414 #include "AlphaJITInfo.h"
1515 #include "AlphaRelocations.h"
1616 #include "llvm/Function.h"
17 #include "llvm/CodeGen/MachineCodeEmitter.h"
17 #include "llvm/CodeGen/JITCodeEmitter.h"
1818 #include "llvm/Config/alloca.h"
1919 #include "llvm/Support/Debug.h"
2020 #include
191191 }
192192
193193 void *AlphaJITInfo::emitFunctionStub(const Function* F, void *Fn,
194 MachineCodeEmitter &MCE) {
194 JITCodeEmitter &JCE) {
195195 //assert(Fn == AlphaCompilationCallback && "Where are you going?\n");
196196 //Do things in a stupid slow way!
197 MCE.startGVStub(F, 19*4);
198 void* Addr = (void*)(intptr_t)MCE.getCurrentPCValue();
197 JCE.startGVStub(F, 19*4);
198 void* Addr = (void*)(intptr_t)JCE.getCurrentPCValue();
199199 for (int x = 0; x < 19; ++ x)
200 MCE.emitWordLE(0);
200 JCE.emitWordLE(0);
201201 EmitBranchToAt(Addr, Fn);
202202 DOUT << "Emitting Stub to " << Fn << " at [" << Addr << "]\n";
203 return MCE.finishGVStub(F);
203 return JCE.finishGVStub(F);
204204 }
205205
206206 TargetJITInfo::LazyResolverFn
2626 { useGOT = true; }
2727
2828 virtual void *emitFunctionStub(const Function* F, void *Fn,
29 MachineCodeEmitter &MCE);
29 JITCodeEmitter &JCE);
3030 virtual LazyResolverFn getLazyResolverFunction(JITCompilerFn);
3131 virtual void relocate(void *Function, MachineRelocation *MR,
3232 unsigned NumRelocs, unsigned char* GOTBase);
102102 PM.add(createAlphaCodePrinterPass(errs(), *this, OptLevel, true));
103103 return false;
104104 }
105 bool AlphaTargetMachine::addCodeEmitter(PassManagerBase &PM,
106 CodeGenOpt::Level OptLevel,
107 bool DumpAsm, JITCodeEmitter &JCE) {
108 PM.add(createAlphaJITCodeEmitterPass(*this, JCE));
109 if (DumpAsm)
110 PM.add(createAlphaCodePrinterPass(errs(), *this, OptLevel, true));
111 return false;
112 }
105113 bool AlphaTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
106114 CodeGenOpt::Level OptLevel,
107115 bool DumpAsm,
108116 MachineCodeEmitter &MCE) {
109117 return addCodeEmitter(PM, OptLevel, DumpAsm, MCE);
110118 }
119 bool AlphaTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
120 CodeGenOpt::Level OptLevel,
121 bool DumpAsm,
122 JITCodeEmitter &JCE) {
123 return addCodeEmitter(PM, OptLevel, DumpAsm, JCE);
124 }
125
6464 bool Verbose, raw_ostream &Out);
6565 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
6666 bool DumpAsm, MachineCodeEmitter &MCE);
67 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
68 bool DumpAsm, JITCodeEmitter &JCE);
6769 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
6870 CodeGenOpt::Level OptLevel,
6971 bool DumpAsm,
7072 MachineCodeEmitter &MCE);
73 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
74 CodeGenOpt::Level OptLevel,
75 bool DumpAsm,
76 JITCodeEmitter &JCE);
7177 };
7278
7379 } // end namespace llvm
3232 CodeGenOpt::Level OptLevel, bool Verbose);
3333 FunctionPass *createPPCCodeEmitterPass(PPCTargetMachine &TM,
3434 MachineCodeEmitter &MCE);
35 FunctionPass *createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
36 JITCodeEmitter &MCE);
3537 } // end namespace llvm;
3638
3739 // Defines symbolic names for PowerPC registers. This defines a mapping from
1717 #include "llvm/Module.h"
1818 #include "llvm/PassManager.h"
1919 #include "llvm/CodeGen/MachineCodeEmitter.h"
20 #include "llvm/CodeGen/JITCodeEmitter.h"
2021 #include "llvm/CodeGen/MachineFunctionPass.h"
2122 #include "llvm/CodeGen/MachineInstrBuilder.h"
2223 #include "llvm/CodeGen/MachineModuleInfo.h"
2728 using namespace llvm;
2829
2930 namespace {
30 class VISIBILITY_HIDDEN PPCCodeEmitter : public MachineFunctionPass {
31 class PPCCodeEmitter {
3132 TargetMachine &TM;
3233 MachineCodeEmitter &MCE;
34 public:
35 PPCCodeEmitter( TargetMachine &tm, MachineCodeEmitter &mce) :
36 TM( tm), MCE( mce) {}
37
38 /// getBinaryCodeForInstr - This function, generated by the
39 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
40 /// machine instructions.
41
42 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
43
44 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
45
46 unsigned getMachineOpValue(const MachineInstr &MI, const MachineOperand &MO);
3347
3448 /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
3549 /// its address in the function into this pointer.
50
3651 void *MovePCtoLROffset;
37
38 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
39 ///
40 unsigned getMachineOpValue(const MachineInstr &MI, const MachineOperand &MO);
41
52 };
53
54 template
55 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass,
56 public PPCCodeEmitter
57 {
58 TargetMachine &TM;
59 machineCodeEmitter &MCE;
60
4261 void getAnalysisUsage(AnalysisUsage &AU) const {
4362 AU.addRequired();
4463 MachineFunctionPass::getAnalysisUsage(AU);
4665
4766 public:
4867 static char ID;
49 PPCCodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
50 : MachineFunctionPass(&ID), TM(T), MCE(M) {}
68 Emitter(TargetMachine &tm, machineCodeEmitter &mce)
69 : MachineFunctionPass(&ID), PPCCodeEmitter( tm, mce), TM(tm), MCE(mce) {}
5170
5271 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
5372
6281 /// getValueBit - return the particular bit of Val
6382 ///
6483 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
65
66 /// getBinaryCodeForInstr - This function, generated by the
67 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
68 /// machine instructions.
69 ///
70 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
7184 };
72 char PPCCodeEmitter::ID = 0;
73 }
74
85
86 template
87 char Emitter::ID = 0;
88 }
89
7590 /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
7691 /// to the specified MCE object.
7792 FunctionPass *llvm::createPPCCodeEmitterPass(PPCTargetMachine &TM,
78 MachineCodeEmitter &MCE) {
79 return new PPCCodeEmitter(TM, MCE);
80 }
81
82 bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
93 MachineCodeEmitter &MCE) {
94 return new Emitter(TM, MCE);
95 }
96
97 FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
98 JITCodeEmitter &JCE) {
99 return new Emitter(TM, JCE);
100 }
101
102 template
103 bool Emitter::runOnMachineFunction(MachineFunction &MF) {
83104 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
84105 MF.getTarget().getRelocationModel() != Reloc::Static) &&
85106 "JIT relocation model must be set to static or default!");
95116 return false;
96117 }
97118
98 void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
119 template
120 void Emitter::emitBasicBlock(MachineBasicBlock &MBB) {
99121 MCE.StartMachineBasicBlock(&MBB);
100122
101123 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
1515 #include "PPCRelocations.h"
1616 #include "PPCTargetMachine.h"
1717 #include "llvm/Function.h"
18 #include "llvm/CodeGen/MachineCodeEmitter.h"
1918 #include "llvm/System/Memory.h"
2019 #include "llvm/Support/Debug.h"
2120 using namespace llvm;
329328 #endif
330329
331330 void *PPCJITInfo::emitFunctionStub(const Function* F, void *Fn,
332 MachineCodeEmitter &MCE) {
331 JITCodeEmitter &JCE) {
333332 // If this is just a call to an external function, emit a branch instead of a
334333 // call. The code is the same except for one bit of the last instruction.
335334 if (Fn != (void*)(intptr_t)PPC32CompilationCallback &&
336335 Fn != (void*)(intptr_t)PPC64CompilationCallback) {
337 MCE.startGVStub(F, 7*4);
338 intptr_t Addr = (intptr_t)MCE.getCurrentPCValue();
339 MCE.emitWordBE(0);
340 MCE.emitWordBE(0);
341 MCE.emitWordBE(0);
342 MCE.emitWordBE(0);
343 MCE.emitWordBE(0);
344 MCE.emitWordBE(0);
345 MCE.emitWordBE(0);
336 JCE.startGVStub(F, 7*4);
337 intptr_t Addr = (intptr_t)JCE.getCurrentPCValue();
338 JCE.emitWordBE(0);
339 JCE.emitWordBE(0);
340 JCE.emitWordBE(0);
341 JCE.emitWordBE(0);
342 JCE.emitWordBE(0);
343 JCE.emitWordBE(0);
344 JCE.emitWordBE(0);
346345 EmitBranchToAt(Addr, (intptr_t)Fn, false, is64Bit);
347346 sys::Memory::InvalidateInstructionCache((void*)Addr, 7*4);
348 return MCE.finishGVStub(F);
349 }
350
351 MCE.startGVStub(F, 10*4);
352 intptr_t Addr = (intptr_t)MCE.getCurrentPCValue();
347 return JCE.finishGVStub(F);
348 }
349
350 JCE.startGVStub(F, 10*4);
351 intptr_t Addr = (intptr_t)JCE.getCurrentPCValue();
353352 if (is64Bit) {
354 MCE.emitWordBE(0xf821ffb1); // stdu r1,-80(r1)
355 MCE.emitWordBE(0x7d6802a6); // mflr r11
356 MCE.emitWordBE(0xf9610060); // std r11, 96(r1)
353 JCE.emitWordBE(0xf821ffb1); // stdu r1,-80(r1)
354 JCE.emitWordBE(0x7d6802a6); // mflr r11
355 JCE.emitWordBE(0xf9610060); // std r11, 96(r1)
357356 } else if (TM.getSubtargetImpl()->isMachoABI()){
358 MCE.emitWordBE(0x9421ffe0); // stwu r1,-32(r1)
359 MCE.emitWordBE(0x7d6802a6); // mflr r11
360 MCE.emitWordBE(0x91610028); // stw r11, 40(r1)
357 JCE.emitWordBE(0x9421ffe0); // stwu r1,-32(r1)
358 JCE.emitWordBE(0x7d6802a6); // mflr r11
359 JCE.emitWordBE(0x91610028); // stw r11, 40(r1)
361360 } else {
362 MCE.emitWordBE(0x9421ffe0); // stwu r1,-32(r1)
363 MCE.emitWordBE(0x7d6802a6); // mflr r11
364 MCE.emitWordBE(0x91610024); // stw r11, 36(r1)
365 }
366 intptr_t BranchAddr = (intptr_t)MCE.getCurrentPCValue();
367 MCE.emitWordBE(0);
368 MCE.emitWordBE(0);
369 MCE.emitWordBE(0);
370 MCE.emitWordBE(0);
371 MCE.emitWordBE(0);
372 MCE.emitWordBE(0);
373 MCE.emitWordBE(0);
361 JCE.emitWordBE(0x9421ffe0); // stwu r1,-32(r1)
362 JCE.emitWordBE(0x7d6802a6); // mflr r11
363 JCE.emitWordBE(0x91610024); // stw r11, 36(r1)
364 }
365 intptr_t BranchAddr = (intptr_t)JCE.getCurrentPCValue();
366 JCE.emitWordBE(0);
367 JCE.emitWordBE(0);
368 JCE.emitWordBE(0);
369 JCE.emitWordBE(0);
370 JCE.emitWordBE(0);
371 JCE.emitWordBE(0);
372 JCE.emitWordBE(0);
374373 EmitBranchToAt(BranchAddr, (intptr_t)Fn, true, is64Bit);
375374 sys::Memory::InvalidateInstructionCache((void*)Addr, 10*4);
376 return MCE.finishGVStub(F);
375 return JCE.finishGVStub(F);
377376 }
378377
379378
1414 #define POWERPC_JITINFO_H
1515
1616 #include "llvm/Target/TargetJITInfo.h"
17 #include "llvm/CodeGen/JITCodeEmitter.h"
1718
1819 namespace llvm {
1920 class PPCTargetMachine;
2930 }
3031
3132 virtual void *emitFunctionStub(const Function* F, void *Fn,
32 MachineCodeEmitter &MCE);
33 JITCodeEmitter &JCE);
3334 virtual LazyResolverFn getLazyResolverFunction(JITCompilerFn);
3435 virtual void relocate(void *Function, MachineRelocation *MR,
3536 unsigned NumRelocs, unsigned char* GOTBase);
185185 return false;
186186 }
187187
188 bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM,
189 CodeGenOpt::Level OptLevel,
190 bool DumpAsm, JITCodeEmitter &JCE) {
191 // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
192 // FIXME: This should be moved to TargetJITInfo!!
193 if (Subtarget.isPPC64()) {
194 // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
195 // instructions to materialize arbitrary global variable + function +
196 // constant pool addresses.
197 setRelocationModel(Reloc::PIC_);
198 // Temporary workaround for the inability of PPC64 JIT to handle jump
199 // tables.
200 DisableJumpTables = true;
201 } else {
202 setRelocationModel(Reloc::Static);
203 }
204
205 // Inform the subtarget that we are in JIT mode. FIXME: does this break macho
206 // writing?
207 Subtarget.SetJITMode();
208
209 // Machine code emitter pass for PowerPC.
210 PM.add(createPPCJITCodeEmitterPass(*this, JCE));
211 if (DumpAsm) {
212 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
213 if (AsmPrinterCtor)
214 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
215 }
216
217 return false;
218 }
219
188220 bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
189221 CodeGenOpt::Level OptLevel,
190222 bool DumpAsm, MachineCodeEmitter &MCE) {
198230
199231 return false;
200232 }
233
234 bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
235 CodeGenOpt::Level OptLevel,
236 bool DumpAsm, JITCodeEmitter &JCE) {
237 // Machine code emitter pass for PowerPC.
238 PM.add(createPPCJITCodeEmitterPass(*this, JCE));
239 if (DumpAsm) {
240 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
241 if (AsmPrinterCtor)
242 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
243 }
244
245 return false;
246 }
247
8383 bool Verbose, raw_ostream &Out);
8484 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
8585 bool DumpAsm, MachineCodeEmitter &MCE);
86 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
87 bool DumpAsm, JITCodeEmitter &JCE);
8688 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
8789 CodeGenOpt::Level OptLevel,
8890 bool DumpAsm, MachineCodeEmitter &MCE);
91 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
92 CodeGenOpt::Level OptLevel,
93 bool DumpAsm, JITCodeEmitter &JCE);
8994 virtual bool getEnableTailMergeDefault() const;
9095 };
9196
2121 class X86TargetMachine;
2222 class FunctionPass;
2323 class MachineCodeEmitter;
24 class JITCodeEmitter;
2425 class raw_ostream;
2526
2627 /// createX86ISelDag - This pass converts a legalized DAG into a
5051
5152 /// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
5253 /// to the specified MCE object.
53 FunctionPass *createX86CodeEmitterPass(X86TargetMachine &TM,
54 MachineCodeEmitter &MCE);
54
55 FunctionPass *createX86CodeEmitterPass(
56 X86TargetMachine &TM, MachineCodeEmitter &MCE);
57 FunctionPass *createX86JITCodeEmitterPass(
58 X86TargetMachine &TM, JITCodeEmitter &JCE);
5559
5660 /// createX86EmitCodeToMemory - Returns a pass that converts a register
5761 /// allocated function into raw machine code in a dynamically
2020 #include "X86.h"
2121 #include "llvm/PassManager.h"
2222 #include "llvm/CodeGen/MachineCodeEmitter.h"
23 #include "llvm/CodeGen/JITCodeEmitter.h"
2324 #include "llvm/CodeGen/MachineFunctionPass.h"
2425 #include "llvm/CodeGen/MachineInstr.h"
2526 #include "llvm/CodeGen/MachineModuleInfo.h"
3435 STATISTIC(NumEmitted, "Number of machine instructions emitted");
3536
3637 namespace {
38 template< class machineCodeEmitter>
3739 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass {
3840 const X86InstrInfo *II;
3941 const TargetData *TD;
4042 X86TargetMachine &TM;
41 MachineCodeEmitter &MCE;
43 machineCodeEmitter &MCE;
4244 intptr_t PICBaseOffset;
4345 bool Is64BitMode;
4446 bool IsPIC;
4547 public:
4648 static char ID;
47 explicit Emitter(X86TargetMachine &tm, MachineCodeEmitter &mce)
49 explicit Emitter(X86TargetMachine &tm, machineCodeEmitter &mce)
4850 : MachineFunctionPass(&ID), II(0), TD(0), TM(tm),
4951 MCE(mce), PICBaseOffset(0), Is64BitMode(false),
5052 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
51 Emitter(X86TargetMachine &tm, MachineCodeEmitter &mce,
53 Emitter(X86TargetMachine &tm, machineCodeEmitter &mce,
5254 const X86InstrInfo &ii, const TargetData &td, bool is64)
5355 : MachineFunctionPass(&ID), II(&ii), TD(&td), TM(tm),
5456 MCE(mce), PICBaseOffset(0), Is64BitMode(is64),
9597
9698 bool gvNeedsNonLazyPtr(const GlobalValue *GV);
9799 };
98 char Emitter::ID = 0;
100
101 template< class machineCodeEmitter>
102 char Emitter::ID = 0;
99103 }
100104
101105 /// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
102 /// to the specified MCE object.
103 FunctionPass *llvm::createX86CodeEmitterPass(X86TargetMachine &TM,
104 MachineCodeEmitter &MCE) {
105 return new Emitter(TM, MCE);
106 }
107
108 bool Emitter::runOnMachineFunction(MachineFunction &MF) {
106 /// to the specified templated MachineCodeEmitter object.
107
108 namespace llvm {
109
110 FunctionPass *createX86CodeEmitterPass(
111 X86TargetMachine &TM, MachineCodeEmitter &MCE)
112 {
113 return new Emitter(TM, MCE);
114 }
115 FunctionPass *createX86JITCodeEmitterPass(
116 X86TargetMachine &TM, JITCodeEmitter &JCE)
117 {
118 return new Emitter(TM, JCE);
119 }
120
121 } // end namespace llvm
122
123 template< class machineCodeEmitter>
124 bool Emitter::runOnMachineFunction(MachineFunction &MF) {
109125
110126 MCE.setModuleInfo(&getAnalysis());
111127
139155 /// necessary to resolve the address of this block later and emits a dummy
140156 /// value.
141157 ///
142 void Emitter::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
158 template< class machineCodeEmitter>
159 void Emitter::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
143160 // Remember where this reference was and where it is to so we can
144161 // deal with it later.
145162 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
150167 /// emitGlobalAddress - Emit the specified address to the code stream assuming
151168 /// this is part of a "take the address of a global" instruction.
152169 ///
153 void Emitter::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
170 template< class machineCodeEmitter>
171 void Emitter::emitGlobalAddress(GlobalValue *GV, unsigned Reloc,
154172 intptr_t Disp /* = 0 */,
155173 intptr_t PCAdj /* = 0 */,
156174 bool NeedStub /* = false */,
176194 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to
177195 /// be emitted to the current location in the function, and allow it to be PC
178196 /// relative.
179 void Emitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
197 template< class machineCodeEmitter>
198 void Emitter::emitExternalSymbolAddress(const char *ES, unsigned Reloc) {
180199 intptr_t RelocCST = (Reloc == X86::reloc_picrel_word) ? PICBaseOffset : 0;
181200 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
182201 Reloc, ES, RelocCST));
189208 /// emitConstPoolAddress - Arrange for the address of an constant pool
190209 /// to be emitted to the current location in the function, and allow it to be PC
191210 /// relative.
192 void Emitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
211 template< class machineCodeEmitter>
212 void Emitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc,
193213 intptr_t Disp /* = 0 */,
194214 intptr_t PCAdj /* = 0 */) {
195215 intptr_t RelocCST = 0;
209229 /// emitJumpTableAddress - Arrange for the address of a jump table to
210230 /// be emitted to the current location in the function, and allow it to be PC
211231 /// relative.
212 void Emitter::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
232 template< class machineCodeEmitter>
233 void Emitter::emitJumpTableAddress(unsigned JTI, unsigned Reloc,
213234 intptr_t PCAdj /* = 0 */) {
214235 intptr_t RelocCST = 0;
215236 if (Reloc == X86::reloc_picrel_word)
225246 MCE.emitWordLE(0);
226247 }
227248
228 unsigned Emitter::getX86RegNum(unsigned RegNo) const {
249 template< class machineCodeEmitter>
250 unsigned Emitter::getX86RegNum(unsigned RegNo) const {
229251 return II->getRegisterInfo().getX86RegNum(RegNo);
230252 }
231253
235257 return RM | (RegOpcode << 3) | (Mod << 6);
236258 }
237259
238 void Emitter::emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeFld){
260 template< class machineCodeEmitter>
261 void Emitter::emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeFld){
239262 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
240263 }
241264
242 void Emitter::emitRegModRMByte(unsigned RegOpcodeFld) {
265 template< class machineCodeEmitter>
266 void Emitter::emitRegModRMByte(unsigned RegOpcodeFld) {
243267 MCE.emitByte(ModRMByte(3, RegOpcodeFld, 0));
244268 }
245269
246 void Emitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base) {
270 template< class machineCodeEmitter>
271 void Emitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base) {
247272 // SIB byte is in the same format as the ModRMByte...
248273 MCE.emitByte(ModRMByte(SS, Index, Base));
249274 }
250275
251 void Emitter::emitConstant(uint64_t Val, unsigned Size) {
276 template< class machineCodeEmitter>
277 void Emitter::emitConstant(uint64_t Val, unsigned Size) {
252278 // Output the constant in little endian byte order...
253279 for (unsigned i = 0; i != Size; ++i) {
254280 MCE.emitByte(Val & 255);
262288 return Value == (signed char)Value;
263289 }
264290
265 bool Emitter::gvNeedsNonLazyPtr(const GlobalValue *GV) {
291 template< class machineCodeEmitter>
292 bool Emitter::gvNeedsNonLazyPtr(const GlobalValue *GV) {
266293 // For Darwin, simulate the linktime GOT by using the same non-lazy-pointer
267294 // mechanism as 32-bit mode.
268295 return (!Is64BitMode || TM.getSubtarget().isTargetDarwin()) &&
269296 TM.getSubtarget().GVRequiresExtraLoad(GV, TM, false);
270297 }
271298
272 void Emitter::emitDisplacementField(const MachineOperand *RelocOp,
299 template< class machineCodeEmitter>
300 void Emitter::emitDisplacementField(const MachineOperand *RelocOp,
273301 int DispVal, intptr_t PCAdj) {
274302 // If this is a simple integer displacement that doesn't require a relocation,
275303 // emit it now.
303331 }
304332 }
305333
306 void Emitter::emitMemModRMByte(const MachineInstr &MI,
334 template< class machineCodeEmitter>
335 void Emitter::emitMemModRMByte(const MachineInstr &MI,
307336 unsigned Op, unsigned RegOpcodeField,
308337 intptr_t PCAdj) {
309338 const MachineOperand &Op3 = MI.getOperand(Op+3);
420449 }
421450 }
422451
423 void Emitter::emitInstruction(const MachineInstr &MI,
452 template< class machineCodeEmitter>
453 void Emitter::emitInstruction(
454 const MachineInstr &MI,
424455 const TargetInstrDesc *Desc) {
425456 DOUT << MI;
426457
772803 abort();
773804 }
774805 }
806
1515 #include "X86Relocations.h"
1616 #include "X86Subtarget.h"
1717 #include "llvm/Function.h"
18 #include "llvm/CodeGen/MachineCodeEmitter.h"
1918 #include "llvm/Config/alloca.h"
2019 #include "llvm/Support/Compiler.h"
2120 #include
429428 }
430429
431430 void *X86JITInfo::emitGlobalValueIndirectSym(const GlobalValue* GV, void *ptr,
432 MachineCodeEmitter &MCE) {
433 #if defined (X86_64_JIT)
434 MCE.startGVStub(GV, 8, 8);
435 MCE.emitWordLE((unsigned)(intptr_t)ptr);
436 MCE.emitWordLE((unsigned)(((intptr_t)ptr) >> 32));
437 #else
438 MCE.startGVStub(GV, 4, 4);
439 MCE.emitWordLE((intptr_t)ptr);
440 #endif
441 return MCE.finishGVStub(GV);
431 JITCodeEmitter &JCE) {
432 #if defined (X86_64_JIT)
433 JCE.startGVStub(GV, 8, 8);
434 JCE.emitWordLE((unsigned)(intptr_t)ptr);
435 JCE.emitWordLE((unsigned)(((intptr_t)ptr) >> 32));
436 #else
437 JCE.startGVStub(GV, 4, 4);
438 JCE.emitWordLE((intptr_t)ptr);
439 #endif
440 return JCE.finishGVStub(GV);
442441 }
443442
444443 void *X86JITInfo::emitFunctionStub(const Function* F, void *Fn,
445 MachineCodeEmitter &MCE) {
444 JITCodeEmitter &JCE) {
446445 // Note, we cast to intptr_t here to silence a -pedantic warning that
447446 // complains about casting a function pointer to a normal pointer.
448447 #if defined (X86_32_JIT) && !defined (_MSC_VER)
453452 #endif
454453 if (NotCC) {
455454 #if defined (X86_64_JIT)
456 MCE.startGVStub(F, 13, 4);
457 MCE.emitByte(0x49); // REX prefix
458 MCE.emitByte(0xB8+2); // movabsq r10
459 MCE.emitWordLE((unsigned)(intptr_t)Fn);
460 MCE.emitWordLE((unsigned)(((intptr_t)Fn) >> 32));
461 MCE.emitByte(0x41); // REX prefix
462 MCE.emitByte(0xFF); // jmpq *r10
463 MCE.emitByte(2 | (4 << 3) | (3 << 6));
464 #else
465 MCE.startGVStub(F, 5, 4);
466 MCE.emitByte(0xE9);
467 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
468 #endif
469 return MCE.finishGVStub(F);
455 JCE.startGVStub(F, 13, 4);
456 JCE.emitByte(0x49); // REX prefix
457 JCE.emitByte(0xB8+2); // movabsq r10
458 JCE.emitWordLE((unsigned)(intptr_t)Fn);
459 JCE.emitWordLE((unsigned)(((intptr_t)Fn) >> 32));
460 JCE.emitByte(0x41); // REX prefix
461 JCE.emitByte(0xFF); // jmpq *r10
462 JCE.emitByte(2 | (4 << 3) | (3 << 6));
463 #else
464 JCE.startGVStub(F, 5, 4);
465 JCE.emitByte(0xE9);
466 JCE.emitWordLE((intptr_t)Fn-JCE.getCurrentPCValue()-4);
467 #endif
468 return JCE.finishGVStub(F);
470469 }
471470
472471 #if defined (X86_64_JIT)
473 MCE.startGVStub(F, 14, 4);
474 MCE.emitByte(0x49); // REX prefix
475 MCE.emitByte(0xB8+2); // movabsq r10
476 MCE.emitWordLE((unsigned)(intptr_t)Fn);
477 MCE.emitWordLE((unsigned)(((intptr_t)Fn) >> 32));
478 MCE.emitByte(0x41); // REX prefix
479 MCE.emitByte(0xFF); // callq *r10
480 MCE.emitByte(2 | (2 << 3) | (3 << 6));
481 #else
482 MCE.startGVStub(F, 6, 4);
483 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
484
485 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
486 #endif
487
488 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
489 return MCE.finishGVStub(F);
472 JCE.startGVStub(F, 14, 4);
473 JCE.emitByte(0x49); // REX prefix
474 JCE.emitByte(0xB8+2); // movabsq r10
475 JCE.emitWordLE((unsigned)(intptr_t)Fn);
476 JCE.emitWordLE((unsigned)(((intptr_t)Fn) >> 32));
477 JCE.emitByte(0x41); // REX prefix
478 JCE.emitByte(0xFF); // callq *r10
479 JCE.emitByte(2 | (2 << 3) | (3 << 6));
480 #else
481 JCE.startGVStub(F, 6, 4);
482 JCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
483
484 JCE.emitWordLE((intptr_t)Fn-JCE.getCurrentPCValue()-4);
485 #endif
486
487 JCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
488 return JCE.finishGVStub(F);
490489 }
491490
492491 void X86JITInfo::emitFunctionStubAtAddr(const Function* F, void *Fn, void *Stub,
493 MachineCodeEmitter &MCE) {
492 JITCodeEmitter &JCE) {
494493 // Note, we cast to intptr_t here to silence a -pedantic warning that
495494 // complains about casting a function pointer to a normal pointer.
496 MCE.startGVStub(F, Stub, 5);
497 MCE.emitByte(0xE9);
498 #if defined (X86_64_JIT)
499 assert(((((intptr_t)Fn-MCE.getCurrentPCValue()-5) << 32) >> 32) ==
500 ((intptr_t)Fn-MCE.getCurrentPCValue()-5)
495 JCE.startGVStub(F, Stub, 5);
496 JCE.emitByte(0xE9);
497 #if defined (X86_64_JIT)
498 assert(((((intptr_t)Fn-JCE.getCurrentPCValue()-5) << 32) >> 32) ==
499 ((intptr_t)Fn-JCE.getCurrentPCValue()-5)
501500 && "PIC displacement does not fit in displacement field!");
502501 #endif
503 MCE.emitWordLE((intptr_t)Fn-MCE.getCurrentPCValue()-4);
504 MCE.finishGVStub(F);
502 JCE.emitWordLE((intptr_t)Fn-JCE.getCurrentPCValue()-4);
503 JCE.finishGVStub(F);
505504 }
506505
507506 /// getPICJumpTableEntry - Returns the value of the jumptable entry for the
1414 #define X86JITINFO_H
1515
1616 #include "llvm/Function.h"
17 #include "llvm/CodeGen/JITCodeEmitter.h"
1718 #include "llvm/Target/TargetJITInfo.h"
1819
1920 namespace llvm {
3637 ///
3738 virtual void replaceMachineCodeForFunction(void *Old, void *New);
3839
39 /// emitGlobalValueIndirectSym - Use the specified MachineCodeEmitter object
40 /// emitGlobalValueIndirectSym - Use the specified JITCodeEmitter object
4041 /// to emit an indirect symbol which contains the address of the specified
4142 /// ptr.
4243 virtual void *emitGlobalValueIndirectSym(const GlobalValue* GV, void *ptr,
43 MachineCodeEmitter &MCE);
44 JITCodeEmitter &JCE);
4445
45 /// emitFunctionStub - Use the specified MachineCodeEmitter object to emit a
46 /// emitFunctionStub - Use the specified JITCodeEmitter object to emit a
4647 /// small native function that simply calls the function at the specified
4748 /// address.
4849 virtual void *emitFunctionStub(const Function* F, void *Fn,
49 MachineCodeEmitter &MCE);
50 JITCodeEmitter &JCE);
5051
51 /// emitFunctionStubAtAddr - Use the specified MachineCodeEmitter object to
52 /// emitFunctionStubAtAddr - Use the specified JITCodeEmitter object to
5253 /// emit a small native function that simply calls Fn. Emit the stub into
5354 /// the supplied buffer.
5455 virtual void emitFunctionStubAtAddr(const Function* F, void *Fn,
55 void *Buffer, MachineCodeEmitter &MCE);
56 void *Buffer, JITCodeEmitter &JCE);
5657
5758 /// getPICJumpTableEntry - Returns the value of the jumptable entry for the
5859 /// specific basic block.
247247 return false;
248248 }
249249
250 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
251 CodeGenOpt::Level OptLevel,
252 bool DumpAsm, JITCodeEmitter &JCE) {
253 // FIXME: Move this to TargetJITInfo!
254 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
255 if (DefRelocModel == Reloc::Default &&
256 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit()))
257 setRelocationModel(Reloc::Static);
258
259 // 64-bit JIT places everything in the same buffer except external functions.
260 // On Darwin, use small code model but hack the call instruction for
261 // externals. Elsewhere, do not assume globals are in the lower 4G.
262 if (Subtarget.is64Bit()) {
263 if (Subtarget.isTargetDarwin())
264 setCodeModel(CodeModel::Small);
265 else
266 setCodeModel(CodeModel::Large);
267 }
268
269 PM.add(createX86JITCodeEmitterPass(*this, JCE));
270 if (DumpAsm) {
271 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
272 if (AsmPrinterCtor)
273 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
274 }
275
276 return false;
277 }
278
250279 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
251280 CodeGenOpt::Level OptLevel,
252281 bool DumpAsm,
261290 return false;
262291 }
263292
293 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
294 CodeGenOpt::Level OptLevel,
295 bool DumpAsm,
296 JITCodeEmitter &JCE) {
297 PM.add(createX86JITCodeEmitterPass(*this, JCE));
298 if (DumpAsm) {
299 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
300 if (AsmPrinterCtor)
301 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
302 }
303
304 return false;
305 }
306
264307 /// symbolicAddressesAreRIPRel - Return true if symbolic addresses are
265308 /// RIP-relative on this machine, taking into consideration the relocation
266309 /// model and subtarget. RIP-relative addresses cannot have a separate
8282 bool Verbose, raw_ostream &Out);
8383 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
8484 bool DumpAsm, MachineCodeEmitter &MCE);
85 virtual bool addCodeEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
86 bool DumpAsm, JITCodeEmitter &JCE);
8587 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
8688 CodeGenOpt::Level OptLevel,
8789 bool DumpAsm, MachineCodeEmitter &MCE);
90 virtual bool addSimpleCodeEmitter(PassManagerBase &PM,
91 CodeGenOpt::Level OptLevel,
92 bool DumpAsm, JITCodeEmitter &JCE);
8893
8994 /// symbolicAddressesAreRIPRel - Return true if symbolic addresses are
9095 /// RIP-relative on this machine, taking into consideration the relocation