Rearrange symbol_create parameters
[deliverable/binutils-gdb.git] / gas / config / tc-cr16.c
1 /* tc-cr16.c -- Assembler code for the CR16 CPU core.
2 Copyright (C) 2007-2020 Free Software Foundation, Inc.
3
4 Contributed by M R Swami Reddy <MR.Swami.Reddy@nsc.com>
5
6 This file is part of GAS, the GNU Assembler.
7
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)
11 any later version.
12
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.
17
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 the
20 Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 #include "as.h"
24 #include "safe-ctype.h"
25 #include "dwarf2dbg.h"
26 #include "opcode/cr16.h"
27 #include "elf/cr16.h"
28
29
30 /* Word is considered here as a 16-bit unsigned short int. */
31 #define WORD_SHIFT 16
32
33 /* Register is 2-byte size. */
34 #define REG_SIZE 2
35
36 /* Maximum size of a single instruction (in words). */
37 #define INSN_MAX_SIZE 3
38
39 /* Maximum bits which may be set in a `mask16' operand. */
40 #define MAX_REGS_IN_MASK16 8
41
42 /* Assign a number NUM, shifted by SHIFT bytes, into a location
43 pointed by index BYTE of array 'output_opcode'. */
44 #define CR16_PRINT(BYTE, NUM, SHIFT) output_opcode[BYTE] |= (NUM << SHIFT)
45
46 /* Operand errors. */
47 typedef enum
48 {
49 OP_LEGAL = 0, /* Legal operand. */
50 OP_OUT_OF_RANGE, /* Operand not within permitted range. */
51 OP_NOT_EVEN /* Operand is Odd number, should be even. */
52 }
53 op_err;
54
55 /* Opcode mnemonics hash table. */
56 static htab_t cr16_inst_hash;
57 /* CR16 registers hash table. */
58 static htab_t reg_hash;
59 /* CR16 register pair hash table. */
60 static htab_t regp_hash;
61 /* CR16 processor registers hash table. */
62 static htab_t preg_hash;
63 /* CR16 processor registers 32 bit hash table. */
64 static htab_t pregp_hash;
65 /* Current instruction we're assembling. */
66 const inst *instruction;
67
68
69 static int code_label = 0;
70
71 /* Global variables. */
72
73 /* Array to hold an instruction encoding. */
74 long output_opcode[2];
75
76 /* Nonzero means a relocatable symbol. */
77 int relocatable;
78
79 /* A copy of the original instruction (used in error messages). */
80 char ins_parse[MAX_INST_LEN];
81
82 /* The current processed argument number. */
83 int cur_arg_num;
84
85 /* Generic assembler global variables which must be defined by all targets. */
86
87 /* Characters which always start a comment. */
88 const char comment_chars[] = "#";
89
90 /* Characters which start a comment at the beginning of a line. */
91 const char line_comment_chars[] = "#";
92
93 /* This array holds machine specific line separator characters. */
94 const char line_separator_chars[] = ";";
95
96 /* Chars that can be used to separate mant from exp in floating point nums. */
97 const char EXP_CHARS[] = "eE";
98
99 /* Chars that mean this number is a floating point constant as in 0f12.456 */
100 const char FLT_CHARS[] = "f'";
101
102 #ifdef OBJ_ELF
103 /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
104 symbolS * GOT_symbol;
105 #endif
106
107 /* Target-specific multicharacter options, not const-declared at usage. */
108 const char *md_shortopts = "";
109 struct option md_longopts[] =
110 {
111 {NULL, no_argument, NULL, 0}
112 };
113 size_t md_longopts_size = sizeof (md_longopts);
114
115 static void
116 l_cons (int nbytes)
117 {
118 int c;
119 expressionS exp;
120
121 #ifdef md_flush_pending_output
122 md_flush_pending_output ();
123 #endif
124
125 if (is_it_end_of_statement ())
126 {
127 demand_empty_rest_of_line ();
128 return;
129 }
130
131 #ifdef TC_ADDRESS_BYTES
132 if (nbytes == 0)
133 nbytes = TC_ADDRESS_BYTES ();
134 #endif
135
136 #ifdef md_cons_align
137 md_cons_align (nbytes);
138 #endif
139
140 c = 0;
141 do
142 {
143 unsigned int bits_available = BITS_PER_CHAR * nbytes;
144 char *hold = input_line_pointer;
145
146 expression (&exp);
147
148 if (*input_line_pointer == ':')
149 {
150 /* Bitfields. */
151 long value = 0;
152
153 for (;;)
154 {
155 unsigned long width;
156
157 if (*input_line_pointer != ':')
158 {
159 input_line_pointer = hold;
160 break;
161 }
162 if (exp.X_op == O_absent)
163 {
164 as_warn (_("using a bit field width of zero"));
165 exp.X_add_number = 0;
166 exp.X_op = O_constant;
167 }
168
169 if (exp.X_op != O_constant)
170 {
171 *input_line_pointer = '\0';
172 as_bad (_("field width \"%s\" too complex for a bitfield"), hold);
173 *input_line_pointer = ':';
174 demand_empty_rest_of_line ();
175 return;
176 }
177
178 if ((width = exp.X_add_number) >
179 (unsigned int)(BITS_PER_CHAR * nbytes))
180 {
181 as_warn (ngettext ("field width %lu too big to fit in %d"
182 " byte: truncated to %d bits",
183 "field width %lu too big to fit in %d"
184 " bytes: truncated to %d bits",
185 nbytes),
186 width, nbytes, (BITS_PER_CHAR * nbytes));
187 width = BITS_PER_CHAR * nbytes;
188 } /* Too big. */
189
190
191 if (width > bits_available)
192 {
193 /* FIXME-SOMEDAY: backing up and reparsing is wasteful. */
194 input_line_pointer = hold;
195 exp.X_add_number = value;
196 break;
197 }
198
199 /* Skip ':'. */
200 hold = ++input_line_pointer;
201
202 expression (&exp);
203 if (exp.X_op != O_constant)
204 {
205 char cache = *input_line_pointer;
206
207 *input_line_pointer = '\0';
208 as_bad (_("field value \"%s\" too complex for a bitfield"), hold);
209 *input_line_pointer = cache;
210 demand_empty_rest_of_line ();
211 return;
212 }
213
214 value |= ((~(-(1 << width)) & exp.X_add_number)
215 << ((BITS_PER_CHAR * nbytes) - bits_available));
216
217 if ((bits_available -= width) == 0
218 || is_it_end_of_statement ()
219 || *input_line_pointer != ',')
220 break;
221
222 hold = ++input_line_pointer;
223 expression (&exp);
224 }
225
226 exp.X_add_number = value;
227 exp.X_op = O_constant;
228 exp.X_unsigned = 1;
229 }
230
231 if ((*(input_line_pointer) == '@') && (*(input_line_pointer +1) == 'c'))
232 code_label = 1;
233 emit_expr (&exp, (unsigned int) nbytes);
234 ++c;
235 if ((*(input_line_pointer) == '@') && (*(input_line_pointer +1) == 'c'))
236 {
237 input_line_pointer +=3;
238 break;
239 }
240 }
241 while ((*input_line_pointer++ == ','));
242
243 /* Put terminator back into stream. */
244 input_line_pointer--;
245
246 demand_empty_rest_of_line ();
247 }
248
249 /* This table describes all the machine specific pseudo-ops
250 the assembler has to support. The fields are:
251 *** Pseudo-op name without dot.
252 *** Function to call to execute this pseudo-op.
253 *** Integer arg to pass to the function. */
254
255 const pseudo_typeS md_pseudo_table[] =
256 {
257 /* In CR16 machine, align is in bytes (not a ptwo boundary). */
258 {"align", s_align_bytes, 0},
259 {"long", l_cons, 4 },
260 {"4byte", l_cons, 4 },
261 {0, 0, 0}
262 };
263
264 /* CR16 relaxation table. */
265 const relax_typeS md_relax_table[] =
266 {
267 /* bCC */
268 {0x7f, -0x80, 2, 1}, /* 8 */
269 {0xfffe, -0x10000, 4, 2}, /* 16 */
270 {0xfffffe, -0x1000000, 6, 0}, /* 24 */
271 };
272
273 /* Return the bit size for a given operand. */
274
275 static int
276 get_opbits (operand_type op)
277 {
278 if (op < MAX_OPRD)
279 return cr16_optab[op].bit_size;
280
281 return 0;
282 }
283
284 /* Return the argument type of a given operand. */
285
286 static argtype
287 get_optype (operand_type op)
288 {
289 if (op < MAX_OPRD)
290 return cr16_optab[op].arg_type;
291 else
292 return nullargs;
293 }
294
295 /* Return the flags of a given operand. */
296
297 static int
298 get_opflags (operand_type op)
299 {
300 if (op < MAX_OPRD)
301 return cr16_optab[op].flags;
302
303 return 0;
304 }
305
306 /* Get the cc code. */
307
308 static int
309 get_cc (char *cc_name)
310 {
311 unsigned int i;
312
313 for (i = 0; i < cr16_num_cc; i++)
314 if (strcmp (cc_name, cr16_b_cond_tab[i]) == 0)
315 return i;
316
317 return -1;
318 }
319
320 /* Get the core processor register 'reg_name'. */
321
322 static reg
323 get_register (char *reg_name)
324 {
325 const reg_entry *rreg;
326
327 rreg = (const reg_entry *) str_hash_find (reg_hash, reg_name);
328
329 if (rreg != NULL)
330 return rreg->value.reg_val;
331
332 return nullregister;
333 }
334 /* Get the core processor register-pair 'reg_name'. */
335
336 static reg
337 get_register_pair (char *reg_name)
338 {
339 const reg_entry *rreg;
340 char tmp_rp[16]="\0";
341
342 /* Add '(' and ')' to the reg pair, if it's not present. */
343 if (reg_name[0] != '(')
344 {
345 tmp_rp[0] = '(';
346 strcat (tmp_rp, reg_name);
347 strcat (tmp_rp,")");
348 rreg = (const reg_entry *) str_hash_find (regp_hash, tmp_rp);
349 }
350 else
351 rreg = (const reg_entry *) str_hash_find (regp_hash, reg_name);
352
353 if (rreg != NULL)
354 return rreg->value.reg_val;
355
356 return nullregister;
357 }
358
359 /* Get the index register 'reg_name'. */
360
361 static reg
362 get_index_register (char *reg_name)
363 {
364 const reg_entry *rreg;
365
366 rreg = (const reg_entry *) str_hash_find (reg_hash, reg_name);
367
368 if ((rreg != NULL)
369 && ((rreg->value.reg_val == 12) || (rreg->value.reg_val == 13)))
370 return rreg->value.reg_val;
371
372 return nullregister;
373 }
374 /* Get the core processor index register-pair 'reg_name'. */
375
376 static reg
377 get_index_register_pair (char *reg_name)
378 {
379 const reg_entry *rreg;
380
381 rreg = (const reg_entry *) str_hash_find (regp_hash, reg_name);
382
383 if (rreg != NULL)
384 {
385 if ((rreg->value.reg_val != 1) || (rreg->value.reg_val != 7)
386 || (rreg->value.reg_val != 9) || (rreg->value.reg_val > 10))
387 return rreg->value.reg_val;
388
389 as_bad (_("Unknown register pair - index relative mode: `%d'"), rreg->value.reg_val);
390 }
391
392 return nullregister;
393 }
394
395 /* Get the processor register 'preg_name'. */
396
397 static preg
398 get_pregister (char *preg_name)
399 {
400 const reg_entry *prreg;
401
402 prreg = (const reg_entry *) str_hash_find (preg_hash, preg_name);
403
404 if (prreg != NULL)
405 return prreg->value.preg_val;
406
407 return nullpregister;
408 }
409
410 /* Get the processor register 'preg_name 32 bit'. */
411
412 static preg
413 get_pregisterp (char *preg_name)
414 {
415 const reg_entry *prreg;
416
417 prreg = (const reg_entry *) str_hash_find (pregp_hash, preg_name);
418
419 if (prreg != NULL)
420 return prreg->value.preg_val;
421
422 return nullpregister;
423 }
424
425
426 /* Round up a section size to the appropriate boundary. */
427
428 valueT
429 md_section_align (segT seg, valueT val)
430 {
431 /* Round .text section to a multiple of 2. */
432 if (seg == text_section)
433 return (val + 1) & ~1;
434 return val;
435 }
436
437 /* Parse an operand that is machine-specific (remove '*'). */
438
439 void
440 md_operand (expressionS * exp)
441 {
442 char c = *input_line_pointer;
443
444 switch (c)
445 {
446 case '*':
447 input_line_pointer++;
448 expression (exp);
449 break;
450 default:
451 break;
452 }
453 }
454
455 /* Reset global variables before parsing a new instruction. */
456
457 static void
458 reset_vars (char *op)
459 {
460 cur_arg_num = relocatable = 0;
461 memset (& output_opcode, '\0', sizeof (output_opcode));
462
463 /* Save a copy of the original OP (used in error messages). */
464 strncpy (ins_parse, op, sizeof ins_parse - 1);
465 ins_parse [sizeof ins_parse - 1] = 0;
466 }
467
468 /* This macro decides whether a particular reloc is an entry in a
469 switch table. It is used when relaxing, because the linker needs
470 to know about all such entries so that it can adjust them if
471 necessary. */
472
473 #define SWITCH_TABLE(fix) \
474 ( (fix)->fx_addsy != NULL \
475 && (fix)->fx_subsy != NULL \
476 && S_GET_SEGMENT ((fix)->fx_addsy) == \
477 S_GET_SEGMENT ((fix)->fx_subsy) \
478 && S_GET_SEGMENT (fix->fx_addsy) != undefined_section \
479 && ( (fix)->fx_r_type == BFD_RELOC_CR16_NUM8 \
480 || (fix)->fx_r_type == BFD_RELOC_CR16_NUM16 \
481 || (fix)->fx_r_type == BFD_RELOC_CR16_NUM32 \
482 || (fix)->fx_r_type == BFD_RELOC_CR16_NUM32a))
483
484 /* See whether we need to force a relocation into the output file.
485 This is used to force out switch and PC relative relocations when
486 relaxing. */
487
488 int
489 cr16_force_relocation (fixS *fix)
490 {
491 if (generic_force_reloc (fix) || SWITCH_TABLE (fix))
492 return 1;
493
494 return 0;
495 }
496
497 /* Record a fixup for a cons expression. */
498
499 void
500 cr16_cons_fix_new (fragS *frag, int offset, int len, expressionS *exp,
501 bfd_reloc_code_real_type rtype)
502 {
503 switch (len)
504 {
505 default: rtype = BFD_RELOC_NONE; break;
506 case 1: rtype = BFD_RELOC_CR16_NUM8 ; break;
507 case 2: rtype = BFD_RELOC_CR16_NUM16; break;
508 case 4:
509 if (code_label)
510 {
511 rtype = BFD_RELOC_CR16_NUM32a;
512 code_label = 0;
513 }
514 else
515 rtype = BFD_RELOC_CR16_NUM32;
516 break;
517 }
518
519 fix_new_exp (frag, offset, len, exp, 0, rtype);
520 }
521
522 /* Generate a relocation entry for a fixup. */
523
524 arelent *
525 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS * fixP)
526 {
527 arelent * reloc;
528
529 /* If symbols are local and resolved, then no relocation needed. */
530 if ( ((fixP->fx_addsy)
531 && (S_GET_SEGMENT (fixP->fx_addsy) == absolute_section))
532 || ((fixP->fx_subsy)
533 && (S_GET_SEGMENT (fixP->fx_subsy) == absolute_section)))
534 return NULL;
535
536 reloc = XNEW (arelent);
537 reloc->sym_ptr_ptr = XNEW (asymbol *);
538 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
539 reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
540 reloc->addend = fixP->fx_offset;
541
542 if (fixP->fx_subsy != NULL)
543 {
544 if (SWITCH_TABLE (fixP))
545 {
546 /* Keep the current difference in the addend. */
547 reloc->addend = (S_GET_VALUE (fixP->fx_addsy)
548 - S_GET_VALUE (fixP->fx_subsy) + fixP->fx_offset);
549
550 switch (fixP->fx_r_type)
551 {
552 case BFD_RELOC_CR16_NUM8:
553 fixP->fx_r_type = BFD_RELOC_CR16_SWITCH8;
554 break;
555 case BFD_RELOC_CR16_NUM16:
556 fixP->fx_r_type = BFD_RELOC_CR16_SWITCH16;
557 break;
558 case BFD_RELOC_CR16_NUM32:
559 fixP->fx_r_type = BFD_RELOC_CR16_SWITCH32;
560 break;
561 case BFD_RELOC_CR16_NUM32a:
562 fixP->fx_r_type = BFD_RELOC_CR16_NUM32a;
563 break;
564 default:
565 abort ();
566 break;
567 }
568 }
569 else
570 {
571 /* We only resolve difference expressions in the same section. */
572 as_bad_where (fixP->fx_file, fixP->fx_line,
573 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
574 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
575 segment_name (fixP->fx_addsy
576 ? S_GET_SEGMENT (fixP->fx_addsy)
577 : absolute_section),
578 S_GET_NAME (fixP->fx_subsy),
579 segment_name (S_GET_SEGMENT (fixP->fx_addsy)));
580 }
581 }
582 #ifdef OBJ_ELF
583 if ((fixP->fx_r_type == BFD_RELOC_CR16_GOT_REGREL20)
584 && GOT_symbol
585 && fixP->fx_addsy == GOT_symbol)
586 {
587 reloc->addend = fixP->fx_offset = reloc->address;
588 }
589 else if ((fixP->fx_r_type == BFD_RELOC_CR16_GOTC_REGREL20)
590 && GOT_symbol
591 && fixP->fx_addsy == GOT_symbol)
592 {
593 reloc->addend = fixP->fx_offset = reloc->address;
594 }
595 #endif
596
597 gas_assert ((int) fixP->fx_r_type > 0);
598 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
599
600 if (reloc->howto == NULL)
601 {
602 as_bad_where (fixP->fx_file, fixP->fx_line,
603 _("internal error: reloc %d (`%s') not supported by object file format"),
604 fixP->fx_r_type,
605 bfd_get_reloc_code_name (fixP->fx_r_type));
606 return NULL;
607 }
608 gas_assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
609
610 return reloc;
611 }
612
613 /* Prepare machine-dependent frags for relaxation. */
614
615 int
616 md_estimate_size_before_relax (fragS *fragp, asection *seg)
617 {
618 /* If symbol is undefined or located in a different section,
619 select the largest supported relocation. */
620 relax_substateT subtype;
621 relax_substateT rlx_state[] = {0, 2};
622
623 for (subtype = 0; subtype < ARRAY_SIZE (rlx_state); subtype += 2)
624 {
625 if (fragp->fr_subtype == rlx_state[subtype]
626 && (!S_IS_DEFINED (fragp->fr_symbol)
627 || seg != S_GET_SEGMENT (fragp->fr_symbol)))
628 {
629 fragp->fr_subtype = rlx_state[subtype + 1];
630 break;
631 }
632 }
633
634 if (fragp->fr_subtype >= ARRAY_SIZE (md_relax_table))
635 abort ();
636
637 return md_relax_table[fragp->fr_subtype].rlx_length;
638 }
639
640 void
641 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, fragS *fragP)
642 {
643 /* 'opcode' points to the start of the instruction, whether
644 we need to change the instruction's fixed encoding. */
645 char *opcode = &fragP->fr_literal[0] + fragP->fr_fix;
646 bfd_reloc_code_real_type reloc;
647
648 subseg_change (sec, 0);
649
650 switch (fragP->fr_subtype)
651 {
652 case 0:
653 reloc = BFD_RELOC_CR16_DISP8;
654 break;
655 case 1:
656 /* If the subtype is not changed due to :m operand qualifier,
657 then no need to update the opcode value. */
658 if ((int)opcode[1] != 0x18)
659 {
660 opcode[0] = (opcode[0] & 0xf0);
661 opcode[1] = 0x18;
662 }
663 reloc = BFD_RELOC_CR16_DISP16;
664 break;
665 case 2:
666 /* If the subtype is not changed due to :l operand qualifier,
667 then no need to update the opcode value. */
668 if ((int)opcode[1] != 0)
669 {
670 opcode[2] = opcode[0];
671 opcode[0] = opcode[1];
672 opcode[1] = 0x0;
673 }
674 reloc = BFD_RELOC_CR16_DISP24;
675 break;
676 default:
677 abort();
678 }
679
680 fix_new (fragP, fragP->fr_fix,
681 bfd_get_reloc_size (bfd_reloc_type_lookup (stdoutput, reloc)),
682 fragP->fr_symbol, fragP->fr_offset, 1, reloc);
683 fragP->fr_var = 0;
684 fragP->fr_fix += md_relax_table[fragP->fr_subtype].rlx_length;
685 }
686
687 symbolS *
688 md_undefined_symbol (char *name)
689 {
690 if (*name == '_' && *(name + 1) == 'G'
691 && strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
692 {
693 if (!GOT_symbol)
694 {
695 if (symbol_find (name))
696 as_bad (_("GOT already in symbol table"));
697 GOT_symbol = symbol_new (name, undefined_section,
698 &zero_address_frag, 0);
699 }
700 return GOT_symbol;
701 }
702 return 0;
703 }
704
705 /* Process machine-dependent command line options. Called once for
706 each option on the command line that the machine-independent part of
707 GAS does not understand. */
708
709 int
710 md_parse_option (int c ATTRIBUTE_UNUSED, const char *arg ATTRIBUTE_UNUSED)
711 {
712 return 0;
713 }
714
715 /* Machine-dependent usage-output. */
716
717 void
718 md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
719 {
720 return;
721 }
722
723 const char *
724 md_atof (int type, char *litP, int *sizeP)
725 {
726 return ieee_md_atof (type, litP, sizeP, target_big_endian);
727 }
728
729 /* Apply a fixS (fixup of an instruction or data that we didn't have
730 enough info to complete immediately) to the data in a frag.
731 Since linkrelax is nonzero and TC_LINKRELAX_FIXUP is defined to disable
732 relaxation of debug sections, this function is called only when
733 fixuping relocations of debug sections. */
734
735 void
736 md_apply_fix (fixS *fixP, valueT *valP, segT seg)
737 {
738 valueT val = * valP;
739
740 if (fixP->fx_addsy == NULL
741 && fixP->fx_pcrel == 0)
742 fixP->fx_done = 1;
743 else if (fixP->fx_pcrel == 1
744 && fixP->fx_addsy != NULL
745 && S_GET_SEGMENT (fixP->fx_addsy) == seg)
746 fixP->fx_done = 1;
747 else
748 fixP->fx_done = 0;
749
750 if (fixP->fx_addsy != NULL && !fixP->fx_pcrel)
751 {
752 val = fixP->fx_offset;
753 fixP->fx_done = 1;
754 }
755
756 if (fixP->fx_done)
757 {
758 char *buf = fixP->fx_frag->fr_literal + fixP->fx_where;
759
760 fixP->fx_offset = 0;
761
762 switch (fixP->fx_r_type)
763 {
764 case BFD_RELOC_CR16_NUM8:
765 bfd_put_8 (stdoutput, (unsigned char) val, buf);
766 break;
767 case BFD_RELOC_CR16_NUM16:
768 bfd_put_16 (stdoutput, val, buf);
769 break;
770 case BFD_RELOC_CR16_NUM32:
771 bfd_put_32 (stdoutput, val, buf);
772 break;
773 case BFD_RELOC_CR16_NUM32a:
774 bfd_put_32 (stdoutput, val, buf);
775 break;
776 default:
777 /* We shouldn't ever get here because linkrelax is nonzero. */
778 abort ();
779 break;
780 }
781 fixP->fx_done = 0;
782 }
783 else
784 fixP->fx_offset = * valP;
785 }
786
787 /* The location from which a PC relative jump should be calculated,
788 given a PC relative reloc. */
789
790 long
791 md_pcrel_from (fixS *fixp)
792 {
793 return fixp->fx_frag->fr_address + fixp->fx_where;
794 }
795
796 static void
797 initialise_reg_hash_table (htab_t * hash_table,
798 const reg_entry * register_table,
799 const unsigned int num_entries)
800 {
801 const reg_entry * rreg;
802 if ((* hash_table = str_htab_create ()) == NULL)
803 as_fatal (_("Virtual memory exhausted"));
804
805 for (rreg = register_table;
806 rreg < (register_table + num_entries);
807 rreg++)
808 str_hash_insert (* hash_table, rreg->name, (char *) rreg);
809 }
810
811 /* This function is called once, at assembler startup time. This should
812 set up all the tables, etc that the MD part of the assembler needs. */
813
814 void
815 md_begin (void)
816 {
817 int i = 0;
818
819 /* Set up a hash table for the instructions. */
820 if ((cr16_inst_hash = str_htab_create ()) == NULL)
821 as_fatal (_("Virtual memory exhausted"));
822
823 while (cr16_instruction[i].mnemonic != NULL)
824 {
825 const char *mnemonic = cr16_instruction[i].mnemonic;
826
827 str_hash_insert (cr16_inst_hash, mnemonic,
828 (char *)(cr16_instruction + i));
829 /* Insert unique names into hash table. The CR16 instruction set
830 has many identical opcode names that have different opcodes based
831 on the operands. This hash table then provides a quick index to
832 the first opcode with a particular name in the opcode table. */
833 do
834 {
835 ++i;
836 }
837 while (cr16_instruction[i].mnemonic != NULL
838 && streq (cr16_instruction[i].mnemonic, mnemonic));
839 }
840
841 /* Initialize reg_hash hash table. */
842 initialise_reg_hash_table (& reg_hash, cr16_regtab, NUMREGS);
843 /* Initialize regp_hash hash table. */
844 initialise_reg_hash_table (& regp_hash, cr16_regptab, NUMREGPS);
845 /* Initialize preg_hash hash table. */
846 initialise_reg_hash_table (& preg_hash, cr16_pregtab, NUMPREGS);
847 /* Initialize pregp_hash hash table. */
848 initialise_reg_hash_table (& pregp_hash, cr16_pregptab, NUMPREGPS);
849
850 /* Set linkrelax here to avoid fixups in most sections. */
851 linkrelax = 1;
852 }
853
854 /* Process constants (immediate/absolute)
855 and labels (jump targets/Memory locations). */
856
857 static void
858 process_label_constant (char *str, ins * cr16_ins)
859 {
860 char *saved_input_line_pointer;
861 int symbol_with_at = 0;
862 int symbol_with_s = 0;
863 int symbol_with_m = 0;
864 int symbol_with_l = 0;
865 int symbol_with_at_got = 0;
866 int symbol_with_at_gotc = 0;
867 argument *cur_arg = cr16_ins->arg + cur_arg_num; /* Current argument. */
868
869 saved_input_line_pointer = input_line_pointer;
870 input_line_pointer = str;
871
872 expression (&cr16_ins->exp);
873
874 switch (cr16_ins->exp.X_op)
875 {
876 case O_big:
877 case O_absent:
878 /* Missing or bad expr becomes absolute 0. */
879 as_bad (_("missing or invalid displacement expression `%s' taken as 0"),
880 str);
881 cr16_ins->exp.X_op = O_constant;
882 cr16_ins->exp.X_add_number = 0;
883 cr16_ins->exp.X_add_symbol = NULL;
884 cr16_ins->exp.X_op_symbol = NULL;
885 /* Fall through. */
886
887 case O_constant:
888 cur_arg->X_op = O_constant;
889 cur_arg->constant = cr16_ins->exp.X_add_number;
890 break;
891
892 case O_symbol:
893 case O_subtract:
894 case O_add:
895 cur_arg->X_op = O_symbol;
896 cur_arg->constant = cr16_ins->exp.X_add_number;
897 cr16_ins->exp.X_add_number = 0;
898 cr16_ins->rtype = BFD_RELOC_NONE;
899 relocatable = 1;
900
901 if (strneq (input_line_pointer, "@c", 2))
902 symbol_with_at = 1;
903
904 if (strneq (input_line_pointer, "@l", 2)
905 || strneq (input_line_pointer, ":l", 2))
906 symbol_with_l = 1;
907
908 if (strneq (input_line_pointer, "@m", 2)
909 || strneq (input_line_pointer, ":m", 2))
910 symbol_with_m = 1;
911
912 if (strneq (input_line_pointer, "@s", 2)
913 || strneq (input_line_pointer, ":s", 2))
914 symbol_with_s = 1;
915
916 if (strneq (input_line_pointer, "@cGOT", 5)
917 || strneq (input_line_pointer, "@cgot", 5))
918 {
919 if (GOT_symbol == NULL)
920 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
921
922 symbol_with_at_gotc = 1;
923 }
924 else if (strneq (input_line_pointer, "@GOT", 4)
925 || strneq (input_line_pointer, "@got", 4))
926 {
927 if ((strneq (input_line_pointer, "+", 1))
928 || (strneq (input_line_pointer, "-", 1)))
929 as_warn (_("GOT bad expression with %s."), input_line_pointer);
930
931 if (GOT_symbol == NULL)
932 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
933
934 symbol_with_at_got = 1;
935 }
936
937 switch (cur_arg->type)
938 {
939 case arg_cr:
940 if (IS_INSN_TYPE (LD_STOR_INS) || IS_INSN_TYPE (CSTBIT_INS))
941 {
942 if (symbol_with_at_got)
943 cr16_ins->rtype = BFD_RELOC_CR16_GOT_REGREL20;
944 else if (symbol_with_at_gotc)
945 cr16_ins->rtype = BFD_RELOC_CR16_GOTC_REGREL20;
946 else if (cur_arg->size == 20)
947 cr16_ins->rtype = BFD_RELOC_CR16_REGREL20;
948 else
949 cr16_ins->rtype = BFD_RELOC_CR16_REGREL20a;
950 }
951 break;
952
953 case arg_crp:
954 if (IS_INSN_TYPE (LD_STOR_INS) || IS_INSN_TYPE (CSTBIT_INS))
955 {
956 if (symbol_with_at_got)
957 cr16_ins->rtype = BFD_RELOC_CR16_GOT_REGREL20;
958 else if (symbol_with_at_gotc)
959 cr16_ins->rtype = BFD_RELOC_CR16_GOTC_REGREL20;
960 } else {
961 switch (instruction->size)
962 {
963 case 1:
964 switch (cur_arg->size)
965 {
966 case 0:
967 cr16_ins->rtype = BFD_RELOC_CR16_REGREL0;
968 break;
969 case 4:
970 if (IS_INSN_MNEMONIC ("loadb") || IS_INSN_MNEMONIC ("storb"))
971 cr16_ins->rtype = BFD_RELOC_CR16_REGREL4;
972 else
973 cr16_ins->rtype = BFD_RELOC_CR16_REGREL4a;
974 break;
975 default: break;
976 }
977 break;
978 case 2:
979 cr16_ins->rtype = BFD_RELOC_CR16_REGREL16;
980 break;
981 case 3:
982 if (cur_arg->size == 20)
983 cr16_ins->rtype = BFD_RELOC_CR16_REGREL20;
984 else
985 cr16_ins->rtype = BFD_RELOC_CR16_REGREL20a;
986 break;
987 default:
988 break;
989 }
990 }
991 break;
992
993 case arg_idxr:
994 if (IS_INSN_TYPE (LD_STOR_INS) || IS_INSN_TYPE (CSTBIT_INS))
995 {
996 if (symbol_with_at_got)
997 cr16_ins->rtype = BFD_RELOC_CR16_GOT_REGREL20;
998 else if (symbol_with_at_gotc)
999 cr16_ins->rtype = BFD_RELOC_CR16_GOTC_REGREL20;
1000 else
1001 cr16_ins->rtype = BFD_RELOC_CR16_REGREL20;
1002 }
1003 break;
1004
1005 case arg_idxrp:
1006 if (IS_INSN_TYPE (LD_STOR_INS) || IS_INSN_TYPE (CSTBIT_INS))
1007 {
1008 if (symbol_with_at_got)
1009 cr16_ins->rtype = BFD_RELOC_CR16_GOT_REGREL20;
1010 else if (symbol_with_at_gotc)
1011 cr16_ins->rtype = BFD_RELOC_CR16_GOTC_REGREL20;
1012 else {
1013 switch (instruction->size)
1014 {
1015 case 1: cr16_ins->rtype = BFD_RELOC_CR16_REGREL0; break;
1016 case 2: cr16_ins->rtype = BFD_RELOC_CR16_REGREL14; break;
1017 case 3: cr16_ins->rtype = BFD_RELOC_CR16_REGREL20; break;
1018 default: break;
1019 }
1020 }
1021 }
1022 break;
1023
1024 case arg_c:
1025 if (IS_INSN_MNEMONIC ("bal"))
1026 cr16_ins->rtype = BFD_RELOC_CR16_DISP24;
1027 else if (IS_INSN_TYPE (BRANCH_INS))
1028 {
1029 if (symbol_with_l)
1030 cr16_ins->rtype = BFD_RELOC_CR16_DISP24;
1031 else if (symbol_with_m)
1032 cr16_ins->rtype = BFD_RELOC_CR16_DISP16;
1033 else
1034 cr16_ins->rtype = BFD_RELOC_CR16_DISP8;
1035 }
1036 else if (IS_INSN_TYPE (STOR_IMM_INS) || IS_INSN_TYPE (LD_STOR_INS)
1037 || IS_INSN_TYPE (CSTBIT_INS))
1038 {
1039 if (symbol_with_s)
1040 as_bad (_("operand %d: illegal use expression: `%s`"), cur_arg_num + 1, str);
1041 if (symbol_with_at_got)
1042 cr16_ins->rtype = BFD_RELOC_CR16_GOT_REGREL20;
1043 else if (symbol_with_at_gotc)
1044 cr16_ins->rtype = BFD_RELOC_CR16_GOTC_REGREL20;
1045 else if (symbol_with_m)
1046 cr16_ins->rtype = BFD_RELOC_CR16_ABS20;
1047 else /* Default to (symbol_with_l) */
1048 cr16_ins->rtype = BFD_RELOC_CR16_ABS24;
1049 }
1050 else if (IS_INSN_TYPE (BRANCH_NEQ_INS))
1051 cr16_ins->rtype = BFD_RELOC_CR16_DISP4;
1052 break;
1053
1054 case arg_ic:
1055 if (IS_INSN_TYPE (ARITH_INS))
1056 {
1057 if (symbol_with_at_got)
1058 cr16_ins->rtype = BFD_RELOC_CR16_GOT_REGREL20;
1059 else if (symbol_with_at_gotc)
1060 cr16_ins->rtype = BFD_RELOC_CR16_GOTC_REGREL20;
1061 else if (symbol_with_s)
1062 cr16_ins->rtype = BFD_RELOC_CR16_IMM4;
1063 else if (symbol_with_m)
1064 cr16_ins->rtype = BFD_RELOC_CR16_IMM20;
1065 else if (symbol_with_at)
1066 cr16_ins->rtype = BFD_RELOC_CR16_IMM32a;
1067 else /* Default to (symbol_with_l) */
1068 cr16_ins->rtype = BFD_RELOC_CR16_IMM32;
1069 }
1070 else if (IS_INSN_TYPE (ARITH_BYTE_INS))
1071 {
1072 cr16_ins->rtype = BFD_RELOC_CR16_IMM16;
1073 }
1074 break;
1075 default:
1076 break;
1077 }
1078 break;
1079
1080 default:
1081 cur_arg->X_op = cr16_ins->exp.X_op;
1082 break;
1083 }
1084
1085 input_line_pointer = saved_input_line_pointer;
1086 return;
1087 }
1088
1089 /* Retrieve the opcode image of a given register.
1090 If the register is illegal for the current instruction,
1091 issue an error. */
1092
1093 static int
1094 getreg_image (reg r)
1095 {
1096 const reg_entry *rreg;
1097 char *reg_name;
1098 int is_procreg = 0; /* Nonzero means argument should be processor reg. */
1099
1100 /* Check whether the register is in registers table. */
1101 if (r < MAX_REG)
1102 rreg = cr16_regtab + r;
1103 else /* Register not found. */
1104 {
1105 as_bad (_("Unknown register: `%d'"), r);
1106 return 0;
1107 }
1108
1109 reg_name = rreg->name;
1110
1111 /* Issue a error message when register is illegal. */
1112 #define IMAGE_ERR \
1113 as_bad (_("Illegal register (`%s') in Instruction: `%s'"), \
1114 reg_name, ins_parse);
1115
1116 switch (rreg->type)
1117 {
1118 case CR16_R_REGTYPE:
1119 if (! is_procreg)
1120 return rreg->image;
1121 else
1122 IMAGE_ERR;
1123 break;
1124
1125 case CR16_P_REGTYPE:
1126 return rreg->image;
1127 break;
1128
1129 default:
1130 IMAGE_ERR;
1131 break;
1132 }
1133
1134 return 0;
1135 }
1136
1137 /* Parsing different types of operands
1138 -> constants Immediate/Absolute/Relative numbers
1139 -> Labels Relocatable symbols
1140 -> (reg pair base) Register pair base
1141 -> (rbase) Register base
1142 -> disp(rbase) Register relative
1143 -> [rinx]disp(reg pair) Register index with reg pair mode
1144 -> disp(rbase,ridx,scl) Register index mode. */
1145
1146 static void
1147 set_operand (char *operand, ins * cr16_ins)
1148 {
1149 char *operandS; /* Pointer to start of sub-operand. */
1150 char *operandE; /* Pointer to end of sub-operand. */
1151
1152 argument *cur_arg = &cr16_ins->arg[cur_arg_num]; /* Current argument. */
1153
1154 /* Initialize pointers. */
1155 operandS = operandE = operand;
1156
1157 switch (cur_arg->type)
1158 {
1159 case arg_ic: /* Case $0x18. */
1160 operandS++;
1161 /* Fall through. */
1162 case arg_c: /* Case 0x18. */
1163 /* Set constant. */
1164 process_label_constant (operandS, cr16_ins);
1165
1166 if (cur_arg->type != arg_ic)
1167 cur_arg->type = arg_c;
1168 break;
1169
1170 case arg_icr: /* Case $0x18(r1). */
1171 operandS++;
1172 case arg_cr: /* Case 0x18(r1). */
1173 /* Set displacement constant. */
1174 while (*operandE != '(')
1175 operandE++;
1176 *operandE = '\0';
1177 process_label_constant (operandS, cr16_ins);
1178 operandS = operandE;
1179 /* Fall through. */
1180 case arg_rbase: /* Case (r1) or (r1,r0). */
1181 operandS++;
1182 /* Set register base. */
1183 while (*operandE != ')')
1184 operandE++;
1185 *operandE = '\0';
1186 if ((cur_arg->r = get_register (operandS)) == nullregister)
1187 as_bad (_("Illegal register `%s' in Instruction `%s'"),
1188 operandS, ins_parse);
1189
1190 /* set the arg->rp, if reg is "r12" or "r13" or "14" or "15" */
1191 if ((cur_arg->type != arg_rbase)
1192 && ((getreg_image (cur_arg->r) == 12)
1193 || (getreg_image (cur_arg->r) == 13)
1194 || (getreg_image (cur_arg->r) == 14)
1195 || (getreg_image (cur_arg->r) == 15)))
1196 {
1197 cur_arg->type = arg_crp;
1198 cur_arg->rp = cur_arg->r;
1199 }
1200 break;
1201
1202 case arg_crp: /* Case 0x18(r1,r0). */
1203 /* Set displacement constant. */
1204 while (*operandE != '(')
1205 operandE++;
1206 *operandE = '\0';
1207 process_label_constant (operandS, cr16_ins);
1208 operandS = operandE;
1209 operandS++;
1210 /* Set register pair base. */
1211 while (*operandE != ')')
1212 operandE++;
1213 *operandE = '\0';
1214 if ((cur_arg->rp = get_register_pair (operandS)) == nullregister)
1215 as_bad (_("Illegal register pair `%s' in Instruction `%s'"),
1216 operandS, ins_parse);
1217 break;
1218
1219 case arg_idxr:
1220 /* Set register pair base. */
1221 if ((strchr (operandS,'(') != NULL))
1222 {
1223 while ((*operandE != '(') && (! ISSPACE (*operandE)))
1224 operandE++;
1225 if ((cur_arg->rp = get_index_register_pair (operandE)) == nullregister)
1226 as_bad (_("Illegal register pair `%s' in Instruction `%s'"),
1227 operandS, ins_parse);
1228 *operandE++ = '\0';
1229 cur_arg->type = arg_idxrp;
1230 }
1231 else
1232 cur_arg->rp = -1;
1233
1234 operandE = operandS;
1235 /* Set displacement constant. */
1236 while (*operandE != ']')
1237 operandE++;
1238 process_label_constant (++operandE, cr16_ins);
1239 *operandE++ = '\0';
1240 operandE = operandS;
1241
1242 /* Set index register . */
1243 operandS = strchr (operandE,'[');
1244 if (operandS != NULL)
1245 { /* Eliminate '[', detach from rest of operand. */
1246 *operandS++ = '\0';
1247
1248 operandE = strchr (operandS, ']');
1249
1250 if (operandE == NULL)
1251 as_bad (_("unmatched '['"));
1252 else
1253 { /* Eliminate ']' and make sure it was the last thing
1254 in the string. */
1255 *operandE = '\0';
1256 if (*(operandE + 1) != '\0')
1257 as_bad (_("garbage after index spec ignored"));
1258 }
1259 }
1260
1261 if ((cur_arg->i_r = get_index_register (operandS)) == nullregister)
1262 as_bad (_("Illegal register `%s' in Instruction `%s'"),
1263 operandS, ins_parse);
1264 *operandE = '\0';
1265 *operandS = '\0';
1266 break;
1267
1268 default:
1269 break;
1270 }
1271 }
1272
1273 /* Parse a single operand.
1274 operand - Current operand to parse.
1275 cr16_ins - Current assembled instruction. */
1276
1277 static void
1278 parse_operand (char *operand, ins * cr16_ins)
1279 {
1280 int ret_val;
1281 argument *cur_arg = cr16_ins->arg + cur_arg_num; /* Current argument. */
1282
1283 /* Initialize the type to NULL before parsing. */
1284 cur_arg->type = nullargs;
1285
1286 /* Check whether this is a condition code . */
1287 if ((IS_INSN_MNEMONIC ("b")) && ((ret_val = get_cc (operand)) != -1))
1288 {
1289 cur_arg->type = arg_cc;
1290 cur_arg->cc = ret_val;
1291 cur_arg->X_op = O_register;
1292 return;
1293 }
1294
1295 /* Check whether this is a general processor register. */
1296 if ((ret_val = get_register (operand)) != nullregister)
1297 {
1298 cur_arg->type = arg_r;
1299 cur_arg->r = ret_val;
1300 cur_arg->X_op = 0;
1301 return;
1302 }
1303
1304 /* Check whether this is a general processor register pair. */
1305 if ((operand[0] == '(')
1306 && ((ret_val = get_register_pair (operand)) != nullregister))
1307 {
1308 cur_arg->type = arg_rp;
1309 cur_arg->rp = ret_val;
1310 cur_arg->X_op = O_register;
1311 return;
1312 }
1313
1314 /* Check whether the operand is a processor register.
1315 For "lprd" and "sprd" instruction, only 32 bit
1316 processor registers used. */
1317 if (!(IS_INSN_MNEMONIC ("lprd") || (IS_INSN_MNEMONIC ("sprd")))
1318 && ((ret_val = get_pregister (operand)) != nullpregister))
1319 {
1320 cur_arg->type = arg_pr;
1321 cur_arg->pr = ret_val;
1322 cur_arg->X_op = O_register;
1323 return;
1324 }
1325
1326 /* Check whether this is a processor register - 32 bit. */
1327 if ((ret_val = get_pregisterp (operand)) != nullpregister)
1328 {
1329 cur_arg->type = arg_prp;
1330 cur_arg->prp = ret_val;
1331 cur_arg->X_op = O_register;
1332 return;
1333 }
1334
1335 /* Deal with special characters. */
1336 switch (operand[0])
1337 {
1338 case '$':
1339 if (strchr (operand, '(') != NULL)
1340 cur_arg->type = arg_icr;
1341 else
1342 cur_arg->type = arg_ic;
1343 goto set_params;
1344 break;
1345
1346 case '(':
1347 cur_arg->type = arg_rbase;
1348 goto set_params;
1349 break;
1350
1351 case '[':
1352 cur_arg->type = arg_idxr;
1353 goto set_params;
1354 break;
1355
1356 default:
1357 break;
1358 }
1359
1360 if (strchr (operand, '(') != NULL)
1361 {
1362 if (strchr (operand, ',') != NULL
1363 && (strchr (operand, ',') > strchr (operand, '(')))
1364 cur_arg->type = arg_crp;
1365 else
1366 cur_arg->type = arg_cr;
1367 }
1368 else
1369 cur_arg->type = arg_c;
1370
1371 /* Parse an operand according to its type. */
1372 set_params:
1373 cur_arg->constant = 0;
1374 set_operand (operand, cr16_ins);
1375 }
1376
1377 /* Parse the various operands. Each operand is then analyzed to fillup
1378 the fields in the cr16_ins data structure. */
1379
1380 static void
1381 parse_operands (ins * cr16_ins, char *operands)
1382 {
1383 char *operandS; /* Operands string. */
1384 char *operandH, *operandT; /* Single operand head/tail pointers. */
1385 int allocated = 0; /* Indicates a new operands string was allocated.*/
1386 char *operand[MAX_OPERANDS];/* Separating the operands. */
1387 int op_num = 0; /* Current operand number we are parsing. */
1388 int bracket_flag = 0; /* Indicates a bracket '(' was found. */
1389 int sq_bracket_flag = 0; /* Indicates a square bracket '[' was found. */
1390
1391 /* Preprocess the list of registers, if necessary. */
1392 operandS = operandH = operandT = operands;
1393
1394 while (*operandT != '\0')
1395 {
1396 if (*operandT == ',' && bracket_flag != 1 && sq_bracket_flag != 1)
1397 {
1398 *operandT++ = '\0';
1399 operand[op_num++] = strdup (operandH);
1400 operandH = operandT;
1401 continue;
1402 }
1403
1404 if (*operandT == ' ')
1405 as_bad (_("Illegal operands (whitespace): `%s'"), ins_parse);
1406
1407 if (*operandT == '(')
1408 bracket_flag = 1;
1409 else if (*operandT == '[')
1410 sq_bracket_flag = 1;
1411
1412 if (*operandT == ')')
1413 {
1414 if (bracket_flag)
1415 bracket_flag = 0;
1416 else
1417 as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
1418 }
1419 else if (*operandT == ']')
1420 {
1421 if (sq_bracket_flag)
1422 sq_bracket_flag = 0;
1423 else
1424 as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
1425 }
1426
1427 if (bracket_flag == 1 && *operandT == ')')
1428 bracket_flag = 0;
1429 else if (sq_bracket_flag == 1 && *operandT == ']')
1430 sq_bracket_flag = 0;
1431
1432 operandT++;
1433 }
1434
1435 /* Adding the last operand. */
1436 operand[op_num++] = strdup (operandH);
1437 cr16_ins->nargs = op_num;
1438
1439 /* Verifying correct syntax of operands (all brackets should be closed). */
1440 if (bracket_flag || sq_bracket_flag)
1441 as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
1442
1443 /* Now we parse each operand separately. */
1444 for (op_num = 0; op_num < cr16_ins->nargs; op_num++)
1445 {
1446 cur_arg_num = op_num;
1447 parse_operand (operand[op_num], cr16_ins);
1448 free (operand[op_num]);
1449 }
1450
1451 if (allocated)
1452 free (operandS);
1453 }
1454
1455 /* Get the trap index in dispatch table, given its name.
1456 This routine is used by assembling the 'excp' instruction. */
1457
1458 static int
1459 gettrap (char *s)
1460 {
1461 const trap_entry *trap;
1462
1463 for (trap = cr16_traps; trap < (cr16_traps + NUMTRAPS); trap++)
1464 if (strcasecmp (trap->name, s) == 0)
1465 return trap->entry;
1466
1467 /* To make compatible with CR16 4.1 tools, the below 3-lines of
1468 * code added. Refer: Development Tracker item #123 */
1469 for (trap = cr16_traps; trap < (cr16_traps + NUMTRAPS); trap++)
1470 if (trap->entry == (unsigned int) atoi (s))
1471 return trap->entry;
1472
1473 as_bad (_("Unknown exception: `%s'"), s);
1474 return 0;
1475 }
1476
1477 /* Top level module where instruction parsing starts.
1478 cr16_ins - data structure holds some information.
1479 operands - holds the operands part of the whole instruction. */
1480
1481 static void
1482 parse_insn (ins *insn, char *operands)
1483 {
1484 int i;
1485
1486 /* Handle instructions with no operands. */
1487 for (i = 0; cr16_no_op_insn[i] != NULL; i++)
1488 {
1489 if (streq (cr16_no_op_insn[i], instruction->mnemonic))
1490 {
1491 insn->nargs = 0;
1492 return;
1493 }
1494 }
1495
1496 /* Handle 'excp' instructions. */
1497 if (IS_INSN_MNEMONIC ("excp"))
1498 {
1499 insn->nargs = 1;
1500 insn->arg[0].type = arg_ic;
1501 insn->arg[0].constant = gettrap (operands);
1502 insn->arg[0].X_op = O_constant;
1503 return;
1504 }
1505
1506 if (operands != NULL)
1507 parse_operands (insn, operands);
1508 }
1509
1510 /* bCC instruction requires special handling. */
1511 static char *
1512 get_b_cc (char * op)
1513 {
1514 unsigned int i;
1515 char op1[5];
1516
1517 for (i = 1; i < strlen (op); i++)
1518 op1[i-1] = op[i];
1519
1520 op1[i-1] = '\0';
1521
1522 for (i = 0; i < cr16_num_cc ; i++)
1523 if (streq (op1, cr16_b_cond_tab[i]))
1524 return (char *) cr16_b_cond_tab[i];
1525
1526 return NULL;
1527 }
1528
1529 /* bCC instruction requires special handling. */
1530 static int
1531 is_bcc_insn (char * op)
1532 {
1533 if (!(streq (op, "bal") || streq (op, "beq0b") || streq (op, "bnq0b")
1534 || streq (op, "beq0w") || streq (op, "bnq0w")))
1535 if ((op[0] == 'b') && (get_b_cc (op) != NULL))
1536 return 1;
1537 return 0;
1538 }
1539
1540 /* Cinv instruction requires special handling. */
1541
1542 static void
1543 check_cinv_options (char * operand)
1544 {
1545 char *p = operand;
1546
1547 while (*++p != ']')
1548 {
1549 switch (*p)
1550 {
1551 case ',':
1552 case ' ':
1553 case 'i':
1554 case 'u':
1555 case 'd':
1556 break;
1557 default:
1558 as_bad (_("Illegal `cinv' parameter: `%c'"), *p);
1559 }
1560 }
1561 }
1562
1563 /* Retrieve the opcode image of a given register pair.
1564 If the register is illegal for the current instruction,
1565 issue an error. */
1566
1567 static int
1568 getregp_image (reg r)
1569 {
1570 const reg_entry *rreg;
1571 char *reg_name;
1572
1573 /* Check whether the register is in registers table. */
1574 if (r < MAX_REG)
1575 rreg = cr16_regptab + r;
1576 /* Register not found. */
1577 else
1578 {
1579 as_bad (_("Unknown register pair: `%d'"), r);
1580 return 0;
1581 }
1582
1583 reg_name = rreg->name;
1584
1585 /* Issue a error message when register pair is illegal. */
1586 #define RPAIR_IMAGE_ERR \
1587 as_bad (_("Illegal register pair (`%s') in Instruction: `%s'"), \
1588 reg_name, ins_parse); \
1589 break;
1590
1591 switch (rreg->type)
1592 {
1593 case CR16_RP_REGTYPE:
1594 return rreg->image;
1595 default:
1596 RPAIR_IMAGE_ERR;
1597 }
1598
1599 return 0;
1600 }
1601
1602 /* Retrieve the opcode image of a given index register pair.
1603 If the register is illegal for the current instruction,
1604 issue an error. */
1605
1606 static int
1607 getidxregp_image (reg r)
1608 {
1609 const reg_entry *rreg;
1610 char *reg_name;
1611
1612 /* Check whether the register is in registers table. */
1613 if (r < MAX_REG)
1614 rreg = cr16_regptab + r;
1615 /* Register not found. */
1616 else
1617 {
1618 as_bad (_("Unknown register pair: `%d'"), r);
1619 return 0;
1620 }
1621
1622 reg_name = rreg->name;
1623
1624 /* Issue a error message when register pair is illegal. */
1625 #define IDX_RPAIR_IMAGE_ERR \
1626 as_bad (_("Illegal index register pair (`%s') in Instruction: `%s'"), \
1627 reg_name, ins_parse); \
1628
1629 if (rreg->type == CR16_RP_REGTYPE)
1630 {
1631 switch (rreg->image)
1632 {
1633 case 0: return 0; break;
1634 case 2: return 1; break;
1635 case 4: return 2; break;
1636 case 6: return 3; break;
1637 case 8: return 4; break;
1638 case 10: return 5; break;
1639 case 3: return 6; break;
1640 case 5: return 7; break;
1641 default:
1642 break;
1643 }
1644 }
1645
1646 IDX_RPAIR_IMAGE_ERR;
1647 return 0;
1648 }
1649
1650 /* Retrieve the opcode image of a given processor register.
1651 If the register is illegal for the current instruction,
1652 issue an error. */
1653 static int
1654 getprocreg_image (int r)
1655 {
1656 const reg_entry *rreg;
1657 char *reg_name;
1658
1659 /* Check whether the register is in registers table. */
1660 if (r >= MAX_REG && r < MAX_PREG)
1661 rreg = &cr16_pregtab[r - MAX_REG];
1662 /* Register not found. */
1663 else
1664 {
1665 as_bad (_("Unknown processor register : `%d'"), r);
1666 return 0;
1667 }
1668
1669 reg_name = rreg->name;
1670
1671 /* Issue a error message when register pair is illegal. */
1672 #define PROCREG_IMAGE_ERR \
1673 as_bad (_("Illegal processor register (`%s') in Instruction: `%s'"), \
1674 reg_name, ins_parse); \
1675 break;
1676
1677 switch (rreg->type)
1678 {
1679 case CR16_P_REGTYPE:
1680 return rreg->image;
1681 default:
1682 PROCREG_IMAGE_ERR;
1683 }
1684
1685 return 0;
1686 }
1687
1688 /* Retrieve the opcode image of a given processor register.
1689 If the register is illegal for the current instruction,
1690 issue an error. */
1691 static int
1692 getprocregp_image (int r)
1693 {
1694 const reg_entry *rreg;
1695 char *reg_name;
1696 int pregptab_disp = 0;
1697
1698 /* Check whether the register is in registers table. */
1699 if (r >= MAX_REG && r < MAX_PREG)
1700 {
1701 r = r - MAX_REG;
1702 switch (r)
1703 {
1704 case 4: pregptab_disp = 1; break;
1705 case 6: pregptab_disp = 2; break;
1706 case 8:
1707 case 9:
1708 case 10:
1709 pregptab_disp = 3; break;
1710 case 12:
1711 pregptab_disp = 4; break;
1712 case 14:
1713 pregptab_disp = 5; break;
1714 default: break;
1715 }
1716 rreg = &cr16_pregptab[r - pregptab_disp];
1717 }
1718 /* Register not found. */
1719 else
1720 {
1721 as_bad (_("Unknown processor register (32 bit) : `%d'"), r);
1722 return 0;
1723 }
1724
1725 reg_name = rreg->name;
1726
1727 /* Issue a error message when register pair is illegal. */
1728 #define PROCREGP_IMAGE_ERR \
1729 as_bad (_("Illegal 32 bit - processor register (`%s') in Instruction: `%s'"),\
1730 reg_name, ins_parse); \
1731 break;
1732
1733 switch (rreg->type)
1734 {
1735 case CR16_P_REGTYPE:
1736 return rreg->image;
1737 default:
1738 PROCREGP_IMAGE_ERR;
1739 }
1740
1741 return 0;
1742 }
1743
1744 /* Routine used to represent integer X using NBITS bits. */
1745
1746 static long
1747 getconstant (long x, int nbits)
1748 {
1749 /* The following expression avoids overflow if
1750 'nbits' is the number of bits in 'bfd_vma'. */
1751 return (x & ((((1 << (nbits - 1)) - 1) << 1) | 1));
1752 }
1753
1754 /* Print a constant value to 'output_opcode':
1755 ARG holds the operand's type and value.
1756 SHIFT represents the location of the operand to be print into.
1757 NBITS determines the size (in bits) of the constant. */
1758
1759 static void
1760 print_constant (int nbits, int shift, argument *arg)
1761 {
1762 unsigned long mask = 0;
1763
1764 long constant = getconstant (arg->constant, nbits);
1765
1766 switch (nbits)
1767 {
1768 case 32:
1769 case 28:
1770 /* mask the upper part of the constant, that is, the bits
1771 going to the lowest byte of output_opcode[0].
1772 The upper part of output_opcode[1] is always filled,
1773 therefore it is always masked with 0xFFFF. */
1774 mask = (1 << (nbits - 16)) - 1;
1775 /* Divide the constant between two consecutive words :
1776 0 1 2 3
1777 +---------+---------+---------+---------+
1778 | | X X X X | x X x X | |
1779 +---------+---------+---------+---------+
1780 output_opcode[0] output_opcode[1] */
1781
1782 CR16_PRINT (0, (constant >> WORD_SHIFT) & mask, 0);
1783 CR16_PRINT (1, (constant & 0xFFFF), WORD_SHIFT);
1784 break;
1785
1786 case 21:
1787 if ((nbits == 21) && (IS_INSN_TYPE (LD_STOR_INS)))
1788 nbits = 20;
1789 /* Fall through. */
1790 case 24:
1791 case 22:
1792 case 20:
1793 /* mask the upper part of the constant, that is, the bits
1794 going to the lowest byte of output_opcode[0].
1795 The upper part of output_opcode[1] is always filled,
1796 therefore it is always masked with 0xFFFF. */
1797 mask = (1 << (nbits - 16)) - 1;
1798 /* Divide the constant between two consecutive words :
1799 0 1 2 3
1800 +---------+---------+---------+---------+
1801 | | X X X X | - X - X | |
1802 +---------+---------+---------+---------+
1803 output_opcode[0] output_opcode[1] */
1804
1805 if ((instruction->size > 2) && (shift == WORD_SHIFT))
1806 {
1807 if (arg->type == arg_idxrp)
1808 {
1809 CR16_PRINT (0, ((constant >> WORD_SHIFT) & mask) << 8, 0);
1810 CR16_PRINT (1, (constant & 0xFFFF), WORD_SHIFT);
1811 }
1812 else
1813 {
1814 CR16_PRINT (0, (((((constant >> WORD_SHIFT) & mask) << 8) & 0x0f00) | ((((constant >> WORD_SHIFT) & mask) >> 4) & 0xf)),0);
1815 CR16_PRINT (1, (constant & 0xFFFF), WORD_SHIFT);
1816 }
1817 }
1818 else
1819 CR16_PRINT (0, constant, shift);
1820 break;
1821
1822 case 14:
1823 if (arg->type == arg_idxrp)
1824 {
1825 if (instruction->size == 2)
1826 {
1827 CR16_PRINT (0, ((constant) & 0xf), shift); /* 0-3 bits. */
1828 CR16_PRINT (0, ((constant >> 4) & 0x3), (shift + 20)); /* 4-5 bits. */
1829 CR16_PRINT (0, ((constant >> 6) & 0x3), (shift + 14)); /* 6-7 bits. */
1830 CR16_PRINT (0, ((constant >> 8) & 0x3f), (shift + 8)); /* 8-13 bits. */
1831 }
1832 else
1833 CR16_PRINT (0, constant, shift);
1834 }
1835 break;
1836
1837 case 16:
1838 case 12:
1839 /* When instruction size is 3 and 'shift' is 16, a 16-bit constant is
1840 always filling the upper part of output_opcode[1]. If we mistakenly
1841 write it to output_opcode[0], the constant prefix (that is, 'match')
1842 will be overridden.
1843 0 1 2 3
1844 +---------+---------+---------+---------+
1845 | 'match' | | X X X X | |
1846 +---------+---------+---------+---------+
1847 output_opcode[0] output_opcode[1] */
1848
1849 if ((instruction->size > 2) && (shift == WORD_SHIFT))
1850 CR16_PRINT (1, constant, WORD_SHIFT);
1851 else
1852 CR16_PRINT (0, constant, shift);
1853 break;
1854
1855 case 8:
1856 CR16_PRINT (0, ((constant / 2) & 0xf), shift);
1857 CR16_PRINT (0, ((constant / 2) >> 4), (shift + 8));
1858 break;
1859
1860 default:
1861 CR16_PRINT (0, constant, shift);
1862 break;
1863 }
1864 }
1865
1866 /* Print an operand to 'output_opcode', which later on will be
1867 printed to the object file:
1868 ARG holds the operand's type, size and value.
1869 SHIFT represents the printing location of operand.
1870 NBITS determines the size (in bits) of a constant operand. */
1871
1872 static void
1873 print_operand (int nbits, int shift, argument *arg)
1874 {
1875 switch (arg->type)
1876 {
1877 case arg_cc:
1878 CR16_PRINT (0, arg->cc, shift);
1879 break;
1880
1881 case arg_r:
1882 CR16_PRINT (0, getreg_image (arg->r), shift);
1883 break;
1884
1885 case arg_rp:
1886 CR16_PRINT (0, getregp_image (arg->rp), shift);
1887 break;
1888
1889 case arg_pr:
1890 CR16_PRINT (0, getprocreg_image (arg->pr), shift);
1891 break;
1892
1893 case arg_prp:
1894 CR16_PRINT (0, getprocregp_image (arg->prp), shift);
1895 break;
1896
1897 case arg_idxrp:
1898 /* 16 12 8 6 0
1899 +-----------------------------+
1900 | r_index | disp | rp_base |
1901 +-----------------------------+ */
1902
1903 if (instruction->size == 3)
1904 {
1905 CR16_PRINT (0, getidxregp_image (arg->rp), 0);
1906 if (getreg_image (arg->i_r) == 12)
1907 CR16_PRINT (0, 0, 3);
1908 else
1909 CR16_PRINT (0, 1, 3);
1910 }
1911 else
1912 {
1913 CR16_PRINT (0, getidxregp_image (arg->rp), 16);
1914 if (getreg_image (arg->i_r) == 12)
1915 CR16_PRINT (0, 0, 19);
1916 else
1917 CR16_PRINT (0, 1, 19);
1918 }
1919 print_constant (nbits, shift, arg);
1920 break;
1921
1922 case arg_idxr:
1923 if (getreg_image (arg->i_r) == 12)
1924 if (IS_INSN_MNEMONIC ("cbitb") || IS_INSN_MNEMONIC ("sbitb")
1925 || IS_INSN_MNEMONIC ("tbitb"))
1926 CR16_PRINT (0, 0, 23);
1927 else CR16_PRINT (0, 0, 24);
1928 else
1929 if (IS_INSN_MNEMONIC ("cbitb") || IS_INSN_MNEMONIC ("sbitb")
1930 || IS_INSN_MNEMONIC ("tbitb"))
1931 CR16_PRINT (0, 1, 23);
1932 else CR16_PRINT (0, 1, 24);
1933
1934 print_constant (nbits, shift, arg);
1935 break;
1936
1937 case arg_ic:
1938 case arg_c:
1939 print_constant (nbits, shift, arg);
1940 break;
1941
1942 case arg_rbase:
1943 CR16_PRINT (0, getreg_image (arg->r), shift);
1944 break;
1945
1946 case arg_cr:
1947 print_constant (nbits, shift , arg);
1948 /* Add the register argument to the output_opcode. */
1949 CR16_PRINT (0, getreg_image (arg->r), (shift+16));
1950 break;
1951
1952 case arg_crp:
1953 print_constant (nbits, shift , arg);
1954 if (instruction->size > 1)
1955 CR16_PRINT (0, getregp_image (arg->rp), (shift + 16));
1956 else if (IS_INSN_TYPE (LD_STOR_INS) || (IS_INSN_TYPE (CSTBIT_INS)))
1957 {
1958 if (instruction->size == 2)
1959 CR16_PRINT (0, getregp_image (arg->rp), (shift - 8));
1960 else if (instruction->size == 1)
1961 CR16_PRINT (0, getregp_image (arg->rp), 16);
1962 }
1963 else
1964 CR16_PRINT (0, getregp_image (arg->rp), shift);
1965 break;
1966
1967 default:
1968 break;
1969 }
1970 }
1971
1972 /* Retrieve the number of operands for the current assembled instruction. */
1973
1974 static int
1975 get_number_of_operands (void)
1976 {
1977 int i;
1978
1979 for (i = 0; instruction->operands[i].op_type && i < MAX_OPERANDS; i++)
1980 ;
1981 return i;
1982 }
1983
1984 /* Verify that the number NUM can be represented in BITS bits (that is,
1985 within its permitted range), based on the instruction's FLAGS.
1986 If UPDATE is nonzero, update the value of NUM if necessary.
1987 Return OP_LEGAL upon success, actual error type upon failure. */
1988
1989 static op_err
1990 check_range (long *num, int bits, int unsigned flags, int update)
1991 {
1992 long min, max;
1993 op_err retval = OP_LEGAL;
1994 long value = *num;
1995
1996 if (bits == 0 && value > 0) return OP_OUT_OF_RANGE;
1997
1998 /* For hosts with longs bigger than 32-bits make sure that the top
1999 bits of a 32-bit negative value read in by the parser are set,
2000 so that the correct comparisons are made. */
2001 if (value & 0x80000000)
2002 value |= (-1UL << 31);
2003
2004
2005 /* Verify operand value is even. */
2006 if (flags & OP_EVEN)
2007 {
2008 if (value % 2)
2009 return OP_NOT_EVEN;
2010 }
2011
2012 if (flags & OP_DEC)
2013 {
2014 value -= 1;
2015 if (update)
2016 *num = value;
2017 }
2018
2019 if (flags & OP_SHIFT)
2020 {
2021 value >>= 1;
2022 if (update)
2023 *num = value;
2024 }
2025 else if (flags & OP_SHIFT_DEC)
2026 {
2027 value = (value >> 1) - 1;
2028 if (update)
2029 *num = value;
2030 }
2031
2032 if (flags & OP_ABS20)
2033 {
2034 if (value > 0xEFFFF)
2035 return OP_OUT_OF_RANGE;
2036 }
2037
2038 if (flags & OP_ESC)
2039 {
2040 if (value == 0xB || value == 0x9)
2041 return OP_OUT_OF_RANGE;
2042 else if (value == -1)
2043 {
2044 if (update)
2045 *num = 9;
2046 return retval;
2047 }
2048 }
2049
2050 if (flags & OP_ESC1)
2051 {
2052 if (value > 13)
2053 return OP_OUT_OF_RANGE;
2054 }
2055
2056 if (flags & OP_SIGNED)
2057 {
2058 max = (1 << (bits - 1)) - 1;
2059 min = - (1 << (bits - 1));
2060 if ((value > max) || (value < min))
2061 retval = OP_OUT_OF_RANGE;
2062 }
2063 else if (flags & OP_UNSIGNED)
2064 {
2065 max = ((((1 << (bits - 1)) - 1) << 1) | 1);
2066 min = 0;
2067 if (((unsigned long) value > (unsigned long) max)
2068 || ((unsigned long) value < (unsigned long) min))
2069 retval = OP_OUT_OF_RANGE;
2070 }
2071 else if (flags & OP_NEG)
2072 {
2073 max = - 1;
2074 min = - ((1 << (bits - 1)) - 1);
2075 if ((value > max) || (value < min))
2076 retval = OP_OUT_OF_RANGE;
2077 }
2078 return retval;
2079 }
2080
2081 /* Bunch of error checking.
2082 The checks are made after a matching instruction was found. */
2083
2084 static void
2085 warn_if_needed (ins *insn)
2086 {
2087 /* If the post-increment address mode is used and the load/store
2088 source register is the same as rbase, the result of the
2089 instruction is undefined. */
2090 if (IS_INSN_TYPE (LD_STOR_INS_INC))
2091 {
2092 /* Enough to verify that one of the arguments is a simple reg. */
2093 if ((insn->arg[0].type == arg_r) || (insn->arg[1].type == arg_r))
2094 if (insn->arg[0].r == insn->arg[1].r)
2095 as_bad (_("Same src/dest register is used (`r%d'), result is undefined"), insn->arg[0].r);
2096 }
2097
2098 if (IS_INSN_MNEMONIC ("pop")
2099 || IS_INSN_MNEMONIC ("push")
2100 || IS_INSN_MNEMONIC ("popret"))
2101 {
2102 unsigned int count = insn->arg[0].constant, reg_val;
2103
2104 /* Check if count operand caused to save/retrieve the RA twice
2105 to generate warning message. */
2106 if (insn->nargs > 2)
2107 {
2108 reg_val = getreg_image (insn->arg[1].r);
2109
2110 if ( ((reg_val == 9) && (count > 7))
2111 || ((reg_val == 10) && (count > 6))
2112 || ((reg_val == 11) && (count > 5))
2113 || ((reg_val == 12) && (count > 4))
2114 || ((reg_val == 13) && (count > 2))
2115 || ((reg_val == 14) && (count > 0)))
2116 as_warn (_("RA register is saved twice."));
2117
2118 /* Check if the third operand is "RA" or "ra" */
2119 if (!(((insn->arg[2].r) == ra) || ((insn->arg[2].r) == RA)))
2120 as_bad (_("`%s' Illegal use of registers."), ins_parse);
2121 }
2122
2123 if (insn->nargs > 1)
2124 {
2125 reg_val = getreg_image (insn->arg[1].r);
2126
2127 /* If register is a register pair ie r12/r13/r14 in operand1, then
2128 the count constant should be validated. */
2129 if (((reg_val == 11) && (count > 7))
2130 || ((reg_val == 12) && (count > 6))
2131 || ((reg_val == 13) && (count > 4))
2132 || ((reg_val == 14) && (count > 2))
2133 || ((reg_val == 15) && (count > 0)))
2134 as_bad (_("`%s' Illegal count-register combination."), ins_parse);
2135 }
2136 else
2137 {
2138 /* Check if the operand is "RA" or "ra" */
2139 if (!(((insn->arg[0].r) == ra) || ((insn->arg[0].r) == RA)))
2140 as_bad (_("`%s' Illegal use of register."), ins_parse);
2141 }
2142 }
2143
2144 /* Some instruction assume the stack pointer as rptr operand.
2145 Issue an error when the register to be loaded is also SP. */
2146 if (instruction->flags & NO_SP)
2147 {
2148 if (getreg_image (insn->arg[1].r) == getreg_image (sp))
2149 as_bad (_("`%s' has undefined result"), ins_parse);
2150 }
2151
2152 /* If the rptr register is specified as one of the registers to be loaded,
2153 the final contents of rptr are undefined. Thus, we issue an error. */
2154 if (instruction->flags & NO_RPTR)
2155 {
2156 if ((1 << getreg_image (insn->arg[0].r)) & insn->arg[1].constant)
2157 as_bad (_("Same src/dest register is used (`r%d'),result is undefined"),
2158 getreg_image (insn->arg[0].r));
2159 }
2160 }
2161
2162 /* In some cases, we need to adjust the instruction pointer although a
2163 match was already found. Here, we gather all these cases.
2164 Returns 1 if instruction pointer was adjusted, otherwise 0. */
2165
2166 static int
2167 adjust_if_needed (ins *insn ATTRIBUTE_UNUSED)
2168 {
2169 int ret_value = 0;
2170
2171 if ((IS_INSN_TYPE (CSTBIT_INS)) || (IS_INSN_TYPE (LD_STOR_INS)))
2172 {
2173 if ((instruction->operands[0].op_type == abs24)
2174 && ((insn->arg[0].constant) > 0xF00000))
2175 {
2176 insn->arg[0].constant &= 0xFFFFF;
2177 instruction--;
2178 ret_value = 1;
2179 }
2180 }
2181
2182 return ret_value;
2183 }
2184
2185 /* Assemble a single instruction:
2186 INSN is already parsed (that is, all operand values and types are set).
2187 For instruction to be assembled, we need to find an appropriate template in
2188 the instruction table, meeting the following conditions:
2189 1: Has the same number of operands.
2190 2: Has the same operand types.
2191 3: Each operand size is sufficient to represent the instruction's values.
2192 Returns 1 upon success, 0 upon failure. */
2193
2194 static int
2195 assemble_insn (const char *mnemonic, ins *insn)
2196 {
2197 /* Type of each operand in the current template. */
2198 argtype cur_type[MAX_OPERANDS];
2199 /* Size (in bits) of each operand in the current template. */
2200 unsigned int cur_size[MAX_OPERANDS];
2201 /* Flags of each operand in the current template. */
2202 unsigned int cur_flags[MAX_OPERANDS];
2203 /* Instruction type to match. */
2204 unsigned int ins_type;
2205 /* Boolean flag to mark whether a match was found. */
2206 int match = 0;
2207 int i;
2208 /* Nonzero if an instruction with same number of operands was found. */
2209 int found_same_number_of_operands = 0;
2210 /* Nonzero if an instruction with same argument types was found. */
2211 int found_same_argument_types = 0;
2212 /* Nonzero if a constant was found within the required range. */
2213 int found_const_within_range = 0;
2214 /* Argument number of an operand with invalid type. */
2215 int invalid_optype = -1;
2216 /* Argument number of an operand with invalid constant value. */
2217 int invalid_const = -1;
2218 /* Operand error (used for issuing various constant error messages). */
2219 op_err op_error, const_err = OP_LEGAL;
2220
2221 /* Retrieve data (based on FUNC) for each operand of a given instruction. */
2222 #define GET_CURRENT_DATA(FUNC, ARRAY) \
2223 for (i = 0; i < insn->nargs; i++) \
2224 ARRAY[i] = FUNC (instruction->operands[i].op_type)
2225
2226 #define GET_CURRENT_TYPE GET_CURRENT_DATA (get_optype, cur_type)
2227 #define GET_CURRENT_SIZE GET_CURRENT_DATA (get_opbits, cur_size)
2228 #define GET_CURRENT_FLAGS GET_CURRENT_DATA (get_opflags, cur_flags)
2229
2230 /* Instruction has no operands -> only copy the constant opcode. */
2231 if (insn->nargs == 0)
2232 {
2233 output_opcode[0] = BIN (instruction->match, instruction->match_bits);
2234 return 1;
2235 }
2236
2237 /* In some case, same mnemonic can appear with different instruction types.
2238 For example, 'storb' is supported with 3 different types :
2239 LD_STOR_INS, LD_STOR_INS_INC, STOR_IMM_INS.
2240 We assume that when reaching this point, the instruction type was
2241 pre-determined. We need to make sure that the type stays the same
2242 during a search for matching instruction. */
2243 ins_type = CR16_INS_TYPE (instruction->flags);
2244
2245 while (/* Check that match is still not found. */
2246 match != 1
2247 /* Check we didn't get to end of table. */
2248 && instruction->mnemonic != NULL
2249 /* Check that the actual mnemonic is still available. */
2250 && IS_INSN_MNEMONIC (mnemonic)
2251 /* Check that the instruction type wasn't changed. */
2252 && IS_INSN_TYPE (ins_type))
2253 {
2254 /* Check whether number of arguments is legal. */
2255 if (get_number_of_operands () != insn->nargs)
2256 goto next_insn;
2257 found_same_number_of_operands = 1;
2258
2259 /* Initialize arrays with data of each operand in current template. */
2260 GET_CURRENT_TYPE;
2261 GET_CURRENT_SIZE;
2262 GET_CURRENT_FLAGS;
2263
2264 /* Check for type compatibility. */
2265 for (i = 0; i < insn->nargs; i++)
2266 {
2267 if (cur_type[i] != insn->arg[i].type)
2268 {
2269 if (invalid_optype == -1)
2270 invalid_optype = i + 1;
2271 goto next_insn;
2272 }
2273 }
2274 found_same_argument_types = 1;
2275
2276 for (i = 0; i < insn->nargs; i++)
2277 {
2278 /* If 'bal' instruction size is '2' and reg operand is not 'ra'
2279 then goto next instruction. */
2280 if (IS_INSN_MNEMONIC ("bal") && (i == 0)
2281 && (instruction->size == 2) && (insn->arg[i].rp != 14))
2282 goto next_insn;
2283
2284 /* If 'storb' instruction with 'sp' reg and 16-bit disp of
2285 * reg-pair, leads to undefined trap, so this should use
2286 * 20-bit disp of reg-pair. */
2287 if (IS_INSN_MNEMONIC ("storb") && (instruction->size == 2)
2288 && (insn->arg[i].r == 15) && (insn->arg[i + 1].type == arg_crp))
2289 goto next_insn;
2290
2291 /* Only check range - don't update the constant's value, since the
2292 current instruction may not be the last we try to match.
2293 The constant's value will be updated later, right before printing
2294 it to the object file. */
2295 if ((insn->arg[i].X_op == O_constant)
2296 && (op_error = check_range (&insn->arg[i].constant, cur_size[i],
2297 cur_flags[i], 0)))
2298 {
2299 if (invalid_const == -1)
2300 {
2301 invalid_const = i + 1;
2302 const_err = op_error;
2303 }
2304 goto next_insn;
2305 }
2306 /* For symbols, we make sure the relocation size (which was already
2307 determined) is sufficient. */
2308 else if ((insn->arg[i].X_op == O_symbol)
2309 && ((bfd_reloc_type_lookup (stdoutput, insn->rtype))->bitsize
2310 > cur_size[i]))
2311 goto next_insn;
2312 }
2313 found_const_within_range = 1;
2314
2315 /* If we got till here -> Full match is found. */
2316 match = 1;
2317 break;
2318
2319 /* Try again with next instruction. */
2320 next_insn:
2321 instruction++;
2322 }
2323
2324 if (!match)
2325 {
2326 /* We haven't found a match - instruction can't be assembled. */
2327 if (!found_same_number_of_operands)
2328 as_bad (_("Incorrect number of operands"));
2329 else if (!found_same_argument_types)
2330 as_bad (_("Illegal type of operand (arg %d)"), invalid_optype);
2331 else if (!found_const_within_range)
2332 {
2333 switch (const_err)
2334 {
2335 case OP_OUT_OF_RANGE:
2336 as_bad (_("Operand out of range (arg %d)"), invalid_const);
2337 break;
2338 case OP_NOT_EVEN:
2339 as_bad (_("Operand has odd displacement (arg %d)"), invalid_const);
2340 break;
2341 default:
2342 as_bad (_("Illegal operand (arg %d)"), invalid_const);
2343 break;
2344 }
2345 }
2346
2347 return 0;
2348 }
2349 else
2350 /* Full match - print the encoding to output file. */
2351 {
2352 /* Make further checking (such that couldn't be made earlier).
2353 Warn the user if necessary. */
2354 warn_if_needed (insn);
2355
2356 /* Check whether we need to adjust the instruction pointer. */
2357 if (adjust_if_needed (insn))
2358 /* If instruction pointer was adjusted, we need to update
2359 the size of the current template operands. */
2360 GET_CURRENT_SIZE;
2361
2362 for (i = 0; i < insn->nargs; i++)
2363 {
2364 int j = instruction->flags & REVERSE_MATCH ?
2365 i == 0 ? 1 :
2366 i == 1 ? 0 : i :
2367 i;
2368
2369 /* This time, update constant value before printing it. */
2370 if ((insn->arg[j].X_op == O_constant)
2371 && (check_range (&insn->arg[j].constant, cur_size[j],
2372 cur_flags[j], 1) != OP_LEGAL))
2373 as_fatal (_("Illegal operand (arg %d)"), j+1);
2374 }
2375
2376 /* First, copy the instruction's opcode. */
2377 output_opcode[0] = BIN (instruction->match, instruction->match_bits);
2378
2379 for (i = 0; i < insn->nargs; i++)
2380 {
2381 /* For BAL (ra),disp17 instruction only. And also set the
2382 DISP24a relocation type. */
2383 if (IS_INSN_MNEMONIC ("bal") && (instruction->size == 2) && i == 0)
2384 {
2385 insn->rtype = BFD_RELOC_CR16_DISP24a;
2386 continue;
2387 }
2388 cur_arg_num = i;
2389 print_operand (cur_size[i], instruction->operands[i].shift,
2390 &insn->arg[i]);
2391 }
2392 }
2393
2394 return 1;
2395 }
2396
2397 /* Print the instruction.
2398 Handle also cases where the instruction is relaxable/relocatable. */
2399
2400 static void
2401 print_insn (ins *insn)
2402 {
2403 unsigned int i, j, insn_size;
2404 char *this_frag;
2405 unsigned short words[4];
2406 int addr_mod;
2407
2408 /* Arrange the insn encodings in a WORD size array. */
2409 for (i = 0, j = 0; i < 2; i++)
2410 {
2411 words[j++] = (output_opcode[i] >> 16) & 0xFFFF;
2412 words[j++] = output_opcode[i] & 0xFFFF;
2413 }
2414
2415 /* Handle relocation. */
2416 if ((instruction->flags & RELAXABLE) && relocatable)
2417 {
2418 int relax_subtype;
2419 /* Write the maximal instruction size supported. */
2420 insn_size = INSN_MAX_SIZE;
2421
2422 if (IS_INSN_TYPE (BRANCH_INS))
2423 {
2424 switch (insn->rtype)
2425 {
2426 case BFD_RELOC_CR16_DISP24:
2427 relax_subtype = 2;
2428 break;
2429 case BFD_RELOC_CR16_DISP16:
2430 relax_subtype = 1;
2431 break;
2432 default:
2433 relax_subtype = 0;
2434 break;
2435 }
2436 }
2437 else
2438 abort ();
2439
2440 this_frag = frag_var (rs_machine_dependent, insn_size *2,
2441 4, relax_subtype,
2442 insn->exp.X_add_symbol,
2443 0,
2444 0);
2445 }
2446 else
2447 {
2448 insn_size = instruction->size;
2449 this_frag = frag_more (insn_size * 2);
2450
2451 if ((relocatable) && (insn->rtype != BFD_RELOC_NONE))
2452 {
2453 reloc_howto_type *reloc_howto;
2454 int size;
2455
2456 reloc_howto = bfd_reloc_type_lookup (stdoutput, insn->rtype);
2457
2458 if (!reloc_howto)
2459 abort ();
2460
2461 size = bfd_get_reloc_size (reloc_howto);
2462
2463 if (size < 1 || size > 4)
2464 abort ();
2465
2466 fix_new_exp (frag_now, this_frag - frag_now->fr_literal,
2467 size, &insn->exp, reloc_howto->pc_relative,
2468 insn->rtype);
2469 }
2470 }
2471
2472 /* Verify a 2-byte code alignment. */
2473 addr_mod = frag_now_fix () & 1;
2474 if (frag_now->has_code && frag_now->insn_addr != addr_mod)
2475 as_bad (_("instruction address is not a multiple of 2"));
2476 frag_now->insn_addr = addr_mod;
2477 frag_now->has_code = 1;
2478
2479 /* Write the instruction encoding to frag. */
2480 for (i = 0; i < insn_size; i++)
2481 {
2482 md_number_to_chars (this_frag, (valueT) words[i], 2);
2483 this_frag += 2;
2484 }
2485 }
2486
2487 /* Actually assemble an instruction. */
2488
2489 static void
2490 cr16_assemble (const char *op, char *param)
2491 {
2492 ins cr16_ins;
2493
2494 /* Find the instruction. */
2495 instruction = (const inst *) str_hash_find (cr16_inst_hash, op);
2496 if (instruction == NULL)
2497 {
2498 as_bad (_("Unknown opcode: `%s'"), op);
2499 return;
2500 }
2501
2502 /* Tie dwarf2 debug info to the address at the start of the insn. */
2503 dwarf2_emit_insn (0);
2504
2505 /* Parse the instruction's operands. */
2506 parse_insn (&cr16_ins, param);
2507
2508 /* Assemble the instruction - return upon failure. */
2509 if (assemble_insn (op, &cr16_ins) == 0)
2510 return;
2511
2512 /* Print the instruction. */
2513 print_insn (&cr16_ins);
2514 }
2515
2516 /* This is the guts of the machine-dependent assembler. OP points to a
2517 machine dependent instruction. This function is supposed to emit
2518 the frags/bytes it assembles to. */
2519
2520 void
2521 md_assemble (char *op)
2522 {
2523 ins cr16_ins;
2524 char *param, param1[32];
2525
2526 /* Reset global variables for a new instruction. */
2527 reset_vars (op);
2528
2529 /* Strip the mnemonic. */
2530 for (param = op; *param != 0 && !ISSPACE (*param); param++)
2531 ;
2532 *param++ = '\0';
2533
2534 /* bCC instructions and adjust the mnemonic by adding extra white spaces. */
2535 if (is_bcc_insn (op))
2536 {
2537 strcpy (param1, get_b_cc (op));
2538 strcat (param1,",");
2539 strcat (param1, param);
2540 param = (char *) &param1;
2541 cr16_assemble ("b", param);
2542 return;
2543 }
2544
2545 /* Checking the cinv options and adjust the mnemonic by removing the
2546 extra white spaces. */
2547 if (streq ("cinv", op))
2548 {
2549 /* Validate the cinv options. */
2550 unsigned int op_len, param_len;
2551 check_cinv_options (param);
2552 op_len = strlen (op);
2553 param_len = strlen (param) + 1;
2554 memmove (op + op_len, param, param_len);
2555 }
2556
2557 /* MAPPING - SHIFT INSN, if imm4/imm16 positive values
2558 lsh[b/w] imm4/imm6, reg ==> ashu[b/w] imm4/imm16, reg
2559 as CR16 core doesn't support lsh[b/w] right shift operations. */
2560 if ((streq ("lshb", op) || streq ("lshw", op) || streq ("lshd", op))
2561 && (param [0] == '$'))
2562 {
2563 strcpy (param1, param);
2564 /* Find the instruction. */
2565 instruction = (const inst *) str_hash_find (cr16_inst_hash, op);
2566 parse_operands (&cr16_ins, param1);
2567 if (((&cr16_ins)->arg[0].type == arg_ic)
2568 && ((&cr16_ins)->arg[0].constant >= 0))
2569 {
2570 if (streq ("lshb", op))
2571 cr16_assemble ("ashub", param);
2572 else if (streq ("lshd", op))
2573 cr16_assemble ("ashud", param);
2574 else
2575 cr16_assemble ("ashuw", param);
2576 return;
2577 }
2578 }
2579
2580 cr16_assemble (op, param);
2581 }
This page took 0.164076 seconds and 4 git commands to generate.