PR26481 UBSAN: tc-pj.c:281 index out of bounds
[deliverable/binutils-gdb.git] / gas / config / tc-crx.c
1 /* tc-crx.c -- Assembler code for the CRX CPU core.
2 Copyright (C) 2004-2020 Free Software Foundation, Inc.
3
4 Contributed by Tomer Levi, NSC, Israel.
5 Originally written for GAS 2.12 by Tomer Levi, NSC, Israel.
6 Updates, BFDizing, GNUifying and ELF support by Tomer Levi.
7
8 This file is part of GAS, the GNU Assembler.
9
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
14
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the
22 Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
23 MA 02110-1301, USA. */
24
25 #include "as.h"
26 #include "bfd_stdint.h"
27 #include "safe-ctype.h"
28 #include "dwarf2dbg.h"
29 #include "opcode/crx.h"
30 #include "elf/crx.h"
31
32 /* Word is considered here as a 16-bit unsigned short int. */
33 #define WORD_SHIFT 16
34
35 /* Register is 4-bit size. */
36 #define REG_SIZE 4
37
38 /* Maximum size of a single instruction (in words). */
39 #define INSN_MAX_SIZE 3
40
41 /* Maximum bits which may be set in a `mask16' operand. */
42 #define MAX_REGS_IN_MASK16 8
43
44 /* Utility macros for string comparison. */
45 #define streq(a, b) (strcmp (a, b) == 0)
46 #define strneq(a, b, c) (strncmp (a, b, c) == 0)
47
48 /* Assign a number NUM, shifted by SHIFT bytes, into a location
49 pointed by index BYTE of array 'output_opcode'. */
50 #define CRX_PRINT(BYTE, NUM, SHIFT) output_opcode[BYTE] |= (NUM << SHIFT)
51
52 /* Operand errors. */
53 typedef enum
54 {
55 OP_LEGAL = 0, /* Legal operand. */
56 OP_OUT_OF_RANGE, /* Operand not within permitted range. */
57 OP_NOT_EVEN, /* Operand is Odd number, should be even. */
58 OP_ILLEGAL_DISPU4, /* Operand is not within DISPU4 range. */
59 OP_ILLEGAL_CST4, /* Operand is not within CST4 range. */
60 OP_NOT_UPPER_64KB /* Operand is not within the upper 64KB
61 (0xFFFF0000-0xFFFFFFFF). */
62 }
63 op_err;
64
65 /* Opcode mnemonics hash table. */
66 static htab_t crx_inst_hash;
67 /* CRX registers hash table. */
68 static htab_t reg_hash;
69 /* CRX coprocessor registers hash table. */
70 static htab_t copreg_hash;
71 /* Current instruction we're assembling. */
72 static const inst *instruction;
73
74 /* Global variables. */
75
76 /* Array to hold an instruction encoding. */
77 static long output_opcode[2];
78
79 /* Nonzero means a relocatable symbol. */
80 static int relocatable;
81
82 /* A copy of the original instruction (used in error messages). */
83 static char ins_parse[MAX_INST_LEN];
84
85 /* The current processed argument number. */
86 static int cur_arg_num;
87
88 /* Generic assembler global variables which must be defined by all targets. */
89
90 /* Characters which always start a comment. */
91 const char comment_chars[] = "#";
92
93 /* Characters which start a comment at the beginning of a line. */
94 const char line_comment_chars[] = "#";
95
96 /* This array holds machine specific line separator characters. */
97 const char line_separator_chars[] = ";";
98
99 /* Chars that can be used to separate mant from exp in floating point nums. */
100 const char EXP_CHARS[] = "eE";
101
102 /* Chars that mean this number is a floating point constant as in 0f12.456 */
103 const char FLT_CHARS[] = "f'";
104
105 /* Target-specific multicharacter options, not const-declared at usage. */
106 const char *md_shortopts = "";
107 struct option md_longopts[] =
108 {
109 {NULL, no_argument, NULL, 0}
110 };
111 size_t md_longopts_size = sizeof (md_longopts);
112
113 /* This table describes all the machine specific pseudo-ops
114 the assembler has to support. The fields are:
115 *** Pseudo-op name without dot.
116 *** Function to call to execute this pseudo-op.
117 *** Integer arg to pass to the function. */
118
119 const pseudo_typeS md_pseudo_table[] =
120 {
121 /* In CRX machine, align is in bytes (not a ptwo boundary). */
122 {"align", s_align_bytes, 0},
123 {0, 0, 0}
124 };
125
126 /* CRX relaxation table. */
127 const relax_typeS md_relax_table[] =
128 {
129 /* bCC */
130 {0xfa, -0x100, 2, 1}, /* 8 */
131 {0xfffe, -0x10000, 4, 2}, /* 16 */
132 {0xfffffffe, -0xfffffffe, 6, 0}, /* 32 */
133
134 /* bal */
135 {0xfffe, -0x10000, 4, 4}, /* 16 */
136 {0xfffffffe, -0xfffffffe, 6, 0}, /* 32 */
137
138 /* cmpbr/bcop */
139 {0xfe, -0x100, 4, 6}, /* 8 */
140 {0xfffffe, -0x1000000, 6, 0} /* 24 */
141 };
142
143 static void reset_vars (char *);
144 static reg get_register (char *);
145 static copreg get_copregister (char *);
146 static argtype get_optype (operand_type);
147 static int get_opbits (operand_type);
148 static int get_opflags (operand_type);
149 static int get_number_of_operands (void);
150 static void parse_operand (char *, ins *);
151 static int gettrap (const char *);
152 static void handle_LoadStor (const char *);
153 static int get_cinv_parameters (const char *);
154 static long getconstant (long, int);
155 static op_err check_range (long *, int, unsigned int, int);
156 static int getreg_image (int);
157 static void parse_operands (ins *, char *);
158 static void parse_insn (ins *, char *);
159 static void print_operand (int, int, argument *);
160 static void print_constant (int, int, argument *);
161 static int exponent2scale (int);
162 static void mask_reg (int, unsigned short *);
163 static void process_label_constant (char *, ins *);
164 static void set_operand (char *, ins *);
165 static char * preprocess_reglist (char *, int *);
166 static int assemble_insn (char *, ins *);
167 static void print_insn (ins *);
168 static void warn_if_needed (ins *);
169 static int adjust_if_needed (ins *);
170
171 /* Return the bit size for a given operand. */
172
173 static int
174 get_opbits (operand_type op)
175 {
176 if (op < MAX_OPRD)
177 return crx_optab[op].bit_size;
178 else
179 return 0;
180 }
181
182 /* Return the argument type of a given operand. */
183
184 static argtype
185 get_optype (operand_type op)
186 {
187 if (op < MAX_OPRD)
188 return crx_optab[op].arg_type;
189 else
190 return nullargs;
191 }
192
193 /* Return the flags of a given operand. */
194
195 static int
196 get_opflags (operand_type op)
197 {
198 if (op < MAX_OPRD)
199 return crx_optab[op].flags;
200 else
201 return 0;
202 }
203
204 /* Get the core processor register 'reg_name'. */
205
206 static reg
207 get_register (char *reg_name)
208 {
209 const reg_entry *rreg;
210
211 rreg = (const reg_entry *) str_hash_find (reg_hash, reg_name);
212
213 if (rreg != NULL)
214 return rreg->value.reg_val;
215 else
216 return nullregister;
217 }
218
219 /* Get the coprocessor register 'copreg_name'. */
220
221 static copreg
222 get_copregister (char *copreg_name)
223 {
224 const reg_entry *coreg;
225
226 coreg = (const reg_entry *) str_hash_find (copreg_hash, copreg_name);
227
228 if (coreg != NULL)
229 return coreg->value.copreg_val;
230 else
231 return nullcopregister;
232 }
233
234 /* Round up a section size to the appropriate boundary. */
235
236 valueT
237 md_section_align (segT seg, valueT val)
238 {
239 /* Round .text section to a multiple of 2. */
240 if (seg == text_section)
241 return (val + 1) & ~1;
242 return val;
243 }
244
245 /* Parse an operand that is machine-specific (remove '*'). */
246
247 void
248 md_operand (expressionS * exp)
249 {
250 char c = *input_line_pointer;
251
252 switch (c)
253 {
254 case '*':
255 input_line_pointer++;
256 expression (exp);
257 break;
258 default:
259 break;
260 }
261 }
262
263 /* Reset global variables before parsing a new instruction. */
264
265 static void
266 reset_vars (char *op)
267 {
268 cur_arg_num = relocatable = 0;
269 memset (& output_opcode, '\0', sizeof (output_opcode));
270
271 /* Save a copy of the original OP (used in error messages). */
272 strncpy (ins_parse, op, sizeof ins_parse - 1);
273 ins_parse [sizeof ins_parse - 1] = 0;
274 }
275
276 /* This macro decides whether a particular reloc is an entry in a
277 switch table. It is used when relaxing, because the linker needs
278 to know about all such entries so that it can adjust them if
279 necessary. */
280
281 #define SWITCH_TABLE(fix) \
282 ( (fix)->fx_addsy != NULL \
283 && (fix)->fx_subsy != NULL \
284 && S_GET_SEGMENT ((fix)->fx_addsy) == \
285 S_GET_SEGMENT ((fix)->fx_subsy) \
286 && S_GET_SEGMENT (fix->fx_addsy) != undefined_section \
287 && ( (fix)->fx_r_type == BFD_RELOC_CRX_NUM8 \
288 || (fix)->fx_r_type == BFD_RELOC_CRX_NUM16 \
289 || (fix)->fx_r_type == BFD_RELOC_CRX_NUM32))
290
291 /* See whether we need to force a relocation into the output file.
292 This is used to force out switch and PC relative relocations when
293 relaxing. */
294
295 int
296 crx_force_relocation (fixS *fix)
297 {
298 if (generic_force_reloc (fix) || SWITCH_TABLE (fix))
299 return 1;
300
301 return 0;
302 }
303
304 /* Generate a relocation entry for a fixup. */
305
306 arelent *
307 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS * fixP)
308 {
309 arelent * reloc;
310
311 reloc = XNEW (arelent);
312 reloc->sym_ptr_ptr = XNEW (asymbol *);
313 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
314 reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
315 reloc->addend = fixP->fx_offset;
316
317 if (fixP->fx_subsy != NULL)
318 {
319 if (SWITCH_TABLE (fixP))
320 {
321 /* Keep the current difference in the addend. */
322 reloc->addend = (S_GET_VALUE (fixP->fx_addsy)
323 - S_GET_VALUE (fixP->fx_subsy) + fixP->fx_offset);
324
325 switch (fixP->fx_r_type)
326 {
327 case BFD_RELOC_CRX_NUM8:
328 fixP->fx_r_type = BFD_RELOC_CRX_SWITCH8;
329 break;
330 case BFD_RELOC_CRX_NUM16:
331 fixP->fx_r_type = BFD_RELOC_CRX_SWITCH16;
332 break;
333 case BFD_RELOC_CRX_NUM32:
334 fixP->fx_r_type = BFD_RELOC_CRX_SWITCH32;
335 break;
336 default:
337 abort ();
338 break;
339 }
340 }
341 else
342 {
343 /* We only resolve difference expressions in the same section. */
344 as_bad_where (fixP->fx_file, fixP->fx_line,
345 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
346 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
347 segment_name (fixP->fx_addsy
348 ? S_GET_SEGMENT (fixP->fx_addsy)
349 : absolute_section),
350 S_GET_NAME (fixP->fx_subsy),
351 segment_name (S_GET_SEGMENT (fixP->fx_addsy)));
352 }
353 }
354
355 gas_assert ((int) fixP->fx_r_type > 0);
356 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
357
358 if (reloc->howto == (reloc_howto_type *) NULL)
359 {
360 as_bad_where (fixP->fx_file, fixP->fx_line,
361 _("internal error: reloc %d (`%s') not supported by object file format"),
362 fixP->fx_r_type,
363 bfd_get_reloc_code_name (fixP->fx_r_type));
364 return NULL;
365 }
366 gas_assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
367
368 return reloc;
369 }
370
371 /* Prepare machine-dependent frags for relaxation. */
372
373 int
374 md_estimate_size_before_relax (fragS *fragp, asection *seg)
375 {
376 /* If symbol is undefined or located in a different section,
377 select the largest supported relocation. */
378 relax_substateT subtype;
379 relax_substateT rlx_state[] = {0, 2,
380 3, 4,
381 5, 6};
382
383 for (subtype = 0; subtype < ARRAY_SIZE (rlx_state); subtype += 2)
384 {
385 if (fragp->fr_subtype == rlx_state[subtype]
386 && (!S_IS_DEFINED (fragp->fr_symbol)
387 || seg != S_GET_SEGMENT (fragp->fr_symbol)))
388 {
389 fragp->fr_subtype = rlx_state[subtype + 1];
390 break;
391 }
392 }
393
394 if (fragp->fr_subtype >= ARRAY_SIZE (md_relax_table))
395 abort ();
396
397 return md_relax_table[fragp->fr_subtype].rlx_length;
398 }
399
400 void
401 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, fragS *fragP)
402 {
403 /* 'opcode' points to the start of the instruction, whether
404 we need to change the instruction's fixed encoding. */
405 char *opcode = &fragP->fr_literal[0] + fragP->fr_fix;
406 bfd_reloc_code_real_type reloc;
407
408 subseg_change (sec, 0);
409
410 switch (fragP->fr_subtype)
411 {
412 case 0:
413 reloc = BFD_RELOC_CRX_REL8;
414 break;
415 case 1:
416 *opcode = 0x7e;
417 reloc = BFD_RELOC_CRX_REL16;
418 break;
419 case 2:
420 *opcode = 0x7f;
421 reloc = BFD_RELOC_CRX_REL32;
422 break;
423 case 3:
424 reloc = BFD_RELOC_CRX_REL16;
425 break;
426 case 4:
427 *++opcode = 0x31;
428 reloc = BFD_RELOC_CRX_REL32;
429 break;
430 case 5:
431 reloc = BFD_RELOC_CRX_REL8_CMP;
432 break;
433 case 6:
434 *++opcode = 0x31;
435 reloc = BFD_RELOC_CRX_REL24;
436 break;
437 default:
438 abort ();
439 break;
440 }
441
442 fix_new (fragP, fragP->fr_fix,
443 bfd_get_reloc_size (bfd_reloc_type_lookup (stdoutput, reloc)),
444 fragP->fr_symbol, fragP->fr_offset, 1, reloc);
445 fragP->fr_var = 0;
446 fragP->fr_fix += md_relax_table[fragP->fr_subtype].rlx_length;
447 }
448
449 /* Process machine-dependent command line options. Called once for
450 each option on the command line that the machine-independent part of
451 GAS does not understand. */
452
453 int
454 md_parse_option (int c ATTRIBUTE_UNUSED, const char *arg ATTRIBUTE_UNUSED)
455 {
456 return 0;
457 }
458
459 /* Machine-dependent usage-output. */
460
461 void
462 md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
463 {
464 return;
465 }
466
467 const char *
468 md_atof (int type, char *litP, int *sizeP)
469 {
470 return ieee_md_atof (type, litP, sizeP, target_big_endian);
471 }
472
473 /* Apply a fixS (fixup of an instruction or data that we didn't have
474 enough info to complete immediately) to the data in a frag.
475 Since linkrelax is nonzero and TC_LINKRELAX_FIXUP is defined to disable
476 relaxation of debug sections, this function is called only when
477 fixuping relocations of debug sections. */
478
479 void
480 md_apply_fix (fixS *fixP, valueT *valP, segT seg)
481 {
482 valueT val = * valP;
483 char *buf = fixP->fx_frag->fr_literal + fixP->fx_where;
484 fixP->fx_offset = 0;
485
486 switch (fixP->fx_r_type)
487 {
488 case BFD_RELOC_CRX_NUM8:
489 bfd_put_8 (stdoutput, (unsigned char) val, buf);
490 break;
491 case BFD_RELOC_CRX_NUM16:
492 bfd_put_16 (stdoutput, val, buf);
493 break;
494 case BFD_RELOC_CRX_NUM32:
495 bfd_put_32 (stdoutput, val, buf);
496 break;
497 default:
498 /* We shouldn't ever get here because linkrelax is nonzero. */
499 abort ();
500 break;
501 }
502
503 fixP->fx_done = 0;
504
505 if (fixP->fx_addsy == NULL
506 && fixP->fx_pcrel == 0)
507 fixP->fx_done = 1;
508
509 if (fixP->fx_pcrel == 1
510 && fixP->fx_addsy != NULL
511 && S_GET_SEGMENT (fixP->fx_addsy) == seg)
512 fixP->fx_done = 1;
513 }
514
515 /* The location from which a PC relative jump should be calculated,
516 given a PC relative reloc. */
517
518 long
519 md_pcrel_from (fixS *fixp)
520 {
521 return fixp->fx_frag->fr_address + fixp->fx_where;
522 }
523
524 /* This function is called once, at assembler startup time. This should
525 set up all the tables, etc that the MD part of the assembler needs. */
526
527 void
528 md_begin (void)
529 {
530 int i = 0;
531
532 /* Set up a hash table for the instructions. */
533 crx_inst_hash = str_htab_create ();
534
535 while (crx_instruction[i].mnemonic != NULL)
536 {
537 const char *mnemonic = crx_instruction[i].mnemonic;
538
539 if (str_hash_insert (crx_inst_hash, mnemonic, &crx_instruction[i], 0))
540 as_fatal (_("duplicate %s"), mnemonic);
541
542 /* Insert unique names into hash table. The CRX instruction set
543 has many identical opcode names that have different opcodes based
544 on the operands. This hash table then provides a quick index to
545 the first opcode with a particular name in the opcode table. */
546 do
547 {
548 ++i;
549 }
550 while (crx_instruction[i].mnemonic != NULL
551 && streq (crx_instruction[i].mnemonic, mnemonic));
552 }
553
554 /* Initialize reg_hash hash table. */
555 reg_hash = str_htab_create ();
556 {
557 const reg_entry *regtab;
558
559 for (regtab = crx_regtab;
560 regtab < (crx_regtab + NUMREGS); regtab++)
561 if (str_hash_insert (reg_hash, regtab->name, regtab, 0) != NULL)
562 as_fatal (_("duplicate %s"), regtab->name);
563 }
564
565 /* Initialize copreg_hash hash table. */
566 copreg_hash = str_htab_create ();
567 {
568 const reg_entry *copregtab;
569
570 for (copregtab = crx_copregtab; copregtab < (crx_copregtab + NUMCOPREGS);
571 copregtab++)
572 if (str_hash_insert (copreg_hash, copregtab->name, copregtab, 0) != NULL)
573 as_fatal (_("duplicate %s"), copregtab->name);
574 }
575 /* Set linkrelax here to avoid fixups in most sections. */
576 linkrelax = 1;
577 }
578
579 /* Process constants (immediate/absolute)
580 and labels (jump targets/Memory locations). */
581
582 static void
583 process_label_constant (char *str, ins * crx_ins)
584 {
585 char *saved_input_line_pointer;
586 argument *cur_arg = &crx_ins->arg[cur_arg_num]; /* Current argument. */
587
588 saved_input_line_pointer = input_line_pointer;
589 input_line_pointer = str;
590
591 expression (&crx_ins->exp);
592
593 switch (crx_ins->exp.X_op)
594 {
595 case O_big:
596 case O_absent:
597 /* Missing or bad expr becomes absolute 0. */
598 as_bad (_("missing or invalid displacement expression `%s' taken as 0"),
599 str);
600 crx_ins->exp.X_op = O_constant;
601 crx_ins->exp.X_add_number = 0;
602 crx_ins->exp.X_add_symbol = (symbolS *) 0;
603 crx_ins->exp.X_op_symbol = (symbolS *) 0;
604 /* Fall through. */
605
606 case O_constant:
607 cur_arg->X_op = O_constant;
608 cur_arg->constant = crx_ins->exp.X_add_number;
609 break;
610
611 case O_symbol:
612 case O_subtract:
613 case O_add:
614 cur_arg->X_op = O_symbol;
615 crx_ins->rtype = BFD_RELOC_NONE;
616 relocatable = 1;
617
618 switch (cur_arg->type)
619 {
620 case arg_cr:
621 if (IS_INSN_TYPE (LD_STOR_INS_INC))
622 crx_ins->rtype = BFD_RELOC_CRX_REGREL12;
623 else if (IS_INSN_TYPE (CSTBIT_INS)
624 || IS_INSN_TYPE (STOR_IMM_INS))
625 crx_ins->rtype = BFD_RELOC_CRX_REGREL28;
626 else
627 crx_ins->rtype = BFD_RELOC_CRX_REGREL32;
628 break;
629
630 case arg_idxr:
631 crx_ins->rtype = BFD_RELOC_CRX_REGREL22;
632 break;
633
634 case arg_c:
635 if (IS_INSN_MNEMONIC ("bal") || IS_INSN_TYPE (DCR_BRANCH_INS))
636 crx_ins->rtype = BFD_RELOC_CRX_REL16;
637 else if (IS_INSN_TYPE (BRANCH_INS))
638 crx_ins->rtype = BFD_RELOC_CRX_REL8;
639 else if (IS_INSN_TYPE (LD_STOR_INS) || IS_INSN_TYPE (STOR_IMM_INS)
640 || IS_INSN_TYPE (CSTBIT_INS))
641 crx_ins->rtype = BFD_RELOC_CRX_ABS32;
642 else if (IS_INSN_TYPE (BRANCH_NEQ_INS))
643 crx_ins->rtype = BFD_RELOC_CRX_REL4;
644 else if (IS_INSN_TYPE (CMPBR_INS) || IS_INSN_TYPE (COP_BRANCH_INS))
645 crx_ins->rtype = BFD_RELOC_CRX_REL8_CMP;
646 break;
647
648 case arg_ic:
649 if (IS_INSN_TYPE (ARITH_INS))
650 crx_ins->rtype = BFD_RELOC_CRX_IMM32;
651 else if (IS_INSN_TYPE (ARITH_BYTE_INS))
652 crx_ins->rtype = BFD_RELOC_CRX_IMM16;
653 break;
654 default:
655 break;
656 }
657 break;
658
659 default:
660 cur_arg->X_op = crx_ins->exp.X_op;
661 break;
662 }
663
664 input_line_pointer = saved_input_line_pointer;
665 return;
666 }
667
668 /* Get the values of the scale to be encoded -
669 used for the scaled index mode of addressing. */
670
671 static int
672 exponent2scale (int val)
673 {
674 int exponent;
675
676 /* If 'val' is 0, the following 'for' will be an endless loop. */
677 if (val == 0)
678 return 0;
679
680 for (exponent = 0; (val != 1); val >>= 1, exponent++)
681 ;
682
683 return exponent;
684 }
685
686 /* Parsing different types of operands
687 -> constants Immediate/Absolute/Relative numbers
688 -> Labels Relocatable symbols
689 -> (rbase) Register base
690 -> disp(rbase) Register relative
691 -> disp(rbase)+ Post-increment mode
692 -> disp(rbase,ridx,scl) Register index mode */
693
694 static void
695 set_operand (char *operand, ins * crx_ins)
696 {
697 char *operandS; /* Pointer to start of sub-operand. */
698 char *operandE; /* Pointer to end of sub-operand. */
699 expressionS scale;
700 int scale_val;
701 char *input_save, c;
702 argument *cur_arg = &crx_ins->arg[cur_arg_num]; /* Current argument. */
703
704 /* Initialize pointers. */
705 operandS = operandE = operand;
706
707 switch (cur_arg->type)
708 {
709 case arg_sc: /* Case *+0x18. */
710 case arg_ic: /* Case $0x18. */
711 operandS++;
712 /* Fall through. */
713 case arg_c: /* Case 0x18. */
714 /* Set constant. */
715 process_label_constant (operandS, crx_ins);
716
717 if (cur_arg->type != arg_ic)
718 cur_arg->type = arg_c;
719 break;
720
721 case arg_icr: /* Case $0x18(r1). */
722 operandS++;
723 case arg_cr: /* Case 0x18(r1). */
724 /* Set displacement constant. */
725 while (*operandE != '(')
726 operandE++;
727 *operandE = '\0';
728 process_label_constant (operandS, crx_ins);
729 operandS = operandE;
730 /* Fall through. */
731 case arg_rbase: /* Case (r1). */
732 operandS++;
733 /* Set register base. */
734 while (*operandE != ')')
735 operandE++;
736 *operandE = '\0';
737 if ((cur_arg->r = get_register (operandS)) == nullregister)
738 as_bad (_("Illegal register `%s' in instruction `%s'"),
739 operandS, ins_parse);
740
741 if (cur_arg->type != arg_rbase)
742 cur_arg->type = arg_cr;
743 break;
744
745 case arg_idxr:
746 /* Set displacement constant. */
747 while (*operandE != '(')
748 operandE++;
749 *operandE = '\0';
750 process_label_constant (operandS, crx_ins);
751 operandS = ++operandE;
752
753 /* Set register base. */
754 while ((*operandE != ',') && (! ISSPACE (*operandE)))
755 operandE++;
756 *operandE++ = '\0';
757 if ((cur_arg->r = get_register (operandS)) == nullregister)
758 as_bad (_("Illegal register `%s' in instruction `%s'"),
759 operandS, ins_parse);
760
761 /* Skip leading white space. */
762 while (ISSPACE (*operandE))
763 operandE++;
764 operandS = operandE;
765
766 /* Set register index. */
767 while ((*operandE != ')') && (*operandE != ','))
768 operandE++;
769 c = *operandE;
770 *operandE++ = '\0';
771
772 if ((cur_arg->i_r = get_register (operandS)) == nullregister)
773 as_bad (_("Illegal register `%s' in instruction `%s'"),
774 operandS, ins_parse);
775
776 /* Skip leading white space. */
777 while (ISSPACE (*operandE))
778 operandE++;
779 operandS = operandE;
780
781 /* Set the scale. */
782 if (c == ')')
783 cur_arg->scale = 0;
784 else
785 {
786 while (*operandE != ')')
787 operandE++;
788 *operandE = '\0';
789
790 /* Preprocess the scale string. */
791 input_save = input_line_pointer;
792 input_line_pointer = operandS;
793 expression (&scale);
794 input_line_pointer = input_save;
795
796 scale_val = scale.X_add_number;
797
798 /* Check if the scale value is legal. */
799 if (scale_val != 1 && scale_val != 2
800 && scale_val != 4 && scale_val != 8)
801 as_bad (_("Illegal Scale - `%d'"), scale_val);
802
803 cur_arg->scale = exponent2scale (scale_val);
804 }
805 break;
806
807 default:
808 break;
809 }
810 }
811
812 /* Parse a single operand.
813 operand - Current operand to parse.
814 crx_ins - Current assembled instruction. */
815
816 static void
817 parse_operand (char *operand, ins * crx_ins)
818 {
819 int ret_val;
820 argument *cur_arg = &crx_ins->arg[cur_arg_num]; /* Current argument. */
821
822 /* Initialize the type to NULL before parsing. */
823 cur_arg->type = nullargs;
824
825 /* Check whether this is a general processor register. */
826 if ((ret_val = get_register (operand)) != nullregister)
827 {
828 cur_arg->type = arg_r;
829 cur_arg->r = ret_val;
830 cur_arg->X_op = O_register;
831 return;
832 }
833
834 /* Check whether this is a core [special] coprocessor register. */
835 if ((ret_val = get_copregister (operand)) != nullcopregister)
836 {
837 cur_arg->type = arg_copr;
838 if (ret_val >= cs0)
839 cur_arg->type = arg_copsr;
840 cur_arg->cr = ret_val;
841 cur_arg->X_op = O_register;
842 return;
843 }
844
845 /* Deal with special characters. */
846 switch (operand[0])
847 {
848 case '$':
849 if (strchr (operand, '(') != NULL)
850 cur_arg->type = arg_icr;
851 else
852 cur_arg->type = arg_ic;
853 goto set_params;
854 break;
855
856 case '*':
857 cur_arg->type = arg_sc;
858 goto set_params;
859 break;
860
861 case '(':
862 cur_arg->type = arg_rbase;
863 goto set_params;
864 break;
865
866 default:
867 break;
868 }
869
870 if (strchr (operand, '(') != NULL)
871 {
872 if (strchr (operand, ',') != NULL
873 && (strchr (operand, ',') > strchr (operand, '(')))
874 cur_arg->type = arg_idxr;
875 else
876 cur_arg->type = arg_cr;
877 }
878 else
879 cur_arg->type = arg_c;
880 goto set_params;
881
882 /* Parse an operand according to its type. */
883 set_params:
884 cur_arg->constant = 0;
885 set_operand (operand, crx_ins);
886 }
887
888 /* Parse the various operands. Each operand is then analyzed to fillup
889 the fields in the crx_ins data structure. */
890
891 static void
892 parse_operands (ins * crx_ins, char *operands)
893 {
894 char *operandS; /* Operands string. */
895 char *operandH, *operandT; /* Single operand head/tail pointers. */
896 int allocated = 0; /* Indicates a new operands string was allocated. */
897 char *operand[MAX_OPERANDS]; /* Separating the operands. */
898 int op_num = 0; /* Current operand number we are parsing. */
899 int bracket_flag = 0; /* Indicates a bracket '(' was found. */
900 int sq_bracket_flag = 0; /* Indicates a square bracket '[' was found. */
901
902 /* Preprocess the list of registers, if necessary. */
903 operandS = operandH = operandT = (INST_HAS_REG_LIST) ?
904 preprocess_reglist (operands, &allocated) : operands;
905
906 while (*operandT != '\0')
907 {
908 if (*operandT == ',' && bracket_flag != 1 && sq_bracket_flag != 1)
909 {
910 *operandT++ = '\0';
911 operand[op_num++] = strdup (operandH);
912 operandH = operandT;
913 continue;
914 }
915
916 if (*operandT == ' ')
917 as_bad (_("Illegal operands (whitespace): `%s'"), ins_parse);
918
919 if (*operandT == '(')
920 bracket_flag = 1;
921 else if (*operandT == '[')
922 sq_bracket_flag = 1;
923
924 if (*operandT == ')')
925 {
926 if (bracket_flag)
927 bracket_flag = 0;
928 else
929 as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
930 }
931 else if (*operandT == ']')
932 {
933 if (sq_bracket_flag)
934 sq_bracket_flag = 0;
935 else
936 as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
937 }
938
939 if (bracket_flag == 1 && *operandT == ')')
940 bracket_flag = 0;
941 else if (sq_bracket_flag == 1 && *operandT == ']')
942 sq_bracket_flag = 0;
943
944 operandT++;
945 }
946
947 /* Adding the last operand. */
948 operand[op_num++] = strdup (operandH);
949 crx_ins->nargs = op_num;
950
951 /* Verifying correct syntax of operands (all brackets should be closed). */
952 if (bracket_flag || sq_bracket_flag)
953 as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
954
955 /* Now we parse each operand separately. */
956 for (op_num = 0; op_num < crx_ins->nargs; op_num++)
957 {
958 cur_arg_num = op_num;
959 parse_operand (operand[op_num], crx_ins);
960 free (operand[op_num]);
961 }
962
963 if (allocated)
964 free (operandS);
965 }
966
967 /* Get the trap index in dispatch table, given its name.
968 This routine is used by assembling the 'excp' instruction. */
969
970 static int
971 gettrap (const char *s)
972 {
973 const trap_entry *trap;
974
975 for (trap = crx_traps; trap < (crx_traps + NUMTRAPS); trap++)
976 if (strcasecmp (trap->name, s) == 0)
977 return trap->entry;
978
979 as_bad (_("Unknown exception: `%s'"), s);
980 return 0;
981 }
982
983 /* Post-Increment instructions, as well as Store-Immediate instructions, are a
984 sub-group within load/stor instruction groups.
985 Therefore, when parsing a Post-Increment/Store-Immediate insn, we have to
986 advance the instruction pointer to the start of that sub-group (that is, up
987 to the first instruction of that type).
988 Otherwise, the insn will be mistakenly identified as of type LD_STOR_INS. */
989
990 static void
991 handle_LoadStor (const char *operands)
992 {
993 /* Post-Increment instructions precede Store-Immediate instructions in
994 CRX instruction table, hence they are handled before.
995 This synchronization should be kept. */
996
997 /* Assuming Post-Increment insn has the following format :
998 'MNEMONIC DISP(REG)+, REG' (e.g. 'loadw 12(r5)+, r6').
999 LD_STOR_INS_INC are the only store insns containing a plus sign (+). */
1000 if (strstr (operands, ")+") != NULL)
1001 {
1002 while (! IS_INSN_TYPE (LD_STOR_INS_INC))
1003 instruction++;
1004 return;
1005 }
1006
1007 /* Assuming Store-Immediate insn has the following format :
1008 'MNEMONIC $DISP, ...' (e.g. 'storb $1, 12(r5)').
1009 STOR_IMM_INS are the only store insns containing a dollar sign ($). */
1010 if (strstr (operands, "$") != NULL)
1011 while (! IS_INSN_TYPE (STOR_IMM_INS))
1012 instruction++;
1013 }
1014
1015 /* Top level module where instruction parsing starts.
1016 crx_ins - data structure holds some information.
1017 operands - holds the operands part of the whole instruction. */
1018
1019 static void
1020 parse_insn (ins *insn, char *operands)
1021 {
1022 int i;
1023
1024 /* Handle instructions with no operands. */
1025 for (i = 0; crx_no_op_insn[i] != NULL; i++)
1026 {
1027 if (streq (crx_no_op_insn[i], instruction->mnemonic))
1028 {
1029 insn->nargs = 0;
1030 return;
1031 }
1032 }
1033
1034 /* Handle 'excp'/'cinv' instructions. */
1035 if (IS_INSN_MNEMONIC ("excp") || IS_INSN_MNEMONIC ("cinv"))
1036 {
1037 insn->nargs = 1;
1038 insn->arg[0].type = arg_ic;
1039 insn->arg[0].constant = IS_INSN_MNEMONIC ("excp") ?
1040 gettrap (operands) : get_cinv_parameters (operands);
1041 insn->arg[0].X_op = O_constant;
1042 return;
1043 }
1044
1045 /* Handle load/stor unique instructions before parsing. */
1046 if (IS_INSN_TYPE (LD_STOR_INS))
1047 handle_LoadStor (operands);
1048
1049 if (operands != NULL)
1050 parse_operands (insn, operands);
1051 }
1052
1053 /* Cinv instruction requires special handling. */
1054
1055 static int
1056 get_cinv_parameters (const char *operand)
1057 {
1058 const char *p = operand;
1059 int d_used = 0, i_used = 0, u_used = 0, b_used = 0;
1060
1061 while (*++p != ']')
1062 {
1063 if (*p == ',' || *p == ' ')
1064 continue;
1065
1066 if (*p == 'd')
1067 d_used = 1;
1068 else if (*p == 'i')
1069 i_used = 1;
1070 else if (*p == 'u')
1071 u_used = 1;
1072 else if (*p == 'b')
1073 b_used = 1;
1074 else
1075 as_bad (_("Illegal `cinv' parameter: `%c'"), *p);
1076 }
1077
1078 return ((b_used ? 8 : 0)
1079 + (d_used ? 4 : 0)
1080 + (i_used ? 2 : 0)
1081 + (u_used ? 1 : 0));
1082 }
1083
1084 /* Retrieve the opcode image of a given register.
1085 If the register is illegal for the current instruction,
1086 issue an error. */
1087
1088 static int
1089 getreg_image (int r)
1090 {
1091 const reg_entry *rreg;
1092 char *reg_name;
1093 int is_procreg = 0; /* Nonzero means argument should be processor reg. */
1094
1095 if (((IS_INSN_MNEMONIC ("mtpr")) && (cur_arg_num == 1))
1096 || ((IS_INSN_MNEMONIC ("mfpr")) && (cur_arg_num == 0)) )
1097 is_procreg = 1;
1098
1099 /* Check whether the register is in registers table. */
1100 if (r < MAX_REG)
1101 rreg = &crx_regtab[r];
1102 /* Check whether the register is in coprocessor registers table. */
1103 else if (r < (int) MAX_COPREG)
1104 rreg = &crx_copregtab[r-MAX_REG];
1105 /* Register not found. */
1106 else
1107 {
1108 as_bad (_("Unknown register: `%d'"), r);
1109 return 0;
1110 }
1111
1112 reg_name = rreg->name;
1113
1114 /* Issue a error message when register is illegal. */
1115 #define IMAGE_ERR \
1116 as_bad (_("Illegal register (`%s') in instruction: `%s'"), \
1117 reg_name, ins_parse);
1118
1119 switch (rreg->type)
1120 {
1121 case CRX_U_REGTYPE:
1122 if (is_procreg || (instruction->flags & USER_REG))
1123 return rreg->image;
1124 else
1125 IMAGE_ERR;
1126 break;
1127
1128 case CRX_CFG_REGTYPE:
1129 if (is_procreg)
1130 return rreg->image;
1131 else
1132 IMAGE_ERR;
1133 break;
1134
1135 case CRX_R_REGTYPE:
1136 if (! is_procreg)
1137 return rreg->image;
1138 else
1139 IMAGE_ERR;
1140 break;
1141
1142 case CRX_C_REGTYPE:
1143 case CRX_CS_REGTYPE:
1144 return rreg->image;
1145 break;
1146
1147 default:
1148 IMAGE_ERR;
1149 break;
1150 }
1151
1152 return 0;
1153 }
1154
1155 /* Routine used to represent integer X using NBITS bits. */
1156
1157 static long
1158 getconstant (long x, int nbits)
1159 {
1160 return x & ((((1U << (nbits - 1)) - 1) << 1) | 1);
1161 }
1162
1163 /* Print a constant value to 'output_opcode':
1164 ARG holds the operand's type and value.
1165 SHIFT represents the location of the operand to be print into.
1166 NBITS determines the size (in bits) of the constant. */
1167
1168 static void
1169 print_constant (int nbits, int shift, argument *arg)
1170 {
1171 unsigned long mask = 0;
1172
1173 long constant = getconstant (arg->constant, nbits);
1174
1175 switch (nbits)
1176 {
1177 case 32:
1178 case 28:
1179 case 24:
1180 case 22:
1181 /* mask the upper part of the constant, that is, the bits
1182 going to the lowest byte of output_opcode[0].
1183 The upper part of output_opcode[1] is always filled,
1184 therefore it is always masked with 0xFFFF. */
1185 mask = (1 << (nbits - 16)) - 1;
1186 /* Divide the constant between two consecutive words :
1187 0 1 2 3
1188 +---------+---------+---------+---------+
1189 | | X X X X | X X X X | |
1190 +---------+---------+---------+---------+
1191 output_opcode[0] output_opcode[1] */
1192
1193 CRX_PRINT (0, (constant >> WORD_SHIFT) & mask, 0);
1194 CRX_PRINT (1, (constant & 0xFFFF), WORD_SHIFT);
1195 break;
1196
1197 case 16:
1198 case 12:
1199 /* Special case - in arg_cr, the SHIFT represents the location
1200 of the REGISTER, not the constant, which is itself not shifted. */
1201 if (arg->type == arg_cr)
1202 {
1203 CRX_PRINT (0, constant, 0);
1204 break;
1205 }
1206
1207 /* When instruction size is 3 and 'shift' is 16, a 16-bit constant is
1208 always filling the upper part of output_opcode[1]. If we mistakenly
1209 write it to output_opcode[0], the constant prefix (that is, 'match')
1210 will be overridden.
1211 0 1 2 3
1212 +---------+---------+---------+---------+
1213 | 'match' | | X X X X | |
1214 +---------+---------+---------+---------+
1215 output_opcode[0] output_opcode[1] */
1216
1217 if ((instruction->size > 2) && (shift == WORD_SHIFT))
1218 CRX_PRINT (1, constant, WORD_SHIFT);
1219 else
1220 CRX_PRINT (0, constant, shift);
1221 break;
1222
1223 default:
1224 CRX_PRINT (0, constant, shift);
1225 break;
1226 }
1227 }
1228
1229 /* Print an operand to 'output_opcode', which later on will be
1230 printed to the object file:
1231 ARG holds the operand's type, size and value.
1232 SHIFT represents the printing location of operand.
1233 NBITS determines the size (in bits) of a constant operand. */
1234
1235 static void
1236 print_operand (int nbits, int shift, argument *arg)
1237 {
1238 switch (arg->type)
1239 {
1240 case arg_r:
1241 CRX_PRINT (0, getreg_image (arg->r), shift);
1242 break;
1243
1244 case arg_copr:
1245 if (arg->cr < c0 || arg->cr > c15)
1246 as_bad (_("Illegal co-processor register in instruction `%s'"),
1247 ins_parse);
1248 CRX_PRINT (0, getreg_image (arg->cr), shift);
1249 break;
1250
1251 case arg_copsr:
1252 if (arg->cr < cs0 || arg->cr > cs15)
1253 as_bad (_("Illegal co-processor special register in instruction `%s'"),
1254 ins_parse);
1255 CRX_PRINT (0, getreg_image (arg->cr), shift);
1256 break;
1257
1258 case arg_idxr:
1259 /* 16 12 8 6 0
1260 +--------------------------------+
1261 | r_base | r_idx | scl| disp |
1262 +--------------------------------+ */
1263 CRX_PRINT (0, getreg_image (arg->r), 12);
1264 CRX_PRINT (0, getreg_image (arg->i_r), 8);
1265 CRX_PRINT (0, arg->scale, 6);
1266 /* Fall through. */
1267 case arg_ic:
1268 case arg_c:
1269 print_constant (nbits, shift, arg);
1270 break;
1271
1272 case arg_rbase:
1273 CRX_PRINT (0, getreg_image (arg->r), shift);
1274 break;
1275
1276 case arg_cr:
1277 /* case base_cst4. */
1278 if (instruction->flags & DISPU4MAP)
1279 print_constant (nbits, shift + REG_SIZE, arg);
1280 else
1281 /* rbase_disps<NN> and other such cases. */
1282 print_constant (nbits, shift, arg);
1283 /* Add the register argument to the output_opcode. */
1284 CRX_PRINT (0, getreg_image (arg->r), shift);
1285 break;
1286
1287 default:
1288 break;
1289 }
1290 }
1291
1292 /* Retrieve the number of operands for the current assembled instruction. */
1293
1294 static int
1295 get_number_of_operands (void)
1296 {
1297 int i;
1298
1299 for (i = 0; instruction->operands[i].op_type && i < MAX_OPERANDS; i++)
1300 ;
1301 return i;
1302 }
1303
1304 /* Verify that the number NUM can be represented in BITS bits (that is,
1305 within its permitted range), based on the instruction's FLAGS.
1306 If UPDATE is nonzero, update the value of NUM if necessary.
1307 Return OP_LEGAL upon success, actual error type upon failure. */
1308
1309 static op_err
1310 check_range (long *num, int bits, int unsigned flags, int update)
1311 {
1312 uint32_t max;
1313 op_err retval = OP_LEGAL;
1314 int bin;
1315 uint32_t upper_64kb = 0xffff0000;
1316 uint32_t value = *num;
1317
1318 /* Verify operand value is even. */
1319 if (flags & OP_EVEN)
1320 {
1321 if (value % 2)
1322 return OP_NOT_EVEN;
1323 }
1324
1325 if (flags & OP_UPPER_64KB)
1326 {
1327 /* Check if value is to be mapped to upper 64 KB memory area. */
1328 if ((value & upper_64kb) == upper_64kb)
1329 {
1330 value -= upper_64kb;
1331 if (update)
1332 *num = value;
1333 }
1334 else
1335 return OP_NOT_UPPER_64KB;
1336 }
1337
1338 if (flags & OP_SHIFT)
1339 {
1340 /* All OP_SHIFT args are also OP_SIGNED, so we want to keep the
1341 sign. However, right shift of a signed type with a negative
1342 value is implementation defined. See ISO C 6.5.7. So we use
1343 an unsigned type and sign extend afterwards. */
1344 value >>= 1;
1345 value = (value ^ 0x40000000) - 0x40000000;
1346 if (update)
1347 *num = value;
1348 }
1349 else if (flags & OP_SHIFT_DEC)
1350 {
1351 value = (value >> 1) - 1;
1352 if (update)
1353 *num = value;
1354 }
1355
1356 if (flags & OP_ESC)
1357 {
1358 /* 0x7e and 0x7f are reserved escape sequences of dispe9. */
1359 if (value == 0x7e || value == 0x7f)
1360 return OP_OUT_OF_RANGE;
1361 }
1362
1363 if (flags & OP_DISPU4)
1364 {
1365 int is_dispu4 = 0;
1366
1367 uint32_t mul = (instruction->flags & DISPUB4 ? 1
1368 : instruction->flags & DISPUW4 ? 2
1369 : instruction->flags & DISPUD4 ? 4
1370 : 0);
1371
1372 for (bin = 0; bin < crx_cst4_maps; bin++)
1373 {
1374 if (value == mul * bin)
1375 {
1376 is_dispu4 = 1;
1377 if (update)
1378 *num = bin;
1379 break;
1380 }
1381 }
1382 if (!is_dispu4)
1383 retval = OP_ILLEGAL_DISPU4;
1384 }
1385 else if (flags & OP_CST4)
1386 {
1387 int is_cst4 = 0;
1388
1389 for (bin = 0; bin < crx_cst4_maps; bin++)
1390 {
1391 if (value == (uint32_t) crx_cst4_map[bin])
1392 {
1393 is_cst4 = 1;
1394 if (update)
1395 *num = bin;
1396 break;
1397 }
1398 }
1399 if (!is_cst4)
1400 retval = OP_ILLEGAL_CST4;
1401 }
1402 else if (flags & OP_SIGNED)
1403 {
1404 max = 1;
1405 max = max << (bits - 1);
1406 value += max;
1407 max = ((max - 1) << 1) | 1;
1408 if (value > max)
1409 retval = OP_OUT_OF_RANGE;
1410 }
1411 else if (flags & OP_UNSIGNED)
1412 {
1413 max = 1;
1414 max = max << (bits - 1);
1415 max = ((max - 1) << 1) | 1;
1416 if (value > max)
1417 retval = OP_OUT_OF_RANGE;
1418 }
1419 return retval;
1420 }
1421
1422 /* Assemble a single instruction:
1423 INSN is already parsed (that is, all operand values and types are set).
1424 For instruction to be assembled, we need to find an appropriate template in
1425 the instruction table, meeting the following conditions:
1426 1: Has the same number of operands.
1427 2: Has the same operand types.
1428 3: Each operand size is sufficient to represent the instruction's values.
1429 Returns 1 upon success, 0 upon failure. */
1430
1431 static int
1432 assemble_insn (char *mnemonic, ins *insn)
1433 {
1434 /* Type of each operand in the current template. */
1435 argtype cur_type[MAX_OPERANDS];
1436 /* Size (in bits) of each operand in the current template. */
1437 unsigned int cur_size[MAX_OPERANDS];
1438 /* Flags of each operand in the current template. */
1439 unsigned int cur_flags[MAX_OPERANDS];
1440 /* Instruction type to match. */
1441 unsigned int ins_type;
1442 /* Boolean flag to mark whether a match was found. */
1443 int match = 0;
1444 int i;
1445 /* Nonzero if an instruction with same number of operands was found. */
1446 int found_same_number_of_operands = 0;
1447 /* Nonzero if an instruction with same argument types was found. */
1448 int found_same_argument_types = 0;
1449 /* Nonzero if a constant was found within the required range. */
1450 int found_const_within_range = 0;
1451 /* Argument number of an operand with invalid type. */
1452 int invalid_optype = -1;
1453 /* Argument number of an operand with invalid constant value. */
1454 int invalid_const = -1;
1455 /* Operand error (used for issuing various constant error messages). */
1456 op_err op_error, const_err = OP_LEGAL;
1457
1458 /* Retrieve data (based on FUNC) for each operand of a given instruction. */
1459 #define GET_CURRENT_DATA(FUNC, ARRAY) \
1460 for (i = 0; i < insn->nargs; i++) \
1461 ARRAY[i] = FUNC (instruction->operands[i].op_type)
1462
1463 #define GET_CURRENT_TYPE GET_CURRENT_DATA(get_optype, cur_type)
1464 #define GET_CURRENT_SIZE GET_CURRENT_DATA(get_opbits, cur_size)
1465 #define GET_CURRENT_FLAGS GET_CURRENT_DATA(get_opflags, cur_flags)
1466
1467 /* Instruction has no operands -> only copy the constant opcode. */
1468 if (insn->nargs == 0)
1469 {
1470 output_opcode[0] = BIN (instruction->match, instruction->match_bits);
1471 return 1;
1472 }
1473
1474 /* In some case, same mnemonic can appear with different instruction types.
1475 For example, 'storb' is supported with 3 different types :
1476 LD_STOR_INS, LD_STOR_INS_INC, STOR_IMM_INS.
1477 We assume that when reaching this point, the instruction type was
1478 pre-determined. We need to make sure that the type stays the same
1479 during a search for matching instruction. */
1480 ins_type = CRX_INS_TYPE(instruction->flags);
1481
1482 while (/* Check that match is still not found. */
1483 match != 1
1484 /* Check we didn't get to end of table. */
1485 && instruction->mnemonic != NULL
1486 /* Check that the actual mnemonic is still available. */
1487 && IS_INSN_MNEMONIC (mnemonic)
1488 /* Check that the instruction type wasn't changed. */
1489 && IS_INSN_TYPE(ins_type))
1490 {
1491 /* Check whether number of arguments is legal. */
1492 if (get_number_of_operands () != insn->nargs)
1493 goto next_insn;
1494 found_same_number_of_operands = 1;
1495
1496 /* Initialize arrays with data of each operand in current template. */
1497 GET_CURRENT_TYPE;
1498 GET_CURRENT_SIZE;
1499 GET_CURRENT_FLAGS;
1500
1501 /* Check for type compatibility. */
1502 for (i = 0; i < insn->nargs; i++)
1503 {
1504 if (cur_type[i] != insn->arg[i].type)
1505 {
1506 if (invalid_optype == -1)
1507 invalid_optype = i + 1;
1508 goto next_insn;
1509 }
1510 }
1511 found_same_argument_types = 1;
1512
1513 for (i = 0; i < insn->nargs; i++)
1514 {
1515 /* Reverse the operand indices for certain opcodes:
1516 Index 0 -->> 1
1517 Index 1 -->> 0
1518 Other index -->> stays the same. */
1519 int j = instruction->flags & REVERSE_MATCH ?
1520 i == 0 ? 1 :
1521 i == 1 ? 0 : i :
1522 i;
1523
1524 /* Only check range - don't update the constant's value, since the
1525 current instruction may not be the last we try to match.
1526 The constant's value will be updated later, right before printing
1527 it to the object file. */
1528 if ((insn->arg[j].X_op == O_constant)
1529 && (op_error = check_range (&insn->arg[j].constant, cur_size[j],
1530 cur_flags[j], 0)))
1531 {
1532 if (invalid_const == -1)
1533 {
1534 invalid_const = j + 1;
1535 const_err = op_error;
1536 }
1537 goto next_insn;
1538 }
1539 /* For symbols, we make sure the relocation size (which was already
1540 determined) is sufficient. */
1541 else if ((insn->arg[j].X_op == O_symbol)
1542 && ((bfd_reloc_type_lookup (stdoutput, insn->rtype))->bitsize
1543 > cur_size[j]))
1544 goto next_insn;
1545 }
1546 found_const_within_range = 1;
1547
1548 /* If we got till here -> Full match is found. */
1549 match = 1;
1550 break;
1551
1552 /* Try again with next instruction. */
1553 next_insn:
1554 instruction++;
1555 }
1556
1557 if (!match)
1558 {
1559 /* We haven't found a match - instruction can't be assembled. */
1560 if (!found_same_number_of_operands)
1561 as_bad (_("Incorrect number of operands"));
1562 else if (!found_same_argument_types)
1563 as_bad (_("Illegal type of operand (arg %d)"), invalid_optype);
1564 else if (!found_const_within_range)
1565 {
1566 switch (const_err)
1567 {
1568 case OP_OUT_OF_RANGE:
1569 as_bad (_("Operand out of range (arg %d)"), invalid_const);
1570 break;
1571 case OP_NOT_EVEN:
1572 as_bad (_("Operand has odd displacement (arg %d)"), invalid_const);
1573 break;
1574 case OP_ILLEGAL_DISPU4:
1575 as_bad (_("Invalid DISPU4 operand value (arg %d)"), invalid_const);
1576 break;
1577 case OP_ILLEGAL_CST4:
1578 as_bad (_("Invalid CST4 operand value (arg %d)"), invalid_const);
1579 break;
1580 case OP_NOT_UPPER_64KB:
1581 as_bad (_("Operand value is not within upper 64 KB (arg %d)"),
1582 invalid_const);
1583 break;
1584 default:
1585 as_bad (_("Illegal operand (arg %d)"), invalid_const);
1586 break;
1587 }
1588 }
1589
1590 return 0;
1591 }
1592 else
1593 /* Full match - print the encoding to output file. */
1594 {
1595 /* Make further checking (such that couldn't be made earlier).
1596 Warn the user if necessary. */
1597 warn_if_needed (insn);
1598
1599 /* Check whether we need to adjust the instruction pointer. */
1600 if (adjust_if_needed (insn))
1601 /* If instruction pointer was adjusted, we need to update
1602 the size of the current template operands. */
1603 GET_CURRENT_SIZE;
1604
1605 for (i = 0; i < insn->nargs; i++)
1606 {
1607 int j = instruction->flags & REVERSE_MATCH ?
1608 i == 0 ? 1 :
1609 i == 1 ? 0 : i :
1610 i;
1611
1612 /* This time, update constant value before printing it. */
1613 if ((insn->arg[j].X_op == O_constant)
1614 && (check_range (&insn->arg[j].constant, cur_size[j],
1615 cur_flags[j], 1) != OP_LEGAL))
1616 as_fatal (_("Illegal operand (arg %d)"), j+1);
1617 }
1618
1619 /* First, copy the instruction's opcode. */
1620 output_opcode[0] = BIN (instruction->match, instruction->match_bits);
1621
1622 for (i = 0; i < insn->nargs; i++)
1623 {
1624 cur_arg_num = i;
1625 print_operand (cur_size[i], instruction->operands[i].shift,
1626 &insn->arg[i]);
1627 }
1628 }
1629
1630 return 1;
1631 }
1632
1633 /* Bunch of error checking.
1634 The checks are made after a matching instruction was found. */
1635
1636 void
1637 warn_if_needed (ins *insn)
1638 {
1639 /* If the post-increment address mode is used and the load/store
1640 source register is the same as rbase, the result of the
1641 instruction is undefined. */
1642 if (IS_INSN_TYPE (LD_STOR_INS_INC))
1643 {
1644 /* Enough to verify that one of the arguments is a simple reg. */
1645 if ((insn->arg[0].type == arg_r) || (insn->arg[1].type == arg_r))
1646 if (insn->arg[0].r == insn->arg[1].r)
1647 as_bad (_("Same src/dest register is used (`r%d'), result is undefined"),
1648 insn->arg[0].r);
1649 }
1650
1651 /* Some instruction assume the stack pointer as rptr operand.
1652 Issue an error when the register to be loaded is also SP. */
1653 if (instruction->flags & NO_SP)
1654 {
1655 if (getreg_image (insn->arg[0].r) == getreg_image (sp))
1656 as_bad (_("`%s' has undefined result"), ins_parse);
1657 }
1658
1659 /* If the rptr register is specified as one of the registers to be loaded,
1660 the final contents of rptr are undefined. Thus, we issue an error. */
1661 if (instruction->flags & NO_RPTR)
1662 {
1663 if ((1 << getreg_image (insn->arg[0].r)) & insn->arg[1].constant)
1664 as_bad (_("Same src/dest register is used (`r%d'), result is undefined"),
1665 getreg_image (insn->arg[0].r));
1666 }
1667 }
1668
1669 /* In some cases, we need to adjust the instruction pointer although a
1670 match was already found. Here, we gather all these cases.
1671 Returns 1 if instruction pointer was adjusted, otherwise 0. */
1672
1673 int
1674 adjust_if_needed (ins *insn)
1675 {
1676 int ret_value = 0;
1677
1678 /* Special check for 'addub $0, r0' instruction -
1679 The opcode '0000 0000 0000 0000' is not allowed. */
1680 if (IS_INSN_MNEMONIC ("addub"))
1681 {
1682 if ((instruction->operands[0].op_type == cst4)
1683 && instruction->operands[1].op_type == regr)
1684 {
1685 if (insn->arg[0].constant == 0 && insn->arg[1].r == r0)
1686 {
1687 instruction++;
1688 ret_value = 1;
1689 }
1690 }
1691 }
1692
1693 /* Optimization: Omit a zero displacement in bit operations,
1694 saving 2-byte encoding space (e.g., 'cbitw $8, 0(r1)'). */
1695 if (IS_INSN_TYPE (CSTBIT_INS))
1696 {
1697 if ((instruction->operands[1].op_type == rbase_disps12)
1698 && (insn->arg[1].X_op == O_constant)
1699 && (insn->arg[1].constant == 0))
1700 {
1701 instruction--;
1702 ret_value = 1;
1703 }
1704 }
1705
1706 return ret_value;
1707 }
1708
1709 /* Set the appropriate bit for register 'r' in 'mask'.
1710 This indicates that this register is loaded or stored by
1711 the instruction. */
1712
1713 static void
1714 mask_reg (int r, unsigned short int *mask)
1715 {
1716 if ((reg)r > (reg)sp)
1717 {
1718 as_bad (_("Invalid register in register list"));
1719 return;
1720 }
1721
1722 *mask |= (1 << r);
1723 }
1724
1725 /* Preprocess register list - create a 16-bit mask with one bit for each
1726 of the 16 general purpose registers. If a bit is set, it indicates
1727 that this register is loaded or stored by the instruction. */
1728
1729 static char *
1730 preprocess_reglist (char *param, int *allocated)
1731 {
1732 char reg_name[MAX_REGNAME_LEN]; /* Current parsed register name. */
1733 char *regP; /* Pointer to 'reg_name' string. */
1734 int reg_counter = 0; /* Count number of parsed registers. */
1735 unsigned short int mask = 0; /* Mask for 16 general purpose registers. */
1736 char *new_param; /* New created operands string. */
1737 char *paramP = param; /* Pointer to original operands string. */
1738 char maskstring[10]; /* Array to print the mask as a string. */
1739 int hi_found = 0, lo_found = 0; /* Boolean flags for hi/lo registers. */
1740 reg r;
1741 copreg cr;
1742
1743 /* If 'param' is already in form of a number, no need to preprocess. */
1744 if (strchr (paramP, '{') == NULL)
1745 return param;
1746
1747 /* Verifying correct syntax of operand. */
1748 if (strchr (paramP, '}') == NULL)
1749 as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
1750
1751 while (*paramP++ != '{');
1752
1753 new_param = XCNEWVEC (char, MAX_INST_LEN);
1754 *allocated = 1;
1755 strncpy (new_param, param, paramP - param - 1);
1756
1757 while (*paramP != '}')
1758 {
1759 regP = paramP;
1760 memset (&reg_name, '\0', sizeof (reg_name));
1761
1762 while (ISALNUM (*paramP))
1763 paramP++;
1764
1765 strncpy (reg_name, regP, paramP - regP);
1766
1767 /* Coprocessor register c<N>. */
1768 if (IS_INSN_TYPE (COP_REG_INS))
1769 {
1770 if (((cr = get_copregister (reg_name)) == nullcopregister)
1771 || (crx_copregtab[cr-MAX_REG].type != CRX_C_REGTYPE))
1772 as_fatal (_("Illegal register `%s' in cop-register list"), reg_name);
1773 mask_reg (getreg_image (cr - c0), &mask);
1774 }
1775 /* Coprocessor Special register cs<N>. */
1776 else if (IS_INSN_TYPE (COPS_REG_INS))
1777 {
1778 if (((cr = get_copregister (reg_name)) == nullcopregister)
1779 || (crx_copregtab[cr-MAX_REG].type != CRX_CS_REGTYPE))
1780 as_fatal (_("Illegal register `%s' in cop-special-register list"),
1781 reg_name);
1782 mask_reg (getreg_image (cr - cs0), &mask);
1783 }
1784 /* User register u<N>. */
1785 else if (instruction->flags & USER_REG)
1786 {
1787 if (streq(reg_name, "uhi"))
1788 {
1789 hi_found = 1;
1790 goto next_inst;
1791 }
1792 else if (streq(reg_name, "ulo"))
1793 {
1794 lo_found = 1;
1795 goto next_inst;
1796 }
1797 else if (((r = get_register (reg_name)) == nullregister)
1798 || (crx_regtab[r].type != CRX_U_REGTYPE))
1799 as_fatal (_("Illegal register `%s' in user register list"), reg_name);
1800
1801 mask_reg (getreg_image (r - u0), &mask);
1802 }
1803 /* General purpose register r<N>. */
1804 else
1805 {
1806 if (streq(reg_name, "hi"))
1807 {
1808 hi_found = 1;
1809 goto next_inst;
1810 }
1811 else if (streq(reg_name, "lo"))
1812 {
1813 lo_found = 1;
1814 goto next_inst;
1815 }
1816 else if (((r = get_register (reg_name)) == nullregister)
1817 || (crx_regtab[r].type != CRX_R_REGTYPE))
1818 as_fatal (_("Illegal register `%s' in register list"), reg_name);
1819
1820 mask_reg (getreg_image (r - r0), &mask);
1821 }
1822
1823 if (++reg_counter > MAX_REGS_IN_MASK16)
1824 as_bad (_("Maximum %d bits may be set in `mask16' operand"),
1825 MAX_REGS_IN_MASK16);
1826
1827 next_inst:
1828 while (!ISALNUM (*paramP) && *paramP != '}')
1829 paramP++;
1830 }
1831
1832 if (*++paramP != '\0')
1833 as_warn (_("rest of line ignored; first ignored character is `%c'"),
1834 *paramP);
1835
1836 switch (hi_found + lo_found)
1837 {
1838 case 0:
1839 /* At least one register should be specified. */
1840 if (mask == 0)
1841 as_bad (_("Illegal `mask16' operand, operation is undefined - `%s'"),
1842 ins_parse);
1843 break;
1844
1845 case 1:
1846 /* HI can't be specified without LO (and vise-versa). */
1847 as_bad (_("HI/LO registers should be specified together"));
1848 break;
1849
1850 case 2:
1851 /* HI/LO registers mustn't be masked with additional registers. */
1852 if (mask != 0)
1853 as_bad (_("HI/LO registers should be specified without additional registers"));
1854
1855 default:
1856 break;
1857 }
1858
1859 sprintf (maskstring, "$0x%x", mask);
1860 strcat (new_param, maskstring);
1861 return new_param;
1862 }
1863
1864 /* Print the instruction.
1865 Handle also cases where the instruction is relaxable/relocatable. */
1866
1867 void
1868 print_insn (ins *insn)
1869 {
1870 unsigned int i, j, insn_size;
1871 char *this_frag;
1872 unsigned short words[4];
1873 int addr_mod;
1874
1875 /* Arrange the insn encodings in a WORD size array. */
1876 for (i = 0, j = 0; i < 2; i++)
1877 {
1878 words[j++] = (output_opcode[i] >> 16) & 0xFFFF;
1879 words[j++] = output_opcode[i] & 0xFFFF;
1880 }
1881
1882 /* Handle relaxation. */
1883 if ((instruction->flags & RELAXABLE) && relocatable)
1884 {
1885 int relax_subtype;
1886
1887 /* Write the maximal instruction size supported. */
1888 insn_size = INSN_MAX_SIZE;
1889
1890 /* bCC */
1891 if (IS_INSN_TYPE (BRANCH_INS))
1892 relax_subtype = 0;
1893 /* bal */
1894 else if (IS_INSN_TYPE (DCR_BRANCH_INS) || IS_INSN_MNEMONIC ("bal"))
1895 relax_subtype = 3;
1896 /* cmpbr/bcop */
1897 else if (IS_INSN_TYPE (CMPBR_INS) || IS_INSN_TYPE (COP_BRANCH_INS))
1898 relax_subtype = 5;
1899 else
1900 abort ();
1901
1902 this_frag = frag_var (rs_machine_dependent, insn_size * 2,
1903 4, relax_subtype,
1904 insn->exp.X_add_symbol,
1905 insn->exp.X_add_number,
1906 0);
1907 }
1908 else
1909 {
1910 insn_size = instruction->size;
1911 this_frag = frag_more (insn_size * 2);
1912
1913 /* Handle relocation. */
1914 if ((relocatable) && (insn->rtype != BFD_RELOC_NONE))
1915 {
1916 reloc_howto_type *reloc_howto;
1917 int size;
1918
1919 reloc_howto = bfd_reloc_type_lookup (stdoutput, insn->rtype);
1920
1921 if (!reloc_howto)
1922 abort ();
1923
1924 size = bfd_get_reloc_size (reloc_howto);
1925
1926 if (size < 1 || size > 4)
1927 abort ();
1928
1929 fix_new_exp (frag_now, this_frag - frag_now->fr_literal,
1930 size, &insn->exp, reloc_howto->pc_relative,
1931 insn->rtype);
1932 }
1933 }
1934
1935 /* Verify a 2-byte code alignment. */
1936 addr_mod = frag_now_fix () & 1;
1937 if (frag_now->has_code && frag_now->insn_addr != addr_mod)
1938 as_bad (_("instruction address is not a multiple of 2"));
1939 frag_now->insn_addr = addr_mod;
1940 frag_now->has_code = 1;
1941
1942 /* Write the instruction encoding to frag. */
1943 for (i = 0; i < insn_size; i++)
1944 {
1945 md_number_to_chars (this_frag, (valueT) words[i], 2);
1946 this_frag += 2;
1947 }
1948 }
1949
1950 /* This is the guts of the machine-dependent assembler. OP points to a
1951 machine dependent instruction. This function is supposed to emit
1952 the frags/bytes it assembles to. */
1953
1954 void
1955 md_assemble (char *op)
1956 {
1957 ins crx_ins;
1958 char *param;
1959 char c;
1960
1961 /* Reset global variables for a new instruction. */
1962 reset_vars (op);
1963
1964 /* Strip the mnemonic. */
1965 for (param = op; *param != 0 && !ISSPACE (*param); param++)
1966 ;
1967 c = *param;
1968 *param++ = '\0';
1969
1970 /* Find the instruction. */
1971 instruction = (const inst *) str_hash_find (crx_inst_hash, op);
1972 if (instruction == NULL)
1973 {
1974 as_bad (_("Unknown opcode: `%s'"), op);
1975 param[-1] = c;
1976 return;
1977 }
1978
1979 /* Tie dwarf2 debug info to the address at the start of the insn. */
1980 dwarf2_emit_insn (0);
1981
1982 /* Parse the instruction's operands. */
1983 parse_insn (&crx_ins, param);
1984
1985 /* Assemble the instruction - return upon failure. */
1986 if (assemble_insn (op, &crx_ins) == 0)
1987 {
1988 param[-1] = c;
1989 return;
1990 }
1991
1992 /* Print the instruction. */
1993 param[-1] = c;
1994 print_insn (&crx_ins);
1995 }
This page took 0.070663 seconds and 4 git commands to generate.