Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 16 | |
| 17 | #include "assembler_x86.h" |
| 18 | #include "jni_internal.h" |
| 19 | #include "object.h" |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 20 | #include "stack_indirect_reference_table.h" |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 21 | |
| 22 | #define __ assembler-> |
| 23 | |
| 24 | namespace art { |
| 25 | namespace x86 { |
| 26 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 27 | ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 28 | UniquePtr<X86Assembler> assembler(static_cast<X86Assembler*>(Assembler::Create(kX86))); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 29 | |
| 30 | // TODO: unimplemented |
| 31 | __ int3(); |
| 32 | |
| 33 | assembler->EmitSlowPaths(); |
| 34 | size_t cs = assembler->CodeSize(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 35 | SirtRef<ByteArray> resolution_trampoline(ByteArray::Alloc(cs)); |
| 36 | CHECK(resolution_trampoline.get() != NULL); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 37 | MemoryRegion code(resolution_trampoline->GetData(), resolution_trampoline->GetLength()); |
| 38 | assembler->FinalizeInstructions(code); |
| 39 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 40 | return resolution_trampoline.get(); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 43 | typedef void (*ThrowAme)(Method*, Thread*); |
| 44 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 45 | ByteArray* CreateAbstractMethodErrorStub() { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 46 | UniquePtr<X86Assembler> assembler(static_cast<X86Assembler*>(Assembler::Create(kX86))); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 47 | |
| 48 | // Pad stack to ensure 16-byte alignment |
| 49 | __ pushl(Immediate(0)); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 50 | __ fs()->pushl(Address::Absolute(Thread::SelfOffset())); // Thread* |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 51 | __ pushl(EAX); // Method* |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 52 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 53 | // Call to throw AbstractMethodError |
| 54 | __ Call(ThreadOffset(OFFSETOF_MEMBER(Thread, pThrowAbstractMethodErrorFromCode)), |
| 55 | X86ManagedRegister::FromCpuRegister(ECX)); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 56 | |
| 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 Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 64 | SirtRef<ByteArray> abstract_stub(ByteArray::Alloc(cs)); |
| 65 | CHECK(abstract_stub.get() != NULL); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 66 | MemoryRegion code(abstract_stub->GetData(), abstract_stub->GetLength()); |
| 67 | assembler->FinalizeInstructions(code); |
| 68 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 69 | return abstract_stub.get(); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 72 | ByteArray* CreateJniDlsymLookupStub() { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 73 | UniquePtr<X86Assembler> assembler(static_cast<X86Assembler*>(Assembler::Create(kX86))); |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 74 | |
| 75 | // Pad stack to ensure 16-byte alignment |
| 76 | __ pushl(Immediate(0)); |
| 77 | __ pushl(Immediate(0)); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 78 | __ fs()->pushl(Address::Absolute(Thread::SelfOffset())); // Thread* |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 79 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 80 | __ Call(ThreadOffset(OFFSETOF_MEMBER(Thread, pFindNativeMethod)), |
| 81 | X86ManagedRegister::FromCpuRegister(ECX)); |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 82 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 83 | __ addl(ESP, Immediate(12)); |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 84 | |
| 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 Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 97 | SirtRef<ByteArray> jni_stub(ByteArray::Alloc(cs)); |
| 98 | CHECK(jni_stub.get() != NULL); |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 99 | MemoryRegion code(jni_stub->GetData(), jni_stub->GetLength()); |
| 100 | assembler->FinalizeInstructions(code); |
| 101 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 102 | return jni_stub.get(); |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | } // namespace x86 |
| 106 | } // namespace art |