1/* Utility macros to handle Java(TM) byte codes.
2
3   Copyright (C) 1996-2015 Free Software Foundation, Inc.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3.  If not see
19<http://www.gnu.org/licenses/>.
20
21Java and all Java-based marks are trademarks or registered trademarks
22of Sun Microsystems, Inc. in the United States and other countries.
23The Free Software Foundation is independent of Sun Microsystems, Inc.  */
24
25/* Written by Per Bothner <bothner@cygnus.com>, February 1996. */
26
27#ifndef GCC_JAVAOP_H
28#define GCC_JAVAOP_H
29
30typedef	unsigned char	uint8;
31#ifndef int16
32#if __SHRT_MAX__ == 32767
33#define int16 short
34#elif __INT_MAX__ == 32767
35#define int16 int
36#elif __LONG_MAX__ == 32767
37#define int16 long
38#else
39#define int16 short
40#endif
41#endif
42typedef unsigned int16	uint16;
43
44#ifndef int32
45#if __INT_MAX__ == 2147483647
46#define int32 int
47#elif __LONG_MAX__ == 2147483647
48#define int32 long
49#elif __SHRT_MAX__ == 2147483647
50#define int32 short
51#else
52#define int32 int
53#endif
54#endif
55typedef unsigned int32	uint32;
56
57/* A signed 64-bit (or more) integral type, suitable for Java's 'long'.  */
58#ifndef int64
59#if __LONG_MAX__ == 9223372036854775807LL
60#define int64 long
61#elif __LONG_LONG_MAX__ == 9223372036854775807LL
62#define int64 long long
63#else
64#define int64 long long
65#endif
66#endif
67/* An unsigned 64-bit (or more) integral type, same length as int64. */
68#ifndef uint64
69#define uint64 unsigned int64
70#endif
71
72typedef uint16			jchar;
73typedef	signed char		jbyte;
74typedef int16                   jshort;
75typedef int32                   jint;
76typedef int64                   jlong;
77typedef void*                   jref;
78
79/* A 32-bit big-endian IEEE single-precision float. */
80typedef struct _jfloat {
81  unsigned int negative : 1;
82  unsigned int exponent : 8;
83  unsigned int mantissa : 23;
84} jfloat;
85#define JFLOAT_FINITE(f) ((f).exponent != 0xFF)
86#define JFLOAT_QNAN_MASK 0x400000
87#define JFLOAT_EXP_BIAS 0x7f
88
89/* A 32-bit big-endian IEEE double-precision float. */
90typedef struct _jdouble {
91  unsigned int negative : 1;
92  unsigned int exponent : 11;
93  unsigned int mantissa0: 20;
94  unsigned int mantissa1: 32;
95} jdouble;
96#define JDOUBLE_FINITE(f) ((f).exponent != 0x7FF)
97#define JDOUBLE_QNAN_MASK 0x80000  /* apply to mantissa0 */
98#define JDOUBLE_EXP_BIAS 0x3ff
99
100/* A jword is an unsigned integral type big enough for a 32-bit jint
101   or jfloat *or* a pointer.  It is the type appropriate for stack
102   locations and local variables in a Java interpreter. */
103
104
105#ifndef jword
106#if defined (__LP64__) || defined (__alpha__) || defined (__MMIX__) \
107    || (defined (_ARCH_PPC) && defined (__64BIT__)) \
108    || defined (__powerpc64__) || defined (__s390x__) || defined (__x86_64__) \
109    || defined (__sparcv9) || (defined (__sparc__) && defined (__arch64__))
110#define jword uint64
111#else
112#define jword uint32
113#endif
114#endif
115
116#ifndef IMMEDIATE_u1
117#define IMMEDIATE_u1 (PC++, CHECK_PC_IN_RANGE(PC), BCODE[PC-1])
118#endif
119#ifndef IMMEDIATE_s1
120#define IMMEDIATE_s1 (PC++, CHECK_PC_IN_RANGE(PC), (signed char)BCODE[PC-1])
121#endif
122#ifndef IMMEDIATE_s2
123#define IMMEDIATE_s2 (PC+=2, CHECK_PC_IN_RANGE(PC), \
124  (signed char) BCODE[PC-2] * 256 + BCODE[PC-1])
125#endif
126#ifndef IMMEDIATE_u2
127#define IMMEDIATE_u2 (PC+=2, CHECK_PC_IN_RANGE(PC),\
128  (BCODE[PC-2] * 256 + BCODE[PC-1]))
129#endif
130#ifndef IMMEDIATE_s4
131#define IMMEDIATE_s4 (PC+=4, CHECK_PC_IN_RANGE(PC), \
132  (WORD_TO_INT((BCODE[PC-4] << 24) | (BCODE[PC-3] << 16) \
133         | (BCODE[PC-2] << 8) | (BCODE[PC-1]))))
134#endif
135
136static inline jfloat
137WORD_TO_FLOAT(jword w)
138{
139  jfloat f;
140
141  f.negative = (w & 0x80000000) >> 31;
142  f.exponent = (w & 0x7f800000) >> 23;
143  f.mantissa = (w & 0x007fffff);
144
145  return f;
146}
147
148/* Sign extend w.  If the host on which this cross-compiler runs uses
149   a 64-bit type for jword the appropriate sign extension is
150   performed; if it's a 32-bit type the arithmetic does nothing but is
151   harmless.  */
152static inline jint
153WORD_TO_INT(jword w)
154{
155  jint n = w & 0xffffffff; /* Mask lower 32 bits.  */
156  n ^= (jint)1 << 31;
157  n -= (uint32)1 << 31; /* Sign extend lower 32 bits to upper.  */
158  return n;
159}
160
161static inline jlong
162WORDS_TO_LONG(jword hi, jword lo)
163{
164  return ((jlong) hi << 32) | ((jlong)lo & (((jlong)1 << 32) -1));
165}
166
167static inline jdouble
168WORDS_TO_DOUBLE(jword hi, jword lo)
169{
170  jdouble d;
171
172  d.negative  = (hi & 0x80000000) >> 31;
173  d.exponent  = (hi & 0x7ff00000) >> 20;
174  d.mantissa0 = (hi & 0x000fffff);
175  d.mantissa1 = lo;
176
177  return d;
178}
179
180/* If PREFIX_CHAR is the first character of the Utf8 encoding of a character,
181   return the number of bytes taken by the encoding.
182   Return -1 for a continuation character.  */
183#define UT8_CHAR_LENGTH(PREFIX_CHAR) \
184  ((unsigned char)(PREFIX_CHAR) < 128 ? 1 \
185   : ((PREFIX_CHAR) & 0x40) == 0 ? -1 \
186   : ((PREFIX_CHAR) & 0x20) == 0 ? 2 \
187   : ((PREFIX_CHAR) & 0x10) == 0 ? 3 \
188   : ((PREFIX_CHAR) & 0x08) == 0 ? 4 : 5)
189
190#endif /* ! GCC_JAVAOP_H */
191