Fix seg fault in linker when performing garbage collection on COFF based targets.
[external/binutils.git] / opcodes / xstormy16-ibld.c
1 /* Instruction building/extraction support for xstormy16. -*- C -*-
2
3    THIS FILE IS MACHINE GENERATED WITH CGEN: Cpu tools GENerator.
4    - the resultant file is machine generated, cgen-ibld.in isn't
5
6    Copyright (C) 1996-2016 Free Software Foundation, Inc.
7
8    This file is part of libopcodes.
9
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.
14
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.
19
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.  */
23
24 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
25    Keep that in mind.  */
26
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 "xstormy16-desc.h"
34 #include "xstormy16-opc.h"
35 #include "cgen/basic-modes.h"
36 #include "opintl.h"
37 #include "safe-ctype.h"
38
39 #undef  min
40 #define min(a,b) ((a) < (b) ? (a) : (b))
41 #undef  max
42 #define max(a,b) ((a) > (b) ? (a) : (b))
43
44 /* Used by the ifield rtx function.  */
45 #define FLD(f) (fields->f)
46
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
72 \f
73 /* Operand insertion.  */
74
75 #if ! CGEN_INT_INSN_P
76
77 /* Subroutine of insert_normal.  */
78
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)
86 {
87   unsigned long x,mask;
88   int shift;
89
90   x = cgen_get_insn_value (cd, bufp, word_length);
91
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);
99
100   cgen_put_insn_value (cd, bufp, word_length, (bfd_vma) x);
101 }
102
103 #endif /* ! CGEN_INT_INSN_P */
104
105 /* Default insertion routine.
106
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.
113
114    The result is an error message or NULL if success.  */
115
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.  */
120
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)
131 {
132   static char errbuf[100];
133   /* Written this way to avoid undefined behaviour.  */
134   unsigned long mask = (((1L << (length - 1)) - 1) << 1) | 1;
135
136   /* If LENGTH is zero, this operand doesn't contribute to the value.  */
137   if (length == 0)
138     return NULL;
139
140   if (word_length > 8 * sizeof (CGEN_INSN_INT))
141     abort ();
142
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     }
151
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;
157
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;
172
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;
179
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;
195
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     }
206
207 #if CGEN_INT_INSN_P
208
209   {
210     int shift_within_word, shift_to_word, shift;
211
212     /* How to shift the value to BIT0 of the word.  */
213     shift_to_word = total_length - (word_offset + word_length);
214
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;
220
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   }
225
226 #else /* ! CGEN_INT_INSN_P */
227
228   {
229     unsigned char *bufp = (unsigned char *) buffer + word_offset / 8;
230
231     insert_1 (cd, value, start, length, word_length, bufp);
232   }
233
234 #endif /* ! CGEN_INT_INSN_P */
235
236   return NULL;
237 }
238
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.  */
245
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)
252 {
253   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
254   unsigned long value;
255   const CGEN_SYNTAX_CHAR_TYPE * syn;
256
257   CGEN_INIT_INSERT (cd);
258   value = CGEN_INSN_BASE_VALUE (insn);
259
260   /* If we're recording insns as numbers (rather than a string of bytes),
261      target byte order handling is deferred until later.  */
262
263 #if CGEN_INT_INSN_P
264
265   put_insn_int_value (cd, buffer, cd->base_insn_bitsize,
266                       CGEN_FIELDS_BITSIZE (fields), value);
267
268 #else
269
270   cgen_put_insn_value (cd, buffer, min ((unsigned) cd->base_insn_bitsize,
271                                         (unsigned) CGEN_FIELDS_BITSIZE (fields)),
272                        value);
273
274 #endif /* ! CGEN_INT_INSN_P */
275
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.  */
280
281   for (syn = CGEN_SYNTAX_STRING (syntax); * syn; ++ syn)
282     {
283       const char *errmsg;
284
285       if (CGEN_SYNTAX_CHAR_P (* syn))
286         continue;
287
288       errmsg = (* cd->insert_operand) (cd, CGEN_SYNTAX_FIELD (*syn),
289                                        fields, buffer, pc);
290       if (errmsg)
291         return errmsg;
292     }
293
294   return NULL;
295 }
296
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.  */
300
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)
307 {
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;
317
318       *buf = (*buf & ~(mask << shift)) | ((value & mask) << shift);
319     }
320 }
321 #endif
322 \f
323 /* Operand extraction.  */
324
325 #if ! CGEN_INT_INSN_P
326
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.  */
332
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)
339 {
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;
344
345   /* First do a quick check.  */
346   mask = (1 << bytes) - 1;
347   if (((ex_info->valid >> offset) & mask) == mask)
348     return 1;
349
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;
354
355   if (bytes)
356     {
357       int status;
358
359       pc += offset;
360       status = (*info->read_memory_func)
361         (pc, ex_info->insn_bytes + offset, bytes, info);
362
363       if (status != 0)
364         {
365           (*info->memory_error_func) (status, pc, info);
366           return 0;
367         }
368
369       ex_info->valid |= ((1 << bytes) - 1) << offset;
370     }
371
372   return 1;
373 }
374
375 /* Subroutine of extract_normal.  */
376
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)
385 {
386   unsigned long x;
387   int shift;
388
389   x = cgen_get_insn_value (cd, bufp, word_length);
390
391   if (CGEN_INSN_LSB0_P)
392     shift = (start + 1) - length;
393   else
394     shift = (word_length - (start + length));
395   return x >> shift;
396 }
397
398 #endif /* ! CGEN_INT_INSN_P */
399
400 /* Default extraction routine.
401
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.
412
413    Returns 1 for success, 0 for failure.  */
414
415 /* ??? The return code isn't properly used.  wip.  */
416
417 /* ??? This doesn't handle bfd_vma's.  Create another function when
418    necessary.  */
419
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)
440 {
441   long value, mask;
442
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     }
450
451   if (word_length > 8 * sizeof (CGEN_INSN_INT))
452     abort ();
453
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     }
461
462   /* Does the value reside in INSN_VALUE, and at the right alignment?  */
463
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     }
471
472 #if ! CGEN_INT_INSN_P
473
474   else
475     {
476       unsigned char *bufp = ex_info->insn_bytes + word_offset / 8;
477
478       if (word_length > 8 * sizeof (CGEN_INSN_INT))
479         abort ();
480
481       if (fill_cache (cd, ex_info, word_offset / 8, word_length / 8, pc) == 0)
482         return 0;
483
484       value = extract_1 (cd, ex_info, start, length, word_length, bufp, pc);
485     }
486
487 #endif /* ! CGEN_INT_INSN_P */
488
489   /* Written this way to avoid undefined behaviour.  */
490   mask = (((1L << (length - 1)) - 1) << 1) | 1;
491
492   value &= mask;
493   /* sign extend? */
494   if (CGEN_BOOL_ATTR (attrs, CGEN_IFLD_SIGNED)
495       && (value & (1L << (length - 1))))
496     value |= ~mask;
497
498   *valuep = value;
499
500   return 1;
501 }
502
503 /* Default insn extractor.
504
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).  */
511
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)
519 {
520   const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
521   const CGEN_SYNTAX_CHAR_TYPE *syn;
522
523   CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
524
525   CGEN_INIT_EXTRACT (cd);
526
527   for (syn = CGEN_SYNTAX_STRING (syntax); *syn; ++syn)
528     {
529       int length;
530
531       if (CGEN_SYNTAX_CHAR_P (*syn))
532         continue;
533
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     }
539
540   /* We recognized and successfully extracted this insn.  */
541   return CGEN_INSN_BITSIZE (insn);
542 }
543 \f
544 /* Machine generated code added here.  */
545
546 const char * xstormy16_cgen_insert_operand
547   (CGEN_CPU_DESC, int, CGEN_FIELDS *, CGEN_INSN_BYTES_PTR, bfd_vma);
548
549 /* Main entry point for operand insertion.
550
551    This function is basically just a big switch statement.  Earlier versions
552    used tables to look up the function to use, but
553    - if the table contains both assembler and disassembler functions then
554      the disassembler contains much of the assembler and vice-versa,
555    - there's a lot of inlining possibilities as things grow,
556    - using a switch statement avoids the function call overhead.
557
558    This function could be moved into `parse_insn_normal', but keeping it
559    separate makes clear the interface between `parse_insn_normal' and each of
560    the handlers.  It's also needed by GAS to insert operands that couldn't be
561    resolved during parsing.  */
562
563 const char *
564 xstormy16_cgen_insert_operand (CGEN_CPU_DESC cd,
565                              int opindex,
566                              CGEN_FIELDS * fields,
567                              CGEN_INSN_BYTES_PTR buffer,
568                              bfd_vma pc ATTRIBUTE_UNUSED)
569 {
570   const char * errmsg = NULL;
571   unsigned int total_length = CGEN_FIELDS_BITSIZE (fields);
572
573   switch (opindex)
574     {
575     case XSTORMY16_OPERAND_RB :
576       errmsg = insert_normal (cd, fields->f_Rb, 0, 0, 17, 3, 32, total_length, buffer);
577       break;
578     case XSTORMY16_OPERAND_RBJ :
579       errmsg = insert_normal (cd, fields->f_Rbj, 0, 0, 11, 1, 32, total_length, buffer);
580       break;
581     case XSTORMY16_OPERAND_RD :
582       errmsg = insert_normal (cd, fields->f_Rd, 0, 0, 12, 4, 32, total_length, buffer);
583       break;
584     case XSTORMY16_OPERAND_RDM :
585       errmsg = insert_normal (cd, fields->f_Rdm, 0, 0, 13, 3, 32, total_length, buffer);
586       break;
587     case XSTORMY16_OPERAND_RM :
588       errmsg = insert_normal (cd, fields->f_Rm, 0, 0, 4, 3, 32, total_length, buffer);
589       break;
590     case XSTORMY16_OPERAND_RS :
591       errmsg = insert_normal (cd, fields->f_Rs, 0, 0, 8, 4, 32, total_length, buffer);
592       break;
593     case XSTORMY16_OPERAND_ABS24 :
594       {
595 {
596   FLD (f_abs24_1) = ((FLD (f_abs24)) & (255));
597   FLD (f_abs24_2) = ((UINT) (FLD (f_abs24)) >> (8));
598 }
599         errmsg = insert_normal (cd, fields->f_abs24_1, 0, 0, 8, 8, 32, total_length, buffer);
600         if (errmsg)
601           break;
602         errmsg = insert_normal (cd, fields->f_abs24_2, 0, 0, 16, 16, 32, total_length, buffer);
603         if (errmsg)
604           break;
605       }
606       break;
607     case XSTORMY16_OPERAND_BCOND2 :
608       errmsg = insert_normal (cd, fields->f_op2, 0, 0, 4, 4, 32, total_length, buffer);
609       break;
610     case XSTORMY16_OPERAND_BCOND5 :
611       errmsg = insert_normal (cd, fields->f_op5, 0, 0, 16, 4, 32, total_length, buffer);
612       break;
613     case XSTORMY16_OPERAND_HMEM8 :
614       {
615         long value = fields->f_hmem8;
616         value = ((value) - (32512));
617         errmsg = insert_normal (cd, value, 0|(1<<CGEN_IFLD_ABS_ADDR), 0, 8, 8, 32, total_length, buffer);
618       }
619       break;
620     case XSTORMY16_OPERAND_IMM12 :
621       errmsg = insert_normal (cd, fields->f_imm12, 0|(1<<CGEN_IFLD_SIGNED), 0, 20, 12, 32, total_length, buffer);
622       break;
623     case XSTORMY16_OPERAND_IMM16 :
624       errmsg = insert_normal (cd, fields->f_imm16, 0|(1<<CGEN_IFLD_SIGN_OPT), 0, 16, 16, 32, total_length, buffer);
625       break;
626     case XSTORMY16_OPERAND_IMM2 :
627       errmsg = insert_normal (cd, fields->f_imm2, 0, 0, 10, 2, 32, total_length, buffer);
628       break;
629     case XSTORMY16_OPERAND_IMM3 :
630       errmsg = insert_normal (cd, fields->f_imm3, 0, 0, 4, 3, 32, total_length, buffer);
631       break;
632     case XSTORMY16_OPERAND_IMM3B :
633       errmsg = insert_normal (cd, fields->f_imm3b, 0, 0, 17, 3, 32, total_length, buffer);
634       break;
635     case XSTORMY16_OPERAND_IMM4 :
636       errmsg = insert_normal (cd, fields->f_imm4, 0, 0, 8, 4, 32, total_length, buffer);
637       break;
638     case XSTORMY16_OPERAND_IMM8 :
639       errmsg = insert_normal (cd, fields->f_imm8, 0, 0, 8, 8, 32, total_length, buffer);
640       break;
641     case XSTORMY16_OPERAND_IMM8SMALL :
642       errmsg = insert_normal (cd, fields->f_imm8, 0, 0, 8, 8, 32, total_length, buffer);
643       break;
644     case XSTORMY16_OPERAND_LMEM8 :
645       errmsg = insert_normal (cd, fields->f_lmem8, 0|(1<<CGEN_IFLD_ABS_ADDR), 0, 8, 8, 32, total_length, buffer);
646       break;
647     case XSTORMY16_OPERAND_REL12 :
648       {
649         long value = fields->f_rel12;
650         value = ((value) - (((pc) + (4))));
651         errmsg = insert_normal (cd, value, 0|(1<<CGEN_IFLD_SIGNED)|(1<<CGEN_IFLD_PCREL_ADDR), 0, 20, 12, 32, total_length, buffer);
652       }
653       break;
654     case XSTORMY16_OPERAND_REL12A :
655       {
656         long value = fields->f_rel12a;
657         value = ((SI) (((value) - (((pc) + (2))))) >> (1));
658         errmsg = insert_normal (cd, value, 0|(1<<CGEN_IFLD_SIGNED)|(1<<CGEN_IFLD_PCREL_ADDR), 0, 4, 11, 32, total_length, buffer);
659       }
660       break;
661     case XSTORMY16_OPERAND_REL8_2 :
662       {
663         long value = fields->f_rel8_2;
664         value = ((value) - (((pc) + (2))));
665         errmsg = insert_normal (cd, value, 0|(1<<CGEN_IFLD_SIGNED)|(1<<CGEN_IFLD_PCREL_ADDR), 0, 8, 8, 32, total_length, buffer);
666       }
667       break;
668     case XSTORMY16_OPERAND_REL8_4 :
669       {
670         long value = fields->f_rel8_4;
671         value = ((value) - (((pc) + (4))));
672         errmsg = insert_normal (cd, value, 0|(1<<CGEN_IFLD_SIGNED)|(1<<CGEN_IFLD_PCREL_ADDR), 0, 8, 8, 32, total_length, buffer);
673       }
674       break;
675     case XSTORMY16_OPERAND_WS2 :
676       errmsg = insert_normal (cd, fields->f_op2m, 0, 0, 7, 1, 32, total_length, buffer);
677       break;
678
679     default :
680       /* xgettext:c-format */
681       fprintf (stderr, _("Unrecognized field %d while building insn.\n"),
682                opindex);
683       abort ();
684   }
685
686   return errmsg;
687 }
688
689 int xstormy16_cgen_extract_operand
690   (CGEN_CPU_DESC, int, CGEN_EXTRACT_INFO *, CGEN_INSN_INT, CGEN_FIELDS *, bfd_vma);
691
692 /* Main entry point for operand extraction.
693    The result is <= 0 for error, >0 for success.
694    ??? Actual values aren't well defined right now.
695
696    This function is basically just a big switch statement.  Earlier versions
697    used tables to look up the function to use, but
698    - if the table contains both assembler and disassembler functions then
699      the disassembler contains much of the assembler and vice-versa,
700    - there's a lot of inlining possibilities as things grow,
701    - using a switch statement avoids the function call overhead.
702
703    This function could be moved into `print_insn_normal', but keeping it
704    separate makes clear the interface between `print_insn_normal' and each of
705    the handlers.  */
706
707 int
708 xstormy16_cgen_extract_operand (CGEN_CPU_DESC cd,
709                              int opindex,
710                              CGEN_EXTRACT_INFO *ex_info,
711                              CGEN_INSN_INT insn_value,
712                              CGEN_FIELDS * fields,
713                              bfd_vma pc)
714 {
715   /* Assume success (for those operands that are nops).  */
716   int length = 1;
717   unsigned int total_length = CGEN_FIELDS_BITSIZE (fields);
718
719   switch (opindex)
720     {
721     case XSTORMY16_OPERAND_RB :
722       length = extract_normal (cd, ex_info, insn_value, 0, 0, 17, 3, 32, total_length, pc, & fields->f_Rb);
723       break;
724     case XSTORMY16_OPERAND_RBJ :
725       length = extract_normal (cd, ex_info, insn_value, 0, 0, 11, 1, 32, total_length, pc, & fields->f_Rbj);
726       break;
727     case XSTORMY16_OPERAND_RD :
728       length = extract_normal (cd, ex_info, insn_value, 0, 0, 12, 4, 32, total_length, pc, & fields->f_Rd);
729       break;
730     case XSTORMY16_OPERAND_RDM :
731       length = extract_normal (cd, ex_info, insn_value, 0, 0, 13, 3, 32, total_length, pc, & fields->f_Rdm);
732       break;
733     case XSTORMY16_OPERAND_RM :
734       length = extract_normal (cd, ex_info, insn_value, 0, 0, 4, 3, 32, total_length, pc, & fields->f_Rm);
735       break;
736     case XSTORMY16_OPERAND_RS :
737       length = extract_normal (cd, ex_info, insn_value, 0, 0, 8, 4, 32, total_length, pc, & fields->f_Rs);
738       break;
739     case XSTORMY16_OPERAND_ABS24 :
740       {
741         length = extract_normal (cd, ex_info, insn_value, 0, 0, 8, 8, 32, total_length, pc, & fields->f_abs24_1);
742         if (length <= 0) break;
743         length = extract_normal (cd, ex_info, insn_value, 0, 0, 16, 16, 32, total_length, pc, & fields->f_abs24_2);
744         if (length <= 0) break;
745   FLD (f_abs24) = ((((FLD (f_abs24_2)) << (8))) | (FLD (f_abs24_1)));
746       }
747       break;
748     case XSTORMY16_OPERAND_BCOND2 :
749       length = extract_normal (cd, ex_info, insn_value, 0, 0, 4, 4, 32, total_length, pc, & fields->f_op2);
750       break;
751     case XSTORMY16_OPERAND_BCOND5 :
752       length = extract_normal (cd, ex_info, insn_value, 0, 0, 16, 4, 32, total_length, pc, & fields->f_op5);
753       break;
754     case XSTORMY16_OPERAND_HMEM8 :
755       {
756         long value;
757         length = extract_normal (cd, ex_info, insn_value, 0|(1<<CGEN_IFLD_ABS_ADDR), 0, 8, 8, 32, total_length, pc, & value);
758         value = ((value) + (32512));
759         fields->f_hmem8 = value;
760       }
761       break;
762     case XSTORMY16_OPERAND_IMM12 :
763       length = extract_normal (cd, ex_info, insn_value, 0|(1<<CGEN_IFLD_SIGNED), 0, 20, 12, 32, total_length, pc, & fields->f_imm12);
764       break;
765     case XSTORMY16_OPERAND_IMM16 :
766       length = extract_normal (cd, ex_info, insn_value, 0|(1<<CGEN_IFLD_SIGN_OPT), 0, 16, 16, 32, total_length, pc, & fields->f_imm16);
767       break;
768     case XSTORMY16_OPERAND_IMM2 :
769       length = extract_normal (cd, ex_info, insn_value, 0, 0, 10, 2, 32, total_length, pc, & fields->f_imm2);
770       break;
771     case XSTORMY16_OPERAND_IMM3 :
772       length = extract_normal (cd, ex_info, insn_value, 0, 0, 4, 3, 32, total_length, pc, & fields->f_imm3);
773       break;
774     case XSTORMY16_OPERAND_IMM3B :
775       length = extract_normal (cd, ex_info, insn_value, 0, 0, 17, 3, 32, total_length, pc, & fields->f_imm3b);
776       break;
777     case XSTORMY16_OPERAND_IMM4 :
778       length = extract_normal (cd, ex_info, insn_value, 0, 0, 8, 4, 32, total_length, pc, & fields->f_imm4);
779       break;
780     case XSTORMY16_OPERAND_IMM8 :
781       length = extract_normal (cd, ex_info, insn_value, 0, 0, 8, 8, 32, total_length, pc, & fields->f_imm8);
782       break;
783     case XSTORMY16_OPERAND_IMM8SMALL :
784       length = extract_normal (cd, ex_info, insn_value, 0, 0, 8, 8, 32, total_length, pc, & fields->f_imm8);
785       break;
786     case XSTORMY16_OPERAND_LMEM8 :
787       length = extract_normal (cd, ex_info, insn_value, 0|(1<<CGEN_IFLD_ABS_ADDR), 0, 8, 8, 32, total_length, pc, & fields->f_lmem8);
788       break;
789     case XSTORMY16_OPERAND_REL12 :
790       {
791         long value;
792         length = extract_normal (cd, ex_info, insn_value, 0|(1<<CGEN_IFLD_SIGNED)|(1<<CGEN_IFLD_PCREL_ADDR), 0, 20, 12, 32, total_length, pc, & value);
793         value = ((value) + (((pc) + (4))));
794         fields->f_rel12 = value;
795       }
796       break;
797     case XSTORMY16_OPERAND_REL12A :
798       {
799         long value;
800         length = extract_normal (cd, ex_info, insn_value, 0|(1<<CGEN_IFLD_SIGNED)|(1<<CGEN_IFLD_PCREL_ADDR), 0, 4, 11, 32, total_length, pc, & value);
801         value = ((((value) << (1))) + (((pc) + (2))));
802         fields->f_rel12a = value;
803       }
804       break;
805     case XSTORMY16_OPERAND_REL8_2 :
806       {
807         long value;
808         length = extract_normal (cd, ex_info, insn_value, 0|(1<<CGEN_IFLD_SIGNED)|(1<<CGEN_IFLD_PCREL_ADDR), 0, 8, 8, 32, total_length, pc, & value);
809         value = ((value) + (((pc) + (2))));
810         fields->f_rel8_2 = value;
811       }
812       break;
813     case XSTORMY16_OPERAND_REL8_4 :
814       {
815         long value;
816         length = extract_normal (cd, ex_info, insn_value, 0|(1<<CGEN_IFLD_SIGNED)|(1<<CGEN_IFLD_PCREL_ADDR), 0, 8, 8, 32, total_length, pc, & value);
817         value = ((value) + (((pc) + (4))));
818         fields->f_rel8_4 = value;
819       }
820       break;
821     case XSTORMY16_OPERAND_WS2 :
822       length = extract_normal (cd, ex_info, insn_value, 0, 0, 7, 1, 32, total_length, pc, & fields->f_op2m);
823       break;
824
825     default :
826       /* xgettext:c-format */
827       fprintf (stderr, _("Unrecognized field %d while decoding insn.\n"),
828                opindex);
829       abort ();
830     }
831
832   return length;
833 }
834
835 cgen_insert_fn * const xstormy16_cgen_insert_handlers[] =
836 {
837   insert_insn_normal,
838 };
839
840 cgen_extract_fn * const xstormy16_cgen_extract_handlers[] =
841 {
842   extract_insn_normal,
843 };
844
845 int xstormy16_cgen_get_int_operand     (CGEN_CPU_DESC, int, const CGEN_FIELDS *);
846 bfd_vma xstormy16_cgen_get_vma_operand (CGEN_CPU_DESC, int, const CGEN_FIELDS *);
847
848 /* Getting values from cgen_fields is handled by a collection of functions.
849    They are distinguished by the type of the VALUE argument they return.
850    TODO: floating point, inlining support, remove cases where result type
851    not appropriate.  */
852
853 int
854 xstormy16_cgen_get_int_operand (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
855                              int opindex,
856                              const CGEN_FIELDS * fields)
857 {
858   int value;
859
860   switch (opindex)
861     {
862     case XSTORMY16_OPERAND_RB :
863       value = fields->f_Rb;
864       break;
865     case XSTORMY16_OPERAND_RBJ :
866       value = fields->f_Rbj;
867       break;
868     case XSTORMY16_OPERAND_RD :
869       value = fields->f_Rd;
870       break;
871     case XSTORMY16_OPERAND_RDM :
872       value = fields->f_Rdm;
873       break;
874     case XSTORMY16_OPERAND_RM :
875       value = fields->f_Rm;
876       break;
877     case XSTORMY16_OPERAND_RS :
878       value = fields->f_Rs;
879       break;
880     case XSTORMY16_OPERAND_ABS24 :
881       value = fields->f_abs24;
882       break;
883     case XSTORMY16_OPERAND_BCOND2 :
884       value = fields->f_op2;
885       break;
886     case XSTORMY16_OPERAND_BCOND5 :
887       value = fields->f_op5;
888       break;
889     case XSTORMY16_OPERAND_HMEM8 :
890       value = fields->f_hmem8;
891       break;
892     case XSTORMY16_OPERAND_IMM12 :
893       value = fields->f_imm12;
894       break;
895     case XSTORMY16_OPERAND_IMM16 :
896       value = fields->f_imm16;
897       break;
898     case XSTORMY16_OPERAND_IMM2 :
899       value = fields->f_imm2;
900       break;
901     case XSTORMY16_OPERAND_IMM3 :
902       value = fields->f_imm3;
903       break;
904     case XSTORMY16_OPERAND_IMM3B :
905       value = fields->f_imm3b;
906       break;
907     case XSTORMY16_OPERAND_IMM4 :
908       value = fields->f_imm4;
909       break;
910     case XSTORMY16_OPERAND_IMM8 :
911       value = fields->f_imm8;
912       break;
913     case XSTORMY16_OPERAND_IMM8SMALL :
914       value = fields->f_imm8;
915       break;
916     case XSTORMY16_OPERAND_LMEM8 :
917       value = fields->f_lmem8;
918       break;
919     case XSTORMY16_OPERAND_REL12 :
920       value = fields->f_rel12;
921       break;
922     case XSTORMY16_OPERAND_REL12A :
923       value = fields->f_rel12a;
924       break;
925     case XSTORMY16_OPERAND_REL8_2 :
926       value = fields->f_rel8_2;
927       break;
928     case XSTORMY16_OPERAND_REL8_4 :
929       value = fields->f_rel8_4;
930       break;
931     case XSTORMY16_OPERAND_WS2 :
932       value = fields->f_op2m;
933       break;
934
935     default :
936       /* xgettext:c-format */
937       fprintf (stderr, _("Unrecognized field %d while getting int operand.\n"),
938                        opindex);
939       abort ();
940   }
941
942   return value;
943 }
944
945 bfd_vma
946 xstormy16_cgen_get_vma_operand (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
947                              int opindex,
948                              const CGEN_FIELDS * fields)
949 {
950   bfd_vma value;
951
952   switch (opindex)
953     {
954     case XSTORMY16_OPERAND_RB :
955       value = fields->f_Rb;
956       break;
957     case XSTORMY16_OPERAND_RBJ :
958       value = fields->f_Rbj;
959       break;
960     case XSTORMY16_OPERAND_RD :
961       value = fields->f_Rd;
962       break;
963     case XSTORMY16_OPERAND_RDM :
964       value = fields->f_Rdm;
965       break;
966     case XSTORMY16_OPERAND_RM :
967       value = fields->f_Rm;
968       break;
969     case XSTORMY16_OPERAND_RS :
970       value = fields->f_Rs;
971       break;
972     case XSTORMY16_OPERAND_ABS24 :
973       value = fields->f_abs24;
974       break;
975     case XSTORMY16_OPERAND_BCOND2 :
976       value = fields->f_op2;
977       break;
978     case XSTORMY16_OPERAND_BCOND5 :
979       value = fields->f_op5;
980       break;
981     case XSTORMY16_OPERAND_HMEM8 :
982       value = fields->f_hmem8;
983       break;
984     case XSTORMY16_OPERAND_IMM12 :
985       value = fields->f_imm12;
986       break;
987     case XSTORMY16_OPERAND_IMM16 :
988       value = fields->f_imm16;
989       break;
990     case XSTORMY16_OPERAND_IMM2 :
991       value = fields->f_imm2;
992       break;
993     case XSTORMY16_OPERAND_IMM3 :
994       value = fields->f_imm3;
995       break;
996     case XSTORMY16_OPERAND_IMM3B :
997       value = fields->f_imm3b;
998       break;
999     case XSTORMY16_OPERAND_IMM4 :
1000       value = fields->f_imm4;
1001       break;
1002     case XSTORMY16_OPERAND_IMM8 :
1003       value = fields->f_imm8;
1004       break;
1005     case XSTORMY16_OPERAND_IMM8SMALL :
1006       value = fields->f_imm8;
1007       break;
1008     case XSTORMY16_OPERAND_LMEM8 :
1009       value = fields->f_lmem8;
1010       break;
1011     case XSTORMY16_OPERAND_REL12 :
1012       value = fields->f_rel12;
1013       break;
1014     case XSTORMY16_OPERAND_REL12A :
1015       value = fields->f_rel12a;
1016       break;
1017     case XSTORMY16_OPERAND_REL8_2 :
1018       value = fields->f_rel8_2;
1019       break;
1020     case XSTORMY16_OPERAND_REL8_4 :
1021       value = fields->f_rel8_4;
1022       break;
1023     case XSTORMY16_OPERAND_WS2 :
1024       value = fields->f_op2m;
1025       break;
1026
1027     default :
1028       /* xgettext:c-format */
1029       fprintf (stderr, _("Unrecognized field %d while getting vma operand.\n"),
1030                        opindex);
1031       abort ();
1032   }
1033
1034   return value;
1035 }
1036
1037 void xstormy16_cgen_set_int_operand  (CGEN_CPU_DESC, int, CGEN_FIELDS *, int);
1038 void xstormy16_cgen_set_vma_operand  (CGEN_CPU_DESC, int, CGEN_FIELDS *, bfd_vma);
1039
1040 /* Stuffing values in cgen_fields is handled by a collection of functions.
1041    They are distinguished by the type of the VALUE argument they accept.
1042    TODO: floating point, inlining support, remove cases where argument type
1043    not appropriate.  */
1044
1045 void
1046 xstormy16_cgen_set_int_operand (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
1047                              int opindex,
1048                              CGEN_FIELDS * fields,
1049                              int value)
1050 {
1051   switch (opindex)
1052     {
1053     case XSTORMY16_OPERAND_RB :
1054       fields->f_Rb = value;
1055       break;
1056     case XSTORMY16_OPERAND_RBJ :
1057       fields->f_Rbj = value;
1058       break;
1059     case XSTORMY16_OPERAND_RD :
1060       fields->f_Rd = value;
1061       break;
1062     case XSTORMY16_OPERAND_RDM :
1063       fields->f_Rdm = value;
1064       break;
1065     case XSTORMY16_OPERAND_RM :
1066       fields->f_Rm = value;
1067       break;
1068     case XSTORMY16_OPERAND_RS :
1069       fields->f_Rs = value;
1070       break;
1071     case XSTORMY16_OPERAND_ABS24 :
1072       fields->f_abs24 = value;
1073       break;
1074     case XSTORMY16_OPERAND_BCOND2 :
1075       fields->f_op2 = value;
1076       break;
1077     case XSTORMY16_OPERAND_BCOND5 :
1078       fields->f_op5 = value;
1079       break;
1080     case XSTORMY16_OPERAND_HMEM8 :
1081       fields->f_hmem8 = value;
1082       break;
1083     case XSTORMY16_OPERAND_IMM12 :
1084       fields->f_imm12 = value;
1085       break;
1086     case XSTORMY16_OPERAND_IMM16 :
1087       fields->f_imm16 = value;
1088       break;
1089     case XSTORMY16_OPERAND_IMM2 :
1090       fields->f_imm2 = value;
1091       break;
1092     case XSTORMY16_OPERAND_IMM3 :
1093       fields->f_imm3 = value;
1094       break;
1095     case XSTORMY16_OPERAND_IMM3B :
1096       fields->f_imm3b = value;
1097       break;
1098     case XSTORMY16_OPERAND_IMM4 :
1099       fields->f_imm4 = value;
1100       break;
1101     case XSTORMY16_OPERAND_IMM8 :
1102       fields->f_imm8 = value;
1103       break;
1104     case XSTORMY16_OPERAND_IMM8SMALL :
1105       fields->f_imm8 = value;
1106       break;
1107     case XSTORMY16_OPERAND_LMEM8 :
1108       fields->f_lmem8 = value;
1109       break;
1110     case XSTORMY16_OPERAND_REL12 :
1111       fields->f_rel12 = value;
1112       break;
1113     case XSTORMY16_OPERAND_REL12A :
1114       fields->f_rel12a = value;
1115       break;
1116     case XSTORMY16_OPERAND_REL8_2 :
1117       fields->f_rel8_2 = value;
1118       break;
1119     case XSTORMY16_OPERAND_REL8_4 :
1120       fields->f_rel8_4 = value;
1121       break;
1122     case XSTORMY16_OPERAND_WS2 :
1123       fields->f_op2m = value;
1124       break;
1125
1126     default :
1127       /* xgettext:c-format */
1128       fprintf (stderr, _("Unrecognized field %d while setting int operand.\n"),
1129                        opindex);
1130       abort ();
1131   }
1132 }
1133
1134 void
1135 xstormy16_cgen_set_vma_operand (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
1136                              int opindex,
1137                              CGEN_FIELDS * fields,
1138                              bfd_vma value)
1139 {
1140   switch (opindex)
1141     {
1142     case XSTORMY16_OPERAND_RB :
1143       fields->f_Rb = value;
1144       break;
1145     case XSTORMY16_OPERAND_RBJ :
1146       fields->f_Rbj = value;
1147       break;
1148     case XSTORMY16_OPERAND_RD :
1149       fields->f_Rd = value;
1150       break;
1151     case XSTORMY16_OPERAND_RDM :
1152       fields->f_Rdm = value;
1153       break;
1154     case XSTORMY16_OPERAND_RM :
1155       fields->f_Rm = value;
1156       break;
1157     case XSTORMY16_OPERAND_RS :
1158       fields->f_Rs = value;
1159       break;
1160     case XSTORMY16_OPERAND_ABS24 :
1161       fields->f_abs24 = value;
1162       break;
1163     case XSTORMY16_OPERAND_BCOND2 :
1164       fields->f_op2 = value;
1165       break;
1166     case XSTORMY16_OPERAND_BCOND5 :
1167       fields->f_op5 = value;
1168       break;
1169     case XSTORMY16_OPERAND_HMEM8 :
1170       fields->f_hmem8 = value;
1171       break;
1172     case XSTORMY16_OPERAND_IMM12 :
1173       fields->f_imm12 = value;
1174       break;
1175     case XSTORMY16_OPERAND_IMM16 :
1176       fields->f_imm16 = value;
1177       break;
1178     case XSTORMY16_OPERAND_IMM2 :
1179       fields->f_imm2 = value;
1180       break;
1181     case XSTORMY16_OPERAND_IMM3 :
1182       fields->f_imm3 = value;
1183       break;
1184     case XSTORMY16_OPERAND_IMM3B :
1185       fields->f_imm3b = value;
1186       break;
1187     case XSTORMY16_OPERAND_IMM4 :
1188       fields->f_imm4 = value;
1189       break;
1190     case XSTORMY16_OPERAND_IMM8 :
1191       fields->f_imm8 = value;
1192       break;
1193     case XSTORMY16_OPERAND_IMM8SMALL :
1194       fields->f_imm8 = value;
1195       break;
1196     case XSTORMY16_OPERAND_LMEM8 :
1197       fields->f_lmem8 = value;
1198       break;
1199     case XSTORMY16_OPERAND_REL12 :
1200       fields->f_rel12 = value;
1201       break;
1202     case XSTORMY16_OPERAND_REL12A :
1203       fields->f_rel12a = value;
1204       break;
1205     case XSTORMY16_OPERAND_REL8_2 :
1206       fields->f_rel8_2 = value;
1207       break;
1208     case XSTORMY16_OPERAND_REL8_4 :
1209       fields->f_rel8_4 = value;
1210       break;
1211     case XSTORMY16_OPERAND_WS2 :
1212       fields->f_op2m = value;
1213       break;
1214
1215     default :
1216       /* xgettext:c-format */
1217       fprintf (stderr, _("Unrecognized field %d while setting vma operand.\n"),
1218                        opindex);
1219       abort ();
1220   }
1221 }
1222
1223 /* Function to call before using the instruction builder tables.  */
1224
1225 void
1226 xstormy16_cgen_init_ibld_table (CGEN_CPU_DESC cd)
1227 {
1228   cd->insert_handlers = & xstormy16_cgen_insert_handlers[0];
1229   cd->extract_handlers = & xstormy16_cgen_extract_handlers[0];
1230
1231   cd->insert_operand = xstormy16_cgen_insert_operand;
1232   cd->extract_operand = xstormy16_cgen_extract_operand;
1233
1234   cd->get_int_operand = xstormy16_cgen_get_int_operand;
1235   cd->set_int_operand = xstormy16_cgen_set_int_operand;
1236   cd->get_vma_operand = xstormy16_cgen_get_vma_operand;
1237   cd->set_vma_operand = xstormy16_cgen_set_vma_operand;
1238 }