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