xref: /netbsd-src/external/gpl3/gdb/dist/sim/common/sim-bits.c (revision 0d3e0572e40d81edb4fdbff937458d47b685c34c)
1 /* The common simulator framework for GDB, the GNU Debugger.
2 
3    Copyright 2002-2024 Free Software Foundation, Inc.
4 
5    Contributed by Andrew Cagney and Red Hat.
6 
7    This file is part of GDB.
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21 
22 
23 #ifndef _SIM_BITS_C_
24 #define _SIM_BITS_C_
25 
26 /* This must come before any other includes.  */
27 #include "defs.h"
28 
29 #include "sim-basics.h"
30 #include "sim-assert.h"
31 #include "sim-io.h"
32 
33 
34 INLINE_SIM_BITS\
35 (unsigned_word)
36 LSMASKED (unsigned_word val,
37 	  int start,
38 	  int stop)
39 {
40   /* NOTE - start, stop can wrap */
41   val &= LSMASK (start, stop);
42   return val;
43 }
44 
45 
46 INLINE_SIM_BITS\
47 (unsigned_word)
48 MSMASKED (unsigned_word val,
49 	  int start,
50 	  int stop)
51 {
52   /* NOTE - start, stop can wrap */
53   val &= MSMASK (start, stop);
54   return val;
55 }
56 
57 
58 INLINE_SIM_BITS\
59 (unsigned_word)
60 LSEXTRACTED (unsigned_word val,
61 	     int start,
62 	     int stop)
63 {
64   ASSERT (start >= stop);
65 #if (WITH_TARGET_WORD_BITSIZE == 64)
66   return LSEXTRACTED64 (val, start, stop);
67 #endif
68 #if (WITH_TARGET_WORD_BITSIZE == 32)
69   if (stop >= 32)
70     return 0;
71   else
72     {
73       if (start < 32)
74 	val &= LSMASK (start, 0);
75       val >>= stop;
76       return val;
77     }
78 #endif
79 #if (WITH_TARGET_WORD_BITSIZE == 16)
80   if (stop >= 16)
81     return 0;
82   else
83     {
84       if (start < 16)
85 	val &= LSMASK (start, 0);
86       val >>= stop;
87       return val;
88     }
89 #endif
90 }
91 
92 
93 INLINE_SIM_BITS\
94 (unsigned_word)
95 MSEXTRACTED (unsigned_word val,
96 	     int start,
97 	     int stop)
98 {
99   ASSERT (start <= stop);
100 #if (WITH_TARGET_WORD_BITSIZE == 64)
101   return MSEXTRACTED64 (val, start, stop);
102 #endif
103 #if (WITH_TARGET_WORD_BITSIZE == 32)
104   if (stop < 32)
105     return 0;
106   else
107     {
108       if (start >= 32)
109 	val &= MSMASK (start, 64 - 1);
110       val >>= (64 - stop - 1);
111       return val;
112     }
113 #endif
114 #if (WITH_TARGET_WORD_BITSIZE == 16)
115   if (stop < 16)
116     return 0;
117   else
118     {
119       if (start >= 16)
120 	val &= MSMASK (start, 64 - 1);
121       val >>= (64 - stop - 1);
122       return val;
123     }
124 #endif
125 }
126 
127 
128 INLINE_SIM_BITS\
129 (unsigned_word)
130 LSINSERTED (unsigned_word val,
131 	    int start,
132 	    int stop)
133 {
134   ASSERT (start >= stop);
135 #if (WITH_TARGET_WORD_BITSIZE == 64)
136   return LSINSERTED64 (val, start, stop);
137 #endif
138 #if (WITH_TARGET_WORD_BITSIZE == 32)
139   /* Bit numbers are 63..0, even for 32 bit targets.
140      On 32 bit targets we ignore 63..32  */
141   if (stop >= 32)
142     return 0;
143   else
144     {
145       val <<= stop;
146       val &= LSMASK (start, stop);
147       return val;
148     }
149 #endif
150 #if (WITH_TARGET_WORD_BITSIZE == 16)
151   /* Bit numbers are 63..0, even for 16 bit targets.
152      On 16 bit targets we ignore 63..16  */
153   if (stop >= 16)
154     return 0;
155   else
156     {
157       val <<= stop;
158       val &= LSMASK (start, stop);
159       return val;
160     }
161 #endif
162 }
163 
164 INLINE_SIM_BITS\
165 (unsigned_word)
166 MSINSERTED (unsigned_word val,
167 	    int start,
168 	    int stop)
169 {
170   ASSERT (start <= stop);
171 #if (WITH_TARGET_WORD_BITSIZE == 64)
172   return MSINSERTED64 (val, start, stop);
173 #endif
174 #if (WITH_TARGET_WORD_BITSIZE == 32)
175   /* Bit numbers are 0..63, even for 32 bit targets.
176      On 32 bit targets we ignore 0..31.  */
177   if (stop < 32)
178     return 0;
179   else
180     {
181       val <<= ((64 - 1) - stop);
182       val &= MSMASK (start, stop);
183       return val;
184     }
185 #endif
186 #if (WITH_TARGET_WORD_BITSIZE == 16)
187   /* Bit numbers are 0..63, even for 16 bit targets.
188      On 16 bit targets we ignore 0..47.  */
189   if (stop < 32 + 16)
190     return 0;
191   else
192     {
193       val <<= ((64 - 1) - stop);
194       val &= MSMASK (start, stop);
195       return val;
196     }
197 #endif
198 }
199 
200 
201 
202 INLINE_SIM_BITS\
203 (unsigned_word)
204 LSSEXT (signed_word val,
205 	int sign_bit)
206 {
207   ASSERT (sign_bit < 64);
208 #if (WITH_TARGET_WORD_BITSIZE == 64)
209   return LSSEXT64 (val, sign_bit);
210 #endif
211 #if (WITH_TARGET_WORD_BITSIZE == 32)
212   if (sign_bit >= 32)
213     return val;
214   else {
215     val = LSSEXT32 (val, sign_bit);
216     return val;
217   }
218 #endif
219 #if (WITH_TARGET_WORD_BITSIZE == 16)
220   if (sign_bit >= 16)
221     return val;
222   else {
223     val = LSSEXT16 (val, sign_bit);
224     return val;
225   }
226 #endif
227 }
228 
229 INLINE_SIM_BITS\
230 (unsigned_word)
231 MSSEXT (signed_word val,
232 	int sign_bit)
233 {
234   ASSERT (sign_bit < 64);
235 #if (WITH_TARGET_WORD_BITSIZE == 64)
236   return MSSEXT64 (val, sign_bit);
237 #endif
238 #if (WITH_TARGET_WORD_BITSIZE == 32)
239   if (sign_bit < 32)
240     return val;
241   else {
242     val = MSSEXT32 (val, sign_bit - 32);
243     return val;
244   }
245 #endif
246 #if (WITH_TARGET_WORD_BITSIZE == 16)
247   if (sign_bit < 32 + 16)
248     return val;
249   else {
250     val = MSSEXT16 (val, sign_bit - 32 - 16);
251     return val;
252   }
253 #endif
254 }
255 
256 
257 
258 #define N 8
259 #include "sim-n-bits.h"
260 #undef N
261 
262 #define N 16
263 #include "sim-n-bits.h"
264 #undef N
265 
266 #define N 32
267 #include "sim-n-bits.h"
268 #undef N
269 
270 #define N 64
271 #include "sim-n-bits.h"
272 #undef N
273 
274 #endif /* _SIM_BITS_C_ */
275