blob: 69568d7a1949fa19f2ea6045242f9d20207312a1 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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 */
16
17#include "stubs/stubs.h"
18
19#include "jni_internal.h"
20#include "oat/utils/arm/assembler_arm.h"
21#include "oat/utils/mips/assembler_mips.h"
22#include "oat/utils/x86/assembler_x86.h"
23#include "oat/runtime/oat_support_entrypoints.h"
24#include "stack_indirect_reference_table.h"
25#include "sirt_ref.h"
26
27#define __ assembler->
28
29namespace art {
30
31namespace arm {
32const std::vector<uint8_t>* CreatePortableResolutionTrampoline() {
33 UniquePtr<ArmAssembler> assembler(static_cast<ArmAssembler*>(Assembler::Create(kArm)));
34 RegList save = (1 << R0) | (1 << R1) | (1 << R2) | (1 << R3) | (1 << LR);
35
36 __ PushList(save);
37 __ LoadFromOffset(kLoadWord, R12, TR, ENTRYPOINT_OFFSET(pPortableResolutionTrampolineFromCode));
38 __ mov(R3, ShifterOperand(TR)); // Pass Thread::Current() in R3
39 __ mov(R2, ShifterOperand(SP)); // Pass sp for Method** callee_addr
40 __ IncreaseFrameSize(12); // 3 words of space for alignment
41 // Call to resolution trampoline (callee, receiver, callee_addr, Thread*)
42 __ blx(R12);
43 __ mov(R12, ShifterOperand(R0)); // Save code address returned into R12
44 __ DecreaseFrameSize(12);
45 __ PopList(save);
46 __ cmp(R12, ShifterOperand(0));
47 __ bx(R12, NE); // If R12 != 0 tail call method's code
48 __ bx(LR); // Return to caller to handle exception
49
50 assembler->EmitSlowPaths();
51 size_t cs = assembler->CodeSize();
52 UniquePtr<std::vector<uint8_t> > resolution_trampoline(new std::vector<uint8_t>(cs));
53 MemoryRegion code(&(*resolution_trampoline)[0], resolution_trampoline->size());
54 assembler->FinalizeInstructions(code);
55
56 return resolution_trampoline.release();
57}
Brian Carlstrom7934ac22013-07-26 10:54:15 -070058} // namespace arm
Brian Carlstrom7940e442013-07-12 13:46:57 -070059
60namespace mips {
61const std::vector<uint8_t>* CreatePortableResolutionTrampoline() {
62 UniquePtr<MipsAssembler> assembler(static_cast<MipsAssembler*>(Assembler::Create(kMips)));
63 // Build frame and save argument registers and RA.
64 __ AddConstant(SP, SP, -32);
65 __ StoreToOffset(kStoreWord, RA, SP, 28);
66 __ StoreToOffset(kStoreWord, A3, SP, 12);
67 __ StoreToOffset(kStoreWord, A2, SP, 8);
68 __ StoreToOffset(kStoreWord, A1, SP, 4);
69 __ StoreToOffset(kStoreWord, A0, SP, 0);
70
71 __ LoadFromOffset(kLoadWord, T9, S1,
72 ENTRYPOINT_OFFSET(pPortableResolutionTrampolineFromCode));
73 __ Move(A3, S1); // Pass Thread::Current() in A3
74 __ Move(A2, SP); // Pass SP for Method** callee_addr
Brian Carlstrom7934ac22013-07-26 10:54:15 -070075 __ Jalr(T9); // Call to resolution trampoline (callee, receiver, callee_addr, Thread*)
Brian Carlstrom7940e442013-07-12 13:46:57 -070076
77 // Restore frame, argument registers, and RA.
78 __ LoadFromOffset(kLoadWord, A0, SP, 0);
79 __ LoadFromOffset(kLoadWord, A1, SP, 4);
80 __ LoadFromOffset(kLoadWord, A2, SP, 8);
81 __ LoadFromOffset(kLoadWord, A3, SP, 12);
82 __ LoadFromOffset(kLoadWord, RA, SP, 28);
83 __ AddConstant(SP, SP, 32);
84
85 Label resolve_fail;
86 __ EmitBranch(V0, ZERO, &resolve_fail, true);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070087 __ Jr(V0); // If V0 != 0 tail call method's code
Brian Carlstrom7940e442013-07-12 13:46:57 -070088 __ Bind(&resolve_fail, false);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070089 __ Jr(RA); // Return to caller to handle exception
Brian Carlstrom7940e442013-07-12 13:46:57 -070090
91 assembler->EmitSlowPaths();
92 size_t cs = assembler->CodeSize();
93 UniquePtr<std::vector<uint8_t> > resolution_trampoline(new std::vector<uint8_t>(cs));
94 MemoryRegion code(&(*resolution_trampoline)[0], resolution_trampoline->size());
95 assembler->FinalizeInstructions(code);
96
97 return resolution_trampoline.release();
98}
Brian Carlstrom7934ac22013-07-26 10:54:15 -070099} // namespace mips
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100
101namespace x86 {
102const std::vector<uint8_t>* CreatePortableResolutionTrampoline() {
103 UniquePtr<X86Assembler> assembler(static_cast<X86Assembler*>(Assembler::Create(kX86)));
104
105 __ pushl(EBP);
106 __ movl(EBP, ESP); // save ESP
107 __ subl(ESP, Immediate(8)); // Align stack
108 __ movl(EAX, Address(EBP, 8)); // Method* called
109 __ leal(EDX, Address(EBP, 8)); // Method** called_addr
110 __ fs()->pushl(Address::Absolute(Thread::SelfOffset())); // pass thread
111 __ pushl(EDX); // pass called_addr
112 __ pushl(ECX); // pass receiver
113 __ pushl(EAX); // pass called
114 // Call to resolve method.
115 __ Call(ThreadOffset(ENTRYPOINT_OFFSET(pPortableResolutionTrampolineFromCode)),
116 X86ManagedRegister::FromCpuRegister(ECX));
117 __ leave();
118
119 Label resolve_fail; // forward declaration
120 __ cmpl(EAX, Immediate(0));
121 __ j(kEqual, &resolve_fail);
122 __ jmp(EAX);
123 // Tail call to intended method.
124 __ Bind(&resolve_fail);
125 __ ret();
126
127 assembler->EmitSlowPaths();
128 size_t cs = assembler->CodeSize();
129 UniquePtr<std::vector<uint8_t> > resolution_trampoline(new std::vector<uint8_t>(cs));
130 MemoryRegion code(&(*resolution_trampoline)[0], resolution_trampoline->size());
131 assembler->FinalizeInstructions(code);
132
133 return resolution_trampoline.release();
134}
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700135} // namespace x86
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700137} // namespace art