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