2000-08-04 Kazu Hirata <kazu@hxi.com>
[deliverable/binutils-gdb.git] / gas / config / tc-cris.c
1 /* tc-cris.c -- Assembler code for the CRIS CPU core.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3
4 Contributed by Axis Communications AB, Lund, Sweden.
5 Originally written for GAS 1.38.1 by Mikael Asker.
6 Updated, BFDized and GNUified by Hans-Peter Nilsson.
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 2, 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, 59 Temple Place - Suite 330, Boston,
23 MA 02111-1307, USA. */
24
25 #include <stdio.h>
26 #include <ctype.h>
27 #include "as.h"
28 #include "subsegs.h"
29 #include "opcode/cris.h"
30
31 /* Conventions used here:
32 Generally speaking, pointers to binutils types such as "fragS" and
33 "expressionS" get parameter and variable names ending in "P", such as
34 "fragP", to harmonize with the rest of the binutils code. Other
35 pointers get a "p" suffix, such as "bufp". Any function or type-name
36 that could clash with a current or future binutils or GAS function get
37 a "cris_" prefix. */
38
39 /* This might be CRIS_INSN_NONE if we're assembling a prefix-insn only.
40 Note that some prefix-insns might be assembled as CRIS_INSN_NORMAL. */
41 enum cris_insn_kind
42 {
43 CRIS_INSN_NORMAL, CRIS_INSN_NONE, CRIS_INSN_BRANCH
44 };
45
46 /* An instruction will have one of these prefixes.
47 Although the same bit-pattern, we handle BDAP with an immediate
48 expression (eventually quick or [pc+]) different from when we only have
49 register expressions. */
50 enum prefix_kind
51 {
52 PREFIX_NONE, PREFIX_BDAP_IMM, PREFIX_BDAP, PREFIX_BIAP, PREFIX_DIP,
53 PREFIX_PUSH
54 };
55
56 /* The prefix for an instruction. */
57 struct cris_prefix
58 {
59 enum prefix_kind kind;
60 int base_reg_number;
61 unsigned int opcode;
62
63 /* There might be an expression to be evaluated, like I in [rN+I]. */
64 expressionS expr;
65
66 /* If there's an expression, we might need a relocation. Here's the
67 type of what relocation to start relaxaton with.
68 The relocation is assumed to start immediately after the prefix insn,
69 so we don't provide an offset. */
70 enum bfd_reloc_code_real reloc;
71 };
72
73 /* The description of the instruction being assembled. */
74 struct cris_instruction
75 {
76 /* If CRIS_INSN_NONE, then this insn is of zero length. */
77 enum cris_insn_kind insn_type;
78
79 /* If a special register was mentioned, this is its description, else
80 it is NULL. */
81 const struct cris_spec_reg *spec_reg;
82
83 unsigned int opcode;
84
85 /* An insn may have at most one expression; theoretically there could be
86 another in its prefix (but I don't see how that could happen). */
87 expressionS expr;
88
89 /* The expression might need a relocation. Here's one to start
90 relaxation with. */
91 enum bfd_reloc_code_real reloc;
92
93 /* The size in bytes of an immediate expression, or zero in
94 nonapplicable. */
95 int imm_oprnd_size;
96 };
97
98 static void cris_process_instruction PARAMS ((char *,
99 struct cris_instruction *,
100 struct cris_prefix *));
101 static int get_bwd_size_modifier PARAMS ((char **, int *));
102 static int get_bw_size_modifier PARAMS ((char **, int *));
103 static int get_gen_reg PARAMS ((char **, int *));
104 static int get_spec_reg PARAMS ((char **,
105 const struct cris_spec_reg **));
106 static int get_autoinc_prefix_or_indir_op PARAMS ((char **,
107 struct cris_prefix *,
108 int *, int *, int *,
109 expressionS *));
110 static int get_3op_or_dip_prefix_op PARAMS ((char **,
111 struct cris_prefix *));
112 static int cris_get_expression PARAMS ((char **, expressionS *));
113 static int get_flags PARAMS ((char **, int *));
114 static void gen_bdap PARAMS ((int, expressionS *));
115 static int branch_disp PARAMS ((int));
116 static void gen_cond_branch_32 PARAMS ((char *, char *, fragS *,
117 symbolS *, symbolS *, long int));
118 static void cris_number_to_imm PARAMS ((char *, long, int, fixS *));
119 static void cris_create_short_jump PARAMS ((char *, addressT, addressT,
120 fragS *, symbolS *));
121 /* Handle to the opcode hash table. */
122 static struct hash_control *op_hash = NULL;
123
124 const pseudo_typeS md_pseudo_table[] =
125 {
126 {"dword", cons, 4},
127 {NULL, 0, 0}
128 };
129
130 static int warn_for_branch_expansion = 0;
131
132 const char cris_comment_chars[] = ";";
133
134 /* This array holds the chars that only start a comment at the beginning of
135 a line. If the line seems to have the form '# 123 filename'
136 .line and .file directives will appear in the pre-processed output */
137 /* Note that input_file.c hand-checks for '#' at the beginning of the
138 first line of the input file. This is because the compiler outputs
139 #NO_APP at the beginning of its output. */
140 /* Also note that slash-star will always start a comment */
141 const char line_comment_chars[] = "#";
142 const char line_separator_chars[] = "@";
143
144 /* Now all floating point support is shut off. See md_atof. */
145 const char EXP_CHARS[] = "";
146 const char FLT_CHARS[] = "";
147
148
149 /* For CRIS, we encode the relax_substateTs (in e.g. fr_substate) as:
150 2 1 0
151 ---/ /--+-----------------+-----------------+-----------------+
152 | what state ? | how long ? |
153 ---/ /--+-----------------+-----------------+-----------------+
154
155 The "how long" bits are 00 = byte, 01 = word, 10 = dword (long).
156 This is a Un*x convention.
157 Not all lengths are legit for a given value of (what state).
158
159 Groups for CRIS address relaxing:
160
161 1. Bcc
162 length: byte, word, 10-byte expansion
163
164 2. BDAP
165 length: byte, word, dword */
166
167 #define STATE_CONDITIONAL_BRANCH (1)
168 #define STATE_BASE_PLUS_DISP_PREFIX (2)
169
170 #define STATE_LENGTH_MASK (3)
171 #define STATE_BYTE (0)
172 #define STATE_WORD (1)
173 #define STATE_DWORD (2)
174 /* Symbol undefined. */
175 #define STATE_UNDF (3)
176 #define STATE_MAX_LENGTH (3)
177
178
179 /* These displacements are relative to the adress following the opcode
180 word of the instruction. The first letter is Byte, Word. The 2nd
181 letter is Forward, Backward. */
182
183 #define BRANCH_BF ( 254)
184 #define BRANCH_BB (-256)
185 #define BRANCH_WF (2+ 32767)
186 #define BRANCH_WB (2+-32768)
187
188 #define BDAP_BF ( 127)
189 #define BDAP_BB (-128)
190 #define BDAP_WF ( 32767)
191 #define BDAP_WB (-32768)
192
193 #define ENCODE_RELAX(what, length) (((what) << 2) + (length))
194
195 const relax_typeS md_cris_relax_table[] =
196 {
197 /* Error sentinel (0, 0). */
198 {1, 1, 0, 0},
199
200 /* Unused (0, 1). */
201 {1, 1, 0, 0},
202
203 /* Unused (0, 2). */
204 {1, 1, 0, 0},
205
206 /* Unused (0, 3). */
207 {1, 1, 0, 0},
208
209 /* Bcc o (1, 0). */
210 {BRANCH_BF, BRANCH_BB, 0, ENCODE_RELAX (1, 1)},
211
212 /* Bcc [PC+] (1, 1). */
213 {BRANCH_WF, BRANCH_WB, 2, ENCODE_RELAX (1, 2)},
214
215 /* BEXT/BWF, BA, JUMP (external), JUMP (always), Bnot_cc, JUMP (default)
216 (1, 2). */
217 {0, 0, 10, 0},
218
219 /* Unused (1, 3). */
220 {1, 1, 0, 0},
221
222 /* BDAP o (2, 0). */
223 {BDAP_BF, BDAP_BB, 0, ENCODE_RELAX (2, 1)},
224
225 /* BDAP.[bw] [PC+] (2, 1). */
226 {BDAP_WF, BDAP_WB, 2, ENCODE_RELAX (2, 2)},
227
228 /* BDAP.d [PC+] (2, 2). */
229 {0, 0, 4, 0}
230 };
231
232 #undef BRANCH_BF
233 #undef BRANCH_BB
234 #undef BRANCH_WF
235 #undef BRANCH_WB
236 #undef BDAP_BF
237 #undef BDAP_BB
238 #undef BDAP_WF
239 #undef BDAP_WB
240
241
242 /* Target-specific multicharacter options, not const-declared at usage
243 in 2.9.1 and CVS of 2000-02-16. */
244 struct option md_longopts[] =
245 {
246 {NULL, no_argument, NULL, 0}
247 };
248
249 /* Not const-declared at usage in 2.9.1. */
250 size_t md_longopts_size = sizeof (md_longopts);
251 const char *md_shortopts = "hHN";
252
253
254 /* At first glance, this may seems wrong and should be 4 (ba + nop); but
255 since a short_jump must skip a *number* of long jumps, it must also be
256 a long jump. Here, we hope to make it a "ba [16bit_offs]" and a "nop"
257 for the delay slot and hope that the jump table at most needs
258 32767/4=8191 long-jumps. A branch is better than a jump, since it is
259 relative; we will not have a reloc to fix up somewhere.
260
261 Note that we can't add relocs, because relaxation uses these fixed
262 numbers, and md_create_short_jump is called after relaxation. */
263
264 const int md_short_jump_size = 6;
265 const int md_long_jump_size = 6;
266
267
268 /* Report output format. */
269 const char *
270 cris_target_format ()
271 {
272 switch (OUTPUT_FLAVOR)
273 {
274 case bfd_target_aout_flavour:
275 return "a.out-cris";
276
277 case bfd_target_elf_flavour:
278 return "elf32-cris";
279
280 default:
281 abort ();
282 return NULL;
283 }
284 }
285
286 /* Prepare machine-dependent frags for relaxation.
287
288 Called just before relaxation starts. Any symbol that is now undefined
289 will not become defined.
290
291 Return the correct fr_subtype in the frag.
292
293 Return the initial "guess for fr_var" to caller. The guess for fr_var
294 is *actually* the growth beyond fr_fix. Whatever we do to grow fr_fix
295 or fr_var contributes to our returned value.
296
297 Although it may not be explicit in the frag, pretend
298 fr_var starts with a value. */
299
300 int
301 md_estimate_size_before_relax (fragP, segment_type)
302 fragS *fragP;
303 /* The segment is either N_DATA or N_TEXT. */
304 segT segment_type;
305 {
306 int old_fr_fix;
307
308 old_fr_fix = fragP->fr_fix;
309
310 switch (fragP->fr_subtype)
311 {
312 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_UNDF):
313 if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
314 {
315 /* The symbol lies in the same segment - a relaxable case. */
316 fragP->fr_subtype
317 = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE);
318 }
319 else
320 {
321 /* Unknown or not the same segment, so not relaxable. */
322 char *writep;
323
324 /* A small branch-always (2 bytes) to the "real" branch
325 instruction, plus a delay-slot nop (2 bytes), plus a
326 jump (2 plus 4 bytes). See gen_cond_branch_32. */
327 fragP->fr_fix += 2 + 2 + 2 + 4;
328 writep = fragP->fr_literal + old_fr_fix;
329 gen_cond_branch_32 (fragP->fr_opcode, writep, fragP,
330 fragP->fr_symbol, (symbolS *)NULL,
331 fragP->fr_offset);
332 frag_wane (fragP);
333 }
334 break;
335
336 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE):
337 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD):
338 /* We *might* give a better initial guess if we peek at offsets
339 now, but the caller will relax correctly and without this, so
340 don't bother. */
341 break;
342
343 case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_UNDF):
344 /* Note that we can not do anything sane with relaxing
345 [rX + a_known_symbol_in_text], it will have to be a 32-bit
346 value.
347
348 We could play tricks with managing a constant pool and make
349 a_known_symbol_in_text a "bdap [pc + offset]" pointing there, but
350 that's pointless, it can only be longer and slower.
351
352 Off-topic: If PIC becomes *really* important, and has to be done
353 in the assembler and linker only (which would be weird or
354 clueless), we can so something. Imagine:
355 move.x [r + 32_bit_symbol],r
356 move.x [32_bit_symbol],r
357 move.x 32_bit_symbol,r
358 can be shortened by a word (8-bit offset) if we are close to the
359 symbol or keep its length (16-bit offset) or be a word longer
360 (32-bit offset). Then change the 32_bit_symbol into a "bdap [pc
361 + offset]", and put the offset to the 32_bit_symbol in "offset".
362 Weird, to say the least, and we still have to add support for a
363 PC-relative relocation in the loader (shared libraries). But
364 it's an interesting thought. */
365
366 if (S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
367 {
368 /* Go for dword if not absolute or same segment. */
369 fragP->fr_subtype
370 = ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_DWORD);
371 fragP->fr_var += 4;
372 }
373 else
374 {
375 /* Absolute expression. */
376 long int value;
377 value = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
378
379 if (value >= -128 && value <= 127)
380 {
381 /* Byte displacement. */
382 (fragP->fr_opcode)[0] = value;
383 }
384 else
385 {
386 /* Word or dword displacement. */
387 int pow2_of_size = 1;
388 char *writep;
389
390 if (value < -32768 || value > 32767)
391 {
392 /* Outside word range, make it a dword. */
393 pow2_of_size = 2;
394 }
395
396 /* Modify the byte-offset BDAP into a word or dword offset
397 BDAP. Or really, a BDAP rX,8bit into a
398 BDAP.[wd] rX,[PC+] followed by a and a word or dword. */
399 (fragP->fr_opcode)[0] = BDAP_PC_LOW + pow2_of_size * 16;
400
401 /* Keep the register number in the highest four bits. */
402 (fragP->fr_opcode)[1] &= 0xF0;
403 (fragP->fr_opcode)[1] |= BDAP_INCR_HIGH;
404
405 /* It grew by two or four bytes. */
406 fragP->fr_fix += 1 << pow2_of_size;
407 writep = fragP->fr_literal + old_fr_fix;
408 md_number_to_chars (writep, value, 1 << pow2_of_size);
409 }
410 frag_wane (fragP);
411 }
412 break;
413
414 default:
415 BAD_CASE (fragP->fr_subtype);
416 }
417
418 return fragP->fr_var + (fragP->fr_fix - old_fr_fix);
419 }
420
421
422 /* Perform post-processing of machine-dependent frags after relaxation.
423 Called after relaxation is finished.
424 In: Address of frag.
425 fr_type == rs_machine_dependent.
426 fr_subtype is what the address relaxed to.
427
428 Out: Any fixS:s and constants are set up.
429
430 The caller will turn the frag into a ".space 0". */
431
432 void
433 md_convert_frag (abfd, sec, fragP)
434 bfd *abfd ATTRIBUTE_UNUSED;
435 segT sec ATTRIBUTE_UNUSED;
436 fragS *fragP;
437 {
438 /* Pointer to first byte in variable-sized part of the frag. */
439 char *var_partp;
440
441 /* Pointer to first opcode byte in frag. */
442 char *opcodep;
443
444 /* Used to check integrity of the relaxation.
445 One of 2 = long, 1 = word, or 0 = byte. */
446 int length_code;
447
448 /* Size in bytes of variable-sized part of frag. */
449 int var_part_size = 0;
450
451 /* This is part of *fragP. It contains all information about addresses
452 and offsets to varying parts. */
453 symbolS *symbolP;
454 unsigned long var_part_offset;
455
456 /* Where, in file space, is _var of *fragP? */
457 unsigned long address_of_var_part = 0;
458
459 /* Where, in file space, does addr point? */
460 unsigned long target_address;
461
462 know (fragP->fr_type == rs_machine_dependent);
463
464 length_code = fragP->fr_subtype & STATE_LENGTH_MASK;
465 know (length_code >= 0 && length_code < STATE_MAX_LENGTH);
466
467 var_part_offset = fragP->fr_fix;
468 var_partp = fragP->fr_literal + var_part_offset;
469 opcodep = fragP->fr_opcode;
470
471 symbolP = fragP->fr_symbol;
472 target_address
473 = (symbolP
474 ? S_GET_VALUE (symbolP) + symbol_get_frag(fragP->fr_symbol)->fr_address
475 : 0 ) + fragP->fr_offset;
476 address_of_var_part = fragP->fr_address + var_part_offset;
477
478 switch (fragP->fr_subtype)
479 {
480 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE):
481 opcodep[0] = branch_disp ((target_address - address_of_var_part));
482 var_part_size = 0;
483 break;
484
485 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD):
486 /* We had a quick immediate branch, now turn it into a word one i.e. a
487 PC autoincrement. */
488 opcodep[0] = BRANCH_PC_LOW;
489 opcodep[1] &= 0xF0;
490 opcodep[1] |= BRANCH_INCR_HIGH;
491 md_number_to_chars (var_partp,
492 (long) (target_address - (address_of_var_part + 2)),
493 2);
494 var_part_size = 2;
495 break;
496
497 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_DWORD):
498 gen_cond_branch_32 (fragP->fr_opcode, var_partp, fragP,
499 fragP->fr_symbol, (symbolS *)NULL,
500 fragP->fr_offset);
501 /* Ten bytes added: a branch, nop and a jump. */
502 var_part_size = 2 + 2 + 4 + 2;
503 break;
504
505 case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_BYTE):
506 var_partp[0] = target_address - (address_of_var_part + 1);
507 var_part_size = 0;
508 break;
509
510 case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_WORD):
511 /* We had a BDAP 8-bit "quick immediate", now turn it into a 16-bit
512 one that uses PC autoincrement. */
513 opcodep[0] = BDAP_PC_LOW + (1 << 4);
514 opcodep[1] &= 0xF0;
515 opcodep[1] |= BDAP_INCR_HIGH;
516 md_number_to_chars (var_partp, (long)(target_address), 2);
517 var_part_size = 2;
518 break;
519
520 case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_DWORD):
521 /* We had a BDAP 16-bit "word", change the offset to a dword. */
522 opcodep[0] = BDAP_PC_LOW + (2 << 4);
523 opcodep[1] &= 0xF0;
524 opcodep[1] |= BDAP_INCR_HIGH;
525 if (fragP->fr_symbol == NULL)
526 md_number_to_chars (var_partp, fragP->fr_offset, 4);
527 else
528 fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
529 fragP->fr_offset, 0, BFD_RELOC_32);
530 var_part_size = 4;
531 break;
532
533 default:
534 BAD_CASE (fragP->fr_subtype);
535 break;
536 }
537
538 fragP->fr_fix += var_part_size;
539
540 }
541
542 /* Generate a short jump around a secondary jump table.
543 Used by md_create_long_jump.
544
545 This used to be md_create_short_jump, but is now called from
546 md_create_long_jump instead, when sufficient.
547 since the sizes of the jumps are the same. It used to be brittle,
548 making possibilities for creating bad code. */
549
550 static void
551 cris_create_short_jump (storep, from_addr, to_addr, fragP, to_symbol)
552 char *storep;
553 addressT from_addr;
554 addressT to_addr;
555 fragS *fragP ATTRIBUTE_UNUSED;
556 symbolS *to_symbol ATTRIBUTE_UNUSED;
557 {
558 long int distance;
559
560 distance = to_addr - from_addr;
561
562 if (-254 <= distance && distance <= 256)
563 {
564 /* Create a "short" short jump: "BA distance - 2". */
565 storep[0] = branch_disp (distance-2);
566 storep[1] = BA_QUICK_HIGH;
567
568 /* A nop for the delay slot. */
569 md_number_to_chars (storep+2, NOP_OPCODE, 2);
570
571 /* The extra word should be filled with something sane too. Make it
572 a nop to keep disassembly sane. */
573 md_number_to_chars (storep+4, NOP_OPCODE, 2);
574 }
575 else
576 {
577 /* Make it a "long" short jump: "BA (PC+)". */
578 md_number_to_chars (storep, BA_PC_INCR_OPCODE, 2);
579
580 /* ".WORD distance - 4". */
581 md_number_to_chars (storep + 2, (long) (distance - 4), 2);
582
583 /* A nop for the delay slot. */
584 md_number_to_chars (storep+4, NOP_OPCODE, 2);
585 }
586 }
587
588
589 /* Generate a long jump in a secondary jump table.
590
591 storep Where to store the jump instruction.
592 from_addr Address of the jump instruction.
593 to_addr Destination address of the jump.
594 fragP Which frag the destination address operand
595 lies in.
596 to_symbol Destination symbol. */
597
598 void
599 md_create_long_jump (storep, from_addr, to_addr, fragP, to_symbol)
600 char *storep;
601 addressT from_addr;
602 addressT to_addr;
603 fragS *fragP;
604 symbolS *to_symbol;
605 {
606 long int distance;
607
608 distance = to_addr - from_addr;
609
610 if (-32763 <= distance && distance <= 32772)
611 {
612 /* Then make it a "short" long jump. */
613 cris_create_short_jump (storep, from_addr, to_addr, fragP,
614 to_symbol);
615 }
616 else
617 {
618 /* We have a "long" long jump: "JUMP (PC+)". */
619 md_number_to_chars (storep, JUMP_PC_INCR_OPCODE, 2);
620
621 /* Follow with a ".DWORD to_addr". */
622 fix_new (fragP, storep + 2 - fragP->fr_literal, 4, to_symbol,
623 0, 0, BFD_RELOC_32);
624 }
625 }
626
627
628 /* Port-specific assembler initialization. */
629 void
630 md_begin ()
631 {
632 const char *hashret = NULL;
633 int i = 0;
634
635 /* Set up a hash table for the instructions. */
636 op_hash = hash_new ();
637 if (op_hash == NULL)
638 as_fatal (_("Virtual memory exhausted"));
639
640 while (cris_opcodes[i].name != NULL)
641 {
642 const char *name = cris_opcodes[i].name;
643 hashret = hash_insert (op_hash, name, (PTR) &cris_opcodes[i]);
644
645 if (hashret != NULL && *hashret != '\0')
646 as_fatal (_("Can't hash `%s': %s\n"), cris_opcodes[i].name,
647 *hashret == 0 ? _("(unknown reason)") : hashret);
648 do
649 {
650 if (cris_opcodes[i].match & cris_opcodes[i].lose)
651 as_fatal (_("Buggy opcode: `%s' \"%s\"\n"), cris_opcodes[i].name,
652 cris_opcodes[i].args);
653
654 ++i;
655 } while (cris_opcodes[i].name != NULL
656 && strcmp (cris_opcodes[i].name, name) == 0);
657 }
658 }
659
660
661 /* Assemble a source line. */
662 void
663 md_assemble (str)
664 char *str;
665 {
666 struct cris_instruction output_instruction;
667 struct cris_prefix prefix;
668 char *opcodep;
669 char *p;
670
671 know (str);
672
673 /* Do the low-level grunt - assemble to bits and split up into a prefix
674 and ordinary insn. */
675 cris_process_instruction (str, &output_instruction, &prefix);
676
677 /* Handle any prefixes to the instruction. */
678 switch (prefix.kind)
679 {
680 case PREFIX_NONE:
681 break;
682
683 /* When the expression is unknown for a BDAP, it can need 0, 2 or 4
684 extra bytes, so we handle it separately. */
685 case PREFIX_BDAP_IMM:
686 gen_bdap (prefix.base_reg_number, &prefix.expr);
687 break;
688
689 case PREFIX_BDAP:
690 case PREFIX_BIAP:
691 case PREFIX_DIP:
692 opcodep = frag_more (2);
693
694 /* Output the prefix opcode. */
695 md_number_to_chars (opcodep, (long) prefix.opcode, 2);
696
697 /* This only happens for DIP, but is ok for the others as they have
698 no reloc. */
699 if (prefix.reloc != BFD_RELOC_NONE)
700 {
701 /* Output an absolute mode address. */
702 p = frag_more (4);
703 fix_new_exp (frag_now, (p - frag_now->fr_literal), 4,
704 &prefix.expr, 0, prefix.reloc);
705 }
706 break;
707
708 case PREFIX_PUSH:
709 opcodep = frag_more (2);
710
711 /* Output the prefix opcode. Being a "push", we add the negative
712 size of the register to "sp". */
713 if (output_instruction.spec_reg != NULL)
714 {
715 /* Special register. */
716 opcodep[0] = -output_instruction.spec_reg->reg_size;
717 }
718 else
719 {
720 /* General register. */
721 opcodep[0] = -4;
722 }
723 opcodep[1] = (REG_SP << 4) + (BDAP_QUICK_OPCODE >> 8);
724 break;
725
726 default:
727 BAD_CASE (prefix.kind);
728 }
729
730 /* If we only had a prefix insn, we're done. */
731 if (output_instruction.insn_type == CRIS_INSN_NONE)
732 return;
733
734 /* Done with the prefix. Continue with the main instruction. */
735 opcodep = frag_more (2);
736
737 /* Output the instruction opcode. */
738 md_number_to_chars (opcodep, (long)(output_instruction.opcode), 2);
739
740 /* Output the symbol-dependent instruction stuff. */
741 if (output_instruction.insn_type == CRIS_INSN_BRANCH)
742 {
743 segT to_seg = absolute_section;
744 int is_undefined = 0;
745 int length_code;
746
747 if (output_instruction.expr.X_op != O_constant)
748 {
749 to_seg = S_GET_SEGMENT (output_instruction.expr.X_add_symbol);
750
751 if (to_seg == undefined_section)
752 is_undefined = 1;
753 }
754
755 if (output_instruction.expr.X_op == O_constant
756 || to_seg == now_seg || is_undefined)
757 {
758 /* If is_undefined, then the expression may BECOME now_seg. */
759 length_code = is_undefined ? STATE_UNDF : STATE_BYTE;
760
761 /* Make room for max ten bytes of variable length. */
762 frag_var (rs_machine_dependent, 10, 0,
763 ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, length_code),
764 output_instruction.expr.X_add_symbol,
765 output_instruction.expr.X_add_number,
766 opcodep);
767 }
768 else
769 {
770 /* We have: to_seg != now_seg && to_seg != undefined_section.
771 This means it is a branch to a known symbol in another
772 section. Code in data? Weird but valid. Emit a 32-bit
773 branch. */
774 gen_cond_branch_32 (opcodep, frag_more (10), frag_now,
775 output_instruction.expr.X_add_symbol,
776 (symbolS *)NULL,
777 output_instruction.expr.X_add_number);
778 }
779 }
780 else
781 {
782 if (output_instruction.imm_oprnd_size > 0)
783 {
784 /* The intruction has an immediate operand. */
785 enum bfd_reloc_code_real reloc = 0;
786
787 switch (output_instruction.imm_oprnd_size)
788 {
789 /* Any byte-size immediate constants are treated as
790 word-size. FIXME: Thus overflow check does not work
791 correctly. */
792
793 case 2:
794 reloc = BFD_RELOC_16;
795 break;
796
797 case 4:
798 reloc = BFD_RELOC_32;
799 break;
800
801 default:
802 BAD_CASE (output_instruction.imm_oprnd_size);
803 }
804
805 p = frag_more (output_instruction.imm_oprnd_size);
806 fix_new_exp (frag_now, (p - frag_now->fr_literal),
807 output_instruction.imm_oprnd_size,
808 &output_instruction.expr, 0, reloc);
809 }
810 else if (output_instruction.reloc != BFD_RELOC_NONE)
811 {
812 /* An immediate operand that has a relocation and needs to be
813 processed further. */
814
815 /* It is important to use fix_new_exp here and everywhere else
816 (and not fix_new), as fix_new_exp can handle "difference
817 expressions" - where the expression contains a difference of
818 two symbols in the same segment. */
819 fix_new_exp (frag_now, (opcodep - frag_now->fr_literal), 2,
820 &output_instruction.expr, 0,
821 output_instruction.reloc);
822 }
823 }
824 }
825
826
827 /* Low level text-to-bits assembly. */
828 static void
829 cris_process_instruction (insn_text, out_insnp, prefixp)
830 char *insn_text;
831 struct cris_instruction *out_insnp;
832 struct cris_prefix *prefixp;
833 {
834 char *s;
835 char modified_char = 0;
836 const char *args;
837 struct cris_opcode *instruction;
838 char *operands;
839 int match = 0;
840 int mode;
841 int regno;
842 int size_bits;
843
844 /* Reset these fields to a harmless state in case we need to return in
845 error. */
846 prefixp->kind = PREFIX_NONE;
847 prefixp->reloc = BFD_RELOC_NONE;
848 out_insnp->insn_type = CRIS_INSN_NORMAL;
849 out_insnp->imm_oprnd_size = 0;
850
851 /* Find the end of the opcode mnemonic. We assume (true in 2.9.1)
852 that the caller has translated the opcode to lower-case, up to the
853 first non-letter. */
854 for (operands = insn_text;
855 islower (*operands);
856 ++operands)
857 ;
858
859 /* Terminate the opcode after letters, but save the character there if
860 it was of significance. */
861 switch (*operands)
862 {
863 case '\0':
864 break;
865
866 case '.':
867 /* Put back the modified character later */
868 modified_char = *operands;
869 /* FALLTHROUGH. */
870
871 case ' ':
872 /* Consume the character after the mnemonic and replace */
873 /* it with '\0'. */
874 *operands++ = '\0';
875 break;
876
877 default:
878 as_bad (_("Unknown opcode: `%s'"), insn_text);
879 return;
880 }
881
882 /* Find the instruction. */
883 instruction = (struct cris_opcode *) hash_find (op_hash, insn_text);
884 if (instruction == NULL)
885 {
886 as_bad (_("Unknown opcode: `%s'"), insn_text);
887 return;
888 }
889
890 /* Put back the modified character. */
891 switch (modified_char)
892 {
893 case 0:
894 break;
895
896 default:
897 *--operands = modified_char;
898 }
899
900
901 /* Try to match an opcode table slot. */
902 for (s = operands;
903 ;
904 )
905 {
906 int imm_expr_found;
907
908 /* Initialize *prefixp, perhaps after being modified for a
909 "near match". */
910 prefixp->kind = PREFIX_NONE;
911 prefixp->reloc = BFD_RELOC_NONE;
912
913 /* Initialize *out_insnp. */
914 memset (out_insnp, 0, sizeof (*out_insnp));
915 out_insnp->opcode = instruction->match;
916 out_insnp->reloc = BFD_RELOC_NONE;
917 out_insnp->insn_type = CRIS_INSN_NORMAL;
918 out_insnp->imm_oprnd_size = 0;
919
920 imm_expr_found = 0;
921
922 /* Build the opcode, checking as we go to make sure that the
923 operands match. */
924 for (args = instruction->args;
925 ;
926 ++args)
927 {
928 switch (*args)
929 {
930 case '\0':
931 /* If we've come to the end of arguments, we're done. */
932 if (*s == '\0')
933 match = 1;
934 break;
935
936 case '!':
937 /* Non-matcher character for disassembly.
938 Ignore it here. */
939 continue;
940
941 case ',':
942 case ' ':
943 /* These must match exactly. */
944 if (*s++ == *args)
945 continue;
946 break;
947
948 case 'B':
949 /* This is not really an operand, but causes a "BDAP
950 -size,SP" prefix to be output, for PUSH instructions. */
951 prefixp->kind = PREFIX_PUSH;
952 continue;
953
954 case 'b':
955 /* This letter marks an operand that should not be matched
956 in the assembler. It is a branch with 16-bit
957 displacement. The assembler will create them from the
958 8-bit flavor when necessary. The assembler does not
959 support the [rN+] operand, as the [r15+] that is
960 generated for 16-bit displacements. */
961 break;
962
963 case 'c':
964 /* A 5-bit unsigned immediate in bits <4:0>. */
965 if (! cris_get_expression (&s, &out_insnp->expr))
966 break;
967 else
968 {
969 if (out_insnp->expr.X_op == O_constant
970 && (out_insnp->expr.X_add_number < 0
971 || out_insnp->expr.X_add_number > 31))
972 as_bad (_("Immediate value not in 5 bit unsigned range: %ld"),
973 out_insnp->expr.X_add_number);
974
975 out_insnp->reloc = BFD_RELOC_CRIS_UNSIGNED_5;
976 continue;
977 }
978
979 case 'C':
980 /* A 4-bit unsigned immediate in bits <3:0>. */
981 if (! cris_get_expression (&s, &out_insnp->expr))
982 break;
983 else
984 {
985 if (out_insnp->expr.X_op == O_constant
986 && (out_insnp->expr.X_add_number < 0
987 || out_insnp->expr.X_add_number > 15))
988 as_bad (_("Immediate value not in 4 bit unsigned range: %ld"),
989 out_insnp->expr.X_add_number);
990
991 out_insnp->reloc = BFD_RELOC_CRIS_UNSIGNED_4;
992 continue;
993 }
994
995 case 'D':
996 /* General register in bits <15:12> and <3:0>. */
997 if (! get_gen_reg (&s, &regno))
998 break;
999 else
1000 {
1001 out_insnp->opcode |= regno /* << 0 */;
1002 out_insnp->opcode |= regno << 12;
1003 continue;
1004 }
1005
1006 case 'f':
1007 /* Flags from the condition code register. */
1008 {
1009 int flags = 0;
1010
1011 if (! get_flags (&s, &flags))
1012 break;
1013
1014 out_insnp->opcode
1015 |= ((flags & 0xf0) << 8) | (flags & 0xf);
1016 continue;
1017 }
1018
1019 case 'i':
1020 /* A 6-bit signed immediate in bits <5:0>. */
1021 if (! cris_get_expression (&s, &out_insnp->expr))
1022 break;
1023 else
1024 {
1025 if (out_insnp->expr.X_op == O_constant
1026 && (out_insnp->expr.X_add_number < -32
1027 || out_insnp->expr.X_add_number > 31))
1028 as_bad (_("Immediate value not in 6 bit range: %ld"),
1029 out_insnp->expr.X_add_number);
1030 out_insnp->reloc = BFD_RELOC_CRIS_SIGNED_6;
1031 continue;
1032 }
1033
1034 case 'I':
1035 /* A 6-bit unsigned immediate in bits <5:0>. */
1036 if (! cris_get_expression (&s, &out_insnp->expr))
1037 break;
1038 else
1039 {
1040 if (out_insnp->expr.X_op == O_constant
1041 && (out_insnp->expr.X_add_number < 0
1042 || out_insnp->expr.X_add_number > 63))
1043 as_bad (_("Immediate value not in 6 bit unsigned range: %ld"),
1044 out_insnp->expr.X_add_number);
1045 out_insnp->reloc = BFD_RELOC_CRIS_UNSIGNED_6;
1046 continue;
1047 }
1048
1049 case 'M':
1050 /* A size modifier, B, W or D, to be put in a bit position
1051 suitable for CLEAR instructions (i.e. reflecting a zero
1052 register). */
1053 if (! get_bwd_size_modifier (&s, &size_bits))
1054 break;
1055 else
1056 {
1057 switch (size_bits)
1058 {
1059 case 0:
1060 out_insnp->opcode |= 0 << 12;
1061 break;
1062
1063 case 1:
1064 out_insnp->opcode |= 4 << 12;
1065 break;
1066
1067 case 2:
1068 out_insnp->opcode |= 8 << 12;
1069 break;
1070 }
1071 continue;
1072 }
1073
1074 case 'm':
1075 /* A size modifier, B, W or D, to be put in bits <5:4>. */
1076 if (! get_bwd_size_modifier (&s, &size_bits))
1077 break;
1078 else
1079 {
1080 out_insnp->opcode |= size_bits << 4;
1081 continue;
1082 }
1083
1084 case 'o':
1085 /* A branch expression. */
1086 if (! cris_get_expression (&s, &out_insnp->expr))
1087 break;
1088 else
1089 {
1090 out_insnp->insn_type = CRIS_INSN_BRANCH;
1091 continue;
1092 }
1093
1094 case 'O':
1095 /* A BDAP expression for any size, "expr,r". */
1096 if (! cris_get_expression (&s, &prefixp->expr))
1097 break;
1098 else
1099 {
1100 if (*s != ',')
1101 break;
1102
1103 s++;
1104
1105 if (!get_gen_reg (&s, &prefixp->base_reg_number))
1106 break;
1107
1108 /* Since 'O' is used with an explicit bdap, we have no
1109 "real" instruction. */
1110 prefixp->kind = PREFIX_BDAP_IMM;
1111 out_insnp->insn_type = CRIS_INSN_NONE;
1112 continue;
1113 }
1114
1115 case 'P':
1116 /* Special register in bits <15:12>. */
1117 if (! get_spec_reg (&s, &out_insnp->spec_reg))
1118 break;
1119 else
1120 {
1121 /* Use of some special register names come with a
1122 specific warning. Note that we have no ".cpu type"
1123 pseudo yet, so some of this is just unused
1124 framework. */
1125 if (out_insnp->spec_reg->warning)
1126 as_warn (out_insnp->spec_reg->warning);
1127 else if (out_insnp->spec_reg->applicable_version
1128 == cris_ver_warning)
1129 /* Others have a generic warning. */
1130 as_warn (_("Unimplemented register `%s' specified"),
1131 out_insnp->spec_reg->name);
1132
1133 out_insnp->opcode
1134 |= out_insnp->spec_reg->number << 12;
1135 continue;
1136 }
1137
1138 case 'p':
1139 /* This character is used in the disassembler to
1140 recognize a prefix instruction to fold into the
1141 addressing mode for the next instruction. It is
1142 ignored here. */
1143 continue;
1144
1145 case 'R':
1146 /* General register in bits <15:12>. */
1147 if (! get_gen_reg (&s, &regno))
1148 break;
1149 else
1150 {
1151 out_insnp->opcode |= regno << 12;
1152 continue;
1153 }
1154
1155 case 'r':
1156 /* General register in bits <3:0>. */
1157 if (! get_gen_reg (&s, &regno))
1158 break;
1159 else
1160 {
1161 out_insnp->opcode |= regno /* << 0 */;
1162 continue;
1163 }
1164
1165 case 'S':
1166 /* Source operand in bit <10> and a prefix; a 3-operand
1167 prefix. */
1168 if (! get_3op_or_dip_prefix_op (&s, prefixp))
1169 break;
1170 else
1171 continue;
1172
1173 case 's':
1174 /* Source operand in bits <10>, <3:0> and optionally a
1175 prefix; i.e. an indirect operand or an side-effect
1176 prefix. */
1177 if (! get_autoinc_prefix_or_indir_op (&s, prefixp, &mode,
1178 &regno,
1179 &imm_expr_found,
1180 &out_insnp->expr))
1181 break;
1182 else
1183 {
1184 if (prefixp->kind != PREFIX_NONE)
1185 {
1186 /* A prefix, so it has the autoincrement bit
1187 set. */
1188 out_insnp->opcode |= (AUTOINCR_BIT << 8);
1189 }
1190 else
1191 /* No prefix. The "mode" variable contains bits like
1192 whether or not this is autoincrement mode. */
1193 out_insnp->opcode |= (mode << 10);
1194
1195 out_insnp->opcode |= regno /* << 0 */ ;
1196 continue;
1197 }
1198
1199 case 'x':
1200 /* Rs.m in bits <15:12> and <5:4>. */
1201 if (! get_gen_reg (&s, &regno)
1202 || ! get_bwd_size_modifier (&s, &size_bits))
1203 break;
1204 else
1205 {
1206 out_insnp->opcode |=
1207 (regno << 12) | (size_bits << 4);
1208 continue;
1209 }
1210
1211 case 'y':
1212 /* Source operand in bits <10>, <3:0> and optionally a
1213 prefix; i.e. an indirect operand or an side-effect
1214 prefix.
1215
1216 The difference to 's' is that this does not allow an
1217 "immediate" expression. */
1218 if (! get_autoinc_prefix_or_indir_op (&s, prefixp,
1219 &mode, &regno,
1220 &imm_expr_found,
1221 &out_insnp->expr)
1222 || imm_expr_found)
1223 break;
1224 else
1225 {
1226 if (prefixp->kind != PREFIX_NONE)
1227 {
1228 /* A prefix, and those matched here always have
1229 side-effects (see 's' case). */
1230 out_insnp->opcode |= (AUTOINCR_BIT << 8);
1231 }
1232 else
1233 {
1234 /* No prefix. The "mode" variable contains bits
1235 like whether or not this is autoincrement
1236 mode. */
1237 out_insnp->opcode |= (mode << 10);
1238 }
1239
1240 out_insnp->opcode |= regno /* << 0 */;
1241 continue;
1242 }
1243
1244 case 'z':
1245 /* Size modifier (B or W) in bit <4>. */
1246 if (! get_bw_size_modifier (&s, &size_bits))
1247 break;
1248 else
1249 {
1250 out_insnp->opcode |= size_bits << 4;
1251 continue;
1252 }
1253
1254 default:
1255 BAD_CASE (*args);
1256 }
1257
1258 /* We get here when we fail a match above or we found a
1259 complete match. Break out of this loop. */
1260 break;
1261 }
1262
1263 /* Was it a match or a miss? */
1264 if (match == 0)
1265 {
1266 /* If it's just that the args don't match, maybe the next
1267 item in the table is the same opcode but with
1268 matching operands. */
1269 if (instruction[1].name != NULL
1270 && ! strcmp (instruction->name, instruction[1].name))
1271 {
1272 /* Yep. Restart and try that one instead. */
1273 ++instruction;
1274 s = operands;
1275 continue;
1276 }
1277 else
1278 {
1279 /* We've come to the end of instructions with this
1280 opcode, so it must be an error. */
1281 as_bad (_("Illegal operands"));
1282 return;
1283 }
1284 }
1285 else
1286 {
1287 /* We have a match. Check if there's anything more to do. */
1288 if (imm_expr_found)
1289 {
1290 /* There was an immediate mode operand, so we must check
1291 that it has an appropriate size. */
1292
1293 switch (instruction->imm_oprnd_size)
1294 {
1295 default:
1296 case SIZE_NONE:
1297 /* Shouldn't happen; this one does not have immediate
1298 operands with different sizes. */
1299 BAD_CASE (instruction->imm_oprnd_size);
1300 break;
1301
1302 case SIZE_FIX_32:
1303 out_insnp->imm_oprnd_size = 4;
1304 break;
1305
1306 case SIZE_SPEC_REG:
1307 switch (out_insnp->spec_reg->reg_size)
1308 {
1309 case 1:
1310 if (out_insnp->expr.X_op == O_constant
1311 && (out_insnp->expr.X_add_number < -128
1312 || out_insnp->expr.X_add_number > 255))
1313 as_bad (_("Immediate value not in 8 bit range: %ld"),
1314 out_insnp->expr.X_add_number);
1315 /* FALLTHROUGH. */
1316 case 2:
1317 /* FIXME: We need an indicator in the instruction
1318 table to pass on, to indicate if we need to check
1319 overflow for a signed or unsigned number. */
1320 if (out_insnp->expr.X_op == O_constant
1321 && (out_insnp->expr.X_add_number < -32768
1322 || out_insnp->expr.X_add_number > 65535))
1323 as_bad (_("Immediate value not in 16 bit range: %ld"),
1324 out_insnp->expr.X_add_number);
1325 out_insnp->imm_oprnd_size = 2;
1326 break;
1327
1328 case 4:
1329 out_insnp->imm_oprnd_size = 4;
1330 break;
1331
1332 default:
1333 BAD_CASE (out_insnp->spec_reg->reg_size);
1334 }
1335 break;
1336
1337 case SIZE_FIELD:
1338 switch (size_bits)
1339 {
1340 case 0:
1341 if (out_insnp->expr.X_op == O_constant
1342 && (out_insnp->expr.X_add_number < -128
1343 || out_insnp->expr.X_add_number > 255))
1344 as_bad (_("Immediate value not in 8 bit range: %ld"),
1345 out_insnp->expr.X_add_number);
1346 /* FALLTHROUGH. */
1347 case 1:
1348 if (out_insnp->expr.X_op == O_constant
1349 && (out_insnp->expr.X_add_number < -32768
1350 || out_insnp->expr.X_add_number > 65535))
1351 as_bad (_("Immediate value not in 16 bit range: %ld"),
1352 out_insnp->expr.X_add_number);
1353 out_insnp->imm_oprnd_size = 2;
1354 break;
1355
1356 case 2:
1357 out_insnp->imm_oprnd_size = 4;
1358 break;
1359
1360 default:
1361 BAD_CASE (out_insnp->spec_reg->reg_size);
1362 }
1363 }
1364 }
1365 }
1366 break;
1367 }
1368 }
1369
1370
1371 /* Get a B, W, or D size modifier from the string pointed out by *cPP,
1372 which must point to a '.' in front of the modifier. On successful
1373 return, *cPP is advanced to the character following the size
1374 modifier, and is undefined otherwise.
1375
1376 cPP Pointer to pointer to string starting
1377 with the size modifier.
1378
1379 size_bitsp Pointer to variable to contain the size bits on
1380 successful return.
1381
1382 Return 1 iff a correct size modifier is found, else 0. */
1383
1384 static int
1385 get_bwd_size_modifier (cPP, size_bitsp)
1386 char **cPP;
1387 int *size_bitsp;
1388 {
1389 if (**cPP != '.')
1390 return 0;
1391 else
1392 {
1393 /* Consume the '.' */
1394 (*cPP)++;
1395
1396 switch (**cPP)
1397 {
1398 case 'B':
1399 case 'b':
1400 *size_bitsp = 0;
1401 break;
1402
1403 case 'W':
1404 case 'w':
1405 *size_bitsp = 1;
1406 break;
1407
1408 case 'D':
1409 case 'd':
1410 *size_bitsp = 2;
1411 break;
1412
1413 default:
1414 return 0;
1415 }
1416
1417 /* Consume the size letter. */
1418 (*cPP)++;
1419 return 1;
1420 }
1421 }
1422
1423
1424 /* Get a B or W size modifier from the string pointed out by *cPP,
1425 which must point to a '.' in front of the modifier. On successful
1426 return, *cPP is advanced to the character following the size
1427 modifier, and is undefined otherwise.
1428
1429 cPP Pointer to pointer to string starting
1430 with the size modifier.
1431
1432 size_bitsp Pointer to variable to contain the size bits on
1433 successful return.
1434
1435 Return 1 iff a correct size modifier is found, else 0. */
1436
1437 static int
1438 get_bw_size_modifier (cPP, size_bitsp)
1439 char **cPP;
1440 int *size_bitsp;
1441 {
1442 if (**cPP != '.')
1443 return 0;
1444 else
1445 {
1446 /* Consume the '.' */
1447 (*cPP)++;
1448
1449 switch (**cPP)
1450 {
1451 case 'B':
1452 case 'b':
1453 *size_bitsp = 0;
1454 break;
1455
1456 case 'W':
1457 case 'w':
1458 *size_bitsp = 1;
1459 break;
1460
1461 default:
1462 return 0;
1463 }
1464
1465 /* Consume the size letter. */
1466 (*cPP)++;
1467 return 1;
1468 }
1469 }
1470
1471
1472 /* Get a general register from the string pointed out by *cPP. The
1473 variable *cPP is advanced to the character following the general
1474 register name on a successful return, and has its initial position
1475 otherwise.
1476
1477 cPP Pointer to pointer to string, beginning with a general
1478 register name.
1479
1480 regnop Pointer to int containing the register number.
1481
1482 Return 1 iff a correct general register designator is found,
1483 else 0. */
1484
1485 static int
1486 get_gen_reg (cPP, regnop)
1487 char **cPP;
1488 int *regnop;
1489 {
1490 char *oldp;
1491 oldp = *cPP;
1492
1493 switch (**cPP)
1494 {
1495 case 'P':
1496 case 'p':
1497 /* "P" as in "PC"? Consume the "P". */
1498 (*cPP)++;
1499
1500 if ((**cPP == 'C' || **cPP == 'c')
1501 && ! isalnum ((*cPP)[1]))
1502 {
1503 /* It's "PC": consume the "c" and we're done. */
1504 (*cPP)++;
1505 *regnop = REG_PC;
1506 return 1;
1507 }
1508 break;
1509
1510 case 'R':
1511 case 'r':
1512 /* Hopefully r[0-9] or r1[0-5]. Consume 'R' or 'r' */
1513 (*cPP)++;
1514
1515 if (isdigit (**cPP))
1516 {
1517 /* It's r[0-9]. Consume and check the next digit. */
1518 *regnop = **cPP - '0';
1519 (*cPP)++;
1520
1521 if (! isalnum (**cPP))
1522 {
1523 /* No more digits, we're done. */
1524 return 1;
1525 }
1526 else
1527 {
1528 /* One more digit. Consume and add. */
1529 *regnop = *regnop*10 + (**cPP - '0');
1530
1531 /* We need to check for a valid register number; Rn,
1532 0 <= n <= MAX_REG. */
1533 if (*regnop <= MAX_REG)
1534 {
1535 /* Consume second digit. */
1536 (*cPP)++;
1537 return 1;
1538 }
1539 }
1540 }
1541 break;
1542
1543 case 'S':
1544 case 's':
1545 /* "S" as in "SP"? Consume the "S". */
1546 (*cPP)++;
1547 if (**cPP == 'P' || **cPP == 'p')
1548 {
1549 /* It's "SP": consume the "p" and we're done. */
1550 (*cPP)++;
1551 *regnop = REG_SP;
1552 return 1;
1553 }
1554 break;
1555
1556 default:
1557 /* Just here to silence compilation warnings. */
1558 ;
1559 }
1560
1561 /* We get here if we fail. Restore the pointer. */
1562 *cPP = oldp;
1563 return 0;
1564 }
1565
1566
1567 /* Get a special register from the string pointed out by *cPP. The
1568 variable *cPP is advanced to the character following the special
1569 register name if one is found, and retains its original position
1570 otherwise.
1571
1572 cPP Pointer to pointer to string starting with a special register
1573 name.
1574
1575 sregpp Pointer to Pointer to struct spec_reg, where a pointer to the
1576 register description will be stored.
1577
1578 Return 1 iff a correct special register name is found. */
1579
1580 static int
1581 get_spec_reg (cPP, sregpp)
1582 char **cPP;
1583 const struct cris_spec_reg **sregpp;
1584 {
1585 char *s1;
1586 const char *s2;
1587
1588 const struct cris_spec_reg *sregp;
1589
1590 /* Loop over all special registers. */
1591 for (sregp = cris_spec_regs;
1592 sregp->name != NULL;
1593 sregp++)
1594 {
1595
1596 /* Start over from beginning of the supposed name. */
1597 s1 = *cPP;
1598 s2 = sregp->name;
1599
1600 while (*s2 != '\0'
1601 && (isupper (*s1) ? tolower (*s1) == *s2 : *s1 == *s2))
1602 {
1603 s1++;
1604 s2++;
1605 }
1606
1607 /* For a match, we must have consumed the name in the table, and we
1608 must be outside what could be part of a name. Assume here that a
1609 test for alphanumerics is sufficient for a name test. */
1610 if (*s2 == 0 && ! isalnum (*s1))
1611 {
1612 /* We have a match. Update the pointer and be done. */
1613 *cPP = s1;
1614 *sregpp = sregp;
1615 return 1;
1616 }
1617 }
1618
1619 /* If we got here, we did not find any name. */
1620 return 0;
1621 }
1622
1623
1624 /* Get an unprefixed or side-effect-prefix operand from the string pointed
1625 out by *cPP. The pointer *cPP is advanced to the character following
1626 the indirect operand if we have success, else it contains an undefined
1627 value.
1628
1629 cPP Pointer to pointer to string beginning with the first
1630 character of the supposed operand.
1631
1632 prefixp Pointer to structure containing an optional instruction
1633 prefix.
1634
1635 is_autoincp Pointer to int indicating the indirect or autoincrement
1636 bits.
1637
1638 src_regnop Pointer to int containing the source register number in
1639 the instruction.
1640
1641 imm_foundp Pointer to an int indicating if an immediate expression
1642 is found.
1643
1644 imm_exprP Pointer to a structure containing an immediate
1645 expression, if success and if *imm_foundp is nonzero.
1646
1647 Return 1 iff a correct indirect operand is found. */
1648
1649 static int
1650 get_autoinc_prefix_or_indir_op (cPP, prefixp, is_autoincp, src_regnop,
1651 imm_foundp, imm_exprP)
1652 char **cPP;
1653 struct cris_prefix *prefixp;
1654 int *is_autoincp;
1655 int *src_regnop;
1656 int *imm_foundp;
1657 expressionS *imm_exprP;
1658 {
1659 /* Assume there was no immediate mode expression. */
1660 *imm_foundp = 0;
1661
1662 if (**cPP == '[')
1663 {
1664 /* So this operand is one of:
1665 Indirect: [rN]
1666 Autoincrement: [rN+]
1667 Indexed with assign: [rN=rM+rO.S]
1668 Offset with assign: [rN=rM+I], [rN=rM+[rO].s], [rN=rM+[rO+].s]
1669
1670 Either way, consume the '['. */
1671 (*cPP)++;
1672
1673 /* Get the rN register. */
1674 if (! get_gen_reg (cPP, src_regnop))
1675 /* If there was no register, then this cannot match. */
1676 return 0;
1677 else
1678 {
1679 /* We got the register, now check the next character. */
1680 switch (**cPP)
1681 {
1682 case ']':
1683 /* Indirect mode. We're done here. */
1684 prefixp->kind = PREFIX_NONE;
1685 *is_autoincp = 0;
1686 break;
1687
1688 case '+':
1689 /* This must be an auto-increment mode, if there's a
1690 match. */
1691 prefixp->kind = PREFIX_NONE;
1692 *is_autoincp = 1;
1693
1694 /* We consume this character and break out to check the
1695 closing ']'. */
1696 (*cPP)++;
1697 break;
1698
1699 case '=':
1700 /* This must be indexed with assign, or offset with assign
1701 to match. */
1702 (*cPP)++;
1703
1704 /* Either way, the next thing must be a register. */
1705 if (! get_gen_reg (cPP, &prefixp->base_reg_number))
1706 /* No register, no match. */
1707 return 0;
1708 else
1709 {
1710 /* We've consumed "[rN=rM", so we must be looking at
1711 "+rO.s]" or "+I]", or "-I]", or "+[rO].s]" or
1712 "+[rO+].s]". */
1713 if (**cPP == '+')
1714 {
1715 int index_reg_number;
1716 (*cPP)++;
1717
1718 if (**cPP == '[')
1719 {
1720 int size_bits;
1721 /* This must be [rx=ry+[rz].s] or
1722 [rx=ry+[rz+].s] or no match. We must be
1723 looking at rz after consuming the '['. */
1724 (*cPP)++;
1725
1726 if (!get_gen_reg (cPP, &index_reg_number))
1727 return 0;
1728
1729 prefixp->kind = PREFIX_BDAP;
1730 prefixp->opcode
1731 = (BDAP_INDIR_OPCODE
1732 + (prefixp->base_reg_number << 12)
1733 + index_reg_number);
1734
1735 if (**cPP == '+')
1736 {
1737 /* We've seen "[rx=ry+[rz+" here, so now we
1738 know that there must be "].s]" left to
1739 check. */
1740 (*cPP)++;
1741 prefixp->opcode |= AUTOINCR_BIT << 8;
1742 }
1743
1744 /* If it wasn't autoincrement, we don't need to
1745 add anything. */
1746
1747 /* Check the next-to-last ']'. */
1748 if (**cPP != ']')
1749 return 0;
1750
1751 (*cPP)++;
1752
1753 /* Check the ".s" modifier. */
1754 if (! get_bwd_size_modifier (cPP, &size_bits))
1755 return 0;
1756
1757 prefixp->opcode |= size_bits << 4;
1758
1759 /* Now we got [rx=ry+[rz+].s or [rx=ry+[rz].s.
1760 We break out to check the final ']'. */
1761 break;
1762 }
1763 /* It wasn't an indirection. Check if it's a
1764 register. */
1765 else if (get_gen_reg (cPP, &index_reg_number))
1766 {
1767 int size_bits;
1768
1769 /* Indexed with assign mode: "[rN+rM.S]". */
1770 prefixp->kind = PREFIX_BIAP;
1771 prefixp->opcode
1772 = (BIAP_OPCODE + (index_reg_number << 12)
1773 + prefixp->base_reg_number /* << 0 */);
1774
1775 if (! get_bwd_size_modifier (cPP, &size_bits))
1776 /* Size missing, this isn't a match. */
1777 return 0;
1778 else
1779 {
1780 /* Size found, break out to check the
1781 final ']'. */
1782 prefixp->opcode |= size_bits << 4;
1783 break;
1784 }
1785 }
1786 /* Not a register. Then this must be "[rN+I]". */
1787 else if (cris_get_expression (cPP, &prefixp->expr))
1788 {
1789 /* We've got offset with assign mode. Fill
1790 in the blanks and break out to match the
1791 final ']'. */
1792 prefixp->kind = PREFIX_BDAP_IMM;
1793 break;
1794 }
1795 else
1796 /* Neither register nor expression found, so
1797 this can't be a match. */
1798 return 0;
1799 }
1800 /* Not "[rN+" but perhaps "[rN-"? */
1801 else if (**cPP == '-')
1802 {
1803 /* We must have an offset with assign mode. */
1804 if (! cris_get_expression (cPP, &prefixp->expr))
1805 /* No expression, no match. */
1806 return 0;
1807 else
1808 {
1809 /* We've got offset with assign mode. Fill
1810 in the blanks and break out to match the
1811 final ']'. */
1812 prefixp->kind = PREFIX_BDAP_IMM;
1813 break;
1814 }
1815 }
1816 else
1817 /* Neither '+' nor '-' after "[rN=rM". Lose. */
1818 return 0;
1819 }
1820 default:
1821 /* Neither ']' nor '+' nor '=' after "[rN". Lose. */
1822 return 0;
1823 }
1824 }
1825
1826 /* When we get here, we have a match and will just check the closing
1827 ']'. We can still fail though. */
1828 if (**cPP != ']')
1829 return 0;
1830 else
1831 {
1832 /* Don't forget to consume the final ']'.
1833 Then return in glory. */
1834 (*cPP)++;
1835 return 1;
1836 }
1837 }
1838 /* No indirection. Perhaps a constant? */
1839 else if (cris_get_expression (cPP, imm_exprP))
1840 {
1841 /* Expression found, this is immediate mode. */
1842 prefixp->kind = PREFIX_NONE;
1843 *is_autoincp = 1;
1844 *src_regnop = REG_PC;
1845 *imm_foundp = 1;
1846 return 1;
1847 }
1848
1849 /* No luck today. */
1850 return 0;
1851 }
1852
1853
1854 /* This function gets an indirect operand in a three-address operand
1855 combination from the string pointed out by *cPP. The pointer *cPP is
1856 advanced to the character following the indirect operand on success, or
1857 has an unspecified value on failure.
1858
1859 cPP Pointer to pointer to string begining
1860 with the operand
1861
1862 prefixp Pointer to structure containing an
1863 instruction prefix
1864
1865 Returns 1 iff a correct indirect operand is found. */
1866
1867 static int
1868 get_3op_or_dip_prefix_op (cPP, prefixp)
1869 char **cPP;
1870 struct cris_prefix *prefixp;
1871 {
1872 int reg_number;
1873
1874 if (**cPP != '[')
1875 /* We must have a '[' or it's a clean failure. */
1876 return 0;
1877
1878 /* Eat the first '['. */
1879 (*cPP)++;
1880
1881 if (**cPP == '[')
1882 {
1883 /* A second '[', so this must be double-indirect mode. */
1884 (*cPP)++;
1885 prefixp->kind = PREFIX_DIP;
1886 prefixp->opcode = DIP_OPCODE;
1887
1888 /* Get the register or fail entirely. */
1889 if (! get_gen_reg (cPP, &reg_number))
1890 return 0;
1891 else
1892 {
1893 prefixp->opcode |= reg_number /* << 0 */ ;
1894 if (**cPP == '+')
1895 {
1896 /* Since we found a '+', this must be double-indirect
1897 autoincrement mode. */
1898 (*cPP)++;
1899 prefixp->opcode |= AUTOINCR_BIT << 8;
1900 }
1901
1902 /* There's nothing particular to do, if this was a
1903 double-indirect *without* autoincrement. */
1904 }
1905
1906 /* Check the first ']'. The second one is checked at the end. */
1907 if (**cPP != ']')
1908 return 0;
1909
1910 /* Eat the first ']', so we'll be looking at a second ']'. */
1911 (*cPP)++;
1912 }
1913 /* No second '['. Then we should have a register here, making
1914 it "[rN". */
1915 else if (get_gen_reg (cPP, &prefixp->base_reg_number))
1916 {
1917 /* This must be indexed or offset mode: "[rN+I]" or
1918 "[rN+rM.S]" or "[rN+[rM].S]" or "[rN+[rM+].S]". */
1919 if (**cPP == '+')
1920 {
1921 /* Not the first alternative, must be one of the last
1922 three. */
1923 int index_reg_number;
1924
1925 (*cPP)++;
1926
1927 if (**cPP == '[')
1928 {
1929 /* This is "[rx+["... Expect a register next. */
1930 int size_bits;
1931 (*cPP)++;
1932
1933 if (!get_gen_reg (cPP, &index_reg_number))
1934 return 0;
1935
1936 prefixp->kind = PREFIX_BDAP;
1937 prefixp->opcode
1938 = (BDAP_INDIR_OPCODE
1939 + (prefixp->base_reg_number << 12)
1940 + index_reg_number);
1941
1942 /* We've seen "[rx+[ry", so check if this is
1943 autoincrement. */
1944 if (**cPP == '+')
1945 {
1946 /* Yep, now at "[rx+[ry+". */
1947 (*cPP)++;
1948 prefixp->opcode |= AUTOINCR_BIT << 8;
1949 }
1950 /* If it wasn't autoincrement, we don't need to
1951 add anything. */
1952
1953 /* Check a first closing ']': "[rx+[ry]" or
1954 "[rx+[ry+]". */
1955 if (**cPP != ']')
1956 return 0;
1957 (*cPP)++;
1958
1959 /* Now expect a size modifier ".S". */
1960 if (! get_bwd_size_modifier (cPP, &size_bits))
1961 return 0;
1962
1963 prefixp->opcode |= size_bits << 4;
1964
1965 /* Ok, all interesting stuff has been seen:
1966 "[rx+[ry+].S" or "[rx+[ry].S". We only need to
1967 expect a final ']', which we'll do in a common
1968 closing session. */
1969 }
1970 /* Seen "[rN+", but not a '[', so check if we have a
1971 register. */
1972 else if (get_gen_reg (cPP, &index_reg_number))
1973 {
1974 /* This is indexed mode: "[rN+rM.S]" or
1975 "[rN+rM.S+]". */
1976 int size_bits;
1977 prefixp->kind = PREFIX_BIAP;
1978 prefixp->opcode
1979 = (BIAP_OPCODE
1980 | prefixp->base_reg_number /* << 0 */
1981 | (index_reg_number << 12));
1982
1983 /* */
1984 if (! get_bwd_size_modifier (cPP, &size_bits))
1985 /* Missing size, so fail. */
1986 return 0;
1987 else
1988 /* Size found. Add that piece and drop down to
1989 the common checking of the closing ']'. */
1990 prefixp->opcode |= size_bits << 4;
1991 }
1992 /* Seen "[rN+", but not a '[' or a register, so then
1993 it must be a constant "I". */
1994 else if (cris_get_expression (cPP, &prefixp->expr))
1995 {
1996 /* Expression found, so fill in the bits of offset
1997 mode and drop down to check the closing ']'. */
1998 prefixp->kind = PREFIX_BDAP_IMM;
1999 }
2000 else
2001 /* Nothing valid here: lose. */
2002 return 0;
2003 }
2004 /* Seen "[rN" but no '+', so check if it's a '-'. */
2005 else if (**cPP == '-')
2006 {
2007 /* Yep, we must have offset mode. */
2008 if (! cris_get_expression (cPP, &prefixp->expr))
2009 /* No expression, so we lose. */
2010 return 0;
2011 else
2012 {
2013 /* Expression found to make this offset mode, so
2014 fill those bits and drop down to check the
2015 closing ']'. */
2016 prefixp->kind = PREFIX_BDAP_IMM;
2017 }
2018 }
2019 else
2020 {
2021 /* We've seen "[rN", but not '+' or '-'; rather a ']'.
2022 Hmm. Normally this is a simple indirect mode that we
2023 shouldn't match, but if we expect ']', then we have a
2024 zero offset, so it can be a three-address-operand,
2025 like "[rN],rO,rP", thus offset mode.
2026
2027 Don't eat the ']', that will be done in the closing
2028 ceremony. */
2029 prefixp->expr.X_op = O_constant;
2030 prefixp->expr.X_add_number = 0;
2031 prefixp->expr.X_add_symbol = NULL;
2032 prefixp->expr.X_op_symbol = NULL;
2033 prefixp->kind = PREFIX_BDAP_IMM;
2034 }
2035 }
2036 /* A '[', but no second '[', and no register. Check if we
2037 have an expression, making this "[I]" for a double-indirect
2038 prefix. */
2039 else if (cris_get_expression (cPP, &prefixp->expr))
2040 {
2041 /* Expression found, the so called absolute mode for a
2042 double-indirect prefix on PC. */
2043 prefixp->kind = PREFIX_DIP;
2044 prefixp->opcode
2045 = DIP_OPCODE | (AUTOINCR_BIT << 8) | REG_PC;
2046 prefixp->reloc = BFD_RELOC_32;
2047 }
2048 else
2049 /* Neither '[' nor register nor expression. We lose. */
2050 return 0;
2051
2052 /* We get here as a closing ceremony to a successful match. We just
2053 need to check the closing ']'. */
2054 if (**cPP != ']')
2055 /* Oops. Close but no air-polluter. */
2056 return 0;
2057
2058 /* Don't forget to consume that ']', before returning in glory. */
2059 (*cPP)++;
2060 return 1;
2061 }
2062
2063
2064 /* Get an expression from the string pointed out by *cPP.
2065 The pointer *cPP is advanced to the character following the expression
2066 on a success, or retains its original value otherwise.
2067
2068 cPP Pointer to pointer to string beginning with the expression.
2069
2070 exprP Pointer to structure containing the expression.
2071
2072 Return 1 iff a correct expression is found. */
2073
2074 static int
2075 cris_get_expression (cPP, exprP)
2076 char **cPP;
2077 expressionS *exprP;
2078 {
2079 char *saved_input_line_pointer;
2080 segT exp;
2081
2082 /* The "expression" function expects to find an expression at the
2083 global variable input_line_pointer, so we have to save it to give
2084 the impression that we don't fiddle with global variables. */
2085 saved_input_line_pointer = input_line_pointer;
2086 input_line_pointer = *cPP;
2087
2088 exp = expression (exprP);
2089 if (exprP->X_op == O_illegal || exprP->X_op == O_absent)
2090 {
2091 input_line_pointer = saved_input_line_pointer;
2092 return 0;
2093 }
2094
2095 /* Everything seems to be fine, just restore the global
2096 input_line_pointer and say we're successful. */
2097 *cPP = input_line_pointer;
2098 input_line_pointer = saved_input_line_pointer;
2099 return 1;
2100 }
2101
2102
2103 /* Get a sequence of flag characters from *spp. The pointer *cPP is
2104 advanced to the character following the expression. The flag
2105 characters are consecutive, no commas or spaces.
2106
2107 cPP Pointer to pointer to string beginning with the expression.
2108
2109 flagp Pointer to int to return the flags expression.
2110
2111 Return 1 iff a correct flags expression is found. */
2112
2113 static int
2114 get_flags (cPP, flagsp)
2115 char **cPP;
2116 int *flagsp;
2117 {
2118 for (;;)
2119 {
2120 switch (**cPP)
2121 {
2122 case 'd':
2123 case 'D':
2124 case 'm':
2125 case 'M':
2126 *flagsp |= 0x80;
2127 break;
2128
2129 case 'e':
2130 case 'E':
2131 case 'b':
2132 case 'B':
2133 *flagsp |= 0x40;
2134 break;
2135
2136 case 'i':
2137 case 'I':
2138 *flagsp |= 0x20;
2139 break;
2140
2141 case 'x':
2142 case 'X':
2143 *flagsp |= 0x10;
2144 break;
2145
2146 case 'n':
2147 case 'N':
2148 *flagsp |= 0x8;
2149 break;
2150
2151 case 'z':
2152 case 'Z':
2153 *flagsp |= 0x4;
2154 break;
2155
2156 case 'v':
2157 case 'V':
2158 *flagsp |= 0x2;
2159 break;
2160
2161 case 'c':
2162 case 'C':
2163 *flagsp |= 1;
2164 break;
2165
2166 default:
2167 /* We consider this successful if we stop at a comma or
2168 whitespace. Anything else, and we consider it a failure. */
2169 if (**cPP != ','
2170 && **cPP != 0
2171 && ! isspace (**cPP))
2172 return 0;
2173 else
2174 return 1;
2175 }
2176
2177 /* Don't forget to consume each flag character. */
2178 (*cPP)++;
2179 }
2180 }
2181
2182
2183 /* Generate code and fixes for a BDAP prefix.
2184
2185 base_regno Int containing the base register number.
2186
2187 exprP Pointer to structure containing the offset expression. */
2188
2189 static void
2190 gen_bdap (base_regno, exprP)
2191 int base_regno;
2192 expressionS *exprP;
2193 {
2194 unsigned int opcode;
2195 char *opcodep;
2196
2197 /* Put out the prefix opcode; assume quick immediate mode at first. */
2198 opcode = BDAP_QUICK_OPCODE | (base_regno << 12);
2199 opcodep = frag_more (2);
2200 md_number_to_chars (opcodep, opcode, 2);
2201
2202 if (exprP->X_op == O_constant)
2203 {
2204 /* We have an absolute expression that we know the size of right
2205 now. */
2206 long int value;
2207 int size;
2208
2209 value = exprP->X_add_number;
2210 if (value < -32768 || value > 32767)
2211 /* Outside range for a "word", make it a dword. */
2212 size = 2;
2213 else
2214 /* Assume "word" size. */
2215 size = 1;
2216
2217 /* If this is a signed-byte value, we can fit it into the prefix
2218 insn itself. */
2219 if (value >= -128 && value <= 127)
2220 opcodep[0] = value;
2221 else
2222 {
2223 /* This is a word or dword displacement, which will be put in a
2224 word or dword after the prefix. */
2225 char *p;
2226
2227 opcodep[0] = BDAP_PC_LOW + (size << 4);
2228 opcodep[1] &= 0xF0;
2229 opcodep[1] |= BDAP_INCR_HIGH;
2230 p = frag_more (1 << size);
2231 md_number_to_chars (p, value, 1 << size);
2232 }
2233 }
2234 else
2235 /* The expression is not defined yet but may become absolute. We make
2236 it a relocation to be relaxed. */
2237 frag_var (rs_machine_dependent, 4, 0,
2238 ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_UNDF),
2239 exprP->X_add_symbol, exprP->X_add_number, opcodep);
2240 }
2241
2242
2243 /* Encode a branch displacement in the range -256..254 into the form used
2244 by CRIS conditional branch instructions.
2245
2246 offset The displacement value in bytes. */
2247
2248 static int
2249 branch_disp (offset)
2250 int offset;
2251 {
2252 int disp;
2253
2254 disp = offset & 0xFE;
2255
2256 if (offset < 0)
2257 disp |= 1;
2258
2259 return disp;
2260 }
2261
2262
2263 /* Generate code and fixes for a 32-bit conditional branch instruction
2264 created by "extending" an existing 8-bit branch instruction.
2265
2266 opcodep Pointer to the word containing the original 8-bit branch
2267 instruction.
2268
2269 writep Pointer to "extension area" following the first instruction
2270 word.
2271
2272 fragP Pointer to the frag containing the instruction.
2273
2274 add_symP, Parts of the destination address expression.
2275 sub_symP,
2276 add_num. */
2277
2278 static void
2279 gen_cond_branch_32 (opcodep, writep, fragP, add_symP, sub_symP, add_num)
2280 char *opcodep;
2281 char *writep;
2282 fragS *fragP;
2283 symbolS *add_symP;
2284 symbolS *sub_symP;
2285 long int add_num;
2286 {
2287 if (warn_for_branch_expansion)
2288 {
2289 /* FIXME: Find out and change to as_warn_where. Add testcase. */
2290 as_warn (_("32-bit conditional branch generated"));
2291 }
2292
2293 /* Here, writep points to what will be opcodep + 2. First, we change
2294 the actual branch in opcodep[0] and opcodep[1], so that in the
2295 final insn, it will look like:
2296 opcodep+10: Bcc .-6
2297
2298 This means we don't have to worry about changing the opcode or
2299 messing with te delay-slot instruction. So, we move it to last in
2300 the "extended" branch, and just change the displacement. Admittedly,
2301 it's not the optimal extended construct, but we should get this
2302 rarely enough that it shouldn't matter. */
2303
2304 writep[8] = branch_disp (-2-6);
2305 writep[9] = opcodep[1];
2306
2307 /* Then, we change the branch to an unconditional branch over the
2308 extended part, to the new location of the Bcc:
2309 opcodep: BA .+10
2310 opcodep+2: NOP
2311
2312 Note that these two writes are to currently different locations,
2313 merged later. */
2314
2315 md_number_to_chars (opcodep, BA_QUICK_OPCODE + 8, 2);
2316 md_number_to_chars (writep, NOP_OPCODE, 2);
2317
2318 /* Then the extended thing, the 32-bit jump insn.
2319 opcodep+4: JUMP [PC+] */
2320
2321 md_number_to_chars (writep + 2, JUMP_PC_INCR_OPCODE, 2);
2322
2323 /* We have to fill in the actual value too.
2324 opcodep+6: .DWORD
2325 This is most probably an expression, but we can cope with an absolute
2326 value too. FIXME: Testcase needed. */
2327
2328 if (add_symP == NULL && sub_symP == NULL)
2329 /* An absolute address. */
2330 md_number_to_chars (writep + 4, add_num, 4);
2331 else
2332 {
2333 /* Not absolute, we have to make it a frag for later evaluation. */
2334 know (sub_symP == 0);
2335
2336 fix_new (fragP, writep + 4 - fragP->fr_literal, 4, add_symP,
2337 add_num, 0, BFD_RELOC_32);
2338 }
2339 }
2340
2341
2342 /* This *could* be:
2343
2344 Turn a string in input_line_pointer into a floating point constant of
2345 type type, and store the appropriate bytes in *litp. The number of
2346 LITTLENUMS emitted is stored in *sizep.
2347
2348 type A character from FLTCHARS that describes what kind of
2349 floating-point number is wanted.
2350
2351 litp A pointer to an array that the result should be stored in.
2352
2353 sizep A pointer to an integer where the size of the result is stored.
2354
2355 But we don't support floating point constants in assembly code *at all*,
2356 since it's suboptimal and just opens up bug opportunities. GCC emits
2357 the bit patterns as hex. All we could do here is to emit what GCC
2358 would have done in the first place. *Nobody* writes floating-point
2359 code as assembly code, but if they do, they should be able enough to
2360 find out the correct bit patterns and use them. */
2361
2362 char *
2363 md_atof (type, litp, sizep)
2364 char type ATTRIBUTE_UNUSED;
2365 char *litp ATTRIBUTE_UNUSED;
2366 int *sizep ATTRIBUTE_UNUSED;
2367 {
2368 /* FIXME: Is this function mentioned in the internals.texi manual? If
2369 not, add it. */
2370 return _("Bad call to md_atof () - floating point formats are not supported");
2371 }
2372
2373
2374 /* Turn a number as a fixS * into a series of bytes that represents the
2375 number on the target machine. The purpose of this procedure is the
2376 same as that of md_number_to_chars but this procedure is supposed to
2377 handle general bit field fixes and machine-dependent fixups.
2378
2379 bufp Pointer to an array where the result should be stored.
2380
2381 val The value to store.
2382
2383 n The number of bytes in "val" that should be stored.
2384
2385 fixP The fix to be applied to the bit field starting at bufp. */
2386
2387 static void
2388 cris_number_to_imm (bufp, val, n, fixP)
2389 char *bufp;
2390 long val;
2391 int n;
2392 fixS *fixP;
2393 {
2394 segT sym_seg;
2395
2396 know (n <= 4);
2397 know (fixP);
2398
2399 /* We put the relative "vma" for the other segment for inter-segment
2400 relocations in the object data to stay binary "compatible" (with an
2401 uninteresting old version) for the relocation.
2402 Maybe delete some day. */
2403 if (fixP->fx_addsy
2404 && (sym_seg = S_GET_SEGMENT (fixP->fx_addsy)) != now_seg)
2405 val += sym_seg->vma;
2406
2407 switch (fixP->fx_r_type)
2408 {
2409 /* Ditto here, we put the addend into the object code as
2410 well as the reloc addend. Keep it that way for now, to simplify
2411 regression tests on the object file contents. FIXME: Seems
2412 uninteresting now that we have a test suite. */
2413
2414 case BFD_RELOC_32:
2415 /* No use having warnings here, since most hosts have a 32-bit type
2416 for "long" (which will probably change soon, now that I wrote
2417 this). */
2418 bufp[3] = (val >> 24) & 0xFF;
2419 bufp[2] = (val >> 16) & 0xFF;
2420 bufp[1] = (val >> 8) & 0xFF;
2421 bufp[0] = val & 0xFF;
2422 break;
2423
2424 /* FIXME: The 16 and 8-bit cases should have a way to check
2425 whether a signed or unsigned (or any signedness) number is
2426 accepted.
2427 FIXME: Does the as_bad calls find the line number by themselves,
2428 or should we change them into as_bad_where? */
2429
2430 case BFD_RELOC_16:
2431 if (val > 0xffff || val < -32768)
2432 as_bad (_("Value not in 16 bit range: %ld"), val);
2433 if (! fixP->fx_addsy)
2434 {
2435 bufp[1] = (val >> 8) & 0xFF;
2436 bufp[0] = val & 0xFF;
2437 }
2438 break;
2439
2440 case BFD_RELOC_8:
2441 if (val > 255 || val < -128)
2442 as_bad (_("Value not in 8 bit range: %ld"), val);
2443 if (! fixP->fx_addsy)
2444 bufp[0] = val & 0xFF;
2445 break;
2446
2447 case BFD_RELOC_CRIS_UNSIGNED_4:
2448 if (val > 15 || val < 0)
2449 as_bad (_("Value not in 4 bit unsigned range: %ld"), val);
2450 if (! fixP->fx_addsy)
2451 bufp[0] |= val & 0x0F;
2452 break;
2453
2454 case BFD_RELOC_CRIS_UNSIGNED_5:
2455 if (val > 31 || val < 0)
2456 as_bad (_("Value not in 5 bit unsigned range: %ld"), val);
2457 if (! fixP->fx_addsy)
2458 bufp[0] |= val & 0x1F;
2459 break;
2460
2461 case BFD_RELOC_CRIS_SIGNED_6:
2462 if (val > 31 || val < -32)
2463 as_bad (_("Value not in 6 bit range: %ld"), val);
2464 if (! fixP->fx_addsy)
2465 bufp[0] |= val & 0x3F;
2466 break;
2467
2468 case BFD_RELOC_CRIS_UNSIGNED_6:
2469 if (val > 63 || val < 0)
2470 as_bad (_("Value not in 6 bit unsigned range: %ld"), val);
2471 if (! fixP->fx_addsy)
2472 bufp[0] |= val & 0x3F;
2473 break;
2474
2475 case BFD_RELOC_CRIS_BDISP8:
2476 if (! fixP->fx_addsy)
2477 bufp[0] = branch_disp (val);
2478 break;
2479
2480 case BFD_RELOC_NONE:
2481 /* May actually happen automatically. For example at broken
2482 words, if the word turns out not to be broken.
2483 FIXME: When? Which testcase? */
2484 if (! fixP->fx_addsy)
2485 md_number_to_chars (bufp, val, n);
2486 break;
2487
2488 case BFD_RELOC_VTABLE_INHERIT:
2489 /* This borrowed from tc-ppc.c on a whim. */
2490 if (fixP->fx_addsy
2491 && !S_IS_DEFINED (fixP->fx_addsy)
2492 && !S_IS_WEAK (fixP->fx_addsy))
2493 S_SET_WEAK (fixP->fx_addsy);
2494 /* FALLTHROUGH. */
2495 case BFD_RELOC_VTABLE_ENTRY:
2496 /* FIXME: I'm not sure why we do this (as does other ports), but it
2497 might be that this relocation can only be finished at link time. */
2498 fixP->fx_done = 0;
2499 break;
2500
2501 default:
2502 BAD_CASE (fixP->fx_r_type);
2503 }
2504 }
2505
2506
2507 /* Processes machine-dependent command line options. Called once for
2508 each option on the command line that the machine-independent part of
2509 GAS does not understand. */
2510 int
2511 md_parse_option (arg, argp)
2512 int arg;
2513 char *argp ATTRIBUTE_UNUSED;
2514 {
2515 switch (arg)
2516 {
2517 case 'H':
2518 case 'h':
2519 md_show_usage (stdout);
2520 exit (0); /* Don't continue */
2521
2522 case 'N':
2523 warn_for_branch_expansion = 1;
2524 return 1;
2525
2526 default:
2527 return 0;
2528 }
2529 }
2530
2531 /* Round up a section size to the appropriate boundary. */
2532 valueT
2533 md_section_align (segment, size)
2534 segT segment;
2535 valueT size;
2536 {
2537 /* Round all sects to multiple of 4, except the bss section, which
2538 we'll round to word-size.
2539
2540 FIXME: Check if this really matters. All sections should be
2541 rounded up, and all sections should (optionally) be assumed to be
2542 dword-aligned, it's just that there is actual usage of linking to a
2543 multiple of two. */
2544 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
2545 {
2546 if (segment == bss_section)
2547 return (size + 1) & ~1;
2548 return (size + 3) & ~3;
2549 }
2550 else
2551 {
2552 /* FIXME: Is this wanted? It matches the testsuite, but that's not
2553 really a valid reason. */
2554 if (segment == text_section)
2555 return (size + 3) & ~3;
2556 }
2557
2558 return size;
2559 }
2560
2561
2562 /* Generate a machine-dependent relocation. */
2563 arelent *
2564 tc_gen_reloc (section, fixP)
2565 asection *section ATTRIBUTE_UNUSED;
2566 fixS *fixP;
2567 {
2568 arelent *relP;
2569 bfd_reloc_code_real_type code;
2570
2571 switch (fixP->fx_r_type)
2572 {
2573 case BFD_RELOC_32:
2574 case BFD_RELOC_16:
2575 case BFD_RELOC_8:
2576 case BFD_RELOC_VTABLE_INHERIT:
2577 case BFD_RELOC_VTABLE_ENTRY:
2578 code = fixP->fx_r_type;
2579 break;
2580 default:
2581 as_bad_where (fixP->fx_file, fixP->fx_line,
2582 _("Semantics error. This type of operand can not be relocated, it must be an assembly-time constant"));
2583 return 0;
2584 }
2585
2586 relP = (arelent *) xmalloc (sizeof (arelent));
2587 assert (relP != 0);
2588 relP->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2589 *relP->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
2590 relP->address = fixP->fx_frag->fr_address + fixP->fx_where;
2591
2592 if (fixP->fx_pcrel)
2593 /* FIXME: Is this correct? */
2594 relP->addend = fixP->fx_addnumber;
2595 else
2596 /* At least *this one* is correct. */
2597 relP->addend = fixP->fx_offset;
2598
2599 /* This is the standard place for KLUDGEs to work around bugs in
2600 bfd_install_relocation (first such note in the documentation
2601 appears with binutils-2.8).
2602
2603 That function bfd_install_relocation does the wrong thing with
2604 putting stuff into the addend of a reloc (it should stay out) for a
2605 weak symbol. The really bad thing is that it adds the
2606 "segment-relative offset" of the symbol into the reloc. In this
2607 case, the reloc should instead be relative to the symbol with no
2608 other offset than the assembly code shows; and since the symbol is
2609 weak, any local definition should be ignored until link time (or
2610 thereafter).
2611 To wit: weaksym+42 should be weaksym+42 in the reloc,
2612 not weaksym+(offset_from_segment_of_local_weaksym_definition)
2613
2614 To "work around" this, we subtract the segment-relative offset of
2615 "known" weak symbols. This evens out the extra offset.
2616
2617 That happens for a.out but not for ELF, since for ELF,
2618 bfd_install_relocation uses the "special function" field of the
2619 howto, and does not execute the code that needs to be undone. */
2620
2621 if (OUTPUT_FLAVOR == bfd_target_aout_flavour
2622 && fixP->fx_addsy && S_IS_WEAK (fixP->fx_addsy)
2623 && ! bfd_is_und_section (S_GET_SEGMENT (fixP->fx_addsy)))
2624 {
2625 relP->addend -= S_GET_VALUE (fixP->fx_addsy);
2626 }
2627
2628 relP->howto = bfd_reloc_type_lookup (stdoutput, code);
2629 if (! relP->howto)
2630 {
2631 const char *name;
2632
2633 name = S_GET_NAME (fixP->fx_addsy);
2634 if (name == NULL)
2635 name = _("<unknown>");
2636 as_fatal (_("Cannot generate relocation type for symbol %s, code %s"),
2637 name, bfd_get_reloc_code_name (code));
2638 }
2639
2640 return relP;
2641 }
2642
2643
2644 /* Machine-dependent usage-output. */
2645 void
2646 md_show_usage (stream)
2647 FILE *stream;
2648 {
2649 fprintf (stream, _("\n-- GNU as for CRIS\n"));
2650 fprintf (stream, "\n");
2651 fprintf (stream,
2652 _("*** Usage: as-cris [switches] [-o objectfile] [files...]\n"));
2653 fprintf (stream, _("Target-specific switches:\n"));
2654 fprintf (stream, _(" -h, -H : Don't execute, print this help text.\n"));
2655 fprintf (stream,
2656 _(" -N : Warn when branches are expanded to jumps.\n\n"));
2657 fprintf (stream, _("Use as-cris --help to see more options.\n"));
2658 fprintf (stream, _("objectfile : Output file in the a.out format %s"),
2659 _("described in the users manual.\n"));
2660 fprintf (stream, _(" Default output file is \"a.out\".\n"));
2661 fprintf (stream, _("files ... : Input files in the source format %s"),
2662 _(" described in the users manual.\n"));
2663 fprintf (stream, _(" Default input file is standard input.\n"));
2664 fprintf (stream, _("Description : Assembler for the CRIS processor.\n"));
2665 }
2666
2667
2668 /* Apply a fixS (fixup of an instruction or data that we didn't have
2669 enough info to complete immediately) to the data in a frag. */
2670
2671 int
2672 md_apply_fix (fixP, valP)
2673 fixS *fixP;
2674 valueT *valP;
2675 {
2676 long val = *valP;
2677
2678 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
2679
2680 if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
2681 fixP->fx_done = 1;
2682
2683 if (fixP->fx_bit_fixP || fixP->fx_im_disp != 0)
2684 {
2685 as_bad_where (fixP->fx_file, fixP->fx_line, _("Invalid relocation"));
2686 fixP->fx_done = 1;
2687 }
2688 else
2689 {
2690 /* I took this from tc-arc.c, since we used to not support
2691 fx_subsy != NULL. I'm not totally sure it's TRT. */
2692 if (fixP->fx_subsy != (symbolS *) NULL)
2693 {
2694 if (S_GET_SEGMENT (fixP->fx_subsy) == absolute_section)
2695 val -= S_GET_VALUE (fixP->fx_subsy);
2696 else
2697 {
2698 /* We can't actually support subtracting a symbol. */
2699 as_bad_where (fixP->fx_file, fixP->fx_line,
2700 _("expression too complex"));
2701 }
2702 }
2703
2704 cris_number_to_imm (buf, val, fixP->fx_size, fixP);
2705 }
2706
2707 return 1;
2708 }
2709
2710
2711 /* All relocations are relative to the location just after the fixup;
2712 the address of the fixup plus its size. */
2713
2714 long
2715 md_pcrel_from (fixP)
2716 fixS *fixP;
2717 {
2718 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
2719
2720 /* FIXME: We get here only at the end of assembly, when X in ".-X" is
2721 still unknown. Since we don't have pc-relative relocations, this
2722 is invalid. What to do if anything for a.out, is to add
2723 pc-relative relocations everywhere including the elinux program
2724 loader. */
2725 as_bad_where (fixP->fx_file, fixP->fx_line,
2726 _("Invalid pc-relative relocation"));
2727 return fixP->fx_size + addr;
2728 }
2729
2730
2731 /* We have no need to give defaults for symbol-values. */
2732 symbolS *
2733 md_undefined_symbol (name)
2734 char *name ATTRIBUTE_UNUSED;
2735 {
2736 return 0;
2737 }
2738
2739
2740 /* Definition of TC_FORCE_RELOCATION.
2741 FIXME: Unsure of this. Can we omit it? Just copied from tc-i386.c
2742 when doing multi-object format with ELF, since it's the only other
2743 multi-object-format target with a.out and ELF. */
2744 int
2745 md_cris_force_relocation (fixp)
2746 struct fix *fixp;
2747 {
2748 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2749 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2750 return 1;
2751 return 0;
2752 }
2753
2754 /* Check and emit error if broken-word handling has failed to fix up a
2755 case-table. This is called from write.c, after doing everything it
2756 knows about how to handle broken words. */
2757
2758 void
2759 tc_cris_check_adjusted_broken_word (new_offset, brokwP)
2760 offsetT new_offset;
2761 struct broken_word *brokwP;
2762 {
2763 if (new_offset > 32767 || new_offset < -32768)
2764 /* We really want a genuine error, not a warning, so make it one. */
2765 as_bad_where (brokwP->frag->fr_file, brokwP->frag->fr_line,
2766 _("Adjusted signed .word (%ld) overflows: `switch'-statement too large."),
2767 (long) new_offset);
2768 }
2769
2770 /*
2771 * Local variables:
2772 * eval: (c-set-style "gnu")
2773 * indent-tabs-mode: t
2774 * End:
2775 */
This page took 0.089185 seconds and 5 git commands to generate.