blob: c330f78bef88e552e2c111e6f635ff7a96d41709 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Shih-wei Liao31384c52011-09-06 15:27:45 -070016
17#include "assembler_x86.h"
18#include "jni_internal.h"
19#include "object.h"
Brian Carlstrom40381fb2011-10-19 14:13:40 -070020#include "stack_indirect_reference_table.h"
Shih-wei Liao31384c52011-09-06 15:27:45 -070021
22#define __ assembler->
23
24namespace art {
25namespace x86 {
26
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070027ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -070028 UniquePtr<X86Assembler> assembler(static_cast<X86Assembler*>(Assembler::Create(kX86)));
Ian Rogersad25ac52011-10-04 19:13:33 -070029
30 // TODO: unimplemented
31 __ int3();
32
33 assembler->EmitSlowPaths();
34 size_t cs = assembler->CodeSize();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070035 SirtRef<ByteArray> resolution_trampoline(ByteArray::Alloc(cs));
36 CHECK(resolution_trampoline.get() != NULL);
Ian Rogersad25ac52011-10-04 19:13:33 -070037 MemoryRegion code(resolution_trampoline->GetData(), resolution_trampoline->GetLength());
38 assembler->FinalizeInstructions(code);
39
Brian Carlstrom40381fb2011-10-19 14:13:40 -070040 return resolution_trampoline.get();
Ian Rogersad25ac52011-10-04 19:13:33 -070041}
42
Shih-wei Liaoc486c112011-09-13 16:43:52 -070043typedef void (*ThrowAme)(Method*, Thread*);
44
Ian Rogersbdb03912011-09-14 00:55:44 -070045ByteArray* CreateAbstractMethodErrorStub() {
Elliott Hughes362f9bc2011-10-17 18:56:41 -070046 UniquePtr<X86Assembler> assembler(static_cast<X86Assembler*>(Assembler::Create(kX86)));
Shih-wei Liaoc486c112011-09-13 16:43:52 -070047
48 // Pad stack to ensure 16-byte alignment
49 __ pushl(Immediate(0));
Shih-wei Liaoc486c112011-09-13 16:43:52 -070050 __ fs()->pushl(Address::Absolute(Thread::SelfOffset())); // Thread*
Ian Rogers67375ac2011-09-14 00:55:44 -070051 __ pushl(EAX); // Method*
Shih-wei Liaoc486c112011-09-13 16:43:52 -070052
Ian Rogersbdb03912011-09-14 00:55:44 -070053 // Call to throw AbstractMethodError
54 __ Call(ThreadOffset(OFFSETOF_MEMBER(Thread, pThrowAbstractMethodErrorFromCode)),
55 X86ManagedRegister::FromCpuRegister(ECX));
Shih-wei Liaoc486c112011-09-13 16:43:52 -070056
57 // Because the call above never returns, we do not need to do ESP+=16 here.
58
59 __ int3();
60
61 assembler->EmitSlowPaths();
62
63 size_t cs = assembler->CodeSize();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070064 SirtRef<ByteArray> abstract_stub(ByteArray::Alloc(cs));
65 CHECK(abstract_stub.get() != NULL);
Shih-wei Liaoc486c112011-09-13 16:43:52 -070066 MemoryRegion code(abstract_stub->GetData(), abstract_stub->GetLength());
67 assembler->FinalizeInstructions(code);
68
Brian Carlstrom40381fb2011-10-19 14:13:40 -070069 return abstract_stub.get();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070070}
71
Elliott Hughes8add92d2012-01-18 18:18:43 -080072ByteArray* CreateJniDlsymLookupStub() {
Elliott Hughes362f9bc2011-10-17 18:56:41 -070073 UniquePtr<X86Assembler> assembler(static_cast<X86Assembler*>(Assembler::Create(kX86)));
Shih-wei Liao31384c52011-09-06 15:27:45 -070074
75 // Pad stack to ensure 16-byte alignment
76 __ pushl(Immediate(0));
77 __ pushl(Immediate(0));
Ian Rogers67375ac2011-09-14 00:55:44 -070078 __ fs()->pushl(Address::Absolute(Thread::SelfOffset())); // Thread*
Shih-wei Liao31384c52011-09-06 15:27:45 -070079
Ian Rogersbdb03912011-09-14 00:55:44 -070080 __ Call(ThreadOffset(OFFSETOF_MEMBER(Thread, pFindNativeMethod)),
81 X86ManagedRegister::FromCpuRegister(ECX));
Shih-wei Liao31384c52011-09-06 15:27:45 -070082
Ian Rogers67375ac2011-09-14 00:55:44 -070083 __ addl(ESP, Immediate(12));
Shih-wei Liao31384c52011-09-06 15:27:45 -070084
85 Label no_native_code_found; // forward declaration
86 __ cmpl(EAX, Immediate(0));
87 __ j(kEqual, &no_native_code_found);
88
89 __ jmp(EAX); // Tail call into native code
90
91 __ Bind(&no_native_code_found);
92 __ ret(); // return to caller to handle exception
93
94 assembler->EmitSlowPaths();
95
96 size_t cs = assembler->CodeSize();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070097 SirtRef<ByteArray> jni_stub(ByteArray::Alloc(cs));
98 CHECK(jni_stub.get() != NULL);
Shih-wei Liao31384c52011-09-06 15:27:45 -070099 MemoryRegion code(jni_stub->GetData(), jni_stub->GetLength());
100 assembler->FinalizeInstructions(code);
101
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700102 return jni_stub.get();
Shih-wei Liao31384c52011-09-06 15:27:45 -0700103}
104
105} // namespace x86
106} // namespace art