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