1 /* tc-xgate.c -- Assembler code for Freescale XGATE
2 Copyright 2010, 2011, 2012
3 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street - Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include "safe-ctype.h"
26 #include "opcode/xgate.h"
27 #include "dwarf2dbg.h"
28 #include "elf/xgate.h"
30 const char comment_chars[] = ";!";
31 const char line_comment_chars[] = "#*";
32 const char line_separator_chars[] = "";
33 const char EXP_CHARS[] = "eE";
34 const char FLT_CHARS[] = "dD";
36 #define SIXTEENTH_BIT 0x8000
37 #define N_BITS_IN_WORD 16
39 /* #define STATE_CONDITIONAL_BRANCH (1) */
40 #define STATE_PC_RELATIVE (2)
41 #define REGISTER_P(ptr) (ptr == 'r')
44 #define MAXREGISTER 07
45 #define MINREGISTER 00
47 #define OPTION_MMCU 'm'
49 /* This macro has no side-effects. */
50 #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
52 /* Each unique opcode name has a handle. That handle may
53 contain pointers to opcodes with the same name but
54 different address modes. */
55 struct xgate_opcode_handle
59 struct xgate_opcode *opc0[MAX_OPCODES];
64 xgate_parse_exp (char *, expressionS *);
66 skip_whitespace (char *);
68 get_default_target (void);
70 extract_word (char *, char *, int);
72 xgate_new_instruction (int size);
74 xgate_apply_operand (unsigned short, unsigned short *, unsigned short,
77 xgate_operands (struct xgate_opcode *, char **);
79 xgate_operand (struct xgate_opcode *, int *, int where, char **, char **);
80 static struct xgate_opcode *
81 xgate_find_match (struct xgate_opcode_handle *, int, unsigned int);
83 cmp_opcode (struct xgate_opcode *, struct xgate_opcode *);
85 xgate_detect_format (char *);
87 xgate_print_syntax (char *);
89 xgate_print_table (void);
92 static struct hash_control *xgate_hash;
94 /* Previous opcode. */
95 static unsigned int prev = 0;
97 static unsigned char fixup_required = 0;
99 /* Used to enable clipping of 16 bit operands into 8 bit constraints. */
100 static unsigned char macroClipping = 0;
102 static char oper_check;
103 static char flag_print_insn_syntax = 0;
104 static char flag_print_opcodes = 0;
106 static int current_architecture;
107 static const char *default_cpu;
109 /* ELF flags to set in the output file header. */
110 static int elf_flags = E_XGATE_F64;
112 /* This table describes how you change sizes for the various types of variable
113 size expressions. This version only supports two kinds. */
116 How far Forward this mode will reach.
117 How far Backward this mode will reach.
118 How many bytes this mode will add to the size of the frag.
119 Which mode to go to if the offset won't fit in this one. */
121 relax_typeS md_relax_table[] =
123 {1, 1, 0, 0}, /* First entries aren't used. */
124 {1, 1, 0, 0}, /* For no good reason except. */
125 {1, 1, 0, 0}, /* that the VAX doesn't either. */
127 /* XGATE 9 and 10 bit pc rel todo complete and test */
128 /*{(511), (-512), 0, ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD)},
129 {(1023), (-1024), 0, ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD)}, */
133 /* XGATE's registers all are 16-bit general purpose. They are numbered according to the specifications. */
134 typedef enum register_id
149 /* This table describes all the machine specific pseudo-ops the assembler
150 has to support. The fields are: pseudo-op name without dot function to
151 call to execute this pseudo-op Integer arg to pass to the function. */
152 const pseudo_typeS md_pseudo_table[] =
154 /* The following pseudo-ops are supported for MRI compatibility. */
158 const char *md_shortopts = "m:";
160 struct option md_longopts[] =
162 #define OPTION_PRINT_INSN_SYNTAX (OPTION_MD_BASE + 0)
163 { "print-insn-syntax", no_argument, NULL, OPTION_PRINT_INSN_SYNTAX },
165 #define OPTION_PRINT_OPCODES (OPTION_MD_BASE + 1)
166 { "print-opcodes", no_argument, NULL, OPTION_PRINT_OPCODES },
168 #define OPTION_GENERATE_EXAMPLE (OPTION_MD_BASE + 2)
169 { "generate-example", no_argument, NULL, OPTION_GENERATE_EXAMPLE },
171 #define OPTION_MSHORT (OPTION_MD_BASE + 3)
172 { "mshort", no_argument, NULL, OPTION_MSHORT },
174 #define OPTION_MLONG (OPTION_MD_BASE + 4)
175 { "mlong", no_argument, NULL, OPTION_MLONG },
177 #define OPTION_MSHORT_DOUBLE (OPTION_MD_BASE + 5)
178 { "mshort-double", no_argument, NULL, OPTION_MSHORT_DOUBLE },
180 #define OPTION_MLONG_DOUBLE (OPTION_MD_BASE + 6)
181 { "mlong-double", no_argument, NULL, OPTION_MLONG_DOUBLE },
183 { NULL, no_argument, NULL, 0 }
186 size_t md_longopts_size = sizeof(md_longopts);
189 md_atof (int type, char *litP, int *sizeP)
191 return ieee_md_atof (type, litP, sizeP, TRUE);
195 md_parse_option (int c, char *arg)
200 if (strcasecmp (arg, "v1") == 0)
201 current_architecture = XGATE_V1;
202 else if (strcasecmp (arg, "v2") == 0)
203 current_architecture = XGATE_V2;
204 else if (strcasecmp (arg, "v3") == 0)
205 current_architecture = XGATE_V3;
207 as_bad (_(" architecture variant invalid"));
210 case OPTION_PRINT_INSN_SYNTAX:
211 flag_print_insn_syntax = 1;
214 case OPTION_PRINT_OPCODES:
215 flag_print_opcodes = 1;
218 case OPTION_GENERATE_EXAMPLE:
219 flag_print_opcodes = 2;
223 elf_flags &= ~E_XGATE_I32;
227 elf_flags |= E_XGATE_I32;
230 case OPTION_MSHORT_DOUBLE:
231 elf_flags &= ~E_XGATE_F64;
234 case OPTION_MLONG_DOUBLE:
235 elf_flags |= E_XGATE_F64;
245 xgate_arch_format (void)
247 get_default_target ();
249 if (current_architecture & cpuxgate)
250 return "elf32-xgate";
256 get_default_target (void)
258 const bfd_target *target;
261 if (current_architecture != 0)
264 default_cpu = "unknown";
265 target = bfd_find_target (0, &abfd);
267 if (target && target->name)
269 if (strcmp (target->name, "elf32-xgate") == 0)
271 current_architecture = cpuxgate;
272 default_cpu = "XGATE V1";
276 as_bad (_("Default target `%s' is not supported."), target->name);
283 struct xgate_opcode *xgate_opcode_ptr = NULL;
284 struct xgate_opcode *xgate_op_table = NULL;
285 struct xgate_opcode_handle *op_handles = 0;
286 char *prev_op_name = 0;
288 int number_of_op_handles = 0;
291 /* Create a local copy of our opcode table
292 including an extra line for NULL termination. */
293 xgate_op_table = (struct xgate_opcode *)
294 xmalloc ((xgate_num_opcodes) * sizeof (struct xgate_opcode));
296 memset (xgate_op_table, 0,
297 sizeof(struct xgate_opcode) * (xgate_num_opcodes));
299 for (xgate_opcode_ptr = (struct xgate_opcode*) xgate_opcodes, i = 0;
300 i < xgate_num_opcodes; i++)
301 xgate_op_table[i] = xgate_opcode_ptr[i];
303 qsort (xgate_op_table, xgate_num_opcodes, sizeof(struct xgate_opcode),
304 (int (*)(const void *, const void *)) cmp_opcode);
306 /* Calculate number of handles since this will be
307 smaller than the raw number of opcodes in the table. */
309 for (xgate_opcode_ptr = xgate_op_table, i = 0; i < xgate_num_opcodes;
310 xgate_opcode_ptr++, i++)
312 if (strcmp (prev_op_name, xgate_opcode_ptr->name))
313 number_of_op_handles++;
314 prev_op_name = xgate_opcode_ptr->name;
317 op_handles = (struct xgate_opcode_handle *)
318 xmalloc (sizeof(struct xgate_opcode_handle) * (number_of_op_handles));
320 /* Insert unique opcode names into hash table, aliasing duplicates. */
321 xgate_hash = hash_new ();
324 for (xgate_opcode_ptr = xgate_op_table, i = 0, j = 0; i < xgate_num_opcodes;
325 i++, xgate_opcode_ptr++)
327 if (!strcmp (prev_op_name, xgate_opcode_ptr->name))
330 op_handles[j].opc0[handle_enum] = xgate_opcode_ptr;
337 op_handles[j].name = xgate_opcode_ptr->name;
338 op_handles[j].opc0[0] = xgate_opcode_ptr;
339 hash_insert (xgate_hash, (char *) op_handles[j].name,
340 (char *) &(op_handles[j]));
342 op_handles[j].number_of_modes = handle_enum;
343 prev_op_name = op_handles[j].name;
346 if (flag_print_opcodes == 1)
347 xgate_print_table ();
351 xgate_init_after_args (void)
356 md_show_usage (FILE * stream)
358 get_default_target ();
363 Freescale XGATE co-processor options:\n \
364 -mshort use 16-bit int ABI (default)\n \
365 -mlong use 32-bit int ABI\n \
366 -mshort-double use 32-bit double ABI\n \
367 -mlong-double use 64-bit double ABI (default)\n\
368 --mxgate specify the processor variant[default %s]\n\
369 --print-insn-syntax print the syntax of instruction in case of error\n\
370 --print-opcodes print the list of instructions with syntax\n\
371 --generate-example generate an example of each instruction"),
375 enum bfd_architecture
378 get_default_target ();
379 return bfd_arch_xgate;
389 xgate_print_syntax (char *name)
393 for (i = 0; i < xgate_num_opcodes; i++)
395 if (!strcmp (xgate_opcodes[i].name, name))
397 if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_IDR))
398 printf ("\tFormat is %s\tRx, Rx, Rx+|-Rx|Rx\n",
399 xgate_opcodes[i].name);
400 if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_INH))
401 printf ("\tFormat is %s\n", xgate_opcodes[i].name);
402 if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_TRI))
403 printf ("\tFormat is %s\tRx, Rx, Rx\n", xgate_opcodes[i].name);
404 if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_DYA))
405 printf ("\tFormat is %s\tRx, Rx\n", xgate_opcodes[i].name);
406 if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_DYA_MON)
407 || !strcmp (xgate_opcodes[i].constraints, XGATE_OP_MON))
408 printf ("\tFormat is %s\tRx\n", xgate_opcodes[i].name);
409 if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_IMM3))
410 printf ("\tFormat is %s\t<3-bit value>\n", xgate_opcodes[i].name);
411 if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_IMM4))
412 printf ("\tFormat is %s\t<4 -bit value>\n", xgate_opcodes[i].name);
413 if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_IMM8))
414 printf ("\tFormat is %s\tRx, <8-bit value>\n",
415 xgate_opcodes[i].name);
416 if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_IMM16))
417 printf ("\tFormat is %s\tRx, <16-bit value>\n",
418 xgate_opcodes[i].name);
419 if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_MON_R_C))
420 printf ("\tFormat is %s\tRx, CCR\n", xgate_opcodes[i].name);
421 if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_MON_C_R))
422 printf ("\tFormat is %s\tCCR, Rx\n", xgate_opcodes[i].name);
423 if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_MON_R_P))
424 printf ("\tFormat is %s\tRx, PC\n", xgate_opcodes[i].name);
425 if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_IMM16mLDW))
426 printf ("\tFormat is %s\tRx, <16-bit value>\n",
427 xgate_opcodes[i].name);
433 xgate_print_table (void)
437 for (i = 0; i < xgate_num_opcodes; i++)
438 xgate_print_syntax (xgate_opcodes[i].name);
444 xgate_listing_header (void)
446 if (current_architecture & cpuxgate)
449 return "ERROR MC9S12X GAS ";
453 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
458 /* GAS will call this function for each section at the end of the assembly,
459 to permit the CPU backend to adjust the alignment of a section. */
462 md_section_align (asection * seg, valueT addr)
464 int align = bfd_get_section_alignment (stdoutput, seg);
465 return ((addr + (1 << align) - 1) & (-1 << align));
469 md_assemble (char *input_line)
471 struct xgate_opcode *opcode = 0;
472 struct xgate_opcode *macro_opcode = 0;
473 struct xgate_opcode_handle *opcode_handle = 0;
474 /* Caller expects it to be returned as it was passed. */
475 char *saved_input_line = input_line;
476 char op_name[9] = { 0 };
477 unsigned int sh_format = 0;
481 oper_check = 0; /* set error flags */
482 input_line = extract_word (input_line, op_name, sizeof(op_name));
484 /* Check to make sure we are not reading a bogus line. */
486 as_bad (_("opcode missing or not found on input line"));
488 if (!(opcode_handle = (struct xgate_opcode_handle *) hash_find (xgate_hash,
491 as_bad (_("opcode %s not found in opcode hash table"), op_name);
495 /* Detect operand format so we can pull the proper opcode bin. */
496 sh_format = xgate_detect_format (input_line);
498 opcode = xgate_find_match (opcode_handle, opcode_handle->number_of_modes,
503 as_bad (_("matching operands to opcode "));
504 xgate_print_syntax (opcode_handle->opc0[0]->name);
506 else if (opcode->size == 2)
508 /* Size is one word - assemble that native insn. */
509 xgate_operands (opcode, &input_line);
513 /* Insn is a simplified instruction - expand it out. */
517 /* skip past our ';' separator. */
518 for (i = strlen (opcode->constraints), p = opcode->constraints; i > 0;
527 input_line = skip_whitespace (input_line);
528 char *macro_inline = input_line;
530 /* Loop though the macro's opcode list and apply operands to each real opcode. */
531 for (i = 0; *p && i < (opcode->size / 2); i++)
533 /* Loop though macro operand list. */
534 input_line = macro_inline; /* Rewind. */
535 p = extract_word (p, op_name, 10);
537 if (!(opcode_handle = (struct xgate_opcode_handle *)
538 hash_find (xgate_hash, op_name)))
541 _(": processing macro, real opcode handle not found in hash"));
546 sh_format = xgate_detect_format (input_line);
547 macro_opcode = xgate_find_match (opcode_handle,
548 opcode_handle->number_of_modes, sh_format);
549 xgate_operands (macro_opcode, &input_line);
555 input_line = saved_input_line;
558 /* Force truly undefined symbols to their maximum size, and generally set up
559 the frag list to be relaxed. */
562 md_estimate_size_before_relax (fragS *fragp, asection *seg)
564 /* If symbol is undefined or located in a different section,
565 select the largest supported relocation. */
566 relax_substateT subtype;
567 relax_substateT rlx_state[] =
570 for (subtype = 0; subtype < ARRAY_SIZE (rlx_state); subtype += 2)
572 if (fragp->fr_subtype == rlx_state[subtype]
573 && (!S_IS_DEFINED (fragp->fr_symbol)
574 || seg != S_GET_SEGMENT (fragp->fr_symbol)))
576 fragp->fr_subtype = rlx_state[subtype + 1];
581 if (fragp->fr_subtype >= ARRAY_SIZE (md_relax_table))
584 return md_relax_table[fragp->fr_subtype].rlx_length;
588 /* Relocation, relaxation and frag conversions. */
590 /* PC-relative offsets are relative to the start of the
591 next instruction. That is, the address of the offset, plus its
592 size, since the offset is always the last part of the insn. */
595 md_pcrel_from (fixS * fixP)
597 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
600 /* If while processing a fixup, a reloc really needs to be created
601 then it is done here. */
604 tc_gen_reloc (asection * section ATTRIBUTE_UNUSED, fixS * fixp)
608 reloc = (arelent *) xmalloc (sizeof(arelent));
609 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof(asymbol *));
610 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
611 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
613 if (fixp->fx_r_type == 0)
615 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
619 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
622 if (reloc->howto == (reloc_howto_type *) NULL)
624 as_bad_where (fixp->fx_file, fixp->fx_line, _
625 ("Relocation %d is not supported by object file format."),
626 (int) fixp->fx_r_type);
630 /* Since we use Rel instead of Rela, encode the vtable entry to be
631 used in the relocation's section offset. */
632 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
633 reloc->address = fixp->fx_offset;
638 /* Patch the instruction with the resolved operand. Elf relocation
639 info will also be generated to take care of linker/loader fixups.
640 The XGATE addresses only 16-bit addresses.The BFD_RELOC_32 is necessary
641 for the support of --gstabs. */
644 md_apply_fix (fixS * fixP, valueT * valP, segT seg ATTRIBUTE_UNUSED)
651 /* If the fixup is done mark it done so no further symbol resolution will take place. */
652 if (fixP->fx_addsy == (symbolS *) NULL)
657 /* We don't actually support subtracting a symbol. */
658 if (fixP->fx_subsy != (symbolS *) NULL)
659 as_bad_where (fixP->fx_file, fixP->fx_line, _("Expression too complex."));
661 where = fixP->fx_frag->fr_literal + fixP->fx_where;
662 opcode = bfd_getl16 (where);
665 switch (fixP->fx_r_type)
667 case R_XGATE_PCREL_9:
668 if (value < -512 || value > 511)
669 as_bad_where (fixP->fx_file, fixP->fx_line,
670 _("Value %ld too large for 9-bit PC-relative branch."), value);
671 result = ldiv (value, 2); /* from bytes to words */
674 as_bad_where (fixP->fx_file, fixP->fx_line, _
675 ("Value %ld not aligned by 2 for 9-bit PC-relative branch."), value);
676 mask = 0x1FF; /* Clip into 8-bit field FIXME I'm sure there is a more proper place for this */
678 number_to_chars_bigendian (where, (opcode | value), 2);
680 case R_XGATE_PCREL_10:
681 if (value < -1024 || value > 1023)
682 as_bad_where (fixP->fx_file, fixP->fx_line,
683 _("Value %ld too large for 10-bit PC-relative branch."), value);
684 result = ldiv (value, 2); /* from bytes to words */
687 as_bad_where (fixP->fx_file, fixP->fx_line, _
688 ("Value %ld not aligned by 2 for 10-bit PC-relative branch."), value);
689 mask = 0x3FF; /* Clip into 9-bit field FIXME I'm sure there is a more proper place for this */
691 number_to_chars_bigendian (where, (opcode | value), 2);
693 case BFD_RELOC_XGATE_IMM8_HI:
694 if (value < -65537 || value > 65535)
695 as_bad_where (fixP->fx_file, fixP->fx_line,
696 _("Value out of 16-bit range."));
699 bfd_putb16 ((bfd_vma) value | opcode, (void *) where);
701 case BFD_RELOC_XGATE_24:
702 case BFD_RELOC_XGATE_IMM8_LO:
703 if (value < -65537 || value > 65535)
704 as_bad_where (fixP->fx_file, fixP->fx_line,
705 _("Value out of 16-bit range."));
707 bfd_putb16 ((bfd_vma) value | opcode, (void *) where);
709 case BFD_RELOC_XGATE_IMM3:
710 if (value < 0 || value > 7)
711 as_bad_where (fixP->fx_file, fixP->fx_line,
712 _("Value out of 3-bit range."));
713 value <<= 8; /* make big endian */
714 number_to_chars_bigendian (where, (opcode | value), 2);
716 case BFD_RELOC_XGATE_IMM4:
717 if (value < 0 || value > 15)
718 as_bad_where (fixP->fx_file, fixP->fx_line,
719 _("Value out of 4-bit range."));
720 value <<= 4; /* align the operand bits */
721 number_to_chars_bigendian (where, (opcode | value), 2);
723 case BFD_RELOC_XGATE_IMM5:
724 if (value < 0 || value > 31)
725 as_bad_where (fixP->fx_file, fixP->fx_line,
726 _("Value out of 5-bit range."));
727 value <<= 5; /* align the operand bits */
728 number_to_chars_bigendian (where, (opcode | value), 2);
731 ((bfd_byte *) where)[0] = (bfd_byte) value;
734 bfd_putb32 ((bfd_vma) value, (unsigned char *) where);
735 /* todo figure out how to make BFD_RELOC_16 the default */
738 bfd_putb16 ((bfd_vma) value, (unsigned char *) where);
741 as_fatal (_("Line %d: unknown relocation type: 0x%x."), fixP->fx_line,
747 /* See whether we need to force a relocation into the output file. */
750 tc_xgate_force_relocation (fixS * fixP)
752 if (fixP->fx_r_type == BFD_RELOC_XGATE_RL_GROUP)
754 return generic_force_reloc (fixP);
757 /* Here we decide which fixups can be adjusted to make them relative
758 to the beginning of the section instead of the symbol. Basically
759 we need to make sure that the linker relaxation is done
760 correctly, so in some cases we force the original symbol to be
764 tc_xgate_fix_adjustable (fixS * fixP)
766 switch (fixP->fx_r_type)
768 /* For the linker relaxation to work correctly, these relocs
769 need to be on the symbol itself. */
771 case BFD_RELOC_XGATE_RL_JUMP:
772 case BFD_RELOC_XGATE_RL_GROUP:
773 case BFD_RELOC_VTABLE_INHERIT:
774 case BFD_RELOC_VTABLE_ENTRY:
783 md_convert_frag (bfd * abfd ATTRIBUTE_UNUSED,
784 asection * sec ATTRIBUTE_UNUSED,
785 fragS * fragP ATTRIBUTE_UNUSED)
787 as_bad (("md_convert_frag not implemented yet"));
791 /* Set the ELF specific flags. */
794 xgate_elf_final_processing (void)
796 elf_flags |= EF_XGATE_MACH;
797 elf_elfheader (stdoutput)->e_flags &= ~EF_XGATE_ABI;
798 elf_elfheader (stdoutput)->e_flags |= elf_flags;
802 skip_whitespace (char *s)
804 while (*s == ' ' || *s == '\t' || *s == '(' || *s == ')')
810 /* Extract a word (continuous alpha-numeric chars) from the input line. */
813 extract_word (char *from, char *to, int limit)
818 /* Drop leading whitespace. */
819 from = skip_whitespace (from);
821 /* Find the op code end. */
822 for (op_end = from; *op_end != 0 && is_part_of_name (*op_end);)
824 to[size++] = *op_end++;
825 if (size + 1 >= limit)
833 xgate_new_instruction (int size)
835 char *f = frag_more (size);
836 dwarf2_emit_insn (size);
841 xgate_apply_operand (unsigned short new_mask,
842 unsigned short *availiable_mask_bits,
844 unsigned char n_bits)
846 unsigned short n_shifts;
847 unsigned int n_drop_bits;
849 /* Shift until you find an available operand bit "1" and record the number of shifts. */
851 !(*availiable_mask_bits & SIXTEENTH_BIT) && n_shifts < 16;
853 *availiable_mask_bits <<= 1;
855 /* Shift for the number of bits your operand requires while bits are available. */
856 for (n_drop_bits = n_bits;
857 n_drop_bits && (*availiable_mask_bits & SIXTEENTH_BIT);
859 *availiable_mask_bits <<= 1;
862 as_bad (_(":operand has too many bits"));
863 *availiable_mask_bits >>= n_shifts + n_bits;
864 if ((n_drop_bits == 0) && (*availiable_mask_bits == 0))
866 oper_check = 1; /* flag operand check as good */
868 new_mask <<= N_BITS_IN_WORD - (n_shifts + n_bits);
873 /* Parse ordinary expression. */
876 xgate_parse_exp (char *s, expressionS * op)
878 input_line_pointer = s;
880 if (op->X_op == O_absent)
881 as_bad (_("missing operand"));
882 return input_line_pointer;
885 /* For testing. Comment out to prevent defined but not used warning
888 xgate_get_constant(char *str, int max)
892 str = skip_whitespace(str);
893 input_line_pointer = str;
896 if (ex.X_op != O_constant)
897 as_bad(_("constant value required"));
899 if (ex.X_add_number > max || ex.X_add_number < 0)
900 as_bad(_("number must be positive and less than %d"), max + 1);
902 return ex.X_add_number;
907 cmp_opcode (struct xgate_opcode *op1, struct xgate_opcode *op2)
909 return strcmp (op1->name, op2->name);
912 /* Parse instruction operands. */
915 xgate_operands (struct xgate_opcode *opcode, char **line)
917 char *frag = xgate_new_instruction (opcode->size);
918 int where = frag - frag_now->fr_literal;
919 char *op = opcode->constraints;
920 unsigned int bin = (int) opcode->bin_opcode;
922 unsigned short oper_mask = 0;
923 int operand_bit_length = 0;
924 unsigned int operand = 0;
925 char n_operand_bits = 0;
926 char first_operand_equals_second = 0;
930 /* Generate available operand bits mask. */
931 for (i = 0; (c = opcode->format[i]); i++)
933 if (ISDIGIT (c) || (c == 's'))
945 /* Opcode has operands. */
946 /* Parse first operand. */
951 first_operand_equals_second = 1;
954 operand = xgate_operand (opcode, &operand_bit_length, where, &op, &str);
956 bin = xgate_apply_operand (operand, &oper_mask, bin, operand_bit_length);
957 /* Parse second operand. */
962 str = skip_whitespace (str);
965 if (first_operand_equals_second)
967 bin = xgate_apply_operand (operand, &oper_mask, bin,
973 as_bad (_("`,' required before second operand"));
978 str = skip_whitespace (str);
979 operand = xgate_operand (opcode, &operand_bit_length, where, &op,
981 bin = xgate_apply_operand (operand, &oper_mask, bin,
987 /* Parse the third register. */
992 str = skip_whitespace (str);
994 as_bad (_("`,' required before third operand"));
995 str = skip_whitespace (str);
996 operand = xgate_operand (opcode, &operand_bit_length, where, &op,
998 bin = xgate_apply_operand (operand, &oper_mask, bin,
1002 if (opcode->size == 2 && fixup_required)
1004 bfd_putl16 (bin, frag);
1006 else if ((opcode->sh_format & XG_PCREL))
1008 /* Write our data to a frag for further processing. */
1009 bfd_putl16 (opcode->bin_opcode, frag);
1013 /* Apply operand mask(s)to bin opcode and write the output. */
1014 /* Since we are done write this frag in xgate BE format. */
1015 number_to_chars_bigendian (frag, bin, opcode->size);
1023 xgate_operand (struct xgate_opcode *opcode,
1029 expressionS op_expr;
1031 char *op_constraint = *op_con;
1032 unsigned int op_mask = 0;
1033 char *str = skip_whitespace (*line);
1036 unsigned int pp_fix = 0;
1037 unsigned short max_size = 0;
1043 switch (*op_constraint)
1045 case '+': /* Indexed register operand +/- or plain r. */
1046 /* TODO should be able to combine with with case R. */
1048 /* Default to neither inc or dec. */
1051 str = skip_whitespace (str);
1052 while (*str != ' ' && *str != '\t')
1056 else if (*str == '+')
1058 else if (*str == 'r' || *str == 'R')
1060 str = extract_word (str, r_name, sizeof(r_name));
1061 if (ISDIGIT (r_name[1]))
1063 if (r_name[2] == '\0' && (r_name[1] - '0' < 8))
1064 op_mask = r_name[1] - '0';
1065 if (r_name[2] != '\0' && (r_name[1] - '0' > 7))
1066 as_bad (_(": expected register name r0-r7 read %s"), r_name);
1076 case 'r': /* Register operand. */
1077 if (*str == 'r' || *str == 'R')
1080 str = extract_word (str, r_name, sizeof(r_name));
1082 if (ISDIGIT (r_name[1]))
1084 if (r_name[2] == '\0')
1085 op_mask = r_name[1] - '0';
1086 else if (r_name[1] != '0' && ISDIGIT (r_name[2])
1087 && r_name[3] == '\0')
1088 op_mask = (r_name[1] - '0') * 10 + r_name[2] - '0';
1089 if (op_mask > MAXREGISTER)
1090 as_bad (_(": expected register name r0-r7 read %s "), r_name);
1095 as_bad (_(": expected register name r0-r7 read %s "), r_name);
1099 case 'i': /* Immediate value or expression expected. */
1100 /* Advance the original format pointer. */
1103 if (ISDIGIT (*op_constraint))
1105 *bit_width = (int) *op_constraint - '0';
1107 else if (*op_constraint == 'a')
1111 else if (*op_constraint == 'f')
1115 /* http://tigcc.ticalc.org/doc/gnuasm.html#SEC31 */
1118 str = xgate_parse_exp (str, &op_expr);
1119 if (op_expr.X_op == O_constant)
1121 if (!ISDIGIT (*op_constraint))
1123 _(":expected bit length with constraint type i(# immediate) read %c"),
1125 op_mask = op_expr.X_add_number;
1126 if ((opcode->name[strlen (opcode->name) - 1] == 'l') && macroClipping)
1130 else if ((opcode->name[strlen (opcode->name) - 1]) == 'h'
1136 /* Make sure it fits. */
1137 for (i = *bit_width; i; i--)
1142 if (op_mask > max_size)
1143 as_bad (_(":operand value(%d) too big for constraint"), op_mask);
1148 if (*op_constraint == '8')
1150 if ((opcode->name[strlen (opcode->name) - 1] == 'l')
1153 fixp = fix_new_exp (frag_now, where, 2, &op_expr, FALSE,
1154 BFD_RELOC_XGATE_24);
1155 /* Should be BFD_RELOC_XGATE_IMM8_LO TODO fix. */
1156 fixp->fx_pcrel_adjust = 0;
1158 if ((opcode->name[strlen (opcode->name) - 1]) == 'h'
1161 fixp = fix_new_exp (frag_now, where, 2, &op_expr, FALSE,
1162 BFD_RELOC_XGATE_IMM8_HI);
1163 fixp->fx_pcrel_adjust = 0;
1166 as_bad (_(":unknown relocation"));
1168 else if (*op_constraint == '5')
1170 fixp = fix_new_exp (frag_now, where, 2, &op_expr, FALSE,
1171 BFD_RELOC_XGATE_IMM5);
1172 fixp->fx_pcrel_adjust = 0;
1174 else if (*op_constraint == '4')
1176 fixp = fix_new_exp (frag_now, where, 2, &op_expr, FALSE,
1177 BFD_RELOC_XGATE_IMM4);
1178 fixp->fx_pcrel_adjust = 0;
1180 else if (*op_constraint == '3')
1182 fixp = fix_new_exp (frag_now, where, 2, &op_expr, FALSE,
1183 BFD_RELOC_XGATE_IMM3);
1184 fixp->fx_pcrel_adjust = 0;
1188 as_bad (_(":unknown relocation constraint size"));
1193 case 'c': /* CCR register expected. */
1194 if (*str == 'c' || *str == 'C')
1197 str = extract_word (str, r_name, sizeof(r_name));
1198 if (!(strcmp (r_name, "ccr") || strcmp (r_name, "CCR")))
1199 as_bad (_(": expected register name ccr read %s "), r_name);
1203 as_bad (_(": expected character c or C read %c"), *str);
1207 case 'p': /* PC register expected. */
1208 if (*str == 'p' || *str == 'P')
1211 str = extract_word (str, r_name, sizeof(r_name));
1212 if (!(strcmp (r_name, "pc") || strcmp (r_name, "PC")))
1213 as_bad (_(": expected register name pc read %s "), r_name);
1217 as_bad (_(": expected character p or P read %c "), *str);
1221 case 'b': /* Branch expected. */
1222 str = xgate_parse_exp (str, &op_expr);
1225 if (op_expr.X_op != O_register)
1227 if (*op_constraint == '9')
1229 /* mode == M68XG_OP_REL9 */
1230 fixp = fix_new_exp (frag_now, where, 2, &op_expr, TRUE,
1232 fixp->fx_pcrel_adjust = 1;
1234 else if (*op_constraint == 'a')
1235 { /* mode == M68XG_OP_REL10 */
1236 fixp = fix_new_exp (frag_now, where, 2, &op_expr, TRUE,
1238 fixp->fx_pcrel_adjust = 1;
1243 as_fatal (_("Operand `%x' not recognized in fixup8."), op_expr.X_op);
1251 as_bad (_("unknown constraint `%c'"), *op_constraint);
1259 xgate_detect_format (char *line_in)
1261 char num_operands = 0;
1262 char *str = skip_whitespace (line_in);
1266 unsigned int stripped_length = 0;
1267 char sh_format[10] =
1268 { 0 }; /* Shorthand format. */
1269 char operands_stripped[3][20] =
1273 /* Strings. TODO maybe structure this. */
1280 char *r_r_r_string =
1290 char *r_r_i_string =
1293 /* If the length is zero this is an inherent instruction. */
1294 if (strlen (str) == 0)
1297 for (i = 0, j = 0, num_operands = 1; (c = TOLOWER (*str)) != 0; str++)
1299 if (c == ' ' || c == '\t' || c == '(' || c == ')' || c == '-' || c == '+')
1310 if (i > MAX_DETECT_CHARS)
1313 operands_stripped[j][i++] = c;
1316 /* Process our substrings to see what we have. */
1317 for (i = 0, j = 0; num_operands > i; i++)
1319 stripped_length = strlen (&operands_stripped[i][0]);
1321 /* Add separator if we have more than one operand. */
1323 sh_format[j++] = ',';
1325 /* Try to process by length first. */
1326 if (stripped_length > 3)
1328 sh_format[j++] = 'i';
1330 else if (stripped_length == 1)
1332 sh_format[j++] = 'i';
1334 else if (stripped_length == 2)
1336 if (operands_stripped[i][0]
1337 == 'r' && ISDIGIT (operands_stripped[i][1]))
1339 sh_format[j++] = 'r';
1341 else if (operands_stripped[i][0] == 'p'
1342 && operands_stripped[i][1] == 'c')
1344 sh_format[j++] = 'p';
1348 sh_format[j++] = 'i';
1351 else if (stripped_length == 3)
1353 if (operands_stripped[i][0] == 'c'
1354 && (operands_stripped[i][1] == 'c'
1355 && operands_stripped[i][2] == 'r'))
1357 sh_format[j++] = 'c';
1359 else if (operands_stripped[i][0] == '#')
1361 sh_format[j++] = 'i';
1365 sh_format[j++] = 'i';
1368 else /* default to immediate */
1370 sh_format[j++] = 'i';
1374 /* See if we have a match. */
1375 if (!strcmp (i_string, sh_format) && num_operands == 1)
1377 if (!strcmp (r_i_string, sh_format) && num_operands == 2)
1379 if (!strcmp (r_r_r_string, sh_format) && num_operands == 3)
1381 if (!strcmp (r_r_string, sh_format) && num_operands == 2)
1383 if (!strcmp (r_string, sh_format) && num_operands == 1)
1385 if (!strcmp (r_c_string, sh_format) && num_operands == 2)
1387 if (!strcmp (c_r_string, sh_format) && num_operands == 2)
1389 if (!strcmp (r_p_string, sh_format) && num_operands == 2)
1391 if (!strcmp (r_r_i_string, sh_format) && num_operands == 3)
1397 static struct xgate_opcode *
1398 xgate_find_match (struct xgate_opcode_handle *opcode_handle,
1400 unsigned int sh_format)
1404 if (numberOfModes == 0)
1405 return opcode_handle->opc0[0];
1407 for (i = 0; i <= numberOfModes; i++)
1408 if (opcode_handle->opc0[i]->sh_format & sh_format)
1409 return opcode_handle->opc0[i];