blob: ae9acd6275925d63ede08dce40cd313a7fdd06e2 [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
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -070018 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -070019 // | R3 | possible argument
20 // | R2 | possible argument
21 // | R1 | possible argument
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -070022 // | R0 | junk on call to UnresolvedDirectMethodTrampolineFromCode, holds result Method*
23 // | Method* | Callee save Method* set up by UnresolvedDirectMethodTrampolineFromCode
24 // Save callee saves and ready frame for exception delivery
25 RegList save = (1 << R1) | (1 << R2) | (1 << R3) | (1 << R5) | (1 << R6) | (1 << R7) | (1 << R8) |
26 (1 << R10) | (1 << R11) | (1 << LR);
27 // TODO: enable when GetCalleeSaveMethod is available at stub generation time
28 // DCHECK_EQ(save, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetCoreSpillMask());
Ian Rogersad25ac52011-10-04 19:13:33 -070029 __ PushList(save);
Ian Rogersad25ac52011-10-04 19:13:33 -070030 __ LoadFromOffset(kLoadWord, R12, TR,
31 OFFSETOF_MEMBER(Thread, pUnresolvedDirectMethodTrampolineFromCode));
32 __ mov(R2, ShifterOperand(TR)); // Pass Thread::Current() in R2
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070033 __ LoadImmediate(R3, type);
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -070034 __ IncreaseFrameSize(8); // 2 words of space for alignment
35 __ mov(R1, ShifterOperand(SP)); // Pass SP
Ian Rogersad25ac52011-10-04 19:13:33 -070036 // Call to unresolved direct method trampoline (method_idx, sp, Thread*, is_static)
37 __ blx(R12);
Ian Rogers4f0d07c2011-10-06 23:38:47 -070038 __ mov(R12, ShifterOperand(R0)); // Save code address returned into R12
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -070039 // Restore registers which may have been modified by GC, "R0" will hold the Method*
40 __ DecreaseFrameSize(4);
41 __ PopList((1 << R0) | save);
Ian Rogersae675992011-10-09 17:10:22 -070042 __ bx(R12); // Leaf call to method's code
Ian Rogersad25ac52011-10-04 19:13:33 -070043
44 __ bkpt(0);
45
46 assembler->EmitSlowPaths();
47 size_t cs = assembler->CodeSize();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070048 SirtRef<ByteArray> resolution_trampoline(ByteArray::Alloc(cs));
49 CHECK(resolution_trampoline.get() != NULL);
Ian Rogersad25ac52011-10-04 19:13:33 -070050 MemoryRegion code(resolution_trampoline->GetData(), resolution_trampoline->GetLength());
51 assembler->FinalizeInstructions(code);
52
Brian Carlstrom40381fb2011-10-19 14:13:40 -070053 return resolution_trampoline.get();
Ian Rogersad25ac52011-10-04 19:13:33 -070054}
55
Shih-wei Liaoc486c112011-09-13 16:43:52 -070056typedef void (*ThrowAme)(Method*, Thread*);
57
Ian Rogersbdb03912011-09-14 00:55:44 -070058ByteArray* CreateAbstractMethodErrorStub() {
Elliott Hughes362f9bc2011-10-17 18:56:41 -070059 UniquePtr<ArmAssembler> assembler(static_cast<ArmAssembler*>(Assembler::Create(kArm)));
Ian Rogersff1ed472011-09-20 13:46:24 -070060 // Save callee saves and ready frame for exception delivery
Ian Rogers4f0d07c2011-10-06 23:38:47 -070061 RegList save = (1 << R4) | (1 << R5) | (1 << R6) | (1 << R7) | (1 << R8) | (1 << R9) |
62 (1 << R10) | (1 << R11) | (1 << LR);
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -070063 // TODO: enable when GetCalleeSaveMethod is available at stub generation time
64 // DCHECK_EQ(save, Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)->GetCoreSpillMask());
Ian Rogers4f0d07c2011-10-06 23:38:47 -070065 __ PushList(save); // push {r4-r11, lr} - 9 words of callee saves
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -070066 // TODO: enable when GetCalleeSaveMethod is available at stub generation time
67 // DCHECK_EQ(Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveAll)->GetFpSpillMask(), 0xFFFFU);
Ian Rogers4f0d07c2011-10-06 23:38:47 -070068 __ Emit(0xed2d0a20); // vpush {s0-s31}
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -070069
Ian Rogers4f0d07c2011-10-06 23:38:47 -070070 __ IncreaseFrameSize(12); // 3 words of space, bottom word will hold callee save Method*
Shih-wei Liaoc486c112011-09-13 16:43:52 -070071
Ian Rogersff1ed472011-09-20 13:46:24 -070072 // R0 is the Method* already
73 __ mov(R1, ShifterOperand(R9)); // Pass Thread::Current() in R1
74 __ mov(R2, ShifterOperand(SP)); // Pass SP in R2
Ian Rogersbdb03912011-09-14 00:55:44 -070075 // Call to throw AbstractMethodError
76 __ LoadFromOffset(kLoadWord, R12, TR, OFFSETOF_MEMBER(Thread, pThrowAbstractMethodErrorFromCode));
Ian Rogers4f0d07c2011-10-06 23:38:47 -070077 __ mov(PC, ShifterOperand(R12)); // Leaf call to routine that never returns
Shih-wei Liaoc486c112011-09-13 16:43:52 -070078
79 __ bkpt(0);
80
81 assembler->EmitSlowPaths();
82
83 size_t cs = assembler->CodeSize();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070084 SirtRef<ByteArray> abstract_stub(ByteArray::Alloc(cs));
85 CHECK(abstract_stub.get() != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -070086 CHECK(abstract_stub->GetClass()->GetDescriptor());
Shih-wei Liaoc486c112011-09-13 16:43:52 -070087 MemoryRegion code(abstract_stub->GetData(), abstract_stub->GetLength());
88 assembler->FinalizeInstructions(code);
89
Brian Carlstrom40381fb2011-10-19 14:13:40 -070090 return abstract_stub.get();
Shih-wei Liaoc486c112011-09-13 16:43:52 -070091}
92
Shih-wei Liao31384c52011-09-06 15:27:45 -070093ByteArray* CreateJniStub() {
Elliott Hughes362f9bc2011-10-17 18:56:41 -070094 UniquePtr<ArmAssembler> assembler(static_cast<ArmAssembler*>(Assembler::Create(kArm)));
Ian Rogers4f0d07c2011-10-06 23:38:47 -070095 // Build frame and save argument registers and LR.
Shih-wei Liao31384c52011-09-06 15:27:45 -070096 RegList save = (1 << R0) | (1 << R1) | (1 << R2) | (1 << R3) | (1 << LR);
Shih-wei Liao31384c52011-09-06 15:27:45 -070097 __ PushList(save);
Ian Rogers4f0d07c2011-10-06 23:38:47 -070098 __ AddConstant(SP, -12); // Ensure 16-byte alignment
99 __ mov(R0, ShifterOperand(R9)); // Pass Thread::Current() in R0
Shih-wei Liao31384c52011-09-06 15:27:45 -0700100 // Call FindNativeMethod
Brian Carlstrom16192862011-09-12 17:50:06 -0700101 __ LoadFromOffset(kLoadWord, R12, TR, OFFSETOF_MEMBER(Thread, pFindNativeMethod));
Shih-wei Liao31384c52011-09-06 15:27:45 -0700102 __ blx(R12);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700103 __ mov(R12, ShifterOperand(R0)); // Save result of FindNativeMethod in R12
Ian Rogersae675992011-10-09 17:10:22 -0700104 __ AddConstant(SP, 12); // Restore registers (including outgoing arguments)
Shih-wei Liao31384c52011-09-06 15:27:45 -0700105 __ PopList(save);
Shih-wei Liao31384c52011-09-06 15:27:45 -0700106 __ cmp(R12, ShifterOperand(0));
Ian Rogersae675992011-10-09 17:10:22 -0700107 __ bx(R12, NE); // If R12 != 0 tail call into native code
108 __ bx(LR); // Return to caller to handle exception
Shih-wei Liao31384c52011-09-06 15:27:45 -0700109
110 assembler->EmitSlowPaths();
111
112 size_t cs = assembler->CodeSize();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700113 SirtRef<ByteArray> jni_stub(ByteArray::Alloc(cs));
114 CHECK(jni_stub.get() != NULL);
Shih-wei Liao31384c52011-09-06 15:27:45 -0700115 MemoryRegion code(jni_stub->GetData(), jni_stub->GetLength());
116 assembler->FinalizeInstructions(code);
117
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700118 return jni_stub.get();
Shih-wei Liao31384c52011-09-06 15:27:45 -0700119}
120
121} // namespace arm
122} // namespace art