Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "assembler_x86.h" |
| 4 | #include "jni_internal.h" |
| 5 | #include "object.h" |
| 6 | |
| 7 | #define __ assembler-> |
| 8 | |
| 9 | namespace art { |
| 10 | namespace x86 { |
| 11 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 12 | ByteArray* X86CreateResolutionTrampoline(Runtime::TrampolineType) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame^] | 13 | UniquePtr<X86Assembler> assembler(static_cast<X86Assembler*>(Assembler::Create(kX86))); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 14 | |
| 15 | // TODO: unimplemented |
| 16 | __ int3(); |
| 17 | |
| 18 | assembler->EmitSlowPaths(); |
| 19 | size_t cs = assembler->CodeSize(); |
| 20 | ByteArray* resolution_trampoline = ByteArray::Alloc(cs); |
| 21 | CHECK(resolution_trampoline != NULL); |
| 22 | MemoryRegion code(resolution_trampoline->GetData(), resolution_trampoline->GetLength()); |
| 23 | assembler->FinalizeInstructions(code); |
| 24 | |
| 25 | return resolution_trampoline; |
| 26 | } |
| 27 | |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 28 | typedef void (*ThrowAme)(Method*, Thread*); |
| 29 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 30 | ByteArray* CreateAbstractMethodErrorStub() { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame^] | 31 | UniquePtr<X86Assembler> assembler(static_cast<X86Assembler*>(Assembler::Create(kX86))); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 32 | |
| 33 | // Pad stack to ensure 16-byte alignment |
| 34 | __ pushl(Immediate(0)); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 35 | __ fs()->pushl(Address::Absolute(Thread::SelfOffset())); // Thread* |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 36 | __ pushl(EAX); // Method* |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 37 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 38 | // Call to throw AbstractMethodError |
| 39 | __ Call(ThreadOffset(OFFSETOF_MEMBER(Thread, pThrowAbstractMethodErrorFromCode)), |
| 40 | X86ManagedRegister::FromCpuRegister(ECX)); |
Shih-wei Liao | c486c11 | 2011-09-13 16:43:52 -0700 | [diff] [blame] | 41 | |
| 42 | // Because the call above never returns, we do not need to do ESP+=16 here. |
| 43 | |
| 44 | __ int3(); |
| 45 | |
| 46 | assembler->EmitSlowPaths(); |
| 47 | |
| 48 | size_t cs = assembler->CodeSize(); |
| 49 | ByteArray* abstract_stub = ByteArray::Alloc(cs); |
| 50 | CHECK(abstract_stub != NULL); |
| 51 | MemoryRegion code(abstract_stub->GetData(), abstract_stub->GetLength()); |
| 52 | assembler->FinalizeInstructions(code); |
| 53 | |
| 54 | return abstract_stub; |
| 55 | } |
| 56 | |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 57 | ByteArray* CreateJniStub() { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame^] | 58 | UniquePtr<X86Assembler> assembler(static_cast<X86Assembler*>(Assembler::Create(kX86))); |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 59 | |
| 60 | // Pad stack to ensure 16-byte alignment |
| 61 | __ pushl(Immediate(0)); |
| 62 | __ pushl(Immediate(0)); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 63 | __ fs()->pushl(Address::Absolute(Thread::SelfOffset())); // Thread* |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 64 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 65 | __ Call(ThreadOffset(OFFSETOF_MEMBER(Thread, pFindNativeMethod)), |
| 66 | X86ManagedRegister::FromCpuRegister(ECX)); |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 67 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 68 | __ addl(ESP, Immediate(12)); |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 69 | |
| 70 | Label no_native_code_found; // forward declaration |
| 71 | __ cmpl(EAX, Immediate(0)); |
| 72 | __ j(kEqual, &no_native_code_found); |
| 73 | |
| 74 | __ jmp(EAX); // Tail call into native code |
| 75 | |
| 76 | __ Bind(&no_native_code_found); |
| 77 | __ ret(); // return to caller to handle exception |
| 78 | |
| 79 | assembler->EmitSlowPaths(); |
| 80 | |
| 81 | size_t cs = assembler->CodeSize(); |
| 82 | ByteArray* jni_stub = ByteArray::Alloc(cs); |
| 83 | CHECK(jni_stub != NULL); |
| 84 | MemoryRegion code(jni_stub->GetData(), jni_stub->GetLength()); |
| 85 | assembler->FinalizeInstructions(code); |
| 86 | |
| 87 | return jni_stub; |
| 88 | } |
| 89 | |
| 90 | } // namespace x86 |
| 91 | } // namespace art |