More patches from Jeff Law, plus a little cleanup of my own.
[external/binutils.git] / bfd / libhppa.h
1 /* HP PA-RISC SOM object file format:  definitions internal to BFD.
2    Copyright (C) 1990-1991 Free Software Foundation, Inc.
3
4    Contributed by the Center for Software Science at the
5    University of Utah (pa-gdb-bugs@cs.utah.edu).
6
7    This file is part of BFD, the Binary File Descriptor library.
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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
22
23 #ifndef _HPPA_H
24 #define _HPPA_H
25
26 #define BYTES_IN_WORD 4
27
28 #ifndef INLINE
29 #ifdef __GNUC__
30 #define INLINE inline
31 #else
32 #define INLINE
33 #endif /* GNU C? */
34 #endif /* INLINE */
35
36 /* HP PA-RISC relocation types */
37
38 enum hppa_reloc_field_selector_type
39   {
40     R_HPPA_FSEL = 0x0,
41     R_HPPA_LSSEL = 0x1,
42     R_HPPA_RSSEL = 0x2,
43     R_HPPA_LSEL = 0x3,
44     R_HPPA_RSEL = 0x4,
45     R_HPPA_LDSEL = 0x5,
46     R_HPPA_RDSEL = 0x6,
47     R_HPPA_LRSEL = 0x7,
48     R_HPPA_RRSEL = 0x8,
49     R_HPPA_PSEL = 0x9,
50     R_HPPA_LPSEL = 0xa,
51     R_HPPA_RPSEL = 0xb,
52     R_HPPA_TSEL = 0xc,
53     R_HPPA_LTSEL = 0xd,
54     R_HPPA_RTSEL = 0xe
55   };
56
57 /* for compatibility */
58 enum hppa_reloc_field_selector_type_alt
59   {
60     e_fsel = R_HPPA_FSEL,
61     e_lssel = R_HPPA_LSSEL,
62     e_rssel = R_HPPA_RSSEL,
63     e_lsel = R_HPPA_LSEL,
64     e_rsel = R_HPPA_RSEL,
65     e_ldsel = R_HPPA_LDSEL,
66     e_rdsel = R_HPPA_RDSEL,
67     e_lrsel = R_HPPA_LRSEL,
68     e_rrsel = R_HPPA_RRSEL,
69     e_psel = R_HPPA_PSEL,
70     e_lpsel = R_HPPA_LPSEL,
71     e_rpsel = R_HPPA_RPSEL,
72     e_tsel = R_HPPA_TSEL,
73     e_ltsel = R_HPPA_LTSEL,
74     e_rtsel = R_HPPA_RTSEL
75   };
76
77 enum hppa_reloc_expr_type
78   {
79     R_HPPA_E_ONE = 0,
80     R_HPPA_E_TWO = 1,
81     R_HPPA_E_PCREL = 2,
82     R_HPPA_E_CON = 3,
83     R_HPPA_E_PLABEL = 7,
84     R_HPPA_E_ABS = 18
85   };
86
87 /* for compatibility */
88 enum hppa_reloc_expr_type_alt
89   {
90     e_one = R_HPPA_E_ONE,
91     e_two = R_HPPA_E_TWO,
92     e_pcrel = R_HPPA_E_PCREL,
93     e_con = R_HPPA_E_CON,
94     e_plabel = R_HPPA_E_PLABEL,
95     e_abs = R_HPPA_E_ABS
96   };
97
98
99 /* Some functions to manipulate PA instructions.  */
100 static INLINE unsigned int
101 assemble_3 (x)
102      unsigned int x;
103 {
104   return (((x & 1) << 2) | ((x & 6) >> 1)) & 7;
105 }
106
107 static INLINE void
108 dis_assemble_3 (x, r)
109      unsigned int x;
110      unsigned int *r;
111 {
112   *r = (((x & 4) >> 2) | ((x & 3) << 1)) & 7;
113 }
114
115 static INLINE unsigned int
116 assemble_12 (x, y)
117      unsigned int x, y;
118 {
119   return (((y & 1) << 11) | ((x & 1) << 10) | ((x & 0x7fe) >> 1)) & 0xfff;
120 }
121
122 static INLINE void
123 dis_assemble_12 (as12, x, y)
124      unsigned int as12;
125      unsigned int *x, *y;
126 {
127   *y = (as12 & 0x800) >> 11;
128   *x = ((as12 & 0x3ff) << 1) | ((as12 & 0x400) >> 10);
129 }
130
131 static INLINE unsigned long
132 assemble_17 (x, y, z)
133      unsigned int x, y, z;
134 {
135   unsigned long temp;
136
137   temp = ((z & 1) << 16) |
138     ((x & 0x1f) << 11) |
139     ((y & 1) << 10) |
140     ((y & 0x7fe) >> 1);
141   return temp & 0x1ffff;
142 }
143
144 static INLINE void
145 dis_assemble_17 (as17, x, y, z)
146      unsigned int as17;
147      unsigned int *x, *y, *z;
148 {
149
150   *z = (as17 & 0x10000) >> 16;
151   *x = (as17 & 0x0f800) >> 11;
152   *y = (((as17 & 0x00400) >> 10) | ((as17 & 0x3ff) << 1)) & 0x7ff;
153 }
154
155 static INLINE unsigned long
156 assemble_21 (x)
157      unsigned int x;
158 {
159   unsigned long temp;
160
161   temp = ((x & 1) << 20) |
162     ((x & 0xffe) << 8) |
163     ((x & 0xc000) >> 7) |
164     ((x & 0x1f0000) >> 14) |
165     ((x & 0x003000) >> 12);
166   return temp & 0x1fffff;
167 }
168
169 static INLINE void
170 dis_assemble_21 (as21, x)
171      unsigned int as21, *x;
172 {
173   unsigned long temp;
174
175
176   temp = (as21 & 0x100000) >> 20;
177   temp |= (as21 & 0x0ffe00) >> 8;
178   temp |= (as21 & 0x000180) << 7;
179   temp |= (as21 & 0x00007c) << 14;
180   temp |= (as21 & 0x000003) << 12;
181   *x = temp;
182 }
183
184 static INLINE unsigned long
185 sign_ext (x, len)
186      unsigned int x, len;
187 {
188   return (x << (32 - len)) >> (32 - len);
189 }
190
191 static INLINE unsigned int
192 ones (n)
193      int n;
194 {
195   unsigned int len_ones;
196   int i;
197
198   i = 0;
199   len_ones = 0;
200   while (i < n)
201     {
202       len_ones = (len_ones << 1) | 1;
203       i++;
204     }
205
206   return len_ones;
207 }
208
209 static INLINE void
210 sign_unext (x, len, result)
211      unsigned int x, len;
212      unsigned int *result;
213 {
214   unsigned int len_ones;
215
216   len_ones = ones (len);
217
218   *result = x & len_ones;
219 }
220
221 static INLINE unsigned long
222 low_sign_ext (x, len)
223      unsigned int x, len;
224 {
225   unsigned int temp1, temp2;
226   unsigned int len_ones;
227
228   len_ones = ones (len);
229
230   temp1 = (x & 1) << (len - 1);
231   temp2 = ((x & 0xfffffffe) & len_ones) >> 1;
232   return sign_ext ((temp1 | temp2), len);
233 }
234
235 static INLINE void
236 low_sign_unext (x, len, result)
237      unsigned int x, len;
238      unsigned int *result;
239 {
240   unsigned int temp;
241   unsigned int sign;
242   unsigned int rest;
243   unsigned int one_bit_at_len;
244   unsigned int len_ones;
245
246   len_ones = ones (len);
247   one_bit_at_len = 1 << (len - 1);
248
249   sign_unext (x, len, &temp);
250   sign = temp & one_bit_at_len;
251   sign >>= (len - 1);
252
253   rest = temp & (len_ones ^ one_bit_at_len);
254   rest <<= 1;
255
256   *result = rest | sign;
257 }
258
259 /* Handle field selectors for PA instructions.  */
260
261 static INLINE unsigned long
262 hppa_field_adjust (value, constant_value, r_field)
263      unsigned long value;
264      unsigned long constant_value;
265      unsigned short r_field;
266 {
267   unsigned long init_value = value;
268   value += constant_value;
269   switch (r_field)
270     {
271     case e_fsel:                /* F  : no change                      */
272       break;
273
274     case e_lssel:               /* LS : if (bit 21) then add 0x800
275                                    arithmetic shift right 11 bits */
276       if (value & 0x00000400)
277         value += 0x800;
278       value = (value & 0xfffff800) >> 11;
279       break;
280
281     case e_rssel:               /* RS : Sign extend from bit 21        */
282       if (value & 0x00000400)
283         value |= 0xfffff800;
284       else
285         value &= 0x7ff;
286       break;
287
288     case e_lsel:                /* L  : Arithmetic shift right 11 bits */
289       value = (value & 0xfffff800) >> 11;
290       break;
291
292     case e_rsel:                /* R  : Set bits 0-20 to zero          */
293       value = value & 0x7ff;
294       break;
295
296     case e_ldsel:               /* LD : Add 0x800, arithmetic shift
297                                    right 11 bits                  */
298       value += 0x800;
299       value = (value & 0xfffff800) >> 11;
300       break;
301
302     case e_rdsel:               /* RD : Set bits 0-20 to one           */
303       value |= 0xfffff800;
304       break;
305
306     case e_lrsel:               /* LR : L with "rounded" constant      */
307       value = value + ((constant_value + 0x1000) & 0xffffe000);
308       value = (value & 0xfffff800) >> 11;
309       break;
310
311     case e_rrsel:               /* RR : R with "rounded" constant      */
312       value = value + ((constant_value + 0x1000) & 0xffffe000);
313       value = (value & 0x7ff) + constant_value - ((constant_value + 0x1000) & 0xffffe000);
314       break;
315
316     default:
317       abort ();
318     }
319   return value;
320
321 }
322 #endif /* _HPPA_H */