1 /*
2  * Copyright (C) 2014 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 "art_method.h"
18 #include "base/utils.h"  // For RoundUp().
19 
20 namespace art HIDDEN {
21 
22 // Assembly stub that does the final part of the up-call into Java.
23 extern "C" void art_quick_invoke_stub_internal(ArtMethod*, uint32_t*, uint32_t,
24                                                Thread* self, JValue* result, uint32_t, uint32_t*,
25                                                uint32_t*);
26 
27 template <bool kIsStatic>
28 NO_STACK_PROTECTOR
quick_invoke_reg_setup(ArtMethod * method,uint32_t * args,uint32_t args_size,Thread * self,JValue * result,const char * shorty)29 static void quick_invoke_reg_setup(ArtMethod* method, uint32_t* args, uint32_t args_size,
30                                    Thread* self, JValue* result, const char* shorty) {
31   // Note: We do not follow aapcs ABI in quick code for both softfp and hardfp.
32   uint32_t core_reg_args[4];  // r0 ~ r3
33   uint32_t fp_reg_args[16];  // s0 ~ s15 (d0 ~ d7)
34   uint32_t gpr_index = 1;  // Index into core registers. Reserve r0 for ArtMethod*.
35   uint32_t fpr_index = 0;  // Index into float registers.
36   uint32_t fpr_double_index = 0;  // Index into float registers for doubles.
37   uint32_t arg_index = 0;  // Index into argument array.
38   const uint32_t result_in_float = (shorty[0] == 'F' || shorty[0] == 'D') ? 1 : 0;
39 
40   if (!kIsStatic) {
41     // Copy receiver for non-static methods.
42     core_reg_args[gpr_index++] = args[arg_index++];
43   }
44 
45   for (uint32_t shorty_index = 1; shorty[shorty_index] != '\0'; ++shorty_index, ++arg_index) {
46     char arg_type = shorty[shorty_index];
47     switch (arg_type) {
48       case 'D': {
49         // Copy double argument into fp_reg_args if there are still floating point reg arguments.
50         // Double should not overlap with float.
51         fpr_double_index = std::max(fpr_double_index, RoundUp(fpr_index, 2));
52         if (fpr_double_index < arraysize(fp_reg_args)) {
53           fp_reg_args[fpr_double_index++] = args[arg_index];
54           fp_reg_args[fpr_double_index++] = args[arg_index + 1];
55         }
56         ++arg_index;
57         break;
58       }
59       case 'F':
60         // Copy float argument into fp_reg_args if there are still floating point reg arguments.
61         // If fpr_index is odd then its pointing at a hole next to an existing float argument. If we
62         // encounter a float argument then pick it up from that hole. In the case fpr_index is even,
63         // ensure that we don't pick up an argument that overlaps with with a double from
64         // fpr_double_index. In either case, take care not to go beyond the maximum number of
65         // floating point arguments.
66         if (fpr_index % 2 == 0) {
67           fpr_index = std::max(fpr_double_index, fpr_index);
68         }
69         if (fpr_index < arraysize(fp_reg_args)) {
70           fp_reg_args[fpr_index++] = args[arg_index];
71         }
72         break;
73       case 'J':
74         if (gpr_index == 1) {
75           // Don't use r1-r2 as a register pair, move to r2-r3 instead.
76           gpr_index++;
77         }
78         if (gpr_index < arraysize(core_reg_args)) {
79           // Note that we don't need to do this if two registers are not available
80           // when using hard-fp. We do it anyway to leave this
81           // code simple.
82           core_reg_args[gpr_index++] = args[arg_index];
83         }
84         ++arg_index;
85         FALLTHROUGH_INTENDED;  // Fall-through to take of the high part.
86       default:
87         if (gpr_index < arraysize(core_reg_args)) {
88           core_reg_args[gpr_index++] = args[arg_index];
89         }
90         break;
91     }
92   }
93 
94   art_quick_invoke_stub_internal(method, args, args_size, self, result, result_in_float,
95       core_reg_args, fp_reg_args);
96 }
97 
98 // Called by art::ArtMethod::Invoke to do entry into a non-static method.
99 // TODO: migrate into an assembly implementation as with ARM64.
100 NO_STACK_PROTECTOR
art_quick_invoke_stub(ArtMethod * method,uint32_t * args,uint32_t args_size,Thread * self,JValue * result,const char * shorty)101 extern "C" void art_quick_invoke_stub(ArtMethod* method, uint32_t* args, uint32_t args_size,
102                                       Thread* self, JValue* result, const char* shorty) {
103   quick_invoke_reg_setup<false>(method, args, args_size, self, result, shorty);
104 }
105 
106 // Called by art::ArtMethod::Invoke to do entry into a static method.
107 // TODO: migrate into an assembly implementation as with ARM64.
108 NO_STACK_PROTECTOR
art_quick_invoke_static_stub(ArtMethod * method,uint32_t * args,uint32_t args_size,Thread * self,JValue * result,const char * shorty)109 extern "C" void art_quick_invoke_static_stub(ArtMethod* method, uint32_t* args,
110                                              uint32_t args_size, Thread* self, JValue* result,
111                                              const char* shorty) {
112   quick_invoke_reg_setup<true>(method, args, args_size, self, result, shorty);
113 }
114 
115 }  // namespace art
116