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 N
24 #error "N must be #defined"
25 #endif
26 
27 #include "symcat.h"
28 
29 #if defined(__STDC__) && defined(signed)
30 /* If signed were defined to be say __signed (ie, some versions of Linux),
31    then the signedN macro would not work correctly.  If we have a standard
32    compiler, we have signed.  */
33 #undef signed
34 #endif
35 
36 /* NOTE: See end of file for #undef */
37 #define unsignedN XCONCAT2(unsigned,N)
38 #define signedN XCONCAT2(signed,N)
39 #define LSMASKn XCONCAT2(LSMASK,N)
40 #define MSMASKn XCONCAT2(MSMASK,N)
41 #define LSMASKEDn XCONCAT2(LSMASKED,N)
42 #define MSMASKEDn XCONCAT2(MSMASKED,N)
43 #define LSEXTRACTEDn XCONCAT2(LSEXTRACTED,N)
44 #define MSEXTRACTEDn XCONCAT2(MSEXTRACTED,N)
45 #define LSINSERTEDn XCONCAT2(LSINSERTED,N)
46 #define MSINSERTEDn XCONCAT2(MSINSERTED,N)
47 #define ROTn XCONCAT2(ROT,N)
48 #define ROTLn XCONCAT2(ROTL,N)
49 #define ROTRn XCONCAT2(ROTR,N)
50 #define MSSEXTn XCONCAT2(MSSEXT,N)
51 #define LSSEXTn XCONCAT2(LSSEXT,N)
52 
53 /* TAGS: LSMASKED16 LSMASKED32 LSMASKED64 */
54 
55 INLINE_SIM_BITS\
56 (unsignedN)
LSMASKEDn(unsignedN word,int start,int stop)57 LSMASKEDn (unsignedN word,
58 	   int start,
59 	   int stop)
60 {
61   word &= LSMASKn (start, stop);
62   return word;
63 }
64 
65 /* TAGS: MSMASKED16 MSMASKED32 MSMASKED64 */
66 
67 INLINE_SIM_BITS\
68 (unsignedN)
MSMASKEDn(unsignedN word,int start,int stop)69 MSMASKEDn (unsignedN word,
70 	   int start,
71 	   int stop)
72 {
73   word &= MSMASKn (start, stop);
74   return word;
75 }
76 
77 /* TAGS: LSEXTRACTED16 LSEXTRACTED32 LSEXTRACTED64 */
78 
79 INLINE_SIM_BITS\
80 (unsignedN)
LSEXTRACTEDn(unsignedN val,int start,int stop)81 LSEXTRACTEDn (unsignedN val,
82 	      int start,
83 	      int stop)
84 {
85   val <<= (N - 1 - start); /* drop high bits */
86   val >>= (N - 1 - start) + (stop); /* drop low bits */
87   return val;
88 }
89 
90 /* TAGS: MSEXTRACTED16 MSEXTRACTED32 MSEXTRACTED64 */
91 
92 INLINE_SIM_BITS\
93 (unsignedN)
MSEXTRACTEDn(unsignedN val,int start,int stop)94 MSEXTRACTEDn (unsignedN val,
95 	      int start,
96 	      int stop)
97 {
98   val <<= (start); /* drop high bits */
99   val >>= (start) + (N - 1 - stop); /* drop low bits */
100   return val;
101 }
102 
103 /* TAGS: LSINSERTED16 LSINSERTED32 LSINSERTED64 */
104 
105 INLINE_SIM_BITS\
106 (unsignedN)
LSINSERTEDn(unsignedN val,int start,int stop)107 LSINSERTEDn (unsignedN val,
108 	     int start,
109 	     int stop)
110 {
111   val <<= stop;
112   val &= LSMASKn (start, stop);
113   return val;
114 }
115 
116 /* TAGS: MSINSERTED16 MSINSERTED32 MSINSERTED64 */
117 
118 INLINE_SIM_BITS\
119 (unsignedN)
MSINSERTEDn(unsignedN val,int start,int stop)120 MSINSERTEDn (unsignedN val,
121 	     int start,
122 	     int stop)
123 {
124   val <<= ((N - 1) - stop);
125   val &= MSMASKn (start, stop);
126   return val;
127 }
128 
129 /* TAGS: ROT16 ROT32 ROT64 */
130 
131 INLINE_SIM_BITS\
132 (unsignedN)
ROTn(unsignedN val,int shift)133 ROTn (unsignedN val,
134       int shift)
135 {
136   if (shift > 0)
137     return ROTRn (val, shift);
138   else if (shift < 0)
139     return ROTLn (val, -shift);
140   else
141     return val;
142 }
143 
144 /* TAGS: ROTL16 ROTL32 ROTL64 */
145 
146 INLINE_SIM_BITS\
147 (unsignedN)
ROTLn(unsignedN val,int shift)148 ROTLn (unsignedN val,
149        int shift)
150 {
151   unsignedN result;
152   ASSERT (shift <= N);
153   result = (((val) << (shift)) | ((val) >> ((N)-(shift))));
154   return result;
155 }
156 
157 /* TAGS: ROTR16 ROTR32 ROTR64 */
158 
159 INLINE_SIM_BITS\
160 (unsignedN)
ROTRn(unsignedN val,int shift)161 ROTRn (unsignedN val,
162        int shift)
163 {
164   unsignedN result;
165   ASSERT (shift <= N);
166   result = (((val) >> (shift)) | ((val) << ((N)-(shift))));
167   return result;
168 }
169 
170 /* TAGS: LSSEXT16 LSSEXT32 LSSEXT64 */
171 
172 INLINE_SIM_BITS\
173 (unsignedN)
LSSEXTn(signedN val,int sign_bit)174 LSSEXTn (signedN val,
175 	 int sign_bit)
176 {
177   int shift;
178   /* make the sign-bit most significant and then smear it back into
179      position */
180   ASSERT (sign_bit < N);
181   shift = ((N - 1) - sign_bit);
182   val <<= shift;
183   val >>= shift;
184   return val;
185 }
186 
187 /* TAGS: MSSEXT16 MSSEXT32 MSSEXT64 */
188 
189 INLINE_SIM_BITS\
190 (unsignedN)
MSSEXTn(signedN val,int sign_bit)191 MSSEXTn (signedN val,
192 	 int sign_bit)
193 {
194   /* make the sign-bit most significant and then smear it back into
195      position */
196   ASSERT (sign_bit < N);
197   val <<= sign_bit;
198   val >>= sign_bit;
199   return val;
200 }
201 
202 
203 /* NOTE: See start of file for #define */
204 #undef LSSEXTn
205 #undef MSSEXTn
206 #undef ROTLn
207 #undef ROTRn
208 #undef ROTn
209 #undef LSINSERTEDn
210 #undef MSINSERTEDn
211 #undef LSEXTRACTEDn
212 #undef MSEXTRACTEDn
213 #undef LSMASKEDn
214 #undef LSMASKn
215 #undef MSMASKEDn
216 #undef MSMASKn
217 #undef signedN
218 #undef unsignedN
219