blob: 37687215ef9871e8b00f9f8b4988eef397c8d0bb [file] [log] [blame]
Shih-wei Liao31384c52011-09-06 15:27:45 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "assembler_arm.h"
4#include "jni_internal.h"
5#include "object.h"
Brian Carlstrom40381fb2011-10-19 14:13:40 -07006#include "stack_indirect_reference_table.h"
Shih-wei Liao31384c52011-09-06 15:27:45 -07007
8#define __ assembler->
9
10namespace art {
11namespace arm {
12
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070013ByteArray* ArmCreateResolutionTrampoline(Runtime::TrampolineType type) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -070014 UniquePtr<ArmAssembler> assembler(static_cast<ArmAssembler*>(Assembler::Create(kArm)));
Ian Rogersad25ac52011-10-04 19:13:33 -070015 // | Out args |
16 // | Method* | <- SP on entry
17 // | LR | return address into caller
18 // | R3 | possible argument
19 // | R2 | possible argument
20 // | R1 | possible argument
21 // | R0 | method index (loaded from code and method array - will be converted to Method*)
Ian Rogers4f0d07c2011-10-06 23:38:47 -070022 RegList save = (1 << R0) | (1 << R1) | (1 << R2) | (1 << R3) | (1 << LR);
Ian Rogersad25ac52011-10-04 19:13:33 -070023 __ PushList(save);
24 __ mov(R1, ShifterOperand(SP)); // Pass address of saved R0... in R1
25 __ LoadFromOffset(kLoadWord, R12, TR,
26 OFFSETOF_MEMBER(Thread, pUnresolvedDirectMethodTrampolineFromCode));
27 __ mov(R2, ShifterOperand(TR)); // Pass Thread::Current() in R2
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070028 __ LoadImmediate(R3, type);
Ian Rogersad25ac52011-10-04 19:13:33 -070029 __ IncreaseFrameSize(12); // 3 words of space for alignment
30 // Call to unresolved direct method trampoline (method_idx, sp, Thread*, is_static)
31 __ blx(R12);
Ian Rogers4f0d07c2011-10-06 23:38:47 -070032 __ mov(R12, ShifterOperand(R0)); // Save code address returned into R12
Ian Rogersad25ac52011-10-04 19:13:33 -070033 // Restore registers which may have been modified by GC and R0 which will now hold the method*
34 __ DecreaseFrameSize(12);
35 __ PopList(save);
Ian Rogersae675992011-10-09 17:10:22 -070036 __ bx(R12); // Leaf call to method's code
Ian Rogersad25ac52011-10-04 19:13:33 -070037
38 __ bkpt(0);
39
40 assembler->EmitSlowPaths();
41 size_t cs = assembler->CodeSize();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070042 SirtRef<ByteArray> resolution_trampoline(ByteArray::Alloc(cs));
43 CHECK(resolution_trampoline.get() != NULL);
Ian Rogersad25ac52011-10-04 19:13:33 -070044 MemoryRegion code(resolution_trampoline->GetData(), resolution_trampoline->GetLength());
45 assembler->FinalizeInstructions(code);
46
Brian Carlstrom40381fb2011-10-19 14:13:40 -070047 return resolution_trampoline.get();
Ian Rogersad25ac52011-10-04 19:13:33 -070048}
49
Shih-wei Liaoc486c112011-09-13 16:43:52 -070050typedef void (*ThrowAme)(Method*, Thread*);
51
Ian Rogersbdb03912011-09-14 00:55:44 -070052ByteArray* CreateAbstractMethodErrorStub() {
Elliott Hughes362f9bc2011-10-17 18:56:41 -070053 UniquePtr<ArmAssembler> assembler(static_cast<ArmAssembler*>(Assembler::Create(kArm)));
Ian Rogersff1ed472011-09-20 13:46:24 -070054 // Save callee saves and ready frame for exception delivery
Ian Rogers4f0d07c2011-10-06 23:38:47 -070055 RegList save = (1 << R4) | (1 << R5) | (1 << R6) | (1 << R7) | (1 << R8) | (1 << R9) |
56 (1 << R10) | (1 << R11) | (1 << LR);
57 __ PushList(save); // push {r4-r11, lr} - 9 words of callee saves
58 __ Emit(0xed2d0a20); // vpush {s0-s31}
59 __ IncreaseFrameSize(12); // 3 words of space, bottom word will hold callee save Method*
Shih-wei Liaoc486c112011-09-13 16:43:52 -070060
Ian Rogersff1ed472011-09-20 13:46:24 -070061 // R0 is the Method* already
62 __ mov(R1, ShifterOperand(R9)); // Pass Thread::Current() in R1
63 __ mov(R2, ShifterOperand(SP)); // Pass SP in R2
Ian Rogersbdb03912011-09-14 00:55:44 -070064 // Call to throw AbstractMethodError
65 __ LoadFromOffset(kLoadWord, R12, TR, OFFSETOF_MEMBER(Thread, pThrowAbstractMethodErrorFromCode));
Ian Rogers4f0d07c2011-10-06 23:38:47 -070066 __ mov(PC, ShifterOperand(R12)); // Leaf call to routine that never returns
Shih-wei Liaoc486c112011-09-13 16:43:52 -070067
68 __ bkpt(0);
69
70 assembler->EmitSlowPaths();
71
72 size_t cs = assembler->CodeSize();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070073 SirtRef<ByteArray> abstract_stub(ByteArray::Alloc(cs));
74 CHECK(abstract_stub.get() != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 CHECK(abstract_stub->GetClass()->GetDescriptor());
Shih-wei Liaoc486c112011-09-13 16:43:52 -070076 MemoryRegion code(abstract_stub->GetData(), abstract_stub->GetLength());
77 assembler->FinalizeInstructions(code);
78
Brian Carlstrom40381fb2011-10-19 14:13:40 -070079 return abstract_stub.get();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070080}
81
Shih-wei Liao31384c52011-09-06 15:27:45 -070082ByteArray* CreateJniStub() {
Elliott Hughes362f9bc2011-10-17 18:56:41 -070083 UniquePtr<ArmAssembler> assembler(static_cast<ArmAssembler*>(Assembler::Create(kArm)));
Ian Rogers4f0d07c2011-10-06 23:38:47 -070084 // Build frame and save argument registers and LR.
Shih-wei Liao31384c52011-09-06 15:27:45 -070085 RegList save = (1 << R0) | (1 << R1) | (1 << R2) | (1 << R3) | (1 << LR);
Shih-wei Liao31384c52011-09-06 15:27:45 -070086 __ PushList(save);
Ian Rogers4f0d07c2011-10-06 23:38:47 -070087 __ AddConstant(SP, -12); // Ensure 16-byte alignment
88 __ mov(R0, ShifterOperand(R9)); // Pass Thread::Current() in R0
Shih-wei Liao31384c52011-09-06 15:27:45 -070089 // Call FindNativeMethod
Brian Carlstrom16192862011-09-12 17:50:06 -070090 __ LoadFromOffset(kLoadWord, R12, TR, OFFSETOF_MEMBER(Thread, pFindNativeMethod));
Shih-wei Liao31384c52011-09-06 15:27:45 -070091 __ blx(R12);
Ian Rogers4f0d07c2011-10-06 23:38:47 -070092 __ mov(R12, ShifterOperand(R0)); // Save result of FindNativeMethod in R12
Ian Rogersae675992011-10-09 17:10:22 -070093 __ AddConstant(SP, 12); // Restore registers (including outgoing arguments)
Shih-wei Liao31384c52011-09-06 15:27:45 -070094 __ PopList(save);
Shih-wei Liao31384c52011-09-06 15:27:45 -070095 __ cmp(R12, ShifterOperand(0));
Ian Rogersae675992011-10-09 17:10:22 -070096 __ bx(R12, NE); // If R12 != 0 tail call into native code
97 __ bx(LR); // Return to caller to handle exception
Shih-wei Liao31384c52011-09-06 15:27:45 -070098
99 assembler->EmitSlowPaths();
100
101 size_t cs = assembler->CodeSize();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700102 SirtRef<ByteArray> jni_stub(ByteArray::Alloc(cs));
103 CHECK(jni_stub.get() != NULL);
Shih-wei Liao31384c52011-09-06 15:27:45 -0700104 MemoryRegion code(jni_stub->GetData(), jni_stub->GetLength());
105 assembler->FinalizeInstructions(code);
106
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700107 return jni_stub.get();
Shih-wei Liao31384c52011-09-06 15:27:45 -0700108}
109
110} // namespace arm
111} // namespace art