1/*
2 * Copyright (C) 2008 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 * Target-specific optimization and run-time hints
18 */
19
20
21#include "Dalvik.h"
22#include "libdex/DexClass.h"
23
24#include <stdlib.h>
25#include <stddef.h>
26#include <sys/stat.h>
27
28
29/*
30 * The class loader will associate with each method a 32-bit info word
31 * (jniArgInfo) to support JNI calls.  The high order 4 bits of this word
32 * are the same for all targets, while the lower 28 are used for hints to
33 * allow accelerated JNI bridge transfers.
34 *
35 * jniArgInfo (32-bit int) layout:
36 *
37 *    SRRRHHHH HHHHHHHH HHHHHHHH HHHHHHHH
38 *
39 *    S - if set, ignore the hints and do things the hard way (scan signature)
40 *    R - return-type enumeration
41 *    H - target-specific hints (see below for details)
42 *
43 * This function produces x86-specific hints for the standard 32-bit 386 ABI.
44 * Note that the JNI requirements are very close to the 386 runtime model.  In
45 * particular, natural datatype alignments do not apply to passed arguments.
46 * All arguments have 32-bit alignment.  As a result, we don't have to worry
47 * about padding - just total size.  The only tricky bit is that floating point
48 * return values come back on the FP stack.
49 *
50 *
51 * 386 ABI JNI hint format
52 *
53 *       ZZZZ ZZZZZZZZ AAAAAAAA AAAAAAAA
54 *
55 *   Z - reserved, must be 0
56 *   A - size of variable argument block in 32-bit words (note - does not
57 *       include JNIEnv or clazz)
58 *
59 * For the 386 ABI, valid hints should always be generated.
60 */
61u4 dvmPlatformInvokeHints( const DexProto* proto)
62{
63    const char* sig = dexProtoGetShorty(proto);
64    unsigned int jniHints, wordCount;
65    char sigByte;
66
67    wordCount = 0;
68    while (true) {
69        sigByte = *(sig++);
70
71        if (sigByte == '\0')
72            break;
73
74        wordCount++;
75
76        if (sigByte == 'D' || sigByte == 'J') {
77            wordCount++;
78        }
79    }
80
81    if (wordCount > 0xFFFF) {
82        /* Invalid - Dex file limitation */
83        jniHints = DALVIK_JNI_NO_ARG_INFO;
84    } else {
85        jniHints = wordCount;
86    }
87
88    return jniHints;
89}
90