OSDN Git Service

Check target endianness at run time, not compile time
[pf3gnuchains/gcc-fork.git] / gcc / bi-run.h
1 /* Definitions for Bytecode Interpreter.
2    Copyright (C) 1993, 1994 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #define MAXLITERALS 5
21
22 struct arityvec
23 {
24   char ninputs;
25   char noutputs;
26   char nliterals;
27   char literals[MAXLITERALS];
28 };
29
30 struct argtype
31 {
32   int modealign;                /* Argument mode:alignment */
33   int size;                     /* Argument size, in bytes */
34 };
35
36 struct callinfo
37 {
38   int nargs;                    /* Number of arguments in call */
39   struct argtype retvaltype;    /* Type of return value */
40   struct argtype argtypes[1];   /* Argument types */
41 };
42   
43 /* Structure describing a bytecode function.  If this changes, we also
44    need to change expand_function_end () in bc-trans.c  */
45 struct bytecode
46 {
47   int stacksize;                /* Depth required of evaluation stack.  */
48   int localsize;                /* Size in bytes of local variables.  */
49   unsigned char *pc0;           /* Initial program counter. */
50   void **ptrlit;                /* Vector of (relocatable) pointer literals. */
51   struct callinfo *callinfo;    /* Vector of procedure call type info. */
52 };
53
54
55 #define INTERP_BPC 8            /* Bits per char */
56 #define INTERP_BPI \
57   (sizeof (int) * INTERP_BPC)   /* Bits per int */
58
59
60 #ifndef min
61 #define min(L, R)  ((L) < (R) ? (L) : (R))
62 #endif
63
64
65 /* bit field operations. */
66
67 /* Low (high) mask: int with low (high) N bits set */
68
69 #define LM(N)   ((1 << (N)) - 1)
70 #define HM(N)   ((~LM (INTERP_BPI - (N))))
71
72
73 /* Sign-extend SIZE low bits of VALUE to integer (typeof VALUE)
74    Signed bitfields are loaded from memory by the sxloadBI instruction,
75    which first retrieves the bitfield with XFIELD and then sign extends
76    it to an SItype. */
77
78 #define EXTEND(SIZE, VALUE)                                                   \
79   ({ SUtype value = (SUtype) (VALUE);                                         \
80     (value & (1 << ((SIZE) - 1)) ? value | ~LM (SIZE) : value); })
81
82
83 /* Given OFFSET:SIZE for  a bitfield, calculate:
84
85    [1] BYTE_OFFSET  = the byte offset of the bit field.
86    [2] BIT_OFFSET   = the bit offset of the bit field (less than INTERP_BPC).
87    [3] NBYTES       = the number of integral bytes in the bit field.
88    [4] TRAILING_BITS= the number of trailing bits (less than INTERP_BPC).
89
90
91    ,        ,        ,        ,        ,    (memory bytes)
92                     ----------------        (bitfield)
93    |        |       ||        |    |        (divisions)
94         ^         ^       ^      ^
95         |         |       |      |__ [4]  (bits)
96         |         |       |_________ [3]  (bytes)
97         |         |_________________ [2]  (bits)
98         |___________________________ [1]  (bytes)
99
100
101    The above applies to BYTE_LOW_ENDIAN machines. In BYTE_BIG_ENDIAN machines, the
102    bit numbering is reversed (i.e. bit 0 is the sign bit).
103
104    (Alright, so I drew this to keep my tongue in cheek while writing the code below,
105     not because I'm into ASCII art.) */
106
107
108 #define BI_PARAMS(OFFSET, SIZE, BYTE_OFFSET, BIT_OFFSET, NBYTES, TRAILING_BITS)         \
109   { BYTE_OFFSET = (OFFSET) / (INTERP_BPC);                              \
110     BIT_OFFSET = (OFFSET) % (INTERP_BPC);                               \
111     NBYTES = ((SIZE) - (INTERP_BPC - (BIT_OFFSET))) / INTERP_BPC;       \
112     if ((NBYTES) < 0 || ((NBYTES) > 64))                                \
113       NBYTES = 0;                                                       \
114     if ((SIZE) + (BIT_OFFSET) <= INTERP_BPC)                            \
115       TRAILING_BITS = 0;                                                \
116     else                                                                \
117       TRAILING_BITS = ((SIZE) - (INTERP_BPC - (BIT_OFFSET))) % INTERP_BPC; }
118
119
120 /* SHIFT_IN_BITS retrieves NBITS bits from SOURCE and shifts into
121    DEST. The bit field starts OFFSET bits into SOURCE.
122
123    OR_IN_BITS copies the NBITS low bits from VALUE into a the bitfield in
124    DEST offset by OFFSET bits. */
125
126
127 #define SHIFT_IN_BITS(DEST, SOURCE, OFFSET, NBITS)              \
128   (DEST = ((DEST) << (NBITS))                                   \
129    | (LM ((NBITS))                                              \
130       & ((SOURCE)                                               \
131          >> (BYTES_BIG_ENDIAN                                   \
132              ? (INTERP_BPC - (OFFSET) - (NBITS))                \
133              : (OFFSET)))))
134
135 #define OR_IN_BITS(DEST, VALUE, OFFSET, NBITS)                  \
136   (DEST = ((DEST) & ~(LM ((NBITS))                              \
137                       << (BIG_ENDIAN                            \
138                           ? (INTERP_BPC - (OFFSET) - (NBITS))   \
139                           : (OFFSET)))                          \
140    | (((VALUE) & LM ((NBITS)))                                  \
141       << (BIG_ENDIAN                                            \
142           ? (INTERP_BPC - (OFFSET) - (NBITS))                   \
143           : (OFFSET)))))
144
145 /* Procedure call; arguments are a pointer to the function to be called,
146    a pointer to a place to store the return value, a pointer to a vector
147    describing the type of procedure call, and the interpreter's stack pointer,
148    which will point to the first of the arguments at this point.  */
149
150 #define CALL(FUNC, CALLDESC, RETVAL, SP) __call(FUNC, CALLDESC, RETVAL, SP)
151
152
153 /* Procedure return; arguments are a pointer to the calldesc for this
154    function, and a pointer to the place where the value to be returned
155    may be found.  Generally the MACHARGS above contain a machine dependent
156    cookie that is used to determine where to jump to.  */
157
158 #define PROCRET(CALLDESC, RETVAL) return