Add missing changelogs for previous commits.
[binutils-gdb.git] / opcodes / cgen-ibld.in
blob134ce3df76eae15185fc81adf43934a0ae4e2d28
1 /* Instruction building/extraction support for @arch@. -*- C -*-
3    THIS FILE IS MACHINE GENERATED WITH CGEN: Cpu tools GENerator.
4    - the resultant file is machine generated, cgen-ibld.in isn't
6    Copyright (C) 1996-2019 Free Software Foundation, Inc.
8    This file is part of libopcodes.
10    This library is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3, or (at your option)
13    any later version.
15    It is distributed in the hope that it will be useful, but WITHOUT
16    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18    License for more details.
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software Foundation, Inc.,
22    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
24 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
25    Keep that in mind.  */
27 #include "sysdep.h"
28 #include <stdio.h>
29 #include "ansidecl.h"
30 #include "dis-asm.h"
31 #include "bfd.h"
32 #include "symcat.h"
33 #include "@prefix@-desc.h"
34 #include "@prefix@-opc.h"
35 #include "cgen/basic-modes.h"
36 #include "opintl.h"
37 #include "safe-ctype.h"
39 #undef  min
40 #define min(a,b) ((a) < (b) ? (a) : (b))
41 #undef  max
42 #define max(a,b) ((a) > (b) ? (a) : (b))
44 /* Used by the ifield rtx function.  */
45 #define FLD(f) (fields->f)
47 static const char * insert_normal
48   (CGEN_CPU_DESC, long, unsigned int, unsigned int, unsigned int,
49    unsigned int, unsigned int, unsigned int, CGEN_INSN_BYTES_PTR);
50 static const char * insert_insn_normal
51   (CGEN_CPU_DESC, const CGEN_INSN *,
52    CGEN_FIELDS *, CGEN_INSN_BYTES_PTR, bfd_vma);
53 static int extract_normal
54   (CGEN_CPU_DESC, CGEN_EXTRACT_INFO *, CGEN_INSN_INT,
55    unsigned int, unsigned int, unsigned int, unsigned int,
56    unsigned int, unsigned int, bfd_vma, long *);
57 static int extract_insn_normal
58   (CGEN_CPU_DESC, const CGEN_INSN *, CGEN_EXTRACT_INFO *,
59    CGEN_INSN_INT, CGEN_FIELDS *, bfd_vma);
60 #if CGEN_INT_INSN_P
61 static void put_insn_int_value
62   (CGEN_CPU_DESC, CGEN_INSN_BYTES_PTR, int, int, CGEN_INSN_INT);
63 #endif
64 #if ! CGEN_INT_INSN_P
65 static CGEN_INLINE void insert_1
66   (CGEN_CPU_DESC, unsigned long, int, int, int, unsigned char *);
67 static CGEN_INLINE int fill_cache
68   (CGEN_CPU_DESC, CGEN_EXTRACT_INFO *,  int, int, bfd_vma);
69 static CGEN_INLINE long extract_1
70   (CGEN_CPU_DESC, CGEN_EXTRACT_INFO *, int, int, int, unsigned char *, bfd_vma);
71 #endif
73 /* Operand insertion.  */
75 #if ! CGEN_INT_INSN_P
77 /* Subroutine of insert_normal.  */
79 static CGEN_INLINE void
80 insert_1 (CGEN_CPU_DESC cd,
81           unsigned long value,
82           int start,
83           int length,
84           int word_length,
85           unsigned char *bufp)
87   unsigned long x,mask;
88   int shift;
90   x = cgen_get_insn_value (cd, bufp, word_length);
92   /* Written this way to avoid undefined behaviour.  */
93   mask = (((1L << (length - 1)) - 1) << 1) | 1;
94   if (CGEN_INSN_LSB0_P)
95     shift = (start + 1) - length;
96   else
97     shift = (word_length - (start + length));
98   x = (x & ~(mask << shift)) | ((value & mask) << shift);
100   cgen_put_insn_value (cd, bufp, word_length, (bfd_vma) x);
103 #endif /* ! CGEN_INT_INSN_P */
105 /* Default insertion routine.
107    ATTRS is a mask of the boolean attributes.
108    WORD_OFFSET is the offset in bits from the start of the insn of the value.
109    WORD_LENGTH is the length of the word in bits in which the value resides.
110    START is the starting bit number in the word, architecture origin.
111    LENGTH is the length of VALUE in bits.
112    TOTAL_LENGTH is the total length of the insn in bits.
114    The result is an error message or NULL if success.  */
116 /* ??? This duplicates functionality with bfd's howto table and
117    bfd_install_relocation.  */
118 /* ??? This doesn't handle bfd_vma's.  Create another function when
119    necessary.  */
121 static const char *
122 insert_normal (CGEN_CPU_DESC cd,
123                long value,
124                unsigned int attrs,
125                unsigned int word_offset,
126                unsigned int start,
127                unsigned int length,
128                unsigned int word_length,
129                unsigned int total_length,
130                CGEN_INSN_BYTES_PTR buffer)
132   static char errbuf[100];
133   /* Written this way to avoid undefined behaviour.  */
134   unsigned long mask = (((1L << (length - 1)) - 1) << 1) | 1;
136   /* If LENGTH is zero, this operand doesn't contribute to the value.  */
137   if (length == 0)
138     return NULL;
140   if (word_length > 8 * sizeof (CGEN_INSN_INT))
141     abort ();
143   /* For architectures with insns smaller than the base-insn-bitsize,
144      word_length may be too big.  */
145   if (cd->min_insn_bitsize < cd->base_insn_bitsize)
146     {
147       if (word_offset == 0
148           && word_length > total_length)
149         word_length = total_length;
150     }
152   /* Ensure VALUE will fit.  */
153   if (CGEN_BOOL_ATTR (attrs, CGEN_IFLD_SIGN_OPT))
154     {
155       long minval = - (1L << (length - 1));
156       unsigned long maxval = mask;
158       if ((value > 0 && (unsigned long) value > maxval)
159           || value < minval)
160         {
161           /* xgettext:c-format */
162           sprintf (errbuf,
163                    _("operand out of range (%ld not between %ld and %lu)"),
164                    value, minval, maxval);
165           return errbuf;
166         }
167     }
168   else if (! CGEN_BOOL_ATTR (attrs, CGEN_IFLD_SIGNED))
169     {
170       unsigned long maxval = mask;
171       unsigned long val = (unsigned long) value;
173       /* For hosts with a word size > 32 check to see if value has been sign
174          extended beyond 32 bits.  If so then ignore these higher sign bits
175          as the user is attempting to store a 32-bit signed value into an
176          unsigned 32-bit field which is allowed.  */
177       if (sizeof (unsigned long) > 4 && ((value >> 32) == -1))
178         val &= 0xFFFFFFFF;
180       if (val > maxval)
181         {
182           /* xgettext:c-format */
183           sprintf (errbuf,
184                    _("operand out of range (0x%lx not between 0 and 0x%lx)"),
185                    val, maxval);
186           return errbuf;
187         }
188     }
189   else
190     {
191       if (! cgen_signed_overflow_ok_p (cd))
192         {
193           long minval = - (1L << (length - 1));
194           long maxval =   (1L << (length - 1)) - 1;
196           if (value < minval || value > maxval)
197             {
198               sprintf
199                 /* xgettext:c-format */
200                 (errbuf, _("operand out of range (%ld not between %ld and %ld)"),
201                  value, minval, maxval);
202               return errbuf;
203             }
204         }
205     }
207 #if CGEN_INT_INSN_P
209   {
210     int shift_within_word, shift_to_word, shift;
212     /* How to shift the value to BIT0 of the word.  */
213     shift_to_word = total_length - (word_offset + word_length);
215     /* How to shift the value to the field within the word.  */
216     if (CGEN_INSN_LSB0_P)
217       shift_within_word = start + 1 - length;
218     else
219       shift_within_word = word_length - start - length;
221     /* The total SHIFT, then mask in the value.  */
222     shift = shift_to_word + shift_within_word;
223     *buffer = (*buffer & ~(mask << shift)) | ((value & mask) << shift);
224   }
226 #else /* ! CGEN_INT_INSN_P */
228   {
229     unsigned char *bufp = (unsigned char *) buffer + word_offset / 8;
231     insert_1 (cd, value, start, length, word_length, bufp);
232   }
234 #endif /* ! CGEN_INT_INSN_P */
236   return NULL;
239 /* Default insn builder (insert handler).
240    The instruction is recorded in CGEN_INT_INSN_P byte order (meaning
241    that if CGEN_INSN_BYTES_PTR is an int * and thus, the value is
242    recorded in host byte order, otherwise BUFFER is an array of bytes
243    and the value is recorded in target byte order).
244    The result is an error message or NULL if success.  */
246 static const char *
247 insert_insn_normal (CGEN_CPU_DESC cd,
248                     const CGEN_INSN * insn,
249                     CGEN_FIELDS * fields,
250                     CGEN_INSN_BYTES_PTR buffer,
251                     bfd_vma pc)
253   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
254   unsigned long value;
255   const CGEN_SYNTAX_CHAR_TYPE * syn;
257   CGEN_INIT_INSERT (cd);
258   value = CGEN_INSN_BASE_VALUE (insn);
260   /* If we're recording insns as numbers (rather than a string of bytes),
261      target byte order handling is deferred until later.  */
263 #if CGEN_INT_INSN_P
265   put_insn_int_value (cd, buffer, cd->base_insn_bitsize,
266                       CGEN_FIELDS_BITSIZE (fields), value);
268 #else
270   cgen_put_insn_value (cd, buffer, min ((unsigned) cd->base_insn_bitsize,
271                                         (unsigned) CGEN_FIELDS_BITSIZE (fields)),
272                        value);
274 #endif /* ! CGEN_INT_INSN_P */
276   /* ??? It would be better to scan the format's fields.
277      Still need to be able to insert a value based on the operand though;
278      e.g. storing a branch displacement that got resolved later.
279      Needs more thought first.  */
281   for (syn = CGEN_SYNTAX_STRING (syntax); * syn; ++ syn)
282     {
283       const char *errmsg;
285       if (CGEN_SYNTAX_CHAR_P (* syn))
286         continue;
288       errmsg = (* cd->insert_operand) (cd, CGEN_SYNTAX_FIELD (*syn),
289                                        fields, buffer, pc);
290       if (errmsg)
291         return errmsg;
292     }
294   return NULL;
297 #if CGEN_INT_INSN_P
298 /* Cover function to store an insn value into an integral insn.  Must go here
299    because it needs <prefix>-desc.h for CGEN_INT_INSN_P.  */
301 static void
302 put_insn_int_value (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
303                     CGEN_INSN_BYTES_PTR buf,
304                     int length,
305                     int insn_length,
306                     CGEN_INSN_INT value)
308   /* For architectures with insns smaller than the base-insn-bitsize,
309      length may be too big.  */
310   if (length > insn_length)
311     *buf = value;
312   else
313     {
314       int shift = insn_length - length;
315       /* Written this way to avoid undefined behaviour.  */
316       CGEN_INSN_INT mask = (((1L << (length - 1)) - 1) << 1) | 1;
318       *buf = (*buf & ~(mask << shift)) | ((value & mask) << shift);
319     }
321 #endif
323 /* Operand extraction.  */
325 #if ! CGEN_INT_INSN_P
327 /* Subroutine of extract_normal.
328    Ensure sufficient bytes are cached in EX_INFO.
329    OFFSET is the offset in bytes from the start of the insn of the value.
330    BYTES is the length of the needed value.
331    Returns 1 for success, 0 for failure.  */
333 static CGEN_INLINE int
334 fill_cache (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
335             CGEN_EXTRACT_INFO *ex_info,
336             int offset,
337             int bytes,
338             bfd_vma pc)
340   /* It's doubtful that the middle part has already been fetched so
341      we don't optimize that case.  kiss.  */
342   unsigned int mask;
343   disassemble_info *info = (disassemble_info *) ex_info->dis_info;
345   /* First do a quick check.  */
346   mask = (1 << bytes) - 1;
347   if (((ex_info->valid >> offset) & mask) == mask)
348     return 1;
350   /* Search for the first byte we need to read.  */
351   for (mask = 1 << offset; bytes > 0; --bytes, ++offset, mask <<= 1)
352     if (! (mask & ex_info->valid))
353       break;
355   if (bytes)
356     {
357       int status;
359       pc += offset;
360       status = (*info->read_memory_func)
361         (pc, ex_info->insn_bytes + offset, bytes, info);
363       if (status != 0)
364         {
365           (*info->memory_error_func) (status, pc, info);
366           return 0;
367         }
369       ex_info->valid |= ((1 << bytes) - 1) << offset;
370     }
372   return 1;
375 /* Subroutine of extract_normal.  */
377 static CGEN_INLINE long
378 extract_1 (CGEN_CPU_DESC cd,
379            CGEN_EXTRACT_INFO *ex_info ATTRIBUTE_UNUSED,
380            int start,
381            int length,
382            int word_length,
383            unsigned char *bufp,
384            bfd_vma pc ATTRIBUTE_UNUSED)
386   unsigned long x;
387   int shift;
389   x = cgen_get_insn_value (cd, bufp, word_length);
391   if (CGEN_INSN_LSB0_P)
392     shift = (start + 1) - length;
393   else
394     shift = (word_length - (start + length));
395   return x >> shift;
398 #endif /* ! CGEN_INT_INSN_P */
400 /* Default extraction routine.
402    INSN_VALUE is the first base_insn_bitsize bits of the insn in host order,
403    or sometimes less for cases like the m32r where the base insn size is 32
404    but some insns are 16 bits.
405    ATTRS is a mask of the boolean attributes.  We only need `SIGNED',
406    but for generality we take a bitmask of all of them.
407    WORD_OFFSET is the offset in bits from the start of the insn of the value.
408    WORD_LENGTH is the length of the word in bits in which the value resides.
409    START is the starting bit number in the word, architecture origin.
410    LENGTH is the length of VALUE in bits.
411    TOTAL_LENGTH is the total length of the insn in bits.
413    Returns 1 for success, 0 for failure.  */
415 /* ??? The return code isn't properly used.  wip.  */
417 /* ??? This doesn't handle bfd_vma's.  Create another function when
418    necessary.  */
420 static int
421 extract_normal (CGEN_CPU_DESC cd,
422 #if ! CGEN_INT_INSN_P
423                 CGEN_EXTRACT_INFO *ex_info,
424 #else
425                 CGEN_EXTRACT_INFO *ex_info ATTRIBUTE_UNUSED,
426 #endif
427                 CGEN_INSN_INT insn_value,
428                 unsigned int attrs,
429                 unsigned int word_offset,
430                 unsigned int start,
431                 unsigned int length,
432                 unsigned int word_length,
433                 unsigned int total_length,
434 #if ! CGEN_INT_INSN_P
435                 bfd_vma pc,
436 #else
437                 bfd_vma pc ATTRIBUTE_UNUSED,
438 #endif
439                 long *valuep)
441   long value, mask;
443   /* If LENGTH is zero, this operand doesn't contribute to the value
444      so give it a standard value of zero.  */
445   if (length == 0)
446     {
447       *valuep = 0;
448       return 1;
449     }
451   if (word_length > 8 * sizeof (CGEN_INSN_INT))
452     abort ();
454   /* For architectures with insns smaller than the insn-base-bitsize,
455      word_length may be too big.  */
456   if (cd->min_insn_bitsize < cd->base_insn_bitsize)
457     {
458       if (word_offset + word_length > total_length)
459         word_length = total_length - word_offset;
460     }
462   /* Does the value reside in INSN_VALUE, and at the right alignment?  */
464   if (CGEN_INT_INSN_P || (word_offset == 0 && word_length == total_length))
465     {
466       if (CGEN_INSN_LSB0_P)
467         value = insn_value >> ((word_offset + start + 1) - length);
468       else
469         value = insn_value >> (total_length - ( word_offset + start + length));
470     }
472 #if ! CGEN_INT_INSN_P
474   else
475     {
476       unsigned char *bufp = ex_info->insn_bytes + word_offset / 8;
478       if (word_length > 8 * sizeof (CGEN_INSN_INT))
479         abort ();
481       if (fill_cache (cd, ex_info, word_offset / 8, word_length / 8, pc) == 0)
482         return 0;
484       value = extract_1 (cd, ex_info, start, length, word_length, bufp, pc);
485     }
487 #endif /* ! CGEN_INT_INSN_P */
489   /* Written this way to avoid undefined behaviour.  */
490   mask = (((1L << (length - 1)) - 1) << 1) | 1;
492   value &= mask;
493   /* sign extend? */
494   if (CGEN_BOOL_ATTR (attrs, CGEN_IFLD_SIGNED)
495       && (value & (1L << (length - 1))))
496     value |= ~mask;
498   *valuep = value;
500   return 1;
503 /* Default insn extractor.
505    INSN_VALUE is the first base_insn_bitsize bits, translated to host order.
506    The extracted fields are stored in FIELDS.
507    EX_INFO is used to handle reading variable length insns.
508    Return the length of the insn in bits, or 0 if no match,
509    or -1 if an error occurs fetching data (memory_error_func will have
510    been called).  */
512 static int
513 extract_insn_normal (CGEN_CPU_DESC cd,
514                      const CGEN_INSN *insn,
515                      CGEN_EXTRACT_INFO *ex_info,
516                      CGEN_INSN_INT insn_value,
517                      CGEN_FIELDS *fields,
518                      bfd_vma pc)
520   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
521   const CGEN_SYNTAX_CHAR_TYPE *syn;
523   CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
525   CGEN_INIT_EXTRACT (cd);
527   for (syn = CGEN_SYNTAX_STRING (syntax); *syn; ++syn)
528     {
529       int length;
531       if (CGEN_SYNTAX_CHAR_P (*syn))
532         continue;
534       length = (* cd->extract_operand) (cd, CGEN_SYNTAX_FIELD (*syn),
535                                         ex_info, insn_value, fields, pc);
536       if (length <= 0)
537         return length;
538     }
540   /* We recognized and successfully extracted this insn.  */
541   return CGEN_INSN_BITSIZE (insn);
544 /* Machine generated code added here.  */