1 /* The common simulator framework for GDB, the GNU Debugger.
2 
3    Copyright 2002-2013 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 #include "sim-basics.h"
27 #include "sim-assert.h"
28 #include "sim-io.h"
29 
30 
31 INLINE_SIM_BITS\
32 (unsigned_word)
LSMASKED(unsigned_word val,int start,int stop)33 LSMASKED (unsigned_word val,
34 	  int start,
35 	  int stop)
36 {
37   /* NOTE - start, stop can wrap */
38   val &= LSMASK (start, stop);
39   return val;
40 }
41 
42 
43 INLINE_SIM_BITS\
44 (unsigned_word)
MSMASKED(unsigned_word val,int start,int stop)45 MSMASKED (unsigned_word val,
46 	  int start,
47 	  int stop)
48 {
49   /* NOTE - start, stop can wrap */
50   val &= MSMASK (start, stop);
51   return val;
52 }
53 
54 
55 INLINE_SIM_BITS\
56 (unsigned_word)
LSEXTRACTED(unsigned_word val,int start,int stop)57 LSEXTRACTED (unsigned_word val,
58 	     int start,
59 	     int stop)
60 {
61   ASSERT (start >= stop);
62 #if (WITH_TARGET_WORD_BITSIZE == 64)
63   return LSEXTRACTED64 (val, start, stop);
64 #endif
65 #if (WITH_TARGET_WORD_BITSIZE == 32)
66   if (stop >= 32)
67     return 0;
68   else
69     {
70       if (start < 32)
71 	val &= LSMASK (start, 0);
72       val >>= stop;
73       return val;
74     }
75 #endif
76 #if (WITH_TARGET_WORD_BITSIZE == 16)
77   if (stop >= 16)
78     return 0;
79   else
80     {
81       if (start < 16)
82 	val &= LSMASK (start, 0);
83       val >>= stop;
84       return val;
85     }
86 #endif
87 }
88 
89 
90 INLINE_SIM_BITS\
91 (unsigned_word)
MSEXTRACTED(unsigned_word val,int start,int stop)92 MSEXTRACTED (unsigned_word val,
93 	     int start,
94 	     int stop)
95 {
96   ASSERT (start <= stop);
97 #if (WITH_TARGET_WORD_BITSIZE == 64)
98   return MSEXTRACTED64 (val, start, stop);
99 #endif
100 #if (WITH_TARGET_WORD_BITSIZE == 32)
101   if (stop < 32)
102     return 0;
103   else
104     {
105       if (start >= 32)
106 	val &= MSMASK (start, 64 - 1);
107       val >>= (64 - stop - 1);
108       return val;
109     }
110 #endif
111 #if (WITH_TARGET_WORD_BITSIZE == 16)
112   if (stop < 16)
113     return 0;
114   else
115     {
116       if (start >= 16)
117 	val &= MSMASK (start, 64 - 1);
118       val >>= (64 - stop - 1);
119       return val;
120     }
121 #endif
122 }
123 
124 
125 INLINE_SIM_BITS\
126 (unsigned_word)
LSINSERTED(unsigned_word val,int start,int stop)127 LSINSERTED (unsigned_word val,
128 	    int start,
129 	    int stop)
130 {
131   ASSERT (start >= stop);
132 #if (WITH_TARGET_WORD_BITSIZE == 64)
133   return LSINSERTED64 (val, start, stop);
134 #endif
135 #if (WITH_TARGET_WORD_BITSIZE == 32)
136   /* Bit numbers are 63..0, even for 32 bit targets.
137      On 32 bit targets we ignore 63..32  */
138   if (stop >= 32)
139     return 0;
140   else
141     {
142       val <<= stop;
143       val &= LSMASK (start, stop);
144       return val;
145     }
146 #endif
147 #if (WITH_TARGET_WORD_BITSIZE == 16)
148   /* Bit numbers are 63..0, even for 16 bit targets.
149      On 16 bit targets we ignore 63..16  */
150   if (stop >= 16)
151     return 0;
152   else
153     {
154       val <<= stop;
155       val &= LSMASK (start, stop);
156       return val;
157     }
158 #endif
159 }
160 
161 INLINE_SIM_BITS\
162 (unsigned_word)
MSINSERTED(unsigned_word val,int start,int stop)163 MSINSERTED (unsigned_word val,
164 	    int start,
165 	    int stop)
166 {
167   ASSERT (start <= stop);
168 #if (WITH_TARGET_WORD_BITSIZE == 64)
169   return MSINSERTED64 (val, start, stop);
170 #endif
171 #if (WITH_TARGET_WORD_BITSIZE == 32)
172   /* Bit numbers are 0..63, even for 32 bit targets.
173      On 32 bit targets we ignore 0..31.  */
174   if (stop < 32)
175     return 0;
176   else
177     {
178       val <<= ((64 - 1) - stop);
179       val &= MSMASK (start, stop);
180       return val;
181     }
182 #endif
183 #if (WITH_TARGET_WORD_BITSIZE == 16)
184   /* Bit numbers are 0..63, even for 16 bit targets.
185      On 16 bit targets we ignore 0..47.  */
186   if (stop < 32 + 16)
187     return 0;
188   else
189     {
190       val <<= ((64 - 1) - stop);
191       val &= MSMASK (start, stop);
192       return val;
193     }
194 #endif
195 }
196 
197 
198 
199 INLINE_SIM_BITS\
200 (unsigned_word)
LSSEXT(signed_word val,int sign_bit)201 LSSEXT (signed_word val,
202 	int sign_bit)
203 {
204   ASSERT (sign_bit < 64);
205 #if (WITH_TARGET_WORD_BITSIZE == 64)
206   return LSSEXT64 (val, sign_bit);
207 #endif
208 #if (WITH_TARGET_WORD_BITSIZE == 32)
209   if (sign_bit >= 32)
210     return val;
211   else {
212     val = LSSEXT32 (val, sign_bit);
213     return val;
214   }
215 #endif
216 #if (WITH_TARGET_WORD_BITSIZE == 16)
217   if (sign_bit >= 16)
218     return val;
219   else {
220     val = LSSEXT16 (val, sign_bit);
221     return val;
222   }
223 #endif
224 }
225 
226 INLINE_SIM_BITS\
227 (unsigned_word)
MSSEXT(signed_word val,int sign_bit)228 MSSEXT (signed_word val,
229 	int sign_bit)
230 {
231   ASSERT (sign_bit < 64);
232 #if (WITH_TARGET_WORD_BITSIZE == 64)
233   return MSSEXT64 (val, sign_bit);
234 #endif
235 #if (WITH_TARGET_WORD_BITSIZE == 32)
236   if (sign_bit < 32)
237     return val;
238   else {
239     val = MSSEXT32 (val, sign_bit - 32);
240     return val;
241   }
242 #endif
243 #if (WITH_TARGET_WORD_BITSIZE == 16)
244   if (sign_bit < 32 + 16)
245     return val;
246   else {
247     val = MSSEXT16 (val, sign_bit - 32 - 16);
248     return val;
249   }
250 #endif
251 }
252 
253 
254 
255 #define N 8
256 #include "sim-n-bits.h"
257 #undef N
258 
259 #define N 16
260 #include "sim-n-bits.h"
261 #undef N
262 
263 #define N 32
264 #include "sim-n-bits.h"
265 #undef N
266 
267 #define N 64
268 #include "sim-n-bits.h"
269 #undef N
270 
271 #endif /* _SIM_BITS_C_ */
272