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